]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gnat_ugn.texi
c++: Handle multiple aggregate overloads [PR95319].
[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 1.4.6.@*
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 , Dec 10, 2019
25
26 AdaCore
27
28 Copyright @copyright{} 2008-2020, 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 * A Note to Readers of Previous Versions of the Manual::
89 * Conventions::
90
91 Getting Started with GNAT
92
93 * Running GNAT::
94 * Running a Simple Ada Program::
95 * Running a Program with Multiple Units::
96 * Using the gnatmake Utility::
97
98 The GNAT Compilation Model
99
100 * Source Representation::
101 * Foreign Language Representation::
102 * File Naming Topics and Utilities::
103 * Configuration Pragmas::
104 * Generating Object Files::
105 * Source Dependencies::
106 * The Ada Library Information Files::
107 * Binding an Ada Program::
108 * GNAT and Libraries::
109 * Conditional Compilation::
110 * Mixed Language Programming::
111 * GNAT and Other Compilation Models::
112 * Using GNAT Files with External Tools::
113
114 Foreign Language Representation
115
116 * Latin-1::
117 * Other 8-Bit Codes::
118 * Wide_Character Encodings::
119 * Wide_Wide_Character Encodings::
120
121 File Naming Topics and Utilities
122
123 * File Naming Rules::
124 * Using Other File Names::
125 * Alternative File Naming Schemes::
126 * Handling Arbitrary File Naming Conventions with gnatname::
127 * File Name Krunching with gnatkr::
128 * Renaming Files with gnatchop::
129
130 Handling Arbitrary File Naming Conventions with gnatname
131
132 * Arbitrary File Naming Conventions::
133 * Running gnatname::
134 * Switches for gnatname::
135 * Examples of gnatname Usage::
136
137 File Name Krunching with gnatkr
138
139 * About gnatkr::
140 * Using gnatkr::
141 * Krunching Method::
142 * Examples of gnatkr Usage::
143
144 Renaming Files with gnatchop
145
146 * Handling Files with Multiple Units::
147 * Operating gnatchop in Compilation Mode::
148 * Command Line for gnatchop::
149 * Switches for gnatchop::
150 * Examples of gnatchop Usage::
151
152 Configuration Pragmas
153
154 * Handling of Configuration Pragmas::
155 * The Configuration Pragmas Files::
156
157 GNAT and Libraries
158
159 * Introduction to Libraries in GNAT::
160 * General Ada Libraries::
161 * Stand-alone Ada Libraries::
162 * Rebuilding the GNAT Run-Time Library::
163
164 General Ada Libraries
165
166 * Building a library::
167 * Installing a library::
168 * Using a library::
169
170 Stand-alone Ada Libraries
171
172 * Introduction to Stand-alone Libraries::
173 * Building a Stand-alone Library::
174 * Creating a Stand-alone Library to be used in a non-Ada context::
175 * Restrictions in Stand-alone Libraries::
176
177 Conditional Compilation
178
179 * Modeling Conditional Compilation in Ada::
180 * Preprocessing with gnatprep::
181 * Integrated Preprocessing::
182
183 Modeling Conditional Compilation in Ada
184
185 * Use of Boolean Constants::
186 * Debugging - A Special Case::
187 * Conditionalizing Declarations::
188 * Use of Alternative Implementations::
189 * Preprocessing::
190
191 Preprocessing with gnatprep
192
193 * Preprocessing Symbols::
194 * Using gnatprep::
195 * Switches for gnatprep::
196 * Form of Definitions File::
197 * Form of Input Text for gnatprep::
198
199 Mixed Language Programming
200
201 * Interfacing to C::
202 * Calling Conventions::
203 * Building Mixed Ada and C++ Programs::
204 * Generating Ada Bindings for C and C++ headers::
205 * Generating C Headers for Ada Specifications::
206
207 Building Mixed Ada and C++ Programs
208
209 * Interfacing to C++::
210 * Linking a Mixed C++ & Ada Program::
211 * A Simple Example::
212 * Interfacing with C++ constructors::
213 * Interfacing with C++ at the Class Level::
214
215 Generating Ada Bindings for C and C++ headers
216
217 * Running the Binding Generator::
218 * Generating Bindings for C++ Headers::
219 * Switches::
220
221 Generating C Headers for Ada Specifications
222
223 * Running the C Header Generator::
224
225 GNAT and Other Compilation Models
226
227 * Comparison between GNAT and C/C++ Compilation Models::
228 * Comparison between GNAT and Conventional Ada Library Models::
229
230 Using GNAT Files with External Tools
231
232 * Using Other Utility Programs with GNAT::
233 * The External Symbol Naming Scheme of GNAT::
234
235 Building Executable Programs with GNAT
236
237 * Building with gnatmake::
238 * Compiling with gcc::
239 * Compiler Switches::
240 * Linker Switches::
241 * Binding with gnatbind::
242 * Linking with gnatlink::
243 * Using the GNU make Utility::
244
245 Building with gnatmake
246
247 * Running gnatmake::
248 * Switches for gnatmake::
249 * Mode Switches for gnatmake::
250 * Notes on the Command Line::
251 * How gnatmake Works::
252 * Examples of gnatmake Usage::
253
254 Compiling with gcc
255
256 * Compiling Programs::
257 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
258 * Order of Compilation Issues::
259 * Examples::
260
261 Compiler Switches
262
263 * Alphabetical List of All Switches::
264 * Output and Error Message Control::
265 * Warning Message Control::
266 * Debugging and Assertion Control::
267 * Validity Checking::
268 * Style Checking::
269 * Run-Time Checks::
270 * Using gcc for Syntax Checking::
271 * Using gcc for Semantic Checking::
272 * Compiling Different Versions of Ada::
273 * Character Set Control::
274 * File Naming Control::
275 * Subprogram Inlining Control::
276 * Auxiliary Output Control::
277 * Debugging Control::
278 * Exception Handling Control::
279 * Units to Sources Mapping Files::
280 * Code Generation Control::
281
282 Binding with gnatbind
283
284 * Running gnatbind::
285 * Switches for gnatbind::
286 * Command-Line Access::
287 * Search Paths for gnatbind::
288 * Examples of gnatbind Usage::
289
290 Switches for gnatbind
291
292 * Consistency-Checking Modes::
293 * Binder Error Message Control::
294 * Elaboration Control::
295 * Output Control::
296 * Dynamic Allocation Control::
297 * Binding with Non-Ada Main Programs::
298 * Binding Programs with No Main Subprogram::
299
300 Linking with gnatlink
301
302 * Running gnatlink::
303 * Switches for gnatlink::
304
305 Using the GNU make Utility
306
307 * Using gnatmake in a Makefile::
308 * Automatically Creating a List of Directories::
309 * Generating the Command Line Switches::
310 * Overcoming Command Line Length Limits::
311
312 GNAT Utility Programs
313
314 * The File Cleanup Utility gnatclean::
315 * The GNAT Library Browser gnatls::
316 * The Cross-Referencing Tools gnatxref and gnatfind::
317 * The Ada to HTML Converter gnathtml::
318
319 The File Cleanup Utility gnatclean
320
321 * Running gnatclean::
322 * Switches for gnatclean::
323
324 The GNAT Library Browser gnatls
325
326 * Running gnatls::
327 * Switches for gnatls::
328 * Example of gnatls Usage::
329
330 The Cross-Referencing Tools gnatxref and gnatfind
331
332 * gnatxref Switches::
333 * gnatfind Switches::
334 * Configuration Files for gnatxref and gnatfind::
335 * Regular Expressions in gnatfind and gnatxref::
336 * Examples of gnatxref Usage::
337 * Examples of gnatfind Usage::
338
339 Examples of gnatxref Usage
340
341 * General Usage::
342 * Using gnatxref with vi::
343
344 The Ada to HTML Converter gnathtml
345
346 * Invoking gnathtml::
347 * Installing gnathtml::
348
349 GNAT and Program Execution
350
351 * Running and Debugging Ada Programs::
352 * Profiling::
353 * Improving Performance::
354 * Overflow Check Handling in GNAT::
355 * Performing Dimensionality Analysis in GNAT::
356 * Stack Related Facilities::
357 * Memory Management Issues::
358
359 Running and Debugging Ada Programs
360
361 * The GNAT Debugger GDB::
362 * Running GDB::
363 * Introduction to GDB Commands::
364 * Using Ada Expressions::
365 * Calling User-Defined Subprograms::
366 * Using the next Command in a Function::
367 * Stopping When Ada Exceptions Are Raised::
368 * Ada Tasks::
369 * Debugging Generic Units::
370 * Remote Debugging with gdbserver::
371 * GNAT Abnormal Termination or Failure to Terminate::
372 * Naming Conventions for GNAT Source Files::
373 * Getting Internal Debugging Information::
374 * Stack Traceback::
375 * Pretty-Printers for the GNAT runtime::
376
377 Stack Traceback
378
379 * Non-Symbolic Traceback::
380 * Symbolic Traceback::
381
382 Profiling
383
384 * Profiling an Ada Program with gprof::
385
386 Profiling an Ada Program with gprof
387
388 * Compilation for profiling::
389 * Program execution::
390 * Running gprof::
391 * Interpretation of profiling results::
392
393 Improving Performance
394
395 * Performance Considerations::
396 * Text_IO Suggestions::
397 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
398
399 Performance Considerations
400
401 * Controlling Run-Time Checks::
402 * Use of Restrictions::
403 * Optimization Levels::
404 * Debugging Optimized Code::
405 * Inlining of Subprograms::
406 * Floating_Point_Operations::
407 * Vectorization of loops::
408 * Other Optimization Switches::
409 * Optimization and Strict Aliasing::
410 * Aliased Variables and Optimization::
411 * Atomic Variables and Optimization::
412 * Passive Task Optimization::
413
414 Reducing Size of Executables with Unused Subprogram/Data Elimination
415
416 * About unused subprogram/data elimination::
417 * Compilation options::
418 * Example of unused subprogram/data elimination::
419
420 Overflow Check Handling in GNAT
421
422 * Background::
423 * Management of Overflows in GNAT::
424 * Specifying the Desired Mode::
425 * Default Settings::
426 * Implementation Notes::
427
428 Stack Related Facilities
429
430 * Stack Overflow Checking::
431 * Static Stack Usage Analysis::
432 * Dynamic Stack Usage Analysis::
433
434 Memory Management Issues
435
436 * Some Useful Memory Pools::
437 * The GNAT Debug Pool Facility::
438
439 Platform-Specific Information
440
441 * Run-Time Libraries::
442 * Specifying a Run-Time Library::
443 * GNU/Linux Topics::
444 * Microsoft Windows Topics::
445 * Mac OS Topics::
446
447 Run-Time Libraries
448
449 * Summary of Run-Time Configurations::
450
451 Specifying a Run-Time Library
452
453 * Choosing the Scheduling Policy::
454
455 GNU/Linux Topics
456
457 * Required Packages on GNU/Linux::
458
459 Microsoft Windows Topics
460
461 * Using GNAT on Windows::
462 * Using a network installation of GNAT::
463 * CONSOLE and WINDOWS subsystems::
464 * Temporary Files::
465 * Disabling Command Line Argument Expansion::
466 * Windows Socket Timeouts::
467 * Mixed-Language Programming on Windows::
468 * Windows Specific Add-Ons::
469
470 Mixed-Language Programming on Windows
471
472 * Windows Calling Conventions::
473 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
474 * Using DLLs with GNAT::
475 * Building DLLs with GNAT Project files::
476 * Building DLLs with GNAT::
477 * Building DLLs with gnatdll::
478 * Ada DLLs and Finalization::
479 * Creating a Spec for Ada DLLs::
480 * GNAT and Windows Resources::
481 * Using GNAT DLLs from Microsoft Visual Studio Applications::
482 * Debugging a DLL::
483 * Setting Stack Size from gnatlink::
484 * Setting Heap Size from gnatlink::
485
486 Windows Calling Conventions
487
488 * C Calling Convention::
489 * Stdcall Calling Convention::
490 * Win32 Calling Convention::
491 * DLL Calling Convention::
492
493 Using DLLs with GNAT
494
495 * Creating an Ada Spec for the DLL Services::
496 * Creating an Import Library::
497
498 Building DLLs with gnatdll
499
500 * Limitations When Using Ada DLLs from Ada::
501 * Exporting Ada Entities::
502 * Ada DLLs and Elaboration::
503
504 Creating a Spec for Ada DLLs
505
506 * Creating the Definition File::
507 * Using gnatdll::
508
509 GNAT and Windows Resources
510
511 * Building Resources::
512 * Compiling Resources::
513 * Using Resources::
514
515 Debugging a DLL
516
517 * Program and DLL Both Built with GCC/GNAT::
518 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
519
520 Windows Specific Add-Ons
521
522 * Win32Ada::
523 * wPOSIX::
524
525 Mac OS Topics
526
527 * Codesigning the Debugger::
528
529 Elaboration Order Handling in GNAT
530
531 * Elaboration Code::
532 * Elaboration Order::
533 * Checking the Elaboration Order::
534 * Controlling the Elaboration Order in Ada::
535 * Controlling the Elaboration Order in GNAT::
536 * Mixing Elaboration Models::
537 * ABE Diagnostics::
538 * SPARK Diagnostics::
539 * Elaboration Circularities::
540 * Resolving Elaboration Circularities::
541 * Elaboration-related Compiler Switches::
542 * Summary of Procedures for Elaboration Control::
543 * Inspecting the Chosen Elaboration Order::
544
545 Inline Assembler
546
547 * Basic Assembler Syntax::
548 * A Simple Example of Inline Assembler::
549 * Output Variables in Inline Assembler::
550 * Input Variables in Inline Assembler::
551 * Inlining Inline Assembler Code::
552 * Other Asm Functionality::
553
554 Other Asm Functionality
555
556 * The Clobber Parameter::
557 * The Volatile Parameter::
558
559 @end detailmenu
560 @end menu
561
562 @node About This Guide,Getting Started with GNAT,Top,Top
563 @anchor{gnat_ugn/about_this_guide about-this-guide}@anchor{2}@anchor{gnat_ugn/about_this_guide doc}@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}
564 @chapter About This Guide
565
566
567
568 This guide describes the use of GNAT,
569 a compiler and software development
570 toolset for the full Ada programming language.
571 It documents the features of the compiler and tools, and explains
572 how to use them to build Ada applications.
573
574 GNAT implements Ada 95, Ada 2005 and Ada 2012, and it may also be
575 invoked in Ada 83 compatibility mode.
576 By default, GNAT assumes Ada 2012, but you can override with a
577 compiler switch (@ref{6,,Compiling Different Versions of Ada})
578 to explicitly specify the language version.
579 Throughout this manual, references to 'Ada' without a year suffix
580 apply to all Ada 95/2005/2012 versions of the language.
581
582 @menu
583 * What This Guide Contains::
584 * What You Should Know before Reading This Guide::
585 * Related Information::
586 * A Note to Readers of Previous Versions of the Manual::
587 * Conventions::
588
589 @end menu
590
591 @node What This Guide Contains,What You Should Know before Reading This Guide,,About This Guide
592 @anchor{gnat_ugn/about_this_guide what-this-guide-contains}@anchor{7}
593 @section What This Guide Contains
594
595
596 This guide contains the following chapters:
597
598
599 @itemize *
600
601 @item
602 @ref{8,,Getting Started with GNAT} describes how to get started compiling
603 and running Ada programs with the GNAT Ada programming environment.
604
605 @item
606 @ref{9,,The GNAT Compilation Model} describes the compilation model used
607 by GNAT.
608
609 @item
610 @ref{a,,Building Executable Programs with GNAT} describes how to use the
611 main GNAT tools to build executable programs, and it also gives examples of
612 using the GNU make utility with GNAT.
613
614 @item
615 @ref{b,,GNAT Utility Programs} explains the various utility programs that
616 are included in the GNAT environment
617
618 @item
619 @ref{c,,GNAT and Program Execution} covers a number of topics related to
620 running, debugging, and tuning the performace of programs developed
621 with GNAT
622 @end itemize
623
624 Appendices cover several additional topics:
625
626
627 @itemize *
628
629 @item
630 @ref{d,,Platform-Specific Information} describes the different run-time
631 library implementations and also presents information on how to use
632 GNAT on several specific platforms
633
634 @item
635 @ref{e,,Example of Binder Output File} shows the source code for the binder
636 output file for a sample program.
637
638 @item
639 @ref{f,,Elaboration Order Handling in GNAT} describes how GNAT helps
640 you deal with elaboration order issues.
641
642 @item
643 @ref{10,,Inline Assembler} shows how to use the inline assembly facility
644 in an Ada program.
645 @end itemize
646
647 @node What You Should Know before Reading This Guide,Related Information,What This Guide Contains,About This Guide
648 @anchor{gnat_ugn/about_this_guide what-you-should-know-before-reading-this-guide}@anchor{11}
649 @section What You Should Know before Reading This Guide
650
651
652 @geindex Ada 95 Language Reference Manual
653
654 @geindex Ada 2005 Language Reference Manual
655
656 This guide assumes a basic familiarity with the Ada 95 language, as
657 described in the International Standard ANSI/ISO/IEC-8652:1995, January
658 1995.
659 It does not require knowledge of the features introduced by Ada 2005
660 or Ada 2012.
661 Reference manuals for Ada 95, Ada 2005, and Ada 2012 are included in
662 the GNAT documentation package.
663
664 @node Related Information,A Note to Readers of Previous Versions of the Manual,What You Should Know before Reading This Guide,About This Guide
665 @anchor{gnat_ugn/about_this_guide related-information}@anchor{12}
666 @section Related Information
667
668
669 For further information about Ada and related tools, please refer to the
670 following documents:
671
672
673 @itemize *
674
675 @item
676 @cite{Ada 95 Reference Manual}, @cite{Ada 2005 Reference Manual}, and
677 @cite{Ada 2012 Reference Manual}, which contain reference
678 material for the several revisions of the Ada language standard.
679
680 @item
681 @cite{GNAT Reference_Manual}, which contains all reference material for the GNAT
682 implementation of Ada.
683
684 @item
685 @cite{Using GNAT Studio}, which describes the GNAT Studio
686 Integrated Development Environment.
687
688 @item
689 @cite{GNAT Studio Tutorial}, which introduces the
690 main GNAT Studio features through examples.
691
692 @item
693 @cite{Debugging with GDB},
694 for all details on the use of the GNU source-level debugger.
695
696 @item
697 @cite{GNU Emacs Manual},
698 for full information on the extensible editor and programming
699 environment Emacs.
700 @end itemize
701
702 @node A Note to Readers of Previous Versions of the Manual,Conventions,Related Information,About This Guide
703 @anchor{gnat_ugn/about_this_guide a-note-to-readers-of-previous-versions-of-the-manual}@anchor{13}
704 @section A Note to Readers of Previous Versions of the Manual
705
706
707 In early 2015 the GNAT manuals were transitioned to the
708 reStructuredText (rst) / Sphinx documentation generator technology.
709 During that process the @cite{GNAT User's Guide} was reorganized
710 so that related topics would be described together in the same chapter
711 or appendix. Here's a summary of the major changes realized in
712 the new document structure.
713
714
715 @itemize *
716
717 @item
718 @ref{9,,The GNAT Compilation Model} has been extended so that it now covers
719 the following material:
720
721
722 @itemize -
723
724 @item
725 The @code{gnatname}, @code{gnatkr}, and @code{gnatchop} tools
726
727 @item
728 @ref{14,,Configuration Pragmas}
729
730 @item
731 @ref{15,,GNAT and Libraries}
732
733 @item
734 @ref{16,,Conditional Compilation} including @ref{17,,Preprocessing with gnatprep}
735 and @ref{18,,Integrated Preprocessing}
736
737 @item
738 @ref{19,,Generating Ada Bindings for C and C++ headers}
739
740 @item
741 @ref{1a,,Using GNAT Files with External Tools}
742 @end itemize
743
744 @item
745 @ref{a,,Building Executable Programs with GNAT} is a new chapter consolidating
746 the following content:
747
748
749 @itemize -
750
751 @item
752 @ref{1b,,Building with gnatmake}
753
754 @item
755 @ref{1c,,Compiling with gcc}
756
757 @item
758 @ref{1d,,Binding with gnatbind}
759
760 @item
761 @ref{1e,,Linking with gnatlink}
762
763 @item
764 @ref{1f,,Using the GNU make Utility}
765 @end itemize
766
767 @item
768 @ref{b,,GNAT Utility Programs} is a new chapter consolidating the information about several
769 GNAT tools:
770
771
772
773 @itemize -
774
775 @item
776 @ref{20,,The File Cleanup Utility gnatclean}
777
778 @item
779 @ref{21,,The GNAT Library Browser gnatls}
780
781 @item
782 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
783
784 @item
785 @ref{23,,The Ada to HTML Converter gnathtml}
786 @end itemize
787
788 @item
789 @ref{c,,GNAT and Program Execution} is a new chapter consolidating the following:
790
791
792 @itemize -
793
794 @item
795 @ref{24,,Running and Debugging Ada Programs}
796
797 @item
798 @ref{25,,Profiling}
799
800 @item
801 @ref{26,,Improving Performance}
802
803 @item
804 @ref{27,,Overflow Check Handling in GNAT}
805
806 @item
807 @ref{28,,Performing Dimensionality Analysis in GNAT}
808
809 @item
810 @ref{29,,Stack Related Facilities}
811
812 @item
813 @ref{2a,,Memory Management Issues}
814 @end itemize
815
816 @item
817 @ref{d,,Platform-Specific Information} is a new appendix consolidating the following:
818
819
820 @itemize -
821
822 @item
823 @ref{2b,,Run-Time Libraries}
824
825 @item
826 @ref{2c,,Microsoft Windows Topics}
827
828 @item
829 @ref{2d,,Mac OS Topics}
830 @end itemize
831
832 @item
833 The @emph{Compatibility and Porting Guide} appendix has been moved to the
834 @cite{GNAT Reference Manual}. It now includes a section
835 @emph{Writing Portable Fixed-Point Declarations} which was previously
836 a separate chapter in the @cite{GNAT User's Guide}.
837 @end itemize
838
839 @node Conventions,,A Note to Readers of Previous Versions of the Manual,About This Guide
840 @anchor{gnat_ugn/about_this_guide conventions}@anchor{2e}
841 @section Conventions
842
843
844 @geindex Conventions
845 @geindex typographical
846
847 @geindex Typographical conventions
848
849 Following are examples of the typographical and graphic conventions used
850 in this guide:
851
852
853 @itemize *
854
855 @item
856 @code{Functions}, @code{utility program names}, @code{standard names},
857 and @code{classes}.
858
859 @item
860 @code{Option flags}
861
862 @item
863 @code{File names}
864
865 @item
866 @code{Variables}
867
868 @item
869 @emph{Emphasis}
870
871 @item
872 [optional information or parameters]
873
874 @item
875 Examples are described by text
876
877 @example
878 and then shown this way.
879 @end example
880
881 @item
882 Commands that are entered by the user are shown as preceded by a prompt string
883 comprising the @code{$} character followed by a space.
884
885 @item
886 Full file names are shown with the '/' character
887 as the directory separator; e.g., @code{parent-dir/subdir/myfile.adb}.
888 If you are using GNAT on a Windows platform, please note that
889 the '\' character should be used instead.
890 @end itemize
891
892 @node Getting Started with GNAT,The GNAT Compilation Model,About This Guide,Top
893 @anchor{gnat_ugn/getting_started_with_gnat getting-started-with-gnat}@anchor{8}@anchor{gnat_ugn/getting_started_with_gnat doc}@anchor{2f}@anchor{gnat_ugn/getting_started_with_gnat id1}@anchor{30}
894 @chapter Getting Started with GNAT
895
896
897 This chapter describes how to use GNAT's command line interface to build
898 executable Ada programs.
899 On most platforms a visually oriented Integrated Development Environment
900 is also available, the GNAT Programming Studio (GNAT Studio).
901 GNAT Studio offers a graphical "look and feel", support for development in
902 other programming languages, comprehensive browsing features, and
903 many other capabilities.
904 For information on GNAT Studio please refer to
905 @cite{Using the GNAT Programming Studio}.
906
907 @menu
908 * Running GNAT::
909 * Running a Simple Ada Program::
910 * Running a Program with Multiple Units::
911 * Using the gnatmake Utility::
912
913 @end menu
914
915 @node Running GNAT,Running a Simple Ada Program,,Getting Started with GNAT
916 @anchor{gnat_ugn/getting_started_with_gnat running-gnat}@anchor{31}@anchor{gnat_ugn/getting_started_with_gnat id2}@anchor{32}
917 @section Running GNAT
918
919
920 Three steps are needed to create an executable file from an Ada source
921 file:
922
923
924 @itemize *
925
926 @item
927 The source file(s) must be compiled.
928
929 @item
930 The file(s) must be bound using the GNAT binder.
931
932 @item
933 All appropriate object files must be linked to produce an executable.
934 @end itemize
935
936 All three steps are most commonly handled by using the @code{gnatmake}
937 utility program that, given the name of the main program, automatically
938 performs the necessary compilation, binding and linking steps.
939
940 @node Running a Simple Ada Program,Running a Program with Multiple Units,Running GNAT,Getting Started with GNAT
941 @anchor{gnat_ugn/getting_started_with_gnat running-a-simple-ada-program}@anchor{33}@anchor{gnat_ugn/getting_started_with_gnat id3}@anchor{34}
942 @section Running a Simple Ada Program
943
944
945 Any text editor may be used to prepare an Ada program.
946 (If Emacs is used, the optional Ada mode may be helpful in laying out the
947 program.)
948 The program text is a normal text file. We will assume in our initial
949 example that you have used your editor to prepare the following
950 standard format text file:
951
952 @example
953 with Ada.Text_IO; use Ada.Text_IO;
954 procedure Hello is
955 begin
956 Put_Line ("Hello WORLD!");
957 end Hello;
958 @end example
959
960 This file should be named @code{hello.adb}.
961 With the normal default file naming conventions, GNAT requires
962 that each file
963 contain a single compilation unit whose file name is the
964 unit name,
965 with periods replaced by hyphens; the
966 extension is @code{ads} for a
967 spec and @code{adb} for a body.
968 You can override this default file naming convention by use of the
969 special pragma @code{Source_File_Name} (for further information please
970 see @ref{35,,Using Other File Names}).
971 Alternatively, if you want to rename your files according to this default
972 convention, which is probably more convenient if you will be using GNAT
973 for all your compilations, then the @code{gnatchop} utility
974 can be used to generate correctly-named source files
975 (see @ref{36,,Renaming Files with gnatchop}).
976
977 You can compile the program using the following command (@code{$} is used
978 as the command prompt in the examples in this document):
979
980 @example
981 $ gcc -c hello.adb
982 @end example
983
984 @code{gcc} is the command used to run the compiler. This compiler is
985 capable of compiling programs in several languages, including Ada and
986 C. It assumes that you have given it an Ada program if the file extension is
987 either @code{.ads} or @code{.adb}, and it will then call
988 the GNAT compiler to compile the specified file.
989
990 The @code{-c} switch is required. It tells @code{gcc} to only do a
991 compilation. (For C programs, @code{gcc} can also do linking, but this
992 capability is not used directly for Ada programs, so the @code{-c}
993 switch must always be present.)
994
995 This compile command generates a file
996 @code{hello.o}, which is the object
997 file corresponding to your Ada program. It also generates
998 an 'Ada Library Information' file @code{hello.ali},
999 which contains additional information used to check
1000 that an Ada program is consistent.
1001 To build an executable file,
1002 use @code{gnatbind} to bind the program
1003 and @code{gnatlink} to link it. The
1004 argument to both @code{gnatbind} and @code{gnatlink} is the name of the
1005 @code{ALI} file, but the default extension of @code{.ali} can
1006 be omitted. This means that in the most common case, the argument
1007 is simply the name of the main program:
1008
1009 @example
1010 $ gnatbind hello
1011 $ gnatlink hello
1012 @end example
1013
1014 A simpler method of carrying out these steps is to use @code{gnatmake},
1015 a master program that invokes all the required
1016 compilation, binding and linking tools in the correct order. In particular,
1017 @code{gnatmake} automatically recompiles any sources that have been
1018 modified since they were last compiled, or sources that depend
1019 on such modified sources, so that 'version skew' is avoided.
1020
1021 @geindex Version skew (avoided by `@w{`}gnatmake`@w{`})
1022
1023 @example
1024 $ gnatmake hello.adb
1025 @end example
1026
1027 The result is an executable program called @code{hello}, which can be
1028 run by entering:
1029
1030 @example
1031 $ hello
1032 @end example
1033
1034 assuming that the current directory is on the search path
1035 for executable programs.
1036
1037 and, if all has gone well, you will see:
1038
1039 @example
1040 Hello WORLD!
1041 @end example
1042
1043 appear in response to this command.
1044
1045 @node Running a Program with Multiple Units,Using the gnatmake Utility,Running a Simple Ada Program,Getting Started with GNAT
1046 @anchor{gnat_ugn/getting_started_with_gnat id4}@anchor{37}@anchor{gnat_ugn/getting_started_with_gnat running-a-program-with-multiple-units}@anchor{38}
1047 @section Running a Program with Multiple Units
1048
1049
1050 Consider a slightly more complicated example that has three files: a
1051 main program, and the spec and body of a package:
1052
1053 @example
1054 package Greetings is
1055 procedure Hello;
1056 procedure Goodbye;
1057 end Greetings;
1058
1059 with Ada.Text_IO; use Ada.Text_IO;
1060 package body Greetings is
1061 procedure Hello is
1062 begin
1063 Put_Line ("Hello WORLD!");
1064 end Hello;
1065
1066 procedure Goodbye is
1067 begin
1068 Put_Line ("Goodbye WORLD!");
1069 end Goodbye;
1070 end Greetings;
1071
1072 with Greetings;
1073 procedure Gmain is
1074 begin
1075 Greetings.Hello;
1076 Greetings.Goodbye;
1077 end Gmain;
1078 @end example
1079
1080 Following the one-unit-per-file rule, place this program in the
1081 following three separate files:
1082
1083
1084 @table @asis
1085
1086 @item @emph{greetings.ads}
1087
1088 spec of package @code{Greetings}
1089
1090 @item @emph{greetings.adb}
1091
1092 body of package @code{Greetings}
1093
1094 @item @emph{gmain.adb}
1095
1096 body of main program
1097 @end table
1098
1099 To build an executable version of
1100 this program, we could use four separate steps to compile, bind, and link
1101 the program, as follows:
1102
1103 @example
1104 $ gcc -c gmain.adb
1105 $ gcc -c greetings.adb
1106 $ gnatbind gmain
1107 $ gnatlink gmain
1108 @end example
1109
1110 Note that there is no required order of compilation when using GNAT.
1111 In particular it is perfectly fine to compile the main program first.
1112 Also, it is not necessary to compile package specs in the case where
1113 there is an accompanying body; you only need to compile the body. If you want
1114 to submit these files to the compiler for semantic checking and not code
1115 generation, then use the @code{-gnatc} switch:
1116
1117 @example
1118 $ gcc -c greetings.ads -gnatc
1119 @end example
1120
1121 Although the compilation can be done in separate steps as in the
1122 above example, in practice it is almost always more convenient
1123 to use the @code{gnatmake} tool. All you need to know in this case
1124 is the name of the main program's source file. The effect of the above four
1125 commands can be achieved with a single one:
1126
1127 @example
1128 $ gnatmake gmain.adb
1129 @end example
1130
1131 In the next section we discuss the advantages of using @code{gnatmake} in
1132 more detail.
1133
1134 @node Using the gnatmake Utility,,Running a Program with Multiple Units,Getting Started with GNAT
1135 @anchor{gnat_ugn/getting_started_with_gnat using-the-gnatmake-utility}@anchor{39}@anchor{gnat_ugn/getting_started_with_gnat id5}@anchor{3a}
1136 @section Using the @code{gnatmake} Utility
1137
1138
1139 If you work on a program by compiling single components at a time using
1140 @code{gcc}, you typically keep track of the units you modify. In order to
1141 build a consistent system, you compile not only these units, but also any
1142 units that depend on the units you have modified.
1143 For example, in the preceding case,
1144 if you edit @code{gmain.adb}, you only need to recompile that file. But if
1145 you edit @code{greetings.ads}, you must recompile both
1146 @code{greetings.adb} and @code{gmain.adb}, because both files contain
1147 units that depend on @code{greetings.ads}.
1148
1149 @code{gnatbind} will warn you if you forget one of these compilation
1150 steps, so that it is impossible to generate an inconsistent program as a
1151 result of forgetting to do a compilation. Nevertheless it is tedious and
1152 error-prone to keep track of dependencies among units.
1153 One approach to handle the dependency-bookkeeping is to use a
1154 makefile. However, makefiles present maintenance problems of their own:
1155 if the dependencies change as you change the program, you must make
1156 sure that the makefile is kept up-to-date manually, which is also an
1157 error-prone process.
1158
1159 The @code{gnatmake} utility takes care of these details automatically.
1160 Invoke it using either one of the following forms:
1161
1162 @example
1163 $ gnatmake gmain.adb
1164 $ gnatmake gmain
1165 @end example
1166
1167 The argument is the name of the file containing the main program;
1168 you may omit the extension. @code{gnatmake}
1169 examines the environment, automatically recompiles any files that need
1170 recompiling, and binds and links the resulting set of object files,
1171 generating the executable file, @code{gmain}.
1172 In a large program, it
1173 can be extremely helpful to use @code{gnatmake}, because working out by hand
1174 what needs to be recompiled can be difficult.
1175
1176 Note that @code{gnatmake} takes into account all the Ada rules that
1177 establish dependencies among units. These include dependencies that result
1178 from inlining subprogram bodies, and from
1179 generic instantiation. Unlike some other
1180 Ada make tools, @code{gnatmake} does not rely on the dependencies that were
1181 found by the compiler on a previous compilation, which may possibly
1182 be wrong when sources change. @code{gnatmake} determines the exact set of
1183 dependencies from scratch each time it is run.
1184
1185 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
1186
1187 @node The GNAT Compilation Model,Building Executable Programs with GNAT,Getting Started with GNAT,Top
1188 @anchor{gnat_ugn/the_gnat_compilation_model doc}@anchor{3b}@anchor{gnat_ugn/the_gnat_compilation_model the-gnat-compilation-model}@anchor{9}@anchor{gnat_ugn/the_gnat_compilation_model id1}@anchor{3c}
1189 @chapter The GNAT Compilation Model
1190
1191
1192 @geindex GNAT compilation model
1193
1194 @geindex Compilation model
1195
1196 This chapter describes the compilation model used by GNAT. Although
1197 similar to that used by other languages such as C and C++, this model
1198 is substantially different from the traditional Ada compilation models,
1199 which are based on a centralized program library. The chapter covers
1200 the following material:
1201
1202
1203 @itemize *
1204
1205 @item
1206 Topics related to source file makeup and naming
1207
1208
1209 @itemize *
1210
1211 @item
1212 @ref{3d,,Source Representation}
1213
1214 @item
1215 @ref{3e,,Foreign Language Representation}
1216
1217 @item
1218 @ref{3f,,File Naming Topics and Utilities}
1219 @end itemize
1220
1221 @item
1222 @ref{14,,Configuration Pragmas}
1223
1224 @item
1225 @ref{40,,Generating Object Files}
1226
1227 @item
1228 @ref{41,,Source Dependencies}
1229
1230 @item
1231 @ref{42,,The Ada Library Information Files}
1232
1233 @item
1234 @ref{43,,Binding an Ada Program}
1235
1236 @item
1237 @ref{15,,GNAT and Libraries}
1238
1239 @item
1240 @ref{16,,Conditional Compilation}
1241
1242 @item
1243 @ref{44,,Mixed Language Programming}
1244
1245 @item
1246 @ref{45,,GNAT and Other Compilation Models}
1247
1248 @item
1249 @ref{1a,,Using GNAT Files with External Tools}
1250 @end itemize
1251
1252 @menu
1253 * Source Representation::
1254 * Foreign Language Representation::
1255 * File Naming Topics and Utilities::
1256 * Configuration Pragmas::
1257 * Generating Object Files::
1258 * Source Dependencies::
1259 * The Ada Library Information Files::
1260 * Binding an Ada Program::
1261 * GNAT and Libraries::
1262 * Conditional Compilation::
1263 * Mixed Language Programming::
1264 * GNAT and Other Compilation Models::
1265 * Using GNAT Files with External Tools::
1266
1267 @end menu
1268
1269 @node Source Representation,Foreign Language Representation,,The GNAT Compilation Model
1270 @anchor{gnat_ugn/the_gnat_compilation_model source-representation}@anchor{3d}@anchor{gnat_ugn/the_gnat_compilation_model id2}@anchor{46}
1271 @section Source Representation
1272
1273
1274 @geindex Latin-1
1275
1276 @geindex VT
1277 @geindex HT
1278 @geindex CR
1279 @geindex LF
1280 @geindex FF
1281
1282 Ada source programs are represented in standard text files, using
1283 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1284 7-bit ASCII set, plus additional characters used for
1285 representing foreign languages (see @ref{3e,,Foreign Language Representation}
1286 for support of non-USA character sets). The format effector characters
1287 are represented using their standard ASCII encodings, as follows:
1288
1289 @quotation
1290
1291
1292 @multitable {xxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxx}
1293 @item
1294
1295 Character
1296
1297 @tab
1298
1299 Effect
1300
1301 @tab
1302
1303 Code
1304
1305 @item
1306
1307 @code{VT}
1308
1309 @tab
1310
1311 Vertical tab
1312
1313 @tab
1314
1315 @code{16#0B#}
1316
1317 @item
1318
1319 @code{HT}
1320
1321 @tab
1322
1323 Horizontal tab
1324
1325 @tab
1326
1327 @code{16#09#}
1328
1329 @item
1330
1331 @code{CR}
1332
1333 @tab
1334
1335 Carriage return
1336
1337 @tab
1338
1339 @code{16#0D#}
1340
1341 @item
1342
1343 @code{LF}
1344
1345 @tab
1346
1347 Line feed
1348
1349 @tab
1350
1351 @code{16#0A#}
1352
1353 @item
1354
1355 @code{FF}
1356
1357 @tab
1358
1359 Form feed
1360
1361 @tab
1362
1363 @code{16#0C#}
1364
1365 @end multitable
1366
1367 @end quotation
1368
1369 Source files are in standard text file format. In addition, GNAT will
1370 recognize a wide variety of stream formats, in which the end of
1371 physical lines is marked by any of the following sequences:
1372 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1373 in accommodating files that are imported from other operating systems.
1374
1375 @geindex End of source file; Source file@comma{} end
1376
1377 @geindex SUB (control character)
1378
1379 The end of a source file is normally represented by the physical end of
1380 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1381 recognized as signalling the end of the source file. Again, this is
1382 provided for compatibility with other operating systems where this
1383 code is used to represent the end of file.
1384
1385 @geindex spec (definition)
1386 @geindex compilation (definition)
1387
1388 Each file contains a single Ada compilation unit, including any pragmas
1389 associated with the unit. For example, this means you must place a
1390 package declaration (a package @emph{spec}) and the corresponding body in
1391 separate files. An Ada @emph{compilation} (which is a sequence of
1392 compilation units) is represented using a sequence of files. Similarly,
1393 you will place each subunit or child unit in a separate file.
1394
1395 @node Foreign Language Representation,File Naming Topics and Utilities,Source Representation,The GNAT Compilation Model
1396 @anchor{gnat_ugn/the_gnat_compilation_model foreign-language-representation}@anchor{3e}@anchor{gnat_ugn/the_gnat_compilation_model id3}@anchor{47}
1397 @section Foreign Language Representation
1398
1399
1400 GNAT supports the standard character sets defined in Ada as well as
1401 several other non-standard character sets for use in localized versions
1402 of the compiler (@ref{48,,Character Set Control}).
1403
1404 @menu
1405 * Latin-1::
1406 * Other 8-Bit Codes::
1407 * Wide_Character Encodings::
1408 * Wide_Wide_Character Encodings::
1409
1410 @end menu
1411
1412 @node Latin-1,Other 8-Bit Codes,,Foreign Language Representation
1413 @anchor{gnat_ugn/the_gnat_compilation_model id4}@anchor{49}@anchor{gnat_ugn/the_gnat_compilation_model latin-1}@anchor{4a}
1414 @subsection Latin-1
1415
1416
1417 @geindex Latin-1
1418
1419 The basic character set is Latin-1. This character set is defined by ISO
1420 standard 8859, part 1. The lower half (character codes @code{16#00#}
1421 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper
1422 half is used to represent additional characters. These include extended letters
1423 used by European languages, such as French accents, the vowels with umlauts
1424 used in German, and the extra letter A-ring used in Swedish.
1425
1426 @geindex Ada.Characters.Latin_1
1427
1428 For a complete list of Latin-1 codes and their encodings, see the source
1429 file of library unit @code{Ada.Characters.Latin_1} in file
1430 @code{a-chlat1.ads}.
1431 You may use any of these extended characters freely in character or
1432 string literals. In addition, the extended characters that represent
1433 letters can be used in identifiers.
1434
1435 @node Other 8-Bit Codes,Wide_Character Encodings,Latin-1,Foreign Language Representation
1436 @anchor{gnat_ugn/the_gnat_compilation_model other-8-bit-codes}@anchor{4b}@anchor{gnat_ugn/the_gnat_compilation_model id5}@anchor{4c}
1437 @subsection Other 8-Bit Codes
1438
1439
1440 GNAT also supports several other 8-bit coding schemes:
1441
1442 @geindex Latin-2
1443
1444 @geindex ISO 8859-2
1445
1446
1447 @table @asis
1448
1449 @item @emph{ISO 8859-2 (Latin-2)}
1450
1451 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1452 equivalence.
1453 @end table
1454
1455 @geindex Latin-3
1456
1457 @geindex ISO 8859-3
1458
1459
1460 @table @asis
1461
1462 @item @emph{ISO 8859-3 (Latin-3)}
1463
1464 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1465 equivalence.
1466 @end table
1467
1468 @geindex Latin-4
1469
1470 @geindex ISO 8859-4
1471
1472
1473 @table @asis
1474
1475 @item @emph{ISO 8859-4 (Latin-4)}
1476
1477 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1478 equivalence.
1479 @end table
1480
1481 @geindex ISO 8859-5
1482
1483 @geindex Cyrillic
1484
1485
1486 @table @asis
1487
1488 @item @emph{ISO 8859-5 (Cyrillic)}
1489
1490 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1491 lowercase equivalence.
1492 @end table
1493
1494 @geindex ISO 8859-15
1495
1496 @geindex Latin-9
1497
1498
1499 @table @asis
1500
1501 @item @emph{ISO 8859-15 (Latin-9)}
1502
1503 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1504 lowercase equivalence
1505 @end table
1506
1507 @geindex code page 437 (IBM PC)
1508
1509
1510 @table @asis
1511
1512 @item @emph{IBM PC (code page 437)}
1513
1514 This code page is the normal default for PCs in the U.S. It corresponds
1515 to the original IBM PC character set. This set has some, but not all, of
1516 the extended Latin-1 letters, but these letters do not have the same
1517 encoding as Latin-1. In this mode, these letters are allowed in
1518 identifiers with uppercase and lowercase equivalence.
1519 @end table
1520
1521 @geindex code page 850 (IBM PC)
1522
1523
1524 @table @asis
1525
1526 @item @emph{IBM PC (code page 850)}
1527
1528 This code page is a modification of 437 extended to include all the
1529 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1530 mode, all these letters are allowed in identifiers with uppercase and
1531 lowercase equivalence.
1532
1533 @item @emph{Full Upper 8-bit}
1534
1535 Any character in the range 80-FF allowed in identifiers, and all are
1536 considered distinct. In other words, there are no uppercase and lowercase
1537 equivalences in this range. This is useful in conjunction with
1538 certain encoding schemes used for some foreign character sets (e.g.,
1539 the typical method of representing Chinese characters on the PC).
1540
1541 @item @emph{No Upper-Half}
1542
1543 No upper-half characters in the range 80-FF are allowed in identifiers.
1544 This gives Ada 83 compatibility for identifier names.
1545 @end table
1546
1547 For precise data on the encodings permitted, and the uppercase and lowercase
1548 equivalences that are recognized, see the file @code{csets.adb} in
1549 the GNAT compiler sources. You will need to obtain a full source release
1550 of GNAT to obtain this file.
1551
1552 @node Wide_Character Encodings,Wide_Wide_Character Encodings,Other 8-Bit Codes,Foreign Language Representation
1553 @anchor{gnat_ugn/the_gnat_compilation_model id6}@anchor{4d}@anchor{gnat_ugn/the_gnat_compilation_model wide-character-encodings}@anchor{4e}
1554 @subsection Wide_Character Encodings
1555
1556
1557 GNAT allows wide character codes to appear in character and string
1558 literals, and also optionally in identifiers, by means of the following
1559 possible encoding schemes:
1560
1561
1562 @table @asis
1563
1564 @item @emph{Hex Coding}
1565
1566 In this encoding, a wide character is represented by the following five
1567 character sequence:
1568
1569 @example
1570 ESC a b c d
1571 @end example
1572
1573 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1574 characters (using uppercase letters) of the wide character code. For
1575 example, ESC A345 is used to represent the wide character with code
1576 @code{16#A345#}.
1577 This scheme is compatible with use of the full Wide_Character set.
1578
1579 @item @emph{Upper-Half Coding}
1580
1581 @geindex Upper-Half Coding
1582
1583 The wide character with encoding @code{16#abcd#} where the upper bit is on
1584 (in other words, 'a' is in the range 8-F) is represented as two bytes,
1585 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1586 character, but is not required to be in the upper half. This method can
1587 be also used for shift-JIS or EUC, where the internal coding matches the
1588 external coding.
1589
1590 @item @emph{Shift JIS Coding}
1591
1592 @geindex Shift JIS Coding
1593
1594 A wide character is represented by a two-character sequence,
1595 @code{16#ab#} and
1596 @code{16#cd#}, with the restrictions described for upper-half encoding as
1597 described above. The internal character code is the corresponding JIS
1598 character according to the standard algorithm for Shift-JIS
1599 conversion. Only characters defined in the JIS code set table can be
1600 used with this encoding method.
1601
1602 @item @emph{EUC Coding}
1603
1604 @geindex EUC Coding
1605
1606 A wide character is represented by a two-character sequence
1607 @code{16#ab#} and
1608 @code{16#cd#}, with both characters being in the upper half. The internal
1609 character code is the corresponding JIS character according to the EUC
1610 encoding algorithm. Only characters defined in the JIS code set table
1611 can be used with this encoding method.
1612
1613 @item @emph{UTF-8 Coding}
1614
1615 A wide character is represented using
1616 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1617 10646-1/Am.2. Depending on the character value, the representation
1618 is a one, two, or three byte sequence:
1619
1620 @example
1621 16#0000#-16#007f#: 2#0xxxxxxx#
1622 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1623 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1624 @end example
1625
1626 where the @code{xxx} bits correspond to the left-padded bits of the
1627 16-bit character value. Note that all lower half ASCII characters
1628 are represented as ASCII bytes and all upper half characters and
1629 other wide characters are represented as sequences of upper-half
1630 (The full UTF-8 scheme allows for encoding 31-bit characters as
1631 6-byte sequences, and in the following section on wide wide
1632 characters, the use of these sequences is documented).
1633
1634 @item @emph{Brackets Coding}
1635
1636 In this encoding, a wide character is represented by the following eight
1637 character sequence:
1638
1639 @example
1640 [ " a b c d " ]
1641 @end example
1642
1643 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1644 characters (using uppercase letters) of the wide character code. For
1645 example, ['A345'] is used to represent the wide character with code
1646 @code{16#A345#}. It is also possible (though not required) to use the
1647 Brackets coding for upper half characters. For example, the code
1648 @code{16#A3#} can be represented as @code{['A3']}.
1649
1650 This scheme is compatible with use of the full Wide_Character set,
1651 and is also the method used for wide character encoding in some standard
1652 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1653 @end table
1654
1655 @cartouche
1656 @quotation Note
1657 Some of these coding schemes do not permit the full use of the
1658 Ada character set. For example, neither Shift JIS nor EUC allow the
1659 use of the upper half of the Latin-1 set.
1660 @end quotation
1661 @end cartouche
1662
1663 @node Wide_Wide_Character Encodings,,Wide_Character Encodings,Foreign Language Representation
1664 @anchor{gnat_ugn/the_gnat_compilation_model id7}@anchor{4f}@anchor{gnat_ugn/the_gnat_compilation_model wide-wide-character-encodings}@anchor{50}
1665 @subsection Wide_Wide_Character Encodings
1666
1667
1668 GNAT allows wide wide character codes to appear in character and string
1669 literals, and also optionally in identifiers, by means of the following
1670 possible encoding schemes:
1671
1672
1673 @table @asis
1674
1675 @item @emph{UTF-8 Coding}
1676
1677 A wide character is represented using
1678 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1679 10646-1/Am.2. Depending on the character value, the representation
1680 of character codes with values greater than 16#FFFF# is a
1681 is a four, five, or six byte sequence:
1682
1683 @example
1684 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx
1685 10xxxxxx
1686 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
1687 10xxxxxx 10xxxxxx
1688 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
1689 10xxxxxx 10xxxxxx 10xxxxxx
1690 @end example
1691
1692 where the @code{xxx} bits correspond to the left-padded bits of the
1693 32-bit character value.
1694
1695 @item @emph{Brackets Coding}
1696
1697 In this encoding, a wide wide character is represented by the following ten or
1698 twelve byte character sequence:
1699
1700 @example
1701 [ " a b c d e f " ]
1702 [ " a b c d e f g h " ]
1703 @end example
1704
1705 where @code{a-h} are the six or eight hexadecimal
1706 characters (using uppercase letters) of the wide wide character code. For
1707 example, ["1F4567"] is used to represent the wide wide character with code
1708 @code{16#001F_4567#}.
1709
1710 This scheme is compatible with use of the full Wide_Wide_Character set,
1711 and is also the method used for wide wide character encoding in some standard
1712 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1713 @end table
1714
1715 @node File Naming Topics and Utilities,Configuration Pragmas,Foreign Language Representation,The GNAT Compilation Model
1716 @anchor{gnat_ugn/the_gnat_compilation_model id8}@anchor{51}@anchor{gnat_ugn/the_gnat_compilation_model file-naming-topics-and-utilities}@anchor{3f}
1717 @section File Naming Topics and Utilities
1718
1719
1720 GNAT has a default file naming scheme and also provides the user with
1721 a high degree of control over how the names and extensions of the
1722 source files correspond to the Ada compilation units that they contain.
1723
1724 @menu
1725 * File Naming Rules::
1726 * Using Other File Names::
1727 * Alternative File Naming Schemes::
1728 * Handling Arbitrary File Naming Conventions with gnatname::
1729 * File Name Krunching with gnatkr::
1730 * Renaming Files with gnatchop::
1731
1732 @end menu
1733
1734 @node File Naming Rules,Using Other File Names,,File Naming Topics and Utilities
1735 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-rules}@anchor{52}@anchor{gnat_ugn/the_gnat_compilation_model id9}@anchor{53}
1736 @subsection File Naming Rules
1737
1738
1739 The default file name is determined by the name of the unit that the
1740 file contains. The name is formed by taking the full expanded name of
1741 the unit and replacing the separating dots with hyphens and using
1742 lowercase for all letters.
1743
1744 An exception arises if the file name generated by the above rules starts
1745 with one of the characters
1746 @code{a}, @code{g}, @code{i}, or @code{s}, and the second character is a
1747 minus. In this case, the character tilde is used in place
1748 of the minus. The reason for this special rule is to avoid clashes with
1749 the standard names for child units of the packages System, Ada,
1750 Interfaces, and GNAT, which use the prefixes
1751 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
1752 respectively.
1753
1754 The file extension is @code{.ads} for a spec and
1755 @code{.adb} for a body. The following table shows some
1756 examples of these rules.
1757
1758 @quotation
1759
1760
1761 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
1762 @item
1763
1764 Source File
1765
1766 @tab
1767
1768 Ada Compilation Unit
1769
1770 @item
1771
1772 @code{main.ads}
1773
1774 @tab
1775
1776 Main (spec)
1777
1778 @item
1779
1780 @code{main.adb}
1781
1782 @tab
1783
1784 Main (body)
1785
1786 @item
1787
1788 @code{arith_functions.ads}
1789
1790 @tab
1791
1792 Arith_Functions (package spec)
1793
1794 @item
1795
1796 @code{arith_functions.adb}
1797
1798 @tab
1799
1800 Arith_Functions (package body)
1801
1802 @item
1803
1804 @code{func-spec.ads}
1805
1806 @tab
1807
1808 Func.Spec (child package spec)
1809
1810 @item
1811
1812 @code{func-spec.adb}
1813
1814 @tab
1815
1816 Func.Spec (child package body)
1817
1818 @item
1819
1820 @code{main-sub.adb}
1821
1822 @tab
1823
1824 Sub (subunit of Main)
1825
1826 @item
1827
1828 @code{a~bad.adb}
1829
1830 @tab
1831
1832 A.Bad (child package body)
1833
1834 @end multitable
1835
1836 @end quotation
1837
1838 Following these rules can result in excessively long
1839 file names if corresponding
1840 unit names are long (for example, if child units or subunits are
1841 heavily nested). An option is available to shorten such long file names
1842 (called file name 'krunching'). This may be particularly useful when
1843 programs being developed with GNAT are to be used on operating systems
1844 with limited file name lengths. @ref{54,,Using gnatkr}.
1845
1846 Of course, no file shortening algorithm can guarantee uniqueness over
1847 all possible unit names; if file name krunching is used, it is your
1848 responsibility to ensure no name clashes occur. Alternatively you
1849 can specify the exact file names that you want used, as described
1850 in the next section. Finally, if your Ada programs are migrating from a
1851 compiler with a different naming convention, you can use the gnatchop
1852 utility to produce source files that follow the GNAT naming conventions.
1853 (For details see @ref{36,,Renaming Files with gnatchop}.)
1854
1855 Note: in the case of Windows or Mac OS operating systems, case is not
1856 significant. So for example on Windows if the canonical name is
1857 @code{main-sub.adb}, you can use the file name @code{Main-Sub.adb} instead.
1858 However, case is significant for other operating systems, so for example,
1859 if you want to use other than canonically cased file names on a Unix system,
1860 you need to follow the procedures described in the next section.
1861
1862 @node Using Other File Names,Alternative File Naming Schemes,File Naming Rules,File Naming Topics and Utilities
1863 @anchor{gnat_ugn/the_gnat_compilation_model id10}@anchor{55}@anchor{gnat_ugn/the_gnat_compilation_model using-other-file-names}@anchor{35}
1864 @subsection Using Other File Names
1865
1866
1867 @geindex File names
1868
1869 In the previous section, we have described the default rules used by
1870 GNAT to determine the file name in which a given unit resides. It is
1871 often convenient to follow these default rules, and if you follow them,
1872 the compiler knows without being explicitly told where to find all
1873 the files it needs.
1874
1875 @geindex Source_File_Name pragma
1876
1877 However, in some cases, particularly when a program is imported from
1878 another Ada compiler environment, it may be more convenient for the
1879 programmer to specify which file names contain which units. GNAT allows
1880 arbitrary file names to be used by means of the Source_File_Name pragma.
1881 The form of this pragma is as shown in the following examples:
1882
1883 @example
1884 pragma Source_File_Name (My_Utilities.Stacks,
1885 Spec_File_Name => "myutilst_a.ada");
1886 pragma Source_File_name (My_Utilities.Stacks,
1887 Body_File_Name => "myutilst.ada");
1888 @end example
1889
1890 As shown in this example, the first argument for the pragma is the unit
1891 name (in this example a child unit). The second argument has the form
1892 of a named association. The identifier
1893 indicates whether the file name is for a spec or a body;
1894 the file name itself is given by a string literal.
1895
1896 The source file name pragma is a configuration pragma, which means that
1897 normally it will be placed in the @code{gnat.adc}
1898 file used to hold configuration
1899 pragmas that apply to a complete compilation environment.
1900 For more details on how the @code{gnat.adc} file is created and used
1901 see @ref{56,,Handling of Configuration Pragmas}.
1902
1903 @geindex gnat.adc
1904
1905 GNAT allows completely arbitrary file names to be specified using the
1906 source file name pragma. However, if the file name specified has an
1907 extension other than @code{.ads} or @code{.adb} it is necessary to use
1908 a special syntax when compiling the file. The name in this case must be
1909 preceded by the special sequence @code{-x} followed by a space and the name
1910 of the language, here @code{ada}, as in:
1911
1912 @example
1913 $ gcc -c -x ada peculiar_file_name.sim
1914 @end example
1915
1916 @code{gnatmake} handles non-standard file names in the usual manner (the
1917 non-standard file name for the main program is simply used as the
1918 argument to gnatmake). Note that if the extension is also non-standard,
1919 then it must be included in the @code{gnatmake} command, it may not
1920 be omitted.
1921
1922 @node Alternative File Naming Schemes,Handling Arbitrary File Naming Conventions with gnatname,Using Other File Names,File Naming Topics and Utilities
1923 @anchor{gnat_ugn/the_gnat_compilation_model id11}@anchor{57}@anchor{gnat_ugn/the_gnat_compilation_model alternative-file-naming-schemes}@anchor{58}
1924 @subsection Alternative File Naming Schemes
1925
1926
1927 @geindex File naming schemes
1928 @geindex alternative
1929
1930 @geindex File names
1931
1932 The previous section described the use of the @code{Source_File_Name}
1933 pragma to allow arbitrary names to be assigned to individual source files.
1934 However, this approach requires one pragma for each file, and especially in
1935 large systems can result in very long @code{gnat.adc} files, and also create
1936 a maintenance problem.
1937
1938 @geindex Source_File_Name pragma
1939
1940 GNAT also provides a facility for specifying systematic file naming schemes
1941 other than the standard default naming scheme previously described. An
1942 alternative scheme for naming is specified by the use of
1943 @code{Source_File_Name} pragmas having the following format:
1944
1945 @example
1946 pragma Source_File_Name (
1947 Spec_File_Name => FILE_NAME_PATTERN
1948 [ , Casing => CASING_SPEC]
1949 [ , Dot_Replacement => STRING_LITERAL ] );
1950
1951 pragma Source_File_Name (
1952 Body_File_Name => FILE_NAME_PATTERN
1953 [ , Casing => CASING_SPEC ]
1954 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1955
1956 pragma Source_File_Name (
1957 Subunit_File_Name => FILE_NAME_PATTERN
1958 [ , Casing => CASING_SPEC ]
1959 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1960
1961 FILE_NAME_PATTERN ::= STRING_LITERAL
1962 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1963 @end example
1964
1965 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1966 It contains a single asterisk character, and the unit name is substituted
1967 systematically for this asterisk. The optional parameter
1968 @code{Casing} indicates
1969 whether the unit name is to be all upper-case letters, all lower-case letters,
1970 or mixed-case. If no
1971 @code{Casing} parameter is used, then the default is all
1972 lower-case.
1973
1974 The optional @code{Dot_Replacement} string is used to replace any periods
1975 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1976 argument is used then separating dots appear unchanged in the resulting
1977 file name.
1978 Although the above syntax indicates that the
1979 @code{Casing} argument must appear
1980 before the @code{Dot_Replacement} argument, but it
1981 is also permissible to write these arguments in the opposite order.
1982
1983 As indicated, it is possible to specify different naming schemes for
1984 bodies, specs, and subunits. Quite often the rule for subunits is the
1985 same as the rule for bodies, in which case, there is no need to give
1986 a separate @code{Subunit_File_Name} rule, and in this case the
1987 @code{Body_File_name} rule is used for subunits as well.
1988
1989 The separate rule for subunits can also be used to implement the rather
1990 unusual case of a compilation environment (e.g., a single directory) which
1991 contains a subunit and a child unit with the same unit name. Although
1992 both units cannot appear in the same partition, the Ada Reference Manual
1993 allows (but does not require) the possibility of the two units coexisting
1994 in the same environment.
1995
1996 The file name translation works in the following steps:
1997
1998
1999 @itemize *
2000
2001 @item
2002 If there is a specific @code{Source_File_Name} pragma for the given unit,
2003 then this is always used, and any general pattern rules are ignored.
2004
2005 @item
2006 If there is a pattern type @code{Source_File_Name} pragma that applies to
2007 the unit, then the resulting file name will be used if the file exists. If
2008 more than one pattern matches, the latest one will be tried first, and the
2009 first attempt resulting in a reference to a file that exists will be used.
2010
2011 @item
2012 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2013 for which the corresponding file exists, then the standard GNAT default
2014 naming rules are used.
2015 @end itemize
2016
2017 As an example of the use of this mechanism, consider a commonly used scheme
2018 in which file names are all lower case, with separating periods copied
2019 unchanged to the resulting file name, and specs end with @code{.1.ada}, and
2020 bodies end with @code{.2.ada}. GNAT will follow this scheme if the following
2021 two pragmas appear:
2022
2023 @example
2024 pragma Source_File_Name
2025 (Spec_File_Name => ".1.ada");
2026 pragma Source_File_Name
2027 (Body_File_Name => ".2.ada");
2028 @end example
2029
2030 The default GNAT scheme is actually implemented by providing the following
2031 default pragmas internally:
2032
2033 @example
2034 pragma Source_File_Name
2035 (Spec_File_Name => ".ads", Dot_Replacement => "-");
2036 pragma Source_File_Name
2037 (Body_File_Name => ".adb", Dot_Replacement => "-");
2038 @end example
2039
2040 Our final example implements a scheme typically used with one of the
2041 Ada 83 compilers, where the separator character for subunits was '__'
2042 (two underscores), specs were identified by adding @code{_.ADA}, bodies
2043 by adding @code{.ADA}, and subunits by
2044 adding @code{.SEP}. All file names were
2045 upper case. Child units were not present of course since this was an
2046 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2047 the same double underscore separator for child units.
2048
2049 @example
2050 pragma Source_File_Name
2051 (Spec_File_Name => "_.ADA",
2052 Dot_Replacement => "__",
2053 Casing = Uppercase);
2054 pragma Source_File_Name
2055 (Body_File_Name => ".ADA",
2056 Dot_Replacement => "__",
2057 Casing = Uppercase);
2058 pragma Source_File_Name
2059 (Subunit_File_Name => ".SEP",
2060 Dot_Replacement => "__",
2061 Casing = Uppercase);
2062 @end example
2063
2064 @geindex gnatname
2065
2066 @node Handling Arbitrary File Naming Conventions with gnatname,File Name Krunching with gnatkr,Alternative File Naming Schemes,File Naming Topics and Utilities
2067 @anchor{gnat_ugn/the_gnat_compilation_model handling-arbitrary-file-naming-conventions-with-gnatname}@anchor{59}@anchor{gnat_ugn/the_gnat_compilation_model id12}@anchor{5a}
2068 @subsection Handling Arbitrary File Naming Conventions with @code{gnatname}
2069
2070
2071 @geindex File Naming Conventions
2072
2073 @menu
2074 * Arbitrary File Naming Conventions::
2075 * Running gnatname::
2076 * Switches for gnatname::
2077 * Examples of gnatname Usage::
2078
2079 @end menu
2080
2081 @node Arbitrary File Naming Conventions,Running gnatname,,Handling Arbitrary File Naming Conventions with gnatname
2082 @anchor{gnat_ugn/the_gnat_compilation_model arbitrary-file-naming-conventions}@anchor{5b}@anchor{gnat_ugn/the_gnat_compilation_model id13}@anchor{5c}
2083 @subsubsection Arbitrary File Naming Conventions
2084
2085
2086 The GNAT compiler must be able to know the source file name of a compilation
2087 unit. When using the standard GNAT default file naming conventions
2088 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
2089 does not need additional information.
2090
2091 When the source file names do not follow the standard GNAT default file naming
2092 conventions, the GNAT compiler must be given additional information through
2093 a configuration pragmas file (@ref{14,,Configuration Pragmas})
2094 or a project file.
2095 When the non-standard file naming conventions are well-defined,
2096 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
2097 (@ref{58,,Alternative File Naming Schemes}) may be sufficient. However,
2098 if the file naming conventions are irregular or arbitrary, a number
2099 of pragma @code{Source_File_Name} for individual compilation units
2100 must be defined.
2101 To help maintain the correspondence between compilation unit names and
2102 source file names within the compiler,
2103 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
2104 set of files.
2105
2106 @node Running gnatname,Switches for gnatname,Arbitrary File Naming Conventions,Handling Arbitrary File Naming Conventions with gnatname
2107 @anchor{gnat_ugn/the_gnat_compilation_model running-gnatname}@anchor{5d}@anchor{gnat_ugn/the_gnat_compilation_model id14}@anchor{5e}
2108 @subsubsection Running @code{gnatname}
2109
2110
2111 The usual form of the @code{gnatname} command is:
2112
2113 @example
2114 $ gnatname [ switches ] naming_pattern [ naming_patterns ]
2115 [--and [ switches ] naming_pattern [ naming_patterns ]]
2116 @end example
2117
2118 All of the arguments are optional. If invoked without any argument,
2119 @code{gnatname} will display its usage.
2120
2121 When used with at least one naming pattern, @code{gnatname} will attempt to
2122 find all the compilation units in files that follow at least one of the
2123 naming patterns. To find these compilation units,
2124 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
2125 regular files.
2126
2127 One or several Naming Patterns may be given as arguments to @code{gnatname}.
2128 Each Naming Pattern is enclosed between double quotes (or single
2129 quotes on Windows).
2130 A Naming Pattern is a regular expression similar to the wildcard patterns
2131 used in file names by the Unix shells or the DOS prompt.
2132
2133 @code{gnatname} may be called with several sections of directories/patterns.
2134 Sections are separated by the switch @code{--and}. In each section, there must be
2135 at least one pattern. If no directory is specified in a section, the current
2136 directory (or the project directory if @code{-P} is used) is implied.
2137 The options other that the directory switches and the patterns apply globally
2138 even if they are in different sections.
2139
2140 Examples of Naming Patterns are:
2141
2142 @example
2143 "*.[12].ada"
2144 "*.ad[sb]*"
2145 "body_*" "spec_*"
2146 @end example
2147
2148 For a more complete description of the syntax of Naming Patterns,
2149 see the second kind of regular expressions described in @code{g-regexp.ads}
2150 (the 'Glob' regular expressions).
2151
2152 When invoked without the switch @code{-P}, @code{gnatname} will create a
2153 configuration pragmas file @code{gnat.adc} in the current working directory,
2154 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
2155 unit.
2156
2157 @node Switches for gnatname,Examples of gnatname Usage,Running gnatname,Handling Arbitrary File Naming Conventions with gnatname
2158 @anchor{gnat_ugn/the_gnat_compilation_model id15}@anchor{5f}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatname}@anchor{60}
2159 @subsubsection Switches for @code{gnatname}
2160
2161
2162 Switches for @code{gnatname} must precede any specified Naming Pattern.
2163
2164 You may specify any of the following switches to @code{gnatname}:
2165
2166 @geindex --version (gnatname)
2167
2168
2169 @table @asis
2170
2171 @item @code{--version}
2172
2173 Display Copyright and version, then exit disregarding all other options.
2174 @end table
2175
2176 @geindex --help (gnatname)
2177
2178
2179 @table @asis
2180
2181 @item @code{--help}
2182
2183 If @code{--version} was not used, display usage, then exit disregarding
2184 all other options.
2185
2186 @item @code{--subdirs=@emph{dir}}
2187
2188 Real object, library or exec directories are subdirectories <dir> of the
2189 specified ones.
2190
2191 @item @code{--no-backup}
2192
2193 Do not create a backup copy of an existing project file.
2194
2195 @item @code{--and}
2196
2197 Start another section of directories/patterns.
2198 @end table
2199
2200 @geindex -c (gnatname)
2201
2202
2203 @table @asis
2204
2205 @item @code{-c@emph{filename}}
2206
2207 Create a configuration pragmas file @code{filename} (instead of the default
2208 @code{gnat.adc}).
2209 There may be zero, one or more space between @code{-c} and
2210 @code{filename}.
2211 @code{filename} may include directory information. @code{filename} must be
2212 writable. There may be only one switch @code{-c}.
2213 When a switch @code{-c} is
2214 specified, no switch @code{-P} may be specified (see below).
2215 @end table
2216
2217 @geindex -d (gnatname)
2218
2219
2220 @table @asis
2221
2222 @item @code{-d@emph{dir}}
2223
2224 Look for source files in directory @code{dir}. There may be zero, one or more
2225 spaces between @code{-d} and @code{dir}.
2226 @code{dir} may end with @code{/**}, that is it may be of the form
2227 @code{root_dir/**}. In this case, the directory @code{root_dir} and all of its
2228 subdirectories, recursively, have to be searched for sources.
2229 When a switch @code{-d}
2230 is specified, the current working directory will not be searched for source
2231 files, unless it is explicitly specified with a @code{-d}
2232 or @code{-D} switch.
2233 Several switches @code{-d} may be specified.
2234 If @code{dir} is a relative path, it is relative to the directory of
2235 the configuration pragmas file specified with switch
2236 @code{-c},
2237 or to the directory of the project file specified with switch
2238 @code{-P} or,
2239 if neither switch @code{-c}
2240 nor switch @code{-P} are specified, it is relative to the
2241 current working directory. The directory
2242 specified with switch @code{-d} must exist and be readable.
2243 @end table
2244
2245 @geindex -D (gnatname)
2246
2247
2248 @table @asis
2249
2250 @item @code{-D@emph{filename}}
2251
2252 Look for source files in all directories listed in text file @code{filename}.
2253 There may be zero, one or more spaces between @code{-D}
2254 and @code{filename}.
2255 @code{filename} must be an existing, readable text file.
2256 Each nonempty line in @code{filename} must be a directory.
2257 Specifying switch @code{-D} is equivalent to specifying as many
2258 switches @code{-d} as there are nonempty lines in
2259 @code{file}.
2260
2261 @item @code{-eL}
2262
2263 Follow symbolic links when processing project files.
2264
2265 @geindex -f (gnatname)
2266
2267 @item @code{-f@emph{pattern}}
2268
2269 Foreign patterns. Using this switch, it is possible to add sources of languages
2270 other than Ada to the list of sources of a project file.
2271 It is only useful if a -P switch is used.
2272 For example,
2273
2274 @example
2275 gnatname -Pprj -f"*.c" "*.ada"
2276 @end example
2277
2278 will look for Ada units in all files with the @code{.ada} extension,
2279 and will add to the list of file for project @code{prj.gpr} the C files
2280 with extension @code{.c}.
2281
2282 @geindex -h (gnatname)
2283
2284 @item @code{-h}
2285
2286 Output usage (help) information. The output is written to @code{stdout}.
2287
2288 @geindex -P (gnatname)
2289
2290 @item @code{-P@emph{proj}}
2291
2292 Create or update project file @code{proj}. There may be zero, one or more space
2293 between @code{-P} and @code{proj}. @code{proj} may include directory
2294 information. @code{proj} must be writable.
2295 There may be only one switch @code{-P}.
2296 When a switch @code{-P} is specified,
2297 no switch @code{-c} may be specified.
2298 On all platforms, except on VMS, when @code{gnatname} is invoked for an
2299 existing project file <proj>.gpr, a backup copy of the project file is created
2300 in the project directory with file name <proj>.gpr.saved_x. 'x' is the first
2301 non negative number that makes this backup copy a new file.
2302
2303 @geindex -v (gnatname)
2304
2305 @item @code{-v}
2306
2307 Verbose mode. Output detailed explanation of behavior to @code{stdout}.
2308 This includes name of the file written, the name of the directories to search
2309 and, for each file in those directories whose name matches at least one of
2310 the Naming Patterns, an indication of whether the file contains a unit,
2311 and if so the name of the unit.
2312 @end table
2313
2314 @geindex -v -v (gnatname)
2315
2316
2317 @table @asis
2318
2319 @item @code{-v -v}
2320
2321 Very Verbose mode. In addition to the output produced in verbose mode,
2322 for each file in the searched directories whose name matches none of
2323 the Naming Patterns, an indication is given that there is no match.
2324
2325 @geindex -x (gnatname)
2326
2327 @item @code{-x@emph{pattern}}
2328
2329 Excluded patterns. Using this switch, it is possible to exclude some files
2330 that would match the name patterns. For example,
2331
2332 @example
2333 gnatname -x "*_nt.ada" "*.ada"
2334 @end example
2335
2336 will look for Ada units in all files with the @code{.ada} extension,
2337 except those whose names end with @code{_nt.ada}.
2338 @end table
2339
2340 @node Examples of gnatname Usage,,Switches for gnatname,Handling Arbitrary File Naming Conventions with gnatname
2341 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatname-usage}@anchor{61}@anchor{gnat_ugn/the_gnat_compilation_model id16}@anchor{62}
2342 @subsubsection Examples of @code{gnatname} Usage
2343
2344
2345 @example
2346 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
2347 @end example
2348
2349 In this example, the directory @code{/home/me} must already exist
2350 and be writable. In addition, the directory
2351 @code{/home/me/sources} (specified by
2352 @code{-d sources}) must exist and be readable.
2353
2354 Note the optional spaces after @code{-c} and @code{-d}.
2355
2356 @example
2357 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
2358 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
2359 @end example
2360
2361 Note that several switches @code{-d} may be used,
2362 even in conjunction with one or several switches
2363 @code{-D}. Several Naming Patterns and one excluded pattern
2364 are used in this example.
2365
2366 @node File Name Krunching with gnatkr,Renaming Files with gnatchop,Handling Arbitrary File Naming Conventions with gnatname,File Naming Topics and Utilities
2367 @anchor{gnat_ugn/the_gnat_compilation_model file-name-krunching-with-gnatkr}@anchor{63}@anchor{gnat_ugn/the_gnat_compilation_model id17}@anchor{64}
2368 @subsection File Name Krunching with @code{gnatkr}
2369
2370
2371 @geindex gnatkr
2372
2373 This section discusses the method used by the compiler to shorten
2374 the default file names chosen for Ada units so that they do not
2375 exceed the maximum length permitted. It also describes the
2376 @code{gnatkr} utility that can be used to determine the result of
2377 applying this shortening.
2378
2379 @menu
2380 * About gnatkr::
2381 * Using gnatkr::
2382 * Krunching Method::
2383 * Examples of gnatkr Usage::
2384
2385 @end menu
2386
2387 @node About gnatkr,Using gnatkr,,File Name Krunching with gnatkr
2388 @anchor{gnat_ugn/the_gnat_compilation_model id18}@anchor{65}@anchor{gnat_ugn/the_gnat_compilation_model about-gnatkr}@anchor{66}
2389 @subsubsection About @code{gnatkr}
2390
2391
2392 The default file naming rule in GNAT
2393 is that the file name must be derived from
2394 the unit name. The exact default rule is as follows:
2395
2396
2397 @itemize *
2398
2399 @item
2400 Take the unit name and replace all dots by hyphens.
2401
2402 @item
2403 If such a replacement occurs in the
2404 second character position of a name, and the first character is
2405 @code{a}, @code{g}, @code{s}, or @code{i},
2406 then replace the dot by the character
2407 @code{~} (tilde)
2408 instead of a minus.
2409
2410 The reason for this exception is to avoid clashes
2411 with the standard names for children of System, Ada, Interfaces,
2412 and GNAT, which use the prefixes
2413 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
2414 respectively.
2415 @end itemize
2416
2417 The @code{-gnatk@emph{nn}}
2418 switch of the compiler activates a 'krunching'
2419 circuit that limits file names to nn characters (where nn is a decimal
2420 integer).
2421
2422 The @code{gnatkr} utility can be used to determine the krunched name for
2423 a given file, when krunched to a specified maximum length.
2424
2425 @node Using gnatkr,Krunching Method,About gnatkr,File Name Krunching with gnatkr
2426 @anchor{gnat_ugn/the_gnat_compilation_model id19}@anchor{67}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatkr}@anchor{54}
2427 @subsubsection Using @code{gnatkr}
2428
2429
2430 The @code{gnatkr} command has the form:
2431
2432 @example
2433 $ gnatkr name [ length ]
2434 @end example
2435
2436 @code{name} is the uncrunched file name, derived from the name of the unit
2437 in the standard manner described in the previous section (i.e., in particular
2438 all dots are replaced by hyphens). The file name may or may not have an
2439 extension (defined as a suffix of the form period followed by arbitrary
2440 characters other than period). If an extension is present then it will
2441 be preserved in the output. For example, when krunching @code{hellofile.ads}
2442 to eight characters, the result will be hellofil.ads.
2443
2444 Note: for compatibility with previous versions of @code{gnatkr} dots may
2445 appear in the name instead of hyphens, but the last dot will always be
2446 taken as the start of an extension. So if @code{gnatkr} is given an argument
2447 such as @code{Hello.World.adb} it will be treated exactly as if the first
2448 period had been a hyphen, and for example krunching to eight characters
2449 gives the result @code{hellworl.adb}.
2450
2451 Note that the result is always all lower case.
2452 Characters of the other case are folded as required.
2453
2454 @code{length} represents the length of the krunched name. The default
2455 when no argument is given is 8 characters. A length of zero stands for
2456 unlimited, in other words do not chop except for system files where the
2457 implied crunching length is always eight characters.
2458
2459 The output is the krunched name. The output has an extension only if the
2460 original argument was a file name with an extension.
2461
2462 @node Krunching Method,Examples of gnatkr Usage,Using gnatkr,File Name Krunching with gnatkr
2463 @anchor{gnat_ugn/the_gnat_compilation_model id20}@anchor{68}@anchor{gnat_ugn/the_gnat_compilation_model krunching-method}@anchor{69}
2464 @subsubsection Krunching Method
2465
2466
2467 The initial file name is determined by the name of the unit that the file
2468 contains. The name is formed by taking the full expanded name of the
2469 unit and replacing the separating dots with hyphens and
2470 using lowercase
2471 for all letters, except that a hyphen in the second character position is
2472 replaced by a tilde if the first character is
2473 @code{a}, @code{i}, @code{g}, or @code{s}.
2474 The extension is @code{.ads} for a
2475 spec and @code{.adb} for a body.
2476 Krunching does not affect the extension, but the file name is shortened to
2477 the specified length by following these rules:
2478
2479
2480 @itemize *
2481
2482 @item
2483 The name is divided into segments separated by hyphens, tildes or
2484 underscores and all hyphens, tildes, and underscores are
2485 eliminated. If this leaves the name short enough, we are done.
2486
2487 @item
2488 If the name is too long, the longest segment is located (left-most
2489 if there are two of equal length), and shortened by dropping
2490 its last character. This is repeated until the name is short enough.
2491
2492 As an example, consider the krunching of @code{our-strings-wide_fixed.adb}
2493 to fit the name into 8 characters as required by some operating systems:
2494
2495 @example
2496 our-strings-wide_fixed 22
2497 our strings wide fixed 19
2498 our string wide fixed 18
2499 our strin wide fixed 17
2500 our stri wide fixed 16
2501 our stri wide fixe 15
2502 our str wide fixe 14
2503 our str wid fixe 13
2504 our str wid fix 12
2505 ou str wid fix 11
2506 ou st wid fix 10
2507 ou st wi fix 9
2508 ou st wi fi 8
2509 Final file name: oustwifi.adb
2510 @end example
2511
2512 @item
2513 The file names for all predefined units are always krunched to eight
2514 characters. The krunching of these predefined units uses the following
2515 special prefix replacements:
2516
2517
2518 @multitable {xxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx}
2519 @item
2520
2521 Prefix
2522
2523 @tab
2524
2525 Replacement
2526
2527 @item
2528
2529 @code{ada-}
2530
2531 @tab
2532
2533 @code{a-}
2534
2535 @item
2536
2537 @code{gnat-}
2538
2539 @tab
2540
2541 @code{g-}
2542
2543 @item
2544
2545 @code{interfac es-}
2546
2547 @tab
2548
2549 @code{i-}
2550
2551 @item
2552
2553 @code{system-}
2554
2555 @tab
2556
2557 @code{s-}
2558
2559 @end multitable
2560
2561
2562 These system files have a hyphen in the second character position. That
2563 is why normal user files replace such a character with a
2564 tilde, to avoid confusion with system file names.
2565
2566 As an example of this special rule, consider
2567 @code{ada-strings-wide_fixed.adb}, which gets krunched as follows:
2568
2569 @example
2570 ada-strings-wide_fixed 22
2571 a- strings wide fixed 18
2572 a- string wide fixed 17
2573 a- strin wide fixed 16
2574 a- stri wide fixed 15
2575 a- stri wide fixe 14
2576 a- str wide fixe 13
2577 a- str wid fixe 12
2578 a- str wid fix 11
2579 a- st wid fix 10
2580 a- st wi fix 9
2581 a- st wi fi 8
2582 Final file name: a-stwifi.adb
2583 @end example
2584 @end itemize
2585
2586 Of course no file shortening algorithm can guarantee uniqueness over all
2587 possible unit names, and if file name krunching is used then it is your
2588 responsibility to ensure that no name clashes occur. The utility
2589 program @code{gnatkr} is supplied for conveniently determining the
2590 krunched name of a file.
2591
2592 @node Examples of gnatkr Usage,,Krunching Method,File Name Krunching with gnatkr
2593 @anchor{gnat_ugn/the_gnat_compilation_model id21}@anchor{6a}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatkr-usage}@anchor{6b}
2594 @subsubsection Examples of @code{gnatkr} Usage
2595
2596
2597 @example
2598 $ gnatkr very_long_unit_name.ads --> velounna.ads
2599 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
2600 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
2601 $ gnatkr grandparent-parent-child --> grparchi
2602 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
2603 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
2604 @end example
2605
2606 @node Renaming Files with gnatchop,,File Name Krunching with gnatkr,File Naming Topics and Utilities
2607 @anchor{gnat_ugn/the_gnat_compilation_model id22}@anchor{6c}@anchor{gnat_ugn/the_gnat_compilation_model renaming-files-with-gnatchop}@anchor{36}
2608 @subsection Renaming Files with @code{gnatchop}
2609
2610
2611 @geindex gnatchop
2612
2613 This section discusses how to handle files with multiple units by using
2614 the @code{gnatchop} utility. This utility is also useful in renaming
2615 files to meet the standard GNAT default file naming conventions.
2616
2617 @menu
2618 * Handling Files with Multiple Units::
2619 * Operating gnatchop in Compilation Mode::
2620 * Command Line for gnatchop::
2621 * Switches for gnatchop::
2622 * Examples of gnatchop Usage::
2623
2624 @end menu
2625
2626 @node Handling Files with Multiple Units,Operating gnatchop in Compilation Mode,,Renaming Files with gnatchop
2627 @anchor{gnat_ugn/the_gnat_compilation_model id23}@anchor{6d}@anchor{gnat_ugn/the_gnat_compilation_model handling-files-with-multiple-units}@anchor{6e}
2628 @subsubsection Handling Files with Multiple Units
2629
2630
2631 The basic compilation model of GNAT requires that a file submitted to the
2632 compiler have only one unit and there be a strict correspondence
2633 between the file name and the unit name.
2634
2635 The @code{gnatchop} utility allows both of these rules to be relaxed,
2636 allowing GNAT to process files which contain multiple compilation units
2637 and files with arbitrary file names. @code{gnatchop}
2638 reads the specified file and generates one or more output files,
2639 containing one unit per file. The unit and the file name correspond,
2640 as required by GNAT.
2641
2642 If you want to permanently restructure a set of 'foreign' files so that
2643 they match the GNAT rules, and do the remaining development using the
2644 GNAT structure, you can simply use @code{gnatchop} once, generate the
2645 new set of files and work with them from that point on.
2646
2647 Alternatively, if you want to keep your files in the 'foreign' format,
2648 perhaps to maintain compatibility with some other Ada compilation
2649 system, you can set up a procedure where you use @code{gnatchop} each
2650 time you compile, regarding the source files that it writes as temporary
2651 files that you throw away.
2652
2653 Note that if your file containing multiple units starts with a byte order
2654 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
2655 will each start with a copy of this BOM, meaning that they can be compiled
2656 automatically in UTF-8 mode without needing to specify an explicit encoding.
2657
2658 @node Operating gnatchop in Compilation Mode,Command Line for gnatchop,Handling Files with Multiple Units,Renaming Files with gnatchop
2659 @anchor{gnat_ugn/the_gnat_compilation_model operating-gnatchop-in-compilation-mode}@anchor{6f}@anchor{gnat_ugn/the_gnat_compilation_model id24}@anchor{70}
2660 @subsubsection Operating gnatchop in Compilation Mode
2661
2662
2663 The basic function of @code{gnatchop} is to take a file with multiple units
2664 and split it into separate files. The boundary between files is reasonably
2665 clear, except for the issue of comments and pragmas. In default mode, the
2666 rule is that any pragmas between units belong to the previous unit, except
2667 that configuration pragmas always belong to the following unit. Any comments
2668 belong to the following unit. These rules
2669 almost always result in the right choice of
2670 the split point without needing to mark it explicitly and most users will
2671 find this default to be what they want. In this default mode it is incorrect to
2672 submit a file containing only configuration pragmas, or one that ends in
2673 configuration pragmas, to @code{gnatchop}.
2674
2675 However, using a special option to activate 'compilation mode',
2676 @code{gnatchop}
2677 can perform another function, which is to provide exactly the semantics
2678 required by the RM for handling of configuration pragmas in a compilation.
2679 In the absence of configuration pragmas (at the main file level), this
2680 option has no effect, but it causes such configuration pragmas to be handled
2681 in a quite different manner.
2682
2683 First, in compilation mode, if @code{gnatchop} is given a file that consists of
2684 only configuration pragmas, then this file is appended to the
2685 @code{gnat.adc} file in the current directory. This behavior provides
2686 the required behavior described in the RM for the actions to be taken
2687 on submitting such a file to the compiler, namely that these pragmas
2688 should apply to all subsequent compilations in the same compilation
2689 environment. Using GNAT, the current directory, possibly containing a
2690 @code{gnat.adc} file is the representation
2691 of a compilation environment. For more information on the
2692 @code{gnat.adc} file, see @ref{56,,Handling of Configuration Pragmas}.
2693
2694 Second, in compilation mode, if @code{gnatchop}
2695 is given a file that starts with
2696 configuration pragmas, and contains one or more units, then these
2697 configuration pragmas are prepended to each of the chopped files. This
2698 behavior provides the required behavior described in the RM for the
2699 actions to be taken on compiling such a file, namely that the pragmas
2700 apply to all units in the compilation, but not to subsequently compiled
2701 units.
2702
2703 Finally, if configuration pragmas appear between units, they are appended
2704 to the previous unit. This results in the previous unit being illegal,
2705 since the compiler does not accept configuration pragmas that follow
2706 a unit. This provides the required RM behavior that forbids configuration
2707 pragmas other than those preceding the first compilation unit of a
2708 compilation.
2709
2710 For most purposes, @code{gnatchop} will be used in default mode. The
2711 compilation mode described above is used only if you need exactly
2712 accurate behavior with respect to compilations, and you have files
2713 that contain multiple units and configuration pragmas. In this
2714 circumstance the use of @code{gnatchop} with the compilation mode
2715 switch provides the required behavior, and is for example the mode
2716 in which GNAT processes the ACVC tests.
2717
2718 @node Command Line for gnatchop,Switches for gnatchop,Operating gnatchop in Compilation Mode,Renaming Files with gnatchop
2719 @anchor{gnat_ugn/the_gnat_compilation_model id25}@anchor{71}@anchor{gnat_ugn/the_gnat_compilation_model command-line-for-gnatchop}@anchor{72}
2720 @subsubsection Command Line for @code{gnatchop}
2721
2722
2723 The @code{gnatchop} command has the form:
2724
2725 @example
2726 $ gnatchop switches file_name [file_name ...]
2727 [directory]
2728 @end example
2729
2730 The only required argument is the file name of the file to be chopped.
2731 There are no restrictions on the form of this file name. The file itself
2732 contains one or more Ada units, in normal GNAT format, concatenated
2733 together. As shown, more than one file may be presented to be chopped.
2734
2735 When run in default mode, @code{gnatchop} generates one output file in
2736 the current directory for each unit in each of the files.
2737
2738 @code{directory}, if specified, gives the name of the directory to which
2739 the output files will be written. If it is not specified, all files are
2740 written to the current directory.
2741
2742 For example, given a
2743 file called @code{hellofiles} containing
2744
2745 @example
2746 procedure Hello;
2747
2748 with Ada.Text_IO; use Ada.Text_IO;
2749 procedure Hello is
2750 begin
2751 Put_Line ("Hello");
2752 end Hello;
2753 @end example
2754
2755 the command
2756
2757 @example
2758 $ gnatchop hellofiles
2759 @end example
2760
2761 generates two files in the current directory, one called
2762 @code{hello.ads} containing the single line that is the procedure spec,
2763 and the other called @code{hello.adb} containing the remaining text. The
2764 original file is not affected. The generated files can be compiled in
2765 the normal manner.
2766
2767 When gnatchop is invoked on a file that is empty or that contains only empty
2768 lines and/or comments, gnatchop will not fail, but will not produce any
2769 new sources.
2770
2771 For example, given a
2772 file called @code{toto.txt} containing
2773
2774 @example
2775 -- Just a comment
2776 @end example
2777
2778 the command
2779
2780 @example
2781 $ gnatchop toto.txt
2782 @end example
2783
2784 will not produce any new file and will result in the following warnings:
2785
2786 @example
2787 toto.txt:1:01: warning: empty file, contains no compilation units
2788 no compilation units found
2789 no source files written
2790 @end example
2791
2792 @node Switches for gnatchop,Examples of gnatchop Usage,Command Line for gnatchop,Renaming Files with gnatchop
2793 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatchop}@anchor{73}@anchor{gnat_ugn/the_gnat_compilation_model id26}@anchor{74}
2794 @subsubsection Switches for @code{gnatchop}
2795
2796
2797 @code{gnatchop} recognizes the following switches:
2798
2799 @geindex --version (gnatchop)
2800
2801
2802 @table @asis
2803
2804 @item @code{--version}
2805
2806 Display Copyright and version, then exit disregarding all other options.
2807 @end table
2808
2809 @geindex --help (gnatchop)
2810
2811
2812 @table @asis
2813
2814 @item @code{--help}
2815
2816 If @code{--version} was not used, display usage, then exit disregarding
2817 all other options.
2818 @end table
2819
2820 @geindex -c (gnatchop)
2821
2822
2823 @table @asis
2824
2825 @item @code{-c}
2826
2827 Causes @code{gnatchop} to operate in compilation mode, in which
2828 configuration pragmas are handled according to strict RM rules. See
2829 previous section for a full description of this mode.
2830
2831 @item @code{-gnat@emph{xxx}}
2832
2833 This passes the given @code{-gnat@emph{xxx}} switch to @code{gnat} which is
2834 used to parse the given file. Not all @emph{xxx} options make sense,
2835 but for example, the use of @code{-gnati2} allows @code{gnatchop} to
2836 process a source file that uses Latin-2 coding for identifiers.
2837
2838 @item @code{-h}
2839
2840 Causes @code{gnatchop} to generate a brief help summary to the standard
2841 output file showing usage information.
2842 @end table
2843
2844 @geindex -k (gnatchop)
2845
2846
2847 @table @asis
2848
2849 @item @code{-k@emph{mm}}
2850
2851 Limit generated file names to the specified number @code{mm}
2852 of characters.
2853 This is useful if the
2854 resulting set of files is required to be interoperable with systems
2855 which limit the length of file names.
2856 No space is allowed between the @code{-k} and the numeric value. The numeric
2857 value may be omitted in which case a default of @code{-k8},
2858 suitable for use
2859 with DOS-like file systems, is used. If no @code{-k} switch
2860 is present then
2861 there is no limit on the length of file names.
2862 @end table
2863
2864 @geindex -p (gnatchop)
2865
2866
2867 @table @asis
2868
2869 @item @code{-p}
2870
2871 Causes the file modification time stamp of the input file to be
2872 preserved and used for the time stamp of the output file(s). This may be
2873 useful for preserving coherency of time stamps in an environment where
2874 @code{gnatchop} is used as part of a standard build process.
2875 @end table
2876
2877 @geindex -q (gnatchop)
2878
2879
2880 @table @asis
2881
2882 @item @code{-q}
2883
2884 Causes output of informational messages indicating the set of generated
2885 files to be suppressed. Warnings and error messages are unaffected.
2886 @end table
2887
2888 @geindex -r (gnatchop)
2889
2890 @geindex Source_Reference pragmas
2891
2892
2893 @table @asis
2894
2895 @item @code{-r}
2896
2897 Generate @code{Source_Reference} pragmas. Use this switch if the output
2898 files are regarded as temporary and development is to be done in terms
2899 of the original unchopped file. This switch causes
2900 @code{Source_Reference} pragmas to be inserted into each of the
2901 generated files to refers back to the original file name and line number.
2902 The result is that all error messages refer back to the original
2903 unchopped file.
2904 In addition, the debugging information placed into the object file (when
2905 the @code{-g} switch of @code{gcc} or @code{gnatmake} is
2906 specified)
2907 also refers back to this original file so that tools like profilers and
2908 debuggers will give information in terms of the original unchopped file.
2909
2910 If the original file to be chopped itself contains
2911 a @code{Source_Reference}
2912 pragma referencing a third file, then gnatchop respects
2913 this pragma, and the generated @code{Source_Reference} pragmas
2914 in the chopped file refer to the original file, with appropriate
2915 line numbers. This is particularly useful when @code{gnatchop}
2916 is used in conjunction with @code{gnatprep} to compile files that
2917 contain preprocessing statements and multiple units.
2918 @end table
2919
2920 @geindex -v (gnatchop)
2921
2922
2923 @table @asis
2924
2925 @item @code{-v}
2926
2927 Causes @code{gnatchop} to operate in verbose mode. The version
2928 number and copyright notice are output, as well as exact copies of
2929 the gnat1 commands spawned to obtain the chop control information.
2930 @end table
2931
2932 @geindex -w (gnatchop)
2933
2934
2935 @table @asis
2936
2937 @item @code{-w}
2938
2939 Overwrite existing file names. Normally @code{gnatchop} regards it as a
2940 fatal error if there is already a file with the same name as a
2941 file it would otherwise output, in other words if the files to be
2942 chopped contain duplicated units. This switch bypasses this
2943 check, and causes all but the last instance of such duplicated
2944 units to be skipped.
2945 @end table
2946
2947 @geindex --GCC= (gnatchop)
2948
2949
2950 @table @asis
2951
2952 @item @code{--GCC=@emph{xxxx}}
2953
2954 Specify the path of the GNAT parser to be used. When this switch is used,
2955 no attempt is made to add the prefix to the GNAT parser executable.
2956 @end table
2957
2958 @node Examples of gnatchop Usage,,Switches for gnatchop,Renaming Files with gnatchop
2959 @anchor{gnat_ugn/the_gnat_compilation_model id27}@anchor{75}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatchop-usage}@anchor{76}
2960 @subsubsection Examples of @code{gnatchop} Usage
2961
2962
2963 @example
2964 $ gnatchop -w hello_s.ada prerelease/files
2965 @end example
2966
2967 Chops the source file @code{hello_s.ada}. The output files will be
2968 placed in the directory @code{prerelease/files},
2969 overwriting any
2970 files with matching names in that directory (no files in the current
2971 directory are modified).
2972
2973 @example
2974 $ gnatchop archive
2975 @end example
2976
2977 Chops the source file @code{archive}
2978 into the current directory. One
2979 useful application of @code{gnatchop} is in sending sets of sources
2980 around, for example in email messages. The required sources are simply
2981 concatenated (for example, using a Unix @code{cat}
2982 command), and then
2983 @code{gnatchop} is used at the other end to reconstitute the original
2984 file names.
2985
2986 @example
2987 $ gnatchop file1 file2 file3 direc
2988 @end example
2989
2990 Chops all units in files @code{file1}, @code{file2}, @code{file3}, placing
2991 the resulting files in the directory @code{direc}. Note that if any units
2992 occur more than once anywhere within this set of files, an error message
2993 is generated, and no files are written. To override this check, use the
2994 @code{-w} switch,
2995 in which case the last occurrence in the last file will
2996 be the one that is output, and earlier duplicate occurrences for a given
2997 unit will be skipped.
2998
2999 @node Configuration Pragmas,Generating Object Files,File Naming Topics and Utilities,The GNAT Compilation Model
3000 @anchor{gnat_ugn/the_gnat_compilation_model id28}@anchor{77}@anchor{gnat_ugn/the_gnat_compilation_model configuration-pragmas}@anchor{14}
3001 @section Configuration Pragmas
3002
3003
3004 @geindex Configuration pragmas
3005
3006 @geindex Pragmas
3007 @geindex configuration
3008
3009 Configuration pragmas include those pragmas described as
3010 such in the Ada Reference Manual, as well as
3011 implementation-dependent pragmas that are configuration pragmas.
3012 See the @code{Implementation_Defined_Pragmas} chapter in the
3013 @cite{GNAT_Reference_Manual} for details on these
3014 additional GNAT-specific configuration pragmas.
3015 Most notably, the pragma @code{Source_File_Name}, which allows
3016 specifying non-default names for source files, is a configuration
3017 pragma. The following is a complete list of configuration pragmas
3018 recognized by GNAT:
3019
3020 @example
3021 Ada_83
3022 Ada_95
3023 Ada_05
3024 Ada_2005
3025 Ada_12
3026 Ada_2012
3027 Allow_Integer_Address
3028 Annotate
3029 Assertion_Policy
3030 Assume_No_Invalid_Values
3031 C_Pass_By_Copy
3032 Check_Float_Overflow
3033 Check_Name
3034 Check_Policy
3035 Compile_Time_Error
3036 Compile_Time_Warning
3037 Compiler_Unit
3038 Compiler_Unit_Warning
3039 Component_Alignment
3040 Convention_Identifier
3041 Debug_Policy
3042 Detect_Blocking
3043 Default_Scalar_Storage_Order
3044 Default_Storage_Pool
3045 Disable_Atomic_Synchronization
3046 Discard_Names
3047 Elaboration_Checks
3048 Eliminate
3049 Enable_Atomic_Synchronization
3050 Extend_System
3051 Extensions_Allowed
3052 External_Name_Casing
3053 Fast_Math
3054 Favor_Top_Level
3055 Ignore_Pragma
3056 Implicit_Packing
3057 Initialize_Scalars
3058 Interrupt_State
3059 License
3060 Locking_Policy
3061 No_Component_Reordering
3062 No_Heap_Finalization
3063 No_Run_Time
3064 No_Strict_Aliasing
3065 Normalize_Scalars
3066 Optimize_Alignment
3067 Overflow_Mode
3068 Overriding_Renamings
3069 Partition_Elaboration_Policy
3070 Persistent_BSS
3071 Polling
3072 Prefix_Exception_Messages
3073 Priority_Specific_Dispatching
3074 Profile
3075 Profile_Warnings
3076 Propagate_Exceptions
3077 Queuing_Policy
3078 Rational
3079 Ravenscar
3080 Rename_Pragma
3081 Restricted_Run_Time
3082 Restrictions
3083 Restrictions_Warnings
3084 Reviewable
3085 Short_Circuit_And_Or
3086 Short_Descriptors
3087 Source_File_Name
3088 Source_File_Name_Project
3089 SPARK_Mode
3090 Style_Checks
3091 Suppress
3092 Suppress_Exception_Locations
3093 Task_Dispatching_Policy
3094 Unevaluated_Use_Of_Old
3095 Universal_Data
3096 Unsuppress
3097 Use_VADS_Size
3098 Validity_Checks
3099 Warning_As_Error
3100 Warnings
3101 Wide_Character_Encoding
3102 @end example
3103
3104 @menu
3105 * Handling of Configuration Pragmas::
3106 * The Configuration Pragmas Files::
3107
3108 @end menu
3109
3110 @node Handling of Configuration Pragmas,The Configuration Pragmas Files,,Configuration Pragmas
3111 @anchor{gnat_ugn/the_gnat_compilation_model id29}@anchor{78}@anchor{gnat_ugn/the_gnat_compilation_model handling-of-configuration-pragmas}@anchor{56}
3112 @subsection Handling of Configuration Pragmas
3113
3114
3115 Configuration pragmas may either appear at the start of a compilation
3116 unit, or they can appear in a configuration pragma file to apply to
3117 all compilations performed in a given compilation environment.
3118
3119 GNAT also provides the @code{gnatchop} utility to provide an automatic
3120 way to handle configuration pragmas following the semantics for
3121 compilations (that is, files with multiple units), described in the RM.
3122 See @ref{6f,,Operating gnatchop in Compilation Mode} for details.
3123 However, for most purposes, it will be more convenient to edit the
3124 @code{gnat.adc} file that contains configuration pragmas directly,
3125 as described in the following section.
3126
3127 In the case of @code{Restrictions} pragmas appearing as configuration
3128 pragmas in individual compilation units, the exact handling depends on
3129 the type of restriction.
3130
3131 Restrictions that require partition-wide consistency (like
3132 @code{No_Tasking}) are
3133 recognized wherever they appear
3134 and can be freely inherited, e.g. from a @emph{with}ed unit to the @emph{with}ing
3135 unit. This makes sense since the binder will in any case insist on seeing
3136 consistent use, so any unit not conforming to any restrictions that are
3137 anywhere in the partition will be rejected, and you might as well find
3138 that out at compile time rather than at bind time.
3139
3140 For restrictions that do not require partition-wide consistency, e.g.
3141 SPARK or No_Implementation_Attributes, in general the restriction applies
3142 only to the unit in which the pragma appears, and not to any other units.
3143
3144 The exception is No_Elaboration_Code which always applies to the entire
3145 object file from a compilation, i.e. to the body, spec, and all subunits.
3146 This restriction can be specified in a configuration pragma file, or it
3147 can be on the body and/or the spec (in eithe case it applies to all the
3148 relevant units). It can appear on a subunit only if it has previously
3149 appeared in the body of spec.
3150
3151 @node The Configuration Pragmas Files,,Handling of Configuration Pragmas,Configuration Pragmas
3152 @anchor{gnat_ugn/the_gnat_compilation_model the-configuration-pragmas-files}@anchor{79}@anchor{gnat_ugn/the_gnat_compilation_model id30}@anchor{7a}
3153 @subsection The Configuration Pragmas Files
3154
3155
3156 @geindex gnat.adc
3157
3158 In GNAT a compilation environment is defined by the current
3159 directory at the time that a compile command is given. This current
3160 directory is searched for a file whose name is @code{gnat.adc}. If
3161 this file is present, it is expected to contain one or more
3162 configuration pragmas that will be applied to the current compilation.
3163 However, if the switch @code{-gnatA} is used, @code{gnat.adc} is not
3164 considered. When taken into account, @code{gnat.adc} is added to the
3165 dependencies, so that if @code{gnat.adc} is modified later, an invocation of
3166 @code{gnatmake} will recompile the source.
3167
3168 Configuration pragmas may be entered into the @code{gnat.adc} file
3169 either by running @code{gnatchop} on a source file that consists only of
3170 configuration pragmas, or more conveniently by direct editing of the
3171 @code{gnat.adc} file, which is a standard format source file.
3172
3173 Besides @code{gnat.adc}, additional files containing configuration
3174 pragmas may be applied to the current compilation using the switch
3175 @code{-gnatec=@emph{path}} where @code{path} must designate an existing file that
3176 contains only configuration pragmas. These configuration pragmas are
3177 in addition to those found in @code{gnat.adc} (provided @code{gnat.adc}
3178 is present and switch @code{-gnatA} is not used).
3179
3180 It is allowable to specify several switches @code{-gnatec=}, all of which
3181 will be taken into account.
3182
3183 Files containing configuration pragmas specified with switches
3184 @code{-gnatec=} are added to the dependencies, unless they are
3185 temporary files. A file is considered temporary if its name ends in
3186 @code{.tmp} or @code{.TMP}. Certain tools follow this naming
3187 convention because they pass information to @code{gcc} via
3188 temporary files that are immediately deleted; it doesn't make sense to
3189 depend on a file that no longer exists. Such tools include
3190 @code{gprbuild}, @code{gnatmake}, and @code{gnatcheck}.
3191
3192 If you are using project file, a separate mechanism is provided using
3193 project attributes.
3194
3195 @c --Comment
3196 @c See :ref:`Specifying_Configuration_Pragmas` for more details.
3197
3198 @node Generating Object Files,Source Dependencies,Configuration Pragmas,The GNAT Compilation Model
3199 @anchor{gnat_ugn/the_gnat_compilation_model generating-object-files}@anchor{40}@anchor{gnat_ugn/the_gnat_compilation_model id31}@anchor{7b}
3200 @section Generating Object Files
3201
3202
3203 An Ada program consists of a set of source files, and the first step in
3204 compiling the program is to generate the corresponding object files.
3205 These are generated by compiling a subset of these source files.
3206 The files you need to compile are the following:
3207
3208
3209 @itemize *
3210
3211 @item
3212 If a package spec has no body, compile the package spec to produce the
3213 object file for the package.
3214
3215 @item
3216 If a package has both a spec and a body, compile the body to produce the
3217 object file for the package. The source file for the package spec need
3218 not be compiled in this case because there is only one object file, which
3219 contains the code for both the spec and body of the package.
3220
3221 @item
3222 For a subprogram, compile the subprogram body to produce the object file
3223 for the subprogram. The spec, if one is present, is as usual in a
3224 separate file, and need not be compiled.
3225 @end itemize
3226
3227 @geindex Subunits
3228
3229
3230 @itemize *
3231
3232 @item
3233 In the case of subunits, only compile the parent unit. A single object
3234 file is generated for the entire subunit tree, which includes all the
3235 subunits.
3236
3237 @item
3238 Compile child units independently of their parent units
3239 (though, of course, the spec of all the ancestor unit must be present in order
3240 to compile a child unit).
3241
3242 @geindex Generics
3243
3244 @item
3245 Compile generic units in the same manner as any other units. The object
3246 files in this case are small dummy files that contain at most the
3247 flag used for elaboration checking. This is because GNAT always handles generic
3248 instantiation by means of macro expansion. However, it is still necessary to
3249 compile generic units, for dependency checking and elaboration purposes.
3250 @end itemize
3251
3252 The preceding rules describe the set of files that must be compiled to
3253 generate the object files for a program. Each object file has the same
3254 name as the corresponding source file, except that the extension is
3255 @code{.o} as usual.
3256
3257 You may wish to compile other files for the purpose of checking their
3258 syntactic and semantic correctness. For example, in the case where a
3259 package has a separate spec and body, you would not normally compile the
3260 spec. However, it is convenient in practice to compile the spec to make
3261 sure it is error-free before compiling clients of this spec, because such
3262 compilations will fail if there is an error in the spec.
3263
3264 GNAT provides an option for compiling such files purely for the
3265 purposes of checking correctness; such compilations are not required as
3266 part of the process of building a program. To compile a file in this
3267 checking mode, use the @code{-gnatc} switch.
3268
3269 @node Source Dependencies,The Ada Library Information Files,Generating Object Files,The GNAT Compilation Model
3270 @anchor{gnat_ugn/the_gnat_compilation_model id32}@anchor{7c}@anchor{gnat_ugn/the_gnat_compilation_model source-dependencies}@anchor{41}
3271 @section Source Dependencies
3272
3273
3274 A given object file clearly depends on the source file which is compiled
3275 to produce it. Here we are using "depends" in the sense of a typical
3276 @code{make} utility; in other words, an object file depends on a source
3277 file if changes to the source file require the object file to be
3278 recompiled.
3279 In addition to this basic dependency, a given object may depend on
3280 additional source files as follows:
3281
3282
3283 @itemize *
3284
3285 @item
3286 If a file being compiled @emph{with}s a unit @code{X}, the object file
3287 depends on the file containing the spec of unit @code{X}. This includes
3288 files that are @emph{with}ed implicitly either because they are parents
3289 of @emph{with}ed child units or they are run-time units required by the
3290 language constructs used in a particular unit.
3291
3292 @item
3293 If a file being compiled instantiates a library level generic unit, the
3294 object file depends on both the spec and body files for this generic
3295 unit.
3296
3297 @item
3298 If a file being compiled instantiates a generic unit defined within a
3299 package, the object file depends on the body file for the package as
3300 well as the spec file.
3301 @end itemize
3302
3303 @geindex Inline
3304
3305 @geindex -gnatn switch
3306
3307
3308 @itemize *
3309
3310 @item
3311 If a file being compiled contains a call to a subprogram for which
3312 pragma @code{Inline} applies and inlining is activated with the
3313 @code{-gnatn} switch, the object file depends on the file containing the
3314 body of this subprogram as well as on the file containing the spec. Note
3315 that for inlining to actually occur as a result of the use of this switch,
3316 it is necessary to compile in optimizing mode.
3317
3318 @geindex -gnatN switch
3319
3320 The use of @code{-gnatN} activates inlining optimization
3321 that is performed by the front end of the compiler. This inlining does
3322 not require that the code generation be optimized. Like @code{-gnatn},
3323 the use of this switch generates additional dependencies.
3324
3325 When using a gcc-based back end (in practice this means using any version
3326 of GNAT other than for the JVM, .NET or GNAAMP platforms), then the use of
3327 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
3328 Historically front end inlining was more extensive than the gcc back end
3329 inlining, but that is no longer the case.
3330
3331 @item
3332 If an object file @code{O} depends on the proper body of a subunit through
3333 inlining or instantiation, it depends on the parent unit of the subunit.
3334 This means that any modification of the parent unit or one of its subunits
3335 affects the compilation of @code{O}.
3336
3337 @item
3338 The object file for a parent unit depends on all its subunit body files.
3339
3340 @item
3341 The previous two rules meant that for purposes of computing dependencies and
3342 recompilation, a body and all its subunits are treated as an indivisible whole.
3343
3344 These rules are applied transitively: if unit @code{A} @emph{with}s
3345 unit @code{B}, whose elaboration calls an inlined procedure in package
3346 @code{C}, the object file for unit @code{A} will depend on the body of
3347 @code{C}, in file @code{c.adb}.
3348
3349 The set of dependent files described by these rules includes all the
3350 files on which the unit is semantically dependent, as dictated by the
3351 Ada language standard. However, it is a superset of what the
3352 standard describes, because it includes generic, inline, and subunit
3353 dependencies.
3354
3355 An object file must be recreated by recompiling the corresponding source
3356 file if any of the source files on which it depends are modified. For
3357 example, if the @code{make} utility is used to control compilation,
3358 the rule for an Ada object file must mention all the source files on
3359 which the object file depends, according to the above definition.
3360 The determination of the necessary
3361 recompilations is done automatically when one uses @code{gnatmake}.
3362 @end itemize
3363
3364 @node The Ada Library Information Files,Binding an Ada Program,Source Dependencies,The GNAT Compilation Model
3365 @anchor{gnat_ugn/the_gnat_compilation_model id33}@anchor{7d}@anchor{gnat_ugn/the_gnat_compilation_model the-ada-library-information-files}@anchor{42}
3366 @section The Ada Library Information Files
3367
3368
3369 @geindex Ada Library Information files
3370
3371 @geindex ALI files
3372
3373 Each compilation actually generates two output files. The first of these
3374 is the normal object file that has a @code{.o} extension. The second is a
3375 text file containing full dependency information. It has the same
3376 name as the source file, but an @code{.ali} extension.
3377 This file is known as the Ada Library Information (@code{ALI}) file.
3378 The following information is contained in the @code{ALI} file.
3379
3380
3381 @itemize *
3382
3383 @item
3384 Version information (indicates which version of GNAT was used to compile
3385 the unit(s) in question)
3386
3387 @item
3388 Main program information (including priority and time slice settings,
3389 as well as the wide character encoding used during compilation).
3390
3391 @item
3392 List of arguments used in the @code{gcc} command for the compilation
3393
3394 @item
3395 Attributes of the unit, including configuration pragmas used, an indication
3396 of whether the compilation was successful, exception model used etc.
3397
3398 @item
3399 A list of relevant restrictions applying to the unit (used for consistency)
3400 checking.
3401
3402 @item
3403 Categorization information (e.g., use of pragma @code{Pure}).
3404
3405 @item
3406 Information on all @emph{with}ed units, including presence of
3407 @code{Elaborate} or @code{Elaborate_All} pragmas.
3408
3409 @item
3410 Information from any @code{Linker_Options} pragmas used in the unit
3411
3412 @item
3413 Information on the use of @code{Body_Version} or @code{Version}
3414 attributes in the unit.
3415
3416 @item
3417 Dependency information. This is a list of files, together with
3418 time stamp and checksum information. These are files on which
3419 the unit depends in the sense that recompilation is required
3420 if any of these units are modified.
3421
3422 @item
3423 Cross-reference data. Contains information on all entities referenced
3424 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
3425 provide cross-reference information.
3426 @end itemize
3427
3428 For a full detailed description of the format of the @code{ALI} file,
3429 see the source of the body of unit @code{Lib.Writ}, contained in file
3430 @code{lib-writ.adb} in the GNAT compiler sources.
3431
3432 @node Binding an Ada Program,GNAT and Libraries,The Ada Library Information Files,The GNAT Compilation Model
3433 @anchor{gnat_ugn/the_gnat_compilation_model id34}@anchor{7e}@anchor{gnat_ugn/the_gnat_compilation_model binding-an-ada-program}@anchor{43}
3434 @section Binding an Ada Program
3435
3436
3437 When using languages such as C and C++, once the source files have been
3438 compiled the only remaining step in building an executable program
3439 is linking the object modules together. This means that it is possible to
3440 link an inconsistent version of a program, in which two units have
3441 included different versions of the same header.
3442
3443 The rules of Ada do not permit such an inconsistent program to be built.
3444 For example, if two clients have different versions of the same package,
3445 it is illegal to build a program containing these two clients.
3446 These rules are enforced by the GNAT binder, which also determines an
3447 elaboration order consistent with the Ada rules.
3448
3449 The GNAT binder is run after all the object files for a program have
3450 been created. It is given the name of the main program unit, and from
3451 this it determines the set of units required by the program, by reading the
3452 corresponding ALI files. It generates error messages if the program is
3453 inconsistent or if no valid order of elaboration exists.
3454
3455 If no errors are detected, the binder produces a main program, in Ada by
3456 default, that contains calls to the elaboration procedures of those
3457 compilation unit that require them, followed by
3458 a call to the main program. This Ada program is compiled to generate the
3459 object file for the main program. The name of
3460 the Ada file is @code{b~xxx}.adb` (with the corresponding spec
3461 @code{b~xxx}.ads`) where @code{xxx} is the name of the
3462 main program unit.
3463
3464 Finally, the linker is used to build the resulting executable program,
3465 using the object from the main program from the bind step as well as the
3466 object files for the Ada units of the program.
3467
3468 @node GNAT and Libraries,Conditional Compilation,Binding an Ada Program,The GNAT Compilation Model
3469 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-libraries}@anchor{15}@anchor{gnat_ugn/the_gnat_compilation_model id35}@anchor{7f}
3470 @section GNAT and Libraries
3471
3472
3473 @geindex Library building and using
3474
3475 This section describes how to build and use libraries with GNAT, and also shows
3476 how to recompile the GNAT run-time library. You should be familiar with the
3477 Project Manager facility (see the @emph{GNAT_Project_Manager} chapter of the
3478 @emph{GPRbuild User's Guide}) before reading this chapter.
3479
3480 @menu
3481 * Introduction to Libraries in GNAT::
3482 * General Ada Libraries::
3483 * Stand-alone Ada Libraries::
3484 * Rebuilding the GNAT Run-Time Library::
3485
3486 @end menu
3487
3488 @node Introduction to Libraries in GNAT,General Ada Libraries,,GNAT and Libraries
3489 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-libraries-in-gnat}@anchor{80}@anchor{gnat_ugn/the_gnat_compilation_model id36}@anchor{81}
3490 @subsection Introduction to Libraries in GNAT
3491
3492
3493 A library is, conceptually, a collection of objects which does not have its
3494 own main thread of execution, but rather provides certain services to the
3495 applications that use it. A library can be either statically linked with the
3496 application, in which case its code is directly included in the application,
3497 or, on platforms that support it, be dynamically linked, in which case
3498 its code is shared by all applications making use of this library.
3499
3500 GNAT supports both types of libraries.
3501 In the static case, the compiled code can be provided in different ways. The
3502 simplest approach is to provide directly the set of objects resulting from
3503 compilation of the library source files. Alternatively, you can group the
3504 objects into an archive using whatever commands are provided by the operating
3505 system. For the latter case, the objects are grouped into a shared library.
3506
3507 In the GNAT environment, a library has three types of components:
3508
3509
3510 @itemize *
3511
3512 @item
3513 Source files,
3514
3515 @item
3516 @code{ALI} files (see @ref{42,,The Ada Library Information Files}), and
3517
3518 @item
3519 Object files, an archive or a shared library.
3520 @end itemize
3521
3522 A GNAT library may expose all its source files, which is useful for
3523 documentation purposes. Alternatively, it may expose only the units needed by
3524 an external user to make use of the library. That is to say, the specs
3525 reflecting the library services along with all the units needed to compile
3526 those specs, which can include generic bodies or any body implementing an
3527 inlined routine. In the case of @emph{stand-alone libraries} those exposed
3528 units are called @emph{interface units} (@ref{82,,Stand-alone Ada Libraries}).
3529
3530 All compilation units comprising an application, including those in a library,
3531 need to be elaborated in an order partially defined by Ada's semantics. GNAT
3532 computes the elaboration order from the @code{ALI} files and this is why they
3533 constitute a mandatory part of GNAT libraries.
3534 @emph{Stand-alone libraries} are the exception to this rule because a specific
3535 library elaboration routine is produced independently of the application(s)
3536 using the library.
3537
3538 @node General Ada Libraries,Stand-alone Ada Libraries,Introduction to Libraries in GNAT,GNAT and Libraries
3539 @anchor{gnat_ugn/the_gnat_compilation_model general-ada-libraries}@anchor{83}@anchor{gnat_ugn/the_gnat_compilation_model id37}@anchor{84}
3540 @subsection General Ada Libraries
3541
3542
3543 @menu
3544 * Building a library::
3545 * Installing a library::
3546 * Using a library::
3547
3548 @end menu
3549
3550 @node Building a library,Installing a library,,General Ada Libraries
3551 @anchor{gnat_ugn/the_gnat_compilation_model building-a-library}@anchor{85}@anchor{gnat_ugn/the_gnat_compilation_model id38}@anchor{86}
3552 @subsubsection Building a library
3553
3554
3555 The easiest way to build a library is to use the Project Manager,
3556 which supports a special type of project called a @emph{Library Project}
3557 (see the @emph{Library Projects} section in the @emph{GNAT Project Manager}
3558 chapter of the @emph{GPRbuild User's Guide}).
3559
3560 A project is considered a library project, when two project-level attributes
3561 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
3562 control different aspects of library configuration, additional optional
3563 project-level attributes can be specified:
3564
3565
3566 @itemize *
3567
3568 @item
3569
3570 @table @asis
3571
3572 @item @code{Library_Kind}
3573
3574 This attribute controls whether the library is to be static or dynamic
3575 @end table
3576
3577 @item
3578
3579 @table @asis
3580
3581 @item @code{Library_Version}
3582
3583 This attribute specifies the library version; this value is used
3584 during dynamic linking of shared libraries to determine if the currently
3585 installed versions of the binaries are compatible.
3586 @end table
3587
3588 @item
3589 @code{Library_Options}
3590
3591 @item
3592
3593 @table @asis
3594
3595 @item @code{Library_GCC}
3596
3597 These attributes specify additional low-level options to be used during
3598 library generation, and redefine the actual application used to generate
3599 library.
3600 @end table
3601 @end itemize
3602
3603 The GNAT Project Manager takes full care of the library maintenance task,
3604 including recompilation of the source files for which objects do not exist
3605 or are not up to date, assembly of the library archive, and installation of
3606 the library (i.e., copying associated source, object and @code{ALI} files
3607 to the specified location).
3608
3609 Here is a simple library project file:
3610
3611 @example
3612 project My_Lib is
3613 for Source_Dirs use ("src1", "src2");
3614 for Object_Dir use "obj";
3615 for Library_Name use "mylib";
3616 for Library_Dir use "lib";
3617 for Library_Kind use "dynamic";
3618 end My_lib;
3619 @end example
3620
3621 and the compilation command to build and install the library:
3622
3623 @example
3624 $ gnatmake -Pmy_lib
3625 @end example
3626
3627 It is not entirely trivial to perform manually all the steps required to
3628 produce a library. We recommend that you use the GNAT Project Manager
3629 for this task. In special cases where this is not desired, the necessary
3630 steps are discussed below.
3631
3632 There are various possibilities for compiling the units that make up the
3633 library: for example with a Makefile (@ref{1f,,Using the GNU make Utility}) or
3634 with a conventional script. For simple libraries, it is also possible to create
3635 a dummy main program which depends upon all the packages that comprise the
3636 interface of the library. This dummy main program can then be given to
3637 @code{gnatmake}, which will ensure that all necessary objects are built.
3638
3639 After this task is accomplished, you should follow the standard procedure
3640 of the underlying operating system to produce the static or shared library.
3641
3642 Here is an example of such a dummy program:
3643
3644 @example
3645 with My_Lib.Service1;
3646 with My_Lib.Service2;
3647 with My_Lib.Service3;
3648 procedure My_Lib_Dummy is
3649 begin
3650 null;
3651 end;
3652 @end example
3653
3654 Here are the generic commands that will build an archive or a shared library.
3655
3656 @example
3657 # compiling the library
3658 $ gnatmake -c my_lib_dummy.adb
3659
3660 # we don't need the dummy object itself
3661 $ rm my_lib_dummy.o my_lib_dummy.ali
3662
3663 # create an archive with the remaining objects
3664 $ ar rc libmy_lib.a *.o
3665 # some systems may require "ranlib" to be run as well
3666
3667 # or create a shared library
3668 $ gcc -shared -o libmy_lib.so *.o
3669 # some systems may require the code to have been compiled with -fPIC
3670
3671 # remove the object files that are now in the library
3672 $ rm *.o
3673
3674 # Make the ALI files read-only so that gnatmake will not try to
3675 # regenerate the objects that are in the library
3676 $ chmod -w *.ali
3677 @end example
3678
3679 Please note that the library must have a name of the form @code{lib@emph{xxx}.a}
3680 or @code{lib@emph{xxx}.so} (or @code{lib@emph{xxx}.dll} on Windows) in order to
3681 be accessed by the directive @code{-l@emph{xxx}} at link time.
3682
3683 @node Installing a library,Using a library,Building a library,General Ada Libraries
3684 @anchor{gnat_ugn/the_gnat_compilation_model installing-a-library}@anchor{87}@anchor{gnat_ugn/the_gnat_compilation_model id39}@anchor{88}
3685 @subsubsection Installing a library
3686
3687
3688 @geindex ADA_PROJECT_PATH
3689
3690 @geindex GPR_PROJECT_PATH
3691
3692 If you use project files, library installation is part of the library build
3693 process (see the @emph{Installing a Library with Project Files} section of the
3694 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}).
3695
3696 When project files are not an option, it is also possible, but not recommended,
3697 to install the library so that the sources needed to use the library are on the
3698 Ada source path and the ALI files & libraries be on the Ada Object path (see
3699 @ref{89,,Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
3700 administrator can place general-purpose libraries in the default compiler
3701 paths, by specifying the libraries' location in the configuration files
3702 @code{ada_source_path} and @code{ada_object_path}. These configuration files
3703 must be located in the GNAT installation tree at the same place as the gcc spec
3704 file. The location of the gcc spec file can be determined as follows:
3705
3706 @example
3707 $ gcc -v
3708 @end example
3709
3710 The configuration files mentioned above have a simple format: each line
3711 must contain one unique directory name.
3712 Those names are added to the corresponding path
3713 in their order of appearance in the file. The names can be either absolute
3714 or relative; in the latter case, they are relative to where theses files
3715 are located.
3716
3717 The files @code{ada_source_path} and @code{ada_object_path} might not be
3718 present in a
3719 GNAT installation, in which case, GNAT will look for its run-time library in
3720 the directories @code{adainclude} (for the sources) and @code{adalib} (for the
3721 objects and @code{ALI} files). When the files exist, the compiler does not
3722 look in @code{adainclude} and @code{adalib}, and thus the
3723 @code{ada_source_path} file
3724 must contain the location for the GNAT run-time sources (which can simply
3725 be @code{adainclude}). In the same way, the @code{ada_object_path} file must
3726 contain the location for the GNAT run-time objects (which can simply
3727 be @code{adalib}).
3728
3729 You can also specify a new default path to the run-time library at compilation
3730 time with the switch @code{--RTS=rts-path}. You can thus choose / change
3731 the run-time library you want your program to be compiled with. This switch is
3732 recognized by @code{gcc}, @code{gnatmake}, @code{gnatbind},
3733 @code{gnatls}, @code{gnatfind} and @code{gnatxref}.
3734
3735 It is possible to install a library before or after the standard GNAT
3736 library, by reordering the lines in the configuration files. In general, a
3737 library must be installed before the GNAT library if it redefines
3738 any part of it.
3739
3740 @node Using a library,,Installing a library,General Ada Libraries
3741 @anchor{gnat_ugn/the_gnat_compilation_model using-a-library}@anchor{8a}@anchor{gnat_ugn/the_gnat_compilation_model id40}@anchor{8b}
3742 @subsubsection Using a library
3743
3744
3745 Once again, the project facility greatly simplifies the use of
3746 libraries. In this context, using a library is just a matter of adding a
3747 @emph{with} clause in the user project. For instance, to make use of the
3748 library @code{My_Lib} shown in examples in earlier sections, you can
3749 write:
3750
3751 @example
3752 with "my_lib";
3753 project My_Proj is
3754 ...
3755 end My_Proj;
3756 @end example
3757
3758 Even if you have a third-party, non-Ada library, you can still use GNAT's
3759 Project Manager facility to provide a wrapper for it. For example, the
3760 following project, when @emph{with}ed by your main project, will link with the
3761 third-party library @code{liba.a}:
3762
3763 @example
3764 project Liba is
3765 for Externally_Built use "true";
3766 for Source_Files use ();
3767 for Library_Dir use "lib";
3768 for Library_Name use "a";
3769 for Library_Kind use "static";
3770 end Liba;
3771 @end example
3772
3773 This is an alternative to the use of @code{pragma Linker_Options}. It is
3774 especially interesting in the context of systems with several interdependent
3775 static libraries where finding a proper linker order is not easy and best be
3776 left to the tools having visibility over project dependence information.
3777
3778 In order to use an Ada library manually, you need to make sure that this
3779 library is on both your source and object path
3780 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}
3781 and @ref{8c,,Search Paths for gnatbind}). Furthermore, when the objects are grouped
3782 in an archive or a shared library, you need to specify the desired
3783 library at link time.
3784
3785 For example, you can use the library @code{mylib} installed in
3786 @code{/dir/my_lib_src} and @code{/dir/my_lib_obj} with the following commands:
3787
3788 @example
3789 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
3790 -largs -lmy_lib
3791 @end example
3792
3793 This can be expressed more simply:
3794
3795 @example
3796 $ gnatmake my_appl
3797 @end example
3798
3799 when the following conditions are met:
3800
3801
3802 @itemize *
3803
3804 @item
3805 @code{/dir/my_lib_src} has been added by the user to the environment
3806 variable
3807 @geindex ADA_INCLUDE_PATH
3808 @geindex environment variable; ADA_INCLUDE_PATH
3809 @code{ADA_INCLUDE_PATH}, or by the administrator to the file
3810 @code{ada_source_path}
3811
3812 @item
3813 @code{/dir/my_lib_obj} has been added by the user to the environment
3814 variable
3815 @geindex ADA_OBJECTS_PATH
3816 @geindex environment variable; ADA_OBJECTS_PATH
3817 @code{ADA_OBJECTS_PATH}, or by the administrator to the file
3818 @code{ada_object_path}
3819
3820 @item
3821 a pragma @code{Linker_Options} has been added to one of the sources.
3822 For example:
3823
3824 @example
3825 pragma Linker_Options ("-lmy_lib");
3826 @end example
3827 @end itemize
3828
3829 Note that you may also load a library dynamically at
3830 run time given its filename, as illustrated in the GNAT @code{plugins} example
3831 in the directory @code{share/examples/gnat/plugins} within the GNAT
3832 install area.
3833
3834 @node Stand-alone Ada Libraries,Rebuilding the GNAT Run-Time Library,General Ada Libraries,GNAT and Libraries
3835 @anchor{gnat_ugn/the_gnat_compilation_model stand-alone-ada-libraries}@anchor{82}@anchor{gnat_ugn/the_gnat_compilation_model id41}@anchor{8d}
3836 @subsection Stand-alone Ada Libraries
3837
3838
3839 @geindex Stand-alone libraries
3840
3841 @menu
3842 * Introduction to Stand-alone Libraries::
3843 * Building a Stand-alone Library::
3844 * Creating a Stand-alone Library to be used in a non-Ada context::
3845 * Restrictions in Stand-alone Libraries::
3846
3847 @end menu
3848
3849 @node Introduction to Stand-alone Libraries,Building a Stand-alone Library,,Stand-alone Ada Libraries
3850 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-stand-alone-libraries}@anchor{8e}@anchor{gnat_ugn/the_gnat_compilation_model id42}@anchor{8f}
3851 @subsubsection Introduction to Stand-alone Libraries
3852
3853
3854 A Stand-alone Library (abbreviated 'SAL') is a library that contains the
3855 necessary code to
3856 elaborate the Ada units that are included in the library. In contrast with
3857 an ordinary library, which consists of all sources, objects and @code{ALI}
3858 files of the
3859 library, a SAL may specify a restricted subset of compilation units
3860 to serve as a library interface. In this case, the fully
3861 self-sufficient set of files will normally consist of an objects
3862 archive, the sources of interface units' specs, and the @code{ALI}
3863 files of interface units.
3864 If an interface spec contains a generic unit or an inlined subprogram,
3865 the body's
3866 source must also be provided; if the units that must be provided in the source
3867 form depend on other units, the source and @code{ALI} files of those must
3868 also be provided.
3869
3870 The main purpose of a SAL is to minimize the recompilation overhead of client
3871 applications when a new version of the library is installed. Specifically,
3872 if the interface sources have not changed, client applications do not need to
3873 be recompiled. If, furthermore, a SAL is provided in the shared form and its
3874 version, controlled by @code{Library_Version} attribute, is not changed,
3875 then the clients do not need to be relinked.
3876
3877 SALs also allow the library providers to minimize the amount of library source
3878 text exposed to the clients. Such 'information hiding' might be useful or
3879 necessary for various reasons.
3880
3881 Stand-alone libraries are also well suited to be used in an executable whose
3882 main routine is not written in Ada.
3883
3884 @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
3885 @anchor{gnat_ugn/the_gnat_compilation_model id43}@anchor{90}@anchor{gnat_ugn/the_gnat_compilation_model building-a-stand-alone-library}@anchor{91}
3886 @subsubsection Building a Stand-alone Library
3887
3888
3889 GNAT's Project facility provides a simple way of building and installing
3890 stand-alone libraries; see the @emph{Stand-alone Library Projects} section
3891 in the @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}.
3892 To be a Stand-alone Library Project, in addition to the two attributes
3893 that make a project a Library Project (@code{Library_Name} and
3894 @code{Library_Dir}; see the @emph{Library Projects} section in the
3895 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}),
3896 the attribute @code{Library_Interface} must be defined. For example:
3897
3898 @example
3899 for Library_Dir use "lib_dir";
3900 for Library_Name use "dummy";
3901 for Library_Interface use ("int1", "int1.child");
3902 @end example
3903
3904 Attribute @code{Library_Interface} has a non-empty string list value,
3905 each string in the list designating a unit contained in an immediate source
3906 of the project file.
3907
3908 When a Stand-alone Library is built, first the binder is invoked to build
3909 a package whose name depends on the library name
3910 (@code{b~dummy.ads/b} in the example above).
3911 This binder-generated package includes initialization and
3912 finalization procedures whose
3913 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
3914 in the example
3915 above). The object corresponding to this package is included in the library.
3916
3917 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
3918 calling of these procedures if a static SAL is built, or if a shared SAL
3919 is built
3920 with the project-level attribute @code{Library_Auto_Init} set to
3921 @code{"false"}.
3922
3923 For a Stand-Alone Library, only the @code{ALI} files of the Interface Units
3924 (those that are listed in attribute @code{Library_Interface}) are copied to
3925 the Library Directory. As a consequence, only the Interface Units may be
3926 imported from Ada units outside of the library. If other units are imported,
3927 the binding phase will fail.
3928
3929 It is also possible to build an encapsulated library where not only
3930 the code to elaborate and finalize the library is embedded but also
3931 ensuring that the library is linked only against static
3932 libraries. So an encapsulated library only depends on system
3933 libraries, all other code, including the GNAT runtime, is embedded. To
3934 build an encapsulated library the attribute
3935 @code{Library_Standalone} must be set to @code{encapsulated}:
3936
3937 @example
3938 for Library_Dir use "lib_dir";
3939 for Library_Name use "dummy";
3940 for Library_Kind use "dynamic";
3941 for Library_Interface use ("int1", "int1.child");
3942 for Library_Standalone use "encapsulated";
3943 @end example
3944
3945 The default value for this attribute is @code{standard} in which case
3946 a stand-alone library is built.
3947
3948 The attribute @code{Library_Src_Dir} may be specified for a
3949 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
3950 single string value. Its value must be the path (absolute or relative to the
3951 project directory) of an existing directory. This directory cannot be the
3952 object directory or one of the source directories, but it can be the same as
3953 the library directory. The sources of the Interface
3954 Units of the library that are needed by an Ada client of the library will be
3955 copied to the designated directory, called the Interface Copy directory.
3956 These sources include the specs of the Interface Units, but they may also
3957 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
3958 are used, or when there is a generic unit in the spec. Before the sources
3959 are copied to the Interface Copy directory, an attempt is made to delete all
3960 files in the Interface Copy directory.
3961
3962 Building stand-alone libraries by hand is somewhat tedious, but for those
3963 occasions when it is necessary here are the steps that you need to perform:
3964
3965
3966 @itemize *
3967
3968 @item
3969 Compile all library sources.
3970
3971 @item
3972 Invoke the binder with the switch @code{-n} (No Ada main program),
3973 with all the @code{ALI} files of the interfaces, and
3974 with the switch @code{-L} to give specific names to the @code{init}
3975 and @code{final} procedures. For example:
3976
3977 @example
3978 $ gnatbind -n int1.ali int2.ali -Lsal1
3979 @end example
3980
3981 @item
3982 Compile the binder generated file:
3983
3984 @example
3985 $ gcc -c b~int2.adb
3986 @end example
3987
3988 @item
3989 Link the dynamic library with all the necessary object files,
3990 indicating to the linker the names of the @code{init} (and possibly
3991 @code{final}) procedures for automatic initialization (and finalization).
3992 The built library should be placed in a directory different from
3993 the object directory.
3994
3995 @item
3996 Copy the @code{ALI} files of the interface to the library directory,
3997 add in this copy an indication that it is an interface to a SAL
3998 (i.e., add a word @code{SL} on the line in the @code{ALI} file that starts
3999 with letter 'P') and make the modified copy of the @code{ALI} file
4000 read-only.
4001 @end itemize
4002
4003 Using SALs is not different from using other libraries
4004 (see @ref{8a,,Using a library}).
4005
4006 @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
4007 @anchor{gnat_ugn/the_gnat_compilation_model creating-a-stand-alone-library-to-be-used-in-a-non-ada-context}@anchor{92}@anchor{gnat_ugn/the_gnat_compilation_model id44}@anchor{93}
4008 @subsubsection Creating a Stand-alone Library to be used in a non-Ada context
4009
4010
4011 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
4012 a non-Ada context.
4013
4014 The only extra step required is to ensure that library interface subprograms
4015 are compatible with the main program, by means of @code{pragma Export}
4016 or @code{pragma Convention}.
4017
4018 Here is an example of simple library interface for use with C main program:
4019
4020 @example
4021 package My_Package is
4022
4023 procedure Do_Something;
4024 pragma Export (C, Do_Something, "do_something");
4025
4026 procedure Do_Something_Else;
4027 pragma Export (C, Do_Something_Else, "do_something_else");
4028
4029 end My_Package;
4030 @end example
4031
4032 On the foreign language side, you must provide a 'foreign' view of the
4033 library interface; remember that it should contain elaboration routines in
4034 addition to interface subprograms.
4035
4036 The example below shows the content of @code{mylib_interface.h} (note
4037 that there is no rule for the naming of this file, any name can be used)
4038
4039 @example
4040 /* the library elaboration procedure */
4041 extern void mylibinit (void);
4042
4043 /* the library finalization procedure */
4044 extern void mylibfinal (void);
4045
4046 /* the interface exported by the library */
4047 extern void do_something (void);
4048 extern void do_something_else (void);
4049 @end example
4050
4051 Libraries built as explained above can be used from any program, provided
4052 that the elaboration procedures (named @code{mylibinit} in the previous
4053 example) are called before the library services are used. Any number of
4054 libraries can be used simultaneously, as long as the elaboration
4055 procedure of each library is called.
4056
4057 Below is an example of a C program that uses the @code{mylib} library.
4058
4059 @example
4060 #include "mylib_interface.h"
4061
4062 int
4063 main (void)
4064 @{
4065 /* First, elaborate the library before using it */
4066 mylibinit ();
4067
4068 /* Main program, using the library exported entities */
4069 do_something ();
4070 do_something_else ();
4071
4072 /* Library finalization at the end of the program */
4073 mylibfinal ();
4074 return 0;
4075 @}
4076 @end example
4077
4078 Note that invoking any library finalization procedure generated by
4079 @code{gnatbind} shuts down the Ada run-time environment.
4080 Consequently, the
4081 finalization of all Ada libraries must be performed at the end of the program.
4082 No call to these libraries or to the Ada run-time library should be made
4083 after the finalization phase.
4084
4085 Note also that special care must be taken with multi-tasks
4086 applications. The initialization and finalization routines are not
4087 protected against concurrent access. If such requirement is needed it
4088 must be ensured at the application level using a specific operating
4089 system services like a mutex or a critical-section.
4090
4091 @node Restrictions in Stand-alone Libraries,,Creating a Stand-alone Library to be used in a non-Ada context,Stand-alone Ada Libraries
4092 @anchor{gnat_ugn/the_gnat_compilation_model id45}@anchor{94}@anchor{gnat_ugn/the_gnat_compilation_model restrictions-in-stand-alone-libraries}@anchor{95}
4093 @subsubsection Restrictions in Stand-alone Libraries
4094
4095
4096 The pragmas listed below should be used with caution inside libraries,
4097 as they can create incompatibilities with other Ada libraries:
4098
4099
4100 @itemize *
4101
4102 @item
4103 pragma @code{Locking_Policy}
4104
4105 @item
4106 pragma @code{Partition_Elaboration_Policy}
4107
4108 @item
4109 pragma @code{Queuing_Policy}
4110
4111 @item
4112 pragma @code{Task_Dispatching_Policy}
4113
4114 @item
4115 pragma @code{Unreserve_All_Interrupts}
4116 @end itemize
4117
4118 When using a library that contains such pragmas, the user must make sure
4119 that all libraries use the same pragmas with the same values. Otherwise,
4120 @code{Program_Error} will
4121 be raised during the elaboration of the conflicting
4122 libraries. The usage of these pragmas and its consequences for the user
4123 should therefore be well documented.
4124
4125 Similarly, the traceback in the exception occurrence mechanism should be
4126 enabled or disabled in a consistent manner across all libraries.
4127 Otherwise, Program_Error will be raised during the elaboration of the
4128 conflicting libraries.
4129
4130 If the @code{Version} or @code{Body_Version}
4131 attributes are used inside a library, then you need to
4132 perform a @code{gnatbind} step that specifies all @code{ALI} files in all
4133 libraries, so that version identifiers can be properly computed.
4134 In practice these attributes are rarely used, so this is unlikely
4135 to be a consideration.
4136
4137 @node Rebuilding the GNAT Run-Time Library,,Stand-alone Ada Libraries,GNAT and Libraries
4138 @anchor{gnat_ugn/the_gnat_compilation_model id46}@anchor{96}@anchor{gnat_ugn/the_gnat_compilation_model rebuilding-the-gnat-run-time-library}@anchor{97}
4139 @subsection Rebuilding the GNAT Run-Time Library
4140
4141
4142 @geindex GNAT Run-Time Library
4143 @geindex rebuilding
4144
4145 @geindex Building the GNAT Run-Time Library
4146
4147 @geindex Rebuilding the GNAT Run-Time Library
4148
4149 @geindex Run-Time Library
4150 @geindex rebuilding
4151
4152 It may be useful to recompile the GNAT library in various contexts, the
4153 most important one being the use of partition-wide configuration pragmas
4154 such as @code{Normalize_Scalars}. A special Makefile called
4155 @code{Makefile.adalib} is provided to that effect and can be found in
4156 the directory containing the GNAT library. The location of this
4157 directory depends on the way the GNAT environment has been installed and can
4158 be determined by means of the command:
4159
4160 @example
4161 $ gnatls -v
4162 @end example
4163
4164 The last entry in the object search path usually contains the
4165 gnat library. This Makefile contains its own documentation and in
4166 particular the set of instructions needed to rebuild a new library and
4167 to use it.
4168
4169 @geindex Conditional compilation
4170
4171 @node Conditional Compilation,Mixed Language Programming,GNAT and Libraries,The GNAT Compilation Model
4172 @anchor{gnat_ugn/the_gnat_compilation_model id47}@anchor{98}@anchor{gnat_ugn/the_gnat_compilation_model conditional-compilation}@anchor{16}
4173 @section Conditional Compilation
4174
4175
4176 This section presents some guidelines for modeling conditional compilation in Ada and describes the
4177 gnatprep preprocessor utility.
4178
4179 @geindex Conditional compilation
4180
4181 @menu
4182 * Modeling Conditional Compilation in Ada::
4183 * Preprocessing with gnatprep::
4184 * Integrated Preprocessing::
4185
4186 @end menu
4187
4188 @node Modeling Conditional Compilation in Ada,Preprocessing with gnatprep,,Conditional Compilation
4189 @anchor{gnat_ugn/the_gnat_compilation_model modeling-conditional-compilation-in-ada}@anchor{99}@anchor{gnat_ugn/the_gnat_compilation_model id48}@anchor{9a}
4190 @subsection Modeling Conditional Compilation in Ada
4191
4192
4193 It is often necessary to arrange for a single source program
4194 to serve multiple purposes, where it is compiled in different
4195 ways to achieve these different goals. Some examples of the
4196 need for this feature are
4197
4198
4199 @itemize *
4200
4201 @item
4202 Adapting a program to a different hardware environment
4203
4204 @item
4205 Adapting a program to a different target architecture
4206
4207 @item
4208 Turning debugging features on and off
4209
4210 @item
4211 Arranging for a program to compile with different compilers
4212 @end itemize
4213
4214 In C, or C++, the typical approach would be to use the preprocessor
4215 that is defined as part of the language. The Ada language does not
4216 contain such a feature. This is not an oversight, but rather a very
4217 deliberate design decision, based on the experience that overuse of
4218 the preprocessing features in C and C++ can result in programs that
4219 are extremely difficult to maintain. For example, if we have ten
4220 switches that can be on or off, this means that there are a thousand
4221 separate programs, any one of which might not even be syntactically
4222 correct, and even if syntactically correct, the resulting program
4223 might not work correctly. Testing all combinations can quickly become
4224 impossible.
4225
4226 Nevertheless, the need to tailor programs certainly exists, and in
4227 this section we will discuss how this can
4228 be achieved using Ada in general, and GNAT in particular.
4229
4230 @menu
4231 * Use of Boolean Constants::
4232 * Debugging - A Special Case::
4233 * Conditionalizing Declarations::
4234 * Use of Alternative Implementations::
4235 * Preprocessing::
4236
4237 @end menu
4238
4239 @node Use of Boolean Constants,Debugging - A Special Case,,Modeling Conditional Compilation in Ada
4240 @anchor{gnat_ugn/the_gnat_compilation_model id49}@anchor{9b}@anchor{gnat_ugn/the_gnat_compilation_model use-of-boolean-constants}@anchor{9c}
4241 @subsubsection Use of Boolean Constants
4242
4243
4244 In the case where the difference is simply which code
4245 sequence is executed, the cleanest solution is to use Boolean
4246 constants to control which code is executed.
4247
4248 @example
4249 FP_Initialize_Required : constant Boolean := True;
4250 ...
4251 if FP_Initialize_Required then
4252 ...
4253 end if;
4254 @end example
4255
4256 Not only will the code inside the @code{if} statement not be executed if
4257 the constant Boolean is @code{False}, but it will also be completely
4258 deleted from the program.
4259 However, the code is only deleted after the @code{if} statement
4260 has been checked for syntactic and semantic correctness.
4261 (In contrast, with preprocessors the code is deleted before the
4262 compiler ever gets to see it, so it is not checked until the switch
4263 is turned on.)
4264
4265 @geindex Preprocessors (contrasted with conditional compilation)
4266
4267 Typically the Boolean constants will be in a separate package,
4268 something like:
4269
4270 @example
4271 package Config is
4272 FP_Initialize_Required : constant Boolean := True;
4273 Reset_Available : constant Boolean := False;
4274 ...
4275 end Config;
4276 @end example
4277
4278 The @code{Config} package exists in multiple forms for the various targets,
4279 with an appropriate script selecting the version of @code{Config} needed.
4280 Then any other unit requiring conditional compilation can do a @emph{with}
4281 of @code{Config} to make the constants visible.
4282
4283 @node Debugging - A Special Case,Conditionalizing Declarations,Use of Boolean Constants,Modeling Conditional Compilation in Ada
4284 @anchor{gnat_ugn/the_gnat_compilation_model debugging-a-special-case}@anchor{9d}@anchor{gnat_ugn/the_gnat_compilation_model id50}@anchor{9e}
4285 @subsubsection Debugging - A Special Case
4286
4287
4288 A common use of conditional code is to execute statements (for example
4289 dynamic checks, or output of intermediate results) under control of a
4290 debug switch, so that the debugging behavior can be turned on and off.
4291 This can be done using a Boolean constant to control whether the code
4292 is active:
4293
4294 @example
4295 if Debugging then
4296 Put_Line ("got to the first stage!");
4297 end if;
4298 @end example
4299
4300 or
4301
4302 @example
4303 if Debugging and then Temperature > 999.0 then
4304 raise Temperature_Crazy;
4305 end if;
4306 @end example
4307
4308 @geindex pragma Assert
4309
4310 Since this is a common case, there are special features to deal with
4311 this in a convenient manner. For the case of tests, Ada 2005 has added
4312 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
4313 on the @code{Assert} pragma that has always been available in GNAT, so this
4314 feature may be used with GNAT even if you are not using Ada 2005 features.
4315 The use of pragma @code{Assert} is described in the
4316 @cite{GNAT_Reference_Manual}, but as an
4317 example, the last test could be written:
4318
4319 @example
4320 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
4321 @end example
4322
4323 or simply
4324
4325 @example
4326 pragma Assert (Temperature <= 999.0);
4327 @end example
4328
4329 In both cases, if assertions are active and the temperature is excessive,
4330 the exception @code{Assert_Failure} will be raised, with the given string in
4331 the first case or a string indicating the location of the pragma in the second
4332 case used as the exception message.
4333
4334 @geindex pragma Assertion_Policy
4335
4336 You can turn assertions on and off by using the @code{Assertion_Policy}
4337 pragma.
4338
4339 @geindex -gnata switch
4340
4341 This is an Ada 2005 pragma which is implemented in all modes by
4342 GNAT. Alternatively, you can use the @code{-gnata} switch
4343 to enable assertions from the command line, which applies to
4344 all versions of Ada.
4345
4346 @geindex pragma Debug
4347
4348 For the example above with the @code{Put_Line}, the GNAT-specific pragma
4349 @code{Debug} can be used:
4350
4351 @example
4352 pragma Debug (Put_Line ("got to the first stage!"));
4353 @end example
4354
4355 If debug pragmas are enabled, the argument, which must be of the form of
4356 a procedure call, is executed (in this case, @code{Put_Line} will be called).
4357 Only one call can be present, but of course a special debugging procedure
4358 containing any code you like can be included in the program and then
4359 called in a pragma @code{Debug} argument as needed.
4360
4361 One advantage of pragma @code{Debug} over the @code{if Debugging then}
4362 construct is that pragma @code{Debug} can appear in declarative contexts,
4363 such as at the very beginning of a procedure, before local declarations have
4364 been elaborated.
4365
4366 @geindex pragma Debug_Policy
4367
4368 Debug pragmas are enabled using either the @code{-gnata} switch that also
4369 controls assertions, or with a separate Debug_Policy pragma.
4370
4371 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
4372 in Ada 95 and Ada 83 programs as well), and is analogous to
4373 pragma @code{Assertion_Policy} to control assertions.
4374
4375 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
4376 and thus they can appear in @code{gnat.adc} if you are not using a
4377 project file, or in the file designated to contain configuration pragmas
4378 in a project file.
4379 They then apply to all subsequent compilations. In practice the use of
4380 the @code{-gnata} switch is often the most convenient method of controlling
4381 the status of these pragmas.
4382
4383 Note that a pragma is not a statement, so in contexts where a statement
4384 sequence is required, you can't just write a pragma on its own. You have
4385 to add a @code{null} statement.
4386
4387 @example
4388 if ... then
4389 ... -- some statements
4390 else
4391 pragma Assert (Num_Cases < 10);
4392 null;
4393 end if;
4394 @end example
4395
4396 @node Conditionalizing Declarations,Use of Alternative Implementations,Debugging - A Special Case,Modeling Conditional Compilation in Ada
4397 @anchor{gnat_ugn/the_gnat_compilation_model conditionalizing-declarations}@anchor{9f}@anchor{gnat_ugn/the_gnat_compilation_model id51}@anchor{a0}
4398 @subsubsection Conditionalizing Declarations
4399
4400
4401 In some cases it may be necessary to conditionalize declarations to meet
4402 different requirements. For example we might want a bit string whose length
4403 is set to meet some hardware message requirement.
4404
4405 This may be possible using declare blocks controlled
4406 by conditional constants:
4407
4408 @example
4409 if Small_Machine then
4410 declare
4411 X : Bit_String (1 .. 10);
4412 begin
4413 ...
4414 end;
4415 else
4416 declare
4417 X : Large_Bit_String (1 .. 1000);
4418 begin
4419 ...
4420 end;
4421 end if;
4422 @end example
4423
4424 Note that in this approach, both declarations are analyzed by the
4425 compiler so this can only be used where both declarations are legal,
4426 even though one of them will not be used.
4427
4428 Another approach is to define integer constants, e.g., @code{Bits_Per_Word},
4429 or Boolean constants, e.g., @code{Little_Endian}, and then write declarations
4430 that are parameterized by these constants. For example
4431
4432 @example
4433 for Rec use
4434 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
4435 end record;
4436 @end example
4437
4438 If @code{Bits_Per_Word} is set to 32, this generates either
4439
4440 @example
4441 for Rec use
4442 Field1 at 0 range 0 .. 32;
4443 end record;
4444 @end example
4445
4446 for the big endian case, or
4447
4448 @example
4449 for Rec use record
4450 Field1 at 0 range 10 .. 32;
4451 end record;
4452 @end example
4453
4454 for the little endian case. Since a powerful subset of Ada expression
4455 notation is usable for creating static constants, clever use of this
4456 feature can often solve quite difficult problems in conditionalizing
4457 compilation (note incidentally that in Ada 95, the little endian
4458 constant was introduced as @code{System.Default_Bit_Order}, so you do not
4459 need to define this one yourself).
4460
4461 @node Use of Alternative Implementations,Preprocessing,Conditionalizing Declarations,Modeling Conditional Compilation in Ada
4462 @anchor{gnat_ugn/the_gnat_compilation_model use-of-alternative-implementations}@anchor{a1}@anchor{gnat_ugn/the_gnat_compilation_model id52}@anchor{a2}
4463 @subsubsection Use of Alternative Implementations
4464
4465
4466 In some cases, none of the approaches described above are adequate. This
4467 can occur for example if the set of declarations required is radically
4468 different for two different configurations.
4469
4470 In this situation, the official Ada way of dealing with conditionalizing
4471 such code is to write separate units for the different cases. As long as
4472 this does not result in excessive duplication of code, this can be done
4473 without creating maintenance problems. The approach is to share common
4474 code as far as possible, and then isolate the code and declarations
4475 that are different. Subunits are often a convenient method for breaking
4476 out a piece of a unit that is to be conditionalized, with separate files
4477 for different versions of the subunit for different targets, where the
4478 build script selects the right one to give to the compiler.
4479
4480 @geindex Subunits (and conditional compilation)
4481
4482 As an example, consider a situation where a new feature in Ada 2005
4483 allows something to be done in a really nice way. But your code must be able
4484 to compile with an Ada 95 compiler. Conceptually you want to say:
4485
4486 @example
4487 if Ada_2005 then
4488 ... neat Ada 2005 code
4489 else
4490 ... not quite as neat Ada 95 code
4491 end if;
4492 @end example
4493
4494 where @code{Ada_2005} is a Boolean constant.
4495
4496 But this won't work when @code{Ada_2005} is set to @code{False},
4497 since the @code{then} clause will be illegal for an Ada 95 compiler.
4498 (Recall that although such unreachable code would eventually be deleted
4499 by the compiler, it still needs to be legal. If it uses features
4500 introduced in Ada 2005, it will be illegal in Ada 95.)
4501
4502 So instead we write
4503
4504 @example
4505 procedure Insert is separate;
4506 @end example
4507
4508 Then we have two files for the subunit @code{Insert}, with the two sets of
4509 code.
4510 If the package containing this is called @code{File_Queries}, then we might
4511 have two files
4512
4513
4514 @itemize *
4515
4516 @item
4517 @code{file_queries-insert-2005.adb}
4518
4519 @item
4520 @code{file_queries-insert-95.adb}
4521 @end itemize
4522
4523 and the build script renames the appropriate file to @code{file_queries-insert.adb} and then carries out the compilation.
4524
4525 This can also be done with project files' naming schemes. For example:
4526
4527 @example
4528 for body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
4529 @end example
4530
4531 Note also that with project files it is desirable to use a different extension
4532 than @code{ads} / @code{adb} for alternative versions. Otherwise a naming
4533 conflict may arise through another commonly used feature: to declare as part
4534 of the project a set of directories containing all the sources obeying the
4535 default naming scheme.
4536
4537 The use of alternative units is certainly feasible in all situations,
4538 and for example the Ada part of the GNAT run-time is conditionalized
4539 based on the target architecture using this approach. As a specific example,
4540 consider the implementation of the AST feature in VMS. There is one
4541 spec: @code{s-asthan.ads} which is the same for all architectures, and three
4542 bodies:
4543
4544
4545 @itemize *
4546
4547 @item
4548
4549 @table @asis
4550
4551 @item @code{s-asthan.adb}
4552
4553 used for all non-VMS operating systems
4554 @end table
4555
4556 @item
4557
4558 @table @asis
4559
4560 @item @code{s-asthan-vms-alpha.adb}
4561
4562 used for VMS on the Alpha
4563 @end table
4564
4565 @item
4566
4567 @table @asis
4568
4569 @item @code{s-asthan-vms-ia64.adb}
4570
4571 used for VMS on the ia64
4572 @end table
4573 @end itemize
4574
4575 The dummy version @code{s-asthan.adb} simply raises exceptions noting that
4576 this operating system feature is not available, and the two remaining
4577 versions interface with the corresponding versions of VMS to provide
4578 VMS-compatible AST handling. The GNAT build script knows the architecture
4579 and operating system, and automatically selects the right version,
4580 renaming it if necessary to @code{s-asthan.adb} before the run-time build.
4581
4582 Another style for arranging alternative implementations is through Ada's
4583 access-to-subprogram facility.
4584 In case some functionality is to be conditionally included,
4585 you can declare an access-to-procedure variable @code{Ref} that is initialized
4586 to designate a 'do nothing' procedure, and then invoke @code{Ref.all}
4587 when appropriate.
4588 In some library package, set @code{Ref} to @code{Proc'Access} for some
4589 procedure @code{Proc} that performs the relevant processing.
4590 The initialization only occurs if the library package is included in the
4591 program.
4592 The same idea can also be implemented using tagged types and dispatching
4593 calls.
4594
4595 @node Preprocessing,,Use of Alternative Implementations,Modeling Conditional Compilation in Ada
4596 @anchor{gnat_ugn/the_gnat_compilation_model preprocessing}@anchor{a3}@anchor{gnat_ugn/the_gnat_compilation_model id53}@anchor{a4}
4597 @subsubsection Preprocessing
4598
4599
4600 @geindex Preprocessing
4601
4602 Although it is quite possible to conditionalize code without the use of
4603 C-style preprocessing, as described earlier in this section, it is
4604 nevertheless convenient in some cases to use the C approach. Moreover,
4605 older Ada compilers have often provided some preprocessing capability,
4606 so legacy code may depend on this approach, even though it is not
4607 standard.
4608
4609 To accommodate such use, GNAT provides a preprocessor (modeled to a large
4610 extent on the various preprocessors that have been used
4611 with legacy code on other compilers, to enable easier transition).
4612
4613 @geindex gnatprep
4614
4615 The preprocessor may be used in two separate modes. It can be used quite
4616 separately from the compiler, to generate a separate output source file
4617 that is then fed to the compiler as a separate step. This is the
4618 @code{gnatprep} utility, whose use is fully described in
4619 @ref{17,,Preprocessing with gnatprep}.
4620
4621 The preprocessing language allows such constructs as
4622
4623 @example
4624 #if DEBUG or else (PRIORITY > 4) then
4625 sequence of declarations
4626 #else
4627 completely different sequence of declarations
4628 #end if;
4629 @end example
4630
4631 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
4632 defined either on the command line or in a separate file.
4633
4634 The other way of running the preprocessor is even closer to the C style and
4635 often more convenient. In this approach the preprocessing is integrated into
4636 the compilation process. The compiler is given the preprocessor input which
4637 includes @code{#if} lines etc, and then the compiler carries out the
4638 preprocessing internally and processes the resulting output.
4639 For more details on this approach, see @ref{18,,Integrated Preprocessing}.
4640
4641 @node Preprocessing with gnatprep,Integrated Preprocessing,Modeling Conditional Compilation in Ada,Conditional Compilation
4642 @anchor{gnat_ugn/the_gnat_compilation_model id54}@anchor{a5}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-with-gnatprep}@anchor{17}
4643 @subsection Preprocessing with @code{gnatprep}
4644
4645
4646 @geindex gnatprep
4647
4648 @geindex Preprocessing (gnatprep)
4649
4650 This section discusses how to use GNAT's @code{gnatprep} utility for simple
4651 preprocessing.
4652 Although designed for use with GNAT, @code{gnatprep} does not depend on any
4653 special GNAT features.
4654 For further discussion of conditional compilation in general, see
4655 @ref{16,,Conditional Compilation}.
4656
4657 @menu
4658 * Preprocessing Symbols::
4659 * Using gnatprep::
4660 * Switches for gnatprep::
4661 * Form of Definitions File::
4662 * Form of Input Text for gnatprep::
4663
4664 @end menu
4665
4666 @node Preprocessing Symbols,Using gnatprep,,Preprocessing with gnatprep
4667 @anchor{gnat_ugn/the_gnat_compilation_model id55}@anchor{a6}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-symbols}@anchor{a7}
4668 @subsubsection Preprocessing Symbols
4669
4670
4671 Preprocessing symbols are defined in @emph{definition files} and referenced in the
4672 sources to be preprocessed. A preprocessing symbol is an identifier, following
4673 normal Ada (case-insensitive) rules for its syntax, with the restriction that
4674 all characters need to be in the ASCII set (no accented letters).
4675
4676 @node Using gnatprep,Switches for gnatprep,Preprocessing Symbols,Preprocessing with gnatprep
4677 @anchor{gnat_ugn/the_gnat_compilation_model using-gnatprep}@anchor{a8}@anchor{gnat_ugn/the_gnat_compilation_model id56}@anchor{a9}
4678 @subsubsection Using @code{gnatprep}
4679
4680
4681 To call @code{gnatprep} use:
4682
4683 @example
4684 $ gnatprep [ switches ] infile outfile [ deffile ]
4685 @end example
4686
4687 where
4688
4689
4690 @itemize *
4691
4692 @item
4693
4694 @table @asis
4695
4696 @item @emph{switches}
4697
4698 is an optional sequence of switches as described in the next section.
4699 @end table
4700
4701 @item
4702
4703 @table @asis
4704
4705 @item @emph{infile}
4706
4707 is the full name of the input file, which is an Ada source
4708 file containing preprocessor directives.
4709 @end table
4710
4711 @item
4712
4713 @table @asis
4714
4715 @item @emph{outfile}
4716
4717 is the full name of the output file, which is an Ada source
4718 in standard Ada form. When used with GNAT, this file name will
4719 normally have an @code{ads} or @code{adb} suffix.
4720 @end table
4721
4722 @item
4723
4724 @table @asis
4725
4726 @item @code{deffile}
4727
4728 is the full name of a text file containing definitions of
4729 preprocessing symbols to be referenced by the preprocessor. This argument is
4730 optional, and can be replaced by the use of the @code{-D} switch.
4731 @end table
4732 @end itemize
4733
4734 @node Switches for gnatprep,Form of Definitions File,Using gnatprep,Preprocessing with gnatprep
4735 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatprep}@anchor{aa}@anchor{gnat_ugn/the_gnat_compilation_model id57}@anchor{ab}
4736 @subsubsection Switches for @code{gnatprep}
4737
4738
4739 @geindex --version (gnatprep)
4740
4741
4742 @table @asis
4743
4744 @item @code{--version}
4745
4746 Display Copyright and version, then exit disregarding all other options.
4747 @end table
4748
4749 @geindex --help (gnatprep)
4750
4751
4752 @table @asis
4753
4754 @item @code{--help}
4755
4756 If @code{--version} was not used, display usage and then exit disregarding
4757 all other options.
4758 @end table
4759
4760 @geindex -b (gnatprep)
4761
4762
4763 @table @asis
4764
4765 @item @code{-b}
4766
4767 Causes both preprocessor lines and the lines deleted by
4768 preprocessing to be replaced by blank lines in the output source file,
4769 preserving line numbers in the output file.
4770 @end table
4771
4772 @geindex -c (gnatprep)
4773
4774
4775 @table @asis
4776
4777 @item @code{-c}
4778
4779 Causes both preprocessor lines and the lines deleted
4780 by preprocessing to be retained in the output source as comments marked
4781 with the special string @code{"--! "}. This option will result in line numbers
4782 being preserved in the output file.
4783 @end table
4784
4785 @geindex -C (gnatprep)
4786
4787
4788 @table @asis
4789
4790 @item @code{-C}
4791
4792 Causes comments to be scanned. Normally comments are ignored by gnatprep.
4793 If this option is specified, then comments are scanned and any $symbol
4794 substitutions performed as in program text. This is particularly useful
4795 when structured comments are used (e.g., for programs written in a
4796 pre-2014 version of the SPARK Ada subset). Note that this switch is not
4797 available when doing integrated preprocessing (it would be useless in
4798 this context since comments are ignored by the compiler in any case).
4799 @end table
4800
4801 @geindex -D (gnatprep)
4802
4803
4804 @table @asis
4805
4806 @item @code{-D@emph{symbol}[=@emph{value}]}
4807
4808 Defines a new preprocessing symbol with the specified value. If no value is given
4809 on the command line, then symbol is considered to be @code{True}. This switch
4810 can be used in place of a definition file.
4811 @end table
4812
4813 @geindex -r (gnatprep)
4814
4815
4816 @table @asis
4817
4818 @item @code{-r}
4819
4820 Causes a @code{Source_Reference} pragma to be generated that
4821 references the original input file, so that error messages will use
4822 the file name of this original file. The use of this switch implies
4823 that preprocessor lines are not to be removed from the file, so its
4824 use will force @code{-b} mode if @code{-c}
4825 has not been specified explicitly.
4826
4827 Note that if the file to be preprocessed contains multiple units, then
4828 it will be necessary to @code{gnatchop} the output file from
4829 @code{gnatprep}. If a @code{Source_Reference} pragma is present
4830 in the preprocessed file, it will be respected by
4831 @code{gnatchop -r}
4832 so that the final chopped files will correctly refer to the original
4833 input source file for @code{gnatprep}.
4834 @end table
4835
4836 @geindex -s (gnatprep)
4837
4838
4839 @table @asis
4840
4841 @item @code{-s}
4842
4843 Causes a sorted list of symbol names and values to be
4844 listed on the standard output file.
4845 @end table
4846
4847 @geindex -T (gnatprep)
4848
4849
4850 @table @asis
4851
4852 @item @code{-T}
4853
4854 Use LF as line terminators when writing files. By default the line terminator
4855 of the host (LF under unix, CR/LF under Windows) is used.
4856 @end table
4857
4858 @geindex -u (gnatprep)
4859
4860
4861 @table @asis
4862
4863 @item @code{-u}
4864
4865 Causes undefined symbols to be treated as having the value FALSE in the context
4866 of a preprocessor test. In the absence of this option, an undefined symbol in
4867 a @code{#if} or @code{#elsif} test will be treated as an error.
4868 @end table
4869
4870 @geindex -v (gnatprep)
4871
4872
4873 @table @asis
4874
4875 @item @code{-v}
4876
4877 Verbose mode: generates more output about work done.
4878 @end table
4879
4880 Note: if neither @code{-b} nor @code{-c} is present,
4881 then preprocessor lines and
4882 deleted lines are completely removed from the output, unless -r is
4883 specified, in which case -b is assumed.
4884
4885 @node Form of Definitions File,Form of Input Text for gnatprep,Switches for gnatprep,Preprocessing with gnatprep
4886 @anchor{gnat_ugn/the_gnat_compilation_model form-of-definitions-file}@anchor{ac}@anchor{gnat_ugn/the_gnat_compilation_model id58}@anchor{ad}
4887 @subsubsection Form of Definitions File
4888
4889
4890 The definitions file contains lines of the form:
4891
4892 @example
4893 symbol := value
4894 @end example
4895
4896 where @code{symbol} is a preprocessing symbol, and @code{value} is one of the following:
4897
4898
4899 @itemize *
4900
4901 @item
4902 Empty, corresponding to a null substitution,
4903
4904 @item
4905 A string literal using normal Ada syntax, or
4906
4907 @item
4908 Any sequence of characters from the set @{letters, digits, period, underline@}.
4909 @end itemize
4910
4911 Comment lines may also appear in the definitions file, starting with
4912 the usual @code{--},
4913 and comments may be added to the definitions lines.
4914
4915 @node Form of Input Text for gnatprep,,Form of Definitions File,Preprocessing with gnatprep
4916 @anchor{gnat_ugn/the_gnat_compilation_model id59}@anchor{ae}@anchor{gnat_ugn/the_gnat_compilation_model form-of-input-text-for-gnatprep}@anchor{af}
4917 @subsubsection Form of Input Text for @code{gnatprep}
4918
4919
4920 The input text may contain preprocessor conditional inclusion lines,
4921 as well as general symbol substitution sequences.
4922
4923 The preprocessor conditional inclusion commands have the form:
4924
4925 @example
4926 #if <expression> [then]
4927 lines
4928 #elsif <expression> [then]
4929 lines
4930 #elsif <expression> [then]
4931 lines
4932 ...
4933 #else
4934 lines
4935 #end if;
4936 @end example
4937
4938 In this example, <expression> is defined by the following grammar:
4939
4940 @example
4941 <expression> ::= <symbol>
4942 <expression> ::= <symbol> = "<value>"
4943 <expression> ::= <symbol> = <symbol>
4944 <expression> ::= <symbol> = <integer>
4945 <expression> ::= <symbol> > <integer>
4946 <expression> ::= <symbol> >= <integer>
4947 <expression> ::= <symbol> < <integer>
4948 <expression> ::= <symbol> <= <integer>
4949 <expression> ::= <symbol> 'Defined
4950 <expression> ::= not <expression>
4951 <expression> ::= <expression> and <expression>
4952 <expression> ::= <expression> or <expression>
4953 <expression> ::= <expression> and then <expression>
4954 <expression> ::= <expression> or else <expression>
4955 <expression> ::= ( <expression> )
4956 @end example
4957
4958 Note the following restriction: it is not allowed to have "and" or "or"
4959 following "not" in the same expression without parentheses. For example, this
4960 is not allowed:
4961
4962 @example
4963 not X or Y
4964 @end example
4965
4966 This can be expressed instead as one of the following forms:
4967
4968 @example
4969 (not X) or Y
4970 not (X or Y)
4971 @end example
4972
4973 For the first test (<expression> ::= <symbol>) the symbol must have
4974 either the value true or false, that is to say the right-hand of the
4975 symbol definition must be one of the (case-insensitive) literals
4976 @code{True} or @code{False}. If the value is true, then the
4977 corresponding lines are included, and if the value is false, they are
4978 excluded.
4979
4980 When comparing a symbol to an integer, the integer is any non negative
4981 literal integer as defined in the Ada Reference Manual, such as 3, 16#FF# or
4982 2#11#. The symbol value must also be a non negative integer. Integer values
4983 in the range 0 .. 2**31-1 are supported.
4984
4985 The test (<expression> ::= <symbol>'Defined) is true only if
4986 the symbol has been defined in the definition file or by a @code{-D}
4987 switch on the command line. Otherwise, the test is false.
4988
4989 The equality tests are case insensitive, as are all the preprocessor lines.
4990
4991 If the symbol referenced is not defined in the symbol definitions file,
4992 then the effect depends on whether or not switch @code{-u}
4993 is specified. If so, then the symbol is treated as if it had the value
4994 false and the test fails. If this switch is not specified, then
4995 it is an error to reference an undefined symbol. It is also an error to
4996 reference a symbol that is defined with a value other than @code{True}
4997 or @code{False}.
4998
4999 The use of the @code{not} operator inverts the sense of this logical test.
5000 The @code{not} operator cannot be combined with the @code{or} or @code{and}
5001 operators, without parentheses. For example, "if not X or Y then" is not
5002 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
5003
5004 The @code{then} keyword is optional as shown
5005
5006 The @code{#} must be the first non-blank character on a line, but
5007 otherwise the format is free form. Spaces or tabs may appear between
5008 the @code{#} and the keyword. The keywords and the symbols are case
5009 insensitive as in normal Ada code. Comments may be used on a
5010 preprocessor line, but other than that, no other tokens may appear on a
5011 preprocessor line. Any number of @code{elsif} clauses can be present,
5012 including none at all. The @code{else} is optional, as in Ada.
5013
5014 The @code{#} marking the start of a preprocessor line must be the first
5015 non-blank character on the line, i.e., it must be preceded only by
5016 spaces or horizontal tabs.
5017
5018 Symbol substitution outside of preprocessor lines is obtained by using
5019 the sequence:
5020
5021 @example
5022 $symbol
5023 @end example
5024
5025 anywhere within a source line, except in a comment or within a
5026 string literal. The identifier
5027 following the @code{$} must match one of the symbols defined in the symbol
5028 definition file, and the result is to substitute the value of the
5029 symbol in place of @code{$symbol} in the output file.
5030
5031 Note that although the substitution of strings within a string literal
5032 is not possible, it is possible to have a symbol whose defined value is
5033 a string literal. So instead of setting XYZ to @code{hello} and writing:
5034
5035 @example
5036 Header : String := "$XYZ";
5037 @end example
5038
5039 you should set XYZ to @code{"hello"} and write:
5040
5041 @example
5042 Header : String := $XYZ;
5043 @end example
5044
5045 and then the substitution will occur as desired.
5046
5047 @node Integrated Preprocessing,,Preprocessing with gnatprep,Conditional Compilation
5048 @anchor{gnat_ugn/the_gnat_compilation_model id60}@anchor{b0}@anchor{gnat_ugn/the_gnat_compilation_model integrated-preprocessing}@anchor{18}
5049 @subsection Integrated Preprocessing
5050
5051
5052 As noted above, a file to be preprocessed consists of Ada source code
5053 in which preprocessing lines have been inserted. However,
5054 instead of using @code{gnatprep} to explicitly preprocess a file as a separate
5055 step before compilation, you can carry out the preprocessing implicitly
5056 as part of compilation. Such @emph{integrated preprocessing}, which is the common
5057 style with C, is performed when either or both of the following switches
5058 are passed to the compiler:
5059
5060 @quotation
5061
5062
5063 @itemize *
5064
5065 @item
5066 @code{-gnatep}, which specifies the @emph{preprocessor data file}.
5067 This file dictates how the source files will be preprocessed (e.g., which
5068 symbol definition files apply to which sources).
5069
5070 @item
5071 @code{-gnateD}, which defines values for preprocessing symbols.
5072 @end itemize
5073 @end quotation
5074
5075 Integrated preprocessing applies only to Ada source files, it is
5076 not available for configuration pragma files.
5077
5078 With integrated preprocessing, the output from the preprocessor is not,
5079 by default, written to any external file. Instead it is passed
5080 internally to the compiler. To preserve the result of
5081 preprocessing in a file, either run @code{gnatprep}
5082 in standalone mode or else supply the @code{-gnateG} switch
5083 (described below) to the compiler.
5084
5085 When using project files:
5086
5087 @quotation
5088
5089
5090 @itemize *
5091
5092 @item
5093 the builder switch @code{-x} should be used if any Ada source is
5094 compiled with @code{gnatep=}, so that the compiler finds the
5095 @emph{preprocessor data file}.
5096
5097 @item
5098 the preprocessing data file and the symbol definition files should be
5099 located in the source directories of the project.
5100 @end itemize
5101 @end quotation
5102
5103 Note that the @code{gnatmake} switch @code{-m} will almost
5104 always trigger recompilation for sources that are preprocessed,
5105 because @code{gnatmake} cannot compute the checksum of the source after
5106 preprocessing.
5107
5108 The actual preprocessing function is described in detail in
5109 @ref{17,,Preprocessing with gnatprep}. This section explains the switches
5110 that relate to integrated preprocessing.
5111
5112 @geindex -gnatep (gcc)
5113
5114
5115 @table @asis
5116
5117 @item @code{-gnatep=@emph{preprocessor_data_file}}
5118
5119 This switch specifies the file name (without directory
5120 information) of the preprocessor data file. Either place this file
5121 in one of the source directories, or, when using project
5122 files, reference the project file's directory via the
5123 @code{project_name'Project_Dir} project attribute; e.g:
5124
5125 @quotation
5126
5127 @example
5128 project Prj is
5129 package Compiler is
5130 for Switches ("Ada") use
5131 ("-gnatep=" & Prj'Project_Dir & "prep.def");
5132 end Compiler;
5133 end Prj;
5134 @end example
5135 @end quotation
5136
5137 A preprocessor data file is a text file that contains @emph{preprocessor
5138 control lines}. A preprocessor control line directs the preprocessing of
5139 either a particular source file, or, analogous to @code{others} in Ada,
5140 all sources not specified elsewhere in the preprocessor data file.
5141 A preprocessor control line
5142 can optionally identify a @emph{definition file} that assigns values to
5143 preprocessor symbols, as well as a list of switches that relate to
5144 preprocessing.
5145 Empty lines and comments (using Ada syntax) are also permitted, with no
5146 semantic effect.
5147
5148 Here's an example of a preprocessor data file:
5149
5150 @quotation
5151
5152 @example
5153 "toto.adb" "prep.def" -u
5154 -- Preprocess toto.adb, using definition file prep.def
5155 -- Undefined symbols are treated as False
5156
5157 * -c -DVERSION=V101
5158 -- Preprocess all other sources without using a definition file
5159 -- Suppressed lined are commented
5160 -- Symbol VERSION has the value V101
5161
5162 "tata.adb" "prep2.def" -s
5163 -- Preprocess tata.adb, using definition file prep2.def
5164 -- List all symbols with their values
5165 @end example
5166 @end quotation
5167
5168 A preprocessor control line has the following syntax:
5169
5170 @quotation
5171
5172 @example
5173 <preprocessor_control_line> ::=
5174 <preprocessor_input> [ <definition_file_name> ] @{ <switch> @}
5175
5176 <preprocessor_input> ::= <source_file_name> | '*'
5177
5178 <definition_file_name> ::= <string_literal>
5179
5180 <source_file_name> := <string_literal>
5181
5182 <switch> := (See below for list)
5183 @end example
5184 @end quotation
5185
5186 Thus each preprocessor control line starts with either a literal string or
5187 the character '*':
5188
5189
5190 @itemize *
5191
5192 @item
5193 A literal string is the file name (without directory information) of the source
5194 file that will be input to the preprocessor.
5195
5196 @item
5197 The character '*' is a wild-card indicator; the additional parameters on the line
5198 indicate the preprocessing for all the sources
5199 that are not specified explicitly on other lines (the order of the lines is not
5200 significant).
5201 @end itemize
5202
5203 It is an error to have two lines with the same file name or two
5204 lines starting with the character '*'.
5205
5206 After the file name or '*', an optional literal string specifies the name of
5207 the definition file to be used for preprocessing
5208 (@ref{ac,,Form of Definitions File}). The definition files are found by the
5209 compiler in one of the source directories. In some cases, when compiling
5210 a source in a directory other than the current directory, if the definition
5211 file is in the current directory, it may be necessary to add the current
5212 directory as a source directory through the @code{-I} switch; otherwise
5213 the compiler would not find the definition file.
5214
5215 Finally, switches similar to those of @code{gnatprep} may optionally appear:
5216
5217
5218 @table @asis
5219
5220 @item @code{-b}
5221
5222 Causes both preprocessor lines and the lines deleted by
5223 preprocessing to be replaced by blank lines, preserving the line number.
5224 This switch is always implied; however, if specified after @code{-c}
5225 it cancels the effect of @code{-c}.
5226
5227 @item @code{-c}
5228
5229 Causes both preprocessor lines and the lines deleted
5230 by preprocessing to be retained as comments marked
5231 with the special string '@cite{--!}'.
5232
5233 @item @code{-D@emph{symbol}=@emph{new_value}}
5234
5235 Define or redefine @code{symbol} to have @code{new_value} as its value.
5236 The permitted form for @code{symbol} is either an Ada identifier, or any Ada reserved word
5237 aside from @code{if},
5238 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5239 The permitted form for @code{new_value} is a literal string, an Ada identifier or any Ada reserved
5240 word. A symbol declared with this switch replaces a symbol with the
5241 same name defined in a definition file.
5242
5243 @item @code{-s}
5244
5245 Causes a sorted list of symbol names and values to be
5246 listed on the standard output file.
5247
5248 @item @code{-u}
5249
5250 Causes undefined symbols to be treated as having the value @code{FALSE}
5251 in the context
5252 of a preprocessor test. In the absence of this option, an undefined symbol in
5253 a @code{#if} or @code{#elsif} test will be treated as an error.
5254 @end table
5255 @end table
5256
5257 @geindex -gnateD (gcc)
5258
5259
5260 @table @asis
5261
5262 @item @code{-gnateD@emph{symbol}[=@emph{new_value}]}
5263
5264 Define or redefine @code{symbol} to have @code{new_value} as its value. If no value
5265 is supplied, then the value of @code{symbol} is @code{True}.
5266 The form of @code{symbol} is an identifier, following normal Ada (case-insensitive)
5267 rules for its syntax, and @code{new_value} is either an arbitrary string between double
5268 quotes or any sequence (including an empty sequence) of characters from the
5269 set (letters, digits, period, underline).
5270 Ada reserved words may be used as symbols, with the exceptions of @code{if},
5271 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5272
5273 Examples:
5274
5275 @quotation
5276
5277 @example
5278 -gnateDToto=Tata
5279 -gnateDFoo
5280 -gnateDFoo=\"Foo-Bar\"
5281 @end example
5282 @end quotation
5283
5284 A symbol declared with this switch on the command line replaces a
5285 symbol with the same name either in a definition file or specified with a
5286 switch @code{-D} in the preprocessor data file.
5287
5288 This switch is similar to switch @code{-D} of @code{gnatprep}.
5289
5290 @item @code{-gnateG}
5291
5292 When integrated preprocessing is performed on source file @code{filename.extension},
5293 create or overwrite @code{filename.extension.prep} to contain
5294 the result of the preprocessing.
5295 For example if the source file is @code{foo.adb} then
5296 the output file will be @code{foo.adb.prep}.
5297 @end table
5298
5299 @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model
5300 @anchor{gnat_ugn/the_gnat_compilation_model mixed-language-programming}@anchor{44}@anchor{gnat_ugn/the_gnat_compilation_model id61}@anchor{b1}
5301 @section Mixed Language Programming
5302
5303
5304 @geindex Mixed Language Programming
5305
5306 This section describes how to develop a mixed-language program,
5307 with a focus on combining Ada with C or C++.
5308
5309 @menu
5310 * Interfacing to C::
5311 * Calling Conventions::
5312 * Building Mixed Ada and C++ Programs::
5313 * Generating Ada Bindings for C and C++ headers::
5314 * Generating C Headers for Ada Specifications::
5315
5316 @end menu
5317
5318 @node Interfacing to C,Calling Conventions,,Mixed Language Programming
5319 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-to-c}@anchor{b2}@anchor{gnat_ugn/the_gnat_compilation_model id62}@anchor{b3}
5320 @subsection Interfacing to C
5321
5322
5323 Interfacing Ada with a foreign language such as C involves using
5324 compiler directives to import and/or export entity definitions in each
5325 language -- using @code{extern} statements in C, for instance, and the
5326 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
5327 A full treatment of these topics is provided in Appendix B, section 1
5328 of the Ada Reference Manual.
5329
5330 There are two ways to build a program using GNAT that contains some Ada
5331 sources and some foreign language sources, depending on whether or not
5332 the main subprogram is written in Ada. Here is a source example with
5333 the main subprogram in Ada:
5334
5335 @example
5336 /* file1.c */
5337 #include <stdio.h>
5338
5339 void print_num (int num)
5340 @{
5341 printf ("num is %d.\\n", num);
5342 return;
5343 @}
5344 @end example
5345
5346 @example
5347 /* file2.c */
5348
5349 /* num_from_Ada is declared in my_main.adb */
5350 extern int num_from_Ada;
5351
5352 int get_num (void)
5353 @{
5354 return num_from_Ada;
5355 @}
5356 @end example
5357
5358 @example
5359 -- my_main.adb
5360 procedure My_Main is
5361
5362 -- Declare then export an Integer entity called num_from_Ada
5363 My_Num : Integer := 10;
5364 pragma Export (C, My_Num, "num_from_Ada");
5365
5366 -- Declare an Ada function spec for Get_Num, then use
5367 -- C function get_num for the implementation.
5368 function Get_Num return Integer;
5369 pragma Import (C, Get_Num, "get_num");
5370
5371 -- Declare an Ada procedure spec for Print_Num, then use
5372 -- C function print_num for the implementation.
5373 procedure Print_Num (Num : Integer);
5374 pragma Import (C, Print_Num, "print_num");
5375
5376 begin
5377 Print_Num (Get_Num);
5378 end My_Main;
5379 @end example
5380
5381 To build this example:
5382
5383
5384 @itemize *
5385
5386 @item
5387 First compile the foreign language files to
5388 generate object files:
5389
5390 @example
5391 $ gcc -c file1.c
5392 $ gcc -c file2.c
5393 @end example
5394
5395 @item
5396 Then, compile the Ada units to produce a set of object files and ALI
5397 files:
5398
5399 @example
5400 $ gnatmake -c my_main.adb
5401 @end example
5402
5403 @item
5404 Run the Ada binder on the Ada main program:
5405
5406 @example
5407 $ gnatbind my_main.ali
5408 @end example
5409
5410 @item
5411 Link the Ada main program, the Ada objects and the other language
5412 objects:
5413
5414 @example
5415 $ gnatlink my_main.ali file1.o file2.o
5416 @end example
5417 @end itemize
5418
5419 The last three steps can be grouped in a single command:
5420
5421 @example
5422 $ gnatmake my_main.adb -largs file1.o file2.o
5423 @end example
5424
5425 @geindex Binder output file
5426
5427 If the main program is in a language other than Ada, then you may have
5428 more than one entry point into the Ada subsystem. You must use a special
5429 binder option to generate callable routines that initialize and
5430 finalize the Ada units (@ref{b4,,Binding with Non-Ada Main Programs}).
5431 Calls to the initialization and finalization routines must be inserted
5432 in the main program, or some other appropriate point in the code. The
5433 call to initialize the Ada units must occur before the first Ada
5434 subprogram is called, and the call to finalize the Ada units must occur
5435 after the last Ada subprogram returns. The binder will place the
5436 initialization and finalization subprograms into the
5437 @code{b~xxx.adb} file where they can be accessed by your C
5438 sources. To illustrate, we have the following example:
5439
5440 @example
5441 /* main.c */
5442 extern void adainit (void);
5443 extern void adafinal (void);
5444 extern int add (int, int);
5445 extern int sub (int, int);
5446
5447 int main (int argc, char *argv[])
5448 @{
5449 int a = 21, b = 7;
5450
5451 adainit();
5452
5453 /* Should print "21 + 7 = 28" */
5454 printf ("%d + %d = %d\\n", a, b, add (a, b));
5455
5456 /* Should print "21 - 7 = 14" */
5457 printf ("%d - %d = %d\\n", a, b, sub (a, b));
5458
5459 adafinal();
5460 @}
5461 @end example
5462
5463 @example
5464 -- unit1.ads
5465 package Unit1 is
5466 function Add (A, B : Integer) return Integer;
5467 pragma Export (C, Add, "add");
5468 end Unit1;
5469 @end example
5470
5471 @example
5472 -- unit1.adb
5473 package body Unit1 is
5474 function Add (A, B : Integer) return Integer is
5475 begin
5476 return A + B;
5477 end Add;
5478 end Unit1;
5479 @end example
5480
5481 @example
5482 -- unit2.ads
5483 package Unit2 is
5484 function Sub (A, B : Integer) return Integer;
5485 pragma Export (C, Sub, "sub");
5486 end Unit2;
5487 @end example
5488
5489 @example
5490 -- unit2.adb
5491 package body Unit2 is
5492 function Sub (A, B : Integer) return Integer is
5493 begin
5494 return A - B;
5495 end Sub;
5496 end Unit2;
5497 @end example
5498
5499 The build procedure for this application is similar to the last
5500 example's:
5501
5502
5503 @itemize *
5504
5505 @item
5506 First, compile the foreign language files to generate object files:
5507
5508 @example
5509 $ gcc -c main.c
5510 @end example
5511
5512 @item
5513 Next, compile the Ada units to produce a set of object files and ALI
5514 files:
5515
5516 @example
5517 $ gnatmake -c unit1.adb
5518 $ gnatmake -c unit2.adb
5519 @end example
5520
5521 @item
5522 Run the Ada binder on every generated ALI file. Make sure to use the
5523 @code{-n} option to specify a foreign main program:
5524
5525 @example
5526 $ gnatbind -n unit1.ali unit2.ali
5527 @end example
5528
5529 @item
5530 Link the Ada main program, the Ada objects and the foreign language
5531 objects. You need only list the last ALI file here:
5532
5533 @example
5534 $ gnatlink unit2.ali main.o -o exec_file
5535 @end example
5536
5537 This procedure yields a binary executable called @code{exec_file}.
5538 @end itemize
5539
5540 Depending on the circumstances (for example when your non-Ada main object
5541 does not provide symbol @code{main}), you may also need to instruct the
5542 GNAT linker not to include the standard startup objects by passing the
5543 @code{-nostartfiles} switch to @code{gnatlink}.
5544
5545 @node Calling Conventions,Building Mixed Ada and C++ Programs,Interfacing to C,Mixed Language Programming
5546 @anchor{gnat_ugn/the_gnat_compilation_model calling-conventions}@anchor{b5}@anchor{gnat_ugn/the_gnat_compilation_model id63}@anchor{b6}
5547 @subsection Calling Conventions
5548
5549
5550 @geindex Foreign Languages
5551
5552 @geindex Calling Conventions
5553
5554 GNAT follows standard calling sequence conventions and will thus interface
5555 to any other language that also follows these conventions. The following
5556 Convention identifiers are recognized by GNAT:
5557
5558 @geindex Interfacing to Ada
5559
5560 @geindex Other Ada compilers
5561
5562 @geindex Convention Ada
5563
5564
5565 @table @asis
5566
5567 @item @code{Ada}
5568
5569 This indicates that the standard Ada calling sequence will be
5570 used and all Ada data items may be passed without any limitations in the
5571 case where GNAT is used to generate both the caller and callee. It is also
5572 possible to mix GNAT generated code and code generated by another Ada
5573 compiler. In this case, the data types should be restricted to simple
5574 cases, including primitive types. Whether complex data types can be passed
5575 depends on the situation. Probably it is safe to pass simple arrays, such
5576 as arrays of integers or floats. Records may or may not work, depending
5577 on whether both compilers lay them out identically. Complex structures
5578 involving variant records, access parameters, tasks, or protected types,
5579 are unlikely to be able to be passed.
5580
5581 Note that in the case of GNAT running
5582 on a platform that supports HP Ada 83, a higher degree of compatibility
5583 can be guaranteed, and in particular records are laid out in an identical
5584 manner in the two compilers. Note also that if output from two different
5585 compilers is mixed, the program is responsible for dealing with elaboration
5586 issues. Probably the safest approach is to write the main program in the
5587 version of Ada other than GNAT, so that it takes care of its own elaboration
5588 requirements, and then call the GNAT-generated adainit procedure to ensure
5589 elaboration of the GNAT components. Consult the documentation of the other
5590 Ada compiler for further details on elaboration.
5591
5592 However, it is not possible to mix the tasking run time of GNAT and
5593 HP Ada 83, All the tasking operations must either be entirely within
5594 GNAT compiled sections of the program, or entirely within HP Ada 83
5595 compiled sections of the program.
5596 @end table
5597
5598 @geindex Interfacing to Assembly
5599
5600 @geindex Convention Assembler
5601
5602
5603 @table @asis
5604
5605 @item @code{Assembler}
5606
5607 Specifies assembler as the convention. In practice this has the
5608 same effect as convention Ada (but is not equivalent in the sense of being
5609 considered the same convention).
5610 @end table
5611
5612 @geindex Convention Asm
5613
5614 @geindex Asm
5615
5616
5617 @table @asis
5618
5619 @item @code{Asm}
5620
5621 Equivalent to Assembler.
5622
5623 @geindex Interfacing to COBOL
5624
5625 @geindex Convention COBOL
5626 @end table
5627
5628 @geindex COBOL
5629
5630
5631 @table @asis
5632
5633 @item @code{COBOL}
5634
5635 Data will be passed according to the conventions described
5636 in section B.4 of the Ada Reference Manual.
5637 @end table
5638
5639 @geindex C
5640
5641 @geindex Interfacing to C
5642
5643 @geindex Convention C
5644
5645
5646 @table @asis
5647
5648 @item @code{C}
5649
5650 Data will be passed according to the conventions described
5651 in section B.3 of the Ada Reference Manual.
5652
5653 A note on interfacing to a C 'varargs' function:
5654
5655 @quotation
5656
5657 @geindex C varargs function
5658
5659 @geindex Interfacing to C varargs function
5660
5661 @geindex varargs function interfaces
5662
5663 In C, @code{varargs} allows a function to take a variable number of
5664 arguments. There is no direct equivalent in this to Ada. One
5665 approach that can be used is to create a C wrapper for each
5666 different profile and then interface to this C wrapper. For
5667 example, to print an @code{int} value using @code{printf},
5668 create a C function @code{printfi} that takes two arguments, a
5669 pointer to a string and an int, and calls @code{printf}.
5670 Then in the Ada program, use pragma @code{Import} to
5671 interface to @code{printfi}.
5672
5673 It may work on some platforms to directly interface to
5674 a @code{varargs} function by providing a specific Ada profile
5675 for a particular call. However, this does not work on
5676 all platforms, since there is no guarantee that the
5677 calling sequence for a two argument normal C function
5678 is the same as for calling a @code{varargs} C function with
5679 the same two arguments.
5680 @end quotation
5681 @end table
5682
5683 @geindex Convention Default
5684
5685 @geindex Default
5686
5687
5688 @table @asis
5689
5690 @item @code{Default}
5691
5692 Equivalent to C.
5693 @end table
5694
5695 @geindex Convention External
5696
5697 @geindex External
5698
5699
5700 @table @asis
5701
5702 @item @code{External}
5703
5704 Equivalent to C.
5705 @end table
5706
5707 @geindex C++
5708
5709 @geindex Interfacing to C++
5710
5711 @geindex Convention C++
5712
5713
5714 @table @asis
5715
5716 @item @code{C_Plus_Plus} (or @code{CPP})
5717
5718 This stands for C++. For most purposes this is identical to C.
5719 See the separate description of the specialized GNAT pragmas relating to
5720 C++ interfacing for further details.
5721 @end table
5722
5723 @geindex Fortran
5724
5725 @geindex Interfacing to Fortran
5726
5727 @geindex Convention Fortran
5728
5729
5730 @table @asis
5731
5732 @item @code{Fortran}
5733
5734 Data will be passed according to the conventions described
5735 in section B.5 of the Ada Reference Manual.
5736
5737 @item @code{Intrinsic}
5738
5739 This applies to an intrinsic operation, as defined in the Ada
5740 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
5741 this means that the body of the subprogram is provided by the compiler itself,
5742 usually by means of an efficient code sequence, and that the user does not
5743 supply an explicit body for it. In an application program, the pragma may
5744 be applied to the following sets of names:
5745
5746
5747 @itemize *
5748
5749 @item
5750 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic.
5751 The corresponding subprogram declaration must have
5752 two formal parameters. The
5753 first one must be a signed integer type or a modular type with a binary
5754 modulus, and the second parameter must be of type Natural.
5755 The return type must be the same as the type of the first argument. The size
5756 of this type can only be 8, 16, 32, or 64.
5757
5758 @item
5759 Binary arithmetic operators: '+', '-', '*', '/'.
5760 The corresponding operator declaration must have parameters and result type
5761 that have the same root numeric type (for example, all three are long_float
5762 types). This simplifies the definition of operations that use type checking
5763 to perform dimensional checks:
5764 @end itemize
5765
5766 @example
5767 type Distance is new Long_Float;
5768 type Time is new Long_Float;
5769 type Velocity is new Long_Float;
5770 function "/" (D : Distance; T : Time)
5771 return Velocity;
5772 pragma Import (Intrinsic, "/");
5773
5774 This common idiom is often programmed with a generic definition and an
5775 explicit body. The pragma makes it simpler to introduce such declarations.
5776 It incurs no overhead in compilation time or code size, because it is
5777 implemented as a single machine instruction.
5778 @end example
5779
5780
5781 @itemize *
5782
5783 @item
5784 General subprogram entities. This is used to bind an Ada subprogram
5785 declaration to
5786 a compiler builtin by name with back-ends where such interfaces are
5787 available. A typical example is the set of @code{__builtin} functions
5788 exposed by the GCC back-end, as in the following example:
5789
5790 @example
5791 function builtin_sqrt (F : Float) return Float;
5792 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
5793 @end example
5794
5795 Most of the GCC builtins are accessible this way, and as for other
5796 import conventions (e.g. C), it is the user's responsibility to ensure
5797 that the Ada subprogram profile matches the underlying builtin
5798 expectations.
5799 @end itemize
5800 @end table
5801
5802 @geindex Stdcall
5803
5804 @geindex Convention Stdcall
5805
5806
5807 @table @asis
5808
5809 @item @code{Stdcall}
5810
5811 This is relevant only to Windows implementations of GNAT,
5812 and specifies that the @code{Stdcall} calling sequence will be used,
5813 as defined by the NT API. Nevertheless, to ease building
5814 cross-platform bindings this convention will be handled as a @code{C} calling
5815 convention on non-Windows platforms.
5816 @end table
5817
5818 @geindex DLL
5819
5820 @geindex Convention DLL
5821
5822
5823 @table @asis
5824
5825 @item @code{DLL}
5826
5827 This is equivalent to @code{Stdcall}.
5828 @end table
5829
5830 @geindex Win32
5831
5832 @geindex Convention Win32
5833
5834
5835 @table @asis
5836
5837 @item @code{Win32}
5838
5839 This is equivalent to @code{Stdcall}.
5840 @end table
5841
5842 @geindex Stubbed
5843
5844 @geindex Convention Stubbed
5845
5846
5847 @table @asis
5848
5849 @item @code{Stubbed}
5850
5851 This is a special convention that indicates that the compiler
5852 should provide a stub body that raises @code{Program_Error}.
5853 @end table
5854
5855 GNAT additionally provides a useful pragma @code{Convention_Identifier}
5856 that can be used to parameterize conventions and allow additional synonyms
5857 to be specified. For example if you have legacy code in which the convention
5858 identifier Fortran77 was used for Fortran, you can use the configuration
5859 pragma:
5860
5861 @example
5862 pragma Convention_Identifier (Fortran77, Fortran);
5863 @end example
5864
5865 And from now on the identifier Fortran77 may be used as a convention
5866 identifier (for example in an @code{Import} pragma) with the same
5867 meaning as Fortran.
5868
5869 @node Building Mixed Ada and C++ Programs,Generating Ada Bindings for C and C++ headers,Calling Conventions,Mixed Language Programming
5870 @anchor{gnat_ugn/the_gnat_compilation_model id64}@anchor{b7}@anchor{gnat_ugn/the_gnat_compilation_model building-mixed-ada-and-c-programs}@anchor{b8}
5871 @subsection Building Mixed Ada and C++ Programs
5872
5873
5874 A programmer inexperienced with mixed-language development may find that
5875 building an application containing both Ada and C++ code can be a
5876 challenge. This section gives a few hints that should make this task easier.
5877
5878 @menu
5879 * Interfacing to C++::
5880 * Linking a Mixed C++ & Ada Program::
5881 * A Simple Example::
5882 * Interfacing with C++ constructors::
5883 * Interfacing with C++ at the Class Level::
5884
5885 @end menu
5886
5887 @node Interfacing to C++,Linking a Mixed C++ & Ada Program,,Building Mixed Ada and C++ Programs
5888 @anchor{gnat_ugn/the_gnat_compilation_model id65}@anchor{b9}@anchor{gnat_ugn/the_gnat_compilation_model id66}@anchor{ba}
5889 @subsubsection Interfacing to C++
5890
5891
5892 GNAT supports interfacing with the G++ compiler (or any C++ compiler
5893 generating code that is compatible with the G++ Application Binary
5894 Interface ---see @indicateurl{http://www.codesourcery.com/archives/cxx-abi}).
5895
5896 Interfacing can be done at 3 levels: simple data, subprograms, and
5897 classes. In the first two cases, GNAT offers a specific @code{Convention C_Plus_Plus}
5898 (or @code{CPP}) that behaves exactly like @code{Convention C}.
5899 Usually, C++ mangles the names of subprograms. To generate proper mangled
5900 names automatically, see @ref{19,,Generating Ada Bindings for C and C++ headers}).
5901 This problem can also be addressed manually in two ways:
5902
5903
5904 @itemize *
5905
5906 @item
5907 by modifying the C++ code in order to force a C convention using
5908 the @code{extern "C"} syntax.
5909
5910 @item
5911 by figuring out the mangled name (using e.g. @code{nm}) and using it as the
5912 Link_Name argument of the pragma import.
5913 @end itemize
5914
5915 Interfacing at the class level can be achieved by using the GNAT specific
5916 pragmas such as @code{CPP_Constructor}. See the @cite{GNAT_Reference_Manual} for additional information.
5917
5918 @node Linking a Mixed C++ & Ada Program,A Simple Example,Interfacing to C++,Building Mixed Ada and C++ Programs
5919 @anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-ada-program}@anchor{bb}@anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-and-ada-program}@anchor{bc}
5920 @subsubsection Linking a Mixed C++ & Ada Program
5921
5922
5923 Usually the linker of the C++ development system must be used to link
5924 mixed applications because most C++ systems will resolve elaboration
5925 issues (such as calling constructors on global class instances)
5926 transparently during the link phase. GNAT has been adapted to ease the
5927 use of a foreign linker for the last phase. Three cases can be
5928 considered:
5929
5930
5931 @itemize *
5932
5933 @item
5934 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
5935 The C++ linker can simply be called by using the C++ specific driver
5936 called @code{g++}.
5937
5938 Note that if the C++ code uses inline functions, you will need to
5939 compile your C++ code with the @code{-fkeep-inline-functions} switch in
5940 order to provide an existing function implementation that the Ada code can
5941 link with.
5942
5943 @example
5944 $ g++ -c -fkeep-inline-functions file1.C
5945 $ g++ -c -fkeep-inline-functions file2.C
5946 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
5947 @end example
5948
5949 @item
5950 Using GNAT and G++ from two different GCC installations: If both
5951 compilers are on the :envvar`PATH`, the previous method may be used. It is
5952 important to note that environment variables such as
5953 @geindex C_INCLUDE_PATH
5954 @geindex environment variable; C_INCLUDE_PATH
5955 @code{C_INCLUDE_PATH},
5956 @geindex GCC_EXEC_PREFIX
5957 @geindex environment variable; GCC_EXEC_PREFIX
5958 @code{GCC_EXEC_PREFIX},
5959 @geindex BINUTILS_ROOT
5960 @geindex environment variable; BINUTILS_ROOT
5961 @code{BINUTILS_ROOT}, and
5962 @geindex GCC_ROOT
5963 @geindex environment variable; GCC_ROOT
5964 @code{GCC_ROOT} will affect both compilers
5965 at the same time and may make one of the two compilers operate
5966 improperly if set during invocation of the wrong compiler. It is also
5967 very important that the linker uses the proper @code{libgcc.a} GCC
5968 library -- that is, the one from the C++ compiler installation. The
5969 implicit link command as suggested in the @code{gnatmake} command
5970 from the former example can be replaced by an explicit link command with
5971 the full-verbosity option in order to verify which library is used:
5972
5973 @example
5974 $ gnatbind ada_unit
5975 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
5976 @end example
5977
5978 If there is a problem due to interfering environment variables, it can
5979 be worked around by using an intermediate script. The following example
5980 shows the proper script to use when GNAT has not been installed at its
5981 default location and g++ has been installed at its default location:
5982
5983 @example
5984 $ cat ./my_script
5985 #!/bin/sh
5986 unset BINUTILS_ROOT
5987 unset GCC_ROOT
5988 c++ $*
5989 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
5990 @end example
5991
5992 @item
5993 Using a non-GNU C++ compiler: The commands previously described can be
5994 used to insure that the C++ linker is used. Nonetheless, you need to add
5995 a few more parameters to the link command line, depending on the exception
5996 mechanism used.
5997
5998 If the @code{setjmp} / @code{longjmp} exception mechanism is used, only the paths
5999 to the @code{libgcc} libraries are required:
6000
6001 @example
6002 $ cat ./my_script
6003 #!/bin/sh
6004 CC $* gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a
6005 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6006 @end example
6007
6008 where CC is the name of the non-GNU C++ compiler.
6009
6010 If the "zero cost" exception mechanism is used, and the platform
6011 supports automatic registration of exception tables (e.g., Solaris),
6012 paths to more objects are required:
6013
6014 @example
6015 $ cat ./my_script
6016 #!/bin/sh
6017 CC gcc -print-file-name=crtbegin.o $* \\
6018 gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a \\
6019 gcc -print-file-name=crtend.o
6020 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6021 @end example
6022
6023 If the "zero cost exception" mechanism is used, and the platform
6024 doesn't support automatic registration of exception tables (e.g., HP-UX
6025 or AIX), the simple approach described above will not work and
6026 a pre-linking phase using GNAT will be necessary.
6027 @end itemize
6028
6029 Another alternative is to use the @code{gprbuild} multi-language builder
6030 which has a large knowledge base and knows how to link Ada and C++ code
6031 together automatically in most cases.
6032
6033 @node A Simple Example,Interfacing with C++ constructors,Linking a Mixed C++ & Ada Program,Building Mixed Ada and C++ Programs
6034 @anchor{gnat_ugn/the_gnat_compilation_model id67}@anchor{bd}@anchor{gnat_ugn/the_gnat_compilation_model a-simple-example}@anchor{be}
6035 @subsubsection A Simple Example
6036
6037
6038 The following example, provided as part of the GNAT examples, shows how
6039 to achieve procedural interfacing between Ada and C++ in both
6040 directions. The C++ class A has two methods. The first method is exported
6041 to Ada by the means of an extern C wrapper function. The second method
6042 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
6043 a limited record with a layout comparable to the C++ class. The Ada
6044 subprogram, in turn, calls the C++ method. So, starting from the C++
6045 main program, the process passes back and forth between the two
6046 languages.
6047
6048 Here are the compilation commands:
6049
6050 @example
6051 $ gnatmake -c simple_cpp_interface
6052 $ g++ -c cpp_main.C
6053 $ g++ -c ex7.C
6054 $ gnatbind -n simple_cpp_interface
6055 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ ex7.o cpp_main.o
6056 @end example
6057
6058 Here are the corresponding sources:
6059
6060 @example
6061 //cpp_main.C
6062
6063 #include "ex7.h"
6064
6065 extern "C" @{
6066 void adainit (void);
6067 void adafinal (void);
6068 void method1 (A *t);
6069 @}
6070
6071 void method1 (A *t)
6072 @{
6073 t->method1 ();
6074 @}
6075
6076 int main ()
6077 @{
6078 A obj;
6079 adainit ();
6080 obj.method2 (3030);
6081 adafinal ();
6082 @}
6083 @end example
6084
6085 @example
6086 //ex7.h
6087
6088 class Origin @{
6089 public:
6090 int o_value;
6091 @};
6092 class A : public Origin @{
6093 public:
6094 void method1 (void);
6095 void method2 (int v);
6096 A();
6097 int a_value;
6098 @};
6099 @end example
6100
6101 @example
6102 //ex7.C
6103
6104 #include "ex7.h"
6105 #include <stdio.h>
6106
6107 extern "C" @{ void ada_method2 (A *t, int v);@}
6108
6109 void A::method1 (void)
6110 @{
6111 a_value = 2020;
6112 printf ("in A::method1, a_value = %d \\n",a_value);
6113 @}
6114
6115 void A::method2 (int v)
6116 @{
6117 ada_method2 (this, v);
6118 printf ("in A::method2, a_value = %d \\n",a_value);
6119 @}
6120
6121 A::A(void)
6122 @{
6123 a_value = 1010;
6124 printf ("in A::A, a_value = %d \\n",a_value);
6125 @}
6126 @end example
6127
6128 @example
6129 -- simple_cpp_interface.ads
6130 with System;
6131 package Simple_Cpp_Interface is
6132 type A is limited
6133 record
6134 Vptr : System.Address;
6135 O_Value : Integer;
6136 A_Value : Integer;
6137 end record;
6138 pragma Convention (C, A);
6139
6140 procedure Method1 (This : in out A);
6141 pragma Import (C, Method1);
6142
6143 procedure Ada_Method2 (This : in out A; V : Integer);
6144 pragma Export (C, Ada_Method2);
6145
6146 end Simple_Cpp_Interface;
6147 @end example
6148
6149 @example
6150 -- simple_cpp_interface.adb
6151 package body Simple_Cpp_Interface is
6152
6153 procedure Ada_Method2 (This : in out A; V : Integer) is
6154 begin
6155 Method1 (This);
6156 This.A_Value := V;
6157 end Ada_Method2;
6158
6159 end Simple_Cpp_Interface;
6160 @end example
6161
6162 @node Interfacing with C++ constructors,Interfacing with C++ at the Class Level,A Simple Example,Building Mixed Ada and C++ Programs
6163 @anchor{gnat_ugn/the_gnat_compilation_model id68}@anchor{bf}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-constructors}@anchor{c0}
6164 @subsubsection Interfacing with C++ constructors
6165
6166
6167 In order to interface with C++ constructors GNAT provides the
6168 @code{pragma CPP_Constructor} (see the @cite{GNAT_Reference_Manual}
6169 for additional information).
6170 In this section we present some common uses of C++ constructors
6171 in mixed-languages programs in GNAT.
6172
6173 Let us assume that we need to interface with the following
6174 C++ class:
6175
6176 @example
6177 class Root @{
6178 public:
6179 int a_value;
6180 int b_value;
6181 virtual int Get_Value ();
6182 Root(); // Default constructor
6183 Root(int v); // 1st non-default constructor
6184 Root(int v, int w); // 2nd non-default constructor
6185 @};
6186 @end example
6187
6188 For this purpose we can write the following package spec (further
6189 information on how to build this spec is available in
6190 @ref{c1,,Interfacing with C++ at the Class Level} and
6191 @ref{19,,Generating Ada Bindings for C and C++ headers}).
6192
6193 @example
6194 with Interfaces.C; use Interfaces.C;
6195 package Pkg_Root is
6196 type Root is tagged limited record
6197 A_Value : int;
6198 B_Value : int;
6199 end record;
6200 pragma Import (CPP, Root);
6201
6202 function Get_Value (Obj : Root) return int;
6203 pragma Import (CPP, Get_Value);
6204
6205 function Constructor return Root;
6206 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
6207
6208 function Constructor (v : Integer) return Root;
6209 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
6210
6211 function Constructor (v, w : Integer) return Root;
6212 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
6213 end Pkg_Root;
6214 @end example
6215
6216 On the Ada side the constructor is represented by a function (whose
6217 name is arbitrary) that returns the classwide type corresponding to
6218 the imported C++ class. Although the constructor is described as a
6219 function, it is typically a procedure with an extra implicit argument
6220 (the object being initialized) at the implementation level. GNAT
6221 issues the appropriate call, whatever it is, to get the object
6222 properly initialized.
6223
6224 Constructors can only appear in the following contexts:
6225
6226
6227 @itemize *
6228
6229 @item
6230 On the right side of an initialization of an object of type @code{T}.
6231
6232 @item
6233 On the right side of an initialization of a record component of type @code{T}.
6234
6235 @item
6236 In an Ada 2005 limited aggregate.
6237
6238 @item
6239 In an Ada 2005 nested limited aggregate.
6240
6241 @item
6242 In an Ada 2005 limited aggregate that initializes an object built in
6243 place by an extended return statement.
6244 @end itemize
6245
6246 In a declaration of an object whose type is a class imported from C++,
6247 either the default C++ constructor is implicitly called by GNAT, or
6248 else the required C++ constructor must be explicitly called in the
6249 expression that initializes the object. For example:
6250
6251 @example
6252 Obj1 : Root;
6253 Obj2 : Root := Constructor;
6254 Obj3 : Root := Constructor (v => 10);
6255 Obj4 : Root := Constructor (30, 40);
6256 @end example
6257
6258 The first two declarations are equivalent: in both cases the default C++
6259 constructor is invoked (in the former case the call to the constructor is
6260 implicit, and in the latter case the call is explicit in the object
6261 declaration). @code{Obj3} is initialized by the C++ non-default constructor
6262 that takes an integer argument, and @code{Obj4} is initialized by the
6263 non-default C++ constructor that takes two integers.
6264
6265 Let us derive the imported C++ class in the Ada side. For example:
6266
6267 @example
6268 type DT is new Root with record
6269 C_Value : Natural := 2009;
6270 end record;
6271 @end example
6272
6273 In this case the components DT inherited from the C++ side must be
6274 initialized by a C++ constructor, and the additional Ada components
6275 of type DT are initialized by GNAT. The initialization of such an
6276 object is done either by default, or by means of a function returning
6277 an aggregate of type DT, or by means of an extension aggregate.
6278
6279 @example
6280 Obj5 : DT;
6281 Obj6 : DT := Function_Returning_DT (50);
6282 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
6283 @end example
6284
6285 The declaration of @code{Obj5} invokes the default constructors: the
6286 C++ default constructor of the parent type takes care of the initialization
6287 of the components inherited from Root, and GNAT takes care of the default
6288 initialization of the additional Ada components of type DT (that is,
6289 @code{C_Value} is initialized to value 2009). The order of invocation of
6290 the constructors is consistent with the order of elaboration required by
6291 Ada and C++. That is, the constructor of the parent type is always called
6292 before the constructor of the derived type.
6293
6294 Let us now consider a record that has components whose type is imported
6295 from C++. For example:
6296
6297 @example
6298 type Rec1 is limited record
6299 Data1 : Root := Constructor (10);
6300 Value : Natural := 1000;
6301 end record;
6302
6303 type Rec2 (D : Integer := 20) is limited record
6304 Rec : Rec1;
6305 Data2 : Root := Constructor (D, 30);
6306 end record;
6307 @end example
6308
6309 The initialization of an object of type @code{Rec2} will call the
6310 non-default C++ constructors specified for the imported components.
6311 For example:
6312
6313 @example
6314 Obj8 : Rec2 (40);
6315 @end example
6316
6317 Using Ada 2005 we can use limited aggregates to initialize an object
6318 invoking C++ constructors that differ from those specified in the type
6319 declarations. For example:
6320
6321 @example
6322 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
6323 others => <>),
6324 others => <>);
6325 @end example
6326
6327 The above declaration uses an Ada 2005 limited aggregate to
6328 initialize @code{Obj9}, and the C++ constructor that has two integer
6329 arguments is invoked to initialize the @code{Data1} component instead
6330 of the constructor specified in the declaration of type @code{Rec1}. In
6331 Ada 2005 the box in the aggregate indicates that unspecified components
6332 are initialized using the expression (if any) available in the component
6333 declaration. That is, in this case discriminant @code{D} is initialized
6334 to value @code{20}, @code{Value} is initialized to value 1000, and the
6335 non-default C++ constructor that handles two integers takes care of
6336 initializing component @code{Data2} with values @code{20,30}.
6337
6338 In Ada 2005 we can use the extended return statement to build the Ada
6339 equivalent to C++ non-default constructors. For example:
6340
6341 @example
6342 function Constructor (V : Integer) return Rec2 is
6343 begin
6344 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
6345 others => <>),
6346 others => <>) do
6347 -- Further actions required for construction of
6348 -- objects of type Rec2
6349 ...
6350 end record;
6351 end Constructor;
6352 @end example
6353
6354 In this example the extended return statement construct is used to
6355 build in place the returned object whose components are initialized
6356 by means of a limited aggregate. Any further action associated with
6357 the constructor can be placed inside the construct.
6358
6359 @node Interfacing with C++ at the Class Level,,Interfacing with C++ constructors,Building Mixed Ada and C++ Programs
6360 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-at-the-class-level}@anchor{c1}@anchor{gnat_ugn/the_gnat_compilation_model id69}@anchor{c2}
6361 @subsubsection Interfacing with C++ at the Class Level
6362
6363
6364 In this section we demonstrate the GNAT features for interfacing with
6365 C++ by means of an example making use of Ada 2005 abstract interface
6366 types. This example consists of a classification of animals; classes
6367 have been used to model our main classification of animals, and
6368 interfaces provide support for the management of secondary
6369 classifications. We first demonstrate a case in which the types and
6370 constructors are defined on the C++ side and imported from the Ada
6371 side, and latter the reverse case.
6372
6373 The root of our derivation will be the @code{Animal} class, with a
6374 single private attribute (the @code{Age} of the animal), a constructor,
6375 and two public primitives to set and get the value of this attribute.
6376
6377 @example
6378 class Animal @{
6379 public:
6380 virtual void Set_Age (int New_Age);
6381 virtual int Age ();
6382 Animal() @{Age_Count = 0;@};
6383 private:
6384 int Age_Count;
6385 @};
6386 @end example
6387
6388 Abstract interface types are defined in C++ by means of classes with pure
6389 virtual functions and no data members. In our example we will use two
6390 interfaces that provide support for the common management of @code{Carnivore}
6391 and @code{Domestic} animals:
6392
6393 @example
6394 class Carnivore @{
6395 public:
6396 virtual int Number_Of_Teeth () = 0;
6397 @};
6398
6399 class Domestic @{
6400 public:
6401 virtual void Set_Owner (char* Name) = 0;
6402 @};
6403 @end example
6404
6405 Using these declarations, we can now say that a @code{Dog} is an animal that is
6406 both Carnivore and Domestic, that is:
6407
6408 @example
6409 class Dog : Animal, Carnivore, Domestic @{
6410 public:
6411 virtual int Number_Of_Teeth ();
6412 virtual void Set_Owner (char* Name);
6413
6414 Dog(); // Constructor
6415 private:
6416 int Tooth_Count;
6417 char *Owner;
6418 @};
6419 @end example
6420
6421 In the following examples we will assume that the previous declarations are
6422 located in a file named @code{animals.h}. The following package demonstrates
6423 how to import these C++ declarations from the Ada side:
6424
6425 @example
6426 with Interfaces.C.Strings; use Interfaces.C.Strings;
6427 package Animals is
6428 type Carnivore is limited interface;
6429 pragma Convention (C_Plus_Plus, Carnivore);
6430 function Number_Of_Teeth (X : Carnivore)
6431 return Natural is abstract;
6432
6433 type Domestic is limited interface;
6434 pragma Convention (C_Plus_Plus, Domestic);
6435 procedure Set_Owner
6436 (X : in out Domestic;
6437 Name : Chars_Ptr) is abstract;
6438
6439 type Animal is tagged limited record
6440 Age : Natural;
6441 end record;
6442 pragma Import (C_Plus_Plus, Animal);
6443
6444 procedure Set_Age (X : in out Animal; Age : Integer);
6445 pragma Import (C_Plus_Plus, Set_Age);
6446
6447 function Age (X : Animal) return Integer;
6448 pragma Import (C_Plus_Plus, Age);
6449
6450 function New_Animal return Animal;
6451 pragma CPP_Constructor (New_Animal);
6452 pragma Import (CPP, New_Animal, "_ZN6AnimalC1Ev");
6453
6454 type Dog is new Animal and Carnivore and Domestic with record
6455 Tooth_Count : Natural;
6456 Owner : Chars_Ptr;
6457 end record;
6458 pragma Import (C_Plus_Plus, Dog);
6459
6460 function Number_Of_Teeth (A : Dog) return Natural;
6461 pragma Import (C_Plus_Plus, Number_Of_Teeth);
6462
6463 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6464 pragma Import (C_Plus_Plus, Set_Owner);
6465
6466 function New_Dog return Dog;
6467 pragma CPP_Constructor (New_Dog);
6468 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
6469 end Animals;
6470 @end example
6471
6472 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
6473 interfacing with these C++ classes is easy. The only requirement is that all
6474 the primitives and components must be declared exactly in the same order in
6475 the two languages.
6476
6477 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
6478 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
6479 the arguments to the called primitives will be the same as for C++. For the
6480 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
6481 to indicate that they have been defined on the C++ side; this is required
6482 because the dispatch table associated with these tagged types will be built
6483 in the C++ side and therefore will not contain the predefined Ada primitives
6484 which Ada would otherwise expect.
6485
6486 As the reader can see there is no need to indicate the C++ mangled names
6487 associated with each subprogram because it is assumed that all the calls to
6488 these primitives will be dispatching calls. The only exception is the
6489 constructor, which must be registered with the compiler by means of
6490 @code{pragma CPP_Constructor} and needs to provide its associated C++
6491 mangled name because the Ada compiler generates direct calls to it.
6492
6493 With the above packages we can now declare objects of type Dog on the Ada side
6494 and dispatch calls to the corresponding subprograms on the C++ side. We can
6495 also extend the tagged type Dog with further fields and primitives, and
6496 override some of its C++ primitives on the Ada side. For example, here we have
6497 a type derivation defined on the Ada side that inherits all the dispatching
6498 primitives of the ancestor from the C++ side.
6499
6500 @example
6501 with Animals; use Animals;
6502 package Vaccinated_Animals is
6503 type Vaccinated_Dog is new Dog with null record;
6504 function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
6505 end Vaccinated_Animals;
6506 @end example
6507
6508 It is important to note that, because of the ABI compatibility, the programmer
6509 does not need to add any further information to indicate either the object
6510 layout or the dispatch table entry associated with each dispatching operation.
6511
6512 Now let us define all the types and constructors on the Ada side and export
6513 them to C++, using the same hierarchy of our previous example:
6514
6515 @example
6516 with Interfaces.C.Strings;
6517 use Interfaces.C.Strings;
6518 package Animals is
6519 type Carnivore is limited interface;
6520 pragma Convention (C_Plus_Plus, Carnivore);
6521 function Number_Of_Teeth (X : Carnivore)
6522 return Natural is abstract;
6523
6524 type Domestic is limited interface;
6525 pragma Convention (C_Plus_Plus, Domestic);
6526 procedure Set_Owner
6527 (X : in out Domestic;
6528 Name : Chars_Ptr) is abstract;
6529
6530 type Animal is tagged record
6531 Age : Natural;
6532 end record;
6533 pragma Convention (C_Plus_Plus, Animal);
6534
6535 procedure Set_Age (X : in out Animal; Age : Integer);
6536 pragma Export (C_Plus_Plus, Set_Age);
6537
6538 function Age (X : Animal) return Integer;
6539 pragma Export (C_Plus_Plus, Age);
6540
6541 function New_Animal return Animal'Class;
6542 pragma Export (C_Plus_Plus, New_Animal);
6543
6544 type Dog is new Animal and Carnivore and Domestic with record
6545 Tooth_Count : Natural;
6546 Owner : String (1 .. 30);
6547 end record;
6548 pragma Convention (C_Plus_Plus, Dog);
6549
6550 function Number_Of_Teeth (A : Dog) return Natural;
6551 pragma Export (C_Plus_Plus, Number_Of_Teeth);
6552
6553 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6554 pragma Export (C_Plus_Plus, Set_Owner);
6555
6556 function New_Dog return Dog'Class;
6557 pragma Export (C_Plus_Plus, New_Dog);
6558 end Animals;
6559 @end example
6560
6561 Compared with our previous example the only differences are the use of
6562 @code{pragma Convention} (instead of @code{pragma Import}), and the use of
6563 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
6564 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
6565 nothing else to be done; as explained above, the only requirement is that all
6566 the primitives and components are declared in exactly the same order.
6567
6568 For completeness, let us see a brief C++ main program that uses the
6569 declarations available in @code{animals.h} (presented in our first example) to
6570 import and use the declarations from the Ada side, properly initializing and
6571 finalizing the Ada run-time system along the way:
6572
6573 @example
6574 #include "animals.h"
6575 #include <iostream>
6576 using namespace std;
6577
6578 void Check_Carnivore (Carnivore *obj) @{...@}
6579 void Check_Domestic (Domestic *obj) @{...@}
6580 void Check_Animal (Animal *obj) @{...@}
6581 void Check_Dog (Dog *obj) @{...@}
6582
6583 extern "C" @{
6584 void adainit (void);
6585 void adafinal (void);
6586 Dog* new_dog ();
6587 @}
6588
6589 void test ()
6590 @{
6591 Dog *obj = new_dog(); // Ada constructor
6592 Check_Carnivore (obj); // Check secondary DT
6593 Check_Domestic (obj); // Check secondary DT
6594 Check_Animal (obj); // Check primary DT
6595 Check_Dog (obj); // Check primary DT
6596 @}
6597
6598 int main ()
6599 @{
6600 adainit (); test(); adafinal ();
6601 return 0;
6602 @}
6603 @end example
6604
6605 @node Generating Ada Bindings for C and C++ headers,Generating C Headers for Ada Specifications,Building Mixed Ada and C++ Programs,Mixed Language Programming
6606 @anchor{gnat_ugn/the_gnat_compilation_model id70}@anchor{c3}@anchor{gnat_ugn/the_gnat_compilation_model generating-ada-bindings-for-c-and-c-headers}@anchor{19}
6607 @subsection Generating Ada Bindings for C and C++ headers
6608
6609
6610 @geindex Binding generation (for C and C++ headers)
6611
6612 @geindex C headers (binding generation)
6613
6614 @geindex C++ headers (binding generation)
6615
6616 GNAT includes a binding generator for C and C++ headers which is
6617 intended to do 95% of the tedious work of generating Ada specs from C
6618 or C++ header files.
6619
6620 Note that this capability is not intended to generate 100% correct Ada specs,
6621 and will is some cases require manual adjustments, although it can often
6622 be used out of the box in practice.
6623
6624 Some of the known limitations include:
6625
6626
6627 @itemize *
6628
6629 @item
6630 only very simple character constant macros are translated into Ada
6631 constants. Function macros (macros with arguments) are partially translated
6632 as comments, to be completed manually if needed.
6633
6634 @item
6635 some extensions (e.g. vector types) are not supported
6636
6637 @item
6638 pointers to pointers or complex structures are mapped to System.Address
6639
6640 @item
6641 identifiers with identical name (except casing) will generate compilation
6642 errors (e.g. @code{shm_get} vs @code{SHM_GET}).
6643 @end itemize
6644
6645 The code is generated using Ada 2012 syntax, which makes it easier to interface
6646 with other languages. In most cases you can still use the generated binding
6647 even if your code is compiled using earlier versions of Ada (e.g. @code{-gnat95}).
6648
6649 @menu
6650 * Running the Binding Generator::
6651 * Generating Bindings for C++ Headers::
6652 * Switches::
6653
6654 @end menu
6655
6656 @node Running the Binding Generator,Generating Bindings for C++ Headers,,Generating Ada Bindings for C and C++ headers
6657 @anchor{gnat_ugn/the_gnat_compilation_model id71}@anchor{c4}@anchor{gnat_ugn/the_gnat_compilation_model running-the-binding-generator}@anchor{c5}
6658 @subsubsection Running the Binding Generator
6659
6660
6661 The binding generator is part of the @code{gcc} compiler and can be
6662 invoked via the @code{-fdump-ada-spec} switch, which will generate Ada
6663 spec files for the header files specified on the command line, and all
6664 header files needed by these files transitively. For example:
6665
6666 @example
6667 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
6668 $ gcc -c *.ads
6669 @end example
6670
6671 will generate, under GNU/Linux, the following files: @code{time_h.ads},
6672 @code{bits_time_h.ads}, @code{stddef_h.ads}, @code{bits_types_h.ads} which
6673 correspond to the files @code{/usr/include/time.h},
6674 @code{/usr/include/bits/time.h}, etc..., and will then compile these Ada specs
6675 in Ada 2005 mode.
6676
6677 The @code{-C} switch tells @code{gcc} to extract comments from headers,
6678 and will attempt to generate corresponding Ada comments.
6679
6680 If you want to generate a single Ada file and not the transitive closure, you
6681 can use instead the @code{-fdump-ada-spec-slim} switch.
6682
6683 You can optionally specify a parent unit, of which all generated units will
6684 be children, using @code{-fada-spec-parent=@emph{unit}}.
6685
6686 Note that we recommend when possible to use the @emph{g++} driver to
6687 generate bindings, even for most C headers, since this will in general
6688 generate better Ada specs. For generating bindings for C++ headers, it is
6689 mandatory to use the @emph{g++} command, or @emph{gcc -x c++} which
6690 is equivalent in this case. If @emph{g++} cannot work on your C headers
6691 because of incompatibilities between C and C++, then you can fallback to
6692 @code{gcc} instead.
6693
6694 For an example of better bindings generated from the C++ front-end,
6695 the name of the parameters (when available) are actually ignored by the C
6696 front-end. Consider the following C header:
6697
6698 @example
6699 extern void foo (int variable);
6700 @end example
6701
6702 with the C front-end, @code{variable} is ignored, and the above is handled as:
6703
6704 @example
6705 extern void foo (int);
6706 @end example
6707
6708 generating a generic:
6709
6710 @example
6711 procedure foo (param1 : int);
6712 @end example
6713
6714 with the C++ front-end, the name is available, and we generate:
6715
6716 @example
6717 procedure foo (variable : int);
6718 @end example
6719
6720 In some cases, the generated bindings will be more complete or more meaningful
6721 when defining some macros, which you can do via the @code{-D} switch. This
6722 is for example the case with @code{Xlib.h} under GNU/Linux:
6723
6724 @example
6725 $ g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
6726 @end example
6727
6728 The above will generate more complete bindings than a straight call without
6729 the @code{-DXLIB_ILLEGAL_ACCESS} switch.
6730
6731 In other cases, it is not possible to parse a header file in a stand-alone
6732 manner, because other include files need to be included first. In this
6733 case, the solution is to create a small header file including the needed
6734 @code{#include} and possible @code{#define} directives. For example, to
6735 generate Ada bindings for @code{readline/readline.h}, you need to first
6736 include @code{stdio.h}, so you can create a file with the following two
6737 lines in e.g. @code{readline1.h}:
6738
6739 @example
6740 #include <stdio.h>
6741 #include <readline/readline.h>
6742 @end example
6743
6744 and then generate Ada bindings from this file:
6745
6746 @example
6747 $ g++ -c -fdump-ada-spec readline1.h
6748 @end example
6749
6750 @node Generating Bindings for C++ Headers,Switches,Running the Binding Generator,Generating Ada Bindings for C and C++ headers
6751 @anchor{gnat_ugn/the_gnat_compilation_model id72}@anchor{c6}@anchor{gnat_ugn/the_gnat_compilation_model generating-bindings-for-c-headers}@anchor{c7}
6752 @subsubsection Generating Bindings for C++ Headers
6753
6754
6755 Generating bindings for C++ headers is done using the same options, always
6756 with the @emph{g++} compiler. Note that generating Ada spec from C++ headers is a
6757 much more complex job and support for C++ headers is much more limited that
6758 support for C headers. As a result, you will need to modify the resulting
6759 bindings by hand more extensively when using C++ headers.
6760
6761 In this mode, C++ classes will be mapped to Ada tagged types, constructors
6762 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
6763 multiple inheritance of abstract classes will be mapped to Ada interfaces
6764 (see the @emph{Interfacing to C++} section in the @cite{GNAT Reference Manual}
6765 for additional information on interfacing to C++).
6766
6767 For example, given the following C++ header file:
6768
6769 @example
6770 class Carnivore @{
6771 public:
6772 virtual int Number_Of_Teeth () = 0;
6773 @};
6774
6775 class Domestic @{
6776 public:
6777 virtual void Set_Owner (char* Name) = 0;
6778 @};
6779
6780 class Animal @{
6781 public:
6782 int Age_Count;
6783 virtual void Set_Age (int New_Age);
6784 @};
6785
6786 class Dog : Animal, Carnivore, Domestic @{
6787 public:
6788 int Tooth_Count;
6789 char *Owner;
6790
6791 virtual int Number_Of_Teeth ();
6792 virtual void Set_Owner (char* Name);
6793
6794 Dog();
6795 @};
6796 @end example
6797
6798 The corresponding Ada code is generated:
6799
6800 @example
6801 package Class_Carnivore is
6802 type Carnivore is limited interface;
6803 pragma Import (CPP, Carnivore);
6804
6805 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
6806 end;
6807 use Class_Carnivore;
6808
6809 package Class_Domestic is
6810 type Domestic is limited interface;
6811 pragma Import (CPP, Domestic);
6812
6813 procedure Set_Owner
6814 (this : access Domestic;
6815 Name : Interfaces.C.Strings.chars_ptr) is abstract;
6816 end;
6817 use Class_Domestic;
6818
6819 package Class_Animal is
6820 type Animal is tagged limited record
6821 Age_Count : aliased int;
6822 end record;
6823 pragma Import (CPP, Animal);
6824
6825 procedure Set_Age (this : access Animal; New_Age : int);
6826 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
6827 end;
6828 use Class_Animal;
6829
6830 package Class_Dog is
6831 type Dog is new Animal and Carnivore and Domestic with record
6832 Tooth_Count : aliased int;
6833 Owner : Interfaces.C.Strings.chars_ptr;
6834 end record;
6835 pragma Import (CPP, Dog);
6836
6837 function Number_Of_Teeth (this : access Dog) return int;
6838 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
6839
6840 procedure Set_Owner
6841 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
6842 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
6843
6844 function New_Dog return Dog;
6845 pragma CPP_Constructor (New_Dog);
6846 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
6847 end;
6848 use Class_Dog;
6849 @end example
6850
6851 @node Switches,,Generating Bindings for C++ Headers,Generating Ada Bindings for C and C++ headers
6852 @anchor{gnat_ugn/the_gnat_compilation_model switches}@anchor{c8}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-ada-binding-generation}@anchor{c9}
6853 @subsubsection Switches
6854
6855
6856 @geindex -fdump-ada-spec (gcc)
6857
6858
6859 @table @asis
6860
6861 @item @code{-fdump-ada-spec}
6862
6863 Generate Ada spec files for the given header files transitively (including
6864 all header files that these headers depend upon).
6865 @end table
6866
6867 @geindex -fdump-ada-spec-slim (gcc)
6868
6869
6870 @table @asis
6871
6872 @item @code{-fdump-ada-spec-slim}
6873
6874 Generate Ada spec files for the header files specified on the command line
6875 only.
6876 @end table
6877
6878 @geindex -fada-spec-parent (gcc)
6879
6880
6881 @table @asis
6882
6883 @item @code{-fada-spec-parent=@emph{unit}}
6884
6885 Specifies that all files generated by @code{-fdump-ada-spec} are
6886 to be child units of the specified parent unit.
6887 @end table
6888
6889 @geindex -C (gcc)
6890
6891
6892 @table @asis
6893
6894 @item @code{-C}
6895
6896 Extract comments from headers and generate Ada comments in the Ada spec files.
6897 @end table
6898
6899 @node Generating C Headers for Ada Specifications,,Generating Ada Bindings for C and C++ headers,Mixed Language Programming
6900 @anchor{gnat_ugn/the_gnat_compilation_model generating-c-headers-for-ada-specifications}@anchor{ca}@anchor{gnat_ugn/the_gnat_compilation_model id73}@anchor{cb}
6901 @subsection Generating C Headers for Ada Specifications
6902
6903
6904 @geindex Binding generation (for Ada specs)
6905
6906 @geindex C headers (binding generation)
6907
6908 GNAT includes a C header generator for Ada specifications which supports
6909 Ada types that have a direct mapping to C types. This includes in particular
6910 support for:
6911
6912
6913 @itemize *
6914
6915 @item
6916 Scalar types
6917
6918 @item
6919 Constrained arrays
6920
6921 @item
6922 Records (untagged)
6923
6924 @item
6925 Composition of the above types
6926
6927 @item
6928 Constant declarations
6929
6930 @item
6931 Object declarations
6932
6933 @item
6934 Subprogram declarations
6935 @end itemize
6936
6937 @menu
6938 * Running the C Header Generator::
6939
6940 @end menu
6941
6942 @node Running the C Header Generator,,,Generating C Headers for Ada Specifications
6943 @anchor{gnat_ugn/the_gnat_compilation_model running-the-c-header-generator}@anchor{cc}
6944 @subsubsection Running the C Header Generator
6945
6946
6947 The C header generator is part of the GNAT compiler and can be invoked via
6948 the @code{-gnatceg} combination of switches, which will generate a @code{.h}
6949 file corresponding to the given input file (Ada spec or body). Note that
6950 only spec files are processed in any case, so giving a spec or a body file
6951 as input is equivalent. For example:
6952
6953 @example
6954 $ gcc -c -gnatceg pack1.ads
6955 @end example
6956
6957 will generate a self-contained file called @code{pack1.h} including
6958 common definitions from the Ada Standard package, followed by the
6959 definitions included in @code{pack1.ads}, as well as all the other units
6960 withed by this file.
6961
6962 For instance, given the following Ada files:
6963
6964 @example
6965 package Pack2 is
6966 type Int is range 1 .. 10;
6967 end Pack2;
6968 @end example
6969
6970 @example
6971 with Pack2;
6972
6973 package Pack1 is
6974 type Rec is record
6975 Field1, Field2 : Pack2.Int;
6976 end record;
6977
6978 Global : Rec := (1, 2);
6979
6980 procedure Proc1 (R : Rec);
6981 procedure Proc2 (R : in out Rec);
6982 end Pack1;
6983 @end example
6984
6985 The above @code{gcc} command will generate the following @code{pack1.h} file:
6986
6987 @example
6988 /* Standard definitions skipped */
6989 #ifndef PACK2_ADS
6990 #define PACK2_ADS
6991 typedef short_short_integer pack2__TintB;
6992 typedef pack2__TintB pack2__int;
6993 #endif /* PACK2_ADS */
6994
6995 #ifndef PACK1_ADS
6996 #define PACK1_ADS
6997 typedef struct _pack1__rec @{
6998 pack2__int field1;
6999 pack2__int field2;
7000 @} pack1__rec;
7001 extern pack1__rec pack1__global;
7002 extern void pack1__proc1(const pack1__rec r);
7003 extern void pack1__proc2(pack1__rec *r);
7004 #endif /* PACK1_ADS */
7005 @end example
7006
7007 You can then @code{include} @code{pack1.h} from a C source file and use the types,
7008 call subprograms, reference objects, and constants.
7009
7010 @node GNAT and Other Compilation Models,Using GNAT Files with External Tools,Mixed Language Programming,The GNAT Compilation Model
7011 @anchor{gnat_ugn/the_gnat_compilation_model id74}@anchor{cd}@anchor{gnat_ugn/the_gnat_compilation_model gnat-and-other-compilation-models}@anchor{45}
7012 @section GNAT and Other Compilation Models
7013
7014
7015 This section compares the GNAT model with the approaches taken in
7016 other environents, first the C/C++ model and then the mechanism that
7017 has been used in other Ada systems, in particular those traditionally
7018 used for Ada 83.
7019
7020 @menu
7021 * Comparison between GNAT and C/C++ Compilation Models::
7022 * Comparison between GNAT and Conventional Ada Library Models::
7023
7024 @end menu
7025
7026 @node Comparison between GNAT and C/C++ Compilation Models,Comparison between GNAT and Conventional Ada Library Models,,GNAT and Other Compilation Models
7027 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-c-c-compilation-models}@anchor{ce}@anchor{gnat_ugn/the_gnat_compilation_model id75}@anchor{cf}
7028 @subsection Comparison between GNAT and C/C++ Compilation Models
7029
7030
7031 The GNAT model of compilation is close to the C and C++ models. You can
7032 think of Ada specs as corresponding to header files in C. As in C, you
7033 don't need to compile specs; they are compiled when they are used. The
7034 Ada @emph{with} is similar in effect to the @code{#include} of a C
7035 header.
7036
7037 One notable difference is that, in Ada, you may compile specs separately
7038 to check them for semantic and syntactic accuracy. This is not always
7039 possible with C headers because they are fragments of programs that have
7040 less specific syntactic or semantic rules.
7041
7042 The other major difference is the requirement for running the binder,
7043 which performs two important functions. First, it checks for
7044 consistency. In C or C++, the only defense against assembling
7045 inconsistent programs lies outside the compiler, in a makefile, for
7046 example. The binder satisfies the Ada requirement that it be impossible
7047 to construct an inconsistent program when the compiler is used in normal
7048 mode.
7049
7050 @geindex Elaboration order control
7051
7052 The other important function of the binder is to deal with elaboration
7053 issues. There are also elaboration issues in C++ that are handled
7054 automatically. This automatic handling has the advantage of being
7055 simpler to use, but the C++ programmer has no control over elaboration.
7056 Where @code{gnatbind} might complain there was no valid order of
7057 elaboration, a C++ compiler would simply construct a program that
7058 malfunctioned at run time.
7059
7060 @node Comparison between GNAT and Conventional Ada Library Models,,Comparison between GNAT and C/C++ Compilation Models,GNAT and Other Compilation Models
7061 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-conventional-ada-library-models}@anchor{d0}@anchor{gnat_ugn/the_gnat_compilation_model id76}@anchor{d1}
7062 @subsection Comparison between GNAT and Conventional Ada Library Models
7063
7064
7065 This section is intended for Ada programmers who have
7066 used an Ada compiler implementing the traditional Ada library
7067 model, as described in the Ada Reference Manual.
7068
7069 @geindex GNAT library
7070
7071 In GNAT, there is no 'library' in the normal sense. Instead, the set of
7072 source files themselves acts as the library. Compiling Ada programs does
7073 not generate any centralized information, but rather an object file and
7074 a ALI file, which are of interest only to the binder and linker.
7075 In a traditional system, the compiler reads information not only from
7076 the source file being compiled, but also from the centralized library.
7077 This means that the effect of a compilation depends on what has been
7078 previously compiled. In particular:
7079
7080
7081 @itemize *
7082
7083 @item
7084 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7085 to the version of the unit most recently compiled into the library.
7086
7087 @item
7088 Inlining is effective only if the necessary body has already been
7089 compiled into the library.
7090
7091 @item
7092 Compiling a unit may obsolete other units in the library.
7093 @end itemize
7094
7095 In GNAT, compiling one unit never affects the compilation of any other
7096 units because the compiler reads only source files. Only changes to source
7097 files can affect the results of a compilation. In particular:
7098
7099
7100 @itemize *
7101
7102 @item
7103 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7104 to the source version of the unit that is currently accessible to the
7105 compiler.
7106
7107 @geindex Inlining
7108
7109 @item
7110 Inlining requires the appropriate source files for the package or
7111 subprogram bodies to be available to the compiler. Inlining is always
7112 effective, independent of the order in which units are compiled.
7113
7114 @item
7115 Compiling a unit never affects any other compilations. The editing of
7116 sources may cause previous compilations to be out of date if they
7117 depended on the source file being modified.
7118 @end itemize
7119
7120 The most important result of these differences is that order of compilation
7121 is never significant in GNAT. There is no situation in which one is
7122 required to do one compilation before another. What shows up as order of
7123 compilation requirements in the traditional Ada library becomes, in
7124 GNAT, simple source dependencies; in other words, there is only a set
7125 of rules saying what source files must be present when a file is
7126 compiled.
7127
7128 @node Using GNAT Files with External Tools,,GNAT and Other Compilation Models,The GNAT Compilation Model
7129 @anchor{gnat_ugn/the_gnat_compilation_model using-gnat-files-with-external-tools}@anchor{1a}@anchor{gnat_ugn/the_gnat_compilation_model id77}@anchor{d2}
7130 @section Using GNAT Files with External Tools
7131
7132
7133 This section explains how files that are produced by GNAT may be
7134 used with tools designed for other languages.
7135
7136 @menu
7137 * Using Other Utility Programs with GNAT::
7138 * The External Symbol Naming Scheme of GNAT::
7139
7140 @end menu
7141
7142 @node Using Other Utility Programs with GNAT,The External Symbol Naming Scheme of GNAT,,Using GNAT Files with External Tools
7143 @anchor{gnat_ugn/the_gnat_compilation_model using-other-utility-programs-with-gnat}@anchor{d3}@anchor{gnat_ugn/the_gnat_compilation_model id78}@anchor{d4}
7144 @subsection Using Other Utility Programs with GNAT
7145
7146
7147 The object files generated by GNAT are in standard system format and in
7148 particular the debugging information uses this format. This means
7149 programs generated by GNAT can be used with existing utilities that
7150 depend on these formats.
7151
7152 In general, any utility program that works with C will also often work with
7153 Ada programs generated by GNAT. This includes software utilities such as
7154 gprof (a profiling program), gdb (the FSF debugger), and utilities such
7155 as Purify.
7156
7157 @node The External Symbol Naming Scheme of GNAT,,Using Other Utility Programs with GNAT,Using GNAT Files with External Tools
7158 @anchor{gnat_ugn/the_gnat_compilation_model the-external-symbol-naming-scheme-of-gnat}@anchor{d5}@anchor{gnat_ugn/the_gnat_compilation_model id79}@anchor{d6}
7159 @subsection The External Symbol Naming Scheme of GNAT
7160
7161
7162 In order to interpret the output from GNAT, when using tools that are
7163 originally intended for use with other languages, it is useful to
7164 understand the conventions used to generate link names from the Ada
7165 entity names.
7166
7167 All link names are in all lowercase letters. With the exception of library
7168 procedure names, the mechanism used is simply to use the full expanded
7169 Ada name with dots replaced by double underscores. For example, suppose
7170 we have the following package spec:
7171
7172 @example
7173 package QRS is
7174 MN : Integer;
7175 end QRS;
7176 @end example
7177
7178 @geindex pragma Export
7179
7180 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
7181 the corresponding link name is @code{qrs__mn}.
7182 Of course if a @code{pragma Export} is used this may be overridden:
7183
7184 @example
7185 package Exports is
7186 Var1 : Integer;
7187 pragma Export (Var1, C, External_Name => "var1_name");
7188 Var2 : Integer;
7189 pragma Export (Var2, C, Link_Name => "var2_link_name");
7190 end Exports;
7191 @end example
7192
7193 In this case, the link name for @code{Var1} is whatever link name the
7194 C compiler would assign for the C function @code{var1_name}. This typically
7195 would be either @code{var1_name} or @code{_var1_name}, depending on operating
7196 system conventions, but other possibilities exist. The link name for
7197 @code{Var2} is @code{var2_link_name}, and this is not operating system
7198 dependent.
7199
7200 One exception occurs for library level procedures. A potential ambiguity
7201 arises between the required name @code{_main} for the C main program,
7202 and the name we would otherwise assign to an Ada library level procedure
7203 called @code{Main} (which might well not be the main program).
7204
7205 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
7206 names. So if we have a library level procedure such as:
7207
7208 @example
7209 procedure Hello (S : String);
7210 @end example
7211
7212 the external name of this procedure will be @code{_ada_hello}.
7213
7214 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
7215
7216 @node Building Executable Programs with GNAT,GNAT Utility Programs,The GNAT Compilation Model,Top
7217 @anchor{gnat_ugn/building_executable_programs_with_gnat building-executable-programs-with-gnat}@anchor{a}@anchor{gnat_ugn/building_executable_programs_with_gnat doc}@anchor{d7}@anchor{gnat_ugn/building_executable_programs_with_gnat id1}@anchor{d8}
7218 @chapter Building Executable Programs with GNAT
7219
7220
7221 This chapter describes first the gnatmake tool
7222 (@ref{1b,,Building with gnatmake}),
7223 which automatically determines the set of sources
7224 needed by an Ada compilation unit and executes the necessary
7225 (re)compilations, binding and linking.
7226 It also explains how to use each tool individually: the
7227 compiler (gcc, see @ref{1c,,Compiling with gcc}),
7228 binder (gnatbind, see @ref{1d,,Binding with gnatbind}),
7229 and linker (gnatlink, see @ref{1e,,Linking with gnatlink})
7230 to build executable programs.
7231 Finally, this chapter provides examples of
7232 how to make use of the general GNU make mechanism
7233 in a GNAT context (see @ref{1f,,Using the GNU make Utility}).
7234
7235
7236 @menu
7237 * Building with gnatmake::
7238 * Compiling with gcc::
7239 * Compiler Switches::
7240 * Linker Switches::
7241 * Binding with gnatbind::
7242 * Linking with gnatlink::
7243 * Using the GNU make Utility::
7244
7245 @end menu
7246
7247 @node Building with gnatmake,Compiling with gcc,,Building Executable Programs with GNAT
7248 @anchor{gnat_ugn/building_executable_programs_with_gnat the-gnat-make-program-gnatmake}@anchor{1b}@anchor{gnat_ugn/building_executable_programs_with_gnat building-with-gnatmake}@anchor{d9}
7249 @section Building with @code{gnatmake}
7250
7251
7252 @geindex gnatmake
7253
7254 A typical development cycle when working on an Ada program consists of
7255 the following steps:
7256
7257
7258 @enumerate
7259
7260 @item
7261 Edit some sources to fix bugs;
7262
7263 @item
7264 Add enhancements;
7265
7266 @item
7267 Compile all sources affected;
7268
7269 @item
7270 Rebind and relink; and
7271
7272 @item
7273 Test.
7274 @end enumerate
7275
7276 @geindex Dependency rules (compilation)
7277
7278 The third step in particular can be tricky, because not only do the modified
7279 files have to be compiled, but any files depending on these files must also be
7280 recompiled. The dependency rules in Ada can be quite complex, especially
7281 in the presence of overloading, @code{use} clauses, generics and inlined
7282 subprograms.
7283
7284 @code{gnatmake} automatically takes care of the third and fourth steps
7285 of this process. It determines which sources need to be compiled,
7286 compiles them, and binds and links the resulting object files.
7287
7288 Unlike some other Ada make programs, the dependencies are always
7289 accurately recomputed from the new sources. The source based approach of
7290 the GNAT compilation model makes this possible. This means that if
7291 changes to the source program cause corresponding changes in
7292 dependencies, they will always be tracked exactly correctly by
7293 @code{gnatmake}.
7294
7295 Note that for advanced forms of project structure, we recommend creating
7296 a project file as explained in the @emph{GNAT_Project_Manager} chapter in the
7297 @emph{GPRbuild User's Guide}, and using the
7298 @code{gprbuild} tool which supports building with project files and works similarly
7299 to @code{gnatmake}.
7300
7301 @menu
7302 * Running gnatmake::
7303 * Switches for gnatmake::
7304 * Mode Switches for gnatmake::
7305 * Notes on the Command Line::
7306 * How gnatmake Works::
7307 * Examples of gnatmake Usage::
7308
7309 @end menu
7310
7311 @node Running gnatmake,Switches for gnatmake,,Building with gnatmake
7312 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatmake}@anchor{da}@anchor{gnat_ugn/building_executable_programs_with_gnat id2}@anchor{db}
7313 @subsection Running @code{gnatmake}
7314
7315
7316 The usual form of the @code{gnatmake} command is
7317
7318 @example
7319 $ gnatmake [<switches>] <file_name> [<file_names>] [<mode_switches>]
7320 @end example
7321
7322 The only required argument is one @code{file_name}, which specifies
7323 a compilation unit that is a main program. Several @code{file_names} can be
7324 specified: this will result in several executables being built.
7325 If @code{switches} are present, they can be placed before the first
7326 @code{file_name}, between @code{file_names} or after the last @code{file_name}.
7327 If @code{mode_switches} are present, they must always be placed after
7328 the last @code{file_name} and all @code{switches}.
7329
7330 If you are using standard file extensions (@code{.adb} and
7331 @code{.ads}), then the
7332 extension may be omitted from the @code{file_name} arguments. However, if
7333 you are using non-standard extensions, then it is required that the
7334 extension be given. A relative or absolute directory path can be
7335 specified in a @code{file_name}, in which case, the input source file will
7336 be searched for in the specified directory only. Otherwise, the input
7337 source file will first be searched in the directory where
7338 @code{gnatmake} was invoked and if it is not found, it will be search on
7339 the source path of the compiler as described in
7340 @ref{89,,Search Paths and the Run-Time Library (RTL)}.
7341
7342 All @code{gnatmake} output (except when you specify @code{-M}) is sent to
7343 @code{stderr}. The output produced by the
7344 @code{-M} switch is sent to @code{stdout}.
7345
7346 @node Switches for gnatmake,Mode Switches for gnatmake,Running gnatmake,Building with gnatmake
7347 @anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatmake}@anchor{dc}@anchor{gnat_ugn/building_executable_programs_with_gnat id3}@anchor{dd}
7348 @subsection Switches for @code{gnatmake}
7349
7350
7351 You may specify any of the following switches to @code{gnatmake}:
7352
7353 @geindex --version (gnatmake)
7354
7355
7356 @table @asis
7357
7358 @item @code{--version}
7359
7360 Display Copyright and version, then exit disregarding all other options.
7361 @end table
7362
7363 @geindex --help (gnatmake)
7364
7365
7366 @table @asis
7367
7368 @item @code{--help}
7369
7370 If @code{--version} was not used, display usage, then exit disregarding
7371 all other options.
7372 @end table
7373
7374 @geindex --GCC=compiler_name (gnatmake)
7375
7376
7377 @table @asis
7378
7379 @item @code{--GCC=@emph{compiler_name}}
7380
7381 Program used for compiling. The default is @code{gcc}. You need to use
7382 quotes around @code{compiler_name} if @code{compiler_name} contains
7383 spaces or other separator characters.
7384 As an example @code{--GCC="foo -x -y"}
7385 will instruct @code{gnatmake} to use @code{foo -x -y} as your
7386 compiler. A limitation of this syntax is that the name and path name of
7387 the executable itself must not include any embedded spaces. Note that
7388 switch @code{-c} is always inserted after your command name. Thus in the
7389 above example the compiler command that will be used by @code{gnatmake}
7390 will be @code{foo -c -x -y}. If several @code{--GCC=compiler_name} are
7391 used, only the last @code{compiler_name} is taken into account. However,
7392 all the additional switches are also taken into account. Thus,
7393 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7394 @code{--GCC="bar -x -y -z -t"}.
7395 @end table
7396
7397 @geindex --GNATBIND=binder_name (gnatmake)
7398
7399
7400 @table @asis
7401
7402 @item @code{--GNATBIND=@emph{binder_name}}
7403
7404 Program used for binding. The default is @code{gnatbind}. You need to
7405 use quotes around @code{binder_name} if @code{binder_name} contains spaces
7406 or other separator characters.
7407 As an example @code{--GNATBIND="bar -x -y"}
7408 will instruct @code{gnatmake} to use @code{bar -x -y} as your
7409 binder. Binder switches that are normally appended by @code{gnatmake}
7410 to @code{gnatbind} are now appended to the end of @code{bar -x -y}.
7411 A limitation of this syntax is that the name and path name of the executable
7412 itself must not include any embedded spaces.
7413 @end table
7414
7415 @geindex --GNATLINK=linker_name (gnatmake)
7416
7417
7418 @table @asis
7419
7420 @item @code{--GNATLINK=@emph{linker_name}}
7421
7422 Program used for linking. The default is @code{gnatlink}. You need to
7423 use quotes around @code{linker_name} if @code{linker_name} contains spaces
7424 or other separator characters.
7425 As an example @code{--GNATLINK="lan -x -y"}
7426 will instruct @code{gnatmake} to use @code{lan -x -y} as your
7427 linker. Linker switches that are normally appended by @code{gnatmake} to
7428 @code{gnatlink} are now appended to the end of @code{lan -x -y}.
7429 A limitation of this syntax is that the name and path name of the executable
7430 itself must not include any embedded spaces.
7431
7432 @item @code{--create-map-file}
7433
7434 When linking an executable, create a map file. The name of the map file
7435 has the same name as the executable with extension ".map".
7436
7437 @item @code{--create-map-file=@emph{mapfile}}
7438
7439 When linking an executable, create a map file with the specified name.
7440 @end table
7441
7442 @geindex --create-missing-dirs (gnatmake)
7443
7444
7445 @table @asis
7446
7447 @item @code{--create-missing-dirs}
7448
7449 When using project files (@code{-P@emph{project}}), automatically create
7450 missing object directories, library directories and exec
7451 directories.
7452
7453 @item @code{--single-compile-per-obj-dir}
7454
7455 Disallow simultaneous compilations in the same object directory when
7456 project files are used.
7457
7458 @item @code{--subdirs=@emph{subdir}}
7459
7460 Actual object directory of each project file is the subdirectory subdir of the
7461 object directory specified or defaulted in the project file.
7462
7463 @item @code{--unchecked-shared-lib-imports}
7464
7465 By default, shared library projects are not allowed to import static library
7466 projects. When this switch is used on the command line, this restriction is
7467 relaxed.
7468
7469 @item @code{--source-info=@emph{source info file}}
7470
7471 Specify a source info file. This switch is active only when project files
7472 are used. If the source info file is specified as a relative path, then it is
7473 relative to the object directory of the main project. If the source info file
7474 does not exist, then after the Project Manager has successfully parsed and
7475 processed the project files and found the sources, it creates the source info
7476 file. If the source info file already exists and can be read successfully,
7477 then the Project Manager will get all the needed information about the sources
7478 from the source info file and will not look for them. This reduces the time
7479 to process the project files, especially when looking for sources that take a
7480 long time. If the source info file exists but cannot be parsed successfully,
7481 the Project Manager will attempt to recreate it. If the Project Manager fails
7482 to create the source info file, a message is issued, but gnatmake does not
7483 fail. @code{gnatmake} "trusts" the source info file. This means that
7484 if the source files have changed (addition, deletion, moving to a different
7485 source directory), then the source info file need to be deleted and recreated.
7486 @end table
7487
7488 @geindex -a (gnatmake)
7489
7490
7491 @table @asis
7492
7493 @item @code{-a}
7494
7495 Consider all files in the make process, even the GNAT internal system
7496 files (for example, the predefined Ada library files), as well as any
7497 locked files. Locked files are files whose ALI file is write-protected.
7498 By default,
7499 @code{gnatmake} does not check these files,
7500 because the assumption is that the GNAT internal files are properly up
7501 to date, and also that any write protected ALI files have been properly
7502 installed. Note that if there is an installation problem, such that one
7503 of these files is not up to date, it will be properly caught by the
7504 binder.
7505 You may have to specify this switch if you are working on GNAT
7506 itself. The switch @code{-a} is also useful
7507 in conjunction with @code{-f}
7508 if you need to recompile an entire application,
7509 including run-time files, using special configuration pragmas,
7510 such as a @code{Normalize_Scalars} pragma.
7511
7512 By default
7513 @code{gnatmake -a} compiles all GNAT
7514 internal files with
7515 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
7516 @end table
7517
7518 @geindex -b (gnatmake)
7519
7520
7521 @table @asis
7522
7523 @item @code{-b}
7524
7525 Bind only. Can be combined with @code{-c} to do
7526 compilation and binding, but no link.
7527 Can be combined with @code{-l}
7528 to do binding and linking. When not combined with
7529 @code{-c}
7530 all the units in the closure of the main program must have been previously
7531 compiled and must be up to date. The root unit specified by @code{file_name}
7532 may be given without extension, with the source extension or, if no GNAT
7533 Project File is specified, with the ALI file extension.
7534 @end table
7535
7536 @geindex -c (gnatmake)
7537
7538
7539 @table @asis
7540
7541 @item @code{-c}
7542
7543 Compile only. Do not perform binding, except when @code{-b}
7544 is also specified. Do not perform linking, except if both
7545 @code{-b} and
7546 @code{-l} are also specified.
7547 If the root unit specified by @code{file_name} is not a main unit, this is the
7548 default. Otherwise @code{gnatmake} will attempt binding and linking
7549 unless all objects are up to date and the executable is more recent than
7550 the objects.
7551 @end table
7552
7553 @geindex -C (gnatmake)
7554
7555
7556 @table @asis
7557
7558 @item @code{-C}
7559
7560 Use a temporary mapping file. A mapping file is a way to communicate
7561 to the compiler two mappings: from unit names to file names (without
7562 any directory information) and from file names to path names (with
7563 full directory information). A mapping file can make the compiler's
7564 file searches faster, especially if there are many source directories,
7565 or the sources are read over a slow network connection. If
7566 @code{-P} is used, a mapping file is always used, so
7567 @code{-C} is unnecessary; in this case the mapping file
7568 is initially populated based on the project file. If
7569 @code{-C} is used without
7570 @code{-P},
7571 the mapping file is initially empty. Each invocation of the compiler
7572 will add any newly accessed sources to the mapping file.
7573 @end table
7574
7575 @geindex -C= (gnatmake)
7576
7577
7578 @table @asis
7579
7580 @item @code{-C=@emph{file}}
7581
7582 Use a specific mapping file. The file, specified as a path name (absolute or
7583 relative) by this switch, should already exist, otherwise the switch is
7584 ineffective. The specified mapping file will be communicated to the compiler.
7585 This switch is not compatible with a project file
7586 (-P`file`) or with multiple compiling processes
7587 (-jnnn, when nnn is greater than 1).
7588 @end table
7589
7590 @geindex -d (gnatmake)
7591
7592
7593 @table @asis
7594
7595 @item @code{-d}
7596
7597 Display progress for each source, up to date or not, as a single line:
7598
7599 @example
7600 completed x out of y (zz%)
7601 @end example
7602
7603 If the file needs to be compiled this is displayed after the invocation of
7604 the compiler. These lines are displayed even in quiet output mode.
7605 @end table
7606
7607 @geindex -D (gnatmake)
7608
7609
7610 @table @asis
7611
7612 @item @code{-D @emph{dir}}
7613
7614 Put all object files and ALI file in directory @code{dir}.
7615 If the @code{-D} switch is not used, all object files
7616 and ALI files go in the current working directory.
7617
7618 This switch cannot be used when using a project file.
7619 @end table
7620
7621 @geindex -eI (gnatmake)
7622
7623
7624 @table @asis
7625
7626 @item @code{-eI@emph{nnn}}
7627
7628 Indicates that the main source is a multi-unit source and the rank of the unit
7629 in the source file is nnn. nnn needs to be a positive number and a valid
7630 index in the source. This switch cannot be used when @code{gnatmake} is
7631 invoked for several mains.
7632 @end table
7633
7634 @geindex -eL (gnatmake)
7635
7636 @geindex symbolic links
7637
7638
7639 @table @asis
7640
7641 @item @code{-eL}
7642
7643 Follow all symbolic links when processing project files.
7644 This should be used if your project uses symbolic links for files or
7645 directories, but is not needed in other cases.
7646
7647 @geindex naming scheme
7648
7649 This also assumes that no directory matches the naming scheme for files (for
7650 instance that you do not have a directory called "sources.ads" when using the
7651 default GNAT naming scheme).
7652
7653 When you do not have to use this switch (i.e., by default), gnatmake is able to
7654 save a lot of system calls (several per source file and object file), which
7655 can result in a significant speed up to load and manipulate a project file,
7656 especially when using source files from a remote system.
7657 @end table
7658
7659 @geindex -eS (gnatmake)
7660
7661
7662 @table @asis
7663
7664 @item @code{-eS}
7665
7666 Output the commands for the compiler, the binder and the linker
7667 on standard output,
7668 instead of standard error.
7669 @end table
7670
7671 @geindex -f (gnatmake)
7672
7673
7674 @table @asis
7675
7676 @item @code{-f}
7677
7678 Force recompilations. Recompile all sources, even though some object
7679 files may be up to date, but don't recompile predefined or GNAT internal
7680 files or locked files (files with a write-protected ALI file),
7681 unless the @code{-a} switch is also specified.
7682 @end table
7683
7684 @geindex -F (gnatmake)
7685
7686
7687 @table @asis
7688
7689 @item @code{-F}
7690
7691 When using project files, if some errors or warnings are detected during
7692 parsing and verbose mode is not in effect (no use of switch
7693 -v), then error lines start with the full path name of the project
7694 file, rather than its simple file name.
7695 @end table
7696
7697 @geindex -g (gnatmake)
7698
7699
7700 @table @asis
7701
7702 @item @code{-g}
7703
7704 Enable debugging. This switch is simply passed to the compiler and to the
7705 linker.
7706 @end table
7707
7708 @geindex -i (gnatmake)
7709
7710
7711 @table @asis
7712
7713 @item @code{-i}
7714
7715 In normal mode, @code{gnatmake} compiles all object files and ALI files
7716 into the current directory. If the @code{-i} switch is used,
7717 then instead object files and ALI files that already exist are overwritten
7718 in place. This means that once a large project is organized into separate
7719 directories in the desired manner, then @code{gnatmake} will automatically
7720 maintain and update this organization. If no ALI files are found on the
7721 Ada object path (see @ref{89,,Search Paths and the Run-Time Library (RTL)}),
7722 the new object and ALI files are created in the
7723 directory containing the source being compiled. If another organization
7724 is desired, where objects and sources are kept in different directories,
7725 a useful technique is to create dummy ALI files in the desired directories.
7726 When detecting such a dummy file, @code{gnatmake} will be forced to
7727 recompile the corresponding source file, and it will be put the resulting
7728 object and ALI files in the directory where it found the dummy file.
7729 @end table
7730
7731 @geindex -j (gnatmake)
7732
7733 @geindex Parallel make
7734
7735
7736 @table @asis
7737
7738 @item @code{-j@emph{n}}
7739
7740 Use @code{n} processes to carry out the (re)compilations. On a multiprocessor
7741 machine compilations will occur in parallel. If @code{n} is 0, then the
7742 maximum number of parallel compilations is the number of core processors
7743 on the platform. In the event of compilation errors, messages from various
7744 compilations might get interspersed (but @code{gnatmake} will give you the
7745 full ordered list of failing compiles at the end). If this is problematic,
7746 rerun the make process with n set to 1 to get a clean list of messages.
7747 @end table
7748
7749 @geindex -k (gnatmake)
7750
7751
7752 @table @asis
7753
7754 @item @code{-k}
7755
7756 Keep going. Continue as much as possible after a compilation error. To
7757 ease the programmer's task in case of compilation errors, the list of
7758 sources for which the compile fails is given when @code{gnatmake}
7759 terminates.
7760
7761 If @code{gnatmake} is invoked with several @code{file_names} and with this
7762 switch, if there are compilation errors when building an executable,
7763 @code{gnatmake} will not attempt to build the following executables.
7764 @end table
7765
7766 @geindex -l (gnatmake)
7767
7768
7769 @table @asis
7770
7771 @item @code{-l}
7772
7773 Link only. Can be combined with @code{-b} to binding
7774 and linking. Linking will not be performed if combined with
7775 @code{-c}
7776 but not with @code{-b}.
7777 When not combined with @code{-b}
7778 all the units in the closure of the main program must have been previously
7779 compiled and must be up to date, and the main program needs to have been bound.
7780 The root unit specified by @code{file_name}
7781 may be given without extension, with the source extension or, if no GNAT
7782 Project File is specified, with the ALI file extension.
7783 @end table
7784
7785 @geindex -m (gnatmake)
7786
7787
7788 @table @asis
7789
7790 @item @code{-m}
7791
7792 Specify that the minimum necessary amount of recompilations
7793 be performed. In this mode @code{gnatmake} ignores time
7794 stamp differences when the only
7795 modifications to a source file consist in adding/removing comments,
7796 empty lines, spaces or tabs. This means that if you have changed the
7797 comments in a source file or have simply reformatted it, using this
7798 switch will tell @code{gnatmake} not to recompile files that depend on it
7799 (provided other sources on which these files depend have undergone no
7800 semantic modifications). Note that the debugging information may be
7801 out of date with respect to the sources if the @code{-m} switch causes
7802 a compilation to be switched, so the use of this switch represents a
7803 trade-off between compilation time and accurate debugging information.
7804 @end table
7805
7806 @geindex Dependencies
7807 @geindex producing list
7808
7809 @geindex -M (gnatmake)
7810
7811
7812 @table @asis
7813
7814 @item @code{-M}
7815
7816 Check if all objects are up to date. If they are, output the object
7817 dependences to @code{stdout} in a form that can be directly exploited in
7818 a @code{Makefile}. By default, each source file is prefixed with its
7819 (relative or absolute) directory name. This name is whatever you
7820 specified in the various @code{-aI}
7821 and @code{-I} switches. If you use
7822 @code{gnatmake -M} @code{-q}
7823 (see below), only the source file names,
7824 without relative paths, are output. If you just specify the @code{-M}
7825 switch, dependencies of the GNAT internal system files are omitted. This
7826 is typically what you want. If you also specify
7827 the @code{-a} switch,
7828 dependencies of the GNAT internal files are also listed. Note that
7829 dependencies of the objects in external Ada libraries (see
7830 switch @code{-aL@emph{dir}} in the following list)
7831 are never reported.
7832 @end table
7833
7834 @geindex -n (gnatmake)
7835
7836
7837 @table @asis
7838
7839 @item @code{-n}
7840
7841 Don't compile, bind, or link. Checks if all objects are up to date.
7842 If they are not, the full name of the first file that needs to be
7843 recompiled is printed.
7844 Repeated use of this option, followed by compiling the indicated source
7845 file, will eventually result in recompiling all required units.
7846 @end table
7847
7848 @geindex -o (gnatmake)
7849
7850
7851 @table @asis
7852
7853 @item @code{-o @emph{exec_name}}
7854
7855 Output executable name. The name of the final executable program will be
7856 @code{exec_name}. If the @code{-o} switch is omitted the default
7857 name for the executable will be the name of the input file in appropriate form
7858 for an executable file on the host system.
7859
7860 This switch cannot be used when invoking @code{gnatmake} with several
7861 @code{file_names}.
7862 @end table
7863
7864 @geindex -p (gnatmake)
7865
7866
7867 @table @asis
7868
7869 @item @code{-p}
7870
7871 Same as @code{--create-missing-dirs}
7872 @end table
7873
7874 @geindex -P (gnatmake)
7875
7876
7877 @table @asis
7878
7879 @item @code{-P@emph{project}}
7880
7881 Use project file @code{project}. Only one such switch can be used.
7882 @end table
7883
7884 @c -- Comment:
7885 @c :ref:`gnatmake_and_Project_Files`.
7886
7887 @geindex -q (gnatmake)
7888
7889
7890 @table @asis
7891
7892 @item @code{-q}
7893
7894 Quiet. When this flag is not set, the commands carried out by
7895 @code{gnatmake} are displayed.
7896 @end table
7897
7898 @geindex -s (gnatmake)
7899
7900
7901 @table @asis
7902
7903 @item @code{-s}
7904
7905 Recompile if compiler switches have changed since last compilation.
7906 All compiler switches but -I and -o are taken into account in the
7907 following way:
7908 orders between different 'first letter' switches are ignored, but
7909 orders between same switches are taken into account. For example,
7910 @code{-O -O2} is different than @code{-O2 -O}, but @code{-g -O}
7911 is equivalent to @code{-O -g}.
7912
7913 This switch is recommended when Integrated Preprocessing is used.
7914 @end table
7915
7916 @geindex -u (gnatmake)
7917
7918
7919 @table @asis
7920
7921 @item @code{-u}
7922
7923 Unique. Recompile at most the main files. It implies -c. Combined with
7924 -f, it is equivalent to calling the compiler directly. Note that using
7925 -u with a project file and no main has a special meaning.
7926 @end table
7927
7928 @c --Comment
7929 @c (See :ref:`Project_Files_and_Main_Subprograms`.)
7930
7931 @geindex -U (gnatmake)
7932
7933
7934 @table @asis
7935
7936 @item @code{-U}
7937
7938 When used without a project file or with one or several mains on the command
7939 line, is equivalent to -u. When used with a project file and no main
7940 on the command line, all sources of all project files are checked and compiled
7941 if not up to date, and libraries are rebuilt, if necessary.
7942 @end table
7943
7944 @geindex -v (gnatmake)
7945
7946
7947 @table @asis
7948
7949 @item @code{-v}
7950
7951 Verbose. Display the reason for all recompilations @code{gnatmake}
7952 decides are necessary, with the highest verbosity level.
7953 @end table
7954
7955 @geindex -vl (gnatmake)
7956
7957
7958 @table @asis
7959
7960 @item @code{-vl}
7961
7962 Verbosity level Low. Display fewer lines than in verbosity Medium.
7963 @end table
7964
7965 @geindex -vm (gnatmake)
7966
7967
7968 @table @asis
7969
7970 @item @code{-vm}
7971
7972 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
7973 @end table
7974
7975 @geindex -vm (gnatmake)
7976
7977
7978 @table @asis
7979
7980 @item @code{-vh}
7981
7982 Verbosity level High. Equivalent to -v.
7983
7984 @item @code{-vP@emph{x}}
7985
7986 Indicate the verbosity of the parsing of GNAT project files.
7987 See @ref{de,,Switches Related to Project Files}.
7988 @end table
7989
7990 @geindex -x (gnatmake)
7991
7992
7993 @table @asis
7994
7995 @item @code{-x}
7996
7997 Indicate that sources that are not part of any Project File may be compiled.
7998 Normally, when using Project Files, only sources that are part of a Project
7999 File may be compile. When this switch is used, a source outside of all Project
8000 Files may be compiled. The ALI file and the object file will be put in the
8001 object directory of the main Project. The compilation switches used will only
8002 be those specified on the command line. Even when
8003 @code{-x} is used, mains specified on the
8004 command line need to be sources of a project file.
8005
8006 @item @code{-X@emph{name}=@emph{value}}
8007
8008 Indicate that external variable @code{name} has the value @code{value}.
8009 The Project Manager will use this value for occurrences of
8010 @code{external(name)} when parsing the project file.
8011 @ref{de,,Switches Related to Project Files}.
8012 @end table
8013
8014 @geindex -z (gnatmake)
8015
8016
8017 @table @asis
8018
8019 @item @code{-z}
8020
8021 No main subprogram. Bind and link the program even if the unit name
8022 given on the command line is a package name. The resulting executable
8023 will execute the elaboration routines of the package and its closure,
8024 then the finalization routines.
8025 @end table
8026
8027 @subsubheading GCC switches
8028
8029
8030 Any uppercase or multi-character switch that is not a @code{gnatmake} switch
8031 is passed to @code{gcc} (e.g., @code{-O}, @code{-gnato,} etc.)
8032
8033 @subsubheading Source and library search path switches
8034
8035
8036 @geindex -aI (gnatmake)
8037
8038
8039 @table @asis
8040
8041 @item @code{-aI@emph{dir}}
8042
8043 When looking for source files also look in directory @code{dir}.
8044 The order in which source files search is undertaken is
8045 described in @ref{89,,Search Paths and the Run-Time Library (RTL)}.
8046 @end table
8047
8048 @geindex -aL (gnatmake)
8049
8050
8051 @table @asis
8052
8053 @item @code{-aL@emph{dir}}
8054
8055 Consider @code{dir} as being an externally provided Ada library.
8056 Instructs @code{gnatmake} to skip compilation units whose @code{.ALI}
8057 files have been located in directory @code{dir}. This allows you to have
8058 missing bodies for the units in @code{dir} and to ignore out of date bodies
8059 for the same units. You still need to specify
8060 the location of the specs for these units by using the switches
8061 @code{-aI@emph{dir}} or @code{-I@emph{dir}}.
8062 Note: this switch is provided for compatibility with previous versions
8063 of @code{gnatmake}. The easier method of causing standard libraries
8064 to be excluded from consideration is to write-protect the corresponding
8065 ALI files.
8066 @end table
8067
8068 @geindex -aO (gnatmake)
8069
8070
8071 @table @asis
8072
8073 @item @code{-aO@emph{dir}}
8074
8075 When searching for library and object files, look in directory
8076 @code{dir}. The order in which library files are searched is described in
8077 @ref{8c,,Search Paths for gnatbind}.
8078 @end table
8079
8080 @geindex Search paths
8081 @geindex for gnatmake
8082
8083 @geindex -A (gnatmake)
8084
8085
8086 @table @asis
8087
8088 @item @code{-A@emph{dir}}
8089
8090 Equivalent to @code{-aL@emph{dir}} @code{-aI@emph{dir}}.
8091
8092 @geindex -I (gnatmake)
8093
8094 @item @code{-I@emph{dir}}
8095
8096 Equivalent to @code{-aO@emph{dir} -aI@emph{dir}}.
8097 @end table
8098
8099 @geindex -I- (gnatmake)
8100
8101 @geindex Source files
8102 @geindex suppressing search
8103
8104
8105 @table @asis
8106
8107 @item @code{-I-}
8108
8109 Do not look for source files in the directory containing the source
8110 file named in the command line.
8111 Do not look for ALI or object files in the directory
8112 where @code{gnatmake} was invoked.
8113 @end table
8114
8115 @geindex -L (gnatmake)
8116
8117 @geindex Linker libraries
8118
8119
8120 @table @asis
8121
8122 @item @code{-L@emph{dir}}
8123
8124 Add directory @code{dir} to the list of directories in which the linker
8125 will search for libraries. This is equivalent to
8126 @code{-largs} @code{-L@emph{dir}}.
8127 Furthermore, under Windows, the sources pointed to by the libraries path
8128 set in the registry are not searched for.
8129 @end table
8130
8131 @geindex -nostdinc (gnatmake)
8132
8133
8134 @table @asis
8135
8136 @item @code{-nostdinc}
8137
8138 Do not look for source files in the system default directory.
8139 @end table
8140
8141 @geindex -nostdlib (gnatmake)
8142
8143
8144 @table @asis
8145
8146 @item @code{-nostdlib}
8147
8148 Do not look for library files in the system default directory.
8149 @end table
8150
8151 @geindex --RTS (gnatmake)
8152
8153
8154 @table @asis
8155
8156 @item @code{--RTS=@emph{rts-path}}
8157
8158 Specifies the default location of the run-time library. GNAT looks for the
8159 run-time
8160 in the following directories, and stops as soon as a valid run-time is found
8161 (@code{adainclude} or @code{ada_source_path}, and @code{adalib} or
8162 @code{ada_object_path} present):
8163
8164
8165 @itemize *
8166
8167 @item
8168 @emph{<current directory>/$rts_path}
8169
8170 @item
8171 @emph{<default-search-dir>/$rts_path}
8172
8173 @item
8174 @emph{<default-search-dir>/rts-$rts_path}
8175
8176 @item
8177 The selected path is handled like a normal RTS path.
8178 @end itemize
8179 @end table
8180
8181 @node Mode Switches for gnatmake,Notes on the Command Line,Switches for gnatmake,Building with gnatmake
8182 @anchor{gnat_ugn/building_executable_programs_with_gnat id4}@anchor{df}@anchor{gnat_ugn/building_executable_programs_with_gnat mode-switches-for-gnatmake}@anchor{e0}
8183 @subsection Mode Switches for @code{gnatmake}
8184
8185
8186 The mode switches (referred to as @code{mode_switches}) allow the
8187 inclusion of switches that are to be passed to the compiler itself, the
8188 binder or the linker. The effect of a mode switch is to cause all
8189 subsequent switches up to the end of the switch list, or up to the next
8190 mode switch, to be interpreted as switches to be passed on to the
8191 designated component of GNAT.
8192
8193 @geindex -cargs (gnatmake)
8194
8195
8196 @table @asis
8197
8198 @item @code{-cargs @emph{switches}}
8199
8200 Compiler switches. Here @code{switches} is a list of switches
8201 that are valid switches for @code{gcc}. They will be passed on to
8202 all compile steps performed by @code{gnatmake}.
8203 @end table
8204
8205 @geindex -bargs (gnatmake)
8206
8207
8208 @table @asis
8209
8210 @item @code{-bargs @emph{switches}}
8211
8212 Binder switches. Here @code{switches} is a list of switches
8213 that are valid switches for @code{gnatbind}. They will be passed on to
8214 all bind steps performed by @code{gnatmake}.
8215 @end table
8216
8217 @geindex -largs (gnatmake)
8218
8219
8220 @table @asis
8221
8222 @item @code{-largs @emph{switches}}
8223
8224 Linker switches. Here @code{switches} is a list of switches
8225 that are valid switches for @code{gnatlink}. They will be passed on to
8226 all link steps performed by @code{gnatmake}.
8227 @end table
8228
8229 @geindex -margs (gnatmake)
8230
8231
8232 @table @asis
8233
8234 @item @code{-margs @emph{switches}}
8235
8236 Make switches. The switches are directly interpreted by @code{gnatmake},
8237 regardless of any previous occurrence of @code{-cargs}, @code{-bargs}
8238 or @code{-largs}.
8239 @end table
8240
8241 @node Notes on the Command Line,How gnatmake Works,Mode Switches for gnatmake,Building with gnatmake
8242 @anchor{gnat_ugn/building_executable_programs_with_gnat id5}@anchor{e1}@anchor{gnat_ugn/building_executable_programs_with_gnat notes-on-the-command-line}@anchor{e2}
8243 @subsection Notes on the Command Line
8244
8245
8246 This section contains some additional useful notes on the operation
8247 of the @code{gnatmake} command.
8248
8249 @geindex Recompilation (by gnatmake)
8250
8251
8252 @itemize *
8253
8254 @item
8255 If @code{gnatmake} finds no ALI files, it recompiles the main program
8256 and all other units required by the main program.
8257 This means that @code{gnatmake}
8258 can be used for the initial compile, as well as during subsequent steps of
8259 the development cycle.
8260
8261 @item
8262 If you enter @code{gnatmake foo.adb}, where @code{foo}
8263 is a subunit or body of a generic unit, @code{gnatmake} recompiles
8264 @code{foo.adb} (because it finds no ALI) and stops, issuing a
8265 warning.
8266
8267 @item
8268 In @code{gnatmake} the switch @code{-I}
8269 is used to specify both source and
8270 library file paths. Use @code{-aI}
8271 instead if you just want to specify
8272 source paths only and @code{-aO}
8273 if you want to specify library paths
8274 only.
8275
8276 @item
8277 @code{gnatmake} will ignore any files whose ALI file is write-protected.
8278 This may conveniently be used to exclude standard libraries from
8279 consideration and in particular it means that the use of the
8280 @code{-f} switch will not recompile these files
8281 unless @code{-a} is also specified.
8282
8283 @item
8284 @code{gnatmake} has been designed to make the use of Ada libraries
8285 particularly convenient. Assume you have an Ada library organized
8286 as follows: @emph{obj-dir} contains the objects and ALI files for
8287 of your Ada compilation units,
8288 whereas @emph{include-dir} contains the
8289 specs of these units, but no bodies. Then to compile a unit
8290 stored in @code{main.adb}, which uses this Ada library you would just type:
8291
8292 @example
8293 $ gnatmake -aI`include-dir` -aL`obj-dir` main
8294 @end example
8295
8296 @item
8297 Using @code{gnatmake} along with the @code{-m (minimal recompilation)}
8298 switch provides a mechanism for avoiding unnecessary recompilations. Using
8299 this switch,
8300 you can update the comments/format of your
8301 source files without having to recompile everything. Note, however, that
8302 adding or deleting lines in a source files may render its debugging
8303 info obsolete. If the file in question is a spec, the impact is rather
8304 limited, as that debugging info will only be useful during the
8305 elaboration phase of your program. For bodies the impact can be more
8306 significant. In all events, your debugger will warn you if a source file
8307 is more recent than the corresponding object, and alert you to the fact
8308 that the debugging information may be out of date.
8309 @end itemize
8310
8311 @node How gnatmake Works,Examples of gnatmake Usage,Notes on the Command Line,Building with gnatmake
8312 @anchor{gnat_ugn/building_executable_programs_with_gnat id6}@anchor{e3}@anchor{gnat_ugn/building_executable_programs_with_gnat how-gnatmake-works}@anchor{e4}
8313 @subsection How @code{gnatmake} Works
8314
8315
8316 Generally @code{gnatmake} automatically performs all necessary
8317 recompilations and you don't need to worry about how it works. However,
8318 it may be useful to have some basic understanding of the @code{gnatmake}
8319 approach and in particular to understand how it uses the results of
8320 previous compilations without incorrectly depending on them.
8321
8322 First a definition: an object file is considered @emph{up to date} if the
8323 corresponding ALI file exists and if all the source files listed in the
8324 dependency section of this ALI file have time stamps matching those in
8325 the ALI file. This means that neither the source file itself nor any
8326 files that it depends on have been modified, and hence there is no need
8327 to recompile this file.
8328
8329 @code{gnatmake} works by first checking if the specified main unit is up
8330 to date. If so, no compilations are required for the main unit. If not,
8331 @code{gnatmake} compiles the main program to build a new ALI file that
8332 reflects the latest sources. Then the ALI file of the main unit is
8333 examined to find all the source files on which the main program depends,
8334 and @code{gnatmake} recursively applies the above procedure on all these
8335 files.
8336
8337 This process ensures that @code{gnatmake} only trusts the dependencies
8338 in an existing ALI file if they are known to be correct. Otherwise it
8339 always recompiles to determine a new, guaranteed accurate set of
8340 dependencies. As a result the program is compiled 'upside down' from what may
8341 be more familiar as the required order of compilation in some other Ada
8342 systems. In particular, clients are compiled before the units on which
8343 they depend. The ability of GNAT to compile in any order is critical in
8344 allowing an order of compilation to be chosen that guarantees that
8345 @code{gnatmake} will recompute a correct set of new dependencies if
8346 necessary.
8347
8348 When invoking @code{gnatmake} with several @code{file_names}, if a unit is
8349 imported by several of the executables, it will be recompiled at most once.
8350
8351 Note: when using non-standard naming conventions
8352 (@ref{35,,Using Other File Names}), changing through a configuration pragmas
8353 file the version of a source and invoking @code{gnatmake} to recompile may
8354 have no effect, if the previous version of the source is still accessible
8355 by @code{gnatmake}. It may be necessary to use the switch
8356 -f.
8357
8358 @node Examples of gnatmake Usage,,How gnatmake Works,Building with gnatmake
8359 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatmake-usage}@anchor{e5}@anchor{gnat_ugn/building_executable_programs_with_gnat id7}@anchor{e6}
8360 @subsection Examples of @code{gnatmake} Usage
8361
8362
8363
8364 @table @asis
8365
8366 @item @emph{gnatmake hello.adb}
8367
8368 Compile all files necessary to bind and link the main program
8369 @code{hello.adb} (containing unit @code{Hello}) and bind and link the
8370 resulting object files to generate an executable file @code{hello}.
8371
8372 @item @emph{gnatmake main1 main2 main3}
8373
8374 Compile all files necessary to bind and link the main programs
8375 @code{main1.adb} (containing unit @code{Main1}), @code{main2.adb}
8376 (containing unit @code{Main2}) and @code{main3.adb}
8377 (containing unit @code{Main3}) and bind and link the resulting object files
8378 to generate three executable files @code{main1},
8379 @code{main2} and @code{main3}.
8380
8381 @item @emph{gnatmake -q Main_Unit -cargs -O2 -bargs -l}
8382
8383 Compile all files necessary to bind and link the main program unit
8384 @code{Main_Unit} (from file @code{main_unit.adb}). All compilations will
8385 be done with optimization level 2 and the order of elaboration will be
8386 listed by the binder. @code{gnatmake} will operate in quiet mode, not
8387 displaying commands it is executing.
8388 @end table
8389
8390 @node Compiling with gcc,Compiler Switches,Building with gnatmake,Building Executable Programs with GNAT
8391 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-with-gcc}@anchor{1c}@anchor{gnat_ugn/building_executable_programs_with_gnat id8}@anchor{e7}
8392 @section Compiling with @code{gcc}
8393
8394
8395 This section discusses how to compile Ada programs using the @code{gcc}
8396 command. It also describes the set of switches
8397 that can be used to control the behavior of the compiler.
8398
8399 @menu
8400 * Compiling Programs::
8401 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
8402 * Order of Compilation Issues::
8403 * Examples::
8404
8405 @end menu
8406
8407 @node Compiling Programs,Search Paths and the Run-Time Library RTL,,Compiling with gcc
8408 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-programs}@anchor{e8}@anchor{gnat_ugn/building_executable_programs_with_gnat id9}@anchor{e9}
8409 @subsection Compiling Programs
8410
8411
8412 The first step in creating an executable program is to compile the units
8413 of the program using the @code{gcc} command. You must compile the
8414 following files:
8415
8416
8417 @itemize *
8418
8419 @item
8420 the body file (@code{.adb}) for a library level subprogram or generic
8421 subprogram
8422
8423 @item
8424 the spec file (@code{.ads}) for a library level package or generic
8425 package that has no body
8426
8427 @item
8428 the body file (@code{.adb}) for a library level package
8429 or generic package that has a body
8430 @end itemize
8431
8432 You need @emph{not} compile the following files
8433
8434
8435 @itemize *
8436
8437 @item
8438 the spec of a library unit which has a body
8439
8440 @item
8441 subunits
8442 @end itemize
8443
8444 because they are compiled as part of compiling related units. GNAT
8445 package specs
8446 when the corresponding body is compiled, and subunits when the parent is
8447 compiled.
8448
8449 @geindex cannot generate code
8450
8451 If you attempt to compile any of these files, you will get one of the
8452 following error messages (where @code{fff} is the name of the file you
8453 compiled):
8454
8455 @quotation
8456
8457 @example
8458 cannot generate code for file `@w{`}fff`@w{`} (package spec)
8459 to check package spec, use -gnatc
8460
8461 cannot generate code for file `@w{`}fff`@w{`} (missing subunits)
8462 to check parent unit, use -gnatc
8463
8464 cannot generate code for file `@w{`}fff`@w{`} (subprogram spec)
8465 to check subprogram spec, use -gnatc
8466
8467 cannot generate code for file `@w{`}fff`@w{`} (subunit)
8468 to check subunit, use -gnatc
8469 @end example
8470 @end quotation
8471
8472 As indicated by the above error messages, if you want to submit
8473 one of these files to the compiler to check for correct semantics
8474 without generating code, then use the @code{-gnatc} switch.
8475
8476 The basic command for compiling a file containing an Ada unit is:
8477
8478 @example
8479 $ gcc -c [switches] <file name>
8480 @end example
8481
8482 where @code{file name} is the name of the Ada file (usually
8483 having an extension @code{.ads} for a spec or @code{.adb} for a body).
8484 You specify the
8485 @code{-c} switch to tell @code{gcc} to compile, but not link, the file.
8486 The result of a successful compilation is an object file, which has the
8487 same name as the source file but an extension of @code{.o} and an Ada
8488 Library Information (ALI) file, which also has the same name as the
8489 source file, but with @code{.ali} as the extension. GNAT creates these
8490 two output files in the current directory, but you may specify a source
8491 file in any directory using an absolute or relative path specification
8492 containing the directory information.
8493
8494 TESTING: the @code{--foobar@emph{NN}} switch
8495
8496 @geindex gnat1
8497
8498 @code{gcc} is actually a driver program that looks at the extensions of
8499 the file arguments and loads the appropriate compiler. For example, the
8500 GNU C compiler is @code{cc1}, and the Ada compiler is @code{gnat1}.
8501 These programs are in directories known to the driver program (in some
8502 configurations via environment variables you set), but need not be in
8503 your path. The @code{gcc} driver also calls the assembler and any other
8504 utilities needed to complete the generation of the required object
8505 files.
8506
8507 It is possible to supply several file names on the same @code{gcc}
8508 command. This causes @code{gcc} to call the appropriate compiler for
8509 each file. For example, the following command lists two separate
8510 files to be compiled:
8511
8512 @example
8513 $ gcc -c x.adb y.adb
8514 @end example
8515
8516 calls @code{gnat1} (the Ada compiler) twice to compile @code{x.adb} and
8517 @code{y.adb}.
8518 The compiler generates two object files @code{x.o} and @code{y.o}
8519 and the two ALI files @code{x.ali} and @code{y.ali}.
8520
8521 Any switches apply to all the files listed, see @ref{ea,,Compiler Switches} for a
8522 list of available @code{gcc} switches.
8523
8524 @node Search Paths and the Run-Time Library RTL,Order of Compilation Issues,Compiling Programs,Compiling with gcc
8525 @anchor{gnat_ugn/building_executable_programs_with_gnat id10}@anchor{eb}@anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-and-the-run-time-library-rtl}@anchor{89}
8526 @subsection Search Paths and the Run-Time Library (RTL)
8527
8528
8529 With the GNAT source-based library system, the compiler must be able to
8530 find source files for units that are needed by the unit being compiled.
8531 Search paths are used to guide this process.
8532
8533 The compiler compiles one source file whose name must be given
8534 explicitly on the command line. In other words, no searching is done
8535 for this file. To find all other source files that are needed (the most
8536 common being the specs of units), the compiler examines the following
8537 directories, in the following order:
8538
8539
8540 @itemize *
8541
8542 @item
8543 The directory containing the source file of the main unit being compiled
8544 (the file name on the command line).
8545
8546 @item
8547 Each directory named by an @code{-I} switch given on the @code{gcc}
8548 command line, in the order given.
8549
8550 @geindex ADA_PRJ_INCLUDE_FILE
8551
8552 @item
8553 Each of the directories listed in the text file whose name is given
8554 by the
8555 @geindex ADA_PRJ_INCLUDE_FILE
8556 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8557 @code{ADA_PRJ_INCLUDE_FILE} environment variable.
8558 @geindex ADA_PRJ_INCLUDE_FILE
8559 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8560 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the gnat
8561 driver when project files are used. It should not normally be set
8562 by other means.
8563
8564 @geindex ADA_INCLUDE_PATH
8565
8566 @item
8567 Each of the directories listed in the value of the
8568 @geindex ADA_INCLUDE_PATH
8569 @geindex environment variable; ADA_INCLUDE_PATH
8570 @code{ADA_INCLUDE_PATH} environment variable.
8571 Construct this value
8572 exactly as the
8573 @geindex PATH
8574 @geindex environment variable; PATH
8575 @code{PATH} environment variable: a list of directory
8576 names separated by colons (semicolons when working with the NT version).
8577
8578 @item
8579 The content of the @code{ada_source_path} file which is part of the GNAT
8580 installation tree and is used to store standard libraries such as the
8581 GNAT Run Time Library (RTL) source files.
8582 @ref{87,,Installing a library}
8583 @end itemize
8584
8585 Specifying the switch @code{-I-}
8586 inhibits the use of the directory
8587 containing the source file named in the command line. You can still
8588 have this directory on your search path, but in this case it must be
8589 explicitly requested with a @code{-I} switch.
8590
8591 Specifying the switch @code{-nostdinc}
8592 inhibits the search of the default location for the GNAT Run Time
8593 Library (RTL) source files.
8594
8595 The compiler outputs its object files and ALI files in the current
8596 working directory.
8597 Caution: The object file can be redirected with the @code{-o} switch;
8598 however, @code{gcc} and @code{gnat1} have not been coordinated on this
8599 so the @code{ALI} file will not go to the right place. Therefore, you should
8600 avoid using the @code{-o} switch.
8601
8602 @geindex System.IO
8603
8604 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8605 children make up the GNAT RTL, together with the simple @code{System.IO}
8606 package used in the @code{"Hello World"} example. The sources for these units
8607 are needed by the compiler and are kept together in one directory. Not
8608 all of the bodies are needed, but all of the sources are kept together
8609 anyway. In a normal installation, you need not specify these directory
8610 names when compiling or binding. Either the environment variables or
8611 the built-in defaults cause these files to be found.
8612
8613 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
8614 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
8615 consisting of child units of @code{GNAT}. This is a collection of generally
8616 useful types, subprograms, etc. See the @cite{GNAT_Reference_Manual}
8617 for further details.
8618
8619 Besides simplifying access to the RTL, a major use of search paths is
8620 in compiling sources from multiple directories. This can make
8621 development environments much more flexible.
8622
8623 @node Order of Compilation Issues,Examples,Search Paths and the Run-Time Library RTL,Compiling with gcc
8624 @anchor{gnat_ugn/building_executable_programs_with_gnat id11}@anchor{ec}@anchor{gnat_ugn/building_executable_programs_with_gnat order-of-compilation-issues}@anchor{ed}
8625 @subsection Order of Compilation Issues
8626
8627
8628 If, in our earlier example, there was a spec for the @code{hello}
8629 procedure, it would be contained in the file @code{hello.ads}; yet this
8630 file would not have to be explicitly compiled. This is the result of the
8631 model we chose to implement library management. Some of the consequences
8632 of this model are as follows:
8633
8634
8635 @itemize *
8636
8637 @item
8638 There is no point in compiling specs (except for package
8639 specs with no bodies) because these are compiled as needed by clients. If
8640 you attempt a useless compilation, you will receive an error message.
8641 It is also useless to compile subunits because they are compiled as needed
8642 by the parent.
8643
8644 @item
8645 There are no order of compilation requirements: performing a
8646 compilation never obsoletes anything. The only way you can obsolete
8647 something and require recompilations is to modify one of the
8648 source files on which it depends.
8649
8650 @item
8651 There is no library as such, apart from the ALI files
8652 (@ref{42,,The Ada Library Information Files}, for information on the format
8653 of these files). For now we find it convenient to create separate ALI files,
8654 but eventually the information therein may be incorporated into the object
8655 file directly.
8656
8657 @item
8658 When you compile a unit, the source files for the specs of all units
8659 that it @emph{with}s, all its subunits, and the bodies of any generics it
8660 instantiates must be available (reachable by the search-paths mechanism
8661 described above), or you will receive a fatal error message.
8662 @end itemize
8663
8664 @node Examples,,Order of Compilation Issues,Compiling with gcc
8665 @anchor{gnat_ugn/building_executable_programs_with_gnat id12}@anchor{ee}@anchor{gnat_ugn/building_executable_programs_with_gnat examples}@anchor{ef}
8666 @subsection Examples
8667
8668
8669 The following are some typical Ada compilation command line examples:
8670
8671 @example
8672 $ gcc -c xyz.adb
8673 @end example
8674
8675 Compile body in file @code{xyz.adb} with all default options.
8676
8677 @example
8678 $ gcc -c -O2 -gnata xyz-def.adb
8679 @end example
8680
8681 Compile the child unit package in file @code{xyz-def.adb} with extensive
8682 optimizations, and pragma @code{Assert}/@cite{Debug} statements
8683 enabled.
8684
8685 @example
8686 $ gcc -c -gnatc abc-def.adb
8687 @end example
8688
8689 Compile the subunit in file @code{abc-def.adb} in semantic-checking-only
8690 mode.
8691
8692 @node Compiler Switches,Linker Switches,Compiling with gcc,Building Executable Programs with GNAT
8693 @anchor{gnat_ugn/building_executable_programs_with_gnat compiler-switches}@anchor{f0}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gcc}@anchor{ea}
8694 @section Compiler Switches
8695
8696
8697 The @code{gcc} command accepts switches that control the
8698 compilation process. These switches are fully described in this section:
8699 first an alphabetical listing of all switches with a brief description,
8700 and then functionally grouped sets of switches with more detailed
8701 information.
8702
8703 More switches exist for GCC than those documented here, especially
8704 for specific targets. However, their use is not recommended as
8705 they may change code generation in ways that are incompatible with
8706 the Ada run-time library, or can cause inconsistencies between
8707 compilation units.
8708
8709 @menu
8710 * Alphabetical List of All Switches::
8711 * Output and Error Message Control::
8712 * Warning Message Control::
8713 * Debugging and Assertion Control::
8714 * Validity Checking::
8715 * Style Checking::
8716 * Run-Time Checks::
8717 * Using gcc for Syntax Checking::
8718 * Using gcc for Semantic Checking::
8719 * Compiling Different Versions of Ada::
8720 * Character Set Control::
8721 * File Naming Control::
8722 * Subprogram Inlining Control::
8723 * Auxiliary Output Control::
8724 * Debugging Control::
8725 * Exception Handling Control::
8726 * Units to Sources Mapping Files::
8727 * Code Generation Control::
8728
8729 @end menu
8730
8731 @node Alphabetical List of All Switches,Output and Error Message Control,,Compiler Switches
8732 @anchor{gnat_ugn/building_executable_programs_with_gnat id13}@anchor{f1}@anchor{gnat_ugn/building_executable_programs_with_gnat alphabetical-list-of-all-switches}@anchor{f2}
8733 @subsection Alphabetical List of All Switches
8734
8735
8736 @geindex -b (gcc)
8737
8738
8739 @table @asis
8740
8741 @item @code{-b @emph{target}}
8742
8743 Compile your program to run on @code{target}, which is the name of a
8744 system configuration. You must have a GNAT cross-compiler built if
8745 @code{target} is not the same as your host system.
8746 @end table
8747
8748 @geindex -B (gcc)
8749
8750
8751 @table @asis
8752
8753 @item @code{-B@emph{dir}}
8754
8755 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8756 from @code{dir} instead of the default location. Only use this switch
8757 when multiple versions of the GNAT compiler are available.
8758 See the "Options for Directory Search" section in the
8759 @cite{Using the GNU Compiler Collection (GCC)} manual for further details.
8760 You would normally use the @code{-b} or @code{-V} switch instead.
8761 @end table
8762
8763 @geindex -c (gcc)
8764
8765
8766 @table @asis
8767
8768 @item @code{-c}
8769
8770 Compile. Always use this switch when compiling Ada programs.
8771
8772 Note: for some other languages when using @code{gcc}, notably in
8773 the case of C and C++, it is possible to use
8774 use @code{gcc} without a @code{-c} switch to
8775 compile and link in one step. In the case of GNAT, you
8776 cannot use this approach, because the binder must be run
8777 and @code{gcc} cannot be used to run the GNAT binder.
8778 @end table
8779
8780 @geindex -fcallgraph-info (gcc)
8781
8782
8783 @table @asis
8784
8785 @item @code{-fcallgraph-info[=su,da]}
8786
8787 Makes the compiler output callgraph information for the program, on a
8788 per-file basis. The information is generated in the VCG format. It can
8789 be decorated with additional, per-node and/or per-edge information, if a
8790 list of comma-separated markers is additionally specified. When the
8791 @code{su} marker is specified, the callgraph is decorated with stack usage
8792 information; it is equivalent to @code{-fstack-usage}. When the @code{da}
8793 marker is specified, the callgraph is decorated with information about
8794 dynamically allocated objects.
8795 @end table
8796
8797 @geindex -fdump-scos (gcc)
8798
8799
8800 @table @asis
8801
8802 @item @code{-fdump-scos}
8803
8804 Generates SCO (Source Coverage Obligation) information in the ALI file.
8805 This information is used by advanced coverage tools. See unit @code{SCOs}
8806 in the compiler sources for details in files @code{scos.ads} and
8807 @code{scos.adb}.
8808 @end table
8809
8810 @geindex -fgnat-encodings (gcc)
8811
8812
8813 @table @asis
8814
8815 @item @code{-fgnat-encodings=[all|gdb|minimal]}
8816
8817 This switch controls the balance between GNAT encodings and standard DWARF
8818 emitted in the debug information.
8819 @end table
8820
8821 @geindex -flto (gcc)
8822
8823
8824 @table @asis
8825
8826 @item @code{-flto[=@emph{n}]}
8827
8828 Enables Link Time Optimization. This switch must be used in conjunction
8829 with the @code{-Ox} switches (but not with the @code{-gnatn} switch
8830 since it is a full replacement for the latter) and instructs the compiler
8831 to defer most optimizations until the link stage. The advantage of this
8832 approach is that the compiler can do a whole-program analysis and choose
8833 the best interprocedural optimization strategy based on a complete view
8834 of the program, instead of a fragmentary view with the usual approach.
8835 This can also speed up the compilation of big programs and reduce the
8836 size of the executable, compared with a traditional per-unit compilation
8837 with inlining across units enabled by the @code{-gnatn} switch.
8838 The drawback of this approach is that it may require more memory and that
8839 the debugging information generated by -g with it might be hardly usable.
8840 The switch, as well as the accompanying @code{-Ox} switches, must be
8841 specified both for the compilation and the link phases.
8842 If the @code{n} parameter is specified, the optimization and final code
8843 generation at link time are executed using @code{n} parallel jobs by
8844 means of an installed @code{make} program.
8845 @end table
8846
8847 @geindex -fno-inline (gcc)
8848
8849
8850 @table @asis
8851
8852 @item @code{-fno-inline}
8853
8854 Suppresses all inlining, unless requested with pragma @code{Inline_Always}. The
8855 effect is enforced regardless of other optimization or inlining switches.
8856 Note that inlining can also be suppressed on a finer-grained basis with
8857 pragma @code{No_Inline}.
8858 @end table
8859
8860 @geindex -fno-inline-functions (gcc)
8861
8862
8863 @table @asis
8864
8865 @item @code{-fno-inline-functions}
8866
8867 Suppresses automatic inlining of subprograms, which is enabled
8868 if @code{-O3} is used.
8869 @end table
8870
8871 @geindex -fno-inline-small-functions (gcc)
8872
8873
8874 @table @asis
8875
8876 @item @code{-fno-inline-small-functions}
8877
8878 Suppresses automatic inlining of small subprograms, which is enabled
8879 if @code{-O2} is used.
8880 @end table
8881
8882 @geindex -fno-inline-functions-called-once (gcc)
8883
8884
8885 @table @asis
8886
8887 @item @code{-fno-inline-functions-called-once}
8888
8889 Suppresses inlining of subprograms local to the unit and called once
8890 from within it, which is enabled if @code{-O1} is used.
8891 @end table
8892
8893 @geindex -fno-ivopts (gcc)
8894
8895
8896 @table @asis
8897
8898 @item @code{-fno-ivopts}
8899
8900 Suppresses high-level loop induction variable optimizations, which are
8901 enabled if @code{-O1} is used. These optimizations are generally
8902 profitable but, for some specific cases of loops with numerous uses
8903 of the iteration variable that follow a common pattern, they may end
8904 up destroying the regularity that could be exploited at a lower level
8905 and thus producing inferior code.
8906 @end table
8907
8908 @geindex -fno-strict-aliasing (gcc)
8909
8910
8911 @table @asis
8912
8913 @item @code{-fno-strict-aliasing}
8914
8915 Causes the compiler to avoid assumptions regarding non-aliasing
8916 of objects of different types. See
8917 @ref{f3,,Optimization and Strict Aliasing} for details.
8918 @end table
8919
8920 @geindex -fno-strict-overflow (gcc)
8921
8922
8923 @table @asis
8924
8925 @item @code{-fno-strict-overflow}
8926
8927 Causes the compiler to avoid assumptions regarding the rules of signed
8928 integer overflow. These rules specify that signed integer overflow will
8929 result in a Constraint_Error exception at run time and are enforced in
8930 default mode by the compiler, so this switch should not be necessary in
8931 normal operating mode. It might be useful in conjunction with @code{-gnato0}
8932 for very peculiar cases of low-level programming.
8933 @end table
8934
8935 @geindex -fstack-check (gcc)
8936
8937
8938 @table @asis
8939
8940 @item @code{-fstack-check}
8941
8942 Activates stack checking.
8943 See @ref{f4,,Stack Overflow Checking} for details.
8944 @end table
8945
8946 @geindex -fstack-usage (gcc)
8947
8948
8949 @table @asis
8950
8951 @item @code{-fstack-usage}
8952
8953 Makes the compiler output stack usage information for the program, on a
8954 per-subprogram basis. See @ref{f5,,Static Stack Usage Analysis} for details.
8955 @end table
8956
8957 @geindex -g (gcc)
8958
8959
8960 @table @asis
8961
8962 @item @code{-g}
8963
8964 Generate debugging information. This information is stored in the object
8965 file and copied from there to the final executable file by the linker,
8966 where it can be read by the debugger. You must use the
8967 @code{-g} switch if you plan on using the debugger.
8968 @end table
8969
8970 @geindex -gnat05 (gcc)
8971
8972
8973 @table @asis
8974
8975 @item @code{-gnat05}
8976
8977 Allow full Ada 2005 features.
8978 @end table
8979
8980 @geindex -gnat12 (gcc)
8981
8982
8983 @table @asis
8984
8985 @item @code{-gnat12}
8986
8987 Allow full Ada 2012 features.
8988 @end table
8989
8990 @geindex -gnat83 (gcc)
8991
8992 @geindex -gnat2005 (gcc)
8993
8994
8995 @table @asis
8996
8997 @item @code{-gnat2005}
8998
8999 Allow full Ada 2005 features (same as @code{-gnat05})
9000 @end table
9001
9002 @geindex -gnat2012 (gcc)
9003
9004
9005 @table @asis
9006
9007 @item @code{-gnat2012}
9008
9009 Allow full Ada 2012 features (same as @code{-gnat12})
9010
9011 @item @code{-gnat83}
9012
9013 Enforce Ada 83 restrictions.
9014 @end table
9015
9016 @geindex -gnat95 (gcc)
9017
9018
9019 @table @asis
9020
9021 @item @code{-gnat95}
9022
9023 Enforce Ada 95 restrictions.
9024
9025 Note: for compatibility with some Ada 95 compilers which support only
9026 the @code{overriding} keyword of Ada 2005, the @code{-gnatd.D} switch can
9027 be used along with @code{-gnat95} to achieve a similar effect with GNAT.
9028
9029 @code{-gnatd.D} instructs GNAT to consider @code{overriding} as a keyword
9030 and handle its associated semantic checks, even in Ada 95 mode.
9031 @end table
9032
9033 @geindex -gnata (gcc)
9034
9035
9036 @table @asis
9037
9038 @item @code{-gnata}
9039
9040 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
9041 activated. Note that these pragmas can also be controlled using the
9042 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
9043 It also activates pragmas @code{Check}, @code{Precondition}, and
9044 @code{Postcondition}. Note that these pragmas can also be controlled
9045 using the configuration pragma @code{Check_Policy}. In Ada 2012, it
9046 also activates all assertions defined in the RM as aspects: preconditions,
9047 postconditions, type invariants and (sub)type predicates. In all Ada modes,
9048 corresponding pragmas for type invariants and (sub)type predicates are
9049 also activated. The default is that all these assertions are disabled,
9050 and have no effect, other than being checked for syntactic validity, and
9051 in the case of subtype predicates, constructions such as membership tests
9052 still test predicates even if assertions are turned off.
9053 @end table
9054
9055 @geindex -gnatA (gcc)
9056
9057
9058 @table @asis
9059
9060 @item @code{-gnatA}
9061
9062 Avoid processing @code{gnat.adc}. If a @code{gnat.adc} file is present,
9063 it will be ignored.
9064 @end table
9065
9066 @geindex -gnatb (gcc)
9067
9068
9069 @table @asis
9070
9071 @item @code{-gnatb}
9072
9073 Generate brief messages to @code{stderr} even if verbose mode set.
9074 @end table
9075
9076 @geindex -gnatB (gcc)
9077
9078
9079 @table @asis
9080
9081 @item @code{-gnatB}
9082
9083 Assume no invalid (bad) values except for 'Valid attribute use
9084 (@ref{f6,,Validity Checking}).
9085 @end table
9086
9087 @geindex -gnatc (gcc)
9088
9089
9090 @table @asis
9091
9092 @item @code{-gnatc}
9093
9094 Check syntax and semantics only (no code generation attempted). When the
9095 compiler is invoked by @code{gnatmake}, if the switch @code{-gnatc} is
9096 only given to the compiler (after @code{-cargs} or in package Compiler of
9097 the project file, @code{gnatmake} will fail because it will not find the
9098 object file after compilation. If @code{gnatmake} is called with
9099 @code{-gnatc} as a builder switch (before @code{-cargs} or in package
9100 Builder of the project file) then @code{gnatmake} will not fail because
9101 it will not look for the object files after compilation, and it will not try
9102 to build and link.
9103 @end table
9104
9105 @geindex -gnatC (gcc)
9106
9107
9108 @table @asis
9109
9110 @item @code{-gnatC}
9111
9112 Generate CodePeer intermediate format (no code generation attempted).
9113 This switch will generate an intermediate representation suitable for
9114 use by CodePeer (@code{.scil} files). This switch is not compatible with
9115 code generation (it will, among other things, disable some switches such
9116 as -gnatn, and enable others such as -gnata).
9117 @end table
9118
9119 @geindex -gnatd (gcc)
9120
9121
9122 @table @asis
9123
9124 @item @code{-gnatd}
9125
9126 Specify debug options for the compiler. The string of characters after
9127 the @code{-gnatd} specify the specific debug options. The possible
9128 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
9129 compiler source file @code{debug.adb} for details of the implemented
9130 debug options. Certain debug options are relevant to applications
9131 programmers, and these are documented at appropriate points in this
9132 users guide.
9133 @end table
9134
9135 @geindex -gnatD[nn] (gcc)
9136
9137
9138 @table @asis
9139
9140 @item @code{-gnatD}
9141
9142 Create expanded source files for source level debugging. This switch
9143 also suppresses generation of cross-reference information
9144 (see @code{-gnatx}). Note that this switch is not allowed if a previous
9145 -gnatR switch has been given, since these two switches are not compatible.
9146 @end table
9147
9148 @geindex -gnateA (gcc)
9149
9150
9151 @table @asis
9152
9153 @item @code{-gnateA}
9154
9155 Check that the actual parameters of a subprogram call are not aliases of one
9156 another. To qualify as aliasing, the actuals must denote objects of a composite
9157 type, their memory locations must be identical or overlapping, and at least one
9158 of the corresponding formal parameters must be of mode OUT or IN OUT.
9159
9160 @example
9161 type Rec_Typ is record
9162 Data : Integer := 0;
9163 end record;
9164
9165 function Self (Val : Rec_Typ) return Rec_Typ is
9166 begin
9167 return Val;
9168 end Self;
9169
9170 procedure Detect_Aliasing (Val_1 : in out Rec_Typ; Val_2 : Rec_Typ) is
9171 begin
9172 null;
9173 end Detect_Aliasing;
9174
9175 Obj : Rec_Typ;
9176
9177 Detect_Aliasing (Obj, Obj);
9178 Detect_Aliasing (Obj, Self (Obj));
9179 @end example
9180
9181 In the example above, the first call to @code{Detect_Aliasing} fails with a
9182 @code{Program_Error} at run time because the actuals for @code{Val_1} and
9183 @code{Val_2} denote the same object. The second call executes without raising
9184 an exception because @code{Self(Obj)} produces an anonymous object which does
9185 not share the memory location of @code{Obj}.
9186 @end table
9187
9188 @geindex -gnatec (gcc)
9189
9190
9191 @table @asis
9192
9193 @item @code{-gnatec=@emph{path}}
9194
9195 Specify a configuration pragma file
9196 (the equal sign is optional)
9197 (@ref{79,,The Configuration Pragmas Files}).
9198 @end table
9199
9200 @geindex -gnateC (gcc)
9201
9202
9203 @table @asis
9204
9205 @item @code{-gnateC}
9206
9207 Generate CodePeer messages in a compiler-like format. This switch is only
9208 effective if @code{-gnatcC} is also specified and requires an installation
9209 of CodePeer.
9210 @end table
9211
9212 @geindex -gnated (gcc)
9213
9214
9215 @table @asis
9216
9217 @item @code{-gnated}
9218
9219 Disable atomic synchronization
9220 @end table
9221
9222 @geindex -gnateD (gcc)
9223
9224
9225 @table @asis
9226
9227 @item @code{-gnateDsymbol[=@emph{value}]}
9228
9229 Defines a symbol, associated with @code{value}, for preprocessing.
9230 (@ref{18,,Integrated Preprocessing}).
9231 @end table
9232
9233 @geindex -gnateE (gcc)
9234
9235
9236 @table @asis
9237
9238 @item @code{-gnateE}
9239
9240 Generate extra information in exception messages. In particular, display
9241 extra column information and the value and range associated with index and
9242 range check failures, and extra column information for access checks.
9243 In cases where the compiler is able to determine at compile time that
9244 a check will fail, it gives a warning, and the extra information is not
9245 produced at run time.
9246 @end table
9247
9248 @geindex -gnatef (gcc)
9249
9250
9251 @table @asis
9252
9253 @item @code{-gnatef}
9254
9255 Display full source path name in brief error messages.
9256 @end table
9257
9258 @geindex -gnateF (gcc)
9259
9260
9261 @table @asis
9262
9263 @item @code{-gnateF}
9264
9265 Check for overflow on all floating-point operations, including those
9266 for unconstrained predefined types. See description of pragma
9267 @code{Check_Float_Overflow} in GNAT RM.
9268 @end table
9269
9270 @geindex -gnateg (gcc)
9271
9272 @code{-gnateg}
9273 @code{-gnatceg}
9274
9275 @quotation
9276
9277 The @code{-gnatc} switch must always be specified before this switch, e.g.
9278 @code{-gnatceg}. Generate a C header from the Ada input file. See
9279 @ref{ca,,Generating C Headers for Ada Specifications} for more
9280 information.
9281 @end quotation
9282
9283 @geindex -gnateG (gcc)
9284
9285
9286 @table @asis
9287
9288 @item @code{-gnateG}
9289
9290 Save result of preprocessing in a text file.
9291 @end table
9292
9293 @geindex -gnatei (gcc)
9294
9295
9296 @table @asis
9297
9298 @item @code{-gnatei@emph{nnn}}
9299
9300 Set maximum number of instantiations during compilation of a single unit to
9301 @code{nnn}. This may be useful in increasing the default maximum of 8000 for
9302 the rare case when a single unit legitimately exceeds this limit.
9303 @end table
9304
9305 @geindex -gnateI (gcc)
9306
9307
9308 @table @asis
9309
9310 @item @code{-gnateI@emph{nnn}}
9311
9312 Indicates that the source is a multi-unit source and that the index of the
9313 unit to compile is @code{nnn}. @code{nnn} needs to be a positive number and need
9314 to be a valid index in the multi-unit source.
9315 @end table
9316
9317 @geindex -gnatel (gcc)
9318
9319
9320 @table @asis
9321
9322 @item @code{-gnatel}
9323
9324 This switch can be used with the static elaboration model to issue info
9325 messages showing
9326 where implicit @code{pragma Elaborate} and @code{pragma Elaborate_All}
9327 are generated. This is useful in diagnosing elaboration circularities
9328 caused by these implicit pragmas when using the static elaboration
9329 model. See See the section in this guide on elaboration checking for
9330 further details. These messages are not generated by default, and are
9331 intended only for temporary use when debugging circularity problems.
9332 @end table
9333
9334 @geindex -gnatel (gcc)
9335
9336
9337 @table @asis
9338
9339 @item @code{-gnateL}
9340
9341 This switch turns off the info messages about implicit elaboration pragmas.
9342 @end table
9343
9344 @geindex -gnatem (gcc)
9345
9346
9347 @table @asis
9348
9349 @item @code{-gnatem=@emph{path}}
9350
9351 Specify a mapping file
9352 (the equal sign is optional)
9353 (@ref{f7,,Units to Sources Mapping Files}).
9354 @end table
9355
9356 @geindex -gnatep (gcc)
9357
9358
9359 @table @asis
9360
9361 @item @code{-gnatep=@emph{file}}
9362
9363 Specify a preprocessing data file
9364 (the equal sign is optional)
9365 (@ref{18,,Integrated Preprocessing}).
9366 @end table
9367
9368 @geindex -gnateP (gcc)
9369
9370
9371 @table @asis
9372
9373 @item @code{-gnateP}
9374
9375 Turn categorization dependency errors into warnings.
9376 Ada requires that units that WITH one another have compatible categories, for
9377 example a Pure unit cannot WITH a Preelaborate unit. If this switch is used,
9378 these errors become warnings (which can be ignored, or suppressed in the usual
9379 manner). This can be useful in some specialized circumstances such as the
9380 temporary use of special test software.
9381 @end table
9382
9383 @geindex -gnateS (gcc)
9384
9385
9386 @table @asis
9387
9388 @item @code{-gnateS}
9389
9390 Synonym of @code{-fdump-scos}, kept for backwards compatibility.
9391 @end table
9392
9393 @geindex -gnatet=file (gcc)
9394
9395
9396 @table @asis
9397
9398 @item @code{-gnatet=@emph{path}}
9399
9400 Generate target dependent information. The format of the output file is
9401 described in the section about switch @code{-gnateT}.
9402 @end table
9403
9404 @geindex -gnateT (gcc)
9405
9406
9407 @table @asis
9408
9409 @item @code{-gnateT=@emph{path}}
9410
9411 Read target dependent information, such as endianness or sizes and alignments
9412 of base type. If this switch is passed, the default target dependent
9413 information of the compiler is replaced by the one read from the input file.
9414 This is used by tools other than the compiler, e.g. to do
9415 semantic analysis of programs that will run on some other target than
9416 the machine on which the tool is run.
9417
9418 The following target dependent values should be defined,
9419 where @code{Nat} denotes a natural integer value, @code{Pos} denotes a
9420 positive integer value, and fields marked with a question mark are
9421 boolean fields, where a value of 0 is False, and a value of 1 is True:
9422
9423 @example
9424 Bits_BE : Nat; -- Bits stored big-endian?
9425 Bits_Per_Unit : Pos; -- Bits in a storage unit
9426 Bits_Per_Word : Pos; -- Bits in a word
9427 Bytes_BE : Nat; -- Bytes stored big-endian?
9428 Char_Size : Pos; -- Standard.Character'Size
9429 Double_Float_Alignment : Nat; -- Alignment of double float
9430 Double_Scalar_Alignment : Nat; -- Alignment of double length scalar
9431 Double_Size : Pos; -- Standard.Long_Float'Size
9432 Float_Size : Pos; -- Standard.Float'Size
9433 Float_Words_BE : Nat; -- Float words stored big-endian?
9434 Int_Size : Pos; -- Standard.Integer'Size
9435 Long_Double_Size : Pos; -- Standard.Long_Long_Float'Size
9436 Long_Long_Size : Pos; -- Standard.Long_Long_Integer'Size
9437 Long_Size : Pos; -- Standard.Long_Integer'Size
9438 Maximum_Alignment : Pos; -- Maximum permitted alignment
9439 Max_Unaligned_Field : Pos; -- Maximum size for unaligned bit field
9440 Pointer_Size : Pos; -- System.Address'Size
9441 Short_Enums : Nat; -- Foreign enums use short size?
9442 Short_Size : Pos; -- Standard.Short_Integer'Size
9443 Strict_Alignment : Nat; -- Strict alignment?
9444 System_Allocator_Alignment : Nat; -- Alignment for malloc calls
9445 Wchar_T_Size : Pos; -- Interfaces.C.wchar_t'Size
9446 Words_BE : Nat; -- Words stored big-endian?
9447 @end example
9448
9449 @code{Bits_Per_Unit} is the number of bits in a storage unit, the equivalent of
9450 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.}
9451
9452 @code{Bits_Per_Word} is the number of bits in a machine word, the equivalent of
9453 GCC macro @code{BITS_PER_WORD} documented as follows: @cite{Number of bits in a word; normally 32.}
9454
9455 @code{Double_Float_Alignment}, if not zero, is the maximum alignment that the
9456 compiler can choose by default for a 64-bit floating-point type or object.
9457
9458 @code{Double_Scalar_Alignment}, if not zero, is the maximum alignment that the
9459 compiler can choose by default for a 64-bit or larger scalar type or object.
9460
9461 @code{Maximum_Alignment} is the maximum alignment that the compiler can choose
9462 by default for a type or object, which is also the maximum alignment that can
9463 be specified in GNAT. It is computed for GCC backends as @code{BIGGEST_ALIGNMENT
9464 / BITS_PER_UNIT} where GCC macro @code{BIGGEST_ALIGNMENT} is documented as
9465 follows: @cite{Biggest alignment that any data type can require on this machine@comma{} in bits.}
9466
9467 @code{Max_Unaligned_Field} is the maximum size for unaligned bit field, which is
9468 64 for the majority of GCC targets (but can be different on some targets like
9469 AAMP).
9470
9471 @code{Strict_Alignment} is the equivalent of GCC macro @code{STRICT_ALIGNMENT}
9472 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.}
9473
9474 @code{System_Allocator_Alignment} is the guaranteed alignment of data returned
9475 by calls to @code{malloc}.
9476
9477 The format of the input file is as follows. First come the values of
9478 the variables defined above, with one line per value:
9479
9480 @example
9481 name value
9482 @end example
9483
9484 where @code{name} is the name of the parameter, spelled out in full,
9485 and cased as in the above list, and @code{value} is an unsigned decimal
9486 integer. Two or more blanks separates the name from the value.
9487
9488 All the variables must be present, in alphabetical order (i.e. the
9489 same order as the list above).
9490
9491 Then there is a blank line to separate the two parts of the file. Then
9492 come the lines showing the floating-point types to be registered, with
9493 one line per registered mode:
9494
9495 @example
9496 name digs float_rep size alignment
9497 @end example
9498
9499 where @code{name} is the string name of the type (which can have
9500 single spaces embedded in the name (e.g. long double), @code{digs} is
9501 the number of digits for the floating-point type, @code{float_rep} is
9502 the float representation (I/V/A for IEEE-754-Binary, Vax_Native,
9503 AAMP), @code{size} is the size in bits, @code{alignment} is the
9504 alignment in bits. The name is followed by at least two blanks, fields
9505 are separated by at least one blank, and a LF character immediately
9506 follows the alignment field.
9507
9508 Here is an example of a target parameterization file:
9509
9510 @example
9511 Bits_BE 0
9512 Bits_Per_Unit 8
9513 Bits_Per_Word 64
9514 Bytes_BE 0
9515 Char_Size 8
9516 Double_Float_Alignment 0
9517 Double_Scalar_Alignment 0
9518 Double_Size 64
9519 Float_Size 32
9520 Float_Words_BE 0
9521 Int_Size 64
9522 Long_Double_Size 128
9523 Long_Long_Size 64
9524 Long_Size 64
9525 Maximum_Alignment 16
9526 Max_Unaligned_Field 64
9527 Pointer_Size 64
9528 Short_Size 16
9529 Strict_Alignment 0
9530 System_Allocator_Alignment 16
9531 Wchar_T_Size 32
9532 Words_BE 0
9533
9534 float 15 I 64 64
9535 double 15 I 64 64
9536 long double 18 I 80 128
9537 TF 33 I 128 128
9538 @end example
9539 @end table
9540
9541 @geindex -gnateu (gcc)
9542
9543
9544 @table @asis
9545
9546 @item @code{-gnateu}
9547
9548 Ignore unrecognized validity, warning, and style switches that
9549 appear after this switch is given. This may be useful when
9550 compiling sources developed on a later version of the compiler
9551 with an earlier version. Of course the earlier version must
9552 support this switch.
9553 @end table
9554
9555 @geindex -gnateV (gcc)
9556
9557
9558 @table @asis
9559
9560 @item @code{-gnateV}
9561
9562 Check that all actual parameters of a subprogram call are valid according to
9563 the rules of validity checking (@ref{f6,,Validity Checking}).
9564 @end table
9565
9566 @geindex -gnateY (gcc)
9567
9568
9569 @table @asis
9570
9571 @item @code{-gnateY}
9572
9573 Ignore all STYLE_CHECKS pragmas. Full legality checks
9574 are still carried out, but the pragmas have no effect
9575 on what style checks are active. This allows all style
9576 checking options to be controlled from the command line.
9577 @end table
9578
9579 @geindex -gnatE (gcc)
9580
9581
9582 @table @asis
9583
9584 @item @code{-gnatE}
9585
9586 Dynamic elaboration checking mode enabled. For further details see
9587 @ref{f,,Elaboration Order Handling in GNAT}.
9588 @end table
9589
9590 @geindex -gnatf (gcc)
9591
9592
9593 @table @asis
9594
9595 @item @code{-gnatf}
9596
9597 Full errors. Multiple errors per line, all undefined references, do not
9598 attempt to suppress cascaded errors.
9599 @end table
9600
9601 @geindex -gnatF (gcc)
9602
9603
9604 @table @asis
9605
9606 @item @code{-gnatF}
9607
9608 Externals names are folded to all uppercase.
9609 @end table
9610
9611 @geindex -gnatg (gcc)
9612
9613
9614 @table @asis
9615
9616 @item @code{-gnatg}
9617
9618 Internal GNAT implementation mode. This should not be used for applications
9619 programs, it is intended only for use by the compiler and its run-time
9620 library. For documentation, see the GNAT sources. Note that @code{-gnatg}
9621 implies @code{-gnatw.ge} and @code{-gnatyg} so that all standard
9622 warnings and all standard style options are turned on. All warnings and style
9623 messages are treated as errors.
9624 @end table
9625
9626 @geindex -gnatG[nn] (gcc)
9627
9628
9629 @table @asis
9630
9631 @item @code{-gnatG=nn}
9632
9633 List generated expanded code in source form.
9634 @end table
9635
9636 @geindex -gnath (gcc)
9637
9638
9639 @table @asis
9640
9641 @item @code{-gnath}
9642
9643 Output usage information. The output is written to @code{stdout}.
9644 @end table
9645
9646 @geindex -gnatH (gcc)
9647
9648
9649 @table @asis
9650
9651 @item @code{-gnatH}
9652
9653 Legacy elaboration-checking mode enabled. When this switch is in effect,
9654 the pre-18.x access-before-elaboration model becomes the de facto model.
9655 For further details see @ref{f,,Elaboration Order Handling in GNAT}.
9656 @end table
9657
9658 @geindex -gnati (gcc)
9659
9660
9661 @table @asis
9662
9663 @item @code{-gnati@emph{c}}
9664
9665 Identifier character set (@code{c} = 1/2/3/4/8/9/p/f/n/w).
9666 For details of the possible selections for @code{c},
9667 see @ref{48,,Character Set Control}.
9668 @end table
9669
9670 @geindex -gnatI (gcc)
9671
9672
9673 @table @asis
9674
9675 @item @code{-gnatI}
9676
9677 Ignore representation clauses. When this switch is used,
9678 representation clauses are treated as comments. This is useful
9679 when initially porting code where you want to ignore rep clause
9680 problems, and also for compiling foreign code (particularly
9681 for use with ASIS). The representation clauses that are ignored
9682 are: enumeration_representation_clause, record_representation_clause,
9683 and attribute_definition_clause for the following attributes:
9684 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
9685 Object_Size, Scalar_Storage_Order, Size, Small, Stream_Size,
9686 and Value_Size. Pragma Default_Scalar_Storage_Order is also ignored.
9687 Note that this option should be used only for compiling -- the
9688 code is likely to malfunction at run time.
9689
9690 Note that when @code{-gnatct} is used to generate trees for input
9691 into ASIS tools, these representation clauses are removed
9692 from the tree and ignored. This means that the tool will not see them.
9693 @end table
9694
9695 @geindex -gnatjnn (gcc)
9696
9697
9698 @table @asis
9699
9700 @item @code{-gnatj@emph{nn}}
9701
9702 Reformat error messages to fit on @code{nn} character lines
9703 @end table
9704
9705 @geindex -gnatJ (gcc)
9706
9707
9708 @table @asis
9709
9710 @item @code{-gnatJ}
9711
9712 Permissive elaboration-checking mode enabled. When this switch is in effect,
9713 the post-18.x access-before-elaboration model ignores potential issues with:
9714
9715
9716 @itemize -
9717
9718 @item
9719 Accept statements
9720
9721 @item
9722 Activations of tasks defined in instances
9723
9724 @item
9725 Assertion pragmas
9726
9727 @item
9728 Calls from within an instance to its enclosing context
9729
9730 @item
9731 Calls through generic formal parameters
9732
9733 @item
9734 Calls to subprograms defined in instances
9735
9736 @item
9737 Entry calls
9738
9739 @item
9740 Indirect calls using 'Access
9741
9742 @item
9743 Requeue statements
9744
9745 @item
9746 Select statements
9747
9748 @item
9749 Synchronous task suspension
9750 @end itemize
9751
9752 and does not emit compile-time diagnostics or run-time checks. For further
9753 details see @ref{f,,Elaboration Order Handling in GNAT}.
9754 @end table
9755
9756 @geindex -gnatk (gcc)
9757
9758
9759 @table @asis
9760
9761 @item @code{-gnatk=@emph{n}}
9762
9763 Limit file names to @code{n} (1-999) characters (@code{k} = krunch).
9764 @end table
9765
9766 @geindex -gnatl (gcc)
9767
9768
9769 @table @asis
9770
9771 @item @code{-gnatl}
9772
9773 Output full source listing with embedded error messages.
9774 @end table
9775
9776 @geindex -gnatL (gcc)
9777
9778
9779 @table @asis
9780
9781 @item @code{-gnatL}
9782
9783 Used in conjunction with -gnatG or -gnatD to intersperse original
9784 source lines (as comment lines with line numbers) in the expanded
9785 source output.
9786 @end table
9787
9788 @geindex -gnatm (gcc)
9789
9790
9791 @table @asis
9792
9793 @item @code{-gnatm=@emph{n}}
9794
9795 Limit number of detected error or warning messages to @code{n}
9796 where @code{n} is in the range 1..999999. The default setting if
9797 no switch is given is 9999. If the number of warnings reaches this
9798 limit, then a message is output and further warnings are suppressed,
9799 but the compilation is continued. If the number of error messages
9800 reaches this limit, then a message is output and the compilation
9801 is abandoned. The equal sign here is optional. A value of zero
9802 means that no limit applies.
9803 @end table
9804
9805 @geindex -gnatn (gcc)
9806
9807
9808 @table @asis
9809
9810 @item @code{-gnatn[12]}
9811
9812 Activate inlining across units for subprograms for which pragma @code{Inline}
9813 is specified. This inlining is performed by the GCC back-end. An optional
9814 digit sets the inlining level: 1 for moderate inlining across units
9815 or 2 for full inlining across units. If no inlining level is specified,
9816 the compiler will pick it based on the optimization level.
9817 @end table
9818
9819 @geindex -gnatN (gcc)
9820
9821
9822 @table @asis
9823
9824 @item @code{-gnatN}
9825
9826 Activate front end inlining for subprograms for which
9827 pragma @code{Inline} is specified. This inlining is performed
9828 by the front end and will be visible in the
9829 @code{-gnatG} output.
9830
9831 When using a gcc-based back end (in practice this means using any version
9832 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
9833 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
9834 Historically front end inlining was more extensive than the gcc back end
9835 inlining, but that is no longer the case.
9836 @end table
9837
9838 @geindex -gnato0 (gcc)
9839
9840
9841 @table @asis
9842
9843 @item @code{-gnato0}
9844
9845 Suppresses overflow checking. This causes the behavior of the compiler to
9846 match the default for older versions where overflow checking was suppressed
9847 by default. This is equivalent to having
9848 @code{pragma Suppress (Overflow_Check)} in a configuration pragma file.
9849 @end table
9850
9851 @geindex -gnato?? (gcc)
9852
9853
9854 @table @asis
9855
9856 @item @code{-gnato??}
9857
9858 Set default mode for handling generation of code to avoid intermediate
9859 arithmetic overflow. Here @code{??} is two digits, a
9860 single digit, or nothing. Each digit is one of the digits @code{1}
9861 through @code{3}:
9862
9863
9864 @multitable {xxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
9865 @item
9866
9867 Digit
9868
9869 @tab
9870
9871 Interpretation
9872
9873 @item
9874
9875 @emph{1}
9876
9877 @tab
9878
9879 All intermediate overflows checked against base type (@code{STRICT})
9880
9881 @item
9882
9883 @emph{2}
9884
9885 @tab
9886
9887 Minimize intermediate overflows (@code{MINIMIZED})
9888
9889 @item
9890
9891 @emph{3}
9892
9893 @tab
9894
9895 Eliminate intermediate overflows (@code{ELIMINATED})
9896
9897 @end multitable
9898
9899
9900 If only one digit appears, then it applies to all
9901 cases; if two digits are given, then the first applies outside
9902 assertions, pre/postconditions, and type invariants, and the second
9903 applies within assertions, pre/postconditions, and type invariants.
9904
9905 If no digits follow the @code{-gnato}, then it is equivalent to
9906 @code{-gnato11},
9907 causing all intermediate overflows to be handled in strict
9908 mode.
9909
9910 This switch also causes arithmetic overflow checking to be performed
9911 (as though @code{pragma Unsuppress (Overflow_Check)} had been specified).
9912
9913 The default if no option @code{-gnato} is given is that overflow handling
9914 is in @code{STRICT} mode (computations done using the base type), and that
9915 overflow checking is enabled.
9916
9917 Note that division by zero is a separate check that is not
9918 controlled by this switch (divide-by-zero checking is on by default).
9919
9920 See also @ref{f8,,Specifying the Desired Mode}.
9921 @end table
9922
9923 @geindex -gnatp (gcc)
9924
9925
9926 @table @asis
9927
9928 @item @code{-gnatp}
9929
9930 Suppress all checks. See @ref{f9,,Run-Time Checks} for details. This switch
9931 has no effect if cancelled by a subsequent @code{-gnat-p} switch.
9932 @end table
9933
9934 @geindex -gnat-p (gcc)
9935
9936
9937 @table @asis
9938
9939 @item @code{-gnat-p}
9940
9941 Cancel effect of previous @code{-gnatp} switch.
9942 @end table
9943
9944 @geindex -gnatP (gcc)
9945
9946
9947 @table @asis
9948
9949 @item @code{-gnatP}
9950
9951 Enable polling. This is required on some systems (notably Windows NT) to
9952 obtain asynchronous abort and asynchronous transfer of control capability.
9953 See @code{Pragma_Polling} in the @cite{GNAT_Reference_Manual} for full
9954 details.
9955 @end table
9956
9957 @geindex -gnatq (gcc)
9958
9959
9960 @table @asis
9961
9962 @item @code{-gnatq}
9963
9964 Don't quit. Try semantics, even if parse errors.
9965 @end table
9966
9967 @geindex -gnatQ (gcc)
9968
9969
9970 @table @asis
9971
9972 @item @code{-gnatQ}
9973
9974 Don't quit. Generate @code{ALI} and tree files even if illegalities.
9975 Note that code generation is still suppressed in the presence of any
9976 errors, so even with @code{-gnatQ} no object file is generated.
9977 @end table
9978
9979 @geindex -gnatr (gcc)
9980
9981
9982 @table @asis
9983
9984 @item @code{-gnatr}
9985
9986 Treat pragma Restrictions as Restriction_Warnings.
9987 @end table
9988
9989 @geindex -gnatR (gcc)
9990
9991
9992 @table @asis
9993
9994 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
9995
9996 Output representation information for declared types, objects and
9997 subprograms. Note that this switch is not allowed if a previous
9998 @code{-gnatD} switch has been given, since these two switches
9999 are not compatible.
10000 @end table
10001
10002 @geindex -gnats (gcc)
10003
10004
10005 @table @asis
10006
10007 @item @code{-gnats}
10008
10009 Syntax check only.
10010 @end table
10011
10012 @geindex -gnatS (gcc)
10013
10014
10015 @table @asis
10016
10017 @item @code{-gnatS}
10018
10019 Print package Standard.
10020 @end table
10021
10022 @geindex -gnatt (gcc)
10023
10024
10025 @table @asis
10026
10027 @item @code{-gnatt}
10028
10029 Generate tree output file.
10030 @end table
10031
10032 @geindex -gnatT (gcc)
10033
10034
10035 @table @asis
10036
10037 @item @code{-gnatT@emph{nnn}}
10038
10039 All compiler tables start at @code{nnn} times usual starting size.
10040 @end table
10041
10042 @geindex -gnatu (gcc)
10043
10044
10045 @table @asis
10046
10047 @item @code{-gnatu}
10048
10049 List units for this compilation.
10050 @end table
10051
10052 @geindex -gnatU (gcc)
10053
10054
10055 @table @asis
10056
10057 @item @code{-gnatU}
10058
10059 Tag all error messages with the unique string 'error:'
10060 @end table
10061
10062 @geindex -gnatv (gcc)
10063
10064
10065 @table @asis
10066
10067 @item @code{-gnatv}
10068
10069 Verbose mode. Full error output with source lines to @code{stdout}.
10070 @end table
10071
10072 @geindex -gnatV (gcc)
10073
10074
10075 @table @asis
10076
10077 @item @code{-gnatV}
10078
10079 Control level of validity checking (@ref{f6,,Validity Checking}).
10080 @end table
10081
10082 @geindex -gnatw (gcc)
10083
10084
10085 @table @asis
10086
10087 @item @code{-gnatw@emph{xxx}}
10088
10089 Warning mode where
10090 @code{xxx} is a string of option letters that denotes
10091 the exact warnings that
10092 are enabled or disabled (@ref{fa,,Warning Message Control}).
10093 @end table
10094
10095 @geindex -gnatW (gcc)
10096
10097
10098 @table @asis
10099
10100 @item @code{-gnatW@emph{e}}
10101
10102 Wide character encoding method
10103 (@code{e}=n/h/u/s/e/8).
10104 @end table
10105
10106 @geindex -gnatx (gcc)
10107
10108
10109 @table @asis
10110
10111 @item @code{-gnatx}
10112
10113 Suppress generation of cross-reference information.
10114 @end table
10115
10116 @geindex -gnatX (gcc)
10117
10118
10119 @table @asis
10120
10121 @item @code{-gnatX}
10122
10123 Enable GNAT implementation extensions and latest Ada version.
10124 @end table
10125
10126 @geindex -gnaty (gcc)
10127
10128
10129 @table @asis
10130
10131 @item @code{-gnaty}
10132
10133 Enable built-in style checks (@ref{fb,,Style Checking}).
10134 @end table
10135
10136 @geindex -gnatz (gcc)
10137
10138
10139 @table @asis
10140
10141 @item @code{-gnatz@emph{m}}
10142
10143 Distribution stub generation and compilation
10144 (@code{m}=r/c for receiver/caller stubs).
10145 @end table
10146
10147 @geindex -I (gcc)
10148
10149
10150 @table @asis
10151
10152 @item @code{-I@emph{dir}}
10153
10154 @geindex RTL
10155
10156 Direct GNAT to search the @code{dir} directory for source files needed by
10157 the current compilation
10158 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10159 @end table
10160
10161 @geindex -I- (gcc)
10162
10163
10164 @table @asis
10165
10166 @item @code{-I-}
10167
10168 @geindex RTL
10169
10170 Except for the source file named in the command line, do not look for source
10171 files in the directory containing the source file named in the command line
10172 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10173 @end table
10174
10175 @geindex -o (gcc)
10176
10177
10178 @table @asis
10179
10180 @item @code{-o @emph{file}}
10181
10182 This switch is used in @code{gcc} to redirect the generated object file
10183 and its associated ALI file. Beware of this switch with GNAT, because it may
10184 cause the object file and ALI file to have different names which in turn
10185 may confuse the binder and the linker.
10186 @end table
10187
10188 @geindex -nostdinc (gcc)
10189
10190
10191 @table @asis
10192
10193 @item @code{-nostdinc}
10194
10195 Inhibit the search of the default location for the GNAT Run Time
10196 Library (RTL) source files.
10197 @end table
10198
10199 @geindex -nostdlib (gcc)
10200
10201
10202 @table @asis
10203
10204 @item @code{-nostdlib}
10205
10206 Inhibit the search of the default location for the GNAT Run Time
10207 Library (RTL) ALI files.
10208 @end table
10209
10210 @geindex -O (gcc)
10211
10212
10213 @table @asis
10214
10215 @item @code{-O[@emph{n}]}
10216
10217 @code{n} controls the optimization level:
10218
10219
10220 @multitable {xxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
10221 @item
10222
10223 @emph{n}
10224
10225 @tab
10226
10227 Effect
10228
10229 @item
10230
10231 @emph{0}
10232
10233 @tab
10234
10235 No optimization, the default setting if no @code{-O} appears
10236
10237 @item
10238
10239 @emph{1}
10240
10241 @tab
10242
10243 Normal optimization, the default if you specify @code{-O} without an
10244 operand. A good compromise between code quality and compilation
10245 time.
10246
10247 @item
10248
10249 @emph{2}
10250
10251 @tab
10252
10253 Extensive optimization, may improve execution time, possibly at
10254 the cost of substantially increased compilation time.
10255
10256 @item
10257
10258 @emph{3}
10259
10260 @tab
10261
10262 Same as @code{-O2}, and also includes inline expansion for small
10263 subprograms in the same unit.
10264
10265 @item
10266
10267 @emph{s}
10268
10269 @tab
10270
10271 Optimize space usage
10272
10273 @end multitable
10274
10275
10276 See also @ref{fc,,Optimization Levels}.
10277 @end table
10278
10279 @geindex -pass-exit-codes (gcc)
10280
10281
10282 @table @asis
10283
10284 @item @code{-pass-exit-codes}
10285
10286 Catch exit codes from the compiler and use the most meaningful as
10287 exit status.
10288 @end table
10289
10290 @geindex --RTS (gcc)
10291
10292
10293 @table @asis
10294
10295 @item @code{--RTS=@emph{rts-path}}
10296
10297 Specifies the default location of the run-time library. Same meaning as the
10298 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
10299 @end table
10300
10301 @geindex -S (gcc)
10302
10303
10304 @table @asis
10305
10306 @item @code{-S}
10307
10308 Used in place of @code{-c} to
10309 cause the assembler source file to be
10310 generated, using @code{.s} as the extension,
10311 instead of the object file.
10312 This may be useful if you need to examine the generated assembly code.
10313 @end table
10314
10315 @geindex -fverbose-asm (gcc)
10316
10317
10318 @table @asis
10319
10320 @item @code{-fverbose-asm}
10321
10322 Used in conjunction with @code{-S}
10323 to cause the generated assembly code file to be annotated with variable
10324 names, making it significantly easier to follow.
10325 @end table
10326
10327 @geindex -v (gcc)
10328
10329
10330 @table @asis
10331
10332 @item @code{-v}
10333
10334 Show commands generated by the @code{gcc} driver. Normally used only for
10335 debugging purposes or if you need to be sure what version of the
10336 compiler you are executing.
10337 @end table
10338
10339 @geindex -V (gcc)
10340
10341
10342 @table @asis
10343
10344 @item @code{-V @emph{ver}}
10345
10346 Execute @code{ver} version of the compiler. This is the @code{gcc}
10347 version, not the GNAT version.
10348 @end table
10349
10350 @geindex -w (gcc)
10351
10352
10353 @table @asis
10354
10355 @item @code{-w}
10356
10357 Turn off warnings generated by the back end of the compiler. Use of
10358 this switch also causes the default for front end warnings to be set
10359 to suppress (as though @code{-gnatws} had appeared at the start of
10360 the options).
10361 @end table
10362
10363 @geindex Combining GNAT switches
10364
10365 You may combine a sequence of GNAT switches into a single switch. For
10366 example, the combined switch
10367
10368 @quotation
10369
10370 @example
10371 -gnatofi3
10372 @end example
10373 @end quotation
10374
10375 is equivalent to specifying the following sequence of switches:
10376
10377 @quotation
10378
10379 @example
10380 -gnato -gnatf -gnati3
10381 @end example
10382 @end quotation
10383
10384 The following restrictions apply to the combination of switches
10385 in this manner:
10386
10387
10388 @itemize *
10389
10390 @item
10391 The switch @code{-gnatc} if combined with other switches must come
10392 first in the string.
10393
10394 @item
10395 The switch @code{-gnats} if combined with other switches must come
10396 first in the string.
10397
10398 @item
10399 The switches
10400 @code{-gnatzc} and @code{-gnatzr} may not be combined with any other
10401 switches, and only one of them may appear in the command line.
10402
10403 @item
10404 The switch @code{-gnat-p} may not be combined with any other switch.
10405
10406 @item
10407 Once a 'y' appears in the string (that is a use of the @code{-gnaty}
10408 switch), then all further characters in the switch are interpreted
10409 as style modifiers (see description of @code{-gnaty}).
10410
10411 @item
10412 Once a 'd' appears in the string (that is a use of the @code{-gnatd}
10413 switch), then all further characters in the switch are interpreted
10414 as debug flags (see description of @code{-gnatd}).
10415
10416 @item
10417 Once a 'w' appears in the string (that is a use of the @code{-gnatw}
10418 switch), then all further characters in the switch are interpreted
10419 as warning mode modifiers (see description of @code{-gnatw}).
10420
10421 @item
10422 Once a 'V' appears in the string (that is a use of the @code{-gnatV}
10423 switch), then all further characters in the switch are interpreted
10424 as validity checking options (@ref{f6,,Validity Checking}).
10425
10426 @item
10427 Option 'em', 'ec', 'ep', 'l=' and 'R' must be the last options in
10428 a combined list of options.
10429 @end itemize
10430
10431 @node Output and Error Message Control,Warning Message Control,Alphabetical List of All Switches,Compiler Switches
10432 @anchor{gnat_ugn/building_executable_programs_with_gnat id14}@anchor{fd}@anchor{gnat_ugn/building_executable_programs_with_gnat output-and-error-message-control}@anchor{fe}
10433 @subsection Output and Error Message Control
10434
10435
10436 @geindex stderr
10437
10438 The standard default format for error messages is called 'brief format'.
10439 Brief format messages are written to @code{stderr} (the standard error
10440 file) and have the following form:
10441
10442 @example
10443 e.adb:3:04: Incorrect spelling of keyword "function"
10444 e.adb:4:20: ";" should be "is"
10445 @end example
10446
10447 The first integer after the file name is the line number in the file,
10448 and the second integer is the column number within the line.
10449 @code{GNAT Studio} can parse the error messages
10450 and point to the referenced character.
10451 The following switches provide control over the error message
10452 format:
10453
10454 @geindex -gnatv (gcc)
10455
10456
10457 @table @asis
10458
10459 @item @code{-gnatv}
10460
10461 The @code{v} stands for verbose.
10462 The effect of this setting is to write long-format error
10463 messages to @code{stdout} (the standard output file.
10464 The same program compiled with the
10465 @code{-gnatv} switch would generate:
10466
10467 @example
10468 3. funcion X (Q : Integer)
10469 |
10470 >>> Incorrect spelling of keyword "function"
10471 4. return Integer;
10472 |
10473 >>> ";" should be "is"
10474 @end example
10475
10476 The vertical bar indicates the location of the error, and the @code{>>>}
10477 prefix can be used to search for error messages. When this switch is
10478 used the only source lines output are those with errors.
10479 @end table
10480
10481 @geindex -gnatl (gcc)
10482
10483
10484 @table @asis
10485
10486 @item @code{-gnatl}
10487
10488 The @code{l} stands for list.
10489 This switch causes a full listing of
10490 the file to be generated. In the case where a body is
10491 compiled, the corresponding spec is also listed, along
10492 with any subunits. Typical output from compiling a package
10493 body @code{p.adb} might look like:
10494
10495 @example
10496 Compiling: p.adb
10497
10498 1. package body p is
10499 2. procedure a;
10500 3. procedure a is separate;
10501 4. begin
10502 5. null
10503 |
10504 >>> missing ";"
10505
10506 6. end;
10507
10508 Compiling: p.ads
10509
10510 1. package p is
10511 2. pragma Elaborate_Body
10512 |
10513 >>> missing ";"
10514
10515 3. end p;
10516
10517 Compiling: p-a.adb
10518
10519 1. separate p
10520 |
10521 >>> missing "("
10522
10523 2. procedure a is
10524 3. begin
10525 4. null
10526 |
10527 >>> missing ";"
10528
10529 5. end;
10530 @end example
10531
10532 When you specify the @code{-gnatv} or @code{-gnatl} switches and
10533 standard output is redirected, a brief summary is written to
10534 @code{stderr} (standard error) giving the number of error messages and
10535 warning messages generated.
10536 @end table
10537
10538 @geindex -gnatl=fname (gcc)
10539
10540
10541 @table @asis
10542
10543 @item @code{-gnatl=@emph{fname}}
10544
10545 This has the same effect as @code{-gnatl} except that the output is
10546 written to a file instead of to standard output. If the given name
10547 @code{fname} does not start with a period, then it is the full name
10548 of the file to be written. If @code{fname} is an extension, it is
10549 appended to the name of the file being compiled. For example, if
10550 file @code{xyz.adb} is compiled with @code{-gnatl=.lst},
10551 then the output is written to file xyz.adb.lst.
10552 @end table
10553
10554 @geindex -gnatU (gcc)
10555
10556
10557 @table @asis
10558
10559 @item @code{-gnatU}
10560
10561 This switch forces all error messages to be preceded by the unique
10562 string 'error:'. This means that error messages take a few more
10563 characters in space, but allows easy searching for and identification
10564 of error messages.
10565 @end table
10566
10567 @geindex -gnatb (gcc)
10568
10569
10570 @table @asis
10571
10572 @item @code{-gnatb}
10573
10574 The @code{b} stands for brief.
10575 This switch causes GNAT to generate the
10576 brief format error messages to @code{stderr} (the standard error
10577 file) as well as the verbose
10578 format message or full listing (which as usual is written to
10579 @code{stdout} (the standard output file).
10580 @end table
10581
10582 @geindex -gnatm (gcc)
10583
10584
10585 @table @asis
10586
10587 @item @code{-gnatm=@emph{n}}
10588
10589 The @code{m} stands for maximum.
10590 @code{n} is a decimal integer in the
10591 range of 1 to 999999 and limits the number of error or warning
10592 messages to be generated. For example, using
10593 @code{-gnatm2} might yield
10594
10595 @example
10596 e.adb:3:04: Incorrect spelling of keyword "function"
10597 e.adb:5:35: missing ".."
10598 fatal error: maximum number of errors detected
10599 compilation abandoned
10600 @end example
10601
10602 The default setting if
10603 no switch is given is 9999. If the number of warnings reaches this
10604 limit, then a message is output and further warnings are suppressed,
10605 but the compilation is continued. If the number of error messages
10606 reaches this limit, then a message is output and the compilation
10607 is abandoned. A value of zero means that no limit applies.
10608
10609 Note that the equal sign is optional, so the switches
10610 @code{-gnatm2} and @code{-gnatm=2} are equivalent.
10611 @end table
10612
10613 @geindex -gnatf (gcc)
10614
10615
10616 @table @asis
10617
10618 @item @code{-gnatf}
10619
10620 @geindex Error messages
10621 @geindex suppressing
10622
10623 The @code{f} stands for full.
10624 Normally, the compiler suppresses error messages that are likely to be
10625 redundant. This switch causes all error
10626 messages to be generated. In particular, in the case of
10627 references to undefined variables. If a given variable is referenced
10628 several times, the normal format of messages is
10629
10630 @example
10631 e.adb:7:07: "V" is undefined (more references follow)
10632 @end example
10633
10634 where the parenthetical comment warns that there are additional
10635 references to the variable @code{V}. Compiling the same program with the
10636 @code{-gnatf} switch yields
10637
10638 @example
10639 e.adb:7:07: "V" is undefined
10640 e.adb:8:07: "V" is undefined
10641 e.adb:8:12: "V" is undefined
10642 e.adb:8:16: "V" is undefined
10643 e.adb:9:07: "V" is undefined
10644 e.adb:9:12: "V" is undefined
10645 @end example
10646
10647 The @code{-gnatf} switch also generates additional information for
10648 some error messages. Some examples are:
10649
10650
10651 @itemize *
10652
10653 @item
10654 Details on possibly non-portable unchecked conversion
10655
10656 @item
10657 List possible interpretations for ambiguous calls
10658
10659 @item
10660 Additional details on incorrect parameters
10661 @end itemize
10662 @end table
10663
10664 @geindex -gnatjnn (gcc)
10665
10666
10667 @table @asis
10668
10669 @item @code{-gnatjnn}
10670
10671 In normal operation mode (or if @code{-gnatj0} is used), then error messages
10672 with continuation lines are treated as though the continuation lines were
10673 separate messages (and so a warning with two continuation lines counts as
10674 three warnings, and is listed as three separate messages).
10675
10676 If the @code{-gnatjnn} switch is used with a positive value for nn, then
10677 messages are output in a different manner. A message and all its continuation
10678 lines are treated as a unit, and count as only one warning or message in the
10679 statistics totals. Furthermore, the message is reformatted so that no line
10680 is longer than nn characters.
10681 @end table
10682
10683 @geindex -gnatq (gcc)
10684
10685
10686 @table @asis
10687
10688 @item @code{-gnatq}
10689
10690 The @code{q} stands for quit (really 'don't quit').
10691 In normal operation mode, the compiler first parses the program and
10692 determines if there are any syntax errors. If there are, appropriate
10693 error messages are generated and compilation is immediately terminated.
10694 This switch tells
10695 GNAT to continue with semantic analysis even if syntax errors have been
10696 found. This may enable the detection of more errors in a single run. On
10697 the other hand, the semantic analyzer is more likely to encounter some
10698 internal fatal error when given a syntactically invalid tree.
10699 @end table
10700
10701 @geindex -gnatQ (gcc)
10702
10703
10704 @table @asis
10705
10706 @item @code{-gnatQ}
10707
10708 In normal operation mode, the @code{ALI} file is not generated if any
10709 illegalities are detected in the program. The use of @code{-gnatQ} forces
10710 generation of the @code{ALI} file. This file is marked as being in
10711 error, so it cannot be used for binding purposes, but it does contain
10712 reasonably complete cross-reference information, and thus may be useful
10713 for use by tools (e.g., semantic browsing tools or integrated development
10714 environments) that are driven from the @code{ALI} file. This switch
10715 implies @code{-gnatq}, since the semantic phase must be run to get a
10716 meaningful ALI file.
10717
10718 In addition, if @code{-gnatt} is also specified, then the tree file is
10719 generated even if there are illegalities. It may be useful in this case
10720 to also specify @code{-gnatq} to ensure that full semantic processing
10721 occurs. The resulting tree file can be processed by ASIS, for the purpose
10722 of providing partial information about illegal units, but if the error
10723 causes the tree to be badly malformed, then ASIS may crash during the
10724 analysis.
10725
10726 When @code{-gnatQ} is used and the generated @code{ALI} file is marked as
10727 being in error, @code{gnatmake} will attempt to recompile the source when it
10728 finds such an @code{ALI} file, including with switch @code{-gnatc}.
10729
10730 Note that @code{-gnatQ} has no effect if @code{-gnats} is specified,
10731 since ALI files are never generated if @code{-gnats} is set.
10732 @end table
10733
10734 @node Warning Message Control,Debugging and Assertion Control,Output and Error Message Control,Compiler Switches
10735 @anchor{gnat_ugn/building_executable_programs_with_gnat warning-message-control}@anchor{fa}@anchor{gnat_ugn/building_executable_programs_with_gnat id15}@anchor{ff}
10736 @subsection Warning Message Control
10737
10738
10739 @geindex Warning messages
10740
10741 In addition to error messages, which correspond to illegalities as defined
10742 in the Ada Reference Manual, the compiler detects two kinds of warning
10743 situations.
10744
10745 First, the compiler considers some constructs suspicious and generates a
10746 warning message to alert you to a possible error. Second, if the
10747 compiler detects a situation that is sure to raise an exception at
10748 run time, it generates a warning message. The following shows an example
10749 of warning messages:
10750
10751 @example
10752 e.adb:4:24: warning: creation of object may raise Storage_Error
10753 e.adb:10:17: warning: static value out of range
10754 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
10755 @end example
10756
10757 GNAT considers a large number of situations as appropriate
10758 for the generation of warning messages. As always, warnings are not
10759 definite indications of errors. For example, if you do an out-of-range
10760 assignment with the deliberate intention of raising a
10761 @code{Constraint_Error} exception, then the warning that may be
10762 issued does not indicate an error. Some of the situations for which GNAT
10763 issues warnings (at least some of the time) are given in the following
10764 list. This list is not complete, and new warnings are often added to
10765 subsequent versions of GNAT. The list is intended to give a general idea
10766 of the kinds of warnings that are generated.
10767
10768
10769 @itemize *
10770
10771 @item
10772 Possible infinitely recursive calls
10773
10774 @item
10775 Out-of-range values being assigned
10776
10777 @item
10778 Possible order of elaboration problems
10779
10780 @item
10781 Size not a multiple of alignment for a record type
10782
10783 @item
10784 Assertions (pragma Assert) that are sure to fail
10785
10786 @item
10787 Unreachable code
10788
10789 @item
10790 Address clauses with possibly unaligned values, or where an attempt is
10791 made to overlay a smaller variable with a larger one.
10792
10793 @item
10794 Fixed-point type declarations with a null range
10795
10796 @item
10797 Direct_IO or Sequential_IO instantiated with a type that has access values
10798
10799 @item
10800 Variables that are never assigned a value
10801
10802 @item
10803 Variables that are referenced before being initialized
10804
10805 @item
10806 Task entries with no corresponding @code{accept} statement
10807
10808 @item
10809 Duplicate accepts for the same task entry in a @code{select}
10810
10811 @item
10812 Objects that take too much storage
10813
10814 @item
10815 Unchecked conversion between types of differing sizes
10816
10817 @item
10818 Missing @code{return} statement along some execution path in a function
10819
10820 @item
10821 Incorrect (unrecognized) pragmas
10822
10823 @item
10824 Incorrect external names
10825
10826 @item
10827 Allocation from empty storage pool
10828
10829 @item
10830 Potentially blocking operation in protected type
10831
10832 @item
10833 Suspicious parenthesization of expressions
10834
10835 @item
10836 Mismatching bounds in an aggregate
10837
10838 @item
10839 Attempt to return local value by reference
10840
10841 @item
10842 Premature instantiation of a generic body
10843
10844 @item
10845 Attempt to pack aliased components
10846
10847 @item
10848 Out of bounds array subscripts
10849
10850 @item
10851 Wrong length on string assignment
10852
10853 @item
10854 Violations of style rules if style checking is enabled
10855
10856 @item
10857 Unused @emph{with} clauses
10858
10859 @item
10860 @code{Bit_Order} usage that does not have any effect
10861
10862 @item
10863 @code{Standard.Duration} used to resolve universal fixed expression
10864
10865 @item
10866 Dereference of possibly null value
10867
10868 @item
10869 Declaration that is likely to cause storage error
10870
10871 @item
10872 Internal GNAT unit @emph{with}ed by application unit
10873
10874 @item
10875 Values known to be out of range at compile time
10876
10877 @item
10878 Unreferenced or unmodified variables. Note that a special
10879 exemption applies to variables which contain any of the substrings
10880 @code{DISCARD, DUMMY, IGNORE, JUNK, UNUSED}, in any casing. Such variables
10881 are considered likely to be intentionally used in a situation where
10882 otherwise a warning would be given, so warnings of this kind are
10883 always suppressed for such variables.
10884
10885 @item
10886 Address overlays that could clobber memory
10887
10888 @item
10889 Unexpected initialization when address clause present
10890
10891 @item
10892 Bad alignment for address clause
10893
10894 @item
10895 Useless type conversions
10896
10897 @item
10898 Redundant assignment statements and other redundant constructs
10899
10900 @item
10901 Useless exception handlers
10902
10903 @item
10904 Accidental hiding of name by child unit
10905
10906 @item
10907 Access before elaboration detected at compile time
10908
10909 @item
10910 A range in a @code{for} loop that is known to be null or might be null
10911 @end itemize
10912
10913 The following section lists compiler switches that are available
10914 to control the handling of warning messages. It is also possible
10915 to exercise much finer control over what warnings are issued and
10916 suppressed using the GNAT pragma Warnings (see the description
10917 of the pragma in the @cite{GNAT_Reference_manual}).
10918
10919 @geindex -gnatwa (gcc)
10920
10921
10922 @table @asis
10923
10924 @item @code{-gnatwa}
10925
10926 @emph{Activate most optional warnings.}
10927
10928 This switch activates most optional warning messages. See the remaining list
10929 in this section for details on optional warning messages that can be
10930 individually controlled. The warnings that are not turned on by this
10931 switch are:
10932
10933
10934 @itemize *
10935
10936 @item
10937 @code{-gnatwd} (implicit dereferencing)
10938
10939 @item
10940 @code{-gnatw.d} (tag warnings with -gnatw switch)
10941
10942 @item
10943 @code{-gnatwh} (hiding)
10944
10945 @item
10946 @code{-gnatw.h} (holes in record layouts)
10947
10948 @item
10949 @code{-gnatw.j} (late primitives of tagged types)
10950
10951 @item
10952 @code{-gnatw.k} (redefinition of names in standard)
10953
10954 @item
10955 @code{-gnatwl} (elaboration warnings)
10956
10957 @item
10958 @code{-gnatw.l} (inherited aspects)
10959
10960 @item
10961 @code{-gnatw.n} (atomic synchronization)
10962
10963 @item
10964 @code{-gnatwo} (address clause overlay)
10965
10966 @item
10967 @code{-gnatw.o} (values set by out parameters ignored)
10968
10969 @item
10970 @code{-gnatw.q} (questionable layout of record types)
10971
10972 @item
10973 @code{-gnatw_r} (out-of-order record representation clauses)
10974
10975 @item
10976 @code{-gnatw.s} (overridden size clause)
10977
10978 @item
10979 @code{-gnatwt} (tracking of deleted conditional code)
10980
10981 @item
10982 @code{-gnatw.u} (unordered enumeration)
10983
10984 @item
10985 @code{-gnatw.w} (use of Warnings Off)
10986
10987 @item
10988 @code{-gnatw.y} (reasons for package needing body)
10989 @end itemize
10990
10991 All other optional warnings are turned on.
10992 @end table
10993
10994 @geindex -gnatwA (gcc)
10995
10996
10997 @table @asis
10998
10999 @item @code{-gnatwA}
11000
11001 @emph{Suppress all optional errors.}
11002
11003 This switch suppresses all optional warning messages, see remaining list
11004 in this section for details on optional warning messages that can be
11005 individually controlled. Note that unlike switch @code{-gnatws}, the
11006 use of switch @code{-gnatwA} does not suppress warnings that are
11007 normally given unconditionally and cannot be individually controlled
11008 (for example, the warning about a missing exit path in a function).
11009 Also, again unlike switch @code{-gnatws}, warnings suppressed by
11010 the use of switch @code{-gnatwA} can be individually turned back
11011 on. For example the use of switch @code{-gnatwA} followed by
11012 switch @code{-gnatwd} will suppress all optional warnings except
11013 the warnings for implicit dereferencing.
11014 @end table
11015
11016 @geindex -gnatw.a (gcc)
11017
11018
11019 @table @asis
11020
11021 @item @code{-gnatw.a}
11022
11023 @emph{Activate warnings on failing assertions.}
11024
11025 @geindex Assert failures
11026
11027 This switch activates warnings for assertions where the compiler can tell at
11028 compile time that the assertion will fail. Note that this warning is given
11029 even if assertions are disabled. The default is that such warnings are
11030 generated.
11031 @end table
11032
11033 @geindex -gnatw.A (gcc)
11034
11035
11036 @table @asis
11037
11038 @item @code{-gnatw.A}
11039
11040 @emph{Suppress warnings on failing assertions.}
11041
11042 @geindex Assert failures
11043
11044 This switch suppresses warnings for assertions where the compiler can tell at
11045 compile time that the assertion will fail.
11046 @end table
11047
11048 @geindex -gnatw_a
11049
11050
11051 @table @asis
11052
11053 @item @code{-gnatw_a}
11054
11055 @emph{Activate warnings on anonymous allocators.}
11056
11057 @geindex Anonymous allocators
11058
11059 This switch activates warnings for allocators of anonymous access types,
11060 which can involve run-time accessibility checks and lead to unexpected
11061 accessibility violations. For more details on the rules involved, see
11062 RM 3.10.2 (14).
11063 @end table
11064
11065 @geindex -gnatw_A
11066
11067
11068 @table @asis
11069
11070 @item @code{-gnatw_A}
11071
11072 @emph{Supress warnings on anonymous allocators.}
11073
11074 @geindex Anonymous allocators
11075
11076 This switch suppresses warnings for anonymous access type allocators.
11077 @end table
11078
11079 @geindex -gnatwb (gcc)
11080
11081
11082 @table @asis
11083
11084 @item @code{-gnatwb}
11085
11086 @emph{Activate warnings on bad fixed values.}
11087
11088 @geindex Bad fixed values
11089
11090 @geindex Fixed-point Small value
11091
11092 @geindex Small value
11093
11094 This switch activates warnings for static fixed-point expressions whose
11095 value is not an exact multiple of Small. Such values are implementation
11096 dependent, since an implementation is free to choose either of the multiples
11097 that surround the value. GNAT always chooses the closer one, but this is not
11098 required behavior, and it is better to specify a value that is an exact
11099 multiple, ensuring predictable execution. The default is that such warnings
11100 are not generated.
11101 @end table
11102
11103 @geindex -gnatwB (gcc)
11104
11105
11106 @table @asis
11107
11108 @item @code{-gnatwB}
11109
11110 @emph{Suppress warnings on bad fixed values.}
11111
11112 This switch suppresses warnings for static fixed-point expressions whose
11113 value is not an exact multiple of Small.
11114 @end table
11115
11116 @geindex -gnatw.b (gcc)
11117
11118
11119 @table @asis
11120
11121 @item @code{-gnatw.b}
11122
11123 @emph{Activate warnings on biased representation.}
11124
11125 @geindex Biased representation
11126
11127 This switch activates warnings when a size clause, value size clause, component
11128 clause, or component size clause forces the use of biased representation for an
11129 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
11130 to represent 10/11). The default is that such warnings are generated.
11131 @end table
11132
11133 @geindex -gnatwB (gcc)
11134
11135
11136 @table @asis
11137
11138 @item @code{-gnatw.B}
11139
11140 @emph{Suppress warnings on biased representation.}
11141
11142 This switch suppresses warnings for representation clauses that force the use
11143 of biased representation.
11144 @end table
11145
11146 @geindex -gnatwc (gcc)
11147
11148
11149 @table @asis
11150
11151 @item @code{-gnatwc}
11152
11153 @emph{Activate warnings on conditionals.}
11154
11155 @geindex Conditionals
11156 @geindex constant
11157
11158 This switch activates warnings for conditional expressions used in
11159 tests that are known to be True or False at compile time. The default
11160 is that such warnings are not generated.
11161 Note that this warning does
11162 not get issued for the use of boolean variables or constants whose
11163 values are known at compile time, since this is a standard technique
11164 for conditional compilation in Ada, and this would generate too many
11165 false positive warnings.
11166
11167 This warning option also activates a special test for comparisons using
11168 the operators '>=' and' <='.
11169 If the compiler can tell that only the equality condition is possible,
11170 then it will warn that the '>' or '<' part of the test
11171 is useless and that the operator could be replaced by '='.
11172 An example would be comparing a @code{Natural} variable <= 0.
11173
11174 This warning option also generates warnings if
11175 one or both tests is optimized away in a membership test for integer
11176 values if the result can be determined at compile time. Range tests on
11177 enumeration types are not included, since it is common for such tests
11178 to include an end point.
11179
11180 This warning can also be turned on using @code{-gnatwa}.
11181 @end table
11182
11183 @geindex -gnatwC (gcc)
11184
11185
11186 @table @asis
11187
11188 @item @code{-gnatwC}
11189
11190 @emph{Suppress warnings on conditionals.}
11191
11192 This switch suppresses warnings for conditional expressions used in
11193 tests that are known to be True or False at compile time.
11194 @end table
11195
11196 @geindex -gnatw.c (gcc)
11197
11198
11199 @table @asis
11200
11201 @item @code{-gnatw.c}
11202
11203 @emph{Activate warnings on missing component clauses.}
11204
11205 @geindex Component clause
11206 @geindex missing
11207
11208 This switch activates warnings for record components where a record
11209 representation clause is present and has component clauses for the
11210 majority, but not all, of the components. A warning is given for each
11211 component for which no component clause is present.
11212 @end table
11213
11214 @geindex -gnatw.C (gcc)
11215
11216
11217 @table @asis
11218
11219 @item @code{-gnatw.C}
11220
11221 @emph{Suppress warnings on missing component clauses.}
11222
11223 This switch suppresses warnings for record components that are
11224 missing a component clause in the situation described above.
11225 @end table
11226
11227 @geindex -gnatw_c (gcc)
11228
11229
11230 @table @asis
11231
11232 @item @code{-gnatw_c}
11233
11234 @emph{Activate warnings on unknown condition in Compile_Time_Warning.}
11235
11236 @geindex Compile_Time_Warning
11237
11238 @geindex Compile_Time_Error
11239
11240 This switch activates warnings on a pragma Compile_Time_Warning
11241 or Compile_Time_Error whose condition has a value that is not
11242 known at compile time.
11243 The default is that such warnings are generated.
11244 @end table
11245
11246 @geindex -gnatw_C (gcc)
11247
11248
11249 @table @asis
11250
11251 @item @code{-gnatw_C}
11252
11253 @emph{Suppress warnings on unknown condition in Compile_Time_Warning.}
11254
11255 This switch supresses warnings on a pragma Compile_Time_Warning
11256 or Compile_Time_Error whose condition has a value that is not
11257 known at compile time.
11258 @end table
11259
11260 @geindex -gnatwd (gcc)
11261
11262
11263 @table @asis
11264
11265 @item @code{-gnatwd}
11266
11267 @emph{Activate warnings on implicit dereferencing.}
11268
11269 If this switch is set, then the use of a prefix of an access type
11270 in an indexed component, slice, or selected component without an
11271 explicit @code{.all} will generate a warning. With this warning
11272 enabled, access checks occur only at points where an explicit
11273 @code{.all} appears in the source code (assuming no warnings are
11274 generated as a result of this switch). The default is that such
11275 warnings are not generated.
11276 @end table
11277
11278 @geindex -gnatwD (gcc)
11279
11280
11281 @table @asis
11282
11283 @item @code{-gnatwD}
11284
11285 @emph{Suppress warnings on implicit dereferencing.}
11286
11287 @geindex Implicit dereferencing
11288
11289 @geindex Dereferencing
11290 @geindex implicit
11291
11292 This switch suppresses warnings for implicit dereferences in
11293 indexed components, slices, and selected components.
11294 @end table
11295
11296 @geindex -gnatw.d (gcc)
11297
11298
11299 @table @asis
11300
11301 @item @code{-gnatw.d}
11302
11303 @emph{Activate tagging of warning and info messages.}
11304
11305 If this switch is set, then warning messages are tagged, with one of the
11306 following strings:
11307
11308 @quotation
11309
11310
11311 @itemize -
11312
11313 @item
11314 @emph{[-gnatw?]}
11315 Used to tag warnings controlled by the switch @code{-gnatwx} where x
11316 is a letter a-z.
11317
11318 @item
11319 @emph{[-gnatw.?]}
11320 Used to tag warnings controlled by the switch @code{-gnatw.x} where x
11321 is a letter a-z.
11322
11323 @item
11324 @emph{[-gnatel]}
11325 Used to tag elaboration information (info) messages generated when the
11326 static model of elaboration is used and the @code{-gnatel} switch is set.
11327
11328 @item
11329 @emph{[restriction warning]}
11330 Used to tag warning messages for restriction violations, activated by use
11331 of the pragma @code{Restriction_Warnings}.
11332
11333 @item
11334 @emph{[warning-as-error]}
11335 Used to tag warning messages that have been converted to error messages by
11336 use of the pragma Warning_As_Error. Note that such warnings are prefixed by
11337 the string "error: " rather than "warning: ".
11338
11339 @item
11340 @emph{[enabled by default]}
11341 Used to tag all other warnings that are always given by default, unless
11342 warnings are completely suppressed using pragma @emph{Warnings(Off)} or
11343 the switch @code{-gnatws}.
11344 @end itemize
11345 @end quotation
11346 @end table
11347
11348 @geindex -gnatw.d (gcc)
11349
11350
11351 @table @asis
11352
11353 @item @code{-gnatw.D}
11354
11355 @emph{Deactivate tagging of warning and info messages messages.}
11356
11357 If this switch is set, then warning messages return to the default
11358 mode in which warnings and info messages are not tagged as described above for
11359 @code{-gnatw.d}.
11360 @end table
11361
11362 @geindex -gnatwe (gcc)
11363
11364 @geindex Warnings
11365 @geindex treat as error
11366
11367
11368 @table @asis
11369
11370 @item @code{-gnatwe}
11371
11372 @emph{Treat warnings and style checks as errors.}
11373
11374 This switch causes warning messages and style check messages to be
11375 treated as errors.
11376 The warning string still appears, but the warning messages are counted
11377 as errors, and prevent the generation of an object file. Note that this
11378 is the only -gnatw switch that affects the handling of style check messages.
11379 Note also that this switch has no effect on info (information) messages, which
11380 are not treated as errors if this switch is present.
11381 @end table
11382
11383 @geindex -gnatw.e (gcc)
11384
11385
11386 @table @asis
11387
11388 @item @code{-gnatw.e}
11389
11390 @emph{Activate every optional warning.}
11391
11392 @geindex Warnings
11393 @geindex activate every optional warning
11394
11395 This switch activates all optional warnings, including those which
11396 are not activated by @code{-gnatwa}. The use of this switch is not
11397 recommended for normal use. If you turn this switch on, it is almost
11398 certain that you will get large numbers of useless warnings. The
11399 warnings that are excluded from @code{-gnatwa} are typically highly
11400 specialized warnings that are suitable for use only in code that has
11401 been specifically designed according to specialized coding rules.
11402 @end table
11403
11404 @geindex -gnatwE (gcc)
11405
11406 @geindex Warnings
11407 @geindex treat as error
11408
11409
11410 @table @asis
11411
11412 @item @code{-gnatwE}
11413
11414 @emph{Treat all run-time exception warnings as errors.}
11415
11416 This switch causes warning messages regarding errors that will be raised
11417 during run-time execution to be treated as errors.
11418 @end table
11419
11420 @geindex -gnatwf (gcc)
11421
11422
11423 @table @asis
11424
11425 @item @code{-gnatwf}
11426
11427 @emph{Activate warnings on unreferenced formals.}
11428
11429 @geindex Formals
11430 @geindex unreferenced
11431
11432 This switch causes a warning to be generated if a formal parameter
11433 is not referenced in the body of the subprogram. This warning can
11434 also be turned on using @code{-gnatwu}. The
11435 default is that these warnings are not generated.
11436 @end table
11437
11438 @geindex -gnatwF (gcc)
11439
11440
11441 @table @asis
11442
11443 @item @code{-gnatwF}
11444
11445 @emph{Suppress warnings on unreferenced formals.}
11446
11447 This switch suppresses warnings for unreferenced formal
11448 parameters. Note that the
11449 combination @code{-gnatwu} followed by @code{-gnatwF} has the
11450 effect of warning on unreferenced entities other than subprogram
11451 formals.
11452 @end table
11453
11454 @geindex -gnatwg (gcc)
11455
11456
11457 @table @asis
11458
11459 @item @code{-gnatwg}
11460
11461 @emph{Activate warnings on unrecognized pragmas.}
11462
11463 @geindex Pragmas
11464 @geindex unrecognized
11465
11466 This switch causes a warning to be generated if an unrecognized
11467 pragma is encountered. Apart from issuing this warning, the
11468 pragma is ignored and has no effect. The default
11469 is that such warnings are issued (satisfying the Ada Reference
11470 Manual requirement that such warnings appear).
11471 @end table
11472
11473 @geindex -gnatwG (gcc)
11474
11475
11476 @table @asis
11477
11478 @item @code{-gnatwG}
11479
11480 @emph{Suppress warnings on unrecognized pragmas.}
11481
11482 This switch suppresses warnings for unrecognized pragmas.
11483 @end table
11484
11485 @geindex -gnatw.g (gcc)
11486
11487
11488 @table @asis
11489
11490 @item @code{-gnatw.g}
11491
11492 @emph{Warnings used for GNAT sources.}
11493
11494 This switch sets the warning categories that are used by the standard
11495 GNAT style. Currently this is equivalent to
11496 @code{-gnatwAao.q.s.CI.V.X.Z}
11497 but more warnings may be added in the future without advanced notice.
11498 @end table
11499
11500 @geindex -gnatwh (gcc)
11501
11502
11503 @table @asis
11504
11505 @item @code{-gnatwh}
11506
11507 @emph{Activate warnings on hiding.}
11508
11509 @geindex Hiding of Declarations
11510
11511 This switch activates warnings on hiding declarations that are considered
11512 potentially confusing. Not all cases of hiding cause warnings; for example an
11513 overriding declaration hides an implicit declaration, which is just normal
11514 code. The default is that warnings on hiding are not generated.
11515 @end table
11516
11517 @geindex -gnatwH (gcc)
11518
11519
11520 @table @asis
11521
11522 @item @code{-gnatwH}
11523
11524 @emph{Suppress warnings on hiding.}
11525
11526 This switch suppresses warnings on hiding declarations.
11527 @end table
11528
11529 @geindex -gnatw.h (gcc)
11530
11531
11532 @table @asis
11533
11534 @item @code{-gnatw.h}
11535
11536 @emph{Activate warnings on holes/gaps in records.}
11537
11538 @geindex Record Representation (gaps)
11539
11540 This switch activates warnings on component clauses in record
11541 representation clauses that leave holes (gaps) in the record layout.
11542 If this warning option is active, then record representation clauses
11543 should specify a contiguous layout, adding unused fill fields if needed.
11544 @end table
11545
11546 @geindex -gnatw.H (gcc)
11547
11548
11549 @table @asis
11550
11551 @item @code{-gnatw.H}
11552
11553 @emph{Suppress warnings on holes/gaps in records.}
11554
11555 This switch suppresses warnings on component clauses in record
11556 representation clauses that leave holes (haps) in the record layout.
11557 @end table
11558
11559 @geindex -gnatwi (gcc)
11560
11561
11562 @table @asis
11563
11564 @item @code{-gnatwi}
11565
11566 @emph{Activate warnings on implementation units.}
11567
11568 This switch activates warnings for a @emph{with} of an internal GNAT
11569 implementation unit, defined as any unit from the @code{Ada},
11570 @code{Interfaces}, @code{GNAT},
11571 or @code{System}
11572 hierarchies that is not
11573 documented in either the Ada Reference Manual or the GNAT
11574 Programmer's Reference Manual. Such units are intended only
11575 for internal implementation purposes and should not be @emph{with}ed
11576 by user programs. The default is that such warnings are generated
11577 @end table
11578
11579 @geindex -gnatwI (gcc)
11580
11581
11582 @table @asis
11583
11584 @item @code{-gnatwI}
11585
11586 @emph{Disable warnings on implementation units.}
11587
11588 This switch disables warnings for a @emph{with} of an internal GNAT
11589 implementation unit.
11590 @end table
11591
11592 @geindex -gnatw.i (gcc)
11593
11594
11595 @table @asis
11596
11597 @item @code{-gnatw.i}
11598
11599 @emph{Activate warnings on overlapping actuals.}
11600
11601 This switch enables a warning on statically detectable overlapping actuals in
11602 a subprogram call, when one of the actuals is an in-out parameter, and the
11603 types of the actuals are not by-copy types. This warning is off by default.
11604 @end table
11605
11606 @geindex -gnatw.I (gcc)
11607
11608
11609 @table @asis
11610
11611 @item @code{-gnatw.I}
11612
11613 @emph{Disable warnings on overlapping actuals.}
11614
11615 This switch disables warnings on overlapping actuals in a call..
11616 @end table
11617
11618 @geindex -gnatwj (gcc)
11619
11620
11621 @table @asis
11622
11623 @item @code{-gnatwj}
11624
11625 @emph{Activate warnings on obsolescent features (Annex J).}
11626
11627 @geindex Features
11628 @geindex obsolescent
11629
11630 @geindex Obsolescent features
11631
11632 If this warning option is activated, then warnings are generated for
11633 calls to subprograms marked with @code{pragma Obsolescent} and
11634 for use of features in Annex J of the Ada Reference Manual. In the
11635 case of Annex J, not all features are flagged. In particular use
11636 of the renamed packages (like @code{Text_IO}) and use of package
11637 @code{ASCII} are not flagged, since these are very common and
11638 would generate many annoying positive warnings. The default is that
11639 such warnings are not generated.
11640
11641 In addition to the above cases, warnings are also generated for
11642 GNAT features that have been provided in past versions but which
11643 have been superseded (typically by features in the new Ada standard).
11644 For example, @code{pragma Ravenscar} will be flagged since its
11645 function is replaced by @code{pragma Profile(Ravenscar)}, and
11646 @code{pragma Interface_Name} will be flagged since its function
11647 is replaced by @code{pragma Import}.
11648
11649 Note that this warning option functions differently from the
11650 restriction @code{No_Obsolescent_Features} in two respects.
11651 First, the restriction applies only to annex J features.
11652 Second, the restriction does flag uses of package @code{ASCII}.
11653 @end table
11654
11655 @geindex -gnatwJ (gcc)
11656
11657
11658 @table @asis
11659
11660 @item @code{-gnatwJ}
11661
11662 @emph{Suppress warnings on obsolescent features (Annex J).}
11663
11664 This switch disables warnings on use of obsolescent features.
11665 @end table
11666
11667 @geindex -gnatw.j (gcc)
11668
11669
11670 @table @asis
11671
11672 @item @code{-gnatw.j}
11673
11674 @emph{Activate warnings on late declarations of tagged type primitives.}
11675
11676 This switch activates warnings on visible primitives added to a
11677 tagged type after deriving a private extension from it.
11678 @end table
11679
11680 @geindex -gnatw.J (gcc)
11681
11682
11683 @table @asis
11684
11685 @item @code{-gnatw.J}
11686
11687 @emph{Suppress warnings on late declarations of tagged type primitives.}
11688
11689 This switch suppresses warnings on visible primitives added to a
11690 tagged type after deriving a private extension from it.
11691 @end table
11692
11693 @geindex -gnatwk (gcc)
11694
11695
11696 @table @asis
11697
11698 @item @code{-gnatwk}
11699
11700 @emph{Activate warnings on variables that could be constants.}
11701
11702 This switch activates warnings for variables that are initialized but
11703 never modified, and then could be declared constants. The default is that
11704 such warnings are not given.
11705 @end table
11706
11707 @geindex -gnatwK (gcc)
11708
11709
11710 @table @asis
11711
11712 @item @code{-gnatwK}
11713
11714 @emph{Suppress warnings on variables that could be constants.}
11715
11716 This switch disables warnings on variables that could be declared constants.
11717 @end table
11718
11719 @geindex -gnatw.k (gcc)
11720
11721
11722 @table @asis
11723
11724 @item @code{-gnatw.k}
11725
11726 @emph{Activate warnings on redefinition of names in standard.}
11727
11728 This switch activates warnings for declarations that declare a name that
11729 is defined in package Standard. Such declarations can be confusing,
11730 especially since the names in package Standard continue to be directly
11731 visible, meaning that use visibiliy on such redeclared names does not
11732 work as expected. Names of discriminants and components in records are
11733 not included in this check.
11734 @end table
11735
11736 @geindex -gnatwK (gcc)
11737
11738
11739 @table @asis
11740
11741 @item @code{-gnatw.K}
11742
11743 @emph{Suppress warnings on redefinition of names in standard.}
11744
11745 This switch activates warnings for declarations that declare a name that
11746 is defined in package Standard.
11747 @end table
11748
11749 @geindex -gnatwl (gcc)
11750
11751
11752 @table @asis
11753
11754 @item @code{-gnatwl}
11755
11756 @emph{Activate warnings for elaboration pragmas.}
11757
11758 @geindex Elaboration
11759 @geindex warnings
11760
11761 This switch activates warnings for possible elaboration problems,
11762 including suspicious use
11763 of @code{Elaborate} pragmas, when using the static elaboration model, and
11764 possible situations that may raise @code{Program_Error} when using the
11765 dynamic elaboration model.
11766 See the section in this guide on elaboration checking for further details.
11767 The default is that such warnings
11768 are not generated.
11769 @end table
11770
11771 @geindex -gnatwL (gcc)
11772
11773
11774 @table @asis
11775
11776 @item @code{-gnatwL}
11777
11778 @emph{Suppress warnings for elaboration pragmas.}
11779
11780 This switch suppresses warnings for possible elaboration problems.
11781 @end table
11782
11783 @geindex -gnatw.l (gcc)
11784
11785
11786 @table @asis
11787
11788 @item @code{-gnatw.l}
11789
11790 @emph{List inherited aspects.}
11791
11792 This switch causes the compiler to list inherited invariants,
11793 preconditions, and postconditions from Type_Invariant'Class, Invariant'Class,
11794 Pre'Class, and Post'Class aspects. Also list inherited subtype predicates.
11795 @end table
11796
11797 @geindex -gnatw.L (gcc)
11798
11799
11800 @table @asis
11801
11802 @item @code{-gnatw.L}
11803
11804 @emph{Suppress listing of inherited aspects.}
11805
11806 This switch suppresses listing of inherited aspects.
11807 @end table
11808
11809 @geindex -gnatwm (gcc)
11810
11811
11812 @table @asis
11813
11814 @item @code{-gnatwm}
11815
11816 @emph{Activate warnings on modified but unreferenced variables.}
11817
11818 This switch activates warnings for variables that are assigned (using
11819 an initialization value or with one or more assignment statements) but
11820 whose value is never read. The warning is suppressed for volatile
11821 variables and also for variables that are renamings of other variables
11822 or for which an address clause is given.
11823 The default is that these warnings are not given.
11824 @end table
11825
11826 @geindex -gnatwM (gcc)
11827
11828
11829 @table @asis
11830
11831 @item @code{-gnatwM}
11832
11833 @emph{Disable warnings on modified but unreferenced variables.}
11834
11835 This switch disables warnings for variables that are assigned or
11836 initialized, but never read.
11837 @end table
11838
11839 @geindex -gnatw.m (gcc)
11840
11841
11842 @table @asis
11843
11844 @item @code{-gnatw.m}
11845
11846 @emph{Activate warnings on suspicious modulus values.}
11847
11848 This switch activates warnings for modulus values that seem suspicious.
11849 The cases caught are where the size is the same as the modulus (e.g.
11850 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
11851 with no size clause. The guess in both cases is that 2**x was intended
11852 rather than x. In addition expressions of the form 2*x for small x
11853 generate a warning (the almost certainly accurate guess being that
11854 2**x was intended). The default is that these warnings are given.
11855 @end table
11856
11857 @geindex -gnatw.M (gcc)
11858
11859
11860 @table @asis
11861
11862 @item @code{-gnatw.M}
11863
11864 @emph{Disable warnings on suspicious modulus values.}
11865
11866 This switch disables warnings for suspicious modulus values.
11867 @end table
11868
11869 @geindex -gnatwn (gcc)
11870
11871
11872 @table @asis
11873
11874 @item @code{-gnatwn}
11875
11876 @emph{Set normal warnings mode.}
11877
11878 This switch sets normal warning mode, in which enabled warnings are
11879 issued and treated as warnings rather than errors. This is the default
11880 mode. the switch @code{-gnatwn} can be used to cancel the effect of
11881 an explicit @code{-gnatws} or
11882 @code{-gnatwe}. It also cancels the effect of the
11883 implicit @code{-gnatwe} that is activated by the
11884 use of @code{-gnatg}.
11885 @end table
11886
11887 @geindex -gnatw.n (gcc)
11888
11889 @geindex Atomic Synchronization
11890 @geindex warnings
11891
11892
11893 @table @asis
11894
11895 @item @code{-gnatw.n}
11896
11897 @emph{Activate warnings on atomic synchronization.}
11898
11899 This switch actives warnings when an access to an atomic variable
11900 requires the generation of atomic synchronization code. These
11901 warnings are off by default.
11902 @end table
11903
11904 @geindex -gnatw.N (gcc)
11905
11906
11907 @table @asis
11908
11909 @item @code{-gnatw.N}
11910
11911 @emph{Suppress warnings on atomic synchronization.}
11912
11913 @geindex Atomic Synchronization
11914 @geindex warnings
11915
11916 This switch suppresses warnings when an access to an atomic variable
11917 requires the generation of atomic synchronization code.
11918 @end table
11919
11920 @geindex -gnatwo (gcc)
11921
11922 @geindex Address Clauses
11923 @geindex warnings
11924
11925
11926 @table @asis
11927
11928 @item @code{-gnatwo}
11929
11930 @emph{Activate warnings on address clause overlays.}
11931
11932 This switch activates warnings for possibly unintended initialization
11933 effects of defining address clauses that cause one variable to overlap
11934 another. The default is that such warnings are generated.
11935 @end table
11936
11937 @geindex -gnatwO (gcc)
11938
11939
11940 @table @asis
11941
11942 @item @code{-gnatwO}
11943
11944 @emph{Suppress warnings on address clause overlays.}
11945
11946 This switch suppresses warnings on possibly unintended initialization
11947 effects of defining address clauses that cause one variable to overlap
11948 another.
11949 @end table
11950
11951 @geindex -gnatw.o (gcc)
11952
11953
11954 @table @asis
11955
11956 @item @code{-gnatw.o}
11957
11958 @emph{Activate warnings on modified but unreferenced out parameters.}
11959
11960 This switch activates warnings for variables that are modified by using
11961 them as actuals for a call to a procedure with an out mode formal, where
11962 the resulting assigned value is never read. It is applicable in the case
11963 where there is more than one out mode formal. If there is only one out
11964 mode formal, the warning is issued by default (controlled by -gnatwu).
11965 The warning is suppressed for volatile
11966 variables and also for variables that are renamings of other variables
11967 or for which an address clause is given.
11968 The default is that these warnings are not given.
11969 @end table
11970
11971 @geindex -gnatw.O (gcc)
11972
11973
11974 @table @asis
11975
11976 @item @code{-gnatw.O}
11977
11978 @emph{Disable warnings on modified but unreferenced out parameters.}
11979
11980 This switch suppresses warnings for variables that are modified by using
11981 them as actuals for a call to a procedure with an out mode formal, where
11982 the resulting assigned value is never read.
11983 @end table
11984
11985 @geindex -gnatwp (gcc)
11986
11987 @geindex Inlining
11988 @geindex warnings
11989
11990
11991 @table @asis
11992
11993 @item @code{-gnatwp}
11994
11995 @emph{Activate warnings on ineffective pragma Inlines.}
11996
11997 This switch activates warnings for failure of front end inlining
11998 (activated by @code{-gnatN}) to inline a particular call. There are
11999 many reasons for not being able to inline a call, including most
12000 commonly that the call is too complex to inline. The default is
12001 that such warnings are not given.
12002 Warnings on ineffective inlining by the gcc back-end can be activated
12003 separately, using the gcc switch -Winline.
12004 @end table
12005
12006 @geindex -gnatwP (gcc)
12007
12008
12009 @table @asis
12010
12011 @item @code{-gnatwP}
12012
12013 @emph{Suppress warnings on ineffective pragma Inlines.}
12014
12015 This switch suppresses warnings on ineffective pragma Inlines. If the
12016 inlining mechanism cannot inline a call, it will simply ignore the
12017 request silently.
12018 @end table
12019
12020 @geindex -gnatw.p (gcc)
12021
12022 @geindex Parameter order
12023 @geindex warnings
12024
12025
12026 @table @asis
12027
12028 @item @code{-gnatw.p}
12029
12030 @emph{Activate warnings on parameter ordering.}
12031
12032 This switch activates warnings for cases of suspicious parameter
12033 ordering when the list of arguments are all simple identifiers that
12034 match the names of the formals, but are in a different order. The
12035 warning is suppressed if any use of named parameter notation is used,
12036 so this is the appropriate way to suppress a false positive (and
12037 serves to emphasize that the "misordering" is deliberate). The
12038 default is that such warnings are not given.
12039 @end table
12040
12041 @geindex -gnatw.P (gcc)
12042
12043
12044 @table @asis
12045
12046 @item @code{-gnatw.P}
12047
12048 @emph{Suppress warnings on parameter ordering.}
12049
12050 This switch suppresses warnings on cases of suspicious parameter
12051 ordering.
12052 @end table
12053
12054 @geindex -gnatwq (gcc)
12055
12056 @geindex Parentheses
12057 @geindex warnings
12058
12059
12060 @table @asis
12061
12062 @item @code{-gnatwq}
12063
12064 @emph{Activate warnings on questionable missing parentheses.}
12065
12066 This switch activates warnings for cases where parentheses are not used and
12067 the result is potential ambiguity from a readers point of view. For example
12068 (not a > b) when a and b are modular means ((not a) > b) and very likely the
12069 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
12070 quite likely ((-x) mod 5) was intended. In such situations it seems best to
12071 follow the rule of always parenthesizing to make the association clear, and
12072 this warning switch warns if such parentheses are not present. The default
12073 is that these warnings are given.
12074 @end table
12075
12076 @geindex -gnatwQ (gcc)
12077
12078
12079 @table @asis
12080
12081 @item @code{-gnatwQ}
12082
12083 @emph{Suppress warnings on questionable missing parentheses.}
12084
12085 This switch suppresses warnings for cases where the association is not
12086 clear and the use of parentheses is preferred.
12087 @end table
12088
12089 @geindex -gnatw.q (gcc)
12090
12091 @geindex Layout
12092 @geindex warnings
12093
12094
12095 @table @asis
12096
12097 @item @code{-gnatw.q}
12098
12099 @emph{Activate warnings on questionable layout of record types.}
12100
12101 This switch activates warnings for cases where the default layout of
12102 a record type, that is to say the layout of its components in textual
12103 order of the source code, would very likely cause inefficiencies in
12104 the code generated by the compiler, both in terms of space and speed
12105 during execution. One warning is issued for each problematic component
12106 without representation clause in the nonvariant part and then in each
12107 variant recursively, if any.
12108
12109 The purpose of these warnings is neither to prescribe an optimal layout
12110 nor to force the use of representation clauses, but rather to get rid of
12111 the most blatant inefficiencies in the layout. Therefore, the default
12112 layout is matched against the following synthetic ordered layout and
12113 the deviations are flagged on a component-by-component basis:
12114
12115
12116 @itemize *
12117
12118 @item
12119 first all components or groups of components whose length is fixed
12120 and a multiple of the storage unit,
12121
12122 @item
12123 then the remaining components whose length is fixed and not a multiple
12124 of the storage unit,
12125
12126 @item
12127 then the remaining components whose length doesn't depend on discriminants
12128 (that is to say, with variable but uniform length for all objects),
12129
12130 @item
12131 then all components whose length depends on discriminants,
12132
12133 @item
12134 finally the variant part (if any),
12135 @end itemize
12136
12137 for the nonvariant part and for each variant recursively, if any.
12138
12139 The exact wording of the warning depends on whether the compiler is allowed
12140 to reorder the components in the record type or precluded from doing it by
12141 means of pragma @code{No_Component_Reordering}.
12142
12143 The default is that these warnings are not given.
12144 @end table
12145
12146 @geindex -gnatw.Q (gcc)
12147
12148
12149 @table @asis
12150
12151 @item @code{-gnatw.Q}
12152
12153 @emph{Suppress warnings on questionable layout of record types.}
12154
12155 This switch suppresses warnings for cases where the default layout of
12156 a record type would very likely cause inefficiencies.
12157 @end table
12158
12159 @geindex -gnatwr (gcc)
12160
12161
12162 @table @asis
12163
12164 @item @code{-gnatwr}
12165
12166 @emph{Activate warnings on redundant constructs.}
12167
12168 This switch activates warnings for redundant constructs. The following
12169 is the current list of constructs regarded as redundant:
12170
12171
12172 @itemize *
12173
12174 @item
12175 Assignment of an item to itself.
12176
12177 @item
12178 Type conversion that converts an expression to its own type.
12179
12180 @item
12181 Use of the attribute @code{Base} where @code{typ'Base} is the same
12182 as @code{typ}.
12183
12184 @item
12185 Use of pragma @code{Pack} when all components are placed by a record
12186 representation clause.
12187
12188 @item
12189 Exception handler containing only a reraise statement (raise with no
12190 operand) which has no effect.
12191
12192 @item
12193 Use of the operator abs on an operand that is known at compile time
12194 to be non-negative
12195
12196 @item
12197 Comparison of an object or (unary or binary) operation of boolean type to
12198 an explicit True value.
12199 @end itemize
12200
12201 The default is that warnings for redundant constructs are not given.
12202 @end table
12203
12204 @geindex -gnatwR (gcc)
12205
12206
12207 @table @asis
12208
12209 @item @code{-gnatwR}
12210
12211 @emph{Suppress warnings on redundant constructs.}
12212
12213 This switch suppresses warnings for redundant constructs.
12214 @end table
12215
12216 @geindex -gnatw.r (gcc)
12217
12218
12219 @table @asis
12220
12221 @item @code{-gnatw.r}
12222
12223 @emph{Activate warnings for object renaming function.}
12224
12225 This switch activates warnings for an object renaming that renames a
12226 function call, which is equivalent to a constant declaration (as
12227 opposed to renaming the function itself). The default is that these
12228 warnings are given.
12229 @end table
12230
12231 @geindex -gnatw.R (gcc)
12232
12233
12234 @table @asis
12235
12236 @item @code{-gnatw.R}
12237
12238 @emph{Suppress warnings for object renaming function.}
12239
12240 This switch suppresses warnings for object renaming function.
12241 @end table
12242
12243 @geindex -gnatw_r (gcc)
12244
12245
12246 @table @asis
12247
12248 @item @code{-gnatw_r}
12249
12250 @emph{Activate warnings for out-of-order record representation clauses.}
12251
12252 This switch activates warnings for record representation clauses,
12253 if the order of component declarations, component clauses,
12254 and bit-level layout do not all agree.
12255 The default is that these warnings are not given.
12256 @end table
12257
12258 @geindex -gnatw_R (gcc)
12259
12260
12261 @table @asis
12262
12263 @item @code{-gnatw_R}
12264
12265 @emph{Suppress warnings for out-of-order record representation clauses.}
12266 @end table
12267
12268 @geindex -gnatws (gcc)
12269
12270
12271 @table @asis
12272
12273 @item @code{-gnatws}
12274
12275 @emph{Suppress all warnings.}
12276
12277 This switch completely suppresses the
12278 output of all warning messages from the GNAT front end, including
12279 both warnings that can be controlled by switches described in this
12280 section, and those that are normally given unconditionally. The
12281 effect of this suppress action can only be cancelled by a subsequent
12282 use of the switch @code{-gnatwn}.
12283
12284 Note that switch @code{-gnatws} does not suppress
12285 warnings from the @code{gcc} back end.
12286 To suppress these back end warnings as well, use the switch @code{-w}
12287 in addition to @code{-gnatws}. Also this switch has no effect on the
12288 handling of style check messages.
12289 @end table
12290
12291 @geindex -gnatw.s (gcc)
12292
12293 @geindex Record Representation (component sizes)
12294
12295
12296 @table @asis
12297
12298 @item @code{-gnatw.s}
12299
12300 @emph{Activate warnings on overridden size clauses.}
12301
12302 This switch activates warnings on component clauses in record
12303 representation clauses where the length given overrides that
12304 specified by an explicit size clause for the component type. A
12305 warning is similarly given in the array case if a specified
12306 component size overrides an explicit size clause for the array
12307 component type.
12308 @end table
12309
12310 @geindex -gnatw.S (gcc)
12311
12312
12313 @table @asis
12314
12315 @item @code{-gnatw.S}
12316
12317 @emph{Suppress warnings on overridden size clauses.}
12318
12319 This switch suppresses warnings on component clauses in record
12320 representation clauses that override size clauses, and similar
12321 warnings when an array component size overrides a size clause.
12322 @end table
12323
12324 @geindex -gnatwt (gcc)
12325
12326 @geindex Deactivated code
12327 @geindex warnings
12328
12329 @geindex Deleted code
12330 @geindex warnings
12331
12332
12333 @table @asis
12334
12335 @item @code{-gnatwt}
12336
12337 @emph{Activate warnings for tracking of deleted conditional code.}
12338
12339 This switch activates warnings for tracking of code in conditionals (IF and
12340 CASE statements) that is detected to be dead code which cannot be executed, and
12341 which is removed by the front end. This warning is off by default. This may be
12342 useful for detecting deactivated code in certified applications.
12343 @end table
12344
12345 @geindex -gnatwT (gcc)
12346
12347
12348 @table @asis
12349
12350 @item @code{-gnatwT}
12351
12352 @emph{Suppress warnings for tracking of deleted conditional code.}
12353
12354 This switch suppresses warnings for tracking of deleted conditional code.
12355 @end table
12356
12357 @geindex -gnatw.t (gcc)
12358
12359
12360 @table @asis
12361
12362 @item @code{-gnatw.t}
12363
12364 @emph{Activate warnings on suspicious contracts.}
12365
12366 This switch activates warnings on suspicious contracts. This includes
12367 warnings on suspicious postconditions (whether a pragma @code{Postcondition} or a
12368 @code{Post} aspect in Ada 2012) and suspicious contract cases (pragma or aspect
12369 @code{Contract_Cases}). A function postcondition or contract case is suspicious
12370 when no postcondition or contract case for this function mentions the result
12371 of the function. A procedure postcondition or contract case is suspicious
12372 when it only refers to the pre-state of the procedure, because in that case
12373 it should rather be expressed as a precondition. This switch also controls
12374 warnings on suspicious cases of expressions typically found in contracts like
12375 quantified expressions and uses of Update attribute. The default is that such
12376 warnings are generated.
12377 @end table
12378
12379 @geindex -gnatw.T (gcc)
12380
12381
12382 @table @asis
12383
12384 @item @code{-gnatw.T}
12385
12386 @emph{Suppress warnings on suspicious contracts.}
12387
12388 This switch suppresses warnings on suspicious contracts.
12389 @end table
12390
12391 @geindex -gnatwu (gcc)
12392
12393
12394 @table @asis
12395
12396 @item @code{-gnatwu}
12397
12398 @emph{Activate warnings on unused entities.}
12399
12400 This switch activates warnings to be generated for entities that
12401 are declared but not referenced, and for units that are @emph{with}ed
12402 and not
12403 referenced. In the case of packages, a warning is also generated if
12404 no entities in the package are referenced. This means that if a with'ed
12405 package is referenced but the only references are in @code{use}
12406 clauses or @code{renames}
12407 declarations, a warning is still generated. A warning is also generated
12408 for a generic package that is @emph{with}ed but never instantiated.
12409 In the case where a package or subprogram body is compiled, and there
12410 is a @emph{with} on the corresponding spec
12411 that is only referenced in the body,
12412 a warning is also generated, noting that the
12413 @emph{with} can be moved to the body. The default is that
12414 such warnings are not generated.
12415 This switch also activates warnings on unreferenced formals
12416 (it includes the effect of @code{-gnatwf}).
12417 @end table
12418
12419 @geindex -gnatwU (gcc)
12420
12421
12422 @table @asis
12423
12424 @item @code{-gnatwU}
12425
12426 @emph{Suppress warnings on unused entities.}
12427
12428 This switch suppresses warnings for unused entities and packages.
12429 It also turns off warnings on unreferenced formals (and thus includes
12430 the effect of @code{-gnatwF}).
12431 @end table
12432
12433 @geindex -gnatw.u (gcc)
12434
12435
12436 @table @asis
12437
12438 @item @code{-gnatw.u}
12439
12440 @emph{Activate warnings on unordered enumeration types.}
12441
12442 This switch causes enumeration types to be considered as conceptually
12443 unordered, unless an explicit pragma @code{Ordered} is given for the type.
12444 The effect is to generate warnings in clients that use explicit comparisons
12445 or subranges, since these constructs both treat objects of the type as
12446 ordered. (A @emph{client} is defined as a unit that is other than the unit in
12447 which the type is declared, or its body or subunits.) Please refer to
12448 the description of pragma @code{Ordered} in the
12449 @cite{GNAT Reference Manual} for further details.
12450 The default is that such warnings are not generated.
12451 @end table
12452
12453 @geindex -gnatw.U (gcc)
12454
12455
12456 @table @asis
12457
12458 @item @code{-gnatw.U}
12459
12460 @emph{Deactivate warnings on unordered enumeration types.}
12461
12462 This switch causes all enumeration types to be considered as ordered, so
12463 that no warnings are given for comparisons or subranges for any type.
12464 @end table
12465
12466 @geindex -gnatwv (gcc)
12467
12468 @geindex Unassigned variable warnings
12469
12470
12471 @table @asis
12472
12473 @item @code{-gnatwv}
12474
12475 @emph{Activate warnings on unassigned variables.}
12476
12477 This switch activates warnings for access to variables which
12478 may not be properly initialized. The default is that
12479 such warnings are generated.
12480 @end table
12481
12482 @geindex -gnatwV (gcc)
12483
12484
12485 @table @asis
12486
12487 @item @code{-gnatwV}
12488
12489 @emph{Suppress warnings on unassigned variables.}
12490
12491 This switch suppresses warnings for access to variables which
12492 may not be properly initialized.
12493 For variables of a composite type, the warning can also be suppressed in
12494 Ada 2005 by using a default initialization with a box. For example, if
12495 Table is an array of records whose components are only partially uninitialized,
12496 then the following code:
12497
12498 @example
12499 Tab : Table := (others => <>);
12500 @end example
12501
12502 will suppress warnings on subsequent statements that access components
12503 of variable Tab.
12504 @end table
12505
12506 @geindex -gnatw.v (gcc)
12507
12508 @geindex bit order warnings
12509
12510
12511 @table @asis
12512
12513 @item @code{-gnatw.v}
12514
12515 @emph{Activate info messages for non-default bit order.}
12516
12517 This switch activates messages (labeled "info", they are not warnings,
12518 just informational messages) about the effects of non-default bit-order
12519 on records to which a component clause is applied. The effect of specifying
12520 non-default bit ordering is a bit subtle (and changed with Ada 2005), so
12521 these messages, which are given by default, are useful in understanding the
12522 exact consequences of using this feature.
12523 @end table
12524
12525 @geindex -gnatw.V (gcc)
12526
12527
12528 @table @asis
12529
12530 @item @code{-gnatw.V}
12531
12532 @emph{Suppress info messages for non-default bit order.}
12533
12534 This switch suppresses information messages for the effects of specifying
12535 non-default bit order on record components with component clauses.
12536 @end table
12537
12538 @geindex -gnatww (gcc)
12539
12540 @geindex String indexing warnings
12541
12542
12543 @table @asis
12544
12545 @item @code{-gnatww}
12546
12547 @emph{Activate warnings on wrong low bound assumption.}
12548
12549 This switch activates warnings for indexing an unconstrained string parameter
12550 with a literal or S'Length. This is a case where the code is assuming that the
12551 low bound is one, which is in general not true (for example when a slice is
12552 passed). The default is that such warnings are generated.
12553 @end table
12554
12555 @geindex -gnatwW (gcc)
12556
12557
12558 @table @asis
12559
12560 @item @code{-gnatwW}
12561
12562 @emph{Suppress warnings on wrong low bound assumption.}
12563
12564 This switch suppresses warnings for indexing an unconstrained string parameter
12565 with a literal or S'Length. Note that this warning can also be suppressed
12566 in a particular case by adding an assertion that the lower bound is 1,
12567 as shown in the following example:
12568
12569 @example
12570 procedure K (S : String) is
12571 pragma Assert (S'First = 1);
12572 ...
12573 @end example
12574 @end table
12575
12576 @geindex -gnatw.w (gcc)
12577
12578 @geindex Warnings Off control
12579
12580
12581 @table @asis
12582
12583 @item @code{-gnatw.w}
12584
12585 @emph{Activate warnings on Warnings Off pragmas.}
12586
12587 This switch activates warnings for use of @code{pragma Warnings (Off, entity)}
12588 where either the pragma is entirely useless (because it suppresses no
12589 warnings), or it could be replaced by @code{pragma Unreferenced} or
12590 @code{pragma Unmodified}.
12591 Also activates warnings for the case of
12592 Warnings (Off, String), where either there is no matching
12593 Warnings (On, String), or the Warnings (Off) did not suppress any warning.
12594 The default is that these warnings are not given.
12595 @end table
12596
12597 @geindex -gnatw.W (gcc)
12598
12599
12600 @table @asis
12601
12602 @item @code{-gnatw.W}
12603
12604 @emph{Suppress warnings on unnecessary Warnings Off pragmas.}
12605
12606 This switch suppresses warnings for use of @code{pragma Warnings (Off, ...)}.
12607 @end table
12608
12609 @geindex -gnatwx (gcc)
12610
12611 @geindex Export/Import pragma warnings
12612
12613
12614 @table @asis
12615
12616 @item @code{-gnatwx}
12617
12618 @emph{Activate warnings on Export/Import pragmas.}
12619
12620 This switch activates warnings on Export/Import pragmas when
12621 the compiler detects a possible conflict between the Ada and
12622 foreign language calling sequences. For example, the use of
12623 default parameters in a convention C procedure is dubious
12624 because the C compiler cannot supply the proper default, so
12625 a warning is issued. The default is that such warnings are
12626 generated.
12627 @end table
12628
12629 @geindex -gnatwX (gcc)
12630
12631
12632 @table @asis
12633
12634 @item @code{-gnatwX}
12635
12636 @emph{Suppress warnings on Export/Import pragmas.}
12637
12638 This switch suppresses warnings on Export/Import pragmas.
12639 The sense of this is that you are telling the compiler that
12640 you know what you are doing in writing the pragma, and it
12641 should not complain at you.
12642 @end table
12643
12644 @geindex -gnatwm (gcc)
12645
12646
12647 @table @asis
12648
12649 @item @code{-gnatw.x}
12650
12651 @emph{Activate warnings for No_Exception_Propagation mode.}
12652
12653 This switch activates warnings for exception usage when pragma Restrictions
12654 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
12655 explicit exception raises which are not covered by a local handler, and for
12656 exception handlers which do not cover a local raise. The default is that
12657 these warnings are given for units that contain exception handlers.
12658
12659 @item @code{-gnatw.X}
12660
12661 @emph{Disable warnings for No_Exception_Propagation mode.}
12662
12663 This switch disables warnings for exception usage when pragma Restrictions
12664 (No_Exception_Propagation) is in effect.
12665 @end table
12666
12667 @geindex -gnatwy (gcc)
12668
12669 @geindex Ada compatibility issues warnings
12670
12671
12672 @table @asis
12673
12674 @item @code{-gnatwy}
12675
12676 @emph{Activate warnings for Ada compatibility issues.}
12677
12678 For the most part, newer versions of Ada are upwards compatible
12679 with older versions. For example, Ada 2005 programs will almost
12680 always work when compiled as Ada 2012.
12681 However there are some exceptions (for example the fact that
12682 @code{some} is now a reserved word in Ada 2012). This
12683 switch activates several warnings to help in identifying
12684 and correcting such incompatibilities. The default is that
12685 these warnings are generated. Note that at one point Ada 2005
12686 was called Ada 0Y, hence the choice of character.
12687 @end table
12688
12689 @geindex -gnatwY (gcc)
12690
12691 @geindex Ada compatibility issues warnings
12692
12693
12694 @table @asis
12695
12696 @item @code{-gnatwY}
12697
12698 @emph{Disable warnings for Ada compatibility issues.}
12699
12700 This switch suppresses the warnings intended to help in identifying
12701 incompatibilities between Ada language versions.
12702 @end table
12703
12704 @geindex -gnatw.y (gcc)
12705
12706 @geindex Package spec needing body
12707
12708
12709 @table @asis
12710
12711 @item @code{-gnatw.y}
12712
12713 @emph{Activate information messages for why package spec needs body.}
12714
12715 There are a number of cases in which a package spec needs a body.
12716 For example, the use of pragma Elaborate_Body, or the declaration
12717 of a procedure specification requiring a completion. This switch
12718 causes information messages to be output showing why a package
12719 specification requires a body. This can be useful in the case of
12720 a large package specification which is unexpectedly requiring a
12721 body. The default is that such information messages are not output.
12722 @end table
12723
12724 @geindex -gnatw.Y (gcc)
12725
12726 @geindex No information messages for why package spec needs body
12727
12728
12729 @table @asis
12730
12731 @item @code{-gnatw.Y}
12732
12733 @emph{Disable information messages for why package spec needs body.}
12734
12735 This switch suppresses the output of information messages showing why
12736 a package specification needs a body.
12737 @end table
12738
12739 @geindex -gnatwz (gcc)
12740
12741 @geindex Unchecked_Conversion warnings
12742
12743
12744 @table @asis
12745
12746 @item @code{-gnatwz}
12747
12748 @emph{Activate warnings on unchecked conversions.}
12749
12750 This switch activates warnings for unchecked conversions
12751 where the types are known at compile time to have different
12752 sizes. The default is that such warnings are generated. Warnings are also
12753 generated for subprogram pointers with different conventions.
12754 @end table
12755
12756 @geindex -gnatwZ (gcc)
12757
12758
12759 @table @asis
12760
12761 @item @code{-gnatwZ}
12762
12763 @emph{Suppress warnings on unchecked conversions.}
12764
12765 This switch suppresses warnings for unchecked conversions
12766 where the types are known at compile time to have different
12767 sizes or conventions.
12768 @end table
12769
12770 @geindex -gnatw.z (gcc)
12771
12772 @geindex Size/Alignment warnings
12773
12774
12775 @table @asis
12776
12777 @item @code{-gnatw.z}
12778
12779 @emph{Activate warnings for size not a multiple of alignment.}
12780
12781 This switch activates warnings for cases of array and record types
12782 with specified @code{Size} and @code{Alignment} attributes where the
12783 size is not a multiple of the alignment, resulting in an object
12784 size that is greater than the specified size. The default
12785 is that such warnings are generated.
12786 @end table
12787
12788 @geindex -gnatw.Z (gcc)
12789
12790 @geindex Size/Alignment warnings
12791
12792
12793 @table @asis
12794
12795 @item @code{-gnatw.Z}
12796
12797 @emph{Suppress warnings for size not a multiple of alignment.}
12798
12799 This switch suppresses warnings for cases of array and record types
12800 with specified @code{Size} and @code{Alignment} attributes where the
12801 size is not a multiple of the alignment, resulting in an object
12802 size that is greater than the specified size. The warning can also
12803 be suppressed by giving an explicit @code{Object_Size} value.
12804 @end table
12805
12806 @geindex -Wunused (gcc)
12807
12808
12809 @table @asis
12810
12811 @item @code{-Wunused}
12812
12813 The warnings controlled by the @code{-gnatw} switch are generated by
12814 the front end of the compiler. The GCC back end can provide
12815 additional warnings and they are controlled by the @code{-W} switch.
12816 For example, @code{-Wunused} activates back end
12817 warnings for entities that are declared but not referenced.
12818 @end table
12819
12820 @geindex -Wuninitialized (gcc)
12821
12822
12823 @table @asis
12824
12825 @item @code{-Wuninitialized}
12826
12827 Similarly, @code{-Wuninitialized} activates
12828 the back end warning for uninitialized variables. This switch must be
12829 used in conjunction with an optimization level greater than zero.
12830 @end table
12831
12832 @geindex -Wstack-usage (gcc)
12833
12834
12835 @table @asis
12836
12837 @item @code{-Wstack-usage=@emph{len}}
12838
12839 Warn if the stack usage of a subprogram might be larger than @code{len} bytes.
12840 See @ref{f5,,Static Stack Usage Analysis} for details.
12841 @end table
12842
12843 @geindex -Wall (gcc)
12844
12845
12846 @table @asis
12847
12848 @item @code{-Wall}
12849
12850 This switch enables most warnings from the GCC back end.
12851 The code generator detects a number of warning situations that are missed
12852 by the GNAT front end, and this switch can be used to activate them.
12853 The use of this switch also sets the default front end warning mode to
12854 @code{-gnatwa}, that is, most front end warnings activated as well.
12855 @end table
12856
12857 @geindex -w (gcc)
12858
12859
12860 @table @asis
12861
12862 @item @code{-w}
12863
12864 Conversely, this switch suppresses warnings from the GCC back end.
12865 The use of this switch also sets the default front end warning mode to
12866 @code{-gnatws}, that is, front end warnings suppressed as well.
12867 @end table
12868
12869 @geindex -Werror (gcc)
12870
12871
12872 @table @asis
12873
12874 @item @code{-Werror}
12875
12876 This switch causes warnings from the GCC back end to be treated as
12877 errors. The warning string still appears, but the warning messages are
12878 counted as errors, and prevent the generation of an object file.
12879 @end table
12880
12881 A string of warning parameters can be used in the same parameter. For example:
12882
12883 @example
12884 -gnatwaGe
12885 @end example
12886
12887 will turn on all optional warnings except for unrecognized pragma warnings,
12888 and also specify that warnings should be treated as errors.
12889
12890 When no switch @code{-gnatw} is used, this is equivalent to:
12891
12892 @quotation
12893
12894
12895 @itemize *
12896
12897 @item
12898 @code{-gnatw.a}
12899
12900 @item
12901 @code{-gnatwB}
12902
12903 @item
12904 @code{-gnatw.b}
12905
12906 @item
12907 @code{-gnatwC}
12908
12909 @item
12910 @code{-gnatw.C}
12911
12912 @item
12913 @code{-gnatwD}
12914
12915 @item
12916 @code{-gnatw.D}
12917
12918 @item
12919 @code{-gnatwF}
12920
12921 @item
12922 @code{-gnatw.F}
12923
12924 @item
12925 @code{-gnatwg}
12926
12927 @item
12928 @code{-gnatwH}
12929
12930 @item
12931 @code{-gnatw.H}
12932
12933 @item
12934 @code{-gnatwi}
12935
12936 @item
12937 @code{-gnatwJ}
12938
12939 @item
12940 @code{-gnatw.J}
12941
12942 @item
12943 @code{-gnatwK}
12944
12945 @item
12946 @code{-gnatw.K}
12947
12948 @item
12949 @code{-gnatwL}
12950
12951 @item
12952 @code{-gnatw.L}
12953
12954 @item
12955 @code{-gnatwM}
12956
12957 @item
12958 @code{-gnatw.m}
12959
12960 @item
12961 @code{-gnatwn}
12962
12963 @item
12964 @code{-gnatw.N}
12965
12966 @item
12967 @code{-gnatwo}
12968
12969 @item
12970 @code{-gnatw.O}
12971
12972 @item
12973 @code{-gnatwP}
12974
12975 @item
12976 @code{-gnatw.P}
12977
12978 @item
12979 @code{-gnatwq}
12980
12981 @item
12982 @code{-gnatw.Q}
12983
12984 @item
12985 @code{-gnatwR}
12986
12987 @item
12988 @code{-gnatw.R}
12989
12990 @item
12991 @code{-gnatw.S}
12992
12993 @item
12994 @code{-gnatwT}
12995
12996 @item
12997 @code{-gnatw.t}
12998
12999 @item
13000 @code{-gnatwU}
13001
13002 @item
13003 @code{-gnatw.U}
13004
13005 @item
13006 @code{-gnatwv}
13007
13008 @item
13009 @code{-gnatw.v}
13010
13011 @item
13012 @code{-gnatww}
13013
13014 @item
13015 @code{-gnatw.W}
13016
13017 @item
13018 @code{-gnatwx}
13019
13020 @item
13021 @code{-gnatw.X}
13022
13023 @item
13024 @code{-gnatwy}
13025
13026 @item
13027 @code{-gnatw.Y}
13028
13029 @item
13030 @code{-gnatwz}
13031
13032 @item
13033 @code{-gnatw.z}
13034 @end itemize
13035 @end quotation
13036
13037 @node Debugging and Assertion Control,Validity Checking,Warning Message Control,Compiler Switches
13038 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-and-assertion-control}@anchor{100}@anchor{gnat_ugn/building_executable_programs_with_gnat id16}@anchor{101}
13039 @subsection Debugging and Assertion Control
13040
13041
13042 @geindex -gnata (gcc)
13043
13044
13045 @table @asis
13046
13047 @item @code{-gnata}
13048
13049 @geindex Assert
13050
13051 @geindex Debug
13052
13053 @geindex Assertions
13054
13055 @geindex Precondition
13056
13057 @geindex Postcondition
13058
13059 @geindex Type invariants
13060
13061 @geindex Subtype predicates
13062
13063 The @code{-gnata} option is equivalent to the following @code{Assertion_Policy} pragma:
13064
13065 @example
13066 pragma Assertion_Policy (Check);
13067 @end example
13068
13069 Which is a shorthand for:
13070
13071 @example
13072 pragma Assertion_Policy
13073 (Assert => Check,
13074 Static_Predicate => Check,
13075 Dynamic_Predicate => Check,
13076 Pre => Check,
13077 Pre'Class => Check,
13078 Post => Check,
13079 Post'Class => Check,
13080 Type_Invariant => Check,
13081 Type_Invariant'Class => Check);
13082 @end example
13083
13084 The pragmas @code{Assert} and @code{Debug} normally have no effect and
13085 are ignored. This switch, where @code{a} stands for 'assert', causes
13086 pragmas @code{Assert} and @code{Debug} to be activated. This switch also
13087 causes preconditions, postconditions, subtype predicates, and
13088 type invariants to be activated.
13089
13090 The pragmas have the form:
13091
13092 @example
13093 pragma Assert (<Boolean-expression> [, <static-string-expression>])
13094 pragma Debug (<procedure call>)
13095 pragma Type_Invariant (<type-local-name>, <Boolean-expression>)
13096 pragma Predicate (<type-local-name>, <Boolean-expression>)
13097 pragma Precondition (<Boolean-expression>, <string-expression>)
13098 pragma Postcondition (<Boolean-expression>, <string-expression>)
13099 @end example
13100
13101 The aspects have the form:
13102
13103 @example
13104 with [Pre|Post|Type_Invariant|Dynamic_Predicate|Static_Predicate]
13105 => <Boolean-expression>;
13106 @end example
13107
13108 The @code{Assert} pragma causes @code{Boolean-expression} to be tested.
13109 If the result is @code{True}, the pragma has no effect (other than
13110 possible side effects from evaluating the expression). If the result is
13111 @code{False}, the exception @code{Assert_Failure} declared in the package
13112 @code{System.Assertions} is raised (passing @code{static-string-expression}, if
13113 present, as the message associated with the exception). If no string
13114 expression is given, the default is a string containing the file name and
13115 line number of the pragma.
13116
13117 The @code{Debug} pragma causes @code{procedure} to be called. Note that
13118 @code{pragma Debug} may appear within a declaration sequence, allowing
13119 debugging procedures to be called between declarations.
13120
13121 For the aspect specification, the @code{Boolean-expression} is evaluated.
13122 If the result is @code{True}, the aspect has no effect. If the result
13123 is @code{False}, the exception @code{Assert_Failure} is raised.
13124 @end table
13125
13126 @node Validity Checking,Style Checking,Debugging and Assertion Control,Compiler Switches
13127 @anchor{gnat_ugn/building_executable_programs_with_gnat validity-checking}@anchor{f6}@anchor{gnat_ugn/building_executable_programs_with_gnat id17}@anchor{102}
13128 @subsection Validity Checking
13129
13130
13131 @geindex Validity Checking
13132
13133 The Ada Reference Manual defines the concept of invalid values (see
13134 RM 13.9.1). The primary source of invalid values is uninitialized
13135 variables. A scalar variable that is left uninitialized may contain
13136 an invalid value; the concept of invalid does not apply to access or
13137 composite types.
13138
13139 It is an error to read an invalid value, but the RM does not require
13140 run-time checks to detect such errors, except for some minimal
13141 checking to prevent erroneous execution (i.e. unpredictable
13142 behavior). This corresponds to the @code{-gnatVd} switch below,
13143 which is the default. For example, by default, if the expression of a
13144 case statement is invalid, it will raise Constraint_Error rather than
13145 causing a wild jump, and if an array index on the left-hand side of an
13146 assignment is invalid, it will raise Constraint_Error rather than
13147 overwriting an arbitrary memory location.
13148
13149 The @code{-gnatVa} may be used to enable additional validity checks,
13150 which are not required by the RM. These checks are often very
13151 expensive (which is why the RM does not require them). These checks
13152 are useful in tracking down uninitialized variables, but they are
13153 not usually recommended for production builds, and in particular
13154 we do not recommend using these extra validity checking options in
13155 combination with optimization, since this can confuse the optimizer.
13156 If performance is a consideration, leading to the need to optimize,
13157 then the validity checking options should not be used.
13158
13159 The other @code{-gnatV@emph{x}} switches below allow finer-grained
13160 control; you can enable whichever validity checks you desire. However,
13161 for most debugging purposes, @code{-gnatVa} is sufficient, and the
13162 default @code{-gnatVd} (i.e. standard Ada behavior) is usually
13163 sufficient for non-debugging use.
13164
13165 The @code{-gnatB} switch tells the compiler to assume that all
13166 values are valid (that is, within their declared subtype range)
13167 except in the context of a use of the Valid attribute. This means
13168 the compiler can generate more efficient code, since the range
13169 of values is better known at compile time. However, an uninitialized
13170 variable can cause wild jumps and memory corruption in this mode.
13171
13172 The @code{-gnatV@emph{x}} switch allows control over the validity
13173 checking mode as described below.
13174 The @code{x} argument is a string of letters that
13175 indicate validity checks that are performed or not performed in addition
13176 to the default checks required by Ada as described above.
13177
13178 @geindex -gnatVa (gcc)
13179
13180
13181 @table @asis
13182
13183 @item @code{-gnatVa}
13184
13185 @emph{All validity checks.}
13186
13187 All validity checks are turned on.
13188 That is, @code{-gnatVa} is
13189 equivalent to @code{gnatVcdfimorst}.
13190 @end table
13191
13192 @geindex -gnatVc (gcc)
13193
13194
13195 @table @asis
13196
13197 @item @code{-gnatVc}
13198
13199 @emph{Validity checks for copies.}
13200
13201 The right hand side of assignments, and the initializing values of
13202 object declarations are validity checked.
13203 @end table
13204
13205 @geindex -gnatVd (gcc)
13206
13207
13208 @table @asis
13209
13210 @item @code{-gnatVd}
13211
13212 @emph{Default (RM) validity checks.}
13213
13214 Some validity checks are done by default following normal Ada semantics
13215 (RM 13.9.1 (9-11)).
13216 A check is done in case statements that the expression is within the range
13217 of the subtype. If it is not, Constraint_Error is raised.
13218 For assignments to array components, a check is done that the expression used
13219 as index is within the range. If it is not, Constraint_Error is raised.
13220 Both these validity checks may be turned off using switch @code{-gnatVD}.
13221 They are turned on by default. If @code{-gnatVD} is specified, a subsequent
13222 switch @code{-gnatVd} will leave the checks turned on.
13223 Switch @code{-gnatVD} should be used only if you are sure that all such
13224 expressions have valid values. If you use this switch and invalid values
13225 are present, then the program is erroneous, and wild jumps or memory
13226 overwriting may occur.
13227 @end table
13228
13229 @geindex -gnatVe (gcc)
13230
13231
13232 @table @asis
13233
13234 @item @code{-gnatVe}
13235
13236 @emph{Validity checks for elementary components.}
13237
13238 In the absence of this switch, assignments to record or array components are
13239 not validity checked, even if validity checks for assignments generally
13240 (@code{-gnatVc}) are turned on. In Ada, assignment of composite values do not
13241 require valid data, but assignment of individual components does. So for
13242 example, there is a difference between copying the elements of an array with a
13243 slice assignment, compared to assigning element by element in a loop. This
13244 switch allows you to turn off validity checking for components, even when they
13245 are assigned component by component.
13246 @end table
13247
13248 @geindex -gnatVf (gcc)
13249
13250
13251 @table @asis
13252
13253 @item @code{-gnatVf}
13254
13255 @emph{Validity checks for floating-point values.}
13256
13257 In the absence of this switch, validity checking occurs only for discrete
13258 values. If @code{-gnatVf} is specified, then validity checking also applies
13259 for floating-point values, and NaNs and infinities are considered invalid,
13260 as well as out of range values for constrained types. Note that this means
13261 that standard IEEE infinity mode is not allowed. The exact contexts
13262 in which floating-point values are checked depends on the setting of other
13263 options. For example, @code{-gnatVif} or @code{-gnatVfi}
13264 (the order does not matter) specifies that floating-point parameters of mode
13265 @code{in} should be validity checked.
13266 @end table
13267
13268 @geindex -gnatVi (gcc)
13269
13270
13271 @table @asis
13272
13273 @item @code{-gnatVi}
13274
13275 @emph{Validity checks for `@w{`}in`@w{`} mode parameters.}
13276
13277 Arguments for parameters of mode @code{in} are validity checked in function
13278 and procedure calls at the point of call.
13279 @end table
13280
13281 @geindex -gnatVm (gcc)
13282
13283
13284 @table @asis
13285
13286 @item @code{-gnatVm}
13287
13288 @emph{Validity checks for `@w{`}in out`@w{`} mode parameters.}
13289
13290 Arguments for parameters of mode @code{in out} are validity checked in
13291 procedure calls at the point of call. The @code{'m'} here stands for
13292 modify, since this concerns parameters that can be modified by the call.
13293 Note that there is no specific option to test @code{out} parameters,
13294 but any reference within the subprogram will be tested in the usual
13295 manner, and if an invalid value is copied back, any reference to it
13296 will be subject to validity checking.
13297 @end table
13298
13299 @geindex -gnatVn (gcc)
13300
13301
13302 @table @asis
13303
13304 @item @code{-gnatVn}
13305
13306 @emph{No validity checks.}
13307
13308 This switch turns off all validity checking, including the default checking
13309 for case statements and left hand side subscripts. Note that the use of
13310 the switch @code{-gnatp} suppresses all run-time checks, including
13311 validity checks, and thus implies @code{-gnatVn}. When this switch
13312 is used, it cancels any other @code{-gnatV} previously issued.
13313 @end table
13314
13315 @geindex -gnatVo (gcc)
13316
13317
13318 @table @asis
13319
13320 @item @code{-gnatVo}
13321
13322 @emph{Validity checks for operator and attribute operands.}
13323
13324 Arguments for predefined operators and attributes are validity checked.
13325 This includes all operators in package @code{Standard},
13326 the shift operators defined as intrinsic in package @code{Interfaces}
13327 and operands for attributes such as @code{Pos}. Checks are also made
13328 on individual component values for composite comparisons, and on the
13329 expressions in type conversions and qualified expressions. Checks are
13330 also made on explicit ranges using @code{..} (e.g., slices, loops etc).
13331 @end table
13332
13333 @geindex -gnatVp (gcc)
13334
13335
13336 @table @asis
13337
13338 @item @code{-gnatVp}
13339
13340 @emph{Validity checks for parameters.}
13341
13342 This controls the treatment of parameters within a subprogram (as opposed
13343 to @code{-gnatVi} and @code{-gnatVm} which control validity testing
13344 of parameters on a call. If either of these call options is used, then
13345 normally an assumption is made within a subprogram that the input arguments
13346 have been validity checking at the point of call, and do not need checking
13347 again within a subprogram). If @code{-gnatVp} is set, then this assumption
13348 is not made, and parameters are not assumed to be valid, so their validity
13349 will be checked (or rechecked) within the subprogram.
13350 @end table
13351
13352 @geindex -gnatVr (gcc)
13353
13354
13355 @table @asis
13356
13357 @item @code{-gnatVr}
13358
13359 @emph{Validity checks for function returns.}
13360
13361 The expression in @code{return} statements in functions is validity
13362 checked.
13363 @end table
13364
13365 @geindex -gnatVs (gcc)
13366
13367
13368 @table @asis
13369
13370 @item @code{-gnatVs}
13371
13372 @emph{Validity checks for subscripts.}
13373
13374 All subscripts expressions are checked for validity, whether they appear
13375 on the right side or left side (in default mode only left side subscripts
13376 are validity checked).
13377 @end table
13378
13379 @geindex -gnatVt (gcc)
13380
13381
13382 @table @asis
13383
13384 @item @code{-gnatVt}
13385
13386 @emph{Validity checks for tests.}
13387
13388 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
13389 statements are checked, as well as guard expressions in entry calls.
13390 @end table
13391
13392 The @code{-gnatV} switch may be followed by a string of letters
13393 to turn on a series of validity checking options.
13394 For example, @code{-gnatVcr}
13395 specifies that in addition to the default validity checking, copies and
13396 function return expressions are to be validity checked.
13397 In order to make it easier to specify the desired combination of effects,
13398 the upper case letters @code{CDFIMORST} may
13399 be used to turn off the corresponding lower case option.
13400 Thus @code{-gnatVaM} turns on all validity checking options except for
13401 checking of @code{in out} parameters.
13402
13403 The specification of additional validity checking generates extra code (and
13404 in the case of @code{-gnatVa} the code expansion can be substantial).
13405 However, these additional checks can be very useful in detecting
13406 uninitialized variables, incorrect use of unchecked conversion, and other
13407 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
13408 is useful in conjunction with the extra validity checking, since this
13409 ensures that wherever possible uninitialized variables have invalid values.
13410
13411 See also the pragma @code{Validity_Checks} which allows modification of
13412 the validity checking mode at the program source level, and also allows for
13413 temporary disabling of validity checks.
13414
13415 @node Style Checking,Run-Time Checks,Validity Checking,Compiler Switches
13416 @anchor{gnat_ugn/building_executable_programs_with_gnat id18}@anchor{103}@anchor{gnat_ugn/building_executable_programs_with_gnat style-checking}@anchor{fb}
13417 @subsection Style Checking
13418
13419
13420 @geindex Style checking
13421
13422 @geindex -gnaty (gcc)
13423
13424 The @code{-gnatyx} switch causes the compiler to
13425 enforce specified style rules. A limited set of style rules has been used
13426 in writing the GNAT sources themselves. This switch allows user programs
13427 to activate all or some of these checks. If the source program fails a
13428 specified style check, an appropriate message is given, preceded by
13429 the character sequence '(style)'. This message does not prevent
13430 successful compilation (unless the @code{-gnatwe} switch is used).
13431
13432 Note that this is by no means intended to be a general facility for
13433 checking arbitrary coding standards. It is simply an embedding of the
13434 style rules we have chosen for the GNAT sources. If you are starting
13435 a project which does not have established style standards, you may
13436 find it useful to adopt the entire set of GNAT coding standards, or
13437 some subset of them.
13438
13439
13440 The string @code{x} is a sequence of letters or digits
13441 indicating the particular style
13442 checks to be performed. The following checks are defined:
13443
13444 @geindex -gnaty[0-9] (gcc)
13445
13446
13447 @table @asis
13448
13449 @item @code{-gnaty0}
13450
13451 @emph{Specify indentation level.}
13452
13453 If a digit from 1-9 appears
13454 in the string after @code{-gnaty}
13455 then proper indentation is checked, with the digit indicating the
13456 indentation level required. A value of zero turns off this style check.
13457 The general style of required indentation is as specified by
13458 the examples in the Ada Reference Manual. Full line comments must be
13459 aligned with the @code{--} starting on a column that is a multiple of
13460 the alignment level, or they may be aligned the same way as the following
13461 non-blank line (this is useful when full line comments appear in the middle
13462 of a statement, or they may be aligned with the source line on the previous
13463 non-blank line.
13464 @end table
13465
13466 @geindex -gnatya (gcc)
13467
13468
13469 @table @asis
13470
13471 @item @code{-gnatya}
13472
13473 @emph{Check attribute casing.}
13474
13475 Attribute names, including the case of keywords such as @code{digits}
13476 used as attributes names, must be written in mixed case, that is, the
13477 initial letter and any letter following an underscore must be uppercase.
13478 All other letters must be lowercase.
13479 @end table
13480
13481 @geindex -gnatyA (gcc)
13482
13483
13484 @table @asis
13485
13486 @item @code{-gnatyA}
13487
13488 @emph{Use of array index numbers in array attributes.}
13489
13490 When using the array attributes First, Last, Range,
13491 or Length, the index number must be omitted for one-dimensional arrays
13492 and is required for multi-dimensional arrays.
13493 @end table
13494
13495 @geindex -gnatyb (gcc)
13496
13497
13498 @table @asis
13499
13500 @item @code{-gnatyb}
13501
13502 @emph{Blanks not allowed at statement end.}
13503
13504 Trailing blanks are not allowed at the end of statements. The purpose of this
13505 rule, together with h (no horizontal tabs), is to enforce a canonical format
13506 for the use of blanks to separate source tokens.
13507 @end table
13508
13509 @geindex -gnatyB (gcc)
13510
13511
13512 @table @asis
13513
13514 @item @code{-gnatyB}
13515
13516 @emph{Check Boolean operators.}
13517
13518 The use of AND/OR operators is not permitted except in the cases of modular
13519 operands, array operands, and simple stand-alone boolean variables or
13520 boolean constants. In all other cases @code{and then}/@cite{or else} are
13521 required.
13522 @end table
13523
13524 @geindex -gnatyc (gcc)
13525
13526
13527 @table @asis
13528
13529 @item @code{-gnatyc}
13530
13531 @emph{Check comments, double space.}
13532
13533 Comments must meet the following set of rules:
13534
13535
13536 @itemize *
13537
13538 @item
13539 The @code{--} that starts the column must either start in column one,
13540 or else at least one blank must precede this sequence.
13541
13542 @item
13543 Comments that follow other tokens on a line must have at least one blank
13544 following the @code{--} at the start of the comment.
13545
13546 @item
13547 Full line comments must have at least two blanks following the
13548 @code{--} that starts the comment, with the following exceptions.
13549
13550 @item
13551 A line consisting only of the @code{--} characters, possibly preceded
13552 by blanks is permitted.
13553
13554 @item
13555 A comment starting with @code{--x} where @code{x} is a special character
13556 is permitted.
13557 This allows proper processing of the output from specialized tools
13558 such as @code{gnatprep} (where @code{--!} is used) and in earlier versions of the SPARK
13559 annotation
13560 language (where @code{--#} is used). For the purposes of this rule, a
13561 special character is defined as being in one of the ASCII ranges
13562 @code{16#21#...16#2F#} or @code{16#3A#...16#3F#}.
13563 Note that this usage is not permitted
13564 in GNAT implementation units (i.e., when @code{-gnatg} is used).
13565
13566 @item
13567 A line consisting entirely of minus signs, possibly preceded by blanks, is
13568 permitted. This allows the construction of box comments where lines of minus
13569 signs are used to form the top and bottom of the box.
13570
13571 @item
13572 A comment that starts and ends with @code{--} is permitted as long as at
13573 least one blank follows the initial @code{--}. Together with the preceding
13574 rule, this allows the construction of box comments, as shown in the following
13575 example:
13576
13577 @example
13578 ---------------------------
13579 -- This is a box comment --
13580 -- with two text lines. --
13581 ---------------------------
13582 @end example
13583 @end itemize
13584 @end table
13585
13586 @geindex -gnatyC (gcc)
13587
13588
13589 @table @asis
13590
13591 @item @code{-gnatyC}
13592
13593 @emph{Check comments, single space.}
13594
13595 This is identical to @code{c} except that only one space
13596 is required following the @code{--} of a comment instead of two.
13597 @end table
13598
13599 @geindex -gnatyd (gcc)
13600
13601
13602 @table @asis
13603
13604 @item @code{-gnatyd}
13605
13606 @emph{Check no DOS line terminators present.}
13607
13608 All lines must be terminated by a single ASCII.LF
13609 character (in particular the DOS line terminator sequence CR/LF is not
13610 allowed).
13611 @end table
13612
13613 @geindex -gnatyD (gcc)
13614
13615
13616 @table @asis
13617
13618 @item @code{-gnatyD}
13619
13620 @emph{Check declared identifiers in mixed case.}
13621
13622 Declared identifiers must be in mixed case, as in
13623 This_Is_An_Identifier. Use -gnatyr in addition to ensure
13624 that references match declarations.
13625 @end table
13626
13627 @geindex -gnatye (gcc)
13628
13629
13630 @table @asis
13631
13632 @item @code{-gnatye}
13633
13634 @emph{Check end/exit labels.}
13635
13636 Optional labels on @code{end} statements ending subprograms and on
13637 @code{exit} statements exiting named loops, are required to be present.
13638 @end table
13639
13640 @geindex -gnatyf (gcc)
13641
13642
13643 @table @asis
13644
13645 @item @code{-gnatyf}
13646
13647 @emph{No form feeds or vertical tabs.}
13648
13649 Neither form feeds nor vertical tab characters are permitted
13650 in the source text.
13651 @end table
13652
13653 @geindex -gnatyg (gcc)
13654
13655
13656 @table @asis
13657
13658 @item @code{-gnatyg}
13659
13660 @emph{GNAT style mode.}
13661
13662 The set of style check switches is set to match that used by the GNAT sources.
13663 This may be useful when developing code that is eventually intended to be
13664 incorporated into GNAT. Currently this is equivalent to @code{-gnatyydISux})
13665 but additional style switches may be added to this set in the future without
13666 advance notice.
13667 @end table
13668
13669 @geindex -gnatyh (gcc)
13670
13671
13672 @table @asis
13673
13674 @item @code{-gnatyh}
13675
13676 @emph{No horizontal tabs.}
13677
13678 Horizontal tab characters are not permitted in the source text.
13679 Together with the b (no blanks at end of line) check, this
13680 enforces a canonical form for the use of blanks to separate
13681 source tokens.
13682 @end table
13683
13684 @geindex -gnatyi (gcc)
13685
13686
13687 @table @asis
13688
13689 @item @code{-gnatyi}
13690
13691 @emph{Check if-then layout.}
13692
13693 The keyword @code{then} must appear either on the same
13694 line as corresponding @code{if}, or on a line on its own, lined
13695 up under the @code{if}.
13696 @end table
13697
13698 @geindex -gnatyI (gcc)
13699
13700
13701 @table @asis
13702
13703 @item @code{-gnatyI}
13704
13705 @emph{check mode IN keywords.}
13706
13707 Mode @code{in} (the default mode) is not
13708 allowed to be given explicitly. @code{in out} is fine,
13709 but not @code{in} on its own.
13710 @end table
13711
13712 @geindex -gnatyk (gcc)
13713
13714
13715 @table @asis
13716
13717 @item @code{-gnatyk}
13718
13719 @emph{Check keyword casing.}
13720
13721 All keywords must be in lower case (with the exception of keywords
13722 such as @code{digits} used as attribute names to which this check
13723 does not apply).
13724 @end table
13725
13726 @geindex -gnatyl (gcc)
13727
13728
13729 @table @asis
13730
13731 @item @code{-gnatyl}
13732
13733 @emph{Check layout.}
13734
13735 Layout of statement and declaration constructs must follow the
13736 recommendations in the Ada Reference Manual, as indicated by the
13737 form of the syntax rules. For example an @code{else} keyword must
13738 be lined up with the corresponding @code{if} keyword.
13739
13740 There are two respects in which the style rule enforced by this check
13741 option are more liberal than those in the Ada Reference Manual. First
13742 in the case of record declarations, it is permissible to put the
13743 @code{record} keyword on the same line as the @code{type} keyword, and
13744 then the @code{end} in @code{end record} must line up under @code{type}.
13745 This is also permitted when the type declaration is split on two lines.
13746 For example, any of the following three layouts is acceptable:
13747
13748 @example
13749 type q is record
13750 a : integer;
13751 b : integer;
13752 end record;
13753
13754 type q is
13755 record
13756 a : integer;
13757 b : integer;
13758 end record;
13759
13760 type q is
13761 record
13762 a : integer;
13763 b : integer;
13764 end record;
13765 @end example
13766
13767 Second, in the case of a block statement, a permitted alternative
13768 is to put the block label on the same line as the @code{declare} or
13769 @code{begin} keyword, and then line the @code{end} keyword up under
13770 the block label. For example both the following are permitted:
13771
13772 @example
13773 Block : declare
13774 A : Integer := 3;
13775 begin
13776 Proc (A, A);
13777 end Block;
13778
13779 Block :
13780 declare
13781 A : Integer := 3;
13782 begin
13783 Proc (A, A);
13784 end Block;
13785 @end example
13786
13787 The same alternative format is allowed for loops. For example, both of
13788 the following are permitted:
13789
13790 @example
13791 Clear : while J < 10 loop
13792 A (J) := 0;
13793 end loop Clear;
13794
13795 Clear :
13796 while J < 10 loop
13797 A (J) := 0;
13798 end loop Clear;
13799 @end example
13800 @end table
13801
13802 @geindex -gnatyLnnn (gcc)
13803
13804
13805 @table @asis
13806
13807 @item @code{-gnatyL}
13808
13809 @emph{Set maximum nesting level.}
13810
13811 The maximum level of nesting of constructs (including subprograms, loops,
13812 blocks, packages, and conditionals) may not exceed the given value
13813 @emph{nnn}. A value of zero disconnects this style check.
13814 @end table
13815
13816 @geindex -gnatym (gcc)
13817
13818
13819 @table @asis
13820
13821 @item @code{-gnatym}
13822
13823 @emph{Check maximum line length.}
13824
13825 The length of source lines must not exceed 79 characters, including
13826 any trailing blanks. The value of 79 allows convenient display on an
13827 80 character wide device or window, allowing for possible special
13828 treatment of 80 character lines. Note that this count is of
13829 characters in the source text. This means that a tab character counts
13830 as one character in this count and a wide character sequence counts as
13831 a single character (however many bytes are needed in the encoding).
13832 @end table
13833
13834 @geindex -gnatyMnnn (gcc)
13835
13836
13837 @table @asis
13838
13839 @item @code{-gnatyM}
13840
13841 @emph{Set maximum line length.}
13842
13843 The length of lines must not exceed the
13844 given value @emph{nnn}. The maximum value that can be specified is 32767.
13845 If neither style option for setting the line length is used, then the
13846 default is 255. This also controls the maximum length of lexical elements,
13847 where the only restriction is that they must fit on a single line.
13848 @end table
13849
13850 @geindex -gnatyn (gcc)
13851
13852
13853 @table @asis
13854
13855 @item @code{-gnatyn}
13856
13857 @emph{Check casing of entities in Standard.}
13858
13859 Any identifier from Standard must be cased
13860 to match the presentation in the Ada Reference Manual (for example,
13861 @code{Integer} and @code{ASCII.NUL}).
13862 @end table
13863
13864 @geindex -gnatyN (gcc)
13865
13866
13867 @table @asis
13868
13869 @item @code{-gnatyN}
13870
13871 @emph{Turn off all style checks.}
13872
13873 All style check options are turned off.
13874 @end table
13875
13876 @geindex -gnatyo (gcc)
13877
13878
13879 @table @asis
13880
13881 @item @code{-gnatyo}
13882
13883 @emph{Check order of subprogram bodies.}
13884
13885 All subprogram bodies in a given scope
13886 (e.g., a package body) must be in alphabetical order. The ordering
13887 rule uses normal Ada rules for comparing strings, ignoring casing
13888 of letters, except that if there is a trailing numeric suffix, then
13889 the value of this suffix is used in the ordering (e.g., Junk2 comes
13890 before Junk10).
13891 @end table
13892
13893 @geindex -gnatyO (gcc)
13894
13895
13896 @table @asis
13897
13898 @item @code{-gnatyO}
13899
13900 @emph{Check that overriding subprograms are explicitly marked as such.}
13901
13902 This applies to all subprograms of a derived type that override a primitive
13903 operation of the type, for both tagged and untagged types. In particular,
13904 the declaration of a primitive operation of a type extension that overrides
13905 an inherited operation must carry an overriding indicator. Another case is
13906 the declaration of a function that overrides a predefined operator (such
13907 as an equality operator).
13908 @end table
13909
13910 @geindex -gnatyp (gcc)
13911
13912
13913 @table @asis
13914
13915 @item @code{-gnatyp}
13916
13917 @emph{Check pragma casing.}
13918
13919 Pragma names must be written in mixed case, that is, the
13920 initial letter and any letter following an underscore must be uppercase.
13921 All other letters must be lowercase. An exception is that SPARK_Mode is
13922 allowed as an alternative for Spark_Mode.
13923 @end table
13924
13925 @geindex -gnatyr (gcc)
13926
13927
13928 @table @asis
13929
13930 @item @code{-gnatyr}
13931
13932 @emph{Check references.}
13933
13934 All identifier references must be cased in the same way as the
13935 corresponding declaration. No specific casing style is imposed on
13936 identifiers. The only requirement is for consistency of references
13937 with declarations.
13938 @end table
13939
13940 @geindex -gnatys (gcc)
13941
13942
13943 @table @asis
13944
13945 @item @code{-gnatys}
13946
13947 @emph{Check separate specs.}
13948
13949 Separate declarations ('specs') are required for subprograms (a
13950 body is not allowed to serve as its own declaration). The only
13951 exception is that parameterless library level procedures are
13952 not required to have a separate declaration. This exception covers
13953 the most frequent form of main program procedures.
13954 @end table
13955
13956 @geindex -gnatyS (gcc)
13957
13958
13959 @table @asis
13960
13961 @item @code{-gnatyS}
13962
13963 @emph{Check no statements after then/else.}
13964
13965 No statements are allowed
13966 on the same line as a @code{then} or @code{else} keyword following the
13967 keyword in an @code{if} statement. @code{or else} and @code{and then} are not
13968 affected, and a special exception allows a pragma to appear after @code{else}.
13969 @end table
13970
13971 @geindex -gnatyt (gcc)
13972
13973
13974 @table @asis
13975
13976 @item @code{-gnatyt}
13977
13978 @emph{Check token spacing.}
13979
13980 The following token spacing rules are enforced:
13981
13982
13983 @itemize *
13984
13985 @item
13986 The keywords @code{abs} and @code{not} must be followed by a space.
13987
13988 @item
13989 The token @code{=>} must be surrounded by spaces.
13990
13991 @item
13992 The token @code{<>} must be preceded by a space or a left parenthesis.
13993
13994 @item
13995 Binary operators other than @code{**} must be surrounded by spaces.
13996 There is no restriction on the layout of the @code{**} binary operator.
13997
13998 @item
13999 Colon must be surrounded by spaces.
14000
14001 @item
14002 Colon-equal (assignment, initialization) must be surrounded by spaces.
14003
14004 @item
14005 Comma must be the first non-blank character on the line, or be
14006 immediately preceded by a non-blank character, and must be followed
14007 by a space.
14008
14009 @item
14010 If the token preceding a left parenthesis ends with a letter or digit, then
14011 a space must separate the two tokens.
14012
14013 @item
14014 If the token following a right parenthesis starts with a letter or digit, then
14015 a space must separate the two tokens.
14016
14017 @item
14018 A right parenthesis must either be the first non-blank character on
14019 a line, or it must be preceded by a non-blank character.
14020
14021 @item
14022 A semicolon must not be preceded by a space, and must not be followed by
14023 a non-blank character.
14024
14025 @item
14026 A unary plus or minus may not be followed by a space.
14027
14028 @item
14029 A vertical bar must be surrounded by spaces.
14030 @end itemize
14031
14032 Exactly one blank (and no other white space) must appear between
14033 a @code{not} token and a following @code{in} token.
14034 @end table
14035
14036 @geindex -gnatyu (gcc)
14037
14038
14039 @table @asis
14040
14041 @item @code{-gnatyu}
14042
14043 @emph{Check unnecessary blank lines.}
14044
14045 Unnecessary blank lines are not allowed. A blank line is considered
14046 unnecessary if it appears at the end of the file, or if more than
14047 one blank line occurs in sequence.
14048 @end table
14049
14050 @geindex -gnatyx (gcc)
14051
14052
14053 @table @asis
14054
14055 @item @code{-gnatyx}
14056
14057 @emph{Check extra parentheses.}
14058
14059 Unnecessary extra level of parentheses (C-style) are not allowed
14060 around conditions in @code{if} statements, @code{while} statements and
14061 @code{exit} statements.
14062 @end table
14063
14064 @geindex -gnatyy (gcc)
14065
14066
14067 @table @asis
14068
14069 @item @code{-gnatyy}
14070
14071 @emph{Set all standard style check options.}
14072
14073 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
14074 options enabled with the exception of @code{-gnatyB}, @code{-gnatyd},
14075 @code{-gnatyI}, @code{-gnatyLnnn}, @code{-gnatyo}, @code{-gnatyO},
14076 @code{-gnatyS}, @code{-gnatyu}, and @code{-gnatyx}.
14077 @end table
14078
14079 @geindex -gnaty- (gcc)
14080
14081
14082 @table @asis
14083
14084 @item @code{-gnaty-}
14085
14086 @emph{Remove style check options.}
14087
14088 This causes any subsequent options in the string to act as canceling the
14089 corresponding style check option. To cancel maximum nesting level control,
14090 use the @code{L} parameter without any integer value after that, because any
14091 digit following @emph{-} in the parameter string of the @code{-gnaty}
14092 option will be treated as canceling the indentation check. The same is true
14093 for the @code{M} parameter. @code{y} and @code{N} parameters are not
14094 allowed after @emph{-}.
14095 @end table
14096
14097 @geindex -gnaty+ (gcc)
14098
14099
14100 @table @asis
14101
14102 @item @code{-gnaty+}
14103
14104 @emph{Enable style check options.}
14105
14106 This causes any subsequent options in the string to enable the corresponding
14107 style check option. That is, it cancels the effect of a previous -,
14108 if any.
14109 @end table
14110
14111 @c end of switch description (leave this comment to ease automatic parsing for
14112
14113 @c GNAT Studio
14114
14115 In the above rules, appearing in column one is always permitted, that is,
14116 counts as meeting either a requirement for a required preceding space,
14117 or as meeting a requirement for no preceding space.
14118
14119 Appearing at the end of a line is also always permitted, that is, counts
14120 as meeting either a requirement for a following space, or as meeting
14121 a requirement for no following space.
14122
14123 If any of these style rules is violated, a message is generated giving
14124 details on the violation. The initial characters of such messages are
14125 always '@cite{(style)}'. Note that these messages are treated as warning
14126 messages, so they normally do not prevent the generation of an object
14127 file. The @code{-gnatwe} switch can be used to treat warning messages,
14128 including style messages, as fatal errors.
14129
14130 The switch @code{-gnaty} on its own (that is not
14131 followed by any letters or digits) is equivalent
14132 to the use of @code{-gnatyy} as described above, that is all
14133 built-in standard style check options are enabled.
14134
14135 The switch @code{-gnatyN} clears any previously set style checks.
14136
14137 @node Run-Time Checks,Using gcc for Syntax Checking,Style Checking,Compiler Switches
14138 @anchor{gnat_ugn/building_executable_programs_with_gnat run-time-checks}@anchor{f9}@anchor{gnat_ugn/building_executable_programs_with_gnat id19}@anchor{104}
14139 @subsection Run-Time Checks
14140
14141
14142 @geindex Division by zero
14143
14144 @geindex Access before elaboration
14145
14146 @geindex Checks
14147 @geindex division by zero
14148
14149 @geindex Checks
14150 @geindex access before elaboration
14151
14152 @geindex Checks
14153 @geindex stack overflow checking
14154
14155 By default, the following checks are suppressed: stack overflow
14156 checks, and checks for access before elaboration on subprogram
14157 calls. All other checks, including overflow checks, range checks and
14158 array bounds checks, are turned on by default. The following @code{gcc}
14159 switches refine this default behavior.
14160
14161 @geindex -gnatp (gcc)
14162
14163
14164 @table @asis
14165
14166 @item @code{-gnatp}
14167
14168 @geindex Suppressing checks
14169
14170 @geindex Checks
14171 @geindex suppressing
14172
14173 This switch causes the unit to be compiled
14174 as though @code{pragma Suppress (All_checks)}
14175 had been present in the source. Validity checks are also eliminated (in
14176 other words @code{-gnatp} also implies @code{-gnatVn}.
14177 Use this switch to improve the performance
14178 of the code at the expense of safety in the presence of invalid data or
14179 program bugs.
14180
14181 Note that when checks are suppressed, the compiler is allowed, but not
14182 required, to omit the checking code. If the run-time cost of the
14183 checking code is zero or near-zero, the compiler will generate it even
14184 if checks are suppressed. In particular, if the compiler can prove
14185 that a certain check will necessarily fail, it will generate code to
14186 do an unconditional 'raise', even if checks are suppressed. The
14187 compiler warns in this case. Another case in which checks may not be
14188 eliminated is when they are embedded in certain run-time routines such
14189 as math library routines.
14190
14191 Of course, run-time checks are omitted whenever the compiler can prove
14192 that they will not fail, whether or not checks are suppressed.
14193
14194 Note that if you suppress a check that would have failed, program
14195 execution is erroneous, which means the behavior is totally
14196 unpredictable. The program might crash, or print wrong answers, or
14197 do anything else. It might even do exactly what you wanted it to do
14198 (and then it might start failing mysteriously next week or next
14199 year). The compiler will generate code based on the assumption that
14200 the condition being checked is true, which can result in erroneous
14201 execution if that assumption is wrong.
14202
14203 The checks subject to suppression include all the checks defined by the Ada
14204 standard, the additional implementation defined checks @code{Alignment_Check},
14205 @code{Duplicated_Tag_Check}, @code{Predicate_Check}, @code{Container_Checks}, @code{Tampering_Check},
14206 and @code{Validity_Check}, as well as any checks introduced using @code{pragma Check_Name}.
14207 Note that @code{Atomic_Synchronization} is not automatically suppressed by use of this option.
14208
14209 If the code depends on certain checks being active, you can use
14210 pragma @code{Unsuppress} either as a configuration pragma or as
14211 a local pragma to make sure that a specified check is performed
14212 even if @code{gnatp} is specified.
14213
14214 The @code{-gnatp} switch has no effect if a subsequent
14215 @code{-gnat-p} switch appears.
14216 @end table
14217
14218 @geindex -gnat-p (gcc)
14219
14220 @geindex Suppressing checks
14221
14222 @geindex Checks
14223 @geindex suppressing
14224
14225 @geindex Suppress
14226
14227
14228 @table @asis
14229
14230 @item @code{-gnat-p}
14231
14232 This switch cancels the effect of a previous @code{gnatp} switch.
14233 @end table
14234
14235 @geindex -gnato?? (gcc)
14236
14237 @geindex Overflow checks
14238
14239 @geindex Overflow mode
14240
14241 @geindex Check
14242 @geindex overflow
14243
14244
14245 @table @asis
14246
14247 @item @code{-gnato??}
14248
14249 This switch controls the mode used for computing intermediate
14250 arithmetic integer operations, and also enables overflow checking.
14251 For a full description of overflow mode and checking control, see
14252 the 'Overflow Check Handling in GNAT' appendix in this
14253 User's Guide.
14254
14255 Overflow checks are always enabled by this switch. The argument
14256 controls the mode, using the codes
14257
14258
14259 @table @asis
14260
14261 @item @emph{1 = STRICT}
14262
14263 In STRICT mode, intermediate operations are always done using the
14264 base type, and overflow checking ensures that the result is within
14265 the base type range.
14266
14267 @item @emph{2 = MINIMIZED}
14268
14269 In MINIMIZED mode, overflows in intermediate operations are avoided
14270 where possible by using a larger integer type for the computation
14271 (typically @code{Long_Long_Integer}). Overflow checking ensures that
14272 the result fits in this larger integer type.
14273
14274 @item @emph{3 = ELIMINATED}
14275
14276 In ELIMINATED mode, overflows in intermediate operations are avoided
14277 by using multi-precision arithmetic. In this case, overflow checking
14278 has no effect on intermediate operations (since overflow is impossible).
14279 @end table
14280
14281 If two digits are present after @code{-gnato} then the first digit
14282 sets the mode for expressions outside assertions, and the second digit
14283 sets the mode for expressions within assertions. Here assertions is used
14284 in the technical sense (which includes for example precondition and
14285 postcondition expressions).
14286
14287 If one digit is present, the corresponding mode is applicable to both
14288 expressions within and outside assertion expressions.
14289
14290 If no digits are present, the default is to enable overflow checks
14291 and set STRICT mode for both kinds of expressions. This is compatible
14292 with the use of @code{-gnato} in previous versions of GNAT.
14293
14294 @geindex Machine_Overflows
14295
14296 Note that the @code{-gnato??} switch does not affect the code generated
14297 for any floating-point operations; it applies only to integer semantics.
14298 For floating-point, GNAT has the @code{Machine_Overflows}
14299 attribute set to @code{False} and the normal mode of operation is to
14300 generate IEEE NaN and infinite values on overflow or invalid operations
14301 (such as dividing 0.0 by 0.0).
14302
14303 The reason that we distinguish overflow checking from other kinds of
14304 range constraint checking is that a failure of an overflow check, unlike
14305 for example the failure of a range check, can result in an incorrect
14306 value, but cannot cause random memory destruction (like an out of range
14307 subscript), or a wild jump (from an out of range case value). Overflow
14308 checking is also quite expensive in time and space, since in general it
14309 requires the use of double length arithmetic.
14310
14311 Note again that the default is @code{-gnato11} (equivalent to @code{-gnato1}),
14312 so overflow checking is performed in STRICT mode by default.
14313 @end table
14314
14315 @geindex -gnatE (gcc)
14316
14317 @geindex Elaboration checks
14318
14319 @geindex Check
14320 @geindex elaboration
14321
14322
14323 @table @asis
14324
14325 @item @code{-gnatE}
14326
14327 Enables dynamic checks for access-before-elaboration
14328 on subprogram calls and generic instantiations.
14329 Note that @code{-gnatE} is not necessary for safety, because in the
14330 default mode, GNAT ensures statically that the checks would not fail.
14331 For full details of the effect and use of this switch,
14332 @ref{1c,,Compiling with gcc}.
14333 @end table
14334
14335 @geindex -fstack-check (gcc)
14336
14337 @geindex Stack Overflow Checking
14338
14339 @geindex Checks
14340 @geindex stack overflow checking
14341
14342
14343 @table @asis
14344
14345 @item @code{-fstack-check}
14346
14347 Activates stack overflow checking. For full details of the effect and use of
14348 this switch see @ref{f4,,Stack Overflow Checking}.
14349 @end table
14350
14351 @geindex Unsuppress
14352
14353 The setting of these switches only controls the default setting of the
14354 checks. You may modify them using either @code{Suppress} (to remove
14355 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
14356 the program source.
14357
14358 @node Using gcc for Syntax Checking,Using gcc for Semantic Checking,Run-Time Checks,Compiler Switches
14359 @anchor{gnat_ugn/building_executable_programs_with_gnat id20}@anchor{105}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-syntax-checking}@anchor{106}
14360 @subsection Using @code{gcc} for Syntax Checking
14361
14362
14363 @geindex -gnats (gcc)
14364
14365
14366 @table @asis
14367
14368 @item @code{-gnats}
14369
14370 The @code{s} stands for 'syntax'.
14371
14372 Run GNAT in syntax checking only mode. For
14373 example, the command
14374
14375 @example
14376 $ gcc -c -gnats x.adb
14377 @end example
14378
14379 compiles file @code{x.adb} in syntax-check-only mode. You can check a
14380 series of files in a single command
14381 , and can use wildcards to specify such a group of files.
14382 Note that you must specify the @code{-c} (compile
14383 only) flag in addition to the @code{-gnats} flag.
14384
14385 You may use other switches in conjunction with @code{-gnats}. In
14386 particular, @code{-gnatl} and @code{-gnatv} are useful to control the
14387 format of any generated error messages.
14388
14389 When the source file is empty or contains only empty lines and/or comments,
14390 the output is a warning:
14391
14392 @example
14393 $ gcc -c -gnats -x ada toto.txt
14394 toto.txt:1:01: warning: empty file, contains no compilation units
14395 $
14396 @end example
14397
14398 Otherwise, the output is simply the error messages, if any. No object file or
14399 ALI file is generated by a syntax-only compilation. Also, no units other
14400 than the one specified are accessed. For example, if a unit @code{X}
14401 @emph{with}s a unit @code{Y}, compiling unit @code{X} in syntax
14402 check only mode does not access the source file containing unit
14403 @code{Y}.
14404
14405 @geindex Multiple units
14406 @geindex syntax checking
14407
14408 Normally, GNAT allows only a single unit in a source file. However, this
14409 restriction does not apply in syntax-check-only mode, and it is possible
14410 to check a file containing multiple compilation units concatenated
14411 together. This is primarily used by the @code{gnatchop} utility
14412 (@ref{36,,Renaming Files with gnatchop}).
14413 @end table
14414
14415 @node Using gcc for Semantic Checking,Compiling Different Versions of Ada,Using gcc for Syntax Checking,Compiler Switches
14416 @anchor{gnat_ugn/building_executable_programs_with_gnat id21}@anchor{107}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-semantic-checking}@anchor{108}
14417 @subsection Using @code{gcc} for Semantic Checking
14418
14419
14420 @geindex -gnatc (gcc)
14421
14422
14423 @table @asis
14424
14425 @item @code{-gnatc}
14426
14427 The @code{c} stands for 'check'.
14428 Causes the compiler to operate in semantic check mode,
14429 with full checking for all illegalities specified in the
14430 Ada Reference Manual, but without generation of any object code
14431 (no object file is generated).
14432
14433 Because dependent files must be accessed, you must follow the GNAT
14434 semantic restrictions on file structuring to operate in this mode:
14435
14436
14437 @itemize *
14438
14439 @item
14440 The needed source files must be accessible
14441 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
14442
14443 @item
14444 Each file must contain only one compilation unit.
14445
14446 @item
14447 The file name and unit name must match (@ref{52,,File Naming Rules}).
14448 @end itemize
14449
14450 The output consists of error messages as appropriate. No object file is
14451 generated. An @code{ALI} file is generated for use in the context of
14452 cross-reference tools, but this file is marked as not being suitable
14453 for binding (since no object file is generated).
14454 The checking corresponds exactly to the notion of
14455 legality in the Ada Reference Manual.
14456
14457 Any unit can be compiled in semantics-checking-only mode, including
14458 units that would not normally be compiled (subunits,
14459 and specifications where a separate body is present).
14460 @end table
14461
14462 @node Compiling Different Versions of Ada,Character Set Control,Using gcc for Semantic Checking,Compiler Switches
14463 @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{109}
14464 @subsection Compiling Different Versions of Ada
14465
14466
14467 The switches described in this section allow you to explicitly specify
14468 the version of the Ada language that your programs are written in.
14469 The default mode is Ada 2012,
14470 but you can also specify Ada 95, Ada 2005 mode, or
14471 indicate Ada 83 compatibility mode.
14472
14473 @geindex Compatibility with Ada 83
14474
14475 @geindex -gnat83 (gcc)
14476
14477 @geindex ACVC
14478 @geindex Ada 83 tests
14479
14480 @geindex Ada 83 mode
14481
14482
14483 @table @asis
14484
14485 @item @code{-gnat83} (Ada 83 Compatibility Mode)
14486
14487 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
14488 specifies that the program is to be compiled in Ada 83 mode. With
14489 @code{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
14490 semantics where this can be done easily.
14491 It is not possible to guarantee this switch does a perfect
14492 job; some subtle tests, such as are
14493 found in earlier ACVC tests (and that have been removed from the ACATS suite
14494 for Ada 95), might not compile correctly.
14495 Nevertheless, this switch may be useful in some circumstances, for example
14496 where, due to contractual reasons, existing code needs to be maintained
14497 using only Ada 83 features.
14498
14499 With few exceptions (most notably the need to use @code{<>} on
14500 unconstrained
14501 @geindex Generic formal parameters
14502 generic formal parameters,
14503 the use of the new Ada 95 / Ada 2005
14504 reserved words, and the use of packages
14505 with optional bodies), it is not necessary to specify the
14506 @code{-gnat83} switch when compiling Ada 83 programs, because, with rare
14507 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
14508 a correct Ada 83 program is usually also a correct program
14509 in these later versions of the language standard. For further information
14510 please refer to the @emph{Compatibility and Porting Guide} chapter in the
14511 @cite{GNAT Reference Manual}.
14512 @end table
14513
14514 @geindex -gnat95 (gcc)
14515
14516 @geindex Ada 95 mode
14517
14518
14519 @table @asis
14520
14521 @item @code{-gnat95} (Ada 95 mode)
14522
14523 This switch directs the compiler to implement the Ada 95 version of the
14524 language.
14525 Since Ada 95 is almost completely upwards
14526 compatible with Ada 83, Ada 83 programs may generally be compiled using
14527 this switch (see the description of the @code{-gnat83} switch for further
14528 information about Ada 83 mode).
14529 If an Ada 2005 program is compiled in Ada 95 mode,
14530 uses of the new Ada 2005 features will cause error
14531 messages or warnings.
14532
14533 This switch also can be used to cancel the effect of a previous
14534 @code{-gnat83}, @code{-gnat05/2005}, or @code{-gnat12/2012}
14535 switch earlier in the command line.
14536 @end table
14537
14538 @geindex -gnat05 (gcc)
14539
14540 @geindex -gnat2005 (gcc)
14541
14542 @geindex Ada 2005 mode
14543
14544
14545 @table @asis
14546
14547 @item @code{-gnat05} or @code{-gnat2005} (Ada 2005 mode)
14548
14549 This switch directs the compiler to implement the Ada 2005 version of the
14550 language, as documented in the official Ada standards document.
14551 Since Ada 2005 is almost completely upwards
14552 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
14553 may generally be compiled using this switch (see the description of the
14554 @code{-gnat83} and @code{-gnat95} switches for further
14555 information).
14556 @end table
14557
14558 @geindex -gnat12 (gcc)
14559
14560 @geindex -gnat2012 (gcc)
14561
14562 @geindex Ada 2012 mode
14563
14564
14565 @table @asis
14566
14567 @item @code{-gnat12} or @code{-gnat2012} (Ada 2012 mode)
14568
14569 This switch directs the compiler to implement the Ada 2012 version of the
14570 language (also the default).
14571 Since Ada 2012 is almost completely upwards
14572 compatible with Ada 2005 (and thus also with Ada 83, and Ada 95),
14573 Ada 83 and Ada 95 programs
14574 may generally be compiled using this switch (see the description of the
14575 @code{-gnat83}, @code{-gnat95}, and @code{-gnat05/2005} switches
14576 for further information).
14577 @end table
14578
14579 @geindex -gnatX (gcc)
14580
14581 @geindex Ada language extensions
14582
14583 @geindex GNAT extensions
14584
14585
14586 @table @asis
14587
14588 @item @code{-gnatX} (Enable GNAT Extensions)
14589
14590 This switch directs the compiler to implement the latest version of the
14591 language (currently Ada 2012) and also to enable certain GNAT implementation
14592 extensions that are not part of any Ada standard. For a full list of these
14593 extensions, see the GNAT reference manual.
14594 @end table
14595
14596 @node Character Set Control,File Naming Control,Compiling Different Versions of Ada,Compiler Switches
14597 @anchor{gnat_ugn/building_executable_programs_with_gnat id23}@anchor{10a}@anchor{gnat_ugn/building_executable_programs_with_gnat character-set-control}@anchor{48}
14598 @subsection Character Set Control
14599
14600
14601 @geindex -gnati (gcc)
14602
14603
14604 @table @asis
14605
14606 @item @code{-gnati@emph{c}}
14607
14608 Normally GNAT recognizes the Latin-1 character set in source program
14609 identifiers, as described in the Ada Reference Manual.
14610 This switch causes
14611 GNAT to recognize alternate character sets in identifiers. @code{c} is a
14612 single character indicating the character set, as follows:
14613
14614
14615 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14616 @item
14617
14618 @emph{1}
14619
14620 @tab
14621
14622 ISO 8859-1 (Latin-1) identifiers
14623
14624 @item
14625
14626 @emph{2}
14627
14628 @tab
14629
14630 ISO 8859-2 (Latin-2) letters allowed in identifiers
14631
14632 @item
14633
14634 @emph{3}
14635
14636 @tab
14637
14638 ISO 8859-3 (Latin-3) letters allowed in identifiers
14639
14640 @item
14641
14642 @emph{4}
14643
14644 @tab
14645
14646 ISO 8859-4 (Latin-4) letters allowed in identifiers
14647
14648 @item
14649
14650 @emph{5}
14651
14652 @tab
14653
14654 ISO 8859-5 (Cyrillic) letters allowed in identifiers
14655
14656 @item
14657
14658 @emph{9}
14659
14660 @tab
14661
14662 ISO 8859-15 (Latin-9) letters allowed in identifiers
14663
14664 @item
14665
14666 @emph{p}
14667
14668 @tab
14669
14670 IBM PC letters (code page 437) allowed in identifiers
14671
14672 @item
14673
14674 @emph{8}
14675
14676 @tab
14677
14678 IBM PC letters (code page 850) allowed in identifiers
14679
14680 @item
14681
14682 @emph{f}
14683
14684 @tab
14685
14686 Full upper-half codes allowed in identifiers
14687
14688 @item
14689
14690 @emph{n}
14691
14692 @tab
14693
14694 No upper-half codes allowed in identifiers
14695
14696 @item
14697
14698 @emph{w}
14699
14700 @tab
14701
14702 Wide-character codes (that is, codes greater than 255)
14703 allowed in identifiers
14704
14705 @end multitable
14706
14707
14708 See @ref{3e,,Foreign Language Representation} for full details on the
14709 implementation of these character sets.
14710 @end table
14711
14712 @geindex -gnatW (gcc)
14713
14714
14715 @table @asis
14716
14717 @item @code{-gnatW@emph{e}}
14718
14719 Specify the method of encoding for wide characters.
14720 @code{e} is one of the following:
14721
14722
14723 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14724 @item
14725
14726 @emph{h}
14727
14728 @tab
14729
14730 Hex encoding (brackets coding also recognized)
14731
14732 @item
14733
14734 @emph{u}
14735
14736 @tab
14737
14738 Upper half encoding (brackets encoding also recognized)
14739
14740 @item
14741
14742 @emph{s}
14743
14744 @tab
14745
14746 Shift/JIS encoding (brackets encoding also recognized)
14747
14748 @item
14749
14750 @emph{e}
14751
14752 @tab
14753
14754 EUC encoding (brackets encoding also recognized)
14755
14756 @item
14757
14758 @emph{8}
14759
14760 @tab
14761
14762 UTF-8 encoding (brackets encoding also recognized)
14763
14764 @item
14765
14766 @emph{b}
14767
14768 @tab
14769
14770 Brackets encoding only (default value)
14771
14772 @end multitable
14773
14774
14775 For full details on these encoding
14776 methods see @ref{4e,,Wide_Character Encodings}.
14777 Note that brackets coding is always accepted, even if one of the other
14778 options is specified, so for example @code{-gnatW8} specifies that both
14779 brackets and UTF-8 encodings will be recognized. The units that are
14780 with'ed directly or indirectly will be scanned using the specified
14781 representation scheme, and so if one of the non-brackets scheme is
14782 used, it must be used consistently throughout the program. However,
14783 since brackets encoding is always recognized, it may be conveniently
14784 used in standard libraries, allowing these libraries to be used with
14785 any of the available coding schemes.
14786
14787 Note that brackets encoding only applies to program text. Within comments,
14788 brackets are considered to be normal graphic characters, and bracket sequences
14789 are never recognized as wide characters.
14790
14791 If no @code{-gnatW?} parameter is present, then the default
14792 representation is normally Brackets encoding only. However, if the
14793 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
14794 byte order mark or BOM for UTF-8), then these three characters are
14795 skipped and the default representation for the file is set to UTF-8.
14796
14797 Note that the wide character representation that is specified (explicitly
14798 or by default) for the main program also acts as the default encoding used
14799 for Wide_Text_IO files if not specifically overridden by a WCEM form
14800 parameter.
14801 @end table
14802
14803 When no @code{-gnatW?} is specified, then characters (other than wide
14804 characters represented using brackets notation) are treated as 8-bit
14805 Latin-1 codes. The codes recognized are the Latin-1 graphic characters,
14806 and ASCII format effectors (CR, LF, HT, VT). Other lower half control
14807 characters in the range 16#00#..16#1F# are not accepted in program text
14808 or in comments. Upper half control characters (16#80#..16#9F#) are rejected
14809 in program text, but allowed and ignored in comments. Note in particular
14810 that the Next Line (NEL) character whose encoding is 16#85# is not recognized
14811 as an end of line in this default mode. If your source program contains
14812 instances of the NEL character used as a line terminator,
14813 you must use UTF-8 encoding for the whole
14814 source program. In default mode, all lines must be ended by a standard
14815 end of line sequence (CR, CR/LF, or LF).
14816
14817 Note that the convention of simply accepting all upper half characters in
14818 comments means that programs that use standard ASCII for program text, but
14819 UTF-8 encoding for comments are accepted in default mode, providing that the
14820 comments are ended by an appropriate (CR, or CR/LF, or LF) line terminator.
14821 This is a common mode for many programs with foreign language comments.
14822
14823 @node File Naming Control,Subprogram Inlining Control,Character Set Control,Compiler Switches
14824 @anchor{gnat_ugn/building_executable_programs_with_gnat file-naming-control}@anchor{10b}@anchor{gnat_ugn/building_executable_programs_with_gnat id24}@anchor{10c}
14825 @subsection File Naming Control
14826
14827
14828 @geindex -gnatk (gcc)
14829
14830
14831 @table @asis
14832
14833 @item @code{-gnatk@emph{n}}
14834
14835 Activates file name 'krunching'. @code{n}, a decimal integer in the range
14836 1-999, indicates the maximum allowable length of a file name (not
14837 including the @code{.ads} or @code{.adb} extension). The default is not
14838 to enable file name krunching.
14839
14840 For the source file naming rules, @ref{52,,File Naming Rules}.
14841 @end table
14842
14843 @node Subprogram Inlining Control,Auxiliary Output Control,File Naming Control,Compiler Switches
14844 @anchor{gnat_ugn/building_executable_programs_with_gnat subprogram-inlining-control}@anchor{10d}@anchor{gnat_ugn/building_executable_programs_with_gnat id25}@anchor{10e}
14845 @subsection Subprogram Inlining Control
14846
14847
14848 @geindex -gnatn (gcc)
14849
14850
14851 @table @asis
14852
14853 @item @code{-gnatn[12]}
14854
14855 The @code{n} here is intended to suggest the first syllable of the word 'inline'.
14856 GNAT recognizes and processes @code{Inline} pragmas. However, for inlining to
14857 actually occur, optimization must be enabled and, by default, inlining of
14858 subprograms across units is not performed. If you want to additionally
14859 enable inlining of subprograms specified by pragma @code{Inline} across units,
14860 you must also specify this switch.
14861
14862 In the absence of this switch, GNAT does not attempt inlining across units
14863 and does not access the bodies of subprograms for which @code{pragma Inline} is
14864 specified if they are not in the current unit.
14865
14866 You can optionally specify the inlining level: 1 for moderate inlining across
14867 units, which is a good compromise between compilation times and performances
14868 at run time, or 2 for full inlining across units, which may bring about
14869 longer compilation times. If no inlining level is specified, the compiler will
14870 pick it based on the optimization level: 1 for @code{-O1}, @code{-O2} or
14871 @code{-Os} and 2 for @code{-O3}.
14872
14873 If you specify this switch the compiler will access these bodies,
14874 creating an extra source dependency for the resulting object file, and
14875 where possible, the call will be inlined.
14876 For further details on when inlining is possible
14877 see @ref{10f,,Inlining of Subprograms}.
14878 @end table
14879
14880 @geindex -gnatN (gcc)
14881
14882
14883 @table @asis
14884
14885 @item @code{-gnatN}
14886
14887 This switch activates front-end inlining which also
14888 generates additional dependencies.
14889
14890 When using a gcc-based back end (in practice this means using any version
14891 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
14892 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
14893 Historically front end inlining was more extensive than the gcc back end
14894 inlining, but that is no longer the case.
14895 @end table
14896
14897 @node Auxiliary Output Control,Debugging Control,Subprogram Inlining Control,Compiler Switches
14898 @anchor{gnat_ugn/building_executable_programs_with_gnat auxiliary-output-control}@anchor{110}@anchor{gnat_ugn/building_executable_programs_with_gnat id26}@anchor{111}
14899 @subsection Auxiliary Output Control
14900
14901
14902 @geindex -gnatt (gcc)
14903
14904 @geindex Writing internal trees
14905
14906 @geindex Internal trees
14907 @geindex writing to file
14908
14909
14910 @table @asis
14911
14912 @item @code{-gnatt}
14913
14914 Causes GNAT to write the internal tree for a unit to a file (with the
14915 extension @code{.adt}.
14916 This not normally required, but is used by separate analysis tools.
14917 Typically
14918 these tools do the necessary compilations automatically, so you should
14919 not have to specify this switch in normal operation.
14920 Note that the combination of switches @code{-gnatct}
14921 generates a tree in the form required by ASIS applications.
14922 @end table
14923
14924 @geindex -gnatu (gcc)
14925
14926
14927 @table @asis
14928
14929 @item @code{-gnatu}
14930
14931 Print a list of units required by this compilation on @code{stdout}.
14932 The listing includes all units on which the unit being compiled depends
14933 either directly or indirectly.
14934 @end table
14935
14936 @geindex -pass-exit-codes (gcc)
14937
14938
14939 @table @asis
14940
14941 @item @code{-pass-exit-codes}
14942
14943 If this switch is not used, the exit code returned by @code{gcc} when
14944 compiling multiple files indicates whether all source files have
14945 been successfully used to generate object files or not.
14946
14947 When @code{-pass-exit-codes} is used, @code{gcc} exits with an extended
14948 exit status and allows an integrated development environment to better
14949 react to a compilation failure. Those exit status are:
14950
14951
14952 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14953 @item
14954
14955 @emph{5}
14956
14957 @tab
14958
14959 There was an error in at least one source file.
14960
14961 @item
14962
14963 @emph{3}
14964
14965 @tab
14966
14967 At least one source file did not generate an object file.
14968
14969 @item
14970
14971 @emph{2}
14972
14973 @tab
14974
14975 The compiler died unexpectedly (internal error for example).
14976
14977 @item
14978
14979 @emph{0}
14980
14981 @tab
14982
14983 An object file has been generated for every source file.
14984
14985 @end multitable
14986
14987 @end table
14988
14989 @node Debugging Control,Exception Handling Control,Auxiliary Output Control,Compiler Switches
14990 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-control}@anchor{112}@anchor{gnat_ugn/building_executable_programs_with_gnat id27}@anchor{113}
14991 @subsection Debugging Control
14992
14993
14994 @quotation
14995
14996 @geindex Debugging options
14997 @end quotation
14998
14999 @geindex -gnatd (gcc)
15000
15001
15002 @table @asis
15003
15004 @item @code{-gnatd@emph{x}}
15005
15006 Activate internal debugging switches. @code{x} is a letter or digit, or
15007 string of letters or digits, which specifies the type of debugging
15008 outputs desired. Normally these are used only for internal development
15009 or system debugging purposes. You can find full documentation for these
15010 switches in the body of the @code{Debug} unit in the compiler source
15011 file @code{debug.adb}.
15012 @end table
15013
15014 @geindex -gnatG (gcc)
15015
15016
15017 @table @asis
15018
15019 @item @code{-gnatG[=@emph{nn}]}
15020
15021 This switch causes the compiler to generate auxiliary output containing
15022 a pseudo-source listing of the generated expanded code. Like most Ada
15023 compilers, GNAT works by first transforming the high level Ada code into
15024 lower level constructs. For example, tasking operations are transformed
15025 into calls to the tasking run-time routines. A unique capability of GNAT
15026 is to list this expanded code in a form very close to normal Ada source.
15027 This is very useful in understanding the implications of various Ada
15028 usage on the efficiency of the generated code. There are many cases in
15029 Ada (e.g., the use of controlled types), where simple Ada statements can
15030 generate a lot of run-time code. By using @code{-gnatG} you can identify
15031 these cases, and consider whether it may be desirable to modify the coding
15032 approach to improve efficiency.
15033
15034 The optional parameter @code{nn} if present after -gnatG specifies an
15035 alternative maximum line length that overrides the normal default of 72.
15036 This value is in the range 40-999999, values less than 40 being silently
15037 reset to 40. The equal sign is optional.
15038
15039 The format of the output is very similar to standard Ada source, and is
15040 easily understood by an Ada programmer. The following special syntactic
15041 additions correspond to low level features used in the generated code that
15042 do not have any exact analogies in pure Ada source form. The following
15043 is a partial list of these special constructions. See the spec
15044 of package @code{Sprint} in file @code{sprint.ads} for a full list.
15045
15046 @geindex -gnatL (gcc)
15047
15048 If the switch @code{-gnatL} is used in conjunction with
15049 @code{-gnatG}, then the original source lines are interspersed
15050 in the expanded source (as comment lines with the original line number).
15051
15052
15053 @table @asis
15054
15055 @item @code{new @emph{xxx} [storage_pool = @emph{yyy}]}
15056
15057 Shows the storage pool being used for an allocator.
15058
15059 @item @code{at end @emph{procedure-name};}
15060
15061 Shows the finalization (cleanup) procedure for a scope.
15062
15063 @item @code{(if @emph{expr} then @emph{expr} else @emph{expr})}
15064
15065 Conditional expression equivalent to the @code{x?y:z} construction in C.
15066
15067 @item @code{@emph{target}^(@emph{source})}
15068
15069 A conversion with floating-point truncation instead of rounding.
15070
15071 @item @code{@emph{target}?(@emph{source})}
15072
15073 A conversion that bypasses normal Ada semantic checking. In particular
15074 enumeration types and fixed-point types are treated simply as integers.
15075
15076 @item @code{@emph{target}?^(@emph{source})}
15077
15078 Combines the above two cases.
15079 @end table
15080
15081 @code{@emph{x} #/ @emph{y}}
15082
15083 @code{@emph{x} #mod @emph{y}}
15084
15085 @code{@emph{x} # @emph{y}}
15086
15087
15088 @table @asis
15089
15090 @item @code{@emph{x} #rem @emph{y}}
15091
15092 A division or multiplication of fixed-point values which are treated as
15093 integers without any kind of scaling.
15094
15095 @item @code{free @emph{expr} [storage_pool = @emph{xxx}]}
15096
15097 Shows the storage pool associated with a @code{free} statement.
15098
15099 @item @code{[subtype or type declaration]}
15100
15101 Used to list an equivalent declaration for an internally generated
15102 type that is referenced elsewhere in the listing.
15103
15104 @item @code{freeze @emph{type-name} [@emph{actions}]}
15105
15106 Shows the point at which @code{type-name} is frozen, with possible
15107 associated actions to be performed at the freeze point.
15108
15109 @item @code{reference @emph{itype}}
15110
15111 Reference (and hence definition) to internal type @code{itype}.
15112
15113 @item @code{@emph{function-name}! (@emph{arg}, @emph{arg}, @emph{arg})}
15114
15115 Intrinsic function call.
15116
15117 @item @code{@emph{label-name} : label}
15118
15119 Declaration of label @code{labelname}.
15120
15121 @item @code{#$ @emph{subprogram-name}}
15122
15123 An implicit call to a run-time support routine
15124 (to meet the requirement of H.3.1(9) in a
15125 convenient manner).
15126
15127 @item @code{@emph{expr} && @emph{expr} && @emph{expr} ... && @emph{expr}}
15128
15129 A multiple concatenation (same effect as @code{expr} & @code{expr} &
15130 @code{expr}, but handled more efficiently).
15131
15132 @item @code{[constraint_error]}
15133
15134 Raise the @code{Constraint_Error} exception.
15135
15136 @item @code{@emph{expression}'reference}
15137
15138 A pointer to the result of evaluating @{expression@}.
15139
15140 @item @code{@emph{target-type}!(@emph{source-expression})}
15141
15142 An unchecked conversion of @code{source-expression} to @code{target-type}.
15143
15144 @item @code{[@emph{numerator}/@emph{denominator}]}
15145
15146 Used to represent internal real literals (that) have no exact
15147 representation in base 2-16 (for example, the result of compile time
15148 evaluation of the expression 1.0/27.0).
15149 @end table
15150 @end table
15151
15152 @geindex -gnatD (gcc)
15153
15154
15155 @table @asis
15156
15157 @item @code{-gnatD[=nn]}
15158
15159 When used in conjunction with @code{-gnatG}, this switch causes
15160 the expanded source, as described above for
15161 @code{-gnatG} to be written to files with names
15162 @code{xxx.dg}, where @code{xxx} is the normal file name,
15163 instead of to the standard output file. For
15164 example, if the source file name is @code{hello.adb}, then a file
15165 @code{hello.adb.dg} will be written. The debugging
15166 information generated by the @code{gcc} @code{-g} switch
15167 will refer to the generated @code{xxx.dg} file. This allows
15168 you to do source level debugging using the generated code which is
15169 sometimes useful for complex code, for example to find out exactly
15170 which part of a complex construction raised an exception. This switch
15171 also suppresses generation of cross-reference information (see
15172 @code{-gnatx}) since otherwise the cross-reference information
15173 would refer to the @code{.dg} file, which would cause
15174 confusion since this is not the original source file.
15175
15176 Note that @code{-gnatD} actually implies @code{-gnatG}
15177 automatically, so it is not necessary to give both options.
15178 In other words @code{-gnatD} is equivalent to @code{-gnatDG}).
15179
15180 @geindex -gnatL (gcc)
15181
15182 If the switch @code{-gnatL} is used in conjunction with
15183 @code{-gnatDG}, then the original source lines are interspersed
15184 in the expanded source (as comment lines with the original line number).
15185
15186 The optional parameter @code{nn} if present after -gnatD specifies an
15187 alternative maximum line length that overrides the normal default of 72.
15188 This value is in the range 40-999999, values less than 40 being silently
15189 reset to 40. The equal sign is optional.
15190 @end table
15191
15192 @geindex -gnatr (gcc)
15193
15194 @geindex pragma Restrictions
15195
15196
15197 @table @asis
15198
15199 @item @code{-gnatr}
15200
15201 This switch causes pragma Restrictions to be treated as Restriction_Warnings
15202 so that violation of restrictions causes warnings rather than illegalities.
15203 This is useful during the development process when new restrictions are added
15204 or investigated. The switch also causes pragma Profile to be treated as
15205 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
15206 restriction warnings rather than restrictions.
15207 @end table
15208
15209 @geindex -gnatR (gcc)
15210
15211
15212 @table @asis
15213
15214 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
15215
15216 This switch controls output from the compiler of a listing showing
15217 representation information for declared types, objects and subprograms.
15218 For @code{-gnatR0}, no information is output (equivalent to omitting
15219 the @code{-gnatR} switch). For @code{-gnatR1} (which is the default,
15220 so @code{-gnatR} with no parameter has the same effect), size and
15221 alignment information is listed for declared array and record types.
15222
15223 For @code{-gnatR2}, size and alignment information is listed for all
15224 declared types and objects. The @code{Linker_Section} is also listed for any
15225 entity for which the @code{Linker_Section} is set explicitly or implicitly (the
15226 latter case occurs for objects of a type for which a @code{Linker_Section}
15227 is set).
15228
15229 For @code{-gnatR3}, symbolic expressions for values that are computed
15230 at run time for records are included. These symbolic expressions have
15231 a mostly obvious format with #n being used to represent the value of the
15232 n'th discriminant. See source files @code{repinfo.ads/adb} in the
15233 GNAT sources for full details on the format of @code{-gnatR3} output.
15234
15235 For @code{-gnatR4}, information for relevant compiler-generated types
15236 is also listed, i.e. when they are structurally part of other declared
15237 types and objects.
15238
15239 If the switch is followed by an @code{e} (e.g. @code{-gnatR2e}), then
15240 extended representation information for record sub-components of records
15241 is included.
15242
15243 If the switch is followed by an @code{m} (e.g. @code{-gnatRm}), then
15244 subprogram conventions and parameter passing mechanisms for all the
15245 subprograms are included.
15246
15247 If the switch is followed by a @code{j} (e.g., @code{-gnatRj}), then
15248 the output is in the JSON data interchange format specified by the
15249 ECMA-404 standard. The semantic description of this JSON output is
15250 available in the specification of the Repinfo unit present in the
15251 compiler sources.
15252
15253 If the switch is followed by an @code{s} (e.g., @code{-gnatR3s}), then
15254 the output is to a file with the name @code{file.rep} where @code{file} is
15255 the name of the corresponding source file, except if @code{j} is also
15256 specified, in which case the file name is @code{file.json}.
15257
15258 Note that it is possible for record components to have zero size. In
15259 this case, the component clause uses an obvious extension of permitted
15260 Ada syntax, for example @code{at 0 range 0 .. -1}.
15261 @end table
15262
15263 @geindex -gnatS (gcc)
15264
15265
15266 @table @asis
15267
15268 @item @code{-gnatS}
15269
15270 The use of the switch @code{-gnatS} for an
15271 Ada compilation will cause the compiler to output a
15272 representation of package Standard in a form very
15273 close to standard Ada. It is not quite possible to
15274 do this entirely in standard Ada (since new
15275 numeric base types cannot be created in standard
15276 Ada), but the output is easily
15277 readable to any Ada programmer, and is useful to
15278 determine the characteristics of target dependent
15279 types in package Standard.
15280 @end table
15281
15282 @geindex -gnatx (gcc)
15283
15284
15285 @table @asis
15286
15287 @item @code{-gnatx}
15288
15289 Normally the compiler generates full cross-referencing information in
15290 the @code{ALI} file. This information is used by a number of tools,
15291 including @code{gnatfind} and @code{gnatxref}. The @code{-gnatx} switch
15292 suppresses this information. This saves some space and may slightly
15293 speed up compilation, but means that these tools cannot be used.
15294 @end table
15295
15296 @geindex -fgnat-encodings (gcc)
15297
15298
15299 @table @asis
15300
15301 @item @code{-fgnat-encodings=[all|gdb|minimal]}
15302
15303 This switch controls the balance between GNAT encodings and standard DWARF
15304 emitted in the debug information.
15305
15306 Historically, old debug formats like stabs were not powerful enough to
15307 express some Ada types (for instance, variant records or fixed-point types).
15308 To work around this, GNAT introduced proprietary encodings that embed the
15309 missing information ("GNAT encodings").
15310
15311 Recent versions of the DWARF debug information format are now able to
15312 correctly describe most of these Ada constructs ("standard DWARF"). As
15313 third-party tools started to use this format, GNAT has been enhanced to
15314 generate it. However, most tools (including GDB) are still relying on GNAT
15315 encodings.
15316
15317 To support all tools, GNAT needs to be versatile about the balance between
15318 generation of GNAT encodings and standard DWARF. This is what
15319 @code{-fgnat-encodings} is about.
15320
15321
15322 @itemize *
15323
15324 @item
15325 @code{=all}: Emit all GNAT encodings, and then emit as much standard DWARF as
15326 possible so it does not conflict with GNAT encodings.
15327
15328 @item
15329 @code{=gdb}: Emit as much standard DWARF as possible as long as the current
15330 GDB handles it. Emit GNAT encodings for the rest.
15331
15332 @item
15333 @code{=minimal}: Emit as much standard DWARF as possible and emit GNAT
15334 encodings for the rest.
15335 @end itemize
15336 @end table
15337
15338 @node Exception Handling Control,Units to Sources Mapping Files,Debugging Control,Compiler Switches
15339 @anchor{gnat_ugn/building_executable_programs_with_gnat id28}@anchor{114}@anchor{gnat_ugn/building_executable_programs_with_gnat exception-handling-control}@anchor{115}
15340 @subsection Exception Handling Control
15341
15342
15343 GNAT uses two methods for handling exceptions at run time. The
15344 @code{setjmp/longjmp} method saves the context when entering
15345 a frame with an exception handler. Then when an exception is
15346 raised, the context can be restored immediately, without the
15347 need for tracing stack frames. This method provides very fast
15348 exception propagation, but introduces significant overhead for
15349 the use of exception handlers, even if no exception is raised.
15350
15351 The other approach is called 'zero cost' exception handling.
15352 With this method, the compiler builds static tables to describe
15353 the exception ranges. No dynamic code is required when entering
15354 a frame containing an exception handler. When an exception is
15355 raised, the tables are used to control a back trace of the
15356 subprogram invocation stack to locate the required exception
15357 handler. This method has considerably poorer performance for
15358 the propagation of exceptions, but there is no overhead for
15359 exception handlers if no exception is raised. Note that in this
15360 mode and in the context of mixed Ada and C/C++ programming,
15361 to propagate an exception through a C/C++ code, the C/C++ code
15362 must be compiled with the @code{-funwind-tables} GCC's
15363 option.
15364
15365 The following switches may be used to control which of the
15366 two exception handling methods is used.
15367
15368 @geindex --RTS=sjlj (gnatmake)
15369
15370
15371 @table @asis
15372
15373 @item @code{--RTS=sjlj}
15374
15375 This switch causes the setjmp/longjmp run-time (when available) to be used
15376 for exception handling. If the default
15377 mechanism for the target is zero cost exceptions, then
15378 this switch can be used to modify this default, and must be
15379 used for all units in the partition.
15380 This option is rarely used. One case in which it may be
15381 advantageous is if you have an application where exception
15382 raising is common and the overall performance of the
15383 application is improved by favoring exception propagation.
15384 @end table
15385
15386 @geindex --RTS=zcx (gnatmake)
15387
15388 @geindex Zero Cost Exceptions
15389
15390
15391 @table @asis
15392
15393 @item @code{--RTS=zcx}
15394
15395 This switch causes the zero cost approach to be used
15396 for exception handling. If this is the default mechanism for the
15397 target (see below), then this switch is unneeded. If the default
15398 mechanism for the target is setjmp/longjmp exceptions, then
15399 this switch can be used to modify this default, and must be
15400 used for all units in the partition.
15401 This option can only be used if the zero cost approach
15402 is available for the target in use, otherwise it will generate an error.
15403 @end table
15404
15405 The same option @code{--RTS} must be used both for @code{gcc}
15406 and @code{gnatbind}. Passing this option to @code{gnatmake}
15407 (@ref{dc,,Switches for gnatmake}) will ensure the required consistency
15408 through the compilation and binding steps.
15409
15410 @node Units to Sources Mapping Files,Code Generation Control,Exception Handling Control,Compiler Switches
15411 @anchor{gnat_ugn/building_executable_programs_with_gnat id29}@anchor{116}@anchor{gnat_ugn/building_executable_programs_with_gnat units-to-sources-mapping-files}@anchor{f7}
15412 @subsection Units to Sources Mapping Files
15413
15414
15415 @geindex -gnatem (gcc)
15416
15417
15418 @table @asis
15419
15420 @item @code{-gnatem=@emph{path}}
15421
15422 A mapping file is a way to communicate to the compiler two mappings:
15423 from unit names to file names (without any directory information) and from
15424 file names to path names (with full directory information). These mappings
15425 are used by the compiler to short-circuit the path search.
15426
15427 The use of mapping files is not required for correct operation of the
15428 compiler, but mapping files can improve efficiency, particularly when
15429 sources are read over a slow network connection. In normal operation,
15430 you need not be concerned with the format or use of mapping files,
15431 and the @code{-gnatem} switch is not a switch that you would use
15432 explicitly. It is intended primarily for use by automatic tools such as
15433 @code{gnatmake} running under the project file facility. The
15434 description here of the format of mapping files is provided
15435 for completeness and for possible use by other tools.
15436
15437 A mapping file is a sequence of sets of three lines. In each set, the
15438 first line is the unit name, in lower case, with @code{%s} appended
15439 for specs and @code{%b} appended for bodies; the second line is the
15440 file name; and the third line is the path name.
15441
15442 Example:
15443
15444 @example
15445 main%b
15446 main.2.ada
15447 /gnat/project1/sources/main.2.ada
15448 @end example
15449
15450 When the switch @code{-gnatem} is specified, the compiler will
15451 create in memory the two mappings from the specified file. If there is
15452 any problem (nonexistent file, truncated file or duplicate entries),
15453 no mapping will be created.
15454
15455 Several @code{-gnatem} switches may be specified; however, only the
15456 last one on the command line will be taken into account.
15457
15458 When using a project file, @code{gnatmake} creates a temporary
15459 mapping file and communicates it to the compiler using this switch.
15460 @end table
15461
15462 @node Code Generation Control,,Units to Sources Mapping Files,Compiler Switches
15463 @anchor{gnat_ugn/building_executable_programs_with_gnat code-generation-control}@anchor{117}@anchor{gnat_ugn/building_executable_programs_with_gnat id30}@anchor{118}
15464 @subsection Code Generation Control
15465
15466
15467 The GCC technology provides a wide range of target dependent
15468 @code{-m} switches for controlling
15469 details of code generation with respect to different versions of
15470 architectures. This includes variations in instruction sets (e.g.,
15471 different members of the power pc family), and different requirements
15472 for optimal arrangement of instructions (e.g., different members of
15473 the x86 family). The list of available @code{-m} switches may be
15474 found in the GCC documentation.
15475
15476 Use of these @code{-m} switches may in some cases result in improved
15477 code performance.
15478
15479 The GNAT technology is tested and qualified without any
15480 @code{-m} switches,
15481 so generally the most reliable approach is to avoid the use of these
15482 switches. However, we generally expect most of these switches to work
15483 successfully with GNAT, and many customers have reported successful
15484 use of these options.
15485
15486 Our general advice is to avoid the use of @code{-m} switches unless
15487 special needs lead to requirements in this area. In particular,
15488 there is no point in using @code{-m} switches to improve performance
15489 unless you actually see a performance improvement.
15490
15491 @node Linker Switches,Binding with gnatbind,Compiler Switches,Building Executable Programs with GNAT
15492 @anchor{gnat_ugn/building_executable_programs_with_gnat linker-switches}@anchor{119}@anchor{gnat_ugn/building_executable_programs_with_gnat id31}@anchor{11a}
15493 @section Linker Switches
15494
15495
15496 Linker switches can be specified after @code{-largs} builder switch.
15497
15498 @geindex -fuse-ld=name
15499
15500
15501 @table @asis
15502
15503 @item @code{-fuse-ld=@emph{name}}
15504
15505 Linker to be used. The default is @code{bfd} for @code{ld.bfd},
15506 the alternative being @code{gold} for @code{ld.gold}. The later is
15507 a more recent and faster linker, but only available on GNU/Linux
15508 platforms.
15509 @end table
15510
15511 @node Binding with gnatbind,Linking with gnatlink,Linker Switches,Building Executable Programs with GNAT
15512 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-gnatbind}@anchor{1d}@anchor{gnat_ugn/building_executable_programs_with_gnat id32}@anchor{11b}
15513 @section Binding with @code{gnatbind}
15514
15515
15516 @geindex gnatbind
15517
15518 This chapter describes the GNAT binder, @code{gnatbind}, which is used
15519 to bind compiled GNAT objects.
15520
15521 The @code{gnatbind} program performs four separate functions:
15522
15523
15524 @itemize *
15525
15526 @item
15527 Checks that a program is consistent, in accordance with the rules in
15528 Chapter 10 of the Ada Reference Manual. In particular, error
15529 messages are generated if a program uses inconsistent versions of a
15530 given unit.
15531
15532 @item
15533 Checks that an acceptable order of elaboration exists for the program
15534 and issues an error message if it cannot find an order of elaboration
15535 that satisfies the rules in Chapter 10 of the Ada Language Manual.
15536
15537 @item
15538 Generates a main program incorporating the given elaboration order.
15539 This program is a small Ada package (body and spec) that
15540 must be subsequently compiled
15541 using the GNAT compiler. The necessary compilation step is usually
15542 performed automatically by @code{gnatlink}. The two most important
15543 functions of this program
15544 are to call the elaboration routines of units in an appropriate order
15545 and to call the main program.
15546
15547 @item
15548 Determines the set of object files required by the given main program.
15549 This information is output in the forms of comments in the generated program,
15550 to be read by the @code{gnatlink} utility used to link the Ada application.
15551 @end itemize
15552
15553 @menu
15554 * Running gnatbind::
15555 * Switches for gnatbind::
15556 * Command-Line Access::
15557 * Search Paths for gnatbind::
15558 * Examples of gnatbind Usage::
15559
15560 @end menu
15561
15562 @node Running gnatbind,Switches for gnatbind,,Binding with gnatbind
15563 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatbind}@anchor{11c}@anchor{gnat_ugn/building_executable_programs_with_gnat id33}@anchor{11d}
15564 @subsection Running @code{gnatbind}
15565
15566
15567 The form of the @code{gnatbind} command is
15568
15569 @example
15570 $ gnatbind [ switches ] mainprog[.ali] [ switches ]
15571 @end example
15572
15573 where @code{mainprog.adb} is the Ada file containing the main program
15574 unit body. @code{gnatbind} constructs an Ada
15575 package in two files whose names are
15576 @code{b~mainprog.ads}, and @code{b~mainprog.adb}.
15577 For example, if given the
15578 parameter @code{hello.ali}, for a main program contained in file
15579 @code{hello.adb}, the binder output files would be @code{b~hello.ads}
15580 and @code{b~hello.adb}.
15581
15582 When doing consistency checking, the binder takes into consideration
15583 any source files it can locate. For example, if the binder determines
15584 that the given main program requires the package @code{Pack}, whose
15585 @code{.ALI}
15586 file is @code{pack.ali} and whose corresponding source spec file is
15587 @code{pack.ads}, it attempts to locate the source file @code{pack.ads}
15588 (using the same search path conventions as previously described for the
15589 @code{gcc} command). If it can locate this source file, it checks that
15590 the time stamps
15591 or source checksums of the source and its references to in @code{ALI} files
15592 match. In other words, any @code{ALI} files that mentions this spec must have
15593 resulted from compiling this version of the source file (or in the case
15594 where the source checksums match, a version close enough that the
15595 difference does not matter).
15596
15597 @geindex Source files
15598 @geindex use by binder
15599
15600 The effect of this consistency checking, which includes source files, is
15601 that the binder ensures that the program is consistent with the latest
15602 version of the source files that can be located at bind time. Editing a
15603 source file without compiling files that depend on the source file cause
15604 error messages to be generated by the binder.
15605
15606 For example, suppose you have a main program @code{hello.adb} and a
15607 package @code{P}, from file @code{p.ads} and you perform the following
15608 steps:
15609
15610
15611 @itemize *
15612
15613 @item
15614 Enter @code{gcc -c hello.adb} to compile the main program.
15615
15616 @item
15617 Enter @code{gcc -c p.ads} to compile package @code{P}.
15618
15619 @item
15620 Edit file @code{p.ads}.
15621
15622 @item
15623 Enter @code{gnatbind hello}.
15624 @end itemize
15625
15626 At this point, the file @code{p.ali} contains an out-of-date time stamp
15627 because the file @code{p.ads} has been edited. The attempt at binding
15628 fails, and the binder generates the following error messages:
15629
15630 @example
15631 error: "hello.adb" must be recompiled ("p.ads" has been modified)
15632 error: "p.ads" has been modified and must be recompiled
15633 @end example
15634
15635 Now both files must be recompiled as indicated, and then the bind can
15636 succeed, generating a main program. You need not normally be concerned
15637 with the contents of this file, but for reference purposes a sample
15638 binder output file is given in @ref{e,,Example of Binder Output File}.
15639
15640 In most normal usage, the default mode of @code{gnatbind} which is to
15641 generate the main package in Ada, as described in the previous section.
15642 In particular, this means that any Ada programmer can read and understand
15643 the generated main program. It can also be debugged just like any other
15644 Ada code provided the @code{-g} switch is used for
15645 @code{gnatbind} and @code{gnatlink}.
15646
15647 @node Switches for gnatbind,Command-Line Access,Running gnatbind,Binding with gnatbind
15648 @anchor{gnat_ugn/building_executable_programs_with_gnat id34}@anchor{11e}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatbind}@anchor{11f}
15649 @subsection Switches for @code{gnatbind}
15650
15651
15652 The following switches are available with @code{gnatbind}; details will
15653 be presented in subsequent sections.
15654
15655 @geindex --version (gnatbind)
15656
15657
15658 @table @asis
15659
15660 @item @code{--version}
15661
15662 Display Copyright and version, then exit disregarding all other options.
15663 @end table
15664
15665 @geindex --help (gnatbind)
15666
15667
15668 @table @asis
15669
15670 @item @code{--help}
15671
15672 If @code{--version} was not used, display usage, then exit disregarding
15673 all other options.
15674 @end table
15675
15676 @geindex -a (gnatbind)
15677
15678
15679 @table @asis
15680
15681 @item @code{-a}
15682
15683 Indicates that, if supported by the platform, the adainit procedure should
15684 be treated as an initialisation routine by the linker (a constructor). This
15685 is intended to be used by the Project Manager to automatically initialize
15686 shared Stand-Alone Libraries.
15687 @end table
15688
15689 @geindex -aO (gnatbind)
15690
15691
15692 @table @asis
15693
15694 @item @code{-aO}
15695
15696 Specify directory to be searched for ALI files.
15697 @end table
15698
15699 @geindex -aI (gnatbind)
15700
15701
15702 @table @asis
15703
15704 @item @code{-aI}
15705
15706 Specify directory to be searched for source file.
15707 @end table
15708
15709 @geindex -A (gnatbind)
15710
15711
15712 @table @asis
15713
15714 @item @code{-A[=@emph{filename}]}
15715
15716 Output ALI list (to standard output or to the named file).
15717 @end table
15718
15719 @geindex -b (gnatbind)
15720
15721
15722 @table @asis
15723
15724 @item @code{-b}
15725
15726 Generate brief messages to @code{stderr} even if verbose mode set.
15727 @end table
15728
15729 @geindex -c (gnatbind)
15730
15731
15732 @table @asis
15733
15734 @item @code{-c}
15735
15736 Check only, no generation of binder output file.
15737 @end table
15738
15739 @geindex -dnn[k|m] (gnatbind)
15740
15741
15742 @table @asis
15743
15744 @item @code{-d@emph{nn}[k|m]}
15745
15746 This switch can be used to change the default task stack size value
15747 to a specified size @code{nn}, which is expressed in bytes by default, or
15748 in kilobytes when suffixed with @code{k} or in megabytes when suffixed
15749 with @code{m}.
15750 In the absence of a @code{[k|m]} suffix, this switch is equivalent,
15751 in effect, to completing all task specs with
15752
15753 @example
15754 pragma Storage_Size (nn);
15755 @end example
15756
15757 When they do not already have such a pragma.
15758 @end table
15759
15760 @geindex -D (gnatbind)
15761
15762
15763 @table @asis
15764
15765 @item @code{-D@emph{nn}[k|m]}
15766
15767 Set the default secondary stack size to @code{nn}. The suffix indicates whether
15768 the size is in bytes (no suffix), kilobytes (@code{k} suffix) or megabytes
15769 (@code{m} suffix).
15770
15771 The secondary stack holds objects of unconstrained types that are returned by
15772 functions, for example unconstrained Strings. The size of the secondary stack
15773 can be dynamic or fixed depending on the target.
15774
15775 For most targets, the secondary stack grows on demand and is implemented as
15776 a chain of blocks in the heap. In this case, the default secondary stack size
15777 determines the initial size of the secondary stack for each task and the
15778 smallest amount the secondary stack can grow by.
15779
15780 For Ravenscar, ZFP, and Cert run-times the size of the secondary stack is
15781 fixed. This switch can be used to change the default size of these stacks.
15782 The default secondary stack size can be overridden on a per-task basis if
15783 individual tasks have different secondary stack requirements. This is
15784 achieved through the Secondary_Stack_Size aspect that takes the size of the
15785 secondary stack in bytes.
15786 @end table
15787
15788 @geindex -e (gnatbind)
15789
15790
15791 @table @asis
15792
15793 @item @code{-e}
15794
15795 Output complete list of elaboration-order dependencies.
15796 @end table
15797
15798 @geindex -Ea (gnatbind)
15799
15800
15801 @table @asis
15802
15803 @item @code{-Ea}
15804
15805 Store tracebacks in exception occurrences when the target supports it.
15806 The "a" is for "address"; tracebacks will contain hexadecimal addresses,
15807 unless symbolic tracebacks are enabled.
15808
15809 See also the packages @code{GNAT.Traceback} and
15810 @code{GNAT.Traceback.Symbolic} for more information.
15811 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
15812 @code{gcc} option.
15813 @end table
15814
15815 @geindex -Es (gnatbind)
15816
15817
15818 @table @asis
15819
15820 @item @code{-Es}
15821
15822 Store tracebacks in exception occurrences when the target supports it.
15823 The "s" is for "symbolic"; symbolic tracebacks are enabled.
15824 @end table
15825
15826 @geindex -E (gnatbind)
15827
15828
15829 @table @asis
15830
15831 @item @code{-E}
15832
15833 Currently the same as @code{-Ea}.
15834 @end table
15835
15836 @geindex -f (gnatbind)
15837
15838
15839 @table @asis
15840
15841 @item @code{-f@emph{elab-order}}
15842
15843 Force elaboration order. For further details see @ref{120,,Elaboration Control}
15844 and @ref{f,,Elaboration Order Handling in GNAT}.
15845 @end table
15846
15847 @geindex -F (gnatbind)
15848
15849
15850 @table @asis
15851
15852 @item @code{-F}
15853
15854 Force the checks of elaboration flags. @code{gnatbind} does not normally
15855 generate checks of elaboration flags for the main executable, except when
15856 a Stand-Alone Library is used. However, there are cases when this cannot be
15857 detected by gnatbind. An example is importing an interface of a Stand-Alone
15858 Library through a pragma Import and only specifying through a linker switch
15859 this Stand-Alone Library. This switch is used to guarantee that elaboration
15860 flag checks are generated.
15861 @end table
15862
15863 @geindex -h (gnatbind)
15864
15865
15866 @table @asis
15867
15868 @item @code{-h}
15869
15870 Output usage (help) information.
15871 @end table
15872
15873 @geindex -H (gnatbind)
15874
15875
15876 @table @asis
15877
15878 @item @code{-H}
15879
15880 Legacy elaboration order model enabled. For further details see
15881 @ref{f,,Elaboration Order Handling in GNAT}.
15882 @end table
15883
15884 @geindex -H32 (gnatbind)
15885
15886
15887 @table @asis
15888
15889 @item @code{-H32}
15890
15891 Use 32-bit allocations for @code{__gnat_malloc} (and thus for access types).
15892 For further details see @ref{121,,Dynamic Allocation Control}.
15893 @end table
15894
15895 @geindex -H64 (gnatbind)
15896
15897 @geindex __gnat_malloc
15898
15899
15900 @table @asis
15901
15902 @item @code{-H64}
15903
15904 Use 64-bit allocations for @code{__gnat_malloc} (and thus for access types).
15905 For further details see @ref{121,,Dynamic Allocation Control}.
15906
15907 @geindex -I (gnatbind)
15908
15909 @item @code{-I}
15910
15911 Specify directory to be searched for source and ALI files.
15912
15913 @geindex -I- (gnatbind)
15914
15915 @item @code{-I-}
15916
15917 Do not look for sources in the current directory where @code{gnatbind} was
15918 invoked, and do not look for ALI files in the directory containing the
15919 ALI file named in the @code{gnatbind} command line.
15920
15921 @geindex -l (gnatbind)
15922
15923 @item @code{-l}
15924
15925 Output chosen elaboration order.
15926
15927 @geindex -L (gnatbind)
15928
15929 @item @code{-L@emph{xxx}}
15930
15931 Bind the units for library building. In this case the @code{adainit} and
15932 @code{adafinal} procedures (@ref{b4,,Binding with Non-Ada Main Programs})
15933 are renamed to @code{@emph{xxx}init} and
15934 @code{@emph{xxx}final}.
15935 Implies -n.
15936 (@ref{15,,GNAT and Libraries}, for more details.)
15937
15938 @geindex -M (gnatbind)
15939
15940 @item @code{-M@emph{xyz}}
15941
15942 Rename generated main program from main to xyz. This option is
15943 supported on cross environments only.
15944
15945 @geindex -m (gnatbind)
15946
15947 @item @code{-m@emph{n}}
15948
15949 Limit number of detected errors or warnings to @code{n}, where @code{n} is
15950 in the range 1..999999. The default value if no switch is
15951 given is 9999. If the number of warnings reaches this limit, then a
15952 message is output and further warnings are suppressed, the bind
15953 continues in this case. If the number of errors reaches this
15954 limit, then a message is output and the bind is abandoned.
15955 A value of zero means that no limit is enforced. The equal
15956 sign is optional.
15957
15958 @geindex -minimal (gnatbind)
15959
15960 @item @code{-minimal}
15961
15962 Generate a binder file suitable for space-constrained applications. When
15963 active, binder-generated objects not required for program operation are no
15964 longer generated. @strong{Warning:} this option comes with the following
15965 limitations:
15966
15967
15968 @itemize *
15969
15970 @item
15971 Starting the program's execution in the debugger will cause it to
15972 stop at the start of the @code{main} function instead of the main subprogram.
15973 This can be worked around by manually inserting a breakpoint on that
15974 subprogram and resuming the program's execution until reaching that breakpoint.
15975
15976 @item
15977 Programs using GNAT.Compiler_Version will not link.
15978 @end itemize
15979
15980 @geindex -n (gnatbind)
15981
15982 @item @code{-n}
15983
15984 No main program.
15985
15986 @geindex -nostdinc (gnatbind)
15987
15988 @item @code{-nostdinc}
15989
15990 Do not look for sources in the system default directory.
15991
15992 @geindex -nostdlib (gnatbind)
15993
15994 @item @code{-nostdlib}
15995
15996 Do not look for library files in the system default directory.
15997
15998 @geindex --RTS (gnatbind)
15999
16000 @item @code{--RTS=@emph{rts-path}}
16001
16002 Specifies the default location of the run-time library. Same meaning as the
16003 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
16004
16005 @geindex -o (gnatbind)
16006
16007 @item @code{-o @emph{file}}
16008
16009 Name the output file @code{file} (default is @code{b~`xxx}.adb`).
16010 Note that if this option is used, then linking must be done manually,
16011 gnatlink cannot be used.
16012
16013 @geindex -O (gnatbind)
16014
16015 @item @code{-O[=@emph{filename}]}
16016
16017 Output object list (to standard output or to the named file).
16018
16019 @geindex -p (gnatbind)
16020
16021 @item @code{-p}
16022
16023 Pessimistic (worst-case) elaboration order.
16024
16025 @geindex -P (gnatbind)
16026
16027 @item @code{-P}
16028
16029 Generate binder file suitable for CodePeer.
16030
16031 @geindex -R (gnatbind)
16032
16033 @item @code{-R}
16034
16035 Output closure source list, which includes all non-run-time units that are
16036 included in the bind.
16037
16038 @geindex -Ra (gnatbind)
16039
16040 @item @code{-Ra}
16041
16042 Like @code{-R} but the list includes run-time units.
16043
16044 @geindex -s (gnatbind)
16045
16046 @item @code{-s}
16047
16048 Require all source files to be present.
16049
16050 @geindex -S (gnatbind)
16051
16052 @item @code{-S@emph{xxx}}
16053
16054 Specifies the value to be used when detecting uninitialized scalar
16055 objects with pragma Initialize_Scalars.
16056 The @code{xxx} string specified with the switch is one of:
16057
16058
16059 @itemize *
16060
16061 @item
16062 @code{in} for an invalid value.
16063
16064 If zero is invalid for the discrete type in question,
16065 then the scalar value is set to all zero bits.
16066 For signed discrete types, the largest possible negative value of
16067 the underlying scalar is set (i.e. a one bit followed by all zero bits).
16068 For unsigned discrete types, the underlying scalar value is set to all
16069 one bits. For floating-point types, a NaN value is set
16070 (see body of package System.Scalar_Values for exact values).
16071
16072 @item
16073 @code{lo} for low value.
16074
16075 If zero is invalid for the discrete type in question,
16076 then the scalar value is set to all zero bits.
16077 For signed discrete types, the largest possible negative value of
16078 the underlying scalar is set (i.e. a one bit followed by all zero bits).
16079 For unsigned discrete types, the underlying scalar value is set to all
16080 zero bits. For floating-point, a small value is set
16081 (see body of package System.Scalar_Values for exact values).
16082
16083 @item
16084 @code{hi} for high value.
16085
16086 If zero is invalid for the discrete type in question,
16087 then the scalar value is set to all one bits.
16088 For signed discrete types, the largest possible positive value of
16089 the underlying scalar is set (i.e. a zero bit followed by all one bits).
16090 For unsigned discrete types, the underlying scalar value is set to all
16091 one bits. For floating-point, a large value is set
16092 (see body of package System.Scalar_Values for exact values).
16093
16094 @item
16095 @code{xx} for hex value (two hex digits).
16096
16097 The underlying scalar is set to a value consisting of repeated bytes, whose
16098 value corresponds to the given value. For example if @code{BF} is given,
16099 then a 32-bit scalar value will be set to the bit patterm @code{16#BFBFBFBF#}.
16100 @end itemize
16101
16102 @geindex GNAT_INIT_SCALARS
16103
16104 In addition, you can specify @code{-Sev} to indicate that the value is
16105 to be set at run time. In this case, the program will look for an environment
16106 variable of the form @code{GNAT_INIT_SCALARS=@emph{yy}}, where @code{yy} is one
16107 of @code{in/lo/hi/@emph{xx}} with the same meanings as above.
16108 If no environment variable is found, or if it does not have a valid value,
16109 then the default is @code{in} (invalid values).
16110 @end table
16111
16112 @geindex -static (gnatbind)
16113
16114
16115 @table @asis
16116
16117 @item @code{-static}
16118
16119 Link against a static GNAT run-time.
16120
16121 @geindex -shared (gnatbind)
16122
16123 @item @code{-shared}
16124
16125 Link against a shared GNAT run-time when available.
16126
16127 @geindex -t (gnatbind)
16128
16129 @item @code{-t}
16130
16131 Tolerate time stamp and other consistency errors.
16132
16133 @geindex -T (gnatbind)
16134
16135 @item @code{-T@emph{n}}
16136
16137 Set the time slice value to @code{n} milliseconds. If the system supports
16138 the specification of a specific time slice value, then the indicated value
16139 is used. If the system does not support specific time slice values, but
16140 does support some general notion of round-robin scheduling, then any
16141 nonzero value will activate round-robin scheduling.
16142
16143 A value of zero is treated specially. It turns off time
16144 slicing, and in addition, indicates to the tasking run-time that the
16145 semantics should match as closely as possible the Annex D
16146 requirements of the Ada RM, and in particular sets the default
16147 scheduling policy to @code{FIFO_Within_Priorities}.
16148
16149 @geindex -u (gnatbind)
16150
16151 @item @code{-u@emph{n}}
16152
16153 Enable dynamic stack usage, with @code{n} results stored and displayed
16154 at program termination. A result is generated when a task
16155 terminates. Results that can't be stored are displayed on the fly, at
16156 task termination. This option is currently not supported on Itanium
16157 platforms. (See @ref{122,,Dynamic Stack Usage Analysis} for details.)
16158
16159 @geindex -v (gnatbind)
16160
16161 @item @code{-v}
16162
16163 Verbose mode. Write error messages, header, summary output to
16164 @code{stdout}.
16165
16166 @geindex -V (gnatbind)
16167
16168 @item @code{-V@emph{key}=@emph{value}}
16169
16170 Store the given association of @code{key} to @code{value} in the bind environment.
16171 Values stored this way can be retrieved at run time using
16172 @code{GNAT.Bind_Environment}.
16173
16174 @geindex -w (gnatbind)
16175
16176 @item @code{-w@emph{x}}
16177
16178 Warning mode; @code{x} = s/e for suppress/treat as error.
16179
16180 @geindex -Wx (gnatbind)
16181
16182 @item @code{-Wx@emph{e}}
16183
16184 Override default wide character encoding for standard Text_IO files.
16185
16186 @geindex -x (gnatbind)
16187
16188 @item @code{-x}
16189
16190 Exclude source files (check object consistency only).
16191
16192 @geindex -Xnnn (gnatbind)
16193
16194 @item @code{-X@emph{nnn}}
16195
16196 Set default exit status value, normally 0 for POSIX compliance.
16197
16198 @geindex -y (gnatbind)
16199
16200 @item @code{-y}
16201
16202 Enable leap seconds support in @code{Ada.Calendar} and its children.
16203
16204 @geindex -z (gnatbind)
16205
16206 @item @code{-z}
16207
16208 No main subprogram.
16209 @end table
16210
16211 You may obtain this listing of switches by running @code{gnatbind} with
16212 no arguments.
16213
16214 @menu
16215 * Consistency-Checking Modes::
16216 * Binder Error Message Control::
16217 * Elaboration Control::
16218 * Output Control::
16219 * Dynamic Allocation Control::
16220 * Binding with Non-Ada Main Programs::
16221 * Binding Programs with No Main Subprogram::
16222
16223 @end menu
16224
16225 @node Consistency-Checking Modes,Binder Error Message Control,,Switches for gnatbind
16226 @anchor{gnat_ugn/building_executable_programs_with_gnat consistency-checking-modes}@anchor{123}@anchor{gnat_ugn/building_executable_programs_with_gnat id35}@anchor{124}
16227 @subsubsection Consistency-Checking Modes
16228
16229
16230 As described earlier, by default @code{gnatbind} checks
16231 that object files are consistent with one another and are consistent
16232 with any source files it can locate. The following switches control binder
16233 access to sources.
16234
16235 @quotation
16236
16237 @geindex -s (gnatbind)
16238 @end quotation
16239
16240
16241 @table @asis
16242
16243 @item @code{-s}
16244
16245 Require source files to be present. In this mode, the binder must be
16246 able to locate all source files that are referenced, in order to check
16247 their consistency. In normal mode, if a source file cannot be located it
16248 is simply ignored. If you specify this switch, a missing source
16249 file is an error.
16250
16251 @geindex -Wx (gnatbind)
16252
16253 @item @code{-Wx@emph{e}}
16254
16255 Override default wide character encoding for standard Text_IO files.
16256 Normally the default wide character encoding method used for standard
16257 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
16258 the main source input (see description of switch
16259 @code{-gnatWx} for the compiler). The
16260 use of this switch for the binder (which has the same set of
16261 possible arguments) overrides this default as specified.
16262
16263 @geindex -x (gnatbind)
16264
16265 @item @code{-x}
16266
16267 Exclude source files. In this mode, the binder only checks that ALI
16268 files are consistent with one another. Source files are not accessed.
16269 The binder runs faster in this mode, and there is still a guarantee that
16270 the resulting program is self-consistent.
16271 If a source file has been edited since it was last compiled, and you
16272 specify this switch, the binder will not detect that the object
16273 file is out of date with respect to the source file. Note that this is the
16274 mode that is automatically used by @code{gnatmake} because in this
16275 case the checking against sources has already been performed by
16276 @code{gnatmake} in the course of compilation (i.e., before binding).
16277 @end table
16278
16279 @node Binder Error Message Control,Elaboration Control,Consistency-Checking Modes,Switches for gnatbind
16280 @anchor{gnat_ugn/building_executable_programs_with_gnat id36}@anchor{125}@anchor{gnat_ugn/building_executable_programs_with_gnat binder-error-message-control}@anchor{126}
16281 @subsubsection Binder Error Message Control
16282
16283
16284 The following switches provide control over the generation of error
16285 messages from the binder:
16286
16287 @quotation
16288
16289 @geindex -v (gnatbind)
16290 @end quotation
16291
16292
16293 @table @asis
16294
16295 @item @code{-v}
16296
16297 Verbose mode. In the normal mode, brief error messages are generated to
16298 @code{stderr}. If this switch is present, a header is written
16299 to @code{stdout} and any error messages are directed to @code{stdout}.
16300 All that is written to @code{stderr} is a brief summary message.
16301
16302 @geindex -b (gnatbind)
16303
16304 @item @code{-b}
16305
16306 Generate brief error messages to @code{stderr} even if verbose mode is
16307 specified. This is relevant only when used with the
16308 @code{-v} switch.
16309
16310 @geindex -m (gnatbind)
16311
16312 @item @code{-m@emph{n}}
16313
16314 Limits the number of error messages to @code{n}, a decimal integer in the
16315 range 1-999. The binder terminates immediately if this limit is reached.
16316
16317 @geindex -M (gnatbind)
16318
16319 @item @code{-M@emph{xxx}}
16320
16321 Renames the generated main program from @code{main} to @code{xxx}.
16322 This is useful in the case of some cross-building environments, where
16323 the actual main program is separate from the one generated
16324 by @code{gnatbind}.
16325
16326 @geindex -ws (gnatbind)
16327
16328 @geindex Warnings
16329
16330 @item @code{-ws}
16331
16332 Suppress all warning messages.
16333
16334 @geindex -we (gnatbind)
16335
16336 @item @code{-we}
16337
16338 Treat any warning messages as fatal errors.
16339
16340 @geindex -t (gnatbind)
16341
16342 @geindex Time stamp checks
16343 @geindex in binder
16344
16345 @geindex Binder consistency checks
16346
16347 @geindex Consistency checks
16348 @geindex in binder
16349
16350 @item @code{-t}
16351
16352 The binder performs a number of consistency checks including:
16353
16354
16355 @itemize *
16356
16357 @item
16358 Check that time stamps of a given source unit are consistent
16359
16360 @item
16361 Check that checksums of a given source unit are consistent
16362
16363 @item
16364 Check that consistent versions of @code{GNAT} were used for compilation
16365
16366 @item
16367 Check consistency of configuration pragmas as required
16368 @end itemize
16369
16370 Normally failure of such checks, in accordance with the consistency
16371 requirements of the Ada Reference Manual, causes error messages to be
16372 generated which abort the binder and prevent the output of a binder
16373 file and subsequent link to obtain an executable.
16374
16375 The @code{-t} switch converts these error messages
16376 into warnings, so that
16377 binding and linking can continue to completion even in the presence of such
16378 errors. The result may be a failed link (due to missing symbols), or a
16379 non-functional executable which has undefined semantics.
16380
16381 @cartouche
16382 @quotation Note
16383 This means that @code{-t} should be used only in unusual situations,
16384 with extreme care.
16385 @end quotation
16386 @end cartouche
16387 @end table
16388
16389 @node Elaboration Control,Output Control,Binder Error Message Control,Switches for gnatbind
16390 @anchor{gnat_ugn/building_executable_programs_with_gnat id37}@anchor{127}@anchor{gnat_ugn/building_executable_programs_with_gnat elaboration-control}@anchor{120}
16391 @subsubsection Elaboration Control
16392
16393
16394 The following switches provide additional control over the elaboration
16395 order. For further details see @ref{f,,Elaboration Order Handling in GNAT}.
16396
16397 @geindex -f (gnatbind)
16398
16399
16400 @table @asis
16401
16402 @item @code{-f@emph{elab-order}}
16403
16404 Force elaboration order.
16405
16406 @code{elab-order} should be the name of a "forced elaboration order file", that
16407 is, a text file containing library item names, one per line. A name of the
16408 form "some.unit%s" or "some.unit (spec)" denotes the spec of Some.Unit. A
16409 name of the form "some.unit%b" or "some.unit (body)" denotes the body of
16410 Some.Unit. Each pair of lines is taken to mean that there is an elaboration
16411 dependence of the second line on the first. For example, if the file
16412 contains:
16413
16414 @example
16415 this (spec)
16416 this (body)
16417 that (spec)
16418 that (body)
16419 @end example
16420
16421 then the spec of This will be elaborated before the body of This, and the
16422 body of This will be elaborated before the spec of That, and the spec of That
16423 will be elaborated before the body of That. The first and last of these three
16424 dependences are already required by Ada rules, so this file is really just
16425 forcing the body of This to be elaborated before the spec of That.
16426
16427 The given order must be consistent with Ada rules, or else @code{gnatbind} will
16428 give elaboration cycle errors. For example, if you say x (body) should be
16429 elaborated before x (spec), there will be a cycle, because Ada rules require
16430 x (spec) to be elaborated before x (body); you can't have the spec and body
16431 both elaborated before each other.
16432
16433 If you later add "with That;" to the body of This, there will be a cycle, in
16434 which case you should erase either "this (body)" or "that (spec)" from the
16435 above forced elaboration order file.
16436
16437 Blank lines and Ada-style comments are ignored. Unit names that do not exist
16438 in the program are ignored. Units in the GNAT predefined library are also
16439 ignored.
16440 @end table
16441
16442 @geindex -p (gnatbind)
16443
16444
16445 @table @asis
16446
16447 @item @code{-p}
16448
16449 Pessimistic elaboration order
16450
16451 This switch is only applicable to the pre-20.x legacy elaboration models.
16452 The post-20.x elaboration model uses a more informed approach of ordering
16453 the units.
16454
16455 Normally the binder attempts to choose an elaboration order that is likely to
16456 minimize the likelihood of an elaboration order error resulting in raising a
16457 @code{Program_Error} exception. This switch reverses the action of the binder,
16458 and requests that it deliberately choose an order that is likely to maximize
16459 the likelihood of an elaboration error. This is useful in ensuring
16460 portability and avoiding dependence on accidental fortuitous elaboration
16461 ordering.
16462
16463 Normally it only makes sense to use the @code{-p} switch if dynamic
16464 elaboration checking is used (@code{-gnatE} switch used for compilation).
16465 This is because in the default static elaboration mode, all necessary
16466 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
16467 These implicit pragmas are still respected by the binder in @code{-p}
16468 mode, so a safe elaboration order is assured.
16469
16470 Note that @code{-p} is not intended for production use; it is more for
16471 debugging/experimental use.
16472 @end table
16473
16474 @node Output Control,Dynamic Allocation Control,Elaboration Control,Switches for gnatbind
16475 @anchor{gnat_ugn/building_executable_programs_with_gnat output-control}@anchor{128}@anchor{gnat_ugn/building_executable_programs_with_gnat id38}@anchor{129}
16476 @subsubsection Output Control
16477
16478
16479 The following switches allow additional control over the output
16480 generated by the binder.
16481
16482 @quotation
16483
16484 @geindex -c (gnatbind)
16485 @end quotation
16486
16487
16488 @table @asis
16489
16490 @item @code{-c}
16491
16492 Check only. Do not generate the binder output file. In this mode the
16493 binder performs all error checks but does not generate an output file.
16494
16495 @geindex -e (gnatbind)
16496
16497 @item @code{-e}
16498
16499 Output complete list of elaboration-order dependencies, showing the
16500 reason for each dependency. This output can be rather extensive but may
16501 be useful in diagnosing problems with elaboration order. The output is
16502 written to @code{stdout}.
16503
16504 @geindex -h (gnatbind)
16505
16506 @item @code{-h}
16507
16508 Output usage information. The output is written to @code{stdout}.
16509
16510 @geindex -K (gnatbind)
16511
16512 @item @code{-K}
16513
16514 Output linker options to @code{stdout}. Includes library search paths,
16515 contents of pragmas Ident and Linker_Options, and libraries added
16516 by @code{gnatbind}.
16517
16518 @geindex -l (gnatbind)
16519
16520 @item @code{-l}
16521
16522 Output chosen elaboration order. The output is written to @code{stdout}.
16523
16524 @geindex -O (gnatbind)
16525
16526 @item @code{-O}
16527
16528 Output full names of all the object files that must be linked to provide
16529 the Ada component of the program. The output is written to @code{stdout}.
16530 This list includes the files explicitly supplied and referenced by the user
16531 as well as implicitly referenced run-time unit files. The latter are
16532 omitted if the corresponding units reside in shared libraries. The
16533 directory names for the run-time units depend on the system configuration.
16534
16535 @geindex -o (gnatbind)
16536
16537 @item @code{-o @emph{file}}
16538
16539 Set name of output file to @code{file} instead of the normal
16540 @code{b~`mainprog}.adb` default. Note that @code{file} denote the Ada
16541 binder generated body filename.
16542 Note that if this option is used, then linking must be done manually.
16543 It is not possible to use gnatlink in this case, since it cannot locate
16544 the binder file.
16545
16546 @geindex -r (gnatbind)
16547
16548 @item @code{-r}
16549
16550 Generate list of @code{pragma Restrictions} that could be applied to
16551 the current unit. This is useful for code audit purposes, and also may
16552 be used to improve code generation in some cases.
16553 @end table
16554
16555 @node Dynamic Allocation Control,Binding with Non-Ada Main Programs,Output Control,Switches for gnatbind
16556 @anchor{gnat_ugn/building_executable_programs_with_gnat dynamic-allocation-control}@anchor{121}@anchor{gnat_ugn/building_executable_programs_with_gnat id39}@anchor{12a}
16557 @subsubsection Dynamic Allocation Control
16558
16559
16560 The heap control switches -- @code{-H32} and @code{-H64} --
16561 determine whether dynamic allocation uses 32-bit or 64-bit memory.
16562 They only affect compiler-generated allocations via @code{__gnat_malloc};
16563 explicit calls to @code{malloc} and related functions from the C
16564 run-time library are unaffected.
16565
16566
16567 @table @asis
16568
16569 @item @code{-H32}
16570
16571 Allocate memory on 32-bit heap
16572
16573 @item @code{-H64}
16574
16575 Allocate memory on 64-bit heap. This is the default
16576 unless explicitly overridden by a @code{'Size} clause on the access type.
16577 @end table
16578
16579 These switches are only effective on VMS platforms.
16580
16581 @node Binding with Non-Ada Main Programs,Binding Programs with No Main Subprogram,Dynamic Allocation Control,Switches for gnatbind
16582 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-non-ada-main-programs}@anchor{b4}@anchor{gnat_ugn/building_executable_programs_with_gnat id40}@anchor{12b}
16583 @subsubsection Binding with Non-Ada Main Programs
16584
16585
16586 The description so far has assumed that the main
16587 program is in Ada, and that the task of the binder is to generate a
16588 corresponding function @code{main} that invokes this Ada main
16589 program. GNAT also supports the building of executable programs where
16590 the main program is not in Ada, but some of the called routines are
16591 written in Ada and compiled using GNAT (@ref{44,,Mixed Language Programming}).
16592 The following switch is used in this situation:
16593
16594 @quotation
16595
16596 @geindex -n (gnatbind)
16597 @end quotation
16598
16599
16600 @table @asis
16601
16602 @item @code{-n}
16603
16604 No main program. The main program is not in Ada.
16605 @end table
16606
16607 In this case, most of the functions of the binder are still required,
16608 but instead of generating a main program, the binder generates a file
16609 containing the following callable routines:
16610
16611 @quotation
16612
16613 @geindex adainit
16614
16615
16616 @table @asis
16617
16618 @item @code{adainit}
16619
16620 You must call this routine to initialize the Ada part of the program by
16621 calling the necessary elaboration routines. A call to @code{adainit} is
16622 required before the first call to an Ada subprogram.
16623
16624 Note that it is assumed that the basic execution environment must be setup
16625 to be appropriate for Ada execution at the point where the first Ada
16626 subprogram is called. In particular, if the Ada code will do any
16627 floating-point operations, then the FPU must be setup in an appropriate
16628 manner. For the case of the x86, for example, full precision mode is
16629 required. The procedure GNAT.Float_Control.Reset may be used to ensure
16630 that the FPU is in the right state.
16631 @end table
16632
16633 @geindex adafinal
16634
16635
16636 @table @asis
16637
16638 @item @code{adafinal}
16639
16640 You must call this routine to perform any library-level finalization
16641 required by the Ada subprograms. A call to @code{adafinal} is required
16642 after the last call to an Ada subprogram, and before the program
16643 terminates.
16644 @end table
16645 @end quotation
16646
16647 @geindex -n (gnatbind)
16648
16649 @geindex Binder
16650 @geindex multiple input files
16651
16652 If the @code{-n} switch
16653 is given, more than one ALI file may appear on
16654 the command line for @code{gnatbind}. The normal @code{closure}
16655 calculation is performed for each of the specified units. Calculating
16656 the closure means finding out the set of units involved by tracing
16657 @emph{with} references. The reason it is necessary to be able to
16658 specify more than one ALI file is that a given program may invoke two or
16659 more quite separate groups of Ada units.
16660
16661 The binder takes the name of its output file from the last specified ALI
16662 file, unless overridden by the use of the @code{-o file}.
16663
16664 @geindex -o (gnatbind)
16665
16666 The output is an Ada unit in source form that can be compiled with GNAT.
16667 This compilation occurs automatically as part of the @code{gnatlink}
16668 processing.
16669
16670 Currently the GNAT run-time requires a FPU using 80 bits mode
16671 precision. Under targets where this is not the default it is required to
16672 call GNAT.Float_Control.Reset before using floating point numbers (this
16673 include float computation, float input and output) in the Ada code. A
16674 side effect is that this could be the wrong mode for the foreign code
16675 where floating point computation could be broken after this call.
16676
16677 @node Binding Programs with No Main Subprogram,,Binding with Non-Ada Main Programs,Switches for gnatbind
16678 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-programs-with-no-main-subprogram}@anchor{12c}@anchor{gnat_ugn/building_executable_programs_with_gnat id41}@anchor{12d}
16679 @subsubsection Binding Programs with No Main Subprogram
16680
16681
16682 It is possible to have an Ada program which does not have a main
16683 subprogram. This program will call the elaboration routines of all the
16684 packages, then the finalization routines.
16685
16686 The following switch is used to bind programs organized in this manner:
16687
16688 @quotation
16689
16690 @geindex -z (gnatbind)
16691 @end quotation
16692
16693
16694 @table @asis
16695
16696 @item @code{-z}
16697
16698 Normally the binder checks that the unit name given on the command line
16699 corresponds to a suitable main subprogram. When this switch is used,
16700 a list of ALI files can be given, and the execution of the program
16701 consists of elaboration of these units in an appropriate order. Note
16702 that the default wide character encoding method for standard Text_IO
16703 files is always set to Brackets if this switch is set (you can use
16704 the binder switch
16705 @code{-Wx} to override this default).
16706 @end table
16707
16708 @node Command-Line Access,Search Paths for gnatbind,Switches for gnatbind,Binding with gnatbind
16709 @anchor{gnat_ugn/building_executable_programs_with_gnat id42}@anchor{12e}@anchor{gnat_ugn/building_executable_programs_with_gnat command-line-access}@anchor{12f}
16710 @subsection Command-Line Access
16711
16712
16713 The package @code{Ada.Command_Line} provides access to the command-line
16714 arguments and program name. In order for this interface to operate
16715 correctly, the two variables
16716
16717 @example
16718 int gnat_argc;
16719 char **gnat_argv;
16720 @end example
16721
16722 @geindex gnat_argv
16723
16724 @geindex gnat_argc
16725
16726 are declared in one of the GNAT library routines. These variables must
16727 be set from the actual @code{argc} and @code{argv} values passed to the
16728 main program. With no @emph{n} present, @code{gnatbind}
16729 generates the C main program to automatically set these variables.
16730 If the @emph{n} switch is used, there is no automatic way to
16731 set these variables. If they are not set, the procedures in
16732 @code{Ada.Command_Line} will not be available, and any attempt to use
16733 them will raise @code{Constraint_Error}. If command line access is
16734 required, your main program must set @code{gnat_argc} and
16735 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
16736 it.
16737
16738 @node Search Paths for gnatbind,Examples of gnatbind Usage,Command-Line Access,Binding with gnatbind
16739 @anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-for-gnatbind}@anchor{8c}@anchor{gnat_ugn/building_executable_programs_with_gnat id43}@anchor{130}
16740 @subsection Search Paths for @code{gnatbind}
16741
16742
16743 The binder takes the name of an ALI file as its argument and needs to
16744 locate source files as well as other ALI files to verify object consistency.
16745
16746 For source files, it follows exactly the same search rules as @code{gcc}
16747 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}). For ALI files the
16748 directories searched are:
16749
16750
16751 @itemize *
16752
16753 @item
16754 The directory containing the ALI file named in the command line, unless
16755 the switch @code{-I-} is specified.
16756
16757 @item
16758 All directories specified by @code{-I}
16759 switches on the @code{gnatbind}
16760 command line, in the order given.
16761
16762 @geindex ADA_PRJ_OBJECTS_FILE
16763
16764 @item
16765 Each of the directories listed in the text file whose name is given
16766 by the
16767 @geindex ADA_PRJ_OBJECTS_FILE
16768 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16769 @code{ADA_PRJ_OBJECTS_FILE} environment variable.
16770
16771 @geindex ADA_PRJ_OBJECTS_FILE
16772 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16773 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the gnat
16774 driver when project files are used. It should not normally be set
16775 by other means.
16776
16777 @geindex ADA_OBJECTS_PATH
16778
16779 @item
16780 Each of the directories listed in the value of the
16781 @geindex ADA_OBJECTS_PATH
16782 @geindex environment variable; ADA_OBJECTS_PATH
16783 @code{ADA_OBJECTS_PATH} environment variable.
16784 Construct this value
16785 exactly as the
16786 @geindex PATH
16787 @geindex environment variable; PATH
16788 @code{PATH} environment variable: a list of directory
16789 names separated by colons (semicolons when working with the NT version
16790 of GNAT).
16791
16792 @item
16793 The content of the @code{ada_object_path} file which is part of the GNAT
16794 installation tree and is used to store standard libraries such as the
16795 GNAT Run-Time Library (RTL) unless the switch @code{-nostdlib} is
16796 specified. See @ref{87,,Installing a library}
16797 @end itemize
16798
16799 @geindex -I (gnatbind)
16800
16801 @geindex -aI (gnatbind)
16802
16803 @geindex -aO (gnatbind)
16804
16805 In the binder the switch @code{-I}
16806 is used to specify both source and
16807 library file paths. Use @code{-aI}
16808 instead if you want to specify
16809 source paths only, and @code{-aO}
16810 if you want to specify library paths
16811 only. This means that for the binder
16812 @code{-I@emph{dir}} is equivalent to
16813 @code{-aI@emph{dir}}
16814 @code{-aO`@emph{dir}}.
16815 The binder generates the bind file (a C language source file) in the
16816 current working directory.
16817
16818 @geindex Ada
16819
16820 @geindex System
16821
16822 @geindex Interfaces
16823
16824 @geindex GNAT
16825
16826 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
16827 children make up the GNAT Run-Time Library, together with the package
16828 GNAT and its children, which contain a set of useful additional
16829 library functions provided by GNAT. The sources for these units are
16830 needed by the compiler and are kept together in one directory. The ALI
16831 files and object files generated by compiling the RTL are needed by the
16832 binder and the linker and are kept together in one directory, typically
16833 different from the directory containing the sources. In a normal
16834 installation, you need not specify these directory names when compiling
16835 or binding. Either the environment variables or the built-in defaults
16836 cause these files to be found.
16837
16838 Besides simplifying access to the RTL, a major use of search paths is
16839 in compiling sources from multiple directories. This can make
16840 development environments much more flexible.
16841
16842 @node Examples of gnatbind Usage,,Search Paths for gnatbind,Binding with gnatbind
16843 @anchor{gnat_ugn/building_executable_programs_with_gnat id44}@anchor{131}@anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatbind-usage}@anchor{132}
16844 @subsection Examples of @code{gnatbind} Usage
16845
16846
16847 Here are some examples of @code{gnatbind} invovations:
16848
16849 @quotation
16850
16851 @example
16852 gnatbind hello
16853 @end example
16854
16855 The main program @code{Hello} (source program in @code{hello.adb}) is
16856 bound using the standard switch settings. The generated main program is
16857 @code{b~hello.adb}. This is the normal, default use of the binder.
16858
16859 @example
16860 gnatbind hello -o mainprog.adb
16861 @end example
16862
16863 The main program @code{Hello} (source program in @code{hello.adb}) is
16864 bound using the standard switch settings. The generated main program is
16865 @code{mainprog.adb} with the associated spec in
16866 @code{mainprog.ads}. Note that you must specify the body here not the
16867 spec. Note that if this option is used, then linking must be done manually,
16868 since gnatlink will not be able to find the generated file.
16869 @end quotation
16870
16871 @node Linking with gnatlink,Using the GNU make Utility,Binding with gnatbind,Building Executable Programs with GNAT
16872 @anchor{gnat_ugn/building_executable_programs_with_gnat id45}@anchor{133}@anchor{gnat_ugn/building_executable_programs_with_gnat linking-with-gnatlink}@anchor{1e}
16873 @section Linking with @code{gnatlink}
16874
16875
16876 @geindex gnatlink
16877
16878 This chapter discusses @code{gnatlink}, a tool that links
16879 an Ada program and builds an executable file. This utility
16880 invokes the system linker (via the @code{gcc} command)
16881 with a correct list of object files and library references.
16882 @code{gnatlink} automatically determines the list of files and
16883 references for the Ada part of a program. It uses the binder file
16884 generated by the @code{gnatbind} to determine this list.
16885
16886 @menu
16887 * Running gnatlink::
16888 * Switches for gnatlink::
16889
16890 @end menu
16891
16892 @node Running gnatlink,Switches for gnatlink,,Linking with gnatlink
16893 @anchor{gnat_ugn/building_executable_programs_with_gnat id46}@anchor{134}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatlink}@anchor{135}
16894 @subsection Running @code{gnatlink}
16895
16896
16897 The form of the @code{gnatlink} command is
16898
16899 @example
16900 $ gnatlink [ switches ] mainprog [.ali]
16901 [ non-Ada objects ] [ linker options ]
16902 @end example
16903
16904 The arguments of @code{gnatlink} (switches, main @code{ALI} file,
16905 non-Ada objects
16906 or linker options) may be in any order, provided that no non-Ada object may
16907 be mistaken for a main @code{ALI} file.
16908 Any file name @code{F} without the @code{.ali}
16909 extension will be taken as the main @code{ALI} file if a file exists
16910 whose name is the concatenation of @code{F} and @code{.ali}.
16911
16912 @code{mainprog.ali} references the ALI file of the main program.
16913 The @code{.ali} extension of this file can be omitted. From this
16914 reference, @code{gnatlink} locates the corresponding binder file
16915 @code{b~mainprog.adb} and, using the information in this file along
16916 with the list of non-Ada objects and linker options, constructs a
16917 linker command file to create the executable.
16918
16919 The arguments other than the @code{gnatlink} switches and the main
16920 @code{ALI} file are passed to the linker uninterpreted.
16921 They typically include the names of
16922 object files for units written in other languages than Ada and any library
16923 references required to resolve references in any of these foreign language
16924 units, or in @code{Import} pragmas in any Ada units.
16925
16926 @code{linker options} is an optional list of linker specific
16927 switches.
16928 The default linker called by gnatlink is @code{gcc} which in
16929 turn calls the appropriate system linker.
16930
16931 One useful option for the linker is @code{-s}: it reduces the size of the
16932 executable by removing all symbol table and relocation information from the
16933 executable.
16934
16935 Standard options for the linker such as @code{-lmy_lib} or
16936 @code{-Ldir} can be added as is.
16937 For options that are not recognized by
16938 @code{gcc} as linker options, use the @code{gcc} switches
16939 @code{-Xlinker} or @code{-Wl,}.
16940
16941 Refer to the GCC documentation for
16942 details.
16943
16944 Here is an example showing how to generate a linker map:
16945
16946 @example
16947 $ gnatlink my_prog -Wl,-Map,MAPFILE
16948 @end example
16949
16950 Using @code{linker options} it is possible to set the program stack and
16951 heap size.
16952 See @ref{136,,Setting Stack Size from gnatlink} and
16953 @ref{137,,Setting Heap Size from gnatlink}.
16954
16955 @code{gnatlink} determines the list of objects required by the Ada
16956 program and prepends them to the list of objects passed to the linker.
16957 @code{gnatlink} also gathers any arguments set by the use of
16958 @code{pragma Linker_Options} and adds them to the list of arguments
16959 presented to the linker.
16960
16961 @node Switches for gnatlink,,Running gnatlink,Linking with gnatlink
16962 @anchor{gnat_ugn/building_executable_programs_with_gnat id47}@anchor{138}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatlink}@anchor{139}
16963 @subsection Switches for @code{gnatlink}
16964
16965
16966 The following switches are available with the @code{gnatlink} utility:
16967
16968 @geindex --version (gnatlink)
16969
16970
16971 @table @asis
16972
16973 @item @code{--version}
16974
16975 Display Copyright and version, then exit disregarding all other options.
16976 @end table
16977
16978 @geindex --help (gnatlink)
16979
16980
16981 @table @asis
16982
16983 @item @code{--help}
16984
16985 If @code{--version} was not used, display usage, then exit disregarding
16986 all other options.
16987 @end table
16988
16989 @geindex Command line length
16990
16991 @geindex -f (gnatlink)
16992
16993
16994 @table @asis
16995
16996 @item @code{-f}
16997
16998 On some targets, the command line length is limited, and @code{gnatlink}
16999 will generate a separate file for the linker if the list of object files
17000 is too long.
17001 The @code{-f} switch forces this file
17002 to be generated even if
17003 the limit is not exceeded. This is useful in some cases to deal with
17004 special situations where the command line length is exceeded.
17005 @end table
17006
17007 @geindex Debugging information
17008 @geindex including
17009
17010 @geindex -g (gnatlink)
17011
17012
17013 @table @asis
17014
17015 @item @code{-g}
17016
17017 The option to include debugging information causes the Ada bind file (in
17018 other words, @code{b~mainprog.adb}) to be compiled with @code{-g}.
17019 In addition, the binder does not delete the @code{b~mainprog.adb},
17020 @code{b~mainprog.o} and @code{b~mainprog.ali} files.
17021 Without @code{-g}, the binder removes these files by default.
17022 @end table
17023
17024 @geindex -n (gnatlink)
17025
17026
17027 @table @asis
17028
17029 @item @code{-n}
17030
17031 Do not compile the file generated by the binder. This may be used when
17032 a link is rerun with different options, but there is no need to recompile
17033 the binder file.
17034 @end table
17035
17036 @geindex -v (gnatlink)
17037
17038
17039 @table @asis
17040
17041 @item @code{-v}
17042
17043 Verbose mode. Causes additional information to be output, including a full
17044 list of the included object files.
17045 This switch option is most useful when you want
17046 to see what set of object files are being used in the link step.
17047 @end table
17048
17049 @geindex -v -v (gnatlink)
17050
17051
17052 @table @asis
17053
17054 @item @code{-v -v}
17055
17056 Very verbose mode. Requests that the compiler operate in verbose mode when
17057 it compiles the binder file, and that the system linker run in verbose mode.
17058 @end table
17059
17060 @geindex -o (gnatlink)
17061
17062
17063 @table @asis
17064
17065 @item @code{-o @emph{exec-name}}
17066
17067 @code{exec-name} specifies an alternate name for the generated
17068 executable program. If this switch is omitted, the executable has the same
17069 name as the main unit. For example, @code{gnatlink try.ali} creates
17070 an executable called @code{try}.
17071 @end table
17072
17073 @geindex -B (gnatlink)
17074
17075
17076 @table @asis
17077
17078 @item @code{-B@emph{dir}}
17079
17080 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
17081 from @code{dir} instead of the default location. Only use this switch
17082 when multiple versions of the GNAT compiler are available.
17083 See the @code{Directory Options} section in @cite{The_GNU_Compiler_Collection}
17084 for further details. You would normally use the @code{-b} or
17085 @code{-V} switch instead.
17086 @end table
17087
17088 @geindex -M (gnatlink)
17089
17090
17091 @table @asis
17092
17093 @item @code{-M}
17094
17095 When linking an executable, create a map file. The name of the map file
17096 has the same name as the executable with extension ".map".
17097 @end table
17098
17099 @geindex -M= (gnatlink)
17100
17101
17102 @table @asis
17103
17104 @item @code{-M=@emph{mapfile}}
17105
17106 When linking an executable, create a map file. The name of the map file is
17107 @code{mapfile}.
17108 @end table
17109
17110 @geindex --GCC=compiler_name (gnatlink)
17111
17112
17113 @table @asis
17114
17115 @item @code{--GCC=@emph{compiler_name}}
17116
17117 Program used for compiling the binder file. The default is
17118 @code{gcc}. You need to use quotes around @code{compiler_name} if
17119 @code{compiler_name} contains spaces or other separator characters.
17120 As an example @code{--GCC="foo -x -y"} will instruct @code{gnatlink} to
17121 use @code{foo -x -y} as your compiler. Note that switch @code{-c} is always
17122 inserted after your command name. Thus in the above example the compiler
17123 command that will be used by @code{gnatlink} will be @code{foo -c -x -y}.
17124 A limitation of this syntax is that the name and path name of the executable
17125 itself must not include any embedded spaces. If the compiler executable is
17126 different from the default one (gcc or <prefix>-gcc), then the back-end
17127 switches in the ALI file are not used to compile the binder generated source.
17128 For example, this is the case with @code{--GCC="foo -x -y"}. But the back end
17129 switches will be used for @code{--GCC="gcc -gnatv"}. If several
17130 @code{--GCC=compiler_name} are used, only the last @code{compiler_name}
17131 is taken into account. However, all the additional switches are also taken
17132 into account. Thus,
17133 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
17134 @code{--GCC="bar -x -y -z -t"}.
17135 @end table
17136
17137 @geindex --LINK= (gnatlink)
17138
17139
17140 @table @asis
17141
17142 @item @code{--LINK=@emph{name}}
17143
17144 @code{name} is the name of the linker to be invoked. This is especially
17145 useful in mixed language programs since languages such as C++ require
17146 their own linker to be used. When this switch is omitted, the default
17147 name for the linker is @code{gcc}. When this switch is used, the
17148 specified linker is called instead of @code{gcc} with exactly the same
17149 parameters that would have been passed to @code{gcc} so if the desired
17150 linker requires different parameters it is necessary to use a wrapper
17151 script that massages the parameters before invoking the real linker. It
17152 may be useful to control the exact invocation by using the verbose
17153 switch.
17154 @end table
17155
17156 @node Using the GNU make Utility,,Linking with gnatlink,Building Executable Programs with GNAT
17157 @anchor{gnat_ugn/building_executable_programs_with_gnat using-the-gnu-make-utility}@anchor{1f}@anchor{gnat_ugn/building_executable_programs_with_gnat id48}@anchor{13a}
17158 @section Using the GNU @code{make} Utility
17159
17160
17161 @geindex make (GNU)
17162 @geindex GNU make
17163
17164 This chapter offers some examples of makefiles that solve specific
17165 problems. It does not explain how to write a makefile, nor does it try to replace the
17166 @code{gnatmake} utility (@ref{1b,,Building with gnatmake}).
17167
17168 All the examples in this section are specific to the GNU version of
17169 make. Although @code{make} is a standard utility, and the basic language
17170 is the same, these examples use some advanced features found only in
17171 @code{GNU make}.
17172
17173 @menu
17174 * Using gnatmake in a Makefile::
17175 * Automatically Creating a List of Directories::
17176 * Generating the Command Line Switches::
17177 * Overcoming Command Line Length Limits::
17178
17179 @end menu
17180
17181 @node Using gnatmake in a Makefile,Automatically Creating a List of Directories,,Using the GNU make Utility
17182 @anchor{gnat_ugn/building_executable_programs_with_gnat using-gnatmake-in-a-makefile}@anchor{13b}@anchor{gnat_ugn/building_executable_programs_with_gnat id49}@anchor{13c}
17183 @subsection Using gnatmake in a Makefile
17184
17185
17186 @c index makefile (GNU make)
17187
17188 Complex project organizations can be handled in a very powerful way by
17189 using GNU make combined with gnatmake. For instance, here is a Makefile
17190 which allows you to build each subsystem of a big project into a separate
17191 shared library. Such a makefile allows you to significantly reduce the link
17192 time of very big applications while maintaining full coherence at
17193 each step of the build process.
17194
17195 The list of dependencies are handled automatically by
17196 @code{gnatmake}. The Makefile is simply used to call gnatmake in each of
17197 the appropriate directories.
17198
17199 Note that you should also read the example on how to automatically
17200 create the list of directories
17201 (@ref{13d,,Automatically Creating a List of Directories})
17202 which might help you in case your project has a lot of subdirectories.
17203
17204 @example
17205 ## This Makefile is intended to be used with the following directory
17206 ## configuration:
17207 ## - The sources are split into a series of csc (computer software components)
17208 ## Each of these csc is put in its own directory.
17209 ## Their name are referenced by the directory names.
17210 ## They will be compiled into shared library (although this would also work
17211 ## with static libraries
17212 ## - The main program (and possibly other packages that do not belong to any
17213 ## csc is put in the top level directory (where the Makefile is).
17214 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
17215 ## \\_ second_csc (sources) __ lib (will contain the library)
17216 ## \\_ ...
17217 ## Although this Makefile is build for shared library, it is easy to modify
17218 ## to build partial link objects instead (modify the lines with -shared and
17219 ## gnatlink below)
17220 ##
17221 ## With this makefile, you can change any file in the system or add any new
17222 ## file, and everything will be recompiled correctly (only the relevant shared
17223 ## objects will be recompiled, and the main program will be re-linked).
17224
17225 # The list of computer software component for your project. This might be
17226 # generated automatically.
17227 CSC_LIST=aa bb cc
17228
17229 # Name of the main program (no extension)
17230 MAIN=main
17231
17232 # If we need to build objects with -fPIC, uncomment the following line
17233 #NEED_FPIC=-fPIC
17234
17235 # The following variable should give the directory containing libgnat.so
17236 # You can get this directory through 'gnatls -v'. This is usually the last
17237 # directory in the Object_Path.
17238 GLIB=...
17239
17240 # The directories for the libraries
17241 # (This macro expands the list of CSC to the list of shared libraries, you
17242 # could simply use the expanded form:
17243 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17244 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17245
17246 $@{MAIN@}: objects $@{LIB_DIR@}
17247 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17248 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17249
17250 objects::
17251 # recompile the sources
17252 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17253
17254 # Note: In a future version of GNAT, the following commands will be simplified
17255 # by a new tool, gnatmlib
17256 $@{LIB_DIR@}:
17257 mkdir -p $@{dir $@@ @}
17258 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17259 cd $@{dir $@@ @} && cp -f ../*.ali .
17260
17261 # The dependencies for the modules
17262 # Note that we have to force the expansion of *.o, since in some cases
17263 # make won't be able to do it itself.
17264 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17265 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17266 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17267
17268 # Make sure all of the shared libraries are in the path before starting the
17269 # program
17270 run::
17271 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17272
17273 clean::
17274 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17275 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17276 $@{RM@} $@{CSC_LIST:%=%/*.o@}
17277 $@{RM@} *.o *.ali $@{MAIN@}
17278 @end example
17279
17280 @node Automatically Creating a List of Directories,Generating the Command Line Switches,Using gnatmake in a Makefile,Using the GNU make Utility
17281 @anchor{gnat_ugn/building_executable_programs_with_gnat id50}@anchor{13e}@anchor{gnat_ugn/building_executable_programs_with_gnat automatically-creating-a-list-of-directories}@anchor{13d}
17282 @subsection Automatically Creating a List of Directories
17283
17284
17285 In most makefiles, you will have to specify a list of directories, and
17286 store it in a variable. For small projects, it is often easier to
17287 specify each of them by hand, since you then have full control over what
17288 is the proper order for these directories, which ones should be
17289 included.
17290
17291 However, in larger projects, which might involve hundreds of
17292 subdirectories, it might be more convenient to generate this list
17293 automatically.
17294
17295 The example below presents two methods. The first one, although less
17296 general, gives you more control over the list. It involves wildcard
17297 characters, that are automatically expanded by @code{make}. Its
17298 shortcoming is that you need to explicitly specify some of the
17299 organization of your project, such as for instance the directory tree
17300 depth, whether some directories are found in a separate tree, etc.
17301
17302 The second method is the most general one. It requires an external
17303 program, called @code{find}, which is standard on all Unix systems. All
17304 the directories found under a given root directory will be added to the
17305 list.
17306
17307 @example
17308 # The examples below are based on the following directory hierarchy:
17309 # All the directories can contain any number of files
17310 # ROOT_DIRECTORY -> a -> aa -> aaa
17311 # -> ab
17312 # -> ac
17313 # -> b -> ba -> baa
17314 # -> bb
17315 # -> bc
17316 # This Makefile creates a variable called DIRS, that can be reused any time
17317 # you need this list (see the other examples in this section)
17318
17319 # The root of your project's directory hierarchy
17320 ROOT_DIRECTORY=.
17321
17322 ####
17323 # First method: specify explicitly the list of directories
17324 # This allows you to specify any subset of all the directories you need.
17325 ####
17326
17327 DIRS := a/aa/ a/ab/ b/ba/
17328
17329 ####
17330 # Second method: use wildcards
17331 # Note that the argument(s) to wildcard below should end with a '/'.
17332 # Since wildcards also return file names, we have to filter them out
17333 # to avoid duplicate directory names.
17334 # We thus use make's `@w{`}dir`@w{`} and `@w{`}sort`@w{`} functions.
17335 # It sets DIRs to the following value (note that the directories aaa and baa
17336 # are not given, unless you change the arguments to wildcard).
17337 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
17338 ####
17339
17340 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
17341 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
17342
17343 ####
17344 # Third method: use an external program
17345 # This command is much faster if run on local disks, avoiding NFS slowdowns.
17346 # This is the most complete command: it sets DIRs to the following value:
17347 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
17348 ####
17349
17350 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
17351 @end example
17352
17353 @node Generating the Command Line Switches,Overcoming Command Line Length Limits,Automatically Creating a List of Directories,Using the GNU make Utility
17354 @anchor{gnat_ugn/building_executable_programs_with_gnat id51}@anchor{13f}@anchor{gnat_ugn/building_executable_programs_with_gnat generating-the-command-line-switches}@anchor{140}
17355 @subsection Generating the Command Line Switches
17356
17357
17358 Once you have created the list of directories as explained in the
17359 previous section (@ref{13d,,Automatically Creating a List of Directories}),
17360 you can easily generate the command line arguments to pass to gnatmake.
17361
17362 For the sake of completeness, this example assumes that the source path
17363 is not the same as the object path, and that you have two separate lists
17364 of directories.
17365
17366 @example
17367 # see "Automatically creating a list of directories" to create
17368 # these variables
17369 SOURCE_DIRS=
17370 OBJECT_DIRS=
17371
17372 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
17373 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
17374
17375 all:
17376 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
17377 @end example
17378
17379 @node Overcoming Command Line Length Limits,,Generating the Command Line Switches,Using the GNU make Utility
17380 @anchor{gnat_ugn/building_executable_programs_with_gnat overcoming-command-line-length-limits}@anchor{141}@anchor{gnat_ugn/building_executable_programs_with_gnat id52}@anchor{142}
17381 @subsection Overcoming Command Line Length Limits
17382
17383
17384 One problem that might be encountered on big projects is that many
17385 operating systems limit the length of the command line. It is thus hard to give
17386 gnatmake the list of source and object directories.
17387
17388 This example shows how you can set up environment variables, which will
17389 make @code{gnatmake} behave exactly as if the directories had been
17390 specified on the command line, but have a much higher length limit (or
17391 even none on most systems).
17392
17393 It assumes that you have created a list of directories in your Makefile,
17394 using one of the methods presented in
17395 @ref{13d,,Automatically Creating a List of Directories}.
17396 For the sake of completeness, we assume that the object
17397 path (where the ALI files are found) is different from the sources patch.
17398
17399 Note a small trick in the Makefile below: for efficiency reasons, we
17400 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
17401 expanded immediately by @code{make}. This way we overcome the standard
17402 make behavior which is to expand the variables only when they are
17403 actually used.
17404
17405 On Windows, if you are using the standard Windows command shell, you must
17406 replace colons with semicolons in the assignments to these variables.
17407
17408 @example
17409 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECTS_PATH.
17410 # This is the same thing as putting the -I arguments on the command line.
17411 # (the equivalent of using -aI on the command line would be to define
17412 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECTS_PATH).
17413 # You can of course have different values for these variables.
17414 #
17415 # Note also that we need to keep the previous values of these variables, since
17416 # they might have been set before running 'make' to specify where the GNAT
17417 # library is installed.
17418
17419 # see "Automatically creating a list of directories" to create these
17420 # variables
17421 SOURCE_DIRS=
17422 OBJECT_DIRS=
17423
17424 empty:=
17425 space:=$@{empty@} $@{empty@}
17426 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
17427 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
17428 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
17429 ADA_OBJECTS_PATH += $@{OBJECT_LIST@}
17430 export ADA_INCLUDE_PATH
17431 export ADA_OBJECTS_PATH
17432
17433 all:
17434 gnatmake main_unit
17435 @end example
17436
17437 @node GNAT Utility Programs,GNAT and Program Execution,Building Executable Programs with GNAT,Top
17438 @anchor{gnat_ugn/gnat_utility_programs doc}@anchor{143}@anchor{gnat_ugn/gnat_utility_programs gnat-utility-programs}@anchor{b}@anchor{gnat_ugn/gnat_utility_programs id1}@anchor{144}
17439 @chapter GNAT Utility Programs
17440
17441
17442 This chapter describes a number of utility programs:
17443
17444
17445
17446 @itemize *
17447
17448 @item
17449 @ref{20,,The File Cleanup Utility gnatclean}
17450
17451 @item
17452 @ref{21,,The GNAT Library Browser gnatls}
17453
17454 @item
17455 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
17456
17457 @item
17458 @ref{23,,The Ada to HTML Converter gnathtml}
17459 @end itemize
17460
17461 Other GNAT utilities are described elsewhere in this manual:
17462
17463
17464 @itemize *
17465
17466 @item
17467 @ref{59,,Handling Arbitrary File Naming Conventions with gnatname}
17468
17469 @item
17470 @ref{63,,File Name Krunching with gnatkr}
17471
17472 @item
17473 @ref{36,,Renaming Files with gnatchop}
17474
17475 @item
17476 @ref{17,,Preprocessing with gnatprep}
17477 @end itemize
17478
17479 @menu
17480 * The File Cleanup Utility gnatclean::
17481 * The GNAT Library Browser gnatls::
17482 * The Cross-Referencing Tools gnatxref and gnatfind::
17483 * The Ada to HTML Converter gnathtml::
17484
17485 @end menu
17486
17487 @node The File Cleanup Utility gnatclean,The GNAT Library Browser gnatls,,GNAT Utility Programs
17488 @anchor{gnat_ugn/gnat_utility_programs id2}@anchor{145}@anchor{gnat_ugn/gnat_utility_programs the-file-cleanup-utility-gnatclean}@anchor{20}
17489 @section The File Cleanup Utility @code{gnatclean}
17490
17491
17492 @geindex File cleanup tool
17493
17494 @geindex gnatclean
17495
17496 @code{gnatclean} is a tool that allows the deletion of files produced by the
17497 compiler, binder and linker, including ALI files, object files, tree files,
17498 expanded source files, library files, interface copy source files, binder
17499 generated files and executable files.
17500
17501 @menu
17502 * Running gnatclean::
17503 * Switches for gnatclean::
17504
17505 @end menu
17506
17507 @node Running gnatclean,Switches for gnatclean,,The File Cleanup Utility gnatclean
17508 @anchor{gnat_ugn/gnat_utility_programs running-gnatclean}@anchor{146}@anchor{gnat_ugn/gnat_utility_programs id3}@anchor{147}
17509 @subsection Running @code{gnatclean}
17510
17511
17512 The @code{gnatclean} command has the form:
17513
17514 @quotation
17515
17516 @example
17517 $ gnatclean switches names
17518 @end example
17519 @end quotation
17520
17521 where @code{names} is a list of source file names. Suffixes @code{.ads} and
17522 @code{adb} may be omitted. If a project file is specified using switch
17523 @code{-P}, then @code{names} may be completely omitted.
17524
17525 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17526 if switch @code{-c} is not specified, by the binder and
17527 the linker. In informative-only mode, specified by switch
17528 @code{-n}, the list of files that would have been deleted in
17529 normal mode is listed, but no file is actually deleted.
17530
17531 @node Switches for gnatclean,,Running gnatclean,The File Cleanup Utility gnatclean
17532 @anchor{gnat_ugn/gnat_utility_programs id4}@anchor{148}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatclean}@anchor{149}
17533 @subsection Switches for @code{gnatclean}
17534
17535
17536 @code{gnatclean} recognizes the following switches:
17537
17538 @geindex --version (gnatclean)
17539
17540
17541 @table @asis
17542
17543 @item @code{--version}
17544
17545 Display copyright and version, then exit disregarding all other options.
17546 @end table
17547
17548 @geindex --help (gnatclean)
17549
17550
17551 @table @asis
17552
17553 @item @code{--help}
17554
17555 If @code{--version} was not used, display usage, then exit disregarding
17556 all other options.
17557
17558 @item @code{--subdirs=@emph{subdir}}
17559
17560 Actual object directory of each project file is the subdirectory subdir of the
17561 object directory specified or defaulted in the project file.
17562
17563 @item @code{--unchecked-shared-lib-imports}
17564
17565 By default, shared library projects are not allowed to import static library
17566 projects. When this switch is used on the command line, this restriction is
17567 relaxed.
17568 @end table
17569
17570 @geindex -c (gnatclean)
17571
17572
17573 @table @asis
17574
17575 @item @code{-c}
17576
17577 Only attempt to delete the files produced by the compiler, not those produced
17578 by the binder or the linker. The files that are not to be deleted are library
17579 files, interface copy files, binder generated files and executable files.
17580 @end table
17581
17582 @geindex -D (gnatclean)
17583
17584
17585 @table @asis
17586
17587 @item @code{-D @emph{dir}}
17588
17589 Indicate that ALI and object files should normally be found in directory @code{dir}.
17590 @end table
17591
17592 @geindex -F (gnatclean)
17593
17594
17595 @table @asis
17596
17597 @item @code{-F}
17598
17599 When using project files, if some errors or warnings are detected during
17600 parsing and verbose mode is not in effect (no use of switch
17601 -v), then error lines start with the full path name of the project
17602 file, rather than its simple file name.
17603 @end table
17604
17605 @geindex -h (gnatclean)
17606
17607
17608 @table @asis
17609
17610 @item @code{-h}
17611
17612 Output a message explaining the usage of @code{gnatclean}.
17613 @end table
17614
17615 @geindex -n (gnatclean)
17616
17617
17618 @table @asis
17619
17620 @item @code{-n}
17621
17622 Informative-only mode. Do not delete any files. Output the list of the files
17623 that would have been deleted if this switch was not specified.
17624 @end table
17625
17626 @geindex -P (gnatclean)
17627
17628
17629 @table @asis
17630
17631 @item @code{-P@emph{project}}
17632
17633 Use project file @code{project}. Only one such switch can be used.
17634 When cleaning a project file, the files produced by the compilation of the
17635 immediate sources or inherited sources of the project files are to be
17636 deleted. This is not depending on the presence or not of executable names
17637 on the command line.
17638 @end table
17639
17640 @geindex -q (gnatclean)
17641
17642
17643 @table @asis
17644
17645 @item @code{-q}
17646
17647 Quiet output. If there are no errors, do not output anything, except in
17648 verbose mode (switch -v) or in informative-only mode
17649 (switch -n).
17650 @end table
17651
17652 @geindex -r (gnatclean)
17653
17654
17655 @table @asis
17656
17657 @item @code{-r}
17658
17659 When a project file is specified (using switch -P),
17660 clean all imported and extended project files, recursively. If this switch
17661 is not specified, only the files related to the main project file are to be
17662 deleted. This switch has no effect if no project file is specified.
17663 @end table
17664
17665 @geindex -v (gnatclean)
17666
17667
17668 @table @asis
17669
17670 @item @code{-v}
17671
17672 Verbose mode.
17673 @end table
17674
17675 @geindex -vP (gnatclean)
17676
17677
17678 @table @asis
17679
17680 @item @code{-vP@emph{x}}
17681
17682 Indicates the verbosity of the parsing of GNAT project files.
17683 @ref{de,,Switches Related to Project Files}.
17684 @end table
17685
17686 @geindex -X (gnatclean)
17687
17688
17689 @table @asis
17690
17691 @item @code{-X@emph{name}=@emph{value}}
17692
17693 Indicates that external variable @code{name} has the value @code{value}.
17694 The Project Manager will use this value for occurrences of
17695 @code{external(name)} when parsing the project file.
17696 See @ref{de,,Switches Related to Project Files}.
17697 @end table
17698
17699 @geindex -aO (gnatclean)
17700
17701
17702 @table @asis
17703
17704 @item @code{-aO@emph{dir}}
17705
17706 When searching for ALI and object files, look in directory @code{dir}.
17707 @end table
17708
17709 @geindex -I (gnatclean)
17710
17711
17712 @table @asis
17713
17714 @item @code{-I@emph{dir}}
17715
17716 Equivalent to @code{-aO@emph{dir}}.
17717 @end table
17718
17719 @geindex -I- (gnatclean)
17720
17721 @geindex Source files
17722 @geindex suppressing search
17723
17724
17725 @table @asis
17726
17727 @item @code{-I-}
17728
17729 Do not look for ALI or object files in the directory
17730 where @code{gnatclean} was invoked.
17731 @end table
17732
17733 @node The GNAT Library Browser gnatls,The Cross-Referencing Tools gnatxref and gnatfind,The File Cleanup Utility gnatclean,GNAT Utility Programs
17734 @anchor{gnat_ugn/gnat_utility_programs the-gnat-library-browser-gnatls}@anchor{21}@anchor{gnat_ugn/gnat_utility_programs id5}@anchor{14a}
17735 @section The GNAT Library Browser @code{gnatls}
17736
17737
17738 @geindex Library browser
17739
17740 @geindex gnatls
17741
17742 @code{gnatls} is a tool that outputs information about compiled
17743 units. It gives the relationship between objects, unit names and source
17744 files. It can also be used to check the source dependencies of a unit
17745 as well as various characteristics.
17746
17747 @menu
17748 * Running gnatls::
17749 * Switches for gnatls::
17750 * Example of gnatls Usage::
17751
17752 @end menu
17753
17754 @node Running gnatls,Switches for gnatls,,The GNAT Library Browser gnatls
17755 @anchor{gnat_ugn/gnat_utility_programs id6}@anchor{14b}@anchor{gnat_ugn/gnat_utility_programs running-gnatls}@anchor{14c}
17756 @subsection Running @code{gnatls}
17757
17758
17759 The @code{gnatls} command has the form
17760
17761 @quotation
17762
17763 @example
17764 $ gnatls switches object_or_ali_file
17765 @end example
17766 @end quotation
17767
17768 The main argument is the list of object or @code{ali} files
17769 (see @ref{42,,The Ada Library Information Files})
17770 for which information is requested.
17771
17772 In normal mode, without additional option, @code{gnatls} produces a
17773 four-column listing. Each line represents information for a specific
17774 object. The first column gives the full path of the object, the second
17775 column gives the name of the principal unit in this object, the third
17776 column gives the status of the source and the fourth column gives the
17777 full path of the source representing this unit.
17778 Here is a simple example of use:
17779
17780 @quotation
17781
17782 @example
17783 $ gnatls *.o
17784 ./demo1.o demo1 DIF demo1.adb
17785 ./demo2.o demo2 OK demo2.adb
17786 ./hello.o h1 OK hello.adb
17787 ./instr-child.o instr.child MOK instr-child.adb
17788 ./instr.o instr OK instr.adb
17789 ./tef.o tef DIF tef.adb
17790 ./text_io_example.o text_io_example OK text_io_example.adb
17791 ./tgef.o tgef DIF tgef.adb
17792 @end example
17793 @end quotation
17794
17795 The first line can be interpreted as follows: the main unit which is
17796 contained in
17797 object file @code{demo1.o} is demo1, whose main source is in
17798 @code{demo1.adb}. Furthermore, the version of the source used for the
17799 compilation of demo1 has been modified (DIF). Each source file has a status
17800 qualifier which can be:
17801
17802
17803 @table @asis
17804
17805 @item @emph{OK (unchanged)}
17806
17807 The version of the source file used for the compilation of the
17808 specified unit corresponds exactly to the actual source file.
17809
17810 @item @emph{MOK (slightly modified)}
17811
17812 The version of the source file used for the compilation of the
17813 specified unit differs from the actual source file but not enough to
17814 require recompilation. If you use gnatmake with the option
17815 @code{-m} (minimal recompilation), a file marked
17816 MOK will not be recompiled.
17817
17818 @item @emph{DIF (modified)}
17819
17820 No version of the source found on the path corresponds to the source
17821 used to build this object.
17822
17823 @item @emph{??? (file not found)}
17824
17825 No source file was found for this unit.
17826
17827 @item @emph{HID (hidden, unchanged version not first on PATH)}
17828
17829 The version of the source that corresponds exactly to the source used
17830 for compilation has been found on the path but it is hidden by another
17831 version of the same source that has been modified.
17832 @end table
17833
17834 @node Switches for gnatls,Example of gnatls Usage,Running gnatls,The GNAT Library Browser gnatls
17835 @anchor{gnat_ugn/gnat_utility_programs id7}@anchor{14d}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatls}@anchor{14e}
17836 @subsection Switches for @code{gnatls}
17837
17838
17839 @code{gnatls} recognizes the following switches:
17840
17841 @geindex --version (gnatls)
17842
17843
17844 @table @asis
17845
17846 @item @code{--version}
17847
17848 Display copyright and version, then exit disregarding all other options.
17849 @end table
17850
17851 @geindex --help (gnatls)
17852
17853
17854 @table @asis
17855
17856 @item @code{--help}
17857
17858 If @code{--version} was not used, display usage, then exit disregarding
17859 all other options.
17860 @end table
17861
17862 @geindex -a (gnatls)
17863
17864
17865 @table @asis
17866
17867 @item @code{-a}
17868
17869 Consider all units, including those of the predefined Ada library.
17870 Especially useful with @code{-d}.
17871 @end table
17872
17873 @geindex -d (gnatls)
17874
17875
17876 @table @asis
17877
17878 @item @code{-d}
17879
17880 List sources from which specified units depend on.
17881 @end table
17882
17883 @geindex -h (gnatls)
17884
17885
17886 @table @asis
17887
17888 @item @code{-h}
17889
17890 Output the list of options.
17891 @end table
17892
17893 @geindex -o (gnatls)
17894
17895
17896 @table @asis
17897
17898 @item @code{-o}
17899
17900 Only output information about object files.
17901 @end table
17902
17903 @geindex -s (gnatls)
17904
17905
17906 @table @asis
17907
17908 @item @code{-s}
17909
17910 Only output information about source files.
17911 @end table
17912
17913 @geindex -u (gnatls)
17914
17915
17916 @table @asis
17917
17918 @item @code{-u}
17919
17920 Only output information about compilation units.
17921 @end table
17922
17923 @geindex -files (gnatls)
17924
17925
17926 @table @asis
17927
17928 @item @code{-files=@emph{file}}
17929
17930 Take as arguments the files listed in text file @code{file}.
17931 Text file @code{file} may contain empty lines that are ignored.
17932 Each nonempty line should contain the name of an existing file.
17933 Several such switches may be specified simultaneously.
17934 @end table
17935
17936 @geindex -aO (gnatls)
17937
17938 @geindex -aI (gnatls)
17939
17940 @geindex -I (gnatls)
17941
17942 @geindex -I- (gnatls)
17943
17944
17945 @table @asis
17946
17947 @item @code{-aO@emph{dir}}, @code{-aI@emph{dir}}, @code{-I@emph{dir}}, @code{-I-}, @code{-nostdinc}
17948
17949 Source path manipulation. Same meaning as the equivalent @code{gnatmake}
17950 flags (@ref{dc,,Switches for gnatmake}).
17951 @end table
17952
17953 @geindex -aP (gnatls)
17954
17955
17956 @table @asis
17957
17958 @item @code{-aP@emph{dir}}
17959
17960 Add @code{dir} at the beginning of the project search dir.
17961 @end table
17962
17963 @geindex --RTS (gnatls)
17964
17965
17966 @table @asis
17967
17968 @item @code{--RTS=@emph{rts-path}}
17969
17970 Specifies the default location of the runtime library. Same meaning as the
17971 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
17972 @end table
17973
17974 @geindex -v (gnatls)
17975
17976
17977 @table @asis
17978
17979 @item @code{-v}
17980
17981 Verbose mode. Output the complete source, object and project paths. Do not use
17982 the default column layout but instead use long format giving as much as
17983 information possible on each requested units, including special
17984 characteristics such as:
17985
17986
17987 @itemize *
17988
17989 @item
17990 @emph{Preelaborable}: The unit is preelaborable in the Ada sense.
17991
17992 @item
17993 @emph{No_Elab_Code}: No elaboration code has been produced by the compiler for this unit.
17994
17995 @item
17996 @emph{Pure}: The unit is pure in the Ada sense.
17997
17998 @item
17999 @emph{Elaborate_Body}: The unit contains a pragma Elaborate_Body.
18000
18001 @item
18002 @emph{Remote_Types}: The unit contains a pragma Remote_Types.
18003
18004 @item
18005 @emph{Shared_Passive}: The unit contains a pragma Shared_Passive.
18006
18007 @item
18008 @emph{Predefined}: This unit is part of the predefined environment and cannot be modified
18009 by the user.
18010
18011 @item
18012 @emph{Remote_Call_Interface}: The unit contains a pragma Remote_Call_Interface.
18013 @end itemize
18014 @end table
18015
18016 @node Example of gnatls Usage,,Switches for gnatls,The GNAT Library Browser gnatls
18017 @anchor{gnat_ugn/gnat_utility_programs id8}@anchor{14f}@anchor{gnat_ugn/gnat_utility_programs example-of-gnatls-usage}@anchor{150}
18018 @subsection Example of @code{gnatls} Usage
18019
18020
18021 Example of using the verbose switch. Note how the source and
18022 object paths are affected by the -I switch.
18023
18024 @quotation
18025
18026 @example
18027 $ gnatls -v -I.. demo1.o
18028
18029 GNATLS 5.03w (20041123-34)
18030 Copyright 1997-2004 Free Software Foundation, Inc.
18031
18032 Source Search Path:
18033 <Current_Directory>
18034 ../
18035 /home/comar/local/adainclude/
18036
18037 Object Search Path:
18038 <Current_Directory>
18039 ../
18040 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18041
18042 Project Search Path:
18043 <Current_Directory>
18044 /home/comar/local/lib/gnat/
18045
18046 ./demo1.o
18047 Unit =>
18048 Name => demo1
18049 Kind => subprogram body
18050 Flags => No_Elab_Code
18051 Source => demo1.adb modified
18052 @end example
18053 @end quotation
18054
18055 The following is an example of use of the dependency list.
18056 Note the use of the -s switch
18057 which gives a straight list of source files. This can be useful for
18058 building specialized scripts.
18059
18060 @quotation
18061
18062 @example
18063 $ gnatls -d demo2.o
18064 ./demo2.o demo2 OK demo2.adb
18065 OK gen_list.ads
18066 OK gen_list.adb
18067 OK instr.ads
18068 OK instr-child.ads
18069
18070 $ gnatls -d -s -a demo1.o
18071 demo1.adb
18072 /home/comar/local/adainclude/ada.ads
18073 /home/comar/local/adainclude/a-finali.ads
18074 /home/comar/local/adainclude/a-filico.ads
18075 /home/comar/local/adainclude/a-stream.ads
18076 /home/comar/local/adainclude/a-tags.ads
18077 gen_list.ads
18078 gen_list.adb
18079 /home/comar/local/adainclude/gnat.ads
18080 /home/comar/local/adainclude/g-io.ads
18081 instr.ads
18082 /home/comar/local/adainclude/system.ads
18083 /home/comar/local/adainclude/s-exctab.ads
18084 /home/comar/local/adainclude/s-finimp.ads
18085 /home/comar/local/adainclude/s-finroo.ads
18086 /home/comar/local/adainclude/s-secsta.ads
18087 /home/comar/local/adainclude/s-stalib.ads
18088 /home/comar/local/adainclude/s-stoele.ads
18089 /home/comar/local/adainclude/s-stratt.ads
18090 /home/comar/local/adainclude/s-tasoli.ads
18091 /home/comar/local/adainclude/s-unstyp.ads
18092 /home/comar/local/adainclude/unchconv.ads
18093 @end example
18094 @end quotation
18095
18096 @node The Cross-Referencing Tools gnatxref and gnatfind,The Ada to HTML Converter gnathtml,The GNAT Library Browser gnatls,GNAT Utility Programs
18097 @anchor{gnat_ugn/gnat_utility_programs the-cross-referencing-tools-gnatxref-and-gnatfind}@anchor{22}@anchor{gnat_ugn/gnat_utility_programs id9}@anchor{151}
18098 @section The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
18099
18100
18101 @geindex gnatxref
18102
18103 @geindex gnatfind
18104
18105 The compiler generates cross-referencing information (unless
18106 you set the @code{-gnatx} switch), which are saved in the @code{.ali} files.
18107 This information indicates where in the source each entity is declared and
18108 referenced. Note that entities in package Standard are not included, but
18109 entities in all other predefined units are included in the output.
18110
18111 Before using any of these two tools, you need to compile successfully your
18112 application, so that GNAT gets a chance to generate the cross-referencing
18113 information.
18114
18115 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
18116 information to provide the user with the capability to easily locate the
18117 declaration and references to an entity. These tools are quite similar,
18118 the difference being that @code{gnatfind} is intended for locating
18119 definitions and/or references to a specified entity or entities, whereas
18120 @code{gnatxref} is oriented to generating a full report of all
18121 cross-references.
18122
18123 To use these tools, you must not compile your application using the
18124 @code{-gnatx} switch on the @code{gnatmake} command line
18125 (see @ref{1b,,Building with gnatmake}). Otherwise, cross-referencing
18126 information will not be generated.
18127
18128 @menu
18129 * gnatxref Switches::
18130 * gnatfind Switches::
18131 * Configuration Files for gnatxref and gnatfind::
18132 * Regular Expressions in gnatfind and gnatxref::
18133 * Examples of gnatxref Usage::
18134 * Examples of gnatfind Usage::
18135
18136 @end menu
18137
18138 @node gnatxref Switches,gnatfind Switches,,The Cross-Referencing Tools gnatxref and gnatfind
18139 @anchor{gnat_ugn/gnat_utility_programs id10}@anchor{152}@anchor{gnat_ugn/gnat_utility_programs gnatxref-switches}@anchor{153}
18140 @subsection @code{gnatxref} Switches
18141
18142
18143 The command invocation for @code{gnatxref} is:
18144
18145 @quotation
18146
18147 @example
18148 $ gnatxref [ switches ] sourcefile1 [ sourcefile2 ... ]
18149 @end example
18150 @end quotation
18151
18152 where
18153
18154
18155 @table @asis
18156
18157 @item @code{sourcefile1} [, @code{sourcefile2} ...]
18158
18159 identify the source files for which a report is to be generated. The
18160 @code{with}ed units will be processed too. You must provide at least one file.
18161
18162 These file names are considered to be regular expressions, so for instance
18163 specifying @code{source*.adb} is the same as giving every file in the current
18164 directory whose name starts with @code{source} and whose extension is
18165 @code{adb}.
18166
18167 You shouldn't specify any directory name, just base names. @code{gnatxref}
18168 and @code{gnatfind} will be able to locate these files by themselves using
18169 the source path. If you specify directories, no result is produced.
18170 @end table
18171
18172 The following switches are available for @code{gnatxref}:
18173
18174 @geindex --version (gnatxref)
18175
18176
18177 @table @asis
18178
18179 @item @code{--version}
18180
18181 Display copyright and version, then exit disregarding all other options.
18182 @end table
18183
18184 @geindex --help (gnatxref)
18185
18186
18187 @table @asis
18188
18189 @item @code{--help}
18190
18191 If @code{--version} was not used, display usage, then exit disregarding
18192 all other options.
18193 @end table
18194
18195 @geindex -a (gnatxref)
18196
18197
18198 @table @asis
18199
18200 @item @code{-a}
18201
18202 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
18203 the read-only files found in the library search path. Otherwise, these files
18204 will be ignored. This option can be used to protect Gnat sources or your own
18205 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
18206 much faster, and their output much smaller. Read-only here refers to access
18207 or permissions status in the file system for the current user.
18208 @end table
18209
18210 @geindex -aIDIR (gnatxref)
18211
18212
18213 @table @asis
18214
18215 @item @code{-aI@emph{DIR}}
18216
18217 When looking for source files also look in directory DIR. The order in which
18218 source file search is undertaken is the same as for @code{gnatmake}.
18219 @end table
18220
18221 @geindex -aODIR (gnatxref)
18222
18223
18224 @table @asis
18225
18226 @item @code{aO@emph{DIR}}
18227
18228 When -searching for library and object files, look in directory
18229 DIR. The order in which library files are searched is the same as for
18230 @code{gnatmake}.
18231 @end table
18232
18233 @geindex -nostdinc (gnatxref)
18234
18235
18236 @table @asis
18237
18238 @item @code{-nostdinc}
18239
18240 Do not look for sources in the system default directory.
18241 @end table
18242
18243 @geindex -nostdlib (gnatxref)
18244
18245
18246 @table @asis
18247
18248 @item @code{-nostdlib}
18249
18250 Do not look for library files in the system default directory.
18251 @end table
18252
18253 @geindex --ext (gnatxref)
18254
18255
18256 @table @asis
18257
18258 @item @code{--ext=@emph{extension}}
18259
18260 Specify an alternate ali file extension. The default is @code{ali} and other
18261 extensions (e.g. @code{gli} for C/C++ sources) may be specified via this switch.
18262 Note that if this switch overrides the default, only the new extension will
18263 be considered.
18264 @end table
18265
18266 @geindex --RTS (gnatxref)
18267
18268
18269 @table @asis
18270
18271 @item @code{--RTS=@emph{rts-path}}
18272
18273 Specifies the default location of the runtime library. Same meaning as the
18274 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18275 @end table
18276
18277 @geindex -d (gnatxref)
18278
18279
18280 @table @asis
18281
18282 @item @code{-d}
18283
18284 If this switch is set @code{gnatxref} will output the parent type
18285 reference for each matching derived types.
18286 @end table
18287
18288 @geindex -f (gnatxref)
18289
18290
18291 @table @asis
18292
18293 @item @code{-f}
18294
18295 If this switch is set, the output file names will be preceded by their
18296 directory (if the file was found in the search path). If this switch is
18297 not set, the directory will not be printed.
18298 @end table
18299
18300 @geindex -g (gnatxref)
18301
18302
18303 @table @asis
18304
18305 @item @code{-g}
18306
18307 If this switch is set, information is output only for library-level
18308 entities, ignoring local entities. The use of this switch may accelerate
18309 @code{gnatfind} and @code{gnatxref}.
18310 @end table
18311
18312 @geindex -IDIR (gnatxref)
18313
18314
18315 @table @asis
18316
18317 @item @code{-I@emph{DIR}}
18318
18319 Equivalent to @code{-aODIR -aIDIR}.
18320 @end table
18321
18322 @geindex -pFILE (gnatxref)
18323
18324
18325 @table @asis
18326
18327 @item @code{-p@emph{FILE}}
18328
18329 Specify a configuration file to use to list the source and object directories.
18330
18331 If a file is specified, then the content of the source directory and object
18332 directory lines are added as if they had been specified respectively
18333 by @code{-aI} and @code{-aO}.
18334
18335 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18336 of this configuration file.
18337
18338 @item @code{-u}
18339
18340 Output only unused symbols. This may be really useful if you give your
18341 main compilation unit on the command line, as @code{gnatxref} will then
18342 display every unused entity and 'with'ed package.
18343
18344 @item @code{-v}
18345
18346 Instead of producing the default output, @code{gnatxref} will generate a
18347 @code{tags} file that can be used by vi. For examples how to use this
18348 feature, see @ref{155,,Examples of gnatxref Usage}. The tags file is output
18349 to the standard output, thus you will have to redirect it to a file.
18350 @end table
18351
18352 All these switches may be in any order on the command line, and may even
18353 appear after the file names. They need not be separated by spaces, thus
18354 you can say @code{gnatxref -ag} instead of @code{gnatxref -a -g}.
18355
18356 @node gnatfind Switches,Configuration Files for gnatxref and gnatfind,gnatxref Switches,The Cross-Referencing Tools gnatxref and gnatfind
18357 @anchor{gnat_ugn/gnat_utility_programs id11}@anchor{156}@anchor{gnat_ugn/gnat_utility_programs gnatfind-switches}@anchor{157}
18358 @subsection @code{gnatfind} Switches
18359
18360
18361 The command invocation for @code{gnatfind} is:
18362
18363 @quotation
18364
18365 @example
18366 $ gnatfind [ switches ] pattern[:sourcefile[:line[:column]]]
18367 [file1 file2 ...]
18368 @end example
18369 @end quotation
18370
18371 with the following iterpretation of the command arguments:
18372
18373
18374 @table @asis
18375
18376 @item @emph{pattern}
18377
18378 An entity will be output only if it matches the regular expression found
18379 in @emph{pattern}, see @ref{158,,Regular Expressions in gnatfind and gnatxref}.
18380
18381 Omitting the pattern is equivalent to specifying @code{*}, which
18382 will match any entity. Note that if you do not provide a pattern, you
18383 have to provide both a sourcefile and a line.
18384
18385 Entity names are given in Latin-1, with uppercase/lowercase equivalence
18386 for matching purposes. At the current time there is no support for
18387 8-bit codes other than Latin-1, or for wide characters in identifiers.
18388
18389 @item @emph{sourcefile}
18390
18391 @code{gnatfind} will look for references, bodies or declarations
18392 of symbols referenced in @code{sourcefile}, at line @code{line}
18393 and column @code{column}. See @ref{159,,Examples of gnatfind Usage}
18394 for syntax examples.
18395
18396 @item @emph{line}
18397
18398 A decimal integer identifying the line number containing
18399 the reference to the entity (or entities) to be located.
18400
18401 @item @emph{column}
18402
18403 A decimal integer identifying the exact location on the
18404 line of the first character of the identifier for the
18405 entity reference. Columns are numbered from 1.
18406
18407 @item @emph{file1 file2 ...}
18408
18409 The search will be restricted to these source files. If none are given, then
18410 the search will be conducted for every library file in the search path.
18411 These files must appear only after the pattern or sourcefile.
18412
18413 These file names are considered to be regular expressions, so for instance
18414 specifying @code{source*.adb} is the same as giving every file in the current
18415 directory whose name starts with @code{source} and whose extension is
18416 @code{adb}.
18417
18418 The location of the spec of the entity will always be displayed, even if it
18419 isn't in one of @code{file1}, @code{file2}, ... The
18420 occurrences of the entity in the separate units of the ones given on the
18421 command line will also be displayed.
18422
18423 Note that if you specify at least one file in this part, @code{gnatfind} may
18424 sometimes not be able to find the body of the subprograms.
18425 @end table
18426
18427 At least one of 'sourcefile' or 'pattern' has to be present on
18428 the command line.
18429
18430 The following switches are available:
18431
18432 @geindex --version (gnatfind)
18433
18434
18435 @table @asis
18436
18437 @item @code{--version}
18438
18439 Display copyright and version, then exit disregarding all other options.
18440 @end table
18441
18442 @geindex --help (gnatfind)
18443
18444
18445 @table @asis
18446
18447 @item @code{--help}
18448
18449 If @code{--version} was not used, display usage, then exit disregarding
18450 all other options.
18451 @end table
18452
18453 @geindex -a (gnatfind)
18454
18455
18456 @table @asis
18457
18458 @item @code{-a}
18459
18460 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
18461 the read-only files found in the library search path. Otherwise, these files
18462 will be ignored. This option can be used to protect Gnat sources or your own
18463 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
18464 much faster, and their output much smaller. Read-only here refers to access
18465 or permission status in the file system for the current user.
18466 @end table
18467
18468 @geindex -aIDIR (gnatfind)
18469
18470
18471 @table @asis
18472
18473 @item @code{-aI@emph{DIR}}
18474
18475 When looking for source files also look in directory DIR. The order in which
18476 source file search is undertaken is the same as for @code{gnatmake}.
18477 @end table
18478
18479 @geindex -aODIR (gnatfind)
18480
18481
18482 @table @asis
18483
18484 @item @code{-aO@emph{DIR}}
18485
18486 When searching for library and object files, look in directory
18487 DIR. The order in which library files are searched is the same as for
18488 @code{gnatmake}.
18489 @end table
18490
18491 @geindex -nostdinc (gnatfind)
18492
18493
18494 @table @asis
18495
18496 @item @code{-nostdinc}
18497
18498 Do not look for sources in the system default directory.
18499 @end table
18500
18501 @geindex -nostdlib (gnatfind)
18502
18503
18504 @table @asis
18505
18506 @item @code{-nostdlib}
18507
18508 Do not look for library files in the system default directory.
18509 @end table
18510
18511 @geindex --ext (gnatfind)
18512
18513
18514 @table @asis
18515
18516 @item @code{--ext=@emph{extension}}
18517
18518 Specify an alternate ali file extension. The default is @code{ali} and other
18519 extensions may be specified via this switch. Note that if this switch
18520 overrides the default, only the new extension will be considered.
18521 @end table
18522
18523 @geindex --RTS (gnatfind)
18524
18525
18526 @table @asis
18527
18528 @item @code{--RTS=@emph{rts-path}}
18529
18530 Specifies the default location of the runtime library. Same meaning as the
18531 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18532 @end table
18533
18534 @geindex -d (gnatfind)
18535
18536
18537 @table @asis
18538
18539 @item @code{-d}
18540
18541 If this switch is set, then @code{gnatfind} will output the parent type
18542 reference for each matching derived types.
18543 @end table
18544
18545 @geindex -e (gnatfind)
18546
18547
18548 @table @asis
18549
18550 @item @code{-e}
18551
18552 By default, @code{gnatfind} accept the simple regular expression set for
18553 @code{pattern}. If this switch is set, then the pattern will be
18554 considered as full Unix-style regular expression.
18555 @end table
18556
18557 @geindex -f (gnatfind)
18558
18559
18560 @table @asis
18561
18562 @item @code{-f}
18563
18564 If this switch is set, the output file names will be preceded by their
18565 directory (if the file was found in the search path). If this switch is
18566 not set, the directory will not be printed.
18567 @end table
18568
18569 @geindex -g (gnatfind)
18570
18571
18572 @table @asis
18573
18574 @item @code{-g}
18575
18576 If this switch is set, information is output only for library-level
18577 entities, ignoring local entities. The use of this switch may accelerate
18578 @code{gnatfind} and @code{gnatxref}.
18579 @end table
18580
18581 @geindex -IDIR (gnatfind)
18582
18583
18584 @table @asis
18585
18586 @item @code{-I@emph{DIR}}
18587
18588 Equivalent to @code{-aODIR -aIDIR}.
18589 @end table
18590
18591 @geindex -pFILE (gnatfind)
18592
18593
18594 @table @asis
18595
18596 @item @code{-p@emph{FILE}}
18597
18598 Specify a configuration file to use to list the source and object directories.
18599
18600 If a file is specified, then the content of the source directory and object
18601 directory lines are added as if they had been specified respectively
18602 by @code{-aI} and @code{-aO}.
18603
18604 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18605 of this configuration file.
18606 @end table
18607
18608 @geindex -r (gnatfind)
18609
18610
18611 @table @asis
18612
18613 @item @code{-r}
18614
18615 By default, @code{gnatfind} will output only the information about the
18616 declaration, body or type completion of the entities. If this switch is
18617 set, the @code{gnatfind} will locate every reference to the entities in
18618 the files specified on the command line (or in every file in the search
18619 path if no file is given on the command line).
18620 @end table
18621
18622 @geindex -s (gnatfind)
18623
18624
18625 @table @asis
18626
18627 @item @code{-s}
18628
18629 If this switch is set, then @code{gnatfind} will output the content
18630 of the Ada source file lines were the entity was found.
18631 @end table
18632
18633 @geindex -t (gnatfind)
18634
18635
18636 @table @asis
18637
18638 @item @code{-t}
18639
18640 If this switch is set, then @code{gnatfind} will output the type hierarchy for
18641 the specified type. It act like -d option but recursively from parent
18642 type to parent type. When this switch is set it is not possible to
18643 specify more than one file.
18644 @end table
18645
18646 All these switches may be in any order on the command line, and may even
18647 appear after the file names. They need not be separated by spaces, thus
18648 you can say @code{gnatxref -ag} instead of
18649 @code{gnatxref -a -g}.
18650
18651 As stated previously, @code{gnatfind} will search in every directory in the
18652 search path. You can force it to look only in the current directory if
18653 you specify @code{*} at the end of the command line.
18654
18655 @node Configuration Files for gnatxref and gnatfind,Regular Expressions in gnatfind and gnatxref,gnatfind Switches,The Cross-Referencing Tools gnatxref and gnatfind
18656 @anchor{gnat_ugn/gnat_utility_programs configuration-files-for-gnatxref-and-gnatfind}@anchor{154}@anchor{gnat_ugn/gnat_utility_programs id12}@anchor{15a}
18657 @subsection Configuration Files for @code{gnatxref} and @code{gnatfind}
18658
18659
18660 Configuration files are used by @code{gnatxref} and @code{gnatfind} to specify
18661 the list of source and object directories to consider. They can be
18662 specified via the @code{-p} switch.
18663
18664 The following lines can be included, in any order in the file:
18665
18666
18667 @itemize *
18668
18669 @item
18670
18671 @table @asis
18672
18673 @item @emph{src_dir=DIR}
18674
18675 [default: @code{"./"}].
18676 Specifies a directory where to look for source files. Multiple @code{src_dir}
18677 lines can be specified and they will be searched in the order they
18678 are specified.
18679 @end table
18680
18681 @item
18682
18683 @table @asis
18684
18685 @item @emph{obj_dir=DIR}
18686
18687 [default: @code{"./"}].
18688 Specifies a directory where to look for object and library files. Multiple
18689 @code{obj_dir} lines can be specified, and they will be searched in the order
18690 they are specified
18691 @end table
18692 @end itemize
18693
18694 Any other line will be silently ignored.
18695
18696 @node Regular Expressions in gnatfind and gnatxref,Examples of gnatxref Usage,Configuration Files for gnatxref and gnatfind,The Cross-Referencing Tools gnatxref and gnatfind
18697 @anchor{gnat_ugn/gnat_utility_programs id13}@anchor{15b}@anchor{gnat_ugn/gnat_utility_programs regular-expressions-in-gnatfind-and-gnatxref}@anchor{158}
18698 @subsection Regular Expressions in @code{gnatfind} and @code{gnatxref}
18699
18700
18701 As specified in the section about @code{gnatfind}, the pattern can be a
18702 regular expression. Two kinds of regular expressions
18703 are recognized:
18704
18705
18706 @itemize *
18707
18708 @item
18709
18710 @table @asis
18711
18712 @item @emph{Globbing pattern}
18713
18714 These are the most common regular expression. They are the same as are
18715 generally used in a Unix shell command line, or in a DOS session.
18716
18717 Here is a more formal grammar:
18718
18719 @example
18720 regexp ::= term
18721 term ::= elmt -- matches elmt
18722 term ::= elmt elmt -- concatenation (elmt then elmt)
18723 term ::= * -- any string of 0 or more characters
18724 term ::= ? -- matches any character
18725 term ::= [char @{char@}] -- matches any character listed
18726 term ::= [char - char] -- matches any character in range
18727 @end example
18728 @end table
18729
18730 @item
18731
18732 @table @asis
18733
18734 @item @emph{Full regular expression}
18735
18736 The second set of regular expressions is much more powerful. This is the
18737 type of regular expressions recognized by utilities such as @code{grep}.
18738
18739 The following is the form of a regular expression, expressed in same BNF
18740 style as is found in the Ada Reference Manual:
18741
18742 @example
18743 regexp ::= term @{| term@} -- alternation (term or term ...)
18744
18745 term ::= item @{item@} -- concatenation (item then item)
18746
18747 item ::= elmt -- match elmt
18748 item ::= elmt * -- zero or more elmt's
18749 item ::= elmt + -- one or more elmt's
18750 item ::= elmt ? -- matches elmt or nothing
18751
18752 elmt ::= nschar -- matches given character
18753 elmt ::= [nschar @{nschar@}] -- matches any character listed
18754 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
18755 elmt ::= [char - char] -- matches chars in given range
18756 elmt ::= \\ char -- matches given character
18757 elmt ::= . -- matches any single character
18758 elmt ::= ( regexp ) -- parens used for grouping
18759
18760 char ::= any character, including special characters
18761 nschar ::= any character except ()[].*+?^
18762 @end example
18763
18764 Here are a few examples:
18765
18766 @quotation
18767
18768
18769 @table @asis
18770
18771 @item @code{abcde|fghi}
18772
18773 will match any of the two strings @code{abcde} and @code{fghi},
18774
18775 @item @code{abc*d}
18776
18777 will match any string like @code{abd}, @code{abcd}, @code{abccd},
18778 @code{abcccd}, and so on,
18779
18780 @item @code{[a-z]+}
18781
18782 will match any string which has only lowercase characters in it (and at
18783 least one character.
18784 @end table
18785 @end quotation
18786 @end table
18787 @end itemize
18788
18789 @node Examples of gnatxref Usage,Examples of gnatfind Usage,Regular Expressions in gnatfind and gnatxref,The Cross-Referencing Tools gnatxref and gnatfind
18790 @anchor{gnat_ugn/gnat_utility_programs examples-of-gnatxref-usage}@anchor{155}@anchor{gnat_ugn/gnat_utility_programs id14}@anchor{15c}
18791 @subsection Examples of @code{gnatxref} Usage
18792
18793
18794 @menu
18795 * General Usage::
18796 * Using gnatxref with vi::
18797
18798 @end menu
18799
18800 @node General Usage,Using gnatxref with vi,,Examples of gnatxref Usage
18801 @anchor{gnat_ugn/gnat_utility_programs general-usage}@anchor{15d}
18802 @subsubsection General Usage
18803
18804
18805 For the following examples, we will consider the following units:
18806
18807 @quotation
18808
18809 @example
18810 main.ads:
18811 1: with Bar;
18812 2: package Main is
18813 3: procedure Foo (B : in Integer);
18814 4: C : Integer;
18815 5: private
18816 6: D : Integer;
18817 7: end Main;
18818
18819 main.adb:
18820 1: package body Main is
18821 2: procedure Foo (B : in Integer) is
18822 3: begin
18823 4: C := B;
18824 5: D := B;
18825 6: Bar.Print (B);
18826 7: Bar.Print (C);
18827 8: end Foo;
18828 9: end Main;
18829
18830 bar.ads:
18831 1: package Bar is
18832 2: procedure Print (B : Integer);
18833 3: end bar;
18834 @end example
18835 @end quotation
18836
18837 The first thing to do is to recompile your application (for instance, in
18838 that case just by doing a @code{gnatmake main}, so that GNAT generates
18839 the cross-referencing information.
18840 You can then issue any of the following commands:
18841
18842 @quotation
18843
18844
18845 @itemize *
18846
18847 @item
18848 @code{gnatxref main.adb}
18849 @code{gnatxref} generates cross-reference information for main.adb
18850 and every unit 'with'ed by main.adb.
18851
18852 The output would be:
18853
18854 @quotation
18855
18856 @example
18857 B Type: Integer
18858 Decl: bar.ads 2:22
18859 B Type: Integer
18860 Decl: main.ads 3:20
18861 Body: main.adb 2:20
18862 Ref: main.adb 4:13 5:13 6:19
18863 Bar Type: Unit
18864 Decl: bar.ads 1:9
18865 Ref: main.adb 6:8 7:8
18866 main.ads 1:6
18867 C Type: Integer
18868 Decl: main.ads 4:5
18869 Modi: main.adb 4:8
18870 Ref: main.adb 7:19
18871 D Type: Integer
18872 Decl: main.ads 6:5
18873 Modi: main.adb 5:8
18874 Foo Type: Unit
18875 Decl: main.ads 3:15
18876 Body: main.adb 2:15
18877 Main Type: Unit
18878 Decl: main.ads 2:9
18879 Body: main.adb 1:14
18880 Print Type: Unit
18881 Decl: bar.ads 2:15
18882 Ref: main.adb 6:12 7:12
18883 @end example
18884 @end quotation
18885
18886 This shows that the entity @code{Main} is declared in main.ads, line 2, column 9,
18887 its body is in main.adb, line 1, column 14 and is not referenced any where.
18888
18889 The entity @code{Print} is declared in @code{bar.ads}, line 2, column 15 and it
18890 is referenced in @code{main.adb}, line 6 column 12 and line 7 column 12.
18891
18892 @item
18893 @code{gnatxref package1.adb package2.ads}
18894 @code{gnatxref} will generates cross-reference information for
18895 @code{package1.adb}, @code{package2.ads} and any other package @code{with}ed by any
18896 of these.
18897 @end itemize
18898 @end quotation
18899
18900 @node Using gnatxref with vi,,General Usage,Examples of gnatxref Usage
18901 @anchor{gnat_ugn/gnat_utility_programs using-gnatxref-with-vi}@anchor{15e}
18902 @subsubsection Using @code{gnatxref} with @code{vi}
18903
18904
18905 @code{gnatxref} can generate a tags file output, which can be used
18906 directly from @code{vi}. Note that the standard version of @code{vi}
18907 will not work properly with overloaded symbols. Consider using another
18908 free implementation of @code{vi}, such as @code{vim}.
18909
18910 @quotation
18911
18912 @example
18913 $ gnatxref -v gnatfind.adb > tags
18914 @end example
18915 @end quotation
18916
18917 The following command will generate the tags file for @code{gnatfind} itself
18918 (if the sources are in the search path!):
18919
18920 @quotation
18921
18922 @example
18923 $ gnatxref -v gnatfind.adb > tags
18924 @end example
18925 @end quotation
18926
18927 From @code{vi}, you can then use the command @code{:tag @emph{entity}}
18928 (replacing @code{entity} by whatever you are looking for), and vi will
18929 display a new file with the corresponding declaration of entity.
18930
18931 @node Examples of gnatfind Usage,,Examples of gnatxref Usage,The Cross-Referencing Tools gnatxref and gnatfind
18932 @anchor{gnat_ugn/gnat_utility_programs id15}@anchor{15f}@anchor{gnat_ugn/gnat_utility_programs examples-of-gnatfind-usage}@anchor{159}
18933 @subsection Examples of @code{gnatfind} Usage
18934
18935
18936
18937 @itemize *
18938
18939 @item
18940 @code{gnatfind -f xyz:main.adb}
18941 Find declarations for all entities xyz referenced at least once in
18942 main.adb. The references are search in every library file in the search
18943 path.
18944
18945 The directories will be printed as well (as the @code{-f}
18946 switch is set)
18947
18948 The output will look like:
18949
18950 @quotation
18951
18952 @example
18953 directory/main.ads:106:14: xyz <= declaration
18954 directory/main.adb:24:10: xyz <= body
18955 directory/foo.ads:45:23: xyz <= declaration
18956 @end example
18957 @end quotation
18958
18959 I.e., one of the entities xyz found in main.adb is declared at
18960 line 12 of main.ads (and its body is in main.adb), and another one is
18961 declared at line 45 of foo.ads
18962
18963 @item
18964 @code{gnatfind -fs xyz:main.adb}
18965 This is the same command as the previous one, but @code{gnatfind} will
18966 display the content of the Ada source file lines.
18967
18968 The output will look like:
18969
18970 @example
18971 directory/main.ads:106:14: xyz <= declaration
18972 procedure xyz;
18973 directory/main.adb:24:10: xyz <= body
18974 procedure xyz is
18975 directory/foo.ads:45:23: xyz <= declaration
18976 xyz : Integer;
18977 @end example
18978
18979 This can make it easier to find exactly the location your are looking
18980 for.
18981
18982 @item
18983 @code{gnatfind -r "*x*":main.ads:123 foo.adb}
18984 Find references to all entities containing an x that are
18985 referenced on line 123 of main.ads.
18986 The references will be searched only in main.ads and foo.adb.
18987
18988 @item
18989 @code{gnatfind main.ads:123}
18990 Find declarations and bodies for all entities that are referenced on
18991 line 123 of main.ads.
18992
18993 This is the same as @code{gnatfind "*":main.adb:123`}
18994
18995 @item
18996 @code{gnatfind mydir/main.adb:123:45}
18997 Find the declaration for the entity referenced at column 45 in
18998 line 123 of file main.adb in directory mydir. Note that it
18999 is usual to omit the identifier name when the column is given,
19000 since the column position identifies a unique reference.
19001
19002 The column has to be the beginning of the identifier, and should not
19003 point to any character in the middle of the identifier.
19004 @end itemize
19005
19006 @node The Ada to HTML Converter gnathtml,,The Cross-Referencing Tools gnatxref and gnatfind,GNAT Utility Programs
19007 @anchor{gnat_ugn/gnat_utility_programs the-ada-to-html-converter-gnathtml}@anchor{23}@anchor{gnat_ugn/gnat_utility_programs id16}@anchor{160}
19008 @section The Ada to HTML Converter @code{gnathtml}
19009
19010
19011 @geindex gnathtml
19012
19013 @code{gnathtml} is a Perl script that allows Ada source files to be browsed using
19014 standard Web browsers. For installation information, see @ref{161,,Installing gnathtml}.
19015
19016 Ada reserved keywords are highlighted in a bold font and Ada comments in
19017 a blue font. Unless your program was compiled with the gcc @code{-gnatx}
19018 switch to suppress the generation of cross-referencing information, user
19019 defined variables and types will appear in a different color; you will
19020 be able to click on any identifier and go to its declaration.
19021
19022 @menu
19023 * Invoking gnathtml::
19024 * Installing gnathtml::
19025
19026 @end menu
19027
19028 @node Invoking gnathtml,Installing gnathtml,,The Ada to HTML Converter gnathtml
19029 @anchor{gnat_ugn/gnat_utility_programs invoking-gnathtml}@anchor{162}@anchor{gnat_ugn/gnat_utility_programs id17}@anchor{163}
19030 @subsection Invoking @code{gnathtml}
19031
19032
19033 The command line is as follows:
19034
19035 @quotation
19036
19037 @example
19038 $ perl gnathtml.pl [ switches ] ada-files
19039 @end example
19040 @end quotation
19041
19042 You can specify as many Ada files as you want. @code{gnathtml} will generate
19043 an html file for every ada file, and a global file called @code{index.htm}.
19044 This file is an index of every identifier defined in the files.
19045
19046 The following switches are available:
19047
19048 @geindex -83 (gnathtml)
19049
19050
19051 @table @asis
19052
19053 @item @code{83}
19054
19055 Only the Ada 83 subset of keywords will be highlighted.
19056 @end table
19057
19058 @geindex -cc (gnathtml)
19059
19060
19061 @table @asis
19062
19063 @item @code{cc @emph{color}}
19064
19065 This option allows you to change the color used for comments. The default
19066 value is green. The color argument can be any name accepted by html.
19067 @end table
19068
19069 @geindex -d (gnathtml)
19070
19071
19072 @table @asis
19073
19074 @item @code{d}
19075
19076 If the Ada files depend on some other files (for instance through
19077 @code{with} clauses, the latter files will also be converted to html.
19078 Only the files in the user project will be converted to html, not the files
19079 in the run-time library itself.
19080 @end table
19081
19082 @geindex -D (gnathtml)
19083
19084
19085 @table @asis
19086
19087 @item @code{D}
19088
19089 This command is the same as @code{-d} above, but @code{gnathtml} will
19090 also look for files in the run-time library, and generate html files for them.
19091 @end table
19092
19093 @geindex -ext (gnathtml)
19094
19095
19096 @table @asis
19097
19098 @item @code{ext @emph{extension}}
19099
19100 This option allows you to change the extension of the generated HTML files.
19101 If you do not specify an extension, it will default to @code{htm}.
19102 @end table
19103
19104 @geindex -f (gnathtml)
19105
19106
19107 @table @asis
19108
19109 @item @code{f}
19110
19111 By default, gnathtml will generate html links only for global entities
19112 ('with'ed units, global variables and types,...). If you specify
19113 @code{-f} on the command line, then links will be generated for local
19114 entities too.
19115 @end table
19116
19117 @geindex -l (gnathtml)
19118
19119
19120 @table @asis
19121
19122 @item @code{l @emph{number}}
19123
19124 If this switch is provided and @code{number} is not 0, then
19125 @code{gnathtml} will number the html files every @code{number} line.
19126 @end table
19127
19128 @geindex -I (gnathtml)
19129
19130
19131 @table @asis
19132
19133 @item @code{I @emph{dir}}
19134
19135 Specify a directory to search for library files (@code{.ALI} files) and
19136 source files. You can provide several -I switches on the command line,
19137 and the directories will be parsed in the order of the command line.
19138 @end table
19139
19140 @geindex -o (gnathtml)
19141
19142
19143 @table @asis
19144
19145 @item @code{o @emph{dir}}
19146
19147 Specify the output directory for html files. By default, gnathtml will
19148 saved the generated html files in a subdirectory named @code{html/}.
19149 @end table
19150
19151 @geindex -p (gnathtml)
19152
19153
19154 @table @asis
19155
19156 @item @code{p @emph{file}}
19157
19158 If you are using Emacs and the most recent Emacs Ada mode, which provides
19159 a full Integrated Development Environment for compiling, checking,
19160 running and debugging applications, you may use @code{.gpr} files
19161 to give the directories where Emacs can find sources and object files.
19162
19163 Using this switch, you can tell gnathtml to use these files.
19164 This allows you to get an html version of your application, even if it
19165 is spread over multiple directories.
19166 @end table
19167
19168 @geindex -sc (gnathtml)
19169
19170
19171 @table @asis
19172
19173 @item @code{sc @emph{color}}
19174
19175 This switch allows you to change the color used for symbol
19176 definitions.
19177 The default value is red. The color argument can be any name accepted by html.
19178 @end table
19179
19180 @geindex -t (gnathtml)
19181
19182
19183 @table @asis
19184
19185 @item @code{t @emph{file}}
19186
19187 This switch provides the name of a file. This file contains a list of
19188 file names to be converted, and the effect is exactly as though they had
19189 appeared explicitly on the command line. This
19190 is the recommended way to work around the command line length limit on some
19191 systems.
19192 @end table
19193
19194 @node Installing gnathtml,,Invoking gnathtml,The Ada to HTML Converter gnathtml
19195 @anchor{gnat_ugn/gnat_utility_programs installing-gnathtml}@anchor{161}@anchor{gnat_ugn/gnat_utility_programs id18}@anchor{164}
19196 @subsection Installing @code{gnathtml}
19197
19198
19199 @code{Perl} needs to be installed on your machine to run this script.
19200 @code{Perl} is freely available for almost every architecture and
19201 operating system via the Internet.
19202
19203 On Unix systems, you may want to modify the first line of the script
19204 @code{gnathtml}, to explicitly specify where Perl
19205 is located. The syntax of this line is:
19206
19207 @quotation
19208
19209 @example
19210 #!full_path_name_to_perl
19211 @end example
19212 @end quotation
19213
19214 Alternatively, you may run the script using the following command line:
19215
19216 @quotation
19217
19218 @example
19219 $ perl gnathtml.pl [ switches ] files
19220 @end example
19221 @end quotation
19222
19223 @c -- +---------------------------------------------------------------------+
19224
19225 @c -- | The following sections are present only in the PRO and GPL editions |
19226
19227 @c -- +---------------------------------------------------------------------+
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
19238
19239 @node GNAT and Program Execution,Platform-Specific Information,GNAT Utility Programs,Top
19240 @anchor{gnat_ugn/gnat_and_program_execution gnat-and-program-execution}@anchor{c}@anchor{gnat_ugn/gnat_and_program_execution doc}@anchor{165}@anchor{gnat_ugn/gnat_and_program_execution id1}@anchor{166}
19241 @chapter GNAT and Program Execution
19242
19243
19244 This chapter covers several topics:
19245
19246
19247 @itemize *
19248
19249 @item
19250 @ref{167,,Running and Debugging Ada Programs}
19251
19252 @item
19253 @ref{25,,Profiling}
19254
19255 @item
19256 @ref{168,,Improving Performance}
19257
19258 @item
19259 @ref{169,,Overflow Check Handling in GNAT}
19260
19261 @item
19262 @ref{16a,,Performing Dimensionality Analysis in GNAT}
19263
19264 @item
19265 @ref{16b,,Stack Related Facilities}
19266
19267 @item
19268 @ref{16c,,Memory Management Issues}
19269 @end itemize
19270
19271 @menu
19272 * Running and Debugging Ada Programs::
19273 * Profiling::
19274 * Improving Performance::
19275 * Overflow Check Handling in GNAT::
19276 * Performing Dimensionality Analysis in GNAT::
19277 * Stack Related Facilities::
19278 * Memory Management Issues::
19279
19280 @end menu
19281
19282 @node Running and Debugging Ada Programs,Profiling,,GNAT and Program Execution
19283 @anchor{gnat_ugn/gnat_and_program_execution id2}@anchor{167}@anchor{gnat_ugn/gnat_and_program_execution running-and-debugging-ada-programs}@anchor{24}
19284 @section Running and Debugging Ada Programs
19285
19286
19287 @geindex Debugging
19288
19289 This section discusses how to debug Ada programs.
19290
19291 An incorrect Ada program may be handled in three ways by the GNAT compiler:
19292
19293
19294 @itemize *
19295
19296 @item
19297 The illegality may be a violation of the static semantics of Ada. In
19298 that case GNAT diagnoses the constructs in the program that are illegal.
19299 It is then a straightforward matter for the user to modify those parts of
19300 the program.
19301
19302 @item
19303 The illegality may be a violation of the dynamic semantics of Ada. In
19304 that case the program compiles and executes, but may generate incorrect
19305 results, or may terminate abnormally with some exception.
19306
19307 @item
19308 When presented with a program that contains convoluted errors, GNAT
19309 itself may terminate abnormally without providing full diagnostics on
19310 the incorrect user program.
19311 @end itemize
19312
19313 @geindex Debugger
19314
19315 @geindex gdb
19316
19317 @menu
19318 * The GNAT Debugger GDB::
19319 * Running GDB::
19320 * Introduction to GDB Commands::
19321 * Using Ada Expressions::
19322 * Calling User-Defined Subprograms::
19323 * Using the next Command in a Function::
19324 * Stopping When Ada Exceptions Are Raised::
19325 * Ada Tasks::
19326 * Debugging Generic Units::
19327 * Remote Debugging with gdbserver::
19328 * GNAT Abnormal Termination or Failure to Terminate::
19329 * Naming Conventions for GNAT Source Files::
19330 * Getting Internal Debugging Information::
19331 * Stack Traceback::
19332 * Pretty-Printers for the GNAT runtime::
19333
19334 @end menu
19335
19336 @node The GNAT Debugger GDB,Running GDB,,Running and Debugging Ada Programs
19337 @anchor{gnat_ugn/gnat_and_program_execution the-gnat-debugger-gdb}@anchor{16d}@anchor{gnat_ugn/gnat_and_program_execution id3}@anchor{16e}
19338 @subsection The GNAT Debugger GDB
19339
19340
19341 @code{GDB} is a general purpose, platform-independent debugger that
19342 can be used to debug mixed-language programs compiled with @code{gcc},
19343 and in particular is capable of debugging Ada programs compiled with
19344 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
19345 complex Ada data structures.
19346
19347 See @cite{Debugging with GDB},
19348 for full details on the usage of @code{GDB}, including a section on
19349 its usage on programs. This manual should be consulted for full
19350 details. The section that follows is a brief introduction to the
19351 philosophy and use of @code{GDB}.
19352
19353 When GNAT programs are compiled, the compiler optionally writes debugging
19354 information into the generated object file, including information on
19355 line numbers, and on declared types and variables. This information is
19356 separate from the generated code. It makes the object files considerably
19357 larger, but it does not add to the size of the actual executable that
19358 will be loaded into memory, and has no impact on run-time performance. The
19359 generation of debug information is triggered by the use of the
19360 @code{-g} switch in the @code{gcc} or @code{gnatmake} command
19361 used to carry out the compilations. It is important to emphasize that
19362 the use of these options does not change the generated code.
19363
19364 The debugging information is written in standard system formats that
19365 are used by many tools, including debuggers and profilers. The format
19366 of the information is typically designed to describe C types and
19367 semantics, but GNAT implements a translation scheme which allows full
19368 details about Ada types and variables to be encoded into these
19369 standard C formats. Details of this encoding scheme may be found in
19370 the file exp_dbug.ads in the GNAT source distribution. However, the
19371 details of this encoding are, in general, of no interest to a user,
19372 since @code{GDB} automatically performs the necessary decoding.
19373
19374 When a program is bound and linked, the debugging information is
19375 collected from the object files, and stored in the executable image of
19376 the program. Again, this process significantly increases the size of
19377 the generated executable file, but it does not increase the size of
19378 the executable program itself. Furthermore, if this program is run in
19379 the normal manner, it runs exactly as if the debug information were
19380 not present, and takes no more actual memory.
19381
19382 However, if the program is run under control of @code{GDB}, the
19383 debugger is activated. The image of the program is loaded, at which
19384 point it is ready to run. If a run command is given, then the program
19385 will run exactly as it would have if @code{GDB} were not present. This
19386 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
19387 entirely non-intrusive until a breakpoint is encountered. If no
19388 breakpoint is ever hit, the program will run exactly as it would if no
19389 debugger were present. When a breakpoint is hit, @code{GDB} accesses
19390 the debugging information and can respond to user commands to inspect
19391 variables, and more generally to report on the state of execution.
19392
19393 @node Running GDB,Introduction to GDB Commands,The GNAT Debugger GDB,Running and Debugging Ada Programs
19394 @anchor{gnat_ugn/gnat_and_program_execution id4}@anchor{16f}@anchor{gnat_ugn/gnat_and_program_execution running-gdb}@anchor{170}
19395 @subsection Running GDB
19396
19397
19398 This section describes how to initiate the debugger.
19399
19400 The debugger can be launched from a @code{GNAT Studio} menu or
19401 directly from the command line. The description below covers the latter use.
19402 All the commands shown can be used in the @code{GNAT Studio} debug console window,
19403 but there are usually more GUI-based ways to achieve the same effect.
19404
19405 The command to run @code{GDB} is
19406
19407 @quotation
19408
19409 @example
19410 $ gdb program
19411 @end example
19412 @end quotation
19413
19414 where @code{program} is the name of the executable file. This
19415 activates the debugger and results in a prompt for debugger commands.
19416 The simplest command is simply @code{run}, which causes the program to run
19417 exactly as if the debugger were not present. The following section
19418 describes some of the additional commands that can be given to @code{GDB}.
19419
19420 @node Introduction to GDB Commands,Using Ada Expressions,Running GDB,Running and Debugging Ada Programs
19421 @anchor{gnat_ugn/gnat_and_program_execution introduction-to-gdb-commands}@anchor{171}@anchor{gnat_ugn/gnat_and_program_execution id5}@anchor{172}
19422 @subsection Introduction to GDB Commands
19423
19424
19425 @code{GDB} contains a large repertoire of commands.
19426 See @cite{Debugging with GDB} for extensive documentation on the use
19427 of these commands, together with examples of their use. Furthermore,
19428 the command @emph{help} invoked from within GDB activates a simple help
19429 facility which summarizes the available commands and their options.
19430 In this section we summarize a few of the most commonly
19431 used commands to give an idea of what @code{GDB} is about. You should create
19432 a simple program with debugging information and experiment with the use of
19433 these @code{GDB} commands on the program as you read through the
19434 following section.
19435
19436
19437 @itemize *
19438
19439 @item
19440
19441 @table @asis
19442
19443 @item @code{set args @emph{arguments}}
19444
19445 The @emph{arguments} list above is a list of arguments to be passed to
19446 the program on a subsequent run command, just as though the arguments
19447 had been entered on a normal invocation of the program. The @code{set args}
19448 command is not needed if the program does not require arguments.
19449 @end table
19450
19451 @item
19452
19453 @table @asis
19454
19455 @item @code{run}
19456
19457 The @code{run} command causes execution of the program to start from
19458 the beginning. If the program is already running, that is to say if
19459 you are currently positioned at a breakpoint, then a prompt will ask
19460 for confirmation that you want to abandon the current execution and
19461 restart.
19462 @end table
19463
19464 @item
19465
19466 @table @asis
19467
19468 @item @code{breakpoint @emph{location}}
19469
19470 The breakpoint command sets a breakpoint, that is to say a point at which
19471 execution will halt and @code{GDB} will await further
19472 commands. @emph{location} is
19473 either a line number within a file, given in the format @code{file:linenumber},
19474 or it is the name of a subprogram. If you request that a breakpoint be set on
19475 a subprogram that is overloaded, a prompt will ask you to specify on which of
19476 those subprograms you want to breakpoint. You can also
19477 specify that all of them should be breakpointed. If the program is run
19478 and execution encounters the breakpoint, then the program
19479 stops and @code{GDB} signals that the breakpoint was encountered by
19480 printing the line of code before which the program is halted.
19481 @end table
19482
19483 @item
19484
19485 @table @asis
19486
19487 @item @code{catch exception @emph{name}}
19488
19489 This command causes the program execution to stop whenever exception
19490 @code{name} is raised. If @code{name} is omitted, then the execution is
19491 suspended when any exception is raised.
19492 @end table
19493
19494 @item
19495
19496 @table @asis
19497
19498 @item @code{print @emph{expression}}
19499
19500 This will print the value of the given expression. Most simple
19501 Ada expression formats are properly handled by @code{GDB}, so the expression
19502 can contain function calls, variables, operators, and attribute references.
19503 @end table
19504
19505 @item
19506
19507 @table @asis
19508
19509 @item @code{continue}
19510
19511 Continues execution following a breakpoint, until the next breakpoint or the
19512 termination of the program.
19513 @end table
19514
19515 @item
19516
19517 @table @asis
19518
19519 @item @code{step}
19520
19521 Executes a single line after a breakpoint. If the next statement
19522 is a subprogram call, execution continues into (the first statement of)
19523 the called subprogram.
19524 @end table
19525
19526 @item
19527
19528 @table @asis
19529
19530 @item @code{next}
19531
19532 Executes a single line. If this line is a subprogram call, executes and
19533 returns from the call.
19534 @end table
19535
19536 @item
19537
19538 @table @asis
19539
19540 @item @code{list}
19541
19542 Lists a few lines around the current source location. In practice, it
19543 is usually more convenient to have a separate edit window open with the
19544 relevant source file displayed. Successive applications of this command
19545 print subsequent lines. The command can be given an argument which is a
19546 line number, in which case it displays a few lines around the specified one.
19547 @end table
19548
19549 @item
19550
19551 @table @asis
19552
19553 @item @code{backtrace}
19554
19555 Displays a backtrace of the call chain. This command is typically
19556 used after a breakpoint has occurred, to examine the sequence of calls that
19557 leads to the current breakpoint. The display includes one line for each
19558 activation record (frame) corresponding to an active subprogram.
19559 @end table
19560
19561 @item
19562
19563 @table @asis
19564
19565 @item @code{up}
19566
19567 At a breakpoint, @code{GDB} can display the values of variables local
19568 to the current frame. The command @code{up} can be used to
19569 examine the contents of other active frames, by moving the focus up
19570 the stack, that is to say from callee to caller, one frame at a time.
19571 @end table
19572
19573 @item
19574
19575 @table @asis
19576
19577 @item @code{down}
19578
19579 Moves the focus of @code{GDB} down from the frame currently being
19580 examined to the frame of its callee (the reverse of the previous command),
19581 @end table
19582
19583 @item
19584
19585 @table @asis
19586
19587 @item @code{frame @emph{n}}
19588
19589 Inspect the frame with the given number. The value 0 denotes the frame
19590 of the current breakpoint, that is to say the top of the call stack.
19591 @end table
19592
19593 @item
19594
19595 @table @asis
19596
19597 @item @code{kill}
19598
19599 Kills the child process in which the program is running under GDB.
19600 This may be useful for several purposes:
19601
19602
19603 @itemize *
19604
19605 @item
19606 It allows you to recompile and relink your program, since on many systems
19607 you cannot regenerate an executable file while it is running in a process.
19608
19609 @item
19610 You can run your program outside the debugger, on systems that do not
19611 permit executing a program outside GDB while breakpoints are set
19612 within GDB.
19613
19614 @item
19615 It allows you to debug a core dump rather than a running process.
19616 @end itemize
19617 @end table
19618 @end itemize
19619
19620 The above list is a very short introduction to the commands that
19621 @code{GDB} provides. Important additional capabilities, including conditional
19622 breakpoints, the ability to execute command sequences on a breakpoint,
19623 the ability to debug at the machine instruction level and many other
19624 features are described in detail in @cite{Debugging with GDB}.
19625 Note that most commands can be abbreviated
19626 (for example, c for continue, bt for backtrace).
19627
19628 @node Using Ada Expressions,Calling User-Defined Subprograms,Introduction to GDB Commands,Running and Debugging Ada Programs
19629 @anchor{gnat_ugn/gnat_and_program_execution id6}@anchor{173}@anchor{gnat_ugn/gnat_and_program_execution using-ada-expressions}@anchor{174}
19630 @subsection Using Ada Expressions
19631
19632
19633 @geindex Ada expressions (in gdb)
19634
19635 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
19636 extensions. The philosophy behind the design of this subset is
19637
19638 @quotation
19639
19640
19641 @itemize *
19642
19643 @item
19644 That @code{GDB} should provide basic literals and access to operations for
19645 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
19646 leaving more sophisticated computations to subprograms written into the
19647 program (which therefore may be called from @code{GDB}).
19648
19649 @item
19650 That type safety and strict adherence to Ada language restrictions
19651 are not particularly relevant in a debugging context.
19652
19653 @item
19654 That brevity is important to the @code{GDB} user.
19655 @end itemize
19656 @end quotation
19657
19658 Thus, for brevity, the debugger acts as if there were
19659 implicit @code{with} and @code{use} clauses in effect for all user-written
19660 packages, thus making it unnecessary to fully qualify most names with
19661 their packages, regardless of context. Where this causes ambiguity,
19662 @code{GDB} asks the user's intent.
19663
19664 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
19665
19666 @node Calling User-Defined Subprograms,Using the next Command in a Function,Using Ada Expressions,Running and Debugging Ada Programs
19667 @anchor{gnat_ugn/gnat_and_program_execution id7}@anchor{175}@anchor{gnat_ugn/gnat_and_program_execution calling-user-defined-subprograms}@anchor{176}
19668 @subsection Calling User-Defined Subprograms
19669
19670
19671 An important capability of @code{GDB} is the ability to call user-defined
19672 subprograms while debugging. This is achieved simply by entering
19673 a subprogram call statement in the form:
19674
19675 @quotation
19676
19677 @example
19678 call subprogram-name (parameters)
19679 @end example
19680 @end quotation
19681
19682 The keyword @code{call} can be omitted in the normal case where the
19683 @code{subprogram-name} does not coincide with any of the predefined
19684 @code{GDB} commands.
19685
19686 The effect is to invoke the given subprogram, passing it the
19687 list of parameters that is supplied. The parameters can be expressions and
19688 can include variables from the program being debugged. The
19689 subprogram must be defined
19690 at the library level within your program, and @code{GDB} will call the
19691 subprogram within the environment of your program execution (which
19692 means that the subprogram is free to access or even modify variables
19693 within your program).
19694
19695 The most important use of this facility is in allowing the inclusion of
19696 debugging routines that are tailored to particular data structures
19697 in your program. Such debugging routines can be written to provide a suitably
19698 high-level description of an abstract type, rather than a low-level dump
19699 of its physical layout. After all, the standard
19700 @code{GDB print} command only knows the physical layout of your
19701 types, not their abstract meaning. Debugging routines can provide information
19702 at the desired semantic level and are thus enormously useful.
19703
19704 For example, when debugging GNAT itself, it is crucial to have access to
19705 the contents of the tree nodes used to represent the program internally.
19706 But tree nodes are represented simply by an integer value (which in turn
19707 is an index into a table of nodes).
19708 Using the @code{print} command on a tree node would simply print this integer
19709 value, which is not very useful. But the PN routine (defined in file
19710 treepr.adb in the GNAT sources) takes a tree node as input, and displays
19711 a useful high level representation of the tree node, which includes the
19712 syntactic category of the node, its position in the source, the integers
19713 that denote descendant nodes and parent node, as well as varied
19714 semantic information. To study this example in more detail, you might want to
19715 look at the body of the PN procedure in the stated file.
19716
19717 Another useful application of this capability is to deal with situations of
19718 complex data which are not handled suitably by GDB. For example, if you specify
19719 Convention Fortran for a multi-dimensional array, GDB does not know that
19720 the ordering of array elements has been switched and will not properly
19721 address the array elements. In such a case, instead of trying to print the
19722 elements directly from GDB, you can write a callable procedure that prints
19723 the elements in the desired format.
19724
19725 @node Using the next Command in a Function,Stopping When Ada Exceptions Are Raised,Calling User-Defined Subprograms,Running and Debugging Ada Programs
19726 @anchor{gnat_ugn/gnat_and_program_execution using-the-next-command-in-a-function}@anchor{177}@anchor{gnat_ugn/gnat_and_program_execution id8}@anchor{178}
19727 @subsection Using the @emph{next} Command in a Function
19728
19729
19730 When you use the @code{next} command in a function, the current source
19731 location will advance to the next statement as usual. A special case
19732 arises in the case of a @code{return} statement.
19733
19734 Part of the code for a return statement is the 'epilogue' of the function.
19735 This is the code that returns to the caller. There is only one copy of
19736 this epilogue code, and it is typically associated with the last return
19737 statement in the function if there is more than one return. In some
19738 implementations, this epilogue is associated with the first statement
19739 of the function.
19740
19741 The result is that if you use the @code{next} command from a return
19742 statement that is not the last return statement of the function you
19743 may see a strange apparent jump to the last return statement or to
19744 the start of the function. You should simply ignore this odd jump.
19745 The value returned is always that from the first return statement
19746 that was stepped through.
19747
19748 @node Stopping When Ada Exceptions Are Raised,Ada Tasks,Using the next Command in a Function,Running and Debugging Ada Programs
19749 @anchor{gnat_ugn/gnat_and_program_execution stopping-when-ada-exceptions-are-raised}@anchor{179}@anchor{gnat_ugn/gnat_and_program_execution id9}@anchor{17a}
19750 @subsection Stopping When Ada Exceptions Are Raised
19751
19752
19753 @geindex Exceptions (in gdb)
19754
19755 You can set catchpoints that stop the program execution when your program
19756 raises selected exceptions.
19757
19758
19759 @itemize *
19760
19761 @item
19762
19763 @table @asis
19764
19765 @item @code{catch exception}
19766
19767 Set a catchpoint that stops execution whenever (any task in the) program
19768 raises any exception.
19769 @end table
19770
19771 @item
19772
19773 @table @asis
19774
19775 @item @code{catch exception @emph{name}}
19776
19777 Set a catchpoint that stops execution whenever (any task in the) program
19778 raises the exception @emph{name}.
19779 @end table
19780
19781 @item
19782
19783 @table @asis
19784
19785 @item @code{catch exception unhandled}
19786
19787 Set a catchpoint that stops executing whenever (any task in the) program
19788 raises an exception for which there is no handler.
19789 @end table
19790
19791 @item
19792
19793 @table @asis
19794
19795 @item @code{info exceptions}, @code{info exceptions @emph{regexp}}
19796
19797 The @code{info exceptions} command permits the user to examine all defined
19798 exceptions within Ada programs. With a regular expression, @emph{regexp}, as
19799 argument, prints out only those exceptions whose name matches @emph{regexp}.
19800 @end table
19801 @end itemize
19802
19803 @geindex Tasks (in gdb)
19804
19805 @node Ada Tasks,Debugging Generic Units,Stopping When Ada Exceptions Are Raised,Running and Debugging Ada Programs
19806 @anchor{gnat_ugn/gnat_and_program_execution ada-tasks}@anchor{17b}@anchor{gnat_ugn/gnat_and_program_execution id10}@anchor{17c}
19807 @subsection Ada Tasks
19808
19809
19810 @code{GDB} allows the following task-related commands:
19811
19812
19813 @itemize *
19814
19815 @item
19816
19817 @table @asis
19818
19819 @item @code{info tasks}
19820
19821 This command shows a list of current Ada tasks, as in the following example:
19822
19823 @example
19824 (gdb) info tasks
19825 ID TID P-ID Thread Pri State Name
19826 1 8088000 0 807e000 15 Child Activation Wait main_task
19827 2 80a4000 1 80ae000 15 Accept/Select Wait b
19828 3 809a800 1 80a4800 15 Child Activation Wait a
19829 * 4 80ae800 3 80b8000 15 Running c
19830 @end example
19831
19832 In this listing, the asterisk before the first task indicates it to be the
19833 currently running task. The first column lists the task ID that is used
19834 to refer to tasks in the following commands.
19835 @end table
19836 @end itemize
19837
19838 @geindex Breakpoints and tasks
19839
19840
19841 @itemize *
19842
19843 @item
19844 @code{break`@w{`}*linespec* `@w{`}task} @emph{taskid}, @code{break} @emph{linespec} @code{task} @emph{taskid} @code{if} ...
19845
19846 @quotation
19847
19848 These commands are like the @code{break ... thread ...}.
19849 @emph{linespec} specifies source lines.
19850
19851 Use the qualifier @code{task @emph{taskid}} with a breakpoint command
19852 to specify that you only want @code{GDB} to stop the program when a
19853 particular Ada task reaches this breakpoint. @emph{taskid} is one of the
19854 numeric task identifiers assigned by @code{GDB}, shown in the first
19855 column of the @code{info tasks} display.
19856
19857 If you do not specify @code{task @emph{taskid}} when you set a
19858 breakpoint, the breakpoint applies to @emph{all} tasks of your
19859 program.
19860
19861 You can use the @code{task} qualifier on conditional breakpoints as
19862 well; in this case, place @code{task @emph{taskid}} before the
19863 breakpoint condition (before the @code{if}).
19864 @end quotation
19865 @end itemize
19866
19867 @geindex Task switching (in gdb)
19868
19869
19870 @itemize *
19871
19872 @item
19873 @code{task @emph{taskno}}
19874
19875 @quotation
19876
19877 This command allows switching to the task referred by @emph{taskno}. In
19878 particular, this allows browsing of the backtrace of the specified
19879 task. It is advisable to switch back to the original task before
19880 continuing execution otherwise the scheduling of the program may be
19881 perturbed.
19882 @end quotation
19883 @end itemize
19884
19885 For more detailed information on the tasking support,
19886 see @cite{Debugging with GDB}.
19887
19888 @geindex Debugging Generic Units
19889
19890 @geindex Generics
19891
19892 @node Debugging Generic Units,Remote Debugging with gdbserver,Ada Tasks,Running and Debugging Ada Programs
19893 @anchor{gnat_ugn/gnat_and_program_execution debugging-generic-units}@anchor{17d}@anchor{gnat_ugn/gnat_and_program_execution id11}@anchor{17e}
19894 @subsection Debugging Generic Units
19895
19896
19897 GNAT always uses code expansion for generic instantiation. This means that
19898 each time an instantiation occurs, a complete copy of the original code is
19899 made, with appropriate substitutions of formals by actuals.
19900
19901 It is not possible to refer to the original generic entities in
19902 @code{GDB}, but it is always possible to debug a particular instance of
19903 a generic, by using the appropriate expanded names. For example, if we have
19904
19905 @quotation
19906
19907 @example
19908 procedure g is
19909
19910 generic package k is
19911 procedure kp (v1 : in out integer);
19912 end k;
19913
19914 package body k is
19915 procedure kp (v1 : in out integer) is
19916 begin
19917 v1 := v1 + 1;
19918 end kp;
19919 end k;
19920
19921 package k1 is new k;
19922 package k2 is new k;
19923
19924 var : integer := 1;
19925
19926 begin
19927 k1.kp (var);
19928 k2.kp (var);
19929 k1.kp (var);
19930 k2.kp (var);
19931 end;
19932 @end example
19933 @end quotation
19934
19935 Then to break on a call to procedure kp in the k2 instance, simply
19936 use the command:
19937
19938 @quotation
19939
19940 @example
19941 (gdb) break g.k2.kp
19942 @end example
19943 @end quotation
19944
19945 When the breakpoint occurs, you can step through the code of the
19946 instance in the normal manner and examine the values of local variables, as for
19947 other units.
19948
19949 @geindex Remote Debugging with gdbserver
19950
19951 @node Remote Debugging with gdbserver,GNAT Abnormal Termination or Failure to Terminate,Debugging Generic Units,Running and Debugging Ada Programs
19952 @anchor{gnat_ugn/gnat_and_program_execution remote-debugging-with-gdbserver}@anchor{17f}@anchor{gnat_ugn/gnat_and_program_execution id12}@anchor{180}
19953 @subsection Remote Debugging with gdbserver
19954
19955
19956 On platforms where gdbserver is supported, it is possible to use this tool
19957 to debug your application remotely. This can be useful in situations
19958 where the program needs to be run on a target host that is different
19959 from the host used for development, particularly when the target has
19960 a limited amount of resources (either CPU and/or memory).
19961
19962 To do so, start your program using gdbserver on the target machine.
19963 gdbserver then automatically suspends the execution of your program
19964 at its entry point, waiting for a debugger to connect to it. The
19965 following commands starts an application and tells gdbserver to
19966 wait for a connection with the debugger on localhost port 4444.
19967
19968 @quotation
19969
19970 @example
19971 $ gdbserver localhost:4444 program
19972 Process program created; pid = 5685
19973 Listening on port 4444
19974 @end example
19975 @end quotation
19976
19977 Once gdbserver has started listening, we can tell the debugger to establish
19978 a connection with this gdbserver, and then start the same debugging session
19979 as if the program was being debugged on the same host, directly under
19980 the control of GDB.
19981
19982 @quotation
19983
19984 @example
19985 $ gdb program
19986 (gdb) target remote targethost:4444
19987 Remote debugging using targethost:4444
19988 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
19989 (gdb) b foo.adb:3
19990 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
19991 (gdb) continue
19992 Continuing.
19993
19994 Breakpoint 1, foo () at foo.adb:4
19995 4 end foo;
19996 @end example
19997 @end quotation
19998
19999 It is also possible to use gdbserver to attach to an already running
20000 program, in which case the execution of that program is simply suspended
20001 until the connection between the debugger and gdbserver is established.
20002
20003 For more information on how to use gdbserver, see the @emph{Using the gdbserver Program}
20004 section in @cite{Debugging with GDB}.
20005 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
20006
20007 @geindex Abnormal Termination or Failure to Terminate
20008
20009 @node GNAT Abnormal Termination or Failure to Terminate,Naming Conventions for GNAT Source Files,Remote Debugging with gdbserver,Running and Debugging Ada Programs
20010 @anchor{gnat_ugn/gnat_and_program_execution gnat-abnormal-termination-or-failure-to-terminate}@anchor{181}@anchor{gnat_ugn/gnat_and_program_execution id13}@anchor{182}
20011 @subsection GNAT Abnormal Termination or Failure to Terminate
20012
20013
20014 When presented with programs that contain serious errors in syntax
20015 or semantics,
20016 GNAT may on rare occasions experience problems in operation, such
20017 as aborting with a
20018 segmentation fault or illegal memory access, raising an internal
20019 exception, terminating abnormally, or failing to terminate at all.
20020 In such cases, you can activate
20021 various features of GNAT that can help you pinpoint the construct in your
20022 program that is the likely source of the problem.
20023
20024 The following strategies are presented in increasing order of
20025 difficulty, corresponding to your experience in using GNAT and your
20026 familiarity with compiler internals.
20027
20028
20029 @itemize *
20030
20031 @item
20032 Run @code{gcc} with the @code{-gnatf}. This first
20033 switch causes all errors on a given line to be reported. In its absence,
20034 only the first error on a line is displayed.
20035
20036 The @code{-gnatdO} switch causes errors to be displayed as soon as they
20037 are encountered, rather than after compilation is terminated. If GNAT
20038 terminates prematurely or goes into an infinite loop, the last error
20039 message displayed may help to pinpoint the culprit.
20040
20041 @item
20042 Run @code{gcc} with the @code{-v} (verbose) switch. In this
20043 mode, @code{gcc} produces ongoing information about the progress of the
20044 compilation and provides the name of each procedure as code is
20045 generated. This switch allows you to find which Ada procedure was being
20046 compiled when it encountered a code generation problem.
20047 @end itemize
20048
20049 @geindex -gnatdc switch
20050
20051
20052 @itemize *
20053
20054 @item
20055 Run @code{gcc} with the @code{-gnatdc} switch. This is a GNAT specific
20056 switch that does for the front-end what @code{-v} does
20057 for the back end. The system prints the name of each unit,
20058 either a compilation unit or nested unit, as it is being analyzed.
20059
20060 @item
20061 Finally, you can start
20062 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
20063 front-end of GNAT, and can be run independently (normally it is just
20064 called from @code{gcc}). You can use @code{gdb} on @code{gnat1} as you
20065 would on a C program (but @ref{16d,,The GNAT Debugger GDB} for caveats). The
20066 @code{where} command is the first line of attack; the variable
20067 @code{lineno} (seen by @code{print lineno}), used by the second phase of
20068 @code{gnat1} and by the @code{gcc} backend, indicates the source line at
20069 which the execution stopped, and @code{input_file name} indicates the name of
20070 the source file.
20071 @end itemize
20072
20073 @node Naming Conventions for GNAT Source Files,Getting Internal Debugging Information,GNAT Abnormal Termination or Failure to Terminate,Running and Debugging Ada Programs
20074 @anchor{gnat_ugn/gnat_and_program_execution naming-conventions-for-gnat-source-files}@anchor{183}@anchor{gnat_ugn/gnat_and_program_execution id14}@anchor{184}
20075 @subsection Naming Conventions for GNAT Source Files
20076
20077
20078 In order to examine the workings of the GNAT system, the following
20079 brief description of its organization may be helpful:
20080
20081
20082 @itemize *
20083
20084 @item
20085 Files with prefix @code{sc} contain the lexical scanner.
20086
20087 @item
20088 All files prefixed with @code{par} are components of the parser. The
20089 numbers correspond to chapters of the Ada Reference Manual. For example,
20090 parsing of select statements can be found in @code{par-ch9.adb}.
20091
20092 @item
20093 All files prefixed with @code{sem} perform semantic analysis. The
20094 numbers correspond to chapters of the Ada standard. For example, all
20095 issues involving context clauses can be found in @code{sem_ch10.adb}. In
20096 addition, some features of the language require sufficient special processing
20097 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
20098 dynamic dispatching, etc.
20099
20100 @item
20101 All files prefixed with @code{exp} perform normalization and
20102 expansion of the intermediate representation (abstract syntax tree, or AST).
20103 these files use the same numbering scheme as the parser and semantics files.
20104 For example, the construction of record initialization procedures is done in
20105 @code{exp_ch3.adb}.
20106
20107 @item
20108 The files prefixed with @code{bind} implement the binder, which
20109 verifies the consistency of the compilation, determines an order of
20110 elaboration, and generates the bind file.
20111
20112 @item
20113 The files @code{atree.ads} and @code{atree.adb} detail the low-level
20114 data structures used by the front-end.
20115
20116 @item
20117 The files @code{sinfo.ads} and @code{sinfo.adb} detail the structure of
20118 the abstract syntax tree as produced by the parser.
20119
20120 @item
20121 The files @code{einfo.ads} and @code{einfo.adb} detail the attributes of
20122 all entities, computed during semantic analysis.
20123
20124 @item
20125 Library management issues are dealt with in files with prefix
20126 @code{lib}.
20127
20128 @geindex Annex A (in Ada Reference Manual)
20129
20130 @item
20131 Ada files with the prefix @code{a-} are children of @code{Ada}, as
20132 defined in Annex A.
20133
20134 @geindex Annex B (in Ada reference Manual)
20135
20136 @item
20137 Files with prefix @code{i-} are children of @code{Interfaces}, as
20138 defined in Annex B.
20139
20140 @geindex System (package in Ada Reference Manual)
20141
20142 @item
20143 Files with prefix @code{s-} are children of @code{System}. This includes
20144 both language-defined children and GNAT run-time routines.
20145
20146 @geindex GNAT (package)
20147
20148 @item
20149 Files with prefix @code{g-} are children of @code{GNAT}. These are useful
20150 general-purpose packages, fully documented in their specs. All
20151 the other @code{.c} files are modifications of common @code{gcc} files.
20152 @end itemize
20153
20154 @node Getting Internal Debugging Information,Stack Traceback,Naming Conventions for GNAT Source Files,Running and Debugging Ada Programs
20155 @anchor{gnat_ugn/gnat_and_program_execution id15}@anchor{185}@anchor{gnat_ugn/gnat_and_program_execution getting-internal-debugging-information}@anchor{186}
20156 @subsection Getting Internal Debugging Information
20157
20158
20159 Most compilers have internal debugging switches and modes. GNAT
20160 does also, except GNAT internal debugging switches and modes are not
20161 secret. A summary and full description of all the compiler and binder
20162 debug flags are in the file @code{debug.adb}. You must obtain the
20163 sources of the compiler to see the full detailed effects of these flags.
20164
20165 The switches that print the source of the program (reconstructed from
20166 the internal tree) are of general interest for user programs, as are the
20167 options to print
20168 the full internal tree, and the entity table (the symbol table
20169 information). The reconstructed source provides a readable version of the
20170 program after the front-end has completed analysis and expansion,
20171 and is useful when studying the performance of specific constructs.
20172 For example, constraint checks are indicated, complex aggregates
20173 are replaced with loops and assignments, and tasking primitives
20174 are replaced with run-time calls.
20175
20176 @geindex traceback
20177
20178 @geindex stack traceback
20179
20180 @geindex stack unwinding
20181
20182 @node Stack Traceback,Pretty-Printers for the GNAT runtime,Getting Internal Debugging Information,Running and Debugging Ada Programs
20183 @anchor{gnat_ugn/gnat_and_program_execution stack-traceback}@anchor{187}@anchor{gnat_ugn/gnat_and_program_execution id16}@anchor{188}
20184 @subsection Stack Traceback
20185
20186
20187 Traceback is a mechanism to display the sequence of subprogram calls that
20188 leads to a specified execution point in a program. Often (but not always)
20189 the execution point is an instruction at which an exception has been raised.
20190 This mechanism is also known as @emph{stack unwinding} because it obtains
20191 its information by scanning the run-time stack and recovering the activation
20192 records of all active subprograms. Stack unwinding is one of the most
20193 important tools for program debugging.
20194
20195 The first entry stored in traceback corresponds to the deepest calling level,
20196 that is to say the subprogram currently executing the instruction
20197 from which we want to obtain the traceback.
20198
20199 Note that there is no runtime performance penalty when stack traceback
20200 is enabled, and no exception is raised during program execution.
20201
20202 @geindex traceback
20203 @geindex non-symbolic
20204
20205 @menu
20206 * Non-Symbolic Traceback::
20207 * Symbolic Traceback::
20208
20209 @end menu
20210
20211 @node Non-Symbolic Traceback,Symbolic Traceback,,Stack Traceback
20212 @anchor{gnat_ugn/gnat_and_program_execution non-symbolic-traceback}@anchor{189}@anchor{gnat_ugn/gnat_and_program_execution id17}@anchor{18a}
20213 @subsubsection Non-Symbolic Traceback
20214
20215
20216 Note: this feature is not supported on all platforms. See
20217 @code{GNAT.Traceback} spec in @code{g-traceb.ads}
20218 for a complete list of supported platforms.
20219
20220 @subsubheading Tracebacks From an Unhandled Exception
20221
20222
20223 A runtime non-symbolic traceback is a list of addresses of call instructions.
20224 To enable this feature you must use the @code{-E}
20225 @code{gnatbind} option. With this option a stack traceback is stored as part
20226 of exception information. You can retrieve this information using the
20227 @code{addr2line} tool.
20228
20229 Here is a simple example:
20230
20231 @quotation
20232
20233 @example
20234 procedure STB is
20235
20236 procedure P1 is
20237 begin
20238 raise Constraint_Error;
20239 end P1;
20240
20241 procedure P2 is
20242 begin
20243 P1;
20244 end P2;
20245
20246 begin
20247 P2;
20248 end STB;
20249 @end example
20250
20251 @example
20252 $ gnatmake stb -bargs -E
20253 $ stb
20254
20255 Execution terminated by unhandled exception
20256 Exception name: CONSTRAINT_ERROR
20257 Message: stb.adb:5
20258 Call stack traceback locations:
20259 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20260 @end example
20261 @end quotation
20262
20263 As we see the traceback lists a sequence of addresses for the unhandled
20264 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
20265 guess that this exception come from procedure P1. To translate these
20266 addresses into the source lines where the calls appear, the
20267 @code{addr2line} tool, described below, is invaluable. The use of this tool
20268 requires the program to be compiled with debug information.
20269
20270 @quotation
20271
20272 @example
20273 $ gnatmake -g stb -bargs -E
20274 $ stb
20275
20276 Execution terminated by unhandled exception
20277 Exception name: CONSTRAINT_ERROR
20278 Message: stb.adb:5
20279 Call stack traceback locations:
20280 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20281
20282 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
20283 0x4011f1 0x77e892a4
20284
20285 00401373 at d:/stb/stb.adb:5
20286 0040138B at d:/stb/stb.adb:10
20287 0040139C at d:/stb/stb.adb:14
20288 00401335 at d:/stb/b~stb.adb:104
20289 004011C4 at /build/.../crt1.c:200
20290 004011F1 at /build/.../crt1.c:222
20291 77E892A4 in ?? at ??:0
20292 @end example
20293 @end quotation
20294
20295 The @code{addr2line} tool has several other useful options:
20296
20297 @quotation
20298
20299
20300 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
20301 @item
20302
20303 @code{--functions}
20304
20305 @tab
20306
20307 to get the function name corresponding to any location
20308
20309 @item
20310
20311 @code{--demangle=gnat}
20312
20313 @tab
20314
20315 to use the gnat decoding mode for the function names.
20316 Note that for binutils version 2.9.x the option is
20317 simply @code{--demangle}.
20318
20319 @end multitable
20320
20321
20322 @example
20323 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
20324 0x40139c 0x401335 0x4011c4 0x4011f1
20325
20326 00401373 in stb.p1 at d:/stb/stb.adb:5
20327 0040138B in stb.p2 at d:/stb/stb.adb:10
20328 0040139C in stb at d:/stb/stb.adb:14
20329 00401335 in main at d:/stb/b~stb.adb:104
20330 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
20331 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
20332 @end example
20333 @end quotation
20334
20335 From this traceback we can see that the exception was raised in
20336 @code{stb.adb} at line 5, which was reached from a procedure call in
20337 @code{stb.adb} at line 10, and so on. The @code{b~std.adb} is the binder file,
20338 which contains the call to the main program.
20339 @ref{11c,,Running gnatbind}. The remaining entries are assorted runtime routines,
20340 and the output will vary from platform to platform.
20341
20342 It is also possible to use @code{GDB} with these traceback addresses to debug
20343 the program. For example, we can break at a given code location, as reported
20344 in the stack traceback:
20345
20346 @quotation
20347
20348 @example
20349 $ gdb -nw stb
20350 @end example
20351 @end quotation
20352
20353 Furthermore, this feature is not implemented inside Windows DLL. Only
20354 the non-symbolic traceback is reported in this case.
20355
20356 @quotation
20357
20358 @example
20359 (gdb) break *0x401373
20360 Breakpoint 1 at 0x401373: file stb.adb, line 5.
20361 @end example
20362 @end quotation
20363
20364 It is important to note that the stack traceback addresses
20365 do not change when debug information is included. This is particularly useful
20366 because it makes it possible to release software without debug information (to
20367 minimize object size), get a field report that includes a stack traceback
20368 whenever an internal bug occurs, and then be able to retrieve the sequence
20369 of calls with the same program compiled with debug information.
20370
20371 @subsubheading Tracebacks From Exception Occurrences
20372
20373
20374 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
20375 The stack traceback is attached to the exception information string, and can
20376 be retrieved in an exception handler within the Ada program, by means of the
20377 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
20378
20379 @quotation
20380
20381 @example
20382 with Ada.Text_IO;
20383 with Ada.Exceptions;
20384
20385 procedure STB is
20386
20387 use Ada;
20388 use Ada.Exceptions;
20389
20390 procedure P1 is
20391 K : Positive := 1;
20392 begin
20393 K := K - 1;
20394 exception
20395 when E : others =>
20396 Text_IO.Put_Line (Exception_Information (E));
20397 end P1;
20398
20399 procedure P2 is
20400 begin
20401 P1;
20402 end P2;
20403
20404 begin
20405 P2;
20406 end STB;
20407 @end example
20408 @end quotation
20409
20410 This program will output:
20411
20412 @quotation
20413
20414 @example
20415 $ stb
20416
20417 Exception name: CONSTRAINT_ERROR
20418 Message: stb.adb:12
20419 Call stack traceback locations:
20420 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
20421 @end example
20422 @end quotation
20423
20424 @subsubheading Tracebacks From Anywhere in a Program
20425
20426
20427 It is also possible to retrieve a stack traceback from anywhere in a
20428 program. For this you need to
20429 use the @code{GNAT.Traceback} API. This package includes a procedure called
20430 @code{Call_Chain} that computes a complete stack traceback, as well as useful
20431 display procedures described below. It is not necessary to use the
20432 @code{-E} @code{gnatbind} option in this case, because the stack traceback mechanism
20433 is invoked explicitly.
20434
20435 In the following example we compute a traceback at a specific location in
20436 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
20437 convert addresses to strings:
20438
20439 @quotation
20440
20441 @example
20442 with Ada.Text_IO;
20443 with GNAT.Traceback;
20444 with GNAT.Debug_Utilities;
20445
20446 procedure STB is
20447
20448 use Ada;
20449 use GNAT;
20450 use GNAT.Traceback;
20451
20452 procedure P1 is
20453 TB : Tracebacks_Array (1 .. 10);
20454 -- We are asking for a maximum of 10 stack frames.
20455 Len : Natural;
20456 -- Len will receive the actual number of stack frames returned.
20457 begin
20458 Call_Chain (TB, Len);
20459
20460 Text_IO.Put ("In STB.P1 : ");
20461
20462 for K in 1 .. Len loop
20463 Text_IO.Put (Debug_Utilities.Image (TB (K)));
20464 Text_IO.Put (' ');
20465 end loop;
20466
20467 Text_IO.New_Line;
20468 end P1;
20469
20470 procedure P2 is
20471 begin
20472 P1;
20473 end P2;
20474
20475 begin
20476 P2;
20477 end STB;
20478 @end example
20479
20480 @example
20481 $ gnatmake -g stb
20482 $ stb
20483
20484 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
20485 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
20486 @end example
20487 @end quotation
20488
20489 You can then get further information by invoking the @code{addr2line}
20490 tool as described earlier (note that the hexadecimal addresses
20491 need to be specified in C format, with a leading '0x').
20492
20493 @geindex traceback
20494 @geindex symbolic
20495
20496 @node Symbolic Traceback,,Non-Symbolic Traceback,Stack Traceback
20497 @anchor{gnat_ugn/gnat_and_program_execution id18}@anchor{18b}@anchor{gnat_ugn/gnat_and_program_execution symbolic-traceback}@anchor{18c}
20498 @subsubsection Symbolic Traceback
20499
20500
20501 A symbolic traceback is a stack traceback in which procedure names are
20502 associated with each code location.
20503
20504 Note that this feature is not supported on all platforms. See
20505 @code{GNAT.Traceback.Symbolic} spec in @code{g-trasym.ads} for a complete
20506 list of currently supported platforms.
20507
20508 Note that the symbolic traceback requires that the program be compiled
20509 with debug information. If it is not compiled with debug information
20510 only the non-symbolic information will be valid.
20511
20512 @subsubheading Tracebacks From Exception Occurrences
20513
20514
20515 Here is an example:
20516
20517 @quotation
20518
20519 @example
20520 with Ada.Text_IO;
20521 with GNAT.Traceback.Symbolic;
20522
20523 procedure STB is
20524
20525 procedure P1 is
20526 begin
20527 raise Constraint_Error;
20528 end P1;
20529
20530 procedure P2 is
20531 begin
20532 P1;
20533 end P2;
20534
20535 procedure P3 is
20536 begin
20537 P2;
20538 end P3;
20539
20540 begin
20541 P3;
20542 exception
20543 when E : others =>
20544 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
20545 end STB;
20546 @end example
20547
20548 @example
20549 $ gnatmake -g .\stb -bargs -E
20550 $ stb
20551
20552 0040149F in stb.p1 at stb.adb:8
20553 004014B7 in stb.p2 at stb.adb:13
20554 004014CF in stb.p3 at stb.adb:18
20555 004015DD in ada.stb at stb.adb:22
20556 00401461 in main at b~stb.adb:168
20557 004011C4 in __mingw_CRTStartup at crt1.c:200
20558 004011F1 in mainCRTStartup at crt1.c:222
20559 77E892A4 in ?? at ??:0
20560 @end example
20561 @end quotation
20562
20563 In the above example the @code{.\} syntax in the @code{gnatmake} command
20564 is currently required by @code{addr2line} for files that are in
20565 the current working directory.
20566 Moreover, the exact sequence of linker options may vary from platform
20567 to platform.
20568 The above @code{-largs} section is for Windows platforms. By contrast,
20569 under Unix there is no need for the @code{-largs} section.
20570 Differences across platforms are due to details of linker implementation.
20571
20572 @subsubheading Tracebacks From Anywhere in a Program
20573
20574
20575 It is possible to get a symbolic stack traceback
20576 from anywhere in a program, just as for non-symbolic tracebacks.
20577 The first step is to obtain a non-symbolic
20578 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
20579 information. Here is an example:
20580
20581 @quotation
20582
20583 @example
20584 with Ada.Text_IO;
20585 with GNAT.Traceback;
20586 with GNAT.Traceback.Symbolic;
20587
20588 procedure STB is
20589
20590 use Ada;
20591 use GNAT.Traceback;
20592 use GNAT.Traceback.Symbolic;
20593
20594 procedure P1 is
20595 TB : Tracebacks_Array (1 .. 10);
20596 -- We are asking for a maximum of 10 stack frames.
20597 Len : Natural;
20598 -- Len will receive the actual number of stack frames returned.
20599 begin
20600 Call_Chain (TB, Len);
20601 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
20602 end P1;
20603
20604 procedure P2 is
20605 begin
20606 P1;
20607 end P2;
20608
20609 begin
20610 P2;
20611 end STB;
20612 @end example
20613 @end quotation
20614
20615 @subsubheading Automatic Symbolic Tracebacks
20616
20617
20618 Symbolic tracebacks may also be enabled by using the -Es switch to gnatbind (as
20619 in @code{gprbuild -g ... -bargs -Es}).
20620 This will cause the Exception_Information to contain a symbolic traceback,
20621 which will also be printed if an unhandled exception terminates the
20622 program.
20623
20624 @node Pretty-Printers for the GNAT runtime,,Stack Traceback,Running and Debugging Ada Programs
20625 @anchor{gnat_ugn/gnat_and_program_execution id19}@anchor{18d}@anchor{gnat_ugn/gnat_and_program_execution pretty-printers-for-the-gnat-runtime}@anchor{18e}
20626 @subsection Pretty-Printers for the GNAT runtime
20627
20628
20629 As discussed in @cite{Calling User-Defined Subprograms}, GDB's
20630 @code{print} command only knows about the physical layout of program data
20631 structures and therefore normally displays only low-level dumps, which
20632 are often hard to understand.
20633
20634 An example of this is when trying to display the contents of an Ada
20635 standard container, such as @code{Ada.Containers.Ordered_Maps.Map}:
20636
20637 @quotation
20638
20639 @example
20640 with Ada.Containers.Ordered_Maps;
20641
20642 procedure PP is
20643 package Int_To_Nat is
20644 new Ada.Containers.Ordered_Maps (Integer, Natural);
20645
20646 Map : Int_To_Nat.Map;
20647 begin
20648 Map.Insert (1, 10);
20649 Map.Insert (2, 20);
20650 Map.Insert (3, 30);
20651
20652 Map.Clear; -- BREAK HERE
20653 end PP;
20654 @end example
20655 @end quotation
20656
20657 When this program is built with debugging information and run under
20658 GDB up to the @code{Map.Clear} statement, trying to print @code{Map} will
20659 yield information that is only relevant to the developers of our standard
20660 containers:
20661
20662 @quotation
20663
20664 @example
20665 (gdb) print map
20666 $1 = (
20667 tree => (
20668 first => 0x64e010,
20669 last => 0x64e070,
20670 root => 0x64e040,
20671 length => 3,
20672 tc => (
20673 busy => 0,
20674 lock => 0
20675 )
20676 )
20677 )
20678 @end example
20679 @end quotation
20680
20681 Fortunately, GDB has a feature called pretty-printers@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Introduction},
20682 which allows customizing how GDB displays data structures. The GDB
20683 shipped with GNAT embeds such pretty-printers for the most common
20684 containers in the standard library. To enable them, either run the
20685 following command manually under GDB or add it to your @code{.gdbinit} file:
20686
20687 @quotation
20688
20689 @example
20690 python import gnatdbg; gnatdbg.setup()
20691 @end example
20692 @end quotation
20693
20694 Once this is done, GDB's @code{print} command will automatically use
20695 these pretty-printers when appropriate. Using the previous example:
20696
20697 @quotation
20698
20699 @example
20700 (gdb) print map
20701 $1 = pp.int_to_nat.map of length 3 = @{
20702 [1] = 10,
20703 [2] = 20,
20704 [3] = 30
20705 @}
20706 @end example
20707 @end quotation
20708
20709 Pretty-printers are invoked each time GDB tries to display a value,
20710 including when displaying the arguments of a called subprogram (in
20711 GDB's @code{backtrace} command) or when printing the value returned by a
20712 function (in GDB's @code{finish} command).
20713
20714 To display a value without involving pretty-printers, @code{print} can be
20715 invoked with its @code{/r} option:
20716
20717 @quotation
20718
20719 @example
20720 (gdb) print/r map
20721 $1 = (
20722 tree => (...
20723 @end example
20724 @end quotation
20725
20726 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}
20727 for more information.
20728
20729 @geindex Profiling
20730
20731 @node Profiling,Improving Performance,Running and Debugging Ada Programs,GNAT and Program Execution
20732 @anchor{gnat_ugn/gnat_and_program_execution profiling}@anchor{25}@anchor{gnat_ugn/gnat_and_program_execution id20}@anchor{18f}
20733 @section Profiling
20734
20735
20736 This section describes how to use the @code{gprof} profiler tool on Ada programs.
20737
20738 @geindex gprof
20739
20740 @geindex Profiling
20741
20742 @menu
20743 * Profiling an Ada Program with gprof::
20744
20745 @end menu
20746
20747 @node Profiling an Ada Program with gprof,,,Profiling
20748 @anchor{gnat_ugn/gnat_and_program_execution id21}@anchor{190}@anchor{gnat_ugn/gnat_and_program_execution profiling-an-ada-program-with-gprof}@anchor{191}
20749 @subsection Profiling an Ada Program with gprof
20750
20751
20752 This section is not meant to be an exhaustive documentation of @code{gprof}.
20753 Full documentation for it can be found in the @cite{GNU Profiler User's Guide}
20754 documentation that is part of this GNAT distribution.
20755
20756 Profiling a program helps determine the parts of a program that are executed
20757 most often, and are therefore the most time-consuming.
20758
20759 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
20760 better handle Ada programs and multitasking.
20761 It is currently supported on the following platforms
20762
20763
20764 @itemize *
20765
20766 @item
20767 linux x86/x86_64
20768
20769 @item
20770 windows x86
20771 @end itemize
20772
20773 In order to profile a program using @code{gprof}, several steps are needed:
20774
20775
20776 @enumerate
20777
20778 @item
20779 Instrument the code, which requires a full recompilation of the project with the
20780 proper switches.
20781
20782 @item
20783 Execute the program under the analysis conditions, i.e. with the desired
20784 input.
20785
20786 @item
20787 Analyze the results using the @code{gprof} tool.
20788 @end enumerate
20789
20790 The following sections detail the different steps, and indicate how
20791 to interpret the results.
20792
20793 @menu
20794 * Compilation for profiling::
20795 * Program execution::
20796 * Running gprof::
20797 * Interpretation of profiling results::
20798
20799 @end menu
20800
20801 @node Compilation for profiling,Program execution,,Profiling an Ada Program with gprof
20802 @anchor{gnat_ugn/gnat_and_program_execution id22}@anchor{192}@anchor{gnat_ugn/gnat_and_program_execution compilation-for-profiling}@anchor{193}
20803 @subsubsection Compilation for profiling
20804
20805
20806 @geindex -pg (gcc)
20807 @geindex for profiling
20808
20809 @geindex -pg (gnatlink)
20810 @geindex for profiling
20811
20812 In order to profile a program the first step is to tell the compiler
20813 to generate the necessary profiling information. The compiler switch to be used
20814 is @code{-pg}, which must be added to other compilation switches. This
20815 switch needs to be specified both during compilation and link stages, and can
20816 be specified once when using gnatmake:
20817
20818 @quotation
20819
20820 @example
20821 $ gnatmake -f -pg -P my_project
20822 @end example
20823 @end quotation
20824
20825 Note that only the objects that were compiled with the @code{-pg} switch will
20826 be profiled; if you need to profile your whole project, use the @code{-f}
20827 gnatmake switch to force full recompilation.
20828
20829 @node Program execution,Running gprof,Compilation for profiling,Profiling an Ada Program with gprof
20830 @anchor{gnat_ugn/gnat_and_program_execution program-execution}@anchor{194}@anchor{gnat_ugn/gnat_and_program_execution id23}@anchor{195}
20831 @subsubsection Program execution
20832
20833
20834 Once the program has been compiled for profiling, you can run it as usual.
20835
20836 The only constraint imposed by profiling is that the program must terminate
20837 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
20838 properly analyzed.
20839
20840 Once the program completes execution, a data file called @code{gmon.out} is
20841 generated in the directory where the program was launched from. If this file
20842 already exists, it will be overwritten.
20843
20844 @node Running gprof,Interpretation of profiling results,Program execution,Profiling an Ada Program with gprof
20845 @anchor{gnat_ugn/gnat_and_program_execution running-gprof}@anchor{196}@anchor{gnat_ugn/gnat_and_program_execution id24}@anchor{197}
20846 @subsubsection Running gprof
20847
20848
20849 The @code{gprof} tool is called as follow:
20850
20851 @quotation
20852
20853 @example
20854 $ gprof my_prog gmon.out
20855 @end example
20856 @end quotation
20857
20858 or simply:
20859
20860 @quotation
20861
20862 @example
20863 $ gprof my_prog
20864 @end example
20865 @end quotation
20866
20867 The complete form of the gprof command line is the following:
20868
20869 @quotation
20870
20871 @example
20872 $ gprof [switches] [executable [data-file]]
20873 @end example
20874 @end quotation
20875
20876 @code{gprof} supports numerous switches. The order of these
20877 switch does not matter. The full list of options can be found in
20878 the GNU Profiler User's Guide documentation that comes with this documentation.
20879
20880 The following is the subset of those switches that is most relevant:
20881
20882 @geindex --demangle (gprof)
20883
20884
20885 @table @asis
20886
20887 @item @code{--demangle[=@emph{style}]}, @code{--no-demangle}
20888
20889 These options control whether symbol names should be demangled when
20890 printing output. The default is to demangle C++ symbols. The
20891 @code{--no-demangle} option may be used to turn off demangling. Different
20892 compilers have different mangling styles. The optional demangling style
20893 argument can be used to choose an appropriate demangling style for your
20894 compiler, in particular Ada symbols generated by GNAT can be demangled using
20895 @code{--demangle=gnat}.
20896 @end table
20897
20898 @geindex -e (gprof)
20899
20900
20901 @table @asis
20902
20903 @item @code{-e @emph{function_name}}
20904
20905 The @code{-e @emph{function}} option tells @code{gprof} not to print
20906 information about the function @code{function_name} (and its
20907 children...) in the call graph. The function will still be listed
20908 as a child of any functions that call it, but its index number will be
20909 shown as @code{[not printed]}. More than one @code{-e} option may be
20910 given; only one @code{function_name} may be indicated with each @code{-e}
20911 option.
20912 @end table
20913
20914 @geindex -E (gprof)
20915
20916
20917 @table @asis
20918
20919 @item @code{-E @emph{function_name}}
20920
20921 The @code{-E @emph{function}} option works like the @code{-e} option, but
20922 execution time spent in the function (and children who were not called from
20923 anywhere else), will not be used to compute the percentages-of-time for
20924 the call graph. More than one @code{-E} option may be given; only one
20925 @code{function_name} may be indicated with each @code{-E`} option.
20926 @end table
20927
20928 @geindex -f (gprof)
20929
20930
20931 @table @asis
20932
20933 @item @code{-f @emph{function_name}}
20934
20935 The @code{-f @emph{function}} option causes @code{gprof} to limit the
20936 call graph to the function @code{function_name} and its children (and
20937 their children...). More than one @code{-f} option may be given;
20938 only one @code{function_name} may be indicated with each @code{-f}
20939 option.
20940 @end table
20941
20942 @geindex -F (gprof)
20943
20944
20945 @table @asis
20946
20947 @item @code{-F @emph{function_name}}
20948
20949 The @code{-F @emph{function}} option works like the @code{-f} option, but
20950 only time spent in the function and its children (and their
20951 children...) will be used to determine total-time and
20952 percentages-of-time for the call graph. More than one @code{-F} option
20953 may be given; only one @code{function_name} may be indicated with each
20954 @code{-F} option. The @code{-F} option overrides the @code{-E} option.
20955 @end table
20956
20957 @node Interpretation of profiling results,,Running gprof,Profiling an Ada Program with gprof
20958 @anchor{gnat_ugn/gnat_and_program_execution id25}@anchor{198}@anchor{gnat_ugn/gnat_and_program_execution interpretation-of-profiling-results}@anchor{199}
20959 @subsubsection Interpretation of profiling results
20960
20961
20962 The results of the profiling analysis are represented by two arrays: the
20963 'flat profile' and the 'call graph'. Full documentation of those outputs
20964 can be found in the GNU Profiler User's Guide.
20965
20966 The flat profile shows the time spent in each function of the program, and how
20967 many time it has been called. This allows you to locate easily the most
20968 time-consuming functions.
20969
20970 The call graph shows, for each subprogram, the subprograms that call it,
20971 and the subprograms that it calls. It also provides an estimate of the time
20972 spent in each of those callers/called subprograms.
20973
20974 @node Improving Performance,Overflow Check Handling in GNAT,Profiling,GNAT and Program Execution
20975 @anchor{gnat_ugn/gnat_and_program_execution improving-performance}@anchor{26}@anchor{gnat_ugn/gnat_and_program_execution id26}@anchor{168}
20976 @section Improving Performance
20977
20978
20979 @geindex Improving performance
20980
20981 This section presents several topics related to program performance.
20982 It first describes some of the tradeoffs that need to be considered
20983 and some of the techniques for making your program run faster.
20984
20985 It then documents the unused subprogram/data elimination feature,
20986 which can reduce the size of program executables.
20987
20988 @menu
20989 * Performance Considerations::
20990 * Text_IO Suggestions::
20991 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
20992
20993 @end menu
20994
20995 @node Performance Considerations,Text_IO Suggestions,,Improving Performance
20996 @anchor{gnat_ugn/gnat_and_program_execution performance-considerations}@anchor{19a}@anchor{gnat_ugn/gnat_and_program_execution id27}@anchor{19b}
20997 @subsection Performance Considerations
20998
20999
21000 The GNAT system provides a number of options that allow a trade-off
21001 between
21002
21003
21004 @itemize *
21005
21006 @item
21007 performance of the generated code
21008
21009 @item
21010 speed of compilation
21011
21012 @item
21013 minimization of dependences and recompilation
21014
21015 @item
21016 the degree of run-time checking.
21017 @end itemize
21018
21019 The defaults (if no options are selected) aim at improving the speed
21020 of compilation and minimizing dependences, at the expense of performance
21021 of the generated code:
21022
21023
21024 @itemize *
21025
21026 @item
21027 no optimization
21028
21029 @item
21030 no inlining of subprogram calls
21031
21032 @item
21033 all run-time checks enabled except overflow and elaboration checks
21034 @end itemize
21035
21036 These options are suitable for most program development purposes. This
21037 section describes how you can modify these choices, and also provides
21038 some guidelines on debugging optimized code.
21039
21040 @menu
21041 * Controlling Run-Time Checks::
21042 * Use of Restrictions::
21043 * Optimization Levels::
21044 * Debugging Optimized Code::
21045 * Inlining of Subprograms::
21046 * Floating_Point_Operations::
21047 * Vectorization of loops::
21048 * Other Optimization Switches::
21049 * Optimization and Strict Aliasing::
21050 * Aliased Variables and Optimization::
21051 * Atomic Variables and Optimization::
21052 * Passive Task Optimization::
21053
21054 @end menu
21055
21056 @node Controlling Run-Time Checks,Use of Restrictions,,Performance Considerations
21057 @anchor{gnat_ugn/gnat_and_program_execution id28}@anchor{19c}@anchor{gnat_ugn/gnat_and_program_execution controlling-run-time-checks}@anchor{19d}
21058 @subsubsection Controlling Run-Time Checks
21059
21060
21061 By default, GNAT generates all run-time checks, except stack overflow
21062 checks, and checks for access before elaboration on subprogram
21063 calls. The latter are not required in default mode, because all
21064 necessary checking is done at compile time.
21065
21066 @geindex -gnatp (gcc)
21067
21068 @geindex -gnato (gcc)
21069
21070 The gnat switch, @code{-gnatp} allows this default to be modified. See
21071 @ref{f9,,Run-Time Checks}.
21072
21073 Our experience is that the default is suitable for most development
21074 purposes.
21075
21076 Elaboration checks are off by default, and also not needed by default, since
21077 GNAT uses a static elaboration analysis approach that avoids the need for
21078 run-time checking. This manual contains a full chapter discussing the issue
21079 of elaboration checks, and if the default is not satisfactory for your use,
21080 you should read this chapter.
21081
21082 For validity checks, the minimal checks required by the Ada Reference
21083 Manual (for case statements and assignments to array elements) are on
21084 by default. These can be suppressed by use of the @code{-gnatVn} switch.
21085 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
21086 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
21087 it may be reasonable to routinely use @code{-gnatVn}. Validity checks
21088 are also suppressed entirely if @code{-gnatp} is used.
21089
21090 @geindex Overflow checks
21091
21092 @geindex Checks
21093 @geindex overflow
21094
21095 @geindex Suppress
21096
21097 @geindex Unsuppress
21098
21099 @geindex pragma Suppress
21100
21101 @geindex pragma Unsuppress
21102
21103 Note that the setting of the switches controls the default setting of
21104 the checks. They may be modified using either @code{pragma Suppress} (to
21105 remove checks) or @code{pragma Unsuppress} (to add back suppressed
21106 checks) in the program source.
21107
21108 @node Use of Restrictions,Optimization Levels,Controlling Run-Time Checks,Performance Considerations
21109 @anchor{gnat_ugn/gnat_and_program_execution id29}@anchor{19e}@anchor{gnat_ugn/gnat_and_program_execution use-of-restrictions}@anchor{19f}
21110 @subsubsection Use of Restrictions
21111
21112
21113 The use of pragma Restrictions allows you to control which features are
21114 permitted in your program. Apart from the obvious point that if you avoid
21115 relatively expensive features like finalization (enforceable by the use
21116 of pragma Restrictions (No_Finalization), the use of this pragma does not
21117 affect the generated code in most cases.
21118
21119 One notable exception to this rule is that the possibility of task abort
21120 results in some distributed overhead, particularly if finalization or
21121 exception handlers are used. The reason is that certain sections of code
21122 have to be marked as non-abortable.
21123
21124 If you use neither the @code{abort} statement, nor asynchronous transfer
21125 of control (@code{select ... then abort}), then this distributed overhead
21126 is removed, which may have a general positive effect in improving
21127 overall performance. Especially code involving frequent use of tasking
21128 constructs and controlled types will show much improved performance.
21129 The relevant restrictions pragmas are
21130
21131 @quotation
21132
21133 @example
21134 pragma Restrictions (No_Abort_Statements);
21135 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
21136 @end example
21137 @end quotation
21138
21139 It is recommended that these restriction pragmas be used if possible. Note
21140 that this also means that you can write code without worrying about the
21141 possibility of an immediate abort at any point.
21142
21143 @node Optimization Levels,Debugging Optimized Code,Use of Restrictions,Performance Considerations
21144 @anchor{gnat_ugn/gnat_and_program_execution id30}@anchor{1a0}@anchor{gnat_ugn/gnat_and_program_execution optimization-levels}@anchor{fc}
21145 @subsubsection Optimization Levels
21146
21147
21148 @geindex -O (gcc)
21149
21150 Without any optimization option,
21151 the compiler's goal is to reduce the cost of
21152 compilation and to make debugging produce the expected results.
21153 Statements are independent: if you stop the program with a breakpoint between
21154 statements, you can then assign a new value to any variable or change
21155 the program counter to any other statement in the subprogram and get exactly
21156 the results you would expect from the source code.
21157
21158 Turning on optimization makes the compiler attempt to improve the
21159 performance and/or code size at the expense of compilation time and
21160 possibly the ability to debug the program.
21161
21162 If you use multiple
21163 -O options, with or without level numbers,
21164 the last such option is the one that is effective.
21165
21166 The default is optimization off. This results in the fastest compile
21167 times, but GNAT makes absolutely no attempt to optimize, and the
21168 generated programs are considerably larger and slower than when
21169 optimization is enabled. You can use the
21170 @code{-O} switch (the permitted forms are @code{-O0}, @code{-O1}
21171 @code{-O2}, @code{-O3}, and @code{-Os})
21172 to @code{gcc} to control the optimization level:
21173
21174
21175 @itemize *
21176
21177 @item
21178
21179 @table @asis
21180
21181 @item @code{-O0}
21182
21183 No optimization (the default);
21184 generates unoptimized code but has
21185 the fastest compilation time.
21186
21187 Note that many other compilers do substantial optimization even
21188 if 'no optimization' is specified. With gcc, it is very unusual
21189 to use @code{-O0} for production if execution time is of any concern,
21190 since @code{-O0} means (almost) no optimization. This difference
21191 between gcc and other compilers should be kept in mind when
21192 doing performance comparisons.
21193 @end table
21194
21195 @item
21196
21197 @table @asis
21198
21199 @item @code{-O1}
21200
21201 Moderate optimization;
21202 optimizes reasonably well but does not
21203 degrade compilation time significantly.
21204 @end table
21205
21206 @item
21207
21208 @table @asis
21209
21210 @item @code{-O2}
21211
21212 Full optimization;
21213 generates highly optimized code and has
21214 the slowest compilation time.
21215 @end table
21216
21217 @item
21218
21219 @table @asis
21220
21221 @item @code{-O3}
21222
21223 Full optimization as in @code{-O2};
21224 also uses more aggressive automatic inlining of subprograms within a unit
21225 (@ref{10f,,Inlining of Subprograms}) and attempts to vectorize loops.
21226 @end table
21227
21228 @item
21229
21230 @table @asis
21231
21232 @item @code{-Os}
21233
21234 Optimize space usage (code and data) of resulting program.
21235 @end table
21236 @end itemize
21237
21238 Higher optimization levels perform more global transformations on the
21239 program and apply more expensive analysis algorithms in order to generate
21240 faster and more compact code. The price in compilation time, and the
21241 resulting improvement in execution time,
21242 both depend on the particular application and the hardware environment.
21243 You should experiment to find the best level for your application.
21244
21245 Since the precise set of optimizations done at each level will vary from
21246 release to release (and sometime from target to target), it is best to think
21247 of the optimization settings in general terms.
21248 See the @emph{Options That Control Optimization} section in
21249 @cite{Using the GNU Compiler Collection (GCC)}
21250 for details about
21251 the @code{-O} settings and a number of @code{-f} options that
21252 individually enable or disable specific optimizations.
21253
21254 Unlike some other compilation systems, @code{gcc} has
21255 been tested extensively at all optimization levels. There are some bugs
21256 which appear only with optimization turned on, but there have also been
21257 bugs which show up only in @emph{unoptimized} code. Selecting a lower
21258 level of optimization does not improve the reliability of the code
21259 generator, which in practice is highly reliable at all optimization
21260 levels.
21261
21262 Note regarding the use of @code{-O3}: The use of this optimization level
21263 ought not to be automatically preferred over that of level @code{-O2},
21264 since it often results in larger executables which may run more slowly.
21265 See further discussion of this point in @ref{10f,,Inlining of Subprograms}.
21266
21267 @node Debugging Optimized Code,Inlining of Subprograms,Optimization Levels,Performance Considerations
21268 @anchor{gnat_ugn/gnat_and_program_execution debugging-optimized-code}@anchor{1a1}@anchor{gnat_ugn/gnat_and_program_execution id31}@anchor{1a2}
21269 @subsubsection Debugging Optimized Code
21270
21271
21272 @geindex Debugging optimized code
21273
21274 @geindex Optimization and debugging
21275
21276 Although it is possible to do a reasonable amount of debugging at
21277 nonzero optimization levels,
21278 the higher the level the more likely that
21279 source-level constructs will have been eliminated by optimization.
21280 For example, if a loop is strength-reduced, the loop
21281 control variable may be completely eliminated and thus cannot be
21282 displayed in the debugger.
21283 This can only happen at @code{-O2} or @code{-O3}.
21284 Explicit temporary variables that you code might be eliminated at
21285 level @code{-O1} or higher.
21286
21287 @geindex -g (gcc)
21288
21289 The use of the @code{-g} switch,
21290 which is needed for source-level debugging,
21291 affects the size of the program executable on disk,
21292 and indeed the debugging information can be quite large.
21293 However, it has no effect on the generated code (and thus does not
21294 degrade performance)
21295
21296 Since the compiler generates debugging tables for a compilation unit before
21297 it performs optimizations, the optimizing transformations may invalidate some
21298 of the debugging data. You therefore need to anticipate certain
21299 anomalous situations that may arise while debugging optimized code.
21300 These are the most common cases:
21301
21302
21303 @itemize *
21304
21305 @item
21306 @emph{The 'hopping Program Counter':} Repeated @code{step} or @code{next}
21307 commands show
21308 the PC bouncing back and forth in the code. This may result from any of
21309 the following optimizations:
21310
21311
21312 @itemize -
21313
21314 @item
21315 @emph{Common subexpression elimination:} using a single instance of code for a
21316 quantity that the source computes several times. As a result you
21317 may not be able to stop on what looks like a statement.
21318
21319 @item
21320 @emph{Invariant code motion:} moving an expression that does not change within a
21321 loop, to the beginning of the loop.
21322
21323 @item
21324 @emph{Instruction scheduling:} moving instructions so as to
21325 overlap loads and stores (typically) with other code, or in
21326 general to move computations of values closer to their uses. Often
21327 this causes you to pass an assignment statement without the assignment
21328 happening and then later bounce back to the statement when the
21329 value is actually needed. Placing a breakpoint on a line of code
21330 and then stepping over it may, therefore, not always cause all the
21331 expected side-effects.
21332 @end itemize
21333
21334 @item
21335 @emph{The 'big leap':} More commonly known as @emph{cross-jumping}, in which
21336 two identical pieces of code are merged and the program counter suddenly
21337 jumps to a statement that is not supposed to be executed, simply because
21338 it (and the code following) translates to the same thing as the code
21339 that @emph{was} supposed to be executed. This effect is typically seen in
21340 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
21341 a @code{break} in a C @code{switch} statement.
21342
21343 @item
21344 @emph{The 'roving variable':} The symptom is an unexpected value in a variable.
21345 There are various reasons for this effect:
21346
21347
21348 @itemize -
21349
21350 @item
21351 In a subprogram prologue, a parameter may not yet have been moved to its
21352 'home'.
21353
21354 @item
21355 A variable may be dead, and its register re-used. This is
21356 probably the most common cause.
21357
21358 @item
21359 As mentioned above, the assignment of a value to a variable may
21360 have been moved.
21361
21362 @item
21363 A variable may be eliminated entirely by value propagation or
21364 other means. In this case, GCC may incorrectly generate debugging
21365 information for the variable
21366 @end itemize
21367
21368 In general, when an unexpected value appears for a local variable or parameter
21369 you should first ascertain if that value was actually computed by
21370 your program, as opposed to being incorrectly reported by the debugger.
21371 Record fields or
21372 array elements in an object designated by an access value
21373 are generally less of a problem, once you have ascertained that the access
21374 value is sensible.
21375 Typically, this means checking variables in the preceding code and in the
21376 calling subprogram to verify that the value observed is explainable from other
21377 values (one must apply the procedure recursively to those
21378 other values); or re-running the code and stopping a little earlier
21379 (perhaps before the call) and stepping to better see how the variable obtained
21380 the value in question; or continuing to step @emph{from} the point of the
21381 strange value to see if code motion had simply moved the variable's
21382 assignments later.
21383 @end itemize
21384
21385 In light of such anomalies, a recommended technique is to use @code{-O0}
21386 early in the software development cycle, when extensive debugging capabilities
21387 are most needed, and then move to @code{-O1} and later @code{-O2} as
21388 the debugger becomes less critical.
21389 Whether to use the @code{-g} switch in the release version is
21390 a release management issue.
21391 Note that if you use @code{-g} you can then use the @code{strip} program
21392 on the resulting executable,
21393 which removes both debugging information and global symbols.
21394
21395 @node Inlining of Subprograms,Floating_Point_Operations,Debugging Optimized Code,Performance Considerations
21396 @anchor{gnat_ugn/gnat_and_program_execution id32}@anchor{1a3}@anchor{gnat_ugn/gnat_and_program_execution inlining-of-subprograms}@anchor{10f}
21397 @subsubsection Inlining of Subprograms
21398
21399
21400 A call to a subprogram in the current unit is inlined if all the
21401 following conditions are met:
21402
21403
21404 @itemize *
21405
21406 @item
21407 The optimization level is at least @code{-O1}.
21408
21409 @item
21410 The called subprogram is suitable for inlining: It must be small enough
21411 and not contain something that @code{gcc} cannot support in inlined
21412 subprograms.
21413
21414 @geindex pragma Inline
21415
21416 @geindex Inline
21417
21418 @item
21419 Any one of the following applies: @code{pragma Inline} is applied to the
21420 subprogram; the subprogram is local to the unit and called once from
21421 within it; the subprogram is small and optimization level @code{-O2} is
21422 specified; optimization level @code{-O3} is specified.
21423 @end itemize
21424
21425 Calls to subprograms in @emph{with}ed units are normally not inlined.
21426 To achieve actual inlining (that is, replacement of the call by the code
21427 in the body of the subprogram), the following conditions must all be true:
21428
21429
21430 @itemize *
21431
21432 @item
21433 The optimization level is at least @code{-O1}.
21434
21435 @item
21436 The called subprogram is suitable for inlining: It must be small enough
21437 and not contain something that @code{gcc} cannot support in inlined
21438 subprograms.
21439
21440 @item
21441 There is a @code{pragma Inline} for the subprogram.
21442
21443 @item
21444 The @code{-gnatn} switch is used on the command line.
21445 @end itemize
21446
21447 Even if all these conditions are met, it may not be possible for
21448 the compiler to inline the call, due to the length of the body,
21449 or features in the body that make it impossible for the compiler
21450 to do the inlining.
21451
21452 Note that specifying the @code{-gnatn} switch causes additional
21453 compilation dependencies. Consider the following:
21454
21455 @quotation
21456
21457 @example
21458 package R is
21459 procedure Q;
21460 pragma Inline (Q);
21461 end R;
21462 package body R is
21463 ...
21464 end R;
21465
21466 with R;
21467 procedure Main is
21468 begin
21469 ...
21470 R.Q;
21471 end Main;
21472 @end example
21473 @end quotation
21474
21475 With the default behavior (no @code{-gnatn} switch specified), the
21476 compilation of the @code{Main} procedure depends only on its own source,
21477 @code{main.adb}, and the spec of the package in file @code{r.ads}. This
21478 means that editing the body of @code{R} does not require recompiling
21479 @code{Main}.
21480
21481 On the other hand, the call @code{R.Q} is not inlined under these
21482 circumstances. If the @code{-gnatn} switch is present when @code{Main}
21483 is compiled, the call will be inlined if the body of @code{Q} is small
21484 enough, but now @code{Main} depends on the body of @code{R} in
21485 @code{r.adb} as well as on the spec. This means that if this body is edited,
21486 the main program must be recompiled. Note that this extra dependency
21487 occurs whether or not the call is in fact inlined by @code{gcc}.
21488
21489 The use of front end inlining with @code{-gnatN} generates similar
21490 additional dependencies.
21491
21492 @geindex -fno-inline (gcc)
21493
21494 Note: The @code{-fno-inline} switch overrides all other conditions and ensures that
21495 no inlining occurs, unless requested with pragma Inline_Always for @code{gcc}
21496 back-ends. The extra dependences resulting from @code{-gnatn} will still be active,
21497 even if this switch is used to suppress the resulting inlining actions.
21498
21499 @geindex -fno-inline-functions (gcc)
21500
21501 Note: The @code{-fno-inline-functions} switch can be used to prevent
21502 automatic inlining of subprograms if @code{-O3} is used.
21503
21504 @geindex -fno-inline-small-functions (gcc)
21505
21506 Note: The @code{-fno-inline-small-functions} switch can be used to prevent
21507 automatic inlining of small subprograms if @code{-O2} is used.
21508
21509 @geindex -fno-inline-functions-called-once (gcc)
21510
21511 Note: The @code{-fno-inline-functions-called-once} switch
21512 can be used to prevent inlining of subprograms local to the unit
21513 and called once from within it if @code{-O1} is used.
21514
21515 Note regarding the use of @code{-O3}: @code{-gnatn} is made up of two
21516 sub-switches @code{-gnatn1} and @code{-gnatn2} that can be directly
21517 specified in lieu of it, @code{-gnatn} being translated into one of them
21518 based on the optimization level. With @code{-O2} or below, @code{-gnatn}
21519 is equivalent to @code{-gnatn1} which activates pragma @code{Inline} with
21520 moderate inlining across modules. With @code{-O3}, @code{-gnatn} is
21521 equivalent to @code{-gnatn2} which activates pragma @code{Inline} with
21522 full inlining across modules. If you have used pragma @code{Inline} in
21523 appropriate cases, then it is usually much better to use @code{-O2}
21524 and @code{-gnatn} and avoid the use of @code{-O3} which has the additional
21525 effect of inlining subprograms you did not think should be inlined. We have
21526 found that the use of @code{-O3} may slow down the compilation and increase
21527 the code size by performing excessive inlining, leading to increased
21528 instruction cache pressure from the increased code size and thus minor
21529 performance improvements. So the bottom line here is that you should not
21530 automatically assume that @code{-O3} is better than @code{-O2}, and
21531 indeed you should use @code{-O3} only if tests show that it actually
21532 improves performance for your program.
21533
21534 @node Floating_Point_Operations,Vectorization of loops,Inlining of Subprograms,Performance Considerations
21535 @anchor{gnat_ugn/gnat_and_program_execution floating-point-operations}@anchor{1a4}@anchor{gnat_ugn/gnat_and_program_execution id33}@anchor{1a5}
21536 @subsubsection Floating_Point_Operations
21537
21538
21539 @geindex Floating-Point Operations
21540
21541 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
21542 64-bit standard IEEE floating-point representations, and operations will
21543 use standard IEEE arithmetic as provided by the processor. On most, but
21544 not all, architectures, the attribute Machine_Overflows is False for these
21545 types, meaning that the semantics of overflow is implementation-defined.
21546 In the case of GNAT, these semantics correspond to the normal IEEE
21547 treatment of infinities and NaN (not a number) values. For example,
21548 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
21549 avoiding explicit overflow checks, the performance is greatly improved
21550 on many targets. However, if required, floating-point overflow can be
21551 enabled by the use of the pragma Check_Float_Overflow.
21552
21553 Another consideration that applies specifically to x86 32-bit
21554 architectures is which form of floating-point arithmetic is used.
21555 By default the operations use the old style x86 floating-point,
21556 which implements an 80-bit extended precision form (on these
21557 architectures the type Long_Long_Float corresponds to that form).
21558 In addition, generation of efficient code in this mode means that
21559 the extended precision form will be used for intermediate results.
21560 This may be helpful in improving the final precision of a complex
21561 expression. However it means that the results obtained on the x86
21562 will be different from those on other architectures, and for some
21563 algorithms, the extra intermediate precision can be detrimental.
21564
21565 In addition to this old-style floating-point, all modern x86 chips
21566 implement an alternative floating-point operation model referred
21567 to as SSE2. In this model there is no extended form, and furthermore
21568 execution performance is significantly enhanced. To force GNAT to use
21569 this more modern form, use both of the switches:
21570
21571 @quotation
21572
21573 -msse2 -mfpmath=sse
21574 @end quotation
21575
21576 A unit compiled with these switches will automatically use the more
21577 efficient SSE2 instruction set for Float and Long_Float operations.
21578 Note that the ABI has the same form for both floating-point models,
21579 so it is permissible to mix units compiled with and without these
21580 switches.
21581
21582 @node Vectorization of loops,Other Optimization Switches,Floating_Point_Operations,Performance Considerations
21583 @anchor{gnat_ugn/gnat_and_program_execution id34}@anchor{1a6}@anchor{gnat_ugn/gnat_and_program_execution vectorization-of-loops}@anchor{1a7}
21584 @subsubsection Vectorization of loops
21585
21586
21587 @geindex Optimization Switches
21588
21589 You can take advantage of the auto-vectorizer present in the @code{gcc}
21590 back end to vectorize loops with GNAT. The corresponding command line switch
21591 is @code{-ftree-vectorize} but, as it is enabled by default at @code{-O3}
21592 and other aggressive optimizations helpful for vectorization also are enabled
21593 by default at this level, using @code{-O3} directly is recommended.
21594
21595 You also need to make sure that the target architecture features a supported
21596 SIMD instruction set. For example, for the x86 architecture, you should at
21597 least specify @code{-msse2} to get significant vectorization (but you don't
21598 need to specify it for x86-64 as it is part of the base 64-bit architecture).
21599 Similarly, for the PowerPC architecture, you should specify @code{-maltivec}.
21600
21601 The preferred loop form for vectorization is the @code{for} iteration scheme.
21602 Loops with a @code{while} iteration scheme can also be vectorized if they are
21603 very simple, but the vectorizer will quickly give up otherwise. With either
21604 iteration scheme, the flow of control must be straight, in particular no
21605 @code{exit} statement may appear in the loop body. The loop may however
21606 contain a single nested loop, if it can be vectorized when considered alone:
21607
21608 @quotation
21609
21610 @example
21611 A : array (1..4, 1..4) of Long_Float;
21612 S : array (1..4) of Long_Float;
21613
21614 procedure Sum is
21615 begin
21616 for I in A'Range(1) loop
21617 for J in A'Range(2) loop
21618 S (I) := S (I) + A (I, J);
21619 end loop;
21620 end loop;
21621 end Sum;
21622 @end example
21623 @end quotation
21624
21625 The vectorizable operations depend on the targeted SIMD instruction set, but
21626 the adding and some of the multiplying operators are generally supported, as
21627 well as the logical operators for modular types. Note that compiling
21628 with @code{-gnatp} might well reveal cases where some checks do thwart
21629 vectorization.
21630
21631 Type conversions may also prevent vectorization if they involve semantics that
21632 are not directly supported by the code generator or the SIMD instruction set.
21633 A typical example is direct conversion from floating-point to integer types.
21634 The solution in this case is to use the following idiom:
21635
21636 @quotation
21637
21638 @example
21639 Integer (S'Truncation (F))
21640 @end example
21641 @end quotation
21642
21643 if @code{S} is the subtype of floating-point object @code{F}.
21644
21645 In most cases, the vectorizable loops are loops that iterate over arrays.
21646 All kinds of array types are supported, i.e. constrained array types with
21647 static bounds:
21648
21649 @quotation
21650
21651 @example
21652 type Array_Type is array (1 .. 4) of Long_Float;
21653 @end example
21654 @end quotation
21655
21656 constrained array types with dynamic bounds:
21657
21658 @quotation
21659
21660 @example
21661 type Array_Type is array (1 .. Q.N) of Long_Float;
21662
21663 type Array_Type is array (Q.K .. 4) of Long_Float;
21664
21665 type Array_Type is array (Q.K .. Q.N) of Long_Float;
21666 @end example
21667 @end quotation
21668
21669 or unconstrained array types:
21670
21671 @quotation
21672
21673 @example
21674 type Array_Type is array (Positive range <>) of Long_Float;
21675 @end example
21676 @end quotation
21677
21678 The quality of the generated code decreases when the dynamic aspect of the
21679 array type increases, the worst code being generated for unconstrained array
21680 types. This is so because, the less information the compiler has about the
21681 bounds of the array, the more fallback code it needs to generate in order to
21682 fix things up at run time.
21683
21684 It is possible to specify that a given loop should be subject to vectorization
21685 preferably to other optimizations by means of pragma @code{Loop_Optimize}:
21686
21687 @quotation
21688
21689 @example
21690 pragma Loop_Optimize (Vector);
21691 @end example
21692 @end quotation
21693
21694 placed immediately within the loop will convey the appropriate hint to the
21695 compiler for this loop.
21696
21697 It is also possible to help the compiler generate better vectorized code
21698 for a given loop by asserting that there are no loop-carried dependencies
21699 in the loop. Consider for example the procedure:
21700
21701 @quotation
21702
21703 @example
21704 type Arr is array (1 .. 4) of Long_Float;
21705
21706 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
21707 begin
21708 for I in Arr'Range loop
21709 R(I) := X(I) + Y(I);
21710 end loop;
21711 end;
21712 @end example
21713 @end quotation
21714
21715 By default, the compiler cannot unconditionally vectorize the loop because
21716 assigning to a component of the array designated by R in one iteration could
21717 change the value read from the components of the array designated by X or Y
21718 in a later iteration. As a result, the compiler will generate two versions
21719 of the loop in the object code, one vectorized and the other not vectorized,
21720 as well as a test to select the appropriate version at run time. This can
21721 be overcome by another hint:
21722
21723 @quotation
21724
21725 @example
21726 pragma Loop_Optimize (Ivdep);
21727 @end example
21728 @end quotation
21729
21730 placed immediately within the loop will tell the compiler that it can safely
21731 omit the non-vectorized version of the loop as well as the run-time test.
21732
21733 @node Other Optimization Switches,Optimization and Strict Aliasing,Vectorization of loops,Performance Considerations
21734 @anchor{gnat_ugn/gnat_and_program_execution other-optimization-switches}@anchor{1a8}@anchor{gnat_ugn/gnat_and_program_execution id35}@anchor{1a9}
21735 @subsubsection Other Optimization Switches
21736
21737
21738 @geindex Optimization Switches
21739
21740 Since GNAT uses the @code{gcc} back end, all the specialized
21741 @code{gcc} optimization switches are potentially usable. These switches
21742 have not been extensively tested with GNAT but can generally be expected
21743 to work. Examples of switches in this category are @code{-funroll-loops}
21744 and the various target-specific @code{-m} options (in particular, it has
21745 been observed that @code{-march=xxx} can significantly improve performance
21746 on appropriate machines). For full details of these switches, see
21747 the @emph{Submodel Options} section in the @emph{Hardware Models and Configurations}
21748 chapter of @cite{Using the GNU Compiler Collection (GCC)}.
21749
21750 @node Optimization and Strict Aliasing,Aliased Variables and Optimization,Other Optimization Switches,Performance Considerations
21751 @anchor{gnat_ugn/gnat_and_program_execution optimization-and-strict-aliasing}@anchor{f3}@anchor{gnat_ugn/gnat_and_program_execution id36}@anchor{1aa}
21752 @subsubsection Optimization and Strict Aliasing
21753
21754
21755 @geindex Aliasing
21756
21757 @geindex Strict Aliasing
21758
21759 @geindex No_Strict_Aliasing
21760
21761 The strong typing capabilities of Ada allow an optimizer to generate
21762 efficient code in situations where other languages would be forced to
21763 make worst case assumptions preventing such optimizations. Consider
21764 the following example:
21765
21766 @quotation
21767
21768 @example
21769 procedure R is
21770 type Int1 is new Integer;
21771 type Int2 is new Integer;
21772 type Int1A is access Int1;
21773 type Int2A is access Int2;
21774 Int1V : Int1A;
21775 Int2V : Int2A;
21776 ...
21777
21778 begin
21779 ...
21780 for J in Data'Range loop
21781 if Data (J) = Int1V.all then
21782 Int2V.all := Int2V.all + 1;
21783 end if;
21784 end loop;
21785 ...
21786 end R;
21787 @end example
21788 @end quotation
21789
21790 In this example, since the variable @code{Int1V} can only access objects
21791 of type @code{Int1}, and @code{Int2V} can only access objects of type
21792 @code{Int2}, there is no possibility that the assignment to
21793 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
21794 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
21795 for all iterations of the loop and avoid the extra memory reference
21796 required to dereference it each time through the loop.
21797
21798 This kind of optimization, called strict aliasing analysis, is
21799 triggered by specifying an optimization level of @code{-O2} or
21800 higher or @code{-Os} and allows GNAT to generate more efficient code
21801 when access values are involved.
21802
21803 However, although this optimization is always correct in terms of
21804 the formal semantics of the Ada Reference Manual, difficulties can
21805 arise if features like @code{Unchecked_Conversion} are used to break
21806 the typing system. Consider the following complete program example:
21807
21808 @quotation
21809
21810 @example
21811 package p1 is
21812 type int1 is new integer;
21813 type int2 is new integer;
21814 type a1 is access int1;
21815 type a2 is access int2;
21816 end p1;
21817
21818 with p1; use p1;
21819 package p2 is
21820 function to_a2 (Input : a1) return a2;
21821 end p2;
21822
21823 with Unchecked_Conversion;
21824 package body p2 is
21825 function to_a2 (Input : a1) return a2 is
21826 function to_a2u is
21827 new Unchecked_Conversion (a1, a2);
21828 begin
21829 return to_a2u (Input);
21830 end to_a2;
21831 end p2;
21832
21833 with p2; use p2;
21834 with p1; use p1;
21835 with Text_IO; use Text_IO;
21836 procedure m is
21837 v1 : a1 := new int1;
21838 v2 : a2 := to_a2 (v1);
21839 begin
21840 v1.all := 1;
21841 v2.all := 0;
21842 put_line (int1'image (v1.all));
21843 end;
21844 @end example
21845 @end quotation
21846
21847 This program prints out 0 in @code{-O0} or @code{-O1}
21848 mode, but it prints out 1 in @code{-O2} mode. That's
21849 because in strict aliasing mode, the compiler can and
21850 does assume that the assignment to @code{v2.all} could not
21851 affect the value of @code{v1.all}, since different types
21852 are involved.
21853
21854 This behavior is not a case of non-conformance with the standard, since
21855 the Ada RM specifies that an unchecked conversion where the resulting
21856 bit pattern is not a correct value of the target type can result in an
21857 abnormal value and attempting to reference an abnormal value makes the
21858 execution of a program erroneous. That's the case here since the result
21859 does not point to an object of type @code{int2}. This means that the
21860 effect is entirely unpredictable.
21861
21862 However, although that explanation may satisfy a language
21863 lawyer, in practice an applications programmer expects an
21864 unchecked conversion involving pointers to create true
21865 aliases and the behavior of printing 1 seems plain wrong.
21866 In this case, the strict aliasing optimization is unwelcome.
21867
21868 Indeed the compiler recognizes this possibility, and the
21869 unchecked conversion generates a warning:
21870
21871 @quotation
21872
21873 @example
21874 p2.adb:5:07: warning: possible aliasing problem with type "a2"
21875 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
21876 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
21877 @end example
21878 @end quotation
21879
21880 Unfortunately the problem is recognized when compiling the body of
21881 package @code{p2}, but the actual "bad" code is generated while
21882 compiling the body of @code{m} and this latter compilation does not see
21883 the suspicious @code{Unchecked_Conversion}.
21884
21885 As implied by the warning message, there are approaches you can use to
21886 avoid the unwanted strict aliasing optimization in a case like this.
21887
21888 One possibility is to simply avoid the use of @code{-O2}, but
21889 that is a bit drastic, since it throws away a number of useful
21890 optimizations that do not involve strict aliasing assumptions.
21891
21892 A less drastic approach is to compile the program using the
21893 option @code{-fno-strict-aliasing}. Actually it is only the
21894 unit containing the dereferencing of the suspicious pointer
21895 that needs to be compiled. So in this case, if we compile
21896 unit @code{m} with this switch, then we get the expected
21897 value of zero printed. Analyzing which units might need
21898 the switch can be painful, so a more reasonable approach
21899 is to compile the entire program with options @code{-O2}
21900 and @code{-fno-strict-aliasing}. If the performance is
21901 satisfactory with this combination of options, then the
21902 advantage is that the entire issue of possible "wrong"
21903 optimization due to strict aliasing is avoided.
21904
21905 To avoid the use of compiler switches, the configuration
21906 pragma @code{No_Strict_Aliasing} with no parameters may be
21907 used to specify that for all access types, the strict
21908 aliasing optimization should be suppressed.
21909
21910 However, these approaches are still overkill, in that they causes
21911 all manipulations of all access values to be deoptimized. A more
21912 refined approach is to concentrate attention on the specific
21913 access type identified as problematic.
21914
21915 First, if a careful analysis of uses of the pointer shows
21916 that there are no possible problematic references, then
21917 the warning can be suppressed by bracketing the
21918 instantiation of @code{Unchecked_Conversion} to turn
21919 the warning off:
21920
21921 @quotation
21922
21923 @example
21924 pragma Warnings (Off);
21925 function to_a2u is
21926 new Unchecked_Conversion (a1, a2);
21927 pragma Warnings (On);
21928 @end example
21929 @end quotation
21930
21931 Of course that approach is not appropriate for this particular
21932 example, since indeed there is a problematic reference. In this
21933 case we can take one of two other approaches.
21934
21935 The first possibility is to move the instantiation of unchecked
21936 conversion to the unit in which the type is declared. In
21937 this example, we would move the instantiation of
21938 @code{Unchecked_Conversion} from the body of package
21939 @code{p2} to the spec of package @code{p1}. Now the
21940 warning disappears. That's because any use of the
21941 access type knows there is a suspicious unchecked
21942 conversion, and the strict aliasing optimization
21943 is automatically suppressed for the type.
21944
21945 If it is not practical to move the unchecked conversion to the same unit
21946 in which the destination access type is declared (perhaps because the
21947 source type is not visible in that unit), you may use pragma
21948 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
21949 same declarative sequence as the declaration of the access type:
21950
21951 @quotation
21952
21953 @example
21954 type a2 is access int2;
21955 pragma No_Strict_Aliasing (a2);
21956 @end example
21957 @end quotation
21958
21959 Here again, the compiler now knows that the strict aliasing optimization
21960 should be suppressed for any reference to type @code{a2} and the
21961 expected behavior is obtained.
21962
21963 Finally, note that although the compiler can generate warnings for
21964 simple cases of unchecked conversions, there are tricker and more
21965 indirect ways of creating type incorrect aliases which the compiler
21966 cannot detect. Examples are the use of address overlays and unchecked
21967 conversions involving composite types containing access types as
21968 components. In such cases, no warnings are generated, but there can
21969 still be aliasing problems. One safe coding practice is to forbid the
21970 use of address clauses for type overlaying, and to allow unchecked
21971 conversion only for primitive types. This is not really a significant
21972 restriction since any possible desired effect can be achieved by
21973 unchecked conversion of access values.
21974
21975 The aliasing analysis done in strict aliasing mode can certainly
21976 have significant benefits. We have seen cases of large scale
21977 application code where the time is increased by up to 5% by turning
21978 this optimization off. If you have code that includes significant
21979 usage of unchecked conversion, you might want to just stick with
21980 @code{-O1} and avoid the entire issue. If you get adequate
21981 performance at this level of optimization level, that's probably
21982 the safest approach. If tests show that you really need higher
21983 levels of optimization, then you can experiment with @code{-O2}
21984 and @code{-O2 -fno-strict-aliasing} to see how much effect this
21985 has on size and speed of the code. If you really need to use
21986 @code{-O2} with strict aliasing in effect, then you should
21987 review any uses of unchecked conversion of access types,
21988 particularly if you are getting the warnings described above.
21989
21990 @node Aliased Variables and Optimization,Atomic Variables and Optimization,Optimization and Strict Aliasing,Performance Considerations
21991 @anchor{gnat_ugn/gnat_and_program_execution id37}@anchor{1ab}@anchor{gnat_ugn/gnat_and_program_execution aliased-variables-and-optimization}@anchor{1ac}
21992 @subsubsection Aliased Variables and Optimization
21993
21994
21995 @geindex Aliasing
21996
21997 There are scenarios in which programs may
21998 use low level techniques to modify variables
21999 that otherwise might be considered to be unassigned. For example,
22000 a variable can be passed to a procedure by reference, which takes
22001 the address of the parameter and uses the address to modify the
22002 variable's value, even though it is passed as an IN parameter.
22003 Consider the following example:
22004
22005 @quotation
22006
22007 @example
22008 procedure P is
22009 Max_Length : constant Natural := 16;
22010 type Char_Ptr is access all Character;
22011
22012 procedure Get_String(Buffer: Char_Ptr; Size : Integer);
22013 pragma Import (C, Get_String, "get_string");
22014
22015 Name : aliased String (1 .. Max_Length) := (others => ' ');
22016 Temp : Char_Ptr;
22017
22018 function Addr (S : String) return Char_Ptr is
22019 function To_Char_Ptr is
22020 new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
22021 begin
22022 return To_Char_Ptr (S (S'First)'Address);
22023 end;
22024
22025 begin
22026 Temp := Addr (Name);
22027 Get_String (Temp, Max_Length);
22028 end;
22029 @end example
22030 @end quotation
22031
22032 where Get_String is a C function that uses the address in Temp to
22033 modify the variable @code{Name}. This code is dubious, and arguably
22034 erroneous, and the compiler would be entitled to assume that
22035 @code{Name} is never modified, and generate code accordingly.
22036
22037 However, in practice, this would cause some existing code that
22038 seems to work with no optimization to start failing at high
22039 levels of optimzization.
22040
22041 What the compiler does for such cases is to assume that marking
22042 a variable as aliased indicates that some "funny business" may
22043 be going on. The optimizer recognizes the aliased keyword and
22044 inhibits optimizations that assume the value cannot be assigned.
22045 This means that the above example will in fact "work" reliably,
22046 that is, it will produce the expected results.
22047
22048 @node Atomic Variables and Optimization,Passive Task Optimization,Aliased Variables and Optimization,Performance Considerations
22049 @anchor{gnat_ugn/gnat_and_program_execution atomic-variables-and-optimization}@anchor{1ad}@anchor{gnat_ugn/gnat_and_program_execution id38}@anchor{1ae}
22050 @subsubsection Atomic Variables and Optimization
22051
22052
22053 @geindex Atomic
22054
22055 There are two considerations with regard to performance when
22056 atomic variables are used.
22057
22058 First, the RM only guarantees that access to atomic variables
22059 be atomic, it has nothing to say about how this is achieved,
22060 though there is a strong implication that this should not be
22061 achieved by explicit locking code. Indeed GNAT will never
22062 generate any locking code for atomic variable access (it will
22063 simply reject any attempt to make a variable or type atomic
22064 if the atomic access cannot be achieved without such locking code).
22065
22066 That being said, it is important to understand that you cannot
22067 assume that the entire variable will always be accessed. Consider
22068 this example:
22069
22070 @quotation
22071
22072 @example
22073 type R is record
22074 A,B,C,D : Character;
22075 end record;
22076 for R'Size use 32;
22077 for R'Alignment use 4;
22078
22079 RV : R;
22080 pragma Atomic (RV);
22081 X : Character;
22082 ...
22083 X := RV.B;
22084 @end example
22085 @end quotation
22086
22087 You cannot assume that the reference to @code{RV.B}
22088 will read the entire 32-bit
22089 variable with a single load instruction. It is perfectly legitimate if
22090 the hardware allows it to do a byte read of just the B field. This read
22091 is still atomic, which is all the RM requires. GNAT can and does take
22092 advantage of this, depending on the architecture and optimization level.
22093 Any assumption to the contrary is non-portable and risky. Even if you
22094 examine the assembly language and see a full 32-bit load, this might
22095 change in a future version of the compiler.
22096
22097 If your application requires that all accesses to @code{RV} in this
22098 example be full 32-bit loads, you need to make a copy for the access
22099 as in:
22100
22101 @quotation
22102
22103 @example
22104 declare
22105 RV_Copy : constant R := RV;
22106 begin
22107 X := RV_Copy.B;
22108 end;
22109 @end example
22110 @end quotation
22111
22112 Now the reference to RV must read the whole variable.
22113 Actually one can imagine some compiler which figures
22114 out that the whole copy is not required (because only
22115 the B field is actually accessed), but GNAT
22116 certainly won't do that, and we don't know of any
22117 compiler that would not handle this right, and the
22118 above code will in practice work portably across
22119 all architectures (that permit the Atomic declaration).
22120
22121 The second issue with atomic variables has to do with
22122 the possible requirement of generating synchronization
22123 code. For more details on this, consult the sections on
22124 the pragmas Enable/Disable_Atomic_Synchronization in the
22125 GNAT Reference Manual. If performance is critical, and
22126 such synchronization code is not required, it may be
22127 useful to disable it.
22128
22129 @node Passive Task Optimization,,Atomic Variables and Optimization,Performance Considerations
22130 @anchor{gnat_ugn/gnat_and_program_execution passive-task-optimization}@anchor{1af}@anchor{gnat_ugn/gnat_and_program_execution id39}@anchor{1b0}
22131 @subsubsection Passive Task Optimization
22132
22133
22134 @geindex Passive Task
22135
22136 A passive task is one which is sufficiently simple that
22137 in theory a compiler could recognize it an implement it
22138 efficiently without creating a new thread. The original design
22139 of Ada 83 had in mind this kind of passive task optimization, but
22140 only a few Ada 83 compilers attempted it. The problem was that
22141 it was difficult to determine the exact conditions under which
22142 the optimization was possible. The result is a very fragile
22143 optimization where a very minor change in the program can
22144 suddenly silently make a task non-optimizable.
22145
22146 With the revisiting of this issue in Ada 95, there was general
22147 agreement that this approach was fundamentally flawed, and the
22148 notion of protected types was introduced. When using protected
22149 types, the restrictions are well defined, and you KNOW that the
22150 operations will be optimized, and furthermore this optimized
22151 performance is fully portable.
22152
22153 Although it would theoretically be possible for GNAT to attempt to
22154 do this optimization, but it really doesn't make sense in the
22155 context of Ada 95, and none of the Ada 95 compilers implement
22156 this optimization as far as we know. In particular GNAT never
22157 attempts to perform this optimization.
22158
22159 In any new Ada 95 code that is written, you should always
22160 use protected types in place of tasks that might be able to
22161 be optimized in this manner.
22162 Of course this does not help if you have legacy Ada 83 code
22163 that depends on this optimization, but it is unusual to encounter
22164 a case where the performance gains from this optimization
22165 are significant.
22166
22167 Your program should work correctly without this optimization. If
22168 you have performance problems, then the most practical
22169 approach is to figure out exactly where these performance problems
22170 arise, and update those particular tasks to be protected types. Note
22171 that typically clients of the tasks who call entries, will not have
22172 to be modified, only the task definition itself.
22173
22174 @node Text_IO Suggestions,Reducing Size of Executables with Unused Subprogram/Data Elimination,Performance Considerations,Improving Performance
22175 @anchor{gnat_ugn/gnat_and_program_execution text-io-suggestions}@anchor{1b1}@anchor{gnat_ugn/gnat_and_program_execution id40}@anchor{1b2}
22176 @subsection @code{Text_IO} Suggestions
22177
22178
22179 @geindex Text_IO and performance
22180
22181 The @code{Ada.Text_IO} package has fairly high overheads due in part to
22182 the requirement of maintaining page and line counts. If performance
22183 is critical, a recommendation is to use @code{Stream_IO} instead of
22184 @code{Text_IO} for volume output, since this package has less overhead.
22185
22186 If @code{Text_IO} must be used, note that by default output to the standard
22187 output and standard error files is unbuffered (this provides better
22188 behavior when output statements are used for debugging, or if the
22189 progress of a program is observed by tracking the output, e.g. by
22190 using the Unix @emph{tail -f} command to watch redirected output.
22191
22192 If you are generating large volumes of output with @code{Text_IO} and
22193 performance is an important factor, use a designated file instead
22194 of the standard output file, or change the standard output file to
22195 be buffered using @code{Interfaces.C_Streams.setvbuf}.
22196
22197 @node Reducing Size of Executables with Unused Subprogram/Data Elimination,,Text_IO Suggestions,Improving Performance
22198 @anchor{gnat_ugn/gnat_and_program_execution id41}@anchor{1b3}@anchor{gnat_ugn/gnat_and_program_execution reducing-size-of-executables-with-unused-subprogram-data-elimination}@anchor{1b4}
22199 @subsection Reducing Size of Executables with Unused Subprogram/Data Elimination
22200
22201
22202 @geindex Uunused subprogram/data elimination
22203
22204 This section describes how you can eliminate unused subprograms and data from
22205 your executable just by setting options at compilation time.
22206
22207 @menu
22208 * About unused subprogram/data elimination::
22209 * Compilation options::
22210 * Example of unused subprogram/data elimination::
22211
22212 @end menu
22213
22214 @node About unused subprogram/data elimination,Compilation options,,Reducing Size of Executables with Unused Subprogram/Data Elimination
22215 @anchor{gnat_ugn/gnat_and_program_execution id42}@anchor{1b5}@anchor{gnat_ugn/gnat_and_program_execution about-unused-subprogram-data-elimination}@anchor{1b6}
22216 @subsubsection About unused subprogram/data elimination
22217
22218
22219 By default, an executable contains all code and data of its composing objects
22220 (directly linked or coming from statically linked libraries), even data or code
22221 never used by this executable.
22222
22223 This feature will allow you to eliminate such unused code from your
22224 executable, making it smaller (in disk and in memory).
22225
22226 This functionality is available on all Linux platforms except for the IA-64
22227 architecture and on all cross platforms using the ELF binary file format.
22228 In both cases GNU binutils version 2.16 or later are required to enable it.
22229
22230 @node Compilation options,Example of unused subprogram/data elimination,About unused subprogram/data elimination,Reducing Size of Executables with Unused Subprogram/Data Elimination
22231 @anchor{gnat_ugn/gnat_and_program_execution id43}@anchor{1b7}@anchor{gnat_ugn/gnat_and_program_execution compilation-options}@anchor{1b8}
22232 @subsubsection Compilation options
22233
22234
22235 The operation of eliminating the unused code and data from the final executable
22236 is directly performed by the linker.
22237
22238 @geindex -ffunction-sections (gcc)
22239
22240 @geindex -fdata-sections (gcc)
22241
22242 In order to do this, it has to work with objects compiled with the
22243 following options:
22244 @code{-ffunction-sections} @code{-fdata-sections}.
22245
22246 These options are usable with C and Ada files.
22247 They will place respectively each
22248 function or data in a separate section in the resulting object file.
22249
22250 Once the objects and static libraries are created with these options, the
22251 linker can perform the dead code elimination. You can do this by setting
22252 the @code{-Wl,--gc-sections} option to gcc command or in the
22253 @code{-largs} section of @code{gnatmake}. This will perform a
22254 garbage collection of code and data never referenced.
22255
22256 If the linker performs a partial link (@code{-r} linker option), then you
22257 will need to provide the entry point using the @code{-e} / @code{--entry}
22258 linker option.
22259
22260 Note that objects compiled without the @code{-ffunction-sections} and
22261 @code{-fdata-sections} options can still be linked with the executable.
22262 However, no dead code elimination will be performed on those objects (they will
22263 be linked as is).
22264
22265 The GNAT static library is now compiled with -ffunction-sections and
22266 -fdata-sections on some platforms. This allows you to eliminate the unused code
22267 and data of the GNAT library from your executable.
22268
22269 @node Example of unused subprogram/data elimination,,Compilation options,Reducing Size of Executables with Unused Subprogram/Data Elimination
22270 @anchor{gnat_ugn/gnat_and_program_execution example-of-unused-subprogram-data-elimination}@anchor{1b9}@anchor{gnat_ugn/gnat_and_program_execution id44}@anchor{1ba}
22271 @subsubsection Example of unused subprogram/data elimination
22272
22273
22274 Here is a simple example:
22275
22276 @quotation
22277
22278 @example
22279 with Aux;
22280
22281 procedure Test is
22282 begin
22283 Aux.Used (10);
22284 end Test;
22285
22286 package Aux is
22287 Used_Data : Integer;
22288 Unused_Data : Integer;
22289
22290 procedure Used (Data : Integer);
22291 procedure Unused (Data : Integer);
22292 end Aux;
22293
22294 package body Aux is
22295 procedure Used (Data : Integer) is
22296 begin
22297 Used_Data := Data;
22298 end Used;
22299
22300 procedure Unused (Data : Integer) is
22301 begin
22302 Unused_Data := Data;
22303 end Unused;
22304 end Aux;
22305 @end example
22306 @end quotation
22307
22308 @code{Unused} and @code{Unused_Data} are never referenced in this code
22309 excerpt, and hence they may be safely removed from the final executable.
22310
22311 @quotation
22312
22313 @example
22314 $ gnatmake test
22315
22316 $ nm test | grep used
22317 020015f0 T aux__unused
22318 02005d88 B aux__unused_data
22319 020015cc T aux__used
22320 02005d84 B aux__used_data
22321
22322 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
22323 -largs -Wl,--gc-sections
22324
22325 $ nm test | grep used
22326 02005350 T aux__used
22327 0201ffe0 B aux__used_data
22328 @end example
22329 @end quotation
22330
22331 It can be observed that the procedure @code{Unused} and the object
22332 @code{Unused_Data} are removed by the linker when using the
22333 appropriate options.
22334
22335 @geindex Overflow checks
22336
22337 @geindex Checks (overflow)
22338
22339 @node Overflow Check Handling in GNAT,Performing Dimensionality Analysis in GNAT,Improving Performance,GNAT and Program Execution
22340 @anchor{gnat_ugn/gnat_and_program_execution id45}@anchor{169}@anchor{gnat_ugn/gnat_and_program_execution overflow-check-handling-in-gnat}@anchor{27}
22341 @section Overflow Check Handling in GNAT
22342
22343
22344 This section explains how to control the handling of overflow checks.
22345
22346 @menu
22347 * Background::
22348 * Management of Overflows in GNAT::
22349 * Specifying the Desired Mode::
22350 * Default Settings::
22351 * Implementation Notes::
22352
22353 @end menu
22354
22355 @node Background,Management of Overflows in GNAT,,Overflow Check Handling in GNAT
22356 @anchor{gnat_ugn/gnat_and_program_execution id46}@anchor{1bb}@anchor{gnat_ugn/gnat_and_program_execution background}@anchor{1bc}
22357 @subsection Background
22358
22359
22360 Overflow checks are checks that the compiler may make to ensure
22361 that intermediate results are not out of range. For example:
22362
22363 @quotation
22364
22365 @example
22366 A : Integer;
22367 ...
22368 A := A + 1;
22369 @end example
22370 @end quotation
22371
22372 If @code{A} has the value @code{Integer'Last}, then the addition may cause
22373 overflow since the result is out of range of the type @code{Integer}.
22374 In this case @code{Constraint_Error} will be raised if checks are
22375 enabled.
22376
22377 A trickier situation arises in examples like the following:
22378
22379 @quotation
22380
22381 @example
22382 A, C : Integer;
22383 ...
22384 A := (A + 1) + C;
22385 @end example
22386 @end quotation
22387
22388 where @code{A} is @code{Integer'Last} and @code{C} is @code{-1}.
22389 Now the final result of the expression on the right hand side is
22390 @code{Integer'Last} which is in range, but the question arises whether the
22391 intermediate addition of @code{(A + 1)} raises an overflow error.
22392
22393 The (perhaps surprising) answer is that the Ada language
22394 definition does not answer this question. Instead it leaves
22395 it up to the implementation to do one of two things if overflow
22396 checks are enabled.
22397
22398
22399 @itemize *
22400
22401 @item
22402 raise an exception (@code{Constraint_Error}), or
22403
22404 @item
22405 yield the correct mathematical result which is then used in
22406 subsequent operations.
22407 @end itemize
22408
22409 If the compiler chooses the first approach, then the assignment of this
22410 example will indeed raise @code{Constraint_Error} if overflow checking is
22411 enabled, or result in erroneous execution if overflow checks are suppressed.
22412
22413 But if the compiler
22414 chooses the second approach, then it can perform both additions yielding
22415 the correct mathematical result, which is in range, so no exception
22416 will be raised, and the right result is obtained, regardless of whether
22417 overflow checks are suppressed.
22418
22419 Note that in the first example an
22420 exception will be raised in either case, since if the compiler
22421 gives the correct mathematical result for the addition, it will
22422 be out of range of the target type of the assignment, and thus
22423 fails the range check.
22424
22425 This lack of specified behavior in the handling of overflow for
22426 intermediate results is a source of non-portability, and can thus
22427 be problematic when programs are ported. Most typically this arises
22428 in a situation where the original compiler did not raise an exception,
22429 and then the application is moved to a compiler where the check is
22430 performed on the intermediate result and an unexpected exception is
22431 raised.
22432
22433 Furthermore, when using Ada 2012's preconditions and other
22434 assertion forms, another issue arises. Consider:
22435
22436 @quotation
22437
22438 @example
22439 procedure P (A, B : Integer) with
22440 Pre => A + B <= Integer'Last;
22441 @end example
22442 @end quotation
22443
22444 One often wants to regard arithmetic in a context like this from
22445 a mathematical point of view. So for example, if the two actual parameters
22446 for a call to @code{P} are both @code{Integer'Last}, then
22447 the precondition should be regarded as False. If we are executing
22448 in a mode with run-time checks enabled for preconditions, then we would
22449 like this precondition to fail, rather than raising an exception
22450 because of the intermediate overflow.
22451
22452 However, the language definition leaves the specification of
22453 whether the above condition fails (raising @code{Assert_Error}) or
22454 causes an intermediate overflow (raising @code{Constraint_Error})
22455 up to the implementation.
22456
22457 The situation is worse in a case such as the following:
22458
22459 @quotation
22460
22461 @example
22462 procedure Q (A, B, C : Integer) with
22463 Pre => A + B + C <= Integer'Last;
22464 @end example
22465 @end quotation
22466
22467 Consider the call
22468
22469 @quotation
22470
22471 @example
22472 Q (A => Integer'Last, B => 1, C => -1);
22473 @end example
22474 @end quotation
22475
22476 From a mathematical point of view the precondition
22477 is True, but at run time we may (but are not guaranteed to) get an
22478 exception raised because of the intermediate overflow (and we really
22479 would prefer this precondition to be considered True at run time).
22480
22481 @node Management of Overflows in GNAT,Specifying the Desired Mode,Background,Overflow Check Handling in GNAT
22482 @anchor{gnat_ugn/gnat_and_program_execution id47}@anchor{1bd}@anchor{gnat_ugn/gnat_and_program_execution management-of-overflows-in-gnat}@anchor{1be}
22483 @subsection Management of Overflows in GNAT
22484
22485
22486 To deal with the portability issue, and with the problem of
22487 mathematical versus run-time interpretation of the expressions in
22488 assertions, GNAT provides comprehensive control over the handling
22489 of intermediate overflow. GNAT can operate in three modes, and
22490 furthemore, permits separate selection of operating modes for
22491 the expressions within assertions (here the term 'assertions'
22492 is used in the technical sense, which includes preconditions and so forth)
22493 and for expressions appearing outside assertions.
22494
22495 The three modes are:
22496
22497
22498 @itemize *
22499
22500 @item
22501 @emph{Use base type for intermediate operations} (@code{STRICT})
22502
22503 In this mode, all intermediate results for predefined arithmetic
22504 operators are computed using the base type, and the result must
22505 be in range of the base type. If this is not the
22506 case then either an exception is raised (if overflow checks are
22507 enabled) or the execution is erroneous (if overflow checks are suppressed).
22508 This is the normal default mode.
22509
22510 @item
22511 @emph{Most intermediate overflows avoided} (@code{MINIMIZED})
22512
22513 In this mode, the compiler attempts to avoid intermediate overflows by
22514 using a larger integer type, typically @code{Long_Long_Integer},
22515 as the type in which arithmetic is
22516 performed for predefined arithmetic operators. This may be slightly more
22517 expensive at
22518 run time (compared to suppressing intermediate overflow checks), though
22519 the cost is negligible on modern 64-bit machines. For the examples given
22520 earlier, no intermediate overflows would have resulted in exceptions,
22521 since the intermediate results are all in the range of
22522 @code{Long_Long_Integer} (typically 64-bits on nearly all implementations
22523 of GNAT). In addition, if checks are enabled, this reduces the number of
22524 checks that must be made, so this choice may actually result in an
22525 improvement in space and time behavior.
22526
22527 However, there are cases where @code{Long_Long_Integer} is not large
22528 enough, consider the following example:
22529
22530 @quotation
22531
22532 @example
22533 procedure R (A, B, C, D : Integer) with
22534 Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
22535 @end example
22536 @end quotation
22537
22538 where @code{A} = @code{B} = @code{C} = @code{D} = @code{Integer'Last}.
22539 Now the intermediate results are
22540 out of the range of @code{Long_Long_Integer} even though the final result
22541 is in range and the precondition is True (from a mathematical point
22542 of view). In such a case, operating in this mode, an overflow occurs
22543 for the intermediate computation (which is why this mode
22544 says @emph{most} intermediate overflows are avoided). In this case,
22545 an exception is raised if overflow checks are enabled, and the
22546 execution is erroneous if overflow checks are suppressed.
22547
22548 @item
22549 @emph{All intermediate overflows avoided} (@code{ELIMINATED})
22550
22551 In this mode, the compiler avoids all intermediate overflows
22552 by using arbitrary precision arithmetic as required. In this
22553 mode, the above example with @code{A**2 * B**2} would
22554 not cause intermediate overflow, because the intermediate result
22555 would be evaluated using sufficient precision, and the result
22556 of evaluating the precondition would be True.
22557
22558 This mode has the advantage of avoiding any intermediate
22559 overflows, but at the expense of significant run-time overhead,
22560 including the use of a library (included automatically in this
22561 mode) for multiple-precision arithmetic.
22562
22563 This mode provides cleaner semantics for assertions, since now
22564 the run-time behavior emulates true arithmetic behavior for the
22565 predefined arithmetic operators, meaning that there is never a
22566 conflict between the mathematical view of the assertion, and its
22567 run-time behavior.
22568
22569 Note that in this mode, the behavior is unaffected by whether or
22570 not overflow checks are suppressed, since overflow does not occur.
22571 It is possible for gigantic intermediate expressions to raise
22572 @code{Storage_Error} as a result of attempting to compute the
22573 results of such expressions (e.g. @code{Integer'Last ** Integer'Last})
22574 but overflow is impossible.
22575 @end itemize
22576
22577 Note that these modes apply only to the evaluation of predefined
22578 arithmetic, membership, and comparison operators for signed integer
22579 arithmetic.
22580
22581 For fixed-point arithmetic, checks can be suppressed. But if checks
22582 are enabled
22583 then fixed-point values are always checked for overflow against the
22584 base type for intermediate expressions (that is such checks always
22585 operate in the equivalent of @code{STRICT} mode).
22586
22587 For floating-point, on nearly all architectures, @code{Machine_Overflows}
22588 is False, and IEEE infinities are generated, so overflow exceptions
22589 are never raised. If you want to avoid infinities, and check that
22590 final results of expressions are in range, then you can declare a
22591 constrained floating-point type, and range checks will be carried
22592 out in the normal manner (with infinite values always failing all
22593 range checks).
22594
22595 @node Specifying the Desired Mode,Default Settings,Management of Overflows in GNAT,Overflow Check Handling in GNAT
22596 @anchor{gnat_ugn/gnat_and_program_execution specifying-the-desired-mode}@anchor{f8}@anchor{gnat_ugn/gnat_and_program_execution id48}@anchor{1bf}
22597 @subsection Specifying the Desired Mode
22598
22599
22600 @geindex pragma Overflow_Mode
22601
22602 The desired mode of for handling intermediate overflow can be specified using
22603 either the @code{Overflow_Mode} pragma or an equivalent compiler switch.
22604 The pragma has the form
22605
22606 @quotation
22607
22608 @example
22609 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
22610 @end example
22611 @end quotation
22612
22613 where @code{MODE} is one of
22614
22615
22616 @itemize *
22617
22618 @item
22619 @code{STRICT}: intermediate overflows checked (using base type)
22620
22621 @item
22622 @code{MINIMIZED}: minimize intermediate overflows
22623
22624 @item
22625 @code{ELIMINATED}: eliminate intermediate overflows
22626 @end itemize
22627
22628 The case is ignored, so @code{MINIMIZED}, @code{Minimized} and
22629 @code{minimized} all have the same effect.
22630
22631 If only the @code{General} parameter is present, then the given @code{MODE} applies
22632 to expressions both within and outside assertions. If both arguments
22633 are present, then @code{General} applies to expressions outside assertions,
22634 and @code{Assertions} applies to expressions within assertions. For example:
22635
22636 @quotation
22637
22638 @example
22639 pragma Overflow_Mode
22640 (General => Minimized, Assertions => Eliminated);
22641 @end example
22642 @end quotation
22643
22644 specifies that general expressions outside assertions be evaluated
22645 in 'minimize intermediate overflows' mode, and expressions within
22646 assertions be evaluated in 'eliminate intermediate overflows' mode.
22647 This is often a reasonable choice, avoiding excessive overhead
22648 outside assertions, but assuring a high degree of portability
22649 when importing code from another compiler, while incurring
22650 the extra overhead for assertion expressions to ensure that
22651 the behavior at run time matches the expected mathematical
22652 behavior.
22653
22654 The @code{Overflow_Mode} pragma has the same scoping and placement
22655 rules as pragma @code{Suppress}, so it can occur either as a
22656 configuration pragma, specifying a default for the whole
22657 program, or in a declarative scope, where it applies to the
22658 remaining declarations and statements in that scope.
22659
22660 Note that pragma @code{Overflow_Mode} does not affect whether
22661 overflow checks are enabled or suppressed. It only controls the
22662 method used to compute intermediate values. To control whether
22663 overflow checking is enabled or suppressed, use pragma @code{Suppress}
22664 or @code{Unsuppress} in the usual manner.
22665
22666 @geindex -gnato? (gcc)
22667
22668 @geindex -gnato?? (gcc)
22669
22670 Additionally, a compiler switch @code{-gnato?} or @code{-gnato??}
22671 can be used to control the checking mode default (which can be subsequently
22672 overridden using pragmas).
22673
22674 Here @code{?} is one of the digits @code{1} through @code{3}:
22675
22676 @quotation
22677
22678
22679 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
22680 @item
22681
22682 @code{1}
22683
22684 @tab
22685
22686 use base type for intermediate operations (@code{STRICT})
22687
22688 @item
22689
22690 @code{2}
22691
22692 @tab
22693
22694 minimize intermediate overflows (@code{MINIMIZED})
22695
22696 @item
22697
22698 @code{3}
22699
22700 @tab
22701
22702 eliminate intermediate overflows (@code{ELIMINATED})
22703
22704 @end multitable
22705
22706 @end quotation
22707
22708 As with the pragma, if only one digit appears then it applies to all
22709 cases; if two digits are given, then the first applies outside
22710 assertions, and the second within assertions. Thus the equivalent
22711 of the example pragma above would be
22712 @code{-gnato23}.
22713
22714 If no digits follow the @code{-gnato}, then it is equivalent to
22715 @code{-gnato11},
22716 causing all intermediate operations to be computed using the base
22717 type (@code{STRICT} mode).
22718
22719 @node Default Settings,Implementation Notes,Specifying the Desired Mode,Overflow Check Handling in GNAT
22720 @anchor{gnat_ugn/gnat_and_program_execution id49}@anchor{1c0}@anchor{gnat_ugn/gnat_and_program_execution default-settings}@anchor{1c1}
22721 @subsection Default Settings
22722
22723
22724 The default mode for overflow checks is
22725
22726 @quotation
22727
22728 @example
22729 General => Strict
22730 @end example
22731 @end quotation
22732
22733 which causes all computations both inside and outside assertions to use
22734 the base type.
22735
22736 This retains compatibility with previous versions of
22737 GNAT which suppressed overflow checks by default and always
22738 used the base type for computation of intermediate results.
22739
22740 @c Sphinx allows no emphasis within :index: role. As a workaround we
22741 @c point the index to "switch" and use emphasis for "-gnato".
22742
22743 The
22744 @geindex -gnato (gcc)
22745 switch @code{-gnato} (with no digits following)
22746 is equivalent to
22747
22748 @quotation
22749
22750 @example
22751 General => Strict
22752 @end example
22753 @end quotation
22754
22755 which causes overflow checking of all intermediate overflows
22756 both inside and outside assertions against the base type.
22757
22758 The pragma @code{Suppress (Overflow_Check)} disables overflow
22759 checking, but it has no effect on the method used for computing
22760 intermediate results.
22761
22762 The pragma @code{Unsuppress (Overflow_Check)} enables overflow
22763 checking, but it has no effect on the method used for computing
22764 intermediate results.
22765
22766 @node Implementation Notes,,Default Settings,Overflow Check Handling in GNAT
22767 @anchor{gnat_ugn/gnat_and_program_execution implementation-notes}@anchor{1c2}@anchor{gnat_ugn/gnat_and_program_execution id50}@anchor{1c3}
22768 @subsection Implementation Notes
22769
22770
22771 In practice on typical 64-bit machines, the @code{MINIMIZED} mode is
22772 reasonably efficient, and can be generally used. It also helps
22773 to ensure compatibility with code imported from some other
22774 compiler to GNAT.
22775
22776 Setting all intermediate overflows checking (@code{CHECKED} mode)
22777 makes sense if you want to
22778 make sure that your code is compatible with any other possible
22779 Ada implementation. This may be useful in ensuring portability
22780 for code that is to be exported to some other compiler than GNAT.
22781
22782 The Ada standard allows the reassociation of expressions at
22783 the same precedence level if no parentheses are present. For
22784 example, @code{A+B+C} parses as though it were @code{(A+B)+C}, but
22785 the compiler can reintepret this as @code{A+(B+C)}, possibly
22786 introducing or eliminating an overflow exception. The GNAT
22787 compiler never takes advantage of this freedom, and the
22788 expression @code{A+B+C} will be evaluated as @code{(A+B)+C}.
22789 If you need the other order, you can write the parentheses
22790 explicitly @code{A+(B+C)} and GNAT will respect this order.
22791
22792 The use of @code{ELIMINATED} mode will cause the compiler to
22793 automatically include an appropriate arbitrary precision
22794 integer arithmetic package. The compiler will make calls
22795 to this package, though only in cases where it cannot be
22796 sure that @code{Long_Long_Integer} is sufficient to guard against
22797 intermediate overflows. This package does not use dynamic
22798 allocation, but it does use the secondary stack, so an
22799 appropriate secondary stack package must be present (this
22800 is always true for standard full Ada, but may require
22801 specific steps for restricted run times such as ZFP).
22802
22803 Although @code{ELIMINATED} mode causes expressions to use arbitrary
22804 precision arithmetic, avoiding overflow, the final result
22805 must be in an appropriate range. This is true even if the
22806 final result is of type @code{[Long_[Long_]]Integer'Base}, which
22807 still has the same bounds as its associated constrained
22808 type at run-time.
22809
22810 Currently, the @code{ELIMINATED} mode is only available on target
22811 platforms for which @code{Long_Long_Integer} is 64-bits (nearly all GNAT
22812 platforms).
22813
22814 @node Performing Dimensionality Analysis in GNAT,Stack Related Facilities,Overflow Check Handling in GNAT,GNAT and Program Execution
22815 @anchor{gnat_ugn/gnat_and_program_execution performing-dimensionality-analysis-in-gnat}@anchor{28}@anchor{gnat_ugn/gnat_and_program_execution id51}@anchor{16a}
22816 @section Performing Dimensionality Analysis in GNAT
22817
22818
22819 @geindex Dimensionality analysis
22820
22821 The GNAT compiler supports dimensionality checking. The user can
22822 specify physical units for objects, and the compiler will verify that uses
22823 of these objects are compatible with their dimensions, in a fashion that is
22824 familiar to engineering practice. The dimensions of algebraic expressions
22825 (including powers with static exponents) are computed from their constituents.
22826
22827 @geindex Dimension_System aspect
22828
22829 @geindex Dimension aspect
22830
22831 This feature depends on Ada 2012 aspect specifications, and is available from
22832 version 7.0.1 of GNAT onwards.
22833 The GNAT-specific aspect @code{Dimension_System}
22834 allows you to define a system of units; the aspect @code{Dimension}
22835 then allows the user to declare dimensioned quantities within a given system.
22836 (These aspects are described in the @emph{Implementation Defined Aspects}
22837 chapter of the @emph{GNAT Reference Manual}).
22838
22839 The major advantage of this model is that it does not require the declaration of
22840 multiple operators for all possible combinations of types: it is only necessary
22841 to use the proper subtypes in object declarations.
22842
22843 @geindex System.Dim.Mks package (GNAT library)
22844
22845 @geindex MKS_Type type
22846
22847 The simplest way to impose dimensionality checking on a computation is to make
22848 use of one of the instantiations of the package @code{System.Dim.Generic_Mks}, which
22849 are part of the GNAT library. This generic package defines a floating-point
22850 type @code{MKS_Type}, for which a sequence of dimension names are specified,
22851 together with their conventional abbreviations. The following should be read
22852 together with the full specification of the package, in file
22853 @code{s-digemk.ads}.
22854
22855 @quotation
22856
22857 @geindex s-digemk.ads file
22858
22859 @example
22860 type Mks_Type is new Float_Type
22861 with
22862 Dimension_System => (
22863 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
22864 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
22865 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
22866 (Unit_Name => Ampere, Unit_Symbol => 'A', Dim_Symbol => 'I'),
22867 (Unit_Name => Kelvin, Unit_Symbol => 'K', Dim_Symbol => "Theta"),
22868 (Unit_Name => Mole, Unit_Symbol => "mol", Dim_Symbol => 'N'),
22869 (Unit_Name => Candela, Unit_Symbol => "cd", Dim_Symbol => 'J'));
22870 @end example
22871 @end quotation
22872
22873 The package then defines a series of subtypes that correspond to these
22874 conventional units. For example:
22875
22876 @quotation
22877
22878 @example
22879 subtype Length is Mks_Type
22880 with
22881 Dimension => (Symbol => 'm', Meter => 1, others => 0);
22882 @end example
22883 @end quotation
22884
22885 and similarly for @code{Mass}, @code{Time}, @code{Electric_Current},
22886 @code{Thermodynamic_Temperature}, @code{Amount_Of_Substance}, and
22887 @code{Luminous_Intensity} (the standard set of units of the SI system).
22888
22889 The package also defines conventional names for values of each unit, for
22890 example:
22891
22892 @quotation
22893
22894 @example
22895 m : constant Length := 1.0;
22896 kg : constant Mass := 1.0;
22897 s : constant Time := 1.0;
22898 A : constant Electric_Current := 1.0;
22899 @end example
22900 @end quotation
22901
22902 as well as useful multiples of these units:
22903
22904 @quotation
22905
22906 @example
22907 cm : constant Length := 1.0E-02;
22908 g : constant Mass := 1.0E-03;
22909 min : constant Time := 60.0;
22910 day : constant Time := 60.0 * 24.0 * min;
22911 ...
22912 @end example
22913 @end quotation
22914
22915 There are three instantiations of @code{System.Dim.Generic_Mks} defined in the
22916 GNAT library:
22917
22918
22919 @itemize *
22920
22921 @item
22922 @code{System.Dim.Float_Mks} based on @code{Float} defined in @code{s-diflmk.ads}.
22923
22924 @item
22925 @code{System.Dim.Long_Mks} based on @code{Long_Float} defined in @code{s-dilomk.ads}.
22926
22927 @item
22928 @code{System.Dim.Mks} based on @code{Long_Long_Float} defined in @code{s-dimmks.ads}.
22929 @end itemize
22930
22931 Using one of these packages, you can then define a derived unit by providing
22932 the aspect that specifies its dimensions within the MKS system, as well as the
22933 string to be used for output of a value of that unit:
22934
22935 @quotation
22936
22937 @example
22938 subtype Acceleration is Mks_Type
22939 with Dimension => ("m/sec^2",
22940 Meter => 1,
22941 Second => -2,
22942 others => 0);
22943 @end example
22944 @end quotation
22945
22946 Here is a complete example of use:
22947
22948 @quotation
22949
22950 @example
22951 with System.Dim.MKS; use System.Dim.Mks;
22952 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
22953 with Text_IO; use Text_IO;
22954 procedure Free_Fall is
22955 subtype Acceleration is Mks_Type
22956 with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
22957 G : constant acceleration := 9.81 * m / (s ** 2);
22958 T : Time := 10.0*s;
22959 Distance : Length;
22960
22961 begin
22962 Put ("Gravitational constant: ");
22963 Put (G, Aft => 2, Exp => 0); Put_Line ("");
22964 Distance := 0.5 * G * T ** 2;
22965 Put ("distance travelled in 10 seconds of free fall ");
22966 Put (Distance, Aft => 2, Exp => 0);
22967 Put_Line ("");
22968 end Free_Fall;
22969 @end example
22970 @end quotation
22971
22972 Execution of this program yields:
22973
22974 @quotation
22975
22976 @example
22977 Gravitational constant: 9.81 m/sec^2
22978 distance travelled in 10 seconds of free fall 490.50 m
22979 @end example
22980 @end quotation
22981
22982 However, incorrect assignments such as:
22983
22984 @quotation
22985
22986 @example
22987 Distance := 5.0;
22988 Distance := 5.0 * kg;
22989 @end example
22990 @end quotation
22991
22992 are rejected with the following diagnoses:
22993
22994 @quotation
22995
22996 @example
22997 Distance := 5.0;
22998 >>> dimensions mismatch in assignment
22999 >>> left-hand side has dimension [L]
23000 >>> right-hand side is dimensionless
23001
23002 Distance := 5.0 * kg:
23003 >>> dimensions mismatch in assignment
23004 >>> left-hand side has dimension [L]
23005 >>> right-hand side has dimension [M]
23006 @end example
23007 @end quotation
23008
23009 The dimensions of an expression are properly displayed, even if there is
23010 no explicit subtype for it. If we add to the program:
23011
23012 @quotation
23013
23014 @example
23015 Put ("Final velocity: ");
23016 Put (G * T, Aft =>2, Exp =>0);
23017 Put_Line ("");
23018 @end example
23019 @end quotation
23020
23021 then the output includes:
23022
23023 @quotation
23024
23025 @example
23026 Final velocity: 98.10 m.s**(-1)
23027 @end example
23028
23029 @geindex Dimensionable type
23030
23031 @geindex Dimensioned subtype
23032 @end quotation
23033
23034 The type @code{Mks_Type} is said to be a @emph{dimensionable type} since it has a
23035 @code{Dimension_System} aspect, and the subtypes @code{Length}, @code{Mass}, etc.,
23036 are said to be @emph{dimensioned subtypes} since each one has a @code{Dimension}
23037 aspect.
23038
23039 @quotation
23040
23041 @geindex Dimension Vector (for a dimensioned subtype)
23042
23043 @geindex Dimension aspect
23044
23045 @geindex Dimension_System aspect
23046 @end quotation
23047
23048 The @code{Dimension} aspect of a dimensioned subtype @code{S} defines a mapping
23049 from the base type's Unit_Names to integer (or, more generally, rational)
23050 values. This mapping is the @emph{dimension vector} (also referred to as the
23051 @emph{dimensionality}) for that subtype, denoted by @code{DV(S)}, and thus for each
23052 object of that subtype. Intuitively, the value specified for each
23053 @code{Unit_Name} is the exponent associated with that unit; a zero value
23054 means that the unit is not used. For example:
23055
23056 @quotation
23057
23058 @example
23059 declare
23060 Acc : Acceleration;
23061 ...
23062 begin
23063 ...
23064 end;
23065 @end example
23066 @end quotation
23067
23068 Here @code{DV(Acc)} = @code{DV(Acceleration)} =
23069 @code{(Meter=>1, Kilogram=>0, Second=>-2, Ampere=>0, Kelvin=>0, Mole=>0, Candela=>0)}.
23070 Symbolically, we can express this as @code{Meter / Second**2}.
23071
23072 The dimension vector of an arithmetic expression is synthesized from the
23073 dimension vectors of its components, with compile-time dimensionality checks
23074 that help prevent mismatches such as using an @code{Acceleration} where a
23075 @code{Length} is required.
23076
23077 The dimension vector of the result of an arithmetic expression @emph{expr}, or
23078 @code{DV(@emph{expr})}, is defined as follows, assuming conventional
23079 mathematical definitions for the vector operations that are used:
23080
23081
23082 @itemize *
23083
23084 @item
23085 If @emph{expr} is of the type @emph{universal_real}, or is not of a dimensioned subtype,
23086 then @emph{expr} is dimensionless; @code{DV(@emph{expr})} is the empty vector.
23087
23088 @item
23089 @code{DV(@emph{op expr})}, where @emph{op} is a unary operator, is @code{DV(@emph{expr})}
23090
23091 @item
23092 @code{DV(@emph{expr1 op expr2})} where @emph{op} is "+" or "-" is @code{DV(@emph{expr1})}
23093 provided that @code{DV(@emph{expr1})} = @code{DV(@emph{expr2})}.
23094 If this condition is not met then the construct is illegal.
23095
23096 @item
23097 @code{DV(@emph{expr1} * @emph{expr2})} is @code{DV(@emph{expr1})} + @code{DV(@emph{expr2})},
23098 and @code{DV(@emph{expr1} / @emph{expr2})} = @code{DV(@emph{expr1})} - @code{DV(@emph{expr2})}.
23099 In this context if one of the @emph{expr}s is dimensionless then its empty
23100 dimension vector is treated as @code{(others => 0)}.
23101
23102 @item
23103 @code{DV(@emph{expr} ** @emph{power})} is @emph{power} * @code{DV(@emph{expr})},
23104 provided that @emph{power} is a static rational value. If this condition is not
23105 met then the construct is illegal.
23106 @end itemize
23107
23108 Note that, by the above rules, it is illegal to use binary "+" or "-" to
23109 combine a dimensioned and dimensionless value. Thus an expression such as
23110 @code{acc-10.0} is illegal, where @code{acc} is an object of subtype
23111 @code{Acceleration}.
23112
23113 The dimensionality checks for relationals use the same rules as
23114 for "+" and "-", except when comparing to a literal; thus
23115
23116 @quotation
23117
23118 @example
23119 acc > len
23120 @end example
23121 @end quotation
23122
23123 is equivalent to
23124
23125 @quotation
23126
23127 @example
23128 acc-len > 0.0
23129 @end example
23130 @end quotation
23131
23132 and is thus illegal, but
23133
23134 @quotation
23135
23136 @example
23137 acc > 10.0
23138 @end example
23139 @end quotation
23140
23141 is accepted with a warning. Analogously a conditional expression requires the
23142 same dimension vector for each branch (with no exception for literals).
23143
23144 The dimension vector of a type conversion @code{T(@emph{expr})} is defined
23145 as follows, based on the nature of @code{T}:
23146
23147
23148 @itemize *
23149
23150 @item
23151 If @code{T} is a dimensioned subtype then @code{DV(T(@emph{expr}))} is @code{DV(T)}
23152 provided that either @emph{expr} is dimensionless or
23153 @code{DV(T)} = @code{DV(@emph{expr})}. The conversion is illegal
23154 if @emph{expr} is dimensioned and @code{DV(@emph{expr})} /= @code{DV(T)}.
23155 Note that vector equality does not require that the corresponding
23156 Unit_Names be the same.
23157
23158 As a consequence of the above rule, it is possible to convert between
23159 different dimension systems that follow the same international system
23160 of units, with the seven physical components given in the standard order
23161 (length, mass, time, etc.). Thus a length in meters can be converted to
23162 a length in inches (with a suitable conversion factor) but cannot be
23163 converted, for example, to a mass in pounds.
23164
23165 @item
23166 If @code{T} is the base type for @emph{expr} (and the dimensionless root type of
23167 the dimension system), then @code{DV(T(@emph{expr}))} is @code{DV(expr)}.
23168 Thus, if @emph{expr} is of a dimensioned subtype of @code{T}, the conversion may
23169 be regarded as a "view conversion" that preserves dimensionality.
23170
23171 This rule makes it possible to write generic code that can be instantiated
23172 with compatible dimensioned subtypes. The generic unit will contain
23173 conversions that will consequently be present in instantiations, but
23174 conversions to the base type will preserve dimensionality and make it
23175 possible to write generic code that is correct with respect to
23176 dimensionality.
23177
23178 @item
23179 Otherwise (i.e., @code{T} is neither a dimensioned subtype nor a dimensionable
23180 base type), @code{DV(T(@emph{expr}))} is the empty vector. Thus a dimensioned
23181 value can be explicitly converted to a non-dimensioned subtype, which
23182 of course then escapes dimensionality analysis.
23183 @end itemize
23184
23185 The dimension vector for a type qualification @code{T'(@emph{expr})} is the same
23186 as for the type conversion @code{T(@emph{expr})}.
23187
23188 An assignment statement
23189
23190 @quotation
23191
23192 @example
23193 Source := Target;
23194 @end example
23195 @end quotation
23196
23197 requires @code{DV(Source)} = @code{DV(Target)}, and analogously for parameter
23198 passing (the dimension vector for the actual parameter must be equal to the
23199 dimension vector for the formal parameter).
23200
23201 @node Stack Related Facilities,Memory Management Issues,Performing Dimensionality Analysis in GNAT,GNAT and Program Execution
23202 @anchor{gnat_ugn/gnat_and_program_execution stack-related-facilities}@anchor{29}@anchor{gnat_ugn/gnat_and_program_execution id52}@anchor{16b}
23203 @section Stack Related Facilities
23204
23205
23206 This section describes some useful tools associated with stack
23207 checking and analysis. In
23208 particular, it deals with dynamic and static stack usage measurements.
23209
23210 @menu
23211 * Stack Overflow Checking::
23212 * Static Stack Usage Analysis::
23213 * Dynamic Stack Usage Analysis::
23214
23215 @end menu
23216
23217 @node Stack Overflow Checking,Static Stack Usage Analysis,,Stack Related Facilities
23218 @anchor{gnat_ugn/gnat_and_program_execution id53}@anchor{1c4}@anchor{gnat_ugn/gnat_and_program_execution stack-overflow-checking}@anchor{f4}
23219 @subsection Stack Overflow Checking
23220
23221
23222 @geindex Stack Overflow Checking
23223
23224 @geindex -fstack-check (gcc)
23225
23226 For most operating systems, @code{gcc} does not perform stack overflow
23227 checking by default. This means that if the main environment task or
23228 some other task exceeds the available stack space, then unpredictable
23229 behavior will occur. Most native systems offer some level of protection by
23230 adding a guard page at the end of each task stack. This mechanism is usually
23231 not enough for dealing properly with stack overflow situations because
23232 a large local variable could "jump" above the guard page.
23233 Furthermore, when the
23234 guard page is hit, there may not be any space left on the stack for executing
23235 the exception propagation code. Enabling stack checking avoids
23236 such situations.
23237
23238 To activate stack checking, compile all units with the @code{gcc} option
23239 @code{-fstack-check}. For example:
23240
23241 @quotation
23242
23243 @example
23244 $ gcc -c -fstack-check package1.adb
23245 @end example
23246 @end quotation
23247
23248 Units compiled with this option will generate extra instructions to check
23249 that any use of the stack (for procedure calls or for declaring local
23250 variables in declare blocks) does not exceed the available stack space.
23251 If the space is exceeded, then a @code{Storage_Error} exception is raised.
23252
23253 For declared tasks, the default stack size is defined by the GNAT runtime,
23254 whose size may be modified at bind time through the @code{-d} bind switch
23255 (@ref{11f,,Switches for gnatbind}). Task specific stack sizes may be set using the
23256 @code{Storage_Size} pragma.
23257
23258 For the environment task, the stack size is determined by the operating system.
23259 Consequently, to modify the size of the environment task please refer to your
23260 operating system documentation.
23261
23262 @node Static Stack Usage Analysis,Dynamic Stack Usage Analysis,Stack Overflow Checking,Stack Related Facilities
23263 @anchor{gnat_ugn/gnat_and_program_execution id54}@anchor{1c5}@anchor{gnat_ugn/gnat_and_program_execution static-stack-usage-analysis}@anchor{f5}
23264 @subsection Static Stack Usage Analysis
23265
23266
23267 @geindex Static Stack Usage Analysis
23268
23269 @geindex -fstack-usage
23270
23271 A unit compiled with @code{-fstack-usage} will generate an extra file
23272 that specifies
23273 the maximum amount of stack used, on a per-function basis.
23274 The file has the same
23275 basename as the target object file with a @code{.su} extension.
23276 Each line of this file is made up of three fields:
23277
23278
23279 @itemize *
23280
23281 @item
23282 The name of the function.
23283
23284 @item
23285 A number of bytes.
23286
23287 @item
23288 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
23289 @end itemize
23290
23291 The second field corresponds to the size of the known part of the function
23292 frame.
23293
23294 The qualifier @code{static} means that the function frame size
23295 is purely static.
23296 It usually means that all local variables have a static size.
23297 In this case, the second field is a reliable measure of the function stack
23298 utilization.
23299
23300 The qualifier @code{dynamic} means that the function frame size is not static.
23301 It happens mainly when some local variables have a dynamic size. When this
23302 qualifier appears alone, the second field is not a reliable measure
23303 of the function stack analysis. When it is qualified with @code{bounded}, it
23304 means that the second field is a reliable maximum of the function stack
23305 utilization.
23306
23307 A unit compiled with @code{-Wstack-usage} will issue a warning for each
23308 subprogram whose stack usage might be larger than the specified amount of
23309 bytes. The wording is in keeping with the qualifier documented above.
23310
23311 @node Dynamic Stack Usage Analysis,,Static Stack Usage Analysis,Stack Related Facilities
23312 @anchor{gnat_ugn/gnat_and_program_execution id55}@anchor{1c6}@anchor{gnat_ugn/gnat_and_program_execution dynamic-stack-usage-analysis}@anchor{122}
23313 @subsection Dynamic Stack Usage Analysis
23314
23315
23316 It is possible to measure the maximum amount of stack used by a task, by
23317 adding a switch to @code{gnatbind}, as:
23318
23319 @quotation
23320
23321 @example
23322 $ gnatbind -u0 file
23323 @end example
23324 @end quotation
23325
23326 With this option, at each task termination, its stack usage is output on
23327 @code{stderr}.
23328 Note that this switch is not compatible with tools like
23329 Valgrind and DrMemory; they will report errors.
23330
23331 It is not always convenient to output the stack usage when the program
23332 is still running. Hence, it is possible to delay this output until program
23333 termination. for a given number of tasks specified as the argument of the
23334 @code{-u} option. For instance:
23335
23336 @quotation
23337
23338 @example
23339 $ gnatbind -u100 file
23340 @end example
23341 @end quotation
23342
23343 will buffer the stack usage information of the first 100 tasks to terminate and
23344 output this info at program termination. Results are displayed in four
23345 columns:
23346
23347 @quotation
23348
23349 @example
23350 Index | Task Name | Stack Size | Stack Usage
23351 @end example
23352 @end quotation
23353
23354 where:
23355
23356
23357 @itemize *
23358
23359 @item
23360 @emph{Index} is a number associated with each task.
23361
23362 @item
23363 @emph{Task Name} is the name of the task analyzed.
23364
23365 @item
23366 @emph{Stack Size} is the maximum size for the stack.
23367
23368 @item
23369 @emph{Stack Usage} is the measure done by the stack analyzer.
23370 In order to prevent overflow, the stack
23371 is not entirely analyzed, and it's not possible to know exactly how
23372 much has actually been used.
23373 @end itemize
23374
23375 By default the environment task stack, the stack that contains the main unit,
23376 is not processed. To enable processing of the environment task stack, the
23377 environment variable GNAT_STACK_LIMIT needs to be set to the maximum size of
23378 the environment task stack. This amount is given in kilobytes. For example:
23379
23380 @quotation
23381
23382 @example
23383 $ set GNAT_STACK_LIMIT 1600
23384 @end example
23385 @end quotation
23386
23387 would specify to the analyzer that the environment task stack has a limit
23388 of 1.6 megabytes. Any stack usage beyond this will be ignored by the analysis.
23389
23390 The package @code{GNAT.Task_Stack_Usage} provides facilities to get
23391 stack-usage reports at run time. See its body for the details.
23392
23393 @node Memory Management Issues,,Stack Related Facilities,GNAT and Program Execution
23394 @anchor{gnat_ugn/gnat_and_program_execution id56}@anchor{16c}@anchor{gnat_ugn/gnat_and_program_execution memory-management-issues}@anchor{2a}
23395 @section Memory Management Issues
23396
23397
23398 This section describes some useful memory pools provided in the GNAT library
23399 and in particular the GNAT Debug Pool facility, which can be used to detect
23400 incorrect uses of access values (including 'dangling references').
23401
23402
23403 @menu
23404 * Some Useful Memory Pools::
23405 * The GNAT Debug Pool Facility::
23406
23407 @end menu
23408
23409 @node Some Useful Memory Pools,The GNAT Debug Pool Facility,,Memory Management Issues
23410 @anchor{gnat_ugn/gnat_and_program_execution id57}@anchor{1c7}@anchor{gnat_ugn/gnat_and_program_execution some-useful-memory-pools}@anchor{1c8}
23411 @subsection Some Useful Memory Pools
23412
23413
23414 @geindex Memory Pool
23415
23416 @geindex storage
23417 @geindex pool
23418
23419 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
23420 storage pool. Allocations use the standard system call @code{malloc} while
23421 deallocations use the standard system call @code{free}. No reclamation is
23422 performed when the pool goes out of scope. For performance reasons, the
23423 standard default Ada allocators/deallocators do not use any explicit storage
23424 pools but if they did, they could use this storage pool without any change in
23425 behavior. That is why this storage pool is used when the user
23426 manages to make the default implicit allocator explicit as in this example:
23427
23428 @quotation
23429
23430 @example
23431 type T1 is access Something;
23432 -- no Storage pool is defined for T2
23433
23434 type T2 is access Something_Else;
23435 for T2'Storage_Pool use T1'Storage_Pool;
23436 -- the above is equivalent to
23437 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
23438 @end example
23439 @end quotation
23440
23441 The @code{System.Pool_Local} package offers the @code{Unbounded_Reclaim_Pool} storage
23442 pool. The allocation strategy is similar to @code{Pool_Local}
23443 except that the all
23444 storage allocated with this pool is reclaimed when the pool object goes out of
23445 scope. This pool provides a explicit mechanism similar to the implicit one
23446 provided by several Ada 83 compilers for allocations performed through a local
23447 access type and whose purpose was to reclaim memory when exiting the
23448 scope of a given local access. As an example, the following program does not
23449 leak memory even though it does not perform explicit deallocation:
23450
23451 @quotation
23452
23453 @example
23454 with System.Pool_Local;
23455 procedure Pooloc1 is
23456 procedure Internal is
23457 type A is access Integer;
23458 X : System.Pool_Local.Unbounded_Reclaim_Pool;
23459 for A'Storage_Pool use X;
23460 v : A;
23461 begin
23462 for I in 1 .. 50 loop
23463 v := new Integer;
23464 end loop;
23465 end Internal;
23466 begin
23467 for I in 1 .. 100 loop
23468 Internal;
23469 end loop;
23470 end Pooloc1;
23471 @end example
23472 @end quotation
23473
23474 The @code{System.Pool_Size} package implements the @code{Stack_Bounded_Pool} used when
23475 @code{Storage_Size} is specified for an access type.
23476 The whole storage for the pool is
23477 allocated at once, usually on the stack at the point where the access type is
23478 elaborated. It is automatically reclaimed when exiting the scope where the
23479 access type is defined. This package is not intended to be used directly by the
23480 user and it is implicitly used for each such declaration:
23481
23482 @quotation
23483
23484 @example
23485 type T1 is access Something;
23486 for T1'Storage_Size use 10_000;
23487 @end example
23488 @end quotation
23489
23490 @node The GNAT Debug Pool Facility,,Some Useful Memory Pools,Memory Management Issues
23491 @anchor{gnat_ugn/gnat_and_program_execution id58}@anchor{1c9}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debug-pool-facility}@anchor{1ca}
23492 @subsection The GNAT Debug Pool Facility
23493
23494
23495 @geindex Debug Pool
23496
23497 @geindex storage
23498 @geindex pool
23499 @geindex memory corruption
23500
23501 The use of unchecked deallocation and unchecked conversion can easily
23502 lead to incorrect memory references. The problems generated by such
23503 references are usually difficult to tackle because the symptoms can be
23504 very remote from the origin of the problem. In such cases, it is
23505 very helpful to detect the problem as early as possible. This is the
23506 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
23507
23508 In order to use the GNAT specific debugging pool, the user must
23509 associate a debug pool object with each of the access types that may be
23510 related to suspected memory problems. See Ada Reference Manual 13.11.
23511
23512 @quotation
23513
23514 @example
23515 type Ptr is access Some_Type;
23516 Pool : GNAT.Debug_Pools.Debug_Pool;
23517 for Ptr'Storage_Pool use Pool;
23518 @end example
23519 @end quotation
23520
23521 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
23522 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
23523 allow the user to redefine allocation and deallocation strategies. They
23524 also provide a checkpoint for each dereference, through the use of
23525 the primitive operation @code{Dereference} which is implicitly called at
23526 each dereference of an access value.
23527
23528 Once an access type has been associated with a debug pool, operations on
23529 values of the type may raise four distinct exceptions,
23530 which correspond to four potential kinds of memory corruption:
23531
23532
23533 @itemize *
23534
23535 @item
23536 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
23537
23538 @item
23539 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
23540
23541 @item
23542 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
23543
23544 @item
23545 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage}
23546 @end itemize
23547
23548 For types associated with a Debug_Pool, dynamic allocation is performed using
23549 the standard GNAT allocation routine. References to all allocated chunks of
23550 memory are kept in an internal dictionary. Several deallocation strategies are
23551 provided, whereupon the user can choose to release the memory to the system,
23552 keep it allocated for further invalid access checks, or fill it with an easily
23553 recognizable pattern for debug sessions. The memory pattern is the old IBM
23554 hexadecimal convention: @code{16#DEADBEEF#}.
23555
23556 See the documentation in the file g-debpoo.ads for more information on the
23557 various strategies.
23558
23559 Upon each dereference, a check is made that the access value denotes a
23560 properly allocated memory location. Here is a complete example of use of
23561 @code{Debug_Pools}, that includes typical instances of memory corruption:
23562
23563 @quotation
23564
23565 @example
23566 with Gnat.Io; use Gnat.Io;
23567 with Unchecked_Deallocation;
23568 with Unchecked_Conversion;
23569 with GNAT.Debug_Pools;
23570 with System.Storage_Elements;
23571 with Ada.Exceptions; use Ada.Exceptions;
23572 procedure Debug_Pool_Test is
23573
23574 type T is access Integer;
23575 type U is access all T;
23576
23577 P : GNAT.Debug_Pools.Debug_Pool;
23578 for T'Storage_Pool use P;
23579
23580 procedure Free is new Unchecked_Deallocation (Integer, T);
23581 function UC is new Unchecked_Conversion (U, T);
23582 A, B : aliased T;
23583
23584 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
23585
23586 begin
23587 Info (P);
23588 A := new Integer;
23589 B := new Integer;
23590 B := A;
23591 Info (P);
23592 Free (A);
23593 begin
23594 Put_Line (Integer'Image(B.all));
23595 exception
23596 when E : others => Put_Line ("raised: " & Exception_Name (E));
23597 end;
23598 begin
23599 Free (B);
23600 exception
23601 when E : others => Put_Line ("raised: " & Exception_Name (E));
23602 end;
23603 B := UC(A'Access);
23604 begin
23605 Put_Line (Integer'Image(B.all));
23606 exception
23607 when E : others => Put_Line ("raised: " & Exception_Name (E));
23608 end;
23609 begin
23610 Free (B);
23611 exception
23612 when E : others => Put_Line ("raised: " & Exception_Name (E));
23613 end;
23614 Info (P);
23615 end Debug_Pool_Test;
23616 @end example
23617 @end quotation
23618
23619 The debug pool mechanism provides the following precise diagnostics on the
23620 execution of this erroneous program:
23621
23622 @quotation
23623
23624 @example
23625 Debug Pool info:
23626 Total allocated bytes : 0
23627 Total deallocated bytes : 0
23628 Current Water Mark: 0
23629 High Water Mark: 0
23630
23631 Debug Pool info:
23632 Total allocated bytes : 8
23633 Total deallocated bytes : 0
23634 Current Water Mark: 8
23635 High Water Mark: 8
23636
23637 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
23638 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
23639 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
23640 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
23641 Debug Pool info:
23642 Total allocated bytes : 8
23643 Total deallocated bytes : 4
23644 Current Water Mark: 4
23645 High Water Mark: 8
23646 @end example
23647 @end quotation
23648
23649
23650 @c -- Non-breaking space in running text
23651 @c -- E.g. Ada |nbsp| 95
23652
23653 @node Platform-Specific Information,Example of Binder Output File,GNAT and Program Execution,Top
23654 @anchor{gnat_ugn/platform_specific_information platform-specific-information}@anchor{d}@anchor{gnat_ugn/platform_specific_information doc}@anchor{1cb}@anchor{gnat_ugn/platform_specific_information id1}@anchor{1cc}
23655 @chapter Platform-Specific Information
23656
23657
23658 This appendix contains information relating to the implementation
23659 of run-time libraries on various platforms and also covers
23660 topics related to the GNAT implementation on Windows and Mac OS.
23661
23662 @menu
23663 * Run-Time Libraries::
23664 * Specifying a Run-Time Library::
23665 * GNU/Linux Topics::
23666 * Microsoft Windows Topics::
23667 * Mac OS Topics::
23668
23669 @end menu
23670
23671 @node Run-Time Libraries,Specifying a Run-Time Library,,Platform-Specific Information
23672 @anchor{gnat_ugn/platform_specific_information id2}@anchor{1cd}@anchor{gnat_ugn/platform_specific_information run-time-libraries}@anchor{2b}
23673 @section Run-Time Libraries
23674
23675
23676 @geindex Tasking and threads libraries
23677
23678 @geindex Threads libraries and tasking
23679
23680 @geindex Run-time libraries (platform-specific information)
23681
23682 The GNAT run-time implementation may vary with respect to both the
23683 underlying threads library and the exception-handling scheme.
23684 For threads support, the default run-time will bind to the thread
23685 package of the underlying operating system.
23686
23687 For exception handling, either or both of two models are supplied:
23688
23689 @quotation
23690
23691 @geindex Zero-Cost Exceptions
23692
23693 @geindex ZCX (Zero-Cost Exceptions)
23694 @end quotation
23695
23696
23697 @itemize *
23698
23699 @item
23700 @strong{Zero-Cost Exceptions} ("ZCX"),
23701 which uses binder-generated tables that
23702 are interrogated at run time to locate a handler.
23703
23704 @geindex setjmp/longjmp Exception Model
23705
23706 @geindex SJLJ (setjmp/longjmp Exception Model)
23707
23708 @item
23709 @strong{setjmp / longjmp} ('SJLJ'),
23710 which uses dynamically-set data to establish
23711 the set of handlers
23712 @end itemize
23713
23714 Most programs should experience a substantial speed improvement by
23715 being compiled with a ZCX run-time.
23716 This is especially true for
23717 tasking applications or applications with many exception handlers.
23718 Note however that the ZCX run-time does not support asynchronous abort
23719 of tasks (@code{abort} and @code{select-then-abort} constructs) and will instead
23720 implement abort by polling points in the runtime. You can also add additional
23721 polling points explicitly if needed in your application via @code{pragma
23722 Abort_Defer}.
23723
23724 This section summarizes which combinations of threads and exception support
23725 are supplied on various GNAT platforms.
23726
23727 @menu
23728 * Summary of Run-Time Configurations::
23729
23730 @end menu
23731
23732 @node Summary of Run-Time Configurations,,,Run-Time Libraries
23733 @anchor{gnat_ugn/platform_specific_information summary-of-run-time-configurations}@anchor{1ce}@anchor{gnat_ugn/platform_specific_information id3}@anchor{1cf}
23734 @subsection Summary of Run-Time Configurations
23735
23736
23737
23738 @multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxx}
23739 @headitem
23740
23741 Platform
23742
23743 @tab
23744
23745 Run-Time
23746
23747 @tab
23748
23749 Tasking
23750
23751 @tab
23752
23753 Exceptions
23754
23755 @item
23756
23757 GNU/Linux
23758
23759 @tab
23760
23761 rts-native
23762 (default)
23763
23764 @tab
23765
23766 pthread library
23767
23768 @tab
23769
23770 ZCX
23771
23772 @item
23773
23774 rts-sjlj
23775
23776 @tab
23777
23778 pthread library
23779
23780 @tab
23781
23782 SJLJ
23783
23784 @item
23785
23786 Windows
23787
23788 @tab
23789
23790 rts-native
23791 (default)
23792
23793 @tab
23794
23795 native Win32 threads
23796
23797 @tab
23798
23799 ZCX
23800
23801 @item
23802
23803 rts-sjlj
23804
23805 @tab
23806
23807 native Win32 threads
23808
23809 @tab
23810
23811 SJLJ
23812
23813 @item
23814
23815 Mac OS
23816
23817 @tab
23818
23819 rts-native
23820
23821 @tab
23822
23823 pthread library
23824
23825 @tab
23826
23827 ZCX
23828
23829 @end multitable
23830
23831
23832 @node Specifying a Run-Time Library,GNU/Linux Topics,Run-Time Libraries,Platform-Specific Information
23833 @anchor{gnat_ugn/platform_specific_information specifying-a-run-time-library}@anchor{1d0}@anchor{gnat_ugn/platform_specific_information id4}@anchor{1d1}
23834 @section Specifying a Run-Time Library
23835
23836
23837 The @code{adainclude} subdirectory containing the sources of the GNAT
23838 run-time library, and the @code{adalib} subdirectory containing the
23839 @code{ALI} files and the static and/or shared GNAT library, are located
23840 in the gcc target-dependent area:
23841
23842 @quotation
23843
23844 @example
23845 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
23846 @end example
23847 @end quotation
23848
23849 As indicated above, on some platforms several run-time libraries are supplied.
23850 These libraries are installed in the target dependent area and
23851 contain a complete source and binary subdirectory. The detailed description
23852 below explains the differences between the different libraries in terms of
23853 their thread support.
23854
23855 The default run-time library (when GNAT is installed) is @emph{rts-native}.
23856 This default run-time is selected by the means of soft links.
23857 For example on x86-linux:
23858
23859 @c --
23860 @c -- $(target-dir)
23861 @c -- |
23862 @c -- +--- adainclude----------+
23863 @c -- | |
23864 @c -- +--- adalib-----------+ |
23865 @c -- | | |
23866 @c -- +--- rts-native | |
23867 @c -- | | | |
23868 @c -- | +--- adainclude <---+
23869 @c -- | | |
23870 @c -- | +--- adalib <----+
23871 @c -- |
23872 @c -- +--- rts-sjlj
23873 @c -- |
23874 @c -- +--- adainclude
23875 @c -- |
23876 @c -- +--- adalib
23877
23878
23879 @example
23880 $(target-dir)
23881 __/ / \ \___
23882 _______/ / \ \_________________
23883 / / \ \
23884 / / \ \
23885 ADAINCLUDE ADALIB rts-native rts-sjlj
23886 : : / \ / \
23887 : : / \ / \
23888 : : / \ / \
23889 : : / \ / \
23890 +-------------> adainclude adalib adainclude adalib
23891 : ^
23892 : :
23893 +---------------------+
23894
23895 Run-Time Library Directory Structure
23896 (Upper-case names and dotted/dashed arrows represent soft links)
23897 @end example
23898
23899 If the @emph{rts-sjlj} library is to be selected on a permanent basis,
23900 these soft links can be modified with the following commands:
23901
23902 @quotation
23903
23904 @example
23905 $ cd $target
23906 $ rm -f adainclude adalib
23907 $ ln -s rts-sjlj/adainclude adainclude
23908 $ ln -s rts-sjlj/adalib adalib
23909 @end example
23910 @end quotation
23911
23912 Alternatively, you can specify @code{rts-sjlj/adainclude} in the file
23913 @code{$target/ada_source_path} and @code{rts-sjlj/adalib} in
23914 @code{$target/ada_object_path}.
23915
23916 @geindex --RTS option
23917
23918 Selecting another run-time library temporarily can be
23919 achieved by using the @code{--RTS} switch, e.g., @code{--RTS=sjlj}
23920 @anchor{gnat_ugn/platform_specific_information choosing-the-scheduling-policy}@anchor{1d2}
23921 @geindex SCHED_FIFO scheduling policy
23922
23923 @geindex SCHED_RR scheduling policy
23924
23925 @geindex SCHED_OTHER scheduling policy
23926
23927 @menu
23928 * Choosing the Scheduling Policy::
23929
23930 @end menu
23931
23932 @node Choosing the Scheduling Policy,,,Specifying a Run-Time Library
23933 @anchor{gnat_ugn/platform_specific_information id5}@anchor{1d3}
23934 @subsection Choosing the Scheduling Policy
23935
23936
23937 When using a POSIX threads implementation, you have a choice of several
23938 scheduling policies: @code{SCHED_FIFO}, @code{SCHED_RR} and @code{SCHED_OTHER}.
23939
23940 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
23941 or @code{SCHED_RR} requires special (e.g., root) privileges.
23942
23943 @geindex pragma Time_Slice
23944
23945 @geindex -T0 option
23946
23947 @geindex pragma Task_Dispatching_Policy
23948
23949 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
23950 @code{SCHED_FIFO},
23951 you can use one of the following:
23952
23953
23954 @itemize *
23955
23956 @item
23957 @code{pragma Time_Slice (0.0)}
23958
23959 @item
23960 the corresponding binder option @code{-T0}
23961
23962 @item
23963 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
23964 @end itemize
23965
23966 To specify @code{SCHED_RR},
23967 you should use @code{pragma Time_Slice} with a
23968 value greater than 0.0, or else use the corresponding @code{-T}
23969 binder option.
23970
23971 To make sure a program is running as root, you can put something like
23972 this in a library package body in your application:
23973
23974 @quotation
23975
23976 @example
23977 function geteuid return Integer;
23978 pragma Import (C, geteuid, "geteuid");
23979 Ignore : constant Boolean :=
23980 (if geteuid = 0 then True else raise Program_Error with "must be root");
23981 @end example
23982 @end quotation
23983
23984 It gets the effective user id, and if it's not 0 (i.e. root), it raises
23985 Program_Error.
23986
23987 @geindex Linux
23988
23989 @geindex GNU/Linux
23990
23991 @node GNU/Linux Topics,Microsoft Windows Topics,Specifying a Run-Time Library,Platform-Specific Information
23992 @anchor{gnat_ugn/platform_specific_information id6}@anchor{1d4}@anchor{gnat_ugn/platform_specific_information gnu-linux-topics}@anchor{1d5}
23993 @section GNU/Linux Topics
23994
23995
23996 This section describes topics that are specific to GNU/Linux platforms.
23997
23998 @menu
23999 * Required Packages on GNU/Linux::
24000
24001 @end menu
24002
24003 @node Required Packages on GNU/Linux,,,GNU/Linux Topics
24004 @anchor{gnat_ugn/platform_specific_information id7}@anchor{1d6}@anchor{gnat_ugn/platform_specific_information required-packages-on-gnu-linux}@anchor{1d7}
24005 @subsection Required Packages on GNU/Linux
24006
24007
24008 GNAT requires the C library developer's package to be installed.
24009 The name of of that package depends on your GNU/Linux distribution:
24010
24011
24012 @itemize *
24013
24014 @item
24015 RedHat, SUSE: @code{glibc-devel};
24016
24017 @item
24018 Debian, Ubuntu: @code{libc6-dev} (normally installed by default).
24019 @end itemize
24020
24021 If using the 32-bit version of GNAT on a 64-bit version of GNU/Linux,
24022 you'll need the 32-bit version of the following packages:
24023
24024
24025 @itemize *
24026
24027 @item
24028 RedHat, SUSE: @code{glibc.i686}, @code{glibc-devel.i686}, @code{ncurses-libs.i686}
24029
24030 @item
24031 Debian, Ubuntu: @code{libc6:i386}, @code{libc6-dev:i386}, @code{lib32ncursesw5}
24032 @end itemize
24033
24034 Other GNU/Linux distributions might be choosing a different name
24035 for those packages.
24036
24037 @geindex Windows
24038
24039 @node Microsoft Windows Topics,Mac OS Topics,GNU/Linux Topics,Platform-Specific Information
24040 @anchor{gnat_ugn/platform_specific_information microsoft-windows-topics}@anchor{2c}@anchor{gnat_ugn/platform_specific_information id8}@anchor{1d8}
24041 @section Microsoft Windows Topics
24042
24043
24044 This section describes topics that are specific to the Microsoft Windows
24045 platforms.
24046
24047
24048 @menu
24049 * Using GNAT on Windows::
24050 * Using a network installation of GNAT::
24051 * CONSOLE and WINDOWS subsystems::
24052 * Temporary Files::
24053 * Disabling Command Line Argument Expansion::
24054 * Windows Socket Timeouts::
24055 * Mixed-Language Programming on Windows::
24056 * Windows Specific Add-Ons::
24057
24058 @end menu
24059
24060 @node Using GNAT on Windows,Using a network installation of GNAT,,Microsoft Windows Topics
24061 @anchor{gnat_ugn/platform_specific_information using-gnat-on-windows}@anchor{1d9}@anchor{gnat_ugn/platform_specific_information id9}@anchor{1da}
24062 @subsection Using GNAT on Windows
24063
24064
24065 One of the strengths of the GNAT technology is that its tool set
24066 (@code{gcc}, @code{gnatbind}, @code{gnatlink}, @code{gnatmake}, the
24067 @code{gdb} debugger, etc.) is used in the same way regardless of the
24068 platform.
24069
24070 On Windows this tool set is complemented by a number of Microsoft-specific
24071 tools that have been provided to facilitate interoperability with Windows
24072 when this is required. With these tools:
24073
24074
24075 @itemize *
24076
24077 @item
24078 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
24079 subsystems.
24080
24081 @item
24082 You can use any Dynamically Linked Library (DLL) in your Ada code (both
24083 relocatable and non-relocatable DLLs are supported).
24084
24085 @item
24086 You can build Ada DLLs for use in other applications. These applications
24087 can be written in a language other than Ada (e.g., C, C++, etc). Again both
24088 relocatable and non-relocatable Ada DLLs are supported.
24089
24090 @item
24091 You can include Windows resources in your Ada application.
24092
24093 @item
24094 You can use or create COM/DCOM objects.
24095 @end itemize
24096
24097 Immediately below are listed all known general GNAT-for-Windows restrictions.
24098 Other restrictions about specific features like Windows Resources and DLLs
24099 are listed in separate sections below.
24100
24101
24102 @itemize *
24103
24104 @item
24105 It is not possible to use @code{GetLastError} and @code{SetLastError}
24106 when tasking, protected records, or exceptions are used. In these
24107 cases, in order to implement Ada semantics, the GNAT run-time system
24108 calls certain Win32 routines that set the last error variable to 0 upon
24109 success. It should be possible to use @code{GetLastError} and
24110 @code{SetLastError} when tasking, protected record, and exception
24111 features are not used, but it is not guaranteed to work.
24112
24113 @item
24114 It is not possible to link against Microsoft C++ libraries except for
24115 import libraries. Interfacing must be done by the mean of DLLs.
24116
24117 @item
24118 It is possible to link against Microsoft C libraries. Yet the preferred
24119 solution is to use C/C++ compiler that comes with GNAT, since it
24120 doesn't require having two different development environments and makes the
24121 inter-language debugging experience smoother.
24122
24123 @item
24124 When the compilation environment is located on FAT32 drives, users may
24125 experience recompilations of the source files that have not changed if
24126 Daylight Saving Time (DST) state has changed since the last time files
24127 were compiled. NTFS drives do not have this problem.
24128
24129 @item
24130 No components of the GNAT toolset use any entries in the Windows
24131 registry. The only entries that can be created are file associations and
24132 PATH settings, provided the user has chosen to create them at installation
24133 time, as well as some minimal book-keeping information needed to correctly
24134 uninstall or integrate different GNAT products.
24135 @end itemize
24136
24137 @node Using a network installation of GNAT,CONSOLE and WINDOWS subsystems,Using GNAT on Windows,Microsoft Windows Topics
24138 @anchor{gnat_ugn/platform_specific_information id10}@anchor{1db}@anchor{gnat_ugn/platform_specific_information using-a-network-installation-of-gnat}@anchor{1dc}
24139 @subsection Using a network installation of GNAT
24140
24141
24142 Make sure the system on which GNAT is installed is accessible from the
24143 current machine, i.e., the install location is shared over the network.
24144 Shared resources are accessed on Windows by means of UNC paths, which
24145 have the format @code{\\\\server\\sharename\\path}
24146
24147 In order to use such a network installation, simply add the UNC path of the
24148 @code{bin} directory of your GNAT installation in front of your PATH. For
24149 example, if GNAT is installed in @code{\GNAT} directory of a share location
24150 called @code{c-drive} on a machine @code{LOKI}, the following command will
24151 make it available:
24152
24153 @quotation
24154
24155 @example
24156 $ path \\loki\c-drive\gnat\bin;%path%`
24157 @end example
24158 @end quotation
24159
24160 Be aware that every compilation using the network installation results in the
24161 transfer of large amounts of data across the network and will likely cause
24162 serious performance penalty.
24163
24164 @node CONSOLE and WINDOWS subsystems,Temporary Files,Using a network installation of GNAT,Microsoft Windows Topics
24165 @anchor{gnat_ugn/platform_specific_information id11}@anchor{1dd}@anchor{gnat_ugn/platform_specific_information console-and-windows-subsystems}@anchor{1de}
24166 @subsection CONSOLE and WINDOWS subsystems
24167
24168
24169 @geindex CONSOLE Subsystem
24170
24171 @geindex WINDOWS Subsystem
24172
24173 @geindex -mwindows
24174
24175 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
24176 (which is the default subsystem) will always create a console when
24177 launching the application. This is not something desirable when the
24178 application has a Windows GUI. To get rid of this console the
24179 application must be using the @code{WINDOWS} subsystem. To do so
24180 the @code{-mwindows} linker option must be specified.
24181
24182 @quotation
24183
24184 @example
24185 $ gnatmake winprog -largs -mwindows
24186 @end example
24187 @end quotation
24188
24189 @node Temporary Files,Disabling Command Line Argument Expansion,CONSOLE and WINDOWS subsystems,Microsoft Windows Topics
24190 @anchor{gnat_ugn/platform_specific_information id12}@anchor{1df}@anchor{gnat_ugn/platform_specific_information temporary-files}@anchor{1e0}
24191 @subsection Temporary Files
24192
24193
24194 @geindex Temporary files
24195
24196 It is possible to control where temporary files gets created by setting
24197 the
24198 @geindex TMP
24199 @geindex environment variable; TMP
24200 @code{TMP} environment variable. The file will be created:
24201
24202
24203 @itemize *
24204
24205 @item
24206 Under the directory pointed to by the
24207 @geindex TMP
24208 @geindex environment variable; TMP
24209 @code{TMP} environment variable if
24210 this directory exists.
24211
24212 @item
24213 Under @code{c:\temp}, if the
24214 @geindex TMP
24215 @geindex environment variable; TMP
24216 @code{TMP} environment variable is not
24217 set (or not pointing to a directory) and if this directory exists.
24218
24219 @item
24220 Under the current working directory otherwise.
24221 @end itemize
24222
24223 This allows you to determine exactly where the temporary
24224 file will be created. This is particularly useful in networked
24225 environments where you may not have write access to some
24226 directories.
24227
24228 @node Disabling Command Line Argument Expansion,Windows Socket Timeouts,Temporary Files,Microsoft Windows Topics
24229 @anchor{gnat_ugn/platform_specific_information disabling-command-line-argument-expansion}@anchor{1e1}
24230 @subsection Disabling Command Line Argument Expansion
24231
24232
24233 @geindex Command Line Argument Expansion
24234
24235 By default, an executable compiled for the Windows platform will do
24236 the following postprocessing on the arguments passed on the command
24237 line:
24238
24239
24240 @itemize *
24241
24242 @item
24243 If the argument contains the characters @code{*} and/or @code{?}, then
24244 file expansion will be attempted. For example, if the current directory
24245 contains @code{a.txt} and @code{b.txt}, then when calling:
24246
24247 @example
24248 $ my_ada_program *.txt
24249 @end example
24250
24251 The following arguments will effectively be passed to the main program
24252 (for example when using @code{Ada.Command_Line.Argument}):
24253
24254 @example
24255 Ada.Command_Line.Argument (1) -> "a.txt"
24256 Ada.Command_Line.Argument (2) -> "b.txt"
24257 @end example
24258
24259 @item
24260 Filename expansion can be disabled for a given argument by using single
24261 quotes. Thus, calling:
24262
24263 @example
24264 $ my_ada_program '*.txt'
24265 @end example
24266
24267 will result in:
24268
24269 @example
24270 Ada.Command_Line.Argument (1) -> "*.txt"
24271 @end example
24272 @end itemize
24273
24274 Note that if the program is launched from a shell such as Cygwin Bash
24275 then quote removal might be performed by the shell.
24276
24277 In some contexts it might be useful to disable this feature (for example if
24278 the program performs its own argument expansion). In order to do this, a C
24279 symbol needs to be defined and set to @code{0}. You can do this by
24280 adding the following code fragment in one of your Ada units:
24281
24282 @example
24283 Do_Argv_Expansion : Integer := 0;
24284 pragma Export (C, Do_Argv_Expansion, "__gnat_do_argv_expansion");
24285 @end example
24286
24287 The results of previous examples will be respectively:
24288
24289 @example
24290 Ada.Command_Line.Argument (1) -> "*.txt"
24291 @end example
24292
24293 and:
24294
24295 @example
24296 Ada.Command_Line.Argument (1) -> "'*.txt'"
24297 @end example
24298
24299 @node Windows Socket Timeouts,Mixed-Language Programming on Windows,Disabling Command Line Argument Expansion,Microsoft Windows Topics
24300 @anchor{gnat_ugn/platform_specific_information windows-socket-timeouts}@anchor{1e2}
24301 @subsection Windows Socket Timeouts
24302
24303
24304 Microsoft Windows desktops older than @code{8.0} and Microsoft Windows Servers
24305 older than @code{2019} set a socket timeout 500 milliseconds longer than the value
24306 set by setsockopt with @code{SO_RCVTIMEO} and @code{SO_SNDTIMEO} options. The GNAT
24307 runtime makes a correction for the difference in the corresponding Windows
24308 versions. For Windows Server starting with version @code{2019}, the user must
24309 provide a manifest file for the GNAT runtime to be able to recognize that
24310 the Windows version does not need the timeout correction. The manifest file
24311 should be located in the same directory as the executable file, and its file
24312 name must match the executable name suffixed by @code{.manifest}. For example,
24313 if the executable name is @code{sock_wto.exe}, then the manifest file name
24314 has to be @code{sock_wto.exe.manifest}. The manifest file must contain at
24315 least the following data:
24316
24317 @example
24318 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
24319 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
24320 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
24321 <application>
24322 <!-- Windows Vista -->
24323 <supportedOS Id="@{e2011457-1546-43c5-a5fe-008deee3d3f0@}"/>
24324 <!-- Windows 7 -->
24325 <supportedOS Id="@{35138b9a-5d96-4fbd-8e2d-a2440225f93a@}"/>
24326 <!-- Windows 8 -->
24327 <supportedOS Id="@{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38@}"/>
24328 <!-- Windows 8.1 -->
24329 <supportedOS Id="@{1f676c76-80e1-4239-95bb-83d0f6d0da78@}"/>
24330 <!-- Windows 10 -->
24331 <supportedOS Id="@{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a@}"/>
24332 </application>
24333 </compatibility>
24334 </assembly>
24335 @end example
24336
24337 Without the manifest file, the socket timeout is going to be overcorrected on
24338 these Windows Server versions and the actual time is going to be 500
24339 milliseconds shorter than what was set with GNAT.Sockets.Set_Socket_Option.
24340 Note that on Microsoft Windows versions where correction is necessary, there
24341 is no way to set a socket timeout shorter than 500 ms. If a socket timeout
24342 shorter than 500 ms is needed on these Windows versions, a call to
24343 Check_Selector should be added before any socket read or write operations.
24344
24345 @node Mixed-Language Programming on Windows,Windows Specific Add-Ons,Windows Socket Timeouts,Microsoft Windows Topics
24346 @anchor{gnat_ugn/platform_specific_information id13}@anchor{1e3}@anchor{gnat_ugn/platform_specific_information mixed-language-programming-on-windows}@anchor{1e4}
24347 @subsection Mixed-Language Programming on Windows
24348
24349
24350 Developing pure Ada applications on Windows is no different than on
24351 other GNAT-supported platforms. However, when developing or porting an
24352 application that contains a mix of Ada and C/C++, the choice of your
24353 Windows C/C++ development environment conditions your overall
24354 interoperability strategy.
24355
24356 If you use @code{gcc} or Microsoft C to compile the non-Ada part of
24357 your application, there are no Windows-specific restrictions that
24358 affect the overall interoperability with your Ada code. If you do want
24359 to use the Microsoft tools for your C++ code, you have two choices:
24360
24361
24362 @itemize *
24363
24364 @item
24365 Encapsulate your C++ code in a DLL to be linked with your Ada
24366 application. In this case, use the Microsoft or whatever environment to
24367 build the DLL and use GNAT to build your executable
24368 (@ref{1e5,,Using DLLs with GNAT}).
24369
24370 @item
24371 Or you can encapsulate your Ada code in a DLL to be linked with the
24372 other part of your application. In this case, use GNAT to build the DLL
24373 (@ref{1e6,,Building DLLs with GNAT Project files}) and use the Microsoft
24374 or whatever environment to build your executable.
24375 @end itemize
24376
24377 In addition to the description about C main in
24378 @ref{44,,Mixed Language Programming} section, if the C main uses a
24379 stand-alone library it is required on x86-windows to
24380 setup the SEH context. For this the C main must looks like this:
24381
24382 @quotation
24383
24384 @example
24385 /* main.c */
24386 extern void adainit (void);
24387 extern void adafinal (void);
24388 extern void __gnat_initialize(void*);
24389 extern void call_to_ada (void);
24390
24391 int main (int argc, char *argv[])
24392 @{
24393 int SEH [2];
24394
24395 /* Initialize the SEH context */
24396 __gnat_initialize (&SEH);
24397
24398 adainit();
24399
24400 /* Then call Ada services in the stand-alone library */
24401
24402 call_to_ada();
24403
24404 adafinal();
24405 @}
24406 @end example
24407 @end quotation
24408
24409 Note that this is not needed on x86_64-windows where the Windows
24410 native SEH support is used.
24411
24412 @menu
24413 * Windows Calling Conventions::
24414 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
24415 * Using DLLs with GNAT::
24416 * Building DLLs with GNAT Project files::
24417 * Building DLLs with GNAT::
24418 * Building DLLs with gnatdll::
24419 * Ada DLLs and Finalization::
24420 * Creating a Spec for Ada DLLs::
24421 * GNAT and Windows Resources::
24422 * Using GNAT DLLs from Microsoft Visual Studio Applications::
24423 * Debugging a DLL::
24424 * Setting Stack Size from gnatlink::
24425 * Setting Heap Size from gnatlink::
24426
24427 @end menu
24428
24429 @node Windows Calling Conventions,Introduction to Dynamic Link Libraries DLLs,,Mixed-Language Programming on Windows
24430 @anchor{gnat_ugn/platform_specific_information windows-calling-conventions}@anchor{1e7}@anchor{gnat_ugn/platform_specific_information id14}@anchor{1e8}
24431 @subsubsection Windows Calling Conventions
24432
24433
24434 @geindex Stdcall
24435
24436 @geindex APIENTRY
24437
24438 This section pertain only to Win32. On Win64 there is a single native
24439 calling convention. All convention specifiers are ignored on this
24440 platform.
24441
24442 When a subprogram @code{F} (caller) calls a subprogram @code{G}
24443 (callee), there are several ways to push @code{G}'s parameters on the
24444 stack and there are several possible scenarios to clean up the stack
24445 upon @code{G}'s return. A calling convention is an agreed upon software
24446 protocol whereby the responsibilities between the caller (@code{F}) and
24447 the callee (@code{G}) are clearly defined. Several calling conventions
24448 are available for Windows:
24449
24450
24451 @itemize *
24452
24453 @item
24454 @code{C} (Microsoft defined)
24455
24456 @item
24457 @code{Stdcall} (Microsoft defined)
24458
24459 @item
24460 @code{Win32} (GNAT specific)
24461
24462 @item
24463 @code{DLL} (GNAT specific)
24464 @end itemize
24465
24466 @menu
24467 * C Calling Convention::
24468 * Stdcall Calling Convention::
24469 * Win32 Calling Convention::
24470 * DLL Calling Convention::
24471
24472 @end menu
24473
24474 @node C Calling Convention,Stdcall Calling Convention,,Windows Calling Conventions
24475 @anchor{gnat_ugn/platform_specific_information c-calling-convention}@anchor{1e9}@anchor{gnat_ugn/platform_specific_information id15}@anchor{1ea}
24476 @subsubsection @code{C} Calling Convention
24477
24478
24479 This is the default calling convention used when interfacing to C/C++
24480 routines compiled with either @code{gcc} or Microsoft Visual C++.
24481
24482 In the @code{C} calling convention subprogram parameters are pushed on the
24483 stack by the caller from right to left. The caller itself is in charge of
24484 cleaning up the stack after the call. In addition, the name of a routine
24485 with @code{C} calling convention is mangled by adding a leading underscore.
24486
24487 The name to use on the Ada side when importing (or exporting) a routine
24488 with @code{C} calling convention is the name of the routine. For
24489 instance the C function:
24490
24491 @quotation
24492
24493 @example
24494 int get_val (long);
24495 @end example
24496 @end quotation
24497
24498 should be imported from Ada as follows:
24499
24500 @quotation
24501
24502 @example
24503 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24504 pragma Import (C, Get_Val, External_Name => "get_val");
24505 @end example
24506 @end quotation
24507
24508 Note that in this particular case the @code{External_Name} parameter could
24509 have been omitted since, when missing, this parameter is taken to be the
24510 name of the Ada entity in lower case. When the @code{Link_Name} parameter
24511 is missing, as in the above example, this parameter is set to be the
24512 @code{External_Name} with a leading underscore.
24513
24514 When importing a variable defined in C, you should always use the @code{C}
24515 calling convention unless the object containing the variable is part of a
24516 DLL (in which case you should use the @code{Stdcall} calling
24517 convention, @ref{1eb,,Stdcall Calling Convention}).
24518
24519 @node Stdcall Calling Convention,Win32 Calling Convention,C Calling Convention,Windows Calling Conventions
24520 @anchor{gnat_ugn/platform_specific_information stdcall-calling-convention}@anchor{1eb}@anchor{gnat_ugn/platform_specific_information id16}@anchor{1ec}
24521 @subsubsection @code{Stdcall} Calling Convention
24522
24523
24524 This convention, which was the calling convention used for Pascal
24525 programs, is used by Microsoft for all the routines in the Win32 API for
24526 efficiency reasons. It must be used to import any routine for which this
24527 convention was specified.
24528
24529 In the @code{Stdcall} calling convention subprogram parameters are pushed
24530 on the stack by the caller from right to left. The callee (and not the
24531 caller) is in charge of cleaning the stack on routine exit. In addition,
24532 the name of a routine with @code{Stdcall} calling convention is mangled by
24533 adding a leading underscore (as for the @code{C} calling convention) and a
24534 trailing @code{@@@emph{nn}}, where @code{nn} is the overall size (in
24535 bytes) of the parameters passed to the routine.
24536
24537 The name to use on the Ada side when importing a C routine with a
24538 @code{Stdcall} calling convention is the name of the C routine. The leading
24539 underscore and trailing @code{@@@emph{nn}} are added automatically by
24540 the compiler. For instance the Win32 function:
24541
24542 @quotation
24543
24544 @example
24545 APIENTRY int get_val (long);
24546 @end example
24547 @end quotation
24548
24549 should be imported from Ada as follows:
24550
24551 @quotation
24552
24553 @example
24554 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24555 pragma Import (Stdcall, Get_Val);
24556 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
24557 @end example
24558 @end quotation
24559
24560 As for the @code{C} calling convention, when the @code{External_Name}
24561 parameter is missing, it is taken to be the name of the Ada entity in lower
24562 case. If instead of writing the above import pragma you write:
24563
24564 @quotation
24565
24566 @example
24567 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24568 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
24569 @end example
24570 @end quotation
24571
24572 then the imported routine is @code{_retrieve_val@@4}. However, if instead
24573 of specifying the @code{External_Name} parameter you specify the
24574 @code{Link_Name} as in the following example:
24575
24576 @quotation
24577
24578 @example
24579 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24580 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
24581 @end example
24582 @end quotation
24583
24584 then the imported routine is @code{retrieve_val}, that is, there is no
24585 decoration at all. No leading underscore and no Stdcall suffix
24586 @code{@@@emph{nn}}.
24587
24588 This is especially important as in some special cases a DLL's entry
24589 point name lacks a trailing @code{@@@emph{nn}} while the exported
24590 name generated for a call has it.
24591
24592 It is also possible to import variables defined in a DLL by using an
24593 import pragma for a variable. As an example, if a DLL contains a
24594 variable defined as:
24595
24596 @quotation
24597
24598 @example
24599 int my_var;
24600 @end example
24601 @end quotation
24602
24603 then, to access this variable from Ada you should write:
24604
24605 @quotation
24606
24607 @example
24608 My_Var : Interfaces.C.int;
24609 pragma Import (Stdcall, My_Var);
24610 @end example
24611 @end quotation
24612
24613 Note that to ease building cross-platform bindings this convention
24614 will be handled as a @code{C} calling convention on non-Windows platforms.
24615
24616 @node Win32 Calling Convention,DLL Calling Convention,Stdcall Calling Convention,Windows Calling Conventions
24617 @anchor{gnat_ugn/platform_specific_information win32-calling-convention}@anchor{1ed}@anchor{gnat_ugn/platform_specific_information id17}@anchor{1ee}
24618 @subsubsection @code{Win32} Calling Convention
24619
24620
24621 This convention, which is GNAT-specific is fully equivalent to the
24622 @code{Stdcall} calling convention described above.
24623
24624 @node DLL Calling Convention,,Win32 Calling Convention,Windows Calling Conventions
24625 @anchor{gnat_ugn/platform_specific_information id18}@anchor{1ef}@anchor{gnat_ugn/platform_specific_information dll-calling-convention}@anchor{1f0}
24626 @subsubsection @code{DLL} Calling Convention
24627
24628
24629 This convention, which is GNAT-specific is fully equivalent to the
24630 @code{Stdcall} calling convention described above.
24631
24632 @node Introduction to Dynamic Link Libraries DLLs,Using DLLs with GNAT,Windows Calling Conventions,Mixed-Language Programming on Windows
24633 @anchor{gnat_ugn/platform_specific_information id19}@anchor{1f1}@anchor{gnat_ugn/platform_specific_information introduction-to-dynamic-link-libraries-dlls}@anchor{1f2}
24634 @subsubsection Introduction to Dynamic Link Libraries (DLLs)
24635
24636
24637 @geindex DLL
24638
24639 A Dynamically Linked Library (DLL) is a library that can be shared by
24640 several applications running under Windows. A DLL can contain any number of
24641 routines and variables.
24642
24643 One advantage of DLLs is that you can change and enhance them without
24644 forcing all the applications that depend on them to be relinked or
24645 recompiled. However, you should be aware than all calls to DLL routines are
24646 slower since, as you will understand below, such calls are indirect.
24647
24648 To illustrate the remainder of this section, suppose that an application
24649 wants to use the services of a DLL @code{API.dll}. To use the services
24650 provided by @code{API.dll} you must statically link against the DLL or
24651 an import library which contains a jump table with an entry for each
24652 routine and variable exported by the DLL. In the Microsoft world this
24653 import library is called @code{API.lib}. When using GNAT this import
24654 library is called either @code{libAPI.dll.a}, @code{libapi.dll.a},
24655 @code{libAPI.a} or @code{libapi.a} (names are case insensitive).
24656
24657 After you have linked your application with the DLL or the import library
24658 and you run your application, here is what happens:
24659
24660
24661 @itemize *
24662
24663 @item
24664 Your application is loaded into memory.
24665
24666 @item
24667 The DLL @code{API.dll} is mapped into the address space of your
24668 application. This means that:
24669
24670
24671 @itemize -
24672
24673 @item
24674 The DLL will use the stack of the calling thread.
24675
24676 @item
24677 The DLL will use the virtual address space of the calling process.
24678
24679 @item
24680 The DLL will allocate memory from the virtual address space of the calling
24681 process.
24682
24683 @item
24684 Handles (pointers) can be safely exchanged between routines in the DLL
24685 routines and routines in the application using the DLL.
24686 @end itemize
24687
24688 @item
24689 The entries in the jump table (from the import library @code{libAPI.dll.a}
24690 or @code{API.lib} or automatically created when linking against a DLL)
24691 which is part of your application are initialized with the addresses
24692 of the routines and variables in @code{API.dll}.
24693
24694 @item
24695 If present in @code{API.dll}, routines @code{DllMain} or
24696 @code{DllMainCRTStartup} are invoked. These routines typically contain
24697 the initialization code needed for the well-being of the routines and
24698 variables exported by the DLL.
24699 @end itemize
24700
24701 There is an additional point which is worth mentioning. In the Windows
24702 world there are two kind of DLLs: relocatable and non-relocatable
24703 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
24704 in the target application address space. If the addresses of two
24705 non-relocatable DLLs overlap and these happen to be used by the same
24706 application, a conflict will occur and the application will run
24707 incorrectly. Hence, when possible, it is always preferable to use and
24708 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
24709 supported by GNAT. Note that the @code{-s} linker option (see GNU Linker
24710 User's Guide) removes the debugging symbols from the DLL but the DLL can
24711 still be relocated.
24712
24713 As a side note, an interesting difference between Microsoft DLLs and
24714 Unix shared libraries, is the fact that on most Unix systems all public
24715 routines are exported by default in a Unix shared library, while under
24716 Windows it is possible (but not required) to list exported routines in
24717 a definition file (see @ref{1f3,,The Definition File}).
24718
24719 @node Using DLLs with GNAT,Building DLLs with GNAT Project files,Introduction to Dynamic Link Libraries DLLs,Mixed-Language Programming on Windows
24720 @anchor{gnat_ugn/platform_specific_information id20}@anchor{1f4}@anchor{gnat_ugn/platform_specific_information using-dlls-with-gnat}@anchor{1e5}
24721 @subsubsection Using DLLs with GNAT
24722
24723
24724 To use the services of a DLL, say @code{API.dll}, in your Ada application
24725 you must have:
24726
24727
24728 @itemize *
24729
24730 @item
24731 The Ada spec for the routines and/or variables you want to access in
24732 @code{API.dll}. If not available this Ada spec must be built from the C/C++
24733 header files provided with the DLL.
24734
24735 @item
24736 The import library (@code{libAPI.dll.a} or @code{API.lib}). As previously
24737 mentioned an import library is a statically linked library containing the
24738 import table which will be filled at load time to point to the actual
24739 @code{API.dll} routines. Sometimes you don't have an import library for the
24740 DLL you want to use. The following sections will explain how to build
24741 one. Note that this is optional.
24742
24743 @item
24744 The actual DLL, @code{API.dll}.
24745 @end itemize
24746
24747 Once you have all the above, to compile an Ada application that uses the
24748 services of @code{API.dll} and whose main subprogram is @code{My_Ada_App},
24749 you simply issue the command
24750
24751 @quotation
24752
24753 @example
24754 $ gnatmake my_ada_app -largs -lAPI
24755 @end example
24756 @end quotation
24757
24758 The argument @code{-largs -lAPI} at the end of the @code{gnatmake} command
24759 tells the GNAT linker to look for an import library. The linker will
24760 look for a library name in this specific order:
24761
24762
24763 @itemize *
24764
24765 @item
24766 @code{libAPI.dll.a}
24767
24768 @item
24769 @code{API.dll.a}
24770
24771 @item
24772 @code{libAPI.a}
24773
24774 @item
24775 @code{API.lib}
24776
24777 @item
24778 @code{libAPI.dll}
24779
24780 @item
24781 @code{API.dll}
24782 @end itemize
24783
24784 The first three are the GNU style import libraries. The third is the
24785 Microsoft style import libraries. The last two are the actual DLL names.
24786
24787 Note that if the Ada package spec for @code{API.dll} contains the
24788 following pragma
24789
24790 @quotation
24791
24792 @example
24793 pragma Linker_Options ("-lAPI");
24794 @end example
24795 @end quotation
24796
24797 you do not have to add @code{-largs -lAPI} at the end of the
24798 @code{gnatmake} command.
24799
24800 If any one of the items above is missing you will have to create it
24801 yourself. The following sections explain how to do so using as an
24802 example a fictitious DLL called @code{API.dll}.
24803
24804 @menu
24805 * Creating an Ada Spec for the DLL Services::
24806 * Creating an Import Library::
24807
24808 @end menu
24809
24810 @node Creating an Ada Spec for the DLL Services,Creating an Import Library,,Using DLLs with GNAT
24811 @anchor{gnat_ugn/platform_specific_information id21}@anchor{1f5}@anchor{gnat_ugn/platform_specific_information creating-an-ada-spec-for-the-dll-services}@anchor{1f6}
24812 @subsubsection Creating an Ada Spec for the DLL Services
24813
24814
24815 A DLL typically comes with a C/C++ header file which provides the
24816 definitions of the routines and variables exported by the DLL. The Ada
24817 equivalent of this header file is a package spec that contains definitions
24818 for the imported entities. If the DLL you intend to use does not come with
24819 an Ada spec you have to generate one such spec yourself. For example if
24820 the header file of @code{API.dll} is a file @code{api.h} containing the
24821 following two definitions:
24822
24823 @quotation
24824
24825 @example
24826 int some_var;
24827 int get (char *);
24828 @end example
24829 @end quotation
24830
24831 then the equivalent Ada spec could be:
24832
24833 @quotation
24834
24835 @example
24836 with Interfaces.C.Strings;
24837 package API is
24838 use Interfaces;
24839
24840 Some_Var : C.int;
24841 function Get (Str : C.Strings.Chars_Ptr) return C.int;
24842
24843 private
24844 pragma Import (C, Get);
24845 pragma Import (DLL, Some_Var);
24846 end API;
24847 @end example
24848 @end quotation
24849
24850 @node Creating an Import Library,,Creating an Ada Spec for the DLL Services,Using DLLs with GNAT
24851 @anchor{gnat_ugn/platform_specific_information id22}@anchor{1f7}@anchor{gnat_ugn/platform_specific_information creating-an-import-library}@anchor{1f8}
24852 @subsubsection Creating an Import Library
24853
24854
24855 @geindex Import library
24856
24857 If a Microsoft-style import library @code{API.lib} or a GNAT-style
24858 import library @code{libAPI.dll.a} or @code{libAPI.a} is available
24859 with @code{API.dll} you can skip this section. You can also skip this
24860 section if @code{API.dll} or @code{libAPI.dll} is built with GNU tools
24861 as in this case it is possible to link directly against the
24862 DLL. Otherwise read on.
24863
24864 @geindex Definition file
24865 @anchor{gnat_ugn/platform_specific_information the-definition-file}@anchor{1f3}
24866 @subsubheading The Definition File
24867
24868
24869 As previously mentioned, and unlike Unix systems, the list of symbols
24870 that are exported from a DLL must be provided explicitly in Windows.
24871 The main goal of a definition file is precisely that: list the symbols
24872 exported by a DLL. A definition file (usually a file with a @code{.def}
24873 suffix) has the following structure:
24874
24875 @quotation
24876
24877 @example
24878 [LIBRARY `@w{`}name`@w{`}]
24879 [DESCRIPTION `@w{`}string`@w{`}]
24880 EXPORTS
24881 `@w{`}symbol1`@w{`}
24882 `@w{`}symbol2`@w{`}
24883 ...
24884 @end example
24885 @end quotation
24886
24887
24888 @table @asis
24889
24890 @item @emph{LIBRARY name}
24891
24892 This section, which is optional, gives the name of the DLL.
24893
24894 @item @emph{DESCRIPTION string}
24895
24896 This section, which is optional, gives a description string that will be
24897 embedded in the import library.
24898
24899 @item @emph{EXPORTS}
24900
24901 This section gives the list of exported symbols (procedures, functions or
24902 variables). For instance in the case of @code{API.dll} the @code{EXPORTS}
24903 section of @code{API.def} looks like:
24904
24905 @example
24906 EXPORTS
24907 some_var
24908 get
24909 @end example
24910 @end table
24911
24912 Note that you must specify the correct suffix (@code{@@@emph{nn}})
24913 (see @ref{1e7,,Windows Calling Conventions}) for a Stdcall
24914 calling convention function in the exported symbols list.
24915
24916 There can actually be other sections in a definition file, but these
24917 sections are not relevant to the discussion at hand.
24918 @anchor{gnat_ugn/platform_specific_information create-def-file-automatically}@anchor{1f9}
24919 @subsubheading Creating a Definition File Automatically
24920
24921
24922 You can automatically create the definition file @code{API.def}
24923 (see @ref{1f3,,The Definition File}) from a DLL.
24924 For that use the @code{dlltool} program as follows:
24925
24926 @quotation
24927
24928 @example
24929 $ dlltool API.dll -z API.def --export-all-symbols
24930 @end example
24931
24932 Note that if some routines in the DLL have the @code{Stdcall} convention
24933 (@ref{1e7,,Windows Calling Conventions}) with stripped @code{@@@emph{nn}}
24934 suffix then you'll have to edit @code{api.def} to add it, and specify
24935 @code{-k} to @code{gnatdll} when creating the import library.
24936
24937 Here are some hints to find the right @code{@@@emph{nn}} suffix.
24938
24939
24940 @itemize -
24941
24942 @item
24943 If you have the Microsoft import library (.lib), it is possible to get
24944 the right symbols by using Microsoft @code{dumpbin} tool (see the
24945 corresponding Microsoft documentation for further details).
24946
24947 @example
24948 $ dumpbin /exports api.lib
24949 @end example
24950
24951 @item
24952 If you have a message about a missing symbol at link time the compiler
24953 tells you what symbol is expected. You just have to go back to the
24954 definition file and add the right suffix.
24955 @end itemize
24956 @end quotation
24957 @anchor{gnat_ugn/platform_specific_information gnat-style-import-library}@anchor{1fa}
24958 @subsubheading GNAT-Style Import Library
24959
24960
24961 To create a static import library from @code{API.dll} with the GNAT tools
24962 you should create the .def file, then use @code{gnatdll} tool
24963 (see @ref{1fb,,Using gnatdll}) as follows:
24964
24965 @quotation
24966
24967 @example
24968 $ gnatdll -e API.def -d API.dll
24969 @end example
24970
24971 @code{gnatdll} takes as input a definition file @code{API.def} and the
24972 name of the DLL containing the services listed in the definition file
24973 @code{API.dll}. The name of the static import library generated is
24974 computed from the name of the definition file as follows: if the
24975 definition file name is @code{xyz.def}, the import library name will
24976 be @code{libxyz.a}. Note that in the previous example option
24977 @code{-e} could have been removed because the name of the definition
24978 file (before the @code{.def} suffix) is the same as the name of the
24979 DLL (@ref{1fb,,Using gnatdll} for more information about @code{gnatdll}).
24980 @end quotation
24981 @anchor{gnat_ugn/platform_specific_information msvs-style-import-library}@anchor{1fc}
24982 @subsubheading Microsoft-Style Import Library
24983
24984
24985 A Microsoft import library is needed only if you plan to make an
24986 Ada DLL available to applications developed with Microsoft
24987 tools (@ref{1e4,,Mixed-Language Programming on Windows}).
24988
24989 To create a Microsoft-style import library for @code{API.dll} you
24990 should create the .def file, then build the actual import library using
24991 Microsoft's @code{lib} utility:
24992
24993 @quotation
24994
24995 @example
24996 $ lib -machine:IX86 -def:API.def -out:API.lib
24997 @end example
24998
24999 If you use the above command the definition file @code{API.def} must
25000 contain a line giving the name of the DLL:
25001
25002 @example
25003 LIBRARY "API"
25004 @end example
25005
25006 See the Microsoft documentation for further details about the usage of
25007 @code{lib}.
25008 @end quotation
25009
25010 @node Building DLLs with GNAT Project files,Building DLLs with GNAT,Using DLLs with GNAT,Mixed-Language Programming on Windows
25011 @anchor{gnat_ugn/platform_specific_information id23}@anchor{1fd}@anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat-project-files}@anchor{1e6}
25012 @subsubsection Building DLLs with GNAT Project files
25013
25014
25015 @geindex DLLs
25016 @geindex building
25017
25018 There is nothing specific to Windows in the build process.
25019 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
25020 chapter of the @emph{GPRbuild User's Guide}.
25021
25022 Due to a system limitation, it is not possible under Windows to create threads
25023 when inside the @code{DllMain} routine which is used for auto-initialization
25024 of shared libraries, so it is not possible to have library level tasks in SALs.
25025
25026 @node Building DLLs with GNAT,Building DLLs with gnatdll,Building DLLs with GNAT Project files,Mixed-Language Programming on Windows
25027 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat}@anchor{1fe}@anchor{gnat_ugn/platform_specific_information id24}@anchor{1ff}
25028 @subsubsection Building DLLs with GNAT
25029
25030
25031 @geindex DLLs
25032 @geindex building
25033
25034 This section explain how to build DLLs using the GNAT built-in DLL
25035 support. With the following procedure it is straight forward to build
25036 and use DLLs with GNAT.
25037
25038
25039 @itemize *
25040
25041 @item
25042 Building object files.
25043 The first step is to build all objects files that are to be included
25044 into the DLL. This is done by using the standard @code{gnatmake} tool.
25045
25046 @item
25047 Building the DLL.
25048 To build the DLL you must use the @code{gcc} @code{-shared} and
25049 @code{-shared-libgcc} options. It is quite simple to use this method:
25050
25051 @example
25052 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
25053 @end example
25054
25055 It is important to note that in this case all symbols found in the
25056 object files are automatically exported. It is possible to restrict
25057 the set of symbols to export by passing to @code{gcc} a definition
25058 file (see @ref{1f3,,The Definition File}).
25059 For example:
25060
25061 @example
25062 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
25063 @end example
25064
25065 If you use a definition file you must export the elaboration procedures
25066 for every package that required one. Elaboration procedures are named
25067 using the package name followed by "_E".
25068
25069 @item
25070 Preparing DLL to be used.
25071 For the DLL to be used by client programs the bodies must be hidden
25072 from it and the .ali set with read-only attribute. This is very important
25073 otherwise GNAT will recompile all packages and will not actually use
25074 the code in the DLL. For example:
25075
25076 @example
25077 $ mkdir apilib
25078 $ copy *.ads *.ali api.dll apilib
25079 $ attrib +R apilib\\*.ali
25080 @end example
25081 @end itemize
25082
25083 At this point it is possible to use the DLL by directly linking
25084 against it. Note that you must use the GNAT shared runtime when using
25085 GNAT shared libraries. This is achieved by using the @code{-shared} binder
25086 option.
25087
25088 @quotation
25089
25090 @example
25091 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
25092 @end example
25093 @end quotation
25094
25095 @node Building DLLs with gnatdll,Ada DLLs and Finalization,Building DLLs with GNAT,Mixed-Language Programming on Windows
25096 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnatdll}@anchor{200}@anchor{gnat_ugn/platform_specific_information id25}@anchor{201}
25097 @subsubsection Building DLLs with gnatdll
25098
25099
25100 @geindex DLLs
25101 @geindex building
25102
25103 Note that it is preferred to use GNAT Project files
25104 (@ref{1e6,,Building DLLs with GNAT Project files}) or the built-in GNAT
25105 DLL support (@ref{1fe,,Building DLLs with GNAT}) or to build DLLs.
25106
25107 This section explains how to build DLLs containing Ada code using
25108 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
25109 remainder of this section.
25110
25111 The steps required to build an Ada DLL that is to be used by Ada as well as
25112 non-Ada applications are as follows:
25113
25114
25115 @itemize *
25116
25117 @item
25118 You need to mark each Ada entity exported by the DLL with a @code{C} or
25119 @code{Stdcall} calling convention to avoid any Ada name mangling for the
25120 entities exported by the DLL
25121 (see @ref{202,,Exporting Ada Entities}). You can
25122 skip this step if you plan to use the Ada DLL only from Ada applications.
25123
25124 @item
25125 Your Ada code must export an initialization routine which calls the routine
25126 @code{adainit} generated by @code{gnatbind} to perform the elaboration of
25127 the Ada code in the DLL (@ref{203,,Ada DLLs and Elaboration}). The initialization
25128 routine exported by the Ada DLL must be invoked by the clients of the DLL
25129 to initialize the DLL.
25130
25131 @item
25132 When useful, the DLL should also export a finalization routine which calls
25133 routine @code{adafinal} generated by @code{gnatbind} to perform the
25134 finalization of the Ada code in the DLL (@ref{204,,Ada DLLs and Finalization}).
25135 The finalization routine exported by the Ada DLL must be invoked by the
25136 clients of the DLL when the DLL services are no further needed.
25137
25138 @item
25139 You must provide a spec for the services exported by the Ada DLL in each
25140 of the programming languages to which you plan to make the DLL available.
25141
25142 @item
25143 You must provide a definition file listing the exported entities
25144 (@ref{1f3,,The Definition File}).
25145
25146 @item
25147 Finally you must use @code{gnatdll} to produce the DLL and the import
25148 library (@ref{1fb,,Using gnatdll}).
25149 @end itemize
25150
25151 Note that a relocatable DLL stripped using the @code{strip}
25152 binutils tool will not be relocatable anymore. To build a DLL without
25153 debug information pass @code{-largs -s} to @code{gnatdll}. This
25154 restriction does not apply to a DLL built using a Library Project.
25155 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
25156 chapter of the @emph{GPRbuild User's Guide}.
25157
25158 @c Limitations_When_Using_Ada_DLLs_from Ada:
25159
25160 @menu
25161 * Limitations When Using Ada DLLs from Ada::
25162 * Exporting Ada Entities::
25163 * Ada DLLs and Elaboration::
25164
25165 @end menu
25166
25167 @node Limitations When Using Ada DLLs from Ada,Exporting Ada Entities,,Building DLLs with gnatdll
25168 @anchor{gnat_ugn/platform_specific_information limitations-when-using-ada-dlls-from-ada}@anchor{205}
25169 @subsubsection Limitations When Using Ada DLLs from Ada
25170
25171
25172 When using Ada DLLs from Ada applications there is a limitation users
25173 should be aware of. Because on Windows the GNAT run-time is not in a DLL of
25174 its own, each Ada DLL includes a part of the GNAT run-time. Specifically,
25175 each Ada DLL includes the services of the GNAT run-time that are necessary
25176 to the Ada code inside the DLL. As a result, when an Ada program uses an
25177 Ada DLL there are two independent GNAT run-times: one in the Ada DLL and
25178 one in the main program.
25179
25180 It is therefore not possible to exchange GNAT run-time objects between the
25181 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
25182 handles (e.g., @code{Text_IO.File_Type}), tasks types, protected objects
25183 types, etc.
25184
25185 It is completely safe to exchange plain elementary, array or record types,
25186 Windows object handles, etc.
25187
25188 @node Exporting Ada Entities,Ada DLLs and Elaboration,Limitations When Using Ada DLLs from Ada,Building DLLs with gnatdll
25189 @anchor{gnat_ugn/platform_specific_information exporting-ada-entities}@anchor{202}@anchor{gnat_ugn/platform_specific_information id26}@anchor{206}
25190 @subsubsection Exporting Ada Entities
25191
25192
25193 @geindex Export table
25194
25195 Building a DLL is a way to encapsulate a set of services usable from any
25196 application. As a result, the Ada entities exported by a DLL should be
25197 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
25198 any Ada name mangling. As an example here is an Ada package
25199 @code{API}, spec and body, exporting two procedures, a function, and a
25200 variable:
25201
25202 @quotation
25203
25204 @example
25205 with Interfaces.C; use Interfaces;
25206 package API is
25207 Count : C.int := 0;
25208 function Factorial (Val : C.int) return C.int;
25209
25210 procedure Initialize_API;
25211 procedure Finalize_API;
25212 -- Initialization & Finalization routines. More in the next section.
25213 private
25214 pragma Export (C, Initialize_API);
25215 pragma Export (C, Finalize_API);
25216 pragma Export (C, Count);
25217 pragma Export (C, Factorial);
25218 end API;
25219 @end example
25220
25221 @example
25222 package body API is
25223 function Factorial (Val : C.int) return C.int is
25224 Fact : C.int := 1;
25225 begin
25226 Count := Count + 1;
25227 for K in 1 .. Val loop
25228 Fact := Fact * K;
25229 end loop;
25230 return Fact;
25231 end Factorial;
25232
25233 procedure Initialize_API is
25234 procedure Adainit;
25235 pragma Import (C, Adainit);
25236 begin
25237 Adainit;
25238 end Initialize_API;
25239
25240 procedure Finalize_API is
25241 procedure Adafinal;
25242 pragma Import (C, Adafinal);
25243 begin
25244 Adafinal;
25245 end Finalize_API;
25246 end API;
25247 @end example
25248 @end quotation
25249
25250 If the Ada DLL you are building will only be used by Ada applications
25251 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
25252 convention. As an example, the previous package could be written as
25253 follows:
25254
25255 @quotation
25256
25257 @example
25258 package API is
25259 Count : Integer := 0;
25260 function Factorial (Val : Integer) return Integer;
25261
25262 procedure Initialize_API;
25263 procedure Finalize_API;
25264 -- Initialization and Finalization routines.
25265 end API;
25266 @end example
25267
25268 @example
25269 package body API is
25270 function Factorial (Val : Integer) return Integer is
25271 Fact : Integer := 1;
25272 begin
25273 Count := Count + 1;
25274 for K in 1 .. Val loop
25275 Fact := Fact * K;
25276 end loop;
25277 return Fact;
25278 end Factorial;
25279
25280 ...
25281 -- The remainder of this package body is unchanged.
25282 end API;
25283 @end example
25284 @end quotation
25285
25286 Note that if you do not export the Ada entities with a @code{C} or
25287 @code{Stdcall} convention you will have to provide the mangled Ada names
25288 in the definition file of the Ada DLL
25289 (@ref{207,,Creating the Definition File}).
25290
25291 @node Ada DLLs and Elaboration,,Exporting Ada Entities,Building DLLs with gnatdll
25292 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-elaboration}@anchor{203}@anchor{gnat_ugn/platform_specific_information id27}@anchor{208}
25293 @subsubsection Ada DLLs and Elaboration
25294
25295
25296 @geindex DLLs and elaboration
25297
25298 The DLL that you are building contains your Ada code as well as all the
25299 routines in the Ada library that are needed by it. The first thing a
25300 user of your DLL must do is elaborate the Ada code
25301 (@ref{f,,Elaboration Order Handling in GNAT}).
25302
25303 To achieve this you must export an initialization routine
25304 (@code{Initialize_API} in the previous example), which must be invoked
25305 before using any of the DLL services. This elaboration routine must call
25306 the Ada elaboration routine @code{adainit} generated by the GNAT binder
25307 (@ref{b4,,Binding with Non-Ada Main Programs}). See the body of
25308 @code{Initialize_Api} for an example. Note that the GNAT binder is
25309 automatically invoked during the DLL build process by the @code{gnatdll}
25310 tool (@ref{1fb,,Using gnatdll}).
25311
25312 When a DLL is loaded, Windows systematically invokes a routine called
25313 @code{DllMain}. It would therefore be possible to call @code{adainit}
25314 directly from @code{DllMain} without having to provide an explicit
25315 initialization routine. Unfortunately, it is not possible to call
25316 @code{adainit} from the @code{DllMain} if your program has library level
25317 tasks because access to the @code{DllMain} entry point is serialized by
25318 the system (that is, only a single thread can execute 'through' it at a
25319 time), which means that the GNAT run-time will deadlock waiting for the
25320 newly created task to complete its initialization.
25321
25322 @node Ada DLLs and Finalization,Creating a Spec for Ada DLLs,Building DLLs with gnatdll,Mixed-Language Programming on Windows
25323 @anchor{gnat_ugn/platform_specific_information id28}@anchor{209}@anchor{gnat_ugn/platform_specific_information ada-dlls-and-finalization}@anchor{204}
25324 @subsubsection Ada DLLs and Finalization
25325
25326
25327 @geindex DLLs and finalization
25328
25329 When the services of an Ada DLL are no longer needed, the client code should
25330 invoke the DLL finalization routine, if available. The DLL finalization
25331 routine is in charge of releasing all resources acquired by the DLL. In the
25332 case of the Ada code contained in the DLL, this is achieved by calling
25333 routine @code{adafinal} generated by the GNAT binder
25334 (@ref{b4,,Binding with Non-Ada Main Programs}).
25335 See the body of @code{Finalize_Api} for an
25336 example. As already pointed out the GNAT binder is automatically invoked
25337 during the DLL build process by the @code{gnatdll} tool
25338 (@ref{1fb,,Using gnatdll}).
25339
25340 @node Creating a Spec for Ada DLLs,GNAT and Windows Resources,Ada DLLs and Finalization,Mixed-Language Programming on Windows
25341 @anchor{gnat_ugn/platform_specific_information id29}@anchor{20a}@anchor{gnat_ugn/platform_specific_information creating-a-spec-for-ada-dlls}@anchor{20b}
25342 @subsubsection Creating a Spec for Ada DLLs
25343
25344
25345 To use the services exported by the Ada DLL from another programming
25346 language (e.g., C), you have to translate the specs of the exported Ada
25347 entities in that language. For instance in the case of @code{API.dll},
25348 the corresponding C header file could look like:
25349
25350 @quotation
25351
25352 @example
25353 extern int *_imp__count;
25354 #define count (*_imp__count)
25355 int factorial (int);
25356 @end example
25357 @end quotation
25358
25359 It is important to understand that when building an Ada DLL to be used by
25360 other Ada applications, you need two different specs for the packages
25361 contained in the DLL: one for building the DLL and the other for using
25362 the DLL. This is because the @code{DLL} calling convention is needed to
25363 use a variable defined in a DLL, but when building the DLL, the variable
25364 must have either the @code{Ada} or @code{C} calling convention. As an
25365 example consider a DLL comprising the following package @code{API}:
25366
25367 @quotation
25368
25369 @example
25370 package API is
25371 Count : Integer := 0;
25372 ...
25373 -- Remainder of the package omitted.
25374 end API;
25375 @end example
25376 @end quotation
25377
25378 After producing a DLL containing package @code{API}, the spec that
25379 must be used to import @code{API.Count} from Ada code outside of the
25380 DLL is:
25381
25382 @quotation
25383
25384 @example
25385 package API is
25386 Count : Integer;
25387 pragma Import (DLL, Count);
25388 end API;
25389 @end example
25390 @end quotation
25391
25392 @menu
25393 * Creating the Definition File::
25394 * Using gnatdll::
25395
25396 @end menu
25397
25398 @node Creating the Definition File,Using gnatdll,,Creating a Spec for Ada DLLs
25399 @anchor{gnat_ugn/platform_specific_information creating-the-definition-file}@anchor{207}@anchor{gnat_ugn/platform_specific_information id30}@anchor{20c}
25400 @subsubsection Creating the Definition File
25401
25402
25403 The definition file is the last file needed to build the DLL. It lists
25404 the exported symbols. As an example, the definition file for a DLL
25405 containing only package @code{API} (where all the entities are exported
25406 with a @code{C} calling convention) is:
25407
25408 @quotation
25409
25410 @example
25411 EXPORTS
25412 count
25413 factorial
25414 finalize_api
25415 initialize_api
25416 @end example
25417 @end quotation
25418
25419 If the @code{C} calling convention is missing from package @code{API},
25420 then the definition file contains the mangled Ada names of the above
25421 entities, which in this case are:
25422
25423 @quotation
25424
25425 @example
25426 EXPORTS
25427 api__count
25428 api__factorial
25429 api__finalize_api
25430 api__initialize_api
25431 @end example
25432 @end quotation
25433
25434 @node Using gnatdll,,Creating the Definition File,Creating a Spec for Ada DLLs
25435 @anchor{gnat_ugn/platform_specific_information using-gnatdll}@anchor{1fb}@anchor{gnat_ugn/platform_specific_information id31}@anchor{20d}
25436 @subsubsection Using @code{gnatdll}
25437
25438
25439 @geindex gnatdll
25440
25441 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
25442 and non-Ada sources that make up your DLL have been compiled.
25443 @code{gnatdll} is actually in charge of two distinct tasks: build the
25444 static import library for the DLL and the actual DLL. The form of the
25445 @code{gnatdll} command is
25446
25447 @quotation
25448
25449 @example
25450 $ gnatdll [ switches ] list-of-files [ -largs opts ]
25451 @end example
25452 @end quotation
25453
25454 where @code{list-of-files} is a list of ALI and object files. The object
25455 file list must be the exact list of objects corresponding to the non-Ada
25456 sources whose services are to be included in the DLL. The ALI file list
25457 must be the exact list of ALI files for the corresponding Ada sources
25458 whose services are to be included in the DLL. If @code{list-of-files} is
25459 missing, only the static import library is generated.
25460
25461 You may specify any of the following switches to @code{gnatdll}:
25462
25463 @quotation
25464
25465 @geindex -a (gnatdll)
25466 @end quotation
25467
25468
25469 @table @asis
25470
25471 @item @code{-a[@emph{address}]}
25472
25473 Build a non-relocatable DLL at @code{address}. If @code{address} is not
25474 specified the default address @code{0x11000000} will be used. By default,
25475 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
25476 advise the reader to build relocatable DLL.
25477
25478 @geindex -b (gnatdll)
25479
25480 @item @code{-b @emph{address}}
25481
25482 Set the relocatable DLL base address. By default the address is
25483 @code{0x11000000}.
25484
25485 @geindex -bargs (gnatdll)
25486
25487 @item @code{-bargs @emph{opts}}
25488
25489 Binder options. Pass @code{opts} to the binder.
25490
25491 @geindex -d (gnatdll)
25492
25493 @item @code{-d @emph{dllfile}}
25494
25495 @code{dllfile} is the name of the DLL. This switch must be present for
25496 @code{gnatdll} to do anything. The name of the generated import library is
25497 obtained algorithmically from @code{dllfile} as shown in the following
25498 example: if @code{dllfile} is @code{xyz.dll}, the import library name is
25499 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
25500 by option @code{-e}) is obtained algorithmically from @code{dllfile}
25501 as shown in the following example:
25502 if @code{dllfile} is @code{xyz.dll}, the definition
25503 file used is @code{xyz.def}.
25504
25505 @geindex -e (gnatdll)
25506
25507 @item @code{-e @emph{deffile}}
25508
25509 @code{deffile} is the name of the definition file.
25510
25511 @geindex -g (gnatdll)
25512
25513 @item @code{-g}
25514
25515 Generate debugging information. This information is stored in the object
25516 file and copied from there to the final DLL file by the linker,
25517 where it can be read by the debugger. You must use the
25518 @code{-g} switch if you plan on using the debugger or the symbolic
25519 stack traceback.
25520
25521 @geindex -h (gnatdll)
25522
25523 @item @code{-h}
25524
25525 Help mode. Displays @code{gnatdll} switch usage information.
25526
25527 @geindex -I (gnatdll)
25528
25529 @item @code{-I@emph{dir}}
25530
25531 Direct @code{gnatdll} to search the @code{dir} directory for source and
25532 object files needed to build the DLL.
25533 (@ref{89,,Search Paths and the Run-Time Library (RTL)}).
25534
25535 @geindex -k (gnatdll)
25536
25537 @item @code{-k}
25538
25539 Removes the @code{@@@emph{nn}} suffix from the import library's exported
25540 names, but keeps them for the link names. You must specify this
25541 option if you want to use a @code{Stdcall} function in a DLL for which
25542 the @code{@@@emph{nn}} suffix has been removed. This is the case for most
25543 of the Windows NT DLL for example. This option has no effect when
25544 @code{-n} option is specified.
25545
25546 @geindex -l (gnatdll)
25547
25548 @item @code{-l @emph{file}}
25549
25550 The list of ALI and object files used to build the DLL are listed in
25551 @code{file}, instead of being given in the command line. Each line in
25552 @code{file} contains the name of an ALI or object file.
25553
25554 @geindex -n (gnatdll)
25555
25556 @item @code{-n}
25557
25558 No Import. Do not create the import library.
25559
25560 @geindex -q (gnatdll)
25561
25562 @item @code{-q}
25563
25564 Quiet mode. Do not display unnecessary messages.
25565
25566 @geindex -v (gnatdll)
25567
25568 @item @code{-v}
25569
25570 Verbose mode. Display extra information.
25571
25572 @geindex -largs (gnatdll)
25573
25574 @item @code{-largs @emph{opts}}
25575
25576 Linker options. Pass @code{opts} to the linker.
25577 @end table
25578
25579 @subsubheading @code{gnatdll} Example
25580
25581
25582 As an example the command to build a relocatable DLL from @code{api.adb}
25583 once @code{api.adb} has been compiled and @code{api.def} created is
25584
25585 @quotation
25586
25587 @example
25588 $ gnatdll -d api.dll api.ali
25589 @end example
25590 @end quotation
25591
25592 The above command creates two files: @code{libapi.dll.a} (the import
25593 library) and @code{api.dll} (the actual DLL). If you want to create
25594 only the DLL, just type:
25595
25596 @quotation
25597
25598 @example
25599 $ gnatdll -d api.dll -n api.ali
25600 @end example
25601 @end quotation
25602
25603 Alternatively if you want to create just the import library, type:
25604
25605 @quotation
25606
25607 @example
25608 $ gnatdll -d api.dll
25609 @end example
25610 @end quotation
25611
25612 @subsubheading @code{gnatdll} behind the Scenes
25613
25614
25615 This section details the steps involved in creating a DLL. @code{gnatdll}
25616 does these steps for you. Unless you are interested in understanding what
25617 goes on behind the scenes, you should skip this section.
25618
25619 We use the previous example of a DLL containing the Ada package @code{API},
25620 to illustrate the steps necessary to build a DLL. The starting point is a
25621 set of objects that will make up the DLL and the corresponding ALI
25622 files. In the case of this example this means that @code{api.o} and
25623 @code{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
25624 the following:
25625
25626
25627 @itemize *
25628
25629 @item
25630 @code{gnatdll} builds the base file (@code{api.base}). A base file gives
25631 the information necessary to generate relocation information for the
25632 DLL.
25633
25634 @example
25635 $ gnatbind -n api
25636 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
25637 @end example
25638
25639 In addition to the base file, the @code{gnatlink} command generates an
25640 output file @code{api.jnk} which can be discarded. The @code{-mdll} switch
25641 asks @code{gnatlink} to generate the routines @code{DllMain} and
25642 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
25643 is loaded into memory.
25644
25645 @item
25646 @code{gnatdll} uses @code{dlltool} (see @ref{20e,,Using dlltool}) to build the
25647 export table (@code{api.exp}). The export table contains the relocation
25648 information in a form which can be used during the final link to ensure
25649 that the Windows loader is able to place the DLL anywhere in memory.
25650
25651 @example
25652 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25653 --output-exp api.exp
25654 @end example
25655
25656 @item
25657 @code{gnatdll} builds the base file using the new export table. Note that
25658 @code{gnatbind} must be called once again since the binder generated file
25659 has been deleted during the previous call to @code{gnatlink}.
25660
25661 @example
25662 $ gnatbind -n api
25663 $ gnatlink api -o api.jnk api.exp -mdll
25664 -Wl,--base-file,api.base
25665 @end example
25666
25667 @item
25668 @code{gnatdll} builds the new export table using the new base file and
25669 generates the DLL import library @code{libAPI.dll.a}.
25670
25671 @example
25672 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25673 --output-exp api.exp --output-lib libAPI.a
25674 @end example
25675
25676 @item
25677 Finally @code{gnatdll} builds the relocatable DLL using the final export
25678 table.
25679
25680 @example
25681 $ gnatbind -n api
25682 $ gnatlink api api.exp -o api.dll -mdll
25683 @end example
25684 @end itemize
25685 @anchor{gnat_ugn/platform_specific_information using-dlltool}@anchor{20e}
25686 @subsubheading Using @code{dlltool}
25687
25688
25689 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
25690 DLLs and static import libraries. This section summarizes the most
25691 common @code{dlltool} switches. The form of the @code{dlltool} command
25692 is
25693
25694 @quotation
25695
25696 @example
25697 $ dlltool [`switches`]
25698 @end example
25699 @end quotation
25700
25701 @code{dlltool} switches include:
25702
25703 @geindex --base-file (dlltool)
25704
25705
25706 @table @asis
25707
25708 @item @code{--base-file @emph{basefile}}
25709
25710 Read the base file @code{basefile} generated by the linker. This switch
25711 is used to create a relocatable DLL.
25712 @end table
25713
25714 @geindex --def (dlltool)
25715
25716
25717 @table @asis
25718
25719 @item @code{--def @emph{deffile}}
25720
25721 Read the definition file.
25722 @end table
25723
25724 @geindex --dllname (dlltool)
25725
25726
25727 @table @asis
25728
25729 @item @code{--dllname @emph{name}}
25730
25731 Gives the name of the DLL. This switch is used to embed the name of the
25732 DLL in the static import library generated by @code{dlltool} with switch
25733 @code{--output-lib}.
25734 @end table
25735
25736 @geindex -k (dlltool)
25737
25738
25739 @table @asis
25740
25741 @item @code{-k}
25742
25743 Kill @code{@@@emph{nn}} from exported names
25744 (@ref{1e7,,Windows Calling Conventions}
25745 for a discussion about @code{Stdcall}-style symbols.
25746 @end table
25747
25748 @geindex --help (dlltool)
25749
25750
25751 @table @asis
25752
25753 @item @code{--help}
25754
25755 Prints the @code{dlltool} switches with a concise description.
25756 @end table
25757
25758 @geindex --output-exp (dlltool)
25759
25760
25761 @table @asis
25762
25763 @item @code{--output-exp @emph{exportfile}}
25764
25765 Generate an export file @code{exportfile}. The export file contains the
25766 export table (list of symbols in the DLL) and is used to create the DLL.
25767 @end table
25768
25769 @geindex --output-lib (dlltool)
25770
25771
25772 @table @asis
25773
25774 @item @code{--output-lib @emph{libfile}}
25775
25776 Generate a static import library @code{libfile}.
25777 @end table
25778
25779 @geindex -v (dlltool)
25780
25781
25782 @table @asis
25783
25784 @item @code{-v}
25785
25786 Verbose mode.
25787 @end table
25788
25789 @geindex --as (dlltool)
25790
25791
25792 @table @asis
25793
25794 @item @code{--as @emph{assembler-name}}
25795
25796 Use @code{assembler-name} as the assembler. The default is @code{as}.
25797 @end table
25798
25799 @node GNAT and Windows Resources,Using GNAT DLLs from Microsoft Visual Studio Applications,Creating a Spec for Ada DLLs,Mixed-Language Programming on Windows
25800 @anchor{gnat_ugn/platform_specific_information gnat-and-windows-resources}@anchor{20f}@anchor{gnat_ugn/platform_specific_information id32}@anchor{210}
25801 @subsubsection GNAT and Windows Resources
25802
25803
25804 @geindex Resources
25805 @geindex windows
25806
25807 Resources are an easy way to add Windows specific objects to your
25808 application. The objects that can be added as resources include:
25809
25810
25811 @itemize *
25812
25813 @item
25814 menus
25815
25816 @item
25817 accelerators
25818
25819 @item
25820 dialog boxes
25821
25822 @item
25823 string tables
25824
25825 @item
25826 bitmaps
25827
25828 @item
25829 cursors
25830
25831 @item
25832 icons
25833
25834 @item
25835 fonts
25836
25837 @item
25838 version information
25839 @end itemize
25840
25841 For example, a version information resource can be defined as follow and
25842 embedded into an executable or DLL:
25843
25844 A version information resource can be used to embed information into an
25845 executable or a DLL. These information can be viewed using the file properties
25846 from the Windows Explorer. Here is an example of a version information
25847 resource:
25848
25849 @quotation
25850
25851 @example
25852 1 VERSIONINFO
25853 FILEVERSION 1,0,0,0
25854 PRODUCTVERSION 1,0,0,0
25855 BEGIN
25856 BLOCK "StringFileInfo"
25857 BEGIN
25858 BLOCK "080904E4"
25859 BEGIN
25860 VALUE "CompanyName", "My Company Name"
25861 VALUE "FileDescription", "My application"
25862 VALUE "FileVersion", "1.0"
25863 VALUE "InternalName", "my_app"
25864 VALUE "LegalCopyright", "My Name"
25865 VALUE "OriginalFilename", "my_app.exe"
25866 VALUE "ProductName", "My App"
25867 VALUE "ProductVersion", "1.0"
25868 END
25869 END
25870
25871 BLOCK "VarFileInfo"
25872 BEGIN
25873 VALUE "Translation", 0x809, 1252
25874 END
25875 END
25876 @end example
25877 @end quotation
25878
25879 The value @code{0809} (langID) is for the U.K English language and
25880 @code{04E4} (charsetID), which is equal to @code{1252} decimal, for
25881 multilingual.
25882
25883 This section explains how to build, compile and use resources. Note that this
25884 section does not cover all resource objects, for a complete description see
25885 the corresponding Microsoft documentation.
25886
25887 @menu
25888 * Building Resources::
25889 * Compiling Resources::
25890 * Using Resources::
25891
25892 @end menu
25893
25894 @node Building Resources,Compiling Resources,,GNAT and Windows Resources
25895 @anchor{gnat_ugn/platform_specific_information building-resources}@anchor{211}@anchor{gnat_ugn/platform_specific_information id33}@anchor{212}
25896 @subsubsection Building Resources
25897
25898
25899 @geindex Resources
25900 @geindex building
25901
25902 A resource file is an ASCII file. By convention resource files have an
25903 @code{.rc} extension.
25904 The easiest way to build a resource file is to use Microsoft tools
25905 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
25906 @code{dlgedit.exe} to build dialogs.
25907 It is always possible to build an @code{.rc} file yourself by writing a
25908 resource script.
25909
25910 It is not our objective to explain how to write a resource file. A
25911 complete description of the resource script language can be found in the
25912 Microsoft documentation.
25913
25914 @node Compiling Resources,Using Resources,Building Resources,GNAT and Windows Resources
25915 @anchor{gnat_ugn/platform_specific_information compiling-resources}@anchor{213}@anchor{gnat_ugn/platform_specific_information id34}@anchor{214}
25916 @subsubsection Compiling Resources
25917
25918
25919 @geindex rc
25920
25921 @geindex windres
25922
25923 @geindex Resources
25924 @geindex compiling
25925
25926 This section describes how to build a GNAT-compatible (COFF) object file
25927 containing the resources. This is done using the Resource Compiler
25928 @code{windres} as follows:
25929
25930 @quotation
25931
25932 @example
25933 $ windres -i myres.rc -o myres.o
25934 @end example
25935 @end quotation
25936
25937 By default @code{windres} will run @code{gcc} to preprocess the @code{.rc}
25938 file. You can specify an alternate preprocessor (usually named
25939 @code{cpp.exe}) using the @code{windres} @code{--preprocessor}
25940 parameter. A list of all possible options may be obtained by entering
25941 the command @code{windres} @code{--help}.
25942
25943 It is also possible to use the Microsoft resource compiler @code{rc.exe}
25944 to produce a @code{.res} file (binary resource file). See the
25945 corresponding Microsoft documentation for further details. In this case
25946 you need to use @code{windres} to translate the @code{.res} file to a
25947 GNAT-compatible object file as follows:
25948
25949 @quotation
25950
25951 @example
25952 $ windres -i myres.res -o myres.o
25953 @end example
25954 @end quotation
25955
25956 @node Using Resources,,Compiling Resources,GNAT and Windows Resources
25957 @anchor{gnat_ugn/platform_specific_information using-resources}@anchor{215}@anchor{gnat_ugn/platform_specific_information id35}@anchor{216}
25958 @subsubsection Using Resources
25959
25960
25961 @geindex Resources
25962 @geindex using
25963
25964 To include the resource file in your program just add the
25965 GNAT-compatible object file for the resource(s) to the linker
25966 arguments. With @code{gnatmake} this is done by using the @code{-largs}
25967 option:
25968
25969 @quotation
25970
25971 @example
25972 $ gnatmake myprog -largs myres.o
25973 @end example
25974 @end quotation
25975
25976 @node Using GNAT DLLs from Microsoft Visual Studio Applications,Debugging a DLL,GNAT and Windows Resources,Mixed-Language Programming on Windows
25977 @anchor{gnat_ugn/platform_specific_information using-gnat-dll-from-msvs}@anchor{217}@anchor{gnat_ugn/platform_specific_information using-gnat-dlls-from-microsoft-visual-studio-applications}@anchor{218}
25978 @subsubsection Using GNAT DLLs from Microsoft Visual Studio Applications
25979
25980
25981 @geindex Microsoft Visual Studio
25982 @geindex use with GNAT DLLs
25983
25984 This section describes a common case of mixed GNAT/Microsoft Visual Studio
25985 application development, where the main program is developed using MSVS, and
25986 is linked with a DLL developed using GNAT. Such a mixed application should
25987 be developed following the general guidelines outlined above; below is the
25988 cookbook-style sequence of steps to follow:
25989
25990
25991 @enumerate
25992
25993 @item
25994 First develop and build the GNAT shared library using a library project
25995 (let's assume the project is @code{mylib.gpr}, producing the library @code{libmylib.dll}):
25996 @end enumerate
25997
25998 @quotation
25999
26000 @example
26001 $ gprbuild -p mylib.gpr
26002 @end example
26003 @end quotation
26004
26005
26006 @enumerate 2
26007
26008 @item
26009 Produce a .def file for the symbols you need to interface with, either by
26010 hand or automatically with possibly some manual adjustments
26011 (see @ref{1f9,,Creating Definition File Automatically}):
26012 @end enumerate
26013
26014 @quotation
26015
26016 @example
26017 $ dlltool libmylib.dll -z libmylib.def --export-all-symbols
26018 @end example
26019 @end quotation
26020
26021
26022 @enumerate 3
26023
26024 @item
26025 Make sure that MSVS command-line tools are accessible on the path.
26026
26027 @item
26028 Create the Microsoft-style import library (see @ref{1fc,,MSVS-Style Import Library}):
26029 @end enumerate
26030
26031 @quotation
26032
26033 @example
26034 $ lib -machine:IX86 -def:libmylib.def -out:libmylib.lib
26035 @end example
26036 @end quotation
26037
26038 If you are using a 64-bit toolchain, the above becomes...
26039
26040 @quotation
26041
26042 @example
26043 $ lib -machine:X64 -def:libmylib.def -out:libmylib.lib
26044 @end example
26045 @end quotation
26046
26047
26048 @enumerate 5
26049
26050 @item
26051 Build the C main
26052 @end enumerate
26053
26054 @quotation
26055
26056 @example
26057 $ cl /O2 /MD main.c libmylib.lib
26058 @end example
26059 @end quotation
26060
26061
26062 @enumerate 6
26063
26064 @item
26065 Before running the executable, make sure you have set the PATH to the DLL,
26066 or copy the DLL into into the directory containing the .exe.
26067 @end enumerate
26068
26069 @node Debugging a DLL,Setting Stack Size from gnatlink,Using GNAT DLLs from Microsoft Visual Studio Applications,Mixed-Language Programming on Windows
26070 @anchor{gnat_ugn/platform_specific_information id36}@anchor{219}@anchor{gnat_ugn/platform_specific_information debugging-a-dll}@anchor{21a}
26071 @subsubsection Debugging a DLL
26072
26073
26074 @geindex DLL debugging
26075
26076 Debugging a DLL is similar to debugging a standard program. But
26077 we have to deal with two different executable parts: the DLL and the
26078 program that uses it. We have the following four possibilities:
26079
26080
26081 @itemize *
26082
26083 @item
26084 The program and the DLL are built with GCC/GNAT.
26085
26086 @item
26087 The program is built with foreign tools and the DLL is built with
26088 GCC/GNAT.
26089
26090 @item
26091 The program is built with GCC/GNAT and the DLL is built with
26092 foreign tools.
26093 @end itemize
26094
26095 In this section we address only cases one and two above.
26096 There is no point in trying to debug
26097 a DLL with GNU/GDB, if there is no GDB-compatible debugging
26098 information in it. To do so you must use a debugger compatible with the
26099 tools suite used to build the DLL.
26100
26101 @menu
26102 * Program and DLL Both Built with GCC/GNAT::
26103 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
26104
26105 @end menu
26106
26107 @node Program and DLL Both Built with GCC/GNAT,Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Debugging a DLL
26108 @anchor{gnat_ugn/platform_specific_information id37}@anchor{21b}@anchor{gnat_ugn/platform_specific_information program-and-dll-both-built-with-gcc-gnat}@anchor{21c}
26109 @subsubsection Program and DLL Both Built with GCC/GNAT
26110
26111
26112 This is the simplest case. Both the DLL and the program have @code{GDB}
26113 compatible debugging information. It is then possible to break anywhere in
26114 the process. Let's suppose here that the main procedure is named
26115 @code{ada_main} and that in the DLL there is an entry point named
26116 @code{ada_dll}.
26117
26118 The DLL (@ref{1f2,,Introduction to Dynamic Link Libraries (DLLs)}) and
26119 program must have been built with the debugging information (see GNAT -g
26120 switch). Here are the step-by-step instructions for debugging it:
26121
26122
26123 @itemize *
26124
26125 @item
26126 Launch @code{GDB} on the main program.
26127
26128 @example
26129 $ gdb -nw ada_main
26130 @end example
26131
26132 @item
26133 Start the program and stop at the beginning of the main procedure
26134
26135 @example
26136 (gdb) start
26137 @end example
26138
26139 This step is required to be able to set a breakpoint inside the DLL. As long
26140 as the program is not run, the DLL is not loaded. This has the
26141 consequence that the DLL debugging information is also not loaded, so it is not
26142 possible to set a breakpoint in the DLL.
26143
26144 @item
26145 Set a breakpoint inside the DLL
26146
26147 @example
26148 (gdb) break ada_dll
26149 (gdb) cont
26150 @end example
26151 @end itemize
26152
26153 At this stage a breakpoint is set inside the DLL. From there on
26154 you can use the standard approach to debug the whole program
26155 (@ref{24,,Running and Debugging Ada Programs}).
26156
26157 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Program and DLL Both Built with GCC/GNAT,Debugging a DLL
26158 @anchor{gnat_ugn/platform_specific_information program-built-with-foreign-tools-and-dll-built-with-gcc-gnat}@anchor{21d}@anchor{gnat_ugn/platform_specific_information id38}@anchor{21e}
26159 @subsubsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
26160
26161
26162 In this case things are slightly more complex because it is not possible to
26163 start the main program and then break at the beginning to load the DLL and the
26164 associated DLL debugging information. It is not possible to break at the
26165 beginning of the program because there is no @code{GDB} debugging information,
26166 and therefore there is no direct way of getting initial control. This
26167 section addresses this issue by describing some methods that can be used
26168 to break somewhere in the DLL to debug it.
26169
26170 First suppose that the main procedure is named @code{main} (this is for
26171 example some C code built with Microsoft Visual C) and that there is a
26172 DLL named @code{test.dll} containing an Ada entry point named
26173 @code{ada_dll}.
26174
26175 The DLL (see @ref{1f2,,Introduction to Dynamic Link Libraries (DLLs)}) must have
26176 been built with debugging information (see the GNAT @code{-g} option).
26177
26178 @subsubheading Debugging the DLL Directly
26179
26180
26181
26182 @itemize *
26183
26184 @item
26185 Find out the executable starting address
26186
26187 @example
26188 $ objdump --file-header main.exe
26189 @end example
26190
26191 The starting address is reported on the last line. For example:
26192
26193 @example
26194 main.exe: file format pei-i386
26195 architecture: i386, flags 0x0000010a:
26196 EXEC_P, HAS_DEBUG, D_PAGED
26197 start address 0x00401010
26198 @end example
26199
26200 @item
26201 Launch the debugger on the executable.
26202
26203 @example
26204 $ gdb main.exe
26205 @end example
26206
26207 @item
26208 Set a breakpoint at the starting address, and launch the program.
26209
26210 @example
26211 $ (gdb) break *0x00401010
26212 $ (gdb) run
26213 @end example
26214
26215 The program will stop at the given address.
26216
26217 @item
26218 Set a breakpoint on a DLL subroutine.
26219
26220 @example
26221 (gdb) break ada_dll.adb:45
26222 @end example
26223
26224 Or if you want to break using a symbol on the DLL, you need first to
26225 select the Ada language (language used by the DLL).
26226
26227 @example
26228 (gdb) set language ada
26229 (gdb) break ada_dll
26230 @end example
26231
26232 @item
26233 Continue the program.
26234
26235 @example
26236 (gdb) cont
26237 @end example
26238
26239 This will run the program until it reaches the breakpoint that has been
26240 set. From that point you can use the standard way to debug a program
26241 as described in (@ref{24,,Running and Debugging Ada Programs}).
26242 @end itemize
26243
26244 It is also possible to debug the DLL by attaching to a running process.
26245
26246 @subsubheading Attaching to a Running Process
26247
26248
26249 @geindex DLL debugging
26250 @geindex attach to process
26251
26252 With @code{GDB} it is always possible to debug a running process by
26253 attaching to it. It is possible to debug a DLL this way. The limitation
26254 of this approach is that the DLL must run long enough to perform the
26255 attach operation. It may be useful for instance to insert a time wasting
26256 loop in the code of the DLL to meet this criterion.
26257
26258
26259 @itemize *
26260
26261 @item
26262 Launch the main program @code{main.exe}.
26263
26264 @example
26265 $ main
26266 @end example
26267
26268 @item
26269 Use the Windows @emph{Task Manager} to find the process ID. Let's say
26270 that the process PID for @code{main.exe} is 208.
26271
26272 @item
26273 Launch gdb.
26274
26275 @example
26276 $ gdb
26277 @end example
26278
26279 @item
26280 Attach to the running process to be debugged.
26281
26282 @example
26283 (gdb) attach 208
26284 @end example
26285
26286 @item
26287 Load the process debugging information.
26288
26289 @example
26290 (gdb) symbol-file main.exe
26291 @end example
26292
26293 @item
26294 Break somewhere in the DLL.
26295
26296 @example
26297 (gdb) break ada_dll
26298 @end example
26299
26300 @item
26301 Continue process execution.
26302
26303 @example
26304 (gdb) cont
26305 @end example
26306 @end itemize
26307
26308 This last step will resume the process execution, and stop at
26309 the breakpoint we have set. From there you can use the standard
26310 approach to debug a program as described in
26311 @ref{24,,Running and Debugging Ada Programs}.
26312
26313 @node Setting Stack Size from gnatlink,Setting Heap Size from gnatlink,Debugging a DLL,Mixed-Language Programming on Windows
26314 @anchor{gnat_ugn/platform_specific_information setting-stack-size-from-gnatlink}@anchor{136}@anchor{gnat_ugn/platform_specific_information id39}@anchor{21f}
26315 @subsubsection Setting Stack Size from @code{gnatlink}
26316
26317
26318 It is possible to specify the program stack size at link time. On modern
26319 versions of Windows, starting with XP, this is mostly useful to set the size of
26320 the main stack (environment task). The other task stacks are set with pragma
26321 Storage_Size or with the @emph{gnatbind -d} command.
26322
26323 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
26324 reserve size of individual tasks, the link-time stack size applies to all
26325 tasks, and pragma Storage_Size has no effect.
26326 In particular, Stack Overflow checks are made against this
26327 link-time specified size.
26328
26329 This setting can be done with @code{gnatlink} using either of the following:
26330
26331
26332 @itemize *
26333
26334 @item
26335 @code{-Xlinker} linker option
26336
26337 @example
26338 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
26339 @end example
26340
26341 This sets the stack reserve size to 0x10000 bytes and the stack commit
26342 size to 0x1000 bytes.
26343
26344 @item
26345 @code{-Wl} linker option
26346
26347 @example
26348 $ gnatlink hello -Wl,--stack=0x1000000
26349 @end example
26350
26351 This sets the stack reserve size to 0x1000000 bytes. Note that with
26352 @code{-Wl} option it is not possible to set the stack commit size
26353 because the comma is a separator for this option.
26354 @end itemize
26355
26356 @node Setting Heap Size from gnatlink,,Setting Stack Size from gnatlink,Mixed-Language Programming on Windows
26357 @anchor{gnat_ugn/platform_specific_information setting-heap-size-from-gnatlink}@anchor{137}@anchor{gnat_ugn/platform_specific_information id40}@anchor{220}
26358 @subsubsection Setting Heap Size from @code{gnatlink}
26359
26360
26361 Under Windows systems, it is possible to specify the program heap size from
26362 @code{gnatlink} using either of the following:
26363
26364
26365 @itemize *
26366
26367 @item
26368 @code{-Xlinker} linker option
26369
26370 @example
26371 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
26372 @end example
26373
26374 This sets the heap reserve size to 0x10000 bytes and the heap commit
26375 size to 0x1000 bytes.
26376
26377 @item
26378 @code{-Wl} linker option
26379
26380 @example
26381 $ gnatlink hello -Wl,--heap=0x1000000
26382 @end example
26383
26384 This sets the heap reserve size to 0x1000000 bytes. Note that with
26385 @code{-Wl} option it is not possible to set the heap commit size
26386 because the comma is a separator for this option.
26387 @end itemize
26388
26389 @node Windows Specific Add-Ons,,Mixed-Language Programming on Windows,Microsoft Windows Topics
26390 @anchor{gnat_ugn/platform_specific_information windows-specific-add-ons}@anchor{221}@anchor{gnat_ugn/platform_specific_information win32-specific-addons}@anchor{222}
26391 @subsection Windows Specific Add-Ons
26392
26393
26394 This section describes the Windows specific add-ons.
26395
26396 @menu
26397 * Win32Ada::
26398 * wPOSIX::
26399
26400 @end menu
26401
26402 @node Win32Ada,wPOSIX,,Windows Specific Add-Ons
26403 @anchor{gnat_ugn/platform_specific_information win32ada}@anchor{223}@anchor{gnat_ugn/platform_specific_information id41}@anchor{224}
26404 @subsubsection Win32Ada
26405
26406
26407 Win32Ada is a binding for the Microsoft Win32 API. This binding can be
26408 easily installed from the provided installer. To use the Win32Ada
26409 binding you need to use a project file, and adding a single with_clause
26410 will give you full access to the Win32Ada binding sources and ensure
26411 that the proper libraries are passed to the linker.
26412
26413 @quotation
26414
26415 @example
26416 with "win32ada";
26417 project P is
26418 for Sources use ...;
26419 end P;
26420 @end example
26421 @end quotation
26422
26423 To build the application you just need to call gprbuild for the
26424 application's project, here p.gpr:
26425
26426 @quotation
26427
26428 @example
26429 gprbuild p.gpr
26430 @end example
26431 @end quotation
26432
26433 @node wPOSIX,,Win32Ada,Windows Specific Add-Ons
26434 @anchor{gnat_ugn/platform_specific_information id42}@anchor{225}@anchor{gnat_ugn/platform_specific_information wposix}@anchor{226}
26435 @subsubsection wPOSIX
26436
26437
26438 wPOSIX is a minimal POSIX binding whose goal is to help with building
26439 cross-platforms applications. This binding is not complete though, as
26440 the Win32 API does not provide the necessary support for all POSIX APIs.
26441
26442 To use the wPOSIX binding you need to use a project file, and adding
26443 a single with_clause will give you full access to the wPOSIX binding
26444 sources and ensure that the proper libraries are passed to the linker.
26445
26446 @quotation
26447
26448 @example
26449 with "wposix";
26450 project P is
26451 for Sources use ...;
26452 end P;
26453 @end example
26454 @end quotation
26455
26456 To build the application you just need to call gprbuild for the
26457 application's project, here p.gpr:
26458
26459 @quotation
26460
26461 @example
26462 gprbuild p.gpr
26463 @end example
26464 @end quotation
26465
26466 @node Mac OS Topics,,Microsoft Windows Topics,Platform-Specific Information
26467 @anchor{gnat_ugn/platform_specific_information mac-os-topics}@anchor{2d}@anchor{gnat_ugn/platform_specific_information id43}@anchor{227}
26468 @section Mac OS Topics
26469
26470
26471 @geindex OS X
26472
26473 This section describes topics that are specific to Apple's OS X
26474 platform.
26475
26476 @menu
26477 * Codesigning the Debugger::
26478
26479 @end menu
26480
26481 @node Codesigning the Debugger,,,Mac OS Topics
26482 @anchor{gnat_ugn/platform_specific_information codesigning-the-debugger}@anchor{228}
26483 @subsection Codesigning the Debugger
26484
26485
26486 The Darwin Kernel requires the debugger to have special permissions
26487 before it is allowed to control other processes. These permissions
26488 are granted by codesigning the GDB executable. Without these
26489 permissions, the debugger will report error messages such as:
26490
26491 @example
26492 Starting program: /x/y/foo
26493 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
26494 (please check gdb is codesigned - see taskgated(8))
26495 @end example
26496
26497 Codesigning requires a certificate. The following procedure explains
26498 how to create one:
26499
26500
26501 @itemize *
26502
26503 @item
26504 Start the Keychain Access application (in
26505 /Applications/Utilities/Keychain Access.app)
26506
26507 @item
26508 Select the Keychain Access -> Certificate Assistant ->
26509 Create a Certificate... menu
26510
26511 @item
26512 Then:
26513
26514
26515 @itemize *
26516
26517 @item
26518 Choose a name for the new certificate (this procedure will use
26519 "gdb-cert" as an example)
26520
26521 @item
26522 Set "Identity Type" to "Self Signed Root"
26523
26524 @item
26525 Set "Certificate Type" to "Code Signing"
26526
26527 @item
26528 Activate the "Let me override defaults" option
26529 @end itemize
26530
26531 @item
26532 Click several times on "Continue" until the "Specify a Location
26533 For The Certificate" screen appears, then set "Keychain" to "System"
26534
26535 @item
26536 Click on "Continue" until the certificate is created
26537
26538 @item
26539 Finally, in the view, double-click on the new certificate,
26540 and set "When using this certificate" to "Always Trust"
26541
26542 @item
26543 Exit the Keychain Access application and restart the computer
26544 (this is unfortunately required)
26545 @end itemize
26546
26547 Once a certificate has been created, the debugger can be codesigned
26548 as follow. In a Terminal, run the following command:
26549
26550 @quotation
26551
26552 @example
26553 $ codesign -f -s "gdb-cert" <gnat_install_prefix>/bin/gdb
26554 @end example
26555 @end quotation
26556
26557 where "gdb-cert" should be replaced by the actual certificate
26558 name chosen above, and <gnat_install_prefix> should be replaced by
26559 the location where you installed GNAT. Also, be sure that users are
26560 in the Unix group @code{_developer}.
26561
26562 @node Example of Binder Output File,Elaboration Order Handling in GNAT,Platform-Specific Information,Top
26563 @anchor{gnat_ugn/example_of_binder_output example-of-binder-output-file}@anchor{e}@anchor{gnat_ugn/example_of_binder_output doc}@anchor{229}@anchor{gnat_ugn/example_of_binder_output id1}@anchor{22a}
26564 @chapter Example of Binder Output File
26565
26566
26567 @geindex Binder output (example)
26568
26569 This Appendix displays the source code for the output file
26570 generated by @emph{gnatbind} for a simple 'Hello World' program.
26571 Comments have been added for clarification purposes.
26572
26573 @example
26574 -- The package is called Ada_Main unless this name is actually used
26575 -- as a unit name in the partition, in which case some other unique
26576 -- name is used.
26577
26578 pragma Ada_95;
26579 with System;
26580 package ada_main is
26581 pragma Warnings (Off);
26582
26583 -- The main program saves the parameters (argument count,
26584 -- argument values, environment pointer) in global variables
26585 -- for later access by other units including
26586 -- Ada.Command_Line.
26587
26588 gnat_argc : Integer;
26589 gnat_argv : System.Address;
26590 gnat_envp : System.Address;
26591
26592 -- The actual variables are stored in a library routine. This
26593 -- is useful for some shared library situations, where there
26594 -- are problems if variables are not in the library.
26595
26596 pragma Import (C, gnat_argc);
26597 pragma Import (C, gnat_argv);
26598 pragma Import (C, gnat_envp);
26599
26600 -- The exit status is similarly an external location
26601
26602 gnat_exit_status : Integer;
26603 pragma Import (C, gnat_exit_status);
26604
26605 GNAT_Version : constant String :=
26606 "GNAT Version: Pro 7.4.0w (20141119-49)" & ASCII.NUL;
26607 pragma Export (C, GNAT_Version, "__gnat_version");
26608
26609 Ada_Main_Program_Name : constant String := "_ada_hello" & ASCII.NUL;
26610 pragma Export (C, Ada_Main_Program_Name, "__gnat_ada_main_program_name");
26611
26612 -- This is the generated adainit routine that performs
26613 -- initialization at the start of execution. In the case
26614 -- where Ada is the main program, this main program makes
26615 -- a call to adainit at program startup.
26616
26617 procedure adainit;
26618 pragma Export (C, adainit, "adainit");
26619
26620 -- This is the generated adafinal routine that performs
26621 -- finalization at the end of execution. In the case where
26622 -- Ada is the main program, this main program makes a call
26623 -- to adafinal at program termination.
26624
26625 procedure adafinal;
26626 pragma Export (C, adafinal, "adafinal");
26627
26628 -- This routine is called at the start of execution. It is
26629 -- a dummy routine that is used by the debugger to breakpoint
26630 -- at the start of execution.
26631
26632 -- This is the actual generated main program (it would be
26633 -- suppressed if the no main program switch were used). As
26634 -- required by standard system conventions, this program has
26635 -- the external name main.
26636
26637 function main
26638 (argc : Integer;
26639 argv : System.Address;
26640 envp : System.Address)
26641 return Integer;
26642 pragma Export (C, main, "main");
26643
26644 -- The following set of constants give the version
26645 -- identification values for every unit in the bound
26646 -- partition. This identification is computed from all
26647 -- dependent semantic units, and corresponds to the
26648 -- string that would be returned by use of the
26649 -- Body_Version or Version attributes.
26650
26651 -- The following Export pragmas export the version numbers
26652 -- with symbolic names ending in B (for body) or S
26653 -- (for spec) so that they can be located in a link. The
26654 -- information provided here is sufficient to track down
26655 -- the exact versions of units used in a given build.
26656
26657 type Version_32 is mod 2 ** 32;
26658 u00001 : constant Version_32 := 16#8ad6e54a#;
26659 pragma Export (C, u00001, "helloB");
26660 u00002 : constant Version_32 := 16#fbff4c67#;
26661 pragma Export (C, u00002, "system__standard_libraryB");
26662 u00003 : constant Version_32 := 16#1ec6fd90#;
26663 pragma Export (C, u00003, "system__standard_libraryS");
26664 u00004 : constant Version_32 := 16#3ffc8e18#;
26665 pragma Export (C, u00004, "adaS");
26666 u00005 : constant Version_32 := 16#28f088c2#;
26667 pragma Export (C, u00005, "ada__text_ioB");
26668 u00006 : constant Version_32 := 16#f372c8ac#;
26669 pragma Export (C, u00006, "ada__text_ioS");
26670 u00007 : constant Version_32 := 16#2c143749#;
26671 pragma Export (C, u00007, "ada__exceptionsB");
26672 u00008 : constant Version_32 := 16#f4f0cce8#;
26673 pragma Export (C, u00008, "ada__exceptionsS");
26674 u00009 : constant Version_32 := 16#a46739c0#;
26675 pragma Export (C, u00009, "ada__exceptions__last_chance_handlerB");
26676 u00010 : constant Version_32 := 16#3aac8c92#;
26677 pragma Export (C, u00010, "ada__exceptions__last_chance_handlerS");
26678 u00011 : constant Version_32 := 16#1d274481#;
26679 pragma Export (C, u00011, "systemS");
26680 u00012 : constant Version_32 := 16#a207fefe#;
26681 pragma Export (C, u00012, "system__soft_linksB");
26682 u00013 : constant Version_32 := 16#467d9556#;
26683 pragma Export (C, u00013, "system__soft_linksS");
26684 u00014 : constant Version_32 := 16#b01dad17#;
26685 pragma Export (C, u00014, "system__parametersB");
26686 u00015 : constant Version_32 := 16#630d49fe#;
26687 pragma Export (C, u00015, "system__parametersS");
26688 u00016 : constant Version_32 := 16#b19b6653#;
26689 pragma Export (C, u00016, "system__secondary_stackB");
26690 u00017 : constant Version_32 := 16#b6468be8#;
26691 pragma Export (C, u00017, "system__secondary_stackS");
26692 u00018 : constant Version_32 := 16#39a03df9#;
26693 pragma Export (C, u00018, "system__storage_elementsB");
26694 u00019 : constant Version_32 := 16#30e40e85#;
26695 pragma Export (C, u00019, "system__storage_elementsS");
26696 u00020 : constant Version_32 := 16#41837d1e#;
26697 pragma Export (C, u00020, "system__stack_checkingB");
26698 u00021 : constant Version_32 := 16#93982f69#;
26699 pragma Export (C, u00021, "system__stack_checkingS");
26700 u00022 : constant Version_32 := 16#393398c1#;
26701 pragma Export (C, u00022, "system__exception_tableB");
26702 u00023 : constant Version_32 := 16#b33e2294#;
26703 pragma Export (C, u00023, "system__exception_tableS");
26704 u00024 : constant Version_32 := 16#ce4af020#;
26705 pragma Export (C, u00024, "system__exceptionsB");
26706 u00025 : constant Version_32 := 16#75442977#;
26707 pragma Export (C, u00025, "system__exceptionsS");
26708 u00026 : constant Version_32 := 16#37d758f1#;
26709 pragma Export (C, u00026, "system__exceptions__machineS");
26710 u00027 : constant Version_32 := 16#b895431d#;
26711 pragma Export (C, u00027, "system__exceptions_debugB");
26712 u00028 : constant Version_32 := 16#aec55d3f#;
26713 pragma Export (C, u00028, "system__exceptions_debugS");
26714 u00029 : constant Version_32 := 16#570325c8#;
26715 pragma Export (C, u00029, "system__img_intB");
26716 u00030 : constant Version_32 := 16#1ffca443#;
26717 pragma Export (C, u00030, "system__img_intS");
26718 u00031 : constant Version_32 := 16#b98c3e16#;
26719 pragma Export (C, u00031, "system__tracebackB");
26720 u00032 : constant Version_32 := 16#831a9d5a#;
26721 pragma Export (C, u00032, "system__tracebackS");
26722 u00033 : constant Version_32 := 16#9ed49525#;
26723 pragma Export (C, u00033, "system__traceback_entriesB");
26724 u00034 : constant Version_32 := 16#1d7cb2f1#;
26725 pragma Export (C, u00034, "system__traceback_entriesS");
26726 u00035 : constant Version_32 := 16#8c33a517#;
26727 pragma Export (C, u00035, "system__wch_conB");
26728 u00036 : constant Version_32 := 16#065a6653#;
26729 pragma Export (C, u00036, "system__wch_conS");
26730 u00037 : constant Version_32 := 16#9721e840#;
26731 pragma Export (C, u00037, "system__wch_stwB");
26732 u00038 : constant Version_32 := 16#2b4b4a52#;
26733 pragma Export (C, u00038, "system__wch_stwS");
26734 u00039 : constant Version_32 := 16#92b797cb#;
26735 pragma Export (C, u00039, "system__wch_cnvB");
26736 u00040 : constant Version_32 := 16#09eddca0#;
26737 pragma Export (C, u00040, "system__wch_cnvS");
26738 u00041 : constant Version_32 := 16#6033a23f#;
26739 pragma Export (C, u00041, "interfacesS");
26740 u00042 : constant Version_32 := 16#ece6fdb6#;
26741 pragma Export (C, u00042, "system__wch_jisB");
26742 u00043 : constant Version_32 := 16#899dc581#;
26743 pragma Export (C, u00043, "system__wch_jisS");
26744 u00044 : constant Version_32 := 16#10558b11#;
26745 pragma Export (C, u00044, "ada__streamsB");
26746 u00045 : constant Version_32 := 16#2e6701ab#;
26747 pragma Export (C, u00045, "ada__streamsS");
26748 u00046 : constant Version_32 := 16#db5c917c#;
26749 pragma Export (C, u00046, "ada__io_exceptionsS");
26750 u00047 : constant Version_32 := 16#12c8cd7d#;
26751 pragma Export (C, u00047, "ada__tagsB");
26752 u00048 : constant Version_32 := 16#ce72c228#;
26753 pragma Export (C, u00048, "ada__tagsS");
26754 u00049 : constant Version_32 := 16#c3335bfd#;
26755 pragma Export (C, u00049, "system__htableB");
26756 u00050 : constant Version_32 := 16#99e5f76b#;
26757 pragma Export (C, u00050, "system__htableS");
26758 u00051 : constant Version_32 := 16#089f5cd0#;
26759 pragma Export (C, u00051, "system__string_hashB");
26760 u00052 : constant Version_32 := 16#3bbb9c15#;
26761 pragma Export (C, u00052, "system__string_hashS");
26762 u00053 : constant Version_32 := 16#807fe041#;
26763 pragma Export (C, u00053, "system__unsigned_typesS");
26764 u00054 : constant Version_32 := 16#d27be59e#;
26765 pragma Export (C, u00054, "system__val_lluB");
26766 u00055 : constant Version_32 := 16#fa8db733#;
26767 pragma Export (C, u00055, "system__val_lluS");
26768 u00056 : constant Version_32 := 16#27b600b2#;
26769 pragma Export (C, u00056, "system__val_utilB");
26770 u00057 : constant Version_32 := 16#b187f27f#;
26771 pragma Export (C, u00057, "system__val_utilS");
26772 u00058 : constant Version_32 := 16#d1060688#;
26773 pragma Export (C, u00058, "system__case_utilB");
26774 u00059 : constant Version_32 := 16#392e2d56#;
26775 pragma Export (C, u00059, "system__case_utilS");
26776 u00060 : constant Version_32 := 16#84a27f0d#;
26777 pragma Export (C, u00060, "interfaces__c_streamsB");
26778 u00061 : constant Version_32 := 16#8bb5f2c0#;
26779 pragma Export (C, u00061, "interfaces__c_streamsS");
26780 u00062 : constant Version_32 := 16#6db6928f#;
26781 pragma Export (C, u00062, "system__crtlS");
26782 u00063 : constant Version_32 := 16#4e6a342b#;
26783 pragma Export (C, u00063, "system__file_ioB");
26784 u00064 : constant Version_32 := 16#ba56a5e4#;
26785 pragma Export (C, u00064, "system__file_ioS");
26786 u00065 : constant Version_32 := 16#b7ab275c#;
26787 pragma Export (C, u00065, "ada__finalizationB");
26788 u00066 : constant Version_32 := 16#19f764ca#;
26789 pragma Export (C, u00066, "ada__finalizationS");
26790 u00067 : constant Version_32 := 16#95817ed8#;
26791 pragma Export (C, u00067, "system__finalization_rootB");
26792 u00068 : constant Version_32 := 16#52d53711#;
26793 pragma Export (C, u00068, "system__finalization_rootS");
26794 u00069 : constant Version_32 := 16#769e25e6#;
26795 pragma Export (C, u00069, "interfaces__cB");
26796 u00070 : constant Version_32 := 16#4a38bedb#;
26797 pragma Export (C, u00070, "interfaces__cS");
26798 u00071 : constant Version_32 := 16#07e6ee66#;
26799 pragma Export (C, u00071, "system__os_libB");
26800 u00072 : constant Version_32 := 16#d7b69782#;
26801 pragma Export (C, u00072, "system__os_libS");
26802 u00073 : constant Version_32 := 16#1a817b8e#;
26803 pragma Export (C, u00073, "system__stringsB");
26804 u00074 : constant Version_32 := 16#639855e7#;
26805 pragma Export (C, u00074, "system__stringsS");
26806 u00075 : constant Version_32 := 16#e0b8de29#;
26807 pragma Export (C, u00075, "system__file_control_blockS");
26808 u00076 : constant Version_32 := 16#b5b2aca1#;
26809 pragma Export (C, u00076, "system__finalization_mastersB");
26810 u00077 : constant Version_32 := 16#69316dc1#;
26811 pragma Export (C, u00077, "system__finalization_mastersS");
26812 u00078 : constant Version_32 := 16#57a37a42#;
26813 pragma Export (C, u00078, "system__address_imageB");
26814 u00079 : constant Version_32 := 16#bccbd9bb#;
26815 pragma Export (C, u00079, "system__address_imageS");
26816 u00080 : constant Version_32 := 16#7268f812#;
26817 pragma Export (C, u00080, "system__img_boolB");
26818 u00081 : constant Version_32 := 16#e8fe356a#;
26819 pragma Export (C, u00081, "system__img_boolS");
26820 u00082 : constant Version_32 := 16#d7aac20c#;
26821 pragma Export (C, u00082, "system__ioB");
26822 u00083 : constant Version_32 := 16#8365b3ce#;
26823 pragma Export (C, u00083, "system__ioS");
26824 u00084 : constant Version_32 := 16#6d4d969a#;
26825 pragma Export (C, u00084, "system__storage_poolsB");
26826 u00085 : constant Version_32 := 16#e87cc305#;
26827 pragma Export (C, u00085, "system__storage_poolsS");
26828 u00086 : constant Version_32 := 16#e34550ca#;
26829 pragma Export (C, u00086, "system__pool_globalB");
26830 u00087 : constant Version_32 := 16#c88d2d16#;
26831 pragma Export (C, u00087, "system__pool_globalS");
26832 u00088 : constant Version_32 := 16#9d39c675#;
26833 pragma Export (C, u00088, "system__memoryB");
26834 u00089 : constant Version_32 := 16#445a22b5#;
26835 pragma Export (C, u00089, "system__memoryS");
26836 u00090 : constant Version_32 := 16#6a859064#;
26837 pragma Export (C, u00090, "system__storage_pools__subpoolsB");
26838 u00091 : constant Version_32 := 16#e3b008dc#;
26839 pragma Export (C, u00091, "system__storage_pools__subpoolsS");
26840 u00092 : constant Version_32 := 16#63f11652#;
26841 pragma Export (C, u00092, "system__storage_pools__subpools__finalizationB");
26842 u00093 : constant Version_32 := 16#fe2f4b3a#;
26843 pragma Export (C, u00093, "system__storage_pools__subpools__finalizationS");
26844
26845 -- BEGIN ELABORATION ORDER
26846 -- ada%s
26847 -- interfaces%s
26848 -- system%s
26849 -- system.case_util%s
26850 -- system.case_util%b
26851 -- system.htable%s
26852 -- system.img_bool%s
26853 -- system.img_bool%b
26854 -- system.img_int%s
26855 -- system.img_int%b
26856 -- system.io%s
26857 -- system.io%b
26858 -- system.parameters%s
26859 -- system.parameters%b
26860 -- system.crtl%s
26861 -- interfaces.c_streams%s
26862 -- interfaces.c_streams%b
26863 -- system.standard_library%s
26864 -- system.exceptions_debug%s
26865 -- system.exceptions_debug%b
26866 -- system.storage_elements%s
26867 -- system.storage_elements%b
26868 -- system.stack_checking%s
26869 -- system.stack_checking%b
26870 -- system.string_hash%s
26871 -- system.string_hash%b
26872 -- system.htable%b
26873 -- system.strings%s
26874 -- system.strings%b
26875 -- system.os_lib%s
26876 -- system.traceback_entries%s
26877 -- system.traceback_entries%b
26878 -- ada.exceptions%s
26879 -- system.soft_links%s
26880 -- system.unsigned_types%s
26881 -- system.val_llu%s
26882 -- system.val_util%s
26883 -- system.val_util%b
26884 -- system.val_llu%b
26885 -- system.wch_con%s
26886 -- system.wch_con%b
26887 -- system.wch_cnv%s
26888 -- system.wch_jis%s
26889 -- system.wch_jis%b
26890 -- system.wch_cnv%b
26891 -- system.wch_stw%s
26892 -- system.wch_stw%b
26893 -- ada.exceptions.last_chance_handler%s
26894 -- ada.exceptions.last_chance_handler%b
26895 -- system.address_image%s
26896 -- system.exception_table%s
26897 -- system.exception_table%b
26898 -- ada.io_exceptions%s
26899 -- ada.tags%s
26900 -- ada.streams%s
26901 -- ada.streams%b
26902 -- interfaces.c%s
26903 -- system.exceptions%s
26904 -- system.exceptions%b
26905 -- system.exceptions.machine%s
26906 -- system.finalization_root%s
26907 -- system.finalization_root%b
26908 -- ada.finalization%s
26909 -- ada.finalization%b
26910 -- system.storage_pools%s
26911 -- system.storage_pools%b
26912 -- system.finalization_masters%s
26913 -- system.storage_pools.subpools%s
26914 -- system.storage_pools.subpools.finalization%s
26915 -- system.storage_pools.subpools.finalization%b
26916 -- system.memory%s
26917 -- system.memory%b
26918 -- system.standard_library%b
26919 -- system.pool_global%s
26920 -- system.pool_global%b
26921 -- system.file_control_block%s
26922 -- system.file_io%s
26923 -- system.secondary_stack%s
26924 -- system.file_io%b
26925 -- system.storage_pools.subpools%b
26926 -- system.finalization_masters%b
26927 -- interfaces.c%b
26928 -- ada.tags%b
26929 -- system.soft_links%b
26930 -- system.os_lib%b
26931 -- system.secondary_stack%b
26932 -- system.address_image%b
26933 -- system.traceback%s
26934 -- ada.exceptions%b
26935 -- system.traceback%b
26936 -- ada.text_io%s
26937 -- ada.text_io%b
26938 -- hello%b
26939 -- END ELABORATION ORDER
26940
26941 end ada_main;
26942 @end example
26943
26944 @example
26945 pragma Ada_95;
26946 -- The following source file name pragmas allow the generated file
26947 -- names to be unique for different main programs. They are needed
26948 -- since the package name will always be Ada_Main.
26949
26950 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26951 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26952
26953 pragma Suppress (Overflow_Check);
26954 with Ada.Exceptions;
26955
26956 -- Generated package body for Ada_Main starts here
26957
26958 package body ada_main is
26959 pragma Warnings (Off);
26960
26961 -- These values are reference counter associated to units which have
26962 -- been elaborated. It is also used to avoid elaborating the
26963 -- same unit twice.
26964
26965 E72 : Short_Integer; pragma Import (Ada, E72, "system__os_lib_E");
26966 E13 : Short_Integer; pragma Import (Ada, E13, "system__soft_links_E");
26967 E23 : Short_Integer; pragma Import (Ada, E23, "system__exception_table_E");
26968 E46 : Short_Integer; pragma Import (Ada, E46, "ada__io_exceptions_E");
26969 E48 : Short_Integer; pragma Import (Ada, E48, "ada__tags_E");
26970 E45 : Short_Integer; pragma Import (Ada, E45, "ada__streams_E");
26971 E70 : Short_Integer; pragma Import (Ada, E70, "interfaces__c_E");
26972 E25 : Short_Integer; pragma Import (Ada, E25, "system__exceptions_E");
26973 E68 : Short_Integer; pragma Import (Ada, E68, "system__finalization_root_E");
26974 E66 : Short_Integer; pragma Import (Ada, E66, "ada__finalization_E");
26975 E85 : Short_Integer; pragma Import (Ada, E85, "system__storage_pools_E");
26976 E77 : Short_Integer; pragma Import (Ada, E77, "system__finalization_masters_E");
26977 E91 : Short_Integer; pragma Import (Ada, E91, "system__storage_pools__subpools_E");
26978 E87 : Short_Integer; pragma Import (Ada, E87, "system__pool_global_E");
26979 E75 : Short_Integer; pragma Import (Ada, E75, "system__file_control_block_E");
26980 E64 : Short_Integer; pragma Import (Ada, E64, "system__file_io_E");
26981 E17 : Short_Integer; pragma Import (Ada, E17, "system__secondary_stack_E");
26982 E06 : Short_Integer; pragma Import (Ada, E06, "ada__text_io_E");
26983
26984 Local_Priority_Specific_Dispatching : constant String := "";
26985 Local_Interrupt_States : constant String := "";
26986
26987 Is_Elaborated : Boolean := False;
26988
26989 procedure finalize_library is
26990 begin
26991 E06 := E06 - 1;
26992 declare
26993 procedure F1;
26994 pragma Import (Ada, F1, "ada__text_io__finalize_spec");
26995 begin
26996 F1;
26997 end;
26998 E77 := E77 - 1;
26999 E91 := E91 - 1;
27000 declare
27001 procedure F2;
27002 pragma Import (Ada, F2, "system__file_io__finalize_body");
27003 begin
27004 E64 := E64 - 1;
27005 F2;
27006 end;
27007 declare
27008 procedure F3;
27009 pragma Import (Ada, F3, "system__file_control_block__finalize_spec");
27010 begin
27011 E75 := E75 - 1;
27012 F3;
27013 end;
27014 E87 := E87 - 1;
27015 declare
27016 procedure F4;
27017 pragma Import (Ada, F4, "system__pool_global__finalize_spec");
27018 begin
27019 F4;
27020 end;
27021 declare
27022 procedure F5;
27023 pragma Import (Ada, F5, "system__storage_pools__subpools__finalize_spec");
27024 begin
27025 F5;
27026 end;
27027 declare
27028 procedure F6;
27029 pragma Import (Ada, F6, "system__finalization_masters__finalize_spec");
27030 begin
27031 F6;
27032 end;
27033 declare
27034 procedure Reraise_Library_Exception_If_Any;
27035 pragma Import (Ada, Reraise_Library_Exception_If_Any, "__gnat_reraise_library_exception_if_any");
27036 begin
27037 Reraise_Library_Exception_If_Any;
27038 end;
27039 end finalize_library;
27040
27041 -------------
27042 -- adainit --
27043 -------------
27044
27045 procedure adainit is
27046
27047 Main_Priority : Integer;
27048 pragma Import (C, Main_Priority, "__gl_main_priority");
27049 Time_Slice_Value : Integer;
27050 pragma Import (C, Time_Slice_Value, "__gl_time_slice_val");
27051 WC_Encoding : Character;
27052 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
27053 Locking_Policy : Character;
27054 pragma Import (C, Locking_Policy, "__gl_locking_policy");
27055 Queuing_Policy : Character;
27056 pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
27057 Task_Dispatching_Policy : Character;
27058 pragma Import (C, Task_Dispatching_Policy, "__gl_task_dispatching_policy");
27059 Priority_Specific_Dispatching : System.Address;
27060 pragma Import (C, Priority_Specific_Dispatching, "__gl_priority_specific_dispatching");
27061 Num_Specific_Dispatching : Integer;
27062 pragma Import (C, Num_Specific_Dispatching, "__gl_num_specific_dispatching");
27063 Main_CPU : Integer;
27064 pragma Import (C, Main_CPU, "__gl_main_cpu");
27065 Interrupt_States : System.Address;
27066 pragma Import (C, Interrupt_States, "__gl_interrupt_states");
27067 Num_Interrupt_States : Integer;
27068 pragma Import (C, Num_Interrupt_States, "__gl_num_interrupt_states");
27069 Unreserve_All_Interrupts : Integer;
27070 pragma Import (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
27071 Detect_Blocking : Integer;
27072 pragma Import (C, Detect_Blocking, "__gl_detect_blocking");
27073 Default_Stack_Size : Integer;
27074 pragma Import (C, Default_Stack_Size, "__gl_default_stack_size");
27075 Leap_Seconds_Support : Integer;
27076 pragma Import (C, Leap_Seconds_Support, "__gl_leap_seconds_support");
27077
27078 procedure Runtime_Initialize;
27079 pragma Import (C, Runtime_Initialize, "__gnat_runtime_initialize");
27080
27081 Finalize_Library_Objects : No_Param_Proc;
27082 pragma Import (C, Finalize_Library_Objects, "__gnat_finalize_library_objects");
27083
27084 -- Start of processing for adainit
27085
27086 begin
27087
27088 -- Record various information for this partition. The values
27089 -- are derived by the binder from information stored in the ali
27090 -- files by the compiler.
27091
27092 if Is_Elaborated then
27093 return;
27094 end if;
27095 Is_Elaborated := True;
27096 Main_Priority := -1;
27097 Time_Slice_Value := -1;
27098 WC_Encoding := 'b';
27099 Locking_Policy := ' ';
27100 Queuing_Policy := ' ';
27101 Task_Dispatching_Policy := ' ';
27102 Priority_Specific_Dispatching :=
27103 Local_Priority_Specific_Dispatching'Address;
27104 Num_Specific_Dispatching := 0;
27105 Main_CPU := -1;
27106 Interrupt_States := Local_Interrupt_States'Address;
27107 Num_Interrupt_States := 0;
27108 Unreserve_All_Interrupts := 0;
27109 Detect_Blocking := 0;
27110 Default_Stack_Size := -1;
27111 Leap_Seconds_Support := 0;
27112
27113 Runtime_Initialize;
27114
27115 Finalize_Library_Objects := finalize_library'access;
27116
27117 -- Now we have the elaboration calls for all units in the partition.
27118 -- The Elab_Spec and Elab_Body attributes generate references to the
27119 -- implicit elaboration procedures generated by the compiler for
27120 -- each unit that requires elaboration. Increment a counter of
27121 -- reference for each unit.
27122
27123 System.Soft_Links'Elab_Spec;
27124 System.Exception_Table'Elab_Body;
27125 E23 := E23 + 1;
27126 Ada.Io_Exceptions'Elab_Spec;
27127 E46 := E46 + 1;
27128 Ada.Tags'Elab_Spec;
27129 Ada.Streams'Elab_Spec;
27130 E45 := E45 + 1;
27131 Interfaces.C'Elab_Spec;
27132 System.Exceptions'Elab_Spec;
27133 E25 := E25 + 1;
27134 System.Finalization_Root'Elab_Spec;
27135 E68 := E68 + 1;
27136 Ada.Finalization'Elab_Spec;
27137 E66 := E66 + 1;
27138 System.Storage_Pools'Elab_Spec;
27139 E85 := E85 + 1;
27140 System.Finalization_Masters'Elab_Spec;
27141 System.Storage_Pools.Subpools'Elab_Spec;
27142 System.Pool_Global'Elab_Spec;
27143 E87 := E87 + 1;
27144 System.File_Control_Block'Elab_Spec;
27145 E75 := E75 + 1;
27146 System.File_Io'Elab_Body;
27147 E64 := E64 + 1;
27148 E91 := E91 + 1;
27149 System.Finalization_Masters'Elab_Body;
27150 E77 := E77 + 1;
27151 E70 := E70 + 1;
27152 Ada.Tags'Elab_Body;
27153 E48 := E48 + 1;
27154 System.Soft_Links'Elab_Body;
27155 E13 := E13 + 1;
27156 System.Os_Lib'Elab_Body;
27157 E72 := E72 + 1;
27158 System.Secondary_Stack'Elab_Body;
27159 E17 := E17 + 1;
27160 Ada.Text_Io'Elab_Spec;
27161 Ada.Text_Io'Elab_Body;
27162 E06 := E06 + 1;
27163 end adainit;
27164
27165 --------------
27166 -- adafinal --
27167 --------------
27168
27169 procedure adafinal is
27170 procedure s_stalib_adafinal;
27171 pragma Import (C, s_stalib_adafinal, "system__standard_library__adafinal");
27172
27173 procedure Runtime_Finalize;
27174 pragma Import (C, Runtime_Finalize, "__gnat_runtime_finalize");
27175
27176 begin
27177 if not Is_Elaborated then
27178 return;
27179 end if;
27180 Is_Elaborated := False;
27181 Runtime_Finalize;
27182 s_stalib_adafinal;
27183 end adafinal;
27184
27185 -- We get to the main program of the partition by using
27186 -- pragma Import because if we try to with the unit and
27187 -- call it Ada style, then not only do we waste time
27188 -- recompiling it, but also, we don't really know the right
27189 -- switches (e.g.@@: identifier character set) to be used
27190 -- to compile it.
27191
27192 procedure Ada_Main_Program;
27193 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
27194
27195 ----------
27196 -- main --
27197 ----------
27198
27199 -- main is actually a function, as in the ANSI C standard,
27200 -- defined to return the exit status. The three parameters
27201 -- are the argument count, argument values and environment
27202 -- pointer.
27203
27204 function main
27205 (argc : Integer;
27206 argv : System.Address;
27207 envp : System.Address)
27208 return Integer
27209 is
27210 -- The initialize routine performs low level system
27211 -- initialization using a standard library routine which
27212 -- sets up signal handling and performs any other
27213 -- required setup. The routine can be found in file
27214 -- a-init.c.
27215
27216 procedure initialize;
27217 pragma Import (C, initialize, "__gnat_initialize");
27218
27219 -- The finalize routine performs low level system
27220 -- finalization using a standard library routine. The
27221 -- routine is found in file a-final.c and in the standard
27222 -- distribution is a dummy routine that does nothing, so
27223 -- really this is a hook for special user finalization.
27224
27225 procedure finalize;
27226 pragma Import (C, finalize, "__gnat_finalize");
27227
27228 -- The following is to initialize the SEH exceptions
27229
27230 SEH : aliased array (1 .. 2) of Integer;
27231
27232 Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
27233 pragma Volatile (Ensure_Reference);
27234
27235 -- Start of processing for main
27236
27237 begin
27238 -- Save global variables
27239
27240 gnat_argc := argc;
27241 gnat_argv := argv;
27242 gnat_envp := envp;
27243
27244 -- Call low level system initialization
27245
27246 Initialize (SEH'Address);
27247
27248 -- Call our generated Ada initialization routine
27249
27250 adainit;
27251
27252 -- Now we call the main program of the partition
27253
27254 Ada_Main_Program;
27255
27256 -- Perform Ada finalization
27257
27258 adafinal;
27259
27260 -- Perform low level system finalization
27261
27262 Finalize;
27263
27264 -- Return the proper exit status
27265 return (gnat_exit_status);
27266 end;
27267
27268 -- This section is entirely comments, so it has no effect on the
27269 -- compilation of the Ada_Main package. It provides the list of
27270 -- object files and linker options, as well as some standard
27271 -- libraries needed for the link. The gnatlink utility parses
27272 -- this b~hello.adb file to read these comment lines to generate
27273 -- the appropriate command line arguments for the call to the
27274 -- system linker. The BEGIN/END lines are used for sentinels for
27275 -- this parsing operation.
27276
27277 -- The exact file names will of course depend on the environment,
27278 -- host/target and location of files on the host system.
27279
27280 -- BEGIN Object file/option list
27281 -- ./hello.o
27282 -- -L./
27283 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
27284 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
27285 -- END Object file/option list
27286
27287 end ada_main;
27288 @end example
27289
27290 The Ada code in the above example is exactly what is generated by the
27291 binder. We have added comments to more clearly indicate the function
27292 of each part of the generated @code{Ada_Main} package.
27293
27294 The code is standard Ada in all respects, and can be processed by any
27295 tools that handle Ada. In particular, it is possible to use the debugger
27296 in Ada mode to debug the generated @code{Ada_Main} package. For example,
27297 suppose that for reasons that you do not understand, your program is crashing
27298 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
27299 you can place a breakpoint on the call:
27300
27301 @quotation
27302
27303 @example
27304 Ada.Text_Io'Elab_Body;
27305 @end example
27306 @end quotation
27307
27308 and trace the elaboration routine for this package to find out where
27309 the problem might be (more usually of course you would be debugging
27310 elaboration code in your own application).
27311
27312 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
27313
27314 @node Elaboration Order Handling in GNAT,Inline Assembler,Example of Binder Output File,Top
27315 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order-handling-in-gnat}@anchor{f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat doc}@anchor{22b}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id1}@anchor{22c}
27316 @chapter Elaboration Order Handling in GNAT
27317
27318
27319 @geindex Order of elaboration
27320
27321 @geindex Elaboration control
27322
27323 This appendix describes the handling of elaboration code in Ada and GNAT, and
27324 discusses how the order of elaboration of program units can be controlled in
27325 GNAT, either automatically or with explicit programming features.
27326
27327 @menu
27328 * Elaboration Code::
27329 * Elaboration Order::
27330 * Checking the Elaboration Order::
27331 * Controlling the Elaboration Order in Ada::
27332 * Controlling the Elaboration Order in GNAT::
27333 * Mixing Elaboration Models::
27334 * ABE Diagnostics::
27335 * SPARK Diagnostics::
27336 * Elaboration Circularities::
27337 * Resolving Elaboration Circularities::
27338 * Elaboration-related Compiler Switches::
27339 * Summary of Procedures for Elaboration Control::
27340 * Inspecting the Chosen Elaboration Order::
27341
27342 @end menu
27343
27344 @node Elaboration Code,Elaboration Order,,Elaboration Order Handling in GNAT
27345 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-code}@anchor{22d}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id2}@anchor{22e}
27346 @section Elaboration Code
27347
27348
27349 Ada defines the term @emph{execution} as the process by which a construct achieves
27350 its run-time effect. This process is also referred to as @strong{elaboration} for
27351 declarations and @emph{evaluation} for expressions.
27352
27353 The execution model in Ada allows for certain sections of an Ada program to be
27354 executed prior to execution of the program itself, primarily with the intent of
27355 initializing data. These sections are referred to as @strong{elaboration code}.
27356 Elaboration code is executed as follows:
27357
27358
27359 @itemize *
27360
27361 @item
27362 All partitions of an Ada program are executed in parallel with one another,
27363 possibly in a separate address space, and possibly on a separate computer.
27364
27365 @item
27366 The execution of a partition involves running the environment task for that
27367 partition.
27368
27369 @item
27370 The environment task executes all elaboration code (if available) for all
27371 units within that partition. This code is said to be executed at
27372 @strong{elaboration time}.
27373
27374 @item
27375 The environment task executes the Ada program (if available) for that
27376 partition.
27377 @end itemize
27378
27379 In addition to the Ada terminology, this appendix defines the following terms:
27380
27381
27382 @itemize *
27383
27384 @item
27385 @emph{Invocation}
27386
27387 The act of calling a subprogram, instantiating a generic, or activating a
27388 task.
27389
27390 @item
27391 @emph{Scenario}
27392
27393 A construct that is elaborated or invoked by elaboration code is referred to
27394 as an @emph{elaboration scenario} or simply a @strong{scenario}. GNAT recognizes the
27395 following scenarios:
27396
27397
27398 @itemize -
27399
27400 @item
27401 @code{'Access} of entries, operators, and subprograms
27402
27403 @item
27404 Activation of tasks
27405
27406 @item
27407 Calls to entries, operators, and subprograms
27408
27409 @item
27410 Instantiations of generic templates
27411 @end itemize
27412
27413 @item
27414 @emph{Target}
27415
27416 A construct elaborated by a scenario is referred to as @emph{elaboration target}
27417 or simply @strong{target}. GNAT recognizes the following targets:
27418
27419
27420 @itemize -
27421
27422 @item
27423 For @code{'Access} of entries, operators, and subprograms, the target is the
27424 entry, operator, or subprogram being aliased.
27425
27426 @item
27427 For activation of tasks, the target is the task body
27428
27429 @item
27430 For calls to entries, operators, and subprograms, the target is the entry,
27431 operator, or subprogram being invoked.
27432
27433 @item
27434 For instantiations of generic templates, the target is the generic template
27435 being instantiated.
27436 @end itemize
27437 @end itemize
27438
27439 Elaboration code may appear in two distinct contexts:
27440
27441
27442 @itemize *
27443
27444 @item
27445 @emph{Library level}
27446
27447 A scenario appears at the library level when it is encapsulated by a package
27448 [body] compilation unit, ignoring any other package [body] declarations in
27449 between.
27450
27451 @example
27452 with Server;
27453 package Client is
27454 procedure Proc;
27455
27456 package Nested is
27457 Val : ... := Server.Func;
27458 end Nested;
27459 end Client;
27460 @end example
27461
27462 In the example above, the call to @code{Server.Func} is an elaboration scenario
27463 because it appears at the library level of package @code{Client}. Note that the
27464 declaration of package @code{Nested} is ignored according to the definition
27465 given above. As a result, the call to @code{Server.Func} will be invoked when
27466 the spec of unit @code{Client} is elaborated.
27467
27468 @item
27469 @emph{Package body statements}
27470
27471 A scenario appears within the statement sequence of a package body when it is
27472 bounded by the region starting from the @code{begin} keyword of the package body
27473 and ending at the @code{end} keyword of the package body.
27474
27475 @example
27476 package body Client is
27477 procedure Proc is
27478 begin
27479 ...
27480 end Proc;
27481 begin
27482 Proc;
27483 end Client;
27484 @end example
27485
27486 In the example above, the call to @code{Proc} is an elaboration scenario because
27487 it appears within the statement sequence of package body @code{Client}. As a
27488 result, the call to @code{Proc} will be invoked when the body of @code{Client} is
27489 elaborated.
27490 @end itemize
27491
27492 @node Elaboration Order,Checking the Elaboration Order,Elaboration Code,Elaboration Order Handling in GNAT
27493 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order}@anchor{22f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id3}@anchor{230}
27494 @section Elaboration Order
27495
27496
27497 The sequence by which the elaboration code of all units within a partition is
27498 executed is referred to as @strong{elaboration order}.
27499
27500 Within a single unit, elaboration code is executed in sequential order.
27501
27502 @quotation
27503
27504 @example
27505 package body Client is
27506 Result : ... := Server.Func;
27507
27508 procedure Proc is
27509 package Inst is new Server.Gen;
27510 begin
27511 Inst.Eval (Result);
27512 end Proc;
27513 begin
27514 Proc;
27515 end Client;
27516 @end example
27517 @end quotation
27518
27519 In the example above, the elaboration order within package body @code{Client} is
27520 as follows:
27521
27522
27523 @enumerate
27524
27525 @item
27526 The object declaration of @code{Result} is elaborated.
27527
27528
27529 @itemize *
27530
27531 @item
27532 Function @code{Server.Func} is invoked.
27533 @end itemize
27534
27535 @item
27536 The subprogram body of @code{Proc} is elaborated.
27537
27538 @item
27539 Procedure @code{Proc} is invoked.
27540
27541
27542 @itemize *
27543
27544 @item
27545 Generic unit @code{Server.Gen} is instantiated as @code{Inst}.
27546
27547 @item
27548 Instance @code{Inst} is elaborated.
27549
27550 @item
27551 Procedure @code{Inst.Eval} is invoked.
27552 @end itemize
27553 @end enumerate
27554
27555 The elaboration order of all units within a partition depends on the following
27556 factors:
27557
27558
27559 @itemize *
27560
27561 @item
27562 @emph{with}ed units
27563
27564 @item
27565 parent units
27566
27567 @item
27568 purity of units
27569
27570 @item
27571 preelaborability of units
27572
27573 @item
27574 presence of elaboration-control pragmas
27575
27576 @item
27577 invocations performed in elaboration code
27578 @end itemize
27579
27580 A program may have several elaboration orders depending on its structure.
27581
27582 @quotation
27583
27584 @example
27585 package Server is
27586 function Func (Index : Integer) return Integer;
27587 end Server;
27588 @end example
27589
27590 @example
27591 package body Server is
27592 Results : array (1 .. 5) of Integer := (1, 2, 3, 4, 5);
27593
27594 function Func (Index : Integer) return Integer is
27595 begin
27596 return Results (Index);
27597 end Func;
27598 end Server;
27599 @end example
27600
27601 @example
27602 with Server;
27603 package Client is
27604 Val : constant Integer := Server.Func (3);
27605 end Client;
27606 @end example
27607
27608 @example
27609 with Client;
27610 procedure Main is begin null; end Main;
27611 @end example
27612 @end quotation
27613
27614 The following elaboration order exhibits a fundamental problem referred to as
27615 @emph{access-before-elaboration} or simply @strong{ABE}.
27616
27617 @quotation
27618
27619 @example
27620 spec of Server
27621 spec of Client
27622 body of Server
27623 body of Main
27624 @end example
27625 @end quotation
27626
27627 The elaboration of @code{Server}'s spec materializes function @code{Func}, making it
27628 callable. The elaboration of @code{Client}'s spec elaborates the declaration of
27629 @code{Val}. This invokes function @code{Server.Func}, however the body of
27630 @code{Server.Func} has not been elaborated yet because @code{Server}'s body comes
27631 after @code{Client}'s spec in the elaboration order. As a result, the value of
27632 constant @code{Val} is now undefined.
27633
27634 Without any guarantees from the language, an undetected ABE problem may hinder
27635 proper initialization of data, which in turn may lead to undefined behavior at
27636 run time. To prevent such ABE problems, Ada employs dynamic checks in the same
27637 vein as index or null exclusion checks. A failed ABE check raises exception
27638 @code{Program_Error}.
27639
27640 The following elaboration order avoids the ABE problem and the program can be
27641 successfully elaborated.
27642
27643 @quotation
27644
27645 @example
27646 spec of Server
27647 body of Server
27648 spec of Client
27649 body of Main
27650 @end example
27651 @end quotation
27652
27653 Ada states that a total elaboration order must exist, but it does not define
27654 what this order is. A compiler is thus tasked with choosing a suitable
27655 elaboration order which satisfies the dependencies imposed by @emph{with} clauses,
27656 unit categorization, elaboration-control pragmas, and invocations performed in
27657 elaboration code. Ideally an order that avoids ABE problems should be chosen,
27658 however a compiler may not always find such an order due to complications with
27659 respect to control and data flow.
27660
27661 @node Checking the Elaboration Order,Controlling the Elaboration Order in Ada,Elaboration Order,Elaboration Order Handling in GNAT
27662 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id4}@anchor{231}@anchor{gnat_ugn/elaboration_order_handling_in_gnat checking-the-elaboration-order}@anchor{232}
27663 @section Checking the Elaboration Order
27664
27665
27666 To avoid placing the entire elaboration-order burden on the programmer, Ada
27667 provides three lines of defense:
27668
27669
27670 @itemize *
27671
27672 @item
27673 @emph{Static semantics}
27674
27675 Static semantic rules restrict the possible choice of elaboration order. For
27676 instance, if unit Client @emph{with}s unit Server, then the spec of Server is
27677 always elaborated prior to Client. The same principle applies to child units
27678 - the spec of a parent unit is always elaborated prior to the child unit.
27679
27680 @item
27681 @emph{Dynamic semantics}
27682
27683 Dynamic checks are performed at run time, to ensure that a target is
27684 elaborated prior to a scenario that invokes it, thus avoiding ABE problems.
27685 A failed run-time check raises exception @code{Program_Error}. The following
27686 restrictions apply:
27687
27688
27689 @itemize -
27690
27691 @item
27692 @emph{Restrictions on calls}
27693
27694 An entry, operator, or subprogram can be called from elaboration code only
27695 when the corresponding body has been elaborated.
27696
27697 @item
27698 @emph{Restrictions on instantiations}
27699
27700 A generic unit can be instantiated by elaboration code only when the
27701 corresponding body has been elaborated.
27702
27703 @item
27704 @emph{Restrictions on task activation}
27705
27706 A task can be activated by elaboration code only when the body of the
27707 associated task type has been elaborated.
27708 @end itemize
27709
27710 The restrictions above can be summarized by the following rule:
27711
27712 @emph{If a target has a body, then this body must be elaborated prior to the
27713 scenario that invokes the target.}
27714
27715 @item
27716 @emph{Elaboration control}
27717
27718 Pragmas are provided for the programmer to specify the desired elaboration
27719 order.
27720 @end itemize
27721
27722 @node Controlling the Elaboration Order in Ada,Controlling the Elaboration Order in GNAT,Checking the Elaboration Order,Elaboration Order Handling in GNAT
27723 @anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-ada}@anchor{233}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id5}@anchor{234}
27724 @section Controlling the Elaboration Order in Ada
27725
27726
27727 Ada provides several idioms and pragmas to aid the programmer with specifying
27728 the desired elaboration order and avoiding ABE problems altogether.
27729
27730
27731 @itemize *
27732
27733 @item
27734 @emph{Packages without a body}
27735
27736 A library package which does not require a completing body does not suffer
27737 from ABE problems.
27738
27739 @example
27740 package Pack is
27741 generic
27742 type Element is private;
27743 package Containers is
27744 type Element_Array is array (1 .. 10) of Element;
27745 end Containers;
27746 end Pack;
27747 @end example
27748
27749 In the example above, package @code{Pack} does not require a body because it
27750 does not contain any constructs which require completion in a body. As a
27751 result, generic @code{Pack.Containers} can be instantiated without encountering
27752 any ABE problems.
27753 @end itemize
27754
27755 @geindex pragma Pure
27756
27757
27758 @itemize *
27759
27760 @item
27761 @emph{pragma Pure}
27762
27763 Pragma @code{Pure} places sufficient restrictions on a unit to guarantee that no
27764 scenario within the unit can result in an ABE problem.
27765 @end itemize
27766
27767 @geindex pragma Preelaborate
27768
27769
27770 @itemize *
27771
27772 @item
27773 @emph{pragma Preelaborate}
27774
27775 Pragma @code{Preelaborate} is slightly less restrictive than pragma @code{Pure},
27776 but still strong enough to prevent ABE problems within a unit.
27777 @end itemize
27778
27779 @geindex pragma Elaborate_Body
27780
27781
27782 @itemize *
27783
27784 @item
27785 @emph{pragma Elaborate_Body}
27786
27787 Pragma @code{Elaborate_Body} requires that the body of a unit is elaborated
27788 immediately after its spec. This restriction guarantees that no client
27789 scenario can invoke a server target before the target body has been
27790 elaborated because the spec and body are effectively "glued" together.
27791
27792 @example
27793 package Server is
27794 pragma Elaborate_Body;
27795
27796 function Func return Integer;
27797 end Server;
27798 @end example
27799
27800 @example
27801 package body Server is
27802 function Func return Integer is
27803 begin
27804 ...
27805 end Func;
27806 end Server;
27807 @end example
27808
27809 @example
27810 with Server;
27811 package Client is
27812 Val : constant Integer := Server.Func;
27813 end Client;
27814 @end example
27815
27816 In the example above, pragma @code{Elaborate_Body} guarantees the following
27817 elaboration order:
27818
27819 @example
27820 spec of Server
27821 body of Server
27822 spec of Client
27823 @end example
27824
27825 because the spec of @code{Server} must be elaborated prior to @code{Client} by
27826 virtue of the @emph{with} clause, and in addition the body of @code{Server} must be
27827 elaborated immediately after the spec of @code{Server}.
27828
27829 Removing pragma @code{Elaborate_Body} could result in the following incorrect
27830 elaboration order:
27831
27832 @example
27833 spec of Server
27834 spec of Client
27835 body of Server
27836 @end example
27837
27838 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func} has
27839 not been elaborated yet.
27840 @end itemize
27841
27842 The pragmas outlined above allow a server unit to guarantee safe elaboration
27843 use by client units. Thus it is a good rule to mark units as @code{Pure} or
27844 @code{Preelaborate}, and if this is not possible, mark them as @code{Elaborate_Body}.
27845
27846 There are however situations where @code{Pure}, @code{Preelaborate}, and
27847 @code{Elaborate_Body} are not applicable. Ada provides another set of pragmas for
27848 use by client units to help ensure the elaboration safety of server units they
27849 depend on.
27850
27851 @geindex pragma Elaborate (Unit)
27852
27853
27854 @itemize *
27855
27856 @item
27857 @emph{pragma Elaborate (Unit)}
27858
27859 Pragma @code{Elaborate} can be placed in the context clauses of a unit, after a
27860 @emph{with} clause. It guarantees that both the spec and body of its argument will
27861 be elaborated prior to the unit with the pragma. Note that other unrelated
27862 units may be elaborated in between the spec and the body.
27863
27864 @example
27865 package Server is
27866 function Func return Integer;
27867 end Server;
27868 @end example
27869
27870 @example
27871 package body Server is
27872 function Func return Integer is
27873 begin
27874 ...
27875 end Func;
27876 end Server;
27877 @end example
27878
27879 @example
27880 with Server;
27881 pragma Elaborate (Server);
27882 package Client is
27883 Val : constant Integer := Server.Func;
27884 end Client;
27885 @end example
27886
27887 In the example above, pragma @code{Elaborate} guarantees the following
27888 elaboration order:
27889
27890 @example
27891 spec of Server
27892 body of Server
27893 spec of Client
27894 @end example
27895
27896 Removing pragma @code{Elaborate} could result in the following incorrect
27897 elaboration order:
27898
27899 @example
27900 spec of Server
27901 spec of Client
27902 body of Server
27903 @end example
27904
27905 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func}
27906 has not been elaborated yet.
27907 @end itemize
27908
27909 @geindex pragma Elaborate_All (Unit)
27910
27911
27912 @itemize *
27913
27914 @item
27915 @emph{pragma Elaborate_All (Unit)}
27916
27917 Pragma @code{Elaborate_All} is placed in the context clauses of a unit, after
27918 a @emph{with} clause. It guarantees that both the spec and body of its argument
27919 will be elaborated prior to the unit with the pragma, as well as all units
27920 @emph{with}ed by the spec and body of the argument, recursively. Note that other
27921 unrelated units may be elaborated in between the spec and the body.
27922
27923 @example
27924 package Math is
27925 function Factorial (Val : Natural) return Natural;
27926 end Math;
27927 @end example
27928
27929 @example
27930 package body Math is
27931 function Factorial (Val : Natural) return Natural is
27932 begin
27933 ...;
27934 end Factorial;
27935 end Math;
27936 @end example
27937
27938 @example
27939 package Computer is
27940 type Operation_Kind is (None, Op_Factorial);
27941
27942 function Compute
27943 (Val : Natural;
27944 Op : Operation_Kind) return Natural;
27945 end Computer;
27946 @end example
27947
27948 @example
27949 with Math;
27950 package body Computer is
27951 function Compute
27952 (Val : Natural;
27953 Op : Operation_Kind) return Natural
27954 is
27955 if Op = Op_Factorial then
27956 return Math.Factorial (Val);
27957 end if;
27958
27959 return 0;
27960 end Compute;
27961 end Computer;
27962 @end example
27963
27964 @example
27965 with Computer;
27966 pragma Elaborate_All (Computer);
27967 package Client is
27968 Val : constant Natural :=
27969 Computer.Compute (123, Computer.Op_Factorial);
27970 end Client;
27971 @end example
27972
27973 In the example above, pragma @code{Elaborate_All} can result in the following
27974 elaboration order:
27975
27976 @example
27977 spec of Math
27978 body of Math
27979 spec of Computer
27980 body of Computer
27981 spec of Client
27982 @end example
27983
27984 Note that there are several allowable suborders for the specs and bodies of
27985 @code{Math} and @code{Computer}, but the point is that these specs and bodies will
27986 be elaborated prior to @code{Client}.
27987
27988 Removing pragma @code{Elaborate_All} could result in the following incorrect
27989 elaboration order:
27990
27991 @example
27992 spec of Math
27993 spec of Computer
27994 body of Computer
27995 spec of Client
27996 body of Math
27997 @end example
27998
27999 where @code{Client} invokes @code{Computer.Compute}, which in turn invokes
28000 @code{Math.Factorial}, but the body of @code{Math.Factorial} has not been
28001 elaborated yet.
28002 @end itemize
28003
28004 All pragmas shown above can be summarized by the following rule:
28005
28006 @emph{If a client unit elaborates a server target directly or indirectly, then if
28007 the server unit requires a body and does not have pragma Pure, Preelaborate,
28008 or Elaborate_Body, then the client unit should have pragma Elaborate or
28009 Elaborate_All for the server unit.}
28010
28011 If the rule outlined above is not followed, then a program may fall in one of
28012 the following states:
28013
28014
28015 @itemize *
28016
28017 @item
28018 @emph{No elaboration order exists}
28019
28020 In this case a compiler must diagnose the situation, and refuse to build an
28021 executable program.
28022
28023 @item
28024 @emph{One or more incorrect elaboration orders exist}
28025
28026 In this case a compiler can build an executable program, but
28027 @code{Program_Error} will be raised when the program is run.
28028
28029 @item
28030 @emph{Several elaboration orders exist, some correct, some incorrect}
28031
28032 In this case the programmer has not controlled the elaboration order. As a
28033 result, a compiler may or may not pick one of the correct orders, and the
28034 program may or may not raise @code{Program_Error} when it is run. This is the
28035 worst possible state because the program may fail on another compiler, or
28036 even another version of the same compiler.
28037
28038 @item
28039 @emph{One or more correct orders exist}
28040
28041 In this case a compiler can build an executable program, and the program is
28042 run successfully. This state may be guaranteed by following the outlined
28043 rules, or may be the result of good program architecture.
28044 @end itemize
28045
28046 Note that one additional advantage of using @code{Elaborate} and @code{Elaborate_All}
28047 is that the program continues to stay in the last state (one or more correct
28048 orders exist) even if maintenance changes the bodies of targets.
28049
28050 @node Controlling the Elaboration Order in GNAT,Mixing Elaboration Models,Controlling the Elaboration Order in Ada,Elaboration Order Handling in GNAT
28051 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id6}@anchor{235}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-gnat}@anchor{236}
28052 @section Controlling the Elaboration Order in GNAT
28053
28054
28055 In addition to Ada semantics and rules synthesized from them, GNAT offers
28056 three elaboration models to aid the programmer with specifying the correct
28057 elaboration order and to diagnose elaboration problems.
28058
28059 @geindex Dynamic elaboration model
28060
28061
28062 @itemize *
28063
28064 @item
28065 @emph{Dynamic elaboration model}
28066
28067 This is the most permissive of the three elaboration models and emulates the
28068 behavior specified by the Ada Reference Manual. When the dynamic model is in
28069 effect, GNAT makes the following assumptions:
28070
28071
28072 @itemize -
28073
28074 @item
28075 All code within all units in a partition is considered to be elaboration
28076 code.
28077
28078 @item
28079 Some of the invocations in elaboration code may not take place at run time
28080 due to conditional execution.
28081 @end itemize
28082
28083 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28084 that invoke internal targets. In addition, GNAT generates run-time checks for
28085 all external targets and for all scenarios that may exhibit ABE problems.
28086
28087 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
28088 preelaborability of units, and elaboration-control pragmas. The dynamic model
28089 attempts to take all invocations in elaboration code into account. If an
28090 invocation leads to a circularity, GNAT ignores the invocation based on the
28091 assumptions stated above. An order obtained using the dynamic model may fail
28092 an ABE check at run time when GNAT ignored an invocation.
28093
28094 The dynamic model is enabled with compiler switch @code{-gnatE}.
28095 @end itemize
28096
28097 @geindex Static elaboration model
28098
28099
28100 @itemize *
28101
28102 @item
28103 @emph{Static elaboration model}
28104
28105 This is the middle ground of the three models. When the static model is in
28106 effect, GNAT makes the following assumptions:
28107
28108
28109 @itemize -
28110
28111 @item
28112 Only code at the library level and in package body statements within all
28113 units in a partition is considered to be elaboration code.
28114
28115 @item
28116 All invocations in elaboration will take place at run time, regardless of
28117 conditional execution.
28118 @end itemize
28119
28120 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28121 that invoke internal targets. In addition, GNAT generates run-time checks for
28122 all external targets and for all scenarios that may exhibit ABE problems.
28123
28124 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
28125 preelaborability of units, presence of elaboration-control pragmas, and all
28126 invocations in elaboration code. An order obtained using the static model is
28127 guaranteed to be ABE problem-free, excluding dispatching calls and
28128 access-to-subprogram types.
28129
28130 The static model is the default model in GNAT.
28131 @end itemize
28132
28133 @geindex SPARK elaboration model
28134
28135
28136 @itemize *
28137
28138 @item
28139 @emph{SPARK elaboration model}
28140
28141 This is the most conservative of the three models and enforces the SPARK
28142 rules of elaboration as defined in the SPARK Reference Manual, section 7.7.
28143 The SPARK model is in effect only when a scenario and a target reside in a
28144 region subject to @code{SPARK_Mode On}, otherwise the dynamic or static model
28145 is in effect.
28146
28147 The SPARK model is enabled with compiler switch @code{-gnatd.v}.
28148 @end itemize
28149
28150 @geindex Legacy elaboration models
28151
28152
28153 @itemize *
28154
28155 @item
28156 @emph{Legacy elaboration models}
28157
28158 In addition to the three elaboration models outlined above, GNAT provides the
28159 following legacy models:
28160
28161
28162 @itemize -
28163
28164 @item
28165 @cite{Legacy elaboration-checking model} available in pre-18.x versions of GNAT.
28166 This model is enabled with compiler switch @code{-gnatH}.
28167
28168 @item
28169 @cite{Legacy elaboration-order model} available in pre-20.x versions of GNAT.
28170 This model is enabled with binder switch @code{-H}.
28171 @end itemize
28172 @end itemize
28173
28174 @geindex Relaxed elaboration mode
28175
28176 The dynamic, legacy, and static models can be relaxed using compiler switch
28177 @code{-gnatJ}, making them more permissive. Note that in this mode, GNAT
28178 may not diagnose certain elaboration issues or install run-time checks.
28179
28180 @node Mixing Elaboration Models,ABE Diagnostics,Controlling the Elaboration Order in GNAT,Elaboration Order Handling in GNAT
28181 @anchor{gnat_ugn/elaboration_order_handling_in_gnat mixing-elaboration-models}@anchor{237}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id7}@anchor{238}
28182 @section Mixing Elaboration Models
28183
28184
28185 It is possible to mix units compiled with a different elaboration model,
28186 however the following rules must be observed:
28187
28188
28189 @itemize *
28190
28191 @item
28192 A client unit compiled with the dynamic model can only @emph{with} a server unit
28193 that meets at least one of the following criteria:
28194
28195
28196 @itemize -
28197
28198 @item
28199 The server unit is compiled with the dynamic model.
28200
28201 @item
28202 The server unit is a GNAT implementation unit from the @code{Ada}, @code{GNAT},
28203 @code{Interfaces}, or @code{System} hierarchies.
28204
28205 @item
28206 The server unit has pragma @code{Pure} or @code{Preelaborate}.
28207
28208 @item
28209 The client unit has an explicit @code{Elaborate_All} pragma for the server
28210 unit.
28211 @end itemize
28212 @end itemize
28213
28214 These rules ensure that elaboration checks are not omitted. If the rules are
28215 violated, the binder emits a warning:
28216
28217 @quotation
28218
28219 @example
28220 warning: "x.ads" has dynamic elaboration checks and with's
28221 warning: "y.ads" which has static elaboration checks
28222 @end example
28223 @end quotation
28224
28225 The warnings can be suppressed by binder switch @code{-ws}.
28226
28227 @node ABE Diagnostics,SPARK Diagnostics,Mixing Elaboration Models,Elaboration Order Handling in GNAT
28228 @anchor{gnat_ugn/elaboration_order_handling_in_gnat abe-diagnostics}@anchor{239}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id8}@anchor{23a}
28229 @section ABE Diagnostics
28230
28231
28232 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28233 that invoke internal targets, regardless of whether the dynamic, SPARK, or
28234 static model is in effect.
28235
28236 Note that GNAT emits warnings rather than hard errors whenever it encounters an
28237 elaboration problem. This is because the elaboration model in effect may be too
28238 conservative, or a particular scenario may not be invoked due conditional
28239 execution. The warnings can be suppressed selectively with @code{pragma Warnings
28240 (Off)} or globally with compiler switch @code{-gnatwL}.
28241
28242 A @emph{guaranteed ABE} arises when the body of a target is not elaborated early
28243 enough, and causes @emph{all} scenarios that directly invoke the target to fail.
28244
28245 @quotation
28246
28247 @example
28248 package body Guaranteed_ABE is
28249 function ABE return Integer;
28250
28251 Val : constant Integer := ABE;
28252
28253 function ABE return Integer is
28254 begin
28255 ...
28256 end ABE;
28257 end Guaranteed_ABE;
28258 @end example
28259 @end quotation
28260
28261 In the example above, the elaboration of @code{Guaranteed_ABE}'s body elaborates
28262 the declaration of @code{Val}. This invokes function @code{ABE}, however the body of
28263 @code{ABE} has not been elaborated yet. GNAT emits the following diagnostic:
28264
28265 @quotation
28266
28267 @example
28268 4. Val : constant Integer := ABE;
28269 |
28270 >>> warning: cannot call "ABE" before body seen
28271 >>> warning: Program_Error will be raised at run time
28272 @end example
28273 @end quotation
28274
28275 A @emph{conditional ABE} arises when the body of a target is not elaborated early
28276 enough, and causes @emph{some} scenarios that directly invoke the target to fail.
28277
28278 @quotation
28279
28280 @example
28281 1. package body Conditional_ABE is
28282 2. procedure Force_Body is null;
28283 3.
28284 4. generic
28285 5. with function Func return Integer;
28286 6. package Gen is
28287 7. Val : constant Integer := Func;
28288 8. end Gen;
28289 9.
28290 10. function ABE return Integer;
28291 11.
28292 12. function Cause_ABE return Boolean is
28293 13. package Inst is new Gen (ABE);
28294 14. begin
28295 15. ...
28296 16. end Cause_ABE;
28297 17.
28298 18. Val : constant Boolean := Cause_ABE;
28299 19.
28300 20. function ABE return Integer is
28301 21. begin
28302 22. ...
28303 23. end ABE;
28304 24.
28305 25. Safe : constant Boolean := Cause_ABE;
28306 26. end Conditional_ABE;
28307 @end example
28308 @end quotation
28309
28310 In the example above, the elaboration of package body @code{Conditional_ABE}
28311 elaborates the declaration of @code{Val}. This invokes function @code{Cause_ABE},
28312 which instantiates generic unit @code{Gen} as @code{Inst}. The elaboration of
28313 @code{Inst} invokes function @code{ABE}, however the body of @code{ABE} has not been
28314 elaborated yet. GNAT emits the following diagnostic:
28315
28316 @quotation
28317
28318 @example
28319 13. package Inst is new Gen (ABE);
28320 |
28321 >>> warning: in instantiation at line 7
28322 >>> warning: cannot call "ABE" before body seen
28323 >>> warning: Program_Error may be raised at run time
28324 >>> warning: body of unit "Conditional_ABE" elaborated
28325 >>> warning: function "Cause_ABE" called at line 18
28326 >>> warning: function "ABE" called at line 7, instance at line 13
28327 @end example
28328 @end quotation
28329
28330 Note that the same ABE problem does not occur with the elaboration of
28331 declaration @code{Safe} because the body of function @code{ABE} has already been
28332 elaborated at that point.
28333
28334 @node SPARK Diagnostics,Elaboration Circularities,ABE Diagnostics,Elaboration Order Handling in GNAT
28335 @anchor{gnat_ugn/elaboration_order_handling_in_gnat spark-diagnostics}@anchor{23b}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id9}@anchor{23c}
28336 @section SPARK Diagnostics
28337
28338
28339 GNAT enforces the SPARK rules of elaboration as defined in the SPARK Reference
28340 Manual section 7.7 when compiler switch @code{-gnatd.v} is in effect. Note
28341 that GNAT emits hard errors whenever it encounters a violation of the SPARK
28342 rules.
28343
28344 @quotation
28345
28346 @example
28347 1. with Server;
28348 2. package body SPARK_Diagnostics with SPARK_Mode is
28349 3. Val : constant Integer := Server.Func;
28350 |
28351 >>> call to "Func" during elaboration in SPARK
28352 >>> unit "SPARK_Diagnostics" requires pragma "Elaborate_All" for "Server"
28353 >>> body of unit "SPARK_Model" elaborated
28354 >>> function "Func" called at line 3
28355
28356 4. end SPARK_Diagnostics;
28357 @end example
28358 @end quotation
28359
28360 @node Elaboration Circularities,Resolving Elaboration Circularities,SPARK Diagnostics,Elaboration Order Handling in GNAT
28361 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id10}@anchor{23d}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-circularities}@anchor{23e}
28362 @section Elaboration Circularities
28363
28364
28365 An @strong{elaboration circularity} occurs whenever the elaboration of a set of
28366 units enters a deadlocked state, where each unit is waiting for another unit
28367 to be elaborated. This situation may be the result of improper use of @emph{with}
28368 clauses, elaboration-control pragmas, or invocations in elaboration code.
28369
28370 The following example exhibits an elaboration circularity.
28371
28372 @quotation
28373
28374 @example
28375 with B; pragma Elaborate (B);
28376 package A is
28377 end A;
28378 @end example
28379
28380 @example
28381 package B is
28382 procedure Force_Body;
28383 end B;
28384 @end example
28385
28386 @example
28387 with C;
28388 package body B is
28389 procedure Force_Body is null;
28390
28391 Elab : constant Integer := C.Func;
28392 end B;
28393 @end example
28394
28395 @example
28396 package C is
28397 function Func return Integer;
28398 end C;
28399 @end example
28400
28401 @example
28402 with A;
28403 package body C is
28404 function Func return Integer is
28405 begin
28406 ...
28407 end Func;
28408 end C;
28409 @end example
28410 @end quotation
28411
28412 The binder emits the following diagnostic:
28413
28414 @quotation
28415
28416 @example
28417 error: Elaboration circularity detected
28418 info:
28419 info: Reason:
28420 info:
28421 info: unit "a (spec)" depends on its own elaboration
28422 info:
28423 info: Circularity:
28424 info:
28425 info: unit "a (spec)" has with clause and pragma Elaborate for unit "b (spec)"
28426 info: unit "b (body)" is in the closure of pragma Elaborate
28427 info: unit "b (body)" invokes a construct of unit "c (body)" at elaboration time
28428 info: unit "c (body)" has with clause for unit "a (spec)"
28429 info:
28430 info: Suggestions:
28431 info:
28432 info: remove pragma Elaborate for unit "b (body)" in unit "a (spec)"
28433 info: use the dynamic elaboration model (compiler switch -gnatE)
28434 @end example
28435 @end quotation
28436
28437 The diagnostic consist of the following sections:
28438
28439
28440 @itemize *
28441
28442 @item
28443 Reason
28444
28445 This section provides a short explanation describing why the set of units
28446 could not be ordered.
28447
28448 @item
28449 Circularity
28450
28451 This section enumerates the units comprising the deadlocked set, along with
28452 their interdependencies.
28453
28454 @item
28455 Suggestions
28456
28457 This section enumerates various tactics for eliminating the circularity.
28458 @end itemize
28459
28460 @node Resolving Elaboration Circularities,Elaboration-related Compiler Switches,Elaboration Circularities,Elaboration Order Handling in GNAT
28461 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id11}@anchor{23f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat resolving-elaboration-circularities}@anchor{240}
28462 @section Resolving Elaboration Circularities
28463
28464
28465 The most desirable option from the point of view of long-term maintenance is to
28466 rearrange the program so that the elaboration problems are avoided. One useful
28467 technique is to place the elaboration code into separate child packages.
28468 Another is to move some of the initialization code to explicitly invoked
28469 subprograms, where the program controls the order of initialization explicitly.
28470 Although this is the most desirable option, it may be impractical and involve
28471 too much modification, especially in the case of complex legacy code.
28472
28473 When faced with an elaboration circularity, the programmer should also consider
28474 the tactics given in the suggestions section of the circularity diagnostic.
28475 Depending on the units involved in the circularity, their @emph{with} clauses,
28476 purity, preelaborability, presence of elaboration-control pragmas and
28477 invocations at elaboration time, the binder may suggest one or more of the
28478 following tactics to eliminate the circularity:
28479
28480
28481 @itemize *
28482
28483 @item
28484 Pragma Elaborate elimination
28485
28486 @example
28487 remove pragma Elaborate for unit "..." in unit "..."
28488 @end example
28489
28490 This tactic is suggested when the binder has determined that pragma
28491 @code{Elaborate}:
28492
28493
28494 @itemize -
28495
28496 @item
28497 Prevents a set of units from being elaborated.
28498
28499 @item
28500 The removal of the pragma will not eliminate the semantic effects of the
28501 pragma. In other words, the argument of the pragma will still be elaborated
28502 prior to the unit containing the pragma.
28503
28504 @item
28505 The removal of the pragma will enable the successful ordering of the units.
28506 @end itemize
28507
28508 The programmer should remove the pragma as advised, and rebuild the program.
28509
28510 @item
28511 Pragma Elaborate_All elimination
28512
28513 @example
28514 remove pragma Elaborate_All for unit "..." in unit "..."
28515 @end example
28516
28517 This tactic is suggested when the binder has determined that pragma
28518 @code{Elaborate_All}:
28519
28520
28521 @itemize -
28522
28523 @item
28524 Prevents a set of units from being elaborated.
28525
28526 @item
28527 The removal of the pragma will not eliminate the semantic effects of the
28528 pragma. In other words, the argument of the pragma along with its @emph{with}
28529 closure will still be elaborated prior to the unit containing the pragma.
28530
28531 @item
28532 The removal of the pragma will enable the successful ordering of the units.
28533 @end itemize
28534
28535 The programmer should remove the pragma as advised, and rebuild the program.
28536
28537 @item
28538 Pragma Elaborate_All downgrade
28539
28540 @example
28541 change pragma Elaborate_All for unit "..." to Elaborate in unit "..."
28542 @end example
28543
28544 This tactic is always suggested with the pragma @code{Elaborate_All} elimination
28545 tactic. It offers a different alernative of guaranteeing that the argument of
28546 the pragma will still be elaborated prior to the unit containing the pragma.
28547
28548 The programmer should update the pragma as advised, and rebuild the program.
28549
28550 @item
28551 Pragma Elaborate_Body elimination
28552
28553 @example
28554 remove pragma Elaborate_Body in unit "..."
28555 @end example
28556
28557 This tactic is suggested when the binder has determined that pragma
28558 @code{Elaborate_Body}:
28559
28560
28561 @itemize -
28562
28563 @item
28564 Prevents a set of units from being elaborated.
28565
28566 @item
28567 The removal of the pragma will enable the successful ordering of the units.
28568 @end itemize
28569
28570 Note that the binder cannot determine whether the pragma is required for
28571 other purposes, such as guaranteeing the initialization of a variable
28572 declared in the spec by elaboration code in the body.
28573
28574 The programmer should remove the pragma as advised, and rebuild the program.
28575
28576 @item
28577 Use of pragma Restrictions
28578
28579 @example
28580 use pragma Restrictions (No_Entry_Calls_In_Elaboration_Code)
28581 @end example
28582
28583 This tactic is suggested when the binder has determined that a task
28584 activation at elaboration time:
28585
28586
28587 @itemize -
28588
28589 @item
28590 Prevents a set of units from being elaborated.
28591 @end itemize
28592
28593 Note that the binder cannot determine with certainty whether the task will
28594 block at elaboration time.
28595
28596 The programmer should create a configuration file, place the pragma within,
28597 update the general compilation arguments, and rebuild the program.
28598
28599 @item
28600 Use of dynamic elaboration model
28601
28602 @example
28603 use the dynamic elaboration model (compiler switch -gnatE)
28604 @end example
28605
28606 This tactic is suggested when the binder has determined that an invocation at
28607 elaboration time:
28608
28609
28610 @itemize -
28611
28612 @item
28613 Prevents a set of units from being elaborated.
28614
28615 @item
28616 The use of the dynamic model will enable the successful ordering of the
28617 units.
28618 @end itemize
28619
28620 The programmer has two options:
28621
28622
28623 @itemize -
28624
28625 @item
28626 Determine the units involved in the invocation using the detailed
28627 invocation information, and add compiler switch @code{-gnatE} to the
28628 compilation arguments of selected files only. This approach will yield
28629 safer elaboration orders compared to the other option because it will
28630 minimize the opportunities presented to the dynamic model for ignoring
28631 invocations.
28632
28633 @item
28634 Add compiler switch @code{-gnatE} to the general compilation arguments.
28635 @end itemize
28636
28637 @item
28638 Use of detailed invocation information
28639
28640 @example
28641 use detailed invocation information (compiler switch -gnatd_F)
28642 @end example
28643
28644 This tactic is always suggested with the use of the dynamic model tactic. It
28645 causes the circularity section of the circularity diagnostic to describe the
28646 flow of elaboration code from a unit to a unit, enumerating all such paths in
28647 the process.
28648
28649 The programmer should analyze this information to determine which units
28650 should be compiled with the dynamic model.
28651
28652 @item
28653 Forced-dependency elimination
28654
28655 @example
28656 remove the dependency of unit "..." on unit "..." from the argument of switch -f
28657 @end example
28658
28659 This tactic is suggested when the binder has determined that a dependency
28660 present in the forced-elaboration-order file indicated by binder switch
28661 @code{-f}:
28662
28663
28664 @itemize -
28665
28666 @item
28667 Prevents a set of units from being elaborated.
28668
28669 @item
28670 The removal of the dependency will enable the successful ordering of the
28671 units.
28672 @end itemize
28673
28674 The programmer should edit the forced-elaboration-order file, remove the
28675 dependency, and rebind the program.
28676
28677 @item
28678 All forced-dependency elimination
28679
28680 @example
28681 remove switch -f
28682 @end example
28683
28684 This tactic is suggested in case editing the forced-elaboration-order file is
28685 not an option.
28686
28687 The programmer should remove binder switch @code{-f} from the binder
28688 arguments, and rebind.
28689
28690 @item
28691 Multiple-circularities diagnostic
28692
28693 @example
28694 diagnose all circularities (binder switch -d_C)
28695 @end example
28696
28697 By default, the binder will diagnose only the highest-precedence circularity.
28698 If the program contains multiple circularities, the binder will suggest the
28699 use of binder switch @code{-d_C} in order to obtain the diagnostics of all
28700 circularities.
28701
28702 The programmer should add binder switch @code{-d_C} to the binder
28703 arguments, and rebind.
28704 @end itemize
28705
28706 If none of the tactics suggested by the binder eliminate the elaboration
28707 circularity, the programmer should consider using one of the legacy elaboration
28708 models, in the following order:
28709
28710
28711 @itemize *
28712
28713 @item
28714 Use the pre-20.x legacy elaboration-order model, with binder switch
28715 @code{-H}.
28716
28717 @item
28718 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
28719 switch @code{-gnatH} and binder switch @code{-H}.
28720
28721 @item
28722 Use the relaxed static-elaboration model, with compiler switches
28723 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
28724
28725 @item
28726 Use the relaxed dynamic-elaboration model, with compiler switches
28727 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
28728 @code{-H}.
28729 @end itemize
28730
28731 @node Elaboration-related Compiler Switches,Summary of Procedures for Elaboration Control,Resolving Elaboration Circularities,Elaboration Order Handling in GNAT
28732 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id12}@anchor{241}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-related-compiler-switches}@anchor{242}
28733 @section Elaboration-related Compiler Switches
28734
28735
28736 GNAT has several switches that affect the elaboration model and consequently
28737 the elaboration order chosen by the binder.
28738
28739 @geindex -gnatE (gnat)
28740
28741
28742 @table @asis
28743
28744 @item @code{-gnatE}
28745
28746 Dynamic elaboration checking mode enabled
28747
28748 When this switch is in effect, GNAT activates the dynamic model.
28749 @end table
28750
28751 @geindex -gnatel (gnat)
28752
28753
28754 @table @asis
28755
28756 @item @code{-gnatel}
28757
28758 Turn on info messages on generated Elaborate[_All] pragmas
28759
28760 This switch is only applicable to the pre-20.x legacy elaboration models.
28761 The post-20.x elaboration model no longer relies on implicitly generated
28762 @code{Elaborate} and @code{Elaborate_All} pragmas to order units.
28763
28764 When this switch is in effect, GNAT will emit the following supplementary
28765 information depending on the elaboration model in effect.
28766
28767
28768 @itemize -
28769
28770 @item
28771 @emph{Dynamic model}
28772
28773 GNAT will indicate missing @code{Elaborate} and @code{Elaborate_All} pragmas for
28774 all library-level scenarios within the partition.
28775
28776 @item
28777 @emph{Static model}
28778
28779 GNAT will indicate all scenarios invoked during elaboration. In addition,
28780 it will provide detailed traceback when an implicit @code{Elaborate} or
28781 @code{Elaborate_All} pragma is generated.
28782
28783 @item
28784 @emph{SPARK model}
28785
28786 GNAT will indicate how an elaboration requirement is met by the context of
28787 a unit. This diagnostic requires compiler switch @code{-gnatd.v}.
28788
28789 @example
28790 1. with Server; pragma Elaborate_All (Server);
28791 2. package Client with SPARK_Mode is
28792 3. Val : constant Integer := Server.Func;
28793 |
28794 >>> info: call to "Func" during elaboration in SPARK
28795 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
28796
28797 4. end Client;
28798 @end example
28799 @end itemize
28800 @end table
28801
28802 @geindex -gnatH (gnat)
28803
28804
28805 @table @asis
28806
28807 @item @code{-gnatH}
28808
28809 Legacy elaboration checking mode enabled
28810
28811 When this switch is in effect, GNAT will utilize the pre-18.x elaboration
28812 model.
28813 @end table
28814
28815 @geindex -gnatJ (gnat)
28816
28817
28818 @table @asis
28819
28820 @item @code{-gnatJ}
28821
28822 Relaxed elaboration checking mode enabled
28823
28824 When this switch is in effect, GNAT will not process certain scenarios,
28825 resulting in a more permissive elaboration model. Note that this may
28826 eliminate some diagnostics and run-time checks.
28827 @end table
28828
28829 @geindex -gnatw.f (gnat)
28830
28831
28832 @table @asis
28833
28834 @item @code{-gnatw.f}
28835
28836 Turn on warnings for suspicious Subp'Access
28837
28838 When this switch is in effect, GNAT will treat @code{'Access} of an entry,
28839 operator, or subprogram as a potential call to the target and issue warnings:
28840
28841 @example
28842 1. package body Attribute_Call is
28843 2. function Func return Integer;
28844 3. type Func_Ptr is access function return Integer;
28845 4.
28846 5. Ptr : constant Func_Ptr := Func'Access;
28847 |
28848 >>> warning: "Access" attribute of "Func" before body seen
28849 >>> warning: possible Program_Error on later references
28850 >>> warning: body of unit "Attribute_Call" elaborated
28851 >>> warning: "Access" of "Func" taken at line 5
28852
28853 6.
28854 7. function Func return Integer is
28855 8. begin
28856 9. ...
28857 10. end Func;
28858 11. end Attribute_Call;
28859 @end example
28860
28861 In the example above, the elaboration of declaration @code{Ptr} is assigned
28862 @code{Func'Access} before the body of @code{Func} has been elaborated.
28863 @end table
28864
28865 @geindex -gnatwl (gnat)
28866
28867
28868 @table @asis
28869
28870 @item @code{-gnatwl}
28871
28872 Turn on warnings for elaboration problems
28873
28874 When this switch is in effect, GNAT emits diagnostics in the form of warnings
28875 concerning various elaboration problems. The warnings are enabled by default.
28876 The switch is provided in case all warnings are suppressed, but elaboration
28877 warnings are still desired.
28878
28879 @item @code{-gnatwL}
28880
28881 Turn off warnings for elaboration problems
28882
28883 When this switch is in effect, GNAT no longer emits any diagnostics in the
28884 form of warnings. Selective suppression of elaboration problems is possible
28885 using @code{pragma Warnings (Off)}.
28886
28887 @example
28888 1. package body Selective_Suppression is
28889 2. function ABE return Integer;
28890 3.
28891 4. Val_1 : constant Integer := ABE;
28892 |
28893 >>> warning: cannot call "ABE" before body seen
28894 >>> warning: Program_Error will be raised at run time
28895
28896 5.
28897 6. pragma Warnings (Off);
28898 7. Val_2 : constant Integer := ABE;
28899 8. pragma Warnings (On);
28900 9.
28901 10. function ABE return Integer is
28902 11. begin
28903 12. ...
28904 13. end ABE;
28905 14. end Selective_Suppression;
28906 @end example
28907
28908 Note that suppressing elaboration warnings does not eliminate run-time
28909 checks. The example above will still fail at run time with an ABE.
28910 @end table
28911
28912 @node Summary of Procedures for Elaboration Control,Inspecting the Chosen Elaboration Order,Elaboration-related Compiler Switches,Elaboration Order Handling in GNAT
28913 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id13}@anchor{243}@anchor{gnat_ugn/elaboration_order_handling_in_gnat summary-of-procedures-for-elaboration-control}@anchor{244}
28914 @section Summary of Procedures for Elaboration Control
28915
28916
28917 A programmer should first compile the program with the default options, using
28918 none of the binder or compiler switches. If the binder succeeds in finding an
28919 elaboration order, then apart from possible cases involing dispatching calls
28920 and access-to-subprogram types, the program is free of elaboration errors.
28921
28922 If it is important for the program to be portable to compilers other than GNAT,
28923 then the programmer should use compiler switch @code{-gnatel} and consider
28924 the messages about missing or implicitly created @code{Elaborate} and
28925 @code{Elaborate_All} pragmas.
28926
28927 If the binder reports an elaboration circularity, the programmer has several
28928 options:
28929
28930
28931 @itemize *
28932
28933 @item
28934 Ensure that elaboration warnings are enabled. This will allow the static
28935 model to output trace information of elaboration issues. The trace
28936 information could shed light on previously unforeseen dependencies, as well
28937 as their origins. Elaboration warnings are enabled with compiler switch
28938 @code{-gnatwl}.
28939
28940 @item
28941 Cosider the tactics given in the suggestions section of the circularity
28942 diagnostic.
28943
28944 @item
28945 If none of the steps outlined above resolve the circularity, use a more
28946 permissive elaboration model, in the following order:
28947
28948
28949 @itemize -
28950
28951 @item
28952 Use the pre-20.x legacy elaboration-order model, with binder switch
28953 @code{-H}.
28954
28955 @item
28956 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
28957 switch @code{-gnatH} and binder switch @code{-H}.
28958
28959 @item
28960 Use the relaxed static elaboration model, with compiler switches
28961 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
28962
28963 @item
28964 Use the relaxed dynamic elaboration model, with compiler switches
28965 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
28966 @code{-H}.
28967 @end itemize
28968 @end itemize
28969
28970 @node Inspecting the Chosen Elaboration Order,,Summary of Procedures for Elaboration Control,Elaboration Order Handling in GNAT
28971 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id14}@anchor{245}@anchor{gnat_ugn/elaboration_order_handling_in_gnat inspecting-the-chosen-elaboration-order}@anchor{246}
28972 @section Inspecting the Chosen Elaboration Order
28973
28974
28975 To see the elaboration order chosen by the binder, inspect the contents of file
28976 @cite{b~xxx.adb}. On certain targets, this file appears as @cite{b_xxx.adb}. The
28977 elaboration order appears as a sequence of calls to @code{Elab_Body} and
28978 @code{Elab_Spec}, interspersed with assignments to @cite{Exxx} which indicates that a
28979 particular unit is elaborated. For example:
28980
28981 @quotation
28982
28983 @example
28984 System.Soft_Links'Elab_Body;
28985 E14 := True;
28986 System.Secondary_Stack'Elab_Body;
28987 E18 := True;
28988 System.Exception_Table'Elab_Body;
28989 E24 := True;
28990 Ada.Io_Exceptions'Elab_Spec;
28991 E67 := True;
28992 Ada.Tags'Elab_Spec;
28993 Ada.Streams'Elab_Spec;
28994 E43 := True;
28995 Interfaces.C'Elab_Spec;
28996 E69 := True;
28997 System.Finalization_Root'Elab_Spec;
28998 E60 := True;
28999 System.Os_Lib'Elab_Body;
29000 E71 := True;
29001 System.Finalization_Implementation'Elab_Spec;
29002 System.Finalization_Implementation'Elab_Body;
29003 E62 := True;
29004 Ada.Finalization'Elab_Spec;
29005 E58 := True;
29006 Ada.Finalization.List_Controller'Elab_Spec;
29007 E76 := True;
29008 System.File_Control_Block'Elab_Spec;
29009 E74 := True;
29010 System.File_Io'Elab_Body;
29011 E56 := True;
29012 Ada.Tags'Elab_Body;
29013 E45 := True;
29014 Ada.Text_Io'Elab_Spec;
29015 Ada.Text_Io'Elab_Body;
29016 E07 := True;
29017 @end example
29018 @end quotation
29019
29020 Note also binder switch @code{-l}, which outputs the chosen elaboration
29021 order and provides a more readable form of the above:
29022
29023 @quotation
29024
29025 @example
29026 ada (spec)
29027 interfaces (spec)
29028 system (spec)
29029 system.case_util (spec)
29030 system.case_util (body)
29031 system.concat_2 (spec)
29032 system.concat_2 (body)
29033 system.concat_3 (spec)
29034 system.concat_3 (body)
29035 system.htable (spec)
29036 system.parameters (spec)
29037 system.parameters (body)
29038 system.crtl (spec)
29039 interfaces.c_streams (spec)
29040 interfaces.c_streams (body)
29041 system.restrictions (spec)
29042 system.restrictions (body)
29043 system.standard_library (spec)
29044 system.exceptions (spec)
29045 system.exceptions (body)
29046 system.storage_elements (spec)
29047 system.storage_elements (body)
29048 system.secondary_stack (spec)
29049 system.stack_checking (spec)
29050 system.stack_checking (body)
29051 system.string_hash (spec)
29052 system.string_hash (body)
29053 system.htable (body)
29054 system.strings (spec)
29055 system.strings (body)
29056 system.traceback (spec)
29057 system.traceback (body)
29058 system.traceback_entries (spec)
29059 system.traceback_entries (body)
29060 ada.exceptions (spec)
29061 ada.exceptions.last_chance_handler (spec)
29062 system.soft_links (spec)
29063 system.soft_links (body)
29064 ada.exceptions.last_chance_handler (body)
29065 system.secondary_stack (body)
29066 system.exception_table (spec)
29067 system.exception_table (body)
29068 ada.io_exceptions (spec)
29069 ada.tags (spec)
29070 ada.streams (spec)
29071 interfaces.c (spec)
29072 interfaces.c (body)
29073 system.finalization_root (spec)
29074 system.finalization_root (body)
29075 system.memory (spec)
29076 system.memory (body)
29077 system.standard_library (body)
29078 system.os_lib (spec)
29079 system.os_lib (body)
29080 system.unsigned_types (spec)
29081 system.stream_attributes (spec)
29082 system.stream_attributes (body)
29083 system.finalization_implementation (spec)
29084 system.finalization_implementation (body)
29085 ada.finalization (spec)
29086 ada.finalization (body)
29087 ada.finalization.list_controller (spec)
29088 ada.finalization.list_controller (body)
29089 system.file_control_block (spec)
29090 system.file_io (spec)
29091 system.file_io (body)
29092 system.val_uns (spec)
29093 system.val_util (spec)
29094 system.val_util (body)
29095 system.val_uns (body)
29096 system.wch_con (spec)
29097 system.wch_con (body)
29098 system.wch_cnv (spec)
29099 system.wch_jis (spec)
29100 system.wch_jis (body)
29101 system.wch_cnv (body)
29102 system.wch_stw (spec)
29103 system.wch_stw (body)
29104 ada.tags (body)
29105 ada.exceptions (body)
29106 ada.text_io (spec)
29107 ada.text_io (body)
29108 text_io (spec)
29109 gdbstr (body)
29110 @end example
29111 @end quotation
29112
29113 @node Inline Assembler,GNU Free Documentation License,Elaboration Order Handling in GNAT,Top
29114 @anchor{gnat_ugn/inline_assembler inline-assembler}@anchor{10}@anchor{gnat_ugn/inline_assembler doc}@anchor{247}@anchor{gnat_ugn/inline_assembler id1}@anchor{248}
29115 @chapter Inline Assembler
29116
29117
29118 @geindex Inline Assembler
29119
29120 If you need to write low-level software that interacts directly
29121 with the hardware, Ada provides two ways to incorporate assembly
29122 language code into your program. First, you can import and invoke
29123 external routines written in assembly language, an Ada feature fully
29124 supported by GNAT. However, for small sections of code it may be simpler
29125 or more efficient to include assembly language statements directly
29126 in your Ada source program, using the facilities of the implementation-defined
29127 package @code{System.Machine_Code}, which incorporates the gcc
29128 Inline Assembler. The Inline Assembler approach offers a number of advantages,
29129 including the following:
29130
29131
29132 @itemize *
29133
29134 @item
29135 No need to use non-Ada tools
29136
29137 @item
29138 Consistent interface over different targets
29139
29140 @item
29141 Automatic usage of the proper calling conventions
29142
29143 @item
29144 Access to Ada constants and variables
29145
29146 @item
29147 Definition of intrinsic routines
29148
29149 @item
29150 Possibility of inlining a subprogram comprising assembler code
29151
29152 @item
29153 Code optimizer can take Inline Assembler code into account
29154 @end itemize
29155
29156 This appendix presents a series of examples to show you how to use
29157 the Inline Assembler. Although it focuses on the Intel x86,
29158 the general approach applies also to other processors.
29159 It is assumed that you are familiar with Ada
29160 and with assembly language programming.
29161
29162 @menu
29163 * Basic Assembler Syntax::
29164 * A Simple Example of Inline Assembler::
29165 * Output Variables in Inline Assembler::
29166 * Input Variables in Inline Assembler::
29167 * Inlining Inline Assembler Code::
29168 * Other Asm Functionality::
29169
29170 @end menu
29171
29172 @node Basic Assembler Syntax,A Simple Example of Inline Assembler,,Inline Assembler
29173 @anchor{gnat_ugn/inline_assembler id2}@anchor{249}@anchor{gnat_ugn/inline_assembler basic-assembler-syntax}@anchor{24a}
29174 @section Basic Assembler Syntax
29175
29176
29177 The assembler used by GNAT and gcc is based not on the Intel assembly
29178 language, but rather on a language that descends from the AT&T Unix
29179 assembler @code{as} (and which is often referred to as 'AT&T syntax').
29180 The following table summarizes the main features of @code{as} syntax
29181 and points out the differences from the Intel conventions.
29182 See the gcc @code{as} and @code{gas} (an @code{as} macro
29183 pre-processor) documentation for further information.
29184
29185
29186 @display
29187 @emph{Register names}@w{ }
29188 @display
29189 gcc / @code{as}: Prefix with '%'; for example @code{%eax}@w{ }
29190 Intel: No extra punctuation; for example @code{eax}@w{ }
29191 @end display
29192 @end display
29193
29194
29195
29196
29197 @display
29198 @emph{Immediate operand}@w{ }
29199 @display
29200 gcc / @code{as}: Prefix with '$'; for example @code{$4}@w{ }
29201 Intel: No extra punctuation; for example @code{4}@w{ }
29202 @end display
29203 @end display
29204
29205
29206
29207
29208 @display
29209 @emph{Address}@w{ }
29210 @display
29211 gcc / @code{as}: Prefix with '$'; for example @code{$loc}@w{ }
29212 Intel: No extra punctuation; for example @code{loc}@w{ }
29213 @end display
29214 @end display
29215
29216
29217
29218
29219 @display
29220 @emph{Memory contents}@w{ }
29221 @display
29222 gcc / @code{as}: No extra punctuation; for example @code{loc}@w{ }
29223 Intel: Square brackets; for example @code{[loc]}@w{ }
29224 @end display
29225 @end display
29226
29227
29228
29229
29230 @display
29231 @emph{Register contents}@w{ }
29232 @display
29233 gcc / @code{as}: Parentheses; for example @code{(%eax)}@w{ }
29234 Intel: Square brackets; for example @code{[eax]}@w{ }
29235 @end display
29236 @end display
29237
29238
29239
29240
29241 @display
29242 @emph{Hexadecimal numbers}@w{ }
29243 @display
29244 gcc / @code{as}: Leading '0x' (C language syntax); for example @code{0xA0}@w{ }
29245 Intel: Trailing 'h'; for example @code{A0h}@w{ }
29246 @end display
29247 @end display
29248
29249
29250
29251
29252 @display
29253 @emph{Operand size}@w{ }
29254 @display
29255 gcc / @code{as}: Explicit in op code; for example @code{movw} to move a 16-bit word@w{ }
29256 Intel: Implicit, deduced by assembler; for example @code{mov}@w{ }
29257 @end display
29258 @end display
29259
29260
29261
29262
29263 @display
29264 @emph{Instruction repetition}@w{ }
29265 @display
29266 gcc / @code{as}: Split into two lines; for example@w{ }
29267 @display
29268 @code{rep}@w{ }
29269 @code{stosl}@w{ }
29270 @end display
29271 Intel: Keep on one line; for example @code{rep stosl}@w{ }
29272 @end display
29273 @end display
29274
29275
29276
29277
29278 @display
29279 @emph{Order of operands}@w{ }
29280 @display
29281 gcc / @code{as}: Source first; for example @code{movw $4, %eax}@w{ }
29282 Intel: Destination first; for example @code{mov eax, 4}@w{ }
29283 @end display
29284 @end display
29285
29286
29287
29288 @node A Simple Example of Inline Assembler,Output Variables in Inline Assembler,Basic Assembler Syntax,Inline Assembler
29289 @anchor{gnat_ugn/inline_assembler a-simple-example-of-inline-assembler}@anchor{24b}@anchor{gnat_ugn/inline_assembler id3}@anchor{24c}
29290 @section A Simple Example of Inline Assembler
29291
29292
29293 The following example will generate a single assembly language statement,
29294 @code{nop}, which does nothing. Despite its lack of run-time effect,
29295 the example will be useful in illustrating the basics of
29296 the Inline Assembler facility.
29297
29298 @quotation
29299
29300 @example
29301 with System.Machine_Code; use System.Machine_Code;
29302 procedure Nothing is
29303 begin
29304 Asm ("nop");
29305 end Nothing;
29306 @end example
29307 @end quotation
29308
29309 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29310 here it takes one parameter, a @emph{template string} that must be a static
29311 expression and that will form the generated instruction.
29312 @code{Asm} may be regarded as a compile-time procedure that parses
29313 the template string and additional parameters (none here),
29314 from which it generates a sequence of assembly language instructions.
29315
29316 The examples in this chapter will illustrate several of the forms
29317 for invoking @code{Asm}; a complete specification of the syntax
29318 is found in the @code{Machine_Code_Insertions} section of the
29319 @cite{GNAT Reference Manual}.
29320
29321 Under the standard GNAT conventions, the @code{Nothing} procedure
29322 should be in a file named @code{nothing.adb}.
29323 You can build the executable in the usual way:
29324
29325 @quotation
29326
29327 @example
29328 $ gnatmake nothing
29329 @end example
29330 @end quotation
29331
29332 However, the interesting aspect of this example is not its run-time behavior
29333 but rather the generated assembly code.
29334 To see this output, invoke the compiler as follows:
29335
29336 @quotation
29337
29338 @example
29339 $ gcc -c -S -fomit-frame-pointer -gnatp nothing.adb
29340 @end example
29341 @end quotation
29342
29343 where the options are:
29344
29345
29346 @itemize *
29347
29348 @item
29349
29350 @table @asis
29351
29352 @item @code{-c}
29353
29354 compile only (no bind or link)
29355 @end table
29356
29357 @item
29358
29359 @table @asis
29360
29361 @item @code{-S}
29362
29363 generate assembler listing
29364 @end table
29365
29366 @item
29367
29368 @table @asis
29369
29370 @item @code{-fomit-frame-pointer}
29371
29372 do not set up separate stack frames
29373 @end table
29374
29375 @item
29376
29377 @table @asis
29378
29379 @item @code{-gnatp}
29380
29381 do not add runtime checks
29382 @end table
29383 @end itemize
29384
29385 This gives a human-readable assembler version of the code. The resulting
29386 file will have the same name as the Ada source file, but with a @code{.s}
29387 extension. In our example, the file @code{nothing.s} has the following
29388 contents:
29389
29390 @quotation
29391
29392 @example
29393 .file "nothing.adb"
29394 gcc2_compiled.:
29395 ___gnu_compiled_ada:
29396 .text
29397 .align 4
29398 .globl __ada_nothing
29399 __ada_nothing:
29400 #APP
29401 nop
29402 #NO_APP
29403 jmp L1
29404 .align 2,0x90
29405 L1:
29406 ret
29407 @end example
29408 @end quotation
29409
29410 The assembly code you included is clearly indicated by
29411 the compiler, between the @code{#APP} and @code{#NO_APP}
29412 delimiters. The character before the 'APP' and 'NOAPP'
29413 can differ on different targets. For example, GNU/Linux uses '#APP' while
29414 on NT you will see '/APP'.
29415
29416 If you make a mistake in your assembler code (such as using the
29417 wrong size modifier, or using a wrong operand for the instruction) GNAT
29418 will report this error in a temporary file, which will be deleted when
29419 the compilation is finished. Generating an assembler file will help
29420 in such cases, since you can assemble this file separately using the
29421 @code{as} assembler that comes with gcc.
29422
29423 Assembling the file using the command
29424
29425 @quotation
29426
29427 @example
29428 $ as nothing.s
29429 @end example
29430 @end quotation
29431
29432 will give you error messages whose lines correspond to the assembler
29433 input file, so you can easily find and correct any mistakes you made.
29434 If there are no errors, @code{as} will generate an object file
29435 @code{nothing.out}.
29436
29437 @node Output Variables in Inline Assembler,Input Variables in Inline Assembler,A Simple Example of Inline Assembler,Inline Assembler
29438 @anchor{gnat_ugn/inline_assembler id4}@anchor{24d}@anchor{gnat_ugn/inline_assembler output-variables-in-inline-assembler}@anchor{24e}
29439 @section Output Variables in Inline Assembler
29440
29441
29442 The examples in this section, showing how to access the processor flags,
29443 illustrate how to specify the destination operands for assembly language
29444 statements.
29445
29446 @quotation
29447
29448 @example
29449 with Interfaces; use Interfaces;
29450 with Ada.Text_IO; use Ada.Text_IO;
29451 with System.Machine_Code; use System.Machine_Code;
29452 procedure Get_Flags is
29453 Flags : Unsigned_32;
29454 use ASCII;
29455 begin
29456 Asm ("pushfl" & LF & HT & -- push flags on stack
29457 "popl %%eax" & LF & HT & -- load eax with flags
29458 "movl %%eax, %0", -- store flags in variable
29459 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29460 Put_Line ("Flags register:" & Flags'Img);
29461 end Get_Flags;
29462 @end example
29463 @end quotation
29464
29465 In order to have a nicely aligned assembly listing, we have separated
29466 multiple assembler statements in the Asm template string with linefeed
29467 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29468 The resulting section of the assembly output file is:
29469
29470 @quotation
29471
29472 @example
29473 #APP
29474 pushfl
29475 popl %eax
29476 movl %eax, -40(%ebp)
29477 #NO_APP
29478 @end example
29479 @end quotation
29480
29481 It would have been legal to write the Asm invocation as:
29482
29483 @quotation
29484
29485 @example
29486 Asm ("pushfl popl %%eax movl %%eax, %0")
29487 @end example
29488 @end quotation
29489
29490 but in the generated assembler file, this would come out as:
29491
29492 @quotation
29493
29494 @example
29495 #APP
29496 pushfl popl %eax movl %eax, -40(%ebp)
29497 #NO_APP
29498 @end example
29499 @end quotation
29500
29501 which is not so convenient for the human reader.
29502
29503 We use Ada comments
29504 at the end of each line to explain what the assembler instructions
29505 actually do. This is a useful convention.
29506
29507 When writing Inline Assembler instructions, you need to precede each register
29508 and variable name with a percent sign. Since the assembler already requires
29509 a percent sign at the beginning of a register name, you need two consecutive
29510 percent signs for such names in the Asm template string, thus @code{%%eax}.
29511 In the generated assembly code, one of the percent signs will be stripped off.
29512
29513 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29514 variables: operands you later define using @code{Input} or @code{Output}
29515 parameters to @code{Asm}.
29516 An output variable is illustrated in
29517 the third statement in the Asm template string:
29518
29519 @quotation
29520
29521 @example
29522 movl %%eax, %0
29523 @end example
29524 @end quotation
29525
29526 The intent is to store the contents of the eax register in a variable that can
29527 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29528 necessarily work, since the compiler might optimize by using a register
29529 to hold Flags, and the expansion of the @code{movl} instruction would not be
29530 aware of this optimization. The solution is not to store the result directly
29531 but rather to advise the compiler to choose the correct operand form;
29532 that is the purpose of the @code{%0} output variable.
29533
29534 Information about the output variable is supplied in the @code{Outputs}
29535 parameter to @code{Asm}:
29536
29537 @quotation
29538
29539 @example
29540 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29541 @end example
29542 @end quotation
29543
29544 The output is defined by the @code{Asm_Output} attribute of the target type;
29545 the general format is
29546
29547 @quotation
29548
29549 @example
29550 Type'Asm_Output (constraint_string, variable_name)
29551 @end example
29552 @end quotation
29553
29554 The constraint string directs the compiler how
29555 to store/access the associated variable. In the example
29556
29557 @quotation
29558
29559 @example
29560 Unsigned_32'Asm_Output ("=m", Flags);
29561 @end example
29562 @end quotation
29563
29564 the @code{"m"} (memory) constraint tells the compiler that the variable
29565 @code{Flags} should be stored in a memory variable, thus preventing
29566 the optimizer from keeping it in a register. In contrast,
29567
29568 @quotation
29569
29570 @example
29571 Unsigned_32'Asm_Output ("=r", Flags);
29572 @end example
29573 @end quotation
29574
29575 uses the @code{"r"} (register) constraint, telling the compiler to
29576 store the variable in a register.
29577
29578 If the constraint is preceded by the equal character '=', it tells
29579 the compiler that the variable will be used to store data into it.
29580
29581 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29582 allowing the optimizer to choose whatever it deems best.
29583
29584 There are a fairly large number of constraints, but the ones that are
29585 most useful (for the Intel x86 processor) are the following:
29586
29587 @quotation
29588
29589
29590 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
29591 @item
29592
29593 @emph{=}
29594
29595 @tab
29596
29597 output constraint
29598
29599 @item
29600
29601 @emph{g}
29602
29603 @tab
29604
29605 global (i.e., can be stored anywhere)
29606
29607 @item
29608
29609 @emph{m}
29610
29611 @tab
29612
29613 in memory
29614
29615 @item
29616
29617 @emph{I}
29618
29619 @tab
29620
29621 a constant
29622
29623 @item
29624
29625 @emph{a}
29626
29627 @tab
29628
29629 use eax
29630
29631 @item
29632
29633 @emph{b}
29634
29635 @tab
29636
29637 use ebx
29638
29639 @item
29640
29641 @emph{c}
29642
29643 @tab
29644
29645 use ecx
29646
29647 @item
29648
29649 @emph{d}
29650
29651 @tab
29652
29653 use edx
29654
29655 @item
29656
29657 @emph{S}
29658
29659 @tab
29660
29661 use esi
29662
29663 @item
29664
29665 @emph{D}
29666
29667 @tab
29668
29669 use edi
29670
29671 @item
29672
29673 @emph{r}
29674
29675 @tab
29676
29677 use one of eax, ebx, ecx or edx
29678
29679 @item
29680
29681 @emph{q}
29682
29683 @tab
29684
29685 use one of eax, ebx, ecx, edx, esi or edi
29686
29687 @end multitable
29688
29689 @end quotation
29690
29691 The full set of constraints is described in the gcc and @code{as}
29692 documentation; note that it is possible to combine certain constraints
29693 in one constraint string.
29694
29695 You specify the association of an output variable with an assembler operand
29696 through the @code{%@emph{n}} notation, where @emph{n} is a non-negative
29697 integer. Thus in
29698
29699 @quotation
29700
29701 @example
29702 Asm ("pushfl" & LF & HT & -- push flags on stack
29703 "popl %%eax" & LF & HT & -- load eax with flags
29704 "movl %%eax, %0", -- store flags in variable
29705 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29706 @end example
29707 @end quotation
29708
29709 @code{%0} will be replaced in the expanded code by the appropriate operand,
29710 whatever
29711 the compiler decided for the @code{Flags} variable.
29712
29713 In general, you may have any number of output variables:
29714
29715
29716 @itemize *
29717
29718 @item
29719 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29720
29721 @item
29722 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29723 of @code{Asm_Output} attributes
29724 @end itemize
29725
29726 For example:
29727
29728 @quotation
29729
29730 @example
29731 Asm ("movl %%eax, %0" & LF & HT &
29732 "movl %%ebx, %1" & LF & HT &
29733 "movl %%ecx, %2",
29734 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29735 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29736 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29737 @end example
29738 @end quotation
29739
29740 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29741 in the Ada program.
29742
29743 As a variation on the @code{Get_Flags} example, we can use the constraints
29744 string to direct the compiler to store the eax register into the @code{Flags}
29745 variable, instead of including the store instruction explicitly in the
29746 @code{Asm} template string:
29747
29748 @quotation
29749
29750 @example
29751 with Interfaces; use Interfaces;
29752 with Ada.Text_IO; use Ada.Text_IO;
29753 with System.Machine_Code; use System.Machine_Code;
29754 procedure Get_Flags_2 is
29755 Flags : Unsigned_32;
29756 use ASCII;
29757 begin
29758 Asm ("pushfl" & LF & HT & -- push flags on stack
29759 "popl %%eax", -- save flags in eax
29760 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29761 Put_Line ("Flags register:" & Flags'Img);
29762 end Get_Flags_2;
29763 @end example
29764 @end quotation
29765
29766 The @code{"a"} constraint tells the compiler that the @code{Flags}
29767 variable will come from the eax register. Here is the resulting code:
29768
29769 @quotation
29770
29771 @example
29772 #APP
29773 pushfl
29774 popl %eax
29775 #NO_APP
29776 movl %eax,-40(%ebp)
29777 @end example
29778 @end quotation
29779
29780 The compiler generated the store of eax into Flags after
29781 expanding the assembler code.
29782
29783 Actually, there was no need to pop the flags into the eax register;
29784 more simply, we could just pop the flags directly into the program variable:
29785
29786 @quotation
29787
29788 @example
29789 with Interfaces; use Interfaces;
29790 with Ada.Text_IO; use Ada.Text_IO;
29791 with System.Machine_Code; use System.Machine_Code;
29792 procedure Get_Flags_3 is
29793 Flags : Unsigned_32;
29794 use ASCII;
29795 begin
29796 Asm ("pushfl" & LF & HT & -- push flags on stack
29797 "pop %0", -- save flags in Flags
29798 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29799 Put_Line ("Flags register:" & Flags'Img);
29800 end Get_Flags_3;
29801 @end example
29802 @end quotation
29803
29804 @node Input Variables in Inline Assembler,Inlining Inline Assembler Code,Output Variables in Inline Assembler,Inline Assembler
29805 @anchor{gnat_ugn/inline_assembler id5}@anchor{24f}@anchor{gnat_ugn/inline_assembler input-variables-in-inline-assembler}@anchor{250}
29806 @section Input Variables in Inline Assembler
29807
29808
29809 The example in this section illustrates how to specify the source operands
29810 for assembly language statements.
29811 The program simply increments its input value by 1:
29812
29813 @quotation
29814
29815 @example
29816 with Interfaces; use Interfaces;
29817 with Ada.Text_IO; use Ada.Text_IO;
29818 with System.Machine_Code; use System.Machine_Code;
29819 procedure Increment is
29820
29821 function Incr (Value : Unsigned_32) return Unsigned_32 is
29822 Result : Unsigned_32;
29823 begin
29824 Asm ("incl %0",
29825 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29826 Inputs => Unsigned_32'Asm_Input ("a", Value));
29827 return Result;
29828 end Incr;
29829
29830 Value : Unsigned_32;
29831
29832 begin
29833 Value := 5;
29834 Put_Line ("Value before is" & Value'Img);
29835 Value := Incr (Value);
29836 Put_Line ("Value after is" & Value'Img);
29837 end Increment;
29838 @end example
29839 @end quotation
29840
29841 The @code{Outputs} parameter to @code{Asm} specifies
29842 that the result will be in the eax register and that it is to be stored
29843 in the @code{Result} variable.
29844
29845 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29846 but with an @code{Asm_Input} attribute.
29847 The @code{"="} constraint, indicating an output value, is not present.
29848
29849 You can have multiple input variables, in the same way that you can have more
29850 than one output variable.
29851
29852 The parameter count (%0, %1) etc, still starts at the first output statement,
29853 and continues with the input statements.
29854
29855 Just as the @code{Outputs} parameter causes the register to be stored into the
29856 target variable after execution of the assembler statements, so does the
29857 @code{Inputs} parameter cause its variable to be loaded into the register
29858 before execution of the assembler statements.
29859
29860 Thus the effect of the @code{Asm} invocation is:
29861
29862
29863 @itemize *
29864
29865 @item
29866 load the 32-bit value of @code{Value} into eax
29867
29868 @item
29869 execute the @code{incl %eax} instruction
29870
29871 @item
29872 store the contents of eax into the @code{Result} variable
29873 @end itemize
29874
29875 The resulting assembler file (with @code{-O2} optimization) contains:
29876
29877 @quotation
29878
29879 @example
29880 _increment__incr.1:
29881 subl $4,%esp
29882 movl 8(%esp),%eax
29883 #APP
29884 incl %eax
29885 #NO_APP
29886 movl %eax,%edx
29887 movl %ecx,(%esp)
29888 addl $4,%esp
29889 ret
29890 @end example
29891 @end quotation
29892
29893 @node Inlining Inline Assembler Code,Other Asm Functionality,Input Variables in Inline Assembler,Inline Assembler
29894 @anchor{gnat_ugn/inline_assembler id6}@anchor{251}@anchor{gnat_ugn/inline_assembler inlining-inline-assembler-code}@anchor{252}
29895 @section Inlining Inline Assembler Code
29896
29897
29898 For a short subprogram such as the @code{Incr} function in the previous
29899 section, the overhead of the call and return (creating / deleting the stack
29900 frame) can be significant, compared to the amount of code in the subprogram
29901 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29902 which directs the compiler to expand invocations of the subprogram at the
29903 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29904 Here is the resulting program:
29905
29906 @quotation
29907
29908 @example
29909 with Interfaces; use Interfaces;
29910 with Ada.Text_IO; use Ada.Text_IO;
29911 with System.Machine_Code; use System.Machine_Code;
29912 procedure Increment_2 is
29913
29914 function Incr (Value : Unsigned_32) return Unsigned_32 is
29915 Result : Unsigned_32;
29916 begin
29917 Asm ("incl %0",
29918 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29919 Inputs => Unsigned_32'Asm_Input ("a", Value));
29920 return Result;
29921 end Incr;
29922 pragma Inline (Increment);
29923
29924 Value : Unsigned_32;
29925
29926 begin
29927 Value := 5;
29928 Put_Line ("Value before is" & Value'Img);
29929 Value := Increment (Value);
29930 Put_Line ("Value after is" & Value'Img);
29931 end Increment_2;
29932 @end example
29933 @end quotation
29934
29935 Compile the program with both optimization (@code{-O2}) and inlining
29936 (@code{-gnatn}) enabled.
29937
29938 The @code{Incr} function is still compiled as usual, but at the
29939 point in @code{Increment} where our function used to be called:
29940
29941 @quotation
29942
29943 @example
29944 pushl %edi
29945 call _increment__incr.1
29946 @end example
29947 @end quotation
29948
29949 the code for the function body directly appears:
29950
29951 @quotation
29952
29953 @example
29954 movl %esi,%eax
29955 #APP
29956 incl %eax
29957 #NO_APP
29958 movl %eax,%edx
29959 @end example
29960 @end quotation
29961
29962 thus saving the overhead of stack frame setup and an out-of-line call.
29963
29964 @node Other Asm Functionality,,Inlining Inline Assembler Code,Inline Assembler
29965 @anchor{gnat_ugn/inline_assembler other-asm-functionality}@anchor{253}@anchor{gnat_ugn/inline_assembler id7}@anchor{254}
29966 @section Other @code{Asm} Functionality
29967
29968
29969 This section describes two important parameters to the @code{Asm}
29970 procedure: @code{Clobber}, which identifies register usage;
29971 and @code{Volatile}, which inhibits unwanted optimizations.
29972
29973 @menu
29974 * The Clobber Parameter::
29975 * The Volatile Parameter::
29976
29977 @end menu
29978
29979 @node The Clobber Parameter,The Volatile Parameter,,Other Asm Functionality
29980 @anchor{gnat_ugn/inline_assembler the-clobber-parameter}@anchor{255}@anchor{gnat_ugn/inline_assembler id8}@anchor{256}
29981 @subsection The @code{Clobber} Parameter
29982
29983
29984 One of the dangers of intermixing assembly language and a compiled language
29985 such as Ada is that the compiler needs to be aware of which registers are
29986 being used by the assembly code. In some cases, such as the earlier examples,
29987 the constraint string is sufficient to indicate register usage (e.g.,
29988 @code{"a"} for
29989 the eax register). But more generally, the compiler needs an explicit
29990 identification of the registers that are used by the Inline Assembly
29991 statements.
29992
29993 Using a register that the compiler doesn't know about
29994 could be a side effect of an instruction (like @code{mull}
29995 storing its result in both eax and edx).
29996 It can also arise from explicit register usage in your
29997 assembly code; for example:
29998
29999 @quotation
30000
30001 @example
30002 Asm ("movl %0, %%ebx" & LF & HT &
30003 "movl %%ebx, %1",
30004 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30005 Inputs => Unsigned_32'Asm_Input ("g", Var_In));
30006 @end example
30007 @end quotation
30008
30009 where the compiler (since it does not analyze the @code{Asm} template string)
30010 does not know you are using the ebx register.
30011
30012 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
30013 to identify the registers that will be used by your assembly code:
30014
30015 @quotation
30016
30017 @example
30018 Asm ("movl %0, %%ebx" & LF & HT &
30019 "movl %%ebx, %1",
30020 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30021 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30022 Clobber => "ebx");
30023 @end example
30024 @end quotation
30025
30026 The Clobber parameter is a static string expression specifying the
30027 register(s) you are using. Note that register names are @emph{not} prefixed
30028 by a percent sign. Also, if more than one register is used then their names
30029 are separated by commas; e.g., @code{"eax, ebx"}
30030
30031 The @code{Clobber} parameter has several additional uses:
30032
30033
30034 @itemize *
30035
30036 @item
30037 Use 'register' name @code{cc} to indicate that flags might have changed
30038
30039 @item
30040 Use 'register' name @code{memory} if you changed a memory location
30041 @end itemize
30042
30043 @node The Volatile Parameter,,The Clobber Parameter,Other Asm Functionality
30044 @anchor{gnat_ugn/inline_assembler the-volatile-parameter}@anchor{257}@anchor{gnat_ugn/inline_assembler id9}@anchor{258}
30045 @subsection The @code{Volatile} Parameter
30046
30047
30048 @geindex Volatile parameter
30049
30050 Compiler optimizations in the presence of Inline Assembler may sometimes have
30051 unwanted effects. For example, when an @code{Asm} invocation with an input
30052 variable is inside a loop, the compiler might move the loading of the input
30053 variable outside the loop, regarding it as a one-time initialization.
30054
30055 If this effect is not desired, you can disable such optimizations by setting
30056 the @code{Volatile} parameter to @code{True}; for example:
30057
30058 @quotation
30059
30060 @example
30061 Asm ("movl %0, %%ebx" & LF & HT &
30062 "movl %%ebx, %1",
30063 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30064 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30065 Clobber => "ebx",
30066 Volatile => True);
30067 @end example
30068 @end quotation
30069
30070 By default, @code{Volatile} is set to @code{False} unless there is no
30071 @code{Outputs} parameter.
30072
30073 Although setting @code{Volatile} to @code{True} prevents unwanted
30074 optimizations, it will also disable other optimizations that might be
30075 important for efficiency. In general, you should set @code{Volatile}
30076 to @code{True} only if the compiler's optimizations have created
30077 problems.
30078
30079 @node GNU Free Documentation License,Index,Inline Assembler,Top
30080 @anchor{share/gnu_free_documentation_license gnu-fdl}@anchor{1}@anchor{share/gnu_free_documentation_license doc}@anchor{259}@anchor{share/gnu_free_documentation_license gnu-free-documentation-license}@anchor{25a}
30081 @chapter GNU Free Documentation License
30082
30083
30084 Version 1.3, 3 November 2008
30085
30086 Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc
30087 @indicateurl{http://fsf.org/}
30088
30089 Everyone is permitted to copy and distribute verbatim copies of this
30090 license document, but changing it is not allowed.
30091
30092 @strong{Preamble}
30093
30094 The purpose of this License is to make a manual, textbook, or other
30095 functional and useful document "free" in the sense of freedom: to
30096 assure everyone the effective freedom to copy and redistribute it,
30097 with or without modifying it, either commercially or noncommercially.
30098 Secondarily, this License preserves for the author and publisher a way
30099 to get credit for their work, while not being considered responsible
30100 for modifications made by others.
30101
30102 This License is a kind of "copyleft", which means that derivative
30103 works of the document must themselves be free in the same sense. It
30104 complements the GNU General Public License, which is a copyleft
30105 license designed for free software.
30106
30107 We have designed this License in order to use it for manuals for free
30108 software, because free software needs free documentation: a free
30109 program should come with manuals providing the same freedoms that the
30110 software does. But this License is not limited to software manuals;
30111 it can be used for any textual work, regardless of subject matter or
30112 whether it is published as a printed book. We recommend this License
30113 principally for works whose purpose is instruction or reference.
30114
30115 @strong{1. APPLICABILITY AND DEFINITIONS}
30116
30117 This License applies to any manual or other work, in any medium, that
30118 contains a notice placed by the copyright holder saying it can be
30119 distributed under the terms of this License. Such a notice grants a
30120 world-wide, royalty-free license, unlimited in duration, to use that
30121 work under the conditions stated herein. The @strong{Document}, below,
30122 refers to any such manual or work. Any member of the public is a
30123 licensee, and is addressed as "@strong{you}". You accept the license if you
30124 copy, modify or distribute the work in a way requiring permission
30125 under copyright law.
30126
30127 A "@strong{Modified Version}" of the Document means any work containing the
30128 Document or a portion of it, either copied verbatim, or with
30129 modifications and/or translated into another language.
30130
30131 A "@strong{Secondary Section}" is a named appendix or a front-matter section of
30132 the Document that deals exclusively with the relationship of the
30133 publishers or authors of the Document to the Document's overall subject
30134 (or to related matters) and contains nothing that could fall directly
30135 within that overall subject. (Thus, if the Document is in part a
30136 textbook of mathematics, a Secondary Section may not explain any
30137 mathematics.) The relationship could be a matter of historical
30138 connection with the subject or with related matters, or of legal,
30139 commercial, philosophical, ethical or political position regarding
30140 them.
30141
30142 The "@strong{Invariant Sections}" are certain Secondary Sections whose titles
30143 are designated, as being those of Invariant Sections, in the notice
30144 that says that the Document is released under this License. If a
30145 section does not fit the above definition of Secondary then it is not
30146 allowed to be designated as Invariant. The Document may contain zero
30147 Invariant Sections. If the Document does not identify any Invariant
30148 Sections then there are none.
30149
30150 The "@strong{Cover Texts}" are certain short passages of text that are listed,
30151 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
30152 the Document is released under this License. A Front-Cover Text may
30153 be at most 5 words, and a Back-Cover Text may be at most 25 words.
30154
30155 A "@strong{Transparent}" copy of the Document means a machine-readable copy,
30156 represented in a format whose specification is available to the
30157 general public, that is suitable for revising the document
30158 straightforwardly with generic text editors or (for images composed of
30159 pixels) generic paint programs or (for drawings) some widely available
30160 drawing editor, and that is suitable for input to text formatters or
30161 for automatic translation to a variety of formats suitable for input
30162 to text formatters. A copy made in an otherwise Transparent file
30163 format whose markup, or absence of markup, has been arranged to thwart
30164 or discourage subsequent modification by readers is not Transparent.
30165 An image format is not Transparent if used for any substantial amount
30166 of text. A copy that is not "Transparent" is called @strong{Opaque}.
30167
30168 Examples of suitable formats for Transparent copies include plain
30169 ASCII without markup, Texinfo input format, LaTeX input format, SGML
30170 or XML using a publicly available DTD, and standard-conforming simple
30171 HTML, PostScript or PDF designed for human modification. Examples of
30172 transparent image formats include PNG, XCF and JPG. Opaque formats
30173 include proprietary formats that can be read and edited only by
30174 proprietary word processors, SGML or XML for which the DTD and/or
30175 processing tools are not generally available, and the
30176 machine-generated HTML, PostScript or PDF produced by some word
30177 processors for output purposes only.
30178
30179 The "@strong{Title Page}" means, for a printed book, the title page itself,
30180 plus such following pages as are needed to hold, legibly, the material
30181 this License requires to appear in the title page. For works in
30182 formats which do not have any title page as such, "Title Page" means
30183 the text near the most prominent appearance of the work's title,
30184 preceding the beginning of the body of the text.
30185
30186 The "@strong{publisher}" means any person or entity that distributes
30187 copies of the Document to the public.
30188
30189 A section "@strong{Entitled XYZ}" means a named subunit of the Document whose
30190 title either is precisely XYZ or contains XYZ in parentheses following
30191 text that translates XYZ in another language. (Here XYZ stands for a
30192 specific section name mentioned below, such as "@strong{Acknowledgements}",
30193 "@strong{Dedications}", "@strong{Endorsements}", or "@strong{History}".)
30194 To "@strong{Preserve the Title}"
30195 of such a section when you modify the Document means that it remains a
30196 section "Entitled XYZ" according to this definition.
30197
30198 The Document may include Warranty Disclaimers next to the notice which
30199 states that this License applies to the Document. These Warranty
30200 Disclaimers are considered to be included by reference in this
30201 License, but only as regards disclaiming warranties: any other
30202 implication that these Warranty Disclaimers may have is void and has
30203 no effect on the meaning of this License.
30204
30205 @strong{2. VERBATIM COPYING}
30206
30207 You may copy and distribute the Document in any medium, either
30208 commercially or noncommercially, provided that this License, the
30209 copyright notices, and the license notice saying this License applies
30210 to the Document are reproduced in all copies, and that you add no other
30211 conditions whatsoever to those of this License. You may not use
30212 technical measures to obstruct or control the reading or further
30213 copying of the copies you make or distribute. However, you may accept
30214 compensation in exchange for copies. If you distribute a large enough
30215 number of copies you must also follow the conditions in section 3.
30216
30217 You may also lend copies, under the same conditions stated above, and
30218 you may publicly display copies.
30219
30220 @strong{3. COPYING IN QUANTITY}
30221
30222 If you publish printed copies (or copies in media that commonly have
30223 printed covers) of the Document, numbering more than 100, and the
30224 Document's license notice requires Cover Texts, you must enclose the
30225 copies in covers that carry, clearly and legibly, all these Cover
30226 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
30227 the back cover. Both covers must also clearly and legibly identify
30228 you as the publisher of these copies. The front cover must present
30229 the full title with all words of the title equally prominent and
30230 visible. You may add other material on the covers in addition.
30231 Copying with changes limited to the covers, as long as they preserve
30232 the title of the Document and satisfy these conditions, can be treated
30233 as verbatim copying in other respects.
30234
30235 If the required texts for either cover are too voluminous to fit
30236 legibly, you should put the first ones listed (as many as fit
30237 reasonably) on the actual cover, and continue the rest onto adjacent
30238 pages.
30239
30240 If you publish or distribute Opaque copies of the Document numbering
30241 more than 100, you must either include a machine-readable Transparent
30242 copy along with each Opaque copy, or state in or with each Opaque copy
30243 a computer-network location from which the general network-using
30244 public has access to download using public-standard network protocols
30245 a complete Transparent copy of the Document, free of added material.
30246 If you use the latter option, you must take reasonably prudent steps,
30247 when you begin distribution of Opaque copies in quantity, to ensure
30248 that this Transparent copy will remain thus accessible at the stated
30249 location until at least one year after the last time you distribute an
30250 Opaque copy (directly or through your agents or retailers) of that
30251 edition to the public.
30252
30253 It is requested, but not required, that you contact the authors of the
30254 Document well before redistributing any large number of copies, to give
30255 them a chance to provide you with an updated version of the Document.
30256
30257 @strong{4. MODIFICATIONS}
30258
30259 You may copy and distribute a Modified Version of the Document under
30260 the conditions of sections 2 and 3 above, provided that you release
30261 the Modified Version under precisely this License, with the Modified
30262 Version filling the role of the Document, thus licensing distribution
30263 and modification of the Modified Version to whoever possesses a copy
30264 of it. In addition, you must do these things in the Modified Version:
30265
30266
30267 @enumerate A
30268
30269 @item
30270 Use in the Title Page (and on the covers, if any) a title distinct
30271 from that of the Document, and from those of previous versions
30272 (which should, if there were any, be listed in the History section
30273 of the Document). You may use the same title as a previous version
30274 if the original publisher of that version gives permission.
30275
30276 @item
30277 List on the Title Page, as authors, one or more persons or entities
30278 responsible for authorship of the modifications in the Modified
30279 Version, together with at least five of the principal authors of the
30280 Document (all of its principal authors, if it has fewer than five),
30281 unless they release you from this requirement.
30282
30283 @item
30284 State on the Title page the name of the publisher of the
30285 Modified Version, as the publisher.
30286
30287 @item
30288 Preserve all the copyright notices of the Document.
30289
30290 @item
30291 Add an appropriate copyright notice for your modifications
30292 adjacent to the other copyright notices.
30293
30294 @item
30295 Include, immediately after the copyright notices, a license notice
30296 giving the public permission to use the Modified Version under the
30297 terms of this License, in the form shown in the Addendum below.
30298
30299 @item
30300 Preserve in that license notice the full lists of Invariant Sections
30301 and required Cover Texts given in the Document's license notice.
30302
30303 @item
30304 Include an unaltered copy of this License.
30305
30306 @item
30307 Preserve the section Entitled "History", Preserve its Title, and add
30308 to it an item stating at least the title, year, new authors, and
30309 publisher of the Modified Version as given on the Title Page. If
30310 there is no section Entitled "History" in the Document, create one
30311 stating the title, year, authors, and publisher of the Document as
30312 given on its Title Page, then add an item describing the Modified
30313 Version as stated in the previous sentence.
30314
30315 @item
30316 Preserve the network location, if any, given in the Document for
30317 public access to a Transparent copy of the Document, and likewise
30318 the network locations given in the Document for previous versions
30319 it was based on. These may be placed in the "History" section.
30320 You may omit a network location for a work that was published at
30321 least four years before the Document itself, or if the original
30322 publisher of the version it refers to gives permission.
30323
30324 @item
30325 For any section Entitled "Acknowledgements" or "Dedications",
30326 Preserve the Title of the section, and preserve in the section all
30327 the substance and tone of each of the contributor acknowledgements
30328 and/or dedications given therein.
30329
30330 @item
30331 Preserve all the Invariant Sections of the Document,
30332 unaltered in their text and in their titles. Section numbers
30333 or the equivalent are not considered part of the section titles.
30334
30335 @item
30336 Delete any section Entitled "Endorsements". Such a section
30337 may not be included in the Modified Version.
30338
30339 @item
30340 Do not retitle any existing section to be Entitled "Endorsements"
30341 or to conflict in title with any Invariant Section.
30342
30343 @item
30344 Preserve any Warranty Disclaimers.
30345 @end enumerate
30346
30347 If the Modified Version includes new front-matter sections or
30348 appendices that qualify as Secondary Sections and contain no material
30349 copied from the Document, you may at your option designate some or all
30350 of these sections as invariant. To do this, add their titles to the
30351 list of Invariant Sections in the Modified Version's license notice.
30352 These titles must be distinct from any other section titles.
30353
30354 You may add a section Entitled "Endorsements", provided it contains
30355 nothing but endorsements of your Modified Version by various
30356 parties---for example, statements of peer review or that the text has
30357 been approved by an organization as the authoritative definition of a
30358 standard.
30359
30360 You may add a passage of up to five words as a Front-Cover Text, and a
30361 passage of up to 25 words as a Back-Cover Text, to the end of the list
30362 of Cover Texts in the Modified Version. Only one passage of
30363 Front-Cover Text and one of Back-Cover Text may be added by (or
30364 through arrangements made by) any one entity. If the Document already
30365 includes a cover text for the same cover, previously added by you or
30366 by arrangement made by the same entity you are acting on behalf of,
30367 you may not add another; but you may replace the old one, on explicit
30368 permission from the previous publisher that added the old one.
30369
30370 The author(s) and publisher(s) of the Document do not by this License
30371 give permission to use their names for publicity for or to assert or
30372 imply endorsement of any Modified Version.
30373
30374 @strong{5. COMBINING DOCUMENTS}
30375
30376 You may combine the Document with other documents released under this
30377 License, under the terms defined in section 4 above for modified
30378 versions, provided that you include in the combination all of the
30379 Invariant Sections of all of the original documents, unmodified, and
30380 list them all as Invariant Sections of your combined work in its
30381 license notice, and that you preserve all their Warranty Disclaimers.
30382
30383 The combined work need only contain one copy of this License, and
30384 multiple identical Invariant Sections may be replaced with a single
30385 copy. If there are multiple Invariant Sections with the same name but
30386 different contents, make the title of each such section unique by
30387 adding at the end of it, in parentheses, the name of the original
30388 author or publisher of that section if known, or else a unique number.
30389 Make the same adjustment to the section titles in the list of
30390 Invariant Sections in the license notice of the combined work.
30391
30392 In the combination, you must combine any sections Entitled "History"
30393 in the various original documents, forming one section Entitled
30394 "History"; likewise combine any sections Entitled "Acknowledgements",
30395 and any sections Entitled "Dedications". You must delete all sections
30396 Entitled "Endorsements".
30397
30398 @strong{6. COLLECTIONS OF DOCUMENTS}
30399
30400 You may make a collection consisting of the Document and other documents
30401 released under this License, and replace the individual copies of this
30402 License in the various documents with a single copy that is included in
30403 the collection, provided that you follow the rules of this License for
30404 verbatim copying of each of the documents in all other respects.
30405
30406 You may extract a single document from such a collection, and distribute
30407 it individually under this License, provided you insert a copy of this
30408 License into the extracted document, and follow this License in all
30409 other respects regarding verbatim copying of that document.
30410
30411 @strong{7. AGGREGATION WITH INDEPENDENT WORKS}
30412
30413 A compilation of the Document or its derivatives with other separate
30414 and independent documents or works, in or on a volume of a storage or
30415 distribution medium, is called an "aggregate" if the copyright
30416 resulting from the compilation is not used to limit the legal rights
30417 of the compilation's users beyond what the individual works permit.
30418 When the Document is included in an aggregate, this License does not
30419 apply to the other works in the aggregate which are not themselves
30420 derivative works of the Document.
30421
30422 If the Cover Text requirement of section 3 is applicable to these
30423 copies of the Document, then if the Document is less than one half of
30424 the entire aggregate, the Document's Cover Texts may be placed on
30425 covers that bracket the Document within the aggregate, or the
30426 electronic equivalent of covers if the Document is in electronic form.
30427 Otherwise they must appear on printed covers that bracket the whole
30428 aggregate.
30429
30430 @strong{8. TRANSLATION}
30431
30432 Translation is considered a kind of modification, so you may
30433 distribute translations of the Document under the terms of section 4.
30434 Replacing Invariant Sections with translations requires special
30435 permission from their copyright holders, but you may include
30436 translations of some or all Invariant Sections in addition to the
30437 original versions of these Invariant Sections. You may include a
30438 translation of this License, and all the license notices in the
30439 Document, and any Warranty Disclaimers, provided that you also include
30440 the original English version of this License and the original versions
30441 of those notices and disclaimers. In case of a disagreement between
30442 the translation and the original version of this License or a notice
30443 or disclaimer, the original version will prevail.
30444
30445 If a section in the Document is Entitled "Acknowledgements",
30446 "Dedications", or "History", the requirement (section 4) to Preserve
30447 its Title (section 1) will typically require changing the actual
30448 title.
30449
30450 @strong{9. TERMINATION}
30451
30452 You may not copy, modify, sublicense, or distribute the Document
30453 except as expressly provided under this License. Any attempt
30454 otherwise to copy, modify, sublicense, or distribute it is void, and
30455 will automatically terminate your rights under this License.
30456
30457 However, if you cease all violation of this License, then your license
30458 from a particular copyright holder is reinstated (a) provisionally,
30459 unless and until the copyright holder explicitly and finally
30460 terminates your license, and (b) permanently, if the copyright holder
30461 fails to notify you of the violation by some reasonable means prior to
30462 60 days after the cessation.
30463
30464 Moreover, your license from a particular copyright holder is
30465 reinstated permanently if the copyright holder notifies you of the
30466 violation by some reasonable means, this is the first time you have
30467 received notice of violation of this License (for any work) from that
30468 copyright holder, and you cure the violation prior to 30 days after
30469 your receipt of the notice.
30470
30471 Termination of your rights under this section does not terminate the
30472 licenses of parties who have received copies or rights from you under
30473 this License. If your rights have been terminated and not permanently
30474 reinstated, receipt of a copy of some or all of the same material does
30475 not give you any rights to use it.
30476
30477 @strong{10. FUTURE REVISIONS OF THIS LICENSE}
30478
30479 The Free Software Foundation may publish new, revised versions
30480 of the GNU Free Documentation License from time to time. Such new
30481 versions will be similar in spirit to the present version, but may
30482 differ in detail to address new problems or concerns. See
30483 @indicateurl{http://www.gnu.org/copyleft/}.
30484
30485 Each version of the License is given a distinguishing version number.
30486 If the Document specifies that a particular numbered version of this
30487 License "or any later version" applies to it, you have the option of
30488 following the terms and conditions either of that specified version or
30489 of any later version that has been published (not as a draft) by the
30490 Free Software Foundation. If the Document does not specify a version
30491 number of this License, you may choose any version ever published (not
30492 as a draft) by the Free Software Foundation. If the Document
30493 specifies that a proxy can decide which future versions of this
30494 License can be used, that proxy's public statement of acceptance of a
30495 version permanently authorizes you to choose that version for the
30496 Document.
30497
30498 @strong{11. RELICENSING}
30499
30500 "Massive Multiauthor Collaboration Site" (or "MMC Site") means any
30501 World Wide Web server that publishes copyrightable works and also
30502 provides prominent facilities for anybody to edit those works. A
30503 public wiki that anybody can edit is an example of such a server. A
30504 "Massive Multiauthor Collaboration" (or "MMC") contained in the
30505 site means any set of copyrightable works thus published on the MMC
30506 site.
30507
30508 "CC-BY-SA" means the Creative Commons Attribution-Share Alike 3.0
30509 license published by Creative Commons Corporation, a not-for-profit
30510 corporation with a principal place of business in San Francisco,
30511 California, as well as future copyleft versions of that license
30512 published by that same organization.
30513
30514 "Incorporate" means to publish or republish a Document, in whole or
30515 in part, as part of another Document.
30516
30517 An MMC is "eligible for relicensing" if it is licensed under this
30518 License, and if all works that were first published under this License
30519 somewhere other than this MMC, and subsequently incorporated in whole
30520 or in part into the MMC, (1) had no cover texts or invariant sections,
30521 and (2) were thus incorporated prior to November 1, 2008.
30522
30523 The operator of an MMC Site may republish an MMC contained in the site
30524 under CC-BY-SA on the same site at any time before August 1, 2009,
30525 provided the MMC is eligible for relicensing.
30526
30527 @strong{ADDENDUM: How to use this License for your documents}
30528
30529 To use this License in a document you have written, include a copy of
30530 the License in the document and put the following copyright and
30531 license notices just after the title page:
30532
30533 @quotation
30534
30535 Copyright © YEAR YOUR NAME.
30536 Permission is granted to copy, distribute and/or modify this document
30537 under the terms of the GNU Free Documentation License, Version 1.3
30538 or any later version published by the Free Software Foundation;
30539 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
30540 A copy of the license is included in the section entitled "GNU
30541 Free Documentation License".
30542 @end quotation
30543
30544 If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
30545 replace the "with ... Texts." line with this:
30546
30547 @quotation
30548
30549 with the Invariant Sections being LIST THEIR TITLES, with the
30550 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
30551 @end quotation
30552
30553 If you have Invariant Sections without Cover Texts, or some other
30554 combination of the three, merge those two alternatives to suit the
30555 situation.
30556
30557 If your document contains nontrivial examples of program code, we
30558 recommend releasing these examples in parallel under your choice of
30559 free software license, such as the GNU General Public License,
30560 to permit their use in free software.
30561
30562 @node Index,,GNU Free Documentation License,Top
30563 @unnumbered Index
30564
30565
30566 @printindex ge
30567
30568 @anchor{de}@w{ }
30569 @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ }
30570
30571 @c %**end of body
30572 @bye