]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/gnat_ugn.texi
d28d53dc7fa65e1020a59f92f140d1c4215d5e4d
[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 , Nov 09, 2018
25
26 AdaCore
27
28 Copyright @copyright{} 2008-2018, 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 * Mixed-Language Programming on Windows::
467 * Windows Specific Add-Ons::
468
469 Mixed-Language Programming on Windows
470
471 * Windows Calling Conventions::
472 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
473 * Using DLLs with GNAT::
474 * Building DLLs with GNAT Project files::
475 * Building DLLs with GNAT::
476 * Building DLLs with gnatdll::
477 * Ada DLLs and Finalization::
478 * Creating a Spec for Ada DLLs::
479 * GNAT and Windows Resources::
480 * Using GNAT DLLs from Microsoft Visual Studio Applications::
481 * Debugging a DLL::
482 * Setting Stack Size from gnatlink::
483 * Setting Heap Size from gnatlink::
484
485 Windows Calling Conventions
486
487 * C Calling Convention::
488 * Stdcall Calling Convention::
489 * Win32 Calling Convention::
490 * DLL Calling Convention::
491
492 Using DLLs with GNAT
493
494 * Creating an Ada Spec for the DLL Services::
495 * Creating an Import Library::
496
497 Building DLLs with gnatdll
498
499 * Limitations When Using Ada DLLs from Ada::
500 * Exporting Ada Entities::
501 * Ada DLLs and Elaboration::
502
503 Creating a Spec for Ada DLLs
504
505 * Creating the Definition File::
506 * Using gnatdll::
507
508 GNAT and Windows Resources
509
510 * Building Resources::
511 * Compiling Resources::
512 * Using Resources::
513
514 Debugging a DLL
515
516 * Program and DLL Both Built with GCC/GNAT::
517 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
518
519 Windows Specific Add-Ons
520
521 * Win32Ada::
522 * wPOSIX::
523
524 Mac OS Topics
525
526 * Codesigning the Debugger::
527
528 Elaboration Order Handling in GNAT
529
530 * Elaboration Code::
531 * Elaboration Order::
532 * Checking the Elaboration Order::
533 * Controlling the Elaboration Order in Ada::
534 * Controlling the Elaboration Order in GNAT::
535 * Common Elaboration-model Traits::
536 * Dynamic Elaboration Model in GNAT::
537 * Static Elaboration Model in GNAT::
538 * SPARK Elaboration Model in GNAT::
539 * Legacy Elaboration Model in GNAT::
540 * Mixing Elaboration Models::
541 * Elaboration Circularities::
542 * Resolving Elaboration Circularities::
543 * Resolving Task Issues::
544 * Elaboration-related Compiler Switches::
545 * Summary of Procedures for Elaboration Control::
546 * Inspecting the Chosen Elaboration Order::
547
548 Inline Assembler
549
550 * Basic Assembler Syntax::
551 * A Simple Example of Inline Assembler::
552 * Output Variables in Inline Assembler::
553 * Input Variables in Inline Assembler::
554 * Inlining Inline Assembler Code::
555 * Other Asm Functionality::
556
557 Other Asm Functionality
558
559 * The Clobber Parameter::
560 * The Volatile Parameter::
561
562 @end detailmenu
563 @end menu
564
565 @node About This Guide,Getting Started with GNAT,Top,Top
566 @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}
567 @chapter About This Guide
568
569
570
571 This guide describes the use of GNAT,
572 a compiler and software development
573 toolset for the full Ada programming language.
574 It documents the features of the compiler and tools, and explains
575 how to use them to build Ada applications.
576
577 GNAT implements Ada 95, Ada 2005 and Ada 2012, and it may also be
578 invoked in Ada 83 compatibility mode.
579 By default, GNAT assumes Ada 2012, but you can override with a
580 compiler switch (@ref{6,,Compiling Different Versions of Ada})
581 to explicitly specify the language version.
582 Throughout this manual, references to 'Ada' without a year suffix
583 apply to all Ada 95/2005/2012 versions of the language.
584
585 @menu
586 * What This Guide Contains::
587 * What You Should Know before Reading This Guide::
588 * Related Information::
589 * A Note to Readers of Previous Versions of the Manual::
590 * Conventions::
591
592 @end menu
593
594 @node What This Guide Contains,What You Should Know before Reading This Guide,,About This Guide
595 @anchor{gnat_ugn/about_this_guide what-this-guide-contains}@anchor{7}
596 @section What This Guide Contains
597
598
599 This guide contains the following chapters:
600
601
602 @itemize *
603
604 @item
605 @ref{8,,Getting Started with GNAT} describes how to get started compiling
606 and running Ada programs with the GNAT Ada programming environment.
607
608 @item
609 @ref{9,,The GNAT Compilation Model} describes the compilation model used
610 by GNAT.
611
612 @item
613 @ref{a,,Building Executable Programs with GNAT} describes how to use the
614 main GNAT tools to build executable programs, and it also gives examples of
615 using the GNU make utility with GNAT.
616
617 @item
618 @ref{b,,GNAT Utility Programs} explains the various utility programs that
619 are included in the GNAT environment
620
621 @item
622 @ref{c,,GNAT and Program Execution} covers a number of topics related to
623 running, debugging, and tuning the performace of programs developed
624 with GNAT
625 @end itemize
626
627 Appendices cover several additional topics:
628
629
630 @itemize *
631
632 @item
633 @ref{d,,Platform-Specific Information} describes the different run-time
634 library implementations and also presents information on how to use
635 GNAT on several specific platforms
636
637 @item
638 @ref{e,,Example of Binder Output File} shows the source code for the binder
639 output file for a sample program.
640
641 @item
642 @ref{f,,Elaboration Order Handling in GNAT} describes how GNAT helps
643 you deal with elaboration order issues.
644
645 @item
646 @ref{10,,Inline Assembler} shows how to use the inline assembly facility
647 in an Ada program.
648 @end itemize
649
650 @node What You Should Know before Reading This Guide,Related Information,What This Guide Contains,About This Guide
651 @anchor{gnat_ugn/about_this_guide what-you-should-know-before-reading-this-guide}@anchor{11}
652 @section What You Should Know before Reading This Guide
653
654
655 @geindex Ada 95 Language Reference Manual
656
657 @geindex Ada 2005 Language Reference Manual
658
659 This guide assumes a basic familiarity with the Ada 95 language, as
660 described in the International Standard ANSI/ISO/IEC-8652:1995, January
661 1995.
662 It does not require knowledge of the features introduced by Ada 2005
663 or Ada 2012.
664 Reference manuals for Ada 95, Ada 2005, and Ada 2012 are included in
665 the GNAT documentation package.
666
667 @node Related Information,A Note to Readers of Previous Versions of the Manual,What You Should Know before Reading This Guide,About This Guide
668 @anchor{gnat_ugn/about_this_guide related-information}@anchor{12}
669 @section Related Information
670
671
672 For further information about Ada and related tools, please refer to the
673 following documents:
674
675
676 @itemize *
677
678 @item
679 @cite{Ada 95 Reference Manual}, @cite{Ada 2005 Reference Manual}, and
680 @cite{Ada 2012 Reference Manual}, which contain reference
681 material for the several revisions of the Ada language standard.
682
683 @item
684 @cite{GNAT Reference_Manual}, which contains all reference material for the GNAT
685 implementation of Ada.
686
687 @item
688 @cite{Using the GNAT Programming Studio}, which describes the GPS
689 Integrated Development Environment.
690
691 @item
692 @cite{GNAT Programming Studio Tutorial}, which introduces the
693 main GPS features through examples.
694
695 @item
696 @cite{Debugging with GDB},
697 for all details on the use of the GNU source-level debugger.
698
699 @item
700 @cite{GNU Emacs Manual},
701 for full information on the extensible editor and programming
702 environment Emacs.
703 @end itemize
704
705 @node A Note to Readers of Previous Versions of the Manual,Conventions,Related Information,About This Guide
706 @anchor{gnat_ugn/about_this_guide a-note-to-readers-of-previous-versions-of-the-manual}@anchor{13}
707 @section A Note to Readers of Previous Versions of the Manual
708
709
710 In early 2015 the GNAT manuals were transitioned to the
711 reStructuredText (rst) / Sphinx documentation generator technology.
712 During that process the @cite{GNAT User's Guide} was reorganized
713 so that related topics would be described together in the same chapter
714 or appendix. Here's a summary of the major changes realized in
715 the new document structure.
716
717
718 @itemize *
719
720 @item
721 @ref{9,,The GNAT Compilation Model} has been extended so that it now covers
722 the following material:
723
724
725 @itemize -
726
727 @item
728 The @code{gnatname}, @code{gnatkr}, and @code{gnatchop} tools
729
730 @item
731 @ref{14,,Configuration Pragmas}
732
733 @item
734 @ref{15,,GNAT and Libraries}
735
736 @item
737 @ref{16,,Conditional Compilation} including @ref{17,,Preprocessing with gnatprep}
738 and @ref{18,,Integrated Preprocessing}
739
740 @item
741 @ref{19,,Generating Ada Bindings for C and C++ headers}
742
743 @item
744 @ref{1a,,Using GNAT Files with External Tools}
745 @end itemize
746
747 @item
748 @ref{a,,Building Executable Programs with GNAT} is a new chapter consolidating
749 the following content:
750
751
752 @itemize -
753
754 @item
755 @ref{1b,,Building with gnatmake}
756
757 @item
758 @ref{1c,,Compiling with gcc}
759
760 @item
761 @ref{1d,,Binding with gnatbind}
762
763 @item
764 @ref{1e,,Linking with gnatlink}
765
766 @item
767 @ref{1f,,Using the GNU make Utility}
768 @end itemize
769
770 @item
771 @ref{b,,GNAT Utility Programs} is a new chapter consolidating the information about several
772 GNAT tools:
773
774
775
776 @itemize -
777
778 @item
779 @ref{20,,The File Cleanup Utility gnatclean}
780
781 @item
782 @ref{21,,The GNAT Library Browser gnatls}
783
784 @item
785 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
786
787 @item
788 @ref{23,,The Ada to HTML Converter gnathtml}
789 @end itemize
790
791 @item
792 @ref{c,,GNAT and Program Execution} is a new chapter consolidating the following:
793
794
795 @itemize -
796
797 @item
798 @ref{24,,Running and Debugging Ada Programs}
799
800 @item
801 @ref{25,,Profiling}
802
803 @item
804 @ref{26,,Improving Performance}
805
806 @item
807 @ref{27,,Overflow Check Handling in GNAT}
808
809 @item
810 @ref{28,,Performing Dimensionality Analysis in GNAT}
811
812 @item
813 @ref{29,,Stack Related Facilities}
814
815 @item
816 @ref{2a,,Memory Management Issues}
817 @end itemize
818
819 @item
820 @ref{d,,Platform-Specific Information} is a new appendix consolidating the following:
821
822
823 @itemize -
824
825 @item
826 @ref{2b,,Run-Time Libraries}
827
828 @item
829 @ref{2c,,Microsoft Windows Topics}
830
831 @item
832 @ref{2d,,Mac OS Topics}
833 @end itemize
834
835 @item
836 The @emph{Compatibility and Porting Guide} appendix has been moved to the
837 @cite{GNAT Reference Manual}. It now includes a section
838 @emph{Writing Portable Fixed-Point Declarations} which was previously
839 a separate chapter in the @cite{GNAT User's Guide}.
840 @end itemize
841
842 @node Conventions,,A Note to Readers of Previous Versions of the Manual,About This Guide
843 @anchor{gnat_ugn/about_this_guide conventions}@anchor{2e}
844 @section Conventions
845
846
847 @geindex Conventions
848 @geindex typographical
849
850 @geindex Typographical conventions
851
852 Following are examples of the typographical and graphic conventions used
853 in this guide:
854
855
856 @itemize *
857
858 @item
859 @code{Functions}, @code{utility program names}, @code{standard names},
860 and @code{classes}.
861
862 @item
863 @code{Option flags}
864
865 @item
866 @code{File names}
867
868 @item
869 @code{Variables}
870
871 @item
872 @emph{Emphasis}
873
874 @item
875 [optional information or parameters]
876
877 @item
878 Examples are described by text
879
880 @example
881 and then shown this way.
882 @end example
883
884 @item
885 Commands that are entered by the user are shown as preceded by a prompt string
886 comprising the @code{$} character followed by a space.
887
888 @item
889 Full file names are shown with the '/' character
890 as the directory separator; e.g., @code{parent-dir/subdir/myfile.adb}.
891 If you are using GNAT on a Windows platform, please note that
892 the '\' character should be used instead.
893 @end itemize
894
895 @node Getting Started with GNAT,The GNAT Compilation Model,About This Guide,Top
896 @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}
897 @chapter Getting Started with GNAT
898
899
900 This chapter describes how to use GNAT's command line interface to build
901 executable Ada programs.
902 On most platforms a visually oriented Integrated Development Environment
903 is also available, the GNAT Programming Studio (GPS).
904 GPS offers a graphical "look and feel", support for development in
905 other programming languages, comprehensive browsing features, and
906 many other capabilities.
907 For information on GPS please refer to
908 @cite{Using the GNAT Programming Studio}.
909
910 @menu
911 * Running GNAT::
912 * Running a Simple Ada Program::
913 * Running a Program with Multiple Units::
914 * Using the gnatmake Utility::
915
916 @end menu
917
918 @node Running GNAT,Running a Simple Ada Program,,Getting Started with GNAT
919 @anchor{gnat_ugn/getting_started_with_gnat running-gnat}@anchor{31}@anchor{gnat_ugn/getting_started_with_gnat id2}@anchor{32}
920 @section Running GNAT
921
922
923 Three steps are needed to create an executable file from an Ada source
924 file:
925
926
927 @itemize *
928
929 @item
930 The source file(s) must be compiled.
931
932 @item
933 The file(s) must be bound using the GNAT binder.
934
935 @item
936 All appropriate object files must be linked to produce an executable.
937 @end itemize
938
939 All three steps are most commonly handled by using the @code{gnatmake}
940 utility program that, given the name of the main program, automatically
941 performs the necessary compilation, binding and linking steps.
942
943 @node Running a Simple Ada Program,Running a Program with Multiple Units,Running GNAT,Getting Started with GNAT
944 @anchor{gnat_ugn/getting_started_with_gnat running-a-simple-ada-program}@anchor{33}@anchor{gnat_ugn/getting_started_with_gnat id3}@anchor{34}
945 @section Running a Simple Ada Program
946
947
948 Any text editor may be used to prepare an Ada program.
949 (If Emacs is used, the optional Ada mode may be helpful in laying out the
950 program.)
951 The program text is a normal text file. We will assume in our initial
952 example that you have used your editor to prepare the following
953 standard format text file:
954
955 @example
956 with Ada.Text_IO; use Ada.Text_IO;
957 procedure Hello is
958 begin
959 Put_Line ("Hello WORLD!");
960 end Hello;
961 @end example
962
963 This file should be named @code{hello.adb}.
964 With the normal default file naming conventions, GNAT requires
965 that each file
966 contain a single compilation unit whose file name is the
967 unit name,
968 with periods replaced by hyphens; the
969 extension is @code{ads} for a
970 spec and @code{adb} for a body.
971 You can override this default file naming convention by use of the
972 special pragma @code{Source_File_Name} (for further information please
973 see @ref{35,,Using Other File Names}).
974 Alternatively, if you want to rename your files according to this default
975 convention, which is probably more convenient if you will be using GNAT
976 for all your compilations, then the @code{gnatchop} utility
977 can be used to generate correctly-named source files
978 (see @ref{36,,Renaming Files with gnatchop}).
979
980 You can compile the program using the following command (@code{$} is used
981 as the command prompt in the examples in this document):
982
983 @example
984 $ gcc -c hello.adb
985 @end example
986
987 @code{gcc} is the command used to run the compiler. This compiler is
988 capable of compiling programs in several languages, including Ada and
989 C. It assumes that you have given it an Ada program if the file extension is
990 either @code{.ads} or @code{.adb}, and it will then call
991 the GNAT compiler to compile the specified file.
992
993 The @code{-c} switch is required. It tells @code{gcc} to only do a
994 compilation. (For C programs, @code{gcc} can also do linking, but this
995 capability is not used directly for Ada programs, so the @code{-c}
996 switch must always be present.)
997
998 This compile command generates a file
999 @code{hello.o}, which is the object
1000 file corresponding to your Ada program. It also generates
1001 an 'Ada Library Information' file @code{hello.ali},
1002 which contains additional information used to check
1003 that an Ada program is consistent.
1004 To build an executable file,
1005 use @code{gnatbind} to bind the program
1006 and @code{gnatlink} to link it. The
1007 argument to both @code{gnatbind} and @code{gnatlink} is the name of the
1008 @code{ALI} file, but the default extension of @code{.ali} can
1009 be omitted. This means that in the most common case, the argument
1010 is simply the name of the main program:
1011
1012 @example
1013 $ gnatbind hello
1014 $ gnatlink hello
1015 @end example
1016
1017 A simpler method of carrying out these steps is to use @code{gnatmake},
1018 a master program that invokes all the required
1019 compilation, binding and linking tools in the correct order. In particular,
1020 @code{gnatmake} automatically recompiles any sources that have been
1021 modified since they were last compiled, or sources that depend
1022 on such modified sources, so that 'version skew' is avoided.
1023
1024 @geindex Version skew (avoided by `@w{`}gnatmake`@w{`})
1025
1026 @example
1027 $ gnatmake hello.adb
1028 @end example
1029
1030 The result is an executable program called @code{hello}, which can be
1031 run by entering:
1032
1033 @example
1034 $ hello
1035 @end example
1036
1037 assuming that the current directory is on the search path
1038 for executable programs.
1039
1040 and, if all has gone well, you will see:
1041
1042 @example
1043 Hello WORLD!
1044 @end example
1045
1046 appear in response to this command.
1047
1048 @node Running a Program with Multiple Units,Using the gnatmake Utility,Running a Simple Ada Program,Getting Started with GNAT
1049 @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}
1050 @section Running a Program with Multiple Units
1051
1052
1053 Consider a slightly more complicated example that has three files: a
1054 main program, and the spec and body of a package:
1055
1056 @example
1057 package Greetings is
1058 procedure Hello;
1059 procedure Goodbye;
1060 end Greetings;
1061
1062 with Ada.Text_IO; use Ada.Text_IO;
1063 package body Greetings is
1064 procedure Hello is
1065 begin
1066 Put_Line ("Hello WORLD!");
1067 end Hello;
1068
1069 procedure Goodbye is
1070 begin
1071 Put_Line ("Goodbye WORLD!");
1072 end Goodbye;
1073 end Greetings;
1074
1075 with Greetings;
1076 procedure Gmain is
1077 begin
1078 Greetings.Hello;
1079 Greetings.Goodbye;
1080 end Gmain;
1081 @end example
1082
1083 Following the one-unit-per-file rule, place this program in the
1084 following three separate files:
1085
1086
1087 @table @asis
1088
1089 @item @emph{greetings.ads}
1090
1091 spec of package @code{Greetings}
1092
1093 @item @emph{greetings.adb}
1094
1095 body of package @code{Greetings}
1096
1097 @item @emph{gmain.adb}
1098
1099 body of main program
1100 @end table
1101
1102 To build an executable version of
1103 this program, we could use four separate steps to compile, bind, and link
1104 the program, as follows:
1105
1106 @example
1107 $ gcc -c gmain.adb
1108 $ gcc -c greetings.adb
1109 $ gnatbind gmain
1110 $ gnatlink gmain
1111 @end example
1112
1113 Note that there is no required order of compilation when using GNAT.
1114 In particular it is perfectly fine to compile the main program first.
1115 Also, it is not necessary to compile package specs in the case where
1116 there is an accompanying body; you only need to compile the body. If you want
1117 to submit these files to the compiler for semantic checking and not code
1118 generation, then use the @code{-gnatc} switch:
1119
1120 @example
1121 $ gcc -c greetings.ads -gnatc
1122 @end example
1123
1124 Although the compilation can be done in separate steps as in the
1125 above example, in practice it is almost always more convenient
1126 to use the @code{gnatmake} tool. All you need to know in this case
1127 is the name of the main program's source file. The effect of the above four
1128 commands can be achieved with a single one:
1129
1130 @example
1131 $ gnatmake gmain.adb
1132 @end example
1133
1134 In the next section we discuss the advantages of using @code{gnatmake} in
1135 more detail.
1136
1137 @node Using the gnatmake Utility,,Running a Program with Multiple Units,Getting Started with GNAT
1138 @anchor{gnat_ugn/getting_started_with_gnat using-the-gnatmake-utility}@anchor{39}@anchor{gnat_ugn/getting_started_with_gnat id5}@anchor{3a}
1139 @section Using the @code{gnatmake} Utility
1140
1141
1142 If you work on a program by compiling single components at a time using
1143 @code{gcc}, you typically keep track of the units you modify. In order to
1144 build a consistent system, you compile not only these units, but also any
1145 units that depend on the units you have modified.
1146 For example, in the preceding case,
1147 if you edit @code{gmain.adb}, you only need to recompile that file. But if
1148 you edit @code{greetings.ads}, you must recompile both
1149 @code{greetings.adb} and @code{gmain.adb}, because both files contain
1150 units that depend on @code{greetings.ads}.
1151
1152 @code{gnatbind} will warn you if you forget one of these compilation
1153 steps, so that it is impossible to generate an inconsistent program as a
1154 result of forgetting to do a compilation. Nevertheless it is tedious and
1155 error-prone to keep track of dependencies among units.
1156 One approach to handle the dependency-bookkeeping is to use a
1157 makefile. However, makefiles present maintenance problems of their own:
1158 if the dependencies change as you change the program, you must make
1159 sure that the makefile is kept up-to-date manually, which is also an
1160 error-prone process.
1161
1162 The @code{gnatmake} utility takes care of these details automatically.
1163 Invoke it using either one of the following forms:
1164
1165 @example
1166 $ gnatmake gmain.adb
1167 $ gnatmake gmain
1168 @end example
1169
1170 The argument is the name of the file containing the main program;
1171 you may omit the extension. @code{gnatmake}
1172 examines the environment, automatically recompiles any files that need
1173 recompiling, and binds and links the resulting set of object files,
1174 generating the executable file, @code{gmain}.
1175 In a large program, it
1176 can be extremely helpful to use @code{gnatmake}, because working out by hand
1177 what needs to be recompiled can be difficult.
1178
1179 Note that @code{gnatmake} takes into account all the Ada rules that
1180 establish dependencies among units. These include dependencies that result
1181 from inlining subprogram bodies, and from
1182 generic instantiation. Unlike some other
1183 Ada make tools, @code{gnatmake} does not rely on the dependencies that were
1184 found by the compiler on a previous compilation, which may possibly
1185 be wrong when sources change. @code{gnatmake} determines the exact set of
1186 dependencies from scratch each time it is run.
1187
1188 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
1189
1190 @node The GNAT Compilation Model,Building Executable Programs with GNAT,Getting Started with GNAT,Top
1191 @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}
1192 @chapter The GNAT Compilation Model
1193
1194
1195 @geindex GNAT compilation model
1196
1197 @geindex Compilation model
1198
1199 This chapter describes the compilation model used by GNAT. Although
1200 similar to that used by other languages such as C and C++, this model
1201 is substantially different from the traditional Ada compilation models,
1202 which are based on a centralized program library. The chapter covers
1203 the following material:
1204
1205
1206 @itemize *
1207
1208 @item
1209 Topics related to source file makeup and naming
1210
1211
1212 @itemize *
1213
1214 @item
1215 @ref{3d,,Source Representation}
1216
1217 @item
1218 @ref{3e,,Foreign Language Representation}
1219
1220 @item
1221 @ref{3f,,File Naming Topics and Utilities}
1222 @end itemize
1223
1224 @item
1225 @ref{14,,Configuration Pragmas}
1226
1227 @item
1228 @ref{40,,Generating Object Files}
1229
1230 @item
1231 @ref{41,,Source Dependencies}
1232
1233 @item
1234 @ref{42,,The Ada Library Information Files}
1235
1236 @item
1237 @ref{43,,Binding an Ada Program}
1238
1239 @item
1240 @ref{15,,GNAT and Libraries}
1241
1242 @item
1243 @ref{16,,Conditional Compilation}
1244
1245 @item
1246 @ref{44,,Mixed Language Programming}
1247
1248 @item
1249 @ref{45,,GNAT and Other Compilation Models}
1250
1251 @item
1252 @ref{1a,,Using GNAT Files with External Tools}
1253 @end itemize
1254
1255 @menu
1256 * Source Representation::
1257 * Foreign Language Representation::
1258 * File Naming Topics and Utilities::
1259 * Configuration Pragmas::
1260 * Generating Object Files::
1261 * Source Dependencies::
1262 * The Ada Library Information Files::
1263 * Binding an Ada Program::
1264 * GNAT and Libraries::
1265 * Conditional Compilation::
1266 * Mixed Language Programming::
1267 * GNAT and Other Compilation Models::
1268 * Using GNAT Files with External Tools::
1269
1270 @end menu
1271
1272 @node Source Representation,Foreign Language Representation,,The GNAT Compilation Model
1273 @anchor{gnat_ugn/the_gnat_compilation_model source-representation}@anchor{3d}@anchor{gnat_ugn/the_gnat_compilation_model id2}@anchor{46}
1274 @section Source Representation
1275
1276
1277 @geindex Latin-1
1278
1279 @geindex VT
1280 @geindex HT
1281 @geindex CR
1282 @geindex LF
1283 @geindex FF
1284
1285 Ada source programs are represented in standard text files, using
1286 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1287 7-bit ASCII set, plus additional characters used for
1288 representing foreign languages (see @ref{3e,,Foreign Language Representation}
1289 for support of non-USA character sets). The format effector characters
1290 are represented using their standard ASCII encodings, as follows:
1291
1292 @quotation
1293
1294
1295 @multitable {xxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxx}
1296 @item
1297
1298 Character
1299
1300 @tab
1301
1302 Effect
1303
1304 @tab
1305
1306 Code
1307
1308 @item
1309
1310 @code{VT}
1311
1312 @tab
1313
1314 Vertical tab
1315
1316 @tab
1317
1318 @code{16#0B#}
1319
1320 @item
1321
1322 @code{HT}
1323
1324 @tab
1325
1326 Horizontal tab
1327
1328 @tab
1329
1330 @code{16#09#}
1331
1332 @item
1333
1334 @code{CR}
1335
1336 @tab
1337
1338 Carriage return
1339
1340 @tab
1341
1342 @code{16#0D#}
1343
1344 @item
1345
1346 @code{LF}
1347
1348 @tab
1349
1350 Line feed
1351
1352 @tab
1353
1354 @code{16#0A#}
1355
1356 @item
1357
1358 @code{FF}
1359
1360 @tab
1361
1362 Form feed
1363
1364 @tab
1365
1366 @code{16#0C#}
1367
1368 @end multitable
1369
1370 @end quotation
1371
1372 Source files are in standard text file format. In addition, GNAT will
1373 recognize a wide variety of stream formats, in which the end of
1374 physical lines is marked by any of the following sequences:
1375 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1376 in accommodating files that are imported from other operating systems.
1377
1378 @geindex End of source file; Source file@comma{} end
1379
1380 @geindex SUB (control character)
1381
1382 The end of a source file is normally represented by the physical end of
1383 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1384 recognized as signalling the end of the source file. Again, this is
1385 provided for compatibility with other operating systems where this
1386 code is used to represent the end of file.
1387
1388 @geindex spec (definition)
1389 @geindex compilation (definition)
1390
1391 Each file contains a single Ada compilation unit, including any pragmas
1392 associated with the unit. For example, this means you must place a
1393 package declaration (a package @emph{spec}) and the corresponding body in
1394 separate files. An Ada @emph{compilation} (which is a sequence of
1395 compilation units) is represented using a sequence of files. Similarly,
1396 you will place each subunit or child unit in a separate file.
1397
1398 @node Foreign Language Representation,File Naming Topics and Utilities,Source Representation,The GNAT Compilation Model
1399 @anchor{gnat_ugn/the_gnat_compilation_model foreign-language-representation}@anchor{3e}@anchor{gnat_ugn/the_gnat_compilation_model id3}@anchor{47}
1400 @section Foreign Language Representation
1401
1402
1403 GNAT supports the standard character sets defined in Ada as well as
1404 several other non-standard character sets for use in localized versions
1405 of the compiler (@ref{48,,Character Set Control}).
1406
1407 @menu
1408 * Latin-1::
1409 * Other 8-Bit Codes::
1410 * Wide_Character Encodings::
1411 * Wide_Wide_Character Encodings::
1412
1413 @end menu
1414
1415 @node Latin-1,Other 8-Bit Codes,,Foreign Language Representation
1416 @anchor{gnat_ugn/the_gnat_compilation_model id4}@anchor{49}@anchor{gnat_ugn/the_gnat_compilation_model latin-1}@anchor{4a}
1417 @subsection Latin-1
1418
1419
1420 @geindex Latin-1
1421
1422 The basic character set is Latin-1. This character set is defined by ISO
1423 standard 8859, part 1. The lower half (character codes @code{16#00#}
1424 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper
1425 half is used to represent additional characters. These include extended letters
1426 used by European languages, such as French accents, the vowels with umlauts
1427 used in German, and the extra letter A-ring used in Swedish.
1428
1429 @geindex Ada.Characters.Latin_1
1430
1431 For a complete list of Latin-1 codes and their encodings, see the source
1432 file of library unit @code{Ada.Characters.Latin_1} in file
1433 @code{a-chlat1.ads}.
1434 You may use any of these extended characters freely in character or
1435 string literals. In addition, the extended characters that represent
1436 letters can be used in identifiers.
1437
1438 @node Other 8-Bit Codes,Wide_Character Encodings,Latin-1,Foreign Language Representation
1439 @anchor{gnat_ugn/the_gnat_compilation_model other-8-bit-codes}@anchor{4b}@anchor{gnat_ugn/the_gnat_compilation_model id5}@anchor{4c}
1440 @subsection Other 8-Bit Codes
1441
1442
1443 GNAT also supports several other 8-bit coding schemes:
1444
1445 @geindex Latin-2
1446
1447 @geindex ISO 8859-2
1448
1449
1450 @table @asis
1451
1452 @item @emph{ISO 8859-2 (Latin-2)}
1453
1454 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1455 equivalence.
1456 @end table
1457
1458 @geindex Latin-3
1459
1460 @geindex ISO 8859-3
1461
1462
1463 @table @asis
1464
1465 @item @emph{ISO 8859-3 (Latin-3)}
1466
1467 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1468 equivalence.
1469 @end table
1470
1471 @geindex Latin-4
1472
1473 @geindex ISO 8859-4
1474
1475
1476 @table @asis
1477
1478 @item @emph{ISO 8859-4 (Latin-4)}
1479
1480 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1481 equivalence.
1482 @end table
1483
1484 @geindex ISO 8859-5
1485
1486 @geindex Cyrillic
1487
1488
1489 @table @asis
1490
1491 @item @emph{ISO 8859-5 (Cyrillic)}
1492
1493 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1494 lowercase equivalence.
1495 @end table
1496
1497 @geindex ISO 8859-15
1498
1499 @geindex Latin-9
1500
1501
1502 @table @asis
1503
1504 @item @emph{ISO 8859-15 (Latin-9)}
1505
1506 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1507 lowercase equivalence
1508 @end table
1509
1510 @geindex code page 437 (IBM PC)
1511
1512
1513 @table @asis
1514
1515 @item @emph{IBM PC (code page 437)}
1516
1517 This code page is the normal default for PCs in the U.S. It corresponds
1518 to the original IBM PC character set. This set has some, but not all, of
1519 the extended Latin-1 letters, but these letters do not have the same
1520 encoding as Latin-1. In this mode, these letters are allowed in
1521 identifiers with uppercase and lowercase equivalence.
1522 @end table
1523
1524 @geindex code page 850 (IBM PC)
1525
1526
1527 @table @asis
1528
1529 @item @emph{IBM PC (code page 850)}
1530
1531 This code page is a modification of 437 extended to include all the
1532 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1533 mode, all these letters are allowed in identifiers with uppercase and
1534 lowercase equivalence.
1535
1536 @item @emph{Full Upper 8-bit}
1537
1538 Any character in the range 80-FF allowed in identifiers, and all are
1539 considered distinct. In other words, there are no uppercase and lowercase
1540 equivalences in this range. This is useful in conjunction with
1541 certain encoding schemes used for some foreign character sets (e.g.,
1542 the typical method of representing Chinese characters on the PC).
1543
1544 @item @emph{No Upper-Half}
1545
1546 No upper-half characters in the range 80-FF are allowed in identifiers.
1547 This gives Ada 83 compatibility for identifier names.
1548 @end table
1549
1550 For precise data on the encodings permitted, and the uppercase and lowercase
1551 equivalences that are recognized, see the file @code{csets.adb} in
1552 the GNAT compiler sources. You will need to obtain a full source release
1553 of GNAT to obtain this file.
1554
1555 @node Wide_Character Encodings,Wide_Wide_Character Encodings,Other 8-Bit Codes,Foreign Language Representation
1556 @anchor{gnat_ugn/the_gnat_compilation_model id6}@anchor{4d}@anchor{gnat_ugn/the_gnat_compilation_model wide-character-encodings}@anchor{4e}
1557 @subsection Wide_Character Encodings
1558
1559
1560 GNAT allows wide character codes to appear in character and string
1561 literals, and also optionally in identifiers, by means of the following
1562 possible encoding schemes:
1563
1564
1565 @table @asis
1566
1567 @item @emph{Hex Coding}
1568
1569 In this encoding, a wide character is represented by the following five
1570 character sequence:
1571
1572 @example
1573 ESC a b c d
1574 @end example
1575
1576 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1577 characters (using uppercase letters) of the wide character code. For
1578 example, ESC A345 is used to represent the wide character with code
1579 @code{16#A345#}.
1580 This scheme is compatible with use of the full Wide_Character set.
1581
1582 @item @emph{Upper-Half Coding}
1583
1584 @geindex Upper-Half Coding
1585
1586 The wide character with encoding @code{16#abcd#} where the upper bit is on
1587 (in other words, 'a' is in the range 8-F) is represented as two bytes,
1588 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1589 character, but is not required to be in the upper half. This method can
1590 be also used for shift-JIS or EUC, where the internal coding matches the
1591 external coding.
1592
1593 @item @emph{Shift JIS Coding}
1594
1595 @geindex Shift JIS Coding
1596
1597 A wide character is represented by a two-character sequence,
1598 @code{16#ab#} and
1599 @code{16#cd#}, with the restrictions described for upper-half encoding as
1600 described above. The internal character code is the corresponding JIS
1601 character according to the standard algorithm for Shift-JIS
1602 conversion. Only characters defined in the JIS code set table can be
1603 used with this encoding method.
1604
1605 @item @emph{EUC Coding}
1606
1607 @geindex EUC Coding
1608
1609 A wide character is represented by a two-character sequence
1610 @code{16#ab#} and
1611 @code{16#cd#}, with both characters being in the upper half. The internal
1612 character code is the corresponding JIS character according to the EUC
1613 encoding algorithm. Only characters defined in the JIS code set table
1614 can be used with this encoding method.
1615
1616 @item @emph{UTF-8 Coding}
1617
1618 A wide character is represented using
1619 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1620 10646-1/Am.2. Depending on the character value, the representation
1621 is a one, two, or three byte sequence:
1622
1623 @example
1624 16#0000#-16#007f#: 2#0xxxxxxx#
1625 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1626 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1627 @end example
1628
1629 where the @code{xxx} bits correspond to the left-padded bits of the
1630 16-bit character value. Note that all lower half ASCII characters
1631 are represented as ASCII bytes and all upper half characters and
1632 other wide characters are represented as sequences of upper-half
1633 (The full UTF-8 scheme allows for encoding 31-bit characters as
1634 6-byte sequences, and in the following section on wide wide
1635 characters, the use of these sequences is documented).
1636
1637 @item @emph{Brackets Coding}
1638
1639 In this encoding, a wide character is represented by the following eight
1640 character sequence:
1641
1642 @example
1643 [ " a b c d " ]
1644 @end example
1645
1646 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1647 characters (using uppercase letters) of the wide character code. For
1648 example, ['A345'] is used to represent the wide character with code
1649 @code{16#A345#}. It is also possible (though not required) to use the
1650 Brackets coding for upper half characters. For example, the code
1651 @code{16#A3#} can be represented as @code{['A3']}.
1652
1653 This scheme is compatible with use of the full Wide_Character set,
1654 and is also the method used for wide character encoding in some standard
1655 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1656 @end table
1657
1658 @cartouche
1659 @quotation Note
1660 Some of these coding schemes do not permit the full use of the
1661 Ada character set. For example, neither Shift JIS nor EUC allow the
1662 use of the upper half of the Latin-1 set.
1663 @end quotation
1664 @end cartouche
1665
1666 @node Wide_Wide_Character Encodings,,Wide_Character Encodings,Foreign Language Representation
1667 @anchor{gnat_ugn/the_gnat_compilation_model id7}@anchor{4f}@anchor{gnat_ugn/the_gnat_compilation_model wide-wide-character-encodings}@anchor{50}
1668 @subsection Wide_Wide_Character Encodings
1669
1670
1671 GNAT allows wide wide character codes to appear in character and string
1672 literals, and also optionally in identifiers, by means of the following
1673 possible encoding schemes:
1674
1675
1676 @table @asis
1677
1678 @item @emph{UTF-8 Coding}
1679
1680 A wide character is represented using
1681 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1682 10646-1/Am.2. Depending on the character value, the representation
1683 of character codes with values greater than 16#FFFF# is a
1684 is a four, five, or six byte sequence:
1685
1686 @example
1687 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx
1688 10xxxxxx
1689 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
1690 10xxxxxx 10xxxxxx
1691 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
1692 10xxxxxx 10xxxxxx 10xxxxxx
1693 @end example
1694
1695 where the @code{xxx} bits correspond to the left-padded bits of the
1696 32-bit character value.
1697
1698 @item @emph{Brackets Coding}
1699
1700 In this encoding, a wide wide character is represented by the following ten or
1701 twelve byte character sequence:
1702
1703 @example
1704 [ " a b c d e f " ]
1705 [ " a b c d e f g h " ]
1706 @end example
1707
1708 where @code{a-h} are the six or eight hexadecimal
1709 characters (using uppercase letters) of the wide wide character code. For
1710 example, ["1F4567"] is used to represent the wide wide character with code
1711 @code{16#001F_4567#}.
1712
1713 This scheme is compatible with use of the full Wide_Wide_Character set,
1714 and is also the method used for wide wide character encoding in some standard
1715 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1716 @end table
1717
1718 @node File Naming Topics and Utilities,Configuration Pragmas,Foreign Language Representation,The GNAT Compilation Model
1719 @anchor{gnat_ugn/the_gnat_compilation_model id8}@anchor{51}@anchor{gnat_ugn/the_gnat_compilation_model file-naming-topics-and-utilities}@anchor{3f}
1720 @section File Naming Topics and Utilities
1721
1722
1723 GNAT has a default file naming scheme and also provides the user with
1724 a high degree of control over how the names and extensions of the
1725 source files correspond to the Ada compilation units that they contain.
1726
1727 @menu
1728 * File Naming Rules::
1729 * Using Other File Names::
1730 * Alternative File Naming Schemes::
1731 * Handling Arbitrary File Naming Conventions with gnatname::
1732 * File Name Krunching with gnatkr::
1733 * Renaming Files with gnatchop::
1734
1735 @end menu
1736
1737 @node File Naming Rules,Using Other File Names,,File Naming Topics and Utilities
1738 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-rules}@anchor{52}@anchor{gnat_ugn/the_gnat_compilation_model id9}@anchor{53}
1739 @subsection File Naming Rules
1740
1741
1742 The default file name is determined by the name of the unit that the
1743 file contains. The name is formed by taking the full expanded name of
1744 the unit and replacing the separating dots with hyphens and using
1745 lowercase for all letters.
1746
1747 An exception arises if the file name generated by the above rules starts
1748 with one of the characters
1749 @code{a}, @code{g}, @code{i}, or @code{s}, and the second character is a
1750 minus. In this case, the character tilde is used in place
1751 of the minus. The reason for this special rule is to avoid clashes with
1752 the standard names for child units of the packages System, Ada,
1753 Interfaces, and GNAT, which use the prefixes
1754 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
1755 respectively.
1756
1757 The file extension is @code{.ads} for a spec and
1758 @code{.adb} for a body. The following table shows some
1759 examples of these rules.
1760
1761 @quotation
1762
1763
1764 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
1765 @item
1766
1767 Source File
1768
1769 @tab
1770
1771 Ada Compilation Unit
1772
1773 @item
1774
1775 @code{main.ads}
1776
1777 @tab
1778
1779 Main (spec)
1780
1781 @item
1782
1783 @code{main.adb}
1784
1785 @tab
1786
1787 Main (body)
1788
1789 @item
1790
1791 @code{arith_functions.ads}
1792
1793 @tab
1794
1795 Arith_Functions (package spec)
1796
1797 @item
1798
1799 @code{arith_functions.adb}
1800
1801 @tab
1802
1803 Arith_Functions (package body)
1804
1805 @item
1806
1807 @code{func-spec.ads}
1808
1809 @tab
1810
1811 Func.Spec (child package spec)
1812
1813 @item
1814
1815 @code{func-spec.adb}
1816
1817 @tab
1818
1819 Func.Spec (child package body)
1820
1821 @item
1822
1823 @code{main-sub.adb}
1824
1825 @tab
1826
1827 Sub (subunit of Main)
1828
1829 @item
1830
1831 @code{a~bad.adb}
1832
1833 @tab
1834
1835 A.Bad (child package body)
1836
1837 @end multitable
1838
1839 @end quotation
1840
1841 Following these rules can result in excessively long
1842 file names if corresponding
1843 unit names are long (for example, if child units or subunits are
1844 heavily nested). An option is available to shorten such long file names
1845 (called file name 'krunching'). This may be particularly useful when
1846 programs being developed with GNAT are to be used on operating systems
1847 with limited file name lengths. @ref{54,,Using gnatkr}.
1848
1849 Of course, no file shortening algorithm can guarantee uniqueness over
1850 all possible unit names; if file name krunching is used, it is your
1851 responsibility to ensure no name clashes occur. Alternatively you
1852 can specify the exact file names that you want used, as described
1853 in the next section. Finally, if your Ada programs are migrating from a
1854 compiler with a different naming convention, you can use the gnatchop
1855 utility to produce source files that follow the GNAT naming conventions.
1856 (For details see @ref{36,,Renaming Files with gnatchop}.)
1857
1858 Note: in the case of Windows or Mac OS operating systems, case is not
1859 significant. So for example on Windows if the canonical name is
1860 @code{main-sub.adb}, you can use the file name @code{Main-Sub.adb} instead.
1861 However, case is significant for other operating systems, so for example,
1862 if you want to use other than canonically cased file names on a Unix system,
1863 you need to follow the procedures described in the next section.
1864
1865 @node Using Other File Names,Alternative File Naming Schemes,File Naming Rules,File Naming Topics and Utilities
1866 @anchor{gnat_ugn/the_gnat_compilation_model id10}@anchor{55}@anchor{gnat_ugn/the_gnat_compilation_model using-other-file-names}@anchor{35}
1867 @subsection Using Other File Names
1868
1869
1870 @geindex File names
1871
1872 In the previous section, we have described the default rules used by
1873 GNAT to determine the file name in which a given unit resides. It is
1874 often convenient to follow these default rules, and if you follow them,
1875 the compiler knows without being explicitly told where to find all
1876 the files it needs.
1877
1878 @geindex Source_File_Name pragma
1879
1880 However, in some cases, particularly when a program is imported from
1881 another Ada compiler environment, it may be more convenient for the
1882 programmer to specify which file names contain which units. GNAT allows
1883 arbitrary file names to be used by means of the Source_File_Name pragma.
1884 The form of this pragma is as shown in the following examples:
1885
1886 @example
1887 pragma Source_File_Name (My_Utilities.Stacks,
1888 Spec_File_Name => "myutilst_a.ada");
1889 pragma Source_File_name (My_Utilities.Stacks,
1890 Body_File_Name => "myutilst.ada");
1891 @end example
1892
1893 As shown in this example, the first argument for the pragma is the unit
1894 name (in this example a child unit). The second argument has the form
1895 of a named association. The identifier
1896 indicates whether the file name is for a spec or a body;
1897 the file name itself is given by a string literal.
1898
1899 The source file name pragma is a configuration pragma, which means that
1900 normally it will be placed in the @code{gnat.adc}
1901 file used to hold configuration
1902 pragmas that apply to a complete compilation environment.
1903 For more details on how the @code{gnat.adc} file is created and used
1904 see @ref{56,,Handling of Configuration Pragmas}.
1905
1906 @geindex gnat.adc
1907
1908 GNAT allows completely arbitrary file names to be specified using the
1909 source file name pragma. However, if the file name specified has an
1910 extension other than @code{.ads} or @code{.adb} it is necessary to use
1911 a special syntax when compiling the file. The name in this case must be
1912 preceded by the special sequence @code{-x} followed by a space and the name
1913 of the language, here @code{ada}, as in:
1914
1915 @example
1916 $ gcc -c -x ada peculiar_file_name.sim
1917 @end example
1918
1919 @code{gnatmake} handles non-standard file names in the usual manner (the
1920 non-standard file name for the main program is simply used as the
1921 argument to gnatmake). Note that if the extension is also non-standard,
1922 then it must be included in the @code{gnatmake} command, it may not
1923 be omitted.
1924
1925 @node Alternative File Naming Schemes,Handling Arbitrary File Naming Conventions with gnatname,Using Other File Names,File Naming Topics and Utilities
1926 @anchor{gnat_ugn/the_gnat_compilation_model id11}@anchor{57}@anchor{gnat_ugn/the_gnat_compilation_model alternative-file-naming-schemes}@anchor{58}
1927 @subsection Alternative File Naming Schemes
1928
1929
1930 @geindex File naming schemes
1931 @geindex alternative
1932
1933 @geindex File names
1934
1935 The previous section described the use of the @code{Source_File_Name}
1936 pragma to allow arbitrary names to be assigned to individual source files.
1937 However, this approach requires one pragma for each file, and especially in
1938 large systems can result in very long @code{gnat.adc} files, and also create
1939 a maintenance problem.
1940
1941 @geindex Source_File_Name pragma
1942
1943 GNAT also provides a facility for specifying systematic file naming schemes
1944 other than the standard default naming scheme previously described. An
1945 alternative scheme for naming is specified by the use of
1946 @code{Source_File_Name} pragmas having the following format:
1947
1948 @example
1949 pragma Source_File_Name (
1950 Spec_File_Name => FILE_NAME_PATTERN
1951 [ , Casing => CASING_SPEC]
1952 [ , Dot_Replacement => STRING_LITERAL ] );
1953
1954 pragma Source_File_Name (
1955 Body_File_Name => FILE_NAME_PATTERN
1956 [ , Casing => CASING_SPEC ]
1957 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1958
1959 pragma Source_File_Name (
1960 Subunit_File_Name => FILE_NAME_PATTERN
1961 [ , Casing => CASING_SPEC ]
1962 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1963
1964 FILE_NAME_PATTERN ::= STRING_LITERAL
1965 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1966 @end example
1967
1968 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1969 It contains a single asterisk character, and the unit name is substituted
1970 systematically for this asterisk. The optional parameter
1971 @code{Casing} indicates
1972 whether the unit name is to be all upper-case letters, all lower-case letters,
1973 or mixed-case. If no
1974 @code{Casing} parameter is used, then the default is all
1975 lower-case.
1976
1977 The optional @code{Dot_Replacement} string is used to replace any periods
1978 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1979 argument is used then separating dots appear unchanged in the resulting
1980 file name.
1981 Although the above syntax indicates that the
1982 @code{Casing} argument must appear
1983 before the @code{Dot_Replacement} argument, but it
1984 is also permissible to write these arguments in the opposite order.
1985
1986 As indicated, it is possible to specify different naming schemes for
1987 bodies, specs, and subunits. Quite often the rule for subunits is the
1988 same as the rule for bodies, in which case, there is no need to give
1989 a separate @code{Subunit_File_Name} rule, and in this case the
1990 @code{Body_File_name} rule is used for subunits as well.
1991
1992 The separate rule for subunits can also be used to implement the rather
1993 unusual case of a compilation environment (e.g., a single directory) which
1994 contains a subunit and a child unit with the same unit name. Although
1995 both units cannot appear in the same partition, the Ada Reference Manual
1996 allows (but does not require) the possibility of the two units coexisting
1997 in the same environment.
1998
1999 The file name translation works in the following steps:
2000
2001
2002 @itemize *
2003
2004 @item
2005 If there is a specific @code{Source_File_Name} pragma for the given unit,
2006 then this is always used, and any general pattern rules are ignored.
2007
2008 @item
2009 If there is a pattern type @code{Source_File_Name} pragma that applies to
2010 the unit, then the resulting file name will be used if the file exists. If
2011 more than one pattern matches, the latest one will be tried first, and the
2012 first attempt resulting in a reference to a file that exists will be used.
2013
2014 @item
2015 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2016 for which the corresponding file exists, then the standard GNAT default
2017 naming rules are used.
2018 @end itemize
2019
2020 As an example of the use of this mechanism, consider a commonly used scheme
2021 in which file names are all lower case, with separating periods copied
2022 unchanged to the resulting file name, and specs end with @code{.1.ada}, and
2023 bodies end with @code{.2.ada}. GNAT will follow this scheme if the following
2024 two pragmas appear:
2025
2026 @example
2027 pragma Source_File_Name
2028 (Spec_File_Name => ".1.ada");
2029 pragma Source_File_Name
2030 (Body_File_Name => ".2.ada");
2031 @end example
2032
2033 The default GNAT scheme is actually implemented by providing the following
2034 default pragmas internally:
2035
2036 @example
2037 pragma Source_File_Name
2038 (Spec_File_Name => ".ads", Dot_Replacement => "-");
2039 pragma Source_File_Name
2040 (Body_File_Name => ".adb", Dot_Replacement => "-");
2041 @end example
2042
2043 Our final example implements a scheme typically used with one of the
2044 Ada 83 compilers, where the separator character for subunits was '__'
2045 (two underscores), specs were identified by adding @code{_.ADA}, bodies
2046 by adding @code{.ADA}, and subunits by
2047 adding @code{.SEP}. All file names were
2048 upper case. Child units were not present of course since this was an
2049 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2050 the same double underscore separator for child units.
2051
2052 @example
2053 pragma Source_File_Name
2054 (Spec_File_Name => "_.ADA",
2055 Dot_Replacement => "__",
2056 Casing = Uppercase);
2057 pragma Source_File_Name
2058 (Body_File_Name => ".ADA",
2059 Dot_Replacement => "__",
2060 Casing = Uppercase);
2061 pragma Source_File_Name
2062 (Subunit_File_Name => ".SEP",
2063 Dot_Replacement => "__",
2064 Casing = Uppercase);
2065 @end example
2066
2067 @geindex gnatname
2068
2069 @node Handling Arbitrary File Naming Conventions with gnatname,File Name Krunching with gnatkr,Alternative File Naming Schemes,File Naming Topics and Utilities
2070 @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}
2071 @subsection Handling Arbitrary File Naming Conventions with @code{gnatname}
2072
2073
2074 @geindex File Naming Conventions
2075
2076 @menu
2077 * Arbitrary File Naming Conventions::
2078 * Running gnatname::
2079 * Switches for gnatname::
2080 * Examples of gnatname Usage::
2081
2082 @end menu
2083
2084 @node Arbitrary File Naming Conventions,Running gnatname,,Handling Arbitrary File Naming Conventions with gnatname
2085 @anchor{gnat_ugn/the_gnat_compilation_model arbitrary-file-naming-conventions}@anchor{5b}@anchor{gnat_ugn/the_gnat_compilation_model id13}@anchor{5c}
2086 @subsubsection Arbitrary File Naming Conventions
2087
2088
2089 The GNAT compiler must be able to know the source file name of a compilation
2090 unit. When using the standard GNAT default file naming conventions
2091 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
2092 does not need additional information.
2093
2094 When the source file names do not follow the standard GNAT default file naming
2095 conventions, the GNAT compiler must be given additional information through
2096 a configuration pragmas file (@ref{14,,Configuration Pragmas})
2097 or a project file.
2098 When the non-standard file naming conventions are well-defined,
2099 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
2100 (@ref{58,,Alternative File Naming Schemes}) may be sufficient. However,
2101 if the file naming conventions are irregular or arbitrary, a number
2102 of pragma @code{Source_File_Name} for individual compilation units
2103 must be defined.
2104 To help maintain the correspondence between compilation unit names and
2105 source file names within the compiler,
2106 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
2107 set of files.
2108
2109 @node Running gnatname,Switches for gnatname,Arbitrary File Naming Conventions,Handling Arbitrary File Naming Conventions with gnatname
2110 @anchor{gnat_ugn/the_gnat_compilation_model running-gnatname}@anchor{5d}@anchor{gnat_ugn/the_gnat_compilation_model id14}@anchor{5e}
2111 @subsubsection Running @code{gnatname}
2112
2113
2114 The usual form of the @code{gnatname} command is:
2115
2116 @example
2117 $ gnatname [ switches ] naming_pattern [ naming_patterns ]
2118 [--and [ switches ] naming_pattern [ naming_patterns ]]
2119 @end example
2120
2121 All of the arguments are optional. If invoked without any argument,
2122 @code{gnatname} will display its usage.
2123
2124 When used with at least one naming pattern, @code{gnatname} will attempt to
2125 find all the compilation units in files that follow at least one of the
2126 naming patterns. To find these compilation units,
2127 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
2128 regular files.
2129
2130 One or several Naming Patterns may be given as arguments to @code{gnatname}.
2131 Each Naming Pattern is enclosed between double quotes (or single
2132 quotes on Windows).
2133 A Naming Pattern is a regular expression similar to the wildcard patterns
2134 used in file names by the Unix shells or the DOS prompt.
2135
2136 @code{gnatname} may be called with several sections of directories/patterns.
2137 Sections are separated by the switch @code{--and}. In each section, there must be
2138 at least one pattern. If no directory is specified in a section, the current
2139 directory (or the project directory if @code{-P} is used) is implied.
2140 The options other that the directory switches and the patterns apply globally
2141 even if they are in different sections.
2142
2143 Examples of Naming Patterns are:
2144
2145 @example
2146 "*.[12].ada"
2147 "*.ad[sb]*"
2148 "body_*" "spec_*"
2149 @end example
2150
2151 For a more complete description of the syntax of Naming Patterns,
2152 see the second kind of regular expressions described in @code{g-regexp.ads}
2153 (the 'Glob' regular expressions).
2154
2155 When invoked without the switch @code{-P}, @code{gnatname} will create a
2156 configuration pragmas file @code{gnat.adc} in the current working directory,
2157 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
2158 unit.
2159
2160 @node Switches for gnatname,Examples of gnatname Usage,Running gnatname,Handling Arbitrary File Naming Conventions with gnatname
2161 @anchor{gnat_ugn/the_gnat_compilation_model id15}@anchor{5f}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatname}@anchor{60}
2162 @subsubsection Switches for @code{gnatname}
2163
2164
2165 Switches for @code{gnatname} must precede any specified Naming Pattern.
2166
2167 You may specify any of the following switches to @code{gnatname}:
2168
2169 @geindex --version (gnatname)
2170
2171
2172 @table @asis
2173
2174 @item @code{--version}
2175
2176 Display Copyright and version, then exit disregarding all other options.
2177 @end table
2178
2179 @geindex --help (gnatname)
2180
2181
2182 @table @asis
2183
2184 @item @code{--help}
2185
2186 If @code{--version} was not used, display usage, then exit disregarding
2187 all other options.
2188
2189 @item @code{--subdirs=@emph{dir}}
2190
2191 Real object, library or exec directories are subdirectories <dir> of the
2192 specified ones.
2193
2194 @item @code{--no-backup}
2195
2196 Do not create a backup copy of an existing project file.
2197
2198 @item @code{--and}
2199
2200 Start another section of directories/patterns.
2201 @end table
2202
2203 @geindex -c (gnatname)
2204
2205
2206 @table @asis
2207
2208 @item @code{-c@emph{filename}}
2209
2210 Create a configuration pragmas file @code{filename} (instead of the default
2211 @code{gnat.adc}).
2212 There may be zero, one or more space between @code{-c} and
2213 @code{filename}.
2214 @code{filename} may include directory information. @code{filename} must be
2215 writable. There may be only one switch @code{-c}.
2216 When a switch @code{-c} is
2217 specified, no switch @code{-P} may be specified (see below).
2218 @end table
2219
2220 @geindex -d (gnatname)
2221
2222
2223 @table @asis
2224
2225 @item @code{-d@emph{dir}}
2226
2227 Look for source files in directory @code{dir}. There may be zero, one or more
2228 spaces between @code{-d} and @code{dir}.
2229 @code{dir} may end with @code{/**}, that is it may be of the form
2230 @code{root_dir/**}. In this case, the directory @code{root_dir} and all of its
2231 subdirectories, recursively, have to be searched for sources.
2232 When a switch @code{-d}
2233 is specified, the current working directory will not be searched for source
2234 files, unless it is explicitly specified with a @code{-d}
2235 or @code{-D} switch.
2236 Several switches @code{-d} may be specified.
2237 If @code{dir} is a relative path, it is relative to the directory of
2238 the configuration pragmas file specified with switch
2239 @code{-c},
2240 or to the directory of the project file specified with switch
2241 @code{-P} or,
2242 if neither switch @code{-c}
2243 nor switch @code{-P} are specified, it is relative to the
2244 current working directory. The directory
2245 specified with switch @code{-d} must exist and be readable.
2246 @end table
2247
2248 @geindex -D (gnatname)
2249
2250
2251 @table @asis
2252
2253 @item @code{-D@emph{filename}}
2254
2255 Look for source files in all directories listed in text file @code{filename}.
2256 There may be zero, one or more spaces between @code{-D}
2257 and @code{filename}.
2258 @code{filename} must be an existing, readable text file.
2259 Each nonempty line in @code{filename} must be a directory.
2260 Specifying switch @code{-D} is equivalent to specifying as many
2261 switches @code{-d} as there are nonempty lines in
2262 @code{file}.
2263
2264 @item @code{-eL}
2265
2266 Follow symbolic links when processing project files.
2267
2268 @geindex -f (gnatname)
2269
2270 @item @code{-f@emph{pattern}}
2271
2272 Foreign patterns. Using this switch, it is possible to add sources of languages
2273 other than Ada to the list of sources of a project file.
2274 It is only useful if a -P switch is used.
2275 For example,
2276
2277 @example
2278 gnatname -Pprj -f"*.c" "*.ada"
2279 @end example
2280
2281 will look for Ada units in all files with the @code{.ada} extension,
2282 and will add to the list of file for project @code{prj.gpr} the C files
2283 with extension @code{.c}.
2284
2285 @geindex -h (gnatname)
2286
2287 @item @code{-h}
2288
2289 Output usage (help) information. The output is written to @code{stdout}.
2290
2291 @geindex -P (gnatname)
2292
2293 @item @code{-P@emph{proj}}
2294
2295 Create or update project file @code{proj}. There may be zero, one or more space
2296 between @code{-P} and @code{proj}. @code{proj} may include directory
2297 information. @code{proj} must be writable.
2298 There may be only one switch @code{-P}.
2299 When a switch @code{-P} is specified,
2300 no switch @code{-c} may be specified.
2301 On all platforms, except on VMS, when @code{gnatname} is invoked for an
2302 existing project file <proj>.gpr, a backup copy of the project file is created
2303 in the project directory with file name <proj>.gpr.saved_x. 'x' is the first
2304 non negative number that makes this backup copy a new file.
2305
2306 @geindex -v (gnatname)
2307
2308 @item @code{-v}
2309
2310 Verbose mode. Output detailed explanation of behavior to @code{stdout}.
2311 This includes name of the file written, the name of the directories to search
2312 and, for each file in those directories whose name matches at least one of
2313 the Naming Patterns, an indication of whether the file contains a unit,
2314 and if so the name of the unit.
2315 @end table
2316
2317 @geindex -v -v (gnatname)
2318
2319
2320 @table @asis
2321
2322 @item @code{-v -v}
2323
2324 Very Verbose mode. In addition to the output produced in verbose mode,
2325 for each file in the searched directories whose name matches none of
2326 the Naming Patterns, an indication is given that there is no match.
2327
2328 @geindex -x (gnatname)
2329
2330 @item @code{-x@emph{pattern}}
2331
2332 Excluded patterns. Using this switch, it is possible to exclude some files
2333 that would match the name patterns. For example,
2334
2335 @example
2336 gnatname -x "*_nt.ada" "*.ada"
2337 @end example
2338
2339 will look for Ada units in all files with the @code{.ada} extension,
2340 except those whose names end with @code{_nt.ada}.
2341 @end table
2342
2343 @node Examples of gnatname Usage,,Switches for gnatname,Handling Arbitrary File Naming Conventions with gnatname
2344 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatname-usage}@anchor{61}@anchor{gnat_ugn/the_gnat_compilation_model id16}@anchor{62}
2345 @subsubsection Examples of @code{gnatname} Usage
2346
2347
2348 @example
2349 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
2350 @end example
2351
2352 In this example, the directory @code{/home/me} must already exist
2353 and be writable. In addition, the directory
2354 @code{/home/me/sources} (specified by
2355 @code{-d sources}) must exist and be readable.
2356
2357 Note the optional spaces after @code{-c} and @code{-d}.
2358
2359 @example
2360 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
2361 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
2362 @end example
2363
2364 Note that several switches @code{-d} may be used,
2365 even in conjunction with one or several switches
2366 @code{-D}. Several Naming Patterns and one excluded pattern
2367 are used in this example.
2368
2369 @node File Name Krunching with gnatkr,Renaming Files with gnatchop,Handling Arbitrary File Naming Conventions with gnatname,File Naming Topics and Utilities
2370 @anchor{gnat_ugn/the_gnat_compilation_model file-name-krunching-with-gnatkr}@anchor{63}@anchor{gnat_ugn/the_gnat_compilation_model id17}@anchor{64}
2371 @subsection File Name Krunching with @code{gnatkr}
2372
2373
2374 @geindex gnatkr
2375
2376 This section discusses the method used by the compiler to shorten
2377 the default file names chosen for Ada units so that they do not
2378 exceed the maximum length permitted. It also describes the
2379 @code{gnatkr} utility that can be used to determine the result of
2380 applying this shortening.
2381
2382 @menu
2383 * About gnatkr::
2384 * Using gnatkr::
2385 * Krunching Method::
2386 * Examples of gnatkr Usage::
2387
2388 @end menu
2389
2390 @node About gnatkr,Using gnatkr,,File Name Krunching with gnatkr
2391 @anchor{gnat_ugn/the_gnat_compilation_model id18}@anchor{65}@anchor{gnat_ugn/the_gnat_compilation_model about-gnatkr}@anchor{66}
2392 @subsubsection About @code{gnatkr}
2393
2394
2395 The default file naming rule in GNAT
2396 is that the file name must be derived from
2397 the unit name. The exact default rule is as follows:
2398
2399
2400 @itemize *
2401
2402 @item
2403 Take the unit name and replace all dots by hyphens.
2404
2405 @item
2406 If such a replacement occurs in the
2407 second character position of a name, and the first character is
2408 @code{a}, @code{g}, @code{s}, or @code{i},
2409 then replace the dot by the character
2410 @code{~} (tilde)
2411 instead of a minus.
2412
2413 The reason for this exception is to avoid clashes
2414 with the standard names for children of System, Ada, Interfaces,
2415 and GNAT, which use the prefixes
2416 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
2417 respectively.
2418 @end itemize
2419
2420 The @code{-gnatk@emph{nn}}
2421 switch of the compiler activates a 'krunching'
2422 circuit that limits file names to nn characters (where nn is a decimal
2423 integer).
2424
2425 The @code{gnatkr} utility can be used to determine the krunched name for
2426 a given file, when krunched to a specified maximum length.
2427
2428 @node Using gnatkr,Krunching Method,About gnatkr,File Name Krunching with gnatkr
2429 @anchor{gnat_ugn/the_gnat_compilation_model id19}@anchor{67}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatkr}@anchor{54}
2430 @subsubsection Using @code{gnatkr}
2431
2432
2433 The @code{gnatkr} command has the form:
2434
2435 @example
2436 $ gnatkr name [ length ]
2437 @end example
2438
2439 @code{name} is the uncrunched file name, derived from the name of the unit
2440 in the standard manner described in the previous section (i.e., in particular
2441 all dots are replaced by hyphens). The file name may or may not have an
2442 extension (defined as a suffix of the form period followed by arbitrary
2443 characters other than period). If an extension is present then it will
2444 be preserved in the output. For example, when krunching @code{hellofile.ads}
2445 to eight characters, the result will be hellofil.ads.
2446
2447 Note: for compatibility with previous versions of @code{gnatkr} dots may
2448 appear in the name instead of hyphens, but the last dot will always be
2449 taken as the start of an extension. So if @code{gnatkr} is given an argument
2450 such as @code{Hello.World.adb} it will be treated exactly as if the first
2451 period had been a hyphen, and for example krunching to eight characters
2452 gives the result @code{hellworl.adb}.
2453
2454 Note that the result is always all lower case.
2455 Characters of the other case are folded as required.
2456
2457 @code{length} represents the length of the krunched name. The default
2458 when no argument is given is 8 characters. A length of zero stands for
2459 unlimited, in other words do not chop except for system files where the
2460 implied crunching length is always eight characters.
2461
2462 The output is the krunched name. The output has an extension only if the
2463 original argument was a file name with an extension.
2464
2465 @node Krunching Method,Examples of gnatkr Usage,Using gnatkr,File Name Krunching with gnatkr
2466 @anchor{gnat_ugn/the_gnat_compilation_model id20}@anchor{68}@anchor{gnat_ugn/the_gnat_compilation_model krunching-method}@anchor{69}
2467 @subsubsection Krunching Method
2468
2469
2470 The initial file name is determined by the name of the unit that the file
2471 contains. The name is formed by taking the full expanded name of the
2472 unit and replacing the separating dots with hyphens and
2473 using lowercase
2474 for all letters, except that a hyphen in the second character position is
2475 replaced by a tilde if the first character is
2476 @code{a}, @code{i}, @code{g}, or @code{s}.
2477 The extension is @code{.ads} for a
2478 spec and @code{.adb} for a body.
2479 Krunching does not affect the extension, but the file name is shortened to
2480 the specified length by following these rules:
2481
2482
2483 @itemize *
2484
2485 @item
2486 The name is divided into segments separated by hyphens, tildes or
2487 underscores and all hyphens, tildes, and underscores are
2488 eliminated. If this leaves the name short enough, we are done.
2489
2490 @item
2491 If the name is too long, the longest segment is located (left-most
2492 if there are two of equal length), and shortened by dropping
2493 its last character. This is repeated until the name is short enough.
2494
2495 As an example, consider the krunching of @code{our-strings-wide_fixed.adb}
2496 to fit the name into 8 characters as required by some operating systems:
2497
2498 @example
2499 our-strings-wide_fixed 22
2500 our strings wide fixed 19
2501 our string wide fixed 18
2502 our strin wide fixed 17
2503 our stri wide fixed 16
2504 our stri wide fixe 15
2505 our str wide fixe 14
2506 our str wid fixe 13
2507 our str wid fix 12
2508 ou str wid fix 11
2509 ou st wid fix 10
2510 ou st wi fix 9
2511 ou st wi fi 8
2512 Final file name: oustwifi.adb
2513 @end example
2514
2515 @item
2516 The file names for all predefined units are always krunched to eight
2517 characters. The krunching of these predefined units uses the following
2518 special prefix replacements:
2519
2520
2521 @multitable {xxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx}
2522 @item
2523
2524 Prefix
2525
2526 @tab
2527
2528 Replacement
2529
2530 @item
2531
2532 @code{ada-}
2533
2534 @tab
2535
2536 @code{a-}
2537
2538 @item
2539
2540 @code{gnat-}
2541
2542 @tab
2543
2544 @code{g-}
2545
2546 @item
2547
2548 @code{interfac es-}
2549
2550 @tab
2551
2552 @code{i-}
2553
2554 @item
2555
2556 @code{system-}
2557
2558 @tab
2559
2560 @code{s-}
2561
2562 @end multitable
2563
2564
2565 These system files have a hyphen in the second character position. That
2566 is why normal user files replace such a character with a
2567 tilde, to avoid confusion with system file names.
2568
2569 As an example of this special rule, consider
2570 @code{ada-strings-wide_fixed.adb}, which gets krunched as follows:
2571
2572 @example
2573 ada-strings-wide_fixed 22
2574 a- strings wide fixed 18
2575 a- string wide fixed 17
2576 a- strin wide fixed 16
2577 a- stri wide fixed 15
2578 a- stri wide fixe 14
2579 a- str wide fixe 13
2580 a- str wid fixe 12
2581 a- str wid fix 11
2582 a- st wid fix 10
2583 a- st wi fix 9
2584 a- st wi fi 8
2585 Final file name: a-stwifi.adb
2586 @end example
2587 @end itemize
2588
2589 Of course no file shortening algorithm can guarantee uniqueness over all
2590 possible unit names, and if file name krunching is used then it is your
2591 responsibility to ensure that no name clashes occur. The utility
2592 program @code{gnatkr} is supplied for conveniently determining the
2593 krunched name of a file.
2594
2595 @node Examples of gnatkr Usage,,Krunching Method,File Name Krunching with gnatkr
2596 @anchor{gnat_ugn/the_gnat_compilation_model id21}@anchor{6a}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatkr-usage}@anchor{6b}
2597 @subsubsection Examples of @code{gnatkr} Usage
2598
2599
2600 @example
2601 $ gnatkr very_long_unit_name.ads --> velounna.ads
2602 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
2603 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
2604 $ gnatkr grandparent-parent-child --> grparchi
2605 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
2606 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
2607 @end example
2608
2609 @node Renaming Files with gnatchop,,File Name Krunching with gnatkr,File Naming Topics and Utilities
2610 @anchor{gnat_ugn/the_gnat_compilation_model id22}@anchor{6c}@anchor{gnat_ugn/the_gnat_compilation_model renaming-files-with-gnatchop}@anchor{36}
2611 @subsection Renaming Files with @code{gnatchop}
2612
2613
2614 @geindex gnatchop
2615
2616 This section discusses how to handle files with multiple units by using
2617 the @code{gnatchop} utility. This utility is also useful in renaming
2618 files to meet the standard GNAT default file naming conventions.
2619
2620 @menu
2621 * Handling Files with Multiple Units::
2622 * Operating gnatchop in Compilation Mode::
2623 * Command Line for gnatchop::
2624 * Switches for gnatchop::
2625 * Examples of gnatchop Usage::
2626
2627 @end menu
2628
2629 @node Handling Files with Multiple Units,Operating gnatchop in Compilation Mode,,Renaming Files with gnatchop
2630 @anchor{gnat_ugn/the_gnat_compilation_model id23}@anchor{6d}@anchor{gnat_ugn/the_gnat_compilation_model handling-files-with-multiple-units}@anchor{6e}
2631 @subsubsection Handling Files with Multiple Units
2632
2633
2634 The basic compilation model of GNAT requires that a file submitted to the
2635 compiler have only one unit and there be a strict correspondence
2636 between the file name and the unit name.
2637
2638 The @code{gnatchop} utility allows both of these rules to be relaxed,
2639 allowing GNAT to process files which contain multiple compilation units
2640 and files with arbitrary file names. @code{gnatchop}
2641 reads the specified file and generates one or more output files,
2642 containing one unit per file. The unit and the file name correspond,
2643 as required by GNAT.
2644
2645 If you want to permanently restructure a set of 'foreign' files so that
2646 they match the GNAT rules, and do the remaining development using the
2647 GNAT structure, you can simply use @code{gnatchop} once, generate the
2648 new set of files and work with them from that point on.
2649
2650 Alternatively, if you want to keep your files in the 'foreign' format,
2651 perhaps to maintain compatibility with some other Ada compilation
2652 system, you can set up a procedure where you use @code{gnatchop} each
2653 time you compile, regarding the source files that it writes as temporary
2654 files that you throw away.
2655
2656 Note that if your file containing multiple units starts with a byte order
2657 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
2658 will each start with a copy of this BOM, meaning that they can be compiled
2659 automatically in UTF-8 mode without needing to specify an explicit encoding.
2660
2661 @node Operating gnatchop in Compilation Mode,Command Line for gnatchop,Handling Files with Multiple Units,Renaming Files with gnatchop
2662 @anchor{gnat_ugn/the_gnat_compilation_model operating-gnatchop-in-compilation-mode}@anchor{6f}@anchor{gnat_ugn/the_gnat_compilation_model id24}@anchor{70}
2663 @subsubsection Operating gnatchop in Compilation Mode
2664
2665
2666 The basic function of @code{gnatchop} is to take a file with multiple units
2667 and split it into separate files. The boundary between files is reasonably
2668 clear, except for the issue of comments and pragmas. In default mode, the
2669 rule is that any pragmas between units belong to the previous unit, except
2670 that configuration pragmas always belong to the following unit. Any comments
2671 belong to the following unit. These rules
2672 almost always result in the right choice of
2673 the split point without needing to mark it explicitly and most users will
2674 find this default to be what they want. In this default mode it is incorrect to
2675 submit a file containing only configuration pragmas, or one that ends in
2676 configuration pragmas, to @code{gnatchop}.
2677
2678 However, using a special option to activate 'compilation mode',
2679 @code{gnatchop}
2680 can perform another function, which is to provide exactly the semantics
2681 required by the RM for handling of configuration pragmas in a compilation.
2682 In the absence of configuration pragmas (at the main file level), this
2683 option has no effect, but it causes such configuration pragmas to be handled
2684 in a quite different manner.
2685
2686 First, in compilation mode, if @code{gnatchop} is given a file that consists of
2687 only configuration pragmas, then this file is appended to the
2688 @code{gnat.adc} file in the current directory. This behavior provides
2689 the required behavior described in the RM for the actions to be taken
2690 on submitting such a file to the compiler, namely that these pragmas
2691 should apply to all subsequent compilations in the same compilation
2692 environment. Using GNAT, the current directory, possibly containing a
2693 @code{gnat.adc} file is the representation
2694 of a compilation environment. For more information on the
2695 @code{gnat.adc} file, see @ref{56,,Handling of Configuration Pragmas}.
2696
2697 Second, in compilation mode, if @code{gnatchop}
2698 is given a file that starts with
2699 configuration pragmas, and contains one or more units, then these
2700 configuration pragmas are prepended to each of the chopped files. This
2701 behavior provides the required behavior described in the RM for the
2702 actions to be taken on compiling such a file, namely that the pragmas
2703 apply to all units in the compilation, but not to subsequently compiled
2704 units.
2705
2706 Finally, if configuration pragmas appear between units, they are appended
2707 to the previous unit. This results in the previous unit being illegal,
2708 since the compiler does not accept configuration pragmas that follow
2709 a unit. This provides the required RM behavior that forbids configuration
2710 pragmas other than those preceding the first compilation unit of a
2711 compilation.
2712
2713 For most purposes, @code{gnatchop} will be used in default mode. The
2714 compilation mode described above is used only if you need exactly
2715 accurate behavior with respect to compilations, and you have files
2716 that contain multiple units and configuration pragmas. In this
2717 circumstance the use of @code{gnatchop} with the compilation mode
2718 switch provides the required behavior, and is for example the mode
2719 in which GNAT processes the ACVC tests.
2720
2721 @node Command Line for gnatchop,Switches for gnatchop,Operating gnatchop in Compilation Mode,Renaming Files with gnatchop
2722 @anchor{gnat_ugn/the_gnat_compilation_model id25}@anchor{71}@anchor{gnat_ugn/the_gnat_compilation_model command-line-for-gnatchop}@anchor{72}
2723 @subsubsection Command Line for @code{gnatchop}
2724
2725
2726 The @code{gnatchop} command has the form:
2727
2728 @example
2729 $ gnatchop switches file_name [file_name ...]
2730 [directory]
2731 @end example
2732
2733 The only required argument is the file name of the file to be chopped.
2734 There are no restrictions on the form of this file name. The file itself
2735 contains one or more Ada units, in normal GNAT format, concatenated
2736 together. As shown, more than one file may be presented to be chopped.
2737
2738 When run in default mode, @code{gnatchop} generates one output file in
2739 the current directory for each unit in each of the files.
2740
2741 @code{directory}, if specified, gives the name of the directory to which
2742 the output files will be written. If it is not specified, all files are
2743 written to the current directory.
2744
2745 For example, given a
2746 file called @code{hellofiles} containing
2747
2748 @example
2749 procedure Hello;
2750
2751 with Ada.Text_IO; use Ada.Text_IO;
2752 procedure Hello is
2753 begin
2754 Put_Line ("Hello");
2755 end Hello;
2756 @end example
2757
2758 the command
2759
2760 @example
2761 $ gnatchop hellofiles
2762 @end example
2763
2764 generates two files in the current directory, one called
2765 @code{hello.ads} containing the single line that is the procedure spec,
2766 and the other called @code{hello.adb} containing the remaining text. The
2767 original file is not affected. The generated files can be compiled in
2768 the normal manner.
2769
2770 When gnatchop is invoked on a file that is empty or that contains only empty
2771 lines and/or comments, gnatchop will not fail, but will not produce any
2772 new sources.
2773
2774 For example, given a
2775 file called @code{toto.txt} containing
2776
2777 @example
2778 -- Just a comment
2779 @end example
2780
2781 the command
2782
2783 @example
2784 $ gnatchop toto.txt
2785 @end example
2786
2787 will not produce any new file and will result in the following warnings:
2788
2789 @example
2790 toto.txt:1:01: warning: empty file, contains no compilation units
2791 no compilation units found
2792 no source files written
2793 @end example
2794
2795 @node Switches for gnatchop,Examples of gnatchop Usage,Command Line for gnatchop,Renaming Files with gnatchop
2796 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatchop}@anchor{73}@anchor{gnat_ugn/the_gnat_compilation_model id26}@anchor{74}
2797 @subsubsection Switches for @code{gnatchop}
2798
2799
2800 @code{gnatchop} recognizes the following switches:
2801
2802 @geindex --version (gnatchop)
2803
2804
2805 @table @asis
2806
2807 @item @code{--version}
2808
2809 Display Copyright and version, then exit disregarding all other options.
2810 @end table
2811
2812 @geindex --help (gnatchop)
2813
2814
2815 @table @asis
2816
2817 @item @code{--help}
2818
2819 If @code{--version} was not used, display usage, then exit disregarding
2820 all other options.
2821 @end table
2822
2823 @geindex -c (gnatchop)
2824
2825
2826 @table @asis
2827
2828 @item @code{-c}
2829
2830 Causes @code{gnatchop} to operate in compilation mode, in which
2831 configuration pragmas are handled according to strict RM rules. See
2832 previous section for a full description of this mode.
2833
2834 @item @code{-gnat@emph{xxx}}
2835
2836 This passes the given @code{-gnat@emph{xxx}} switch to @code{gnat} which is
2837 used to parse the given file. Not all @emph{xxx} options make sense,
2838 but for example, the use of @code{-gnati2} allows @code{gnatchop} to
2839 process a source file that uses Latin-2 coding for identifiers.
2840
2841 @item @code{-h}
2842
2843 Causes @code{gnatchop} to generate a brief help summary to the standard
2844 output file showing usage information.
2845 @end table
2846
2847 @geindex -k (gnatchop)
2848
2849
2850 @table @asis
2851
2852 @item @code{-k@emph{mm}}
2853
2854 Limit generated file names to the specified number @code{mm}
2855 of characters.
2856 This is useful if the
2857 resulting set of files is required to be interoperable with systems
2858 which limit the length of file names.
2859 No space is allowed between the @code{-k} and the numeric value. The numeric
2860 value may be omitted in which case a default of @code{-k8},
2861 suitable for use
2862 with DOS-like file systems, is used. If no @code{-k} switch
2863 is present then
2864 there is no limit on the length of file names.
2865 @end table
2866
2867 @geindex -p (gnatchop)
2868
2869
2870 @table @asis
2871
2872 @item @code{-p}
2873
2874 Causes the file modification time stamp of the input file to be
2875 preserved and used for the time stamp of the output file(s). This may be
2876 useful for preserving coherency of time stamps in an environment where
2877 @code{gnatchop} is used as part of a standard build process.
2878 @end table
2879
2880 @geindex -q (gnatchop)
2881
2882
2883 @table @asis
2884
2885 @item @code{-q}
2886
2887 Causes output of informational messages indicating the set of generated
2888 files to be suppressed. Warnings and error messages are unaffected.
2889 @end table
2890
2891 @geindex -r (gnatchop)
2892
2893 @geindex Source_Reference pragmas
2894
2895
2896 @table @asis
2897
2898 @item @code{-r}
2899
2900 Generate @code{Source_Reference} pragmas. Use this switch if the output
2901 files are regarded as temporary and development is to be done in terms
2902 of the original unchopped file. This switch causes
2903 @code{Source_Reference} pragmas to be inserted into each of the
2904 generated files to refers back to the original file name and line number.
2905 The result is that all error messages refer back to the original
2906 unchopped file.
2907 In addition, the debugging information placed into the object file (when
2908 the @code{-g} switch of @code{gcc} or @code{gnatmake} is
2909 specified)
2910 also refers back to this original file so that tools like profilers and
2911 debuggers will give information in terms of the original unchopped file.
2912
2913 If the original file to be chopped itself contains
2914 a @code{Source_Reference}
2915 pragma referencing a third file, then gnatchop respects
2916 this pragma, and the generated @code{Source_Reference} pragmas
2917 in the chopped file refer to the original file, with appropriate
2918 line numbers. This is particularly useful when @code{gnatchop}
2919 is used in conjunction with @code{gnatprep} to compile files that
2920 contain preprocessing statements and multiple units.
2921 @end table
2922
2923 @geindex -v (gnatchop)
2924
2925
2926 @table @asis
2927
2928 @item @code{-v}
2929
2930 Causes @code{gnatchop} to operate in verbose mode. The version
2931 number and copyright notice are output, as well as exact copies of
2932 the gnat1 commands spawned to obtain the chop control information.
2933 @end table
2934
2935 @geindex -w (gnatchop)
2936
2937
2938 @table @asis
2939
2940 @item @code{-w}
2941
2942 Overwrite existing file names. Normally @code{gnatchop} regards it as a
2943 fatal error if there is already a file with the same name as a
2944 file it would otherwise output, in other words if the files to be
2945 chopped contain duplicated units. This switch bypasses this
2946 check, and causes all but the last instance of such duplicated
2947 units to be skipped.
2948 @end table
2949
2950 @geindex --GCC= (gnatchop)
2951
2952
2953 @table @asis
2954
2955 @item @code{--GCC=@emph{xxxx}}
2956
2957 Specify the path of the GNAT parser to be used. When this switch is used,
2958 no attempt is made to add the prefix to the GNAT parser executable.
2959 @end table
2960
2961 @node Examples of gnatchop Usage,,Switches for gnatchop,Renaming Files with gnatchop
2962 @anchor{gnat_ugn/the_gnat_compilation_model id27}@anchor{75}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatchop-usage}@anchor{76}
2963 @subsubsection Examples of @code{gnatchop} Usage
2964
2965
2966 @example
2967 $ gnatchop -w hello_s.ada prerelease/files
2968 @end example
2969
2970 Chops the source file @code{hello_s.ada}. The output files will be
2971 placed in the directory @code{prerelease/files},
2972 overwriting any
2973 files with matching names in that directory (no files in the current
2974 directory are modified).
2975
2976 @example
2977 $ gnatchop archive
2978 @end example
2979
2980 Chops the source file @code{archive}
2981 into the current directory. One
2982 useful application of @code{gnatchop} is in sending sets of sources
2983 around, for example in email messages. The required sources are simply
2984 concatenated (for example, using a Unix @code{cat}
2985 command), and then
2986 @code{gnatchop} is used at the other end to reconstitute the original
2987 file names.
2988
2989 @example
2990 $ gnatchop file1 file2 file3 direc
2991 @end example
2992
2993 Chops all units in files @code{file1}, @code{file2}, @code{file3}, placing
2994 the resulting files in the directory @code{direc}. Note that if any units
2995 occur more than once anywhere within this set of files, an error message
2996 is generated, and no files are written. To override this check, use the
2997 @code{-w} switch,
2998 in which case the last occurrence in the last file will
2999 be the one that is output, and earlier duplicate occurrences for a given
3000 unit will be skipped.
3001
3002 @node Configuration Pragmas,Generating Object Files,File Naming Topics and Utilities,The GNAT Compilation Model
3003 @anchor{gnat_ugn/the_gnat_compilation_model id28}@anchor{77}@anchor{gnat_ugn/the_gnat_compilation_model configuration-pragmas}@anchor{14}
3004 @section Configuration Pragmas
3005
3006
3007 @geindex Configuration pragmas
3008
3009 @geindex Pragmas
3010 @geindex configuration
3011
3012 Configuration pragmas include those pragmas described as
3013 such in the Ada Reference Manual, as well as
3014 implementation-dependent pragmas that are configuration pragmas.
3015 See the @code{Implementation_Defined_Pragmas} chapter in the
3016 @cite{GNAT_Reference_Manual} for details on these
3017 additional GNAT-specific configuration pragmas.
3018 Most notably, the pragma @code{Source_File_Name}, which allows
3019 specifying non-default names for source files, is a configuration
3020 pragma. The following is a complete list of configuration pragmas
3021 recognized by GNAT:
3022
3023 @example
3024 Ada_83
3025 Ada_95
3026 Ada_05
3027 Ada_2005
3028 Ada_12
3029 Ada_2012
3030 Allow_Integer_Address
3031 Annotate
3032 Assertion_Policy
3033 Assume_No_Invalid_Values
3034 C_Pass_By_Copy
3035 Check_Float_Overflow
3036 Check_Name
3037 Check_Policy
3038 Compile_Time_Error
3039 Compile_Time_Warning
3040 Compiler_Unit
3041 Compiler_Unit_Warning
3042 Component_Alignment
3043 Convention_Identifier
3044 Debug_Policy
3045 Detect_Blocking
3046 Default_Scalar_Storage_Order
3047 Default_Storage_Pool
3048 Disable_Atomic_Synchronization
3049 Discard_Names
3050 Elaboration_Checks
3051 Eliminate
3052 Enable_Atomic_Synchronization
3053 Extend_System
3054 Extensions_Allowed
3055 External_Name_Casing
3056 Fast_Math
3057 Favor_Top_Level
3058 Ignore_Pragma
3059 Implicit_Packing
3060 Initialize_Scalars
3061 Interrupt_State
3062 License
3063 Locking_Policy
3064 No_Component_Reordering
3065 No_Heap_Finalization
3066 No_Run_Time
3067 No_Strict_Aliasing
3068 Normalize_Scalars
3069 Optimize_Alignment
3070 Overflow_Mode
3071 Overriding_Renamings
3072 Partition_Elaboration_Policy
3073 Persistent_BSS
3074 Polling
3075 Prefix_Exception_Messages
3076 Priority_Specific_Dispatching
3077 Profile
3078 Profile_Warnings
3079 Propagate_Exceptions
3080 Queuing_Policy
3081 Rational
3082 Ravenscar
3083 Rename_Pragma
3084 Restricted_Run_Time
3085 Restrictions
3086 Restrictions_Warnings
3087 Reviewable
3088 Short_Circuit_And_Or
3089 Short_Descriptors
3090 Source_File_Name
3091 Source_File_Name_Project
3092 SPARK_Mode
3093 Style_Checks
3094 Suppress
3095 Suppress_Exception_Locations
3096 Task_Dispatching_Policy
3097 Unevaluated_Use_Of_Old
3098 Universal_Data
3099 Unsuppress
3100 Use_VADS_Size
3101 Validity_Checks
3102 Warning_As_Error
3103 Warnings
3104 Wide_Character_Encoding
3105 @end example
3106
3107 @menu
3108 * Handling of Configuration Pragmas::
3109 * The Configuration Pragmas Files::
3110
3111 @end menu
3112
3113 @node Handling of Configuration Pragmas,The Configuration Pragmas Files,,Configuration Pragmas
3114 @anchor{gnat_ugn/the_gnat_compilation_model id29}@anchor{78}@anchor{gnat_ugn/the_gnat_compilation_model handling-of-configuration-pragmas}@anchor{56}
3115 @subsection Handling of Configuration Pragmas
3116
3117
3118 Configuration pragmas may either appear at the start of a compilation
3119 unit, or they can appear in a configuration pragma file to apply to
3120 all compilations performed in a given compilation environment.
3121
3122 GNAT also provides the @code{gnatchop} utility to provide an automatic
3123 way to handle configuration pragmas following the semantics for
3124 compilations (that is, files with multiple units), described in the RM.
3125 See @ref{6f,,Operating gnatchop in Compilation Mode} for details.
3126 However, for most purposes, it will be more convenient to edit the
3127 @code{gnat.adc} file that contains configuration pragmas directly,
3128 as described in the following section.
3129
3130 In the case of @code{Restrictions} pragmas appearing as configuration
3131 pragmas in individual compilation units, the exact handling depends on
3132 the type of restriction.
3133
3134 Restrictions that require partition-wide consistency (like
3135 @code{No_Tasking}) are
3136 recognized wherever they appear
3137 and can be freely inherited, e.g. from a @emph{with}ed unit to the @emph{with}ing
3138 unit. This makes sense since the binder will in any case insist on seeing
3139 consistent use, so any unit not conforming to any restrictions that are
3140 anywhere in the partition will be rejected, and you might as well find
3141 that out at compile time rather than at bind time.
3142
3143 For restrictions that do not require partition-wide consistency, e.g.
3144 SPARK or No_Implementation_Attributes, in general the restriction applies
3145 only to the unit in which the pragma appears, and not to any other units.
3146
3147 The exception is No_Elaboration_Code which always applies to the entire
3148 object file from a compilation, i.e. to the body, spec, and all subunits.
3149 This restriction can be specified in a configuration pragma file, or it
3150 can be on the body and/or the spec (in eithe case it applies to all the
3151 relevant units). It can appear on a subunit only if it has previously
3152 appeared in the body of spec.
3153
3154 @node The Configuration Pragmas Files,,Handling of Configuration Pragmas,Configuration Pragmas
3155 @anchor{gnat_ugn/the_gnat_compilation_model the-configuration-pragmas-files}@anchor{79}@anchor{gnat_ugn/the_gnat_compilation_model id30}@anchor{7a}
3156 @subsection The Configuration Pragmas Files
3157
3158
3159 @geindex gnat.adc
3160
3161 In GNAT a compilation environment is defined by the current
3162 directory at the time that a compile command is given. This current
3163 directory is searched for a file whose name is @code{gnat.adc}. If
3164 this file is present, it is expected to contain one or more
3165 configuration pragmas that will be applied to the current compilation.
3166 However, if the switch @code{-gnatA} is used, @code{gnat.adc} is not
3167 considered. When taken into account, @code{gnat.adc} is added to the
3168 dependencies, so that if @code{gnat.adc} is modified later, an invocation of
3169 @code{gnatmake} will recompile the source.
3170
3171 Configuration pragmas may be entered into the @code{gnat.adc} file
3172 either by running @code{gnatchop} on a source file that consists only of
3173 configuration pragmas, or more conveniently by direct editing of the
3174 @code{gnat.adc} file, which is a standard format source file.
3175
3176 Besides @code{gnat.adc}, additional files containing configuration
3177 pragmas may be applied to the current compilation using the switch
3178 @code{-gnatec=@emph{path}} where @code{path} must designate an existing file that
3179 contains only configuration pragmas. These configuration pragmas are
3180 in addition to those found in @code{gnat.adc} (provided @code{gnat.adc}
3181 is present and switch @code{-gnatA} is not used).
3182
3183 It is allowable to specify several switches @code{-gnatec=}, all of which
3184 will be taken into account.
3185
3186 Files containing configuration pragmas specified with switches
3187 @code{-gnatec=} are added to the dependencies, unless they are
3188 temporary files. A file is considered temporary if its name ends in
3189 @code{.tmp} or @code{.TMP}. Certain tools follow this naming
3190 convention because they pass information to @code{gcc} via
3191 temporary files that are immediately deleted; it doesn't make sense to
3192 depend on a file that no longer exists. Such tools include
3193 @code{gprbuild}, @code{gnatmake}, and @code{gnatcheck}.
3194
3195 If you are using project file, a separate mechanism is provided using
3196 project attributes.
3197
3198 @c --Comment
3199 @c See :ref:`Specifying_Configuration_Pragmas` for more details.
3200
3201 @node Generating Object Files,Source Dependencies,Configuration Pragmas,The GNAT Compilation Model
3202 @anchor{gnat_ugn/the_gnat_compilation_model generating-object-files}@anchor{40}@anchor{gnat_ugn/the_gnat_compilation_model id31}@anchor{7b}
3203 @section Generating Object Files
3204
3205
3206 An Ada program consists of a set of source files, and the first step in
3207 compiling the program is to generate the corresponding object files.
3208 These are generated by compiling a subset of these source files.
3209 The files you need to compile are the following:
3210
3211
3212 @itemize *
3213
3214 @item
3215 If a package spec has no body, compile the package spec to produce the
3216 object file for the package.
3217
3218 @item
3219 If a package has both a spec and a body, compile the body to produce the
3220 object file for the package. The source file for the package spec need
3221 not be compiled in this case because there is only one object file, which
3222 contains the code for both the spec and body of the package.
3223
3224 @item
3225 For a subprogram, compile the subprogram body to produce the object file
3226 for the subprogram. The spec, if one is present, is as usual in a
3227 separate file, and need not be compiled.
3228 @end itemize
3229
3230 @geindex Subunits
3231
3232
3233 @itemize *
3234
3235 @item
3236 In the case of subunits, only compile the parent unit. A single object
3237 file is generated for the entire subunit tree, which includes all the
3238 subunits.
3239
3240 @item
3241 Compile child units independently of their parent units
3242 (though, of course, the spec of all the ancestor unit must be present in order
3243 to compile a child unit).
3244
3245 @geindex Generics
3246
3247 @item
3248 Compile generic units in the same manner as any other units. The object
3249 files in this case are small dummy files that contain at most the
3250 flag used for elaboration checking. This is because GNAT always handles generic
3251 instantiation by means of macro expansion. However, it is still necessary to
3252 compile generic units, for dependency checking and elaboration purposes.
3253 @end itemize
3254
3255 The preceding rules describe the set of files that must be compiled to
3256 generate the object files for a program. Each object file has the same
3257 name as the corresponding source file, except that the extension is
3258 @code{.o} as usual.
3259
3260 You may wish to compile other files for the purpose of checking their
3261 syntactic and semantic correctness. For example, in the case where a
3262 package has a separate spec and body, you would not normally compile the
3263 spec. However, it is convenient in practice to compile the spec to make
3264 sure it is error-free before compiling clients of this spec, because such
3265 compilations will fail if there is an error in the spec.
3266
3267 GNAT provides an option for compiling such files purely for the
3268 purposes of checking correctness; such compilations are not required as
3269 part of the process of building a program. To compile a file in this
3270 checking mode, use the @code{-gnatc} switch.
3271
3272 @node Source Dependencies,The Ada Library Information Files,Generating Object Files,The GNAT Compilation Model
3273 @anchor{gnat_ugn/the_gnat_compilation_model id32}@anchor{7c}@anchor{gnat_ugn/the_gnat_compilation_model source-dependencies}@anchor{41}
3274 @section Source Dependencies
3275
3276
3277 A given object file clearly depends on the source file which is compiled
3278 to produce it. Here we are using "depends" in the sense of a typical
3279 @code{make} utility; in other words, an object file depends on a source
3280 file if changes to the source file require the object file to be
3281 recompiled.
3282 In addition to this basic dependency, a given object may depend on
3283 additional source files as follows:
3284
3285
3286 @itemize *
3287
3288 @item
3289 If a file being compiled @emph{with}s a unit @code{X}, the object file
3290 depends on the file containing the spec of unit @code{X}. This includes
3291 files that are @emph{with}ed implicitly either because they are parents
3292 of @emph{with}ed child units or they are run-time units required by the
3293 language constructs used in a particular unit.
3294
3295 @item
3296 If a file being compiled instantiates a library level generic unit, the
3297 object file depends on both the spec and body files for this generic
3298 unit.
3299
3300 @item
3301 If a file being compiled instantiates a generic unit defined within a
3302 package, the object file depends on the body file for the package as
3303 well as the spec file.
3304 @end itemize
3305
3306 @geindex Inline
3307
3308 @geindex -gnatn switch
3309
3310
3311 @itemize *
3312
3313 @item
3314 If a file being compiled contains a call to a subprogram for which
3315 pragma @code{Inline} applies and inlining is activated with the
3316 @code{-gnatn} switch, the object file depends on the file containing the
3317 body of this subprogram as well as on the file containing the spec. Note
3318 that for inlining to actually occur as a result of the use of this switch,
3319 it is necessary to compile in optimizing mode.
3320
3321 @geindex -gnatN switch
3322
3323 The use of @code{-gnatN} activates inlining optimization
3324 that is performed by the front end of the compiler. This inlining does
3325 not require that the code generation be optimized. Like @code{-gnatn},
3326 the use of this switch generates additional dependencies.
3327
3328 When using a gcc-based back end (in practice this means using any version
3329 of GNAT other than for the JVM, .NET or GNAAMP platforms), then the use of
3330 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
3331 Historically front end inlining was more extensive than the gcc back end
3332 inlining, but that is no longer the case.
3333
3334 @item
3335 If an object file @code{O} depends on the proper body of a subunit through
3336 inlining or instantiation, it depends on the parent unit of the subunit.
3337 This means that any modification of the parent unit or one of its subunits
3338 affects the compilation of @code{O}.
3339
3340 @item
3341 The object file for a parent unit depends on all its subunit body files.
3342
3343 @item
3344 The previous two rules meant that for purposes of computing dependencies and
3345 recompilation, a body and all its subunits are treated as an indivisible whole.
3346
3347 These rules are applied transitively: if unit @code{A} @emph{with}s
3348 unit @code{B}, whose elaboration calls an inlined procedure in package
3349 @code{C}, the object file for unit @code{A} will depend on the body of
3350 @code{C}, in file @code{c.adb}.
3351
3352 The set of dependent files described by these rules includes all the
3353 files on which the unit is semantically dependent, as dictated by the
3354 Ada language standard. However, it is a superset of what the
3355 standard describes, because it includes generic, inline, and subunit
3356 dependencies.
3357
3358 An object file must be recreated by recompiling the corresponding source
3359 file if any of the source files on which it depends are modified. For
3360 example, if the @code{make} utility is used to control compilation,
3361 the rule for an Ada object file must mention all the source files on
3362 which the object file depends, according to the above definition.
3363 The determination of the necessary
3364 recompilations is done automatically when one uses @code{gnatmake}.
3365 @end itemize
3366
3367 @node The Ada Library Information Files,Binding an Ada Program,Source Dependencies,The GNAT Compilation Model
3368 @anchor{gnat_ugn/the_gnat_compilation_model id33}@anchor{7d}@anchor{gnat_ugn/the_gnat_compilation_model the-ada-library-information-files}@anchor{42}
3369 @section The Ada Library Information Files
3370
3371
3372 @geindex Ada Library Information files
3373
3374 @geindex ALI files
3375
3376 Each compilation actually generates two output files. The first of these
3377 is the normal object file that has a @code{.o} extension. The second is a
3378 text file containing full dependency information. It has the same
3379 name as the source file, but an @code{.ali} extension.
3380 This file is known as the Ada Library Information (@code{ALI}) file.
3381 The following information is contained in the @code{ALI} file.
3382
3383
3384 @itemize *
3385
3386 @item
3387 Version information (indicates which version of GNAT was used to compile
3388 the unit(s) in question)
3389
3390 @item
3391 Main program information (including priority and time slice settings,
3392 as well as the wide character encoding used during compilation).
3393
3394 @item
3395 List of arguments used in the @code{gcc} command for the compilation
3396
3397 @item
3398 Attributes of the unit, including configuration pragmas used, an indication
3399 of whether the compilation was successful, exception model used etc.
3400
3401 @item
3402 A list of relevant restrictions applying to the unit (used for consistency)
3403 checking.
3404
3405 @item
3406 Categorization information (e.g., use of pragma @code{Pure}).
3407
3408 @item
3409 Information on all @emph{with}ed units, including presence of
3410 @code{Elaborate} or @code{Elaborate_All} pragmas.
3411
3412 @item
3413 Information from any @code{Linker_Options} pragmas used in the unit
3414
3415 @item
3416 Information on the use of @code{Body_Version} or @code{Version}
3417 attributes in the unit.
3418
3419 @item
3420 Dependency information. This is a list of files, together with
3421 time stamp and checksum information. These are files on which
3422 the unit depends in the sense that recompilation is required
3423 if any of these units are modified.
3424
3425 @item
3426 Cross-reference data. Contains information on all entities referenced
3427 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
3428 provide cross-reference information.
3429 @end itemize
3430
3431 For a full detailed description of the format of the @code{ALI} file,
3432 see the source of the body of unit @code{Lib.Writ}, contained in file
3433 @code{lib-writ.adb} in the GNAT compiler sources.
3434
3435 @node Binding an Ada Program,GNAT and Libraries,The Ada Library Information Files,The GNAT Compilation Model
3436 @anchor{gnat_ugn/the_gnat_compilation_model id34}@anchor{7e}@anchor{gnat_ugn/the_gnat_compilation_model binding-an-ada-program}@anchor{43}
3437 @section Binding an Ada Program
3438
3439
3440 When using languages such as C and C++, once the source files have been
3441 compiled the only remaining step in building an executable program
3442 is linking the object modules together. This means that it is possible to
3443 link an inconsistent version of a program, in which two units have
3444 included different versions of the same header.
3445
3446 The rules of Ada do not permit such an inconsistent program to be built.
3447 For example, if two clients have different versions of the same package,
3448 it is illegal to build a program containing these two clients.
3449 These rules are enforced by the GNAT binder, which also determines an
3450 elaboration order consistent with the Ada rules.
3451
3452 The GNAT binder is run after all the object files for a program have
3453 been created. It is given the name of the main program unit, and from
3454 this it determines the set of units required by the program, by reading the
3455 corresponding ALI files. It generates error messages if the program is
3456 inconsistent or if no valid order of elaboration exists.
3457
3458 If no errors are detected, the binder produces a main program, in Ada by
3459 default, that contains calls to the elaboration procedures of those
3460 compilation unit that require them, followed by
3461 a call to the main program. This Ada program is compiled to generate the
3462 object file for the main program. The name of
3463 the Ada file is @code{b~xxx}.adb` (with the corresponding spec
3464 @code{b~xxx}.ads`) where @code{xxx} is the name of the
3465 main program unit.
3466
3467 Finally, the linker is used to build the resulting executable program,
3468 using the object from the main program from the bind step as well as the
3469 object files for the Ada units of the program.
3470
3471 @node GNAT and Libraries,Conditional Compilation,Binding an Ada Program,The GNAT Compilation Model
3472 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-libraries}@anchor{15}@anchor{gnat_ugn/the_gnat_compilation_model id35}@anchor{7f}
3473 @section GNAT and Libraries
3474
3475
3476 @geindex Library building and using
3477
3478 This section describes how to build and use libraries with GNAT, and also shows
3479 how to recompile the GNAT run-time library. You should be familiar with the
3480 Project Manager facility (see the @emph{GNAT_Project_Manager} chapter of the
3481 @emph{GPRbuild User's Guide}) before reading this chapter.
3482
3483 @menu
3484 * Introduction to Libraries in GNAT::
3485 * General Ada Libraries::
3486 * Stand-alone Ada Libraries::
3487 * Rebuilding the GNAT Run-Time Library::
3488
3489 @end menu
3490
3491 @node Introduction to Libraries in GNAT,General Ada Libraries,,GNAT and Libraries
3492 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-libraries-in-gnat}@anchor{80}@anchor{gnat_ugn/the_gnat_compilation_model id36}@anchor{81}
3493 @subsection Introduction to Libraries in GNAT
3494
3495
3496 A library is, conceptually, a collection of objects which does not have its
3497 own main thread of execution, but rather provides certain services to the
3498 applications that use it. A library can be either statically linked with the
3499 application, in which case its code is directly included in the application,
3500 or, on platforms that support it, be dynamically linked, in which case
3501 its code is shared by all applications making use of this library.
3502
3503 GNAT supports both types of libraries.
3504 In the static case, the compiled code can be provided in different ways. The
3505 simplest approach is to provide directly the set of objects resulting from
3506 compilation of the library source files. Alternatively, you can group the
3507 objects into an archive using whatever commands are provided by the operating
3508 system. For the latter case, the objects are grouped into a shared library.
3509
3510 In the GNAT environment, a library has three types of components:
3511
3512
3513 @itemize *
3514
3515 @item
3516 Source files,
3517
3518 @item
3519 @code{ALI} files (see @ref{42,,The Ada Library Information Files}), and
3520
3521 @item
3522 Object files, an archive or a shared library.
3523 @end itemize
3524
3525 A GNAT library may expose all its source files, which is useful for
3526 documentation purposes. Alternatively, it may expose only the units needed by
3527 an external user to make use of the library. That is to say, the specs
3528 reflecting the library services along with all the units needed to compile
3529 those specs, which can include generic bodies or any body implementing an
3530 inlined routine. In the case of @emph{stand-alone libraries} those exposed
3531 units are called @emph{interface units} (@ref{82,,Stand-alone Ada Libraries}).
3532
3533 All compilation units comprising an application, including those in a library,
3534 need to be elaborated in an order partially defined by Ada's semantics. GNAT
3535 computes the elaboration order from the @code{ALI} files and this is why they
3536 constitute a mandatory part of GNAT libraries.
3537 @emph{Stand-alone libraries} are the exception to this rule because a specific
3538 library elaboration routine is produced independently of the application(s)
3539 using the library.
3540
3541 @node General Ada Libraries,Stand-alone Ada Libraries,Introduction to Libraries in GNAT,GNAT and Libraries
3542 @anchor{gnat_ugn/the_gnat_compilation_model general-ada-libraries}@anchor{83}@anchor{gnat_ugn/the_gnat_compilation_model id37}@anchor{84}
3543 @subsection General Ada Libraries
3544
3545
3546 @menu
3547 * Building a library::
3548 * Installing a library::
3549 * Using a library::
3550
3551 @end menu
3552
3553 @node Building a library,Installing a library,,General Ada Libraries
3554 @anchor{gnat_ugn/the_gnat_compilation_model building-a-library}@anchor{85}@anchor{gnat_ugn/the_gnat_compilation_model id38}@anchor{86}
3555 @subsubsection Building a library
3556
3557
3558 The easiest way to build a library is to use the Project Manager,
3559 which supports a special type of project called a @emph{Library Project}
3560 (see the @emph{Library Projects} section in the @emph{GNAT Project Manager}
3561 chapter of the @emph{GPRbuild User's Guide}).
3562
3563 A project is considered a library project, when two project-level attributes
3564 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
3565 control different aspects of library configuration, additional optional
3566 project-level attributes can be specified:
3567
3568
3569 @itemize *
3570
3571 @item
3572
3573 @table @asis
3574
3575 @item @code{Library_Kind}
3576
3577 This attribute controls whether the library is to be static or dynamic
3578 @end table
3579
3580 @item
3581
3582 @table @asis
3583
3584 @item @code{Library_Version}
3585
3586 This attribute specifies the library version; this value is used
3587 during dynamic linking of shared libraries to determine if the currently
3588 installed versions of the binaries are compatible.
3589 @end table
3590
3591 @item
3592 @code{Library_Options}
3593
3594 @item
3595
3596 @table @asis
3597
3598 @item @code{Library_GCC}
3599
3600 These attributes specify additional low-level options to be used during
3601 library generation, and redefine the actual application used to generate
3602 library.
3603 @end table
3604 @end itemize
3605
3606 The GNAT Project Manager takes full care of the library maintenance task,
3607 including recompilation of the source files for which objects do not exist
3608 or are not up to date, assembly of the library archive, and installation of
3609 the library (i.e., copying associated source, object and @code{ALI} files
3610 to the specified location).
3611
3612 Here is a simple library project file:
3613
3614 @example
3615 project My_Lib is
3616 for Source_Dirs use ("src1", "src2");
3617 for Object_Dir use "obj";
3618 for Library_Name use "mylib";
3619 for Library_Dir use "lib";
3620 for Library_Kind use "dynamic";
3621 end My_lib;
3622 @end example
3623
3624 and the compilation command to build and install the library:
3625
3626 @example
3627 $ gnatmake -Pmy_lib
3628 @end example
3629
3630 It is not entirely trivial to perform manually all the steps required to
3631 produce a library. We recommend that you use the GNAT Project Manager
3632 for this task. In special cases where this is not desired, the necessary
3633 steps are discussed below.
3634
3635 There are various possibilities for compiling the units that make up the
3636 library: for example with a Makefile (@ref{1f,,Using the GNU make Utility}) or
3637 with a conventional script. For simple libraries, it is also possible to create
3638 a dummy main program which depends upon all the packages that comprise the
3639 interface of the library. This dummy main program can then be given to
3640 @code{gnatmake}, which will ensure that all necessary objects are built.
3641
3642 After this task is accomplished, you should follow the standard procedure
3643 of the underlying operating system to produce the static or shared library.
3644
3645 Here is an example of such a dummy program:
3646
3647 @example
3648 with My_Lib.Service1;
3649 with My_Lib.Service2;
3650 with My_Lib.Service3;
3651 procedure My_Lib_Dummy is
3652 begin
3653 null;
3654 end;
3655 @end example
3656
3657 Here are the generic commands that will build an archive or a shared library.
3658
3659 @example
3660 # compiling the library
3661 $ gnatmake -c my_lib_dummy.adb
3662
3663 # we don't need the dummy object itself
3664 $ rm my_lib_dummy.o my_lib_dummy.ali
3665
3666 # create an archive with the remaining objects
3667 $ ar rc libmy_lib.a *.o
3668 # some systems may require "ranlib" to be run as well
3669
3670 # or create a shared library
3671 $ gcc -shared -o libmy_lib.so *.o
3672 # some systems may require the code to have been compiled with -fPIC
3673
3674 # remove the object files that are now in the library
3675 $ rm *.o
3676
3677 # Make the ALI files read-only so that gnatmake will not try to
3678 # regenerate the objects that are in the library
3679 $ chmod -w *.ali
3680 @end example
3681
3682 Please note that the library must have a name of the form @code{lib@emph{xxx}.a}
3683 or @code{lib@emph{xxx}.so} (or @code{lib@emph{xxx}.dll} on Windows) in order to
3684 be accessed by the directive @code{-l@emph{xxx}} at link time.
3685
3686 @node Installing a library,Using a library,Building a library,General Ada Libraries
3687 @anchor{gnat_ugn/the_gnat_compilation_model installing-a-library}@anchor{87}@anchor{gnat_ugn/the_gnat_compilation_model id39}@anchor{88}
3688 @subsubsection Installing a library
3689
3690
3691 @geindex ADA_PROJECT_PATH
3692
3693 @geindex GPR_PROJECT_PATH
3694
3695 If you use project files, library installation is part of the library build
3696 process (see the @emph{Installing a Library with Project Files} section of the
3697 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}).
3698
3699 When project files are not an option, it is also possible, but not recommended,
3700 to install the library so that the sources needed to use the library are on the
3701 Ada source path and the ALI files & libraries be on the Ada Object path (see
3702 @ref{89,,Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
3703 administrator can place general-purpose libraries in the default compiler
3704 paths, by specifying the libraries' location in the configuration files
3705 @code{ada_source_path} and @code{ada_object_path}. These configuration files
3706 must be located in the GNAT installation tree at the same place as the gcc spec
3707 file. The location of the gcc spec file can be determined as follows:
3708
3709 @example
3710 $ gcc -v
3711 @end example
3712
3713 The configuration files mentioned above have a simple format: each line
3714 must contain one unique directory name.
3715 Those names are added to the corresponding path
3716 in their order of appearance in the file. The names can be either absolute
3717 or relative; in the latter case, they are relative to where theses files
3718 are located.
3719
3720 The files @code{ada_source_path} and @code{ada_object_path} might not be
3721 present in a
3722 GNAT installation, in which case, GNAT will look for its run-time library in
3723 the directories @code{adainclude} (for the sources) and @code{adalib} (for the
3724 objects and @code{ALI} files). When the files exist, the compiler does not
3725 look in @code{adainclude} and @code{adalib}, and thus the
3726 @code{ada_source_path} file
3727 must contain the location for the GNAT run-time sources (which can simply
3728 be @code{adainclude}). In the same way, the @code{ada_object_path} file must
3729 contain the location for the GNAT run-time objects (which can simply
3730 be @code{adalib}).
3731
3732 You can also specify a new default path to the run-time library at compilation
3733 time with the switch @code{--RTS=rts-path}. You can thus choose / change
3734 the run-time library you want your program to be compiled with. This switch is
3735 recognized by @code{gcc}, @code{gnatmake}, @code{gnatbind},
3736 @code{gnatls}, @code{gnatfind} and @code{gnatxref}.
3737
3738 It is possible to install a library before or after the standard GNAT
3739 library, by reordering the lines in the configuration files. In general, a
3740 library must be installed before the GNAT library if it redefines
3741 any part of it.
3742
3743 @node Using a library,,Installing a library,General Ada Libraries
3744 @anchor{gnat_ugn/the_gnat_compilation_model using-a-library}@anchor{8a}@anchor{gnat_ugn/the_gnat_compilation_model id40}@anchor{8b}
3745 @subsubsection Using a library
3746
3747
3748 Once again, the project facility greatly simplifies the use of
3749 libraries. In this context, using a library is just a matter of adding a
3750 @emph{with} clause in the user project. For instance, to make use of the
3751 library @code{My_Lib} shown in examples in earlier sections, you can
3752 write:
3753
3754 @example
3755 with "my_lib";
3756 project My_Proj is
3757 ...
3758 end My_Proj;
3759 @end example
3760
3761 Even if you have a third-party, non-Ada library, you can still use GNAT's
3762 Project Manager facility to provide a wrapper for it. For example, the
3763 following project, when @emph{with}ed by your main project, will link with the
3764 third-party library @code{liba.a}:
3765
3766 @example
3767 project Liba is
3768 for Externally_Built use "true";
3769 for Source_Files use ();
3770 for Library_Dir use "lib";
3771 for Library_Name use "a";
3772 for Library_Kind use "static";
3773 end Liba;
3774 @end example
3775
3776 This is an alternative to the use of @code{pragma Linker_Options}. It is
3777 especially interesting in the context of systems with several interdependent
3778 static libraries where finding a proper linker order is not easy and best be
3779 left to the tools having visibility over project dependence information.
3780
3781 In order to use an Ada library manually, you need to make sure that this
3782 library is on both your source and object path
3783 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}
3784 and @ref{8c,,Search Paths for gnatbind}). Furthermore, when the objects are grouped
3785 in an archive or a shared library, you need to specify the desired
3786 library at link time.
3787
3788 For example, you can use the library @code{mylib} installed in
3789 @code{/dir/my_lib_src} and @code{/dir/my_lib_obj} with the following commands:
3790
3791 @example
3792 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
3793 -largs -lmy_lib
3794 @end example
3795
3796 This can be expressed more simply:
3797
3798 @example
3799 $ gnatmake my_appl
3800 @end example
3801
3802 when the following conditions are met:
3803
3804
3805 @itemize *
3806
3807 @item
3808 @code{/dir/my_lib_src} has been added by the user to the environment
3809 variable
3810 @geindex ADA_INCLUDE_PATH
3811 @geindex environment variable; ADA_INCLUDE_PATH
3812 @code{ADA_INCLUDE_PATH}, or by the administrator to the file
3813 @code{ada_source_path}
3814
3815 @item
3816 @code{/dir/my_lib_obj} has been added by the user to the environment
3817 variable
3818 @geindex ADA_OBJECTS_PATH
3819 @geindex environment variable; ADA_OBJECTS_PATH
3820 @code{ADA_OBJECTS_PATH}, or by the administrator to the file
3821 @code{ada_object_path}
3822
3823 @item
3824 a pragma @code{Linker_Options} has been added to one of the sources.
3825 For example:
3826
3827 @example
3828 pragma Linker_Options ("-lmy_lib");
3829 @end example
3830 @end itemize
3831
3832 Note that you may also load a library dynamically at
3833 run time given its filename, as illustrated in the GNAT @code{plugins} example
3834 in the directory @code{share/examples/gnat/plugins} within the GNAT
3835 install area.
3836
3837 @node Stand-alone Ada Libraries,Rebuilding the GNAT Run-Time Library,General Ada Libraries,GNAT and Libraries
3838 @anchor{gnat_ugn/the_gnat_compilation_model stand-alone-ada-libraries}@anchor{82}@anchor{gnat_ugn/the_gnat_compilation_model id41}@anchor{8d}
3839 @subsection Stand-alone Ada Libraries
3840
3841
3842 @geindex Stand-alone libraries
3843
3844 @menu
3845 * Introduction to Stand-alone Libraries::
3846 * Building a Stand-alone Library::
3847 * Creating a Stand-alone Library to be used in a non-Ada context::
3848 * Restrictions in Stand-alone Libraries::
3849
3850 @end menu
3851
3852 @node Introduction to Stand-alone Libraries,Building a Stand-alone Library,,Stand-alone Ada Libraries
3853 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-stand-alone-libraries}@anchor{8e}@anchor{gnat_ugn/the_gnat_compilation_model id42}@anchor{8f}
3854 @subsubsection Introduction to Stand-alone Libraries
3855
3856
3857 A Stand-alone Library (abbreviated 'SAL') is a library that contains the
3858 necessary code to
3859 elaborate the Ada units that are included in the library. In contrast with
3860 an ordinary library, which consists of all sources, objects and @code{ALI}
3861 files of the
3862 library, a SAL may specify a restricted subset of compilation units
3863 to serve as a library interface. In this case, the fully
3864 self-sufficient set of files will normally consist of an objects
3865 archive, the sources of interface units' specs, and the @code{ALI}
3866 files of interface units.
3867 If an interface spec contains a generic unit or an inlined subprogram,
3868 the body's
3869 source must also be provided; if the units that must be provided in the source
3870 form depend on other units, the source and @code{ALI} files of those must
3871 also be provided.
3872
3873 The main purpose of a SAL is to minimize the recompilation overhead of client
3874 applications when a new version of the library is installed. Specifically,
3875 if the interface sources have not changed, client applications do not need to
3876 be recompiled. If, furthermore, a SAL is provided in the shared form and its
3877 version, controlled by @code{Library_Version} attribute, is not changed,
3878 then the clients do not need to be relinked.
3879
3880 SALs also allow the library providers to minimize the amount of library source
3881 text exposed to the clients. Such 'information hiding' might be useful or
3882 necessary for various reasons.
3883
3884 Stand-alone libraries are also well suited to be used in an executable whose
3885 main routine is not written in Ada.
3886
3887 @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
3888 @anchor{gnat_ugn/the_gnat_compilation_model id43}@anchor{90}@anchor{gnat_ugn/the_gnat_compilation_model building-a-stand-alone-library}@anchor{91}
3889 @subsubsection Building a Stand-alone Library
3890
3891
3892 GNAT's Project facility provides a simple way of building and installing
3893 stand-alone libraries; see the @emph{Stand-alone Library Projects} section
3894 in the @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}.
3895 To be a Stand-alone Library Project, in addition to the two attributes
3896 that make a project a Library Project (@code{Library_Name} and
3897 @code{Library_Dir}; see the @emph{Library Projects} section in the
3898 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}),
3899 the attribute @code{Library_Interface} must be defined. For example:
3900
3901 @example
3902 for Library_Dir use "lib_dir";
3903 for Library_Name use "dummy";
3904 for Library_Interface use ("int1", "int1.child");
3905 @end example
3906
3907 Attribute @code{Library_Interface} has a non-empty string list value,
3908 each string in the list designating a unit contained in an immediate source
3909 of the project file.
3910
3911 When a Stand-alone Library is built, first the binder is invoked to build
3912 a package whose name depends on the library name
3913 (@code{b~dummy.ads/b} in the example above).
3914 This binder-generated package includes initialization and
3915 finalization procedures whose
3916 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
3917 in the example
3918 above). The object corresponding to this package is included in the library.
3919
3920 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
3921 calling of these procedures if a static SAL is built, or if a shared SAL
3922 is built
3923 with the project-level attribute @code{Library_Auto_Init} set to
3924 @code{"false"}.
3925
3926 For a Stand-Alone Library, only the @code{ALI} files of the Interface Units
3927 (those that are listed in attribute @code{Library_Interface}) are copied to
3928 the Library Directory. As a consequence, only the Interface Units may be
3929 imported from Ada units outside of the library. If other units are imported,
3930 the binding phase will fail.
3931
3932 It is also possible to build an encapsulated library where not only
3933 the code to elaborate and finalize the library is embedded but also
3934 ensuring that the library is linked only against static
3935 libraries. So an encapsulated library only depends on system
3936 libraries, all other code, including the GNAT runtime, is embedded. To
3937 build an encapsulated library the attribute
3938 @code{Library_Standalone} must be set to @code{encapsulated}:
3939
3940 @example
3941 for Library_Dir use "lib_dir";
3942 for Library_Name use "dummy";
3943 for Library_Kind use "dynamic";
3944 for Library_Interface use ("int1", "int1.child");
3945 for Library_Standalone use "encapsulated";
3946 @end example
3947
3948 The default value for this attribute is @code{standard} in which case
3949 a stand-alone library is built.
3950
3951 The attribute @code{Library_Src_Dir} may be specified for a
3952 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
3953 single string value. Its value must be the path (absolute or relative to the
3954 project directory) of an existing directory. This directory cannot be the
3955 object directory or one of the source directories, but it can be the same as
3956 the library directory. The sources of the Interface
3957 Units of the library that are needed by an Ada client of the library will be
3958 copied to the designated directory, called the Interface Copy directory.
3959 These sources include the specs of the Interface Units, but they may also
3960 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
3961 are used, or when there is a generic unit in the spec. Before the sources
3962 are copied to the Interface Copy directory, an attempt is made to delete all
3963 files in the Interface Copy directory.
3964
3965 Building stand-alone libraries by hand is somewhat tedious, but for those
3966 occasions when it is necessary here are the steps that you need to perform:
3967
3968
3969 @itemize *
3970
3971 @item
3972 Compile all library sources.
3973
3974 @item
3975 Invoke the binder with the switch @code{-n} (No Ada main program),
3976 with all the @code{ALI} files of the interfaces, and
3977 with the switch @code{-L} to give specific names to the @code{init}
3978 and @code{final} procedures. For example:
3979
3980 @example
3981 $ gnatbind -n int1.ali int2.ali -Lsal1
3982 @end example
3983
3984 @item
3985 Compile the binder generated file:
3986
3987 @example
3988 $ gcc -c b~int2.adb
3989 @end example
3990
3991 @item
3992 Link the dynamic library with all the necessary object files,
3993 indicating to the linker the names of the @code{init} (and possibly
3994 @code{final}) procedures for automatic initialization (and finalization).
3995 The built library should be placed in a directory different from
3996 the object directory.
3997
3998 @item
3999 Copy the @code{ALI} files of the interface to the library directory,
4000 add in this copy an indication that it is an interface to a SAL
4001 (i.e., add a word @code{SL} on the line in the @code{ALI} file that starts
4002 with letter 'P') and make the modified copy of the @code{ALI} file
4003 read-only.
4004 @end itemize
4005
4006 Using SALs is not different from using other libraries
4007 (see @ref{8a,,Using a library}).
4008
4009 @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
4010 @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}
4011 @subsubsection Creating a Stand-alone Library to be used in a non-Ada context
4012
4013
4014 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
4015 a non-Ada context.
4016
4017 The only extra step required is to ensure that library interface subprograms
4018 are compatible with the main program, by means of @code{pragma Export}
4019 or @code{pragma Convention}.
4020
4021 Here is an example of simple library interface for use with C main program:
4022
4023 @example
4024 package My_Package is
4025
4026 procedure Do_Something;
4027 pragma Export (C, Do_Something, "do_something");
4028
4029 procedure Do_Something_Else;
4030 pragma Export (C, Do_Something_Else, "do_something_else");
4031
4032 end My_Package;
4033 @end example
4034
4035 On the foreign language side, you must provide a 'foreign' view of the
4036 library interface; remember that it should contain elaboration routines in
4037 addition to interface subprograms.
4038
4039 The example below shows the content of @code{mylib_interface.h} (note
4040 that there is no rule for the naming of this file, any name can be used)
4041
4042 @example
4043 /* the library elaboration procedure */
4044 extern void mylibinit (void);
4045
4046 /* the library finalization procedure */
4047 extern void mylibfinal (void);
4048
4049 /* the interface exported by the library */
4050 extern void do_something (void);
4051 extern void do_something_else (void);
4052 @end example
4053
4054 Libraries built as explained above can be used from any program, provided
4055 that the elaboration procedures (named @code{mylibinit} in the previous
4056 example) are called before the library services are used. Any number of
4057 libraries can be used simultaneously, as long as the elaboration
4058 procedure of each library is called.
4059
4060 Below is an example of a C program that uses the @code{mylib} library.
4061
4062 @example
4063 #include "mylib_interface.h"
4064
4065 int
4066 main (void)
4067 @{
4068 /* First, elaborate the library before using it */
4069 mylibinit ();
4070
4071 /* Main program, using the library exported entities */
4072 do_something ();
4073 do_something_else ();
4074
4075 /* Library finalization at the end of the program */
4076 mylibfinal ();
4077 return 0;
4078 @}
4079 @end example
4080
4081 Note that invoking any library finalization procedure generated by
4082 @code{gnatbind} shuts down the Ada run-time environment.
4083 Consequently, the
4084 finalization of all Ada libraries must be performed at the end of the program.
4085 No call to these libraries or to the Ada run-time library should be made
4086 after the finalization phase.
4087
4088 Note also that special care must be taken with multi-tasks
4089 applications. The initialization and finalization routines are not
4090 protected against concurrent access. If such requirement is needed it
4091 must be ensured at the application level using a specific operating
4092 system services like a mutex or a critical-section.
4093
4094 @node Restrictions in Stand-alone Libraries,,Creating a Stand-alone Library to be used in a non-Ada context,Stand-alone Ada Libraries
4095 @anchor{gnat_ugn/the_gnat_compilation_model id45}@anchor{94}@anchor{gnat_ugn/the_gnat_compilation_model restrictions-in-stand-alone-libraries}@anchor{95}
4096 @subsubsection Restrictions in Stand-alone Libraries
4097
4098
4099 The pragmas listed below should be used with caution inside libraries,
4100 as they can create incompatibilities with other Ada libraries:
4101
4102
4103 @itemize *
4104
4105 @item
4106 pragma @code{Locking_Policy}
4107
4108 @item
4109 pragma @code{Partition_Elaboration_Policy}
4110
4111 @item
4112 pragma @code{Queuing_Policy}
4113
4114 @item
4115 pragma @code{Task_Dispatching_Policy}
4116
4117 @item
4118 pragma @code{Unreserve_All_Interrupts}
4119 @end itemize
4120
4121 When using a library that contains such pragmas, the user must make sure
4122 that all libraries use the same pragmas with the same values. Otherwise,
4123 @code{Program_Error} will
4124 be raised during the elaboration of the conflicting
4125 libraries. The usage of these pragmas and its consequences for the user
4126 should therefore be well documented.
4127
4128 Similarly, the traceback in the exception occurrence mechanism should be
4129 enabled or disabled in a consistent manner across all libraries.
4130 Otherwise, Program_Error will be raised during the elaboration of the
4131 conflicting libraries.
4132
4133 If the @code{Version} or @code{Body_Version}
4134 attributes are used inside a library, then you need to
4135 perform a @code{gnatbind} step that specifies all @code{ALI} files in all
4136 libraries, so that version identifiers can be properly computed.
4137 In practice these attributes are rarely used, so this is unlikely
4138 to be a consideration.
4139
4140 @node Rebuilding the GNAT Run-Time Library,,Stand-alone Ada Libraries,GNAT and Libraries
4141 @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}
4142 @subsection Rebuilding the GNAT Run-Time Library
4143
4144
4145 @geindex GNAT Run-Time Library
4146 @geindex rebuilding
4147
4148 @geindex Building the GNAT Run-Time Library
4149
4150 @geindex Rebuilding the GNAT Run-Time Library
4151
4152 @geindex Run-Time Library
4153 @geindex rebuilding
4154
4155 It may be useful to recompile the GNAT library in various contexts, the
4156 most important one being the use of partition-wide configuration pragmas
4157 such as @code{Normalize_Scalars}. A special Makefile called
4158 @code{Makefile.adalib} is provided to that effect and can be found in
4159 the directory containing the GNAT library. The location of this
4160 directory depends on the way the GNAT environment has been installed and can
4161 be determined by means of the command:
4162
4163 @example
4164 $ gnatls -v
4165 @end example
4166
4167 The last entry in the object search path usually contains the
4168 gnat library. This Makefile contains its own documentation and in
4169 particular the set of instructions needed to rebuild a new library and
4170 to use it.
4171
4172 @geindex Conditional compilation
4173
4174 @node Conditional Compilation,Mixed Language Programming,GNAT and Libraries,The GNAT Compilation Model
4175 @anchor{gnat_ugn/the_gnat_compilation_model id47}@anchor{98}@anchor{gnat_ugn/the_gnat_compilation_model conditional-compilation}@anchor{16}
4176 @section Conditional Compilation
4177
4178
4179 This section presents some guidelines for modeling conditional compilation in Ada and describes the
4180 gnatprep preprocessor utility.
4181
4182 @geindex Conditional compilation
4183
4184 @menu
4185 * Modeling Conditional Compilation in Ada::
4186 * Preprocessing with gnatprep::
4187 * Integrated Preprocessing::
4188
4189 @end menu
4190
4191 @node Modeling Conditional Compilation in Ada,Preprocessing with gnatprep,,Conditional Compilation
4192 @anchor{gnat_ugn/the_gnat_compilation_model modeling-conditional-compilation-in-ada}@anchor{99}@anchor{gnat_ugn/the_gnat_compilation_model id48}@anchor{9a}
4193 @subsection Modeling Conditional Compilation in Ada
4194
4195
4196 It is often necessary to arrange for a single source program
4197 to serve multiple purposes, where it is compiled in different
4198 ways to achieve these different goals. Some examples of the
4199 need for this feature are
4200
4201
4202 @itemize *
4203
4204 @item
4205 Adapting a program to a different hardware environment
4206
4207 @item
4208 Adapting a program to a different target architecture
4209
4210 @item
4211 Turning debugging features on and off
4212
4213 @item
4214 Arranging for a program to compile with different compilers
4215 @end itemize
4216
4217 In C, or C++, the typical approach would be to use the preprocessor
4218 that is defined as part of the language. The Ada language does not
4219 contain such a feature. This is not an oversight, but rather a very
4220 deliberate design decision, based on the experience that overuse of
4221 the preprocessing features in C and C++ can result in programs that
4222 are extremely difficult to maintain. For example, if we have ten
4223 switches that can be on or off, this means that there are a thousand
4224 separate programs, any one of which might not even be syntactically
4225 correct, and even if syntactically correct, the resulting program
4226 might not work correctly. Testing all combinations can quickly become
4227 impossible.
4228
4229 Nevertheless, the need to tailor programs certainly exists, and in
4230 this section we will discuss how this can
4231 be achieved using Ada in general, and GNAT in particular.
4232
4233 @menu
4234 * Use of Boolean Constants::
4235 * Debugging - A Special Case::
4236 * Conditionalizing Declarations::
4237 * Use of Alternative Implementations::
4238 * Preprocessing::
4239
4240 @end menu
4241
4242 @node Use of Boolean Constants,Debugging - A Special Case,,Modeling Conditional Compilation in Ada
4243 @anchor{gnat_ugn/the_gnat_compilation_model id49}@anchor{9b}@anchor{gnat_ugn/the_gnat_compilation_model use-of-boolean-constants}@anchor{9c}
4244 @subsubsection Use of Boolean Constants
4245
4246
4247 In the case where the difference is simply which code
4248 sequence is executed, the cleanest solution is to use Boolean
4249 constants to control which code is executed.
4250
4251 @example
4252 FP_Initialize_Required : constant Boolean := True;
4253 ...
4254 if FP_Initialize_Required then
4255 ...
4256 end if;
4257 @end example
4258
4259 Not only will the code inside the @code{if} statement not be executed if
4260 the constant Boolean is @code{False}, but it will also be completely
4261 deleted from the program.
4262 However, the code is only deleted after the @code{if} statement
4263 has been checked for syntactic and semantic correctness.
4264 (In contrast, with preprocessors the code is deleted before the
4265 compiler ever gets to see it, so it is not checked until the switch
4266 is turned on.)
4267
4268 @geindex Preprocessors (contrasted with conditional compilation)
4269
4270 Typically the Boolean constants will be in a separate package,
4271 something like:
4272
4273 @example
4274 package Config is
4275 FP_Initialize_Required : constant Boolean := True;
4276 Reset_Available : constant Boolean := False;
4277 ...
4278 end Config;
4279 @end example
4280
4281 The @code{Config} package exists in multiple forms for the various targets,
4282 with an appropriate script selecting the version of @code{Config} needed.
4283 Then any other unit requiring conditional compilation can do a @emph{with}
4284 of @code{Config} to make the constants visible.
4285
4286 @node Debugging - A Special Case,Conditionalizing Declarations,Use of Boolean Constants,Modeling Conditional Compilation in Ada
4287 @anchor{gnat_ugn/the_gnat_compilation_model debugging-a-special-case}@anchor{9d}@anchor{gnat_ugn/the_gnat_compilation_model id50}@anchor{9e}
4288 @subsubsection Debugging - A Special Case
4289
4290
4291 A common use of conditional code is to execute statements (for example
4292 dynamic checks, or output of intermediate results) under control of a
4293 debug switch, so that the debugging behavior can be turned on and off.
4294 This can be done using a Boolean constant to control whether the code
4295 is active:
4296
4297 @example
4298 if Debugging then
4299 Put_Line ("got to the first stage!");
4300 end if;
4301 @end example
4302
4303 or
4304
4305 @example
4306 if Debugging and then Temperature > 999.0 then
4307 raise Temperature_Crazy;
4308 end if;
4309 @end example
4310
4311 @geindex pragma Assert
4312
4313 Since this is a common case, there are special features to deal with
4314 this in a convenient manner. For the case of tests, Ada 2005 has added
4315 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
4316 on the @code{Assert} pragma that has always been available in GNAT, so this
4317 feature may be used with GNAT even if you are not using Ada 2005 features.
4318 The use of pragma @code{Assert} is described in the
4319 @cite{GNAT_Reference_Manual}, but as an
4320 example, the last test could be written:
4321
4322 @example
4323 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
4324 @end example
4325
4326 or simply
4327
4328 @example
4329 pragma Assert (Temperature <= 999.0);
4330 @end example
4331
4332 In both cases, if assertions are active and the temperature is excessive,
4333 the exception @code{Assert_Failure} will be raised, with the given string in
4334 the first case or a string indicating the location of the pragma in the second
4335 case used as the exception message.
4336
4337 @geindex pragma Assertion_Policy
4338
4339 You can turn assertions on and off by using the @code{Assertion_Policy}
4340 pragma.
4341
4342 @geindex -gnata switch
4343
4344 This is an Ada 2005 pragma which is implemented in all modes by
4345 GNAT. Alternatively, you can use the @code{-gnata} switch
4346 to enable assertions from the command line, which applies to
4347 all versions of Ada.
4348
4349 @geindex pragma Debug
4350
4351 For the example above with the @code{Put_Line}, the GNAT-specific pragma
4352 @code{Debug} can be used:
4353
4354 @example
4355 pragma Debug (Put_Line ("got to the first stage!"));
4356 @end example
4357
4358 If debug pragmas are enabled, the argument, which must be of the form of
4359 a procedure call, is executed (in this case, @code{Put_Line} will be called).
4360 Only one call can be present, but of course a special debugging procedure
4361 containing any code you like can be included in the program and then
4362 called in a pragma @code{Debug} argument as needed.
4363
4364 One advantage of pragma @code{Debug} over the @code{if Debugging then}
4365 construct is that pragma @code{Debug} can appear in declarative contexts,
4366 such as at the very beginning of a procedure, before local declarations have
4367 been elaborated.
4368
4369 @geindex pragma Debug_Policy
4370
4371 Debug pragmas are enabled using either the @code{-gnata} switch that also
4372 controls assertions, or with a separate Debug_Policy pragma.
4373
4374 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
4375 in Ada 95 and Ada 83 programs as well), and is analogous to
4376 pragma @code{Assertion_Policy} to control assertions.
4377
4378 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
4379 and thus they can appear in @code{gnat.adc} if you are not using a
4380 project file, or in the file designated to contain configuration pragmas
4381 in a project file.
4382 They then apply to all subsequent compilations. In practice the use of
4383 the @code{-gnata} switch is often the most convenient method of controlling
4384 the status of these pragmas.
4385
4386 Note that a pragma is not a statement, so in contexts where a statement
4387 sequence is required, you can't just write a pragma on its own. You have
4388 to add a @code{null} statement.
4389
4390 @example
4391 if ... then
4392 ... -- some statements
4393 else
4394 pragma Assert (Num_Cases < 10);
4395 null;
4396 end if;
4397 @end example
4398
4399 @node Conditionalizing Declarations,Use of Alternative Implementations,Debugging - A Special Case,Modeling Conditional Compilation in Ada
4400 @anchor{gnat_ugn/the_gnat_compilation_model conditionalizing-declarations}@anchor{9f}@anchor{gnat_ugn/the_gnat_compilation_model id51}@anchor{a0}
4401 @subsubsection Conditionalizing Declarations
4402
4403
4404 In some cases it may be necessary to conditionalize declarations to meet
4405 different requirements. For example we might want a bit string whose length
4406 is set to meet some hardware message requirement.
4407
4408 This may be possible using declare blocks controlled
4409 by conditional constants:
4410
4411 @example
4412 if Small_Machine then
4413 declare
4414 X : Bit_String (1 .. 10);
4415 begin
4416 ...
4417 end;
4418 else
4419 declare
4420 X : Large_Bit_String (1 .. 1000);
4421 begin
4422 ...
4423 end;
4424 end if;
4425 @end example
4426
4427 Note that in this approach, both declarations are analyzed by the
4428 compiler so this can only be used where both declarations are legal,
4429 even though one of them will not be used.
4430
4431 Another approach is to define integer constants, e.g., @code{Bits_Per_Word},
4432 or Boolean constants, e.g., @code{Little_Endian}, and then write declarations
4433 that are parameterized by these constants. For example
4434
4435 @example
4436 for Rec use
4437 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
4438 end record;
4439 @end example
4440
4441 If @code{Bits_Per_Word} is set to 32, this generates either
4442
4443 @example
4444 for Rec use
4445 Field1 at 0 range 0 .. 32;
4446 end record;
4447 @end example
4448
4449 for the big endian case, or
4450
4451 @example
4452 for Rec use record
4453 Field1 at 0 range 10 .. 32;
4454 end record;
4455 @end example
4456
4457 for the little endian case. Since a powerful subset of Ada expression
4458 notation is usable for creating static constants, clever use of this
4459 feature can often solve quite difficult problems in conditionalizing
4460 compilation (note incidentally that in Ada 95, the little endian
4461 constant was introduced as @code{System.Default_Bit_Order}, so you do not
4462 need to define this one yourself).
4463
4464 @node Use of Alternative Implementations,Preprocessing,Conditionalizing Declarations,Modeling Conditional Compilation in Ada
4465 @anchor{gnat_ugn/the_gnat_compilation_model use-of-alternative-implementations}@anchor{a1}@anchor{gnat_ugn/the_gnat_compilation_model id52}@anchor{a2}
4466 @subsubsection Use of Alternative Implementations
4467
4468
4469 In some cases, none of the approaches described above are adequate. This
4470 can occur for example if the set of declarations required is radically
4471 different for two different configurations.
4472
4473 In this situation, the official Ada way of dealing with conditionalizing
4474 such code is to write separate units for the different cases. As long as
4475 this does not result in excessive duplication of code, this can be done
4476 without creating maintenance problems. The approach is to share common
4477 code as far as possible, and then isolate the code and declarations
4478 that are different. Subunits are often a convenient method for breaking
4479 out a piece of a unit that is to be conditionalized, with separate files
4480 for different versions of the subunit for different targets, where the
4481 build script selects the right one to give to the compiler.
4482
4483 @geindex Subunits (and conditional compilation)
4484
4485 As an example, consider a situation where a new feature in Ada 2005
4486 allows something to be done in a really nice way. But your code must be able
4487 to compile with an Ada 95 compiler. Conceptually you want to say:
4488
4489 @example
4490 if Ada_2005 then
4491 ... neat Ada 2005 code
4492 else
4493 ... not quite as neat Ada 95 code
4494 end if;
4495 @end example
4496
4497 where @code{Ada_2005} is a Boolean constant.
4498
4499 But this won't work when @code{Ada_2005} is set to @code{False},
4500 since the @code{then} clause will be illegal for an Ada 95 compiler.
4501 (Recall that although such unreachable code would eventually be deleted
4502 by the compiler, it still needs to be legal. If it uses features
4503 introduced in Ada 2005, it will be illegal in Ada 95.)
4504
4505 So instead we write
4506
4507 @example
4508 procedure Insert is separate;
4509 @end example
4510
4511 Then we have two files for the subunit @code{Insert}, with the two sets of
4512 code.
4513 If the package containing this is called @code{File_Queries}, then we might
4514 have two files
4515
4516
4517 @itemize *
4518
4519 @item
4520 @code{file_queries-insert-2005.adb}
4521
4522 @item
4523 @code{file_queries-insert-95.adb}
4524 @end itemize
4525
4526 and the build script renames the appropriate file to @code{file_queries-insert.adb} and then carries out the compilation.
4527
4528 This can also be done with project files' naming schemes. For example:
4529
4530 @example
4531 for body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
4532 @end example
4533
4534 Note also that with project files it is desirable to use a different extension
4535 than @code{ads} / @code{adb} for alternative versions. Otherwise a naming
4536 conflict may arise through another commonly used feature: to declare as part
4537 of the project a set of directories containing all the sources obeying the
4538 default naming scheme.
4539
4540 The use of alternative units is certainly feasible in all situations,
4541 and for example the Ada part of the GNAT run-time is conditionalized
4542 based on the target architecture using this approach. As a specific example,
4543 consider the implementation of the AST feature in VMS. There is one
4544 spec: @code{s-asthan.ads} which is the same for all architectures, and three
4545 bodies:
4546
4547
4548 @itemize *
4549
4550 @item
4551
4552 @table @asis
4553
4554 @item @code{s-asthan.adb}
4555
4556 used for all non-VMS operating systems
4557 @end table
4558
4559 @item
4560
4561 @table @asis
4562
4563 @item @code{s-asthan-vms-alpha.adb}
4564
4565 used for VMS on the Alpha
4566 @end table
4567
4568 @item
4569
4570 @table @asis
4571
4572 @item @code{s-asthan-vms-ia64.adb}
4573
4574 used for VMS on the ia64
4575 @end table
4576 @end itemize
4577
4578 The dummy version @code{s-asthan.adb} simply raises exceptions noting that
4579 this operating system feature is not available, and the two remaining
4580 versions interface with the corresponding versions of VMS to provide
4581 VMS-compatible AST handling. The GNAT build script knows the architecture
4582 and operating system, and automatically selects the right version,
4583 renaming it if necessary to @code{s-asthan.adb} before the run-time build.
4584
4585 Another style for arranging alternative implementations is through Ada's
4586 access-to-subprogram facility.
4587 In case some functionality is to be conditionally included,
4588 you can declare an access-to-procedure variable @code{Ref} that is initialized
4589 to designate a 'do nothing' procedure, and then invoke @code{Ref.all}
4590 when appropriate.
4591 In some library package, set @code{Ref} to @code{Proc'Access} for some
4592 procedure @code{Proc} that performs the relevant processing.
4593 The initialization only occurs if the library package is included in the
4594 program.
4595 The same idea can also be implemented using tagged types and dispatching
4596 calls.
4597
4598 @node Preprocessing,,Use of Alternative Implementations,Modeling Conditional Compilation in Ada
4599 @anchor{gnat_ugn/the_gnat_compilation_model preprocessing}@anchor{a3}@anchor{gnat_ugn/the_gnat_compilation_model id53}@anchor{a4}
4600 @subsubsection Preprocessing
4601
4602
4603 @geindex Preprocessing
4604
4605 Although it is quite possible to conditionalize code without the use of
4606 C-style preprocessing, as described earlier in this section, it is
4607 nevertheless convenient in some cases to use the C approach. Moreover,
4608 older Ada compilers have often provided some preprocessing capability,
4609 so legacy code may depend on this approach, even though it is not
4610 standard.
4611
4612 To accommodate such use, GNAT provides a preprocessor (modeled to a large
4613 extent on the various preprocessors that have been used
4614 with legacy code on other compilers, to enable easier transition).
4615
4616 @geindex gnatprep
4617
4618 The preprocessor may be used in two separate modes. It can be used quite
4619 separately from the compiler, to generate a separate output source file
4620 that is then fed to the compiler as a separate step. This is the
4621 @code{gnatprep} utility, whose use is fully described in
4622 @ref{17,,Preprocessing with gnatprep}.
4623
4624 The preprocessing language allows such constructs as
4625
4626 @example
4627 #if DEBUG or else (PRIORITY > 4) then
4628 sequence of declarations
4629 #else
4630 completely different sequence of declarations
4631 #end if;
4632 @end example
4633
4634 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
4635 defined either on the command line or in a separate file.
4636
4637 The other way of running the preprocessor is even closer to the C style and
4638 often more convenient. In this approach the preprocessing is integrated into
4639 the compilation process. The compiler is given the preprocessor input which
4640 includes @code{#if} lines etc, and then the compiler carries out the
4641 preprocessing internally and processes the resulting output.
4642 For more details on this approach, see @ref{18,,Integrated Preprocessing}.
4643
4644 @node Preprocessing with gnatprep,Integrated Preprocessing,Modeling Conditional Compilation in Ada,Conditional Compilation
4645 @anchor{gnat_ugn/the_gnat_compilation_model id54}@anchor{a5}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-with-gnatprep}@anchor{17}
4646 @subsection Preprocessing with @code{gnatprep}
4647
4648
4649 @geindex gnatprep
4650
4651 @geindex Preprocessing (gnatprep)
4652
4653 This section discusses how to use GNAT's @code{gnatprep} utility for simple
4654 preprocessing.
4655 Although designed for use with GNAT, @code{gnatprep} does not depend on any
4656 special GNAT features.
4657 For further discussion of conditional compilation in general, see
4658 @ref{16,,Conditional Compilation}.
4659
4660 @menu
4661 * Preprocessing Symbols::
4662 * Using gnatprep::
4663 * Switches for gnatprep::
4664 * Form of Definitions File::
4665 * Form of Input Text for gnatprep::
4666
4667 @end menu
4668
4669 @node Preprocessing Symbols,Using gnatprep,,Preprocessing with gnatprep
4670 @anchor{gnat_ugn/the_gnat_compilation_model id55}@anchor{a6}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-symbols}@anchor{a7}
4671 @subsubsection Preprocessing Symbols
4672
4673
4674 Preprocessing symbols are defined in @emph{definition files} and referenced in the
4675 sources to be preprocessed. A preprocessing symbol is an identifier, following
4676 normal Ada (case-insensitive) rules for its syntax, with the restriction that
4677 all characters need to be in the ASCII set (no accented letters).
4678
4679 @node Using gnatprep,Switches for gnatprep,Preprocessing Symbols,Preprocessing with gnatprep
4680 @anchor{gnat_ugn/the_gnat_compilation_model using-gnatprep}@anchor{a8}@anchor{gnat_ugn/the_gnat_compilation_model id56}@anchor{a9}
4681 @subsubsection Using @code{gnatprep}
4682
4683
4684 To call @code{gnatprep} use:
4685
4686 @example
4687 $ gnatprep [ switches ] infile outfile [ deffile ]
4688 @end example
4689
4690 where
4691
4692
4693 @itemize *
4694
4695 @item
4696
4697 @table @asis
4698
4699 @item @emph{switches}
4700
4701 is an optional sequence of switches as described in the next section.
4702 @end table
4703
4704 @item
4705
4706 @table @asis
4707
4708 @item @emph{infile}
4709
4710 is the full name of the input file, which is an Ada source
4711 file containing preprocessor directives.
4712 @end table
4713
4714 @item
4715
4716 @table @asis
4717
4718 @item @emph{outfile}
4719
4720 is the full name of the output file, which is an Ada source
4721 in standard Ada form. When used with GNAT, this file name will
4722 normally have an @code{ads} or @code{adb} suffix.
4723 @end table
4724
4725 @item
4726
4727 @table @asis
4728
4729 @item @code{deffile}
4730
4731 is the full name of a text file containing definitions of
4732 preprocessing symbols to be referenced by the preprocessor. This argument is
4733 optional, and can be replaced by the use of the @code{-D} switch.
4734 @end table
4735 @end itemize
4736
4737 @node Switches for gnatprep,Form of Definitions File,Using gnatprep,Preprocessing with gnatprep
4738 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatprep}@anchor{aa}@anchor{gnat_ugn/the_gnat_compilation_model id57}@anchor{ab}
4739 @subsubsection Switches for @code{gnatprep}
4740
4741
4742 @geindex --version (gnatprep)
4743
4744
4745 @table @asis
4746
4747 @item @code{--version}
4748
4749 Display Copyright and version, then exit disregarding all other options.
4750 @end table
4751
4752 @geindex --help (gnatprep)
4753
4754
4755 @table @asis
4756
4757 @item @code{--help}
4758
4759 If @code{--version} was not used, display usage and then exit disregarding
4760 all other options.
4761 @end table
4762
4763 @geindex -b (gnatprep)
4764
4765
4766 @table @asis
4767
4768 @item @code{-b}
4769
4770 Causes both preprocessor lines and the lines deleted by
4771 preprocessing to be replaced by blank lines in the output source file,
4772 preserving line numbers in the output file.
4773 @end table
4774
4775 @geindex -c (gnatprep)
4776
4777
4778 @table @asis
4779
4780 @item @code{-c}
4781
4782 Causes both preprocessor lines and the lines deleted
4783 by preprocessing to be retained in the output source as comments marked
4784 with the special string @code{"--! "}. This option will result in line numbers
4785 being preserved in the output file.
4786 @end table
4787
4788 @geindex -C (gnatprep)
4789
4790
4791 @table @asis
4792
4793 @item @code{-C}
4794
4795 Causes comments to be scanned. Normally comments are ignored by gnatprep.
4796 If this option is specified, then comments are scanned and any $symbol
4797 substitutions performed as in program text. This is particularly useful
4798 when structured comments are used (e.g., for programs written in a
4799 pre-2014 version of the SPARK Ada subset). Note that this switch is not
4800 available when doing integrated preprocessing (it would be useless in
4801 this context since comments are ignored by the compiler in any case).
4802 @end table
4803
4804 @geindex -D (gnatprep)
4805
4806
4807 @table @asis
4808
4809 @item @code{-D@emph{symbol}[=@emph{value}]}
4810
4811 Defines a new preprocessing symbol with the specified value. If no value is given
4812 on the command line, then symbol is considered to be @code{True}. This switch
4813 can be used in place of a definition file.
4814 @end table
4815
4816 @geindex -r (gnatprep)
4817
4818
4819 @table @asis
4820
4821 @item @code{-r}
4822
4823 Causes a @code{Source_Reference} pragma to be generated that
4824 references the original input file, so that error messages will use
4825 the file name of this original file. The use of this switch implies
4826 that preprocessor lines are not to be removed from the file, so its
4827 use will force @code{-b} mode if @code{-c}
4828 has not been specified explicitly.
4829
4830 Note that if the file to be preprocessed contains multiple units, then
4831 it will be necessary to @code{gnatchop} the output file from
4832 @code{gnatprep}. If a @code{Source_Reference} pragma is present
4833 in the preprocessed file, it will be respected by
4834 @code{gnatchop -r}
4835 so that the final chopped files will correctly refer to the original
4836 input source file for @code{gnatprep}.
4837 @end table
4838
4839 @geindex -s (gnatprep)
4840
4841
4842 @table @asis
4843
4844 @item @code{-s}
4845
4846 Causes a sorted list of symbol names and values to be
4847 listed on the standard output file.
4848 @end table
4849
4850 @geindex -T (gnatprep)
4851
4852
4853 @table @asis
4854
4855 @item @code{-T}
4856
4857 Use LF as line terminators when writing files. By default the line terminator
4858 of the host (LF under unix, CR/LF under Windows) is used.
4859 @end table
4860
4861 @geindex -u (gnatprep)
4862
4863
4864 @table @asis
4865
4866 @item @code{-u}
4867
4868 Causes undefined symbols to be treated as having the value FALSE in the context
4869 of a preprocessor test. In the absence of this option, an undefined symbol in
4870 a @code{#if} or @code{#elsif} test will be treated as an error.
4871 @end table
4872
4873 @geindex -v (gnatprep)
4874
4875
4876 @table @asis
4877
4878 @item @code{-v}
4879
4880 Verbose mode: generates more output about work done.
4881 @end table
4882
4883 Note: if neither @code{-b} nor @code{-c} is present,
4884 then preprocessor lines and
4885 deleted lines are completely removed from the output, unless -r is
4886 specified, in which case -b is assumed.
4887
4888 @node Form of Definitions File,Form of Input Text for gnatprep,Switches for gnatprep,Preprocessing with gnatprep
4889 @anchor{gnat_ugn/the_gnat_compilation_model form-of-definitions-file}@anchor{ac}@anchor{gnat_ugn/the_gnat_compilation_model id58}@anchor{ad}
4890 @subsubsection Form of Definitions File
4891
4892
4893 The definitions file contains lines of the form:
4894
4895 @example
4896 symbol := value
4897 @end example
4898
4899 where @code{symbol} is a preprocessing symbol, and @code{value} is one of the following:
4900
4901
4902 @itemize *
4903
4904 @item
4905 Empty, corresponding to a null substitution,
4906
4907 @item
4908 A string literal using normal Ada syntax, or
4909
4910 @item
4911 Any sequence of characters from the set @{letters, digits, period, underline@}.
4912 @end itemize
4913
4914 Comment lines may also appear in the definitions file, starting with
4915 the usual @code{--},
4916 and comments may be added to the definitions lines.
4917
4918 @node Form of Input Text for gnatprep,,Form of Definitions File,Preprocessing with gnatprep
4919 @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}
4920 @subsubsection Form of Input Text for @code{gnatprep}
4921
4922
4923 The input text may contain preprocessor conditional inclusion lines,
4924 as well as general symbol substitution sequences.
4925
4926 The preprocessor conditional inclusion commands have the form:
4927
4928 @example
4929 #if <expression> [then]
4930 lines
4931 #elsif <expression> [then]
4932 lines
4933 #elsif <expression> [then]
4934 lines
4935 ...
4936 #else
4937 lines
4938 #end if;
4939 @end example
4940
4941 In this example, <expression> is defined by the following grammar:
4942
4943 @example
4944 <expression> ::= <symbol>
4945 <expression> ::= <symbol> = "<value>"
4946 <expression> ::= <symbol> = <symbol>
4947 <expression> ::= <symbol> = <integer>
4948 <expression> ::= <symbol> > <integer>
4949 <expression> ::= <symbol> >= <integer>
4950 <expression> ::= <symbol> < <integer>
4951 <expression> ::= <symbol> <= <integer>
4952 <expression> ::= <symbol> 'Defined
4953 <expression> ::= not <expression>
4954 <expression> ::= <expression> and <expression>
4955 <expression> ::= <expression> or <expression>
4956 <expression> ::= <expression> and then <expression>
4957 <expression> ::= <expression> or else <expression>
4958 <expression> ::= ( <expression> )
4959 @end example
4960
4961 Note the following restriction: it is not allowed to have "and" or "or"
4962 following "not" in the same expression without parentheses. For example, this
4963 is not allowed:
4964
4965 @example
4966 not X or Y
4967 @end example
4968
4969 This can be expressed instead as one of the following forms:
4970
4971 @example
4972 (not X) or Y
4973 not (X or Y)
4974 @end example
4975
4976 For the first test (<expression> ::= <symbol>) the symbol must have
4977 either the value true or false, that is to say the right-hand of the
4978 symbol definition must be one of the (case-insensitive) literals
4979 @code{True} or @code{False}. If the value is true, then the
4980 corresponding lines are included, and if the value is false, they are
4981 excluded.
4982
4983 When comparing a symbol to an integer, the integer is any non negative
4984 literal integer as defined in the Ada Reference Manual, such as 3, 16#FF# or
4985 2#11#. The symbol value must also be a non negative integer. Integer values
4986 in the range 0 .. 2**31-1 are supported.
4987
4988 The test (<expression> ::= <symbol>'Defined) is true only if
4989 the symbol has been defined in the definition file or by a @code{-D}
4990 switch on the command line. Otherwise, the test is false.
4991
4992 The equality tests are case insensitive, as are all the preprocessor lines.
4993
4994 If the symbol referenced is not defined in the symbol definitions file,
4995 then the effect depends on whether or not switch @code{-u}
4996 is specified. If so, then the symbol is treated as if it had the value
4997 false and the test fails. If this switch is not specified, then
4998 it is an error to reference an undefined symbol. It is also an error to
4999 reference a symbol that is defined with a value other than @code{True}
5000 or @code{False}.
5001
5002 The use of the @code{not} operator inverts the sense of this logical test.
5003 The @code{not} operator cannot be combined with the @code{or} or @code{and}
5004 operators, without parentheses. For example, "if not X or Y then" is not
5005 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
5006
5007 The @code{then} keyword is optional as shown
5008
5009 The @code{#} must be the first non-blank character on a line, but
5010 otherwise the format is free form. Spaces or tabs may appear between
5011 the @code{#} and the keyword. The keywords and the symbols are case
5012 insensitive as in normal Ada code. Comments may be used on a
5013 preprocessor line, but other than that, no other tokens may appear on a
5014 preprocessor line. Any number of @code{elsif} clauses can be present,
5015 including none at all. The @code{else} is optional, as in Ada.
5016
5017 The @code{#} marking the start of a preprocessor line must be the first
5018 non-blank character on the line, i.e., it must be preceded only by
5019 spaces or horizontal tabs.
5020
5021 Symbol substitution outside of preprocessor lines is obtained by using
5022 the sequence:
5023
5024 @example
5025 $symbol
5026 @end example
5027
5028 anywhere within a source line, except in a comment or within a
5029 string literal. The identifier
5030 following the @code{$} must match one of the symbols defined in the symbol
5031 definition file, and the result is to substitute the value of the
5032 symbol in place of @code{$symbol} in the output file.
5033
5034 Note that although the substitution of strings within a string literal
5035 is not possible, it is possible to have a symbol whose defined value is
5036 a string literal. So instead of setting XYZ to @code{hello} and writing:
5037
5038 @example
5039 Header : String := "$XYZ";
5040 @end example
5041
5042 you should set XYZ to @code{"hello"} and write:
5043
5044 @example
5045 Header : String := $XYZ;
5046 @end example
5047
5048 and then the substitution will occur as desired.
5049
5050 @node Integrated Preprocessing,,Preprocessing with gnatprep,Conditional Compilation
5051 @anchor{gnat_ugn/the_gnat_compilation_model id60}@anchor{b0}@anchor{gnat_ugn/the_gnat_compilation_model integrated-preprocessing}@anchor{18}
5052 @subsection Integrated Preprocessing
5053
5054
5055 As noted above, a file to be preprocessed consists of Ada source code
5056 in which preprocessing lines have been inserted. However,
5057 instead of using @code{gnatprep} to explicitly preprocess a file as a separate
5058 step before compilation, you can carry out the preprocessing implicitly
5059 as part of compilation. Such @emph{integrated preprocessing}, which is the common
5060 style with C, is performed when either or both of the following switches
5061 are passed to the compiler:
5062
5063 @quotation
5064
5065
5066 @itemize *
5067
5068 @item
5069 @code{-gnatep}, which specifies the @emph{preprocessor data file}.
5070 This file dictates how the source files will be preprocessed (e.g., which
5071 symbol definition files apply to which sources).
5072
5073 @item
5074 @code{-gnateD}, which defines values for preprocessing symbols.
5075 @end itemize
5076 @end quotation
5077
5078 Integrated preprocessing applies only to Ada source files, it is
5079 not available for configuration pragma files.
5080
5081 With integrated preprocessing, the output from the preprocessor is not,
5082 by default, written to any external file. Instead it is passed
5083 internally to the compiler. To preserve the result of
5084 preprocessing in a file, either run @code{gnatprep}
5085 in standalone mode or else supply the @code{-gnateG} switch
5086 (described below) to the compiler.
5087
5088 When using project files:
5089
5090 @quotation
5091
5092
5093 @itemize *
5094
5095 @item
5096 the builder switch @code{-x} should be used if any Ada source is
5097 compiled with @code{gnatep=}, so that the compiler finds the
5098 @emph{preprocessor data file}.
5099
5100 @item
5101 the preprocessing data file and the symbol definition files should be
5102 located in the source directories of the project.
5103 @end itemize
5104 @end quotation
5105
5106 Note that the @code{gnatmake} switch @code{-m} will almost
5107 always trigger recompilation for sources that are preprocessed,
5108 because @code{gnatmake} cannot compute the checksum of the source after
5109 preprocessing.
5110
5111 The actual preprocessing function is described in detail in
5112 @ref{17,,Preprocessing with gnatprep}. This section explains the switches
5113 that relate to integrated preprocessing.
5114
5115 @geindex -gnatep (gcc)
5116
5117
5118 @table @asis
5119
5120 @item @code{-gnatep=@emph{preprocessor_data_file}}
5121
5122 This switch specifies the file name (without directory
5123 information) of the preprocessor data file. Either place this file
5124 in one of the source directories, or, when using project
5125 files, reference the project file's directory via the
5126 @code{project_name'Project_Dir} project attribute; e.g:
5127
5128 @quotation
5129
5130 @example
5131 project Prj is
5132 package Compiler is
5133 for Switches ("Ada") use
5134 ("-gnatep=" & Prj'Project_Dir & "prep.def");
5135 end Compiler;
5136 end Prj;
5137 @end example
5138 @end quotation
5139
5140 A preprocessor data file is a text file that contains @emph{preprocessor
5141 control lines}. A preprocessor control line directs the preprocessing of
5142 either a particular source file, or, analogous to @code{others} in Ada,
5143 all sources not specified elsewhere in the preprocessor data file.
5144 A preprocessor control line
5145 can optionally identify a @emph{definition file} that assigns values to
5146 preprocessor symbols, as well as a list of switches that relate to
5147 preprocessing.
5148 Empty lines and comments (using Ada syntax) are also permitted, with no
5149 semantic effect.
5150
5151 Here's an example of a preprocessor data file:
5152
5153 @quotation
5154
5155 @example
5156 "toto.adb" "prep.def" -u
5157 -- Preprocess toto.adb, using definition file prep.def
5158 -- Undefined symbols are treated as False
5159
5160 * -c -DVERSION=V101
5161 -- Preprocess all other sources without using a definition file
5162 -- Suppressed lined are commented
5163 -- Symbol VERSION has the value V101
5164
5165 "tata.adb" "prep2.def" -s
5166 -- Preprocess tata.adb, using definition file prep2.def
5167 -- List all symbols with their values
5168 @end example
5169 @end quotation
5170
5171 A preprocessor control line has the following syntax:
5172
5173 @quotation
5174
5175 @example
5176 <preprocessor_control_line> ::=
5177 <preprocessor_input> [ <definition_file_name> ] @{ <switch> @}
5178
5179 <preprocessor_input> ::= <source_file_name> | '*'
5180
5181 <definition_file_name> ::= <string_literal>
5182
5183 <source_file_name> := <string_literal>
5184
5185 <switch> := (See below for list)
5186 @end example
5187 @end quotation
5188
5189 Thus each preprocessor control line starts with either a literal string or
5190 the character '*':
5191
5192
5193 @itemize *
5194
5195 @item
5196 A literal string is the file name (without directory information) of the source
5197 file that will be input to the preprocessor.
5198
5199 @item
5200 The character '*' is a wild-card indicator; the additional parameters on the line
5201 indicate the preprocessing for all the sources
5202 that are not specified explicitly on other lines (the order of the lines is not
5203 significant).
5204 @end itemize
5205
5206 It is an error to have two lines with the same file name or two
5207 lines starting with the character '*'.
5208
5209 After the file name or '*', an optional literal string specifies the name of
5210 the definition file to be used for preprocessing
5211 (@ref{ac,,Form of Definitions File}). The definition files are found by the
5212 compiler in one of the source directories. In some cases, when compiling
5213 a source in a directory other than the current directory, if the definition
5214 file is in the current directory, it may be necessary to add the current
5215 directory as a source directory through the @code{-I} switch; otherwise
5216 the compiler would not find the definition file.
5217
5218 Finally, switches similar to those of @code{gnatprep} may optionally appear:
5219
5220
5221 @table @asis
5222
5223 @item @code{-b}
5224
5225 Causes both preprocessor lines and the lines deleted by
5226 preprocessing to be replaced by blank lines, preserving the line number.
5227 This switch is always implied; however, if specified after @code{-c}
5228 it cancels the effect of @code{-c}.
5229
5230 @item @code{-c}
5231
5232 Causes both preprocessor lines and the lines deleted
5233 by preprocessing to be retained as comments marked
5234 with the special string '@cite{--!}'.
5235
5236 @item @code{-D@emph{symbol}=@emph{new_value}}
5237
5238 Define or redefine @code{symbol} to have @code{new_value} as its value.
5239 The permitted form for @code{symbol} is either an Ada identifier, or any Ada reserved word
5240 aside from @code{if},
5241 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5242 The permitted form for @code{new_value} is a literal string, an Ada identifier or any Ada reserved
5243 word. A symbol declared with this switch replaces a symbol with the
5244 same name defined in a definition file.
5245
5246 @item @code{-s}
5247
5248 Causes a sorted list of symbol names and values to be
5249 listed on the standard output file.
5250
5251 @item @code{-u}
5252
5253 Causes undefined symbols to be treated as having the value @code{FALSE}
5254 in the context
5255 of a preprocessor test. In the absence of this option, an undefined symbol in
5256 a @code{#if} or @code{#elsif} test will be treated as an error.
5257 @end table
5258 @end table
5259
5260 @geindex -gnateD (gcc)
5261
5262
5263 @table @asis
5264
5265 @item @code{-gnateD@emph{symbol}[=@emph{new_value}]}
5266
5267 Define or redefine @code{symbol} to have @code{new_value} as its value. If no value
5268 is supplied, then the value of @code{symbol} is @code{True}.
5269 The form of @code{symbol} is an identifier, following normal Ada (case-insensitive)
5270 rules for its syntax, and @code{new_value} is either an arbitrary string between double
5271 quotes or any sequence (including an empty sequence) of characters from the
5272 set (letters, digits, period, underline).
5273 Ada reserved words may be used as symbols, with the exceptions of @code{if},
5274 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5275
5276 Examples:
5277
5278 @quotation
5279
5280 @example
5281 -gnateDToto=Tata
5282 -gnateDFoo
5283 -gnateDFoo=\"Foo-Bar\"
5284 @end example
5285 @end quotation
5286
5287 A symbol declared with this switch on the command line replaces a
5288 symbol with the same name either in a definition file or specified with a
5289 switch @code{-D} in the preprocessor data file.
5290
5291 This switch is similar to switch @code{-D} of @code{gnatprep}.
5292
5293 @item @code{-gnateG}
5294
5295 When integrated preprocessing is performed on source file @code{filename.extension},
5296 create or overwrite @code{filename.extension.prep} to contain
5297 the result of the preprocessing.
5298 For example if the source file is @code{foo.adb} then
5299 the output file will be @code{foo.adb.prep}.
5300 @end table
5301
5302 @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model
5303 @anchor{gnat_ugn/the_gnat_compilation_model mixed-language-programming}@anchor{44}@anchor{gnat_ugn/the_gnat_compilation_model id61}@anchor{b1}
5304 @section Mixed Language Programming
5305
5306
5307 @geindex Mixed Language Programming
5308
5309 This section describes how to develop a mixed-language program,
5310 with a focus on combining Ada with C or C++.
5311
5312 @menu
5313 * Interfacing to C::
5314 * Calling Conventions::
5315 * Building Mixed Ada and C++ Programs::
5316 * Generating Ada Bindings for C and C++ headers::
5317 * Generating C Headers for Ada Specifications::
5318
5319 @end menu
5320
5321 @node Interfacing to C,Calling Conventions,,Mixed Language Programming
5322 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-to-c}@anchor{b2}@anchor{gnat_ugn/the_gnat_compilation_model id62}@anchor{b3}
5323 @subsection Interfacing to C
5324
5325
5326 Interfacing Ada with a foreign language such as C involves using
5327 compiler directives to import and/or export entity definitions in each
5328 language -- using @code{extern} statements in C, for instance, and the
5329 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
5330 A full treatment of these topics is provided in Appendix B, section 1
5331 of the Ada Reference Manual.
5332
5333 There are two ways to build a program using GNAT that contains some Ada
5334 sources and some foreign language sources, depending on whether or not
5335 the main subprogram is written in Ada. Here is a source example with
5336 the main subprogram in Ada:
5337
5338 @example
5339 /* file1.c */
5340 #include <stdio.h>
5341
5342 void print_num (int num)
5343 @{
5344 printf ("num is %d.\\n", num);
5345 return;
5346 @}
5347 @end example
5348
5349 @example
5350 /* file2.c */
5351
5352 /* num_from_Ada is declared in my_main.adb */
5353 extern int num_from_Ada;
5354
5355 int get_num (void)
5356 @{
5357 return num_from_Ada;
5358 @}
5359 @end example
5360
5361 @example
5362 -- my_main.adb
5363 procedure My_Main is
5364
5365 -- Declare then export an Integer entity called num_from_Ada
5366 My_Num : Integer := 10;
5367 pragma Export (C, My_Num, "num_from_Ada");
5368
5369 -- Declare an Ada function spec for Get_Num, then use
5370 -- C function get_num for the implementation.
5371 function Get_Num return Integer;
5372 pragma Import (C, Get_Num, "get_num");
5373
5374 -- Declare an Ada procedure spec for Print_Num, then use
5375 -- C function print_num for the implementation.
5376 procedure Print_Num (Num : Integer);
5377 pragma Import (C, Print_Num, "print_num");
5378
5379 begin
5380 Print_Num (Get_Num);
5381 end My_Main;
5382 @end example
5383
5384 To build this example:
5385
5386
5387 @itemize *
5388
5389 @item
5390 First compile the foreign language files to
5391 generate object files:
5392
5393 @example
5394 $ gcc -c file1.c
5395 $ gcc -c file2.c
5396 @end example
5397
5398 @item
5399 Then, compile the Ada units to produce a set of object files and ALI
5400 files:
5401
5402 @example
5403 $ gnatmake -c my_main.adb
5404 @end example
5405
5406 @item
5407 Run the Ada binder on the Ada main program:
5408
5409 @example
5410 $ gnatbind my_main.ali
5411 @end example
5412
5413 @item
5414 Link the Ada main program, the Ada objects and the other language
5415 objects:
5416
5417 @example
5418 $ gnatlink my_main.ali file1.o file2.o
5419 @end example
5420 @end itemize
5421
5422 The last three steps can be grouped in a single command:
5423
5424 @example
5425 $ gnatmake my_main.adb -largs file1.o file2.o
5426 @end example
5427
5428 @geindex Binder output file
5429
5430 If the main program is in a language other than Ada, then you may have
5431 more than one entry point into the Ada subsystem. You must use a special
5432 binder option to generate callable routines that initialize and
5433 finalize the Ada units (@ref{b4,,Binding with Non-Ada Main Programs}).
5434 Calls to the initialization and finalization routines must be inserted
5435 in the main program, or some other appropriate point in the code. The
5436 call to initialize the Ada units must occur before the first Ada
5437 subprogram is called, and the call to finalize the Ada units must occur
5438 after the last Ada subprogram returns. The binder will place the
5439 initialization and finalization subprograms into the
5440 @code{b~xxx.adb} file where they can be accessed by your C
5441 sources. To illustrate, we have the following example:
5442
5443 @example
5444 /* main.c */
5445 extern void adainit (void);
5446 extern void adafinal (void);
5447 extern int add (int, int);
5448 extern int sub (int, int);
5449
5450 int main (int argc, char *argv[])
5451 @{
5452 int a = 21, b = 7;
5453
5454 adainit();
5455
5456 /* Should print "21 + 7 = 28" */
5457 printf ("%d + %d = %d\\n", a, b, add (a, b));
5458
5459 /* Should print "21 - 7 = 14" */
5460 printf ("%d - %d = %d\\n", a, b, sub (a, b));
5461
5462 adafinal();
5463 @}
5464 @end example
5465
5466 @example
5467 -- unit1.ads
5468 package Unit1 is
5469 function Add (A, B : Integer) return Integer;
5470 pragma Export (C, Add, "add");
5471 end Unit1;
5472 @end example
5473
5474 @example
5475 -- unit1.adb
5476 package body Unit1 is
5477 function Add (A, B : Integer) return Integer is
5478 begin
5479 return A + B;
5480 end Add;
5481 end Unit1;
5482 @end example
5483
5484 @example
5485 -- unit2.ads
5486 package Unit2 is
5487 function Sub (A, B : Integer) return Integer;
5488 pragma Export (C, Sub, "sub");
5489 end Unit2;
5490 @end example
5491
5492 @example
5493 -- unit2.adb
5494 package body Unit2 is
5495 function Sub (A, B : Integer) return Integer is
5496 begin
5497 return A - B;
5498 end Sub;
5499 end Unit2;
5500 @end example
5501
5502 The build procedure for this application is similar to the last
5503 example's:
5504
5505
5506 @itemize *
5507
5508 @item
5509 First, compile the foreign language files to generate object files:
5510
5511 @example
5512 $ gcc -c main.c
5513 @end example
5514
5515 @item
5516 Next, compile the Ada units to produce a set of object files and ALI
5517 files:
5518
5519 @example
5520 $ gnatmake -c unit1.adb
5521 $ gnatmake -c unit2.adb
5522 @end example
5523
5524 @item
5525 Run the Ada binder on every generated ALI file. Make sure to use the
5526 @code{-n} option to specify a foreign main program:
5527
5528 @example
5529 $ gnatbind -n unit1.ali unit2.ali
5530 @end example
5531
5532 @item
5533 Link the Ada main program, the Ada objects and the foreign language
5534 objects. You need only list the last ALI file here:
5535
5536 @example
5537 $ gnatlink unit2.ali main.o -o exec_file
5538 @end example
5539
5540 This procedure yields a binary executable called @code{exec_file}.
5541 @end itemize
5542
5543 Depending on the circumstances (for example when your non-Ada main object
5544 does not provide symbol @code{main}), you may also need to instruct the
5545 GNAT linker not to include the standard startup objects by passing the
5546 @code{-nostartfiles} switch to @code{gnatlink}.
5547
5548 @node Calling Conventions,Building Mixed Ada and C++ Programs,Interfacing to C,Mixed Language Programming
5549 @anchor{gnat_ugn/the_gnat_compilation_model calling-conventions}@anchor{b5}@anchor{gnat_ugn/the_gnat_compilation_model id63}@anchor{b6}
5550 @subsection Calling Conventions
5551
5552
5553 @geindex Foreign Languages
5554
5555 @geindex Calling Conventions
5556
5557 GNAT follows standard calling sequence conventions and will thus interface
5558 to any other language that also follows these conventions. The following
5559 Convention identifiers are recognized by GNAT:
5560
5561 @geindex Interfacing to Ada
5562
5563 @geindex Other Ada compilers
5564
5565 @geindex Convention Ada
5566
5567
5568 @table @asis
5569
5570 @item @code{Ada}
5571
5572 This indicates that the standard Ada calling sequence will be
5573 used and all Ada data items may be passed without any limitations in the
5574 case where GNAT is used to generate both the caller and callee. It is also
5575 possible to mix GNAT generated code and code generated by another Ada
5576 compiler. In this case, the data types should be restricted to simple
5577 cases, including primitive types. Whether complex data types can be passed
5578 depends on the situation. Probably it is safe to pass simple arrays, such
5579 as arrays of integers or floats. Records may or may not work, depending
5580 on whether both compilers lay them out identically. Complex structures
5581 involving variant records, access parameters, tasks, or protected types,
5582 are unlikely to be able to be passed.
5583
5584 Note that in the case of GNAT running
5585 on a platform that supports HP Ada 83, a higher degree of compatibility
5586 can be guaranteed, and in particular records are laid out in an identical
5587 manner in the two compilers. Note also that if output from two different
5588 compilers is mixed, the program is responsible for dealing with elaboration
5589 issues. Probably the safest approach is to write the main program in the
5590 version of Ada other than GNAT, so that it takes care of its own elaboration
5591 requirements, and then call the GNAT-generated adainit procedure to ensure
5592 elaboration of the GNAT components. Consult the documentation of the other
5593 Ada compiler for further details on elaboration.
5594
5595 However, it is not possible to mix the tasking run time of GNAT and
5596 HP Ada 83, All the tasking operations must either be entirely within
5597 GNAT compiled sections of the program, or entirely within HP Ada 83
5598 compiled sections of the program.
5599 @end table
5600
5601 @geindex Interfacing to Assembly
5602
5603 @geindex Convention Assembler
5604
5605
5606 @table @asis
5607
5608 @item @code{Assembler}
5609
5610 Specifies assembler as the convention. In practice this has the
5611 same effect as convention Ada (but is not equivalent in the sense of being
5612 considered the same convention).
5613 @end table
5614
5615 @geindex Convention Asm
5616
5617 @geindex Asm
5618
5619
5620 @table @asis
5621
5622 @item @code{Asm}
5623
5624 Equivalent to Assembler.
5625
5626 @geindex Interfacing to COBOL
5627
5628 @geindex Convention COBOL
5629 @end table
5630
5631 @geindex COBOL
5632
5633
5634 @table @asis
5635
5636 @item @code{COBOL}
5637
5638 Data will be passed according to the conventions described
5639 in section B.4 of the Ada Reference Manual.
5640 @end table
5641
5642 @geindex C
5643
5644 @geindex Interfacing to C
5645
5646 @geindex Convention C
5647
5648
5649 @table @asis
5650
5651 @item @code{C}
5652
5653 Data will be passed according to the conventions described
5654 in section B.3 of the Ada Reference Manual.
5655
5656 A note on interfacing to a C 'varargs' function:
5657
5658 @quotation
5659
5660 @geindex C varargs function
5661
5662 @geindex Interfacing to C varargs function
5663
5664 @geindex varargs function interfaces
5665
5666 In C, @code{varargs} allows a function to take a variable number of
5667 arguments. There is no direct equivalent in this to Ada. One
5668 approach that can be used is to create a C wrapper for each
5669 different profile and then interface to this C wrapper. For
5670 example, to print an @code{int} value using @code{printf},
5671 create a C function @code{printfi} that takes two arguments, a
5672 pointer to a string and an int, and calls @code{printf}.
5673 Then in the Ada program, use pragma @code{Import} to
5674 interface to @code{printfi}.
5675
5676 It may work on some platforms to directly interface to
5677 a @code{varargs} function by providing a specific Ada profile
5678 for a particular call. However, this does not work on
5679 all platforms, since there is no guarantee that the
5680 calling sequence for a two argument normal C function
5681 is the same as for calling a @code{varargs} C function with
5682 the same two arguments.
5683 @end quotation
5684 @end table
5685
5686 @geindex Convention Default
5687
5688 @geindex Default
5689
5690
5691 @table @asis
5692
5693 @item @code{Default}
5694
5695 Equivalent to C.
5696 @end table
5697
5698 @geindex Convention External
5699
5700 @geindex External
5701
5702
5703 @table @asis
5704
5705 @item @code{External}
5706
5707 Equivalent to C.
5708 @end table
5709
5710 @geindex C++
5711
5712 @geindex Interfacing to C++
5713
5714 @geindex Convention C++
5715
5716
5717 @table @asis
5718
5719 @item @code{C_Plus_Plus} (or @code{CPP})
5720
5721 This stands for C++. For most purposes this is identical to C.
5722 See the separate description of the specialized GNAT pragmas relating to
5723 C++ interfacing for further details.
5724 @end table
5725
5726 @geindex Fortran
5727
5728 @geindex Interfacing to Fortran
5729
5730 @geindex Convention Fortran
5731
5732
5733 @table @asis
5734
5735 @item @code{Fortran}
5736
5737 Data will be passed according to the conventions described
5738 in section B.5 of the Ada Reference Manual.
5739
5740 @item @code{Intrinsic}
5741
5742 This applies to an intrinsic operation, as defined in the Ada
5743 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
5744 this means that the body of the subprogram is provided by the compiler itself,
5745 usually by means of an efficient code sequence, and that the user does not
5746 supply an explicit body for it. In an application program, the pragma may
5747 be applied to the following sets of names:
5748
5749
5750 @itemize *
5751
5752 @item
5753 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic.
5754 The corresponding subprogram declaration must have
5755 two formal parameters. The
5756 first one must be a signed integer type or a modular type with a binary
5757 modulus, and the second parameter must be of type Natural.
5758 The return type must be the same as the type of the first argument. The size
5759 of this type can only be 8, 16, 32, or 64.
5760
5761 @item
5762 Binary arithmetic operators: '+', '-', '*', '/'.
5763 The corresponding operator declaration must have parameters and result type
5764 that have the same root numeric type (for example, all three are long_float
5765 types). This simplifies the definition of operations that use type checking
5766 to perform dimensional checks:
5767 @end itemize
5768
5769 @example
5770 type Distance is new Long_Float;
5771 type Time is new Long_Float;
5772 type Velocity is new Long_Float;
5773 function "/" (D : Distance; T : Time)
5774 return Velocity;
5775 pragma Import (Intrinsic, "/");
5776
5777 This common idiom is often programmed with a generic definition and an
5778 explicit body. The pragma makes it simpler to introduce such declarations.
5779 It incurs no overhead in compilation time or code size, because it is
5780 implemented as a single machine instruction.
5781 @end example
5782
5783
5784 @itemize *
5785
5786 @item
5787 General subprogram entities. This is used to bind an Ada subprogram
5788 declaration to
5789 a compiler builtin by name with back-ends where such interfaces are
5790 available. A typical example is the set of @code{__builtin} functions
5791 exposed by the GCC back-end, as in the following example:
5792
5793 @example
5794 function builtin_sqrt (F : Float) return Float;
5795 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
5796 @end example
5797
5798 Most of the GCC builtins are accessible this way, and as for other
5799 import conventions (e.g. C), it is the user's responsibility to ensure
5800 that the Ada subprogram profile matches the underlying builtin
5801 expectations.
5802 @end itemize
5803 @end table
5804
5805 @geindex Stdcall
5806
5807 @geindex Convention Stdcall
5808
5809
5810 @table @asis
5811
5812 @item @code{Stdcall}
5813
5814 This is relevant only to Windows implementations of GNAT,
5815 and specifies that the @code{Stdcall} calling sequence will be used,
5816 as defined by the NT API. Nevertheless, to ease building
5817 cross-platform bindings this convention will be handled as a @code{C} calling
5818 convention on non-Windows platforms.
5819 @end table
5820
5821 @geindex DLL
5822
5823 @geindex Convention DLL
5824
5825
5826 @table @asis
5827
5828 @item @code{DLL}
5829
5830 This is equivalent to @code{Stdcall}.
5831 @end table
5832
5833 @geindex Win32
5834
5835 @geindex Convention Win32
5836
5837
5838 @table @asis
5839
5840 @item @code{Win32}
5841
5842 This is equivalent to @code{Stdcall}.
5843 @end table
5844
5845 @geindex Stubbed
5846
5847 @geindex Convention Stubbed
5848
5849
5850 @table @asis
5851
5852 @item @code{Stubbed}
5853
5854 This is a special convention that indicates that the compiler
5855 should provide a stub body that raises @code{Program_Error}.
5856 @end table
5857
5858 GNAT additionally provides a useful pragma @code{Convention_Identifier}
5859 that can be used to parameterize conventions and allow additional synonyms
5860 to be specified. For example if you have legacy code in which the convention
5861 identifier Fortran77 was used for Fortran, you can use the configuration
5862 pragma:
5863
5864 @example
5865 pragma Convention_Identifier (Fortran77, Fortran);
5866 @end example
5867
5868 And from now on the identifier Fortran77 may be used as a convention
5869 identifier (for example in an @code{Import} pragma) with the same
5870 meaning as Fortran.
5871
5872 @node Building Mixed Ada and C++ Programs,Generating Ada Bindings for C and C++ headers,Calling Conventions,Mixed Language Programming
5873 @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}
5874 @subsection Building Mixed Ada and C++ Programs
5875
5876
5877 A programmer inexperienced with mixed-language development may find that
5878 building an application containing both Ada and C++ code can be a
5879 challenge. This section gives a few hints that should make this task easier.
5880
5881 @menu
5882 * Interfacing to C++::
5883 * Linking a Mixed C++ & Ada Program::
5884 * A Simple Example::
5885 * Interfacing with C++ constructors::
5886 * Interfacing with C++ at the Class Level::
5887
5888 @end menu
5889
5890 @node Interfacing to C++,Linking a Mixed C++ & Ada Program,,Building Mixed Ada and C++ Programs
5891 @anchor{gnat_ugn/the_gnat_compilation_model id65}@anchor{b9}@anchor{gnat_ugn/the_gnat_compilation_model id66}@anchor{ba}
5892 @subsubsection Interfacing to C++
5893
5894
5895 GNAT supports interfacing with the G++ compiler (or any C++ compiler
5896 generating code that is compatible with the G++ Application Binary
5897 Interface ---see @indicateurl{http://www.codesourcery.com/archives/cxx-abi}).
5898
5899 Interfacing can be done at 3 levels: simple data, subprograms, and
5900 classes. In the first two cases, GNAT offers a specific @code{Convention C_Plus_Plus}
5901 (or @code{CPP}) that behaves exactly like @code{Convention C}.
5902 Usually, C++ mangles the names of subprograms. To generate proper mangled
5903 names automatically, see @ref{19,,Generating Ada Bindings for C and C++ headers}).
5904 This problem can also be addressed manually in two ways:
5905
5906
5907 @itemize *
5908
5909 @item
5910 by modifying the C++ code in order to force a C convention using
5911 the @code{extern "C"} syntax.
5912
5913 @item
5914 by figuring out the mangled name (using e.g. @code{nm}) and using it as the
5915 Link_Name argument of the pragma import.
5916 @end itemize
5917
5918 Interfacing at the class level can be achieved by using the GNAT specific
5919 pragmas such as @code{CPP_Constructor}. See the @cite{GNAT_Reference_Manual} for additional information.
5920
5921 @node Linking a Mixed C++ & Ada Program,A Simple Example,Interfacing to C++,Building Mixed Ada and C++ Programs
5922 @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}
5923 @subsubsection Linking a Mixed C++ & Ada Program
5924
5925
5926 Usually the linker of the C++ development system must be used to link
5927 mixed applications because most C++ systems will resolve elaboration
5928 issues (such as calling constructors on global class instances)
5929 transparently during the link phase. GNAT has been adapted to ease the
5930 use of a foreign linker for the last phase. Three cases can be
5931 considered:
5932
5933
5934 @itemize *
5935
5936 @item
5937 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
5938 The C++ linker can simply be called by using the C++ specific driver
5939 called @code{g++}.
5940
5941 Note that if the C++ code uses inline functions, you will need to
5942 compile your C++ code with the @code{-fkeep-inline-functions} switch in
5943 order to provide an existing function implementation that the Ada code can
5944 link with.
5945
5946 @example
5947 $ g++ -c -fkeep-inline-functions file1.C
5948 $ g++ -c -fkeep-inline-functions file2.C
5949 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
5950 @end example
5951
5952 @item
5953 Using GNAT and G++ from two different GCC installations: If both
5954 compilers are on the :envvar`PATH`, the previous method may be used. It is
5955 important to note that environment variables such as
5956 @geindex C_INCLUDE_PATH
5957 @geindex environment variable; C_INCLUDE_PATH
5958 @code{C_INCLUDE_PATH},
5959 @geindex GCC_EXEC_PREFIX
5960 @geindex environment variable; GCC_EXEC_PREFIX
5961 @code{GCC_EXEC_PREFIX},
5962 @geindex BINUTILS_ROOT
5963 @geindex environment variable; BINUTILS_ROOT
5964 @code{BINUTILS_ROOT}, and
5965 @geindex GCC_ROOT
5966 @geindex environment variable; GCC_ROOT
5967 @code{GCC_ROOT} will affect both compilers
5968 at the same time and may make one of the two compilers operate
5969 improperly if set during invocation of the wrong compiler. It is also
5970 very important that the linker uses the proper @code{libgcc.a} GCC
5971 library -- that is, the one from the C++ compiler installation. The
5972 implicit link command as suggested in the @code{gnatmake} command
5973 from the former example can be replaced by an explicit link command with
5974 the full-verbosity option in order to verify which library is used:
5975
5976 @example
5977 $ gnatbind ada_unit
5978 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
5979 @end example
5980
5981 If there is a problem due to interfering environment variables, it can
5982 be worked around by using an intermediate script. The following example
5983 shows the proper script to use when GNAT has not been installed at its
5984 default location and g++ has been installed at its default location:
5985
5986 @example
5987 $ cat ./my_script
5988 #!/bin/sh
5989 unset BINUTILS_ROOT
5990 unset GCC_ROOT
5991 c++ $*
5992 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
5993 @end example
5994
5995 @item
5996 Using a non-GNU C++ compiler: The commands previously described can be
5997 used to insure that the C++ linker is used. Nonetheless, you need to add
5998 a few more parameters to the link command line, depending on the exception
5999 mechanism used.
6000
6001 If the @code{setjmp} / @code{longjmp} exception mechanism is used, only the paths
6002 to the @code{libgcc} libraries are required:
6003
6004 @example
6005 $ cat ./my_script
6006 #!/bin/sh
6007 CC $* gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a
6008 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6009 @end example
6010
6011 where CC is the name of the non-GNU C++ compiler.
6012
6013 If the "zero cost" exception mechanism is used, and the platform
6014 supports automatic registration of exception tables (e.g., Solaris),
6015 paths to more objects are required:
6016
6017 @example
6018 $ cat ./my_script
6019 #!/bin/sh
6020 CC gcc -print-file-name=crtbegin.o $* \\
6021 gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a \\
6022 gcc -print-file-name=crtend.o
6023 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6024 @end example
6025
6026 If the "zero cost exception" mechanism is used, and the platform
6027 doesn't support automatic registration of exception tables (e.g., HP-UX
6028 or AIX), the simple approach described above will not work and
6029 a pre-linking phase using GNAT will be necessary.
6030 @end itemize
6031
6032 Another alternative is to use the @code{gprbuild} multi-language builder
6033 which has a large knowledge base and knows how to link Ada and C++ code
6034 together automatically in most cases.
6035
6036 @node A Simple Example,Interfacing with C++ constructors,Linking a Mixed C++ & Ada Program,Building Mixed Ada and C++ Programs
6037 @anchor{gnat_ugn/the_gnat_compilation_model id67}@anchor{bd}@anchor{gnat_ugn/the_gnat_compilation_model a-simple-example}@anchor{be}
6038 @subsubsection A Simple Example
6039
6040
6041 The following example, provided as part of the GNAT examples, shows how
6042 to achieve procedural interfacing between Ada and C++ in both
6043 directions. The C++ class A has two methods. The first method is exported
6044 to Ada by the means of an extern C wrapper function. The second method
6045 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
6046 a limited record with a layout comparable to the C++ class. The Ada
6047 subprogram, in turn, calls the C++ method. So, starting from the C++
6048 main program, the process passes back and forth between the two
6049 languages.
6050
6051 Here are the compilation commands:
6052
6053 @example
6054 $ gnatmake -c simple_cpp_interface
6055 $ g++ -c cpp_main.C
6056 $ g++ -c ex7.C
6057 $ gnatbind -n simple_cpp_interface
6058 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ ex7.o cpp_main.o
6059 @end example
6060
6061 Here are the corresponding sources:
6062
6063 @example
6064 //cpp_main.C
6065
6066 #include "ex7.h"
6067
6068 extern "C" @{
6069 void adainit (void);
6070 void adafinal (void);
6071 void method1 (A *t);
6072 @}
6073
6074 void method1 (A *t)
6075 @{
6076 t->method1 ();
6077 @}
6078
6079 int main ()
6080 @{
6081 A obj;
6082 adainit ();
6083 obj.method2 (3030);
6084 adafinal ();
6085 @}
6086 @end example
6087
6088 @example
6089 //ex7.h
6090
6091 class Origin @{
6092 public:
6093 int o_value;
6094 @};
6095 class A : public Origin @{
6096 public:
6097 void method1 (void);
6098 void method2 (int v);
6099 A();
6100 int a_value;
6101 @};
6102 @end example
6103
6104 @example
6105 //ex7.C
6106
6107 #include "ex7.h"
6108 #include <stdio.h>
6109
6110 extern "C" @{ void ada_method2 (A *t, int v);@}
6111
6112 void A::method1 (void)
6113 @{
6114 a_value = 2020;
6115 printf ("in A::method1, a_value = %d \\n",a_value);
6116 @}
6117
6118 void A::method2 (int v)
6119 @{
6120 ada_method2 (this, v);
6121 printf ("in A::method2, a_value = %d \\n",a_value);
6122 @}
6123
6124 A::A(void)
6125 @{
6126 a_value = 1010;
6127 printf ("in A::A, a_value = %d \\n",a_value);
6128 @}
6129 @end example
6130
6131 @example
6132 -- simple_cpp_interface.ads
6133 with System;
6134 package Simple_Cpp_Interface is
6135 type A is limited
6136 record
6137 Vptr : System.Address;
6138 O_Value : Integer;
6139 A_Value : Integer;
6140 end record;
6141 pragma Convention (C, A);
6142
6143 procedure Method1 (This : in out A);
6144 pragma Import (C, Method1);
6145
6146 procedure Ada_Method2 (This : in out A; V : Integer);
6147 pragma Export (C, Ada_Method2);
6148
6149 end Simple_Cpp_Interface;
6150 @end example
6151
6152 @example
6153 -- simple_cpp_interface.adb
6154 package body Simple_Cpp_Interface is
6155
6156 procedure Ada_Method2 (This : in out A; V : Integer) is
6157 begin
6158 Method1 (This);
6159 This.A_Value := V;
6160 end Ada_Method2;
6161
6162 end Simple_Cpp_Interface;
6163 @end example
6164
6165 @node Interfacing with C++ constructors,Interfacing with C++ at the Class Level,A Simple Example,Building Mixed Ada and C++ Programs
6166 @anchor{gnat_ugn/the_gnat_compilation_model id68}@anchor{bf}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-constructors}@anchor{c0}
6167 @subsubsection Interfacing with C++ constructors
6168
6169
6170 In order to interface with C++ constructors GNAT provides the
6171 @code{pragma CPP_Constructor} (see the @cite{GNAT_Reference_Manual}
6172 for additional information).
6173 In this section we present some common uses of C++ constructors
6174 in mixed-languages programs in GNAT.
6175
6176 Let us assume that we need to interface with the following
6177 C++ class:
6178
6179 @example
6180 class Root @{
6181 public:
6182 int a_value;
6183 int b_value;
6184 virtual int Get_Value ();
6185 Root(); // Default constructor
6186 Root(int v); // 1st non-default constructor
6187 Root(int v, int w); // 2nd non-default constructor
6188 @};
6189 @end example
6190
6191 For this purpose we can write the following package spec (further
6192 information on how to build this spec is available in
6193 @ref{c1,,Interfacing with C++ at the Class Level} and
6194 @ref{19,,Generating Ada Bindings for C and C++ headers}).
6195
6196 @example
6197 with Interfaces.C; use Interfaces.C;
6198 package Pkg_Root is
6199 type Root is tagged limited record
6200 A_Value : int;
6201 B_Value : int;
6202 end record;
6203 pragma Import (CPP, Root);
6204
6205 function Get_Value (Obj : Root) return int;
6206 pragma Import (CPP, Get_Value);
6207
6208 function Constructor return Root;
6209 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
6210
6211 function Constructor (v : Integer) return Root;
6212 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
6213
6214 function Constructor (v, w : Integer) return Root;
6215 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
6216 end Pkg_Root;
6217 @end example
6218
6219 On the Ada side the constructor is represented by a function (whose
6220 name is arbitrary) that returns the classwide type corresponding to
6221 the imported C++ class. Although the constructor is described as a
6222 function, it is typically a procedure with an extra implicit argument
6223 (the object being initialized) at the implementation level. GNAT
6224 issues the appropriate call, whatever it is, to get the object
6225 properly initialized.
6226
6227 Constructors can only appear in the following contexts:
6228
6229
6230 @itemize *
6231
6232 @item
6233 On the right side of an initialization of an object of type @code{T}.
6234
6235 @item
6236 On the right side of an initialization of a record component of type @code{T}.
6237
6238 @item
6239 In an Ada 2005 limited aggregate.
6240
6241 @item
6242 In an Ada 2005 nested limited aggregate.
6243
6244 @item
6245 In an Ada 2005 limited aggregate that initializes an object built in
6246 place by an extended return statement.
6247 @end itemize
6248
6249 In a declaration of an object whose type is a class imported from C++,
6250 either the default C++ constructor is implicitly called by GNAT, or
6251 else the required C++ constructor must be explicitly called in the
6252 expression that initializes the object. For example:
6253
6254 @example
6255 Obj1 : Root;
6256 Obj2 : Root := Constructor;
6257 Obj3 : Root := Constructor (v => 10);
6258 Obj4 : Root := Constructor (30, 40);
6259 @end example
6260
6261 The first two declarations are equivalent: in both cases the default C++
6262 constructor is invoked (in the former case the call to the constructor is
6263 implicit, and in the latter case the call is explicit in the object
6264 declaration). @code{Obj3} is initialized by the C++ non-default constructor
6265 that takes an integer argument, and @code{Obj4} is initialized by the
6266 non-default C++ constructor that takes two integers.
6267
6268 Let us derive the imported C++ class in the Ada side. For example:
6269
6270 @example
6271 type DT is new Root with record
6272 C_Value : Natural := 2009;
6273 end record;
6274 @end example
6275
6276 In this case the components DT inherited from the C++ side must be
6277 initialized by a C++ constructor, and the additional Ada components
6278 of type DT are initialized by GNAT. The initialization of such an
6279 object is done either by default, or by means of a function returning
6280 an aggregate of type DT, or by means of an extension aggregate.
6281
6282 @example
6283 Obj5 : DT;
6284 Obj6 : DT := Function_Returning_DT (50);
6285 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
6286 @end example
6287
6288 The declaration of @code{Obj5} invokes the default constructors: the
6289 C++ default constructor of the parent type takes care of the initialization
6290 of the components inherited from Root, and GNAT takes care of the default
6291 initialization of the additional Ada components of type DT (that is,
6292 @code{C_Value} is initialized to value 2009). The order of invocation of
6293 the constructors is consistent with the order of elaboration required by
6294 Ada and C++. That is, the constructor of the parent type is always called
6295 before the constructor of the derived type.
6296
6297 Let us now consider a record that has components whose type is imported
6298 from C++. For example:
6299
6300 @example
6301 type Rec1 is limited record
6302 Data1 : Root := Constructor (10);
6303 Value : Natural := 1000;
6304 end record;
6305
6306 type Rec2 (D : Integer := 20) is limited record
6307 Rec : Rec1;
6308 Data2 : Root := Constructor (D, 30);
6309 end record;
6310 @end example
6311
6312 The initialization of an object of type @code{Rec2} will call the
6313 non-default C++ constructors specified for the imported components.
6314 For example:
6315
6316 @example
6317 Obj8 : Rec2 (40);
6318 @end example
6319
6320 Using Ada 2005 we can use limited aggregates to initialize an object
6321 invoking C++ constructors that differ from those specified in the type
6322 declarations. For example:
6323
6324 @example
6325 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
6326 others => <>),
6327 others => <>);
6328 @end example
6329
6330 The above declaration uses an Ada 2005 limited aggregate to
6331 initialize @code{Obj9}, and the C++ constructor that has two integer
6332 arguments is invoked to initialize the @code{Data1} component instead
6333 of the constructor specified in the declaration of type @code{Rec1}. In
6334 Ada 2005 the box in the aggregate indicates that unspecified components
6335 are initialized using the expression (if any) available in the component
6336 declaration. That is, in this case discriminant @code{D} is initialized
6337 to value @code{20}, @code{Value} is initialized to value 1000, and the
6338 non-default C++ constructor that handles two integers takes care of
6339 initializing component @code{Data2} with values @code{20,30}.
6340
6341 In Ada 2005 we can use the extended return statement to build the Ada
6342 equivalent to C++ non-default constructors. For example:
6343
6344 @example
6345 function Constructor (V : Integer) return Rec2 is
6346 begin
6347 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
6348 others => <>),
6349 others => <>) do
6350 -- Further actions required for construction of
6351 -- objects of type Rec2
6352 ...
6353 end record;
6354 end Constructor;
6355 @end example
6356
6357 In this example the extended return statement construct is used to
6358 build in place the returned object whose components are initialized
6359 by means of a limited aggregate. Any further action associated with
6360 the constructor can be placed inside the construct.
6361
6362 @node Interfacing with C++ at the Class Level,,Interfacing with C++ constructors,Building Mixed Ada and C++ Programs
6363 @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}
6364 @subsubsection Interfacing with C++ at the Class Level
6365
6366
6367 In this section we demonstrate the GNAT features for interfacing with
6368 C++ by means of an example making use of Ada 2005 abstract interface
6369 types. This example consists of a classification of animals; classes
6370 have been used to model our main classification of animals, and
6371 interfaces provide support for the management of secondary
6372 classifications. We first demonstrate a case in which the types and
6373 constructors are defined on the C++ side and imported from the Ada
6374 side, and latter the reverse case.
6375
6376 The root of our derivation will be the @code{Animal} class, with a
6377 single private attribute (the @code{Age} of the animal), a constructor,
6378 and two public primitives to set and get the value of this attribute.
6379
6380 @example
6381 class Animal @{
6382 public:
6383 virtual void Set_Age (int New_Age);
6384 virtual int Age ();
6385 Animal() @{Age_Count = 0;@};
6386 private:
6387 int Age_Count;
6388 @};
6389 @end example
6390
6391 Abstract interface types are defined in C++ by means of classes with pure
6392 virtual functions and no data members. In our example we will use two
6393 interfaces that provide support for the common management of @code{Carnivore}
6394 and @code{Domestic} animals:
6395
6396 @example
6397 class Carnivore @{
6398 public:
6399 virtual int Number_Of_Teeth () = 0;
6400 @};
6401
6402 class Domestic @{
6403 public:
6404 virtual void Set_Owner (char* Name) = 0;
6405 @};
6406 @end example
6407
6408 Using these declarations, we can now say that a @code{Dog} is an animal that is
6409 both Carnivore and Domestic, that is:
6410
6411 @example
6412 class Dog : Animal, Carnivore, Domestic @{
6413 public:
6414 virtual int Number_Of_Teeth ();
6415 virtual void Set_Owner (char* Name);
6416
6417 Dog(); // Constructor
6418 private:
6419 int Tooth_Count;
6420 char *Owner;
6421 @};
6422 @end example
6423
6424 In the following examples we will assume that the previous declarations are
6425 located in a file named @code{animals.h}. The following package demonstrates
6426 how to import these C++ declarations from the Ada side:
6427
6428 @example
6429 with Interfaces.C.Strings; use Interfaces.C.Strings;
6430 package Animals is
6431 type Carnivore is limited interface;
6432 pragma Convention (C_Plus_Plus, Carnivore);
6433 function Number_Of_Teeth (X : Carnivore)
6434 return Natural is abstract;
6435
6436 type Domestic is limited interface;
6437 pragma Convention (C_Plus_Plus, Domestic);
6438 procedure Set_Owner
6439 (X : in out Domestic;
6440 Name : Chars_Ptr) is abstract;
6441
6442 type Animal is tagged limited record
6443 Age : Natural;
6444 end record;
6445 pragma Import (C_Plus_Plus, Animal);
6446
6447 procedure Set_Age (X : in out Animal; Age : Integer);
6448 pragma Import (C_Plus_Plus, Set_Age);
6449
6450 function Age (X : Animal) return Integer;
6451 pragma Import (C_Plus_Plus, Age);
6452
6453 function New_Animal return Animal;
6454 pragma CPP_Constructor (New_Animal);
6455 pragma Import (CPP, New_Animal, "_ZN6AnimalC1Ev");
6456
6457 type Dog is new Animal and Carnivore and Domestic with record
6458 Tooth_Count : Natural;
6459 Owner : Chars_Ptr;
6460 end record;
6461 pragma Import (C_Plus_Plus, Dog);
6462
6463 function Number_Of_Teeth (A : Dog) return Natural;
6464 pragma Import (C_Plus_Plus, Number_Of_Teeth);
6465
6466 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6467 pragma Import (C_Plus_Plus, Set_Owner);
6468
6469 function New_Dog return Dog;
6470 pragma CPP_Constructor (New_Dog);
6471 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
6472 end Animals;
6473 @end example
6474
6475 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
6476 interfacing with these C++ classes is easy. The only requirement is that all
6477 the primitives and components must be declared exactly in the same order in
6478 the two languages.
6479
6480 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
6481 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
6482 the arguments to the called primitives will be the same as for C++. For the
6483 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
6484 to indicate that they have been defined on the C++ side; this is required
6485 because the dispatch table associated with these tagged types will be built
6486 in the C++ side and therefore will not contain the predefined Ada primitives
6487 which Ada would otherwise expect.
6488
6489 As the reader can see there is no need to indicate the C++ mangled names
6490 associated with each subprogram because it is assumed that all the calls to
6491 these primitives will be dispatching calls. The only exception is the
6492 constructor, which must be registered with the compiler by means of
6493 @code{pragma CPP_Constructor} and needs to provide its associated C++
6494 mangled name because the Ada compiler generates direct calls to it.
6495
6496 With the above packages we can now declare objects of type Dog on the Ada side
6497 and dispatch calls to the corresponding subprograms on the C++ side. We can
6498 also extend the tagged type Dog with further fields and primitives, and
6499 override some of its C++ primitives on the Ada side. For example, here we have
6500 a type derivation defined on the Ada side that inherits all the dispatching
6501 primitives of the ancestor from the C++ side.
6502
6503 @example
6504 with Animals; use Animals;
6505 package Vaccinated_Animals is
6506 type Vaccinated_Dog is new Dog with null record;
6507 function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
6508 end Vaccinated_Animals;
6509 @end example
6510
6511 It is important to note that, because of the ABI compatibility, the programmer
6512 does not need to add any further information to indicate either the object
6513 layout or the dispatch table entry associated with each dispatching operation.
6514
6515 Now let us define all the types and constructors on the Ada side and export
6516 them to C++, using the same hierarchy of our previous example:
6517
6518 @example
6519 with Interfaces.C.Strings;
6520 use Interfaces.C.Strings;
6521 package Animals is
6522 type Carnivore is limited interface;
6523 pragma Convention (C_Plus_Plus, Carnivore);
6524 function Number_Of_Teeth (X : Carnivore)
6525 return Natural is abstract;
6526
6527 type Domestic is limited interface;
6528 pragma Convention (C_Plus_Plus, Domestic);
6529 procedure Set_Owner
6530 (X : in out Domestic;
6531 Name : Chars_Ptr) is abstract;
6532
6533 type Animal is tagged record
6534 Age : Natural;
6535 end record;
6536 pragma Convention (C_Plus_Plus, Animal);
6537
6538 procedure Set_Age (X : in out Animal; Age : Integer);
6539 pragma Export (C_Plus_Plus, Set_Age);
6540
6541 function Age (X : Animal) return Integer;
6542 pragma Export (C_Plus_Plus, Age);
6543
6544 function New_Animal return Animal'Class;
6545 pragma Export (C_Plus_Plus, New_Animal);
6546
6547 type Dog is new Animal and Carnivore and Domestic with record
6548 Tooth_Count : Natural;
6549 Owner : String (1 .. 30);
6550 end record;
6551 pragma Convention (C_Plus_Plus, Dog);
6552
6553 function Number_Of_Teeth (A : Dog) return Natural;
6554 pragma Export (C_Plus_Plus, Number_Of_Teeth);
6555
6556 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6557 pragma Export (C_Plus_Plus, Set_Owner);
6558
6559 function New_Dog return Dog'Class;
6560 pragma Export (C_Plus_Plus, New_Dog);
6561 end Animals;
6562 @end example
6563
6564 Compared with our previous example the only differences are the use of
6565 @code{pragma Convention} (instead of @code{pragma Import}), and the use of
6566 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
6567 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
6568 nothing else to be done; as explained above, the only requirement is that all
6569 the primitives and components are declared in exactly the same order.
6570
6571 For completeness, let us see a brief C++ main program that uses the
6572 declarations available in @code{animals.h} (presented in our first example) to
6573 import and use the declarations from the Ada side, properly initializing and
6574 finalizing the Ada run-time system along the way:
6575
6576 @example
6577 #include "animals.h"
6578 #include <iostream>
6579 using namespace std;
6580
6581 void Check_Carnivore (Carnivore *obj) @{...@}
6582 void Check_Domestic (Domestic *obj) @{...@}
6583 void Check_Animal (Animal *obj) @{...@}
6584 void Check_Dog (Dog *obj) @{...@}
6585
6586 extern "C" @{
6587 void adainit (void);
6588 void adafinal (void);
6589 Dog* new_dog ();
6590 @}
6591
6592 void test ()
6593 @{
6594 Dog *obj = new_dog(); // Ada constructor
6595 Check_Carnivore (obj); // Check secondary DT
6596 Check_Domestic (obj); // Check secondary DT
6597 Check_Animal (obj); // Check primary DT
6598 Check_Dog (obj); // Check primary DT
6599 @}
6600
6601 int main ()
6602 @{
6603 adainit (); test(); adafinal ();
6604 return 0;
6605 @}
6606 @end example
6607
6608 @node Generating Ada Bindings for C and C++ headers,Generating C Headers for Ada Specifications,Building Mixed Ada and C++ Programs,Mixed Language Programming
6609 @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}
6610 @subsection Generating Ada Bindings for C and C++ headers
6611
6612
6613 @geindex Binding generation (for C and C++ headers)
6614
6615 @geindex C headers (binding generation)
6616
6617 @geindex C++ headers (binding generation)
6618
6619 GNAT includes a binding generator for C and C++ headers which is
6620 intended to do 95% of the tedious work of generating Ada specs from C
6621 or C++ header files.
6622
6623 Note that this capability is not intended to generate 100% correct Ada specs,
6624 and will is some cases require manual adjustments, although it can often
6625 be used out of the box in practice.
6626
6627 Some of the known limitations include:
6628
6629
6630 @itemize *
6631
6632 @item
6633 only very simple character constant macros are translated into Ada
6634 constants. Function macros (macros with arguments) are partially translated
6635 as comments, to be completed manually if needed.
6636
6637 @item
6638 some extensions (e.g. vector types) are not supported
6639
6640 @item
6641 pointers to pointers or complex structures are mapped to System.Address
6642
6643 @item
6644 identifiers with identical name (except casing) will generate compilation
6645 errors (e.g. @code{shm_get} vs @code{SHM_GET}).
6646 @end itemize
6647
6648 The code generated is using the Ada 2005 syntax, which makes it
6649 easier to interface with other languages than previous versions of Ada.
6650
6651 @menu
6652 * Running the Binding Generator::
6653 * Generating Bindings for C++ Headers::
6654 * Switches::
6655
6656 @end menu
6657
6658 @node Running the Binding Generator,Generating Bindings for C++ Headers,,Generating Ada Bindings for C and C++ headers
6659 @anchor{gnat_ugn/the_gnat_compilation_model id71}@anchor{c4}@anchor{gnat_ugn/the_gnat_compilation_model running-the-binding-generator}@anchor{c5}
6660 @subsubsection Running the Binding Generator
6661
6662
6663 The binding generator is part of the @code{gcc} compiler and can be
6664 invoked via the @code{-fdump-ada-spec} switch, which will generate Ada
6665 spec files for the header files specified on the command line, and all
6666 header files needed by these files transitively. For example:
6667
6668 @example
6669 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
6670 $ gcc -c -gnat05 *.ads
6671 @end example
6672
6673 will generate, under GNU/Linux, the following files: @code{time_h.ads},
6674 @code{bits_time_h.ads}, @code{stddef_h.ads}, @code{bits_types_h.ads} which
6675 correspond to the files @code{/usr/include/time.h},
6676 @code{/usr/include/bits/time.h}, etc..., and will then compile these Ada specs
6677 in Ada 2005 mode.
6678
6679 The @code{-C} switch tells @code{gcc} to extract comments from headers,
6680 and will attempt to generate corresponding Ada comments.
6681
6682 If you want to generate a single Ada file and not the transitive closure, you
6683 can use instead the @code{-fdump-ada-spec-slim} switch.
6684
6685 You can optionally specify a parent unit, of which all generated units will
6686 be children, using @code{-fada-spec-parent=@emph{unit}}.
6687
6688 Note that we recommend when possible to use the @emph{g++} driver to
6689 generate bindings, even for most C headers, since this will in general
6690 generate better Ada specs. For generating bindings for C++ headers, it is
6691 mandatory to use the @emph{g++} command, or @emph{gcc -x c++} which
6692 is equivalent in this case. If @emph{g++} cannot work on your C headers
6693 because of incompatibilities between C and C++, then you can fallback to
6694 @code{gcc} instead.
6695
6696 For an example of better bindings generated from the C++ front-end,
6697 the name of the parameters (when available) are actually ignored by the C
6698 front-end. Consider the following C header:
6699
6700 @example
6701 extern void foo (int variable);
6702 @end example
6703
6704 with the C front-end, @code{variable} is ignored, and the above is handled as:
6705
6706 @example
6707 extern void foo (int);
6708 @end example
6709
6710 generating a generic:
6711
6712 @example
6713 procedure foo (param1 : int);
6714 @end example
6715
6716 with the C++ front-end, the name is available, and we generate:
6717
6718 @example
6719 procedure foo (variable : int);
6720 @end example
6721
6722 In some cases, the generated bindings will be more complete or more meaningful
6723 when defining some macros, which you can do via the @code{-D} switch. This
6724 is for example the case with @code{Xlib.h} under GNU/Linux:
6725
6726 @example
6727 $ g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
6728 @end example
6729
6730 The above will generate more complete bindings than a straight call without
6731 the @code{-DXLIB_ILLEGAL_ACCESS} switch.
6732
6733 In other cases, it is not possible to parse a header file in a stand-alone
6734 manner, because other include files need to be included first. In this
6735 case, the solution is to create a small header file including the needed
6736 @code{#include} and possible @code{#define} directives. For example, to
6737 generate Ada bindings for @code{readline/readline.h}, you need to first
6738 include @code{stdio.h}, so you can create a file with the following two
6739 lines in e.g. @code{readline1.h}:
6740
6741 @example
6742 #include <stdio.h>
6743 #include <readline/readline.h>
6744 @end example
6745
6746 and then generate Ada bindings from this file:
6747
6748 @example
6749 $ g++ -c -fdump-ada-spec readline1.h
6750 @end example
6751
6752 @node Generating Bindings for C++ Headers,Switches,Running the Binding Generator,Generating Ada Bindings for C and C++ headers
6753 @anchor{gnat_ugn/the_gnat_compilation_model id72}@anchor{c6}@anchor{gnat_ugn/the_gnat_compilation_model generating-bindings-for-c-headers}@anchor{c7}
6754 @subsubsection Generating Bindings for C++ Headers
6755
6756
6757 Generating bindings for C++ headers is done using the same options, always
6758 with the @emph{g++} compiler. Note that generating Ada spec from C++ headers is a
6759 much more complex job and support for C++ headers is much more limited that
6760 support for C headers. As a result, you will need to modify the resulting
6761 bindings by hand more extensively when using C++ headers.
6762
6763 In this mode, C++ classes will be mapped to Ada tagged types, constructors
6764 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
6765 multiple inheritance of abstract classes will be mapped to Ada interfaces
6766 (see the @emph{Interfacing to C++} section in the @cite{GNAT Reference Manual}
6767 for additional information on interfacing to C++).
6768
6769 For example, given the following C++ header file:
6770
6771 @example
6772 class Carnivore @{
6773 public:
6774 virtual int Number_Of_Teeth () = 0;
6775 @};
6776
6777 class Domestic @{
6778 public:
6779 virtual void Set_Owner (char* Name) = 0;
6780 @};
6781
6782 class Animal @{
6783 public:
6784 int Age_Count;
6785 virtual void Set_Age (int New_Age);
6786 @};
6787
6788 class Dog : Animal, Carnivore, Domestic @{
6789 public:
6790 int Tooth_Count;
6791 char *Owner;
6792
6793 virtual int Number_Of_Teeth ();
6794 virtual void Set_Owner (char* Name);
6795
6796 Dog();
6797 @};
6798 @end example
6799
6800 The corresponding Ada code is generated:
6801
6802 @example
6803 package Class_Carnivore is
6804 type Carnivore is limited interface;
6805 pragma Import (CPP, Carnivore);
6806
6807 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
6808 end;
6809 use Class_Carnivore;
6810
6811 package Class_Domestic is
6812 type Domestic is limited interface;
6813 pragma Import (CPP, Domestic);
6814
6815 procedure Set_Owner
6816 (this : access Domestic;
6817 Name : Interfaces.C.Strings.chars_ptr) is abstract;
6818 end;
6819 use Class_Domestic;
6820
6821 package Class_Animal is
6822 type Animal is tagged limited record
6823 Age_Count : aliased int;
6824 end record;
6825 pragma Import (CPP, Animal);
6826
6827 procedure Set_Age (this : access Animal; New_Age : int);
6828 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
6829 end;
6830 use Class_Animal;
6831
6832 package Class_Dog is
6833 type Dog is new Animal and Carnivore and Domestic with record
6834 Tooth_Count : aliased int;
6835 Owner : Interfaces.C.Strings.chars_ptr;
6836 end record;
6837 pragma Import (CPP, Dog);
6838
6839 function Number_Of_Teeth (this : access Dog) return int;
6840 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
6841
6842 procedure Set_Owner
6843 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
6844 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
6845
6846 function New_Dog return Dog;
6847 pragma CPP_Constructor (New_Dog);
6848 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
6849 end;
6850 use Class_Dog;
6851 @end example
6852
6853 @node Switches,,Generating Bindings for C++ Headers,Generating Ada Bindings for C and C++ headers
6854 @anchor{gnat_ugn/the_gnat_compilation_model switches}@anchor{c8}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-ada-binding-generation}@anchor{c9}
6855 @subsubsection Switches
6856
6857
6858 @geindex -fdump-ada-spec (gcc)
6859
6860
6861 @table @asis
6862
6863 @item @code{-fdump-ada-spec}
6864
6865 Generate Ada spec files for the given header files transitively (including
6866 all header files that these headers depend upon).
6867 @end table
6868
6869 @geindex -fdump-ada-spec-slim (gcc)
6870
6871
6872 @table @asis
6873
6874 @item @code{-fdump-ada-spec-slim}
6875
6876 Generate Ada spec files for the header files specified on the command line
6877 only.
6878 @end table
6879
6880 @geindex -fada-spec-parent (gcc)
6881
6882
6883 @table @asis
6884
6885 @item @code{-fada-spec-parent=@emph{unit}}
6886
6887 Specifies that all files generated by @code{-fdump-ada-spec} are
6888 to be child units of the specified parent unit.
6889 @end table
6890
6891 @geindex -C (gcc)
6892
6893
6894 @table @asis
6895
6896 @item @code{-C}
6897
6898 Extract comments from headers and generate Ada comments in the Ada spec files.
6899 @end table
6900
6901 @node Generating C Headers for Ada Specifications,,Generating Ada Bindings for C and C++ headers,Mixed Language Programming
6902 @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}
6903 @subsection Generating C Headers for Ada Specifications
6904
6905
6906 @geindex Binding generation (for Ada specs)
6907
6908 @geindex C headers (binding generation)
6909
6910 GNAT includes a C header generator for Ada specifications which supports
6911 Ada types that have a direct mapping to C types. This includes in particular
6912 support for:
6913
6914
6915 @itemize *
6916
6917 @item
6918 Scalar types
6919
6920 @item
6921 Constrained arrays
6922
6923 @item
6924 Records (untagged)
6925
6926 @item
6927 Composition of the above types
6928
6929 @item
6930 Constant declarations
6931
6932 @item
6933 Object declarations
6934
6935 @item
6936 Subprogram declarations
6937 @end itemize
6938
6939 @menu
6940 * Running the C Header Generator::
6941
6942 @end menu
6943
6944 @node Running the C Header Generator,,,Generating C Headers for Ada Specifications
6945 @anchor{gnat_ugn/the_gnat_compilation_model running-the-c-header-generator}@anchor{cc}
6946 @subsubsection Running the C Header Generator
6947
6948
6949 The C header generator is part of the GNAT compiler and can be invoked via
6950 the @code{-gnatceg} combination of switches, which will generate a @code{.h}
6951 file corresponding to the given input file (Ada spec or body). Note that
6952 only spec files are processed in any case, so giving a spec or a body file
6953 as input is equivalent. For example:
6954
6955 @example
6956 $ gcc -c -gnatceg pack1.ads
6957 @end example
6958
6959 will generate a self-contained file called @code{pack1.h} including
6960 common definitions from the Ada Standard package, followed by the
6961 definitions included in @code{pack1.ads}, as well as all the other units
6962 withed by this file.
6963
6964 For instance, given the following Ada files:
6965
6966 @example
6967 package Pack2 is
6968 type Int is range 1 .. 10;
6969 end Pack2;
6970 @end example
6971
6972 @example
6973 with Pack2;
6974
6975 package Pack1 is
6976 type Rec is record
6977 Field1, Field2 : Pack2.Int;
6978 end record;
6979
6980 Global : Rec := (1, 2);
6981
6982 procedure Proc1 (R : Rec);
6983 procedure Proc2 (R : in out Rec);
6984 end Pack1;
6985 @end example
6986
6987 The above @code{gcc} command will generate the following @code{pack1.h} file:
6988
6989 @example
6990 /* Standard definitions skipped */
6991 #ifndef PACK2_ADS
6992 #define PACK2_ADS
6993 typedef short_short_integer pack2__TintB;
6994 typedef pack2__TintB pack2__int;
6995 #endif /* PACK2_ADS */
6996
6997 #ifndef PACK1_ADS
6998 #define PACK1_ADS
6999 typedef struct _pack1__rec @{
7000 pack2__int field1;
7001 pack2__int field2;
7002 @} pack1__rec;
7003 extern pack1__rec pack1__global;
7004 extern void pack1__proc1(const pack1__rec r);
7005 extern void pack1__proc2(pack1__rec *r);
7006 #endif /* PACK1_ADS */
7007 @end example
7008
7009 You can then @code{include} @code{pack1.h} from a C source file and use the types,
7010 call subprograms, reference objects, and constants.
7011
7012 @node GNAT and Other Compilation Models,Using GNAT Files with External Tools,Mixed Language Programming,The GNAT Compilation Model
7013 @anchor{gnat_ugn/the_gnat_compilation_model id74}@anchor{cd}@anchor{gnat_ugn/the_gnat_compilation_model gnat-and-other-compilation-models}@anchor{45}
7014 @section GNAT and Other Compilation Models
7015
7016
7017 This section compares the GNAT model with the approaches taken in
7018 other environents, first the C/C++ model and then the mechanism that
7019 has been used in other Ada systems, in particular those traditionally
7020 used for Ada 83.
7021
7022 @menu
7023 * Comparison between GNAT and C/C++ Compilation Models::
7024 * Comparison between GNAT and Conventional Ada Library Models::
7025
7026 @end menu
7027
7028 @node Comparison between GNAT and C/C++ Compilation Models,Comparison between GNAT and Conventional Ada Library Models,,GNAT and Other Compilation Models
7029 @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}
7030 @subsection Comparison between GNAT and C/C++ Compilation Models
7031
7032
7033 The GNAT model of compilation is close to the C and C++ models. You can
7034 think of Ada specs as corresponding to header files in C. As in C, you
7035 don't need to compile specs; they are compiled when they are used. The
7036 Ada @emph{with} is similar in effect to the @code{#include} of a C
7037 header.
7038
7039 One notable difference is that, in Ada, you may compile specs separately
7040 to check them for semantic and syntactic accuracy. This is not always
7041 possible with C headers because they are fragments of programs that have
7042 less specific syntactic or semantic rules.
7043
7044 The other major difference is the requirement for running the binder,
7045 which performs two important functions. First, it checks for
7046 consistency. In C or C++, the only defense against assembling
7047 inconsistent programs lies outside the compiler, in a makefile, for
7048 example. The binder satisfies the Ada requirement that it be impossible
7049 to construct an inconsistent program when the compiler is used in normal
7050 mode.
7051
7052 @geindex Elaboration order control
7053
7054 The other important function of the binder is to deal with elaboration
7055 issues. There are also elaboration issues in C++ that are handled
7056 automatically. This automatic handling has the advantage of being
7057 simpler to use, but the C++ programmer has no control over elaboration.
7058 Where @code{gnatbind} might complain there was no valid order of
7059 elaboration, a C++ compiler would simply construct a program that
7060 malfunctioned at run time.
7061
7062 @node Comparison between GNAT and Conventional Ada Library Models,,Comparison between GNAT and C/C++ Compilation Models,GNAT and Other Compilation Models
7063 @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}
7064 @subsection Comparison between GNAT and Conventional Ada Library Models
7065
7066
7067 This section is intended for Ada programmers who have
7068 used an Ada compiler implementing the traditional Ada library
7069 model, as described in the Ada Reference Manual.
7070
7071 @geindex GNAT library
7072
7073 In GNAT, there is no 'library' in the normal sense. Instead, the set of
7074 source files themselves acts as the library. Compiling Ada programs does
7075 not generate any centralized information, but rather an object file and
7076 a ALI file, which are of interest only to the binder and linker.
7077 In a traditional system, the compiler reads information not only from
7078 the source file being compiled, but also from the centralized library.
7079 This means that the effect of a compilation depends on what has been
7080 previously compiled. In particular:
7081
7082
7083 @itemize *
7084
7085 @item
7086 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7087 to the version of the unit most recently compiled into the library.
7088
7089 @item
7090 Inlining is effective only if the necessary body has already been
7091 compiled into the library.
7092
7093 @item
7094 Compiling a unit may obsolete other units in the library.
7095 @end itemize
7096
7097 In GNAT, compiling one unit never affects the compilation of any other
7098 units because the compiler reads only source files. Only changes to source
7099 files can affect the results of a compilation. In particular:
7100
7101
7102 @itemize *
7103
7104 @item
7105 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7106 to the source version of the unit that is currently accessible to the
7107 compiler.
7108
7109 @geindex Inlining
7110
7111 @item
7112 Inlining requires the appropriate source files for the package or
7113 subprogram bodies to be available to the compiler. Inlining is always
7114 effective, independent of the order in which units are compiled.
7115
7116 @item
7117 Compiling a unit never affects any other compilations. The editing of
7118 sources may cause previous compilations to be out of date if they
7119 depended on the source file being modified.
7120 @end itemize
7121
7122 The most important result of these differences is that order of compilation
7123 is never significant in GNAT. There is no situation in which one is
7124 required to do one compilation before another. What shows up as order of
7125 compilation requirements in the traditional Ada library becomes, in
7126 GNAT, simple source dependencies; in other words, there is only a set
7127 of rules saying what source files must be present when a file is
7128 compiled.
7129
7130 @node Using GNAT Files with External Tools,,GNAT and Other Compilation Models,The GNAT Compilation Model
7131 @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}
7132 @section Using GNAT Files with External Tools
7133
7134
7135 This section explains how files that are produced by GNAT may be
7136 used with tools designed for other languages.
7137
7138 @menu
7139 * Using Other Utility Programs with GNAT::
7140 * The External Symbol Naming Scheme of GNAT::
7141
7142 @end menu
7143
7144 @node Using Other Utility Programs with GNAT,The External Symbol Naming Scheme of GNAT,,Using GNAT Files with External Tools
7145 @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}
7146 @subsection Using Other Utility Programs with GNAT
7147
7148
7149 The object files generated by GNAT are in standard system format and in
7150 particular the debugging information uses this format. This means
7151 programs generated by GNAT can be used with existing utilities that
7152 depend on these formats.
7153
7154 In general, any utility program that works with C will also often work with
7155 Ada programs generated by GNAT. This includes software utilities such as
7156 gprof (a profiling program), gdb (the FSF debugger), and utilities such
7157 as Purify.
7158
7159 @node The External Symbol Naming Scheme of GNAT,,Using Other Utility Programs with GNAT,Using GNAT Files with External Tools
7160 @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}
7161 @subsection The External Symbol Naming Scheme of GNAT
7162
7163
7164 In order to interpret the output from GNAT, when using tools that are
7165 originally intended for use with other languages, it is useful to
7166 understand the conventions used to generate link names from the Ada
7167 entity names.
7168
7169 All link names are in all lowercase letters. With the exception of library
7170 procedure names, the mechanism used is simply to use the full expanded
7171 Ada name with dots replaced by double underscores. For example, suppose
7172 we have the following package spec:
7173
7174 @example
7175 package QRS is
7176 MN : Integer;
7177 end QRS;
7178 @end example
7179
7180 @geindex pragma Export
7181
7182 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
7183 the corresponding link name is @code{qrs__mn}.
7184 Of course if a @code{pragma Export} is used this may be overridden:
7185
7186 @example
7187 package Exports is
7188 Var1 : Integer;
7189 pragma Export (Var1, C, External_Name => "var1_name");
7190 Var2 : Integer;
7191 pragma Export (Var2, C, Link_Name => "var2_link_name");
7192 end Exports;
7193 @end example
7194
7195 In this case, the link name for @code{Var1} is whatever link name the
7196 C compiler would assign for the C function @code{var1_name}. This typically
7197 would be either @code{var1_name} or @code{_var1_name}, depending on operating
7198 system conventions, but other possibilities exist. The link name for
7199 @code{Var2} is @code{var2_link_name}, and this is not operating system
7200 dependent.
7201
7202 One exception occurs for library level procedures. A potential ambiguity
7203 arises between the required name @code{_main} for the C main program,
7204 and the name we would otherwise assign to an Ada library level procedure
7205 called @code{Main} (which might well not be the main program).
7206
7207 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
7208 names. So if we have a library level procedure such as:
7209
7210 @example
7211 procedure Hello (S : String);
7212 @end example
7213
7214 the external name of this procedure will be @code{_ada_hello}.
7215
7216 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
7217
7218 @node Building Executable Programs with GNAT,GNAT Utility Programs,The GNAT Compilation Model,Top
7219 @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}
7220 @chapter Building Executable Programs with GNAT
7221
7222
7223 This chapter describes first the gnatmake tool
7224 (@ref{1b,,Building with gnatmake}),
7225 which automatically determines the set of sources
7226 needed by an Ada compilation unit and executes the necessary
7227 (re)compilations, binding and linking.
7228 It also explains how to use each tool individually: the
7229 compiler (gcc, see @ref{1c,,Compiling with gcc}),
7230 binder (gnatbind, see @ref{1d,,Binding with gnatbind}),
7231 and linker (gnatlink, see @ref{1e,,Linking with gnatlink})
7232 to build executable programs.
7233 Finally, this chapter provides examples of
7234 how to make use of the general GNU make mechanism
7235 in a GNAT context (see @ref{1f,,Using the GNU make Utility}).
7236
7237
7238 @menu
7239 * Building with gnatmake::
7240 * Compiling with gcc::
7241 * Compiler Switches::
7242 * Linker Switches::
7243 * Binding with gnatbind::
7244 * Linking with gnatlink::
7245 * Using the GNU make Utility::
7246
7247 @end menu
7248
7249 @node Building with gnatmake,Compiling with gcc,,Building Executable Programs with GNAT
7250 @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}
7251 @section Building with @code{gnatmake}
7252
7253
7254 @geindex gnatmake
7255
7256 A typical development cycle when working on an Ada program consists of
7257 the following steps:
7258
7259
7260 @enumerate
7261
7262 @item
7263 Edit some sources to fix bugs;
7264
7265 @item
7266 Add enhancements;
7267
7268 @item
7269 Compile all sources affected;
7270
7271 @item
7272 Rebind and relink; and
7273
7274 @item
7275 Test.
7276 @end enumerate
7277
7278 @geindex Dependency rules (compilation)
7279
7280 The third step in particular can be tricky, because not only do the modified
7281 files have to be compiled, but any files depending on these files must also be
7282 recompiled. The dependency rules in Ada can be quite complex, especially
7283 in the presence of overloading, @code{use} clauses, generics and inlined
7284 subprograms.
7285
7286 @code{gnatmake} automatically takes care of the third and fourth steps
7287 of this process. It determines which sources need to be compiled,
7288 compiles them, and binds and links the resulting object files.
7289
7290 Unlike some other Ada make programs, the dependencies are always
7291 accurately recomputed from the new sources. The source based approach of
7292 the GNAT compilation model makes this possible. This means that if
7293 changes to the source program cause corresponding changes in
7294 dependencies, they will always be tracked exactly correctly by
7295 @code{gnatmake}.
7296
7297 Note that for advanced forms of project structure, we recommend creating
7298 a project file as explained in the @emph{GNAT_Project_Manager} chapter in the
7299 @emph{GPRbuild User's Guide}, and using the
7300 @code{gprbuild} tool which supports building with project files and works similarly
7301 to @code{gnatmake}.
7302
7303 @menu
7304 * Running gnatmake::
7305 * Switches for gnatmake::
7306 * Mode Switches for gnatmake::
7307 * Notes on the Command Line::
7308 * How gnatmake Works::
7309 * Examples of gnatmake Usage::
7310
7311 @end menu
7312
7313 @node Running gnatmake,Switches for gnatmake,,Building with gnatmake
7314 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatmake}@anchor{da}@anchor{gnat_ugn/building_executable_programs_with_gnat id2}@anchor{db}
7315 @subsection Running @code{gnatmake}
7316
7317
7318 The usual form of the @code{gnatmake} command is
7319
7320 @example
7321 $ gnatmake [<switches>] <file_name> [<file_names>] [<mode_switches>]
7322 @end example
7323
7324 The only required argument is one @code{file_name}, which specifies
7325 a compilation unit that is a main program. Several @code{file_names} can be
7326 specified: this will result in several executables being built.
7327 If @code{switches} are present, they can be placed before the first
7328 @code{file_name}, between @code{file_names} or after the last @code{file_name}.
7329 If @code{mode_switches} are present, they must always be placed after
7330 the last @code{file_name} and all @code{switches}.
7331
7332 If you are using standard file extensions (@code{.adb} and
7333 @code{.ads}), then the
7334 extension may be omitted from the @code{file_name} arguments. However, if
7335 you are using non-standard extensions, then it is required that the
7336 extension be given. A relative or absolute directory path can be
7337 specified in a @code{file_name}, in which case, the input source file will
7338 be searched for in the specified directory only. Otherwise, the input
7339 source file will first be searched in the directory where
7340 @code{gnatmake} was invoked and if it is not found, it will be search on
7341 the source path of the compiler as described in
7342 @ref{89,,Search Paths and the Run-Time Library (RTL)}.
7343
7344 All @code{gnatmake} output (except when you specify @code{-M}) is sent to
7345 @code{stderr}. The output produced by the
7346 @code{-M} switch is sent to @code{stdout}.
7347
7348 @node Switches for gnatmake,Mode Switches for gnatmake,Running gnatmake,Building with gnatmake
7349 @anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatmake}@anchor{dc}@anchor{gnat_ugn/building_executable_programs_with_gnat id3}@anchor{dd}
7350 @subsection Switches for @code{gnatmake}
7351
7352
7353 You may specify any of the following switches to @code{gnatmake}:
7354
7355 @geindex --version (gnatmake)
7356
7357
7358 @table @asis
7359
7360 @item @code{--version}
7361
7362 Display Copyright and version, then exit disregarding all other options.
7363 @end table
7364
7365 @geindex --help (gnatmake)
7366
7367
7368 @table @asis
7369
7370 @item @code{--help}
7371
7372 If @code{--version} was not used, display usage, then exit disregarding
7373 all other options.
7374 @end table
7375
7376 @geindex --GCC=compiler_name (gnatmake)
7377
7378
7379 @table @asis
7380
7381 @item @code{--GCC=@emph{compiler_name}}
7382
7383 Program used for compiling. The default is @code{gcc}. You need to use
7384 quotes around @code{compiler_name} if @code{compiler_name} contains
7385 spaces or other separator characters.
7386 As an example @code{--GCC="foo -x -y"}
7387 will instruct @code{gnatmake} to use @code{foo -x -y} as your
7388 compiler. A limitation of this syntax is that the name and path name of
7389 the executable itself must not include any embedded spaces. Note that
7390 switch @code{-c} is always inserted after your command name. Thus in the
7391 above example the compiler command that will be used by @code{gnatmake}
7392 will be @code{foo -c -x -y}. If several @code{--GCC=compiler_name} are
7393 used, only the last @code{compiler_name} is taken into account. However,
7394 all the additional switches are also taken into account. Thus,
7395 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7396 @code{--GCC="bar -x -y -z -t"}.
7397 @end table
7398
7399 @geindex --GNATBIND=binder_name (gnatmake)
7400
7401
7402 @table @asis
7403
7404 @item @code{--GNATBIND=@emph{binder_name}}
7405
7406 Program used for binding. The default is @code{gnatbind}. You need to
7407 use quotes around @code{binder_name} if @code{binder_name} contains spaces
7408 or other separator characters.
7409 As an example @code{--GNATBIND="bar -x -y"}
7410 will instruct @code{gnatmake} to use @code{bar -x -y} as your
7411 binder. Binder switches that are normally appended by @code{gnatmake}
7412 to @code{gnatbind} are now appended to the end of @code{bar -x -y}.
7413 A limitation of this syntax is that the name and path name of the executable
7414 itself must not include any embedded spaces.
7415 @end table
7416
7417 @geindex --GNATLINK=linker_name (gnatmake)
7418
7419
7420 @table @asis
7421
7422 @item @code{--GNATLINK=@emph{linker_name}}
7423
7424 Program used for linking. The default is @code{gnatlink}. You need to
7425 use quotes around @code{linker_name} if @code{linker_name} contains spaces
7426 or other separator characters.
7427 As an example @code{--GNATLINK="lan -x -y"}
7428 will instruct @code{gnatmake} to use @code{lan -x -y} as your
7429 linker. Linker switches that are normally appended by @code{gnatmake} to
7430 @code{gnatlink} are now appended to the end of @code{lan -x -y}.
7431 A limitation of this syntax is that the name and path name of the executable
7432 itself must not include any embedded spaces.
7433
7434 @item @code{--create-map-file}
7435
7436 When linking an executable, create a map file. The name of the map file
7437 has the same name as the executable with extension ".map".
7438
7439 @item @code{--create-map-file=@emph{mapfile}}
7440
7441 When linking an executable, create a map file with the specified name.
7442 @end table
7443
7444 @geindex --create-missing-dirs (gnatmake)
7445
7446
7447 @table @asis
7448
7449 @item @code{--create-missing-dirs}
7450
7451 When using project files (@code{-P@emph{project}}), automatically create
7452 missing object directories, library directories and exec
7453 directories.
7454
7455 @item @code{--single-compile-per-obj-dir}
7456
7457 Disallow simultaneous compilations in the same object directory when
7458 project files are used.
7459
7460 @item @code{--subdirs=@emph{subdir}}
7461
7462 Actual object directory of each project file is the subdirectory subdir of the
7463 object directory specified or defaulted in the project file.
7464
7465 @item @code{--unchecked-shared-lib-imports}
7466
7467 By default, shared library projects are not allowed to import static library
7468 projects. When this switch is used on the command line, this restriction is
7469 relaxed.
7470
7471 @item @code{--source-info=@emph{source info file}}
7472
7473 Specify a source info file. This switch is active only when project files
7474 are used. If the source info file is specified as a relative path, then it is
7475 relative to the object directory of the main project. If the source info file
7476 does not exist, then after the Project Manager has successfully parsed and
7477 processed the project files and found the sources, it creates the source info
7478 file. If the source info file already exists and can be read successfully,
7479 then the Project Manager will get all the needed information about the sources
7480 from the source info file and will not look for them. This reduces the time
7481 to process the project files, especially when looking for sources that take a
7482 long time. If the source info file exists but cannot be parsed successfully,
7483 the Project Manager will attempt to recreate it. If the Project Manager fails
7484 to create the source info file, a message is issued, but gnatmake does not
7485 fail. @code{gnatmake} "trusts" the source info file. This means that
7486 if the source files have changed (addition, deletion, moving to a different
7487 source directory), then the source info file need to be deleted and recreated.
7488 @end table
7489
7490 @geindex -a (gnatmake)
7491
7492
7493 @table @asis
7494
7495 @item @code{-a}
7496
7497 Consider all files in the make process, even the GNAT internal system
7498 files (for example, the predefined Ada library files), as well as any
7499 locked files. Locked files are files whose ALI file is write-protected.
7500 By default,
7501 @code{gnatmake} does not check these files,
7502 because the assumption is that the GNAT internal files are properly up
7503 to date, and also that any write protected ALI files have been properly
7504 installed. Note that if there is an installation problem, such that one
7505 of these files is not up to date, it will be properly caught by the
7506 binder.
7507 You may have to specify this switch if you are working on GNAT
7508 itself. The switch @code{-a} is also useful
7509 in conjunction with @code{-f}
7510 if you need to recompile an entire application,
7511 including run-time files, using special configuration pragmas,
7512 such as a @code{Normalize_Scalars} pragma.
7513
7514 By default
7515 @code{gnatmake -a} compiles all GNAT
7516 internal files with
7517 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
7518 @end table
7519
7520 @geindex -b (gnatmake)
7521
7522
7523 @table @asis
7524
7525 @item @code{-b}
7526
7527 Bind only. Can be combined with @code{-c} to do
7528 compilation and binding, but no link.
7529 Can be combined with @code{-l}
7530 to do binding and linking. When not combined with
7531 @code{-c}
7532 all the units in the closure of the main program must have been previously
7533 compiled and must be up to date. The root unit specified by @code{file_name}
7534 may be given without extension, with the source extension or, if no GNAT
7535 Project File is specified, with the ALI file extension.
7536 @end table
7537
7538 @geindex -c (gnatmake)
7539
7540
7541 @table @asis
7542
7543 @item @code{-c}
7544
7545 Compile only. Do not perform binding, except when @code{-b}
7546 is also specified. Do not perform linking, except if both
7547 @code{-b} and
7548 @code{-l} are also specified.
7549 If the root unit specified by @code{file_name} is not a main unit, this is the
7550 default. Otherwise @code{gnatmake} will attempt binding and linking
7551 unless all objects are up to date and the executable is more recent than
7552 the objects.
7553 @end table
7554
7555 @geindex -C (gnatmake)
7556
7557
7558 @table @asis
7559
7560 @item @code{-C}
7561
7562 Use a temporary mapping file. A mapping file is a way to communicate
7563 to the compiler two mappings: from unit names to file names (without
7564 any directory information) and from file names to path names (with
7565 full directory information). A mapping file can make the compiler's
7566 file searches faster, especially if there are many source directories,
7567 or the sources are read over a slow network connection. If
7568 @code{-P} is used, a mapping file is always used, so
7569 @code{-C} is unnecessary; in this case the mapping file
7570 is initially populated based on the project file. If
7571 @code{-C} is used without
7572 @code{-P},
7573 the mapping file is initially empty. Each invocation of the compiler
7574 will add any newly accessed sources to the mapping file.
7575 @end table
7576
7577 @geindex -C= (gnatmake)
7578
7579
7580 @table @asis
7581
7582 @item @code{-C=@emph{file}}
7583
7584 Use a specific mapping file. The file, specified as a path name (absolute or
7585 relative) by this switch, should already exist, otherwise the switch is
7586 ineffective. The specified mapping file will be communicated to the compiler.
7587 This switch is not compatible with a project file
7588 (-P`file`) or with multiple compiling processes
7589 (-jnnn, when nnn is greater than 1).
7590 @end table
7591
7592 @geindex -d (gnatmake)
7593
7594
7595 @table @asis
7596
7597 @item @code{-d}
7598
7599 Display progress for each source, up to date or not, as a single line:
7600
7601 @example
7602 completed x out of y (zz%)
7603 @end example
7604
7605 If the file needs to be compiled this is displayed after the invocation of
7606 the compiler. These lines are displayed even in quiet output mode.
7607 @end table
7608
7609 @geindex -D (gnatmake)
7610
7611
7612 @table @asis
7613
7614 @item @code{-D @emph{dir}}
7615
7616 Put all object files and ALI file in directory @code{dir}.
7617 If the @code{-D} switch is not used, all object files
7618 and ALI files go in the current working directory.
7619
7620 This switch cannot be used when using a project file.
7621 @end table
7622
7623 @geindex -eI (gnatmake)
7624
7625
7626 @table @asis
7627
7628 @item @code{-eI@emph{nnn}}
7629
7630 Indicates that the main source is a multi-unit source and the rank of the unit
7631 in the source file is nnn. nnn needs to be a positive number and a valid
7632 index in the source. This switch cannot be used when @code{gnatmake} is
7633 invoked for several mains.
7634 @end table
7635
7636 @geindex -eL (gnatmake)
7637
7638 @geindex symbolic links
7639
7640
7641 @table @asis
7642
7643 @item @code{-eL}
7644
7645 Follow all symbolic links when processing project files.
7646 This should be used if your project uses symbolic links for files or
7647 directories, but is not needed in other cases.
7648
7649 @geindex naming scheme
7650
7651 This also assumes that no directory matches the naming scheme for files (for
7652 instance that you do not have a directory called "sources.ads" when using the
7653 default GNAT naming scheme).
7654
7655 When you do not have to use this switch (i.e., by default), gnatmake is able to
7656 save a lot of system calls (several per source file and object file), which
7657 can result in a significant speed up to load and manipulate a project file,
7658 especially when using source files from a remote system.
7659 @end table
7660
7661 @geindex -eS (gnatmake)
7662
7663
7664 @table @asis
7665
7666 @item @code{-eS}
7667
7668 Output the commands for the compiler, the binder and the linker
7669 on standard output,
7670 instead of standard error.
7671 @end table
7672
7673 @geindex -f (gnatmake)
7674
7675
7676 @table @asis
7677
7678 @item @code{-f}
7679
7680 Force recompilations. Recompile all sources, even though some object
7681 files may be up to date, but don't recompile predefined or GNAT internal
7682 files or locked files (files with a write-protected ALI file),
7683 unless the @code{-a} switch is also specified.
7684 @end table
7685
7686 @geindex -F (gnatmake)
7687
7688
7689 @table @asis
7690
7691 @item @code{-F}
7692
7693 When using project files, if some errors or warnings are detected during
7694 parsing and verbose mode is not in effect (no use of switch
7695 -v), then error lines start with the full path name of the project
7696 file, rather than its simple file name.
7697 @end table
7698
7699 @geindex -g (gnatmake)
7700
7701
7702 @table @asis
7703
7704 @item @code{-g}
7705
7706 Enable debugging. This switch is simply passed to the compiler and to the
7707 linker.
7708 @end table
7709
7710 @geindex -i (gnatmake)
7711
7712
7713 @table @asis
7714
7715 @item @code{-i}
7716
7717 In normal mode, @code{gnatmake} compiles all object files and ALI files
7718 into the current directory. If the @code{-i} switch is used,
7719 then instead object files and ALI files that already exist are overwritten
7720 in place. This means that once a large project is organized into separate
7721 directories in the desired manner, then @code{gnatmake} will automatically
7722 maintain and update this organization. If no ALI files are found on the
7723 Ada object path (see @ref{89,,Search Paths and the Run-Time Library (RTL)}),
7724 the new object and ALI files are created in the
7725 directory containing the source being compiled. If another organization
7726 is desired, where objects and sources are kept in different directories,
7727 a useful technique is to create dummy ALI files in the desired directories.
7728 When detecting such a dummy file, @code{gnatmake} will be forced to
7729 recompile the corresponding source file, and it will be put the resulting
7730 object and ALI files in the directory where it found the dummy file.
7731 @end table
7732
7733 @geindex -j (gnatmake)
7734
7735 @geindex Parallel make
7736
7737
7738 @table @asis
7739
7740 @item @code{-j@emph{n}}
7741
7742 Use @code{n} processes to carry out the (re)compilations. On a multiprocessor
7743 machine compilations will occur in parallel. If @code{n} is 0, then the
7744 maximum number of parallel compilations is the number of core processors
7745 on the platform. In the event of compilation errors, messages from various
7746 compilations might get interspersed (but @code{gnatmake} will give you the
7747 full ordered list of failing compiles at the end). If this is problematic,
7748 rerun the make process with n set to 1 to get a clean list of messages.
7749 @end table
7750
7751 @geindex -k (gnatmake)
7752
7753
7754 @table @asis
7755
7756 @item @code{-k}
7757
7758 Keep going. Continue as much as possible after a compilation error. To
7759 ease the programmer's task in case of compilation errors, the list of
7760 sources for which the compile fails is given when @code{gnatmake}
7761 terminates.
7762
7763 If @code{gnatmake} is invoked with several @code{file_names} and with this
7764 switch, if there are compilation errors when building an executable,
7765 @code{gnatmake} will not attempt to build the following executables.
7766 @end table
7767
7768 @geindex -l (gnatmake)
7769
7770
7771 @table @asis
7772
7773 @item @code{-l}
7774
7775 Link only. Can be combined with @code{-b} to binding
7776 and linking. Linking will not be performed if combined with
7777 @code{-c}
7778 but not with @code{-b}.
7779 When not combined with @code{-b}
7780 all the units in the closure of the main program must have been previously
7781 compiled and must be up to date, and the main program needs to have been bound.
7782 The root unit specified by @code{file_name}
7783 may be given without extension, with the source extension or, if no GNAT
7784 Project File is specified, with the ALI file extension.
7785 @end table
7786
7787 @geindex -m (gnatmake)
7788
7789
7790 @table @asis
7791
7792 @item @code{-m}
7793
7794 Specify that the minimum necessary amount of recompilations
7795 be performed. In this mode @code{gnatmake} ignores time
7796 stamp differences when the only
7797 modifications to a source file consist in adding/removing comments,
7798 empty lines, spaces or tabs. This means that if you have changed the
7799 comments in a source file or have simply reformatted it, using this
7800 switch will tell @code{gnatmake} not to recompile files that depend on it
7801 (provided other sources on which these files depend have undergone no
7802 semantic modifications). Note that the debugging information may be
7803 out of date with respect to the sources if the @code{-m} switch causes
7804 a compilation to be switched, so the use of this switch represents a
7805 trade-off between compilation time and accurate debugging information.
7806 @end table
7807
7808 @geindex Dependencies
7809 @geindex producing list
7810
7811 @geindex -M (gnatmake)
7812
7813
7814 @table @asis
7815
7816 @item @code{-M}
7817
7818 Check if all objects are up to date. If they are, output the object
7819 dependences to @code{stdout} in a form that can be directly exploited in
7820 a @code{Makefile}. By default, each source file is prefixed with its
7821 (relative or absolute) directory name. This name is whatever you
7822 specified in the various @code{-aI}
7823 and @code{-I} switches. If you use
7824 @code{gnatmake -M} @code{-q}
7825 (see below), only the source file names,
7826 without relative paths, are output. If you just specify the @code{-M}
7827 switch, dependencies of the GNAT internal system files are omitted. This
7828 is typically what you want. If you also specify
7829 the @code{-a} switch,
7830 dependencies of the GNAT internal files are also listed. Note that
7831 dependencies of the objects in external Ada libraries (see
7832 switch @code{-aL@emph{dir}} in the following list)
7833 are never reported.
7834 @end table
7835
7836 @geindex -n (gnatmake)
7837
7838
7839 @table @asis
7840
7841 @item @code{-n}
7842
7843 Don't compile, bind, or link. Checks if all objects are up to date.
7844 If they are not, the full name of the first file that needs to be
7845 recompiled is printed.
7846 Repeated use of this option, followed by compiling the indicated source
7847 file, will eventually result in recompiling all required units.
7848 @end table
7849
7850 @geindex -o (gnatmake)
7851
7852
7853 @table @asis
7854
7855 @item @code{-o @emph{exec_name}}
7856
7857 Output executable name. The name of the final executable program will be
7858 @code{exec_name}. If the @code{-o} switch is omitted the default
7859 name for the executable will be the name of the input file in appropriate form
7860 for an executable file on the host system.
7861
7862 This switch cannot be used when invoking @code{gnatmake} with several
7863 @code{file_names}.
7864 @end table
7865
7866 @geindex -p (gnatmake)
7867
7868
7869 @table @asis
7870
7871 @item @code{-p}
7872
7873 Same as @code{--create-missing-dirs}
7874 @end table
7875
7876 @geindex -P (gnatmake)
7877
7878
7879 @table @asis
7880
7881 @item @code{-P@emph{project}}
7882
7883 Use project file @code{project}. Only one such switch can be used.
7884 @end table
7885
7886 @c -- Comment:
7887 @c :ref:`gnatmake_and_Project_Files`.
7888
7889 @geindex -q (gnatmake)
7890
7891
7892 @table @asis
7893
7894 @item @code{-q}
7895
7896 Quiet. When this flag is not set, the commands carried out by
7897 @code{gnatmake} are displayed.
7898 @end table
7899
7900 @geindex -s (gnatmake)
7901
7902
7903 @table @asis
7904
7905 @item @code{-s}
7906
7907 Recompile if compiler switches have changed since last compilation.
7908 All compiler switches but -I and -o are taken into account in the
7909 following way:
7910 orders between different 'first letter' switches are ignored, but
7911 orders between same switches are taken into account. For example,
7912 @code{-O -O2} is different than @code{-O2 -O}, but @code{-g -O}
7913 is equivalent to @code{-O -g}.
7914
7915 This switch is recommended when Integrated Preprocessing is used.
7916 @end table
7917
7918 @geindex -u (gnatmake)
7919
7920
7921 @table @asis
7922
7923 @item @code{-u}
7924
7925 Unique. Recompile at most the main files. It implies -c. Combined with
7926 -f, it is equivalent to calling the compiler directly. Note that using
7927 -u with a project file and no main has a special meaning.
7928 @end table
7929
7930 @c --Comment
7931 @c (See :ref:`Project_Files_and_Main_Subprograms`.)
7932
7933 @geindex -U (gnatmake)
7934
7935
7936 @table @asis
7937
7938 @item @code{-U}
7939
7940 When used without a project file or with one or several mains on the command
7941 line, is equivalent to -u. When used with a project file and no main
7942 on the command line, all sources of all project files are checked and compiled
7943 if not up to date, and libraries are rebuilt, if necessary.
7944 @end table
7945
7946 @geindex -v (gnatmake)
7947
7948
7949 @table @asis
7950
7951 @item @code{-v}
7952
7953 Verbose. Display the reason for all recompilations @code{gnatmake}
7954 decides are necessary, with the highest verbosity level.
7955 @end table
7956
7957 @geindex -vl (gnatmake)
7958
7959
7960 @table @asis
7961
7962 @item @code{-vl}
7963
7964 Verbosity level Low. Display fewer lines than in verbosity Medium.
7965 @end table
7966
7967 @geindex -vm (gnatmake)
7968
7969
7970 @table @asis
7971
7972 @item @code{-vm}
7973
7974 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
7975 @end table
7976
7977 @geindex -vm (gnatmake)
7978
7979
7980 @table @asis
7981
7982 @item @code{-vh}
7983
7984 Verbosity level High. Equivalent to -v.
7985
7986 @item @code{-vP@emph{x}}
7987
7988 Indicate the verbosity of the parsing of GNAT project files.
7989 See @ref{de,,Switches Related to Project Files}.
7990 @end table
7991
7992 @geindex -x (gnatmake)
7993
7994
7995 @table @asis
7996
7997 @item @code{-x}
7998
7999 Indicate that sources that are not part of any Project File may be compiled.
8000 Normally, when using Project Files, only sources that are part of a Project
8001 File may be compile. When this switch is used, a source outside of all Project
8002 Files may be compiled. The ALI file and the object file will be put in the
8003 object directory of the main Project. The compilation switches used will only
8004 be those specified on the command line. Even when
8005 @code{-x} is used, mains specified on the
8006 command line need to be sources of a project file.
8007
8008 @item @code{-X@emph{name}=@emph{value}}
8009
8010 Indicate that external variable @code{name} has the value @code{value}.
8011 The Project Manager will use this value for occurrences of
8012 @code{external(name)} when parsing the project file.
8013 @ref{de,,Switches Related to Project Files}.
8014 @end table
8015
8016 @geindex -z (gnatmake)
8017
8018
8019 @table @asis
8020
8021 @item @code{-z}
8022
8023 No main subprogram. Bind and link the program even if the unit name
8024 given on the command line is a package name. The resulting executable
8025 will execute the elaboration routines of the package and its closure,
8026 then the finalization routines.
8027 @end table
8028
8029 @subsubheading GCC switches
8030
8031
8032 Any uppercase or multi-character switch that is not a @code{gnatmake} switch
8033 is passed to @code{gcc} (e.g., @code{-O}, @code{-gnato,} etc.)
8034
8035 @subsubheading Source and library search path switches
8036
8037
8038 @geindex -aI (gnatmake)
8039
8040
8041 @table @asis
8042
8043 @item @code{-aI@emph{dir}}
8044
8045 When looking for source files also look in directory @code{dir}.
8046 The order in which source files search is undertaken is
8047 described in @ref{89,,Search Paths and the Run-Time Library (RTL)}.
8048 @end table
8049
8050 @geindex -aL (gnatmake)
8051
8052
8053 @table @asis
8054
8055 @item @code{-aL@emph{dir}}
8056
8057 Consider @code{dir} as being an externally provided Ada library.
8058 Instructs @code{gnatmake} to skip compilation units whose @code{.ALI}
8059 files have been located in directory @code{dir}. This allows you to have
8060 missing bodies for the units in @code{dir} and to ignore out of date bodies
8061 for the same units. You still need to specify
8062 the location of the specs for these units by using the switches
8063 @code{-aI@emph{dir}} or @code{-I@emph{dir}}.
8064 Note: this switch is provided for compatibility with previous versions
8065 of @code{gnatmake}. The easier method of causing standard libraries
8066 to be excluded from consideration is to write-protect the corresponding
8067 ALI files.
8068 @end table
8069
8070 @geindex -aO (gnatmake)
8071
8072
8073 @table @asis
8074
8075 @item @code{-aO@emph{dir}}
8076
8077 When searching for library and object files, look in directory
8078 @code{dir}. The order in which library files are searched is described in
8079 @ref{8c,,Search Paths for gnatbind}.
8080 @end table
8081
8082 @geindex Search paths
8083 @geindex for gnatmake
8084
8085 @geindex -A (gnatmake)
8086
8087
8088 @table @asis
8089
8090 @item @code{-A@emph{dir}}
8091
8092 Equivalent to @code{-aL@emph{dir}} @code{-aI@emph{dir}}.
8093
8094 @geindex -I (gnatmake)
8095
8096 @item @code{-I@emph{dir}}
8097
8098 Equivalent to @code{-aO@emph{dir} -aI@emph{dir}}.
8099 @end table
8100
8101 @geindex -I- (gnatmake)
8102
8103 @geindex Source files
8104 @geindex suppressing search
8105
8106
8107 @table @asis
8108
8109 @item @code{-I-}
8110
8111 Do not look for source files in the directory containing the source
8112 file named in the command line.
8113 Do not look for ALI or object files in the directory
8114 where @code{gnatmake} was invoked.
8115 @end table
8116
8117 @geindex -L (gnatmake)
8118
8119 @geindex Linker libraries
8120
8121
8122 @table @asis
8123
8124 @item @code{-L@emph{dir}}
8125
8126 Add directory @code{dir} to the list of directories in which the linker
8127 will search for libraries. This is equivalent to
8128 @code{-largs} @code{-L@emph{dir}}.
8129 Furthermore, under Windows, the sources pointed to by the libraries path
8130 set in the registry are not searched for.
8131 @end table
8132
8133 @geindex -nostdinc (gnatmake)
8134
8135
8136 @table @asis
8137
8138 @item @code{-nostdinc}
8139
8140 Do not look for source files in the system default directory.
8141 @end table
8142
8143 @geindex -nostdlib (gnatmake)
8144
8145
8146 @table @asis
8147
8148 @item @code{-nostdlib}
8149
8150 Do not look for library files in the system default directory.
8151 @end table
8152
8153 @geindex --RTS (gnatmake)
8154
8155
8156 @table @asis
8157
8158 @item @code{--RTS=@emph{rts-path}}
8159
8160 Specifies the default location of the run-time library. GNAT looks for the
8161 run-time
8162 in the following directories, and stops as soon as a valid run-time is found
8163 (@code{adainclude} or @code{ada_source_path}, and @code{adalib} or
8164 @code{ada_object_path} present):
8165
8166
8167 @itemize *
8168
8169 @item
8170 @emph{<current directory>/$rts_path}
8171
8172 @item
8173 @emph{<default-search-dir>/$rts_path}
8174
8175 @item
8176 @emph{<default-search-dir>/rts-$rts_path}
8177
8178 @item
8179 The selected path is handled like a normal RTS path.
8180 @end itemize
8181 @end table
8182
8183 @node Mode Switches for gnatmake,Notes on the Command Line,Switches for gnatmake,Building with gnatmake
8184 @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}
8185 @subsection Mode Switches for @code{gnatmake}
8186
8187
8188 The mode switches (referred to as @code{mode_switches}) allow the
8189 inclusion of switches that are to be passed to the compiler itself, the
8190 binder or the linker. The effect of a mode switch is to cause all
8191 subsequent switches up to the end of the switch list, or up to the next
8192 mode switch, to be interpreted as switches to be passed on to the
8193 designated component of GNAT.
8194
8195 @geindex -cargs (gnatmake)
8196
8197
8198 @table @asis
8199
8200 @item @code{-cargs @emph{switches}}
8201
8202 Compiler switches. Here @code{switches} is a list of switches
8203 that are valid switches for @code{gcc}. They will be passed on to
8204 all compile steps performed by @code{gnatmake}.
8205 @end table
8206
8207 @geindex -bargs (gnatmake)
8208
8209
8210 @table @asis
8211
8212 @item @code{-bargs @emph{switches}}
8213
8214 Binder switches. Here @code{switches} is a list of switches
8215 that are valid switches for @code{gnatbind}. They will be passed on to
8216 all bind steps performed by @code{gnatmake}.
8217 @end table
8218
8219 @geindex -largs (gnatmake)
8220
8221
8222 @table @asis
8223
8224 @item @code{-largs @emph{switches}}
8225
8226 Linker switches. Here @code{switches} is a list of switches
8227 that are valid switches for @code{gnatlink}. They will be passed on to
8228 all link steps performed by @code{gnatmake}.
8229 @end table
8230
8231 @geindex -margs (gnatmake)
8232
8233
8234 @table @asis
8235
8236 @item @code{-margs @emph{switches}}
8237
8238 Make switches. The switches are directly interpreted by @code{gnatmake},
8239 regardless of any previous occurrence of @code{-cargs}, @code{-bargs}
8240 or @code{-largs}.
8241 @end table
8242
8243 @node Notes on the Command Line,How gnatmake Works,Mode Switches for gnatmake,Building with gnatmake
8244 @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}
8245 @subsection Notes on the Command Line
8246
8247
8248 This section contains some additional useful notes on the operation
8249 of the @code{gnatmake} command.
8250
8251 @geindex Recompilation (by gnatmake)
8252
8253
8254 @itemize *
8255
8256 @item
8257 If @code{gnatmake} finds no ALI files, it recompiles the main program
8258 and all other units required by the main program.
8259 This means that @code{gnatmake}
8260 can be used for the initial compile, as well as during subsequent steps of
8261 the development cycle.
8262
8263 @item
8264 If you enter @code{gnatmake foo.adb}, where @code{foo}
8265 is a subunit or body of a generic unit, @code{gnatmake} recompiles
8266 @code{foo.adb} (because it finds no ALI) and stops, issuing a
8267 warning.
8268
8269 @item
8270 In @code{gnatmake} the switch @code{-I}
8271 is used to specify both source and
8272 library file paths. Use @code{-aI}
8273 instead if you just want to specify
8274 source paths only and @code{-aO}
8275 if you want to specify library paths
8276 only.
8277
8278 @item
8279 @code{gnatmake} will ignore any files whose ALI file is write-protected.
8280 This may conveniently be used to exclude standard libraries from
8281 consideration and in particular it means that the use of the
8282 @code{-f} switch will not recompile these files
8283 unless @code{-a} is also specified.
8284
8285 @item
8286 @code{gnatmake} has been designed to make the use of Ada libraries
8287 particularly convenient. Assume you have an Ada library organized
8288 as follows: @emph{obj-dir} contains the objects and ALI files for
8289 of your Ada compilation units,
8290 whereas @emph{include-dir} contains the
8291 specs of these units, but no bodies. Then to compile a unit
8292 stored in @code{main.adb}, which uses this Ada library you would just type:
8293
8294 @example
8295 $ gnatmake -aI`include-dir` -aL`obj-dir` main
8296 @end example
8297
8298 @item
8299 Using @code{gnatmake} along with the @code{-m (minimal recompilation)}
8300 switch provides a mechanism for avoiding unnecessary recompilations. Using
8301 this switch,
8302 you can update the comments/format of your
8303 source files without having to recompile everything. Note, however, that
8304 adding or deleting lines in a source files may render its debugging
8305 info obsolete. If the file in question is a spec, the impact is rather
8306 limited, as that debugging info will only be useful during the
8307 elaboration phase of your program. For bodies the impact can be more
8308 significant. In all events, your debugger will warn you if a source file
8309 is more recent than the corresponding object, and alert you to the fact
8310 that the debugging information may be out of date.
8311 @end itemize
8312
8313 @node How gnatmake Works,Examples of gnatmake Usage,Notes on the Command Line,Building with gnatmake
8314 @anchor{gnat_ugn/building_executable_programs_with_gnat id6}@anchor{e3}@anchor{gnat_ugn/building_executable_programs_with_gnat how-gnatmake-works}@anchor{e4}
8315 @subsection How @code{gnatmake} Works
8316
8317
8318 Generally @code{gnatmake} automatically performs all necessary
8319 recompilations and you don't need to worry about how it works. However,
8320 it may be useful to have some basic understanding of the @code{gnatmake}
8321 approach and in particular to understand how it uses the results of
8322 previous compilations without incorrectly depending on them.
8323
8324 First a definition: an object file is considered @emph{up to date} if the
8325 corresponding ALI file exists and if all the source files listed in the
8326 dependency section of this ALI file have time stamps matching those in
8327 the ALI file. This means that neither the source file itself nor any
8328 files that it depends on have been modified, and hence there is no need
8329 to recompile this file.
8330
8331 @code{gnatmake} works by first checking if the specified main unit is up
8332 to date. If so, no compilations are required for the main unit. If not,
8333 @code{gnatmake} compiles the main program to build a new ALI file that
8334 reflects the latest sources. Then the ALI file of the main unit is
8335 examined to find all the source files on which the main program depends,
8336 and @code{gnatmake} recursively applies the above procedure on all these
8337 files.
8338
8339 This process ensures that @code{gnatmake} only trusts the dependencies
8340 in an existing ALI file if they are known to be correct. Otherwise it
8341 always recompiles to determine a new, guaranteed accurate set of
8342 dependencies. As a result the program is compiled 'upside down' from what may
8343 be more familiar as the required order of compilation in some other Ada
8344 systems. In particular, clients are compiled before the units on which
8345 they depend. The ability of GNAT to compile in any order is critical in
8346 allowing an order of compilation to be chosen that guarantees that
8347 @code{gnatmake} will recompute a correct set of new dependencies if
8348 necessary.
8349
8350 When invoking @code{gnatmake} with several @code{file_names}, if a unit is
8351 imported by several of the executables, it will be recompiled at most once.
8352
8353 Note: when using non-standard naming conventions
8354 (@ref{35,,Using Other File Names}), changing through a configuration pragmas
8355 file the version of a source and invoking @code{gnatmake} to recompile may
8356 have no effect, if the previous version of the source is still accessible
8357 by @code{gnatmake}. It may be necessary to use the switch
8358 -f.
8359
8360 @node Examples of gnatmake Usage,,How gnatmake Works,Building with gnatmake
8361 @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}
8362 @subsection Examples of @code{gnatmake} Usage
8363
8364
8365
8366 @table @asis
8367
8368 @item @emph{gnatmake hello.adb}
8369
8370 Compile all files necessary to bind and link the main program
8371 @code{hello.adb} (containing unit @code{Hello}) and bind and link the
8372 resulting object files to generate an executable file @code{hello}.
8373
8374 @item @emph{gnatmake main1 main2 main3}
8375
8376 Compile all files necessary to bind and link the main programs
8377 @code{main1.adb} (containing unit @code{Main1}), @code{main2.adb}
8378 (containing unit @code{Main2}) and @code{main3.adb}
8379 (containing unit @code{Main3}) and bind and link the resulting object files
8380 to generate three executable files @code{main1},
8381 @code{main2} and @code{main3}.
8382
8383 @item @emph{gnatmake -q Main_Unit -cargs -O2 -bargs -l}
8384
8385 Compile all files necessary to bind and link the main program unit
8386 @code{Main_Unit} (from file @code{main_unit.adb}). All compilations will
8387 be done with optimization level 2 and the order of elaboration will be
8388 listed by the binder. @code{gnatmake} will operate in quiet mode, not
8389 displaying commands it is executing.
8390 @end table
8391
8392 @node Compiling with gcc,Compiler Switches,Building with gnatmake,Building Executable Programs with GNAT
8393 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-with-gcc}@anchor{1c}@anchor{gnat_ugn/building_executable_programs_with_gnat id8}@anchor{e7}
8394 @section Compiling with @code{gcc}
8395
8396
8397 This section discusses how to compile Ada programs using the @code{gcc}
8398 command. It also describes the set of switches
8399 that can be used to control the behavior of the compiler.
8400
8401 @menu
8402 * Compiling Programs::
8403 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
8404 * Order of Compilation Issues::
8405 * Examples::
8406
8407 @end menu
8408
8409 @node Compiling Programs,Search Paths and the Run-Time Library RTL,,Compiling with gcc
8410 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-programs}@anchor{e8}@anchor{gnat_ugn/building_executable_programs_with_gnat id9}@anchor{e9}
8411 @subsection Compiling Programs
8412
8413
8414 The first step in creating an executable program is to compile the units
8415 of the program using the @code{gcc} command. You must compile the
8416 following files:
8417
8418
8419 @itemize *
8420
8421 @item
8422 the body file (@code{.adb}) for a library level subprogram or generic
8423 subprogram
8424
8425 @item
8426 the spec file (@code{.ads}) for a library level package or generic
8427 package that has no body
8428
8429 @item
8430 the body file (@code{.adb}) for a library level package
8431 or generic package that has a body
8432 @end itemize
8433
8434 You need @emph{not} compile the following files
8435
8436
8437 @itemize *
8438
8439 @item
8440 the spec of a library unit which has a body
8441
8442 @item
8443 subunits
8444 @end itemize
8445
8446 because they are compiled as part of compiling related units. GNAT
8447 package specs
8448 when the corresponding body is compiled, and subunits when the parent is
8449 compiled.
8450
8451 @geindex cannot generate code
8452
8453 If you attempt to compile any of these files, you will get one of the
8454 following error messages (where @code{fff} is the name of the file you
8455 compiled):
8456
8457 @quotation
8458
8459 @example
8460 cannot generate code for file `@w{`}fff`@w{`} (package spec)
8461 to check package spec, use -gnatc
8462
8463 cannot generate code for file `@w{`}fff`@w{`} (missing subunits)
8464 to check parent unit, use -gnatc
8465
8466 cannot generate code for file `@w{`}fff`@w{`} (subprogram spec)
8467 to check subprogram spec, use -gnatc
8468
8469 cannot generate code for file `@w{`}fff`@w{`} (subunit)
8470 to check subunit, use -gnatc
8471 @end example
8472 @end quotation
8473
8474 As indicated by the above error messages, if you want to submit
8475 one of these files to the compiler to check for correct semantics
8476 without generating code, then use the @code{-gnatc} switch.
8477
8478 The basic command for compiling a file containing an Ada unit is:
8479
8480 @example
8481 $ gcc -c [switches] <file name>
8482 @end example
8483
8484 where @code{file name} is the name of the Ada file (usually
8485 having an extension @code{.ads} for a spec or @code{.adb} for a body).
8486 You specify the
8487 @code{-c} switch to tell @code{gcc} to compile, but not link, the file.
8488 The result of a successful compilation is an object file, which has the
8489 same name as the source file but an extension of @code{.o} and an Ada
8490 Library Information (ALI) file, which also has the same name as the
8491 source file, but with @code{.ali} as the extension. GNAT creates these
8492 two output files in the current directory, but you may specify a source
8493 file in any directory using an absolute or relative path specification
8494 containing the directory information.
8495
8496 TESTING: the @code{--foobar@emph{NN}} switch
8497
8498 @geindex gnat1
8499
8500 @code{gcc} is actually a driver program that looks at the extensions of
8501 the file arguments and loads the appropriate compiler. For example, the
8502 GNU C compiler is @code{cc1}, and the Ada compiler is @code{gnat1}.
8503 These programs are in directories known to the driver program (in some
8504 configurations via environment variables you set), but need not be in
8505 your path. The @code{gcc} driver also calls the assembler and any other
8506 utilities needed to complete the generation of the required object
8507 files.
8508
8509 It is possible to supply several file names on the same @code{gcc}
8510 command. This causes @code{gcc} to call the appropriate compiler for
8511 each file. For example, the following command lists two separate
8512 files to be compiled:
8513
8514 @example
8515 $ gcc -c x.adb y.adb
8516 @end example
8517
8518 calls @code{gnat1} (the Ada compiler) twice to compile @code{x.adb} and
8519 @code{y.adb}.
8520 The compiler generates two object files @code{x.o} and @code{y.o}
8521 and the two ALI files @code{x.ali} and @code{y.ali}.
8522
8523 Any switches apply to all the files listed, see @ref{ea,,Compiler Switches} for a
8524 list of available @code{gcc} switches.
8525
8526 @node Search Paths and the Run-Time Library RTL,Order of Compilation Issues,Compiling Programs,Compiling with gcc
8527 @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}
8528 @subsection Search Paths and the Run-Time Library (RTL)
8529
8530
8531 With the GNAT source-based library system, the compiler must be able to
8532 find source files for units that are needed by the unit being compiled.
8533 Search paths are used to guide this process.
8534
8535 The compiler compiles one source file whose name must be given
8536 explicitly on the command line. In other words, no searching is done
8537 for this file. To find all other source files that are needed (the most
8538 common being the specs of units), the compiler examines the following
8539 directories, in the following order:
8540
8541
8542 @itemize *
8543
8544 @item
8545 The directory containing the source file of the main unit being compiled
8546 (the file name on the command line).
8547
8548 @item
8549 Each directory named by an @code{-I} switch given on the @code{gcc}
8550 command line, in the order given.
8551
8552 @geindex ADA_PRJ_INCLUDE_FILE
8553
8554 @item
8555 Each of the directories listed in the text file whose name is given
8556 by the
8557 @geindex ADA_PRJ_INCLUDE_FILE
8558 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8559 @code{ADA_PRJ_INCLUDE_FILE} environment variable.
8560 @geindex ADA_PRJ_INCLUDE_FILE
8561 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8562 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the gnat
8563 driver when project files are used. It should not normally be set
8564 by other means.
8565
8566 @geindex ADA_INCLUDE_PATH
8567
8568 @item
8569 Each of the directories listed in the value of the
8570 @geindex ADA_INCLUDE_PATH
8571 @geindex environment variable; ADA_INCLUDE_PATH
8572 @code{ADA_INCLUDE_PATH} environment variable.
8573 Construct this value
8574 exactly as the
8575 @geindex PATH
8576 @geindex environment variable; PATH
8577 @code{PATH} environment variable: a list of directory
8578 names separated by colons (semicolons when working with the NT version).
8579
8580 @item
8581 The content of the @code{ada_source_path} file which is part of the GNAT
8582 installation tree and is used to store standard libraries such as the
8583 GNAT Run Time Library (RTL) source files.
8584 @ref{87,,Installing a library}
8585 @end itemize
8586
8587 Specifying the switch @code{-I-}
8588 inhibits the use of the directory
8589 containing the source file named in the command line. You can still
8590 have this directory on your search path, but in this case it must be
8591 explicitly requested with a @code{-I} switch.
8592
8593 Specifying the switch @code{-nostdinc}
8594 inhibits the search of the default location for the GNAT Run Time
8595 Library (RTL) source files.
8596
8597 The compiler outputs its object files and ALI files in the current
8598 working directory.
8599 Caution: The object file can be redirected with the @code{-o} switch;
8600 however, @code{gcc} and @code{gnat1} have not been coordinated on this
8601 so the @code{ALI} file will not go to the right place. Therefore, you should
8602 avoid using the @code{-o} switch.
8603
8604 @geindex System.IO
8605
8606 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8607 children make up the GNAT RTL, together with the simple @code{System.IO}
8608 package used in the @code{"Hello World"} example. The sources for these units
8609 are needed by the compiler and are kept together in one directory. Not
8610 all of the bodies are needed, but all of the sources are kept together
8611 anyway. In a normal installation, you need not specify these directory
8612 names when compiling or binding. Either the environment variables or
8613 the built-in defaults cause these files to be found.
8614
8615 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
8616 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
8617 consisting of child units of @code{GNAT}. This is a collection of generally
8618 useful types, subprograms, etc. See the @cite{GNAT_Reference_Manual}
8619 for further details.
8620
8621 Besides simplifying access to the RTL, a major use of search paths is
8622 in compiling sources from multiple directories. This can make
8623 development environments much more flexible.
8624
8625 @node Order of Compilation Issues,Examples,Search Paths and the Run-Time Library RTL,Compiling with gcc
8626 @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}
8627 @subsection Order of Compilation Issues
8628
8629
8630 If, in our earlier example, there was a spec for the @code{hello}
8631 procedure, it would be contained in the file @code{hello.ads}; yet this
8632 file would not have to be explicitly compiled. This is the result of the
8633 model we chose to implement library management. Some of the consequences
8634 of this model are as follows:
8635
8636
8637 @itemize *
8638
8639 @item
8640 There is no point in compiling specs (except for package
8641 specs with no bodies) because these are compiled as needed by clients. If
8642 you attempt a useless compilation, you will receive an error message.
8643 It is also useless to compile subunits because they are compiled as needed
8644 by the parent.
8645
8646 @item
8647 There are no order of compilation requirements: performing a
8648 compilation never obsoletes anything. The only way you can obsolete
8649 something and require recompilations is to modify one of the
8650 source files on which it depends.
8651
8652 @item
8653 There is no library as such, apart from the ALI files
8654 (@ref{42,,The Ada Library Information Files}, for information on the format
8655 of these files). For now we find it convenient to create separate ALI files,
8656 but eventually the information therein may be incorporated into the object
8657 file directly.
8658
8659 @item
8660 When you compile a unit, the source files for the specs of all units
8661 that it @emph{with}s, all its subunits, and the bodies of any generics it
8662 instantiates must be available (reachable by the search-paths mechanism
8663 described above), or you will receive a fatal error message.
8664 @end itemize
8665
8666 @node Examples,,Order of Compilation Issues,Compiling with gcc
8667 @anchor{gnat_ugn/building_executable_programs_with_gnat id12}@anchor{ee}@anchor{gnat_ugn/building_executable_programs_with_gnat examples}@anchor{ef}
8668 @subsection Examples
8669
8670
8671 The following are some typical Ada compilation command line examples:
8672
8673 @example
8674 $ gcc -c xyz.adb
8675 @end example
8676
8677 Compile body in file @code{xyz.adb} with all default options.
8678
8679 @example
8680 $ gcc -c -O2 -gnata xyz-def.adb
8681 @end example
8682
8683 Compile the child unit package in file @code{xyz-def.adb} with extensive
8684 optimizations, and pragma @code{Assert}/@cite{Debug} statements
8685 enabled.
8686
8687 @example
8688 $ gcc -c -gnatc abc-def.adb
8689 @end example
8690
8691 Compile the subunit in file @code{abc-def.adb} in semantic-checking-only
8692 mode.
8693
8694 @node Compiler Switches,Linker Switches,Compiling with gcc,Building Executable Programs with GNAT
8695 @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}
8696 @section Compiler Switches
8697
8698
8699 The @code{gcc} command accepts switches that control the
8700 compilation process. These switches are fully described in this section:
8701 first an alphabetical listing of all switches with a brief description,
8702 and then functionally grouped sets of switches with more detailed
8703 information.
8704
8705 More switches exist for GCC than those documented here, especially
8706 for specific targets. However, their use is not recommended as
8707 they may change code generation in ways that are incompatible with
8708 the Ada run-time library, or can cause inconsistencies between
8709 compilation units.
8710
8711 @menu
8712 * Alphabetical List of All Switches::
8713 * Output and Error Message Control::
8714 * Warning Message Control::
8715 * Debugging and Assertion Control::
8716 * Validity Checking::
8717 * Style Checking::
8718 * Run-Time Checks::
8719 * Using gcc for Syntax Checking::
8720 * Using gcc for Semantic Checking::
8721 * Compiling Different Versions of Ada::
8722 * Character Set Control::
8723 * File Naming Control::
8724 * Subprogram Inlining Control::
8725 * Auxiliary Output Control::
8726 * Debugging Control::
8727 * Exception Handling Control::
8728 * Units to Sources Mapping Files::
8729 * Code Generation Control::
8730
8731 @end menu
8732
8733 @node Alphabetical List of All Switches,Output and Error Message Control,,Compiler Switches
8734 @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}
8735 @subsection Alphabetical List of All Switches
8736
8737
8738 @geindex -b (gcc)
8739
8740
8741 @table @asis
8742
8743 @item @code{-b @emph{target}}
8744
8745 Compile your program to run on @code{target}, which is the name of a
8746 system configuration. You must have a GNAT cross-compiler built if
8747 @code{target} is not the same as your host system.
8748 @end table
8749
8750 @geindex -B (gcc)
8751
8752
8753 @table @asis
8754
8755 @item @code{-B@emph{dir}}
8756
8757 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8758 from @code{dir} instead of the default location. Only use this switch
8759 when multiple versions of the GNAT compiler are available.
8760 See the "Options for Directory Search" section in the
8761 @cite{Using the GNU Compiler Collection (GCC)} manual for further details.
8762 You would normally use the @code{-b} or @code{-V} switch instead.
8763 @end table
8764
8765 @geindex -c (gcc)
8766
8767
8768 @table @asis
8769
8770 @item @code{-c}
8771
8772 Compile. Always use this switch when compiling Ada programs.
8773
8774 Note: for some other languages when using @code{gcc}, notably in
8775 the case of C and C++, it is possible to use
8776 use @code{gcc} without a @code{-c} switch to
8777 compile and link in one step. In the case of GNAT, you
8778 cannot use this approach, because the binder must be run
8779 and @code{gcc} cannot be used to run the GNAT binder.
8780 @end table
8781
8782 @geindex -fcallgraph-info (gcc)
8783
8784
8785 @table @asis
8786
8787 @item @code{-fcallgraph-info[=su,da]}
8788
8789 Makes the compiler output callgraph information for the program, on a
8790 per-file basis. The information is generated in the VCG format. It can
8791 be decorated with additional, per-node and/or per-edge information, if a
8792 list of comma-separated markers is additionally specified. When the
8793 @code{su} marker is specified, the callgraph is decorated with stack usage
8794 information; it is equivalent to @code{-fstack-usage}. When the @code{da}
8795 marker is specified, the callgraph is decorated with information about
8796 dynamically allocated objects.
8797 @end table
8798
8799 @geindex -fdump-scos (gcc)
8800
8801
8802 @table @asis
8803
8804 @item @code{-fdump-scos}
8805
8806 Generates SCO (Source Coverage Obligation) information in the ALI file.
8807 This information is used by advanced coverage tools. See unit @code{SCOs}
8808 in the compiler sources for details in files @code{scos.ads} and
8809 @code{scos.adb}.
8810 @end table
8811
8812 @geindex -flto (gcc)
8813
8814
8815 @table @asis
8816
8817 @item @code{-flto[=@emph{n}]}
8818
8819 Enables Link Time Optimization. This switch must be used in conjunction
8820 with the @code{-Ox} switches (but not with the @code{-gnatn} switch
8821 since it is a full replacement for the latter) and instructs the compiler
8822 to defer most optimizations until the link stage. The advantage of this
8823 approach is that the compiler can do a whole-program analysis and choose
8824 the best interprocedural optimization strategy based on a complete view
8825 of the program, instead of a fragmentary view with the usual approach.
8826 This can also speed up the compilation of big programs and reduce the
8827 size of the executable, compared with a traditional per-unit compilation
8828 with inlining across units enabled by the @code{-gnatn} switch.
8829 The drawback of this approach is that it may require more memory and that
8830 the debugging information generated by -g with it might be hardly usable.
8831 The switch, as well as the accompanying @code{-Ox} switches, must be
8832 specified both for the compilation and the link phases.
8833 If the @code{n} parameter is specified, the optimization and final code
8834 generation at link time are executed using @code{n} parallel jobs by
8835 means of an installed @code{make} program.
8836 @end table
8837
8838 @geindex -fno-inline (gcc)
8839
8840
8841 @table @asis
8842
8843 @item @code{-fno-inline}
8844
8845 Suppresses all inlining, unless requested with pragma @code{Inline_Always}. The
8846 effect is enforced regardless of other optimization or inlining switches.
8847 Note that inlining can also be suppressed on a finer-grained basis with
8848 pragma @code{No_Inline}.
8849 @end table
8850
8851 @geindex -fno-inline-functions (gcc)
8852
8853
8854 @table @asis
8855
8856 @item @code{-fno-inline-functions}
8857
8858 Suppresses automatic inlining of subprograms, which is enabled
8859 if @code{-O3} is used.
8860 @end table
8861
8862 @geindex -fno-inline-small-functions (gcc)
8863
8864
8865 @table @asis
8866
8867 @item @code{-fno-inline-small-functions}
8868
8869 Suppresses automatic inlining of small subprograms, which is enabled
8870 if @code{-O2} is used.
8871 @end table
8872
8873 @geindex -fno-inline-functions-called-once (gcc)
8874
8875
8876 @table @asis
8877
8878 @item @code{-fno-inline-functions-called-once}
8879
8880 Suppresses inlining of subprograms local to the unit and called once
8881 from within it, which is enabled if @code{-O1} is used.
8882 @end table
8883
8884 @geindex -fno-ivopts (gcc)
8885
8886
8887 @table @asis
8888
8889 @item @code{-fno-ivopts}
8890
8891 Suppresses high-level loop induction variable optimizations, which are
8892 enabled if @code{-O1} is used. These optimizations are generally
8893 profitable but, for some specific cases of loops with numerous uses
8894 of the iteration variable that follow a common pattern, they may end
8895 up destroying the regularity that could be exploited at a lower level
8896 and thus producing inferior code.
8897 @end table
8898
8899 @geindex -fno-strict-aliasing (gcc)
8900
8901
8902 @table @asis
8903
8904 @item @code{-fno-strict-aliasing}
8905
8906 Causes the compiler to avoid assumptions regarding non-aliasing
8907 of objects of different types. See
8908 @ref{f3,,Optimization and Strict Aliasing} for details.
8909 @end table
8910
8911 @geindex -fno-strict-overflow (gcc)
8912
8913
8914 @table @asis
8915
8916 @item @code{-fno-strict-overflow}
8917
8918 Causes the compiler to avoid assumptions regarding the rules of signed
8919 integer overflow. These rules specify that signed integer overflow will
8920 result in a Constraint_Error exception at run time and are enforced in
8921 default mode by the compiler, so this switch should not be necessary in
8922 normal operating mode. It might be useful in conjunction with @code{-gnato0}
8923 for very peculiar cases of low-level programming.
8924 @end table
8925
8926 @geindex -fstack-check (gcc)
8927
8928
8929 @table @asis
8930
8931 @item @code{-fstack-check}
8932
8933 Activates stack checking.
8934 See @ref{f4,,Stack Overflow Checking} for details.
8935 @end table
8936
8937 @geindex -fstack-usage (gcc)
8938
8939
8940 @table @asis
8941
8942 @item @code{-fstack-usage}
8943
8944 Makes the compiler output stack usage information for the program, on a
8945 per-subprogram basis. See @ref{f5,,Static Stack Usage Analysis} for details.
8946 @end table
8947
8948 @geindex -g (gcc)
8949
8950
8951 @table @asis
8952
8953 @item @code{-g}
8954
8955 Generate debugging information. This information is stored in the object
8956 file and copied from there to the final executable file by the linker,
8957 where it can be read by the debugger. You must use the
8958 @code{-g} switch if you plan on using the debugger.
8959 @end table
8960
8961 @geindex -gnat05 (gcc)
8962
8963
8964 @table @asis
8965
8966 @item @code{-gnat05}
8967
8968 Allow full Ada 2005 features.
8969 @end table
8970
8971 @geindex -gnat12 (gcc)
8972
8973
8974 @table @asis
8975
8976 @item @code{-gnat12}
8977
8978 Allow full Ada 2012 features.
8979 @end table
8980
8981 @geindex -gnat83 (gcc)
8982
8983 @geindex -gnat2005 (gcc)
8984
8985
8986 @table @asis
8987
8988 @item @code{-gnat2005}
8989
8990 Allow full Ada 2005 features (same as @code{-gnat05})
8991 @end table
8992
8993 @geindex -gnat2012 (gcc)
8994
8995
8996 @table @asis
8997
8998 @item @code{-gnat2012}
8999
9000 Allow full Ada 2012 features (same as @code{-gnat12})
9001
9002 @item @code{-gnat83}
9003
9004 Enforce Ada 83 restrictions.
9005 @end table
9006
9007 @geindex -gnat95 (gcc)
9008
9009
9010 @table @asis
9011
9012 @item @code{-gnat95}
9013
9014 Enforce Ada 95 restrictions.
9015
9016 Note: for compatibility with some Ada 95 compilers which support only
9017 the @code{overriding} keyword of Ada 2005, the @code{-gnatd.D} switch can
9018 be used along with @code{-gnat95} to achieve a similar effect with GNAT.
9019
9020 @code{-gnatd.D} instructs GNAT to consider @code{overriding} as a keyword
9021 and handle its associated semantic checks, even in Ada 95 mode.
9022 @end table
9023
9024 @geindex -gnata (gcc)
9025
9026
9027 @table @asis
9028
9029 @item @code{-gnata}
9030
9031 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
9032 activated. Note that these pragmas can also be controlled using the
9033 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
9034 It also activates pragmas @code{Check}, @code{Precondition}, and
9035 @code{Postcondition}. Note that these pragmas can also be controlled
9036 using the configuration pragma @code{Check_Policy}. In Ada 2012, it
9037 also activates all assertions defined in the RM as aspects: preconditions,
9038 postconditions, type invariants and (sub)type predicates. In all Ada modes,
9039 corresponding pragmas for type invariants and (sub)type predicates are
9040 also activated. The default is that all these assertions are disabled,
9041 and have no effect, other than being checked for syntactic validity, and
9042 in the case of subtype predicates, constructions such as membership tests
9043 still test predicates even if assertions are turned off.
9044 @end table
9045
9046 @geindex -gnatA (gcc)
9047
9048
9049 @table @asis
9050
9051 @item @code{-gnatA}
9052
9053 Avoid processing @code{gnat.adc}. If a @code{gnat.adc} file is present,
9054 it will be ignored.
9055 @end table
9056
9057 @geindex -gnatb (gcc)
9058
9059
9060 @table @asis
9061
9062 @item @code{-gnatb}
9063
9064 Generate brief messages to @code{stderr} even if verbose mode set.
9065 @end table
9066
9067 @geindex -gnatB (gcc)
9068
9069
9070 @table @asis
9071
9072 @item @code{-gnatB}
9073
9074 Assume no invalid (bad) values except for 'Valid attribute use
9075 (@ref{f6,,Validity Checking}).
9076 @end table
9077
9078 @geindex -gnatc (gcc)
9079
9080
9081 @table @asis
9082
9083 @item @code{-gnatc}
9084
9085 Check syntax and semantics only (no code generation attempted). When the
9086 compiler is invoked by @code{gnatmake}, if the switch @code{-gnatc} is
9087 only given to the compiler (after @code{-cargs} or in package Compiler of
9088 the project file, @code{gnatmake} will fail because it will not find the
9089 object file after compilation. If @code{gnatmake} is called with
9090 @code{-gnatc} as a builder switch (before @code{-cargs} or in package
9091 Builder of the project file) then @code{gnatmake} will not fail because
9092 it will not look for the object files after compilation, and it will not try
9093 to build and link.
9094 @end table
9095
9096 @geindex -gnatC (gcc)
9097
9098
9099 @table @asis
9100
9101 @item @code{-gnatC}
9102
9103 Generate CodePeer intermediate format (no code generation attempted).
9104 This switch will generate an intermediate representation suitable for
9105 use by CodePeer (@code{.scil} files). This switch is not compatible with
9106 code generation (it will, among other things, disable some switches such
9107 as -gnatn, and enable others such as -gnata).
9108 @end table
9109
9110 @geindex -gnatd (gcc)
9111
9112
9113 @table @asis
9114
9115 @item @code{-gnatd}
9116
9117 Specify debug options for the compiler. The string of characters after
9118 the @code{-gnatd} specify the specific debug options. The possible
9119 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
9120 compiler source file @code{debug.adb} for details of the implemented
9121 debug options. Certain debug options are relevant to applications
9122 programmers, and these are documented at appropriate points in this
9123 users guide.
9124 @end table
9125
9126 @geindex -gnatD[nn] (gcc)
9127
9128
9129 @table @asis
9130
9131 @item @code{-gnatD}
9132
9133 Create expanded source files for source level debugging. This switch
9134 also suppresses generation of cross-reference information
9135 (see @code{-gnatx}). Note that this switch is not allowed if a previous
9136 -gnatR switch has been given, since these two switches are not compatible.
9137 @end table
9138
9139 @geindex -gnateA (gcc)
9140
9141
9142 @table @asis
9143
9144 @item @code{-gnateA}
9145
9146 Check that the actual parameters of a subprogram call are not aliases of one
9147 another. To qualify as aliasing, the actuals must denote objects of a composite
9148 type, their memory locations must be identical or overlapping, and at least one
9149 of the corresponding formal parameters must be of mode OUT or IN OUT.
9150
9151 @example
9152 type Rec_Typ is record
9153 Data : Integer := 0;
9154 end record;
9155
9156 function Self (Val : Rec_Typ) return Rec_Typ is
9157 begin
9158 return Val;
9159 end Self;
9160
9161 procedure Detect_Aliasing (Val_1 : in out Rec_Typ; Val_2 : Rec_Typ) is
9162 begin
9163 null;
9164 end Detect_Aliasing;
9165
9166 Obj : Rec_Typ;
9167
9168 Detect_Aliasing (Obj, Obj);
9169 Detect_Aliasing (Obj, Self (Obj));
9170 @end example
9171
9172 In the example above, the first call to @code{Detect_Aliasing} fails with a
9173 @code{Program_Error} at run time because the actuals for @code{Val_1} and
9174 @code{Val_2} denote the same object. The second call executes without raising
9175 an exception because @code{Self(Obj)} produces an anonymous object which does
9176 not share the memory location of @code{Obj}.
9177 @end table
9178
9179 @geindex -gnatec (gcc)
9180
9181
9182 @table @asis
9183
9184 @item @code{-gnatec=@emph{path}}
9185
9186 Specify a configuration pragma file
9187 (the equal sign is optional)
9188 (@ref{79,,The Configuration Pragmas Files}).
9189 @end table
9190
9191 @geindex -gnateC (gcc)
9192
9193
9194 @table @asis
9195
9196 @item @code{-gnateC}
9197
9198 Generate CodePeer messages in a compiler-like format. This switch is only
9199 effective if @code{-gnatcC} is also specified and requires an installation
9200 of CodePeer.
9201 @end table
9202
9203 @geindex -gnated (gcc)
9204
9205
9206 @table @asis
9207
9208 @item @code{-gnated}
9209
9210 Disable atomic synchronization
9211 @end table
9212
9213 @geindex -gnateD (gcc)
9214
9215
9216 @table @asis
9217
9218 @item @code{-gnateDsymbol[=@emph{value}]}
9219
9220 Defines a symbol, associated with @code{value}, for preprocessing.
9221 (@ref{18,,Integrated Preprocessing}).
9222 @end table
9223
9224 @geindex -gnateE (gcc)
9225
9226
9227 @table @asis
9228
9229 @item @code{-gnateE}
9230
9231 Generate extra information in exception messages. In particular, display
9232 extra column information and the value and range associated with index and
9233 range check failures, and extra column information for access checks.
9234 In cases where the compiler is able to determine at compile time that
9235 a check will fail, it gives a warning, and the extra information is not
9236 produced at run time.
9237 @end table
9238
9239 @geindex -gnatef (gcc)
9240
9241
9242 @table @asis
9243
9244 @item @code{-gnatef}
9245
9246 Display full source path name in brief error messages.
9247 @end table
9248
9249 @geindex -gnateF (gcc)
9250
9251
9252 @table @asis
9253
9254 @item @code{-gnateF}
9255
9256 Check for overflow on all floating-point operations, including those
9257 for unconstrained predefined types. See description of pragma
9258 @code{Check_Float_Overflow} in GNAT RM.
9259 @end table
9260
9261 @geindex -gnateg (gcc)
9262
9263 @code{-gnateg}
9264 @code{-gnatceg}
9265
9266 @quotation
9267
9268 The @code{-gnatc} switch must always be specified before this switch, e.g.
9269 @code{-gnatceg}. Generate a C header from the Ada input file. See
9270 @ref{ca,,Generating C Headers for Ada Specifications} for more
9271 information.
9272 @end quotation
9273
9274 @geindex -gnateG (gcc)
9275
9276
9277 @table @asis
9278
9279 @item @code{-gnateG}
9280
9281 Save result of preprocessing in a text file.
9282 @end table
9283
9284 @geindex -gnatei (gcc)
9285
9286
9287 @table @asis
9288
9289 @item @code{-gnatei@emph{nnn}}
9290
9291 Set maximum number of instantiations during compilation of a single unit to
9292 @code{nnn}. This may be useful in increasing the default maximum of 8000 for
9293 the rare case when a single unit legitimately exceeds this limit.
9294 @end table
9295
9296 @geindex -gnateI (gcc)
9297
9298
9299 @table @asis
9300
9301 @item @code{-gnateI@emph{nnn}}
9302
9303 Indicates that the source is a multi-unit source and that the index of the
9304 unit to compile is @code{nnn}. @code{nnn} needs to be a positive number and need
9305 to be a valid index in the multi-unit source.
9306 @end table
9307
9308 @geindex -gnatel (gcc)
9309
9310
9311 @table @asis
9312
9313 @item @code{-gnatel}
9314
9315 This switch can be used with the static elaboration model to issue info
9316 messages showing
9317 where implicit @code{pragma Elaborate} and @code{pragma Elaborate_All}
9318 are generated. This is useful in diagnosing elaboration circularities
9319 caused by these implicit pragmas when using the static elaboration
9320 model. See See the section in this guide on elaboration checking for
9321 further details. These messages are not generated by default, and are
9322 intended only for temporary use when debugging circularity problems.
9323 @end table
9324
9325 @geindex -gnatel (gcc)
9326
9327
9328 @table @asis
9329
9330 @item @code{-gnateL}
9331
9332 This switch turns off the info messages about implicit elaboration pragmas.
9333 @end table
9334
9335 @geindex -gnatem (gcc)
9336
9337
9338 @table @asis
9339
9340 @item @code{-gnatem=@emph{path}}
9341
9342 Specify a mapping file
9343 (the equal sign is optional)
9344 (@ref{f7,,Units to Sources Mapping Files}).
9345 @end table
9346
9347 @geindex -gnatep (gcc)
9348
9349
9350 @table @asis
9351
9352 @item @code{-gnatep=@emph{file}}
9353
9354 Specify a preprocessing data file
9355 (the equal sign is optional)
9356 (@ref{18,,Integrated Preprocessing}).
9357 @end table
9358
9359 @geindex -gnateP (gcc)
9360
9361
9362 @table @asis
9363
9364 @item @code{-gnateP}
9365
9366 Turn categorization dependency errors into warnings.
9367 Ada requires that units that WITH one another have compatible categories, for
9368 example a Pure unit cannot WITH a Preelaborate unit. If this switch is used,
9369 these errors become warnings (which can be ignored, or suppressed in the usual
9370 manner). This can be useful in some specialized circumstances such as the
9371 temporary use of special test software.
9372 @end table
9373
9374 @geindex -gnateS (gcc)
9375
9376
9377 @table @asis
9378
9379 @item @code{-gnateS}
9380
9381 Synonym of @code{-fdump-scos}, kept for backwards compatibility.
9382 @end table
9383
9384 @geindex -gnatet=file (gcc)
9385
9386
9387 @table @asis
9388
9389 @item @code{-gnatet=@emph{path}}
9390
9391 Generate target dependent information. The format of the output file is
9392 described in the section about switch @code{-gnateT}.
9393 @end table
9394
9395 @geindex -gnateT (gcc)
9396
9397
9398 @table @asis
9399
9400 @item @code{-gnateT=@emph{path}}
9401
9402 Read target dependent information, such as endianness or sizes and alignments
9403 of base type. If this switch is passed, the default target dependent
9404 information of the compiler is replaced by the one read from the input file.
9405 This is used by tools other than the compiler, e.g. to do
9406 semantic analysis of programs that will run on some other target than
9407 the machine on which the tool is run.
9408
9409 The following target dependent values should be defined,
9410 where @code{Nat} denotes a natural integer value, @code{Pos} denotes a
9411 positive integer value, and fields marked with a question mark are
9412 boolean fields, where a value of 0 is False, and a value of 1 is True:
9413
9414 @example
9415 Bits_BE : Nat; -- Bits stored big-endian?
9416 Bits_Per_Unit : Pos; -- Bits in a storage unit
9417 Bits_Per_Word : Pos; -- Bits in a word
9418 Bytes_BE : Nat; -- Bytes stored big-endian?
9419 Char_Size : Pos; -- Standard.Character'Size
9420 Double_Float_Alignment : Nat; -- Alignment of double float
9421 Double_Scalar_Alignment : Nat; -- Alignment of double length scalar
9422 Double_Size : Pos; -- Standard.Long_Float'Size
9423 Float_Size : Pos; -- Standard.Float'Size
9424 Float_Words_BE : Nat; -- Float words stored big-endian?
9425 Int_Size : Pos; -- Standard.Integer'Size
9426 Long_Double_Size : Pos; -- Standard.Long_Long_Float'Size
9427 Long_Long_Size : Pos; -- Standard.Long_Long_Integer'Size
9428 Long_Size : Pos; -- Standard.Long_Integer'Size
9429 Maximum_Alignment : Pos; -- Maximum permitted alignment
9430 Max_Unaligned_Field : Pos; -- Maximum size for unaligned bit field
9431 Pointer_Size : Pos; -- System.Address'Size
9432 Short_Enums : Nat; -- Foreign enums use short size?
9433 Short_Size : Pos; -- Standard.Short_Integer'Size
9434 Strict_Alignment : Nat; -- Strict alignment?
9435 System_Allocator_Alignment : Nat; -- Alignment for malloc calls
9436 Wchar_T_Size : Pos; -- Interfaces.C.wchar_t'Size
9437 Words_BE : Nat; -- Words stored big-endian?
9438 @end example
9439
9440 @code{Bits_Per_Unit} is the number of bits in a storage unit, the equivalent of
9441 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.}
9442
9443 @code{Bits_Per_Word} is the number of bits in a machine word, the equivalent of
9444 GCC macro @code{BITS_PER_WORD} documented as follows: @cite{Number of bits in a word; normally 32.}
9445
9446 @code{Double_Scalar_Alignment} is the alignment for a scalar whose size is two
9447 machine words. It should be the same as the alignment for C @code{long_long} on
9448 most targets.
9449
9450 @code{Maximum_Alignment} is the maximum alignment that the compiler might choose
9451 by default for a type or object, which is also the maximum alignment that can
9452 be specified in GNAT. It is computed for GCC backends as @code{BIGGEST_ALIGNMENT
9453 / BITS_PER_UNIT} where GCC macro @code{BIGGEST_ALIGNMENT} is documented as
9454 follows: @cite{Biggest alignment that any data type can require on this machine@comma{} in bits.}
9455
9456 @code{Max_Unaligned_Field} is the maximum size for unaligned bit field, which is
9457 64 for the majority of GCC targets (but can be different on some targets like
9458 AAMP).
9459
9460 @code{Strict_Alignment} is the equivalent of GCC macro @code{STRICT_ALIGNMENT}
9461 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.}
9462
9463 @code{System_Allocator_Alignment} is the guaranteed alignment of data returned
9464 by calls to @code{malloc}.
9465
9466 The format of the input file is as follows. First come the values of
9467 the variables defined above, with one line per value:
9468
9469 @example
9470 name value
9471 @end example
9472
9473 where @code{name} is the name of the parameter, spelled out in full,
9474 and cased as in the above list, and @code{value} is an unsigned decimal
9475 integer. Two or more blanks separates the name from the value.
9476
9477 All the variables must be present, in alphabetical order (i.e. the
9478 same order as the list above).
9479
9480 Then there is a blank line to separate the two parts of the file. Then
9481 come the lines showing the floating-point types to be registered, with
9482 one line per registered mode:
9483
9484 @example
9485 name digs float_rep size alignment
9486 @end example
9487
9488 where @code{name} is the string name of the type (which can have
9489 single spaces embedded in the name (e.g. long double), @code{digs} is
9490 the number of digits for the floating-point type, @code{float_rep} is
9491 the float representation (I/V/A for IEEE-754-Binary, Vax_Native,
9492 AAMP), @code{size} is the size in bits, @code{alignment} is the
9493 alignment in bits. The name is followed by at least two blanks, fields
9494 are separated by at least one blank, and a LF character immediately
9495 follows the alignment field.
9496
9497 Here is an example of a target parameterization file:
9498
9499 @example
9500 Bits_BE 0
9501 Bits_Per_Unit 8
9502 Bits_Per_Word 64
9503 Bytes_BE 0
9504 Char_Size 8
9505 Double_Float_Alignment 0
9506 Double_Scalar_Alignment 0
9507 Double_Size 64
9508 Float_Size 32
9509 Float_Words_BE 0
9510 Int_Size 64
9511 Long_Double_Size 128
9512 Long_Long_Size 64
9513 Long_Size 64
9514 Maximum_Alignment 16
9515 Max_Unaligned_Field 64
9516 Pointer_Size 64
9517 Short_Size 16
9518 Strict_Alignment 0
9519 System_Allocator_Alignment 16
9520 Wchar_T_Size 32
9521 Words_BE 0
9522
9523 float 15 I 64 64
9524 double 15 I 64 64
9525 long double 18 I 80 128
9526 TF 33 I 128 128
9527 @end example
9528 @end table
9529
9530 @geindex -gnateu (gcc)
9531
9532
9533 @table @asis
9534
9535 @item @code{-gnateu}
9536
9537 Ignore unrecognized validity, warning, and style switches that
9538 appear after this switch is given. This may be useful when
9539 compiling sources developed on a later version of the compiler
9540 with an earlier version. Of course the earlier version must
9541 support this switch.
9542 @end table
9543
9544 @geindex -gnateV (gcc)
9545
9546
9547 @table @asis
9548
9549 @item @code{-gnateV}
9550
9551 Check that all actual parameters of a subprogram call are valid according to
9552 the rules of validity checking (@ref{f6,,Validity Checking}).
9553 @end table
9554
9555 @geindex -gnateY (gcc)
9556
9557
9558 @table @asis
9559
9560 @item @code{-gnateY}
9561
9562 Ignore all STYLE_CHECKS pragmas. Full legality checks
9563 are still carried out, but the pragmas have no effect
9564 on what style checks are active. This allows all style
9565 checking options to be controlled from the command line.
9566 @end table
9567
9568 @geindex -gnatE (gcc)
9569
9570
9571 @table @asis
9572
9573 @item @code{-gnatE}
9574
9575 Full dynamic elaboration checks.
9576 @end table
9577
9578 @geindex -gnatf (gcc)
9579
9580
9581 @table @asis
9582
9583 @item @code{-gnatf}
9584
9585 Full errors. Multiple errors per line, all undefined references, do not
9586 attempt to suppress cascaded errors.
9587 @end table
9588
9589 @geindex -gnatF (gcc)
9590
9591
9592 @table @asis
9593
9594 @item @code{-gnatF}
9595
9596 Externals names are folded to all uppercase.
9597 @end table
9598
9599 @geindex -gnatg (gcc)
9600
9601
9602 @table @asis
9603
9604 @item @code{-gnatg}
9605
9606 Internal GNAT implementation mode. This should not be used for applications
9607 programs, it is intended only for use by the compiler and its run-time
9608 library. For documentation, see the GNAT sources. Note that @code{-gnatg}
9609 implies @code{-gnatw.ge} and @code{-gnatyg} so that all standard
9610 warnings and all standard style options are turned on. All warnings and style
9611 messages are treated as errors.
9612 @end table
9613
9614 @geindex -gnatG[nn] (gcc)
9615
9616
9617 @table @asis
9618
9619 @item @code{-gnatG=nn}
9620
9621 List generated expanded code in source form.
9622 @end table
9623
9624 @geindex -gnath (gcc)
9625
9626
9627 @table @asis
9628
9629 @item @code{-gnath}
9630
9631 Output usage information. The output is written to @code{stdout}.
9632 @end table
9633
9634 @geindex -gnatH (gcc)
9635
9636
9637 @table @asis
9638
9639 @item @code{-gnatH}
9640
9641 Legacy elaboration-checking mode enabled. When this switch is in effect, the
9642 pre-18.x access-before-elaboration model becomes the de facto model.
9643 @end table
9644
9645 @geindex -gnati (gcc)
9646
9647
9648 @table @asis
9649
9650 @item @code{-gnati@emph{c}}
9651
9652 Identifier character set (@code{c} = 1/2/3/4/8/9/p/f/n/w).
9653 For details of the possible selections for @code{c},
9654 see @ref{48,,Character Set Control}.
9655 @end table
9656
9657 @geindex -gnatI (gcc)
9658
9659
9660 @table @asis
9661
9662 @item @code{-gnatI}
9663
9664 Ignore representation clauses. When this switch is used,
9665 representation clauses are treated as comments. This is useful
9666 when initially porting code where you want to ignore rep clause
9667 problems, and also for compiling foreign code (particularly
9668 for use with ASIS). The representation clauses that are ignored
9669 are: enumeration_representation_clause, record_representation_clause,
9670 and attribute_definition_clause for the following attributes:
9671 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
9672 Object_Size, Scalar_Storage_Order, Size, Small, Stream_Size,
9673 and Value_Size. Pragma Default_Scalar_Storage_Order is also ignored.
9674 Note that this option should be used only for compiling -- the
9675 code is likely to malfunction at run time.
9676
9677 Note that when @code{-gnatct} is used to generate trees for input
9678 into ASIS tools, these representation clauses are removed
9679 from the tree and ignored. This means that the tool will not see them.
9680 @end table
9681
9682 @geindex -gnatjnn (gcc)
9683
9684
9685 @table @asis
9686
9687 @item @code{-gnatj@emph{nn}}
9688
9689 Reformat error messages to fit on @code{nn} character lines
9690 @end table
9691
9692 @geindex -gnatJ (gcc)
9693
9694
9695 @table @asis
9696
9697 @item @code{-gnatJ}
9698
9699 Permissive elaboration-checking mode enabled. When this switch is in effect,
9700 the post-18.x access-before-elaboration model ignores potential issues with:
9701
9702
9703 @itemize -
9704
9705 @item
9706 Accept statements
9707
9708 @item
9709 Activations of tasks defined in instances
9710
9711 @item
9712 Assertion pragmas
9713
9714 @item
9715 Calls from within an instance to its enclosing context
9716
9717 @item
9718 Calls through generic formal parameters
9719
9720 @item
9721 Calls to subprograms defined in instances
9722
9723 @item
9724 Entry calls
9725
9726 @item
9727 Indirect calls using 'Access
9728
9729 @item
9730 Requeue statements
9731
9732 @item
9733 Select statements
9734
9735 @item
9736 Synchronous task suspension
9737 @end itemize
9738
9739 and does not emit compile-time diagnostics or run-time checks.
9740 @end table
9741
9742 @geindex -gnatk (gcc)
9743
9744
9745 @table @asis
9746
9747 @item @code{-gnatk=@emph{n}}
9748
9749 Limit file names to @code{n} (1-999) characters (@code{k} = krunch).
9750 @end table
9751
9752 @geindex -gnatl (gcc)
9753
9754
9755 @table @asis
9756
9757 @item @code{-gnatl}
9758
9759 Output full source listing with embedded error messages.
9760 @end table
9761
9762 @geindex -gnatL (gcc)
9763
9764
9765 @table @asis
9766
9767 @item @code{-gnatL}
9768
9769 Used in conjunction with -gnatG or -gnatD to intersperse original
9770 source lines (as comment lines with line numbers) in the expanded
9771 source output.
9772 @end table
9773
9774 @geindex -gnatm (gcc)
9775
9776
9777 @table @asis
9778
9779 @item @code{-gnatm=@emph{n}}
9780
9781 Limit number of detected error or warning messages to @code{n}
9782 where @code{n} is in the range 1..999999. The default setting if
9783 no switch is given is 9999. If the number of warnings reaches this
9784 limit, then a message is output and further warnings are suppressed,
9785 but the compilation is continued. If the number of error messages
9786 reaches this limit, then a message is output and the compilation
9787 is abandoned. The equal sign here is optional. A value of zero
9788 means that no limit applies.
9789 @end table
9790
9791 @geindex -gnatn (gcc)
9792
9793
9794 @table @asis
9795
9796 @item @code{-gnatn[12]}
9797
9798 Activate inlining across units for subprograms for which pragma @code{Inline}
9799 is specified. This inlining is performed by the GCC back-end. An optional
9800 digit sets the inlining level: 1 for moderate inlining across units
9801 or 2 for full inlining across units. If no inlining level is specified,
9802 the compiler will pick it based on the optimization level.
9803 @end table
9804
9805 @geindex -gnatN (gcc)
9806
9807
9808 @table @asis
9809
9810 @item @code{-gnatN}
9811
9812 Activate front end inlining for subprograms for which
9813 pragma @code{Inline} is specified. This inlining is performed
9814 by the front end and will be visible in the
9815 @code{-gnatG} output.
9816
9817 When using a gcc-based back end (in practice this means using any version
9818 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
9819 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
9820 Historically front end inlining was more extensive than the gcc back end
9821 inlining, but that is no longer the case.
9822 @end table
9823
9824 @geindex -gnato0 (gcc)
9825
9826
9827 @table @asis
9828
9829 @item @code{-gnato0}
9830
9831 Suppresses overflow checking. This causes the behavior of the compiler to
9832 match the default for older versions where overflow checking was suppressed
9833 by default. This is equivalent to having
9834 @code{pragma Suppress (Overflow_Check)} in a configuration pragma file.
9835 @end table
9836
9837 @geindex -gnato?? (gcc)
9838
9839
9840 @table @asis
9841
9842 @item @code{-gnato??}
9843
9844 Set default mode for handling generation of code to avoid intermediate
9845 arithmetic overflow. Here @code{??} is two digits, a
9846 single digit, or nothing. Each digit is one of the digits @code{1}
9847 through @code{3}:
9848
9849
9850 @multitable {xxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
9851 @item
9852
9853 Digit
9854
9855 @tab
9856
9857 Interpretation
9858
9859 @item
9860
9861 @emph{1}
9862
9863 @tab
9864
9865 All intermediate overflows checked against base type (@code{STRICT})
9866
9867 @item
9868
9869 @emph{2}
9870
9871 @tab
9872
9873 Minimize intermediate overflows (@code{MINIMIZED})
9874
9875 @item
9876
9877 @emph{3}
9878
9879 @tab
9880
9881 Eliminate intermediate overflows (@code{ELIMINATED})
9882
9883 @end multitable
9884
9885
9886 If only one digit appears, then it applies to all
9887 cases; if two digits are given, then the first applies outside
9888 assertions, pre/postconditions, and type invariants, and the second
9889 applies within assertions, pre/postconditions, and type invariants.
9890
9891 If no digits follow the @code{-gnato}, then it is equivalent to
9892 @code{-gnato11},
9893 causing all intermediate overflows to be handled in strict
9894 mode.
9895
9896 This switch also causes arithmetic overflow checking to be performed
9897 (as though @code{pragma Unsuppress (Overflow_Check)} had been specified).
9898
9899 The default if no option @code{-gnato} is given is that overflow handling
9900 is in @code{STRICT} mode (computations done using the base type), and that
9901 overflow checking is enabled.
9902
9903 Note that division by zero is a separate check that is not
9904 controlled by this switch (divide-by-zero checking is on by default).
9905
9906 See also @ref{f8,,Specifying the Desired Mode}.
9907 @end table
9908
9909 @geindex -gnatp (gcc)
9910
9911
9912 @table @asis
9913
9914 @item @code{-gnatp}
9915
9916 Suppress all checks. See @ref{f9,,Run-Time Checks} for details. This switch
9917 has no effect if cancelled by a subsequent @code{-gnat-p} switch.
9918 @end table
9919
9920 @geindex -gnat-p (gcc)
9921
9922
9923 @table @asis
9924
9925 @item @code{-gnat-p}
9926
9927 Cancel effect of previous @code{-gnatp} switch.
9928 @end table
9929
9930 @geindex -gnatP (gcc)
9931
9932
9933 @table @asis
9934
9935 @item @code{-gnatP}
9936
9937 Enable polling. This is required on some systems (notably Windows NT) to
9938 obtain asynchronous abort and asynchronous transfer of control capability.
9939 See @code{Pragma_Polling} in the @cite{GNAT_Reference_Manual} for full
9940 details.
9941 @end table
9942
9943 @geindex -gnatq (gcc)
9944
9945
9946 @table @asis
9947
9948 @item @code{-gnatq}
9949
9950 Don't quit. Try semantics, even if parse errors.
9951 @end table
9952
9953 @geindex -gnatQ (gcc)
9954
9955
9956 @table @asis
9957
9958 @item @code{-gnatQ}
9959
9960 Don't quit. Generate @code{ALI} and tree files even if illegalities.
9961 Note that code generation is still suppressed in the presence of any
9962 errors, so even with @code{-gnatQ} no object file is generated.
9963 @end table
9964
9965 @geindex -gnatr (gcc)
9966
9967
9968 @table @asis
9969
9970 @item @code{-gnatr}
9971
9972 Treat pragma Restrictions as Restriction_Warnings.
9973 @end table
9974
9975 @geindex -gnatR (gcc)
9976
9977
9978 @table @asis
9979
9980 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
9981
9982 Output representation information for declared types, objects and
9983 subprograms. Note that this switch is not allowed if a previous
9984 @code{-gnatD} switch has been given, since these two switches
9985 are not compatible.
9986 @end table
9987
9988 @geindex -gnats (gcc)
9989
9990
9991 @table @asis
9992
9993 @item @code{-gnats}
9994
9995 Syntax check only.
9996 @end table
9997
9998 @geindex -gnatS (gcc)
9999
10000
10001 @table @asis
10002
10003 @item @code{-gnatS}
10004
10005 Print package Standard.
10006 @end table
10007
10008 @geindex -gnatt (gcc)
10009
10010
10011 @table @asis
10012
10013 @item @code{-gnatt}
10014
10015 Generate tree output file.
10016 @end table
10017
10018 @geindex -gnatT (gcc)
10019
10020
10021 @table @asis
10022
10023 @item @code{-gnatT@emph{nnn}}
10024
10025 All compiler tables start at @code{nnn} times usual starting size.
10026 @end table
10027
10028 @geindex -gnatu (gcc)
10029
10030
10031 @table @asis
10032
10033 @item @code{-gnatu}
10034
10035 List units for this compilation.
10036 @end table
10037
10038 @geindex -gnatU (gcc)
10039
10040
10041 @table @asis
10042
10043 @item @code{-gnatU}
10044
10045 Tag all error messages with the unique string 'error:'
10046 @end table
10047
10048 @geindex -gnatv (gcc)
10049
10050
10051 @table @asis
10052
10053 @item @code{-gnatv}
10054
10055 Verbose mode. Full error output with source lines to @code{stdout}.
10056 @end table
10057
10058 @geindex -gnatV (gcc)
10059
10060
10061 @table @asis
10062
10063 @item @code{-gnatV}
10064
10065 Control level of validity checking (@ref{f6,,Validity Checking}).
10066 @end table
10067
10068 @geindex -gnatw (gcc)
10069
10070
10071 @table @asis
10072
10073 @item @code{-gnatw@emph{xxx}}
10074
10075 Warning mode where
10076 @code{xxx} is a string of option letters that denotes
10077 the exact warnings that
10078 are enabled or disabled (@ref{fa,,Warning Message Control}).
10079 @end table
10080
10081 @geindex -gnatW (gcc)
10082
10083
10084 @table @asis
10085
10086 @item @code{-gnatW@emph{e}}
10087
10088 Wide character encoding method
10089 (@code{e}=n/h/u/s/e/8).
10090 @end table
10091
10092 @geindex -gnatx (gcc)
10093
10094
10095 @table @asis
10096
10097 @item @code{-gnatx}
10098
10099 Suppress generation of cross-reference information.
10100 @end table
10101
10102 @geindex -gnatX (gcc)
10103
10104
10105 @table @asis
10106
10107 @item @code{-gnatX}
10108
10109 Enable GNAT implementation extensions and latest Ada version.
10110 @end table
10111
10112 @geindex -gnaty (gcc)
10113
10114
10115 @table @asis
10116
10117 @item @code{-gnaty}
10118
10119 Enable built-in style checks (@ref{fb,,Style Checking}).
10120 @end table
10121
10122 @geindex -gnatz (gcc)
10123
10124
10125 @table @asis
10126
10127 @item @code{-gnatz@emph{m}}
10128
10129 Distribution stub generation and compilation
10130 (@code{m}=r/c for receiver/caller stubs).
10131 @end table
10132
10133 @geindex -I (gcc)
10134
10135
10136 @table @asis
10137
10138 @item @code{-I@emph{dir}}
10139
10140 @geindex RTL
10141
10142 Direct GNAT to search the @code{dir} directory for source files needed by
10143 the current compilation
10144 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10145 @end table
10146
10147 @geindex -I- (gcc)
10148
10149
10150 @table @asis
10151
10152 @item @code{-I-}
10153
10154 @geindex RTL
10155
10156 Except for the source file named in the command line, do not look for source
10157 files in the directory containing the source file named in the command line
10158 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10159 @end table
10160
10161 @geindex -o (gcc)
10162
10163
10164 @table @asis
10165
10166 @item @code{-o @emph{file}}
10167
10168 This switch is used in @code{gcc} to redirect the generated object file
10169 and its associated ALI file. Beware of this switch with GNAT, because it may
10170 cause the object file and ALI file to have different names which in turn
10171 may confuse the binder and the linker.
10172 @end table
10173
10174 @geindex -nostdinc (gcc)
10175
10176
10177 @table @asis
10178
10179 @item @code{-nostdinc}
10180
10181 Inhibit the search of the default location for the GNAT Run Time
10182 Library (RTL) source files.
10183 @end table
10184
10185 @geindex -nostdlib (gcc)
10186
10187
10188 @table @asis
10189
10190 @item @code{-nostdlib}
10191
10192 Inhibit the search of the default location for the GNAT Run Time
10193 Library (RTL) ALI files.
10194 @end table
10195
10196 @geindex -O (gcc)
10197
10198
10199 @table @asis
10200
10201 @item @code{-O[@emph{n}]}
10202
10203 @code{n} controls the optimization level:
10204
10205
10206 @multitable {xxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
10207 @item
10208
10209 @emph{n}
10210
10211 @tab
10212
10213 Effect
10214
10215 @item
10216
10217 @emph{0}
10218
10219 @tab
10220
10221 No optimization, the default setting if no @code{-O} appears
10222
10223 @item
10224
10225 @emph{1}
10226
10227 @tab
10228
10229 Normal optimization, the default if you specify @code{-O} without an
10230 operand. A good compromise between code quality and compilation
10231 time.
10232
10233 @item
10234
10235 @emph{2}
10236
10237 @tab
10238
10239 Extensive optimization, may improve execution time, possibly at
10240 the cost of substantially increased compilation time.
10241
10242 @item
10243
10244 @emph{3}
10245
10246 @tab
10247
10248 Same as @code{-O2}, and also includes inline expansion for small
10249 subprograms in the same unit.
10250
10251 @item
10252
10253 @emph{s}
10254
10255 @tab
10256
10257 Optimize space usage
10258
10259 @end multitable
10260
10261
10262 See also @ref{fc,,Optimization Levels}.
10263 @end table
10264
10265 @geindex -pass-exit-codes (gcc)
10266
10267
10268 @table @asis
10269
10270 @item @code{-pass-exit-codes}
10271
10272 Catch exit codes from the compiler and use the most meaningful as
10273 exit status.
10274 @end table
10275
10276 @geindex --RTS (gcc)
10277
10278
10279 @table @asis
10280
10281 @item @code{--RTS=@emph{rts-path}}
10282
10283 Specifies the default location of the run-time library. Same meaning as the
10284 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
10285 @end table
10286
10287 @geindex -S (gcc)
10288
10289
10290 @table @asis
10291
10292 @item @code{-S}
10293
10294 Used in place of @code{-c} to
10295 cause the assembler source file to be
10296 generated, using @code{.s} as the extension,
10297 instead of the object file.
10298 This may be useful if you need to examine the generated assembly code.
10299 @end table
10300
10301 @geindex -fverbose-asm (gcc)
10302
10303
10304 @table @asis
10305
10306 @item @code{-fverbose-asm}
10307
10308 Used in conjunction with @code{-S}
10309 to cause the generated assembly code file to be annotated with variable
10310 names, making it significantly easier to follow.
10311 @end table
10312
10313 @geindex -v (gcc)
10314
10315
10316 @table @asis
10317
10318 @item @code{-v}
10319
10320 Show commands generated by the @code{gcc} driver. Normally used only for
10321 debugging purposes or if you need to be sure what version of the
10322 compiler you are executing.
10323 @end table
10324
10325 @geindex -V (gcc)
10326
10327
10328 @table @asis
10329
10330 @item @code{-V @emph{ver}}
10331
10332 Execute @code{ver} version of the compiler. This is the @code{gcc}
10333 version, not the GNAT version.
10334 @end table
10335
10336 @geindex -w (gcc)
10337
10338
10339 @table @asis
10340
10341 @item @code{-w}
10342
10343 Turn off warnings generated by the back end of the compiler. Use of
10344 this switch also causes the default for front end warnings to be set
10345 to suppress (as though @code{-gnatws} had appeared at the start of
10346 the options).
10347 @end table
10348
10349 @geindex Combining GNAT switches
10350
10351 You may combine a sequence of GNAT switches into a single switch. For
10352 example, the combined switch
10353
10354 @quotation
10355
10356 @example
10357 -gnatofi3
10358 @end example
10359 @end quotation
10360
10361 is equivalent to specifying the following sequence of switches:
10362
10363 @quotation
10364
10365 @example
10366 -gnato -gnatf -gnati3
10367 @end example
10368 @end quotation
10369
10370 The following restrictions apply to the combination of switches
10371 in this manner:
10372
10373
10374 @itemize *
10375
10376 @item
10377 The switch @code{-gnatc} if combined with other switches must come
10378 first in the string.
10379
10380 @item
10381 The switch @code{-gnats} if combined with other switches must come
10382 first in the string.
10383
10384 @item
10385 The switches
10386 @code{-gnatzc} and @code{-gnatzr} may not be combined with any other
10387 switches, and only one of them may appear in the command line.
10388
10389 @item
10390 The switch @code{-gnat-p} may not be combined with any other switch.
10391
10392 @item
10393 Once a 'y' appears in the string (that is a use of the @code{-gnaty}
10394 switch), then all further characters in the switch are interpreted
10395 as style modifiers (see description of @code{-gnaty}).
10396
10397 @item
10398 Once a 'd' appears in the string (that is a use of the @code{-gnatd}
10399 switch), then all further characters in the switch are interpreted
10400 as debug flags (see description of @code{-gnatd}).
10401
10402 @item
10403 Once a 'w' appears in the string (that is a use of the @code{-gnatw}
10404 switch), then all further characters in the switch are interpreted
10405 as warning mode modifiers (see description of @code{-gnatw}).
10406
10407 @item
10408 Once a 'V' appears in the string (that is a use of the @code{-gnatV}
10409 switch), then all further characters in the switch are interpreted
10410 as validity checking options (@ref{f6,,Validity Checking}).
10411
10412 @item
10413 Option 'em', 'ec', 'ep', 'l=' and 'R' must be the last options in
10414 a combined list of options.
10415 @end itemize
10416
10417 @node Output and Error Message Control,Warning Message Control,Alphabetical List of All Switches,Compiler Switches
10418 @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}
10419 @subsection Output and Error Message Control
10420
10421
10422 @geindex stderr
10423
10424 The standard default format for error messages is called 'brief format'.
10425 Brief format messages are written to @code{stderr} (the standard error
10426 file) and have the following form:
10427
10428 @example
10429 e.adb:3:04: Incorrect spelling of keyword "function"
10430 e.adb:4:20: ";" should be "is"
10431 @end example
10432
10433 The first integer after the file name is the line number in the file,
10434 and the second integer is the column number within the line.
10435 @code{GPS} can parse the error messages
10436 and point to the referenced character.
10437 The following switches provide control over the error message
10438 format:
10439
10440 @geindex -gnatv (gcc)
10441
10442
10443 @table @asis
10444
10445 @item @code{-gnatv}
10446
10447 The @code{v} stands for verbose.
10448 The effect of this setting is to write long-format error
10449 messages to @code{stdout} (the standard output file.
10450 The same program compiled with the
10451 @code{-gnatv} switch would generate:
10452
10453 @example
10454 3. funcion X (Q : Integer)
10455 |
10456 >>> Incorrect spelling of keyword "function"
10457 4. return Integer;
10458 |
10459 >>> ";" should be "is"
10460 @end example
10461
10462 The vertical bar indicates the location of the error, and the @code{>>>}
10463 prefix can be used to search for error messages. When this switch is
10464 used the only source lines output are those with errors.
10465 @end table
10466
10467 @geindex -gnatl (gcc)
10468
10469
10470 @table @asis
10471
10472 @item @code{-gnatl}
10473
10474 The @code{l} stands for list.
10475 This switch causes a full listing of
10476 the file to be generated. In the case where a body is
10477 compiled, the corresponding spec is also listed, along
10478 with any subunits. Typical output from compiling a package
10479 body @code{p.adb} might look like:
10480
10481 @example
10482 Compiling: p.adb
10483
10484 1. package body p is
10485 2. procedure a;
10486 3. procedure a is separate;
10487 4. begin
10488 5. null
10489 |
10490 >>> missing ";"
10491
10492 6. end;
10493
10494 Compiling: p.ads
10495
10496 1. package p is
10497 2. pragma Elaborate_Body
10498 |
10499 >>> missing ";"
10500
10501 3. end p;
10502
10503 Compiling: p-a.adb
10504
10505 1. separate p
10506 |
10507 >>> missing "("
10508
10509 2. procedure a is
10510 3. begin
10511 4. null
10512 |
10513 >>> missing ";"
10514
10515 5. end;
10516 @end example
10517
10518 When you specify the @code{-gnatv} or @code{-gnatl} switches and
10519 standard output is redirected, a brief summary is written to
10520 @code{stderr} (standard error) giving the number of error messages and
10521 warning messages generated.
10522 @end table
10523
10524 @geindex -gnatl=fname (gcc)
10525
10526
10527 @table @asis
10528
10529 @item @code{-gnatl=@emph{fname}}
10530
10531 This has the same effect as @code{-gnatl} except that the output is
10532 written to a file instead of to standard output. If the given name
10533 @code{fname} does not start with a period, then it is the full name
10534 of the file to be written. If @code{fname} is an extension, it is
10535 appended to the name of the file being compiled. For example, if
10536 file @code{xyz.adb} is compiled with @code{-gnatl=.lst},
10537 then the output is written to file xyz.adb.lst.
10538 @end table
10539
10540 @geindex -gnatU (gcc)
10541
10542
10543 @table @asis
10544
10545 @item @code{-gnatU}
10546
10547 This switch forces all error messages to be preceded by the unique
10548 string 'error:'. This means that error messages take a few more
10549 characters in space, but allows easy searching for and identification
10550 of error messages.
10551 @end table
10552
10553 @geindex -gnatb (gcc)
10554
10555
10556 @table @asis
10557
10558 @item @code{-gnatb}
10559
10560 The @code{b} stands for brief.
10561 This switch causes GNAT to generate the
10562 brief format error messages to @code{stderr} (the standard error
10563 file) as well as the verbose
10564 format message or full listing (which as usual is written to
10565 @code{stdout} (the standard output file).
10566 @end table
10567
10568 @geindex -gnatm (gcc)
10569
10570
10571 @table @asis
10572
10573 @item @code{-gnatm=@emph{n}}
10574
10575 The @code{m} stands for maximum.
10576 @code{n} is a decimal integer in the
10577 range of 1 to 999999 and limits the number of error or warning
10578 messages to be generated. For example, using
10579 @code{-gnatm2} might yield
10580
10581 @example
10582 e.adb:3:04: Incorrect spelling of keyword "function"
10583 e.adb:5:35: missing ".."
10584 fatal error: maximum number of errors detected
10585 compilation abandoned
10586 @end example
10587
10588 The default setting if
10589 no switch is given is 9999. If the number of warnings reaches this
10590 limit, then a message is output and further warnings are suppressed,
10591 but the compilation is continued. If the number of error messages
10592 reaches this limit, then a message is output and the compilation
10593 is abandoned. A value of zero means that no limit applies.
10594
10595 Note that the equal sign is optional, so the switches
10596 @code{-gnatm2} and @code{-gnatm=2} are equivalent.
10597 @end table
10598
10599 @geindex -gnatf (gcc)
10600
10601
10602 @table @asis
10603
10604 @item @code{-gnatf}
10605
10606 @geindex Error messages
10607 @geindex suppressing
10608
10609 The @code{f} stands for full.
10610 Normally, the compiler suppresses error messages that are likely to be
10611 redundant. This switch causes all error
10612 messages to be generated. In particular, in the case of
10613 references to undefined variables. If a given variable is referenced
10614 several times, the normal format of messages is
10615
10616 @example
10617 e.adb:7:07: "V" is undefined (more references follow)
10618 @end example
10619
10620 where the parenthetical comment warns that there are additional
10621 references to the variable @code{V}. Compiling the same program with the
10622 @code{-gnatf} switch yields
10623
10624 @example
10625 e.adb:7:07: "V" is undefined
10626 e.adb:8:07: "V" is undefined
10627 e.adb:8:12: "V" is undefined
10628 e.adb:8:16: "V" is undefined
10629 e.adb:9:07: "V" is undefined
10630 e.adb:9:12: "V" is undefined
10631 @end example
10632
10633 The @code{-gnatf} switch also generates additional information for
10634 some error messages. Some examples are:
10635
10636
10637 @itemize *
10638
10639 @item
10640 Details on possibly non-portable unchecked conversion
10641
10642 @item
10643 List possible interpretations for ambiguous calls
10644
10645 @item
10646 Additional details on incorrect parameters
10647 @end itemize
10648 @end table
10649
10650 @geindex -gnatjnn (gcc)
10651
10652
10653 @table @asis
10654
10655 @item @code{-gnatjnn}
10656
10657 In normal operation mode (or if @code{-gnatj0} is used), then error messages
10658 with continuation lines are treated as though the continuation lines were
10659 separate messages (and so a warning with two continuation lines counts as
10660 three warnings, and is listed as three separate messages).
10661
10662 If the @code{-gnatjnn} switch is used with a positive value for nn, then
10663 messages are output in a different manner. A message and all its continuation
10664 lines are treated as a unit, and count as only one warning or message in the
10665 statistics totals. Furthermore, the message is reformatted so that no line
10666 is longer than nn characters.
10667 @end table
10668
10669 @geindex -gnatq (gcc)
10670
10671
10672 @table @asis
10673
10674 @item @code{-gnatq}
10675
10676 The @code{q} stands for quit (really 'don't quit').
10677 In normal operation mode, the compiler first parses the program and
10678 determines if there are any syntax errors. If there are, appropriate
10679 error messages are generated and compilation is immediately terminated.
10680 This switch tells
10681 GNAT to continue with semantic analysis even if syntax errors have been
10682 found. This may enable the detection of more errors in a single run. On
10683 the other hand, the semantic analyzer is more likely to encounter some
10684 internal fatal error when given a syntactically invalid tree.
10685 @end table
10686
10687 @geindex -gnatQ (gcc)
10688
10689
10690 @table @asis
10691
10692 @item @code{-gnatQ}
10693
10694 In normal operation mode, the @code{ALI} file is not generated if any
10695 illegalities are detected in the program. The use of @code{-gnatQ} forces
10696 generation of the @code{ALI} file. This file is marked as being in
10697 error, so it cannot be used for binding purposes, but it does contain
10698 reasonably complete cross-reference information, and thus may be useful
10699 for use by tools (e.g., semantic browsing tools or integrated development
10700 environments) that are driven from the @code{ALI} file. This switch
10701 implies @code{-gnatq}, since the semantic phase must be run to get a
10702 meaningful ALI file.
10703
10704 In addition, if @code{-gnatt} is also specified, then the tree file is
10705 generated even if there are illegalities. It may be useful in this case
10706 to also specify @code{-gnatq} to ensure that full semantic processing
10707 occurs. The resulting tree file can be processed by ASIS, for the purpose
10708 of providing partial information about illegal units, but if the error
10709 causes the tree to be badly malformed, then ASIS may crash during the
10710 analysis.
10711
10712 When @code{-gnatQ} is used and the generated @code{ALI} file is marked as
10713 being in error, @code{gnatmake} will attempt to recompile the source when it
10714 finds such an @code{ALI} file, including with switch @code{-gnatc}.
10715
10716 Note that @code{-gnatQ} has no effect if @code{-gnats} is specified,
10717 since ALI files are never generated if @code{-gnats} is set.
10718 @end table
10719
10720 @node Warning Message Control,Debugging and Assertion Control,Output and Error Message Control,Compiler Switches
10721 @anchor{gnat_ugn/building_executable_programs_with_gnat warning-message-control}@anchor{fa}@anchor{gnat_ugn/building_executable_programs_with_gnat id15}@anchor{ff}
10722 @subsection Warning Message Control
10723
10724
10725 @geindex Warning messages
10726
10727 In addition to error messages, which correspond to illegalities as defined
10728 in the Ada Reference Manual, the compiler detects two kinds of warning
10729 situations.
10730
10731 First, the compiler considers some constructs suspicious and generates a
10732 warning message to alert you to a possible error. Second, if the
10733 compiler detects a situation that is sure to raise an exception at
10734 run time, it generates a warning message. The following shows an example
10735 of warning messages:
10736
10737 @example
10738 e.adb:4:24: warning: creation of object may raise Storage_Error
10739 e.adb:10:17: warning: static value out of range
10740 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
10741 @end example
10742
10743 GNAT considers a large number of situations as appropriate
10744 for the generation of warning messages. As always, warnings are not
10745 definite indications of errors. For example, if you do an out-of-range
10746 assignment with the deliberate intention of raising a
10747 @code{Constraint_Error} exception, then the warning that may be
10748 issued does not indicate an error. Some of the situations for which GNAT
10749 issues warnings (at least some of the time) are given in the following
10750 list. This list is not complete, and new warnings are often added to
10751 subsequent versions of GNAT. The list is intended to give a general idea
10752 of the kinds of warnings that are generated.
10753
10754
10755 @itemize *
10756
10757 @item
10758 Possible infinitely recursive calls
10759
10760 @item
10761 Out-of-range values being assigned
10762
10763 @item
10764 Possible order of elaboration problems
10765
10766 @item
10767 Size not a multiple of alignment for a record type
10768
10769 @item
10770 Assertions (pragma Assert) that are sure to fail
10771
10772 @item
10773 Unreachable code
10774
10775 @item
10776 Address clauses with possibly unaligned values, or where an attempt is
10777 made to overlay a smaller variable with a larger one.
10778
10779 @item
10780 Fixed-point type declarations with a null range
10781
10782 @item
10783 Direct_IO or Sequential_IO instantiated with a type that has access values
10784
10785 @item
10786 Variables that are never assigned a value
10787
10788 @item
10789 Variables that are referenced before being initialized
10790
10791 @item
10792 Task entries with no corresponding @code{accept} statement
10793
10794 @item
10795 Duplicate accepts for the same task entry in a @code{select}
10796
10797 @item
10798 Objects that take too much storage
10799
10800 @item
10801 Unchecked conversion between types of differing sizes
10802
10803 @item
10804 Missing @code{return} statement along some execution path in a function
10805
10806 @item
10807 Incorrect (unrecognized) pragmas
10808
10809 @item
10810 Incorrect external names
10811
10812 @item
10813 Allocation from empty storage pool
10814
10815 @item
10816 Potentially blocking operation in protected type
10817
10818 @item
10819 Suspicious parenthesization of expressions
10820
10821 @item
10822 Mismatching bounds in an aggregate
10823
10824 @item
10825 Attempt to return local value by reference
10826
10827 @item
10828 Premature instantiation of a generic body
10829
10830 @item
10831 Attempt to pack aliased components
10832
10833 @item
10834 Out of bounds array subscripts
10835
10836 @item
10837 Wrong length on string assignment
10838
10839 @item
10840 Violations of style rules if style checking is enabled
10841
10842 @item
10843 Unused @emph{with} clauses
10844
10845 @item
10846 @code{Bit_Order} usage that does not have any effect
10847
10848 @item
10849 @code{Standard.Duration} used to resolve universal fixed expression
10850
10851 @item
10852 Dereference of possibly null value
10853
10854 @item
10855 Declaration that is likely to cause storage error
10856
10857 @item
10858 Internal GNAT unit @emph{with}ed by application unit
10859
10860 @item
10861 Values known to be out of range at compile time
10862
10863 @item
10864 Unreferenced or unmodified variables. Note that a special
10865 exemption applies to variables which contain any of the substrings
10866 @code{DISCARD, DUMMY, IGNORE, JUNK, UNUSED}, in any casing. Such variables
10867 are considered likely to be intentionally used in a situation where
10868 otherwise a warning would be given, so warnings of this kind are
10869 always suppressed for such variables.
10870
10871 @item
10872 Address overlays that could clobber memory
10873
10874 @item
10875 Unexpected initialization when address clause present
10876
10877 @item
10878 Bad alignment for address clause
10879
10880 @item
10881 Useless type conversions
10882
10883 @item
10884 Redundant assignment statements and other redundant constructs
10885
10886 @item
10887 Useless exception handlers
10888
10889 @item
10890 Accidental hiding of name by child unit
10891
10892 @item
10893 Access before elaboration detected at compile time
10894
10895 @item
10896 A range in a @code{for} loop that is known to be null or might be null
10897 @end itemize
10898
10899 The following section lists compiler switches that are available
10900 to control the handling of warning messages. It is also possible
10901 to exercise much finer control over what warnings are issued and
10902 suppressed using the GNAT pragma Warnings (see the description
10903 of the pragma in the @cite{GNAT_Reference_manual}).
10904
10905 @geindex -gnatwa (gcc)
10906
10907
10908 @table @asis
10909
10910 @item @code{-gnatwa}
10911
10912 @emph{Activate most optional warnings.}
10913
10914 This switch activates most optional warning messages. See the remaining list
10915 in this section for details on optional warning messages that can be
10916 individually controlled. The warnings that are not turned on by this
10917 switch are:
10918
10919
10920 @itemize *
10921
10922 @item
10923 @code{-gnatwd} (implicit dereferencing)
10924
10925 @item
10926 @code{-gnatw.d} (tag warnings with -gnatw switch)
10927
10928 @item
10929 @code{-gnatwh} (hiding)
10930
10931 @item
10932 @code{-gnatw.h} (holes in record layouts)
10933
10934 @item
10935 @code{-gnatw.j} (late primitives of tagged types)
10936
10937 @item
10938 @code{-gnatw.k} (redefinition of names in standard)
10939
10940 @item
10941 @code{-gnatwl} (elaboration warnings)
10942
10943 @item
10944 @code{-gnatw.l} (inherited aspects)
10945
10946 @item
10947 @code{-gnatw.n} (atomic synchronization)
10948
10949 @item
10950 @code{-gnatwo} (address clause overlay)
10951
10952 @item
10953 @code{-gnatw.o} (values set by out parameters ignored)
10954
10955 @item
10956 @code{-gnatw.q} (questionable layout of record types)
10957
10958 @item
10959 @code{-gnatw.s} (overridden size clause)
10960
10961 @item
10962 @code{-gnatwt} (tracking of deleted conditional code)
10963
10964 @item
10965 @code{-gnatw.u} (unordered enumeration)
10966
10967 @item
10968 @code{-gnatw.w} (use of Warnings Off)
10969
10970 @item
10971 @code{-gnatw.y} (reasons for package needing body)
10972 @end itemize
10973
10974 All other optional warnings are turned on.
10975 @end table
10976
10977 @geindex -gnatwA (gcc)
10978
10979
10980 @table @asis
10981
10982 @item @code{-gnatwA}
10983
10984 @emph{Suppress all optional errors.}
10985
10986 This switch suppresses all optional warning messages, see remaining list
10987 in this section for details on optional warning messages that can be
10988 individually controlled. Note that unlike switch @code{-gnatws}, the
10989 use of switch @code{-gnatwA} does not suppress warnings that are
10990 normally given unconditionally and cannot be individually controlled
10991 (for example, the warning about a missing exit path in a function).
10992 Also, again unlike switch @code{-gnatws}, warnings suppressed by
10993 the use of switch @code{-gnatwA} can be individually turned back
10994 on. For example the use of switch @code{-gnatwA} followed by
10995 switch @code{-gnatwd} will suppress all optional warnings except
10996 the warnings for implicit dereferencing.
10997 @end table
10998
10999 @geindex -gnatw.a (gcc)
11000
11001
11002 @table @asis
11003
11004 @item @code{-gnatw.a}
11005
11006 @emph{Activate warnings on failing assertions.}
11007
11008 @geindex Assert failures
11009
11010 This switch activates warnings for assertions where the compiler can tell at
11011 compile time that the assertion will fail. Note that this warning is given
11012 even if assertions are disabled. The default is that such warnings are
11013 generated.
11014 @end table
11015
11016 @geindex -gnatw.A (gcc)
11017
11018
11019 @table @asis
11020
11021 @item @code{-gnatw.A}
11022
11023 @emph{Suppress warnings on failing assertions.}
11024
11025 @geindex Assert failures
11026
11027 This switch suppresses warnings for assertions where the compiler can tell at
11028 compile time that the assertion will fail.
11029 @end table
11030
11031 @geindex -gnatwb (gcc)
11032
11033
11034 @table @asis
11035
11036 @item @code{-gnatwb}
11037
11038 @emph{Activate warnings on bad fixed values.}
11039
11040 @geindex Bad fixed values
11041
11042 @geindex Fixed-point Small value
11043
11044 @geindex Small value
11045
11046 This switch activates warnings for static fixed-point expressions whose
11047 value is not an exact multiple of Small. Such values are implementation
11048 dependent, since an implementation is free to choose either of the multiples
11049 that surround the value. GNAT always chooses the closer one, but this is not
11050 required behavior, and it is better to specify a value that is an exact
11051 multiple, ensuring predictable execution. The default is that such warnings
11052 are not generated.
11053 @end table
11054
11055 @geindex -gnatwB (gcc)
11056
11057
11058 @table @asis
11059
11060 @item @code{-gnatwB}
11061
11062 @emph{Suppress warnings on bad fixed values.}
11063
11064 This switch suppresses warnings for static fixed-point expressions whose
11065 value is not an exact multiple of Small.
11066 @end table
11067
11068 @geindex -gnatw.b (gcc)
11069
11070
11071 @table @asis
11072
11073 @item @code{-gnatw.b}
11074
11075 @emph{Activate warnings on biased representation.}
11076
11077 @geindex Biased representation
11078
11079 This switch activates warnings when a size clause, value size clause, component
11080 clause, or component size clause forces the use of biased representation for an
11081 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
11082 to represent 10/11). The default is that such warnings are generated.
11083 @end table
11084
11085 @geindex -gnatwB (gcc)
11086
11087
11088 @table @asis
11089
11090 @item @code{-gnatw.B}
11091
11092 @emph{Suppress warnings on biased representation.}
11093
11094 This switch suppresses warnings for representation clauses that force the use
11095 of biased representation.
11096 @end table
11097
11098 @geindex -gnatwc (gcc)
11099
11100
11101 @table @asis
11102
11103 @item @code{-gnatwc}
11104
11105 @emph{Activate warnings on conditionals.}
11106
11107 @geindex Conditionals
11108 @geindex constant
11109
11110 This switch activates warnings for conditional expressions used in
11111 tests that are known to be True or False at compile time. The default
11112 is that such warnings are not generated.
11113 Note that this warning does
11114 not get issued for the use of boolean variables or constants whose
11115 values are known at compile time, since this is a standard technique
11116 for conditional compilation in Ada, and this would generate too many
11117 false positive warnings.
11118
11119 This warning option also activates a special test for comparisons using
11120 the operators '>=' and' <='.
11121 If the compiler can tell that only the equality condition is possible,
11122 then it will warn that the '>' or '<' part of the test
11123 is useless and that the operator could be replaced by '='.
11124 An example would be comparing a @code{Natural} variable <= 0.
11125
11126 This warning option also generates warnings if
11127 one or both tests is optimized away in a membership test for integer
11128 values if the result can be determined at compile time. Range tests on
11129 enumeration types are not included, since it is common for such tests
11130 to include an end point.
11131
11132 This warning can also be turned on using @code{-gnatwa}.
11133 @end table
11134
11135 @geindex -gnatwC (gcc)
11136
11137
11138 @table @asis
11139
11140 @item @code{-gnatwC}
11141
11142 @emph{Suppress warnings on conditionals.}
11143
11144 This switch suppresses warnings for conditional expressions used in
11145 tests that are known to be True or False at compile time.
11146 @end table
11147
11148 @geindex -gnatw.c (gcc)
11149
11150
11151 @table @asis
11152
11153 @item @code{-gnatw.c}
11154
11155 @emph{Activate warnings on missing component clauses.}
11156
11157 @geindex Component clause
11158 @geindex missing
11159
11160 This switch activates warnings for record components where a record
11161 representation clause is present and has component clauses for the
11162 majority, but not all, of the components. A warning is given for each
11163 component for which no component clause is present.
11164 @end table
11165
11166 @geindex -gnatwC (gcc)
11167
11168
11169 @table @asis
11170
11171 @item @code{-gnatw.C}
11172
11173 @emph{Suppress warnings on missing component clauses.}
11174
11175 This switch suppresses warnings for record components that are
11176 missing a component clause in the situation described above.
11177 @end table
11178
11179 @geindex -gnatwd (gcc)
11180
11181
11182 @table @asis
11183
11184 @item @code{-gnatwd}
11185
11186 @emph{Activate warnings on implicit dereferencing.}
11187
11188 If this switch is set, then the use of a prefix of an access type
11189 in an indexed component, slice, or selected component without an
11190 explicit @code{.all} will generate a warning. With this warning
11191 enabled, access checks occur only at points where an explicit
11192 @code{.all} appears in the source code (assuming no warnings are
11193 generated as a result of this switch). The default is that such
11194 warnings are not generated.
11195 @end table
11196
11197 @geindex -gnatwD (gcc)
11198
11199
11200 @table @asis
11201
11202 @item @code{-gnatwD}
11203
11204 @emph{Suppress warnings on implicit dereferencing.}
11205
11206 @geindex Implicit dereferencing
11207
11208 @geindex Dereferencing
11209 @geindex implicit
11210
11211 This switch suppresses warnings for implicit dereferences in
11212 indexed components, slices, and selected components.
11213 @end table
11214
11215 @geindex -gnatw.d (gcc)
11216
11217
11218 @table @asis
11219
11220 @item @code{-gnatw.d}
11221
11222 @emph{Activate tagging of warning and info messages.}
11223
11224 If this switch is set, then warning messages are tagged, with one of the
11225 following strings:
11226
11227 @quotation
11228
11229
11230 @itemize -
11231
11232 @item
11233 @emph{[-gnatw?]}
11234 Used to tag warnings controlled by the switch @code{-gnatwx} where x
11235 is a letter a-z.
11236
11237 @item
11238 @emph{[-gnatw.?]}
11239 Used to tag warnings controlled by the switch @code{-gnatw.x} where x
11240 is a letter a-z.
11241
11242 @item
11243 @emph{[-gnatel]}
11244 Used to tag elaboration information (info) messages generated when the
11245 static model of elaboration is used and the @code{-gnatel} switch is set.
11246
11247 @item
11248 @emph{[restriction warning]}
11249 Used to tag warning messages for restriction violations, activated by use
11250 of the pragma @code{Restriction_Warnings}.
11251
11252 @item
11253 @emph{[warning-as-error]}
11254 Used to tag warning messages that have been converted to error messages by
11255 use of the pragma Warning_As_Error. Note that such warnings are prefixed by
11256 the string "error: " rather than "warning: ".
11257
11258 @item
11259 @emph{[enabled by default]}
11260 Used to tag all other warnings that are always given by default, unless
11261 warnings are completely suppressed using pragma @emph{Warnings(Off)} or
11262 the switch @code{-gnatws}.
11263 @end itemize
11264 @end quotation
11265 @end table
11266
11267 @geindex -gnatw.d (gcc)
11268
11269
11270 @table @asis
11271
11272 @item @code{-gnatw.D}
11273
11274 @emph{Deactivate tagging of warning and info messages messages.}
11275
11276 If this switch is set, then warning messages return to the default
11277 mode in which warnings and info messages are not tagged as described above for
11278 @code{-gnatw.d}.
11279 @end table
11280
11281 @geindex -gnatwe (gcc)
11282
11283 @geindex Warnings
11284 @geindex treat as error
11285
11286
11287 @table @asis
11288
11289 @item @code{-gnatwe}
11290
11291 @emph{Treat warnings and style checks as errors.}
11292
11293 This switch causes warning messages and style check messages to be
11294 treated as errors.
11295 The warning string still appears, but the warning messages are counted
11296 as errors, and prevent the generation of an object file. Note that this
11297 is the only -gnatw switch that affects the handling of style check messages.
11298 Note also that this switch has no effect on info (information) messages, which
11299 are not treated as errors if this switch is present.
11300 @end table
11301
11302 @geindex -gnatw.e (gcc)
11303
11304
11305 @table @asis
11306
11307 @item @code{-gnatw.e}
11308
11309 @emph{Activate every optional warning.}
11310
11311 @geindex Warnings
11312 @geindex activate every optional warning
11313
11314 This switch activates all optional warnings, including those which
11315 are not activated by @code{-gnatwa}. The use of this switch is not
11316 recommended for normal use. If you turn this switch on, it is almost
11317 certain that you will get large numbers of useless warnings. The
11318 warnings that are excluded from @code{-gnatwa} are typically highly
11319 specialized warnings that are suitable for use only in code that has
11320 been specifically designed according to specialized coding rules.
11321 @end table
11322
11323 @geindex -gnatwE (gcc)
11324
11325 @geindex Warnings
11326 @geindex treat as error
11327
11328
11329 @table @asis
11330
11331 @item @code{-gnatwE}
11332
11333 @emph{Treat all run-time exception warnings as errors.}
11334
11335 This switch causes warning messages regarding errors that will be raised
11336 during run-time execution to be treated as errors.
11337 @end table
11338
11339 @geindex -gnatwf (gcc)
11340
11341
11342 @table @asis
11343
11344 @item @code{-gnatwf}
11345
11346 @emph{Activate warnings on unreferenced formals.}
11347
11348 @geindex Formals
11349 @geindex unreferenced
11350
11351 This switch causes a warning to be generated if a formal parameter
11352 is not referenced in the body of the subprogram. This warning can
11353 also be turned on using @code{-gnatwu}. The
11354 default is that these warnings are not generated.
11355 @end table
11356
11357 @geindex -gnatwF (gcc)
11358
11359
11360 @table @asis
11361
11362 @item @code{-gnatwF}
11363
11364 @emph{Suppress warnings on unreferenced formals.}
11365
11366 This switch suppresses warnings for unreferenced formal
11367 parameters. Note that the
11368 combination @code{-gnatwu} followed by @code{-gnatwF} has the
11369 effect of warning on unreferenced entities other than subprogram
11370 formals.
11371 @end table
11372
11373 @geindex -gnatwg (gcc)
11374
11375
11376 @table @asis
11377
11378 @item @code{-gnatwg}
11379
11380 @emph{Activate warnings on unrecognized pragmas.}
11381
11382 @geindex Pragmas
11383 @geindex unrecognized
11384
11385 This switch causes a warning to be generated if an unrecognized
11386 pragma is encountered. Apart from issuing this warning, the
11387 pragma is ignored and has no effect. The default
11388 is that such warnings are issued (satisfying the Ada Reference
11389 Manual requirement that such warnings appear).
11390 @end table
11391
11392 @geindex -gnatwG (gcc)
11393
11394
11395 @table @asis
11396
11397 @item @code{-gnatwG}
11398
11399 @emph{Suppress warnings on unrecognized pragmas.}
11400
11401 This switch suppresses warnings for unrecognized pragmas.
11402 @end table
11403
11404 @geindex -gnatw.g (gcc)
11405
11406
11407 @table @asis
11408
11409 @item @code{-gnatw.g}
11410
11411 @emph{Warnings used for GNAT sources.}
11412
11413 This switch sets the warning categories that are used by the standard
11414 GNAT style. Currently this is equivalent to
11415 @code{-gnatwAao.q.s.CI.V.X.Z}
11416 but more warnings may be added in the future without advanced notice.
11417 @end table
11418
11419 @geindex -gnatwh (gcc)
11420
11421
11422 @table @asis
11423
11424 @item @code{-gnatwh}
11425
11426 @emph{Activate warnings on hiding.}
11427
11428 @geindex Hiding of Declarations
11429
11430 This switch activates warnings on hiding declarations that are considered
11431 potentially confusing. Not all cases of hiding cause warnings; for example an
11432 overriding declaration hides an implicit declaration, which is just normal
11433 code. The default is that warnings on hiding are not generated.
11434 @end table
11435
11436 @geindex -gnatwH (gcc)
11437
11438
11439 @table @asis
11440
11441 @item @code{-gnatwH}
11442
11443 @emph{Suppress warnings on hiding.}
11444
11445 This switch suppresses warnings on hiding declarations.
11446 @end table
11447
11448 @geindex -gnatw.h (gcc)
11449
11450
11451 @table @asis
11452
11453 @item @code{-gnatw.h}
11454
11455 @emph{Activate warnings on holes/gaps in records.}
11456
11457 @geindex Record Representation (gaps)
11458
11459 This switch activates warnings on component clauses in record
11460 representation clauses that leave holes (gaps) in the record layout.
11461 If this warning option is active, then record representation clauses
11462 should specify a contiguous layout, adding unused fill fields if needed.
11463 @end table
11464
11465 @geindex -gnatw.H (gcc)
11466
11467
11468 @table @asis
11469
11470 @item @code{-gnatw.H}
11471
11472 @emph{Suppress warnings on holes/gaps in records.}
11473
11474 This switch suppresses warnings on component clauses in record
11475 representation clauses that leave holes (haps) in the record layout.
11476 @end table
11477
11478 @geindex -gnatwi (gcc)
11479
11480
11481 @table @asis
11482
11483 @item @code{-gnatwi}
11484
11485 @emph{Activate warnings on implementation units.}
11486
11487 This switch activates warnings for a @emph{with} of an internal GNAT
11488 implementation unit, defined as any unit from the @code{Ada},
11489 @code{Interfaces}, @code{GNAT},
11490 or @code{System}
11491 hierarchies that is not
11492 documented in either the Ada Reference Manual or the GNAT
11493 Programmer's Reference Manual. Such units are intended only
11494 for internal implementation purposes and should not be @emph{with}ed
11495 by user programs. The default is that such warnings are generated
11496 @end table
11497
11498 @geindex -gnatwI (gcc)
11499
11500
11501 @table @asis
11502
11503 @item @code{-gnatwI}
11504
11505 @emph{Disable warnings on implementation units.}
11506
11507 This switch disables warnings for a @emph{with} of an internal GNAT
11508 implementation unit.
11509 @end table
11510
11511 @geindex -gnatw.i (gcc)
11512
11513
11514 @table @asis
11515
11516 @item @code{-gnatw.i}
11517
11518 @emph{Activate warnings on overlapping actuals.}
11519
11520 This switch enables a warning on statically detectable overlapping actuals in
11521 a subprogram call, when one of the actuals is an in-out parameter, and the
11522 types of the actuals are not by-copy types. This warning is off by default.
11523 @end table
11524
11525 @geindex -gnatw.I (gcc)
11526
11527
11528 @table @asis
11529
11530 @item @code{-gnatw.I}
11531
11532 @emph{Disable warnings on overlapping actuals.}
11533
11534 This switch disables warnings on overlapping actuals in a call..
11535 @end table
11536
11537 @geindex -gnatwj (gcc)
11538
11539
11540 @table @asis
11541
11542 @item @code{-gnatwj}
11543
11544 @emph{Activate warnings on obsolescent features (Annex J).}
11545
11546 @geindex Features
11547 @geindex obsolescent
11548
11549 @geindex Obsolescent features
11550
11551 If this warning option is activated, then warnings are generated for
11552 calls to subprograms marked with @code{pragma Obsolescent} and
11553 for use of features in Annex J of the Ada Reference Manual. In the
11554 case of Annex J, not all features are flagged. In particular use
11555 of the renamed packages (like @code{Text_IO}) and use of package
11556 @code{ASCII} are not flagged, since these are very common and
11557 would generate many annoying positive warnings. The default is that
11558 such warnings are not generated.
11559
11560 In addition to the above cases, warnings are also generated for
11561 GNAT features that have been provided in past versions but which
11562 have been superseded (typically by features in the new Ada standard).
11563 For example, @code{pragma Ravenscar} will be flagged since its
11564 function is replaced by @code{pragma Profile(Ravenscar)}, and
11565 @code{pragma Interface_Name} will be flagged since its function
11566 is replaced by @code{pragma Import}.
11567
11568 Note that this warning option functions differently from the
11569 restriction @code{No_Obsolescent_Features} in two respects.
11570 First, the restriction applies only to annex J features.
11571 Second, the restriction does flag uses of package @code{ASCII}.
11572 @end table
11573
11574 @geindex -gnatwJ (gcc)
11575
11576
11577 @table @asis
11578
11579 @item @code{-gnatwJ}
11580
11581 @emph{Suppress warnings on obsolescent features (Annex J).}
11582
11583 This switch disables warnings on use of obsolescent features.
11584 @end table
11585
11586 @geindex -gnatw.j (gcc)
11587
11588
11589 @table @asis
11590
11591 @item @code{-gnatw.j}
11592
11593 @emph{Activate warnings on late declarations of tagged type primitives.}
11594
11595 This switch activates warnings on visible primitives added to a
11596 tagged type after deriving a private extension from it.
11597 @end table
11598
11599 @geindex -gnatw.J (gcc)
11600
11601
11602 @table @asis
11603
11604 @item @code{-gnatw.J}
11605
11606 @emph{Suppress warnings on late declarations of tagged type primitives.}
11607
11608 This switch suppresses warnings on visible primitives added to a
11609 tagged type after deriving a private extension from it.
11610 @end table
11611
11612 @geindex -gnatwk (gcc)
11613
11614
11615 @table @asis
11616
11617 @item @code{-gnatwk}
11618
11619 @emph{Activate warnings on variables that could be constants.}
11620
11621 This switch activates warnings for variables that are initialized but
11622 never modified, and then could be declared constants. The default is that
11623 such warnings are not given.
11624 @end table
11625
11626 @geindex -gnatwK (gcc)
11627
11628
11629 @table @asis
11630
11631 @item @code{-gnatwK}
11632
11633 @emph{Suppress warnings on variables that could be constants.}
11634
11635 This switch disables warnings on variables that could be declared constants.
11636 @end table
11637
11638 @geindex -gnatw.k (gcc)
11639
11640
11641 @table @asis
11642
11643 @item @code{-gnatw.k}
11644
11645 @emph{Activate warnings on redefinition of names in standard.}
11646
11647 This switch activates warnings for declarations that declare a name that
11648 is defined in package Standard. Such declarations can be confusing,
11649 especially since the names in package Standard continue to be directly
11650 visible, meaning that use visibiliy on such redeclared names does not
11651 work as expected. Names of discriminants and components in records are
11652 not included in this check.
11653 @end table
11654
11655 @geindex -gnatwK (gcc)
11656
11657
11658 @table @asis
11659
11660 @item @code{-gnatw.K}
11661
11662 @emph{Suppress warnings on redefinition of names in standard.}
11663
11664 This switch activates warnings for declarations that declare a name that
11665 is defined in package Standard.
11666 @end table
11667
11668 @geindex -gnatwl (gcc)
11669
11670
11671 @table @asis
11672
11673 @item @code{-gnatwl}
11674
11675 @emph{Activate warnings for elaboration pragmas.}
11676
11677 @geindex Elaboration
11678 @geindex warnings
11679
11680 This switch activates warnings for possible elaboration problems,
11681 including suspicious use
11682 of @code{Elaborate} pragmas, when using the static elaboration model, and
11683 possible situations that may raise @code{Program_Error} when using the
11684 dynamic elaboration model.
11685 See the section in this guide on elaboration checking for further details.
11686 The default is that such warnings
11687 are not generated.
11688 @end table
11689
11690 @geindex -gnatwL (gcc)
11691
11692
11693 @table @asis
11694
11695 @item @code{-gnatwL}
11696
11697 @emph{Suppress warnings for elaboration pragmas.}
11698
11699 This switch suppresses warnings for possible elaboration problems.
11700 @end table
11701
11702 @geindex -gnatw.l (gcc)
11703
11704
11705 @table @asis
11706
11707 @item @code{-gnatw.l}
11708
11709 @emph{List inherited aspects.}
11710
11711 This switch causes the compiler to list inherited invariants,
11712 preconditions, and postconditions from Type_Invariant'Class, Invariant'Class,
11713 Pre'Class, and Post'Class aspects. Also list inherited subtype predicates.
11714 @end table
11715
11716 @geindex -gnatw.L (gcc)
11717
11718
11719 @table @asis
11720
11721 @item @code{-gnatw.L}
11722
11723 @emph{Suppress listing of inherited aspects.}
11724
11725 This switch suppresses listing of inherited aspects.
11726 @end table
11727
11728 @geindex -gnatwm (gcc)
11729
11730
11731 @table @asis
11732
11733 @item @code{-gnatwm}
11734
11735 @emph{Activate warnings on modified but unreferenced variables.}
11736
11737 This switch activates warnings for variables that are assigned (using
11738 an initialization value or with one or more assignment statements) but
11739 whose value is never read. The warning is suppressed for volatile
11740 variables and also for variables that are renamings of other variables
11741 or for which an address clause is given.
11742 The default is that these warnings are not given.
11743 @end table
11744
11745 @geindex -gnatwM (gcc)
11746
11747
11748 @table @asis
11749
11750 @item @code{-gnatwM}
11751
11752 @emph{Disable warnings on modified but unreferenced variables.}
11753
11754 This switch disables warnings for variables that are assigned or
11755 initialized, but never read.
11756 @end table
11757
11758 @geindex -gnatw.m (gcc)
11759
11760
11761 @table @asis
11762
11763 @item @code{-gnatw.m}
11764
11765 @emph{Activate warnings on suspicious modulus values.}
11766
11767 This switch activates warnings for modulus values that seem suspicious.
11768 The cases caught are where the size is the same as the modulus (e.g.
11769 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
11770 with no size clause. The guess in both cases is that 2**x was intended
11771 rather than x. In addition expressions of the form 2*x for small x
11772 generate a warning (the almost certainly accurate guess being that
11773 2**x was intended). The default is that these warnings are given.
11774 @end table
11775
11776 @geindex -gnatw.M (gcc)
11777
11778
11779 @table @asis
11780
11781 @item @code{-gnatw.M}
11782
11783 @emph{Disable warnings on suspicious modulus values.}
11784
11785 This switch disables warnings for suspicious modulus values.
11786 @end table
11787
11788 @geindex -gnatwn (gcc)
11789
11790
11791 @table @asis
11792
11793 @item @code{-gnatwn}
11794
11795 @emph{Set normal warnings mode.}
11796
11797 This switch sets normal warning mode, in which enabled warnings are
11798 issued and treated as warnings rather than errors. This is the default
11799 mode. the switch @code{-gnatwn} can be used to cancel the effect of
11800 an explicit @code{-gnatws} or
11801 @code{-gnatwe}. It also cancels the effect of the
11802 implicit @code{-gnatwe} that is activated by the
11803 use of @code{-gnatg}.
11804 @end table
11805
11806 @geindex -gnatw.n (gcc)
11807
11808 @geindex Atomic Synchronization
11809 @geindex warnings
11810
11811
11812 @table @asis
11813
11814 @item @code{-gnatw.n}
11815
11816 @emph{Activate warnings on atomic synchronization.}
11817
11818 This switch actives warnings when an access to an atomic variable
11819 requires the generation of atomic synchronization code. These
11820 warnings are off by default.
11821 @end table
11822
11823 @geindex -gnatw.N (gcc)
11824
11825
11826 @table @asis
11827
11828 @item @code{-gnatw.N}
11829
11830 @emph{Suppress warnings on atomic synchronization.}
11831
11832 @geindex Atomic Synchronization
11833 @geindex warnings
11834
11835 This switch suppresses warnings when an access to an atomic variable
11836 requires the generation of atomic synchronization code.
11837 @end table
11838
11839 @geindex -gnatwo (gcc)
11840
11841 @geindex Address Clauses
11842 @geindex warnings
11843
11844
11845 @table @asis
11846
11847 @item @code{-gnatwo}
11848
11849 @emph{Activate warnings on address clause overlays.}
11850
11851 This switch activates warnings for possibly unintended initialization
11852 effects of defining address clauses that cause one variable to overlap
11853 another. The default is that such warnings are generated.
11854 @end table
11855
11856 @geindex -gnatwO (gcc)
11857
11858
11859 @table @asis
11860
11861 @item @code{-gnatwO}
11862
11863 @emph{Suppress warnings on address clause overlays.}
11864
11865 This switch suppresses warnings on possibly unintended initialization
11866 effects of defining address clauses that cause one variable to overlap
11867 another.
11868 @end table
11869
11870 @geindex -gnatw.o (gcc)
11871
11872
11873 @table @asis
11874
11875 @item @code{-gnatw.o}
11876
11877 @emph{Activate warnings on modified but unreferenced out parameters.}
11878
11879 This switch activates warnings for variables that are modified by using
11880 them as actuals for a call to a procedure with an out mode formal, where
11881 the resulting assigned value is never read. It is applicable in the case
11882 where there is more than one out mode formal. If there is only one out
11883 mode formal, the warning is issued by default (controlled by -gnatwu).
11884 The warning is suppressed for volatile
11885 variables and also for variables that are renamings of other variables
11886 or for which an address clause is given.
11887 The default is that these warnings are not given.
11888 @end table
11889
11890 @geindex -gnatw.O (gcc)
11891
11892
11893 @table @asis
11894
11895 @item @code{-gnatw.O}
11896
11897 @emph{Disable warnings on modified but unreferenced out parameters.}
11898
11899 This switch suppresses warnings for variables that are modified by using
11900 them as actuals for a call to a procedure with an out mode formal, where
11901 the resulting assigned value is never read.
11902 @end table
11903
11904 @geindex -gnatwp (gcc)
11905
11906 @geindex Inlining
11907 @geindex warnings
11908
11909
11910 @table @asis
11911
11912 @item @code{-gnatwp}
11913
11914 @emph{Activate warnings on ineffective pragma Inlines.}
11915
11916 This switch activates warnings for failure of front end inlining
11917 (activated by @code{-gnatN}) to inline a particular call. There are
11918 many reasons for not being able to inline a call, including most
11919 commonly that the call is too complex to inline. The default is
11920 that such warnings are not given.
11921 Warnings on ineffective inlining by the gcc back-end can be activated
11922 separately, using the gcc switch -Winline.
11923 @end table
11924
11925 @geindex -gnatwP (gcc)
11926
11927
11928 @table @asis
11929
11930 @item @code{-gnatwP}
11931
11932 @emph{Suppress warnings on ineffective pragma Inlines.}
11933
11934 This switch suppresses warnings on ineffective pragma Inlines. If the
11935 inlining mechanism cannot inline a call, it will simply ignore the
11936 request silently.
11937 @end table
11938
11939 @geindex -gnatw.p (gcc)
11940
11941 @geindex Parameter order
11942 @geindex warnings
11943
11944
11945 @table @asis
11946
11947 @item @code{-gnatw.p}
11948
11949 @emph{Activate warnings on parameter ordering.}
11950
11951 This switch activates warnings for cases of suspicious parameter
11952 ordering when the list of arguments are all simple identifiers that
11953 match the names of the formals, but are in a different order. The
11954 warning is suppressed if any use of named parameter notation is used,
11955 so this is the appropriate way to suppress a false positive (and
11956 serves to emphasize that the "misordering" is deliberate). The
11957 default is that such warnings are not given.
11958 @end table
11959
11960 @geindex -gnatw.P (gcc)
11961
11962
11963 @table @asis
11964
11965 @item @code{-gnatw.P}
11966
11967 @emph{Suppress warnings on parameter ordering.}
11968
11969 This switch suppresses warnings on cases of suspicious parameter
11970 ordering.
11971 @end table
11972
11973 @geindex -gnatwq (gcc)
11974
11975 @geindex Parentheses
11976 @geindex warnings
11977
11978
11979 @table @asis
11980
11981 @item @code{-gnatwq}
11982
11983 @emph{Activate warnings on questionable missing parentheses.}
11984
11985 This switch activates warnings for cases where parentheses are not used and
11986 the result is potential ambiguity from a readers point of view. For example
11987 (not a > b) when a and b are modular means ((not a) > b) and very likely the
11988 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
11989 quite likely ((-x) mod 5) was intended. In such situations it seems best to
11990 follow the rule of always parenthesizing to make the association clear, and
11991 this warning switch warns if such parentheses are not present. The default
11992 is that these warnings are given.
11993 @end table
11994
11995 @geindex -gnatwQ (gcc)
11996
11997
11998 @table @asis
11999
12000 @item @code{-gnatwQ}
12001
12002 @emph{Suppress warnings on questionable missing parentheses.}
12003
12004 This switch suppresses warnings for cases where the association is not
12005 clear and the use of parentheses is preferred.
12006 @end table
12007
12008 @geindex -gnatw.q (gcc)
12009
12010 @geindex Layout
12011 @geindex warnings
12012
12013
12014 @table @asis
12015
12016 @item @code{-gnatw.q}
12017
12018 @emph{Activate warnings on questionable layout of record types.}
12019
12020 This switch activates warnings for cases where the default layout of
12021 a record type, that is to say the layout of its components in textual
12022 order of the source code, would very likely cause inefficiencies in
12023 the code generated by the compiler, both in terms of space and speed
12024 during execution. One warning is issued for each problematic component
12025 without representation clause in the nonvariant part and then in each
12026 variant recursively, if any.
12027
12028 The purpose of these warnings is neither to prescribe an optimal layout
12029 nor to force the use of representation clauses, but rather to get rid of
12030 the most blatant inefficiencies in the layout. Therefore, the default
12031 layout is matched against the following synthetic ordered layout and
12032 the deviations are flagged on a component-by-component basis:
12033
12034
12035 @itemize *
12036
12037 @item
12038 first all components or groups of components whose length is fixed
12039 and a multiple of the storage unit,
12040
12041 @item
12042 then the remaining components whose length is fixed and not a multiple
12043 of the storage unit,
12044
12045 @item
12046 then the remaining components whose length doesn't depend on discriminants
12047 (that is to say, with variable but uniform length for all objects),
12048
12049 @item
12050 then all components whose length depends on discriminants,
12051
12052 @item
12053 finally the variant part (if any),
12054 @end itemize
12055
12056 for the nonvariant part and for each variant recursively, if any.
12057
12058 The exact wording of the warning depends on whether the compiler is allowed
12059 to reorder the components in the record type or precluded from doing it by
12060 means of pragma @code{No_Component_Reordering}.
12061
12062 The default is that these warnings are not given.
12063 @end table
12064
12065 @geindex -gnatw.Q (gcc)
12066
12067
12068 @table @asis
12069
12070 @item @code{-gnatw.Q}
12071
12072 @emph{Suppress warnings on questionable layout of record types.}
12073
12074 This switch suppresses warnings for cases where the default layout of
12075 a record type would very likely cause inefficiencies.
12076 @end table
12077
12078 @geindex -gnatwr (gcc)
12079
12080
12081 @table @asis
12082
12083 @item @code{-gnatwr}
12084
12085 @emph{Activate warnings on redundant constructs.}
12086
12087 This switch activates warnings for redundant constructs. The following
12088 is the current list of constructs regarded as redundant:
12089
12090
12091 @itemize *
12092
12093 @item
12094 Assignment of an item to itself.
12095
12096 @item
12097 Type conversion that converts an expression to its own type.
12098
12099 @item
12100 Use of the attribute @code{Base} where @code{typ'Base} is the same
12101 as @code{typ}.
12102
12103 @item
12104 Use of pragma @code{Pack} when all components are placed by a record
12105 representation clause.
12106
12107 @item
12108 Exception handler containing only a reraise statement (raise with no
12109 operand) which has no effect.
12110
12111 @item
12112 Use of the operator abs on an operand that is known at compile time
12113 to be non-negative
12114
12115 @item
12116 Comparison of an object or (unary or binary) operation of boolean type to
12117 an explicit True value.
12118 @end itemize
12119
12120 The default is that warnings for redundant constructs are not given.
12121 @end table
12122
12123 @geindex -gnatwR (gcc)
12124
12125
12126 @table @asis
12127
12128 @item @code{-gnatwR}
12129
12130 @emph{Suppress warnings on redundant constructs.}
12131
12132 This switch suppresses warnings for redundant constructs.
12133 @end table
12134
12135 @geindex -gnatw.r (gcc)
12136
12137
12138 @table @asis
12139
12140 @item @code{-gnatw.r}
12141
12142 @emph{Activate warnings for object renaming function.}
12143
12144 This switch activates warnings for an object renaming that renames a
12145 function call, which is equivalent to a constant declaration (as
12146 opposed to renaming the function itself). The default is that these
12147 warnings are given.
12148 @end table
12149
12150 @geindex -gnatwT (gcc)
12151
12152
12153 @table @asis
12154
12155 @item @code{-gnatw.R}
12156
12157 @emph{Suppress warnings for object renaming function.}
12158
12159 This switch suppresses warnings for object renaming function.
12160 @end table
12161
12162 @geindex -gnatws (gcc)
12163
12164
12165 @table @asis
12166
12167 @item @code{-gnatws}
12168
12169 @emph{Suppress all warnings.}
12170
12171 This switch completely suppresses the
12172 output of all warning messages from the GNAT front end, including
12173 both warnings that can be controlled by switches described in this
12174 section, and those that are normally given unconditionally. The
12175 effect of this suppress action can only be cancelled by a subsequent
12176 use of the switch @code{-gnatwn}.
12177
12178 Note that switch @code{-gnatws} does not suppress
12179 warnings from the @code{gcc} back end.
12180 To suppress these back end warnings as well, use the switch @code{-w}
12181 in addition to @code{-gnatws}. Also this switch has no effect on the
12182 handling of style check messages.
12183 @end table
12184
12185 @geindex -gnatw.s (gcc)
12186
12187 @geindex Record Representation (component sizes)
12188
12189
12190 @table @asis
12191
12192 @item @code{-gnatw.s}
12193
12194 @emph{Activate warnings on overridden size clauses.}
12195
12196 This switch activates warnings on component clauses in record
12197 representation clauses where the length given overrides that
12198 specified by an explicit size clause for the component type. A
12199 warning is similarly given in the array case if a specified
12200 component size overrides an explicit size clause for the array
12201 component type.
12202 @end table
12203
12204 @geindex -gnatw.S (gcc)
12205
12206
12207 @table @asis
12208
12209 @item @code{-gnatw.S}
12210
12211 @emph{Suppress warnings on overridden size clauses.}
12212
12213 This switch suppresses warnings on component clauses in record
12214 representation clauses that override size clauses, and similar
12215 warnings when an array component size overrides a size clause.
12216 @end table
12217
12218 @geindex -gnatwt (gcc)
12219
12220 @geindex Deactivated code
12221 @geindex warnings
12222
12223 @geindex Deleted code
12224 @geindex warnings
12225
12226
12227 @table @asis
12228
12229 @item @code{-gnatwt}
12230
12231 @emph{Activate warnings for tracking of deleted conditional code.}
12232
12233 This switch activates warnings for tracking of code in conditionals (IF and
12234 CASE statements) that is detected to be dead code which cannot be executed, and
12235 which is removed by the front end. This warning is off by default. This may be
12236 useful for detecting deactivated code in certified applications.
12237 @end table
12238
12239 @geindex -gnatwT (gcc)
12240
12241
12242 @table @asis
12243
12244 @item @code{-gnatwT}
12245
12246 @emph{Suppress warnings for tracking of deleted conditional code.}
12247
12248 This switch suppresses warnings for tracking of deleted conditional code.
12249 @end table
12250
12251 @geindex -gnatw.t (gcc)
12252
12253
12254 @table @asis
12255
12256 @item @code{-gnatw.t}
12257
12258 @emph{Activate warnings on suspicious contracts.}
12259
12260 This switch activates warnings on suspicious contracts. This includes
12261 warnings on suspicious postconditions (whether a pragma @code{Postcondition} or a
12262 @code{Post} aspect in Ada 2012) and suspicious contract cases (pragma or aspect
12263 @code{Contract_Cases}). A function postcondition or contract case is suspicious
12264 when no postcondition or contract case for this function mentions the result
12265 of the function. A procedure postcondition or contract case is suspicious
12266 when it only refers to the pre-state of the procedure, because in that case
12267 it should rather be expressed as a precondition. This switch also controls
12268 warnings on suspicious cases of expressions typically found in contracts like
12269 quantified expressions and uses of Update attribute. The default is that such
12270 warnings are generated.
12271 @end table
12272
12273 @geindex -gnatw.T (gcc)
12274
12275
12276 @table @asis
12277
12278 @item @code{-gnatw.T}
12279
12280 @emph{Suppress warnings on suspicious contracts.}
12281
12282 This switch suppresses warnings on suspicious contracts.
12283 @end table
12284
12285 @geindex -gnatwu (gcc)
12286
12287
12288 @table @asis
12289
12290 @item @code{-gnatwu}
12291
12292 @emph{Activate warnings on unused entities.}
12293
12294 This switch activates warnings to be generated for entities that
12295 are declared but not referenced, and for units that are @emph{with}ed
12296 and not
12297 referenced. In the case of packages, a warning is also generated if
12298 no entities in the package are referenced. This means that if a with'ed
12299 package is referenced but the only references are in @code{use}
12300 clauses or @code{renames}
12301 declarations, a warning is still generated. A warning is also generated
12302 for a generic package that is @emph{with}ed but never instantiated.
12303 In the case where a package or subprogram body is compiled, and there
12304 is a @emph{with} on the corresponding spec
12305 that is only referenced in the body,
12306 a warning is also generated, noting that the
12307 @emph{with} can be moved to the body. The default is that
12308 such warnings are not generated.
12309 This switch also activates warnings on unreferenced formals
12310 (it includes the effect of @code{-gnatwf}).
12311 @end table
12312
12313 @geindex -gnatwU (gcc)
12314
12315
12316 @table @asis
12317
12318 @item @code{-gnatwU}
12319
12320 @emph{Suppress warnings on unused entities.}
12321
12322 This switch suppresses warnings for unused entities and packages.
12323 It also turns off warnings on unreferenced formals (and thus includes
12324 the effect of @code{-gnatwF}).
12325 @end table
12326
12327 @geindex -gnatw.u (gcc)
12328
12329
12330 @table @asis
12331
12332 @item @code{-gnatw.u}
12333
12334 @emph{Activate warnings on unordered enumeration types.}
12335
12336 This switch causes enumeration types to be considered as conceptually
12337 unordered, unless an explicit pragma @code{Ordered} is given for the type.
12338 The effect is to generate warnings in clients that use explicit comparisons
12339 or subranges, since these constructs both treat objects of the type as
12340 ordered. (A @emph{client} is defined as a unit that is other than the unit in
12341 which the type is declared, or its body or subunits.) Please refer to
12342 the description of pragma @code{Ordered} in the
12343 @cite{GNAT Reference Manual} for further details.
12344 The default is that such warnings are not generated.
12345 @end table
12346
12347 @geindex -gnatw.U (gcc)
12348
12349
12350 @table @asis
12351
12352 @item @code{-gnatw.U}
12353
12354 @emph{Deactivate warnings on unordered enumeration types.}
12355
12356 This switch causes all enumeration types to be considered as ordered, so
12357 that no warnings are given for comparisons or subranges for any type.
12358 @end table
12359
12360 @geindex -gnatwv (gcc)
12361
12362 @geindex Unassigned variable warnings
12363
12364
12365 @table @asis
12366
12367 @item @code{-gnatwv}
12368
12369 @emph{Activate warnings on unassigned variables.}
12370
12371 This switch activates warnings for access to variables which
12372 may not be properly initialized. The default is that
12373 such warnings are generated.
12374 @end table
12375
12376 @geindex -gnatwV (gcc)
12377
12378
12379 @table @asis
12380
12381 @item @code{-gnatwV}
12382
12383 @emph{Suppress warnings on unassigned variables.}
12384
12385 This switch suppresses warnings for access to variables which
12386 may not be properly initialized.
12387 For variables of a composite type, the warning can also be suppressed in
12388 Ada 2005 by using a default initialization with a box. For example, if
12389 Table is an array of records whose components are only partially uninitialized,
12390 then the following code:
12391
12392 @example
12393 Tab : Table := (others => <>);
12394 @end example
12395
12396 will suppress warnings on subsequent statements that access components
12397 of variable Tab.
12398 @end table
12399
12400 @geindex -gnatw.v (gcc)
12401
12402 @geindex bit order warnings
12403
12404
12405 @table @asis
12406
12407 @item @code{-gnatw.v}
12408
12409 @emph{Activate info messages for non-default bit order.}
12410
12411 This switch activates messages (labeled "info", they are not warnings,
12412 just informational messages) about the effects of non-default bit-order
12413 on records to which a component clause is applied. The effect of specifying
12414 non-default bit ordering is a bit subtle (and changed with Ada 2005), so
12415 these messages, which are given by default, are useful in understanding the
12416 exact consequences of using this feature.
12417 @end table
12418
12419 @geindex -gnatw.V (gcc)
12420
12421
12422 @table @asis
12423
12424 @item @code{-gnatw.V}
12425
12426 @emph{Suppress info messages for non-default bit order.}
12427
12428 This switch suppresses information messages for the effects of specifying
12429 non-default bit order on record components with component clauses.
12430 @end table
12431
12432 @geindex -gnatww (gcc)
12433
12434 @geindex String indexing warnings
12435
12436
12437 @table @asis
12438
12439 @item @code{-gnatww}
12440
12441 @emph{Activate warnings on wrong low bound assumption.}
12442
12443 This switch activates warnings for indexing an unconstrained string parameter
12444 with a literal or S'Length. This is a case where the code is assuming that the
12445 low bound is one, which is in general not true (for example when a slice is
12446 passed). The default is that such warnings are generated.
12447 @end table
12448
12449 @geindex -gnatwW (gcc)
12450
12451
12452 @table @asis
12453
12454 @item @code{-gnatwW}
12455
12456 @emph{Suppress warnings on wrong low bound assumption.}
12457
12458 This switch suppresses warnings for indexing an unconstrained string parameter
12459 with a literal or S'Length. Note that this warning can also be suppressed
12460 in a particular case by adding an assertion that the lower bound is 1,
12461 as shown in the following example:
12462
12463 @example
12464 procedure K (S : String) is
12465 pragma Assert (S'First = 1);
12466 ...
12467 @end example
12468 @end table
12469
12470 @geindex -gnatw.w (gcc)
12471
12472 @geindex Warnings Off control
12473
12474
12475 @table @asis
12476
12477 @item @code{-gnatw.w}
12478
12479 @emph{Activate warnings on Warnings Off pragmas.}
12480
12481 This switch activates warnings for use of @code{pragma Warnings (Off, entity)}
12482 where either the pragma is entirely useless (because it suppresses no
12483 warnings), or it could be replaced by @code{pragma Unreferenced} or
12484 @code{pragma Unmodified}.
12485 Also activates warnings for the case of
12486 Warnings (Off, String), where either there is no matching
12487 Warnings (On, String), or the Warnings (Off) did not suppress any warning.
12488 The default is that these warnings are not given.
12489 @end table
12490
12491 @geindex -gnatw.W (gcc)
12492
12493
12494 @table @asis
12495
12496 @item @code{-gnatw.W}
12497
12498 @emph{Suppress warnings on unnecessary Warnings Off pragmas.}
12499
12500 This switch suppresses warnings for use of @code{pragma Warnings (Off, ...)}.
12501 @end table
12502
12503 @geindex -gnatwx (gcc)
12504
12505 @geindex Export/Import pragma warnings
12506
12507
12508 @table @asis
12509
12510 @item @code{-gnatwx}
12511
12512 @emph{Activate warnings on Export/Import pragmas.}
12513
12514 This switch activates warnings on Export/Import pragmas when
12515 the compiler detects a possible conflict between the Ada and
12516 foreign language calling sequences. For example, the use of
12517 default parameters in a convention C procedure is dubious
12518 because the C compiler cannot supply the proper default, so
12519 a warning is issued. The default is that such warnings are
12520 generated.
12521 @end table
12522
12523 @geindex -gnatwX (gcc)
12524
12525
12526 @table @asis
12527
12528 @item @code{-gnatwX}
12529
12530 @emph{Suppress warnings on Export/Import pragmas.}
12531
12532 This switch suppresses warnings on Export/Import pragmas.
12533 The sense of this is that you are telling the compiler that
12534 you know what you are doing in writing the pragma, and it
12535 should not complain at you.
12536 @end table
12537
12538 @geindex -gnatwm (gcc)
12539
12540
12541 @table @asis
12542
12543 @item @code{-gnatw.x}
12544
12545 @emph{Activate warnings for No_Exception_Propagation mode.}
12546
12547 This switch activates warnings for exception usage when pragma Restrictions
12548 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
12549 explicit exception raises which are not covered by a local handler, and for
12550 exception handlers which do not cover a local raise. The default is that
12551 these warnings are given for units that contain exception handlers.
12552
12553 @item @code{-gnatw.X}
12554
12555 @emph{Disable warnings for No_Exception_Propagation mode.}
12556
12557 This switch disables warnings for exception usage when pragma Restrictions
12558 (No_Exception_Propagation) is in effect.
12559 @end table
12560
12561 @geindex -gnatwy (gcc)
12562
12563 @geindex Ada compatibility issues warnings
12564
12565
12566 @table @asis
12567
12568 @item @code{-gnatwy}
12569
12570 @emph{Activate warnings for Ada compatibility issues.}
12571
12572 For the most part, newer versions of Ada are upwards compatible
12573 with older versions. For example, Ada 2005 programs will almost
12574 always work when compiled as Ada 2012.
12575 However there are some exceptions (for example the fact that
12576 @code{some} is now a reserved word in Ada 2012). This
12577 switch activates several warnings to help in identifying
12578 and correcting such incompatibilities. The default is that
12579 these warnings are generated. Note that at one point Ada 2005
12580 was called Ada 0Y, hence the choice of character.
12581 @end table
12582
12583 @geindex -gnatwY (gcc)
12584
12585 @geindex Ada compatibility issues warnings
12586
12587
12588 @table @asis
12589
12590 @item @code{-gnatwY}
12591
12592 @emph{Disable warnings for Ada compatibility issues.}
12593
12594 This switch suppresses the warnings intended to help in identifying
12595 incompatibilities between Ada language versions.
12596 @end table
12597
12598 @geindex -gnatw.y (gcc)
12599
12600 @geindex Package spec needing body
12601
12602
12603 @table @asis
12604
12605 @item @code{-gnatw.y}
12606
12607 @emph{Activate information messages for why package spec needs body.}
12608
12609 There are a number of cases in which a package spec needs a body.
12610 For example, the use of pragma Elaborate_Body, or the declaration
12611 of a procedure specification requiring a completion. This switch
12612 causes information messages to be output showing why a package
12613 specification requires a body. This can be useful in the case of
12614 a large package specification which is unexpectedly requiring a
12615 body. The default is that such information messages are not output.
12616 @end table
12617
12618 @geindex -gnatw.Y (gcc)
12619
12620 @geindex No information messages for why package spec needs body
12621
12622
12623 @table @asis
12624
12625 @item @code{-gnatw.Y}
12626
12627 @emph{Disable information messages for why package spec needs body.}
12628
12629 This switch suppresses the output of information messages showing why
12630 a package specification needs a body.
12631 @end table
12632
12633 @geindex -gnatwz (gcc)
12634
12635 @geindex Unchecked_Conversion warnings
12636
12637
12638 @table @asis
12639
12640 @item @code{-gnatwz}
12641
12642 @emph{Activate warnings on unchecked conversions.}
12643
12644 This switch activates warnings for unchecked conversions
12645 where the types are known at compile time to have different
12646 sizes. The default is that such warnings are generated. Warnings are also
12647 generated for subprogram pointers with different conventions.
12648 @end table
12649
12650 @geindex -gnatwZ (gcc)
12651
12652
12653 @table @asis
12654
12655 @item @code{-gnatwZ}
12656
12657 @emph{Suppress warnings on unchecked conversions.}
12658
12659 This switch suppresses warnings for unchecked conversions
12660 where the types are known at compile time to have different
12661 sizes or conventions.
12662 @end table
12663
12664 @geindex -gnatw.z (gcc)
12665
12666 @geindex Size/Alignment warnings
12667
12668
12669 @table @asis
12670
12671 @item @code{-gnatw.z}
12672
12673 @emph{Activate warnings for size not a multiple of alignment.}
12674
12675 This switch activates warnings for cases of record types with
12676 specified @code{Size} and @code{Alignment} attributes where the
12677 size is not a multiple of the alignment, resulting in an object
12678 size that is greater than the specified size. The default
12679 is that such warnings are generated.
12680 @end table
12681
12682 @geindex -gnatw.Z (gcc)
12683
12684 @geindex Size/Alignment warnings
12685
12686
12687 @table @asis
12688
12689 @item @code{-gnatw.Z}
12690
12691 @emph{Suppress warnings for size not a multiple of alignment.}
12692
12693 This switch suppresses warnings for cases of record types with
12694 specified @code{Size} and @code{Alignment} attributes where the
12695 size is not a multiple of the alignment, resulting in an object
12696 size that is greater than the specified size.
12697 The warning can also be
12698 suppressed by giving an explicit @code{Object_Size} value.
12699 @end table
12700
12701 @geindex -Wunused (gcc)
12702
12703
12704 @table @asis
12705
12706 @item @code{-Wunused}
12707
12708 The warnings controlled by the @code{-gnatw} switch are generated by
12709 the front end of the compiler. The GCC back end can provide
12710 additional warnings and they are controlled by the @code{-W} switch.
12711 For example, @code{-Wunused} activates back end
12712 warnings for entities that are declared but not referenced.
12713 @end table
12714
12715 @geindex -Wuninitialized (gcc)
12716
12717
12718 @table @asis
12719
12720 @item @code{-Wuninitialized}
12721
12722 Similarly, @code{-Wuninitialized} activates
12723 the back end warning for uninitialized variables. This switch must be
12724 used in conjunction with an optimization level greater than zero.
12725 @end table
12726
12727 @geindex -Wstack-usage (gcc)
12728
12729
12730 @table @asis
12731
12732 @item @code{-Wstack-usage=@emph{len}}
12733
12734 Warn if the stack usage of a subprogram might be larger than @code{len} bytes.
12735 See @ref{f5,,Static Stack Usage Analysis} for details.
12736 @end table
12737
12738 @geindex -Wall (gcc)
12739
12740
12741 @table @asis
12742
12743 @item @code{-Wall}
12744
12745 This switch enables most warnings from the GCC back end.
12746 The code generator detects a number of warning situations that are missed
12747 by the GNAT front end, and this switch can be used to activate them.
12748 The use of this switch also sets the default front end warning mode to
12749 @code{-gnatwa}, that is, most front end warnings activated as well.
12750 @end table
12751
12752 @geindex -w (gcc)
12753
12754
12755 @table @asis
12756
12757 @item @code{-w}
12758
12759 Conversely, this switch suppresses warnings from the GCC back end.
12760 The use of this switch also sets the default front end warning mode to
12761 @code{-gnatws}, that is, front end warnings suppressed as well.
12762 @end table
12763
12764 @geindex -Werror (gcc)
12765
12766
12767 @table @asis
12768
12769 @item @code{-Werror}
12770
12771 This switch causes warnings from the GCC back end to be treated as
12772 errors. The warning string still appears, but the warning messages are
12773 counted as errors, and prevent the generation of an object file.
12774 @end table
12775
12776 A string of warning parameters can be used in the same parameter. For example:
12777
12778 @example
12779 -gnatwaGe
12780 @end example
12781
12782 will turn on all optional warnings except for unrecognized pragma warnings,
12783 and also specify that warnings should be treated as errors.
12784
12785 When no switch @code{-gnatw} is used, this is equivalent to:
12786
12787 @quotation
12788
12789
12790 @itemize *
12791
12792 @item
12793 @code{-gnatw.a}
12794
12795 @item
12796 @code{-gnatwB}
12797
12798 @item
12799 @code{-gnatw.b}
12800
12801 @item
12802 @code{-gnatwC}
12803
12804 @item
12805 @code{-gnatw.C}
12806
12807 @item
12808 @code{-gnatwD}
12809
12810 @item
12811 @code{-gnatw.D}
12812
12813 @item
12814 @code{-gnatwF}
12815
12816 @item
12817 @code{-gnatw.F}
12818
12819 @item
12820 @code{-gnatwg}
12821
12822 @item
12823 @code{-gnatwH}
12824
12825 @item
12826 @code{-gnatw.H}
12827
12828 @item
12829 @code{-gnatwi}
12830
12831 @item
12832 @code{-gnatwJ}
12833
12834 @item
12835 @code{-gnatw.J}
12836
12837 @item
12838 @code{-gnatwK}
12839
12840 @item
12841 @code{-gnatw.K}
12842
12843 @item
12844 @code{-gnatwL}
12845
12846 @item
12847 @code{-gnatw.L}
12848
12849 @item
12850 @code{-gnatwM}
12851
12852 @item
12853 @code{-gnatw.m}
12854
12855 @item
12856 @code{-gnatwn}
12857
12858 @item
12859 @code{-gnatw.N}
12860
12861 @item
12862 @code{-gnatwo}
12863
12864 @item
12865 @code{-gnatw.O}
12866
12867 @item
12868 @code{-gnatwP}
12869
12870 @item
12871 @code{-gnatw.P}
12872
12873 @item
12874 @code{-gnatwq}
12875
12876 @item
12877 @code{-gnatw.Q}
12878
12879 @item
12880 @code{-gnatwR}
12881
12882 @item
12883 @code{-gnatw.R}
12884
12885 @item
12886 @code{-gnatw.S}
12887
12888 @item
12889 @code{-gnatwT}
12890
12891 @item
12892 @code{-gnatw.t}
12893
12894 @item
12895 @code{-gnatwU}
12896
12897 @item
12898 @code{-gnatw.U}
12899
12900 @item
12901 @code{-gnatwv}
12902
12903 @item
12904 @code{-gnatw.v}
12905
12906 @item
12907 @code{-gnatww}
12908
12909 @item
12910 @code{-gnatw.W}
12911
12912 @item
12913 @code{-gnatwx}
12914
12915 @item
12916 @code{-gnatw.X}
12917
12918 @item
12919 @code{-gnatwy}
12920
12921 @item
12922 @code{-gnatw.Y}
12923
12924 @item
12925 @code{-gnatwz}
12926
12927 @item
12928 @code{-gnatw.z}
12929 @end itemize
12930 @end quotation
12931
12932 @node Debugging and Assertion Control,Validity Checking,Warning Message Control,Compiler Switches
12933 @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}
12934 @subsection Debugging and Assertion Control
12935
12936
12937 @geindex -gnata (gcc)
12938
12939
12940 @table @asis
12941
12942 @item @code{-gnata}
12943
12944 @geindex Assert
12945
12946 @geindex Debug
12947
12948 @geindex Assertions
12949
12950 @geindex Precondition
12951
12952 @geindex Postcondition
12953
12954 @geindex Type invariants
12955
12956 @geindex Subtype predicates
12957
12958 The @code{-gnata} option is equivalent to the following @code{Assertion_Policy} pragma:
12959
12960 @example
12961 pragma Assertion_Policy (Check);
12962 @end example
12963
12964 Which is a shorthand for:
12965
12966 @example
12967 pragma Assertion_Policy
12968 (Assert => Check,
12969 Static_Predicate => Check,
12970 Dynamic_Predicate => Check,
12971 Pre => Check,
12972 Pre'Class => Check,
12973 Post => Check,
12974 Post'Class => Check,
12975 Type_Invariant => Check,
12976 Type_Invariant'Class => Check);
12977 @end example
12978
12979 The pragmas @code{Assert} and @code{Debug} normally have no effect and
12980 are ignored. This switch, where @code{a} stands for 'assert', causes
12981 pragmas @code{Assert} and @code{Debug} to be activated. This switch also
12982 causes preconditions, postconditions, subtype predicates, and
12983 type invariants to be activated.
12984
12985 The pragmas have the form:
12986
12987 @example
12988 pragma Assert (<Boolean-expression> [, <static-string-expression>])
12989 pragma Debug (<procedure call>)
12990 pragma Type_Invariant (<type-local-name>, <Boolean-expression>)
12991 pragma Predicate (<type-local-name>, <Boolean-expression>)
12992 pragma Precondition (<Boolean-expression>, <string-expression>)
12993 pragma Postcondition (<Boolean-expression>, <string-expression>)
12994 @end example
12995
12996 The aspects have the form:
12997
12998 @example
12999 with [Pre|Post|Type_Invariant|Dynamic_Predicate|Static_Predicate]
13000 => <Boolean-expression>;
13001 @end example
13002
13003 The @code{Assert} pragma causes @code{Boolean-expression} to be tested.
13004 If the result is @code{True}, the pragma has no effect (other than
13005 possible side effects from evaluating the expression). If the result is
13006 @code{False}, the exception @code{Assert_Failure} declared in the package
13007 @code{System.Assertions} is raised (passing @code{static-string-expression}, if
13008 present, as the message associated with the exception). If no string
13009 expression is given, the default is a string containing the file name and
13010 line number of the pragma.
13011
13012 The @code{Debug} pragma causes @code{procedure} to be called. Note that
13013 @code{pragma Debug} may appear within a declaration sequence, allowing
13014 debugging procedures to be called between declarations.
13015
13016 For the aspect specification, the @code{Boolean-expression} is evaluated.
13017 If the result is @code{True}, the aspect has no effect. If the result
13018 is @code{False}, the exception @code{Assert_Failure} is raised.
13019 @end table
13020
13021 @node Validity Checking,Style Checking,Debugging and Assertion Control,Compiler Switches
13022 @anchor{gnat_ugn/building_executable_programs_with_gnat validity-checking}@anchor{f6}@anchor{gnat_ugn/building_executable_programs_with_gnat id17}@anchor{102}
13023 @subsection Validity Checking
13024
13025
13026 @geindex Validity Checking
13027
13028 The Ada Reference Manual defines the concept of invalid values (see
13029 RM 13.9.1). The primary source of invalid values is uninitialized
13030 variables. A scalar variable that is left uninitialized may contain
13031 an invalid value; the concept of invalid does not apply to access or
13032 composite types.
13033
13034 It is an error to read an invalid value, but the RM does not require
13035 run-time checks to detect such errors, except for some minimal
13036 checking to prevent erroneous execution (i.e. unpredictable
13037 behavior). This corresponds to the @code{-gnatVd} switch below,
13038 which is the default. For example, by default, if the expression of a
13039 case statement is invalid, it will raise Constraint_Error rather than
13040 causing a wild jump, and if an array index on the left-hand side of an
13041 assignment is invalid, it will raise Constraint_Error rather than
13042 overwriting an arbitrary memory location.
13043
13044 The @code{-gnatVa} may be used to enable additional validity checks,
13045 which are not required by the RM. These checks are often very
13046 expensive (which is why the RM does not require them). These checks
13047 are useful in tracking down uninitialized variables, but they are
13048 not usually recommended for production builds, and in particular
13049 we do not recommend using these extra validity checking options in
13050 combination with optimization, since this can confuse the optimizer.
13051 If performance is a consideration, leading to the need to optimize,
13052 then the validity checking options should not be used.
13053
13054 The other @code{-gnatV@emph{x}} switches below allow finer-grained
13055 control; you can enable whichever validity checks you desire. However,
13056 for most debugging purposes, @code{-gnatVa} is sufficient, and the
13057 default @code{-gnatVd} (i.e. standard Ada behavior) is usually
13058 sufficient for non-debugging use.
13059
13060 The @code{-gnatB} switch tells the compiler to assume that all
13061 values are valid (that is, within their declared subtype range)
13062 except in the context of a use of the Valid attribute. This means
13063 the compiler can generate more efficient code, since the range
13064 of values is better known at compile time. However, an uninitialized
13065 variable can cause wild jumps and memory corruption in this mode.
13066
13067 The @code{-gnatV@emph{x}} switch allows control over the validity
13068 checking mode as described below.
13069 The @code{x} argument is a string of letters that
13070 indicate validity checks that are performed or not performed in addition
13071 to the default checks required by Ada as described above.
13072
13073 @geindex -gnatVa (gcc)
13074
13075
13076 @table @asis
13077
13078 @item @code{-gnatVa}
13079
13080 @emph{All validity checks.}
13081
13082 All validity checks are turned on.
13083 That is, @code{-gnatVa} is
13084 equivalent to @code{gnatVcdfimorst}.
13085 @end table
13086
13087 @geindex -gnatVc (gcc)
13088
13089
13090 @table @asis
13091
13092 @item @code{-gnatVc}
13093
13094 @emph{Validity checks for copies.}
13095
13096 The right hand side of assignments, and the initializing values of
13097 object declarations are validity checked.
13098 @end table
13099
13100 @geindex -gnatVd (gcc)
13101
13102
13103 @table @asis
13104
13105 @item @code{-gnatVd}
13106
13107 @emph{Default (RM) validity checks.}
13108
13109 Some validity checks are done by default following normal Ada semantics
13110 (RM 13.9.1 (9-11)).
13111 A check is done in case statements that the expression is within the range
13112 of the subtype. If it is not, Constraint_Error is raised.
13113 For assignments to array components, a check is done that the expression used
13114 as index is within the range. If it is not, Constraint_Error is raised.
13115 Both these validity checks may be turned off using switch @code{-gnatVD}.
13116 They are turned on by default. If @code{-gnatVD} is specified, a subsequent
13117 switch @code{-gnatVd} will leave the checks turned on.
13118 Switch @code{-gnatVD} should be used only if you are sure that all such
13119 expressions have valid values. If you use this switch and invalid values
13120 are present, then the program is erroneous, and wild jumps or memory
13121 overwriting may occur.
13122 @end table
13123
13124 @geindex -gnatVe (gcc)
13125
13126
13127 @table @asis
13128
13129 @item @code{-gnatVe}
13130
13131 @emph{Validity checks for elementary components.}
13132
13133 In the absence of this switch, assignments to record or array components are
13134 not validity checked, even if validity checks for assignments generally
13135 (@code{-gnatVc}) are turned on. In Ada, assignment of composite values do not
13136 require valid data, but assignment of individual components does. So for
13137 example, there is a difference between copying the elements of an array with a
13138 slice assignment, compared to assigning element by element in a loop. This
13139 switch allows you to turn off validity checking for components, even when they
13140 are assigned component by component.
13141 @end table
13142
13143 @geindex -gnatVf (gcc)
13144
13145
13146 @table @asis
13147
13148 @item @code{-gnatVf}
13149
13150 @emph{Validity checks for floating-point values.}
13151
13152 In the absence of this switch, validity checking occurs only for discrete
13153 values. If @code{-gnatVf} is specified, then validity checking also applies
13154 for floating-point values, and NaNs and infinities are considered invalid,
13155 as well as out of range values for constrained types. Note that this means
13156 that standard IEEE infinity mode is not allowed. The exact contexts
13157 in which floating-point values are checked depends on the setting of other
13158 options. For example, @code{-gnatVif} or @code{-gnatVfi}
13159 (the order does not matter) specifies that floating-point parameters of mode
13160 @code{in} should be validity checked.
13161 @end table
13162
13163 @geindex -gnatVi (gcc)
13164
13165
13166 @table @asis
13167
13168 @item @code{-gnatVi}
13169
13170 @emph{Validity checks for `@w{`}in`@w{`} mode parameters.}
13171
13172 Arguments for parameters of mode @code{in} are validity checked in function
13173 and procedure calls at the point of call.
13174 @end table
13175
13176 @geindex -gnatVm (gcc)
13177
13178
13179 @table @asis
13180
13181 @item @code{-gnatVm}
13182
13183 @emph{Validity checks for `@w{`}in out`@w{`} mode parameters.}
13184
13185 Arguments for parameters of mode @code{in out} are validity checked in
13186 procedure calls at the point of call. The @code{'m'} here stands for
13187 modify, since this concerns parameters that can be modified by the call.
13188 Note that there is no specific option to test @code{out} parameters,
13189 but any reference within the subprogram will be tested in the usual
13190 manner, and if an invalid value is copied back, any reference to it
13191 will be subject to validity checking.
13192 @end table
13193
13194 @geindex -gnatVn (gcc)
13195
13196
13197 @table @asis
13198
13199 @item @code{-gnatVn}
13200
13201 @emph{No validity checks.}
13202
13203 This switch turns off all validity checking, including the default checking
13204 for case statements and left hand side subscripts. Note that the use of
13205 the switch @code{-gnatp} suppresses all run-time checks, including
13206 validity checks, and thus implies @code{-gnatVn}. When this switch
13207 is used, it cancels any other @code{-gnatV} previously issued.
13208 @end table
13209
13210 @geindex -gnatVo (gcc)
13211
13212
13213 @table @asis
13214
13215 @item @code{-gnatVo}
13216
13217 @emph{Validity checks for operator and attribute operands.}
13218
13219 Arguments for predefined operators and attributes are validity checked.
13220 This includes all operators in package @code{Standard},
13221 the shift operators defined as intrinsic in package @code{Interfaces}
13222 and operands for attributes such as @code{Pos}. Checks are also made
13223 on individual component values for composite comparisons, and on the
13224 expressions in type conversions and qualified expressions. Checks are
13225 also made on explicit ranges using @code{..} (e.g., slices, loops etc).
13226 @end table
13227
13228 @geindex -gnatVp (gcc)
13229
13230
13231 @table @asis
13232
13233 @item @code{-gnatVp}
13234
13235 @emph{Validity checks for parameters.}
13236
13237 This controls the treatment of parameters within a subprogram (as opposed
13238 to @code{-gnatVi} and @code{-gnatVm} which control validity testing
13239 of parameters on a call. If either of these call options is used, then
13240 normally an assumption is made within a subprogram that the input arguments
13241 have been validity checking at the point of call, and do not need checking
13242 again within a subprogram). If @code{-gnatVp} is set, then this assumption
13243 is not made, and parameters are not assumed to be valid, so their validity
13244 will be checked (or rechecked) within the subprogram.
13245 @end table
13246
13247 @geindex -gnatVr (gcc)
13248
13249
13250 @table @asis
13251
13252 @item @code{-gnatVr}
13253
13254 @emph{Validity checks for function returns.}
13255
13256 The expression in @code{return} statements in functions is validity
13257 checked.
13258 @end table
13259
13260 @geindex -gnatVs (gcc)
13261
13262
13263 @table @asis
13264
13265 @item @code{-gnatVs}
13266
13267 @emph{Validity checks for subscripts.}
13268
13269 All subscripts expressions are checked for validity, whether they appear
13270 on the right side or left side (in default mode only left side subscripts
13271 are validity checked).
13272 @end table
13273
13274 @geindex -gnatVt (gcc)
13275
13276
13277 @table @asis
13278
13279 @item @code{-gnatVt}
13280
13281 @emph{Validity checks for tests.}
13282
13283 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
13284 statements are checked, as well as guard expressions in entry calls.
13285 @end table
13286
13287 The @code{-gnatV} switch may be followed by a string of letters
13288 to turn on a series of validity checking options.
13289 For example, @code{-gnatVcr}
13290 specifies that in addition to the default validity checking, copies and
13291 function return expressions are to be validity checked.
13292 In order to make it easier to specify the desired combination of effects,
13293 the upper case letters @code{CDFIMORST} may
13294 be used to turn off the corresponding lower case option.
13295 Thus @code{-gnatVaM} turns on all validity checking options except for
13296 checking of @code{in out} parameters.
13297
13298 The specification of additional validity checking generates extra code (and
13299 in the case of @code{-gnatVa} the code expansion can be substantial).
13300 However, these additional checks can be very useful in detecting
13301 uninitialized variables, incorrect use of unchecked conversion, and other
13302 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
13303 is useful in conjunction with the extra validity checking, since this
13304 ensures that wherever possible uninitialized variables have invalid values.
13305
13306 See also the pragma @code{Validity_Checks} which allows modification of
13307 the validity checking mode at the program source level, and also allows for
13308 temporary disabling of validity checks.
13309
13310 @node Style Checking,Run-Time Checks,Validity Checking,Compiler Switches
13311 @anchor{gnat_ugn/building_executable_programs_with_gnat id18}@anchor{103}@anchor{gnat_ugn/building_executable_programs_with_gnat style-checking}@anchor{fb}
13312 @subsection Style Checking
13313
13314
13315 @geindex Style checking
13316
13317 @geindex -gnaty (gcc)
13318
13319 The @code{-gnatyx} switch causes the compiler to
13320 enforce specified style rules. A limited set of style rules has been used
13321 in writing the GNAT sources themselves. This switch allows user programs
13322 to activate all or some of these checks. If the source program fails a
13323 specified style check, an appropriate message is given, preceded by
13324 the character sequence '(style)'. This message does not prevent
13325 successful compilation (unless the @code{-gnatwe} switch is used).
13326
13327 Note that this is by no means intended to be a general facility for
13328 checking arbitrary coding standards. It is simply an embedding of the
13329 style rules we have chosen for the GNAT sources. If you are starting
13330 a project which does not have established style standards, you may
13331 find it useful to adopt the entire set of GNAT coding standards, or
13332 some subset of them.
13333
13334
13335 The string @code{x} is a sequence of letters or digits
13336 indicating the particular style
13337 checks to be performed. The following checks are defined:
13338
13339 @geindex -gnaty[0-9] (gcc)
13340
13341
13342 @table @asis
13343
13344 @item @code{-gnaty0}
13345
13346 @emph{Specify indentation level.}
13347
13348 If a digit from 1-9 appears
13349 in the string after @code{-gnaty}
13350 then proper indentation is checked, with the digit indicating the
13351 indentation level required. A value of zero turns off this style check.
13352 The general style of required indentation is as specified by
13353 the examples in the Ada Reference Manual. Full line comments must be
13354 aligned with the @code{--} starting on a column that is a multiple of
13355 the alignment level, or they may be aligned the same way as the following
13356 non-blank line (this is useful when full line comments appear in the middle
13357 of a statement, or they may be aligned with the source line on the previous
13358 non-blank line.
13359 @end table
13360
13361 @geindex -gnatya (gcc)
13362
13363
13364 @table @asis
13365
13366 @item @code{-gnatya}
13367
13368 @emph{Check attribute casing.}
13369
13370 Attribute names, including the case of keywords such as @code{digits}
13371 used as attributes names, must be written in mixed case, that is, the
13372 initial letter and any letter following an underscore must be uppercase.
13373 All other letters must be lowercase.
13374 @end table
13375
13376 @geindex -gnatyA (gcc)
13377
13378
13379 @table @asis
13380
13381 @item @code{-gnatyA}
13382
13383 @emph{Use of array index numbers in array attributes.}
13384
13385 When using the array attributes First, Last, Range,
13386 or Length, the index number must be omitted for one-dimensional arrays
13387 and is required for multi-dimensional arrays.
13388 @end table
13389
13390 @geindex -gnatyb (gcc)
13391
13392
13393 @table @asis
13394
13395 @item @code{-gnatyb}
13396
13397 @emph{Blanks not allowed at statement end.}
13398
13399 Trailing blanks are not allowed at the end of statements. The purpose of this
13400 rule, together with h (no horizontal tabs), is to enforce a canonical format
13401 for the use of blanks to separate source tokens.
13402 @end table
13403
13404 @geindex -gnatyB (gcc)
13405
13406
13407 @table @asis
13408
13409 @item @code{-gnatyB}
13410
13411 @emph{Check Boolean operators.}
13412
13413 The use of AND/OR operators is not permitted except in the cases of modular
13414 operands, array operands, and simple stand-alone boolean variables or
13415 boolean constants. In all other cases @code{and then}/@cite{or else} are
13416 required.
13417 @end table
13418
13419 @geindex -gnatyc (gcc)
13420
13421
13422 @table @asis
13423
13424 @item @code{-gnatyc}
13425
13426 @emph{Check comments, double space.}
13427
13428 Comments must meet the following set of rules:
13429
13430
13431 @itemize *
13432
13433 @item
13434 The @code{--} that starts the column must either start in column one,
13435 or else at least one blank must precede this sequence.
13436
13437 @item
13438 Comments that follow other tokens on a line must have at least one blank
13439 following the @code{--} at the start of the comment.
13440
13441 @item
13442 Full line comments must have at least two blanks following the
13443 @code{--} that starts the comment, with the following exceptions.
13444
13445 @item
13446 A line consisting only of the @code{--} characters, possibly preceded
13447 by blanks is permitted.
13448
13449 @item
13450 A comment starting with @code{--x} where @code{x} is a special character
13451 is permitted.
13452 This allows proper processing of the output from specialized tools
13453 such as @code{gnatprep} (where @code{--!} is used) and in earlier versions of the SPARK
13454 annotation
13455 language (where @code{--#} is used). For the purposes of this rule, a
13456 special character is defined as being in one of the ASCII ranges
13457 @code{16#21#...16#2F#} or @code{16#3A#...16#3F#}.
13458 Note that this usage is not permitted
13459 in GNAT implementation units (i.e., when @code{-gnatg} is used).
13460
13461 @item
13462 A line consisting entirely of minus signs, possibly preceded by blanks, is
13463 permitted. This allows the construction of box comments where lines of minus
13464 signs are used to form the top and bottom of the box.
13465
13466 @item
13467 A comment that starts and ends with @code{--} is permitted as long as at
13468 least one blank follows the initial @code{--}. Together with the preceding
13469 rule, this allows the construction of box comments, as shown in the following
13470 example:
13471
13472 @example
13473 ---------------------------
13474 -- This is a box comment --
13475 -- with two text lines. --
13476 ---------------------------
13477 @end example
13478 @end itemize
13479 @end table
13480
13481 @geindex -gnatyC (gcc)
13482
13483
13484 @table @asis
13485
13486 @item @code{-gnatyC}
13487
13488 @emph{Check comments, single space.}
13489
13490 This is identical to @code{c} except that only one space
13491 is required following the @code{--} of a comment instead of two.
13492 @end table
13493
13494 @geindex -gnatyd (gcc)
13495
13496
13497 @table @asis
13498
13499 @item @code{-gnatyd}
13500
13501 @emph{Check no DOS line terminators present.}
13502
13503 All lines must be terminated by a single ASCII.LF
13504 character (in particular the DOS line terminator sequence CR/LF is not
13505 allowed).
13506 @end table
13507
13508 @geindex -gnatye (gcc)
13509
13510
13511 @table @asis
13512
13513 @item @code{-gnatye}
13514
13515 @emph{Check end/exit labels.}
13516
13517 Optional labels on @code{end} statements ending subprograms and on
13518 @code{exit} statements exiting named loops, are required to be present.
13519 @end table
13520
13521 @geindex -gnatyf (gcc)
13522
13523
13524 @table @asis
13525
13526 @item @code{-gnatyf}
13527
13528 @emph{No form feeds or vertical tabs.}
13529
13530 Neither form feeds nor vertical tab characters are permitted
13531 in the source text.
13532 @end table
13533
13534 @geindex -gnatyg (gcc)
13535
13536
13537 @table @asis
13538
13539 @item @code{-gnatyg}
13540
13541 @emph{GNAT style mode.}
13542
13543 The set of style check switches is set to match that used by the GNAT sources.
13544 This may be useful when developing code that is eventually intended to be
13545 incorporated into GNAT. Currently this is equivalent to @code{-gnatwydISux})
13546 but additional style switches may be added to this set in the future without
13547 advance notice.
13548 @end table
13549
13550 @geindex -gnatyh (gcc)
13551
13552
13553 @table @asis
13554
13555 @item @code{-gnatyh}
13556
13557 @emph{No horizontal tabs.}
13558
13559 Horizontal tab characters are not permitted in the source text.
13560 Together with the b (no blanks at end of line) check, this
13561 enforces a canonical form for the use of blanks to separate
13562 source tokens.
13563 @end table
13564
13565 @geindex -gnatyi (gcc)
13566
13567
13568 @table @asis
13569
13570 @item @code{-gnatyi}
13571
13572 @emph{Check if-then layout.}
13573
13574 The keyword @code{then} must appear either on the same
13575 line as corresponding @code{if}, or on a line on its own, lined
13576 up under the @code{if}.
13577 @end table
13578
13579 @geindex -gnatyI (gcc)
13580
13581
13582 @table @asis
13583
13584 @item @code{-gnatyI}
13585
13586 @emph{check mode IN keywords.}
13587
13588 Mode @code{in} (the default mode) is not
13589 allowed to be given explicitly. @code{in out} is fine,
13590 but not @code{in} on its own.
13591 @end table
13592
13593 @geindex -gnatyk (gcc)
13594
13595
13596 @table @asis
13597
13598 @item @code{-gnatyk}
13599
13600 @emph{Check keyword casing.}
13601
13602 All keywords must be in lower case (with the exception of keywords
13603 such as @code{digits} used as attribute names to which this check
13604 does not apply).
13605 @end table
13606
13607 @geindex -gnatyl (gcc)
13608
13609
13610 @table @asis
13611
13612 @item @code{-gnatyl}
13613
13614 @emph{Check layout.}
13615
13616 Layout of statement and declaration constructs must follow the
13617 recommendations in the Ada Reference Manual, as indicated by the
13618 form of the syntax rules. For example an @code{else} keyword must
13619 be lined up with the corresponding @code{if} keyword.
13620
13621 There are two respects in which the style rule enforced by this check
13622 option are more liberal than those in the Ada Reference Manual. First
13623 in the case of record declarations, it is permissible to put the
13624 @code{record} keyword on the same line as the @code{type} keyword, and
13625 then the @code{end} in @code{end record} must line up under @code{type}.
13626 This is also permitted when the type declaration is split on two lines.
13627 For example, any of the following three layouts is acceptable:
13628
13629 @example
13630 type q is record
13631 a : integer;
13632 b : integer;
13633 end record;
13634
13635 type q is
13636 record
13637 a : integer;
13638 b : integer;
13639 end record;
13640
13641 type q is
13642 record
13643 a : integer;
13644 b : integer;
13645 end record;
13646 @end example
13647
13648 Second, in the case of a block statement, a permitted alternative
13649 is to put the block label on the same line as the @code{declare} or
13650 @code{begin} keyword, and then line the @code{end} keyword up under
13651 the block label. For example both the following are permitted:
13652
13653 @example
13654 Block : declare
13655 A : Integer := 3;
13656 begin
13657 Proc (A, A);
13658 end Block;
13659
13660 Block :
13661 declare
13662 A : Integer := 3;
13663 begin
13664 Proc (A, A);
13665 end Block;
13666 @end example
13667
13668 The same alternative format is allowed for loops. For example, both of
13669 the following are permitted:
13670
13671 @example
13672 Clear : while J < 10 loop
13673 A (J) := 0;
13674 end loop Clear;
13675
13676 Clear :
13677 while J < 10 loop
13678 A (J) := 0;
13679 end loop Clear;
13680 @end example
13681 @end table
13682
13683 @geindex -gnatyLnnn (gcc)
13684
13685
13686 @table @asis
13687
13688 @item @code{-gnatyL}
13689
13690 @emph{Set maximum nesting level.}
13691
13692 The maximum level of nesting of constructs (including subprograms, loops,
13693 blocks, packages, and conditionals) may not exceed the given value
13694 @emph{nnn}. A value of zero disconnects this style check.
13695 @end table
13696
13697 @geindex -gnatym (gcc)
13698
13699
13700 @table @asis
13701
13702 @item @code{-gnatym}
13703
13704 @emph{Check maximum line length.}
13705
13706 The length of source lines must not exceed 79 characters, including
13707 any trailing blanks. The value of 79 allows convenient display on an
13708 80 character wide device or window, allowing for possible special
13709 treatment of 80 character lines. Note that this count is of
13710 characters in the source text. This means that a tab character counts
13711 as one character in this count and a wide character sequence counts as
13712 a single character (however many bytes are needed in the encoding).
13713 @end table
13714
13715 @geindex -gnatyMnnn (gcc)
13716
13717
13718 @table @asis
13719
13720 @item @code{-gnatyM}
13721
13722 @emph{Set maximum line length.}
13723
13724 The length of lines must not exceed the
13725 given value @emph{nnn}. The maximum value that can be specified is 32767.
13726 If neither style option for setting the line length is used, then the
13727 default is 255. This also controls the maximum length of lexical elements,
13728 where the only restriction is that they must fit on a single line.
13729 @end table
13730
13731 @geindex -gnatyn (gcc)
13732
13733
13734 @table @asis
13735
13736 @item @code{-gnatyn}
13737
13738 @emph{Check casing of entities in Standard.}
13739
13740 Any identifier from Standard must be cased
13741 to match the presentation in the Ada Reference Manual (for example,
13742 @code{Integer} and @code{ASCII.NUL}).
13743 @end table
13744
13745 @geindex -gnatyN (gcc)
13746
13747
13748 @table @asis
13749
13750 @item @code{-gnatyN}
13751
13752 @emph{Turn off all style checks.}
13753
13754 All style check options are turned off.
13755 @end table
13756
13757 @geindex -gnatyo (gcc)
13758
13759
13760 @table @asis
13761
13762 @item @code{-gnatyo}
13763
13764 @emph{Check order of subprogram bodies.}
13765
13766 All subprogram bodies in a given scope
13767 (e.g., a package body) must be in alphabetical order. The ordering
13768 rule uses normal Ada rules for comparing strings, ignoring casing
13769 of letters, except that if there is a trailing numeric suffix, then
13770 the value of this suffix is used in the ordering (e.g., Junk2 comes
13771 before Junk10).
13772 @end table
13773
13774 @geindex -gnatyO (gcc)
13775
13776
13777 @table @asis
13778
13779 @item @code{-gnatyO}
13780
13781 @emph{Check that overriding subprograms are explicitly marked as such.}
13782
13783 This applies to all subprograms of a derived type that override a primitive
13784 operation of the type, for both tagged and untagged types. In particular,
13785 the declaration of a primitive operation of a type extension that overrides
13786 an inherited operation must carry an overriding indicator. Another case is
13787 the declaration of a function that overrides a predefined operator (such
13788 as an equality operator).
13789 @end table
13790
13791 @geindex -gnatyp (gcc)
13792
13793
13794 @table @asis
13795
13796 @item @code{-gnatyp}
13797
13798 @emph{Check pragma casing.}
13799
13800 Pragma names must be written in mixed case, that is, the
13801 initial letter and any letter following an underscore must be uppercase.
13802 All other letters must be lowercase. An exception is that SPARK_Mode is
13803 allowed as an alternative for Spark_Mode.
13804 @end table
13805
13806 @geindex -gnatyr (gcc)
13807
13808
13809 @table @asis
13810
13811 @item @code{-gnatyr}
13812
13813 @emph{Check references.}
13814
13815 All identifier references must be cased in the same way as the
13816 corresponding declaration. No specific casing style is imposed on
13817 identifiers. The only requirement is for consistency of references
13818 with declarations.
13819 @end table
13820
13821 @geindex -gnatys (gcc)
13822
13823
13824 @table @asis
13825
13826 @item @code{-gnatys}
13827
13828 @emph{Check separate specs.}
13829
13830 Separate declarations ('specs') are required for subprograms (a
13831 body is not allowed to serve as its own declaration). The only
13832 exception is that parameterless library level procedures are
13833 not required to have a separate declaration. This exception covers
13834 the most frequent form of main program procedures.
13835 @end table
13836
13837 @geindex -gnatyS (gcc)
13838
13839
13840 @table @asis
13841
13842 @item @code{-gnatyS}
13843
13844 @emph{Check no statements after then/else.}
13845
13846 No statements are allowed
13847 on the same line as a @code{then} or @code{else} keyword following the
13848 keyword in an @code{if} statement. @code{or else} and @code{and then} are not
13849 affected, and a special exception allows a pragma to appear after @code{else}.
13850 @end table
13851
13852 @geindex -gnatyt (gcc)
13853
13854
13855 @table @asis
13856
13857 @item @code{-gnatyt}
13858
13859 @emph{Check token spacing.}
13860
13861 The following token spacing rules are enforced:
13862
13863
13864 @itemize *
13865
13866 @item
13867 The keywords @code{abs} and @code{not} must be followed by a space.
13868
13869 @item
13870 The token @code{=>} must be surrounded by spaces.
13871
13872 @item
13873 The token @code{<>} must be preceded by a space or a left parenthesis.
13874
13875 @item
13876 Binary operators other than @code{**} must be surrounded by spaces.
13877 There is no restriction on the layout of the @code{**} binary operator.
13878
13879 @item
13880 Colon must be surrounded by spaces.
13881
13882 @item
13883 Colon-equal (assignment, initialization) must be surrounded by spaces.
13884
13885 @item
13886 Comma must be the first non-blank character on the line, or be
13887 immediately preceded by a non-blank character, and must be followed
13888 by a space.
13889
13890 @item
13891 If the token preceding a left parenthesis ends with a letter or digit, then
13892 a space must separate the two tokens.
13893
13894 @item
13895 If the token following a right parenthesis starts with a letter or digit, then
13896 a space must separate the two tokens.
13897
13898 @item
13899 A right parenthesis must either be the first non-blank character on
13900 a line, or it must be preceded by a non-blank character.
13901
13902 @item
13903 A semicolon must not be preceded by a space, and must not be followed by
13904 a non-blank character.
13905
13906 @item
13907 A unary plus or minus may not be followed by a space.
13908
13909 @item
13910 A vertical bar must be surrounded by spaces.
13911 @end itemize
13912
13913 Exactly one blank (and no other white space) must appear between
13914 a @code{not} token and a following @code{in} token.
13915 @end table
13916
13917 @geindex -gnatyu (gcc)
13918
13919
13920 @table @asis
13921
13922 @item @code{-gnatyu}
13923
13924 @emph{Check unnecessary blank lines.}
13925
13926 Unnecessary blank lines are not allowed. A blank line is considered
13927 unnecessary if it appears at the end of the file, or if more than
13928 one blank line occurs in sequence.
13929 @end table
13930
13931 @geindex -gnatyx (gcc)
13932
13933
13934 @table @asis
13935
13936 @item @code{-gnatyx}
13937
13938 @emph{Check extra parentheses.}
13939
13940 Unnecessary extra level of parentheses (C-style) are not allowed
13941 around conditions in @code{if} statements, @code{while} statements and
13942 @code{exit} statements.
13943 @end table
13944
13945 @geindex -gnatyy (gcc)
13946
13947
13948 @table @asis
13949
13950 @item @code{-gnatyy}
13951
13952 @emph{Set all standard style check options.}
13953
13954 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
13955 options enabled with the exception of @code{-gnatyB}, @code{-gnatyd},
13956 @code{-gnatyI}, @code{-gnatyLnnn}, @code{-gnatyo}, @code{-gnatyO},
13957 @code{-gnatyS}, @code{-gnatyu}, and @code{-gnatyx}.
13958 @end table
13959
13960 @geindex -gnaty- (gcc)
13961
13962
13963 @table @asis
13964
13965 @item @code{-gnaty-}
13966
13967 @emph{Remove style check options.}
13968
13969 This causes any subsequent options in the string to act as canceling the
13970 corresponding style check option. To cancel maximum nesting level control,
13971 use the @code{L} parameter without any integer value after that, because any
13972 digit following @emph{-} in the parameter string of the @code{-gnaty}
13973 option will be treated as canceling the indentation check. The same is true
13974 for the @code{M} parameter. @code{y} and @code{N} parameters are not
13975 allowed after @emph{-}.
13976 @end table
13977
13978 @geindex -gnaty+ (gcc)
13979
13980
13981 @table @asis
13982
13983 @item @code{-gnaty+}
13984
13985 @emph{Enable style check options.}
13986
13987 This causes any subsequent options in the string to enable the corresponding
13988 style check option. That is, it cancels the effect of a previous -,
13989 if any.
13990 @end table
13991
13992 @c end of switch description (leave this comment to ease automatic parsing for
13993
13994 @c GPS
13995
13996 In the above rules, appearing in column one is always permitted, that is,
13997 counts as meeting either a requirement for a required preceding space,
13998 or as meeting a requirement for no preceding space.
13999
14000 Appearing at the end of a line is also always permitted, that is, counts
14001 as meeting either a requirement for a following space, or as meeting
14002 a requirement for no following space.
14003
14004 If any of these style rules is violated, a message is generated giving
14005 details on the violation. The initial characters of such messages are
14006 always '@cite{(style)}'. Note that these messages are treated as warning
14007 messages, so they normally do not prevent the generation of an object
14008 file. The @code{-gnatwe} switch can be used to treat warning messages,
14009 including style messages, as fatal errors.
14010
14011 The switch @code{-gnaty} on its own (that is not
14012 followed by any letters or digits) is equivalent
14013 to the use of @code{-gnatyy} as described above, that is all
14014 built-in standard style check options are enabled.
14015
14016 The switch @code{-gnatyN} clears any previously set style checks.
14017
14018 @node Run-Time Checks,Using gcc for Syntax Checking,Style Checking,Compiler Switches
14019 @anchor{gnat_ugn/building_executable_programs_with_gnat run-time-checks}@anchor{f9}@anchor{gnat_ugn/building_executable_programs_with_gnat id19}@anchor{104}
14020 @subsection Run-Time Checks
14021
14022
14023 @geindex Division by zero
14024
14025 @geindex Access before elaboration
14026
14027 @geindex Checks
14028 @geindex division by zero
14029
14030 @geindex Checks
14031 @geindex access before elaboration
14032
14033 @geindex Checks
14034 @geindex stack overflow checking
14035
14036 By default, the following checks are suppressed: stack overflow
14037 checks, and checks for access before elaboration on subprogram
14038 calls. All other checks, including overflow checks, range checks and
14039 array bounds checks, are turned on by default. The following @code{gcc}
14040 switches refine this default behavior.
14041
14042 @geindex -gnatp (gcc)
14043
14044
14045 @table @asis
14046
14047 @item @code{-gnatp}
14048
14049 @geindex Suppressing checks
14050
14051 @geindex Checks
14052 @geindex suppressing
14053
14054 This switch causes the unit to be compiled
14055 as though @code{pragma Suppress (All_checks)}
14056 had been present in the source. Validity checks are also eliminated (in
14057 other words @code{-gnatp} also implies @code{-gnatVn}.
14058 Use this switch to improve the performance
14059 of the code at the expense of safety in the presence of invalid data or
14060 program bugs.
14061
14062 Note that when checks are suppressed, the compiler is allowed, but not
14063 required, to omit the checking code. If the run-time cost of the
14064 checking code is zero or near-zero, the compiler will generate it even
14065 if checks are suppressed. In particular, if the compiler can prove
14066 that a certain check will necessarily fail, it will generate code to
14067 do an unconditional 'raise', even if checks are suppressed. The
14068 compiler warns in this case. Another case in which checks may not be
14069 eliminated is when they are embedded in certain run-time routines such
14070 as math library routines.
14071
14072 Of course, run-time checks are omitted whenever the compiler can prove
14073 that they will not fail, whether or not checks are suppressed.
14074
14075 Note that if you suppress a check that would have failed, program
14076 execution is erroneous, which means the behavior is totally
14077 unpredictable. The program might crash, or print wrong answers, or
14078 do anything else. It might even do exactly what you wanted it to do
14079 (and then it might start failing mysteriously next week or next
14080 year). The compiler will generate code based on the assumption that
14081 the condition being checked is true, which can result in erroneous
14082 execution if that assumption is wrong.
14083
14084 The checks subject to suppression include all the checks defined by the Ada
14085 standard, the additional implementation defined checks @code{Alignment_Check},
14086 @code{Duplicated_Tag_Check}, @code{Predicate_Check}, @code{Container_Checks}, @code{Tampering_Check},
14087 and @code{Validity_Check}, as well as any checks introduced using @code{pragma Check_Name}.
14088 Note that @code{Atomic_Synchronization} is not automatically suppressed by use of this option.
14089
14090 If the code depends on certain checks being active, you can use
14091 pragma @code{Unsuppress} either as a configuration pragma or as
14092 a local pragma to make sure that a specified check is performed
14093 even if @code{gnatp} is specified.
14094
14095 The @code{-gnatp} switch has no effect if a subsequent
14096 @code{-gnat-p} switch appears.
14097 @end table
14098
14099 @geindex -gnat-p (gcc)
14100
14101 @geindex Suppressing checks
14102
14103 @geindex Checks
14104 @geindex suppressing
14105
14106 @geindex Suppress
14107
14108
14109 @table @asis
14110
14111 @item @code{-gnat-p}
14112
14113 This switch cancels the effect of a previous @code{gnatp} switch.
14114 @end table
14115
14116 @geindex -gnato?? (gcc)
14117
14118 @geindex Overflow checks
14119
14120 @geindex Overflow mode
14121
14122 @geindex Check
14123 @geindex overflow
14124
14125
14126 @table @asis
14127
14128 @item @code{-gnato??}
14129
14130 This switch controls the mode used for computing intermediate
14131 arithmetic integer operations, and also enables overflow checking.
14132 For a full description of overflow mode and checking control, see
14133 the 'Overflow Check Handling in GNAT' appendix in this
14134 User's Guide.
14135
14136 Overflow checks are always enabled by this switch. The argument
14137 controls the mode, using the codes
14138
14139
14140 @table @asis
14141
14142 @item @emph{1 = STRICT}
14143
14144 In STRICT mode, intermediate operations are always done using the
14145 base type, and overflow checking ensures that the result is within
14146 the base type range.
14147
14148 @item @emph{2 = MINIMIZED}
14149
14150 In MINIMIZED mode, overflows in intermediate operations are avoided
14151 where possible by using a larger integer type for the computation
14152 (typically @code{Long_Long_Integer}). Overflow checking ensures that
14153 the result fits in this larger integer type.
14154
14155 @item @emph{3 = ELIMINATED}
14156
14157 In ELIMINATED mode, overflows in intermediate operations are avoided
14158 by using multi-precision arithmetic. In this case, overflow checking
14159 has no effect on intermediate operations (since overflow is impossible).
14160 @end table
14161
14162 If two digits are present after @code{-gnato} then the first digit
14163 sets the mode for expressions outside assertions, and the second digit
14164 sets the mode for expressions within assertions. Here assertions is used
14165 in the technical sense (which includes for example precondition and
14166 postcondition expressions).
14167
14168 If one digit is present, the corresponding mode is applicable to both
14169 expressions within and outside assertion expressions.
14170
14171 If no digits are present, the default is to enable overflow checks
14172 and set STRICT mode for both kinds of expressions. This is compatible
14173 with the use of @code{-gnato} in previous versions of GNAT.
14174
14175 @geindex Machine_Overflows
14176
14177 Note that the @code{-gnato??} switch does not affect the code generated
14178 for any floating-point operations; it applies only to integer semantics.
14179 For floating-point, GNAT has the @code{Machine_Overflows}
14180 attribute set to @code{False} and the normal mode of operation is to
14181 generate IEEE NaN and infinite values on overflow or invalid operations
14182 (such as dividing 0.0 by 0.0).
14183
14184 The reason that we distinguish overflow checking from other kinds of
14185 range constraint checking is that a failure of an overflow check, unlike
14186 for example the failure of a range check, can result in an incorrect
14187 value, but cannot cause random memory destruction (like an out of range
14188 subscript), or a wild jump (from an out of range case value). Overflow
14189 checking is also quite expensive in time and space, since in general it
14190 requires the use of double length arithmetic.
14191
14192 Note again that the default is @code{-gnato11} (equivalent to @code{-gnato1}),
14193 so overflow checking is performed in STRICT mode by default.
14194 @end table
14195
14196 @geindex -gnatE (gcc)
14197
14198 @geindex Elaboration checks
14199
14200 @geindex Check
14201 @geindex elaboration
14202
14203
14204 @table @asis
14205
14206 @item @code{-gnatE}
14207
14208 Enables dynamic checks for access-before-elaboration
14209 on subprogram calls and generic instantiations.
14210 Note that @code{-gnatE} is not necessary for safety, because in the
14211 default mode, GNAT ensures statically that the checks would not fail.
14212 For full details of the effect and use of this switch,
14213 @ref{1c,,Compiling with gcc}.
14214 @end table
14215
14216 @geindex -fstack-check (gcc)
14217
14218 @geindex Stack Overflow Checking
14219
14220 @geindex Checks
14221 @geindex stack overflow checking
14222
14223
14224 @table @asis
14225
14226 @item @code{-fstack-check}
14227
14228 Activates stack overflow checking. For full details of the effect and use of
14229 this switch see @ref{f4,,Stack Overflow Checking}.
14230 @end table
14231
14232 @geindex Unsuppress
14233
14234 The setting of these switches only controls the default setting of the
14235 checks. You may modify them using either @code{Suppress} (to remove
14236 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
14237 the program source.
14238
14239 @node Using gcc for Syntax Checking,Using gcc for Semantic Checking,Run-Time Checks,Compiler Switches
14240 @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}
14241 @subsection Using @code{gcc} for Syntax Checking
14242
14243
14244 @geindex -gnats (gcc)
14245
14246
14247 @table @asis
14248
14249 @item @code{-gnats}
14250
14251 The @code{s} stands for 'syntax'.
14252
14253 Run GNAT in syntax checking only mode. For
14254 example, the command
14255
14256 @example
14257 $ gcc -c -gnats x.adb
14258 @end example
14259
14260 compiles file @code{x.adb} in syntax-check-only mode. You can check a
14261 series of files in a single command
14262 , and can use wild cards to specify such a group of files.
14263 Note that you must specify the @code{-c} (compile
14264 only) flag in addition to the @code{-gnats} flag.
14265
14266 You may use other switches in conjunction with @code{-gnats}. In
14267 particular, @code{-gnatl} and @code{-gnatv} are useful to control the
14268 format of any generated error messages.
14269
14270 When the source file is empty or contains only empty lines and/or comments,
14271 the output is a warning:
14272
14273 @example
14274 $ gcc -c -gnats -x ada toto.txt
14275 toto.txt:1:01: warning: empty file, contains no compilation units
14276 $
14277 @end example
14278
14279 Otherwise, the output is simply the error messages, if any. No object file or
14280 ALI file is generated by a syntax-only compilation. Also, no units other
14281 than the one specified are accessed. For example, if a unit @code{X}
14282 @emph{with}s a unit @code{Y}, compiling unit @code{X} in syntax
14283 check only mode does not access the source file containing unit
14284 @code{Y}.
14285
14286 @geindex Multiple units
14287 @geindex syntax checking
14288
14289 Normally, GNAT allows only a single unit in a source file. However, this
14290 restriction does not apply in syntax-check-only mode, and it is possible
14291 to check a file containing multiple compilation units concatenated
14292 together. This is primarily used by the @code{gnatchop} utility
14293 (@ref{36,,Renaming Files with gnatchop}).
14294 @end table
14295
14296 @node Using gcc for Semantic Checking,Compiling Different Versions of Ada,Using gcc for Syntax Checking,Compiler Switches
14297 @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}
14298 @subsection Using @code{gcc} for Semantic Checking
14299
14300
14301 @geindex -gnatc (gcc)
14302
14303
14304 @table @asis
14305
14306 @item @code{-gnatc}
14307
14308 The @code{c} stands for 'check'.
14309 Causes the compiler to operate in semantic check mode,
14310 with full checking for all illegalities specified in the
14311 Ada Reference Manual, but without generation of any object code
14312 (no object file is generated).
14313
14314 Because dependent files must be accessed, you must follow the GNAT
14315 semantic restrictions on file structuring to operate in this mode:
14316
14317
14318 @itemize *
14319
14320 @item
14321 The needed source files must be accessible
14322 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
14323
14324 @item
14325 Each file must contain only one compilation unit.
14326
14327 @item
14328 The file name and unit name must match (@ref{52,,File Naming Rules}).
14329 @end itemize
14330
14331 The output consists of error messages as appropriate. No object file is
14332 generated. An @code{ALI} file is generated for use in the context of
14333 cross-reference tools, but this file is marked as not being suitable
14334 for binding (since no object file is generated).
14335 The checking corresponds exactly to the notion of
14336 legality in the Ada Reference Manual.
14337
14338 Any unit can be compiled in semantics-checking-only mode, including
14339 units that would not normally be compiled (subunits,
14340 and specifications where a separate body is present).
14341 @end table
14342
14343 @node Compiling Different Versions of Ada,Character Set Control,Using gcc for Semantic Checking,Compiler Switches
14344 @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}
14345 @subsection Compiling Different Versions of Ada
14346
14347
14348 The switches described in this section allow you to explicitly specify
14349 the version of the Ada language that your programs are written in.
14350 The default mode is Ada 2012,
14351 but you can also specify Ada 95, Ada 2005 mode, or
14352 indicate Ada 83 compatibility mode.
14353
14354 @geindex Compatibility with Ada 83
14355
14356 @geindex -gnat83 (gcc)
14357
14358 @geindex ACVC
14359 @geindex Ada 83 tests
14360
14361 @geindex Ada 83 mode
14362
14363
14364 @table @asis
14365
14366 @item @code{-gnat83} (Ada 83 Compatibility Mode)
14367
14368 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
14369 specifies that the program is to be compiled in Ada 83 mode. With
14370 @code{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
14371 semantics where this can be done easily.
14372 It is not possible to guarantee this switch does a perfect
14373 job; some subtle tests, such as are
14374 found in earlier ACVC tests (and that have been removed from the ACATS suite
14375 for Ada 95), might not compile correctly.
14376 Nevertheless, this switch may be useful in some circumstances, for example
14377 where, due to contractual reasons, existing code needs to be maintained
14378 using only Ada 83 features.
14379
14380 With few exceptions (most notably the need to use @code{<>} on
14381 unconstrained
14382 @geindex Generic formal parameters
14383 generic formal parameters,
14384 the use of the new Ada 95 / Ada 2005
14385 reserved words, and the use of packages
14386 with optional bodies), it is not necessary to specify the
14387 @code{-gnat83} switch when compiling Ada 83 programs, because, with rare
14388 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
14389 a correct Ada 83 program is usually also a correct program
14390 in these later versions of the language standard. For further information
14391 please refer to the @emph{Compatibility and Porting Guide} chapter in the
14392 @cite{GNAT Reference Manual}.
14393 @end table
14394
14395 @geindex -gnat95 (gcc)
14396
14397 @geindex Ada 95 mode
14398
14399
14400 @table @asis
14401
14402 @item @code{-gnat95} (Ada 95 mode)
14403
14404 This switch directs the compiler to implement the Ada 95 version of the
14405 language.
14406 Since Ada 95 is almost completely upwards
14407 compatible with Ada 83, Ada 83 programs may generally be compiled using
14408 this switch (see the description of the @code{-gnat83} switch for further
14409 information about Ada 83 mode).
14410 If an Ada 2005 program is compiled in Ada 95 mode,
14411 uses of the new Ada 2005 features will cause error
14412 messages or warnings.
14413
14414 This switch also can be used to cancel the effect of a previous
14415 @code{-gnat83}, @code{-gnat05/2005}, or @code{-gnat12/2012}
14416 switch earlier in the command line.
14417 @end table
14418
14419 @geindex -gnat05 (gcc)
14420
14421 @geindex -gnat2005 (gcc)
14422
14423 @geindex Ada 2005 mode
14424
14425
14426 @table @asis
14427
14428 @item @code{-gnat05} or @code{-gnat2005} (Ada 2005 mode)
14429
14430 This switch directs the compiler to implement the Ada 2005 version of the
14431 language, as documented in the official Ada standards document.
14432 Since Ada 2005 is almost completely upwards
14433 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
14434 may generally be compiled using this switch (see the description of the
14435 @code{-gnat83} and @code{-gnat95} switches for further
14436 information).
14437 @end table
14438
14439 @geindex -gnat12 (gcc)
14440
14441 @geindex -gnat2012 (gcc)
14442
14443 @geindex Ada 2012 mode
14444
14445
14446 @table @asis
14447
14448 @item @code{-gnat12} or @code{-gnat2012} (Ada 2012 mode)
14449
14450 This switch directs the compiler to implement the Ada 2012 version of the
14451 language (also the default).
14452 Since Ada 2012 is almost completely upwards
14453 compatible with Ada 2005 (and thus also with Ada 83, and Ada 95),
14454 Ada 83 and Ada 95 programs
14455 may generally be compiled using this switch (see the description of the
14456 @code{-gnat83}, @code{-gnat95}, and @code{-gnat05/2005} switches
14457 for further information).
14458 @end table
14459
14460 @geindex -gnatX (gcc)
14461
14462 @geindex Ada language extensions
14463
14464 @geindex GNAT extensions
14465
14466
14467 @table @asis
14468
14469 @item @code{-gnatX} (Enable GNAT Extensions)
14470
14471 This switch directs the compiler to implement the latest version of the
14472 language (currently Ada 2012) and also to enable certain GNAT implementation
14473 extensions that are not part of any Ada standard. For a full list of these
14474 extensions, see the GNAT reference manual.
14475 @end table
14476
14477 @node Character Set Control,File Naming Control,Compiling Different Versions of Ada,Compiler Switches
14478 @anchor{gnat_ugn/building_executable_programs_with_gnat id23}@anchor{10a}@anchor{gnat_ugn/building_executable_programs_with_gnat character-set-control}@anchor{48}
14479 @subsection Character Set Control
14480
14481
14482 @geindex -gnati (gcc)
14483
14484
14485 @table @asis
14486
14487 @item @code{-gnati@emph{c}}
14488
14489 Normally GNAT recognizes the Latin-1 character set in source program
14490 identifiers, as described in the Ada Reference Manual.
14491 This switch causes
14492 GNAT to recognize alternate character sets in identifiers. @code{c} is a
14493 single character indicating the character set, as follows:
14494
14495
14496 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14497 @item
14498
14499 @emph{1}
14500
14501 @tab
14502
14503 ISO 8859-1 (Latin-1) identifiers
14504
14505 @item
14506
14507 @emph{2}
14508
14509 @tab
14510
14511 ISO 8859-2 (Latin-2) letters allowed in identifiers
14512
14513 @item
14514
14515 @emph{3}
14516
14517 @tab
14518
14519 ISO 8859-3 (Latin-3) letters allowed in identifiers
14520
14521 @item
14522
14523 @emph{4}
14524
14525 @tab
14526
14527 ISO 8859-4 (Latin-4) letters allowed in identifiers
14528
14529 @item
14530
14531 @emph{5}
14532
14533 @tab
14534
14535 ISO 8859-5 (Cyrillic) letters allowed in identifiers
14536
14537 @item
14538
14539 @emph{9}
14540
14541 @tab
14542
14543 ISO 8859-15 (Latin-9) letters allowed in identifiers
14544
14545 @item
14546
14547 @emph{p}
14548
14549 @tab
14550
14551 IBM PC letters (code page 437) allowed in identifiers
14552
14553 @item
14554
14555 @emph{8}
14556
14557 @tab
14558
14559 IBM PC letters (code page 850) allowed in identifiers
14560
14561 @item
14562
14563 @emph{f}
14564
14565 @tab
14566
14567 Full upper-half codes allowed in identifiers
14568
14569 @item
14570
14571 @emph{n}
14572
14573 @tab
14574
14575 No upper-half codes allowed in identifiers
14576
14577 @item
14578
14579 @emph{w}
14580
14581 @tab
14582
14583 Wide-character codes (that is, codes greater than 255)
14584 allowed in identifiers
14585
14586 @end multitable
14587
14588
14589 See @ref{3e,,Foreign Language Representation} for full details on the
14590 implementation of these character sets.
14591 @end table
14592
14593 @geindex -gnatW (gcc)
14594
14595
14596 @table @asis
14597
14598 @item @code{-gnatW@emph{e}}
14599
14600 Specify the method of encoding for wide characters.
14601 @code{e} is one of the following:
14602
14603
14604 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14605 @item
14606
14607 @emph{h}
14608
14609 @tab
14610
14611 Hex encoding (brackets coding also recognized)
14612
14613 @item
14614
14615 @emph{u}
14616
14617 @tab
14618
14619 Upper half encoding (brackets encoding also recognized)
14620
14621 @item
14622
14623 @emph{s}
14624
14625 @tab
14626
14627 Shift/JIS encoding (brackets encoding also recognized)
14628
14629 @item
14630
14631 @emph{e}
14632
14633 @tab
14634
14635 EUC encoding (brackets encoding also recognized)
14636
14637 @item
14638
14639 @emph{8}
14640
14641 @tab
14642
14643 UTF-8 encoding (brackets encoding also recognized)
14644
14645 @item
14646
14647 @emph{b}
14648
14649 @tab
14650
14651 Brackets encoding only (default value)
14652
14653 @end multitable
14654
14655
14656 For full details on these encoding
14657 methods see @ref{4e,,Wide_Character Encodings}.
14658 Note that brackets coding is always accepted, even if one of the other
14659 options is specified, so for example @code{-gnatW8} specifies that both
14660 brackets and UTF-8 encodings will be recognized. The units that are
14661 with'ed directly or indirectly will be scanned using the specified
14662 representation scheme, and so if one of the non-brackets scheme is
14663 used, it must be used consistently throughout the program. However,
14664 since brackets encoding is always recognized, it may be conveniently
14665 used in standard libraries, allowing these libraries to be used with
14666 any of the available coding schemes.
14667
14668 Note that brackets encoding only applies to program text. Within comments,
14669 brackets are considered to be normal graphic characters, and bracket sequences
14670 are never recognized as wide characters.
14671
14672 If no @code{-gnatW?} parameter is present, then the default
14673 representation is normally Brackets encoding only. However, if the
14674 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
14675 byte order mark or BOM for UTF-8), then these three characters are
14676 skipped and the default representation for the file is set to UTF-8.
14677
14678 Note that the wide character representation that is specified (explicitly
14679 or by default) for the main program also acts as the default encoding used
14680 for Wide_Text_IO files if not specifically overridden by a WCEM form
14681 parameter.
14682 @end table
14683
14684 When no @code{-gnatW?} is specified, then characters (other than wide
14685 characters represented using brackets notation) are treated as 8-bit
14686 Latin-1 codes. The codes recognized are the Latin-1 graphic characters,
14687 and ASCII format effectors (CR, LF, HT, VT). Other lower half control
14688 characters in the range 16#00#..16#1F# are not accepted in program text
14689 or in comments. Upper half control characters (16#80#..16#9F#) are rejected
14690 in program text, but allowed and ignored in comments. Note in particular
14691 that the Next Line (NEL) character whose encoding is 16#85# is not recognized
14692 as an end of line in this default mode. If your source program contains
14693 instances of the NEL character used as a line terminator,
14694 you must use UTF-8 encoding for the whole
14695 source program. In default mode, all lines must be ended by a standard
14696 end of line sequence (CR, CR/LF, or LF).
14697
14698 Note that the convention of simply accepting all upper half characters in
14699 comments means that programs that use standard ASCII for program text, but
14700 UTF-8 encoding for comments are accepted in default mode, providing that the
14701 comments are ended by an appropriate (CR, or CR/LF, or LF) line terminator.
14702 This is a common mode for many programs with foreign language comments.
14703
14704 @node File Naming Control,Subprogram Inlining Control,Character Set Control,Compiler Switches
14705 @anchor{gnat_ugn/building_executable_programs_with_gnat file-naming-control}@anchor{10b}@anchor{gnat_ugn/building_executable_programs_with_gnat id24}@anchor{10c}
14706 @subsection File Naming Control
14707
14708
14709 @geindex -gnatk (gcc)
14710
14711
14712 @table @asis
14713
14714 @item @code{-gnatk@emph{n}}
14715
14716 Activates file name 'krunching'. @code{n}, a decimal integer in the range
14717 1-999, indicates the maximum allowable length of a file name (not
14718 including the @code{.ads} or @code{.adb} extension). The default is not
14719 to enable file name krunching.
14720
14721 For the source file naming rules, @ref{52,,File Naming Rules}.
14722 @end table
14723
14724 @node Subprogram Inlining Control,Auxiliary Output Control,File Naming Control,Compiler Switches
14725 @anchor{gnat_ugn/building_executable_programs_with_gnat subprogram-inlining-control}@anchor{10d}@anchor{gnat_ugn/building_executable_programs_with_gnat id25}@anchor{10e}
14726 @subsection Subprogram Inlining Control
14727
14728
14729 @geindex -gnatn (gcc)
14730
14731
14732 @table @asis
14733
14734 @item @code{-gnatn[12]}
14735
14736 The @code{n} here is intended to suggest the first syllable of the word 'inline'.
14737 GNAT recognizes and processes @code{Inline} pragmas. However, for inlining to
14738 actually occur, optimization must be enabled and, by default, inlining of
14739 subprograms across units is not performed. If you want to additionally
14740 enable inlining of subprograms specified by pragma @code{Inline} across units,
14741 you must also specify this switch.
14742
14743 In the absence of this switch, GNAT does not attempt inlining across units
14744 and does not access the bodies of subprograms for which @code{pragma Inline} is
14745 specified if they are not in the current unit.
14746
14747 You can optionally specify the inlining level: 1 for moderate inlining across
14748 units, which is a good compromise between compilation times and performances
14749 at run time, or 2 for full inlining across units, which may bring about
14750 longer compilation times. If no inlining level is specified, the compiler will
14751 pick it based on the optimization level: 1 for @code{-O1}, @code{-O2} or
14752 @code{-Os} and 2 for @code{-O3}.
14753
14754 If you specify this switch the compiler will access these bodies,
14755 creating an extra source dependency for the resulting object file, and
14756 where possible, the call will be inlined.
14757 For further details on when inlining is possible
14758 see @ref{10f,,Inlining of Subprograms}.
14759 @end table
14760
14761 @geindex -gnatN (gcc)
14762
14763
14764 @table @asis
14765
14766 @item @code{-gnatN}
14767
14768 This switch activates front-end inlining which also
14769 generates additional dependencies.
14770
14771 When using a gcc-based back end (in practice this means using any version
14772 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
14773 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
14774 Historically front end inlining was more extensive than the gcc back end
14775 inlining, but that is no longer the case.
14776 @end table
14777
14778 @node Auxiliary Output Control,Debugging Control,Subprogram Inlining Control,Compiler Switches
14779 @anchor{gnat_ugn/building_executable_programs_with_gnat auxiliary-output-control}@anchor{110}@anchor{gnat_ugn/building_executable_programs_with_gnat id26}@anchor{111}
14780 @subsection Auxiliary Output Control
14781
14782
14783 @geindex -gnatt (gcc)
14784
14785 @geindex Writing internal trees
14786
14787 @geindex Internal trees
14788 @geindex writing to file
14789
14790
14791 @table @asis
14792
14793 @item @code{-gnatt}
14794
14795 Causes GNAT to write the internal tree for a unit to a file (with the
14796 extension @code{.adt}.
14797 This not normally required, but is used by separate analysis tools.
14798 Typically
14799 these tools do the necessary compilations automatically, so you should
14800 not have to specify this switch in normal operation.
14801 Note that the combination of switches @code{-gnatct}
14802 generates a tree in the form required by ASIS applications.
14803 @end table
14804
14805 @geindex -gnatu (gcc)
14806
14807
14808 @table @asis
14809
14810 @item @code{-gnatu}
14811
14812 Print a list of units required by this compilation on @code{stdout}.
14813 The listing includes all units on which the unit being compiled depends
14814 either directly or indirectly.
14815 @end table
14816
14817 @geindex -pass-exit-codes (gcc)
14818
14819
14820 @table @asis
14821
14822 @item @code{-pass-exit-codes}
14823
14824 If this switch is not used, the exit code returned by @code{gcc} when
14825 compiling multiple files indicates whether all source files have
14826 been successfully used to generate object files or not.
14827
14828 When @code{-pass-exit-codes} is used, @code{gcc} exits with an extended
14829 exit status and allows an integrated development environment to better
14830 react to a compilation failure. Those exit status are:
14831
14832
14833 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14834 @item
14835
14836 @emph{5}
14837
14838 @tab
14839
14840 There was an error in at least one source file.
14841
14842 @item
14843
14844 @emph{3}
14845
14846 @tab
14847
14848 At least one source file did not generate an object file.
14849
14850 @item
14851
14852 @emph{2}
14853
14854 @tab
14855
14856 The compiler died unexpectedly (internal error for example).
14857
14858 @item
14859
14860 @emph{0}
14861
14862 @tab
14863
14864 An object file has been generated for every source file.
14865
14866 @end multitable
14867
14868 @end table
14869
14870 @node Debugging Control,Exception Handling Control,Auxiliary Output Control,Compiler Switches
14871 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-control}@anchor{112}@anchor{gnat_ugn/building_executable_programs_with_gnat id27}@anchor{113}
14872 @subsection Debugging Control
14873
14874
14875 @quotation
14876
14877 @geindex Debugging options
14878 @end quotation
14879
14880 @geindex -gnatd (gcc)
14881
14882
14883 @table @asis
14884
14885 @item @code{-gnatd@emph{x}}
14886
14887 Activate internal debugging switches. @code{x} is a letter or digit, or
14888 string of letters or digits, which specifies the type of debugging
14889 outputs desired. Normally these are used only for internal development
14890 or system debugging purposes. You can find full documentation for these
14891 switches in the body of the @code{Debug} unit in the compiler source
14892 file @code{debug.adb}.
14893 @end table
14894
14895 @geindex -gnatG (gcc)
14896
14897
14898 @table @asis
14899
14900 @item @code{-gnatG[=@emph{nn}]}
14901
14902 This switch causes the compiler to generate auxiliary output containing
14903 a pseudo-source listing of the generated expanded code. Like most Ada
14904 compilers, GNAT works by first transforming the high level Ada code into
14905 lower level constructs. For example, tasking operations are transformed
14906 into calls to the tasking run-time routines. A unique capability of GNAT
14907 is to list this expanded code in a form very close to normal Ada source.
14908 This is very useful in understanding the implications of various Ada
14909 usage on the efficiency of the generated code. There are many cases in
14910 Ada (e.g., the use of controlled types), where simple Ada statements can
14911 generate a lot of run-time code. By using @code{-gnatG} you can identify
14912 these cases, and consider whether it may be desirable to modify the coding
14913 approach to improve efficiency.
14914
14915 The optional parameter @code{nn} if present after -gnatG specifies an
14916 alternative maximum line length that overrides the normal default of 72.
14917 This value is in the range 40-999999, values less than 40 being silently
14918 reset to 40. The equal sign is optional.
14919
14920 The format of the output is very similar to standard Ada source, and is
14921 easily understood by an Ada programmer. The following special syntactic
14922 additions correspond to low level features used in the generated code that
14923 do not have any exact analogies in pure Ada source form. The following
14924 is a partial list of these special constructions. See the spec
14925 of package @code{Sprint} in file @code{sprint.ads} for a full list.
14926
14927 @geindex -gnatL (gcc)
14928
14929 If the switch @code{-gnatL} is used in conjunction with
14930 @code{-gnatG}, then the original source lines are interspersed
14931 in the expanded source (as comment lines with the original line number).
14932
14933
14934 @table @asis
14935
14936 @item @code{new @emph{xxx} [storage_pool = @emph{yyy}]}
14937
14938 Shows the storage pool being used for an allocator.
14939
14940 @item @code{at end @emph{procedure-name};}
14941
14942 Shows the finalization (cleanup) procedure for a scope.
14943
14944 @item @code{(if @emph{expr} then @emph{expr} else @emph{expr})}
14945
14946 Conditional expression equivalent to the @code{x?y:z} construction in C.
14947
14948 @item @code{@emph{target}^(@emph{source})}
14949
14950 A conversion with floating-point truncation instead of rounding.
14951
14952 @item @code{@emph{target}?(@emph{source})}
14953
14954 A conversion that bypasses normal Ada semantic checking. In particular
14955 enumeration types and fixed-point types are treated simply as integers.
14956
14957 @item @code{@emph{target}?^(@emph{source})}
14958
14959 Combines the above two cases.
14960 @end table
14961
14962 @code{@emph{x} #/ @emph{y}}
14963
14964 @code{@emph{x} #mod @emph{y}}
14965
14966 @code{@emph{x} # @emph{y}}
14967
14968
14969 @table @asis
14970
14971 @item @code{@emph{x} #rem @emph{y}}
14972
14973 A division or multiplication of fixed-point values which are treated as
14974 integers without any kind of scaling.
14975
14976 @item @code{free @emph{expr} [storage_pool = @emph{xxx}]}
14977
14978 Shows the storage pool associated with a @code{free} statement.
14979
14980 @item @code{[subtype or type declaration]}
14981
14982 Used to list an equivalent declaration for an internally generated
14983 type that is referenced elsewhere in the listing.
14984
14985 @item @code{freeze @emph{type-name} [@emph{actions}]}
14986
14987 Shows the point at which @code{type-name} is frozen, with possible
14988 associated actions to be performed at the freeze point.
14989
14990 @item @code{reference @emph{itype}}
14991
14992 Reference (and hence definition) to internal type @code{itype}.
14993
14994 @item @code{@emph{function-name}! (@emph{arg}, @emph{arg}, @emph{arg})}
14995
14996 Intrinsic function call.
14997
14998 @item @code{@emph{label-name} : label}
14999
15000 Declaration of label @code{labelname}.
15001
15002 @item @code{#$ @emph{subprogram-name}}
15003
15004 An implicit call to a run-time support routine
15005 (to meet the requirement of H.3.1(9) in a
15006 convenient manner).
15007
15008 @item @code{@emph{expr} && @emph{expr} && @emph{expr} ... && @emph{expr}}
15009
15010 A multiple concatenation (same effect as @code{expr} & @code{expr} &
15011 @code{expr}, but handled more efficiently).
15012
15013 @item @code{[constraint_error]}
15014
15015 Raise the @code{Constraint_Error} exception.
15016
15017 @item @code{@emph{expression}'reference}
15018
15019 A pointer to the result of evaluating @{expression@}.
15020
15021 @item @code{@emph{target-type}!(@emph{source-expression})}
15022
15023 An unchecked conversion of @code{source-expression} to @code{target-type}.
15024
15025 @item @code{[@emph{numerator}/@emph{denominator}]}
15026
15027 Used to represent internal real literals (that) have no exact
15028 representation in base 2-16 (for example, the result of compile time
15029 evaluation of the expression 1.0/27.0).
15030 @end table
15031 @end table
15032
15033 @geindex -gnatD (gcc)
15034
15035
15036 @table @asis
15037
15038 @item @code{-gnatD[=nn]}
15039
15040 When used in conjunction with @code{-gnatG}, this switch causes
15041 the expanded source, as described above for
15042 @code{-gnatG} to be written to files with names
15043 @code{xxx.dg}, where @code{xxx} is the normal file name,
15044 instead of to the standard output file. For
15045 example, if the source file name is @code{hello.adb}, then a file
15046 @code{hello.adb.dg} will be written. The debugging
15047 information generated by the @code{gcc} @code{-g} switch
15048 will refer to the generated @code{xxx.dg} file. This allows
15049 you to do source level debugging using the generated code which is
15050 sometimes useful for complex code, for example to find out exactly
15051 which part of a complex construction raised an exception. This switch
15052 also suppresses generation of cross-reference information (see
15053 @code{-gnatx}) since otherwise the cross-reference information
15054 would refer to the @code{.dg} file, which would cause
15055 confusion since this is not the original source file.
15056
15057 Note that @code{-gnatD} actually implies @code{-gnatG}
15058 automatically, so it is not necessary to give both options.
15059 In other words @code{-gnatD} is equivalent to @code{-gnatDG}).
15060
15061 @geindex -gnatL (gcc)
15062
15063 If the switch @code{-gnatL} is used in conjunction with
15064 @code{-gnatDG}, then the original source lines are interspersed
15065 in the expanded source (as comment lines with the original line number).
15066
15067 The optional parameter @code{nn} if present after -gnatD specifies an
15068 alternative maximum line length that overrides the normal default of 72.
15069 This value is in the range 40-999999, values less than 40 being silently
15070 reset to 40. The equal sign is optional.
15071 @end table
15072
15073 @geindex -gnatr (gcc)
15074
15075 @geindex pragma Restrictions
15076
15077
15078 @table @asis
15079
15080 @item @code{-gnatr}
15081
15082 This switch causes pragma Restrictions to be treated as Restriction_Warnings
15083 so that violation of restrictions causes warnings rather than illegalities.
15084 This is useful during the development process when new restrictions are added
15085 or investigated. The switch also causes pragma Profile to be treated as
15086 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
15087 restriction warnings rather than restrictions.
15088 @end table
15089
15090 @geindex -gnatR (gcc)
15091
15092
15093 @table @asis
15094
15095 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
15096
15097 This switch controls output from the compiler of a listing showing
15098 representation information for declared types, objects and subprograms.
15099 For @code{-gnatR0}, no information is output (equivalent to omitting
15100 the @code{-gnatR} switch). For @code{-gnatR1} (which is the default,
15101 so @code{-gnatR} with no parameter has the same effect), size and
15102 alignment information is listed for declared array and record types.
15103
15104 For @code{-gnatR2}, size and alignment information is listed for all
15105 declared types and objects. The @code{Linker_Section} is also listed for any
15106 entity for which the @code{Linker_Section} is set explicitly or implicitly (the
15107 latter case occurs for objects of a type for which a @code{Linker_Section}
15108 is set).
15109
15110 For @code{-gnatR3}, symbolic expressions for values that are computed
15111 at run time for records are included. These symbolic expressions have
15112 a mostly obvious format with #n being used to represent the value of the
15113 n'th discriminant. See source files @code{repinfo.ads/adb} in the
15114 GNAT sources for full details on the format of @code{-gnatR3} output.
15115
15116 For @code{-gnatR4}, information for relevant compiler-generated types
15117 is also listed, i.e. when they are structurally part of other declared
15118 types and objects.
15119
15120 If the switch is followed by an @code{e} (e.g. @code{-gnatR2e}), then
15121 extended representation information for record sub-components of records
15122 is included.
15123
15124 If the switch is followed by an @code{m} (e.g. @code{-gnatRm}), then
15125 subprogram conventions and parameter passing mechanisms for all the
15126 subprograms are included.
15127
15128 If the switch is followed by a @code{j} (e.g., @code{-gnatRj}), then
15129 the output is in the JSON data interchange format specified by the
15130 ECMA-404 standard. The semantic description of this JSON output is
15131 available in the specification of the Repinfo unit present in the
15132 compiler sources.
15133
15134 If the switch is followed by an @code{s} (e.g., @code{-gnatR3s}), then
15135 the output is to a file with the name @code{file.rep} where file is
15136 the name of the corresponding source file, except if @cite{j`} is also
15137 specified, in which case the file name is @code{file.json}.
15138
15139 Note that it is possible for record components to have zero size. In
15140 this case, the component clause uses an obvious extension of permitted
15141 Ada syntax, for example @code{at 0 range 0 .. -1}.
15142 @end table
15143
15144 @geindex -gnatS (gcc)
15145
15146
15147 @table @asis
15148
15149 @item @code{-gnatS}
15150
15151 The use of the switch @code{-gnatS} for an
15152 Ada compilation will cause the compiler to output a
15153 representation of package Standard in a form very
15154 close to standard Ada. It is not quite possible to
15155 do this entirely in standard Ada (since new
15156 numeric base types cannot be created in standard
15157 Ada), but the output is easily
15158 readable to any Ada programmer, and is useful to
15159 determine the characteristics of target dependent
15160 types in package Standard.
15161 @end table
15162
15163 @geindex -gnatx (gcc)
15164
15165
15166 @table @asis
15167
15168 @item @code{-gnatx}
15169
15170 Normally the compiler generates full cross-referencing information in
15171 the @code{ALI} file. This information is used by a number of tools,
15172 including @code{gnatfind} and @code{gnatxref}. The @code{-gnatx} switch
15173 suppresses this information. This saves some space and may slightly
15174 speed up compilation, but means that these tools cannot be used.
15175 @end table
15176
15177 @node Exception Handling Control,Units to Sources Mapping Files,Debugging Control,Compiler Switches
15178 @anchor{gnat_ugn/building_executable_programs_with_gnat id28}@anchor{114}@anchor{gnat_ugn/building_executable_programs_with_gnat exception-handling-control}@anchor{115}
15179 @subsection Exception Handling Control
15180
15181
15182 GNAT uses two methods for handling exceptions at run time. The
15183 @code{setjmp/longjmp} method saves the context when entering
15184 a frame with an exception handler. Then when an exception is
15185 raised, the context can be restored immediately, without the
15186 need for tracing stack frames. This method provides very fast
15187 exception propagation, but introduces significant overhead for
15188 the use of exception handlers, even if no exception is raised.
15189
15190 The other approach is called 'zero cost' exception handling.
15191 With this method, the compiler builds static tables to describe
15192 the exception ranges. No dynamic code is required when entering
15193 a frame containing an exception handler. When an exception is
15194 raised, the tables are used to control a back trace of the
15195 subprogram invocation stack to locate the required exception
15196 handler. This method has considerably poorer performance for
15197 the propagation of exceptions, but there is no overhead for
15198 exception handlers if no exception is raised. Note that in this
15199 mode and in the context of mixed Ada and C/C++ programming,
15200 to propagate an exception through a C/C++ code, the C/C++ code
15201 must be compiled with the @code{-funwind-tables} GCC's
15202 option.
15203
15204 The following switches may be used to control which of the
15205 two exception handling methods is used.
15206
15207 @geindex --RTS=sjlj (gnatmake)
15208
15209
15210 @table @asis
15211
15212 @item @code{--RTS=sjlj}
15213
15214 This switch causes the setjmp/longjmp run-time (when available) to be used
15215 for exception handling. If the default
15216 mechanism for the target is zero cost exceptions, then
15217 this switch can be used to modify this default, and must be
15218 used for all units in the partition.
15219 This option is rarely used. One case in which it may be
15220 advantageous is if you have an application where exception
15221 raising is common and the overall performance of the
15222 application is improved by favoring exception propagation.
15223 @end table
15224
15225 @geindex --RTS=zcx (gnatmake)
15226
15227 @geindex Zero Cost Exceptions
15228
15229
15230 @table @asis
15231
15232 @item @code{--RTS=zcx}
15233
15234 This switch causes the zero cost approach to be used
15235 for exception handling. If this is the default mechanism for the
15236 target (see below), then this switch is unneeded. If the default
15237 mechanism for the target is setjmp/longjmp exceptions, then
15238 this switch can be used to modify this default, and must be
15239 used for all units in the partition.
15240 This option can only be used if the zero cost approach
15241 is available for the target in use, otherwise it will generate an error.
15242 @end table
15243
15244 The same option @code{--RTS} must be used both for @code{gcc}
15245 and @code{gnatbind}. Passing this option to @code{gnatmake}
15246 (@ref{dc,,Switches for gnatmake}) will ensure the required consistency
15247 through the compilation and binding steps.
15248
15249 @node Units to Sources Mapping Files,Code Generation Control,Exception Handling Control,Compiler Switches
15250 @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}
15251 @subsection Units to Sources Mapping Files
15252
15253
15254 @geindex -gnatem (gcc)
15255
15256
15257 @table @asis
15258
15259 @item @code{-gnatem=@emph{path}}
15260
15261 A mapping file is a way to communicate to the compiler two mappings:
15262 from unit names to file names (without any directory information) and from
15263 file names to path names (with full directory information). These mappings
15264 are used by the compiler to short-circuit the path search.
15265
15266 The use of mapping files is not required for correct operation of the
15267 compiler, but mapping files can improve efficiency, particularly when
15268 sources are read over a slow network connection. In normal operation,
15269 you need not be concerned with the format or use of mapping files,
15270 and the @code{-gnatem} switch is not a switch that you would use
15271 explicitly. It is intended primarily for use by automatic tools such as
15272 @code{gnatmake} running under the project file facility. The
15273 description here of the format of mapping files is provided
15274 for completeness and for possible use by other tools.
15275
15276 A mapping file is a sequence of sets of three lines. In each set, the
15277 first line is the unit name, in lower case, with @code{%s} appended
15278 for specs and @code{%b} appended for bodies; the second line is the
15279 file name; and the third line is the path name.
15280
15281 Example:
15282
15283 @example
15284 main%b
15285 main.2.ada
15286 /gnat/project1/sources/main.2.ada
15287 @end example
15288
15289 When the switch @code{-gnatem} is specified, the compiler will
15290 create in memory the two mappings from the specified file. If there is
15291 any problem (nonexistent file, truncated file or duplicate entries),
15292 no mapping will be created.
15293
15294 Several @code{-gnatem} switches may be specified; however, only the
15295 last one on the command line will be taken into account.
15296
15297 When using a project file, @code{gnatmake} creates a temporary
15298 mapping file and communicates it to the compiler using this switch.
15299 @end table
15300
15301 @node Code Generation Control,,Units to Sources Mapping Files,Compiler Switches
15302 @anchor{gnat_ugn/building_executable_programs_with_gnat code-generation-control}@anchor{117}@anchor{gnat_ugn/building_executable_programs_with_gnat id30}@anchor{118}
15303 @subsection Code Generation Control
15304
15305
15306 The GCC technology provides a wide range of target dependent
15307 @code{-m} switches for controlling
15308 details of code generation with respect to different versions of
15309 architectures. This includes variations in instruction sets (e.g.,
15310 different members of the power pc family), and different requirements
15311 for optimal arrangement of instructions (e.g., different members of
15312 the x86 family). The list of available @code{-m} switches may be
15313 found in the GCC documentation.
15314
15315 Use of these @code{-m} switches may in some cases result in improved
15316 code performance.
15317
15318 The GNAT technology is tested and qualified without any
15319 @code{-m} switches,
15320 so generally the most reliable approach is to avoid the use of these
15321 switches. However, we generally expect most of these switches to work
15322 successfully with GNAT, and many customers have reported successful
15323 use of these options.
15324
15325 Our general advice is to avoid the use of @code{-m} switches unless
15326 special needs lead to requirements in this area. In particular,
15327 there is no point in using @code{-m} switches to improve performance
15328 unless you actually see a performance improvement.
15329
15330 @node Linker Switches,Binding with gnatbind,Compiler Switches,Building Executable Programs with GNAT
15331 @anchor{gnat_ugn/building_executable_programs_with_gnat linker-switches}@anchor{119}@anchor{gnat_ugn/building_executable_programs_with_gnat id31}@anchor{11a}
15332 @section Linker Switches
15333
15334
15335 Linker switches can be specified after @code{-largs} builder switch.
15336
15337 @geindex -fuse-ld=name
15338
15339
15340 @table @asis
15341
15342 @item @code{-fuse-ld=@emph{name}}
15343
15344 Linker to be used. The default is @code{bfd} for @code{ld.bfd},
15345 the alternative being @code{gold} for @code{ld.gold}. The later is
15346 a more recent and faster linker, but only available on GNU/Linux
15347 platforms.
15348 @end table
15349
15350 @node Binding with gnatbind,Linking with gnatlink,Linker Switches,Building Executable Programs with GNAT
15351 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-gnatbind}@anchor{1d}@anchor{gnat_ugn/building_executable_programs_with_gnat id32}@anchor{11b}
15352 @section Binding with @code{gnatbind}
15353
15354
15355 @geindex gnatbind
15356
15357 This chapter describes the GNAT binder, @code{gnatbind}, which is used
15358 to bind compiled GNAT objects.
15359
15360 The @code{gnatbind} program performs four separate functions:
15361
15362
15363 @itemize *
15364
15365 @item
15366 Checks that a program is consistent, in accordance with the rules in
15367 Chapter 10 of the Ada Reference Manual. In particular, error
15368 messages are generated if a program uses inconsistent versions of a
15369 given unit.
15370
15371 @item
15372 Checks that an acceptable order of elaboration exists for the program
15373 and issues an error message if it cannot find an order of elaboration
15374 that satisfies the rules in Chapter 10 of the Ada Language Manual.
15375
15376 @item
15377 Generates a main program incorporating the given elaboration order.
15378 This program is a small Ada package (body and spec) that
15379 must be subsequently compiled
15380 using the GNAT compiler. The necessary compilation step is usually
15381 performed automatically by @code{gnatlink}. The two most important
15382 functions of this program
15383 are to call the elaboration routines of units in an appropriate order
15384 and to call the main program.
15385
15386 @item
15387 Determines the set of object files required by the given main program.
15388 This information is output in the forms of comments in the generated program,
15389 to be read by the @code{gnatlink} utility used to link the Ada application.
15390 @end itemize
15391
15392 @menu
15393 * Running gnatbind::
15394 * Switches for gnatbind::
15395 * Command-Line Access::
15396 * Search Paths for gnatbind::
15397 * Examples of gnatbind Usage::
15398
15399 @end menu
15400
15401 @node Running gnatbind,Switches for gnatbind,,Binding with gnatbind
15402 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatbind}@anchor{11c}@anchor{gnat_ugn/building_executable_programs_with_gnat id33}@anchor{11d}
15403 @subsection Running @code{gnatbind}
15404
15405
15406 The form of the @code{gnatbind} command is
15407
15408 @example
15409 $ gnatbind [ switches ] mainprog[.ali] [ switches ]
15410 @end example
15411
15412 where @code{mainprog.adb} is the Ada file containing the main program
15413 unit body. @code{gnatbind} constructs an Ada
15414 package in two files whose names are
15415 @code{b~mainprog.ads}, and @code{b~mainprog.adb}.
15416 For example, if given the
15417 parameter @code{hello.ali}, for a main program contained in file
15418 @code{hello.adb}, the binder output files would be @code{b~hello.ads}
15419 and @code{b~hello.adb}.
15420
15421 When doing consistency checking, the binder takes into consideration
15422 any source files it can locate. For example, if the binder determines
15423 that the given main program requires the package @code{Pack}, whose
15424 @code{.ALI}
15425 file is @code{pack.ali} and whose corresponding source spec file is
15426 @code{pack.ads}, it attempts to locate the source file @code{pack.ads}
15427 (using the same search path conventions as previously described for the
15428 @code{gcc} command). If it can locate this source file, it checks that
15429 the time stamps
15430 or source checksums of the source and its references to in @code{ALI} files
15431 match. In other words, any @code{ALI} files that mentions this spec must have
15432 resulted from compiling this version of the source file (or in the case
15433 where the source checksums match, a version close enough that the
15434 difference does not matter).
15435
15436 @geindex Source files
15437 @geindex use by binder
15438
15439 The effect of this consistency checking, which includes source files, is
15440 that the binder ensures that the program is consistent with the latest
15441 version of the source files that can be located at bind time. Editing a
15442 source file without compiling files that depend on the source file cause
15443 error messages to be generated by the binder.
15444
15445 For example, suppose you have a main program @code{hello.adb} and a
15446 package @code{P}, from file @code{p.ads} and you perform the following
15447 steps:
15448
15449
15450 @itemize *
15451
15452 @item
15453 Enter @code{gcc -c hello.adb} to compile the main program.
15454
15455 @item
15456 Enter @code{gcc -c p.ads} to compile package @code{P}.
15457
15458 @item
15459 Edit file @code{p.ads}.
15460
15461 @item
15462 Enter @code{gnatbind hello}.
15463 @end itemize
15464
15465 At this point, the file @code{p.ali} contains an out-of-date time stamp
15466 because the file @code{p.ads} has been edited. The attempt at binding
15467 fails, and the binder generates the following error messages:
15468
15469 @example
15470 error: "hello.adb" must be recompiled ("p.ads" has been modified)
15471 error: "p.ads" has been modified and must be recompiled
15472 @end example
15473
15474 Now both files must be recompiled as indicated, and then the bind can
15475 succeed, generating a main program. You need not normally be concerned
15476 with the contents of this file, but for reference purposes a sample
15477 binder output file is given in @ref{e,,Example of Binder Output File}.
15478
15479 In most normal usage, the default mode of @code{gnatbind} which is to
15480 generate the main package in Ada, as described in the previous section.
15481 In particular, this means that any Ada programmer can read and understand
15482 the generated main program. It can also be debugged just like any other
15483 Ada code provided the @code{-g} switch is used for
15484 @code{gnatbind} and @code{gnatlink}.
15485
15486 @node Switches for gnatbind,Command-Line Access,Running gnatbind,Binding with gnatbind
15487 @anchor{gnat_ugn/building_executable_programs_with_gnat id34}@anchor{11e}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatbind}@anchor{11f}
15488 @subsection Switches for @code{gnatbind}
15489
15490
15491 The following switches are available with @code{gnatbind}; details will
15492 be presented in subsequent sections.
15493
15494 @geindex --version (gnatbind)
15495
15496
15497 @table @asis
15498
15499 @item @code{--version}
15500
15501 Display Copyright and version, then exit disregarding all other options.
15502 @end table
15503
15504 @geindex --help (gnatbind)
15505
15506
15507 @table @asis
15508
15509 @item @code{--help}
15510
15511 If @code{--version} was not used, display usage, then exit disregarding
15512 all other options.
15513 @end table
15514
15515 @geindex -a (gnatbind)
15516
15517
15518 @table @asis
15519
15520 @item @code{-a}
15521
15522 Indicates that, if supported by the platform, the adainit procedure should
15523 be treated as an initialisation routine by the linker (a constructor). This
15524 is intended to be used by the Project Manager to automatically initialize
15525 shared Stand-Alone Libraries.
15526 @end table
15527
15528 @geindex -aO (gnatbind)
15529
15530
15531 @table @asis
15532
15533 @item @code{-aO}
15534
15535 Specify directory to be searched for ALI files.
15536 @end table
15537
15538 @geindex -aI (gnatbind)
15539
15540
15541 @table @asis
15542
15543 @item @code{-aI}
15544
15545 Specify directory to be searched for source file.
15546 @end table
15547
15548 @geindex -A (gnatbind)
15549
15550
15551 @table @asis
15552
15553 @item @code{-A[=@emph{filename}]}
15554
15555 Output ALI list (to standard output or to the named file).
15556 @end table
15557
15558 @geindex -b (gnatbind)
15559
15560
15561 @table @asis
15562
15563 @item @code{-b}
15564
15565 Generate brief messages to @code{stderr} even if verbose mode set.
15566 @end table
15567
15568 @geindex -c (gnatbind)
15569
15570
15571 @table @asis
15572
15573 @item @code{-c}
15574
15575 Check only, no generation of binder output file.
15576 @end table
15577
15578 @geindex -dnn[k|m] (gnatbind)
15579
15580
15581 @table @asis
15582
15583 @item @code{-d@emph{nn}[k|m]}
15584
15585 This switch can be used to change the default task stack size value
15586 to a specified size @code{nn}, which is expressed in bytes by default, or
15587 in kilobytes when suffixed with @code{k} or in megabytes when suffixed
15588 with @code{m}.
15589 In the absence of a @code{[k|m]} suffix, this switch is equivalent,
15590 in effect, to completing all task specs with
15591
15592 @example
15593 pragma Storage_Size (nn);
15594 @end example
15595
15596 When they do not already have such a pragma.
15597 @end table
15598
15599 @geindex -D (gnatbind)
15600
15601
15602 @table @asis
15603
15604 @item @code{-D@emph{nn}[k|m]}
15605
15606 Set the default secondary stack size to @code{nn}. The suffix indicates whether
15607 the size is in bytes (no suffix), kilobytes (@code{k} suffix) or megabytes
15608 (@code{m} suffix).
15609
15610 The secondary stack holds objects of unconstrained types that are returned by
15611 functions, for example unconstrained Strings. The size of the secondary stack
15612 can be dynamic or fixed depending on the target.
15613
15614 For most targets, the secondary stack grows on demand and is implemented as
15615 a chain of blocks in the heap. In this case, the default secondary stack size
15616 determines the initial size of the secondary stack for each task and the
15617 smallest amount the secondary stack can grow by.
15618
15619 For Ravenscar, ZFP, and Cert run-times the size of the secondary stack is
15620 fixed. This switch can be used to change the default size of these stacks.
15621 The default secondary stack size can be overridden on a per-task basis if
15622 individual tasks have different secondary stack requirements. This is
15623 achieved through the Secondary_Stack_Size aspect that takes the size of the
15624 secondary stack in bytes.
15625 @end table
15626
15627 @geindex -e (gnatbind)
15628
15629
15630 @table @asis
15631
15632 @item @code{-e}
15633
15634 Output complete list of elaboration-order dependencies.
15635 @end table
15636
15637 @geindex -Ea (gnatbind)
15638
15639
15640 @table @asis
15641
15642 @item @code{-Ea}
15643
15644 Store tracebacks in exception occurrences when the target supports it.
15645 The "a" is for "address"; tracebacks will contain hexadecimal addresses,
15646 unless symbolic tracebacks are enabled.
15647
15648 See also the packages @code{GNAT.Traceback} and
15649 @code{GNAT.Traceback.Symbolic} for more information.
15650 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
15651 @code{gcc} option.
15652 @end table
15653
15654 @geindex -Es (gnatbind)
15655
15656
15657 @table @asis
15658
15659 @item @code{-Es}
15660
15661 Store tracebacks in exception occurrences when the target supports it.
15662 The "s" is for "symbolic"; symbolic tracebacks are enabled.
15663 @end table
15664
15665 @geindex -E (gnatbind)
15666
15667
15668 @table @asis
15669
15670 @item @code{-E}
15671
15672 Currently the same as @code{-Ea}.
15673 @end table
15674
15675 @geindex -f (gnatbind)
15676
15677
15678 @table @asis
15679
15680 @item @code{-f@emph{elab-order}}
15681
15682 Force elaboration order.
15683 @end table
15684
15685 @geindex -F (gnatbind)
15686
15687
15688 @table @asis
15689
15690 @item @code{-F}
15691
15692 Force the checks of elaboration flags. @code{gnatbind} does not normally
15693 generate checks of elaboration flags for the main executable, except when
15694 a Stand-Alone Library is used. However, there are cases when this cannot be
15695 detected by gnatbind. An example is importing an interface of a Stand-Alone
15696 Library through a pragma Import and only specifying through a linker switch
15697 this Stand-Alone Library. This switch is used to guarantee that elaboration
15698 flag checks are generated.
15699 @end table
15700
15701 @geindex -h (gnatbind)
15702
15703
15704 @table @asis
15705
15706 @item @code{-h}
15707
15708 Output usage (help) information.
15709
15710 @geindex -H32 (gnatbind)
15711
15712 @item @code{-H32}
15713
15714 Use 32-bit allocations for @code{__gnat_malloc} (and thus for access types).
15715 For further details see @ref{120,,Dynamic Allocation Control}.
15716
15717 @geindex -H64 (gnatbind)
15718
15719 @geindex __gnat_malloc
15720
15721 @item @code{-H64}
15722
15723 Use 64-bit allocations for @code{__gnat_malloc} (and thus for access types).
15724 For further details see @ref{120,,Dynamic Allocation Control}.
15725
15726 @geindex -I (gnatbind)
15727
15728 @item @code{-I}
15729
15730 Specify directory to be searched for source and ALI files.
15731
15732 @geindex -I- (gnatbind)
15733
15734 @item @code{-I-}
15735
15736 Do not look for sources in the current directory where @code{gnatbind} was
15737 invoked, and do not look for ALI files in the directory containing the
15738 ALI file named in the @code{gnatbind} command line.
15739
15740 @geindex -l (gnatbind)
15741
15742 @item @code{-l}
15743
15744 Output chosen elaboration order.
15745
15746 @geindex -L (gnatbind)
15747
15748 @item @code{-L@emph{xxx}}
15749
15750 Bind the units for library building. In this case the @code{adainit} and
15751 @code{adafinal} procedures (@ref{b4,,Binding with Non-Ada Main Programs})
15752 are renamed to @code{@emph{xxx}init} and
15753 @code{@emph{xxx}final}.
15754 Implies -n.
15755 (@ref{15,,GNAT and Libraries}, for more details.)
15756
15757 @geindex -M (gnatbind)
15758
15759 @item @code{-M@emph{xyz}}
15760
15761 Rename generated main program from main to xyz. This option is
15762 supported on cross environments only.
15763
15764 @geindex -m (gnatbind)
15765
15766 @item @code{-m@emph{n}}
15767
15768 Limit number of detected errors or warnings to @code{n}, where @code{n} is
15769 in the range 1..999999. The default value if no switch is
15770 given is 9999. If the number of warnings reaches this limit, then a
15771 message is output and further warnings are suppressed, the bind
15772 continues in this case. If the number of errors reaches this
15773 limit, then a message is output and the bind is abandoned.
15774 A value of zero means that no limit is enforced. The equal
15775 sign is optional.
15776
15777 @geindex -n (gnatbind)
15778
15779 @item @code{-n}
15780
15781 No main program.
15782
15783 @geindex -nostdinc (gnatbind)
15784
15785 @item @code{-nostdinc}
15786
15787 Do not look for sources in the system default directory.
15788
15789 @geindex -nostdlib (gnatbind)
15790
15791 @item @code{-nostdlib}
15792
15793 Do not look for library files in the system default directory.
15794
15795 @geindex --RTS (gnatbind)
15796
15797 @item @code{--RTS=@emph{rts-path}}
15798
15799 Specifies the default location of the run-time library. Same meaning as the
15800 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
15801
15802 @geindex -o (gnatbind)
15803
15804 @item @code{-o @emph{file}}
15805
15806 Name the output file @code{file} (default is @code{b~`xxx}.adb`).
15807 Note that if this option is used, then linking must be done manually,
15808 gnatlink cannot be used.
15809
15810 @geindex -O (gnatbind)
15811
15812 @item @code{-O[=@emph{filename}]}
15813
15814 Output object list (to standard output or to the named file).
15815
15816 @geindex -p (gnatbind)
15817
15818 @item @code{-p}
15819
15820 Pessimistic (worst-case) elaboration order.
15821
15822 @geindex -P (gnatbind)
15823
15824 @item @code{-P}
15825
15826 Generate binder file suitable for CodePeer.
15827
15828 @geindex -R (gnatbind)
15829
15830 @item @code{-R}
15831
15832 Output closure source list, which includes all non-run-time units that are
15833 included in the bind.
15834
15835 @geindex -Ra (gnatbind)
15836
15837 @item @code{-Ra}
15838
15839 Like @code{-R} but the list includes run-time units.
15840
15841 @geindex -s (gnatbind)
15842
15843 @item @code{-s}
15844
15845 Require all source files to be present.
15846
15847 @geindex -S (gnatbind)
15848
15849 @item @code{-S@emph{xxx}}
15850
15851 Specifies the value to be used when detecting uninitialized scalar
15852 objects with pragma Initialize_Scalars.
15853 The @code{xxx} string specified with the switch is one of:
15854
15855
15856 @itemize *
15857
15858 @item
15859 @code{in} for an invalid value.
15860
15861 If zero is invalid for the discrete type in question,
15862 then the scalar value is set to all zero bits.
15863 For signed discrete types, the largest possible negative value of
15864 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15865 For unsigned discrete types, the underlying scalar value is set to all
15866 one bits. For floating-point types, a NaN value is set
15867 (see body of package System.Scalar_Values for exact values).
15868
15869 @item
15870 @code{lo} for low value.
15871
15872 If zero is invalid for the discrete type in question,
15873 then the scalar value is set to all zero bits.
15874 For signed discrete types, the largest possible negative value of
15875 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15876 For unsigned discrete types, the underlying scalar value is set to all
15877 zero bits. For floating-point, a small value is set
15878 (see body of package System.Scalar_Values for exact values).
15879
15880 @item
15881 @code{hi} for high value.
15882
15883 If zero is invalid for the discrete type in question,
15884 then the scalar value is set to all one bits.
15885 For signed discrete types, the largest possible positive value of
15886 the underlying scalar is set (i.e. a zero bit followed by all one bits).
15887 For unsigned discrete types, the underlying scalar value is set to all
15888 one bits. For floating-point, a large value is set
15889 (see body of package System.Scalar_Values for exact values).
15890
15891 @item
15892 @code{xx} for hex value (two hex digits).
15893
15894 The underlying scalar is set to a value consisting of repeated bytes, whose
15895 value corresponds to the given value. For example if @code{BF} is given,
15896 then a 32-bit scalar value will be set to the bit patterm @code{16#BFBFBFBF#}.
15897 @end itemize
15898
15899 @geindex GNAT_INIT_SCALARS
15900
15901 In addition, you can specify @code{-Sev} to indicate that the value is
15902 to be set at run time. In this case, the program will look for an environment
15903 variable of the form @code{GNAT_INIT_SCALARS=@emph{yy}}, where @code{yy} is one
15904 of @code{in/lo/hi/@emph{xx}} with the same meanings as above.
15905 If no environment variable is found, or if it does not have a valid value,
15906 then the default is @code{in} (invalid values).
15907 @end table
15908
15909 @geindex -static (gnatbind)
15910
15911
15912 @table @asis
15913
15914 @item @code{-static}
15915
15916 Link against a static GNAT run-time.
15917
15918 @geindex -shared (gnatbind)
15919
15920 @item @code{-shared}
15921
15922 Link against a shared GNAT run-time when available.
15923
15924 @geindex -t (gnatbind)
15925
15926 @item @code{-t}
15927
15928 Tolerate time stamp and other consistency errors.
15929
15930 @geindex -T (gnatbind)
15931
15932 @item @code{-T@emph{n}}
15933
15934 Set the time slice value to @code{n} milliseconds. If the system supports
15935 the specification of a specific time slice value, then the indicated value
15936 is used. If the system does not support specific time slice values, but
15937 does support some general notion of round-robin scheduling, then any
15938 nonzero value will activate round-robin scheduling.
15939
15940 A value of zero is treated specially. It turns off time
15941 slicing, and in addition, indicates to the tasking run-time that the
15942 semantics should match as closely as possible the Annex D
15943 requirements of the Ada RM, and in particular sets the default
15944 scheduling policy to @code{FIFO_Within_Priorities}.
15945
15946 @geindex -u (gnatbind)
15947
15948 @item @code{-u@emph{n}}
15949
15950 Enable dynamic stack usage, with @code{n} results stored and displayed
15951 at program termination. A result is generated when a task
15952 terminates. Results that can't be stored are displayed on the fly, at
15953 task termination. This option is currently not supported on Itanium
15954 platforms. (See @ref{121,,Dynamic Stack Usage Analysis} for details.)
15955
15956 @geindex -v (gnatbind)
15957
15958 @item @code{-v}
15959
15960 Verbose mode. Write error messages, header, summary output to
15961 @code{stdout}.
15962
15963 @geindex -V (gnatbind)
15964
15965 @item @code{-V@emph{key}=@emph{value}}
15966
15967 Store the given association of @code{key} to @code{value} in the bind environment.
15968 Values stored this way can be retrieved at run time using
15969 @code{GNAT.Bind_Environment}.
15970
15971 @geindex -w (gnatbind)
15972
15973 @item @code{-w@emph{x}}
15974
15975 Warning mode; @code{x} = s/e for suppress/treat as error.
15976
15977 @geindex -Wx (gnatbind)
15978
15979 @item @code{-Wx@emph{e}}
15980
15981 Override default wide character encoding for standard Text_IO files.
15982
15983 @geindex -x (gnatbind)
15984
15985 @item @code{-x}
15986
15987 Exclude source files (check object consistency only).
15988
15989 @geindex -Xnnn (gnatbind)
15990
15991 @item @code{-X@emph{nnn}}
15992
15993 Set default exit status value, normally 0 for POSIX compliance.
15994
15995 @geindex -y (gnatbind)
15996
15997 @item @code{-y}
15998
15999 Enable leap seconds support in @code{Ada.Calendar} and its children.
16000
16001 @geindex -z (gnatbind)
16002
16003 @item @code{-z}
16004
16005 No main subprogram.
16006 @end table
16007
16008 You may obtain this listing of switches by running @code{gnatbind} with
16009 no arguments.
16010
16011 @menu
16012 * Consistency-Checking Modes::
16013 * Binder Error Message Control::
16014 * Elaboration Control::
16015 * Output Control::
16016 * Dynamic Allocation Control::
16017 * Binding with Non-Ada Main Programs::
16018 * Binding Programs with No Main Subprogram::
16019
16020 @end menu
16021
16022 @node Consistency-Checking Modes,Binder Error Message Control,,Switches for gnatbind
16023 @anchor{gnat_ugn/building_executable_programs_with_gnat consistency-checking-modes}@anchor{122}@anchor{gnat_ugn/building_executable_programs_with_gnat id35}@anchor{123}
16024 @subsubsection Consistency-Checking Modes
16025
16026
16027 As described earlier, by default @code{gnatbind} checks
16028 that object files are consistent with one another and are consistent
16029 with any source files it can locate. The following switches control binder
16030 access to sources.
16031
16032 @quotation
16033
16034 @geindex -s (gnatbind)
16035 @end quotation
16036
16037
16038 @table @asis
16039
16040 @item @code{-s}
16041
16042 Require source files to be present. In this mode, the binder must be
16043 able to locate all source files that are referenced, in order to check
16044 their consistency. In normal mode, if a source file cannot be located it
16045 is simply ignored. If you specify this switch, a missing source
16046 file is an error.
16047
16048 @geindex -Wx (gnatbind)
16049
16050 @item @code{-Wx@emph{e}}
16051
16052 Override default wide character encoding for standard Text_IO files.
16053 Normally the default wide character encoding method used for standard
16054 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
16055 the main source input (see description of switch
16056 @code{-gnatWx} for the compiler). The
16057 use of this switch for the binder (which has the same set of
16058 possible arguments) overrides this default as specified.
16059
16060 @geindex -x (gnatbind)
16061
16062 @item @code{-x}
16063
16064 Exclude source files. In this mode, the binder only checks that ALI
16065 files are consistent with one another. Source files are not accessed.
16066 The binder runs faster in this mode, and there is still a guarantee that
16067 the resulting program is self-consistent.
16068 If a source file has been edited since it was last compiled, and you
16069 specify this switch, the binder will not detect that the object
16070 file is out of date with respect to the source file. Note that this is the
16071 mode that is automatically used by @code{gnatmake} because in this
16072 case the checking against sources has already been performed by
16073 @code{gnatmake} in the course of compilation (i.e., before binding).
16074 @end table
16075
16076 @node Binder Error Message Control,Elaboration Control,Consistency-Checking Modes,Switches for gnatbind
16077 @anchor{gnat_ugn/building_executable_programs_with_gnat id36}@anchor{124}@anchor{gnat_ugn/building_executable_programs_with_gnat binder-error-message-control}@anchor{125}
16078 @subsubsection Binder Error Message Control
16079
16080
16081 The following switches provide control over the generation of error
16082 messages from the binder:
16083
16084 @quotation
16085
16086 @geindex -v (gnatbind)
16087 @end quotation
16088
16089
16090 @table @asis
16091
16092 @item @code{-v}
16093
16094 Verbose mode. In the normal mode, brief error messages are generated to
16095 @code{stderr}. If this switch is present, a header is written
16096 to @code{stdout} and any error messages are directed to @code{stdout}.
16097 All that is written to @code{stderr} is a brief summary message.
16098
16099 @geindex -b (gnatbind)
16100
16101 @item @code{-b}
16102
16103 Generate brief error messages to @code{stderr} even if verbose mode is
16104 specified. This is relevant only when used with the
16105 @code{-v} switch.
16106
16107 @geindex -m (gnatbind)
16108
16109 @item @code{-m@emph{n}}
16110
16111 Limits the number of error messages to @code{n}, a decimal integer in the
16112 range 1-999. The binder terminates immediately if this limit is reached.
16113
16114 @geindex -M (gnatbind)
16115
16116 @item @code{-M@emph{xxx}}
16117
16118 Renames the generated main program from @code{main} to @code{xxx}.
16119 This is useful in the case of some cross-building environments, where
16120 the actual main program is separate from the one generated
16121 by @code{gnatbind}.
16122
16123 @geindex -ws (gnatbind)
16124
16125 @geindex Warnings
16126
16127 @item @code{-ws}
16128
16129 Suppress all warning messages.
16130
16131 @geindex -we (gnatbind)
16132
16133 @item @code{-we}
16134
16135 Treat any warning messages as fatal errors.
16136
16137 @geindex -t (gnatbind)
16138
16139 @geindex Time stamp checks
16140 @geindex in binder
16141
16142 @geindex Binder consistency checks
16143
16144 @geindex Consistency checks
16145 @geindex in binder
16146
16147 @item @code{-t}
16148
16149 The binder performs a number of consistency checks including:
16150
16151
16152 @itemize *
16153
16154 @item
16155 Check that time stamps of a given source unit are consistent
16156
16157 @item
16158 Check that checksums of a given source unit are consistent
16159
16160 @item
16161 Check that consistent versions of @code{GNAT} were used for compilation
16162
16163 @item
16164 Check consistency of configuration pragmas as required
16165 @end itemize
16166
16167 Normally failure of such checks, in accordance with the consistency
16168 requirements of the Ada Reference Manual, causes error messages to be
16169 generated which abort the binder and prevent the output of a binder
16170 file and subsequent link to obtain an executable.
16171
16172 The @code{-t} switch converts these error messages
16173 into warnings, so that
16174 binding and linking can continue to completion even in the presence of such
16175 errors. The result may be a failed link (due to missing symbols), or a
16176 non-functional executable which has undefined semantics.
16177
16178 @cartouche
16179 @quotation Note
16180 This means that @code{-t} should be used only in unusual situations,
16181 with extreme care.
16182 @end quotation
16183 @end cartouche
16184 @end table
16185
16186 @node Elaboration Control,Output Control,Binder Error Message Control,Switches for gnatbind
16187 @anchor{gnat_ugn/building_executable_programs_with_gnat id37}@anchor{126}@anchor{gnat_ugn/building_executable_programs_with_gnat elaboration-control}@anchor{127}
16188 @subsubsection Elaboration Control
16189
16190
16191 The following switches provide additional control over the elaboration
16192 order. For full details see @ref{f,,Elaboration Order Handling in GNAT}.
16193
16194 @geindex -f (gnatbind)
16195
16196
16197 @table @asis
16198
16199 @item @code{-f@emph{elab-order}}
16200
16201 Force elaboration order.
16202
16203 @code{elab-order} should be the name of a "forced elaboration order file", that
16204 is, a text file containing library item names, one per line. A name of the
16205 form "some.unit%s" or "some.unit (spec)" denotes the spec of Some.Unit. A
16206 name of the form "some.unit%b" or "some.unit (body)" denotes the body of
16207 Some.Unit. Each pair of lines is taken to mean that there is an elaboration
16208 dependence of the second line on the first. For example, if the file
16209 contains:
16210
16211 @example
16212 this (spec)
16213 this (body)
16214 that (spec)
16215 that (body)
16216 @end example
16217
16218 then the spec of This will be elaborated before the body of This, and the
16219 body of This will be elaborated before the spec of That, and the spec of That
16220 will be elaborated before the body of That. The first and last of these three
16221 dependences are already required by Ada rules, so this file is really just
16222 forcing the body of This to be elaborated before the spec of That.
16223
16224 The given order must be consistent with Ada rules, or else @code{gnatbind} will
16225 give elaboration cycle errors. For example, if you say x (body) should be
16226 elaborated before x (spec), there will be a cycle, because Ada rules require
16227 x (spec) to be elaborated before x (body); you can't have the spec and body
16228 both elaborated before each other.
16229
16230 If you later add "with That;" to the body of This, there will be a cycle, in
16231 which case you should erase either "this (body)" or "that (spec)" from the
16232 above forced elaboration order file.
16233
16234 Blank lines and Ada-style comments are ignored. Unit names that do not exist
16235 in the program are ignored. Units in the GNAT predefined library are also
16236 ignored.
16237
16238 @geindex -p (gnatbind)
16239
16240 @item @code{-p}
16241
16242 Normally the binder attempts to choose an elaboration order that is
16243 likely to minimize the likelihood of an elaboration order error resulting
16244 in raising a @code{Program_Error} exception. This switch reverses the
16245 action of the binder, and requests that it deliberately choose an order
16246 that is likely to maximize the likelihood of an elaboration error.
16247 This is useful in ensuring portability and avoiding dependence on
16248 accidental fortuitous elaboration ordering.
16249
16250 Normally it only makes sense to use the @code{-p}
16251 switch if dynamic
16252 elaboration checking is used (@code{-gnatE} switch used for compilation).
16253 This is because in the default static elaboration mode, all necessary
16254 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
16255 These implicit pragmas are still respected by the binder in
16256 @code{-p} mode, so a
16257 safe elaboration order is assured.
16258
16259 Note that @code{-p} is not intended for
16260 production use; it is more for debugging/experimental use.
16261 @end table
16262
16263 @node Output Control,Dynamic Allocation Control,Elaboration Control,Switches for gnatbind
16264 @anchor{gnat_ugn/building_executable_programs_with_gnat output-control}@anchor{128}@anchor{gnat_ugn/building_executable_programs_with_gnat id38}@anchor{129}
16265 @subsubsection Output Control
16266
16267
16268 The following switches allow additional control over the output
16269 generated by the binder.
16270
16271 @quotation
16272
16273 @geindex -c (gnatbind)
16274 @end quotation
16275
16276
16277 @table @asis
16278
16279 @item @code{-c}
16280
16281 Check only. Do not generate the binder output file. In this mode the
16282 binder performs all error checks but does not generate an output file.
16283
16284 @geindex -e (gnatbind)
16285
16286 @item @code{-e}
16287
16288 Output complete list of elaboration-order dependencies, showing the
16289 reason for each dependency. This output can be rather extensive but may
16290 be useful in diagnosing problems with elaboration order. The output is
16291 written to @code{stdout}.
16292
16293 @geindex -h (gnatbind)
16294
16295 @item @code{-h}
16296
16297 Output usage information. The output is written to @code{stdout}.
16298
16299 @geindex -K (gnatbind)
16300
16301 @item @code{-K}
16302
16303 Output linker options to @code{stdout}. Includes library search paths,
16304 contents of pragmas Ident and Linker_Options, and libraries added
16305 by @code{gnatbind}.
16306
16307 @geindex -l (gnatbind)
16308
16309 @item @code{-l}
16310
16311 Output chosen elaboration order. The output is written to @code{stdout}.
16312
16313 @geindex -O (gnatbind)
16314
16315 @item @code{-O}
16316
16317 Output full names of all the object files that must be linked to provide
16318 the Ada component of the program. The output is written to @code{stdout}.
16319 This list includes the files explicitly supplied and referenced by the user
16320 as well as implicitly referenced run-time unit files. The latter are
16321 omitted if the corresponding units reside in shared libraries. The
16322 directory names for the run-time units depend on the system configuration.
16323
16324 @geindex -o (gnatbind)
16325
16326 @item @code{-o @emph{file}}
16327
16328 Set name of output file to @code{file} instead of the normal
16329 @code{b~`mainprog}.adb` default. Note that @code{file} denote the Ada
16330 binder generated body filename.
16331 Note that if this option is used, then linking must be done manually.
16332 It is not possible to use gnatlink in this case, since it cannot locate
16333 the binder file.
16334
16335 @geindex -r (gnatbind)
16336
16337 @item @code{-r}
16338
16339 Generate list of @code{pragma Restrictions} that could be applied to
16340 the current unit. This is useful for code audit purposes, and also may
16341 be used to improve code generation in some cases.
16342 @end table
16343
16344 @node Dynamic Allocation Control,Binding with Non-Ada Main Programs,Output Control,Switches for gnatbind
16345 @anchor{gnat_ugn/building_executable_programs_with_gnat dynamic-allocation-control}@anchor{120}@anchor{gnat_ugn/building_executable_programs_with_gnat id39}@anchor{12a}
16346 @subsubsection Dynamic Allocation Control
16347
16348
16349 The heap control switches -- @code{-H32} and @code{-H64} --
16350 determine whether dynamic allocation uses 32-bit or 64-bit memory.
16351 They only affect compiler-generated allocations via @code{__gnat_malloc};
16352 explicit calls to @code{malloc} and related functions from the C
16353 run-time library are unaffected.
16354
16355
16356 @table @asis
16357
16358 @item @code{-H32}
16359
16360 Allocate memory on 32-bit heap
16361
16362 @item @code{-H64}
16363
16364 Allocate memory on 64-bit heap. This is the default
16365 unless explicitly overridden by a @code{'Size} clause on the access type.
16366 @end table
16367
16368 These switches are only effective on VMS platforms.
16369
16370 @node Binding with Non-Ada Main Programs,Binding Programs with No Main Subprogram,Dynamic Allocation Control,Switches for gnatbind
16371 @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}
16372 @subsubsection Binding with Non-Ada Main Programs
16373
16374
16375 The description so far has assumed that the main
16376 program is in Ada, and that the task of the binder is to generate a
16377 corresponding function @code{main} that invokes this Ada main
16378 program. GNAT also supports the building of executable programs where
16379 the main program is not in Ada, but some of the called routines are
16380 written in Ada and compiled using GNAT (@ref{44,,Mixed Language Programming}).
16381 The following switch is used in this situation:
16382
16383 @quotation
16384
16385 @geindex -n (gnatbind)
16386 @end quotation
16387
16388
16389 @table @asis
16390
16391 @item @code{-n}
16392
16393 No main program. The main program is not in Ada.
16394 @end table
16395
16396 In this case, most of the functions of the binder are still required,
16397 but instead of generating a main program, the binder generates a file
16398 containing the following callable routines:
16399
16400 @quotation
16401
16402 @geindex adainit
16403
16404
16405 @table @asis
16406
16407 @item @code{adainit}
16408
16409 You must call this routine to initialize the Ada part of the program by
16410 calling the necessary elaboration routines. A call to @code{adainit} is
16411 required before the first call to an Ada subprogram.
16412
16413 Note that it is assumed that the basic execution environment must be setup
16414 to be appropriate for Ada execution at the point where the first Ada
16415 subprogram is called. In particular, if the Ada code will do any
16416 floating-point operations, then the FPU must be setup in an appropriate
16417 manner. For the case of the x86, for example, full precision mode is
16418 required. The procedure GNAT.Float_Control.Reset may be used to ensure
16419 that the FPU is in the right state.
16420 @end table
16421
16422 @geindex adafinal
16423
16424
16425 @table @asis
16426
16427 @item @code{adafinal}
16428
16429 You must call this routine to perform any library-level finalization
16430 required by the Ada subprograms. A call to @code{adafinal} is required
16431 after the last call to an Ada subprogram, and before the program
16432 terminates.
16433 @end table
16434 @end quotation
16435
16436 @geindex -n (gnatbind)
16437
16438 @geindex Binder
16439 @geindex multiple input files
16440
16441 If the @code{-n} switch
16442 is given, more than one ALI file may appear on
16443 the command line for @code{gnatbind}. The normal @code{closure}
16444 calculation is performed for each of the specified units. Calculating
16445 the closure means finding out the set of units involved by tracing
16446 @emph{with} references. The reason it is necessary to be able to
16447 specify more than one ALI file is that a given program may invoke two or
16448 more quite separate groups of Ada units.
16449
16450 The binder takes the name of its output file from the last specified ALI
16451 file, unless overridden by the use of the @code{-o file}.
16452
16453 @geindex -o (gnatbind)
16454
16455 The output is an Ada unit in source form that can be compiled with GNAT.
16456 This compilation occurs automatically as part of the @code{gnatlink}
16457 processing.
16458
16459 Currently the GNAT run-time requires a FPU using 80 bits mode
16460 precision. Under targets where this is not the default it is required to
16461 call GNAT.Float_Control.Reset before using floating point numbers (this
16462 include float computation, float input and output) in the Ada code. A
16463 side effect is that this could be the wrong mode for the foreign code
16464 where floating point computation could be broken after this call.
16465
16466 @node Binding Programs with No Main Subprogram,,Binding with Non-Ada Main Programs,Switches for gnatbind
16467 @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}
16468 @subsubsection Binding Programs with No Main Subprogram
16469
16470
16471 It is possible to have an Ada program which does not have a main
16472 subprogram. This program will call the elaboration routines of all the
16473 packages, then the finalization routines.
16474
16475 The following switch is used to bind programs organized in this manner:
16476
16477 @quotation
16478
16479 @geindex -z (gnatbind)
16480 @end quotation
16481
16482
16483 @table @asis
16484
16485 @item @code{-z}
16486
16487 Normally the binder checks that the unit name given on the command line
16488 corresponds to a suitable main subprogram. When this switch is used,
16489 a list of ALI files can be given, and the execution of the program
16490 consists of elaboration of these units in an appropriate order. Note
16491 that the default wide character encoding method for standard Text_IO
16492 files is always set to Brackets if this switch is set (you can use
16493 the binder switch
16494 @code{-Wx} to override this default).
16495 @end table
16496
16497 @node Command-Line Access,Search Paths for gnatbind,Switches for gnatbind,Binding with gnatbind
16498 @anchor{gnat_ugn/building_executable_programs_with_gnat id42}@anchor{12e}@anchor{gnat_ugn/building_executable_programs_with_gnat command-line-access}@anchor{12f}
16499 @subsection Command-Line Access
16500
16501
16502 The package @code{Ada.Command_Line} provides access to the command-line
16503 arguments and program name. In order for this interface to operate
16504 correctly, the two variables
16505
16506 @example
16507 int gnat_argc;
16508 char **gnat_argv;
16509 @end example
16510
16511 @geindex gnat_argv
16512
16513 @geindex gnat_argc
16514
16515 are declared in one of the GNAT library routines. These variables must
16516 be set from the actual @code{argc} and @code{argv} values passed to the
16517 main program. With no @emph{n} present, @code{gnatbind}
16518 generates the C main program to automatically set these variables.
16519 If the @emph{n} switch is used, there is no automatic way to
16520 set these variables. If they are not set, the procedures in
16521 @code{Ada.Command_Line} will not be available, and any attempt to use
16522 them will raise @code{Constraint_Error}. If command line access is
16523 required, your main program must set @code{gnat_argc} and
16524 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
16525 it.
16526
16527 @node Search Paths for gnatbind,Examples of gnatbind Usage,Command-Line Access,Binding with gnatbind
16528 @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}
16529 @subsection Search Paths for @code{gnatbind}
16530
16531
16532 The binder takes the name of an ALI file as its argument and needs to
16533 locate source files as well as other ALI files to verify object consistency.
16534
16535 For source files, it follows exactly the same search rules as @code{gcc}
16536 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}). For ALI files the
16537 directories searched are:
16538
16539
16540 @itemize *
16541
16542 @item
16543 The directory containing the ALI file named in the command line, unless
16544 the switch @code{-I-} is specified.
16545
16546 @item
16547 All directories specified by @code{-I}
16548 switches on the @code{gnatbind}
16549 command line, in the order given.
16550
16551 @geindex ADA_PRJ_OBJECTS_FILE
16552
16553 @item
16554 Each of the directories listed in the text file whose name is given
16555 by the
16556 @geindex ADA_PRJ_OBJECTS_FILE
16557 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16558 @code{ADA_PRJ_OBJECTS_FILE} environment variable.
16559
16560 @geindex ADA_PRJ_OBJECTS_FILE
16561 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16562 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the gnat
16563 driver when project files are used. It should not normally be set
16564 by other means.
16565
16566 @geindex ADA_OBJECTS_PATH
16567
16568 @item
16569 Each of the directories listed in the value of the
16570 @geindex ADA_OBJECTS_PATH
16571 @geindex environment variable; ADA_OBJECTS_PATH
16572 @code{ADA_OBJECTS_PATH} environment variable.
16573 Construct this value
16574 exactly as the
16575 @geindex PATH
16576 @geindex environment variable; PATH
16577 @code{PATH} environment variable: a list of directory
16578 names separated by colons (semicolons when working with the NT version
16579 of GNAT).
16580
16581 @item
16582 The content of the @code{ada_object_path} file which is part of the GNAT
16583 installation tree and is used to store standard libraries such as the
16584 GNAT Run-Time Library (RTL) unless the switch @code{-nostdlib} is
16585 specified. See @ref{87,,Installing a library}
16586 @end itemize
16587
16588 @geindex -I (gnatbind)
16589
16590 @geindex -aI (gnatbind)
16591
16592 @geindex -aO (gnatbind)
16593
16594 In the binder the switch @code{-I}
16595 is used to specify both source and
16596 library file paths. Use @code{-aI}
16597 instead if you want to specify
16598 source paths only, and @code{-aO}
16599 if you want to specify library paths
16600 only. This means that for the binder
16601 @code{-I@emph{dir}} is equivalent to
16602 @code{-aI@emph{dir}}
16603 @code{-aO`@emph{dir}}.
16604 The binder generates the bind file (a C language source file) in the
16605 current working directory.
16606
16607 @geindex Ada
16608
16609 @geindex System
16610
16611 @geindex Interfaces
16612
16613 @geindex GNAT
16614
16615 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
16616 children make up the GNAT Run-Time Library, together with the package
16617 GNAT and its children, which contain a set of useful additional
16618 library functions provided by GNAT. The sources for these units are
16619 needed by the compiler and are kept together in one directory. The ALI
16620 files and object files generated by compiling the RTL are needed by the
16621 binder and the linker and are kept together in one directory, typically
16622 different from the directory containing the sources. In a normal
16623 installation, you need not specify these directory names when compiling
16624 or binding. Either the environment variables or the built-in defaults
16625 cause these files to be found.
16626
16627 Besides simplifying access to the RTL, a major use of search paths is
16628 in compiling sources from multiple directories. This can make
16629 development environments much more flexible.
16630
16631 @node Examples of gnatbind Usage,,Search Paths for gnatbind,Binding with gnatbind
16632 @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}
16633 @subsection Examples of @code{gnatbind} Usage
16634
16635
16636 Here are some examples of @code{gnatbind} invovations:
16637
16638 @quotation
16639
16640 @example
16641 gnatbind hello
16642 @end example
16643
16644 The main program @code{Hello} (source program in @code{hello.adb}) is
16645 bound using the standard switch settings. The generated main program is
16646 @code{b~hello.adb}. This is the normal, default use of the binder.
16647
16648 @example
16649 gnatbind hello -o mainprog.adb
16650 @end example
16651
16652 The main program @code{Hello} (source program in @code{hello.adb}) is
16653 bound using the standard switch settings. The generated main program is
16654 @code{mainprog.adb} with the associated spec in
16655 @code{mainprog.ads}. Note that you must specify the body here not the
16656 spec. Note that if this option is used, then linking must be done manually,
16657 since gnatlink will not be able to find the generated file.
16658 @end quotation
16659
16660 @node Linking with gnatlink,Using the GNU make Utility,Binding with gnatbind,Building Executable Programs with GNAT
16661 @anchor{gnat_ugn/building_executable_programs_with_gnat id45}@anchor{133}@anchor{gnat_ugn/building_executable_programs_with_gnat linking-with-gnatlink}@anchor{1e}
16662 @section Linking with @code{gnatlink}
16663
16664
16665 @geindex gnatlink
16666
16667 This chapter discusses @code{gnatlink}, a tool that links
16668 an Ada program and builds an executable file. This utility
16669 invokes the system linker (via the @code{gcc} command)
16670 with a correct list of object files and library references.
16671 @code{gnatlink} automatically determines the list of files and
16672 references for the Ada part of a program. It uses the binder file
16673 generated by the @code{gnatbind} to determine this list.
16674
16675 @menu
16676 * Running gnatlink::
16677 * Switches for gnatlink::
16678
16679 @end menu
16680
16681 @node Running gnatlink,Switches for gnatlink,,Linking with gnatlink
16682 @anchor{gnat_ugn/building_executable_programs_with_gnat id46}@anchor{134}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatlink}@anchor{135}
16683 @subsection Running @code{gnatlink}
16684
16685
16686 The form of the @code{gnatlink} command is
16687
16688 @example
16689 $ gnatlink [ switches ] mainprog [.ali]
16690 [ non-Ada objects ] [ linker options ]
16691 @end example
16692
16693 The arguments of @code{gnatlink} (switches, main @code{ALI} file,
16694 non-Ada objects
16695 or linker options) may be in any order, provided that no non-Ada object may
16696 be mistaken for a main @code{ALI} file.
16697 Any file name @code{F} without the @code{.ali}
16698 extension will be taken as the main @code{ALI} file if a file exists
16699 whose name is the concatenation of @code{F} and @code{.ali}.
16700
16701 @code{mainprog.ali} references the ALI file of the main program.
16702 The @code{.ali} extension of this file can be omitted. From this
16703 reference, @code{gnatlink} locates the corresponding binder file
16704 @code{b~mainprog.adb} and, using the information in this file along
16705 with the list of non-Ada objects and linker options, constructs a
16706 linker command file to create the executable.
16707
16708 The arguments other than the @code{gnatlink} switches and the main
16709 @code{ALI} file are passed to the linker uninterpreted.
16710 They typically include the names of
16711 object files for units written in other languages than Ada and any library
16712 references required to resolve references in any of these foreign language
16713 units, or in @code{Import} pragmas in any Ada units.
16714
16715 @code{linker options} is an optional list of linker specific
16716 switches.
16717 The default linker called by gnatlink is @code{gcc} which in
16718 turn calls the appropriate system linker.
16719
16720 One useful option for the linker is @code{-s}: it reduces the size of the
16721 executable by removing all symbol table and relocation information from the
16722 executable.
16723
16724 Standard options for the linker such as @code{-lmy_lib} or
16725 @code{-Ldir} can be added as is.
16726 For options that are not recognized by
16727 @code{gcc} as linker options, use the @code{gcc} switches
16728 @code{-Xlinker} or @code{-Wl,}.
16729
16730 Refer to the GCC documentation for
16731 details.
16732
16733 Here is an example showing how to generate a linker map:
16734
16735 @example
16736 $ gnatlink my_prog -Wl,-Map,MAPFILE
16737 @end example
16738
16739 Using @code{linker options} it is possible to set the program stack and
16740 heap size.
16741 See @ref{136,,Setting Stack Size from gnatlink} and
16742 @ref{137,,Setting Heap Size from gnatlink}.
16743
16744 @code{gnatlink} determines the list of objects required by the Ada
16745 program and prepends them to the list of objects passed to the linker.
16746 @code{gnatlink} also gathers any arguments set by the use of
16747 @code{pragma Linker_Options} and adds them to the list of arguments
16748 presented to the linker.
16749
16750 @node Switches for gnatlink,,Running gnatlink,Linking with gnatlink
16751 @anchor{gnat_ugn/building_executable_programs_with_gnat id47}@anchor{138}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatlink}@anchor{139}
16752 @subsection Switches for @code{gnatlink}
16753
16754
16755 The following switches are available with the @code{gnatlink} utility:
16756
16757 @geindex --version (gnatlink)
16758
16759
16760 @table @asis
16761
16762 @item @code{--version}
16763
16764 Display Copyright and version, then exit disregarding all other options.
16765 @end table
16766
16767 @geindex --help (gnatlink)
16768
16769
16770 @table @asis
16771
16772 @item @code{--help}
16773
16774 If @code{--version} was not used, display usage, then exit disregarding
16775 all other options.
16776 @end table
16777
16778 @geindex Command line length
16779
16780 @geindex -f (gnatlink)
16781
16782
16783 @table @asis
16784
16785 @item @code{-f}
16786
16787 On some targets, the command line length is limited, and @code{gnatlink}
16788 will generate a separate file for the linker if the list of object files
16789 is too long.
16790 The @code{-f} switch forces this file
16791 to be generated even if
16792 the limit is not exceeded. This is useful in some cases to deal with
16793 special situations where the command line length is exceeded.
16794 @end table
16795
16796 @geindex Debugging information
16797 @geindex including
16798
16799 @geindex -g (gnatlink)
16800
16801
16802 @table @asis
16803
16804 @item @code{-g}
16805
16806 The option to include debugging information causes the Ada bind file (in
16807 other words, @code{b~mainprog.adb}) to be compiled with @code{-g}.
16808 In addition, the binder does not delete the @code{b~mainprog.adb},
16809 @code{b~mainprog.o} and @code{b~mainprog.ali} files.
16810 Without @code{-g}, the binder removes these files by default.
16811 @end table
16812
16813 @geindex -n (gnatlink)
16814
16815
16816 @table @asis
16817
16818 @item @code{-n}
16819
16820 Do not compile the file generated by the binder. This may be used when
16821 a link is rerun with different options, but there is no need to recompile
16822 the binder file.
16823 @end table
16824
16825 @geindex -v (gnatlink)
16826
16827
16828 @table @asis
16829
16830 @item @code{-v}
16831
16832 Verbose mode. Causes additional information to be output, including a full
16833 list of the included object files.
16834 This switch option is most useful when you want
16835 to see what set of object files are being used in the link step.
16836 @end table
16837
16838 @geindex -v -v (gnatlink)
16839
16840
16841 @table @asis
16842
16843 @item @code{-v -v}
16844
16845 Very verbose mode. Requests that the compiler operate in verbose mode when
16846 it compiles the binder file, and that the system linker run in verbose mode.
16847 @end table
16848
16849 @geindex -o (gnatlink)
16850
16851
16852 @table @asis
16853
16854 @item @code{-o @emph{exec-name}}
16855
16856 @code{exec-name} specifies an alternate name for the generated
16857 executable program. If this switch is omitted, the executable has the same
16858 name as the main unit. For example, @code{gnatlink try.ali} creates
16859 an executable called @code{try}.
16860 @end table
16861
16862 @geindex -B (gnatlink)
16863
16864
16865 @table @asis
16866
16867 @item @code{-B@emph{dir}}
16868
16869 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
16870 from @code{dir} instead of the default location. Only use this switch
16871 when multiple versions of the GNAT compiler are available.
16872 See the @code{Directory Options} section in @cite{The_GNU_Compiler_Collection}
16873 for further details. You would normally use the @code{-b} or
16874 @code{-V} switch instead.
16875 @end table
16876
16877 @geindex -M (gnatlink)
16878
16879
16880 @table @asis
16881
16882 @item @code{-M}
16883
16884 When linking an executable, create a map file. The name of the map file
16885 has the same name as the executable with extension ".map".
16886 @end table
16887
16888 @geindex -M= (gnatlink)
16889
16890
16891 @table @asis
16892
16893 @item @code{-M=@emph{mapfile}}
16894
16895 When linking an executable, create a map file. The name of the map file is
16896 @code{mapfile}.
16897 @end table
16898
16899 @geindex --GCC=compiler_name (gnatlink)
16900
16901
16902 @table @asis
16903
16904 @item @code{--GCC=@emph{compiler_name}}
16905
16906 Program used for compiling the binder file. The default is
16907 @code{gcc}. You need to use quotes around @code{compiler_name} if
16908 @code{compiler_name} contains spaces or other separator characters.
16909 As an example @code{--GCC="foo -x -y"} will instruct @code{gnatlink} to
16910 use @code{foo -x -y} as your compiler. Note that switch @code{-c} is always
16911 inserted after your command name. Thus in the above example the compiler
16912 command that will be used by @code{gnatlink} will be @code{foo -c -x -y}.
16913 A limitation of this syntax is that the name and path name of the executable
16914 itself must not include any embedded spaces. If the compiler executable is
16915 different from the default one (gcc or <prefix>-gcc), then the back-end
16916 switches in the ALI file are not used to compile the binder generated source.
16917 For example, this is the case with @code{--GCC="foo -x -y"}. But the back end
16918 switches will be used for @code{--GCC="gcc -gnatv"}. If several
16919 @code{--GCC=compiler_name} are used, only the last @code{compiler_name}
16920 is taken into account. However, all the additional switches are also taken
16921 into account. Thus,
16922 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
16923 @code{--GCC="bar -x -y -z -t"}.
16924 @end table
16925
16926 @geindex --LINK= (gnatlink)
16927
16928
16929 @table @asis
16930
16931 @item @code{--LINK=@emph{name}}
16932
16933 @code{name} is the name of the linker to be invoked. This is especially
16934 useful in mixed language programs since languages such as C++ require
16935 their own linker to be used. When this switch is omitted, the default
16936 name for the linker is @code{gcc}. When this switch is used, the
16937 specified linker is called instead of @code{gcc} with exactly the same
16938 parameters that would have been passed to @code{gcc} so if the desired
16939 linker requires different parameters it is necessary to use a wrapper
16940 script that massages the parameters before invoking the real linker. It
16941 may be useful to control the exact invocation by using the verbose
16942 switch.
16943 @end table
16944
16945 @node Using the GNU make Utility,,Linking with gnatlink,Building Executable Programs with GNAT
16946 @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}
16947 @section Using the GNU @code{make} Utility
16948
16949
16950 @geindex make (GNU)
16951 @geindex GNU make
16952
16953 This chapter offers some examples of makefiles that solve specific
16954 problems. It does not explain how to write a makefile, nor does it try to replace the
16955 @code{gnatmake} utility (@ref{1b,,Building with gnatmake}).
16956
16957 All the examples in this section are specific to the GNU version of
16958 make. Although @code{make} is a standard utility, and the basic language
16959 is the same, these examples use some advanced features found only in
16960 @code{GNU make}.
16961
16962 @menu
16963 * Using gnatmake in a Makefile::
16964 * Automatically Creating a List of Directories::
16965 * Generating the Command Line Switches::
16966 * Overcoming Command Line Length Limits::
16967
16968 @end menu
16969
16970 @node Using gnatmake in a Makefile,Automatically Creating a List of Directories,,Using the GNU make Utility
16971 @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}
16972 @subsection Using gnatmake in a Makefile
16973
16974
16975 @c index makefile (GNU make)
16976
16977 Complex project organizations can be handled in a very powerful way by
16978 using GNU make combined with gnatmake. For instance, here is a Makefile
16979 which allows you to build each subsystem of a big project into a separate
16980 shared library. Such a makefile allows you to significantly reduce the link
16981 time of very big applications while maintaining full coherence at
16982 each step of the build process.
16983
16984 The list of dependencies are handled automatically by
16985 @code{gnatmake}. The Makefile is simply used to call gnatmake in each of
16986 the appropriate directories.
16987
16988 Note that you should also read the example on how to automatically
16989 create the list of directories
16990 (@ref{13d,,Automatically Creating a List of Directories})
16991 which might help you in case your project has a lot of subdirectories.
16992
16993 @example
16994 ## This Makefile is intended to be used with the following directory
16995 ## configuration:
16996 ## - The sources are split into a series of csc (computer software components)
16997 ## Each of these csc is put in its own directory.
16998 ## Their name are referenced by the directory names.
16999 ## They will be compiled into shared library (although this would also work
17000 ## with static libraries
17001 ## - The main program (and possibly other packages that do not belong to any
17002 ## csc is put in the top level directory (where the Makefile is).
17003 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
17004 ## \\_ second_csc (sources) __ lib (will contain the library)
17005 ## \\_ ...
17006 ## Although this Makefile is build for shared library, it is easy to modify
17007 ## to build partial link objects instead (modify the lines with -shared and
17008 ## gnatlink below)
17009 ##
17010 ## With this makefile, you can change any file in the system or add any new
17011 ## file, and everything will be recompiled correctly (only the relevant shared
17012 ## objects will be recompiled, and the main program will be re-linked).
17013
17014 # The list of computer software component for your project. This might be
17015 # generated automatically.
17016 CSC_LIST=aa bb cc
17017
17018 # Name of the main program (no extension)
17019 MAIN=main
17020
17021 # If we need to build objects with -fPIC, uncomment the following line
17022 #NEED_FPIC=-fPIC
17023
17024 # The following variable should give the directory containing libgnat.so
17025 # You can get this directory through 'gnatls -v'. This is usually the last
17026 # directory in the Object_Path.
17027 GLIB=...
17028
17029 # The directories for the libraries
17030 # (This macro expands the list of CSC to the list of shared libraries, you
17031 # could simply use the expanded form:
17032 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17033 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17034
17035 $@{MAIN@}: objects $@{LIB_DIR@}
17036 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17037 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17038
17039 objects::
17040 # recompile the sources
17041 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17042
17043 # Note: In a future version of GNAT, the following commands will be simplified
17044 # by a new tool, gnatmlib
17045 $@{LIB_DIR@}:
17046 mkdir -p $@{dir $@@ @}
17047 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17048 cd $@{dir $@@ @} && cp -f ../*.ali .
17049
17050 # The dependencies for the modules
17051 # Note that we have to force the expansion of *.o, since in some cases
17052 # make won't be able to do it itself.
17053 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17054 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17055 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17056
17057 # Make sure all of the shared libraries are in the path before starting the
17058 # program
17059 run::
17060 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17061
17062 clean::
17063 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17064 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17065 $@{RM@} $@{CSC_LIST:%=%/*.o@}
17066 $@{RM@} *.o *.ali $@{MAIN@}
17067 @end example
17068
17069 @node Automatically Creating a List of Directories,Generating the Command Line Switches,Using gnatmake in a Makefile,Using the GNU make Utility
17070 @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}
17071 @subsection Automatically Creating a List of Directories
17072
17073
17074 In most makefiles, you will have to specify a list of directories, and
17075 store it in a variable. For small projects, it is often easier to
17076 specify each of them by hand, since you then have full control over what
17077 is the proper order for these directories, which ones should be
17078 included.
17079
17080 However, in larger projects, which might involve hundreds of
17081 subdirectories, it might be more convenient to generate this list
17082 automatically.
17083
17084 The example below presents two methods. The first one, although less
17085 general, gives you more control over the list. It involves wildcard
17086 characters, that are automatically expanded by @code{make}. Its
17087 shortcoming is that you need to explicitly specify some of the
17088 organization of your project, such as for instance the directory tree
17089 depth, whether some directories are found in a separate tree, etc.
17090
17091 The second method is the most general one. It requires an external
17092 program, called @code{find}, which is standard on all Unix systems. All
17093 the directories found under a given root directory will be added to the
17094 list.
17095
17096 @example
17097 # The examples below are based on the following directory hierarchy:
17098 # All the directories can contain any number of files
17099 # ROOT_DIRECTORY -> a -> aa -> aaa
17100 # -> ab
17101 # -> ac
17102 # -> b -> ba -> baa
17103 # -> bb
17104 # -> bc
17105 # This Makefile creates a variable called DIRS, that can be reused any time
17106 # you need this list (see the other examples in this section)
17107
17108 # The root of your project's directory hierarchy
17109 ROOT_DIRECTORY=.
17110
17111 ####
17112 # First method: specify explicitly the list of directories
17113 # This allows you to specify any subset of all the directories you need.
17114 ####
17115
17116 DIRS := a/aa/ a/ab/ b/ba/
17117
17118 ####
17119 # Second method: use wildcards
17120 # Note that the argument(s) to wildcard below should end with a '/'.
17121 # Since wildcards also return file names, we have to filter them out
17122 # to avoid duplicate directory names.
17123 # We thus use make's `@w{`}dir`@w{`} and `@w{`}sort`@w{`} functions.
17124 # It sets DIRs to the following value (note that the directories aaa and baa
17125 # are not given, unless you change the arguments to wildcard).
17126 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
17127 ####
17128
17129 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
17130 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
17131
17132 ####
17133 # Third method: use an external program
17134 # This command is much faster if run on local disks, avoiding NFS slowdowns.
17135 # This is the most complete command: it sets DIRs to the following value:
17136 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
17137 ####
17138
17139 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
17140 @end example
17141
17142 @node Generating the Command Line Switches,Overcoming Command Line Length Limits,Automatically Creating a List of Directories,Using the GNU make Utility
17143 @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}
17144 @subsection Generating the Command Line Switches
17145
17146
17147 Once you have created the list of directories as explained in the
17148 previous section (@ref{13d,,Automatically Creating a List of Directories}),
17149 you can easily generate the command line arguments to pass to gnatmake.
17150
17151 For the sake of completeness, this example assumes that the source path
17152 is not the same as the object path, and that you have two separate lists
17153 of directories.
17154
17155 @example
17156 # see "Automatically creating a list of directories" to create
17157 # these variables
17158 SOURCE_DIRS=
17159 OBJECT_DIRS=
17160
17161 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
17162 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
17163
17164 all:
17165 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
17166 @end example
17167
17168 @node Overcoming Command Line Length Limits,,Generating the Command Line Switches,Using the GNU make Utility
17169 @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}
17170 @subsection Overcoming Command Line Length Limits
17171
17172
17173 One problem that might be encountered on big projects is that many
17174 operating systems limit the length of the command line. It is thus hard to give
17175 gnatmake the list of source and object directories.
17176
17177 This example shows how you can set up environment variables, which will
17178 make @code{gnatmake} behave exactly as if the directories had been
17179 specified on the command line, but have a much higher length limit (or
17180 even none on most systems).
17181
17182 It assumes that you have created a list of directories in your Makefile,
17183 using one of the methods presented in
17184 @ref{13d,,Automatically Creating a List of Directories}.
17185 For the sake of completeness, we assume that the object
17186 path (where the ALI files are found) is different from the sources patch.
17187
17188 Note a small trick in the Makefile below: for efficiency reasons, we
17189 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
17190 expanded immediately by @code{make}. This way we overcome the standard
17191 make behavior which is to expand the variables only when they are
17192 actually used.
17193
17194 On Windows, if you are using the standard Windows command shell, you must
17195 replace colons with semicolons in the assignments to these variables.
17196
17197 @example
17198 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECTS_PATH.
17199 # This is the same thing as putting the -I arguments on the command line.
17200 # (the equivalent of using -aI on the command line would be to define
17201 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECTS_PATH).
17202 # You can of course have different values for these variables.
17203 #
17204 # Note also that we need to keep the previous values of these variables, since
17205 # they might have been set before running 'make' to specify where the GNAT
17206 # library is installed.
17207
17208 # see "Automatically creating a list of directories" to create these
17209 # variables
17210 SOURCE_DIRS=
17211 OBJECT_DIRS=
17212
17213 empty:=
17214 space:=$@{empty@} $@{empty@}
17215 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
17216 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
17217 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
17218 ADA_OBJECTS_PATH += $@{OBJECT_LIST@}
17219 export ADA_INCLUDE_PATH
17220 export ADA_OBJECTS_PATH
17221
17222 all:
17223 gnatmake main_unit
17224 @end example
17225
17226 @node GNAT Utility Programs,GNAT and Program Execution,Building Executable Programs with GNAT,Top
17227 @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}
17228 @chapter GNAT Utility Programs
17229
17230
17231 This chapter describes a number of utility programs:
17232
17233
17234
17235 @itemize *
17236
17237 @item
17238 @ref{20,,The File Cleanup Utility gnatclean}
17239
17240 @item
17241 @ref{21,,The GNAT Library Browser gnatls}
17242
17243 @item
17244 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
17245
17246 @item
17247 @ref{23,,The Ada to HTML Converter gnathtml}
17248 @end itemize
17249
17250 Other GNAT utilities are described elsewhere in this manual:
17251
17252
17253 @itemize *
17254
17255 @item
17256 @ref{59,,Handling Arbitrary File Naming Conventions with gnatname}
17257
17258 @item
17259 @ref{63,,File Name Krunching with gnatkr}
17260
17261 @item
17262 @ref{36,,Renaming Files with gnatchop}
17263
17264 @item
17265 @ref{17,,Preprocessing with gnatprep}
17266 @end itemize
17267
17268 @menu
17269 * The File Cleanup Utility gnatclean::
17270 * The GNAT Library Browser gnatls::
17271 * The Cross-Referencing Tools gnatxref and gnatfind::
17272 * The Ada to HTML Converter gnathtml::
17273
17274 @end menu
17275
17276 @node The File Cleanup Utility gnatclean,The GNAT Library Browser gnatls,,GNAT Utility Programs
17277 @anchor{gnat_ugn/gnat_utility_programs id2}@anchor{145}@anchor{gnat_ugn/gnat_utility_programs the-file-cleanup-utility-gnatclean}@anchor{20}
17278 @section The File Cleanup Utility @code{gnatclean}
17279
17280
17281 @geindex File cleanup tool
17282
17283 @geindex gnatclean
17284
17285 @code{gnatclean} is a tool that allows the deletion of files produced by the
17286 compiler, binder and linker, including ALI files, object files, tree files,
17287 expanded source files, library files, interface copy source files, binder
17288 generated files and executable files.
17289
17290 @menu
17291 * Running gnatclean::
17292 * Switches for gnatclean::
17293
17294 @end menu
17295
17296 @node Running gnatclean,Switches for gnatclean,,The File Cleanup Utility gnatclean
17297 @anchor{gnat_ugn/gnat_utility_programs running-gnatclean}@anchor{146}@anchor{gnat_ugn/gnat_utility_programs id3}@anchor{147}
17298 @subsection Running @code{gnatclean}
17299
17300
17301 The @code{gnatclean} command has the form:
17302
17303 @quotation
17304
17305 @example
17306 $ gnatclean switches names
17307 @end example
17308 @end quotation
17309
17310 where @code{names} is a list of source file names. Suffixes @code{.ads} and
17311 @code{adb} may be omitted. If a project file is specified using switch
17312 @code{-P}, then @code{names} may be completely omitted.
17313
17314 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17315 if switch @code{-c} is not specified, by the binder and
17316 the linker. In informative-only mode, specified by switch
17317 @code{-n}, the list of files that would have been deleted in
17318 normal mode is listed, but no file is actually deleted.
17319
17320 @node Switches for gnatclean,,Running gnatclean,The File Cleanup Utility gnatclean
17321 @anchor{gnat_ugn/gnat_utility_programs id4}@anchor{148}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatclean}@anchor{149}
17322 @subsection Switches for @code{gnatclean}
17323
17324
17325 @code{gnatclean} recognizes the following switches:
17326
17327 @geindex --version (gnatclean)
17328
17329
17330 @table @asis
17331
17332 @item @code{--version}
17333
17334 Display Copyright and version, then exit disregarding all other options.
17335 @end table
17336
17337 @geindex --help (gnatclean)
17338
17339
17340 @table @asis
17341
17342 @item @code{--help}
17343
17344 If @code{--version} was not used, display usage, then exit disregarding
17345 all other options.
17346
17347 @item @code{--subdirs=@emph{subdir}}
17348
17349 Actual object directory of each project file is the subdirectory subdir of the
17350 object directory specified or defaulted in the project file.
17351
17352 @item @code{--unchecked-shared-lib-imports}
17353
17354 By default, shared library projects are not allowed to import static library
17355 projects. When this switch is used on the command line, this restriction is
17356 relaxed.
17357 @end table
17358
17359 @geindex -c (gnatclean)
17360
17361
17362 @table @asis
17363
17364 @item @code{-c}
17365
17366 Only attempt to delete the files produced by the compiler, not those produced
17367 by the binder or the linker. The files that are not to be deleted are library
17368 files, interface copy files, binder generated files and executable files.
17369 @end table
17370
17371 @geindex -D (gnatclean)
17372
17373
17374 @table @asis
17375
17376 @item @code{-D @emph{dir}}
17377
17378 Indicate that ALI and object files should normally be found in directory @code{dir}.
17379 @end table
17380
17381 @geindex -F (gnatclean)
17382
17383
17384 @table @asis
17385
17386 @item @code{-F}
17387
17388 When using project files, if some errors or warnings are detected during
17389 parsing and verbose mode is not in effect (no use of switch
17390 -v), then error lines start with the full path name of the project
17391 file, rather than its simple file name.
17392 @end table
17393
17394 @geindex -h (gnatclean)
17395
17396
17397 @table @asis
17398
17399 @item @code{-h}
17400
17401 Output a message explaining the usage of @code{gnatclean}.
17402 @end table
17403
17404 @geindex -n (gnatclean)
17405
17406
17407 @table @asis
17408
17409 @item @code{-n}
17410
17411 Informative-only mode. Do not delete any files. Output the list of the files
17412 that would have been deleted if this switch was not specified.
17413 @end table
17414
17415 @geindex -P (gnatclean)
17416
17417
17418 @table @asis
17419
17420 @item @code{-P@emph{project}}
17421
17422 Use project file @code{project}. Only one such switch can be used.
17423 When cleaning a project file, the files produced by the compilation of the
17424 immediate sources or inherited sources of the project files are to be
17425 deleted. This is not depending on the presence or not of executable names
17426 on the command line.
17427 @end table
17428
17429 @geindex -q (gnatclean)
17430
17431
17432 @table @asis
17433
17434 @item @code{-q}
17435
17436 Quiet output. If there are no errors, do not output anything, except in
17437 verbose mode (switch -v) or in informative-only mode
17438 (switch -n).
17439 @end table
17440
17441 @geindex -r (gnatclean)
17442
17443
17444 @table @asis
17445
17446 @item @code{-r}
17447
17448 When a project file is specified (using switch -P),
17449 clean all imported and extended project files, recursively. If this switch
17450 is not specified, only the files related to the main project file are to be
17451 deleted. This switch has no effect if no project file is specified.
17452 @end table
17453
17454 @geindex -v (gnatclean)
17455
17456
17457 @table @asis
17458
17459 @item @code{-v}
17460
17461 Verbose mode.
17462 @end table
17463
17464 @geindex -vP (gnatclean)
17465
17466
17467 @table @asis
17468
17469 @item @code{-vP@emph{x}}
17470
17471 Indicates the verbosity of the parsing of GNAT project files.
17472 @ref{de,,Switches Related to Project Files}.
17473 @end table
17474
17475 @geindex -X (gnatclean)
17476
17477
17478 @table @asis
17479
17480 @item @code{-X@emph{name}=@emph{value}}
17481
17482 Indicates that external variable @code{name} has the value @code{value}.
17483 The Project Manager will use this value for occurrences of
17484 @code{external(name)} when parsing the project file.
17485 See @ref{de,,Switches Related to Project Files}.
17486 @end table
17487
17488 @geindex -aO (gnatclean)
17489
17490
17491 @table @asis
17492
17493 @item @code{-aO@emph{dir}}
17494
17495 When searching for ALI and object files, look in directory @code{dir}.
17496 @end table
17497
17498 @geindex -I (gnatclean)
17499
17500
17501 @table @asis
17502
17503 @item @code{-I@emph{dir}}
17504
17505 Equivalent to @code{-aO@emph{dir}}.
17506 @end table
17507
17508 @geindex -I- (gnatclean)
17509
17510 @geindex Source files
17511 @geindex suppressing search
17512
17513
17514 @table @asis
17515
17516 @item @code{-I-}
17517
17518 Do not look for ALI or object files in the directory
17519 where @code{gnatclean} was invoked.
17520 @end table
17521
17522 @node The GNAT Library Browser gnatls,The Cross-Referencing Tools gnatxref and gnatfind,The File Cleanup Utility gnatclean,GNAT Utility Programs
17523 @anchor{gnat_ugn/gnat_utility_programs the-gnat-library-browser-gnatls}@anchor{21}@anchor{gnat_ugn/gnat_utility_programs id5}@anchor{14a}
17524 @section The GNAT Library Browser @code{gnatls}
17525
17526
17527 @geindex Library browser
17528
17529 @geindex gnatls
17530
17531 @code{gnatls} is a tool that outputs information about compiled
17532 units. It gives the relationship between objects, unit names and source
17533 files. It can also be used to check the source dependencies of a unit
17534 as well as various characteristics.
17535
17536 @menu
17537 * Running gnatls::
17538 * Switches for gnatls::
17539 * Example of gnatls Usage::
17540
17541 @end menu
17542
17543 @node Running gnatls,Switches for gnatls,,The GNAT Library Browser gnatls
17544 @anchor{gnat_ugn/gnat_utility_programs id6}@anchor{14b}@anchor{gnat_ugn/gnat_utility_programs running-gnatls}@anchor{14c}
17545 @subsection Running @code{gnatls}
17546
17547
17548 The @code{gnatls} command has the form
17549
17550 @quotation
17551
17552 @example
17553 $ gnatls switches object_or_ali_file
17554 @end example
17555 @end quotation
17556
17557 The main argument is the list of object or @code{ali} files
17558 (see @ref{42,,The Ada Library Information Files})
17559 for which information is requested.
17560
17561 In normal mode, without additional option, @code{gnatls} produces a
17562 four-column listing. Each line represents information for a specific
17563 object. The first column gives the full path of the object, the second
17564 column gives the name of the principal unit in this object, the third
17565 column gives the status of the source and the fourth column gives the
17566 full path of the source representing this unit.
17567 Here is a simple example of use:
17568
17569 @quotation
17570
17571 @example
17572 $ gnatls *.o
17573 ./demo1.o demo1 DIF demo1.adb
17574 ./demo2.o demo2 OK demo2.adb
17575 ./hello.o h1 OK hello.adb
17576 ./instr-child.o instr.child MOK instr-child.adb
17577 ./instr.o instr OK instr.adb
17578 ./tef.o tef DIF tef.adb
17579 ./text_io_example.o text_io_example OK text_io_example.adb
17580 ./tgef.o tgef DIF tgef.adb
17581 @end example
17582 @end quotation
17583
17584 The first line can be interpreted as follows: the main unit which is
17585 contained in
17586 object file @code{demo1.o} is demo1, whose main source is in
17587 @code{demo1.adb}. Furthermore, the version of the source used for the
17588 compilation of demo1 has been modified (DIF). Each source file has a status
17589 qualifier which can be:
17590
17591
17592 @table @asis
17593
17594 @item @emph{OK (unchanged)}
17595
17596 The version of the source file used for the compilation of the
17597 specified unit corresponds exactly to the actual source file.
17598
17599 @item @emph{MOK (slightly modified)}
17600
17601 The version of the source file used for the compilation of the
17602 specified unit differs from the actual source file but not enough to
17603 require recompilation. If you use gnatmake with the option
17604 @code{-m} (minimal recompilation), a file marked
17605 MOK will not be recompiled.
17606
17607 @item @emph{DIF (modified)}
17608
17609 No version of the source found on the path corresponds to the source
17610 used to build this object.
17611
17612 @item @emph{??? (file not found)}
17613
17614 No source file was found for this unit.
17615
17616 @item @emph{HID (hidden, unchanged version not first on PATH)}
17617
17618 The version of the source that corresponds exactly to the source used
17619 for compilation has been found on the path but it is hidden by another
17620 version of the same source that has been modified.
17621 @end table
17622
17623 @node Switches for gnatls,Example of gnatls Usage,Running gnatls,The GNAT Library Browser gnatls
17624 @anchor{gnat_ugn/gnat_utility_programs id7}@anchor{14d}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatls}@anchor{14e}
17625 @subsection Switches for @code{gnatls}
17626
17627
17628 @code{gnatls} recognizes the following switches:
17629
17630 @geindex --version (gnatls)
17631
17632
17633 @table @asis
17634
17635 @item @code{--version}
17636
17637 Display Copyright and version, then exit disregarding all other options.
17638 @end table
17639
17640 @geindex --help (gnatls)
17641
17642
17643 @table @asis
17644
17645 @item @code{--help}
17646
17647 If @code{--version} was not used, display usage, then exit disregarding
17648 all other options.
17649 @end table
17650
17651 @geindex -a (gnatls)
17652
17653
17654 @table @asis
17655
17656 @item @code{-a}
17657
17658 Consider all units, including those of the predefined Ada library.
17659 Especially useful with @code{-d}.
17660 @end table
17661
17662 @geindex -d (gnatls)
17663
17664
17665 @table @asis
17666
17667 @item @code{-d}
17668
17669 List sources from which specified units depend on.
17670 @end table
17671
17672 @geindex -h (gnatls)
17673
17674
17675 @table @asis
17676
17677 @item @code{-h}
17678
17679 Output the list of options.
17680 @end table
17681
17682 @geindex -o (gnatls)
17683
17684
17685 @table @asis
17686
17687 @item @code{-o}
17688
17689 Only output information about object files.
17690 @end table
17691
17692 @geindex -s (gnatls)
17693
17694
17695 @table @asis
17696
17697 @item @code{-s}
17698
17699 Only output information about source files.
17700 @end table
17701
17702 @geindex -u (gnatls)
17703
17704
17705 @table @asis
17706
17707 @item @code{-u}
17708
17709 Only output information about compilation units.
17710 @end table
17711
17712 @geindex -files (gnatls)
17713
17714
17715 @table @asis
17716
17717 @item @code{-files=@emph{file}}
17718
17719 Take as arguments the files listed in text file @code{file}.
17720 Text file @code{file} may contain empty lines that are ignored.
17721 Each nonempty line should contain the name of an existing file.
17722 Several such switches may be specified simultaneously.
17723 @end table
17724
17725 @geindex -aO (gnatls)
17726
17727 @geindex -aI (gnatls)
17728
17729 @geindex -I (gnatls)
17730
17731 @geindex -I- (gnatls)
17732
17733
17734 @table @asis
17735
17736 @item @code{-aO@emph{dir}}, @code{-aI@emph{dir}}, @code{-I@emph{dir}}, @code{-I-}, @code{-nostdinc}
17737
17738 Source path manipulation. Same meaning as the equivalent @code{gnatmake}
17739 flags (@ref{dc,,Switches for gnatmake}).
17740 @end table
17741
17742 @geindex -aP (gnatls)
17743
17744
17745 @table @asis
17746
17747 @item @code{-aP@emph{dir}}
17748
17749 Add @code{dir} at the beginning of the project search dir.
17750 @end table
17751
17752 @geindex --RTS (gnatls)
17753
17754
17755 @table @asis
17756
17757 @item @code{--RTS=@emph{rts-path}}
17758
17759 Specifies the default location of the runtime library. Same meaning as the
17760 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
17761 @end table
17762
17763 @geindex -v (gnatls)
17764
17765
17766 @table @asis
17767
17768 @item @code{-v}
17769
17770 Verbose mode. Output the complete source, object and project paths. Do not use
17771 the default column layout but instead use long format giving as much as
17772 information possible on each requested units, including special
17773 characteristics such as:
17774
17775
17776 @itemize *
17777
17778 @item
17779 @emph{Preelaborable}: The unit is preelaborable in the Ada sense.
17780
17781 @item
17782 @emph{No_Elab_Code}: No elaboration code has been produced by the compiler for this unit.
17783
17784 @item
17785 @emph{Pure}: The unit is pure in the Ada sense.
17786
17787 @item
17788 @emph{Elaborate_Body}: The unit contains a pragma Elaborate_Body.
17789
17790 @item
17791 @emph{Remote_Types}: The unit contains a pragma Remote_Types.
17792
17793 @item
17794 @emph{Shared_Passive}: The unit contains a pragma Shared_Passive.
17795
17796 @item
17797 @emph{Predefined}: This unit is part of the predefined environment and cannot be modified
17798 by the user.
17799
17800 @item
17801 @emph{Remote_Call_Interface}: The unit contains a pragma Remote_Call_Interface.
17802 @end itemize
17803 @end table
17804
17805 @node Example of gnatls Usage,,Switches for gnatls,The GNAT Library Browser gnatls
17806 @anchor{gnat_ugn/gnat_utility_programs id8}@anchor{14f}@anchor{gnat_ugn/gnat_utility_programs example-of-gnatls-usage}@anchor{150}
17807 @subsection Example of @code{gnatls} Usage
17808
17809
17810 Example of using the verbose switch. Note how the source and
17811 object paths are affected by the -I switch.
17812
17813 @quotation
17814
17815 @example
17816 $ gnatls -v -I.. demo1.o
17817
17818 GNATLS 5.03w (20041123-34)
17819 Copyright 1997-2004 Free Software Foundation, Inc.
17820
17821 Source Search Path:
17822 <Current_Directory>
17823 ../
17824 /home/comar/local/adainclude/
17825
17826 Object Search Path:
17827 <Current_Directory>
17828 ../
17829 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
17830
17831 Project Search Path:
17832 <Current_Directory>
17833 /home/comar/local/lib/gnat/
17834
17835 ./demo1.o
17836 Unit =>
17837 Name => demo1
17838 Kind => subprogram body
17839 Flags => No_Elab_Code
17840 Source => demo1.adb modified
17841 @end example
17842 @end quotation
17843
17844 The following is an example of use of the dependency list.
17845 Note the use of the -s switch
17846 which gives a straight list of source files. This can be useful for
17847 building specialized scripts.
17848
17849 @quotation
17850
17851 @example
17852 $ gnatls -d demo2.o
17853 ./demo2.o demo2 OK demo2.adb
17854 OK gen_list.ads
17855 OK gen_list.adb
17856 OK instr.ads
17857 OK instr-child.ads
17858
17859 $ gnatls -d -s -a demo1.o
17860 demo1.adb
17861 /home/comar/local/adainclude/ada.ads
17862 /home/comar/local/adainclude/a-finali.ads
17863 /home/comar/local/adainclude/a-filico.ads
17864 /home/comar/local/adainclude/a-stream.ads
17865 /home/comar/local/adainclude/a-tags.ads
17866 gen_list.ads
17867 gen_list.adb
17868 /home/comar/local/adainclude/gnat.ads
17869 /home/comar/local/adainclude/g-io.ads
17870 instr.ads
17871 /home/comar/local/adainclude/system.ads
17872 /home/comar/local/adainclude/s-exctab.ads
17873 /home/comar/local/adainclude/s-finimp.ads
17874 /home/comar/local/adainclude/s-finroo.ads
17875 /home/comar/local/adainclude/s-secsta.ads
17876 /home/comar/local/adainclude/s-stalib.ads
17877 /home/comar/local/adainclude/s-stoele.ads
17878 /home/comar/local/adainclude/s-stratt.ads
17879 /home/comar/local/adainclude/s-tasoli.ads
17880 /home/comar/local/adainclude/s-unstyp.ads
17881 /home/comar/local/adainclude/unchconv.ads
17882 @end example
17883 @end quotation
17884
17885 @node The Cross-Referencing Tools gnatxref and gnatfind,The Ada to HTML Converter gnathtml,The GNAT Library Browser gnatls,GNAT Utility Programs
17886 @anchor{gnat_ugn/gnat_utility_programs the-cross-referencing-tools-gnatxref-and-gnatfind}@anchor{22}@anchor{gnat_ugn/gnat_utility_programs id9}@anchor{151}
17887 @section The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
17888
17889
17890 @geindex gnatxref
17891
17892 @geindex gnatfind
17893
17894 The compiler generates cross-referencing information (unless
17895 you set the @code{-gnatx} switch), which are saved in the @code{.ali} files.
17896 This information indicates where in the source each entity is declared and
17897 referenced. Note that entities in package Standard are not included, but
17898 entities in all other predefined units are included in the output.
17899
17900 Before using any of these two tools, you need to compile successfully your
17901 application, so that GNAT gets a chance to generate the cross-referencing
17902 information.
17903
17904 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
17905 information to provide the user with the capability to easily locate the
17906 declaration and references to an entity. These tools are quite similar,
17907 the difference being that @code{gnatfind} is intended for locating
17908 definitions and/or references to a specified entity or entities, whereas
17909 @code{gnatxref} is oriented to generating a full report of all
17910 cross-references.
17911
17912 To use these tools, you must not compile your application using the
17913 @code{-gnatx} switch on the @code{gnatmake} command line
17914 (see @ref{1b,,Building with gnatmake}). Otherwise, cross-referencing
17915 information will not be generated.
17916
17917 @menu
17918 * gnatxref Switches::
17919 * gnatfind Switches::
17920 * Configuration Files for gnatxref and gnatfind::
17921 * Regular Expressions in gnatfind and gnatxref::
17922 * Examples of gnatxref Usage::
17923 * Examples of gnatfind Usage::
17924
17925 @end menu
17926
17927 @node gnatxref Switches,gnatfind Switches,,The Cross-Referencing Tools gnatxref and gnatfind
17928 @anchor{gnat_ugn/gnat_utility_programs id10}@anchor{152}@anchor{gnat_ugn/gnat_utility_programs gnatxref-switches}@anchor{153}
17929 @subsection @code{gnatxref} Switches
17930
17931
17932 The command invocation for @code{gnatxref} is:
17933
17934 @quotation
17935
17936 @example
17937 $ gnatxref [ switches ] sourcefile1 [ sourcefile2 ... ]
17938 @end example
17939 @end quotation
17940
17941 where
17942
17943
17944 @table @asis
17945
17946 @item @code{sourcefile1} [, @code{sourcefile2} ...]
17947
17948 identify the source files for which a report is to be generated. The
17949 @code{with}ed units will be processed too. You must provide at least one file.
17950
17951 These file names are considered to be regular expressions, so for instance
17952 specifying @code{source*.adb} is the same as giving every file in the current
17953 directory whose name starts with @code{source} and whose extension is
17954 @code{adb}.
17955
17956 You shouldn't specify any directory name, just base names. @code{gnatxref}
17957 and @code{gnatfind} will be able to locate these files by themselves using
17958 the source path. If you specify directories, no result is produced.
17959 @end table
17960
17961 The following switches are available for @code{gnatxref}:
17962
17963 @geindex --version (gnatxref)
17964
17965
17966 @table @asis
17967
17968 @item @code{--version}
17969
17970 Display Copyright and version, then exit disregarding all other options.
17971 @end table
17972
17973 @geindex --help (gnatxref)
17974
17975
17976 @table @asis
17977
17978 @item @code{--help}
17979
17980 If @code{--version} was not used, display usage, then exit disregarding
17981 all other options.
17982 @end table
17983
17984 @geindex -a (gnatxref)
17985
17986
17987 @table @asis
17988
17989 @item @code{-a}
17990
17991 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
17992 the read-only files found in the library search path. Otherwise, these files
17993 will be ignored. This option can be used to protect Gnat sources or your own
17994 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
17995 much faster, and their output much smaller. Read-only here refers to access
17996 or permissions status in the file system for the current user.
17997 @end table
17998
17999 @geindex -aIDIR (gnatxref)
18000
18001
18002 @table @asis
18003
18004 @item @code{-aI@emph{DIR}}
18005
18006 When looking for source files also look in directory DIR. The order in which
18007 source file search is undertaken is the same as for @code{gnatmake}.
18008 @end table
18009
18010 @geindex -aODIR (gnatxref)
18011
18012
18013 @table @asis
18014
18015 @item @code{aO@emph{DIR}}
18016
18017 When -searching for library and object files, look in directory
18018 DIR. The order in which library files are searched is the same as for
18019 @code{gnatmake}.
18020 @end table
18021
18022 @geindex -nostdinc (gnatxref)
18023
18024
18025 @table @asis
18026
18027 @item @code{-nostdinc}
18028
18029 Do not look for sources in the system default directory.
18030 @end table
18031
18032 @geindex -nostdlib (gnatxref)
18033
18034
18035 @table @asis
18036
18037 @item @code{-nostdlib}
18038
18039 Do not look for library files in the system default directory.
18040 @end table
18041
18042 @geindex --ext (gnatxref)
18043
18044
18045 @table @asis
18046
18047 @item @code{--ext=@emph{extension}}
18048
18049 Specify an alternate ali file extension. The default is @code{ali} and other
18050 extensions (e.g. @code{gli} for C/C++ sources) may be specified via this switch.
18051 Note that if this switch overrides the default, only the new extension will
18052 be considered.
18053 @end table
18054
18055 @geindex --RTS (gnatxref)
18056
18057
18058 @table @asis
18059
18060 @item @code{--RTS=@emph{rts-path}}
18061
18062 Specifies the default location of the runtime library. Same meaning as the
18063 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18064 @end table
18065
18066 @geindex -d (gnatxref)
18067
18068
18069 @table @asis
18070
18071 @item @code{-d}
18072
18073 If this switch is set @code{gnatxref} will output the parent type
18074 reference for each matching derived types.
18075 @end table
18076
18077 @geindex -f (gnatxref)
18078
18079
18080 @table @asis
18081
18082 @item @code{-f}
18083
18084 If this switch is set, the output file names will be preceded by their
18085 directory (if the file was found in the search path). If this switch is
18086 not set, the directory will not be printed.
18087 @end table
18088
18089 @geindex -g (gnatxref)
18090
18091
18092 @table @asis
18093
18094 @item @code{-g}
18095
18096 If this switch is set, information is output only for library-level
18097 entities, ignoring local entities. The use of this switch may accelerate
18098 @code{gnatfind} and @code{gnatxref}.
18099 @end table
18100
18101 @geindex -IDIR (gnatxref)
18102
18103
18104 @table @asis
18105
18106 @item @code{-I@emph{DIR}}
18107
18108 Equivalent to @code{-aODIR -aIDIR}.
18109 @end table
18110
18111 @geindex -pFILE (gnatxref)
18112
18113
18114 @table @asis
18115
18116 @item @code{-p@emph{FILE}}
18117
18118 Specify a configuration file to use to list the source and object directories.
18119
18120 If a file is specified, then the content of the source directory and object
18121 directory lines are added as if they had been specified respectively
18122 by @code{-aI} and @code{-aO}.
18123
18124 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18125 of this configuration file.
18126
18127 @item @code{-u}
18128
18129 Output only unused symbols. This may be really useful if you give your
18130 main compilation unit on the command line, as @code{gnatxref} will then
18131 display every unused entity and 'with'ed package.
18132
18133 @item @code{-v}
18134
18135 Instead of producing the default output, @code{gnatxref} will generate a
18136 @code{tags} file that can be used by vi. For examples how to use this
18137 feature, see @ref{155,,Examples of gnatxref Usage}. The tags file is output
18138 to the standard output, thus you will have to redirect it to a file.
18139 @end table
18140
18141 All these switches may be in any order on the command line, and may even
18142 appear after the file names. They need not be separated by spaces, thus
18143 you can say @code{gnatxref -ag} instead of @code{gnatxref -a -g}.
18144
18145 @node gnatfind Switches,Configuration Files for gnatxref and gnatfind,gnatxref Switches,The Cross-Referencing Tools gnatxref and gnatfind
18146 @anchor{gnat_ugn/gnat_utility_programs id11}@anchor{156}@anchor{gnat_ugn/gnat_utility_programs gnatfind-switches}@anchor{157}
18147 @subsection @code{gnatfind} Switches
18148
18149
18150 The command invocation for @code{gnatfind} is:
18151
18152 @quotation
18153
18154 @example
18155 $ gnatfind [ switches ] pattern[:sourcefile[:line[:column]]]
18156 [file1 file2 ...]
18157 @end example
18158 @end quotation
18159
18160 with the following iterpretation of the command arguments:
18161
18162
18163 @table @asis
18164
18165 @item @emph{pattern}
18166
18167 An entity will be output only if it matches the regular expression found
18168 in @emph{pattern}, see @ref{158,,Regular Expressions in gnatfind and gnatxref}.
18169
18170 Omitting the pattern is equivalent to specifying @code{*}, which
18171 will match any entity. Note that if you do not provide a pattern, you
18172 have to provide both a sourcefile and a line.
18173
18174 Entity names are given in Latin-1, with uppercase/lowercase equivalence
18175 for matching purposes. At the current time there is no support for
18176 8-bit codes other than Latin-1, or for wide characters in identifiers.
18177
18178 @item @emph{sourcefile}
18179
18180 @code{gnatfind} will look for references, bodies or declarations
18181 of symbols referenced in @code{sourcefile}, at line @code{line}
18182 and column @code{column}. See @ref{159,,Examples of gnatfind Usage}
18183 for syntax examples.
18184
18185 @item @emph{line}
18186
18187 A decimal integer identifying the line number containing
18188 the reference to the entity (or entities) to be located.
18189
18190 @item @emph{column}
18191
18192 A decimal integer identifying the exact location on the
18193 line of the first character of the identifier for the
18194 entity reference. Columns are numbered from 1.
18195
18196 @item @emph{file1 file2 ...}
18197
18198 The search will be restricted to these source files. If none are given, then
18199 the search will be conducted for every library file in the search path.
18200 These files must appear only after the pattern or sourcefile.
18201
18202 These file names are considered to be regular expressions, so for instance
18203 specifying @code{source*.adb} is the same as giving every file in the current
18204 directory whose name starts with @code{source} and whose extension is
18205 @code{adb}.
18206
18207 The location of the spec of the entity will always be displayed, even if it
18208 isn't in one of @code{file1}, @code{file2}, ... The
18209 occurrences of the entity in the separate units of the ones given on the
18210 command line will also be displayed.
18211
18212 Note that if you specify at least one file in this part, @code{gnatfind} may
18213 sometimes not be able to find the body of the subprograms.
18214 @end table
18215
18216 At least one of 'sourcefile' or 'pattern' has to be present on
18217 the command line.
18218
18219 The following switches are available:
18220
18221 @geindex --version (gnatfind)
18222
18223
18224 @table @asis
18225
18226 @item @code{--version}
18227
18228 Display Copyright and version, then exit disregarding all other options.
18229 @end table
18230
18231 @geindex --help (gnatfind)
18232
18233
18234 @table @asis
18235
18236 @item @code{--help}
18237
18238 If @code{--version} was not used, display usage, then exit disregarding
18239 all other options.
18240 @end table
18241
18242 @geindex -a (gnatfind)
18243
18244
18245 @table @asis
18246
18247 @item @code{-a}
18248
18249 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
18250 the read-only files found in the library search path. Otherwise, these files
18251 will be ignored. This option can be used to protect Gnat sources or your own
18252 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
18253 much faster, and their output much smaller. Read-only here refers to access
18254 or permission status in the file system for the current user.
18255 @end table
18256
18257 @geindex -aIDIR (gnatfind)
18258
18259
18260 @table @asis
18261
18262 @item @code{-aI@emph{DIR}}
18263
18264 When looking for source files also look in directory DIR. The order in which
18265 source file search is undertaken is the same as for @code{gnatmake}.
18266 @end table
18267
18268 @geindex -aODIR (gnatfind)
18269
18270
18271 @table @asis
18272
18273 @item @code{-aO@emph{DIR}}
18274
18275 When searching for library and object files, look in directory
18276 DIR. The order in which library files are searched is the same as for
18277 @code{gnatmake}.
18278 @end table
18279
18280 @geindex -nostdinc (gnatfind)
18281
18282
18283 @table @asis
18284
18285 @item @code{-nostdinc}
18286
18287 Do not look for sources in the system default directory.
18288 @end table
18289
18290 @geindex -nostdlib (gnatfind)
18291
18292
18293 @table @asis
18294
18295 @item @code{-nostdlib}
18296
18297 Do not look for library files in the system default directory.
18298 @end table
18299
18300 @geindex --ext (gnatfind)
18301
18302
18303 @table @asis
18304
18305 @item @code{--ext=@emph{extension}}
18306
18307 Specify an alternate ali file extension. The default is @code{ali} and other
18308 extensions may be specified via this switch. Note that if this switch
18309 overrides the default, only the new extension will be considered.
18310 @end table
18311
18312 @geindex --RTS (gnatfind)
18313
18314
18315 @table @asis
18316
18317 @item @code{--RTS=@emph{rts-path}}
18318
18319 Specifies the default location of the runtime library. Same meaning as the
18320 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18321 @end table
18322
18323 @geindex -d (gnatfind)
18324
18325
18326 @table @asis
18327
18328 @item @code{-d}
18329
18330 If this switch is set, then @code{gnatfind} will output the parent type
18331 reference for each matching derived types.
18332 @end table
18333
18334 @geindex -e (gnatfind)
18335
18336
18337 @table @asis
18338
18339 @item @code{-e}
18340
18341 By default, @code{gnatfind} accept the simple regular expression set for
18342 @code{pattern}. If this switch is set, then the pattern will be
18343 considered as full Unix-style regular expression.
18344 @end table
18345
18346 @geindex -f (gnatfind)
18347
18348
18349 @table @asis
18350
18351 @item @code{-f}
18352
18353 If this switch is set, the output file names will be preceded by their
18354 directory (if the file was found in the search path). If this switch is
18355 not set, the directory will not be printed.
18356 @end table
18357
18358 @geindex -g (gnatfind)
18359
18360
18361 @table @asis
18362
18363 @item @code{-g}
18364
18365 If this switch is set, information is output only for library-level
18366 entities, ignoring local entities. The use of this switch may accelerate
18367 @code{gnatfind} and @code{gnatxref}.
18368 @end table
18369
18370 @geindex -IDIR (gnatfind)
18371
18372
18373 @table @asis
18374
18375 @item @code{-I@emph{DIR}}
18376
18377 Equivalent to @code{-aODIR -aIDIR}.
18378 @end table
18379
18380 @geindex -pFILE (gnatfind)
18381
18382
18383 @table @asis
18384
18385 @item @code{-p@emph{FILE}}
18386
18387 Specify a configuration file to use to list the source and object directories.
18388
18389 If a file is specified, then the content of the source directory and object
18390 directory lines are added as if they had been specified respectively
18391 by @code{-aI} and @code{-aO}.
18392
18393 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18394 of this configuration file.
18395 @end table
18396
18397 @geindex -r (gnatfind)
18398
18399
18400 @table @asis
18401
18402 @item @code{-r}
18403
18404 By default, @code{gnatfind} will output only the information about the
18405 declaration, body or type completion of the entities. If this switch is
18406 set, the @code{gnatfind} will locate every reference to the entities in
18407 the files specified on the command line (or in every file in the search
18408 path if no file is given on the command line).
18409 @end table
18410
18411 @geindex -s (gnatfind)
18412
18413
18414 @table @asis
18415
18416 @item @code{-s}
18417
18418 If this switch is set, then @code{gnatfind} will output the content
18419 of the Ada source file lines were the entity was found.
18420 @end table
18421
18422 @geindex -t (gnatfind)
18423
18424
18425 @table @asis
18426
18427 @item @code{-t}
18428
18429 If this switch is set, then @code{gnatfind} will output the type hierarchy for
18430 the specified type. It act like -d option but recursively from parent
18431 type to parent type. When this switch is set it is not possible to
18432 specify more than one file.
18433 @end table
18434
18435 All these switches may be in any order on the command line, and may even
18436 appear after the file names. They need not be separated by spaces, thus
18437 you can say @code{gnatxref -ag} instead of
18438 @code{gnatxref -a -g}.
18439
18440 As stated previously, @code{gnatfind} will search in every directory in the
18441 search path. You can force it to look only in the current directory if
18442 you specify @code{*} at the end of the command line.
18443
18444 @node Configuration Files for gnatxref and gnatfind,Regular Expressions in gnatfind and gnatxref,gnatfind Switches,The Cross-Referencing Tools gnatxref and gnatfind
18445 @anchor{gnat_ugn/gnat_utility_programs configuration-files-for-gnatxref-and-gnatfind}@anchor{154}@anchor{gnat_ugn/gnat_utility_programs id12}@anchor{15a}
18446 @subsection Configuration Files for @code{gnatxref} and @code{gnatfind}
18447
18448
18449 Configuration files are used by @code{gnatxref} and @code{gnatfind} to specify
18450 the list of source and object directories to consider. They can be
18451 specified via the @code{-p} switch.
18452
18453 The following lines can be included, in any order in the file:
18454
18455
18456 @itemize *
18457
18458 @item
18459
18460 @table @asis
18461
18462 @item @emph{src_dir=DIR}
18463
18464 [default: @code{"./"}].
18465 Specifies a directory where to look for source files. Multiple @code{src_dir}
18466 lines can be specified and they will be searched in the order they
18467 are specified.
18468 @end table
18469
18470 @item
18471
18472 @table @asis
18473
18474 @item @emph{obj_dir=DIR}
18475
18476 [default: @code{"./"}].
18477 Specifies a directory where to look for object and library files. Multiple
18478 @code{obj_dir} lines can be specified, and they will be searched in the order
18479 they are specified
18480 @end table
18481 @end itemize
18482
18483 Any other line will be silently ignored.
18484
18485 @node Regular Expressions in gnatfind and gnatxref,Examples of gnatxref Usage,Configuration Files for gnatxref and gnatfind,The Cross-Referencing Tools gnatxref and gnatfind
18486 @anchor{gnat_ugn/gnat_utility_programs id13}@anchor{15b}@anchor{gnat_ugn/gnat_utility_programs regular-expressions-in-gnatfind-and-gnatxref}@anchor{158}
18487 @subsection Regular Expressions in @code{gnatfind} and @code{gnatxref}
18488
18489
18490 As specified in the section about @code{gnatfind}, the pattern can be a
18491 regular expression. Two kinds of regular expressions
18492 are recognized:
18493
18494
18495 @itemize *
18496
18497 @item
18498
18499 @table @asis
18500
18501 @item @emph{Globbing pattern}
18502
18503 These are the most common regular expression. They are the same as are
18504 generally used in a Unix shell command line, or in a DOS session.
18505
18506 Here is a more formal grammar:
18507
18508 @example
18509 regexp ::= term
18510 term ::= elmt -- matches elmt
18511 term ::= elmt elmt -- concatenation (elmt then elmt)
18512 term ::= * -- any string of 0 or more characters
18513 term ::= ? -- matches any character
18514 term ::= [char @{char@}] -- matches any character listed
18515 term ::= [char - char] -- matches any character in range
18516 @end example
18517 @end table
18518
18519 @item
18520
18521 @table @asis
18522
18523 @item @emph{Full regular expression}
18524
18525 The second set of regular expressions is much more powerful. This is the
18526 type of regular expressions recognized by utilities such as @code{grep}.
18527
18528 The following is the form of a regular expression, expressed in same BNF
18529 style as is found in the Ada Reference Manual:
18530
18531 @example
18532 regexp ::= term @{| term@} -- alternation (term or term ...)
18533
18534 term ::= item @{item@} -- concatenation (item then item)
18535
18536 item ::= elmt -- match elmt
18537 item ::= elmt * -- zero or more elmt's
18538 item ::= elmt + -- one or more elmt's
18539 item ::= elmt ? -- matches elmt or nothing
18540
18541 elmt ::= nschar -- matches given character
18542 elmt ::= [nschar @{nschar@}] -- matches any character listed
18543 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
18544 elmt ::= [char - char] -- matches chars in given range
18545 elmt ::= \\ char -- matches given character
18546 elmt ::= . -- matches any single character
18547 elmt ::= ( regexp ) -- parens used for grouping
18548
18549 char ::= any character, including special characters
18550 nschar ::= any character except ()[].*+?^
18551 @end example
18552
18553 Here are a few examples:
18554
18555 @quotation
18556
18557
18558 @table @asis
18559
18560 @item @code{abcde|fghi}
18561
18562 will match any of the two strings @code{abcde} and @code{fghi},
18563
18564 @item @code{abc*d}
18565
18566 will match any string like @code{abd}, @code{abcd}, @code{abccd},
18567 @code{abcccd}, and so on,
18568
18569 @item @code{[a-z]+}
18570
18571 will match any string which has only lowercase characters in it (and at
18572 least one character.
18573 @end table
18574 @end quotation
18575 @end table
18576 @end itemize
18577
18578 @node Examples of gnatxref Usage,Examples of gnatfind Usage,Regular Expressions in gnatfind and gnatxref,The Cross-Referencing Tools gnatxref and gnatfind
18579 @anchor{gnat_ugn/gnat_utility_programs examples-of-gnatxref-usage}@anchor{155}@anchor{gnat_ugn/gnat_utility_programs id14}@anchor{15c}
18580 @subsection Examples of @code{gnatxref} Usage
18581
18582
18583 @menu
18584 * General Usage::
18585 * Using gnatxref with vi::
18586
18587 @end menu
18588
18589 @node General Usage,Using gnatxref with vi,,Examples of gnatxref Usage
18590 @anchor{gnat_ugn/gnat_utility_programs general-usage}@anchor{15d}
18591 @subsubsection General Usage
18592
18593
18594 For the following examples, we will consider the following units:
18595
18596 @quotation
18597
18598 @example
18599 main.ads:
18600 1: with Bar;
18601 2: package Main is
18602 3: procedure Foo (B : in Integer);
18603 4: C : Integer;
18604 5: private
18605 6: D : Integer;
18606 7: end Main;
18607
18608 main.adb:
18609 1: package body Main is
18610 2: procedure Foo (B : in Integer) is
18611 3: begin
18612 4: C := B;
18613 5: D := B;
18614 6: Bar.Print (B);
18615 7: Bar.Print (C);
18616 8: end Foo;
18617 9: end Main;
18618
18619 bar.ads:
18620 1: package Bar is
18621 2: procedure Print (B : Integer);
18622 3: end bar;
18623 @end example
18624 @end quotation
18625
18626 The first thing to do is to recompile your application (for instance, in
18627 that case just by doing a @code{gnatmake main}, so that GNAT generates
18628 the cross-referencing information.
18629 You can then issue any of the following commands:
18630
18631 @quotation
18632
18633
18634 @itemize *
18635
18636 @item
18637 @code{gnatxref main.adb}
18638 @code{gnatxref} generates cross-reference information for main.adb
18639 and every unit 'with'ed by main.adb.
18640
18641 The output would be:
18642
18643 @quotation
18644
18645 @example
18646 B Type: Integer
18647 Decl: bar.ads 2:22
18648 B Type: Integer
18649 Decl: main.ads 3:20
18650 Body: main.adb 2:20
18651 Ref: main.adb 4:13 5:13 6:19
18652 Bar Type: Unit
18653 Decl: bar.ads 1:9
18654 Ref: main.adb 6:8 7:8
18655 main.ads 1:6
18656 C Type: Integer
18657 Decl: main.ads 4:5
18658 Modi: main.adb 4:8
18659 Ref: main.adb 7:19
18660 D Type: Integer
18661 Decl: main.ads 6:5
18662 Modi: main.adb 5:8
18663 Foo Type: Unit
18664 Decl: main.ads 3:15
18665 Body: main.adb 2:15
18666 Main Type: Unit
18667 Decl: main.ads 2:9
18668 Body: main.adb 1:14
18669 Print Type: Unit
18670 Decl: bar.ads 2:15
18671 Ref: main.adb 6:12 7:12
18672 @end example
18673 @end quotation
18674
18675 This shows that the entity @code{Main} is declared in main.ads, line 2, column 9,
18676 its body is in main.adb, line 1, column 14 and is not referenced any where.
18677
18678 The entity @code{Print} is declared in @code{bar.ads}, line 2, column 15 and it
18679 is referenced in @code{main.adb}, line 6 column 12 and line 7 column 12.
18680
18681 @item
18682 @code{gnatxref package1.adb package2.ads}
18683 @code{gnatxref} will generates cross-reference information for
18684 @code{package1.adb}, @code{package2.ads} and any other package @code{with}ed by any
18685 of these.
18686 @end itemize
18687 @end quotation
18688
18689 @node Using gnatxref with vi,,General Usage,Examples of gnatxref Usage
18690 @anchor{gnat_ugn/gnat_utility_programs using-gnatxref-with-vi}@anchor{15e}
18691 @subsubsection Using @code{gnatxref} with @code{vi}
18692
18693
18694 @code{gnatxref} can generate a tags file output, which can be used
18695 directly from @code{vi}. Note that the standard version of @code{vi}
18696 will not work properly with overloaded symbols. Consider using another
18697 free implementation of @code{vi}, such as @code{vim}.
18698
18699 @quotation
18700
18701 @example
18702 $ gnatxref -v gnatfind.adb > tags
18703 @end example
18704 @end quotation
18705
18706 The following command will generate the tags file for @code{gnatfind} itself
18707 (if the sources are in the search path!):
18708
18709 @quotation
18710
18711 @example
18712 $ gnatxref -v gnatfind.adb > tags
18713 @end example
18714 @end quotation
18715
18716 From @code{vi}, you can then use the command @code{:tag @emph{entity}}
18717 (replacing @code{entity} by whatever you are looking for), and vi will
18718 display a new file with the corresponding declaration of entity.
18719
18720 @node Examples of gnatfind Usage,,Examples of gnatxref Usage,The Cross-Referencing Tools gnatxref and gnatfind
18721 @anchor{gnat_ugn/gnat_utility_programs id15}@anchor{15f}@anchor{gnat_ugn/gnat_utility_programs examples-of-gnatfind-usage}@anchor{159}
18722 @subsection Examples of @code{gnatfind} Usage
18723
18724
18725
18726 @itemize *
18727
18728 @item
18729 @code{gnatfind -f xyz:main.adb}
18730 Find declarations for all entities xyz referenced at least once in
18731 main.adb. The references are search in every library file in the search
18732 path.
18733
18734 The directories will be printed as well (as the @code{-f}
18735 switch is set)
18736
18737 The output will look like:
18738
18739 @quotation
18740
18741 @example
18742 directory/main.ads:106:14: xyz <= declaration
18743 directory/main.adb:24:10: xyz <= body
18744 directory/foo.ads:45:23: xyz <= declaration
18745 @end example
18746 @end quotation
18747
18748 I.e., one of the entities xyz found in main.adb is declared at
18749 line 12 of main.ads (and its body is in main.adb), and another one is
18750 declared at line 45 of foo.ads
18751
18752 @item
18753 @code{gnatfind -fs xyz:main.adb}
18754 This is the same command as the previous one, but @code{gnatfind} will
18755 display the content of the Ada source file lines.
18756
18757 The output will look like:
18758
18759 @example
18760 directory/main.ads:106:14: xyz <= declaration
18761 procedure xyz;
18762 directory/main.adb:24:10: xyz <= body
18763 procedure xyz is
18764 directory/foo.ads:45:23: xyz <= declaration
18765 xyz : Integer;
18766 @end example
18767
18768 This can make it easier to find exactly the location your are looking
18769 for.
18770
18771 @item
18772 @code{gnatfind -r "*x*":main.ads:123 foo.adb}
18773 Find references to all entities containing an x that are
18774 referenced on line 123 of main.ads.
18775 The references will be searched only in main.ads and foo.adb.
18776
18777 @item
18778 @code{gnatfind main.ads:123}
18779 Find declarations and bodies for all entities that are referenced on
18780 line 123 of main.ads.
18781
18782 This is the same as @code{gnatfind "*":main.adb:123`}
18783
18784 @item
18785 @code{gnatfind mydir/main.adb:123:45}
18786 Find the declaration for the entity referenced at column 45 in
18787 line 123 of file main.adb in directory mydir. Note that it
18788 is usual to omit the identifier name when the column is given,
18789 since the column position identifies a unique reference.
18790
18791 The column has to be the beginning of the identifier, and should not
18792 point to any character in the middle of the identifier.
18793 @end itemize
18794
18795 @node The Ada to HTML Converter gnathtml,,The Cross-Referencing Tools gnatxref and gnatfind,GNAT Utility Programs
18796 @anchor{gnat_ugn/gnat_utility_programs the-ada-to-html-converter-gnathtml}@anchor{23}@anchor{gnat_ugn/gnat_utility_programs id16}@anchor{160}
18797 @section The Ada to HTML Converter @code{gnathtml}
18798
18799
18800 @geindex gnathtml
18801
18802 @code{gnathtml} is a Perl script that allows Ada source files to be browsed using
18803 standard Web browsers. For installation information, see @ref{161,,Installing gnathtml}.
18804
18805 Ada reserved keywords are highlighted in a bold font and Ada comments in
18806 a blue font. Unless your program was compiled with the gcc @code{-gnatx}
18807 switch to suppress the generation of cross-referencing information, user
18808 defined variables and types will appear in a different color; you will
18809 be able to click on any identifier and go to its declaration.
18810
18811 @menu
18812 * Invoking gnathtml::
18813 * Installing gnathtml::
18814
18815 @end menu
18816
18817 @node Invoking gnathtml,Installing gnathtml,,The Ada to HTML Converter gnathtml
18818 @anchor{gnat_ugn/gnat_utility_programs invoking-gnathtml}@anchor{162}@anchor{gnat_ugn/gnat_utility_programs id17}@anchor{163}
18819 @subsection Invoking @code{gnathtml}
18820
18821
18822 The command line is as follows:
18823
18824 @quotation
18825
18826 @example
18827 $ perl gnathtml.pl [ switches ] ada-files
18828 @end example
18829 @end quotation
18830
18831 You can specify as many Ada files as you want. @code{gnathtml} will generate
18832 an html file for every ada file, and a global file called @code{index.htm}.
18833 This file is an index of every identifier defined in the files.
18834
18835 The following switches are available:
18836
18837 @geindex -83 (gnathtml)
18838
18839
18840 @table @asis
18841
18842 @item @code{83}
18843
18844 Only the Ada 83 subset of keywords will be highlighted.
18845 @end table
18846
18847 @geindex -cc (gnathtml)
18848
18849
18850 @table @asis
18851
18852 @item @code{cc @emph{color}}
18853
18854 This option allows you to change the color used for comments. The default
18855 value is green. The color argument can be any name accepted by html.
18856 @end table
18857
18858 @geindex -d (gnathtml)
18859
18860
18861 @table @asis
18862
18863 @item @code{d}
18864
18865 If the Ada files depend on some other files (for instance through
18866 @code{with} clauses, the latter files will also be converted to html.
18867 Only the files in the user project will be converted to html, not the files
18868 in the run-time library itself.
18869 @end table
18870
18871 @geindex -D (gnathtml)
18872
18873
18874 @table @asis
18875
18876 @item @code{D}
18877
18878 This command is the same as @code{-d} above, but @code{gnathtml} will
18879 also look for files in the run-time library, and generate html files for them.
18880 @end table
18881
18882 @geindex -ext (gnathtml)
18883
18884
18885 @table @asis
18886
18887 @item @code{ext @emph{extension}}
18888
18889 This option allows you to change the extension of the generated HTML files.
18890 If you do not specify an extension, it will default to @code{htm}.
18891 @end table
18892
18893 @geindex -f (gnathtml)
18894
18895
18896 @table @asis
18897
18898 @item @code{f}
18899
18900 By default, gnathtml will generate html links only for global entities
18901 ('with'ed units, global variables and types,...). If you specify
18902 @code{-f} on the command line, then links will be generated for local
18903 entities too.
18904 @end table
18905
18906 @geindex -l (gnathtml)
18907
18908
18909 @table @asis
18910
18911 @item @code{l @emph{number}}
18912
18913 If this switch is provided and @code{number} is not 0, then
18914 @code{gnathtml} will number the html files every @code{number} line.
18915 @end table
18916
18917 @geindex -I (gnathtml)
18918
18919
18920 @table @asis
18921
18922 @item @code{I @emph{dir}}
18923
18924 Specify a directory to search for library files (@code{.ALI} files) and
18925 source files. You can provide several -I switches on the command line,
18926 and the directories will be parsed in the order of the command line.
18927 @end table
18928
18929 @geindex -o (gnathtml)
18930
18931
18932 @table @asis
18933
18934 @item @code{o @emph{dir}}
18935
18936 Specify the output directory for html files. By default, gnathtml will
18937 saved the generated html files in a subdirectory named @code{html/}.
18938 @end table
18939
18940 @geindex -p (gnathtml)
18941
18942
18943 @table @asis
18944
18945 @item @code{p @emph{file}}
18946
18947 If you are using Emacs and the most recent Emacs Ada mode, which provides
18948 a full Integrated Development Environment for compiling, checking,
18949 running and debugging applications, you may use @code{.gpr} files
18950 to give the directories where Emacs can find sources and object files.
18951
18952 Using this switch, you can tell gnathtml to use these files.
18953 This allows you to get an html version of your application, even if it
18954 is spread over multiple directories.
18955 @end table
18956
18957 @geindex -sc (gnathtml)
18958
18959
18960 @table @asis
18961
18962 @item @code{sc @emph{color}}
18963
18964 This switch allows you to change the color used for symbol
18965 definitions.
18966 The default value is red. The color argument can be any name accepted by html.
18967 @end table
18968
18969 @geindex -t (gnathtml)
18970
18971
18972 @table @asis
18973
18974 @item @code{t @emph{file}}
18975
18976 This switch provides the name of a file. This file contains a list of
18977 file names to be converted, and the effect is exactly as though they had
18978 appeared explicitly on the command line. This
18979 is the recommended way to work around the command line length limit on some
18980 systems.
18981 @end table
18982
18983 @node Installing gnathtml,,Invoking gnathtml,The Ada to HTML Converter gnathtml
18984 @anchor{gnat_ugn/gnat_utility_programs installing-gnathtml}@anchor{161}@anchor{gnat_ugn/gnat_utility_programs id18}@anchor{164}
18985 @subsection Installing @code{gnathtml}
18986
18987
18988 @code{Perl} needs to be installed on your machine to run this script.
18989 @code{Perl} is freely available for almost every architecture and
18990 operating system via the Internet.
18991
18992 On Unix systems, you may want to modify the first line of the script
18993 @code{gnathtml}, to explicitly specify where Perl
18994 is located. The syntax of this line is:
18995
18996 @quotation
18997
18998 @example
18999 #!full_path_name_to_perl
19000 @end example
19001 @end quotation
19002
19003 Alternatively, you may run the script using the following command line:
19004
19005 @quotation
19006
19007 @example
19008 $ perl gnathtml.pl [ switches ] files
19009 @end example
19010 @end quotation
19011
19012 @c -- +---------------------------------------------------------------------+
19013
19014 @c -- | The following sections are present only in the PRO and GPL editions |
19015
19016 @c -- +---------------------------------------------------------------------+
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
19027
19028 @node GNAT and Program Execution,Platform-Specific Information,GNAT Utility Programs,Top
19029 @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}
19030 @chapter GNAT and Program Execution
19031
19032
19033 This chapter covers several topics:
19034
19035
19036 @itemize *
19037
19038 @item
19039 @ref{167,,Running and Debugging Ada Programs}
19040
19041 @item
19042 @ref{25,,Profiling}
19043
19044 @item
19045 @ref{168,,Improving Performance}
19046
19047 @item
19048 @ref{169,,Overflow Check Handling in GNAT}
19049
19050 @item
19051 @ref{16a,,Performing Dimensionality Analysis in GNAT}
19052
19053 @item
19054 @ref{16b,,Stack Related Facilities}
19055
19056 @item
19057 @ref{16c,,Memory Management Issues}
19058 @end itemize
19059
19060 @menu
19061 * Running and Debugging Ada Programs::
19062 * Profiling::
19063 * Improving Performance::
19064 * Overflow Check Handling in GNAT::
19065 * Performing Dimensionality Analysis in GNAT::
19066 * Stack Related Facilities::
19067 * Memory Management Issues::
19068
19069 @end menu
19070
19071 @node Running and Debugging Ada Programs,Profiling,,GNAT and Program Execution
19072 @anchor{gnat_ugn/gnat_and_program_execution id2}@anchor{167}@anchor{gnat_ugn/gnat_and_program_execution running-and-debugging-ada-programs}@anchor{24}
19073 @section Running and Debugging Ada Programs
19074
19075
19076 @geindex Debugging
19077
19078 This section discusses how to debug Ada programs.
19079
19080 An incorrect Ada program may be handled in three ways by the GNAT compiler:
19081
19082
19083 @itemize *
19084
19085 @item
19086 The illegality may be a violation of the static semantics of Ada. In
19087 that case GNAT diagnoses the constructs in the program that are illegal.
19088 It is then a straightforward matter for the user to modify those parts of
19089 the program.
19090
19091 @item
19092 The illegality may be a violation of the dynamic semantics of Ada. In
19093 that case the program compiles and executes, but may generate incorrect
19094 results, or may terminate abnormally with some exception.
19095
19096 @item
19097 When presented with a program that contains convoluted errors, GNAT
19098 itself may terminate abnormally without providing full diagnostics on
19099 the incorrect user program.
19100 @end itemize
19101
19102 @geindex Debugger
19103
19104 @geindex gdb
19105
19106 @menu
19107 * The GNAT Debugger GDB::
19108 * Running GDB::
19109 * Introduction to GDB Commands::
19110 * Using Ada Expressions::
19111 * Calling User-Defined Subprograms::
19112 * Using the next Command in a Function::
19113 * Stopping When Ada Exceptions Are Raised::
19114 * Ada Tasks::
19115 * Debugging Generic Units::
19116 * Remote Debugging with gdbserver::
19117 * GNAT Abnormal Termination or Failure to Terminate::
19118 * Naming Conventions for GNAT Source Files::
19119 * Getting Internal Debugging Information::
19120 * Stack Traceback::
19121 * Pretty-Printers for the GNAT runtime::
19122
19123 @end menu
19124
19125 @node The GNAT Debugger GDB,Running GDB,,Running and Debugging Ada Programs
19126 @anchor{gnat_ugn/gnat_and_program_execution the-gnat-debugger-gdb}@anchor{16d}@anchor{gnat_ugn/gnat_and_program_execution id3}@anchor{16e}
19127 @subsection The GNAT Debugger GDB
19128
19129
19130 @code{GDB} is a general purpose, platform-independent debugger that
19131 can be used to debug mixed-language programs compiled with @code{gcc},
19132 and in particular is capable of debugging Ada programs compiled with
19133 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
19134 complex Ada data structures.
19135
19136 See @cite{Debugging with GDB},
19137 for full details on the usage of @code{GDB}, including a section on
19138 its usage on programs. This manual should be consulted for full
19139 details. The section that follows is a brief introduction to the
19140 philosophy and use of @code{GDB}.
19141
19142 When GNAT programs are compiled, the compiler optionally writes debugging
19143 information into the generated object file, including information on
19144 line numbers, and on declared types and variables. This information is
19145 separate from the generated code. It makes the object files considerably
19146 larger, but it does not add to the size of the actual executable that
19147 will be loaded into memory, and has no impact on run-time performance. The
19148 generation of debug information is triggered by the use of the
19149 @code{-g} switch in the @code{gcc} or @code{gnatmake} command
19150 used to carry out the compilations. It is important to emphasize that
19151 the use of these options does not change the generated code.
19152
19153 The debugging information is written in standard system formats that
19154 are used by many tools, including debuggers and profilers. The format
19155 of the information is typically designed to describe C types and
19156 semantics, but GNAT implements a translation scheme which allows full
19157 details about Ada types and variables to be encoded into these
19158 standard C formats. Details of this encoding scheme may be found in
19159 the file exp_dbug.ads in the GNAT source distribution. However, the
19160 details of this encoding are, in general, of no interest to a user,
19161 since @code{GDB} automatically performs the necessary decoding.
19162
19163 When a program is bound and linked, the debugging information is
19164 collected from the object files, and stored in the executable image of
19165 the program. Again, this process significantly increases the size of
19166 the generated executable file, but it does not increase the size of
19167 the executable program itself. Furthermore, if this program is run in
19168 the normal manner, it runs exactly as if the debug information were
19169 not present, and takes no more actual memory.
19170
19171 However, if the program is run under control of @code{GDB}, the
19172 debugger is activated. The image of the program is loaded, at which
19173 point it is ready to run. If a run command is given, then the program
19174 will run exactly as it would have if @code{GDB} were not present. This
19175 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
19176 entirely non-intrusive until a breakpoint is encountered. If no
19177 breakpoint is ever hit, the program will run exactly as it would if no
19178 debugger were present. When a breakpoint is hit, @code{GDB} accesses
19179 the debugging information and can respond to user commands to inspect
19180 variables, and more generally to report on the state of execution.
19181
19182 @node Running GDB,Introduction to GDB Commands,The GNAT Debugger GDB,Running and Debugging Ada Programs
19183 @anchor{gnat_ugn/gnat_and_program_execution id4}@anchor{16f}@anchor{gnat_ugn/gnat_and_program_execution running-gdb}@anchor{170}
19184 @subsection Running GDB
19185
19186
19187 This section describes how to initiate the debugger.
19188
19189 The debugger can be launched from a @code{GPS} menu or
19190 directly from the command line. The description below covers the latter use.
19191 All the commands shown can be used in the @code{GPS} debug console window,
19192 but there are usually more GUI-based ways to achieve the same effect.
19193
19194 The command to run @code{GDB} is
19195
19196 @quotation
19197
19198 @example
19199 $ gdb program
19200 @end example
19201 @end quotation
19202
19203 where @code{program} is the name of the executable file. This
19204 activates the debugger and results in a prompt for debugger commands.
19205 The simplest command is simply @code{run}, which causes the program to run
19206 exactly as if the debugger were not present. The following section
19207 describes some of the additional commands that can be given to @code{GDB}.
19208
19209 @node Introduction to GDB Commands,Using Ada Expressions,Running GDB,Running and Debugging Ada Programs
19210 @anchor{gnat_ugn/gnat_and_program_execution introduction-to-gdb-commands}@anchor{171}@anchor{gnat_ugn/gnat_and_program_execution id5}@anchor{172}
19211 @subsection Introduction to GDB Commands
19212
19213
19214 @code{GDB} contains a large repertoire of commands.
19215 See @cite{Debugging with GDB} for extensive documentation on the use
19216 of these commands, together with examples of their use. Furthermore,
19217 the command @emph{help} invoked from within GDB activates a simple help
19218 facility which summarizes the available commands and their options.
19219 In this section we summarize a few of the most commonly
19220 used commands to give an idea of what @code{GDB} is about. You should create
19221 a simple program with debugging information and experiment with the use of
19222 these @code{GDB} commands on the program as you read through the
19223 following section.
19224
19225
19226 @itemize *
19227
19228 @item
19229
19230 @table @asis
19231
19232 @item @code{set args @emph{arguments}}
19233
19234 The @emph{arguments} list above is a list of arguments to be passed to
19235 the program on a subsequent run command, just as though the arguments
19236 had been entered on a normal invocation of the program. The @code{set args}
19237 command is not needed if the program does not require arguments.
19238 @end table
19239
19240 @item
19241
19242 @table @asis
19243
19244 @item @code{run}
19245
19246 The @code{run} command causes execution of the program to start from
19247 the beginning. If the program is already running, that is to say if
19248 you are currently positioned at a breakpoint, then a prompt will ask
19249 for confirmation that you want to abandon the current execution and
19250 restart.
19251 @end table
19252
19253 @item
19254
19255 @table @asis
19256
19257 @item @code{breakpoint @emph{location}}
19258
19259 The breakpoint command sets a breakpoint, that is to say a point at which
19260 execution will halt and @code{GDB} will await further
19261 commands. @emph{location} is
19262 either a line number within a file, given in the format @code{file:linenumber},
19263 or it is the name of a subprogram. If you request that a breakpoint be set on
19264 a subprogram that is overloaded, a prompt will ask you to specify on which of
19265 those subprograms you want to breakpoint. You can also
19266 specify that all of them should be breakpointed. If the program is run
19267 and execution encounters the breakpoint, then the program
19268 stops and @code{GDB} signals that the breakpoint was encountered by
19269 printing the line of code before which the program is halted.
19270 @end table
19271
19272 @item
19273
19274 @table @asis
19275
19276 @item @code{catch exception @emph{name}}
19277
19278 This command causes the program execution to stop whenever exception
19279 @code{name} is raised. If @code{name} is omitted, then the execution is
19280 suspended when any exception is raised.
19281 @end table
19282
19283 @item
19284
19285 @table @asis
19286
19287 @item @code{print @emph{expression}}
19288
19289 This will print the value of the given expression. Most simple
19290 Ada expression formats are properly handled by @code{GDB}, so the expression
19291 can contain function calls, variables, operators, and attribute references.
19292 @end table
19293
19294 @item
19295
19296 @table @asis
19297
19298 @item @code{continue}
19299
19300 Continues execution following a breakpoint, until the next breakpoint or the
19301 termination of the program.
19302 @end table
19303
19304 @item
19305
19306 @table @asis
19307
19308 @item @code{step}
19309
19310 Executes a single line after a breakpoint. If the next statement
19311 is a subprogram call, execution continues into (the first statement of)
19312 the called subprogram.
19313 @end table
19314
19315 @item
19316
19317 @table @asis
19318
19319 @item @code{next}
19320
19321 Executes a single line. If this line is a subprogram call, executes and
19322 returns from the call.
19323 @end table
19324
19325 @item
19326
19327 @table @asis
19328
19329 @item @code{list}
19330
19331 Lists a few lines around the current source location. In practice, it
19332 is usually more convenient to have a separate edit window open with the
19333 relevant source file displayed. Successive applications of this command
19334 print subsequent lines. The command can be given an argument which is a
19335 line number, in which case it displays a few lines around the specified one.
19336 @end table
19337
19338 @item
19339
19340 @table @asis
19341
19342 @item @code{backtrace}
19343
19344 Displays a backtrace of the call chain. This command is typically
19345 used after a breakpoint has occurred, to examine the sequence of calls that
19346 leads to the current breakpoint. The display includes one line for each
19347 activation record (frame) corresponding to an active subprogram.
19348 @end table
19349
19350 @item
19351
19352 @table @asis
19353
19354 @item @code{up}
19355
19356 At a breakpoint, @code{GDB} can display the values of variables local
19357 to the current frame. The command @code{up} can be used to
19358 examine the contents of other active frames, by moving the focus up
19359 the stack, that is to say from callee to caller, one frame at a time.
19360 @end table
19361
19362 @item
19363
19364 @table @asis
19365
19366 @item @code{down}
19367
19368 Moves the focus of @code{GDB} down from the frame currently being
19369 examined to the frame of its callee (the reverse of the previous command),
19370 @end table
19371
19372 @item
19373
19374 @table @asis
19375
19376 @item @code{frame @emph{n}}
19377
19378 Inspect the frame with the given number. The value 0 denotes the frame
19379 of the current breakpoint, that is to say the top of the call stack.
19380 @end table
19381
19382 @item
19383
19384 @table @asis
19385
19386 @item @code{kill}
19387
19388 Kills the child process in which the program is running under GDB.
19389 This may be useful for several purposes:
19390
19391
19392 @itemize *
19393
19394 @item
19395 It allows you to recompile and relink your program, since on many systems
19396 you cannot regenerate an executable file while it is running in a process.
19397
19398 @item
19399 You can run your program outside the debugger, on systems that do not
19400 permit executing a program outside GDB while breakpoints are set
19401 within GDB.
19402
19403 @item
19404 It allows you to debug a core dump rather than a running process.
19405 @end itemize
19406 @end table
19407 @end itemize
19408
19409 The above list is a very short introduction to the commands that
19410 @code{GDB} provides. Important additional capabilities, including conditional
19411 breakpoints, the ability to execute command sequences on a breakpoint,
19412 the ability to debug at the machine instruction level and many other
19413 features are described in detail in @cite{Debugging with GDB}.
19414 Note that most commands can be abbreviated
19415 (for example, c for continue, bt for backtrace).
19416
19417 @node Using Ada Expressions,Calling User-Defined Subprograms,Introduction to GDB Commands,Running and Debugging Ada Programs
19418 @anchor{gnat_ugn/gnat_and_program_execution id6}@anchor{173}@anchor{gnat_ugn/gnat_and_program_execution using-ada-expressions}@anchor{174}
19419 @subsection Using Ada Expressions
19420
19421
19422 @geindex Ada expressions (in gdb)
19423
19424 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
19425 extensions. The philosophy behind the design of this subset is
19426
19427 @quotation
19428
19429
19430 @itemize *
19431
19432 @item
19433 That @code{GDB} should provide basic literals and access to operations for
19434 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
19435 leaving more sophisticated computations to subprograms written into the
19436 program (which therefore may be called from @code{GDB}).
19437
19438 @item
19439 That type safety and strict adherence to Ada language restrictions
19440 are not particularly relevant in a debugging context.
19441
19442 @item
19443 That brevity is important to the @code{GDB} user.
19444 @end itemize
19445 @end quotation
19446
19447 Thus, for brevity, the debugger acts as if there were
19448 implicit @code{with} and @code{use} clauses in effect for all user-written
19449 packages, thus making it unnecessary to fully qualify most names with
19450 their packages, regardless of context. Where this causes ambiguity,
19451 @code{GDB} asks the user's intent.
19452
19453 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
19454
19455 @node Calling User-Defined Subprograms,Using the next Command in a Function,Using Ada Expressions,Running and Debugging Ada Programs
19456 @anchor{gnat_ugn/gnat_and_program_execution id7}@anchor{175}@anchor{gnat_ugn/gnat_and_program_execution calling-user-defined-subprograms}@anchor{176}
19457 @subsection Calling User-Defined Subprograms
19458
19459
19460 An important capability of @code{GDB} is the ability to call user-defined
19461 subprograms while debugging. This is achieved simply by entering
19462 a subprogram call statement in the form:
19463
19464 @quotation
19465
19466 @example
19467 call subprogram-name (parameters)
19468 @end example
19469 @end quotation
19470
19471 The keyword @code{call} can be omitted in the normal case where the
19472 @code{subprogram-name} does not coincide with any of the predefined
19473 @code{GDB} commands.
19474
19475 The effect is to invoke the given subprogram, passing it the
19476 list of parameters that is supplied. The parameters can be expressions and
19477 can include variables from the program being debugged. The
19478 subprogram must be defined
19479 at the library level within your program, and @code{GDB} will call the
19480 subprogram within the environment of your program execution (which
19481 means that the subprogram is free to access or even modify variables
19482 within your program).
19483
19484 The most important use of this facility is in allowing the inclusion of
19485 debugging routines that are tailored to particular data structures
19486 in your program. Such debugging routines can be written to provide a suitably
19487 high-level description of an abstract type, rather than a low-level dump
19488 of its physical layout. After all, the standard
19489 @code{GDB print} command only knows the physical layout of your
19490 types, not their abstract meaning. Debugging routines can provide information
19491 at the desired semantic level and are thus enormously useful.
19492
19493 For example, when debugging GNAT itself, it is crucial to have access to
19494 the contents of the tree nodes used to represent the program internally.
19495 But tree nodes are represented simply by an integer value (which in turn
19496 is an index into a table of nodes).
19497 Using the @code{print} command on a tree node would simply print this integer
19498 value, which is not very useful. But the PN routine (defined in file
19499 treepr.adb in the GNAT sources) takes a tree node as input, and displays
19500 a useful high level representation of the tree node, which includes the
19501 syntactic category of the node, its position in the source, the integers
19502 that denote descendant nodes and parent node, as well as varied
19503 semantic information. To study this example in more detail, you might want to
19504 look at the body of the PN procedure in the stated file.
19505
19506 Another useful application of this capability is to deal with situations of
19507 complex data which are not handled suitably by GDB. For example, if you specify
19508 Convention Fortran for a multi-dimensional array, GDB does not know that
19509 the ordering of array elements has been switched and will not properly
19510 address the array elements. In such a case, instead of trying to print the
19511 elements directly from GDB, you can write a callable procedure that prints
19512 the elements in the desired format.
19513
19514 @node Using the next Command in a Function,Stopping When Ada Exceptions Are Raised,Calling User-Defined Subprograms,Running and Debugging Ada Programs
19515 @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}
19516 @subsection Using the @emph{next} Command in a Function
19517
19518
19519 When you use the @code{next} command in a function, the current source
19520 location will advance to the next statement as usual. A special case
19521 arises in the case of a @code{return} statement.
19522
19523 Part of the code for a return statement is the 'epilogue' of the function.
19524 This is the code that returns to the caller. There is only one copy of
19525 this epilogue code, and it is typically associated with the last return
19526 statement in the function if there is more than one return. In some
19527 implementations, this epilogue is associated with the first statement
19528 of the function.
19529
19530 The result is that if you use the @code{next} command from a return
19531 statement that is not the last return statement of the function you
19532 may see a strange apparent jump to the last return statement or to
19533 the start of the function. You should simply ignore this odd jump.
19534 The value returned is always that from the first return statement
19535 that was stepped through.
19536
19537 @node Stopping When Ada Exceptions Are Raised,Ada Tasks,Using the next Command in a Function,Running and Debugging Ada Programs
19538 @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}
19539 @subsection Stopping When Ada Exceptions Are Raised
19540
19541
19542 @geindex Exceptions (in gdb)
19543
19544 You can set catchpoints that stop the program execution when your program
19545 raises selected exceptions.
19546
19547
19548 @itemize *
19549
19550 @item
19551
19552 @table @asis
19553
19554 @item @code{catch exception}
19555
19556 Set a catchpoint that stops execution whenever (any task in the) program
19557 raises any exception.
19558 @end table
19559
19560 @item
19561
19562 @table @asis
19563
19564 @item @code{catch exception @emph{name}}
19565
19566 Set a catchpoint that stops execution whenever (any task in the) program
19567 raises the exception @emph{name}.
19568 @end table
19569
19570 @item
19571
19572 @table @asis
19573
19574 @item @code{catch exception unhandled}
19575
19576 Set a catchpoint that stops executing whenever (any task in the) program
19577 raises an exception for which there is no handler.
19578 @end table
19579
19580 @item
19581
19582 @table @asis
19583
19584 @item @code{info exceptions}, @code{info exceptions @emph{regexp}}
19585
19586 The @code{info exceptions} command permits the user to examine all defined
19587 exceptions within Ada programs. With a regular expression, @emph{regexp}, as
19588 argument, prints out only those exceptions whose name matches @emph{regexp}.
19589 @end table
19590 @end itemize
19591
19592 @geindex Tasks (in gdb)
19593
19594 @node Ada Tasks,Debugging Generic Units,Stopping When Ada Exceptions Are Raised,Running and Debugging Ada Programs
19595 @anchor{gnat_ugn/gnat_and_program_execution ada-tasks}@anchor{17b}@anchor{gnat_ugn/gnat_and_program_execution id10}@anchor{17c}
19596 @subsection Ada Tasks
19597
19598
19599 @code{GDB} allows the following task-related commands:
19600
19601
19602 @itemize *
19603
19604 @item
19605
19606 @table @asis
19607
19608 @item @code{info tasks}
19609
19610 This command shows a list of current Ada tasks, as in the following example:
19611
19612 @example
19613 (gdb) info tasks
19614 ID TID P-ID Thread Pri State Name
19615 1 8088000 0 807e000 15 Child Activation Wait main_task
19616 2 80a4000 1 80ae000 15 Accept/Select Wait b
19617 3 809a800 1 80a4800 15 Child Activation Wait a
19618 * 4 80ae800 3 80b8000 15 Running c
19619 @end example
19620
19621 In this listing, the asterisk before the first task indicates it to be the
19622 currently running task. The first column lists the task ID that is used
19623 to refer to tasks in the following commands.
19624 @end table
19625 @end itemize
19626
19627 @geindex Breakpoints and tasks
19628
19629
19630 @itemize *
19631
19632 @item
19633 @code{break`@w{`}*linespec* `@w{`}task} @emph{taskid}, @code{break} @emph{linespec} @code{task} @emph{taskid} @code{if} ...
19634
19635 @quotation
19636
19637 These commands are like the @code{break ... thread ...}.
19638 @emph{linespec} specifies source lines.
19639
19640 Use the qualifier @code{task @emph{taskid}} with a breakpoint command
19641 to specify that you only want @code{GDB} to stop the program when a
19642 particular Ada task reaches this breakpoint. @emph{taskid} is one of the
19643 numeric task identifiers assigned by @code{GDB}, shown in the first
19644 column of the @code{info tasks} display.
19645
19646 If you do not specify @code{task @emph{taskid}} when you set a
19647 breakpoint, the breakpoint applies to @emph{all} tasks of your
19648 program.
19649
19650 You can use the @code{task} qualifier on conditional breakpoints as
19651 well; in this case, place @code{task @emph{taskid}} before the
19652 breakpoint condition (before the @code{if}).
19653 @end quotation
19654 @end itemize
19655
19656 @geindex Task switching (in gdb)
19657
19658
19659 @itemize *
19660
19661 @item
19662 @code{task @emph{taskno}}
19663
19664 @quotation
19665
19666 This command allows switching to the task referred by @emph{taskno}. In
19667 particular, this allows browsing of the backtrace of the specified
19668 task. It is advisable to switch back to the original task before
19669 continuing execution otherwise the scheduling of the program may be
19670 perturbed.
19671 @end quotation
19672 @end itemize
19673
19674 For more detailed information on the tasking support,
19675 see @cite{Debugging with GDB}.
19676
19677 @geindex Debugging Generic Units
19678
19679 @geindex Generics
19680
19681 @node Debugging Generic Units,Remote Debugging with gdbserver,Ada Tasks,Running and Debugging Ada Programs
19682 @anchor{gnat_ugn/gnat_and_program_execution debugging-generic-units}@anchor{17d}@anchor{gnat_ugn/gnat_and_program_execution id11}@anchor{17e}
19683 @subsection Debugging Generic Units
19684
19685
19686 GNAT always uses code expansion for generic instantiation. This means that
19687 each time an instantiation occurs, a complete copy of the original code is
19688 made, with appropriate substitutions of formals by actuals.
19689
19690 It is not possible to refer to the original generic entities in
19691 @code{GDB}, but it is always possible to debug a particular instance of
19692 a generic, by using the appropriate expanded names. For example, if we have
19693
19694 @quotation
19695
19696 @example
19697 procedure g is
19698
19699 generic package k is
19700 procedure kp (v1 : in out integer);
19701 end k;
19702
19703 package body k is
19704 procedure kp (v1 : in out integer) is
19705 begin
19706 v1 := v1 + 1;
19707 end kp;
19708 end k;
19709
19710 package k1 is new k;
19711 package k2 is new k;
19712
19713 var : integer := 1;
19714
19715 begin
19716 k1.kp (var);
19717 k2.kp (var);
19718 k1.kp (var);
19719 k2.kp (var);
19720 end;
19721 @end example
19722 @end quotation
19723
19724 Then to break on a call to procedure kp in the k2 instance, simply
19725 use the command:
19726
19727 @quotation
19728
19729 @example
19730 (gdb) break g.k2.kp
19731 @end example
19732 @end quotation
19733
19734 When the breakpoint occurs, you can step through the code of the
19735 instance in the normal manner and examine the values of local variables, as for
19736 other units.
19737
19738 @geindex Remote Debugging with gdbserver
19739
19740 @node Remote Debugging with gdbserver,GNAT Abnormal Termination or Failure to Terminate,Debugging Generic Units,Running and Debugging Ada Programs
19741 @anchor{gnat_ugn/gnat_and_program_execution remote-debugging-with-gdbserver}@anchor{17f}@anchor{gnat_ugn/gnat_and_program_execution id12}@anchor{180}
19742 @subsection Remote Debugging with gdbserver
19743
19744
19745 On platforms where gdbserver is supported, it is possible to use this tool
19746 to debug your application remotely. This can be useful in situations
19747 where the program needs to be run on a target host that is different
19748 from the host used for development, particularly when the target has
19749 a limited amount of resources (either CPU and/or memory).
19750
19751 To do so, start your program using gdbserver on the target machine.
19752 gdbserver then automatically suspends the execution of your program
19753 at its entry point, waiting for a debugger to connect to it. The
19754 following commands starts an application and tells gdbserver to
19755 wait for a connection with the debugger on localhost port 4444.
19756
19757 @quotation
19758
19759 @example
19760 $ gdbserver localhost:4444 program
19761 Process program created; pid = 5685
19762 Listening on port 4444
19763 @end example
19764 @end quotation
19765
19766 Once gdbserver has started listening, we can tell the debugger to establish
19767 a connection with this gdbserver, and then start the same debugging session
19768 as if the program was being debugged on the same host, directly under
19769 the control of GDB.
19770
19771 @quotation
19772
19773 @example
19774 $ gdb program
19775 (gdb) target remote targethost:4444
19776 Remote debugging using targethost:4444
19777 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
19778 (gdb) b foo.adb:3
19779 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
19780 (gdb) continue
19781 Continuing.
19782
19783 Breakpoint 1, foo () at foo.adb:4
19784 4 end foo;
19785 @end example
19786 @end quotation
19787
19788 It is also possible to use gdbserver to attach to an already running
19789 program, in which case the execution of that program is simply suspended
19790 until the connection between the debugger and gdbserver is established.
19791
19792 For more information on how to use gdbserver, see the @emph{Using the gdbserver Program}
19793 section in @cite{Debugging with GDB}.
19794 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
19795
19796 @geindex Abnormal Termination or Failure to Terminate
19797
19798 @node GNAT Abnormal Termination or Failure to Terminate,Naming Conventions for GNAT Source Files,Remote Debugging with gdbserver,Running and Debugging Ada Programs
19799 @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}
19800 @subsection GNAT Abnormal Termination or Failure to Terminate
19801
19802
19803 When presented with programs that contain serious errors in syntax
19804 or semantics,
19805 GNAT may on rare occasions experience problems in operation, such
19806 as aborting with a
19807 segmentation fault or illegal memory access, raising an internal
19808 exception, terminating abnormally, or failing to terminate at all.
19809 In such cases, you can activate
19810 various features of GNAT that can help you pinpoint the construct in your
19811 program that is the likely source of the problem.
19812
19813 The following strategies are presented in increasing order of
19814 difficulty, corresponding to your experience in using GNAT and your
19815 familiarity with compiler internals.
19816
19817
19818 @itemize *
19819
19820 @item
19821 Run @code{gcc} with the @code{-gnatf}. This first
19822 switch causes all errors on a given line to be reported. In its absence,
19823 only the first error on a line is displayed.
19824
19825 The @code{-gnatdO} switch causes errors to be displayed as soon as they
19826 are encountered, rather than after compilation is terminated. If GNAT
19827 terminates prematurely or goes into an infinite loop, the last error
19828 message displayed may help to pinpoint the culprit.
19829
19830 @item
19831 Run @code{gcc} with the @code{-v} (verbose) switch. In this
19832 mode, @code{gcc} produces ongoing information about the progress of the
19833 compilation and provides the name of each procedure as code is
19834 generated. This switch allows you to find which Ada procedure was being
19835 compiled when it encountered a code generation problem.
19836 @end itemize
19837
19838 @geindex -gnatdc switch
19839
19840
19841 @itemize *
19842
19843 @item
19844 Run @code{gcc} with the @code{-gnatdc} switch. This is a GNAT specific
19845 switch that does for the front-end what @code{-v} does
19846 for the back end. The system prints the name of each unit,
19847 either a compilation unit or nested unit, as it is being analyzed.
19848
19849 @item
19850 Finally, you can start
19851 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
19852 front-end of GNAT, and can be run independently (normally it is just
19853 called from @code{gcc}). You can use @code{gdb} on @code{gnat1} as you
19854 would on a C program (but @ref{16d,,The GNAT Debugger GDB} for caveats). The
19855 @code{where} command is the first line of attack; the variable
19856 @code{lineno} (seen by @code{print lineno}), used by the second phase of
19857 @code{gnat1} and by the @code{gcc} backend, indicates the source line at
19858 which the execution stopped, and @code{input_file name} indicates the name of
19859 the source file.
19860 @end itemize
19861
19862 @node Naming Conventions for GNAT Source Files,Getting Internal Debugging Information,GNAT Abnormal Termination or Failure to Terminate,Running and Debugging Ada Programs
19863 @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}
19864 @subsection Naming Conventions for GNAT Source Files
19865
19866
19867 In order to examine the workings of the GNAT system, the following
19868 brief description of its organization may be helpful:
19869
19870
19871 @itemize *
19872
19873 @item
19874 Files with prefix @code{sc} contain the lexical scanner.
19875
19876 @item
19877 All files prefixed with @code{par} are components of the parser. The
19878 numbers correspond to chapters of the Ada Reference Manual. For example,
19879 parsing of select statements can be found in @code{par-ch9.adb}.
19880
19881 @item
19882 All files prefixed with @code{sem} perform semantic analysis. The
19883 numbers correspond to chapters of the Ada standard. For example, all
19884 issues involving context clauses can be found in @code{sem_ch10.adb}. In
19885 addition, some features of the language require sufficient special processing
19886 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
19887 dynamic dispatching, etc.
19888
19889 @item
19890 All files prefixed with @code{exp} perform normalization and
19891 expansion of the intermediate representation (abstract syntax tree, or AST).
19892 these files use the same numbering scheme as the parser and semantics files.
19893 For example, the construction of record initialization procedures is done in
19894 @code{exp_ch3.adb}.
19895
19896 @item
19897 The files prefixed with @code{bind} implement the binder, which
19898 verifies the consistency of the compilation, determines an order of
19899 elaboration, and generates the bind file.
19900
19901 @item
19902 The files @code{atree.ads} and @code{atree.adb} detail the low-level
19903 data structures used by the front-end.
19904
19905 @item
19906 The files @code{sinfo.ads} and @code{sinfo.adb} detail the structure of
19907 the abstract syntax tree as produced by the parser.
19908
19909 @item
19910 The files @code{einfo.ads} and @code{einfo.adb} detail the attributes of
19911 all entities, computed during semantic analysis.
19912
19913 @item
19914 Library management issues are dealt with in files with prefix
19915 @code{lib}.
19916
19917 @geindex Annex A (in Ada Reference Manual)
19918
19919 @item
19920 Ada files with the prefix @code{a-} are children of @code{Ada}, as
19921 defined in Annex A.
19922
19923 @geindex Annex B (in Ada reference Manual)
19924
19925 @item
19926 Files with prefix @code{i-} are children of @code{Interfaces}, as
19927 defined in Annex B.
19928
19929 @geindex System (package in Ada Reference Manual)
19930
19931 @item
19932 Files with prefix @code{s-} are children of @code{System}. This includes
19933 both language-defined children and GNAT run-time routines.
19934
19935 @geindex GNAT (package)
19936
19937 @item
19938 Files with prefix @code{g-} are children of @code{GNAT}. These are useful
19939 general-purpose packages, fully documented in their specs. All
19940 the other @code{.c} files are modifications of common @code{gcc} files.
19941 @end itemize
19942
19943 @node Getting Internal Debugging Information,Stack Traceback,Naming Conventions for GNAT Source Files,Running and Debugging Ada Programs
19944 @anchor{gnat_ugn/gnat_and_program_execution id15}@anchor{185}@anchor{gnat_ugn/gnat_and_program_execution getting-internal-debugging-information}@anchor{186}
19945 @subsection Getting Internal Debugging Information
19946
19947
19948 Most compilers have internal debugging switches and modes. GNAT
19949 does also, except GNAT internal debugging switches and modes are not
19950 secret. A summary and full description of all the compiler and binder
19951 debug flags are in the file @code{debug.adb}. You must obtain the
19952 sources of the compiler to see the full detailed effects of these flags.
19953
19954 The switches that print the source of the program (reconstructed from
19955 the internal tree) are of general interest for user programs, as are the
19956 options to print
19957 the full internal tree, and the entity table (the symbol table
19958 information). The reconstructed source provides a readable version of the
19959 program after the front-end has completed analysis and expansion,
19960 and is useful when studying the performance of specific constructs.
19961 For example, constraint checks are indicated, complex aggregates
19962 are replaced with loops and assignments, and tasking primitives
19963 are replaced with run-time calls.
19964
19965 @geindex traceback
19966
19967 @geindex stack traceback
19968
19969 @geindex stack unwinding
19970
19971 @node Stack Traceback,Pretty-Printers for the GNAT runtime,Getting Internal Debugging Information,Running and Debugging Ada Programs
19972 @anchor{gnat_ugn/gnat_and_program_execution stack-traceback}@anchor{187}@anchor{gnat_ugn/gnat_and_program_execution id16}@anchor{188}
19973 @subsection Stack Traceback
19974
19975
19976 Traceback is a mechanism to display the sequence of subprogram calls that
19977 leads to a specified execution point in a program. Often (but not always)
19978 the execution point is an instruction at which an exception has been raised.
19979 This mechanism is also known as @emph{stack unwinding} because it obtains
19980 its information by scanning the run-time stack and recovering the activation
19981 records of all active subprograms. Stack unwinding is one of the most
19982 important tools for program debugging.
19983
19984 The first entry stored in traceback corresponds to the deepest calling level,
19985 that is to say the subprogram currently executing the instruction
19986 from which we want to obtain the traceback.
19987
19988 Note that there is no runtime performance penalty when stack traceback
19989 is enabled, and no exception is raised during program execution.
19990
19991 @geindex traceback
19992 @geindex non-symbolic
19993
19994 @menu
19995 * Non-Symbolic Traceback::
19996 * Symbolic Traceback::
19997
19998 @end menu
19999
20000 @node Non-Symbolic Traceback,Symbolic Traceback,,Stack Traceback
20001 @anchor{gnat_ugn/gnat_and_program_execution non-symbolic-traceback}@anchor{189}@anchor{gnat_ugn/gnat_and_program_execution id17}@anchor{18a}
20002 @subsubsection Non-Symbolic Traceback
20003
20004
20005 Note: this feature is not supported on all platforms. See
20006 @code{GNAT.Traceback} spec in @code{g-traceb.ads}
20007 for a complete list of supported platforms.
20008
20009 @subsubheading Tracebacks From an Unhandled Exception
20010
20011
20012 A runtime non-symbolic traceback is a list of addresses of call instructions.
20013 To enable this feature you must use the @code{-E}
20014 @code{gnatbind} option. With this option a stack traceback is stored as part
20015 of exception information. You can retrieve this information using the
20016 @code{addr2line} tool.
20017
20018 Here is a simple example:
20019
20020 @quotation
20021
20022 @example
20023 procedure STB is
20024
20025 procedure P1 is
20026 begin
20027 raise Constraint_Error;
20028 end P1;
20029
20030 procedure P2 is
20031 begin
20032 P1;
20033 end P2;
20034
20035 begin
20036 P2;
20037 end STB;
20038 @end example
20039
20040 @example
20041 $ gnatmake stb -bargs -E
20042 $ stb
20043
20044 Execution terminated by unhandled exception
20045 Exception name: CONSTRAINT_ERROR
20046 Message: stb.adb:5
20047 Call stack traceback locations:
20048 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20049 @end example
20050 @end quotation
20051
20052 As we see the traceback lists a sequence of addresses for the unhandled
20053 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
20054 guess that this exception come from procedure P1. To translate these
20055 addresses into the source lines where the calls appear, the
20056 @code{addr2line} tool, described below, is invaluable. The use of this tool
20057 requires the program to be compiled with debug information.
20058
20059 @quotation
20060
20061 @example
20062 $ gnatmake -g stb -bargs -E
20063 $ stb
20064
20065 Execution terminated by unhandled exception
20066 Exception name: CONSTRAINT_ERROR
20067 Message: stb.adb:5
20068 Call stack traceback locations:
20069 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20070
20071 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
20072 0x4011f1 0x77e892a4
20073
20074 00401373 at d:/stb/stb.adb:5
20075 0040138B at d:/stb/stb.adb:10
20076 0040139C at d:/stb/stb.adb:14
20077 00401335 at d:/stb/b~stb.adb:104
20078 004011C4 at /build/.../crt1.c:200
20079 004011F1 at /build/.../crt1.c:222
20080 77E892A4 in ?? at ??:0
20081 @end example
20082 @end quotation
20083
20084 The @code{addr2line} tool has several other useful options:
20085
20086 @quotation
20087
20088
20089 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
20090 @item
20091
20092 @code{--functions}
20093
20094 @tab
20095
20096 to get the function name corresponding to any location
20097
20098 @item
20099
20100 @code{--demangle=gnat}
20101
20102 @tab
20103
20104 to use the gnat decoding mode for the function names.
20105 Note that for binutils version 2.9.x the option is
20106 simply @code{--demangle}.
20107
20108 @end multitable
20109
20110
20111 @example
20112 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
20113 0x40139c 0x401335 0x4011c4 0x4011f1
20114
20115 00401373 in stb.p1 at d:/stb/stb.adb:5
20116 0040138B in stb.p2 at d:/stb/stb.adb:10
20117 0040139C in stb at d:/stb/stb.adb:14
20118 00401335 in main at d:/stb/b~stb.adb:104
20119 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
20120 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
20121 @end example
20122 @end quotation
20123
20124 From this traceback we can see that the exception was raised in
20125 @code{stb.adb} at line 5, which was reached from a procedure call in
20126 @code{stb.adb} at line 10, and so on. The @code{b~std.adb} is the binder file,
20127 which contains the call to the main program.
20128 @ref{11c,,Running gnatbind}. The remaining entries are assorted runtime routines,
20129 and the output will vary from platform to platform.
20130
20131 It is also possible to use @code{GDB} with these traceback addresses to debug
20132 the program. For example, we can break at a given code location, as reported
20133 in the stack traceback:
20134
20135 @quotation
20136
20137 @example
20138 $ gdb -nw stb
20139 @end example
20140 @end quotation
20141
20142 Furthermore, this feature is not implemented inside Windows DLL. Only
20143 the non-symbolic traceback is reported in this case.
20144
20145 @quotation
20146
20147 @example
20148 (gdb) break *0x401373
20149 Breakpoint 1 at 0x401373: file stb.adb, line 5.
20150 @end example
20151 @end quotation
20152
20153 It is important to note that the stack traceback addresses
20154 do not change when debug information is included. This is particularly useful
20155 because it makes it possible to release software without debug information (to
20156 minimize object size), get a field report that includes a stack traceback
20157 whenever an internal bug occurs, and then be able to retrieve the sequence
20158 of calls with the same program compiled with debug information.
20159
20160 @subsubheading Tracebacks From Exception Occurrences
20161
20162
20163 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
20164 The stack traceback is attached to the exception information string, and can
20165 be retrieved in an exception handler within the Ada program, by means of the
20166 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
20167
20168 @quotation
20169
20170 @example
20171 with Ada.Text_IO;
20172 with Ada.Exceptions;
20173
20174 procedure STB is
20175
20176 use Ada;
20177 use Ada.Exceptions;
20178
20179 procedure P1 is
20180 K : Positive := 1;
20181 begin
20182 K := K - 1;
20183 exception
20184 when E : others =>
20185 Text_IO.Put_Line (Exception_Information (E));
20186 end P1;
20187
20188 procedure P2 is
20189 begin
20190 P1;
20191 end P2;
20192
20193 begin
20194 P2;
20195 end STB;
20196 @end example
20197 @end quotation
20198
20199 This program will output:
20200
20201 @quotation
20202
20203 @example
20204 $ stb
20205
20206 Exception name: CONSTRAINT_ERROR
20207 Message: stb.adb:12
20208 Call stack traceback locations:
20209 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
20210 @end example
20211 @end quotation
20212
20213 @subsubheading Tracebacks From Anywhere in a Program
20214
20215
20216 It is also possible to retrieve a stack traceback from anywhere in a
20217 program. For this you need to
20218 use the @code{GNAT.Traceback} API. This package includes a procedure called
20219 @code{Call_Chain} that computes a complete stack traceback, as well as useful
20220 display procedures described below. It is not necessary to use the
20221 @code{-E} @code{gnatbind} option in this case, because the stack traceback mechanism
20222 is invoked explicitly.
20223
20224 In the following example we compute a traceback at a specific location in
20225 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
20226 convert addresses to strings:
20227
20228 @quotation
20229
20230 @example
20231 with Ada.Text_IO;
20232 with GNAT.Traceback;
20233 with GNAT.Debug_Utilities;
20234
20235 procedure STB is
20236
20237 use Ada;
20238 use GNAT;
20239 use GNAT.Traceback;
20240
20241 procedure P1 is
20242 TB : Tracebacks_Array (1 .. 10);
20243 -- We are asking for a maximum of 10 stack frames.
20244 Len : Natural;
20245 -- Len will receive the actual number of stack frames returned.
20246 begin
20247 Call_Chain (TB, Len);
20248
20249 Text_IO.Put ("In STB.P1 : ");
20250
20251 for K in 1 .. Len loop
20252 Text_IO.Put (Debug_Utilities.Image (TB (K)));
20253 Text_IO.Put (' ');
20254 end loop;
20255
20256 Text_IO.New_Line;
20257 end P1;
20258
20259 procedure P2 is
20260 begin
20261 P1;
20262 end P2;
20263
20264 begin
20265 P2;
20266 end STB;
20267 @end example
20268
20269 @example
20270 $ gnatmake -g stb
20271 $ stb
20272
20273 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
20274 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
20275 @end example
20276 @end quotation
20277
20278 You can then get further information by invoking the @code{addr2line}
20279 tool as described earlier (note that the hexadecimal addresses
20280 need to be specified in C format, with a leading '0x').
20281
20282 @geindex traceback
20283 @geindex symbolic
20284
20285 @node Symbolic Traceback,,Non-Symbolic Traceback,Stack Traceback
20286 @anchor{gnat_ugn/gnat_and_program_execution id18}@anchor{18b}@anchor{gnat_ugn/gnat_and_program_execution symbolic-traceback}@anchor{18c}
20287 @subsubsection Symbolic Traceback
20288
20289
20290 A symbolic traceback is a stack traceback in which procedure names are
20291 associated with each code location.
20292
20293 Note that this feature is not supported on all platforms. See
20294 @code{GNAT.Traceback.Symbolic} spec in @code{g-trasym.ads} for a complete
20295 list of currently supported platforms.
20296
20297 Note that the symbolic traceback requires that the program be compiled
20298 with debug information. If it is not compiled with debug information
20299 only the non-symbolic information will be valid.
20300
20301 @subsubheading Tracebacks From Exception Occurrences
20302
20303
20304 Here is an example:
20305
20306 @quotation
20307
20308 @example
20309 with Ada.Text_IO;
20310 with GNAT.Traceback.Symbolic;
20311
20312 procedure STB is
20313
20314 procedure P1 is
20315 begin
20316 raise Constraint_Error;
20317 end P1;
20318
20319 procedure P2 is
20320 begin
20321 P1;
20322 end P2;
20323
20324 procedure P3 is
20325 begin
20326 P2;
20327 end P3;
20328
20329 begin
20330 P3;
20331 exception
20332 when E : others =>
20333 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
20334 end STB;
20335 @end example
20336
20337 @example
20338 $ gnatmake -g .\stb -bargs -E
20339 $ stb
20340
20341 0040149F in stb.p1 at stb.adb:8
20342 004014B7 in stb.p2 at stb.adb:13
20343 004014CF in stb.p3 at stb.adb:18
20344 004015DD in ada.stb at stb.adb:22
20345 00401461 in main at b~stb.adb:168
20346 004011C4 in __mingw_CRTStartup at crt1.c:200
20347 004011F1 in mainCRTStartup at crt1.c:222
20348 77E892A4 in ?? at ??:0
20349 @end example
20350 @end quotation
20351
20352 In the above example the @code{.\} syntax in the @code{gnatmake} command
20353 is currently required by @code{addr2line} for files that are in
20354 the current working directory.
20355 Moreover, the exact sequence of linker options may vary from platform
20356 to platform.
20357 The above @code{-largs} section is for Windows platforms. By contrast,
20358 under Unix there is no need for the @code{-largs} section.
20359 Differences across platforms are due to details of linker implementation.
20360
20361 @subsubheading Tracebacks From Anywhere in a Program
20362
20363
20364 It is possible to get a symbolic stack traceback
20365 from anywhere in a program, just as for non-symbolic tracebacks.
20366 The first step is to obtain a non-symbolic
20367 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
20368 information. Here is an example:
20369
20370 @quotation
20371
20372 @example
20373 with Ada.Text_IO;
20374 with GNAT.Traceback;
20375 with GNAT.Traceback.Symbolic;
20376
20377 procedure STB is
20378
20379 use Ada;
20380 use GNAT.Traceback;
20381 use GNAT.Traceback.Symbolic;
20382
20383 procedure P1 is
20384 TB : Tracebacks_Array (1 .. 10);
20385 -- We are asking for a maximum of 10 stack frames.
20386 Len : Natural;
20387 -- Len will receive the actual number of stack frames returned.
20388 begin
20389 Call_Chain (TB, Len);
20390 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
20391 end P1;
20392
20393 procedure P2 is
20394 begin
20395 P1;
20396 end P2;
20397
20398 begin
20399 P2;
20400 end STB;
20401 @end example
20402 @end quotation
20403
20404 @subsubheading Automatic Symbolic Tracebacks
20405
20406
20407 Symbolic tracebacks may also be enabled by using the -Es switch to gnatbind (as
20408 in @code{gprbuild -g ... -bargs -Es}).
20409 This will cause the Exception_Information to contain a symbolic traceback,
20410 which will also be printed if an unhandled exception terminates the
20411 program.
20412
20413 @node Pretty-Printers for the GNAT runtime,,Stack Traceback,Running and Debugging Ada Programs
20414 @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}
20415 @subsection Pretty-Printers for the GNAT runtime
20416
20417
20418 As discussed in @cite{Calling User-Defined Subprograms}, GDB's
20419 @code{print} command only knows about the physical layout of program data
20420 structures and therefore normally displays only low-level dumps, which
20421 are often hard to understand.
20422
20423 An example of this is when trying to display the contents of an Ada
20424 standard container, such as @code{Ada.Containers.Ordered_Maps.Map}:
20425
20426 @quotation
20427
20428 @example
20429 with Ada.Containers.Ordered_Maps;
20430
20431 procedure PP is
20432 package Int_To_Nat is
20433 new Ada.Containers.Ordered_Maps (Integer, Natural);
20434
20435 Map : Int_To_Nat.Map;
20436 begin
20437 Map.Insert (1, 10);
20438 Map.Insert (2, 20);
20439 Map.Insert (3, 30);
20440
20441 Map.Clear; -- BREAK HERE
20442 end PP;
20443 @end example
20444 @end quotation
20445
20446 When this program is built with debugging information and run under
20447 GDB up to the @code{Map.Clear} statement, trying to print @code{Map} will
20448 yield information that is only relevant to the developers of our standard
20449 containers:
20450
20451 @quotation
20452
20453 @example
20454 (gdb) print map
20455 $1 = (
20456 tree => (
20457 first => 0x64e010,
20458 last => 0x64e070,
20459 root => 0x64e040,
20460 length => 3,
20461 tc => (
20462 busy => 0,
20463 lock => 0
20464 )
20465 )
20466 )
20467 @end example
20468 @end quotation
20469
20470 Fortunately, GDB has a feature called pretty-printers@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Introduction},
20471 which allows customizing how GDB displays data structures. The GDB
20472 shipped with GNAT embeds such pretty-printers for the most common
20473 containers in the standard library. To enable them, either run the
20474 following command manually under GDB or add it to your @code{.gdbinit} file:
20475
20476 @quotation
20477
20478 @example
20479 python import gnatdbg; gnatdbg.setup()
20480 @end example
20481 @end quotation
20482
20483 Once this is done, GDB's @code{print} command will automatically use
20484 these pretty-printers when appropriate. Using the previous example:
20485
20486 @quotation
20487
20488 @example
20489 (gdb) print map
20490 $1 = pp.int_to_nat.map of length 3 = @{
20491 [1] = 10,
20492 [2] = 20,
20493 [3] = 30
20494 @}
20495 @end example
20496 @end quotation
20497
20498 Pretty-printers are invoked each time GDB tries to display a value,
20499 including when displaying the arguments of a called subprogram (in
20500 GDB's @code{backtrace} command) or when printing the value returned by a
20501 function (in GDB's @code{finish} command).
20502
20503 To display a value without involving pretty-printers, @code{print} can be
20504 invoked with its @code{/r} option:
20505
20506 @quotation
20507
20508 @example
20509 (gdb) print/r map
20510 $1 = (
20511 tree => (...
20512 @end example
20513 @end quotation
20514
20515 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}
20516 for more information.
20517
20518 @geindex Profiling
20519
20520 @node Profiling,Improving Performance,Running and Debugging Ada Programs,GNAT and Program Execution
20521 @anchor{gnat_ugn/gnat_and_program_execution profiling}@anchor{25}@anchor{gnat_ugn/gnat_and_program_execution id20}@anchor{18f}
20522 @section Profiling
20523
20524
20525 This section describes how to use the the @code{gprof} profiler tool on Ada
20526 programs.
20527
20528 @geindex gprof
20529
20530 @geindex Profiling
20531
20532 @menu
20533 * Profiling an Ada Program with gprof::
20534
20535 @end menu
20536
20537 @node Profiling an Ada Program with gprof,,,Profiling
20538 @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}
20539 @subsection Profiling an Ada Program with gprof
20540
20541
20542 This section is not meant to be an exhaustive documentation of @code{gprof}.
20543 Full documentation for it can be found in the @cite{GNU Profiler User's Guide}
20544 documentation that is part of this GNAT distribution.
20545
20546 Profiling a program helps determine the parts of a program that are executed
20547 most often, and are therefore the most time-consuming.
20548
20549 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
20550 better handle Ada programs and multitasking.
20551 It is currently supported on the following platforms
20552
20553
20554 @itemize *
20555
20556 @item
20557 linux x86/x86_64
20558
20559 @item
20560 windows x86
20561 @end itemize
20562
20563 In order to profile a program using @code{gprof}, several steps are needed:
20564
20565
20566 @enumerate
20567
20568 @item
20569 Instrument the code, which requires a full recompilation of the project with the
20570 proper switches.
20571
20572 @item
20573 Execute the program under the analysis conditions, i.e. with the desired
20574 input.
20575
20576 @item
20577 Analyze the results using the @code{gprof} tool.
20578 @end enumerate
20579
20580 The following sections detail the different steps, and indicate how
20581 to interpret the results.
20582
20583 @menu
20584 * Compilation for profiling::
20585 * Program execution::
20586 * Running gprof::
20587 * Interpretation of profiling results::
20588
20589 @end menu
20590
20591 @node Compilation for profiling,Program execution,,Profiling an Ada Program with gprof
20592 @anchor{gnat_ugn/gnat_and_program_execution id22}@anchor{192}@anchor{gnat_ugn/gnat_and_program_execution compilation-for-profiling}@anchor{193}
20593 @subsubsection Compilation for profiling
20594
20595
20596 @geindex -pg (gcc)
20597 @geindex for profiling
20598
20599 @geindex -pg (gnatlink)
20600 @geindex for profiling
20601
20602 In order to profile a program the first step is to tell the compiler
20603 to generate the necessary profiling information. The compiler switch to be used
20604 is @code{-pg}, which must be added to other compilation switches. This
20605 switch needs to be specified both during compilation and link stages, and can
20606 be specified once when using gnatmake:
20607
20608 @quotation
20609
20610 @example
20611 $ gnatmake -f -pg -P my_project
20612 @end example
20613 @end quotation
20614
20615 Note that only the objects that were compiled with the @code{-pg} switch will
20616 be profiled; if you need to profile your whole project, use the @code{-f}
20617 gnatmake switch to force full recompilation.
20618
20619 @node Program execution,Running gprof,Compilation for profiling,Profiling an Ada Program with gprof
20620 @anchor{gnat_ugn/gnat_and_program_execution program-execution}@anchor{194}@anchor{gnat_ugn/gnat_and_program_execution id23}@anchor{195}
20621 @subsubsection Program execution
20622
20623
20624 Once the program has been compiled for profiling, you can run it as usual.
20625
20626 The only constraint imposed by profiling is that the program must terminate
20627 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
20628 properly analyzed.
20629
20630 Once the program completes execution, a data file called @code{gmon.out} is
20631 generated in the directory where the program was launched from. If this file
20632 already exists, it will be overwritten.
20633
20634 @node Running gprof,Interpretation of profiling results,Program execution,Profiling an Ada Program with gprof
20635 @anchor{gnat_ugn/gnat_and_program_execution running-gprof}@anchor{196}@anchor{gnat_ugn/gnat_and_program_execution id24}@anchor{197}
20636 @subsubsection Running gprof
20637
20638
20639 The @code{gprof} tool is called as follow:
20640
20641 @quotation
20642
20643 @example
20644 $ gprof my_prog gmon.out
20645 @end example
20646 @end quotation
20647
20648 or simply:
20649
20650 @quotation
20651
20652 @example
20653 $ gprof my_prog
20654 @end example
20655 @end quotation
20656
20657 The complete form of the gprof command line is the following:
20658
20659 @quotation
20660
20661 @example
20662 $ gprof [switches] [executable [data-file]]
20663 @end example
20664 @end quotation
20665
20666 @code{gprof} supports numerous switches. The order of these
20667 switch does not matter. The full list of options can be found in
20668 the GNU Profiler User's Guide documentation that comes with this documentation.
20669
20670 The following is the subset of those switches that is most relevant:
20671
20672 @geindex --demangle (gprof)
20673
20674
20675 @table @asis
20676
20677 @item @code{--demangle[=@emph{style}]}, @code{--no-demangle}
20678
20679 These options control whether symbol names should be demangled when
20680 printing output. The default is to demangle C++ symbols. The
20681 @code{--no-demangle} option may be used to turn off demangling. Different
20682 compilers have different mangling styles. The optional demangling style
20683 argument can be used to choose an appropriate demangling style for your
20684 compiler, in particular Ada symbols generated by GNAT can be demangled using
20685 @code{--demangle=gnat}.
20686 @end table
20687
20688 @geindex -e (gprof)
20689
20690
20691 @table @asis
20692
20693 @item @code{-e @emph{function_name}}
20694
20695 The @code{-e @emph{function}} option tells @code{gprof} not to print
20696 information about the function @code{function_name} (and its
20697 children...) in the call graph. The function will still be listed
20698 as a child of any functions that call it, but its index number will be
20699 shown as @code{[not printed]}. More than one @code{-e} option may be
20700 given; only one @code{function_name} may be indicated with each @code{-e}
20701 option.
20702 @end table
20703
20704 @geindex -E (gprof)
20705
20706
20707 @table @asis
20708
20709 @item @code{-E @emph{function_name}}
20710
20711 The @code{-E @emph{function}} option works like the @code{-e} option, but
20712 execution time spent in the function (and children who were not called from
20713 anywhere else), will not be used to compute the percentages-of-time for
20714 the call graph. More than one @code{-E} option may be given; only one
20715 @code{function_name} may be indicated with each @code{-E`} option.
20716 @end table
20717
20718 @geindex -f (gprof)
20719
20720
20721 @table @asis
20722
20723 @item @code{-f @emph{function_name}}
20724
20725 The @code{-f @emph{function}} option causes @code{gprof} to limit the
20726 call graph to the function @code{function_name} and its children (and
20727 their children...). More than one @code{-f} option may be given;
20728 only one @code{function_name} may be indicated with each @code{-f}
20729 option.
20730 @end table
20731
20732 @geindex -F (gprof)
20733
20734
20735 @table @asis
20736
20737 @item @code{-F @emph{function_name}}
20738
20739 The @code{-F @emph{function}} option works like the @code{-f} option, but
20740 only time spent in the function and its children (and their
20741 children...) will be used to determine total-time and
20742 percentages-of-time for the call graph. More than one @code{-F} option
20743 may be given; only one @code{function_name} may be indicated with each
20744 @code{-F} option. The @code{-F} option overrides the @code{-E} option.
20745 @end table
20746
20747 @node Interpretation of profiling results,,Running gprof,Profiling an Ada Program with gprof
20748 @anchor{gnat_ugn/gnat_and_program_execution id25}@anchor{198}@anchor{gnat_ugn/gnat_and_program_execution interpretation-of-profiling-results}@anchor{199}
20749 @subsubsection Interpretation of profiling results
20750
20751
20752 The results of the profiling analysis are represented by two arrays: the
20753 'flat profile' and the 'call graph'. Full documentation of those outputs
20754 can be found in the GNU Profiler User's Guide.
20755
20756 The flat profile shows the time spent in each function of the program, and how
20757 many time it has been called. This allows you to locate easily the most
20758 time-consuming functions.
20759
20760 The call graph shows, for each subprogram, the subprograms that call it,
20761 and the subprograms that it calls. It also provides an estimate of the time
20762 spent in each of those callers/called subprograms.
20763
20764 @node Improving Performance,Overflow Check Handling in GNAT,Profiling,GNAT and Program Execution
20765 @anchor{gnat_ugn/gnat_and_program_execution improving-performance}@anchor{26}@anchor{gnat_ugn/gnat_and_program_execution id26}@anchor{168}
20766 @section Improving Performance
20767
20768
20769 @geindex Improving performance
20770
20771 This section presents several topics related to program performance.
20772 It first describes some of the tradeoffs that need to be considered
20773 and some of the techniques for making your program run faster.
20774
20775
20776 It then documents the unused subprogram/data elimination feature,
20777 which can reduce the size of program executables.
20778
20779 @menu
20780 * Performance Considerations::
20781 * Text_IO Suggestions::
20782 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
20783
20784 @end menu
20785
20786 @node Performance Considerations,Text_IO Suggestions,,Improving Performance
20787 @anchor{gnat_ugn/gnat_and_program_execution performance-considerations}@anchor{19a}@anchor{gnat_ugn/gnat_and_program_execution id27}@anchor{19b}
20788 @subsection Performance Considerations
20789
20790
20791 The GNAT system provides a number of options that allow a trade-off
20792 between
20793
20794
20795 @itemize *
20796
20797 @item
20798 performance of the generated code
20799
20800 @item
20801 speed of compilation
20802
20803 @item
20804 minimization of dependences and recompilation
20805
20806 @item
20807 the degree of run-time checking.
20808 @end itemize
20809
20810 The defaults (if no options are selected) aim at improving the speed
20811 of compilation and minimizing dependences, at the expense of performance
20812 of the generated code:
20813
20814
20815 @itemize *
20816
20817 @item
20818 no optimization
20819
20820 @item
20821 no inlining of subprogram calls
20822
20823 @item
20824 all run-time checks enabled except overflow and elaboration checks
20825 @end itemize
20826
20827 These options are suitable for most program development purposes. This
20828 section describes how you can modify these choices, and also provides
20829 some guidelines on debugging optimized code.
20830
20831 @menu
20832 * Controlling Run-Time Checks::
20833 * Use of Restrictions::
20834 * Optimization Levels::
20835 * Debugging Optimized Code::
20836 * Inlining of Subprograms::
20837 * Floating_Point_Operations::
20838 * Vectorization of loops::
20839 * Other Optimization Switches::
20840 * Optimization and Strict Aliasing::
20841 * Aliased Variables and Optimization::
20842 * Atomic Variables and Optimization::
20843 * Passive Task Optimization::
20844
20845 @end menu
20846
20847 @node Controlling Run-Time Checks,Use of Restrictions,,Performance Considerations
20848 @anchor{gnat_ugn/gnat_and_program_execution id28}@anchor{19c}@anchor{gnat_ugn/gnat_and_program_execution controlling-run-time-checks}@anchor{19d}
20849 @subsubsection Controlling Run-Time Checks
20850
20851
20852 By default, GNAT generates all run-time checks, except stack overflow
20853 checks, and checks for access before elaboration on subprogram
20854 calls. The latter are not required in default mode, because all
20855 necessary checking is done at compile time.
20856
20857 @geindex -gnatp (gcc)
20858
20859 @geindex -gnato (gcc)
20860
20861 The gnat switch, @code{-gnatp} allows this default to be modified. See
20862 @ref{f9,,Run-Time Checks}.
20863
20864 Our experience is that the default is suitable for most development
20865 purposes.
20866
20867 Elaboration checks are off by default, and also not needed by default, since
20868 GNAT uses a static elaboration analysis approach that avoids the need for
20869 run-time checking. This manual contains a full chapter discussing the issue
20870 of elaboration checks, and if the default is not satisfactory for your use,
20871 you should read this chapter.
20872
20873 For validity checks, the minimal checks required by the Ada Reference
20874 Manual (for case statements and assignments to array elements) are on
20875 by default. These can be suppressed by use of the @code{-gnatVn} switch.
20876 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
20877 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
20878 it may be reasonable to routinely use @code{-gnatVn}. Validity checks
20879 are also suppressed entirely if @code{-gnatp} is used.
20880
20881 @geindex Overflow checks
20882
20883 @geindex Checks
20884 @geindex overflow
20885
20886 @geindex Suppress
20887
20888 @geindex Unsuppress
20889
20890 @geindex pragma Suppress
20891
20892 @geindex pragma Unsuppress
20893
20894 Note that the setting of the switches controls the default setting of
20895 the checks. They may be modified using either @code{pragma Suppress} (to
20896 remove checks) or @code{pragma Unsuppress} (to add back suppressed
20897 checks) in the program source.
20898
20899 @node Use of Restrictions,Optimization Levels,Controlling Run-Time Checks,Performance Considerations
20900 @anchor{gnat_ugn/gnat_and_program_execution id29}@anchor{19e}@anchor{gnat_ugn/gnat_and_program_execution use-of-restrictions}@anchor{19f}
20901 @subsubsection Use of Restrictions
20902
20903
20904 The use of pragma Restrictions allows you to control which features are
20905 permitted in your program. Apart from the obvious point that if you avoid
20906 relatively expensive features like finalization (enforceable by the use
20907 of pragma Restrictions (No_Finalization), the use of this pragma does not
20908 affect the generated code in most cases.
20909
20910 One notable exception to this rule is that the possibility of task abort
20911 results in some distributed overhead, particularly if finalization or
20912 exception handlers are used. The reason is that certain sections of code
20913 have to be marked as non-abortable.
20914
20915 If you use neither the @code{abort} statement, nor asynchronous transfer
20916 of control (@code{select ... then abort}), then this distributed overhead
20917 is removed, which may have a general positive effect in improving
20918 overall performance. Especially code involving frequent use of tasking
20919 constructs and controlled types will show much improved performance.
20920 The relevant restrictions pragmas are
20921
20922 @quotation
20923
20924 @example
20925 pragma Restrictions (No_Abort_Statements);
20926 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
20927 @end example
20928 @end quotation
20929
20930 It is recommended that these restriction pragmas be used if possible. Note
20931 that this also means that you can write code without worrying about the
20932 possibility of an immediate abort at any point.
20933
20934 @node Optimization Levels,Debugging Optimized Code,Use of Restrictions,Performance Considerations
20935 @anchor{gnat_ugn/gnat_and_program_execution id30}@anchor{1a0}@anchor{gnat_ugn/gnat_and_program_execution optimization-levels}@anchor{fc}
20936 @subsubsection Optimization Levels
20937
20938
20939 @geindex -O (gcc)
20940
20941 Without any optimization option,
20942 the compiler's goal is to reduce the cost of
20943 compilation and to make debugging produce the expected results.
20944 Statements are independent: if you stop the program with a breakpoint between
20945 statements, you can then assign a new value to any variable or change
20946 the program counter to any other statement in the subprogram and get exactly
20947 the results you would expect from the source code.
20948
20949 Turning on optimization makes the compiler attempt to improve the
20950 performance and/or code size at the expense of compilation time and
20951 possibly the ability to debug the program.
20952
20953 If you use multiple
20954 -O options, with or without level numbers,
20955 the last such option is the one that is effective.
20956
20957 The default is optimization off. This results in the fastest compile
20958 times, but GNAT makes absolutely no attempt to optimize, and the
20959 generated programs are considerably larger and slower than when
20960 optimization is enabled. You can use the
20961 @code{-O} switch (the permitted forms are @code{-O0}, @code{-O1}
20962 @code{-O2}, @code{-O3}, and @code{-Os})
20963 to @code{gcc} to control the optimization level:
20964
20965
20966 @itemize *
20967
20968 @item
20969
20970 @table @asis
20971
20972 @item @code{-O0}
20973
20974 No optimization (the default);
20975 generates unoptimized code but has
20976 the fastest compilation time.
20977
20978 Note that many other compilers do substantial optimization even
20979 if 'no optimization' is specified. With gcc, it is very unusual
20980 to use @code{-O0} for production if execution time is of any concern,
20981 since @code{-O0} means (almost) no optimization. This difference
20982 between gcc and other compilers should be kept in mind when
20983 doing performance comparisons.
20984 @end table
20985
20986 @item
20987
20988 @table @asis
20989
20990 @item @code{-O1}
20991
20992 Moderate optimization;
20993 optimizes reasonably well but does not
20994 degrade compilation time significantly.
20995 @end table
20996
20997 @item
20998
20999 @table @asis
21000
21001 @item @code{-O2}
21002
21003 Full optimization;
21004 generates highly optimized code and has
21005 the slowest compilation time.
21006 @end table
21007
21008 @item
21009
21010 @table @asis
21011
21012 @item @code{-O3}
21013
21014 Full optimization as in @code{-O2};
21015 also uses more aggressive automatic inlining of subprograms within a unit
21016 (@ref{10f,,Inlining of Subprograms}) and attempts to vectorize loops.
21017 @end table
21018
21019 @item
21020
21021 @table @asis
21022
21023 @item @code{-Os}
21024
21025 Optimize space usage (code and data) of resulting program.
21026 @end table
21027 @end itemize
21028
21029 Higher optimization levels perform more global transformations on the
21030 program and apply more expensive analysis algorithms in order to generate
21031 faster and more compact code. The price in compilation time, and the
21032 resulting improvement in execution time,
21033 both depend on the particular application and the hardware environment.
21034 You should experiment to find the best level for your application.
21035
21036 Since the precise set of optimizations done at each level will vary from
21037 release to release (and sometime from target to target), it is best to think
21038 of the optimization settings in general terms.
21039 See the @emph{Options That Control Optimization} section in
21040 @cite{Using the GNU Compiler Collection (GCC)}
21041 for details about
21042 the @code{-O} settings and a number of @code{-f} options that
21043 individually enable or disable specific optimizations.
21044
21045 Unlike some other compilation systems, @code{gcc} has
21046 been tested extensively at all optimization levels. There are some bugs
21047 which appear only with optimization turned on, but there have also been
21048 bugs which show up only in @emph{unoptimized} code. Selecting a lower
21049 level of optimization does not improve the reliability of the code
21050 generator, which in practice is highly reliable at all optimization
21051 levels.
21052
21053 Note regarding the use of @code{-O3}: The use of this optimization level
21054 ought not to be automatically preferred over that of level @code{-O2},
21055 since it often results in larger executables which may run more slowly.
21056 See further discussion of this point in @ref{10f,,Inlining of Subprograms}.
21057
21058 @node Debugging Optimized Code,Inlining of Subprograms,Optimization Levels,Performance Considerations
21059 @anchor{gnat_ugn/gnat_and_program_execution debugging-optimized-code}@anchor{1a1}@anchor{gnat_ugn/gnat_and_program_execution id31}@anchor{1a2}
21060 @subsubsection Debugging Optimized Code
21061
21062
21063 @geindex Debugging optimized code
21064
21065 @geindex Optimization and debugging
21066
21067 Although it is possible to do a reasonable amount of debugging at
21068 nonzero optimization levels,
21069 the higher the level the more likely that
21070 source-level constructs will have been eliminated by optimization.
21071 For example, if a loop is strength-reduced, the loop
21072 control variable may be completely eliminated and thus cannot be
21073 displayed in the debugger.
21074 This can only happen at @code{-O2} or @code{-O3}.
21075 Explicit temporary variables that you code might be eliminated at
21076 level @code{-O1} or higher.
21077
21078 @geindex -g (gcc)
21079
21080 The use of the @code{-g} switch,
21081 which is needed for source-level debugging,
21082 affects the size of the program executable on disk,
21083 and indeed the debugging information can be quite large.
21084 However, it has no effect on the generated code (and thus does not
21085 degrade performance)
21086
21087 Since the compiler generates debugging tables for a compilation unit before
21088 it performs optimizations, the optimizing transformations may invalidate some
21089 of the debugging data. You therefore need to anticipate certain
21090 anomalous situations that may arise while debugging optimized code.
21091 These are the most common cases:
21092
21093
21094 @itemize *
21095
21096 @item
21097 @emph{The 'hopping Program Counter':} Repeated @code{step} or @code{next}
21098 commands show
21099 the PC bouncing back and forth in the code. This may result from any of
21100 the following optimizations:
21101
21102
21103 @itemize -
21104
21105 @item
21106 @emph{Common subexpression elimination:} using a single instance of code for a
21107 quantity that the source computes several times. As a result you
21108 may not be able to stop on what looks like a statement.
21109
21110 @item
21111 @emph{Invariant code motion:} moving an expression that does not change within a
21112 loop, to the beginning of the loop.
21113
21114 @item
21115 @emph{Instruction scheduling:} moving instructions so as to
21116 overlap loads and stores (typically) with other code, or in
21117 general to move computations of values closer to their uses. Often
21118 this causes you to pass an assignment statement without the assignment
21119 happening and then later bounce back to the statement when the
21120 value is actually needed. Placing a breakpoint on a line of code
21121 and then stepping over it may, therefore, not always cause all the
21122 expected side-effects.
21123 @end itemize
21124
21125 @item
21126 @emph{The 'big leap':} More commonly known as @emph{cross-jumping}, in which
21127 two identical pieces of code are merged and the program counter suddenly
21128 jumps to a statement that is not supposed to be executed, simply because
21129 it (and the code following) translates to the same thing as the code
21130 that @emph{was} supposed to be executed. This effect is typically seen in
21131 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
21132 a @code{break} in a C @code{switch} statement.
21133
21134 @item
21135 @emph{The 'roving variable':} The symptom is an unexpected value in a variable.
21136 There are various reasons for this effect:
21137
21138
21139 @itemize -
21140
21141 @item
21142 In a subprogram prologue, a parameter may not yet have been moved to its
21143 'home'.
21144
21145 @item
21146 A variable may be dead, and its register re-used. This is
21147 probably the most common cause.
21148
21149 @item
21150 As mentioned above, the assignment of a value to a variable may
21151 have been moved.
21152
21153 @item
21154 A variable may be eliminated entirely by value propagation or
21155 other means. In this case, GCC may incorrectly generate debugging
21156 information for the variable
21157 @end itemize
21158
21159 In general, when an unexpected value appears for a local variable or parameter
21160 you should first ascertain if that value was actually computed by
21161 your program, as opposed to being incorrectly reported by the debugger.
21162 Record fields or
21163 array elements in an object designated by an access value
21164 are generally less of a problem, once you have ascertained that the access
21165 value is sensible.
21166 Typically, this means checking variables in the preceding code and in the
21167 calling subprogram to verify that the value observed is explainable from other
21168 values (one must apply the procedure recursively to those
21169 other values); or re-running the code and stopping a little earlier
21170 (perhaps before the call) and stepping to better see how the variable obtained
21171 the value in question; or continuing to step @emph{from} the point of the
21172 strange value to see if code motion had simply moved the variable's
21173 assignments later.
21174 @end itemize
21175
21176 In light of such anomalies, a recommended technique is to use @code{-O0}
21177 early in the software development cycle, when extensive debugging capabilities
21178 are most needed, and then move to @code{-O1} and later @code{-O2} as
21179 the debugger becomes less critical.
21180 Whether to use the @code{-g} switch in the release version is
21181 a release management issue.
21182 Note that if you use @code{-g} you can then use the @code{strip} program
21183 on the resulting executable,
21184 which removes both debugging information and global symbols.
21185
21186 @node Inlining of Subprograms,Floating_Point_Operations,Debugging Optimized Code,Performance Considerations
21187 @anchor{gnat_ugn/gnat_and_program_execution id32}@anchor{1a3}@anchor{gnat_ugn/gnat_and_program_execution inlining-of-subprograms}@anchor{10f}
21188 @subsubsection Inlining of Subprograms
21189
21190
21191 A call to a subprogram in the current unit is inlined if all the
21192 following conditions are met:
21193
21194
21195 @itemize *
21196
21197 @item
21198 The optimization level is at least @code{-O1}.
21199
21200 @item
21201 The called subprogram is suitable for inlining: It must be small enough
21202 and not contain something that @code{gcc} cannot support in inlined
21203 subprograms.
21204
21205 @geindex pragma Inline
21206
21207 @geindex Inline
21208
21209 @item
21210 Any one of the following applies: @code{pragma Inline} is applied to the
21211 subprogram; the subprogram is local to the unit and called once from
21212 within it; the subprogram is small and optimization level @code{-O2} is
21213 specified; optimization level @code{-O3} is specified.
21214 @end itemize
21215
21216 Calls to subprograms in @emph{with}ed units are normally not inlined.
21217 To achieve actual inlining (that is, replacement of the call by the code
21218 in the body of the subprogram), the following conditions must all be true:
21219
21220
21221 @itemize *
21222
21223 @item
21224 The optimization level is at least @code{-O1}.
21225
21226 @item
21227 The called subprogram is suitable for inlining: It must be small enough
21228 and not contain something that @code{gcc} cannot support in inlined
21229 subprograms.
21230
21231 @item
21232 There is a @code{pragma Inline} for the subprogram.
21233
21234 @item
21235 The @code{-gnatn} switch is used on the command line.
21236 @end itemize
21237
21238 Even if all these conditions are met, it may not be possible for
21239 the compiler to inline the call, due to the length of the body,
21240 or features in the body that make it impossible for the compiler
21241 to do the inlining.
21242
21243 Note that specifying the @code{-gnatn} switch causes additional
21244 compilation dependencies. Consider the following:
21245
21246 @quotation
21247
21248 @example
21249 package R is
21250 procedure Q;
21251 pragma Inline (Q);
21252 end R;
21253 package body R is
21254 ...
21255 end R;
21256
21257 with R;
21258 procedure Main is
21259 begin
21260 ...
21261 R.Q;
21262 end Main;
21263 @end example
21264 @end quotation
21265
21266 With the default behavior (no @code{-gnatn} switch specified), the
21267 compilation of the @code{Main} procedure depends only on its own source,
21268 @code{main.adb}, and the spec of the package in file @code{r.ads}. This
21269 means that editing the body of @code{R} does not require recompiling
21270 @code{Main}.
21271
21272 On the other hand, the call @code{R.Q} is not inlined under these
21273 circumstances. If the @code{-gnatn} switch is present when @code{Main}
21274 is compiled, the call will be inlined if the body of @code{Q} is small
21275 enough, but now @code{Main} depends on the body of @code{R} in
21276 @code{r.adb} as well as on the spec. This means that if this body is edited,
21277 the main program must be recompiled. Note that this extra dependency
21278 occurs whether or not the call is in fact inlined by @code{gcc}.
21279
21280 The use of front end inlining with @code{-gnatN} generates similar
21281 additional dependencies.
21282
21283 @geindex -fno-inline (gcc)
21284
21285 Note: The @code{-fno-inline} switch overrides all other conditions and ensures that
21286 no inlining occurs, unless requested with pragma Inline_Always for @code{gcc}
21287 back-ends. The extra dependences resulting from @code{-gnatn} will still be active,
21288 even if this switch is used to suppress the resulting inlining actions.
21289
21290 @geindex -fno-inline-functions (gcc)
21291
21292 Note: The @code{-fno-inline-functions} switch can be used to prevent
21293 automatic inlining of subprograms if @code{-O3} is used.
21294
21295 @geindex -fno-inline-small-functions (gcc)
21296
21297 Note: The @code{-fno-inline-small-functions} switch can be used to prevent
21298 automatic inlining of small subprograms if @code{-O2} is used.
21299
21300 @geindex -fno-inline-functions-called-once (gcc)
21301
21302 Note: The @code{-fno-inline-functions-called-once} switch
21303 can be used to prevent inlining of subprograms local to the unit
21304 and called once from within it if @code{-O1} is used.
21305
21306 Note regarding the use of @code{-O3}: @code{-gnatn} is made up of two
21307 sub-switches @code{-gnatn1} and @code{-gnatn2} that can be directly
21308 specified in lieu of it, @code{-gnatn} being translated into one of them
21309 based on the optimization level. With @code{-O2} or below, @code{-gnatn}
21310 is equivalent to @code{-gnatn1} which activates pragma @code{Inline} with
21311 moderate inlining across modules. With @code{-O3}, @code{-gnatn} is
21312 equivalent to @code{-gnatn2} which activates pragma @code{Inline} with
21313 full inlining across modules. If you have used pragma @code{Inline} in
21314 appropriate cases, then it is usually much better to use @code{-O2}
21315 and @code{-gnatn} and avoid the use of @code{-O3} which has the additional
21316 effect of inlining subprograms you did not think should be inlined. We have
21317 found that the use of @code{-O3} may slow down the compilation and increase
21318 the code size by performing excessive inlining, leading to increased
21319 instruction cache pressure from the increased code size and thus minor
21320 performance improvements. So the bottom line here is that you should not
21321 automatically assume that @code{-O3} is better than @code{-O2}, and
21322 indeed you should use @code{-O3} only if tests show that it actually
21323 improves performance for your program.
21324
21325 @node Floating_Point_Operations,Vectorization of loops,Inlining of Subprograms,Performance Considerations
21326 @anchor{gnat_ugn/gnat_and_program_execution floating-point-operations}@anchor{1a4}@anchor{gnat_ugn/gnat_and_program_execution id33}@anchor{1a5}
21327 @subsubsection Floating_Point_Operations
21328
21329
21330 @geindex Floating-Point Operations
21331
21332 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
21333 64-bit standard IEEE floating-point representations, and operations will
21334 use standard IEEE arithmetic as provided by the processor. On most, but
21335 not all, architectures, the attribute Machine_Overflows is False for these
21336 types, meaning that the semantics of overflow is implementation-defined.
21337 In the case of GNAT, these semantics correspond to the normal IEEE
21338 treatment of infinities and NaN (not a number) values. For example,
21339 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
21340 avoiding explicit overflow checks, the performance is greatly improved
21341 on many targets. However, if required, floating-point overflow can be
21342 enabled by the use of the pragma Check_Float_Overflow.
21343
21344 Another consideration that applies specifically to x86 32-bit
21345 architectures is which form of floating-point arithmetic is used.
21346 By default the operations use the old style x86 floating-point,
21347 which implements an 80-bit extended precision form (on these
21348 architectures the type Long_Long_Float corresponds to that form).
21349 In addition, generation of efficient code in this mode means that
21350 the extended precision form will be used for intermediate results.
21351 This may be helpful in improving the final precision of a complex
21352 expression. However it means that the results obtained on the x86
21353 will be different from those on other architectures, and for some
21354 algorithms, the extra intermediate precision can be detrimental.
21355
21356 In addition to this old-style floating-point, all modern x86 chips
21357 implement an alternative floating-point operation model referred
21358 to as SSE2. In this model there is no extended form, and furthermore
21359 execution performance is significantly enhanced. To force GNAT to use
21360 this more modern form, use both of the switches:
21361
21362 @quotation
21363
21364 -msse2 -mfpmath=sse
21365 @end quotation
21366
21367 A unit compiled with these switches will automatically use the more
21368 efficient SSE2 instruction set for Float and Long_Float operations.
21369 Note that the ABI has the same form for both floating-point models,
21370 so it is permissible to mix units compiled with and without these
21371 switches.
21372
21373 @node Vectorization of loops,Other Optimization Switches,Floating_Point_Operations,Performance Considerations
21374 @anchor{gnat_ugn/gnat_and_program_execution id34}@anchor{1a6}@anchor{gnat_ugn/gnat_and_program_execution vectorization-of-loops}@anchor{1a7}
21375 @subsubsection Vectorization of loops
21376
21377
21378 @geindex Optimization Switches
21379
21380 You can take advantage of the auto-vectorizer present in the @code{gcc}
21381 back end to vectorize loops with GNAT. The corresponding command line switch
21382 is @code{-ftree-vectorize} but, as it is enabled by default at @code{-O3}
21383 and other aggressive optimizations helpful for vectorization also are enabled
21384 by default at this level, using @code{-O3} directly is recommended.
21385
21386 You also need to make sure that the target architecture features a supported
21387 SIMD instruction set. For example, for the x86 architecture, you should at
21388 least specify @code{-msse2} to get significant vectorization (but you don't
21389 need to specify it for x86-64 as it is part of the base 64-bit architecture).
21390 Similarly, for the PowerPC architecture, you should specify @code{-maltivec}.
21391
21392 The preferred loop form for vectorization is the @code{for} iteration scheme.
21393 Loops with a @code{while} iteration scheme can also be vectorized if they are
21394 very simple, but the vectorizer will quickly give up otherwise. With either
21395 iteration scheme, the flow of control must be straight, in particular no
21396 @code{exit} statement may appear in the loop body. The loop may however
21397 contain a single nested loop, if it can be vectorized when considered alone:
21398
21399 @quotation
21400
21401 @example
21402 A : array (1..4, 1..4) of Long_Float;
21403 S : array (1..4) of Long_Float;
21404
21405 procedure Sum is
21406 begin
21407 for I in A'Range(1) loop
21408 for J in A'Range(2) loop
21409 S (I) := S (I) + A (I, J);
21410 end loop;
21411 end loop;
21412 end Sum;
21413 @end example
21414 @end quotation
21415
21416 The vectorizable operations depend on the targeted SIMD instruction set, but
21417 the adding and some of the multiplying operators are generally supported, as
21418 well as the logical operators for modular types. Note that compiling
21419 with @code{-gnatp} might well reveal cases where some checks do thwart
21420 vectorization.
21421
21422 Type conversions may also prevent vectorization if they involve semantics that
21423 are not directly supported by the code generator or the SIMD instruction set.
21424 A typical example is direct conversion from floating-point to integer types.
21425 The solution in this case is to use the following idiom:
21426
21427 @quotation
21428
21429 @example
21430 Integer (S'Truncation (F))
21431 @end example
21432 @end quotation
21433
21434 if @code{S} is the subtype of floating-point object @code{F}.
21435
21436 In most cases, the vectorizable loops are loops that iterate over arrays.
21437 All kinds of array types are supported, i.e. constrained array types with
21438 static bounds:
21439
21440 @quotation
21441
21442 @example
21443 type Array_Type is array (1 .. 4) of Long_Float;
21444 @end example
21445 @end quotation
21446
21447 constrained array types with dynamic bounds:
21448
21449 @quotation
21450
21451 @example
21452 type Array_Type is array (1 .. Q.N) of Long_Float;
21453
21454 type Array_Type is array (Q.K .. 4) of Long_Float;
21455
21456 type Array_Type is array (Q.K .. Q.N) of Long_Float;
21457 @end example
21458 @end quotation
21459
21460 or unconstrained array types:
21461
21462 @quotation
21463
21464 @example
21465 type Array_Type is array (Positive range <>) of Long_Float;
21466 @end example
21467 @end quotation
21468
21469 The quality of the generated code decreases when the dynamic aspect of the
21470 array type increases, the worst code being generated for unconstrained array
21471 types. This is so because, the less information the compiler has about the
21472 bounds of the array, the more fallback code it needs to generate in order to
21473 fix things up at run time.
21474
21475 It is possible to specify that a given loop should be subject to vectorization
21476 preferably to other optimizations by means of pragma @code{Loop_Optimize}:
21477
21478 @quotation
21479
21480 @example
21481 pragma Loop_Optimize (Vector);
21482 @end example
21483 @end quotation
21484
21485 placed immediately within the loop will convey the appropriate hint to the
21486 compiler for this loop.
21487
21488 It is also possible to help the compiler generate better vectorized code
21489 for a given loop by asserting that there are no loop-carried dependencies
21490 in the loop. Consider for example the procedure:
21491
21492 @quotation
21493
21494 @example
21495 type Arr is array (1 .. 4) of Long_Float;
21496
21497 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
21498 begin
21499 for I in Arr'Range loop
21500 R(I) := X(I) + Y(I);
21501 end loop;
21502 end;
21503 @end example
21504 @end quotation
21505
21506 By default, the compiler cannot unconditionally vectorize the loop because
21507 assigning to a component of the array designated by R in one iteration could
21508 change the value read from the components of the array designated by X or Y
21509 in a later iteration. As a result, the compiler will generate two versions
21510 of the loop in the object code, one vectorized and the other not vectorized,
21511 as well as a test to select the appropriate version at run time. This can
21512 be overcome by another hint:
21513
21514 @quotation
21515
21516 @example
21517 pragma Loop_Optimize (Ivdep);
21518 @end example
21519 @end quotation
21520
21521 placed immediately within the loop will tell the compiler that it can safely
21522 omit the non-vectorized version of the loop as well as the run-time test.
21523
21524 @node Other Optimization Switches,Optimization and Strict Aliasing,Vectorization of loops,Performance Considerations
21525 @anchor{gnat_ugn/gnat_and_program_execution other-optimization-switches}@anchor{1a8}@anchor{gnat_ugn/gnat_and_program_execution id35}@anchor{1a9}
21526 @subsubsection Other Optimization Switches
21527
21528
21529 @geindex Optimization Switches
21530
21531 Since GNAT uses the @code{gcc} back end, all the specialized
21532 @code{gcc} optimization switches are potentially usable. These switches
21533 have not been extensively tested with GNAT but can generally be expected
21534 to work. Examples of switches in this category are @code{-funroll-loops}
21535 and the various target-specific @code{-m} options (in particular, it has
21536 been observed that @code{-march=xxx} can significantly improve performance
21537 on appropriate machines). For full details of these switches, see
21538 the @emph{Submodel Options} section in the @emph{Hardware Models and Configurations}
21539 chapter of @cite{Using the GNU Compiler Collection (GCC)}.
21540
21541 @node Optimization and Strict Aliasing,Aliased Variables and Optimization,Other Optimization Switches,Performance Considerations
21542 @anchor{gnat_ugn/gnat_and_program_execution optimization-and-strict-aliasing}@anchor{f3}@anchor{gnat_ugn/gnat_and_program_execution id36}@anchor{1aa}
21543 @subsubsection Optimization and Strict Aliasing
21544
21545
21546 @geindex Aliasing
21547
21548 @geindex Strict Aliasing
21549
21550 @geindex No_Strict_Aliasing
21551
21552 The strong typing capabilities of Ada allow an optimizer to generate
21553 efficient code in situations where other languages would be forced to
21554 make worst case assumptions preventing such optimizations. Consider
21555 the following example:
21556
21557 @quotation
21558
21559 @example
21560 procedure R is
21561 type Int1 is new Integer;
21562 type Int2 is new Integer;
21563 type Int1A is access Int1;
21564 type Int2A is access Int2;
21565 Int1V : Int1A;
21566 Int2V : Int2A;
21567 ...
21568
21569 begin
21570 ...
21571 for J in Data'Range loop
21572 if Data (J) = Int1V.all then
21573 Int2V.all := Int2V.all + 1;
21574 end if;
21575 end loop;
21576 ...
21577 end R;
21578 @end example
21579 @end quotation
21580
21581 In this example, since the variable @code{Int1V} can only access objects
21582 of type @code{Int1}, and @code{Int2V} can only access objects of type
21583 @code{Int2}, there is no possibility that the assignment to
21584 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
21585 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
21586 for all iterations of the loop and avoid the extra memory reference
21587 required to dereference it each time through the loop.
21588
21589 This kind of optimization, called strict aliasing analysis, is
21590 triggered by specifying an optimization level of @code{-O2} or
21591 higher or @code{-Os} and allows GNAT to generate more efficient code
21592 when access values are involved.
21593
21594 However, although this optimization is always correct in terms of
21595 the formal semantics of the Ada Reference Manual, difficulties can
21596 arise if features like @code{Unchecked_Conversion} are used to break
21597 the typing system. Consider the following complete program example:
21598
21599 @quotation
21600
21601 @example
21602 package p1 is
21603 type int1 is new integer;
21604 type int2 is new integer;
21605 type a1 is access int1;
21606 type a2 is access int2;
21607 end p1;
21608
21609 with p1; use p1;
21610 package p2 is
21611 function to_a2 (Input : a1) return a2;
21612 end p2;
21613
21614 with Unchecked_Conversion;
21615 package body p2 is
21616 function to_a2 (Input : a1) return a2 is
21617 function to_a2u is
21618 new Unchecked_Conversion (a1, a2);
21619 begin
21620 return to_a2u (Input);
21621 end to_a2;
21622 end p2;
21623
21624 with p2; use p2;
21625 with p1; use p1;
21626 with Text_IO; use Text_IO;
21627 procedure m is
21628 v1 : a1 := new int1;
21629 v2 : a2 := to_a2 (v1);
21630 begin
21631 v1.all := 1;
21632 v2.all := 0;
21633 put_line (int1'image (v1.all));
21634 end;
21635 @end example
21636 @end quotation
21637
21638 This program prints out 0 in @code{-O0} or @code{-O1}
21639 mode, but it prints out 1 in @code{-O2} mode. That's
21640 because in strict aliasing mode, the compiler can and
21641 does assume that the assignment to @code{v2.all} could not
21642 affect the value of @code{v1.all}, since different types
21643 are involved.
21644
21645 This behavior is not a case of non-conformance with the standard, since
21646 the Ada RM specifies that an unchecked conversion where the resulting
21647 bit pattern is not a correct value of the target type can result in an
21648 abnormal value and attempting to reference an abnormal value makes the
21649 execution of a program erroneous. That's the case here since the result
21650 does not point to an object of type @code{int2}. This means that the
21651 effect is entirely unpredictable.
21652
21653 However, although that explanation may satisfy a language
21654 lawyer, in practice an applications programmer expects an
21655 unchecked conversion involving pointers to create true
21656 aliases and the behavior of printing 1 seems plain wrong.
21657 In this case, the strict aliasing optimization is unwelcome.
21658
21659 Indeed the compiler recognizes this possibility, and the
21660 unchecked conversion generates a warning:
21661
21662 @quotation
21663
21664 @example
21665 p2.adb:5:07: warning: possible aliasing problem with type "a2"
21666 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
21667 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
21668 @end example
21669 @end quotation
21670
21671 Unfortunately the problem is recognized when compiling the body of
21672 package @code{p2}, but the actual "bad" code is generated while
21673 compiling the body of @code{m} and this latter compilation does not see
21674 the suspicious @code{Unchecked_Conversion}.
21675
21676 As implied by the warning message, there are approaches you can use to
21677 avoid the unwanted strict aliasing optimization in a case like this.
21678
21679 One possibility is to simply avoid the use of @code{-O2}, but
21680 that is a bit drastic, since it throws away a number of useful
21681 optimizations that do not involve strict aliasing assumptions.
21682
21683 A less drastic approach is to compile the program using the
21684 option @code{-fno-strict-aliasing}. Actually it is only the
21685 unit containing the dereferencing of the suspicious pointer
21686 that needs to be compiled. So in this case, if we compile
21687 unit @code{m} with this switch, then we get the expected
21688 value of zero printed. Analyzing which units might need
21689 the switch can be painful, so a more reasonable approach
21690 is to compile the entire program with options @code{-O2}
21691 and @code{-fno-strict-aliasing}. If the performance is
21692 satisfactory with this combination of options, then the
21693 advantage is that the entire issue of possible "wrong"
21694 optimization due to strict aliasing is avoided.
21695
21696 To avoid the use of compiler switches, the configuration
21697 pragma @code{No_Strict_Aliasing} with no parameters may be
21698 used to specify that for all access types, the strict
21699 aliasing optimization should be suppressed.
21700
21701 However, these approaches are still overkill, in that they causes
21702 all manipulations of all access values to be deoptimized. A more
21703 refined approach is to concentrate attention on the specific
21704 access type identified as problematic.
21705
21706 First, if a careful analysis of uses of the pointer shows
21707 that there are no possible problematic references, then
21708 the warning can be suppressed by bracketing the
21709 instantiation of @code{Unchecked_Conversion} to turn
21710 the warning off:
21711
21712 @quotation
21713
21714 @example
21715 pragma Warnings (Off);
21716 function to_a2u is
21717 new Unchecked_Conversion (a1, a2);
21718 pragma Warnings (On);
21719 @end example
21720 @end quotation
21721
21722 Of course that approach is not appropriate for this particular
21723 example, since indeed there is a problematic reference. In this
21724 case we can take one of two other approaches.
21725
21726 The first possibility is to move the instantiation of unchecked
21727 conversion to the unit in which the type is declared. In
21728 this example, we would move the instantiation of
21729 @code{Unchecked_Conversion} from the body of package
21730 @code{p2} to the spec of package @code{p1}. Now the
21731 warning disappears. That's because any use of the
21732 access type knows there is a suspicious unchecked
21733 conversion, and the strict aliasing optimization
21734 is automatically suppressed for the type.
21735
21736 If it is not practical to move the unchecked conversion to the same unit
21737 in which the destination access type is declared (perhaps because the
21738 source type is not visible in that unit), you may use pragma
21739 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
21740 same declarative sequence as the declaration of the access type:
21741
21742 @quotation
21743
21744 @example
21745 type a2 is access int2;
21746 pragma No_Strict_Aliasing (a2);
21747 @end example
21748 @end quotation
21749
21750 Here again, the compiler now knows that the strict aliasing optimization
21751 should be suppressed for any reference to type @code{a2} and the
21752 expected behavior is obtained.
21753
21754 Finally, note that although the compiler can generate warnings for
21755 simple cases of unchecked conversions, there are tricker and more
21756 indirect ways of creating type incorrect aliases which the compiler
21757 cannot detect. Examples are the use of address overlays and unchecked
21758 conversions involving composite types containing access types as
21759 components. In such cases, no warnings are generated, but there can
21760 still be aliasing problems. One safe coding practice is to forbid the
21761 use of address clauses for type overlaying, and to allow unchecked
21762 conversion only for primitive types. This is not really a significant
21763 restriction since any possible desired effect can be achieved by
21764 unchecked conversion of access values.
21765
21766 The aliasing analysis done in strict aliasing mode can certainly
21767 have significant benefits. We have seen cases of large scale
21768 application code where the time is increased by up to 5% by turning
21769 this optimization off. If you have code that includes significant
21770 usage of unchecked conversion, you might want to just stick with
21771 @code{-O1} and avoid the entire issue. If you get adequate
21772 performance at this level of optimization level, that's probably
21773 the safest approach. If tests show that you really need higher
21774 levels of optimization, then you can experiment with @code{-O2}
21775 and @code{-O2 -fno-strict-aliasing} to see how much effect this
21776 has on size and speed of the code. If you really need to use
21777 @code{-O2} with strict aliasing in effect, then you should
21778 review any uses of unchecked conversion of access types,
21779 particularly if you are getting the warnings described above.
21780
21781 @node Aliased Variables and Optimization,Atomic Variables and Optimization,Optimization and Strict Aliasing,Performance Considerations
21782 @anchor{gnat_ugn/gnat_and_program_execution id37}@anchor{1ab}@anchor{gnat_ugn/gnat_and_program_execution aliased-variables-and-optimization}@anchor{1ac}
21783 @subsubsection Aliased Variables and Optimization
21784
21785
21786 @geindex Aliasing
21787
21788 There are scenarios in which programs may
21789 use low level techniques to modify variables
21790 that otherwise might be considered to be unassigned. For example,
21791 a variable can be passed to a procedure by reference, which takes
21792 the address of the parameter and uses the address to modify the
21793 variable's value, even though it is passed as an IN parameter.
21794 Consider the following example:
21795
21796 @quotation
21797
21798 @example
21799 procedure P is
21800 Max_Length : constant Natural := 16;
21801 type Char_Ptr is access all Character;
21802
21803 procedure Get_String(Buffer: Char_Ptr; Size : Integer);
21804 pragma Import (C, Get_String, "get_string");
21805
21806 Name : aliased String (1 .. Max_Length) := (others => ' ');
21807 Temp : Char_Ptr;
21808
21809 function Addr (S : String) return Char_Ptr is
21810 function To_Char_Ptr is
21811 new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
21812 begin
21813 return To_Char_Ptr (S (S'First)'Address);
21814 end;
21815
21816 begin
21817 Temp := Addr (Name);
21818 Get_String (Temp, Max_Length);
21819 end;
21820 @end example
21821 @end quotation
21822
21823 where Get_String is a C function that uses the address in Temp to
21824 modify the variable @code{Name}. This code is dubious, and arguably
21825 erroneous, and the compiler would be entitled to assume that
21826 @code{Name} is never modified, and generate code accordingly.
21827
21828 However, in practice, this would cause some existing code that
21829 seems to work with no optimization to start failing at high
21830 levels of optimzization.
21831
21832 What the compiler does for such cases is to assume that marking
21833 a variable as aliased indicates that some "funny business" may
21834 be going on. The optimizer recognizes the aliased keyword and
21835 inhibits optimizations that assume the value cannot be assigned.
21836 This means that the above example will in fact "work" reliably,
21837 that is, it will produce the expected results.
21838
21839 @node Atomic Variables and Optimization,Passive Task Optimization,Aliased Variables and Optimization,Performance Considerations
21840 @anchor{gnat_ugn/gnat_and_program_execution atomic-variables-and-optimization}@anchor{1ad}@anchor{gnat_ugn/gnat_and_program_execution id38}@anchor{1ae}
21841 @subsubsection Atomic Variables and Optimization
21842
21843
21844 @geindex Atomic
21845
21846 There are two considerations with regard to performance when
21847 atomic variables are used.
21848
21849 First, the RM only guarantees that access to atomic variables
21850 be atomic, it has nothing to say about how this is achieved,
21851 though there is a strong implication that this should not be
21852 achieved by explicit locking code. Indeed GNAT will never
21853 generate any locking code for atomic variable access (it will
21854 simply reject any attempt to make a variable or type atomic
21855 if the atomic access cannot be achieved without such locking code).
21856
21857 That being said, it is important to understand that you cannot
21858 assume that the entire variable will always be accessed. Consider
21859 this example:
21860
21861 @quotation
21862
21863 @example
21864 type R is record
21865 A,B,C,D : Character;
21866 end record;
21867 for R'Size use 32;
21868 for R'Alignment use 4;
21869
21870 RV : R;
21871 pragma Atomic (RV);
21872 X : Character;
21873 ...
21874 X := RV.B;
21875 @end example
21876 @end quotation
21877
21878 You cannot assume that the reference to @code{RV.B}
21879 will read the entire 32-bit
21880 variable with a single load instruction. It is perfectly legitimate if
21881 the hardware allows it to do a byte read of just the B field. This read
21882 is still atomic, which is all the RM requires. GNAT can and does take
21883 advantage of this, depending on the architecture and optimization level.
21884 Any assumption to the contrary is non-portable and risky. Even if you
21885 examine the assembly language and see a full 32-bit load, this might
21886 change in a future version of the compiler.
21887
21888 If your application requires that all accesses to @code{RV} in this
21889 example be full 32-bit loads, you need to make a copy for the access
21890 as in:
21891
21892 @quotation
21893
21894 @example
21895 declare
21896 RV_Copy : constant R := RV;
21897 begin
21898 X := RV_Copy.B;
21899 end;
21900 @end example
21901 @end quotation
21902
21903 Now the reference to RV must read the whole variable.
21904 Actually one can imagine some compiler which figures
21905 out that the whole copy is not required (because only
21906 the B field is actually accessed), but GNAT
21907 certainly won't do that, and we don't know of any
21908 compiler that would not handle this right, and the
21909 above code will in practice work portably across
21910 all architectures (that permit the Atomic declaration).
21911
21912 The second issue with atomic variables has to do with
21913 the possible requirement of generating synchronization
21914 code. For more details on this, consult the sections on
21915 the pragmas Enable/Disable_Atomic_Synchronization in the
21916 GNAT Reference Manual. If performance is critical, and
21917 such synchronization code is not required, it may be
21918 useful to disable it.
21919
21920 @node Passive Task Optimization,,Atomic Variables and Optimization,Performance Considerations
21921 @anchor{gnat_ugn/gnat_and_program_execution passive-task-optimization}@anchor{1af}@anchor{gnat_ugn/gnat_and_program_execution id39}@anchor{1b0}
21922 @subsubsection Passive Task Optimization
21923
21924
21925 @geindex Passive Task
21926
21927 A passive task is one which is sufficiently simple that
21928 in theory a compiler could recognize it an implement it
21929 efficiently without creating a new thread. The original design
21930 of Ada 83 had in mind this kind of passive task optimization, but
21931 only a few Ada 83 compilers attempted it. The problem was that
21932 it was difficult to determine the exact conditions under which
21933 the optimization was possible. The result is a very fragile
21934 optimization where a very minor change in the program can
21935 suddenly silently make a task non-optimizable.
21936
21937 With the revisiting of this issue in Ada 95, there was general
21938 agreement that this approach was fundamentally flawed, and the
21939 notion of protected types was introduced. When using protected
21940 types, the restrictions are well defined, and you KNOW that the
21941 operations will be optimized, and furthermore this optimized
21942 performance is fully portable.
21943
21944 Although it would theoretically be possible for GNAT to attempt to
21945 do this optimization, but it really doesn't make sense in the
21946 context of Ada 95, and none of the Ada 95 compilers implement
21947 this optimization as far as we know. In particular GNAT never
21948 attempts to perform this optimization.
21949
21950 In any new Ada 95 code that is written, you should always
21951 use protected types in place of tasks that might be able to
21952 be optimized in this manner.
21953 Of course this does not help if you have legacy Ada 83 code
21954 that depends on this optimization, but it is unusual to encounter
21955 a case where the performance gains from this optimization
21956 are significant.
21957
21958 Your program should work correctly without this optimization. If
21959 you have performance problems, then the most practical
21960 approach is to figure out exactly where these performance problems
21961 arise, and update those particular tasks to be protected types. Note
21962 that typically clients of the tasks who call entries, will not have
21963 to be modified, only the task definition itself.
21964
21965 @node Text_IO Suggestions,Reducing Size of Executables with Unused Subprogram/Data Elimination,Performance Considerations,Improving Performance
21966 @anchor{gnat_ugn/gnat_and_program_execution text-io-suggestions}@anchor{1b1}@anchor{gnat_ugn/gnat_and_program_execution id40}@anchor{1b2}
21967 @subsection @code{Text_IO} Suggestions
21968
21969
21970 @geindex Text_IO and performance
21971
21972 The @code{Ada.Text_IO} package has fairly high overheads due in part to
21973 the requirement of maintaining page and line counts. If performance
21974 is critical, a recommendation is to use @code{Stream_IO} instead of
21975 @code{Text_IO} for volume output, since this package has less overhead.
21976
21977 If @code{Text_IO} must be used, note that by default output to the standard
21978 output and standard error files is unbuffered (this provides better
21979 behavior when output statements are used for debugging, or if the
21980 progress of a program is observed by tracking the output, e.g. by
21981 using the Unix @emph{tail -f} command to watch redirected output.
21982
21983 If you are generating large volumes of output with @code{Text_IO} and
21984 performance is an important factor, use a designated file instead
21985 of the standard output file, or change the standard output file to
21986 be buffered using @code{Interfaces.C_Streams.setvbuf}.
21987
21988 @node Reducing Size of Executables with Unused Subprogram/Data Elimination,,Text_IO Suggestions,Improving Performance
21989 @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}
21990 @subsection Reducing Size of Executables with Unused Subprogram/Data Elimination
21991
21992
21993 @geindex Uunused subprogram/data elimination
21994
21995 This section describes how you can eliminate unused subprograms and data from
21996 your executable just by setting options at compilation time.
21997
21998 @menu
21999 * About unused subprogram/data elimination::
22000 * Compilation options::
22001 * Example of unused subprogram/data elimination::
22002
22003 @end menu
22004
22005 @node About unused subprogram/data elimination,Compilation options,,Reducing Size of Executables with Unused Subprogram/Data Elimination
22006 @anchor{gnat_ugn/gnat_and_program_execution id42}@anchor{1b5}@anchor{gnat_ugn/gnat_and_program_execution about-unused-subprogram-data-elimination}@anchor{1b6}
22007 @subsubsection About unused subprogram/data elimination
22008
22009
22010 By default, an executable contains all code and data of its composing objects
22011 (directly linked or coming from statically linked libraries), even data or code
22012 never used by this executable.
22013
22014 This feature will allow you to eliminate such unused code from your
22015 executable, making it smaller (in disk and in memory).
22016
22017 This functionality is available on all Linux platforms except for the IA-64
22018 architecture and on all cross platforms using the ELF binary file format.
22019 In both cases GNU binutils version 2.16 or later are required to enable it.
22020
22021 @node Compilation options,Example of unused subprogram/data elimination,About unused subprogram/data elimination,Reducing Size of Executables with Unused Subprogram/Data Elimination
22022 @anchor{gnat_ugn/gnat_and_program_execution id43}@anchor{1b7}@anchor{gnat_ugn/gnat_and_program_execution compilation-options}@anchor{1b8}
22023 @subsubsection Compilation options
22024
22025
22026 The operation of eliminating the unused code and data from the final executable
22027 is directly performed by the linker.
22028
22029 @geindex -ffunction-sections (gcc)
22030
22031 @geindex -fdata-sections (gcc)
22032
22033 In order to do this, it has to work with objects compiled with the
22034 following options:
22035 @code{-ffunction-sections} @code{-fdata-sections}.
22036
22037 These options are usable with C and Ada files.
22038 They will place respectively each
22039 function or data in a separate section in the resulting object file.
22040
22041 Once the objects and static libraries are created with these options, the
22042 linker can perform the dead code elimination. You can do this by setting
22043 the @code{-Wl,--gc-sections} option to gcc command or in the
22044 @code{-largs} section of @code{gnatmake}. This will perform a
22045 garbage collection of code and data never referenced.
22046
22047 If the linker performs a partial link (@code{-r} linker option), then you
22048 will need to provide the entry point using the @code{-e} / @code{--entry}
22049 linker option.
22050
22051 Note that objects compiled without the @code{-ffunction-sections} and
22052 @code{-fdata-sections} options can still be linked with the executable.
22053 However, no dead code elimination will be performed on those objects (they will
22054 be linked as is).
22055
22056 The GNAT static library is now compiled with -ffunction-sections and
22057 -fdata-sections on some platforms. This allows you to eliminate the unused code
22058 and data of the GNAT library from your executable.
22059
22060 @node Example of unused subprogram/data elimination,,Compilation options,Reducing Size of Executables with Unused Subprogram/Data Elimination
22061 @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}
22062 @subsubsection Example of unused subprogram/data elimination
22063
22064
22065 Here is a simple example:
22066
22067 @quotation
22068
22069 @example
22070 with Aux;
22071
22072 procedure Test is
22073 begin
22074 Aux.Used (10);
22075 end Test;
22076
22077 package Aux is
22078 Used_Data : Integer;
22079 Unused_Data : Integer;
22080
22081 procedure Used (Data : Integer);
22082 procedure Unused (Data : Integer);
22083 end Aux;
22084
22085 package body Aux is
22086 procedure Used (Data : Integer) is
22087 begin
22088 Used_Data := Data;
22089 end Used;
22090
22091 procedure Unused (Data : Integer) is
22092 begin
22093 Unused_Data := Data;
22094 end Unused;
22095 end Aux;
22096 @end example
22097 @end quotation
22098
22099 @code{Unused} and @code{Unused_Data} are never referenced in this code
22100 excerpt, and hence they may be safely removed from the final executable.
22101
22102 @quotation
22103
22104 @example
22105 $ gnatmake test
22106
22107 $ nm test | grep used
22108 020015f0 T aux__unused
22109 02005d88 B aux__unused_data
22110 020015cc T aux__used
22111 02005d84 B aux__used_data
22112
22113 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
22114 -largs -Wl,--gc-sections
22115
22116 $ nm test | grep used
22117 02005350 T aux__used
22118 0201ffe0 B aux__used_data
22119 @end example
22120 @end quotation
22121
22122 It can be observed that the procedure @code{Unused} and the object
22123 @code{Unused_Data} are removed by the linker when using the
22124 appropriate options.
22125
22126 @geindex Overflow checks
22127
22128 @geindex Checks (overflow)
22129
22130
22131 @node Overflow Check Handling in GNAT,Performing Dimensionality Analysis in GNAT,Improving Performance,GNAT and Program Execution
22132 @anchor{gnat_ugn/gnat_and_program_execution id50}@anchor{169}@anchor{gnat_ugn/gnat_and_program_execution overflow-check-handling-in-gnat}@anchor{27}
22133 @section Overflow Check Handling in GNAT
22134
22135
22136 This section explains how to control the handling of overflow checks.
22137
22138 @menu
22139 * Background::
22140 * Management of Overflows in GNAT::
22141 * Specifying the Desired Mode::
22142 * Default Settings::
22143 * Implementation Notes::
22144
22145 @end menu
22146
22147 @node Background,Management of Overflows in GNAT,,Overflow Check Handling in GNAT
22148 @anchor{gnat_ugn/gnat_and_program_execution id51}@anchor{1bb}@anchor{gnat_ugn/gnat_and_program_execution background}@anchor{1bc}
22149 @subsection Background
22150
22151
22152 Overflow checks are checks that the compiler may make to ensure
22153 that intermediate results are not out of range. For example:
22154
22155 @quotation
22156
22157 @example
22158 A : Integer;
22159 ...
22160 A := A + 1;
22161 @end example
22162 @end quotation
22163
22164 If @code{A} has the value @code{Integer'Last}, then the addition may cause
22165 overflow since the result is out of range of the type @code{Integer}.
22166 In this case @code{Constraint_Error} will be raised if checks are
22167 enabled.
22168
22169 A trickier situation arises in examples like the following:
22170
22171 @quotation
22172
22173 @example
22174 A, C : Integer;
22175 ...
22176 A := (A + 1) + C;
22177 @end example
22178 @end quotation
22179
22180 where @code{A} is @code{Integer'Last} and @code{C} is @code{-1}.
22181 Now the final result of the expression on the right hand side is
22182 @code{Integer'Last} which is in range, but the question arises whether the
22183 intermediate addition of @code{(A + 1)} raises an overflow error.
22184
22185 The (perhaps surprising) answer is that the Ada language
22186 definition does not answer this question. Instead it leaves
22187 it up to the implementation to do one of two things if overflow
22188 checks are enabled.
22189
22190
22191 @itemize *
22192
22193 @item
22194 raise an exception (@code{Constraint_Error}), or
22195
22196 @item
22197 yield the correct mathematical result which is then used in
22198 subsequent operations.
22199 @end itemize
22200
22201 If the compiler chooses the first approach, then the assignment of this
22202 example will indeed raise @code{Constraint_Error} if overflow checking is
22203 enabled, or result in erroneous execution if overflow checks are suppressed.
22204
22205 But if the compiler
22206 chooses the second approach, then it can perform both additions yielding
22207 the correct mathematical result, which is in range, so no exception
22208 will be raised, and the right result is obtained, regardless of whether
22209 overflow checks are suppressed.
22210
22211 Note that in the first example an
22212 exception will be raised in either case, since if the compiler
22213 gives the correct mathematical result for the addition, it will
22214 be out of range of the target type of the assignment, and thus
22215 fails the range check.
22216
22217 This lack of specified behavior in the handling of overflow for
22218 intermediate results is a source of non-portability, and can thus
22219 be problematic when programs are ported. Most typically this arises
22220 in a situation where the original compiler did not raise an exception,
22221 and then the application is moved to a compiler where the check is
22222 performed on the intermediate result and an unexpected exception is
22223 raised.
22224
22225 Furthermore, when using Ada 2012's preconditions and other
22226 assertion forms, another issue arises. Consider:
22227
22228 @quotation
22229
22230 @example
22231 procedure P (A, B : Integer) with
22232 Pre => A + B <= Integer'Last;
22233 @end example
22234 @end quotation
22235
22236 One often wants to regard arithmetic in a context like this from
22237 a mathematical point of view. So for example, if the two actual parameters
22238 for a call to @code{P} are both @code{Integer'Last}, then
22239 the precondition should be regarded as False. If we are executing
22240 in a mode with run-time checks enabled for preconditions, then we would
22241 like this precondition to fail, rather than raising an exception
22242 because of the intermediate overflow.
22243
22244 However, the language definition leaves the specification of
22245 whether the above condition fails (raising @code{Assert_Error}) or
22246 causes an intermediate overflow (raising @code{Constraint_Error})
22247 up to the implementation.
22248
22249 The situation is worse in a case such as the following:
22250
22251 @quotation
22252
22253 @example
22254 procedure Q (A, B, C : Integer) with
22255 Pre => A + B + C <= Integer'Last;
22256 @end example
22257 @end quotation
22258
22259 Consider the call
22260
22261 @quotation
22262
22263 @example
22264 Q (A => Integer'Last, B => 1, C => -1);
22265 @end example
22266 @end quotation
22267
22268 From a mathematical point of view the precondition
22269 is True, but at run time we may (but are not guaranteed to) get an
22270 exception raised because of the intermediate overflow (and we really
22271 would prefer this precondition to be considered True at run time).
22272
22273 @node Management of Overflows in GNAT,Specifying the Desired Mode,Background,Overflow Check Handling in GNAT
22274 @anchor{gnat_ugn/gnat_and_program_execution management-of-overflows-in-gnat}@anchor{1bd}@anchor{gnat_ugn/gnat_and_program_execution id52}@anchor{1be}
22275 @subsection Management of Overflows in GNAT
22276
22277
22278 To deal with the portability issue, and with the problem of
22279 mathematical versus run-time interpretation of the expressions in
22280 assertions, GNAT provides comprehensive control over the handling
22281 of intermediate overflow. GNAT can operate in three modes, and
22282 furthemore, permits separate selection of operating modes for
22283 the expressions within assertions (here the term 'assertions'
22284 is used in the technical sense, which includes preconditions and so forth)
22285 and for expressions appearing outside assertions.
22286
22287 The three modes are:
22288
22289
22290 @itemize *
22291
22292 @item
22293 @emph{Use base type for intermediate operations} (@code{STRICT})
22294
22295 In this mode, all intermediate results for predefined arithmetic
22296 operators are computed using the base type, and the result must
22297 be in range of the base type. If this is not the
22298 case then either an exception is raised (if overflow checks are
22299 enabled) or the execution is erroneous (if overflow checks are suppressed).
22300 This is the normal default mode.
22301
22302 @item
22303 @emph{Most intermediate overflows avoided} (@code{MINIMIZED})
22304
22305 In this mode, the compiler attempts to avoid intermediate overflows by
22306 using a larger integer type, typically @code{Long_Long_Integer},
22307 as the type in which arithmetic is
22308 performed for predefined arithmetic operators. This may be slightly more
22309 expensive at
22310 run time (compared to suppressing intermediate overflow checks), though
22311 the cost is negligible on modern 64-bit machines. For the examples given
22312 earlier, no intermediate overflows would have resulted in exceptions,
22313 since the intermediate results are all in the range of
22314 @code{Long_Long_Integer} (typically 64-bits on nearly all implementations
22315 of GNAT). In addition, if checks are enabled, this reduces the number of
22316 checks that must be made, so this choice may actually result in an
22317 improvement in space and time behavior.
22318
22319 However, there are cases where @code{Long_Long_Integer} is not large
22320 enough, consider the following example:
22321
22322 @quotation
22323
22324 @example
22325 procedure R (A, B, C, D : Integer) with
22326 Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
22327 @end example
22328 @end quotation
22329
22330 where @code{A} = @code{B} = @code{C} = @code{D} = @code{Integer'Last}.
22331 Now the intermediate results are
22332 out of the range of @code{Long_Long_Integer} even though the final result
22333 is in range and the precondition is True (from a mathematical point
22334 of view). In such a case, operating in this mode, an overflow occurs
22335 for the intermediate computation (which is why this mode
22336 says @emph{most} intermediate overflows are avoided). In this case,
22337 an exception is raised if overflow checks are enabled, and the
22338 execution is erroneous if overflow checks are suppressed.
22339
22340 @item
22341 @emph{All intermediate overflows avoided} (@code{ELIMINATED})
22342
22343 In this mode, the compiler avoids all intermediate overflows
22344 by using arbitrary precision arithmetic as required. In this
22345 mode, the above example with @code{A**2 * B**2} would
22346 not cause intermediate overflow, because the intermediate result
22347 would be evaluated using sufficient precision, and the result
22348 of evaluating the precondition would be True.
22349
22350 This mode has the advantage of avoiding any intermediate
22351 overflows, but at the expense of significant run-time overhead,
22352 including the use of a library (included automatically in this
22353 mode) for multiple-precision arithmetic.
22354
22355 This mode provides cleaner semantics for assertions, since now
22356 the run-time behavior emulates true arithmetic behavior for the
22357 predefined arithmetic operators, meaning that there is never a
22358 conflict between the mathematical view of the assertion, and its
22359 run-time behavior.
22360
22361 Note that in this mode, the behavior is unaffected by whether or
22362 not overflow checks are suppressed, since overflow does not occur.
22363 It is possible for gigantic intermediate expressions to raise
22364 @code{Storage_Error} as a result of attempting to compute the
22365 results of such expressions (e.g. @code{Integer'Last ** Integer'Last})
22366 but overflow is impossible.
22367 @end itemize
22368
22369 Note that these modes apply only to the evaluation of predefined
22370 arithmetic, membership, and comparison operators for signed integer
22371 arithmetic.
22372
22373 For fixed-point arithmetic, checks can be suppressed. But if checks
22374 are enabled
22375 then fixed-point values are always checked for overflow against the
22376 base type for intermediate expressions (that is such checks always
22377 operate in the equivalent of @code{STRICT} mode).
22378
22379 For floating-point, on nearly all architectures, @code{Machine_Overflows}
22380 is False, and IEEE infinities are generated, so overflow exceptions
22381 are never raised. If you want to avoid infinities, and check that
22382 final results of expressions are in range, then you can declare a
22383 constrained floating-point type, and range checks will be carried
22384 out in the normal manner (with infinite values always failing all
22385 range checks).
22386
22387 @node Specifying the Desired Mode,Default Settings,Management of Overflows in GNAT,Overflow Check Handling in GNAT
22388 @anchor{gnat_ugn/gnat_and_program_execution specifying-the-desired-mode}@anchor{f8}@anchor{gnat_ugn/gnat_and_program_execution id53}@anchor{1bf}
22389 @subsection Specifying the Desired Mode
22390
22391
22392 @geindex pragma Overflow_Mode
22393
22394 The desired mode of for handling intermediate overflow can be specified using
22395 either the @code{Overflow_Mode} pragma or an equivalent compiler switch.
22396 The pragma has the form
22397
22398 @quotation
22399
22400 @example
22401 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
22402 @end example
22403 @end quotation
22404
22405 where @code{MODE} is one of
22406
22407
22408 @itemize *
22409
22410 @item
22411 @code{STRICT}: intermediate overflows checked (using base type)
22412
22413 @item
22414 @code{MINIMIZED}: minimize intermediate overflows
22415
22416 @item
22417 @code{ELIMINATED}: eliminate intermediate overflows
22418 @end itemize
22419
22420 The case is ignored, so @code{MINIMIZED}, @code{Minimized} and
22421 @code{minimized} all have the same effect.
22422
22423 If only the @code{General} parameter is present, then the given @code{MODE} applies
22424 to expressions both within and outside assertions. If both arguments
22425 are present, then @code{General} applies to expressions outside assertions,
22426 and @code{Assertions} applies to expressions within assertions. For example:
22427
22428 @quotation
22429
22430 @example
22431 pragma Overflow_Mode
22432 (General => Minimized, Assertions => Eliminated);
22433 @end example
22434 @end quotation
22435
22436 specifies that general expressions outside assertions be evaluated
22437 in 'minimize intermediate overflows' mode, and expressions within
22438 assertions be evaluated in 'eliminate intermediate overflows' mode.
22439 This is often a reasonable choice, avoiding excessive overhead
22440 outside assertions, but assuring a high degree of portability
22441 when importing code from another compiler, while incurring
22442 the extra overhead for assertion expressions to ensure that
22443 the behavior at run time matches the expected mathematical
22444 behavior.
22445
22446 The @code{Overflow_Mode} pragma has the same scoping and placement
22447 rules as pragma @code{Suppress}, so it can occur either as a
22448 configuration pragma, specifying a default for the whole
22449 program, or in a declarative scope, where it applies to the
22450 remaining declarations and statements in that scope.
22451
22452 Note that pragma @code{Overflow_Mode} does not affect whether
22453 overflow checks are enabled or suppressed. It only controls the
22454 method used to compute intermediate values. To control whether
22455 overflow checking is enabled or suppressed, use pragma @code{Suppress}
22456 or @code{Unsuppress} in the usual manner.
22457
22458 @geindex -gnato? (gcc)
22459
22460 @geindex -gnato?? (gcc)
22461
22462 Additionally, a compiler switch @code{-gnato?} or @code{-gnato??}
22463 can be used to control the checking mode default (which can be subsequently
22464 overridden using pragmas).
22465
22466 Here @code{?} is one of the digits @code{1} through @code{3}:
22467
22468 @quotation
22469
22470
22471 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
22472 @item
22473
22474 @code{1}
22475
22476 @tab
22477
22478 use base type for intermediate operations (@code{STRICT})
22479
22480 @item
22481
22482 @code{2}
22483
22484 @tab
22485
22486 minimize intermediate overflows (@code{MINIMIZED})
22487
22488 @item
22489
22490 @code{3}
22491
22492 @tab
22493
22494 eliminate intermediate overflows (@code{ELIMINATED})
22495
22496 @end multitable
22497
22498 @end quotation
22499
22500 As with the pragma, if only one digit appears then it applies to all
22501 cases; if two digits are given, then the first applies outside
22502 assertions, and the second within assertions. Thus the equivalent
22503 of the example pragma above would be
22504 @code{-gnato23}.
22505
22506 If no digits follow the @code{-gnato}, then it is equivalent to
22507 @code{-gnato11},
22508 causing all intermediate operations to be computed using the base
22509 type (@code{STRICT} mode).
22510
22511 @node Default Settings,Implementation Notes,Specifying the Desired Mode,Overflow Check Handling in GNAT
22512 @anchor{gnat_ugn/gnat_and_program_execution id54}@anchor{1c0}@anchor{gnat_ugn/gnat_and_program_execution default-settings}@anchor{1c1}
22513 @subsection Default Settings
22514
22515
22516 The default mode for overflow checks is
22517
22518 @quotation
22519
22520 @example
22521 General => Strict
22522 @end example
22523 @end quotation
22524
22525 which causes all computations both inside and outside assertions to use
22526 the base type.
22527
22528 This retains compatibility with previous versions of
22529 GNAT which suppressed overflow checks by default and always
22530 used the base type for computation of intermediate results.
22531
22532 @c Sphinx allows no emphasis within :index: role. As a workaround we
22533 @c point the index to "switch" and use emphasis for "-gnato".
22534
22535 The
22536 @geindex -gnato (gcc)
22537 switch @code{-gnato} (with no digits following)
22538 is equivalent to
22539
22540 @quotation
22541
22542 @example
22543 General => Strict
22544 @end example
22545 @end quotation
22546
22547 which causes overflow checking of all intermediate overflows
22548 both inside and outside assertions against the base type.
22549
22550 The pragma @code{Suppress (Overflow_Check)} disables overflow
22551 checking, but it has no effect on the method used for computing
22552 intermediate results.
22553
22554 The pragma @code{Unsuppress (Overflow_Check)} enables overflow
22555 checking, but it has no effect on the method used for computing
22556 intermediate results.
22557
22558 @node Implementation Notes,,Default Settings,Overflow Check Handling in GNAT
22559 @anchor{gnat_ugn/gnat_and_program_execution id55}@anchor{1c2}@anchor{gnat_ugn/gnat_and_program_execution implementation-notes}@anchor{1c3}
22560 @subsection Implementation Notes
22561
22562
22563 In practice on typical 64-bit machines, the @code{MINIMIZED} mode is
22564 reasonably efficient, and can be generally used. It also helps
22565 to ensure compatibility with code imported from some other
22566 compiler to GNAT.
22567
22568 Setting all intermediate overflows checking (@code{CHECKED} mode)
22569 makes sense if you want to
22570 make sure that your code is compatible with any other possible
22571 Ada implementation. This may be useful in ensuring portability
22572 for code that is to be exported to some other compiler than GNAT.
22573
22574 The Ada standard allows the reassociation of expressions at
22575 the same precedence level if no parentheses are present. For
22576 example, @code{A+B+C} parses as though it were @code{(A+B)+C}, but
22577 the compiler can reintepret this as @code{A+(B+C)}, possibly
22578 introducing or eliminating an overflow exception. The GNAT
22579 compiler never takes advantage of this freedom, and the
22580 expression @code{A+B+C} will be evaluated as @code{(A+B)+C}.
22581 If you need the other order, you can write the parentheses
22582 explicitly @code{A+(B+C)} and GNAT will respect this order.
22583
22584 The use of @code{ELIMINATED} mode will cause the compiler to
22585 automatically include an appropriate arbitrary precision
22586 integer arithmetic package. The compiler will make calls
22587 to this package, though only in cases where it cannot be
22588 sure that @code{Long_Long_Integer} is sufficient to guard against
22589 intermediate overflows. This package does not use dynamic
22590 alllocation, but it does use the secondary stack, so an
22591 appropriate secondary stack package must be present (this
22592 is always true for standard full Ada, but may require
22593 specific steps for restricted run times such as ZFP).
22594
22595 Although @code{ELIMINATED} mode causes expressions to use arbitrary
22596 precision arithmetic, avoiding overflow, the final result
22597 must be in an appropriate range. This is true even if the
22598 final result is of type @code{[Long_[Long_]]Integer'Base}, which
22599 still has the same bounds as its associated constrained
22600 type at run-time.
22601
22602 Currently, the @code{ELIMINATED} mode is only available on target
22603 platforms for which @code{Long_Long_Integer} is 64-bits (nearly all GNAT
22604 platforms).
22605
22606 @node Performing Dimensionality Analysis in GNAT,Stack Related Facilities,Overflow Check Handling in GNAT,GNAT and Program Execution
22607 @anchor{gnat_ugn/gnat_and_program_execution id56}@anchor{16a}@anchor{gnat_ugn/gnat_and_program_execution performing-dimensionality-analysis-in-gnat}@anchor{28}
22608 @section Performing Dimensionality Analysis in GNAT
22609
22610
22611 @geindex Dimensionality analysis
22612
22613 The GNAT compiler supports dimensionality checking. The user can
22614 specify physical units for objects, and the compiler will verify that uses
22615 of these objects are compatible with their dimensions, in a fashion that is
22616 familiar to engineering practice. The dimensions of algebraic expressions
22617 (including powers with static exponents) are computed from their constituents.
22618
22619 @geindex Dimension_System aspect
22620
22621 @geindex Dimension aspect
22622
22623 This feature depends on Ada 2012 aspect specifications, and is available from
22624 version 7.0.1 of GNAT onwards.
22625 The GNAT-specific aspect @code{Dimension_System}
22626 allows you to define a system of units; the aspect @code{Dimension}
22627 then allows the user to declare dimensioned quantities within a given system.
22628 (These aspects are described in the @emph{Implementation Defined Aspects}
22629 chapter of the @emph{GNAT Reference Manual}).
22630
22631 The major advantage of this model is that it does not require the declaration of
22632 multiple operators for all possible combinations of types: it is only necessary
22633 to use the proper subtypes in object declarations.
22634
22635 @geindex System.Dim.Mks package (GNAT library)
22636
22637 @geindex MKS_Type type
22638
22639 The simplest way to impose dimensionality checking on a computation is to make
22640 use of one of the instantiations of the package @code{System.Dim.Generic_Mks}, which
22641 are part of the GNAT library. This generic package defines a floating-point
22642 type @code{MKS_Type}, for which a sequence of dimension names are specified,
22643 together with their conventional abbreviations. The following should be read
22644 together with the full specification of the package, in file
22645 @code{s-digemk.ads}.
22646
22647 @quotation
22648
22649 @geindex s-digemk.ads file
22650
22651 @example
22652 type Mks_Type is new Float_Type
22653 with
22654 Dimension_System => (
22655 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
22656 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
22657 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
22658 (Unit_Name => Ampere, Unit_Symbol => 'A', Dim_Symbol => 'I'),
22659 (Unit_Name => Kelvin, Unit_Symbol => 'K', Dim_Symbol => "Theta"),
22660 (Unit_Name => Mole, Unit_Symbol => "mol", Dim_Symbol => 'N'),
22661 (Unit_Name => Candela, Unit_Symbol => "cd", Dim_Symbol => 'J'));
22662 @end example
22663 @end quotation
22664
22665 The package then defines a series of subtypes that correspond to these
22666 conventional units. For example:
22667
22668 @quotation
22669
22670 @example
22671 subtype Length is Mks_Type
22672 with
22673 Dimension => (Symbol => 'm', Meter => 1, others => 0);
22674 @end example
22675 @end quotation
22676
22677 and similarly for @code{Mass}, @code{Time}, @code{Electric_Current},
22678 @code{Thermodynamic_Temperature}, @code{Amount_Of_Substance}, and
22679 @code{Luminous_Intensity} (the standard set of units of the SI system).
22680
22681 The package also defines conventional names for values of each unit, for
22682 example:
22683
22684 @quotation
22685
22686 @example
22687 m : constant Length := 1.0;
22688 kg : constant Mass := 1.0;
22689 s : constant Time := 1.0;
22690 A : constant Electric_Current := 1.0;
22691 @end example
22692 @end quotation
22693
22694 as well as useful multiples of these units:
22695
22696 @quotation
22697
22698 @example
22699 cm : constant Length := 1.0E-02;
22700 g : constant Mass := 1.0E-03;
22701 min : constant Time := 60.0;
22702 day : constant Time := 60.0 * 24.0 * min;
22703 ...
22704 @end example
22705 @end quotation
22706
22707 There are three instantiations of @code{System.Dim.Generic_Mks} defined in the
22708 GNAT library:
22709
22710
22711 @itemize *
22712
22713 @item
22714 @code{System.Dim.Float_Mks} based on @code{Float} defined in @code{s-diflmk.ads}.
22715
22716 @item
22717 @code{System.Dim.Long_Mks} based on @code{Long_Float} defined in @code{s-dilomk.ads}.
22718
22719 @item
22720 @code{System.Dim.Mks} based on @code{Long_Long_Float} defined in @code{s-dimmks.ads}.
22721 @end itemize
22722
22723 Using one of these packages, you can then define a derived unit by providing
22724 the aspect that specifies its dimensions within the MKS system, as well as the
22725 string to be used for output of a value of that unit:
22726
22727 @quotation
22728
22729 @example
22730 subtype Acceleration is Mks_Type
22731 with Dimension => ("m/sec^2",
22732 Meter => 1,
22733 Second => -2,
22734 others => 0);
22735 @end example
22736 @end quotation
22737
22738 Here is a complete example of use:
22739
22740 @quotation
22741
22742 @example
22743 with System.Dim.MKS; use System.Dim.Mks;
22744 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
22745 with Text_IO; use Text_IO;
22746 procedure Free_Fall is
22747 subtype Acceleration is Mks_Type
22748 with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
22749 G : constant acceleration := 9.81 * m / (s ** 2);
22750 T : Time := 10.0*s;
22751 Distance : Length;
22752
22753 begin
22754 Put ("Gravitational constant: ");
22755 Put (G, Aft => 2, Exp => 0); Put_Line ("");
22756 Distance := 0.5 * G * T ** 2;
22757 Put ("distance travelled in 10 seconds of free fall ");
22758 Put (Distance, Aft => 2, Exp => 0);
22759 Put_Line ("");
22760 end Free_Fall;
22761 @end example
22762 @end quotation
22763
22764 Execution of this program yields:
22765
22766 @quotation
22767
22768 @example
22769 Gravitational constant: 9.81 m/sec^2
22770 distance travelled in 10 seconds of free fall 490.50 m
22771 @end example
22772 @end quotation
22773
22774 However, incorrect assignments such as:
22775
22776 @quotation
22777
22778 @example
22779 Distance := 5.0;
22780 Distance := 5.0 * kg;
22781 @end example
22782 @end quotation
22783
22784 are rejected with the following diagnoses:
22785
22786 @quotation
22787
22788 @example
22789 Distance := 5.0;
22790 >>> dimensions mismatch in assignment
22791 >>> left-hand side has dimension [L]
22792 >>> right-hand side is dimensionless
22793
22794 Distance := 5.0 * kg:
22795 >>> dimensions mismatch in assignment
22796 >>> left-hand side has dimension [L]
22797 >>> right-hand side has dimension [M]
22798 @end example
22799 @end quotation
22800
22801 The dimensions of an expression are properly displayed, even if there is
22802 no explicit subtype for it. If we add to the program:
22803
22804 @quotation
22805
22806 @example
22807 Put ("Final velocity: ");
22808 Put (G * T, Aft =>2, Exp =>0);
22809 Put_Line ("");
22810 @end example
22811 @end quotation
22812
22813 then the output includes:
22814
22815 @quotation
22816
22817 @example
22818 Final velocity: 98.10 m.s**(-1)
22819 @end example
22820
22821 @geindex Dimensionable type
22822
22823 @geindex Dimensioned subtype
22824 @end quotation
22825
22826 The type @code{Mks_Type} is said to be a @emph{dimensionable type} since it has a
22827 @code{Dimension_System} aspect, and the subtypes @code{Length}, @code{Mass}, etc.,
22828 are said to be @emph{dimensioned subtypes} since each one has a @code{Dimension}
22829 aspect.
22830
22831 @quotation
22832
22833 @geindex Dimension Vector (for a dimensioned subtype)
22834
22835 @geindex Dimension aspect
22836
22837 @geindex Dimension_System aspect
22838 @end quotation
22839
22840 The @code{Dimension} aspect of a dimensioned subtype @code{S} defines a mapping
22841 from the base type's Unit_Names to integer (or, more generally, rational)
22842 values. This mapping is the @emph{dimension vector} (also referred to as the
22843 @emph{dimensionality}) for that subtype, denoted by @code{DV(S)}, and thus for each
22844 object of that subtype. Intuitively, the value specified for each
22845 @code{Unit_Name} is the exponent associated with that unit; a zero value
22846 means that the unit is not used. For example:
22847
22848 @quotation
22849
22850 @example
22851 declare
22852 Acc : Acceleration;
22853 ...
22854 begin
22855 ...
22856 end;
22857 @end example
22858 @end quotation
22859
22860 Here @code{DV(Acc)} = @code{DV(Acceleration)} =
22861 @code{(Meter=>1, Kilogram=>0, Second=>-2, Ampere=>0, Kelvin=>0, Mole=>0, Candela=>0)}.
22862 Symbolically, we can express this as @code{Meter / Second**2}.
22863
22864 The dimension vector of an arithmetic expression is synthesized from the
22865 dimension vectors of its components, with compile-time dimensionality checks
22866 that help prevent mismatches such as using an @code{Acceleration} where a
22867 @code{Length} is required.
22868
22869 The dimension vector of the result of an arithmetic expression @emph{expr}, or
22870 @code{DV(@emph{expr})}, is defined as follows, assuming conventional
22871 mathematical definitions for the vector operations that are used:
22872
22873
22874 @itemize *
22875
22876 @item
22877 If @emph{expr} is of the type @emph{universal_real}, or is not of a dimensioned subtype,
22878 then @emph{expr} is dimensionless; @code{DV(@emph{expr})} is the empty vector.
22879
22880 @item
22881 @code{DV(@emph{op expr})}, where @emph{op} is a unary operator, is @code{DV(@emph{expr})}
22882
22883 @item
22884 @code{DV(@emph{expr1 op expr2})} where @emph{op} is "+" or "-" is @code{DV(@emph{expr1})}
22885 provided that @code{DV(@emph{expr1})} = @code{DV(@emph{expr2})}.
22886 If this condition is not met then the construct is illegal.
22887
22888 @item
22889 @code{DV(@emph{expr1} * @emph{expr2})} is @code{DV(@emph{expr1})} + @code{DV(@emph{expr2})},
22890 and @code{DV(@emph{expr1} / @emph{expr2})} = @code{DV(@emph{expr1})} - @code{DV(@emph{expr2})}.
22891 In this context if one of the @emph{expr}s is dimensionless then its empty
22892 dimension vector is treated as @code{(others => 0)}.
22893
22894 @item
22895 @code{DV(@emph{expr} ** @emph{power})} is @emph{power} * @code{DV(@emph{expr})},
22896 provided that @emph{power} is a static rational value. If this condition is not
22897 met then the construct is illegal.
22898 @end itemize
22899
22900 Note that, by the above rules, it is illegal to use binary "+" or "-" to
22901 combine a dimensioned and dimensionless value. Thus an expression such as
22902 @code{acc-10.0} is illegal, where @code{acc} is an object of subtype
22903 @code{Acceleration}.
22904
22905 The dimensionality checks for relationals use the same rules as
22906 for "+" and "-", except when comparing to a literal; thus
22907
22908 @quotation
22909
22910 @example
22911 acc > len
22912 @end example
22913 @end quotation
22914
22915 is equivalent to
22916
22917 @quotation
22918
22919 @example
22920 acc-len > 0.0
22921 @end example
22922 @end quotation
22923
22924 and is thus illegal, but
22925
22926 @quotation
22927
22928 @example
22929 acc > 10.0
22930 @end example
22931 @end quotation
22932
22933 is accepted with a warning. Analogously a conditional expression requires the
22934 same dimension vector for each branch (with no exception for literals).
22935
22936 The dimension vector of a type conversion @code{T(@emph{expr})} is defined
22937 as follows, based on the nature of @code{T}:
22938
22939
22940 @itemize *
22941
22942 @item
22943 If @code{T} is a dimensioned subtype then @code{DV(T(@emph{expr}))} is @code{DV(T)}
22944 provided that either @emph{expr} is dimensionless or
22945 @code{DV(T)} = @code{DV(@emph{expr})}. The conversion is illegal
22946 if @emph{expr} is dimensioned and @code{DV(@emph{expr})} /= @code{DV(T)}.
22947 Note that vector equality does not require that the corresponding
22948 Unit_Names be the same.
22949
22950 As a consequence of the above rule, it is possible to convert between
22951 different dimension systems that follow the same international system
22952 of units, with the seven physical components given in the standard order
22953 (length, mass, time, etc.). Thus a length in meters can be converted to
22954 a length in inches (with a suitable conversion factor) but cannot be
22955 converted, for example, to a mass in pounds.
22956
22957 @item
22958 If @code{T} is the base type for @emph{expr} (and the dimensionless root type of
22959 the dimension system), then @code{DV(T(@emph{expr}))} is @code{DV(expr)}.
22960 Thus, if @emph{expr} is of a dimensioned subtype of @code{T}, the conversion may
22961 be regarded as a "view conversion" that preserves dimensionality.
22962
22963 This rule makes it possible to write generic code that can be instantiated
22964 with compatible dimensioned subtypes. The generic unit will contain
22965 conversions that will consequently be present in instantiations, but
22966 conversions to the base type will preserve dimensionality and make it
22967 possible to write generic code that is correct with respect to
22968 dimensionality.
22969
22970 @item
22971 Otherwise (i.e., @code{T} is neither a dimensioned subtype nor a dimensionable
22972 base type), @code{DV(T(@emph{expr}))} is the empty vector. Thus a dimensioned
22973 value can be explicitly converted to a non-dimensioned subtype, which
22974 of course then escapes dimensionality analysis.
22975 @end itemize
22976
22977 The dimension vector for a type qualification @code{T'(@emph{expr})} is the same
22978 as for the type conversion @code{T(@emph{expr})}.
22979
22980 An assignment statement
22981
22982 @quotation
22983
22984 @example
22985 Source := Target;
22986 @end example
22987 @end quotation
22988
22989 requires @code{DV(Source)} = @code{DV(Target)}, and analogously for parameter
22990 passing (the dimension vector for the actual parameter must be equal to the
22991 dimension vector for the formal parameter).
22992
22993 @node Stack Related Facilities,Memory Management Issues,Performing Dimensionality Analysis in GNAT,GNAT and Program Execution
22994 @anchor{gnat_ugn/gnat_and_program_execution id57}@anchor{16b}@anchor{gnat_ugn/gnat_and_program_execution stack-related-facilities}@anchor{29}
22995 @section Stack Related Facilities
22996
22997
22998 This section describes some useful tools associated with stack
22999 checking and analysis. In
23000 particular, it deals with dynamic and static stack usage measurements.
23001
23002 @menu
23003 * Stack Overflow Checking::
23004 * Static Stack Usage Analysis::
23005 * Dynamic Stack Usage Analysis::
23006
23007 @end menu
23008
23009 @node Stack Overflow Checking,Static Stack Usage Analysis,,Stack Related Facilities
23010 @anchor{gnat_ugn/gnat_and_program_execution id58}@anchor{1c4}@anchor{gnat_ugn/gnat_and_program_execution stack-overflow-checking}@anchor{f4}
23011 @subsection Stack Overflow Checking
23012
23013
23014 @geindex Stack Overflow Checking
23015
23016 @geindex -fstack-check (gcc)
23017
23018 For most operating systems, @code{gcc} does not perform stack overflow
23019 checking by default. This means that if the main environment task or
23020 some other task exceeds the available stack space, then unpredictable
23021 behavior will occur. Most native systems offer some level of protection by
23022 adding a guard page at the end of each task stack. This mechanism is usually
23023 not enough for dealing properly with stack overflow situations because
23024 a large local variable could "jump" above the guard page.
23025 Furthermore, when the
23026 guard page is hit, there may not be any space left on the stack for executing
23027 the exception propagation code. Enabling stack checking avoids
23028 such situations.
23029
23030 To activate stack checking, compile all units with the @code{gcc} option
23031 @code{-fstack-check}. For example:
23032
23033 @quotation
23034
23035 @example
23036 $ gcc -c -fstack-check package1.adb
23037 @end example
23038 @end quotation
23039
23040 Units compiled with this option will generate extra instructions to check
23041 that any use of the stack (for procedure calls or for declaring local
23042 variables in declare blocks) does not exceed the available stack space.
23043 If the space is exceeded, then a @code{Storage_Error} exception is raised.
23044
23045 For declared tasks, the default stack size is defined by the GNAT runtime,
23046 whose size may be modified at bind time through the @code{-d} bind switch
23047 (@ref{11f,,Switches for gnatbind}). Task specific stack sizes may be set using the
23048 @code{Storage_Size} pragma.
23049
23050 For the environment task, the stack size is determined by the operating system.
23051 Consequently, to modify the size of the environment task please refer to your
23052 operating system documentation.
23053
23054 @node Static Stack Usage Analysis,Dynamic Stack Usage Analysis,Stack Overflow Checking,Stack Related Facilities
23055 @anchor{gnat_ugn/gnat_and_program_execution static-stack-usage-analysis}@anchor{f5}@anchor{gnat_ugn/gnat_and_program_execution id59}@anchor{1c5}
23056 @subsection Static Stack Usage Analysis
23057
23058
23059 @geindex Static Stack Usage Analysis
23060
23061 @geindex -fstack-usage
23062
23063 A unit compiled with @code{-fstack-usage} will generate an extra file
23064 that specifies
23065 the maximum amount of stack used, on a per-function basis.
23066 The file has the same
23067 basename as the target object file with a @code{.su} extension.
23068 Each line of this file is made up of three fields:
23069
23070
23071 @itemize *
23072
23073 @item
23074 The name of the function.
23075
23076 @item
23077 A number of bytes.
23078
23079 @item
23080 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
23081 @end itemize
23082
23083 The second field corresponds to the size of the known part of the function
23084 frame.
23085
23086 The qualifier @code{static} means that the function frame size
23087 is purely static.
23088 It usually means that all local variables have a static size.
23089 In this case, the second field is a reliable measure of the function stack
23090 utilization.
23091
23092 The qualifier @code{dynamic} means that the function frame size is not static.
23093 It happens mainly when some local variables have a dynamic size. When this
23094 qualifier appears alone, the second field is not a reliable measure
23095 of the function stack analysis. When it is qualified with @code{bounded}, it
23096 means that the second field is a reliable maximum of the function stack
23097 utilization.
23098
23099 A unit compiled with @code{-Wstack-usage} will issue a warning for each
23100 subprogram whose stack usage might be larger than the specified amount of
23101 bytes. The wording is in keeping with the qualifier documented above.
23102
23103 @node Dynamic Stack Usage Analysis,,Static Stack Usage Analysis,Stack Related Facilities
23104 @anchor{gnat_ugn/gnat_and_program_execution dynamic-stack-usage-analysis}@anchor{121}@anchor{gnat_ugn/gnat_and_program_execution id60}@anchor{1c6}
23105 @subsection Dynamic Stack Usage Analysis
23106
23107
23108 It is possible to measure the maximum amount of stack used by a task, by
23109 adding a switch to @code{gnatbind}, as:
23110
23111 @quotation
23112
23113 @example
23114 $ gnatbind -u0 file
23115 @end example
23116 @end quotation
23117
23118 With this option, at each task termination, its stack usage is output on
23119 @code{stderr}.
23120 It is not always convenient to output the stack usage when the program
23121 is still running. Hence, it is possible to delay this output until program
23122 termination. for a given number of tasks specified as the argument of the
23123 @code{-u} option. For instance:
23124
23125 @quotation
23126
23127 @example
23128 $ gnatbind -u100 file
23129 @end example
23130 @end quotation
23131
23132 will buffer the stack usage information of the first 100 tasks to terminate and
23133 output this info at program termination. Results are displayed in four
23134 columns:
23135
23136 @quotation
23137
23138 @example
23139 Index | Task Name | Stack Size | Stack Usage
23140 @end example
23141 @end quotation
23142
23143 where:
23144
23145
23146 @itemize *
23147
23148 @item
23149 @emph{Index} is a number associated with each task.
23150
23151 @item
23152 @emph{Task Name} is the name of the task analyzed.
23153
23154 @item
23155 @emph{Stack Size} is the maximum size for the stack.
23156
23157 @item
23158 @emph{Stack Usage} is the measure done by the stack analyzer.
23159 In order to prevent overflow, the stack
23160 is not entirely analyzed, and it's not possible to know exactly how
23161 much has actually been used.
23162 @end itemize
23163
23164 By default the environment task stack, the stack that contains the main unit,
23165 is not processed. To enable processing of the environment task stack, the
23166 environment variable GNAT_STACK_LIMIT needs to be set to the maximum size of
23167 the environment task stack. This amount is given in kilobytes. For example:
23168
23169 @quotation
23170
23171 @example
23172 $ set GNAT_STACK_LIMIT 1600
23173 @end example
23174 @end quotation
23175
23176 would specify to the analyzer that the environment task stack has a limit
23177 of 1.6 megabytes. Any stack usage beyond this will be ignored by the analysis.
23178
23179 The package @code{GNAT.Task_Stack_Usage} provides facilities to get
23180 stack-usage reports at run time. See its body for the details.
23181
23182 @node Memory Management Issues,,Stack Related Facilities,GNAT and Program Execution
23183 @anchor{gnat_ugn/gnat_and_program_execution id61}@anchor{16c}@anchor{gnat_ugn/gnat_and_program_execution memory-management-issues}@anchor{2a}
23184 @section Memory Management Issues
23185
23186
23187 This section describes some useful memory pools provided in the GNAT library
23188 and in particular the GNAT Debug Pool facility, which can be used to detect
23189 incorrect uses of access values (including 'dangling references').
23190
23191
23192 @menu
23193 * Some Useful Memory Pools::
23194 * The GNAT Debug Pool Facility::
23195
23196 @end menu
23197
23198 @node Some Useful Memory Pools,The GNAT Debug Pool Facility,,Memory Management Issues
23199 @anchor{gnat_ugn/gnat_and_program_execution id62}@anchor{1c7}@anchor{gnat_ugn/gnat_and_program_execution some-useful-memory-pools}@anchor{1c8}
23200 @subsection Some Useful Memory Pools
23201
23202
23203 @geindex Memory Pool
23204
23205 @geindex storage
23206 @geindex pool
23207
23208 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
23209 storage pool. Allocations use the standard system call @code{malloc} while
23210 deallocations use the standard system call @code{free}. No reclamation is
23211 performed when the pool goes out of scope. For performance reasons, the
23212 standard default Ada allocators/deallocators do not use any explicit storage
23213 pools but if they did, they could use this storage pool without any change in
23214 behavior. That is why this storage pool is used when the user
23215 manages to make the default implicit allocator explicit as in this example:
23216
23217 @quotation
23218
23219 @example
23220 type T1 is access Something;
23221 -- no Storage pool is defined for T2
23222
23223 type T2 is access Something_Else;
23224 for T2'Storage_Pool use T1'Storage_Pool;
23225 -- the above is equivalent to
23226 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
23227 @end example
23228 @end quotation
23229
23230 The @code{System.Pool_Local} package offers the @code{Unbounded_Reclaim_Pool} storage
23231 pool. The allocation strategy is similar to @code{Pool_Local}
23232 except that the all
23233 storage allocated with this pool is reclaimed when the pool object goes out of
23234 scope. This pool provides a explicit mechanism similar to the implicit one
23235 provided by several Ada 83 compilers for allocations performed through a local
23236 access type and whose purpose was to reclaim memory when exiting the
23237 scope of a given local access. As an example, the following program does not
23238 leak memory even though it does not perform explicit deallocation:
23239
23240 @quotation
23241
23242 @example
23243 with System.Pool_Local;
23244 procedure Pooloc1 is
23245 procedure Internal is
23246 type A is access Integer;
23247 X : System.Pool_Local.Unbounded_Reclaim_Pool;
23248 for A'Storage_Pool use X;
23249 v : A;
23250 begin
23251 for I in 1 .. 50 loop
23252 v := new Integer;
23253 end loop;
23254 end Internal;
23255 begin
23256 for I in 1 .. 100 loop
23257 Internal;
23258 end loop;
23259 end Pooloc1;
23260 @end example
23261 @end quotation
23262
23263 The @code{System.Pool_Size} package implements the @code{Stack_Bounded_Pool} used when
23264 @code{Storage_Size} is specified for an access type.
23265 The whole storage for the pool is
23266 allocated at once, usually on the stack at the point where the access type is
23267 elaborated. It is automatically reclaimed when exiting the scope where the
23268 access type is defined. This package is not intended to be used directly by the
23269 user and it is implicitly used for each such declaration:
23270
23271 @quotation
23272
23273 @example
23274 type T1 is access Something;
23275 for T1'Storage_Size use 10_000;
23276 @end example
23277 @end quotation
23278
23279 @node The GNAT Debug Pool Facility,,Some Useful Memory Pools,Memory Management Issues
23280 @anchor{gnat_ugn/gnat_and_program_execution id63}@anchor{1c9}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debug-pool-facility}@anchor{1ca}
23281 @subsection The GNAT Debug Pool Facility
23282
23283
23284 @geindex Debug Pool
23285
23286 @geindex storage
23287 @geindex pool
23288 @geindex memory corruption
23289
23290 The use of unchecked deallocation and unchecked conversion can easily
23291 lead to incorrect memory references. The problems generated by such
23292 references are usually difficult to tackle because the symptoms can be
23293 very remote from the origin of the problem. In such cases, it is
23294 very helpful to detect the problem as early as possible. This is the
23295 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
23296
23297 In order to use the GNAT specific debugging pool, the user must
23298 associate a debug pool object with each of the access types that may be
23299 related to suspected memory problems. See Ada Reference Manual 13.11.
23300
23301 @quotation
23302
23303 @example
23304 type Ptr is access Some_Type;
23305 Pool : GNAT.Debug_Pools.Debug_Pool;
23306 for Ptr'Storage_Pool use Pool;
23307 @end example
23308 @end quotation
23309
23310 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
23311 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
23312 allow the user to redefine allocation and deallocation strategies. They
23313 also provide a checkpoint for each dereference, through the use of
23314 the primitive operation @code{Dereference} which is implicitly called at
23315 each dereference of an access value.
23316
23317 Once an access type has been associated with a debug pool, operations on
23318 values of the type may raise four distinct exceptions,
23319 which correspond to four potential kinds of memory corruption:
23320
23321
23322 @itemize *
23323
23324 @item
23325 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
23326
23327 @item
23328 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
23329
23330 @item
23331 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
23332
23333 @item
23334 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage}
23335 @end itemize
23336
23337 For types associated with a Debug_Pool, dynamic allocation is performed using
23338 the standard GNAT allocation routine. References to all allocated chunks of
23339 memory are kept in an internal dictionary. Several deallocation strategies are
23340 provided, whereupon the user can choose to release the memory to the system,
23341 keep it allocated for further invalid access checks, or fill it with an easily
23342 recognizable pattern for debug sessions. The memory pattern is the old IBM
23343 hexadecimal convention: @code{16#DEADBEEF#}.
23344
23345 See the documentation in the file g-debpoo.ads for more information on the
23346 various strategies.
23347
23348 Upon each dereference, a check is made that the access value denotes a
23349 properly allocated memory location. Here is a complete example of use of
23350 @code{Debug_Pools}, that includes typical instances of memory corruption:
23351
23352 @quotation
23353
23354 @example
23355 with Gnat.Io; use Gnat.Io;
23356 with Unchecked_Deallocation;
23357 with Unchecked_Conversion;
23358 with GNAT.Debug_Pools;
23359 with System.Storage_Elements;
23360 with Ada.Exceptions; use Ada.Exceptions;
23361 procedure Debug_Pool_Test is
23362
23363 type T is access Integer;
23364 type U is access all T;
23365
23366 P : GNAT.Debug_Pools.Debug_Pool;
23367 for T'Storage_Pool use P;
23368
23369 procedure Free is new Unchecked_Deallocation (Integer, T);
23370 function UC is new Unchecked_Conversion (U, T);
23371 A, B : aliased T;
23372
23373 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
23374
23375 begin
23376 Info (P);
23377 A := new Integer;
23378 B := new Integer;
23379 B := A;
23380 Info (P);
23381 Free (A);
23382 begin
23383 Put_Line (Integer'Image(B.all));
23384 exception
23385 when E : others => Put_Line ("raised: " & Exception_Name (E));
23386 end;
23387 begin
23388 Free (B);
23389 exception
23390 when E : others => Put_Line ("raised: " & Exception_Name (E));
23391 end;
23392 B := UC(A'Access);
23393 begin
23394 Put_Line (Integer'Image(B.all));
23395 exception
23396 when E : others => Put_Line ("raised: " & Exception_Name (E));
23397 end;
23398 begin
23399 Free (B);
23400 exception
23401 when E : others => Put_Line ("raised: " & Exception_Name (E));
23402 end;
23403 Info (P);
23404 end Debug_Pool_Test;
23405 @end example
23406 @end quotation
23407
23408 The debug pool mechanism provides the following precise diagnostics on the
23409 execution of this erroneous program:
23410
23411 @quotation
23412
23413 @example
23414 Debug Pool info:
23415 Total allocated bytes : 0
23416 Total deallocated bytes : 0
23417 Current Water Mark: 0
23418 High Water Mark: 0
23419
23420 Debug Pool info:
23421 Total allocated bytes : 8
23422 Total deallocated bytes : 0
23423 Current Water Mark: 8
23424 High Water Mark: 8
23425
23426 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
23427 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
23428 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
23429 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
23430 Debug Pool info:
23431 Total allocated bytes : 8
23432 Total deallocated bytes : 4
23433 Current Water Mark: 4
23434 High Water Mark: 8
23435 @end example
23436 @end quotation
23437
23438
23439 @c -- Non-breaking space in running text
23440 @c -- E.g. Ada |nbsp| 95
23441
23442 @node Platform-Specific Information,Example of Binder Output File,GNAT and Program Execution,Top
23443 @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}
23444 @chapter Platform-Specific Information
23445
23446
23447 This appendix contains information relating to the implementation
23448 of run-time libraries on various platforms and also covers
23449 topics related to the GNAT implementation on Windows and Mac OS.
23450
23451 @menu
23452 * Run-Time Libraries::
23453 * Specifying a Run-Time Library::
23454 * GNU/Linux Topics::
23455 * Microsoft Windows Topics::
23456 * Mac OS Topics::
23457
23458 @end menu
23459
23460 @node Run-Time Libraries,Specifying a Run-Time Library,,Platform-Specific Information
23461 @anchor{gnat_ugn/platform_specific_information id2}@anchor{1cd}@anchor{gnat_ugn/platform_specific_information run-time-libraries}@anchor{2b}
23462 @section Run-Time Libraries
23463
23464
23465 @geindex Tasking and threads libraries
23466
23467 @geindex Threads libraries and tasking
23468
23469 @geindex Run-time libraries (platform-specific information)
23470
23471 The GNAT run-time implementation may vary with respect to both the
23472 underlying threads library and the exception-handling scheme.
23473 For threads support, the default run-time will bind to the thread
23474 package of the underlying operating system.
23475
23476 For exception handling, either or both of two models are supplied:
23477
23478 @quotation
23479
23480 @geindex Zero-Cost Exceptions
23481
23482 @geindex ZCX (Zero-Cost Exceptions)
23483 @end quotation
23484
23485
23486 @itemize *
23487
23488 @item
23489 @strong{Zero-Cost Exceptions} ("ZCX"),
23490 which uses binder-generated tables that
23491 are interrogated at run time to locate a handler.
23492
23493 @geindex setjmp/longjmp Exception Model
23494
23495 @geindex SJLJ (setjmp/longjmp Exception Model)
23496
23497 @item
23498 @strong{setjmp / longjmp} ('SJLJ'),
23499 which uses dynamically-set data to establish
23500 the set of handlers
23501 @end itemize
23502
23503 Most programs should experience a substantial speed improvement by
23504 being compiled with a ZCX run-time.
23505 This is especially true for
23506 tasking applications or applications with many exception handlers.@}
23507
23508 This section summarizes which combinations of threads and exception support
23509 are supplied on various GNAT platforms.
23510 It then shows how to select a particular library either
23511 permanently or temporarily,
23512 explains the properties of (and tradeoffs among) the various threads
23513 libraries, and provides some additional
23514 information about several specific platforms.
23515
23516 @menu
23517 * Summary of Run-Time Configurations::
23518
23519 @end menu
23520
23521 @node Summary of Run-Time Configurations,,,Run-Time Libraries
23522 @anchor{gnat_ugn/platform_specific_information summary-of-run-time-configurations}@anchor{1ce}@anchor{gnat_ugn/platform_specific_information id3}@anchor{1cf}
23523 @subsection Summary of Run-Time Configurations
23524
23525
23526
23527 @multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxx}
23528 @headitem
23529
23530 Platform
23531
23532 @tab
23533
23534 Run-Time
23535
23536 @tab
23537
23538 Tasking
23539
23540 @tab
23541
23542 Exceptions
23543
23544 @item
23545
23546 GNU/Linux
23547
23548 @tab
23549
23550 rts-native
23551 (default)
23552
23553 @tab
23554
23555 pthread library
23556
23557 @tab
23558
23559 ZCX
23560
23561 @item
23562
23563 rts-sjlj
23564
23565 @tab
23566
23567 pthread library
23568
23569 @tab
23570
23571 SJLJ
23572
23573 @item
23574
23575 Windows
23576
23577 @tab
23578
23579 rts-native
23580 (default)
23581
23582 @tab
23583
23584 native Win32 threads
23585
23586 @tab
23587
23588 ZCX
23589
23590 @item
23591
23592 rts-sjlj
23593
23594 @tab
23595
23596 native Win32 threads
23597
23598 @tab
23599
23600 SJLJ
23601
23602 @item
23603
23604 Mac OS
23605
23606 @tab
23607
23608 rts-native
23609
23610 @tab
23611
23612 pthread library
23613
23614 @tab
23615
23616 ZCX
23617
23618 @end multitable
23619
23620
23621 @node Specifying a Run-Time Library,GNU/Linux Topics,Run-Time Libraries,Platform-Specific Information
23622 @anchor{gnat_ugn/platform_specific_information specifying-a-run-time-library}@anchor{1d0}@anchor{gnat_ugn/platform_specific_information id4}@anchor{1d1}
23623 @section Specifying a Run-Time Library
23624
23625
23626 The @code{adainclude} subdirectory containing the sources of the GNAT
23627 run-time library, and the @code{adalib} subdirectory containing the
23628 @code{ALI} files and the static and/or shared GNAT library, are located
23629 in the gcc target-dependent area:
23630
23631 @quotation
23632
23633 @example
23634 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
23635 @end example
23636 @end quotation
23637
23638 As indicated above, on some platforms several run-time libraries are supplied.
23639 These libraries are installed in the target dependent area and
23640 contain a complete source and binary subdirectory. The detailed description
23641 below explains the differences between the different libraries in terms of
23642 their thread support.
23643
23644 The default run-time library (when GNAT is installed) is @emph{rts-native}.
23645 This default run-time is selected by the means of soft links.
23646 For example on x86-linux:
23647
23648 @c --
23649 @c -- $(target-dir)
23650 @c -- |
23651 @c -- +--- adainclude----------+
23652 @c -- | |
23653 @c -- +--- adalib-----------+ |
23654 @c -- | | |
23655 @c -- +--- rts-native | |
23656 @c -- | | | |
23657 @c -- | +--- adainclude <---+
23658 @c -- | | |
23659 @c -- | +--- adalib <----+
23660 @c -- |
23661 @c -- +--- rts-sjlj
23662 @c -- |
23663 @c -- +--- adainclude
23664 @c -- |
23665 @c -- +--- adalib
23666
23667
23668 @example
23669 $(target-dir)
23670 __/ / \ \___
23671 _______/ / \ \_________________
23672 / / \ \
23673 / / \ \
23674 ADAINCLUDE ADALIB rts-native rts-sjlj
23675 : : / \ / \
23676 : : / \ / \
23677 : : / \ / \
23678 : : / \ / \
23679 +-------------> adainclude adalib adainclude adalib
23680 : ^
23681 : :
23682 +---------------------+
23683
23684 Run-Time Library Directory Structure
23685 (Upper-case names and dotted/dashed arrows represent soft links)
23686 @end example
23687
23688 If the @emph{rts-sjlj} library is to be selected on a permanent basis,
23689 these soft links can be modified with the following commands:
23690
23691 @quotation
23692
23693 @example
23694 $ cd $target
23695 $ rm -f adainclude adalib
23696 $ ln -s rts-sjlj/adainclude adainclude
23697 $ ln -s rts-sjlj/adalib adalib
23698 @end example
23699 @end quotation
23700
23701 Alternatively, you can specify @code{rts-sjlj/adainclude} in the file
23702 @code{$target/ada_source_path} and @code{rts-sjlj/adalib} in
23703 @code{$target/ada_object_path}.
23704
23705 @geindex --RTS option
23706
23707 Selecting another run-time library temporarily can be
23708 achieved by using the @code{--RTS} switch, e.g., @code{--RTS=sjlj}
23709 @anchor{gnat_ugn/platform_specific_information choosing-the-scheduling-policy}@anchor{1d2}
23710 @geindex SCHED_FIFO scheduling policy
23711
23712 @geindex SCHED_RR scheduling policy
23713
23714 @geindex SCHED_OTHER scheduling policy
23715
23716 @menu
23717 * Choosing the Scheduling Policy::
23718
23719 @end menu
23720
23721 @node Choosing the Scheduling Policy,,,Specifying a Run-Time Library
23722 @anchor{gnat_ugn/platform_specific_information id5}@anchor{1d3}
23723 @subsection Choosing the Scheduling Policy
23724
23725
23726 When using a POSIX threads implementation, you have a choice of several
23727 scheduling policies: @code{SCHED_FIFO}, @code{SCHED_RR} and @code{SCHED_OTHER}.
23728
23729 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
23730 or @code{SCHED_RR} requires special (e.g., root) privileges.
23731
23732 @geindex pragma Time_Slice
23733
23734 @geindex -T0 option
23735
23736 @geindex pragma Task_Dispatching_Policy
23737
23738 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
23739 @code{SCHED_FIFO},
23740 you can use one of the following:
23741
23742
23743 @itemize *
23744
23745 @item
23746 @code{pragma Time_Slice (0.0)}
23747
23748 @item
23749 the corresponding binder option @code{-T0}
23750
23751 @item
23752 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
23753 @end itemize
23754
23755 To specify @code{SCHED_RR},
23756 you should use @code{pragma Time_Slice} with a
23757 value greater than 0.0, or else use the corresponding @code{-T}
23758 binder option.
23759
23760 To make sure a program is running as root, you can put something like
23761 this in a library package body in your application:
23762
23763 @quotation
23764
23765 @example
23766 function geteuid return Integer;
23767 pragma Import (C, geteuid, "geteuid");
23768 Ignore : constant Boolean :=
23769 (if geteuid = 0 then True else raise Program_Error with "must be root");
23770 @end example
23771 @end quotation
23772
23773 It gets the effective user id, and if it's not 0 (i.e. root), it raises
23774 Program_Error.
23775
23776 @geindex Linux
23777
23778 @geindex GNU/Linux
23779
23780 @node GNU/Linux Topics,Microsoft Windows Topics,Specifying a Run-Time Library,Platform-Specific Information
23781 @anchor{gnat_ugn/platform_specific_information id6}@anchor{1d4}@anchor{gnat_ugn/platform_specific_information gnu-linux-topics}@anchor{1d5}
23782 @section GNU/Linux Topics
23783
23784
23785 This section describes topics that are specific to GNU/Linux platforms.
23786
23787 @menu
23788 * Required Packages on GNU/Linux::
23789
23790 @end menu
23791
23792 @node Required Packages on GNU/Linux,,,GNU/Linux Topics
23793 @anchor{gnat_ugn/platform_specific_information id7}@anchor{1d6}@anchor{gnat_ugn/platform_specific_information required-packages-on-gnu-linux}@anchor{1d7}
23794 @subsection Required Packages on GNU/Linux
23795
23796
23797 GNAT requires the C library developer's package to be installed.
23798 The name of of that package depends on your GNU/Linux distribution:
23799
23800
23801 @itemize *
23802
23803 @item
23804 RedHat, SUSE: @code{glibc-devel};
23805
23806 @item
23807 Debian, Ubuntu: @code{libc6-dev} (normally installed by default).
23808 @end itemize
23809
23810 If using the 32-bit version of GNAT on a 64-bit version of GNU/Linux,
23811 you'll need the 32-bit version of the following packages:
23812
23813
23814 @itemize *
23815
23816 @item
23817 RedHat, SUSE: @code{glibc.i686}, @code{glibc-devel.i686}, @code{ncurses-libs.i686}
23818
23819 @item
23820 Debian, Ubuntu: @code{libc6:i386}, @code{libc6-dev:i386}, @code{lib32ncursesw5}
23821 @end itemize
23822
23823 Other GNU/Linux distributions might be choosing a different name
23824 for those packages.
23825
23826 @geindex Windows
23827
23828 @node Microsoft Windows Topics,Mac OS Topics,GNU/Linux Topics,Platform-Specific Information
23829 @anchor{gnat_ugn/platform_specific_information microsoft-windows-topics}@anchor{2c}@anchor{gnat_ugn/platform_specific_information id8}@anchor{1d8}
23830 @section Microsoft Windows Topics
23831
23832
23833 This section describes topics that are specific to the Microsoft Windows
23834 platforms.
23835
23836
23837
23838
23839
23840 @menu
23841 * Using GNAT on Windows::
23842 * Using a network installation of GNAT::
23843 * CONSOLE and WINDOWS subsystems::
23844 * Temporary Files::
23845 * Disabling Command Line Argument Expansion::
23846 * Mixed-Language Programming on Windows::
23847 * Windows Specific Add-Ons::
23848
23849 @end menu
23850
23851 @node Using GNAT on Windows,Using a network installation of GNAT,,Microsoft Windows Topics
23852 @anchor{gnat_ugn/platform_specific_information using-gnat-on-windows}@anchor{1d9}@anchor{gnat_ugn/platform_specific_information id9}@anchor{1da}
23853 @subsection Using GNAT on Windows
23854
23855
23856 One of the strengths of the GNAT technology is that its tool set
23857 (@code{gcc}, @code{gnatbind}, @code{gnatlink}, @code{gnatmake}, the
23858 @code{gdb} debugger, etc.) is used in the same way regardless of the
23859 platform.
23860
23861 On Windows this tool set is complemented by a number of Microsoft-specific
23862 tools that have been provided to facilitate interoperability with Windows
23863 when this is required. With these tools:
23864
23865
23866 @itemize *
23867
23868 @item
23869 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
23870 subsystems.
23871
23872 @item
23873 You can use any Dynamically Linked Library (DLL) in your Ada code (both
23874 relocatable and non-relocatable DLLs are supported).
23875
23876 @item
23877 You can build Ada DLLs for use in other applications. These applications
23878 can be written in a language other than Ada (e.g., C, C++, etc). Again both
23879 relocatable and non-relocatable Ada DLLs are supported.
23880
23881 @item
23882 You can include Windows resources in your Ada application.
23883
23884 @item
23885 You can use or create COM/DCOM objects.
23886 @end itemize
23887
23888 Immediately below are listed all known general GNAT-for-Windows restrictions.
23889 Other restrictions about specific features like Windows Resources and DLLs
23890 are listed in separate sections below.
23891
23892
23893 @itemize *
23894
23895 @item
23896 It is not possible to use @code{GetLastError} and @code{SetLastError}
23897 when tasking, protected records, or exceptions are used. In these
23898 cases, in order to implement Ada semantics, the GNAT run-time system
23899 calls certain Win32 routines that set the last error variable to 0 upon
23900 success. It should be possible to use @code{GetLastError} and
23901 @code{SetLastError} when tasking, protected record, and exception
23902 features are not used, but it is not guaranteed to work.
23903
23904 @item
23905 It is not possible to link against Microsoft C++ libraries except for
23906 import libraries. Interfacing must be done by the mean of DLLs.
23907
23908 @item
23909 It is possible to link against Microsoft C libraries. Yet the preferred
23910 solution is to use C/C++ compiler that comes with GNAT, since it
23911 doesn't require having two different development environments and makes the
23912 inter-language debugging experience smoother.
23913
23914 @item
23915 When the compilation environment is located on FAT32 drives, users may
23916 experience recompilations of the source files that have not changed if
23917 Daylight Saving Time (DST) state has changed since the last time files
23918 were compiled. NTFS drives do not have this problem.
23919
23920 @item
23921 No components of the GNAT toolset use any entries in the Windows
23922 registry. The only entries that can be created are file associations and
23923 PATH settings, provided the user has chosen to create them at installation
23924 time, as well as some minimal book-keeping information needed to correctly
23925 uninstall or integrate different GNAT products.
23926 @end itemize
23927
23928 @node Using a network installation of GNAT,CONSOLE and WINDOWS subsystems,Using GNAT on Windows,Microsoft Windows Topics
23929 @anchor{gnat_ugn/platform_specific_information id10}@anchor{1db}@anchor{gnat_ugn/platform_specific_information using-a-network-installation-of-gnat}@anchor{1dc}
23930 @subsection Using a network installation of GNAT
23931
23932
23933 Make sure the system on which GNAT is installed is accessible from the
23934 current machine, i.e., the install location is shared over the network.
23935 Shared resources are accessed on Windows by means of UNC paths, which
23936 have the format @code{\\\\server\\sharename\\path}
23937
23938 In order to use such a network installation, simply add the UNC path of the
23939 @code{bin} directory of your GNAT installation in front of your PATH. For
23940 example, if GNAT is installed in @code{\GNAT} directory of a share location
23941 called @code{c-drive} on a machine @code{LOKI}, the following command will
23942 make it available:
23943
23944 @quotation
23945
23946 @example
23947 $ path \\loki\c-drive\gnat\bin;%path%`
23948 @end example
23949 @end quotation
23950
23951 Be aware that every compilation using the network installation results in the
23952 transfer of large amounts of data across the network and will likely cause
23953 serious performance penalty.
23954
23955 @node CONSOLE and WINDOWS subsystems,Temporary Files,Using a network installation of GNAT,Microsoft Windows Topics
23956 @anchor{gnat_ugn/platform_specific_information id11}@anchor{1dd}@anchor{gnat_ugn/platform_specific_information console-and-windows-subsystems}@anchor{1de}
23957 @subsection CONSOLE and WINDOWS subsystems
23958
23959
23960 @geindex CONSOLE Subsystem
23961
23962 @geindex WINDOWS Subsystem
23963
23964 @geindex -mwindows
23965
23966 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
23967 (which is the default subsystem) will always create a console when
23968 launching the application. This is not something desirable when the
23969 application has a Windows GUI. To get rid of this console the
23970 application must be using the @code{WINDOWS} subsystem. To do so
23971 the @code{-mwindows} linker option must be specified.
23972
23973 @quotation
23974
23975 @example
23976 $ gnatmake winprog -largs -mwindows
23977 @end example
23978 @end quotation
23979
23980 @node Temporary Files,Disabling Command Line Argument Expansion,CONSOLE and WINDOWS subsystems,Microsoft Windows Topics
23981 @anchor{gnat_ugn/platform_specific_information id12}@anchor{1df}@anchor{gnat_ugn/platform_specific_information temporary-files}@anchor{1e0}
23982 @subsection Temporary Files
23983
23984
23985 @geindex Temporary files
23986
23987 It is possible to control where temporary files gets created by setting
23988 the
23989 @geindex TMP
23990 @geindex environment variable; TMP
23991 @code{TMP} environment variable. The file will be created:
23992
23993
23994 @itemize *
23995
23996 @item
23997 Under the directory pointed to by the
23998 @geindex TMP
23999 @geindex environment variable; TMP
24000 @code{TMP} environment variable if
24001 this directory exists.
24002
24003 @item
24004 Under @code{c:\temp}, if the
24005 @geindex TMP
24006 @geindex environment variable; TMP
24007 @code{TMP} environment variable is not
24008 set (or not pointing to a directory) and if this directory exists.
24009
24010 @item
24011 Under the current working directory otherwise.
24012 @end itemize
24013
24014 This allows you to determine exactly where the temporary
24015 file will be created. This is particularly useful in networked
24016 environments where you may not have write access to some
24017 directories.
24018
24019 @node Disabling Command Line Argument Expansion,Mixed-Language Programming on Windows,Temporary Files,Microsoft Windows Topics
24020 @anchor{gnat_ugn/platform_specific_information disabling-command-line-argument-expansion}@anchor{1e1}
24021 @subsection Disabling Command Line Argument Expansion
24022
24023
24024 @geindex Command Line Argument Expansion
24025
24026 By default, an executable compiled for the Windows platform will do
24027 the following postprocessing on the arguments passed on the command
24028 line:
24029
24030
24031 @itemize *
24032
24033 @item
24034 If the argument contains the characters @code{*} and/or @code{?}, then
24035 file expansion will be attempted. For example, if the current directory
24036 contains @code{a.txt} and @code{b.txt}, then when calling:
24037
24038 @example
24039 $ my_ada_program *.txt
24040 @end example
24041
24042 The following arguments will effectively be passed to the main program
24043 (for example when using @code{Ada.Command_Line.Argument}):
24044
24045 @example
24046 Ada.Command_Line.Argument (1) -> "a.txt"
24047 Ada.Command_Line.Argument (2) -> "b.txt"
24048 @end example
24049
24050 @item
24051 Filename expansion can be disabled for a given argument by using single
24052 quotes. Thus, calling:
24053
24054 @example
24055 $ my_ada_program '*.txt'
24056 @end example
24057
24058 will result in:
24059
24060 @example
24061 Ada.Command_Line.Argument (1) -> "*.txt"
24062 @end example
24063 @end itemize
24064
24065 Note that if the program is launched from a shell such as Cygwin Bash
24066 then quote removal might be performed by the shell.
24067
24068 In some contexts it might be useful to disable this feature (for example if
24069 the program performs its own argument expansion). In order to do this, a C
24070 symbol needs to be defined and set to @code{0}. You can do this by
24071 adding the following code fragment in one of your Ada units:
24072
24073 @example
24074 Do_Argv_Expansion : Integer := 0;
24075 pragma Export (C, Do_Argv_Expansion, "__gnat_do_argv_expansion");
24076 @end example
24077
24078 The results of previous examples will be respectively:
24079
24080 @example
24081 Ada.Command_Line.Argument (1) -> "*.txt"
24082 @end example
24083
24084 and:
24085
24086 @example
24087 Ada.Command_Line.Argument (1) -> "'*.txt'"
24088 @end example
24089
24090 @node Mixed-Language Programming on Windows,Windows Specific Add-Ons,Disabling Command Line Argument Expansion,Microsoft Windows Topics
24091 @anchor{gnat_ugn/platform_specific_information id13}@anchor{1e2}@anchor{gnat_ugn/platform_specific_information mixed-language-programming-on-windows}@anchor{1e3}
24092 @subsection Mixed-Language Programming on Windows
24093
24094
24095 Developing pure Ada applications on Windows is no different than on
24096 other GNAT-supported platforms. However, when developing or porting an
24097 application that contains a mix of Ada and C/C++, the choice of your
24098 Windows C/C++ development environment conditions your overall
24099 interoperability strategy.
24100
24101 If you use @code{gcc} or Microsoft C to compile the non-Ada part of
24102 your application, there are no Windows-specific restrictions that
24103 affect the overall interoperability with your Ada code. If you do want
24104 to use the Microsoft tools for your C++ code, you have two choices:
24105
24106
24107 @itemize *
24108
24109 @item
24110 Encapsulate your C++ code in a DLL to be linked with your Ada
24111 application. In this case, use the Microsoft or whatever environment to
24112 build the DLL and use GNAT to build your executable
24113 (@ref{1e4,,Using DLLs with GNAT}).
24114
24115 @item
24116 Or you can encapsulate your Ada code in a DLL to be linked with the
24117 other part of your application. In this case, use GNAT to build the DLL
24118 (@ref{1e5,,Building DLLs with GNAT Project files}) and use the Microsoft
24119 or whatever environment to build your executable.
24120 @end itemize
24121
24122 In addition to the description about C main in
24123 @ref{44,,Mixed Language Programming} section, if the C main uses a
24124 stand-alone library it is required on x86-windows to
24125 setup the SEH context. For this the C main must looks like this:
24126
24127 @quotation
24128
24129 @example
24130 /* main.c */
24131 extern void adainit (void);
24132 extern void adafinal (void);
24133 extern void __gnat_initialize(void*);
24134 extern void call_to_ada (void);
24135
24136 int main (int argc, char *argv[])
24137 @{
24138 int SEH [2];
24139
24140 /* Initialize the SEH context */
24141 __gnat_initialize (&SEH);
24142
24143 adainit();
24144
24145 /* Then call Ada services in the stand-alone library */
24146
24147 call_to_ada();
24148
24149 adafinal();
24150 @}
24151 @end example
24152 @end quotation
24153
24154 Note that this is not needed on x86_64-windows where the Windows
24155 native SEH support is used.
24156
24157 @menu
24158 * Windows Calling Conventions::
24159 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
24160 * Using DLLs with GNAT::
24161 * Building DLLs with GNAT Project files::
24162 * Building DLLs with GNAT::
24163 * Building DLLs with gnatdll::
24164 * Ada DLLs and Finalization::
24165 * Creating a Spec for Ada DLLs::
24166 * GNAT and Windows Resources::
24167 * Using GNAT DLLs from Microsoft Visual Studio Applications::
24168 * Debugging a DLL::
24169 * Setting Stack Size from gnatlink::
24170 * Setting Heap Size from gnatlink::
24171
24172 @end menu
24173
24174 @node Windows Calling Conventions,Introduction to Dynamic Link Libraries DLLs,,Mixed-Language Programming on Windows
24175 @anchor{gnat_ugn/platform_specific_information windows-calling-conventions}@anchor{1e6}@anchor{gnat_ugn/platform_specific_information id14}@anchor{1e7}
24176 @subsubsection Windows Calling Conventions
24177
24178
24179 @geindex Stdcall
24180
24181 @geindex APIENTRY
24182
24183 This section pertain only to Win32. On Win64 there is a single native
24184 calling convention. All convention specifiers are ignored on this
24185 platform.
24186
24187 When a subprogram @code{F} (caller) calls a subprogram @code{G}
24188 (callee), there are several ways to push @code{G}'s parameters on the
24189 stack and there are several possible scenarios to clean up the stack
24190 upon @code{G}'s return. A calling convention is an agreed upon software
24191 protocol whereby the responsibilities between the caller (@code{F}) and
24192 the callee (@code{G}) are clearly defined. Several calling conventions
24193 are available for Windows:
24194
24195
24196 @itemize *
24197
24198 @item
24199 @code{C} (Microsoft defined)
24200
24201 @item
24202 @code{Stdcall} (Microsoft defined)
24203
24204 @item
24205 @code{Win32} (GNAT specific)
24206
24207 @item
24208 @code{DLL} (GNAT specific)
24209 @end itemize
24210
24211 @menu
24212 * C Calling Convention::
24213 * Stdcall Calling Convention::
24214 * Win32 Calling Convention::
24215 * DLL Calling Convention::
24216
24217 @end menu
24218
24219 @node C Calling Convention,Stdcall Calling Convention,,Windows Calling Conventions
24220 @anchor{gnat_ugn/platform_specific_information c-calling-convention}@anchor{1e8}@anchor{gnat_ugn/platform_specific_information id15}@anchor{1e9}
24221 @subsubsection @code{C} Calling Convention
24222
24223
24224 This is the default calling convention used when interfacing to C/C++
24225 routines compiled with either @code{gcc} or Microsoft Visual C++.
24226
24227 In the @code{C} calling convention subprogram parameters are pushed on the
24228 stack by the caller from right to left. The caller itself is in charge of
24229 cleaning up the stack after the call. In addition, the name of a routine
24230 with @code{C} calling convention is mangled by adding a leading underscore.
24231
24232 The name to use on the Ada side when importing (or exporting) a routine
24233 with @code{C} calling convention is the name of the routine. For
24234 instance the C function:
24235
24236 @quotation
24237
24238 @example
24239 int get_val (long);
24240 @end example
24241 @end quotation
24242
24243 should be imported from Ada as follows:
24244
24245 @quotation
24246
24247 @example
24248 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24249 pragma Import (C, Get_Val, External_Name => "get_val");
24250 @end example
24251 @end quotation
24252
24253 Note that in this particular case the @code{External_Name} parameter could
24254 have been omitted since, when missing, this parameter is taken to be the
24255 name of the Ada entity in lower case. When the @code{Link_Name} parameter
24256 is missing, as in the above example, this parameter is set to be the
24257 @code{External_Name} with a leading underscore.
24258
24259 When importing a variable defined in C, you should always use the @code{C}
24260 calling convention unless the object containing the variable is part of a
24261 DLL (in which case you should use the @code{Stdcall} calling
24262 convention, @ref{1ea,,Stdcall Calling Convention}).
24263
24264 @node Stdcall Calling Convention,Win32 Calling Convention,C Calling Convention,Windows Calling Conventions
24265 @anchor{gnat_ugn/platform_specific_information stdcall-calling-convention}@anchor{1ea}@anchor{gnat_ugn/platform_specific_information id16}@anchor{1eb}
24266 @subsubsection @code{Stdcall} Calling Convention
24267
24268
24269 This convention, which was the calling convention used for Pascal
24270 programs, is used by Microsoft for all the routines in the Win32 API for
24271 efficiency reasons. It must be used to import any routine for which this
24272 convention was specified.
24273
24274 In the @code{Stdcall} calling convention subprogram parameters are pushed
24275 on the stack by the caller from right to left. The callee (and not the
24276 caller) is in charge of cleaning the stack on routine exit. In addition,
24277 the name of a routine with @code{Stdcall} calling convention is mangled by
24278 adding a leading underscore (as for the @code{C} calling convention) and a
24279 trailing @code{@@@emph{nn}}, where @code{nn} is the overall size (in
24280 bytes) of the parameters passed to the routine.
24281
24282 The name to use on the Ada side when importing a C routine with a
24283 @code{Stdcall} calling convention is the name of the C routine. The leading
24284 underscore and trailing @code{@@@emph{nn}} are added automatically by
24285 the compiler. For instance the Win32 function:
24286
24287 @quotation
24288
24289 @example
24290 APIENTRY int get_val (long);
24291 @end example
24292 @end quotation
24293
24294 should be imported from Ada as follows:
24295
24296 @quotation
24297
24298 @example
24299 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24300 pragma Import (Stdcall, Get_Val);
24301 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
24302 @end example
24303 @end quotation
24304
24305 As for the @code{C} calling convention, when the @code{External_Name}
24306 parameter is missing, it is taken to be the name of the Ada entity in lower
24307 case. If instead of writing the above import pragma you write:
24308
24309 @quotation
24310
24311 @example
24312 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24313 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
24314 @end example
24315 @end quotation
24316
24317 then the imported routine is @code{_retrieve_val@@4}. However, if instead
24318 of specifying the @code{External_Name} parameter you specify the
24319 @code{Link_Name} as in the following example:
24320
24321 @quotation
24322
24323 @example
24324 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24325 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
24326 @end example
24327 @end quotation
24328
24329 then the imported routine is @code{retrieve_val}, that is, there is no
24330 decoration at all. No leading underscore and no Stdcall suffix
24331 @code{@@@emph{nn}}.
24332
24333 This is especially important as in some special cases a DLL's entry
24334 point name lacks a trailing @code{@@@emph{nn}} while the exported
24335 name generated for a call has it.
24336
24337 It is also possible to import variables defined in a DLL by using an
24338 import pragma for a variable. As an example, if a DLL contains a
24339 variable defined as:
24340
24341 @quotation
24342
24343 @example
24344 int my_var;
24345 @end example
24346 @end quotation
24347
24348 then, to access this variable from Ada you should write:
24349
24350 @quotation
24351
24352 @example
24353 My_Var : Interfaces.C.int;
24354 pragma Import (Stdcall, My_Var);
24355 @end example
24356 @end quotation
24357
24358 Note that to ease building cross-platform bindings this convention
24359 will be handled as a @code{C} calling convention on non-Windows platforms.
24360
24361 @node Win32 Calling Convention,DLL Calling Convention,Stdcall Calling Convention,Windows Calling Conventions
24362 @anchor{gnat_ugn/platform_specific_information win32-calling-convention}@anchor{1ec}@anchor{gnat_ugn/platform_specific_information id17}@anchor{1ed}
24363 @subsubsection @code{Win32} Calling Convention
24364
24365
24366 This convention, which is GNAT-specific is fully equivalent to the
24367 @code{Stdcall} calling convention described above.
24368
24369 @node DLL Calling Convention,,Win32 Calling Convention,Windows Calling Conventions
24370 @anchor{gnat_ugn/platform_specific_information id18}@anchor{1ee}@anchor{gnat_ugn/platform_specific_information dll-calling-convention}@anchor{1ef}
24371 @subsubsection @code{DLL} Calling Convention
24372
24373
24374 This convention, which is GNAT-specific is fully equivalent to the
24375 @code{Stdcall} calling convention described above.
24376
24377 @node Introduction to Dynamic Link Libraries DLLs,Using DLLs with GNAT,Windows Calling Conventions,Mixed-Language Programming on Windows
24378 @anchor{gnat_ugn/platform_specific_information id19}@anchor{1f0}@anchor{gnat_ugn/platform_specific_information introduction-to-dynamic-link-libraries-dlls}@anchor{1f1}
24379 @subsubsection Introduction to Dynamic Link Libraries (DLLs)
24380
24381
24382 @geindex DLL
24383
24384 A Dynamically Linked Library (DLL) is a library that can be shared by
24385 several applications running under Windows. A DLL can contain any number of
24386 routines and variables.
24387
24388 One advantage of DLLs is that you can change and enhance them without
24389 forcing all the applications that depend on them to be relinked or
24390 recompiled. However, you should be aware than all calls to DLL routines are
24391 slower since, as you will understand below, such calls are indirect.
24392
24393 To illustrate the remainder of this section, suppose that an application
24394 wants to use the services of a DLL @code{API.dll}. To use the services
24395 provided by @code{API.dll} you must statically link against the DLL or
24396 an import library which contains a jump table with an entry for each
24397 routine and variable exported by the DLL. In the Microsoft world this
24398 import library is called @code{API.lib}. When using GNAT this import
24399 library is called either @code{libAPI.dll.a}, @code{libapi.dll.a},
24400 @code{libAPI.a} or @code{libapi.a} (names are case insensitive).
24401
24402 After you have linked your application with the DLL or the import library
24403 and you run your application, here is what happens:
24404
24405
24406 @itemize *
24407
24408 @item
24409 Your application is loaded into memory.
24410
24411 @item
24412 The DLL @code{API.dll} is mapped into the address space of your
24413 application. This means that:
24414
24415
24416 @itemize -
24417
24418 @item
24419 The DLL will use the stack of the calling thread.
24420
24421 @item
24422 The DLL will use the virtual address space of the calling process.
24423
24424 @item
24425 The DLL will allocate memory from the virtual address space of the calling
24426 process.
24427
24428 @item
24429 Handles (pointers) can be safely exchanged between routines in the DLL
24430 routines and routines in the application using the DLL.
24431 @end itemize
24432
24433 @item
24434 The entries in the jump table (from the import library @code{libAPI.dll.a}
24435 or @code{API.lib} or automatically created when linking against a DLL)
24436 which is part of your application are initialized with the addresses
24437 of the routines and variables in @code{API.dll}.
24438
24439 @item
24440 If present in @code{API.dll}, routines @code{DllMain} or
24441 @code{DllMainCRTStartup} are invoked. These routines typically contain
24442 the initialization code needed for the well-being of the routines and
24443 variables exported by the DLL.
24444 @end itemize
24445
24446 There is an additional point which is worth mentioning. In the Windows
24447 world there are two kind of DLLs: relocatable and non-relocatable
24448 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
24449 in the target application address space. If the addresses of two
24450 non-relocatable DLLs overlap and these happen to be used by the same
24451 application, a conflict will occur and the application will run
24452 incorrectly. Hence, when possible, it is always preferable to use and
24453 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
24454 supported by GNAT. Note that the @code{-s} linker option (see GNU Linker
24455 User's Guide) removes the debugging symbols from the DLL but the DLL can
24456 still be relocated.
24457
24458 As a side note, an interesting difference between Microsoft DLLs and
24459 Unix shared libraries, is the fact that on most Unix systems all public
24460 routines are exported by default in a Unix shared library, while under
24461 Windows it is possible (but not required) to list exported routines in
24462 a definition file (see @ref{1f2,,The Definition File}).
24463
24464 @node Using DLLs with GNAT,Building DLLs with GNAT Project files,Introduction to Dynamic Link Libraries DLLs,Mixed-Language Programming on Windows
24465 @anchor{gnat_ugn/platform_specific_information id20}@anchor{1f3}@anchor{gnat_ugn/platform_specific_information using-dlls-with-gnat}@anchor{1e4}
24466 @subsubsection Using DLLs with GNAT
24467
24468
24469 To use the services of a DLL, say @code{API.dll}, in your Ada application
24470 you must have:
24471
24472
24473 @itemize *
24474
24475 @item
24476 The Ada spec for the routines and/or variables you want to access in
24477 @code{API.dll}. If not available this Ada spec must be built from the C/C++
24478 header files provided with the DLL.
24479
24480 @item
24481 The import library (@code{libAPI.dll.a} or @code{API.lib}). As previously
24482 mentioned an import library is a statically linked library containing the
24483 import table which will be filled at load time to point to the actual
24484 @code{API.dll} routines. Sometimes you don't have an import library for the
24485 DLL you want to use. The following sections will explain how to build
24486 one. Note that this is optional.
24487
24488 @item
24489 The actual DLL, @code{API.dll}.
24490 @end itemize
24491
24492 Once you have all the above, to compile an Ada application that uses the
24493 services of @code{API.dll} and whose main subprogram is @code{My_Ada_App},
24494 you simply issue the command
24495
24496 @quotation
24497
24498 @example
24499 $ gnatmake my_ada_app -largs -lAPI
24500 @end example
24501 @end quotation
24502
24503 The argument @code{-largs -lAPI} at the end of the @code{gnatmake} command
24504 tells the GNAT linker to look for an import library. The linker will
24505 look for a library name in this specific order:
24506
24507
24508 @itemize *
24509
24510 @item
24511 @code{libAPI.dll.a}
24512
24513 @item
24514 @code{API.dll.a}
24515
24516 @item
24517 @code{libAPI.a}
24518
24519 @item
24520 @code{API.lib}
24521
24522 @item
24523 @code{libAPI.dll}
24524
24525 @item
24526 @code{API.dll}
24527 @end itemize
24528
24529 The first three are the GNU style import libraries. The third is the
24530 Microsoft style import libraries. The last two are the actual DLL names.
24531
24532 Note that if the Ada package spec for @code{API.dll} contains the
24533 following pragma
24534
24535 @quotation
24536
24537 @example
24538 pragma Linker_Options ("-lAPI");
24539 @end example
24540 @end quotation
24541
24542 you do not have to add @code{-largs -lAPI} at the end of the
24543 @code{gnatmake} command.
24544
24545 If any one of the items above is missing you will have to create it
24546 yourself. The following sections explain how to do so using as an
24547 example a fictitious DLL called @code{API.dll}.
24548
24549 @menu
24550 * Creating an Ada Spec for the DLL Services::
24551 * Creating an Import Library::
24552
24553 @end menu
24554
24555 @node Creating an Ada Spec for the DLL Services,Creating an Import Library,,Using DLLs with GNAT
24556 @anchor{gnat_ugn/platform_specific_information id21}@anchor{1f4}@anchor{gnat_ugn/platform_specific_information creating-an-ada-spec-for-the-dll-services}@anchor{1f5}
24557 @subsubsection Creating an Ada Spec for the DLL Services
24558
24559
24560 A DLL typically comes with a C/C++ header file which provides the
24561 definitions of the routines and variables exported by the DLL. The Ada
24562 equivalent of this header file is a package spec that contains definitions
24563 for the imported entities. If the DLL you intend to use does not come with
24564 an Ada spec you have to generate one such spec yourself. For example if
24565 the header file of @code{API.dll} is a file @code{api.h} containing the
24566 following two definitions:
24567
24568 @quotation
24569
24570 @example
24571 int some_var;
24572 int get (char *);
24573 @end example
24574 @end quotation
24575
24576 then the equivalent Ada spec could be:
24577
24578 @quotation
24579
24580 @example
24581 with Interfaces.C.Strings;
24582 package API is
24583 use Interfaces;
24584
24585 Some_Var : C.int;
24586 function Get (Str : C.Strings.Chars_Ptr) return C.int;
24587
24588 private
24589 pragma Import (C, Get);
24590 pragma Import (DLL, Some_Var);
24591 end API;
24592 @end example
24593 @end quotation
24594
24595 @node Creating an Import Library,,Creating an Ada Spec for the DLL Services,Using DLLs with GNAT
24596 @anchor{gnat_ugn/platform_specific_information id22}@anchor{1f6}@anchor{gnat_ugn/platform_specific_information creating-an-import-library}@anchor{1f7}
24597 @subsubsection Creating an Import Library
24598
24599
24600 @geindex Import library
24601
24602 If a Microsoft-style import library @code{API.lib} or a GNAT-style
24603 import library @code{libAPI.dll.a} or @code{libAPI.a} is available
24604 with @code{API.dll} you can skip this section. You can also skip this
24605 section if @code{API.dll} or @code{libAPI.dll} is built with GNU tools
24606 as in this case it is possible to link directly against the
24607 DLL. Otherwise read on.
24608
24609 @geindex Definition file
24610 @anchor{gnat_ugn/platform_specific_information the-definition-file}@anchor{1f2}
24611 @subsubheading The Definition File
24612
24613
24614 As previously mentioned, and unlike Unix systems, the list of symbols
24615 that are exported from a DLL must be provided explicitly in Windows.
24616 The main goal of a definition file is precisely that: list the symbols
24617 exported by a DLL. A definition file (usually a file with a @code{.def}
24618 suffix) has the following structure:
24619
24620 @quotation
24621
24622 @example
24623 [LIBRARY `@w{`}name`@w{`}]
24624 [DESCRIPTION `@w{`}string`@w{`}]
24625 EXPORTS
24626 `@w{`}symbol1`@w{`}
24627 `@w{`}symbol2`@w{`}
24628 ...
24629 @end example
24630 @end quotation
24631
24632
24633 @table @asis
24634
24635 @item @emph{LIBRARY name}
24636
24637 This section, which is optional, gives the name of the DLL.
24638
24639 @item @emph{DESCRIPTION string}
24640
24641 This section, which is optional, gives a description string that will be
24642 embedded in the import library.
24643
24644 @item @emph{EXPORTS}
24645
24646 This section gives the list of exported symbols (procedures, functions or
24647 variables). For instance in the case of @code{API.dll} the @code{EXPORTS}
24648 section of @code{API.def} looks like:
24649
24650 @example
24651 EXPORTS
24652 some_var
24653 get
24654 @end example
24655 @end table
24656
24657 Note that you must specify the correct suffix (@code{@@@emph{nn}})
24658 (see @ref{1e6,,Windows Calling Conventions}) for a Stdcall
24659 calling convention function in the exported symbols list.
24660
24661 There can actually be other sections in a definition file, but these
24662 sections are not relevant to the discussion at hand.
24663 @anchor{gnat_ugn/platform_specific_information create-def-file-automatically}@anchor{1f8}
24664 @subsubheading Creating a Definition File Automatically
24665
24666
24667 You can automatically create the definition file @code{API.def}
24668 (see @ref{1f2,,The Definition File}) from a DLL.
24669 For that use the @code{dlltool} program as follows:
24670
24671 @quotation
24672
24673 @example
24674 $ dlltool API.dll -z API.def --export-all-symbols
24675 @end example
24676
24677 Note that if some routines in the DLL have the @code{Stdcall} convention
24678 (@ref{1e6,,Windows Calling Conventions}) with stripped @code{@@@emph{nn}}
24679 suffix then you'll have to edit @code{api.def} to add it, and specify
24680 @code{-k} to @code{gnatdll} when creating the import library.
24681
24682 Here are some hints to find the right @code{@@@emph{nn}} suffix.
24683
24684
24685 @itemize -
24686
24687 @item
24688 If you have the Microsoft import library (.lib), it is possible to get
24689 the right symbols by using Microsoft @code{dumpbin} tool (see the
24690 corresponding Microsoft documentation for further details).
24691
24692 @example
24693 $ dumpbin /exports api.lib
24694 @end example
24695
24696 @item
24697 If you have a message about a missing symbol at link time the compiler
24698 tells you what symbol is expected. You just have to go back to the
24699 definition file and add the right suffix.
24700 @end itemize
24701 @end quotation
24702 @anchor{gnat_ugn/platform_specific_information gnat-style-import-library}@anchor{1f9}
24703 @subsubheading GNAT-Style Import Library
24704
24705
24706 To create a static import library from @code{API.dll} with the GNAT tools
24707 you should create the .def file, then use @code{gnatdll} tool
24708 (see @ref{1fa,,Using gnatdll}) as follows:
24709
24710 @quotation
24711
24712 @example
24713 $ gnatdll -e API.def -d API.dll
24714 @end example
24715
24716 @code{gnatdll} takes as input a definition file @code{API.def} and the
24717 name of the DLL containing the services listed in the definition file
24718 @code{API.dll}. The name of the static import library generated is
24719 computed from the name of the definition file as follows: if the
24720 definition file name is @code{xyz.def}, the import library name will
24721 be @code{libxyz.a}. Note that in the previous example option
24722 @code{-e} could have been removed because the name of the definition
24723 file (before the @code{.def} suffix) is the same as the name of the
24724 DLL (@ref{1fa,,Using gnatdll} for more information about @code{gnatdll}).
24725 @end quotation
24726 @anchor{gnat_ugn/platform_specific_information msvs-style-import-library}@anchor{1fb}
24727 @subsubheading Microsoft-Style Import Library
24728
24729
24730 A Microsoft import library is needed only if you plan to make an
24731 Ada DLL available to applications developed with Microsoft
24732 tools (@ref{1e3,,Mixed-Language Programming on Windows}).
24733
24734 To create a Microsoft-style import library for @code{API.dll} you
24735 should create the .def file, then build the actual import library using
24736 Microsoft's @code{lib} utility:
24737
24738 @quotation
24739
24740 @example
24741 $ lib -machine:IX86 -def:API.def -out:API.lib
24742 @end example
24743
24744 If you use the above command the definition file @code{API.def} must
24745 contain a line giving the name of the DLL:
24746
24747 @example
24748 LIBRARY "API"
24749 @end example
24750
24751 See the Microsoft documentation for further details about the usage of
24752 @code{lib}.
24753 @end quotation
24754
24755 @node Building DLLs with GNAT Project files,Building DLLs with GNAT,Using DLLs with GNAT,Mixed-Language Programming on Windows
24756 @anchor{gnat_ugn/platform_specific_information id23}@anchor{1fc}@anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat-project-files}@anchor{1e5}
24757 @subsubsection Building DLLs with GNAT Project files
24758
24759
24760 @geindex DLLs
24761 @geindex building
24762
24763 There is nothing specific to Windows in the build process.
24764 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
24765 chapter of the @emph{GPRbuild User's Guide}.
24766
24767 Due to a system limitation, it is not possible under Windows to create threads
24768 when inside the @code{DllMain} routine which is used for auto-initialization
24769 of shared libraries, so it is not possible to have library level tasks in SALs.
24770
24771 @node Building DLLs with GNAT,Building DLLs with gnatdll,Building DLLs with GNAT Project files,Mixed-Language Programming on Windows
24772 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat}@anchor{1fd}@anchor{gnat_ugn/platform_specific_information id24}@anchor{1fe}
24773 @subsubsection Building DLLs with GNAT
24774
24775
24776 @geindex DLLs
24777 @geindex building
24778
24779 This section explain how to build DLLs using the GNAT built-in DLL
24780 support. With the following procedure it is straight forward to build
24781 and use DLLs with GNAT.
24782
24783
24784 @itemize *
24785
24786 @item
24787 Building object files.
24788 The first step is to build all objects files that are to be included
24789 into the DLL. This is done by using the standard @code{gnatmake} tool.
24790
24791 @item
24792 Building the DLL.
24793 To build the DLL you must use the @code{gcc} @code{-shared} and
24794 @code{-shared-libgcc} options. It is quite simple to use this method:
24795
24796 @example
24797 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
24798 @end example
24799
24800 It is important to note that in this case all symbols found in the
24801 object files are automatically exported. It is possible to restrict
24802 the set of symbols to export by passing to @code{gcc} a definition
24803 file (see @ref{1f2,,The Definition File}).
24804 For example:
24805
24806 @example
24807 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
24808 @end example
24809
24810 If you use a definition file you must export the elaboration procedures
24811 for every package that required one. Elaboration procedures are named
24812 using the package name followed by "_E".
24813
24814 @item
24815 Preparing DLL to be used.
24816 For the DLL to be used by client programs the bodies must be hidden
24817 from it and the .ali set with read-only attribute. This is very important
24818 otherwise GNAT will recompile all packages and will not actually use
24819 the code in the DLL. For example:
24820
24821 @example
24822 $ mkdir apilib
24823 $ copy *.ads *.ali api.dll apilib
24824 $ attrib +R apilib\\*.ali
24825 @end example
24826 @end itemize
24827
24828 At this point it is possible to use the DLL by directly linking
24829 against it. Note that you must use the GNAT shared runtime when using
24830 GNAT shared libraries. This is achieved by using the @code{-shared} binder
24831 option.
24832
24833 @quotation
24834
24835 @example
24836 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
24837 @end example
24838 @end quotation
24839
24840 @node Building DLLs with gnatdll,Ada DLLs and Finalization,Building DLLs with GNAT,Mixed-Language Programming on Windows
24841 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnatdll}@anchor{1ff}@anchor{gnat_ugn/platform_specific_information id25}@anchor{200}
24842 @subsubsection Building DLLs with gnatdll
24843
24844
24845 @geindex DLLs
24846 @geindex building
24847
24848 Note that it is preferred to use GNAT Project files
24849 (@ref{1e5,,Building DLLs with GNAT Project files}) or the built-in GNAT
24850 DLL support (@ref{1fd,,Building DLLs with GNAT}) or to build DLLs.
24851
24852 This section explains how to build DLLs containing Ada code using
24853 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
24854 remainder of this section.
24855
24856 The steps required to build an Ada DLL that is to be used by Ada as well as
24857 non-Ada applications are as follows:
24858
24859
24860 @itemize *
24861
24862 @item
24863 You need to mark each Ada entity exported by the DLL with a @code{C} or
24864 @code{Stdcall} calling convention to avoid any Ada name mangling for the
24865 entities exported by the DLL
24866 (see @ref{201,,Exporting Ada Entities}). You can
24867 skip this step if you plan to use the Ada DLL only from Ada applications.
24868
24869 @item
24870 Your Ada code must export an initialization routine which calls the routine
24871 @code{adainit} generated by @code{gnatbind} to perform the elaboration of
24872 the Ada code in the DLL (@ref{202,,Ada DLLs and Elaboration}). The initialization
24873 routine exported by the Ada DLL must be invoked by the clients of the DLL
24874 to initialize the DLL.
24875
24876 @item
24877 When useful, the DLL should also export a finalization routine which calls
24878 routine @code{adafinal} generated by @code{gnatbind} to perform the
24879 finalization of the Ada code in the DLL (@ref{203,,Ada DLLs and Finalization}).
24880 The finalization routine exported by the Ada DLL must be invoked by the
24881 clients of the DLL when the DLL services are no further needed.
24882
24883 @item
24884 You must provide a spec for the services exported by the Ada DLL in each
24885 of the programming languages to which you plan to make the DLL available.
24886
24887 @item
24888 You must provide a definition file listing the exported entities
24889 (@ref{1f2,,The Definition File}).
24890
24891 @item
24892 Finally you must use @code{gnatdll} to produce the DLL and the import
24893 library (@ref{1fa,,Using gnatdll}).
24894 @end itemize
24895
24896 Note that a relocatable DLL stripped using the @code{strip}
24897 binutils tool will not be relocatable anymore. To build a DLL without
24898 debug information pass @code{-largs -s} to @code{gnatdll}. This
24899 restriction does not apply to a DLL built using a Library Project.
24900 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
24901 chapter of the @emph{GPRbuild User's Guide}.
24902
24903 @c Limitations_When_Using_Ada_DLLs_from Ada:
24904
24905 @menu
24906 * Limitations When Using Ada DLLs from Ada::
24907 * Exporting Ada Entities::
24908 * Ada DLLs and Elaboration::
24909
24910 @end menu
24911
24912 @node Limitations When Using Ada DLLs from Ada,Exporting Ada Entities,,Building DLLs with gnatdll
24913 @anchor{gnat_ugn/platform_specific_information limitations-when-using-ada-dlls-from-ada}@anchor{204}
24914 @subsubsection Limitations When Using Ada DLLs from Ada
24915
24916
24917 When using Ada DLLs from Ada applications there is a limitation users
24918 should be aware of. Because on Windows the GNAT run-time is not in a DLL of
24919 its own, each Ada DLL includes a part of the GNAT run-time. Specifically,
24920 each Ada DLL includes the services of the GNAT run-time that are necessary
24921 to the Ada code inside the DLL. As a result, when an Ada program uses an
24922 Ada DLL there are two independent GNAT run-times: one in the Ada DLL and
24923 one in the main program.
24924
24925 It is therefore not possible to exchange GNAT run-time objects between the
24926 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
24927 handles (e.g., @code{Text_IO.File_Type}), tasks types, protected objects
24928 types, etc.
24929
24930 It is completely safe to exchange plain elementary, array or record types,
24931 Windows object handles, etc.
24932
24933 @node Exporting Ada Entities,Ada DLLs and Elaboration,Limitations When Using Ada DLLs from Ada,Building DLLs with gnatdll
24934 @anchor{gnat_ugn/platform_specific_information exporting-ada-entities}@anchor{201}@anchor{gnat_ugn/platform_specific_information id26}@anchor{205}
24935 @subsubsection Exporting Ada Entities
24936
24937
24938 @geindex Export table
24939
24940 Building a DLL is a way to encapsulate a set of services usable from any
24941 application. As a result, the Ada entities exported by a DLL should be
24942 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
24943 any Ada name mangling. As an example here is an Ada package
24944 @code{API}, spec and body, exporting two procedures, a function, and a
24945 variable:
24946
24947 @quotation
24948
24949 @example
24950 with Interfaces.C; use Interfaces;
24951 package API is
24952 Count : C.int := 0;
24953 function Factorial (Val : C.int) return C.int;
24954
24955 procedure Initialize_API;
24956 procedure Finalize_API;
24957 -- Initialization & Finalization routines. More in the next section.
24958 private
24959 pragma Export (C, Initialize_API);
24960 pragma Export (C, Finalize_API);
24961 pragma Export (C, Count);
24962 pragma Export (C, Factorial);
24963 end API;
24964 @end example
24965
24966 @example
24967 package body API is
24968 function Factorial (Val : C.int) return C.int is
24969 Fact : C.int := 1;
24970 begin
24971 Count := Count + 1;
24972 for K in 1 .. Val loop
24973 Fact := Fact * K;
24974 end loop;
24975 return Fact;
24976 end Factorial;
24977
24978 procedure Initialize_API is
24979 procedure Adainit;
24980 pragma Import (C, Adainit);
24981 begin
24982 Adainit;
24983 end Initialize_API;
24984
24985 procedure Finalize_API is
24986 procedure Adafinal;
24987 pragma Import (C, Adafinal);
24988 begin
24989 Adafinal;
24990 end Finalize_API;
24991 end API;
24992 @end example
24993 @end quotation
24994
24995 If the Ada DLL you are building will only be used by Ada applications
24996 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
24997 convention. As an example, the previous package could be written as
24998 follows:
24999
25000 @quotation
25001
25002 @example
25003 package API is
25004 Count : Integer := 0;
25005 function Factorial (Val : Integer) return Integer;
25006
25007 procedure Initialize_API;
25008 procedure Finalize_API;
25009 -- Initialization and Finalization routines.
25010 end API;
25011 @end example
25012
25013 @example
25014 package body API is
25015 function Factorial (Val : Integer) return Integer is
25016 Fact : Integer := 1;
25017 begin
25018 Count := Count + 1;
25019 for K in 1 .. Val loop
25020 Fact := Fact * K;
25021 end loop;
25022 return Fact;
25023 end Factorial;
25024
25025 ...
25026 -- The remainder of this package body is unchanged.
25027 end API;
25028 @end example
25029 @end quotation
25030
25031 Note that if you do not export the Ada entities with a @code{C} or
25032 @code{Stdcall} convention you will have to provide the mangled Ada names
25033 in the definition file of the Ada DLL
25034 (@ref{206,,Creating the Definition File}).
25035
25036 @node Ada DLLs and Elaboration,,Exporting Ada Entities,Building DLLs with gnatdll
25037 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-elaboration}@anchor{202}@anchor{gnat_ugn/platform_specific_information id27}@anchor{207}
25038 @subsubsection Ada DLLs and Elaboration
25039
25040
25041 @geindex DLLs and elaboration
25042
25043 The DLL that you are building contains your Ada code as well as all the
25044 routines in the Ada library that are needed by it. The first thing a
25045 user of your DLL must do is elaborate the Ada code
25046 (@ref{f,,Elaboration Order Handling in GNAT}).
25047
25048 To achieve this you must export an initialization routine
25049 (@code{Initialize_API} in the previous example), which must be invoked
25050 before using any of the DLL services. This elaboration routine must call
25051 the Ada elaboration routine @code{adainit} generated by the GNAT binder
25052 (@ref{b4,,Binding with Non-Ada Main Programs}). See the body of
25053 @code{Initialize_Api} for an example. Note that the GNAT binder is
25054 automatically invoked during the DLL build process by the @code{gnatdll}
25055 tool (@ref{1fa,,Using gnatdll}).
25056
25057 When a DLL is loaded, Windows systematically invokes a routine called
25058 @code{DllMain}. It would therefore be possible to call @code{adainit}
25059 directly from @code{DllMain} without having to provide an explicit
25060 initialization routine. Unfortunately, it is not possible to call
25061 @code{adainit} from the @code{DllMain} if your program has library level
25062 tasks because access to the @code{DllMain} entry point is serialized by
25063 the system (that is, only a single thread can execute 'through' it at a
25064 time), which means that the GNAT run-time will deadlock waiting for the
25065 newly created task to complete its initialization.
25066
25067 @node Ada DLLs and Finalization,Creating a Spec for Ada DLLs,Building DLLs with gnatdll,Mixed-Language Programming on Windows
25068 @anchor{gnat_ugn/platform_specific_information id28}@anchor{208}@anchor{gnat_ugn/platform_specific_information ada-dlls-and-finalization}@anchor{203}
25069 @subsubsection Ada DLLs and Finalization
25070
25071
25072 @geindex DLLs and finalization
25073
25074 When the services of an Ada DLL are no longer needed, the client code should
25075 invoke the DLL finalization routine, if available. The DLL finalization
25076 routine is in charge of releasing all resources acquired by the DLL. In the
25077 case of the Ada code contained in the DLL, this is achieved by calling
25078 routine @code{adafinal} generated by the GNAT binder
25079 (@ref{b4,,Binding with Non-Ada Main Programs}).
25080 See the body of @code{Finalize_Api} for an
25081 example. As already pointed out the GNAT binder is automatically invoked
25082 during the DLL build process by the @code{gnatdll} tool
25083 (@ref{1fa,,Using gnatdll}).
25084
25085 @node Creating a Spec for Ada DLLs,GNAT and Windows Resources,Ada DLLs and Finalization,Mixed-Language Programming on Windows
25086 @anchor{gnat_ugn/platform_specific_information id29}@anchor{209}@anchor{gnat_ugn/platform_specific_information creating-a-spec-for-ada-dlls}@anchor{20a}
25087 @subsubsection Creating a Spec for Ada DLLs
25088
25089
25090 To use the services exported by the Ada DLL from another programming
25091 language (e.g., C), you have to translate the specs of the exported Ada
25092 entities in that language. For instance in the case of @code{API.dll},
25093 the corresponding C header file could look like:
25094
25095 @quotation
25096
25097 @example
25098 extern int *_imp__count;
25099 #define count (*_imp__count)
25100 int factorial (int);
25101 @end example
25102 @end quotation
25103
25104 It is important to understand that when building an Ada DLL to be used by
25105 other Ada applications, you need two different specs for the packages
25106 contained in the DLL: one for building the DLL and the other for using
25107 the DLL. This is because the @code{DLL} calling convention is needed to
25108 use a variable defined in a DLL, but when building the DLL, the variable
25109 must have either the @code{Ada} or @code{C} calling convention. As an
25110 example consider a DLL comprising the following package @code{API}:
25111
25112 @quotation
25113
25114 @example
25115 package API is
25116 Count : Integer := 0;
25117 ...
25118 -- Remainder of the package omitted.
25119 end API;
25120 @end example
25121 @end quotation
25122
25123 After producing a DLL containing package @code{API}, the spec that
25124 must be used to import @code{API.Count} from Ada code outside of the
25125 DLL is:
25126
25127 @quotation
25128
25129 @example
25130 package API is
25131 Count : Integer;
25132 pragma Import (DLL, Count);
25133 end API;
25134 @end example
25135 @end quotation
25136
25137 @menu
25138 * Creating the Definition File::
25139 * Using gnatdll::
25140
25141 @end menu
25142
25143 @node Creating the Definition File,Using gnatdll,,Creating a Spec for Ada DLLs
25144 @anchor{gnat_ugn/platform_specific_information creating-the-definition-file}@anchor{206}@anchor{gnat_ugn/platform_specific_information id30}@anchor{20b}
25145 @subsubsection Creating the Definition File
25146
25147
25148 The definition file is the last file needed to build the DLL. It lists
25149 the exported symbols. As an example, the definition file for a DLL
25150 containing only package @code{API} (where all the entities are exported
25151 with a @code{C} calling convention) is:
25152
25153 @quotation
25154
25155 @example
25156 EXPORTS
25157 count
25158 factorial
25159 finalize_api
25160 initialize_api
25161 @end example
25162 @end quotation
25163
25164 If the @code{C} calling convention is missing from package @code{API},
25165 then the definition file contains the mangled Ada names of the above
25166 entities, which in this case are:
25167
25168 @quotation
25169
25170 @example
25171 EXPORTS
25172 api__count
25173 api__factorial
25174 api__finalize_api
25175 api__initialize_api
25176 @end example
25177 @end quotation
25178
25179 @node Using gnatdll,,Creating the Definition File,Creating a Spec for Ada DLLs
25180 @anchor{gnat_ugn/platform_specific_information using-gnatdll}@anchor{1fa}@anchor{gnat_ugn/platform_specific_information id31}@anchor{20c}
25181 @subsubsection Using @code{gnatdll}
25182
25183
25184 @geindex gnatdll
25185
25186 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
25187 and non-Ada sources that make up your DLL have been compiled.
25188 @code{gnatdll} is actually in charge of two distinct tasks: build the
25189 static import library for the DLL and the actual DLL. The form of the
25190 @code{gnatdll} command is
25191
25192 @quotation
25193
25194 @example
25195 $ gnatdll [ switches ] list-of-files [ -largs opts ]
25196 @end example
25197 @end quotation
25198
25199 where @code{list-of-files} is a list of ALI and object files. The object
25200 file list must be the exact list of objects corresponding to the non-Ada
25201 sources whose services are to be included in the DLL. The ALI file list
25202 must be the exact list of ALI files for the corresponding Ada sources
25203 whose services are to be included in the DLL. If @code{list-of-files} is
25204 missing, only the static import library is generated.
25205
25206 You may specify any of the following switches to @code{gnatdll}:
25207
25208 @quotation
25209
25210 @geindex -a (gnatdll)
25211 @end quotation
25212
25213
25214 @table @asis
25215
25216 @item @code{-a[@emph{address}]}
25217
25218 Build a non-relocatable DLL at @code{address}. If @code{address} is not
25219 specified the default address @code{0x11000000} will be used. By default,
25220 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
25221 advise the reader to build relocatable DLL.
25222
25223 @geindex -b (gnatdll)
25224
25225 @item @code{-b @emph{address}}
25226
25227 Set the relocatable DLL base address. By default the address is
25228 @code{0x11000000}.
25229
25230 @geindex -bargs (gnatdll)
25231
25232 @item @code{-bargs @emph{opts}}
25233
25234 Binder options. Pass @code{opts} to the binder.
25235
25236 @geindex -d (gnatdll)
25237
25238 @item @code{-d @emph{dllfile}}
25239
25240 @code{dllfile} is the name of the DLL. This switch must be present for
25241 @code{gnatdll} to do anything. The name of the generated import library is
25242 obtained algorithmically from @code{dllfile} as shown in the following
25243 example: if @code{dllfile} is @code{xyz.dll}, the import library name is
25244 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
25245 by option @code{-e}) is obtained algorithmically from @code{dllfile}
25246 as shown in the following example:
25247 if @code{dllfile} is @code{xyz.dll}, the definition
25248 file used is @code{xyz.def}.
25249
25250 @geindex -e (gnatdll)
25251
25252 @item @code{-e @emph{deffile}}
25253
25254 @code{deffile} is the name of the definition file.
25255
25256 @geindex -g (gnatdll)
25257
25258 @item @code{-g}
25259
25260 Generate debugging information. This information is stored in the object
25261 file and copied from there to the final DLL file by the linker,
25262 where it can be read by the debugger. You must use the
25263 @code{-g} switch if you plan on using the debugger or the symbolic
25264 stack traceback.
25265
25266 @geindex -h (gnatdll)
25267
25268 @item @code{-h}
25269
25270 Help mode. Displays @code{gnatdll} switch usage information.
25271
25272 @geindex -I (gnatdll)
25273
25274 @item @code{-I@emph{dir}}
25275
25276 Direct @code{gnatdll} to search the @code{dir} directory for source and
25277 object files needed to build the DLL.
25278 (@ref{89,,Search Paths and the Run-Time Library (RTL)}).
25279
25280 @geindex -k (gnatdll)
25281
25282 @item @code{-k}
25283
25284 Removes the @code{@@@emph{nn}} suffix from the import library's exported
25285 names, but keeps them for the link names. You must specify this
25286 option if you want to use a @code{Stdcall} function in a DLL for which
25287 the @code{@@@emph{nn}} suffix has been removed. This is the case for most
25288 of the Windows NT DLL for example. This option has no effect when
25289 @code{-n} option is specified.
25290
25291 @geindex -l (gnatdll)
25292
25293 @item @code{-l @emph{file}}
25294
25295 The list of ALI and object files used to build the DLL are listed in
25296 @code{file}, instead of being given in the command line. Each line in
25297 @code{file} contains the name of an ALI or object file.
25298
25299 @geindex -n (gnatdll)
25300
25301 @item @code{-n}
25302
25303 No Import. Do not create the import library.
25304
25305 @geindex -q (gnatdll)
25306
25307 @item @code{-q}
25308
25309 Quiet mode. Do not display unnecessary messages.
25310
25311 @geindex -v (gnatdll)
25312
25313 @item @code{-v}
25314
25315 Verbose mode. Display extra information.
25316
25317 @geindex -largs (gnatdll)
25318
25319 @item @code{-largs @emph{opts}}
25320
25321 Linker options. Pass @code{opts} to the linker.
25322 @end table
25323
25324 @subsubheading @code{gnatdll} Example
25325
25326
25327 As an example the command to build a relocatable DLL from @code{api.adb}
25328 once @code{api.adb} has been compiled and @code{api.def} created is
25329
25330 @quotation
25331
25332 @example
25333 $ gnatdll -d api.dll api.ali
25334 @end example
25335 @end quotation
25336
25337 The above command creates two files: @code{libapi.dll.a} (the import
25338 library) and @code{api.dll} (the actual DLL). If you want to create
25339 only the DLL, just type:
25340
25341 @quotation
25342
25343 @example
25344 $ gnatdll -d api.dll -n api.ali
25345 @end example
25346 @end quotation
25347
25348 Alternatively if you want to create just the import library, type:
25349
25350 @quotation
25351
25352 @example
25353 $ gnatdll -d api.dll
25354 @end example
25355 @end quotation
25356
25357 @subsubheading @code{gnatdll} behind the Scenes
25358
25359
25360 This section details the steps involved in creating a DLL. @code{gnatdll}
25361 does these steps for you. Unless you are interested in understanding what
25362 goes on behind the scenes, you should skip this section.
25363
25364 We use the previous example of a DLL containing the Ada package @code{API},
25365 to illustrate the steps necessary to build a DLL. The starting point is a
25366 set of objects that will make up the DLL and the corresponding ALI
25367 files. In the case of this example this means that @code{api.o} and
25368 @code{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
25369 the following:
25370
25371
25372 @itemize *
25373
25374 @item
25375 @code{gnatdll} builds the base file (@code{api.base}). A base file gives
25376 the information necessary to generate relocation information for the
25377 DLL.
25378
25379 @example
25380 $ gnatbind -n api
25381 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
25382 @end example
25383
25384 In addition to the base file, the @code{gnatlink} command generates an
25385 output file @code{api.jnk} which can be discarded. The @code{-mdll} switch
25386 asks @code{gnatlink} to generate the routines @code{DllMain} and
25387 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
25388 is loaded into memory.
25389
25390 @item
25391 @code{gnatdll} uses @code{dlltool} (see @ref{20d,,Using dlltool}) to build the
25392 export table (@code{api.exp}). The export table contains the relocation
25393 information in a form which can be used during the final link to ensure
25394 that the Windows loader is able to place the DLL anywhere in memory.
25395
25396 @example
25397 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25398 --output-exp api.exp
25399 @end example
25400
25401 @item
25402 @code{gnatdll} builds the base file using the new export table. Note that
25403 @code{gnatbind} must be called once again since the binder generated file
25404 has been deleted during the previous call to @code{gnatlink}.
25405
25406 @example
25407 $ gnatbind -n api
25408 $ gnatlink api -o api.jnk api.exp -mdll
25409 -Wl,--base-file,api.base
25410 @end example
25411
25412 @item
25413 @code{gnatdll} builds the new export table using the new base file and
25414 generates the DLL import library @code{libAPI.dll.a}.
25415
25416 @example
25417 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25418 --output-exp api.exp --output-lib libAPI.a
25419 @end example
25420
25421 @item
25422 Finally @code{gnatdll} builds the relocatable DLL using the final export
25423 table.
25424
25425 @example
25426 $ gnatbind -n api
25427 $ gnatlink api api.exp -o api.dll -mdll
25428 @end example
25429 @end itemize
25430 @anchor{gnat_ugn/platform_specific_information using-dlltool}@anchor{20d}
25431 @subsubheading Using @code{dlltool}
25432
25433
25434 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
25435 DLLs and static import libraries. This section summarizes the most
25436 common @code{dlltool} switches. The form of the @code{dlltool} command
25437 is
25438
25439 @quotation
25440
25441 @example
25442 $ dlltool [`switches`]
25443 @end example
25444 @end quotation
25445
25446 @code{dlltool} switches include:
25447
25448 @geindex --base-file (dlltool)
25449
25450
25451 @table @asis
25452
25453 @item @code{--base-file @emph{basefile}}
25454
25455 Read the base file @code{basefile} generated by the linker. This switch
25456 is used to create a relocatable DLL.
25457 @end table
25458
25459 @geindex --def (dlltool)
25460
25461
25462 @table @asis
25463
25464 @item @code{--def @emph{deffile}}
25465
25466 Read the definition file.
25467 @end table
25468
25469 @geindex --dllname (dlltool)
25470
25471
25472 @table @asis
25473
25474 @item @code{--dllname @emph{name}}
25475
25476 Gives the name of the DLL. This switch is used to embed the name of the
25477 DLL in the static import library generated by @code{dlltool} with switch
25478 @code{--output-lib}.
25479 @end table
25480
25481 @geindex -k (dlltool)
25482
25483
25484 @table @asis
25485
25486 @item @code{-k}
25487
25488 Kill @code{@@@emph{nn}} from exported names
25489 (@ref{1e6,,Windows Calling Conventions}
25490 for a discussion about @code{Stdcall}-style symbols.
25491 @end table
25492
25493 @geindex --help (dlltool)
25494
25495
25496 @table @asis
25497
25498 @item @code{--help}
25499
25500 Prints the @code{dlltool} switches with a concise description.
25501 @end table
25502
25503 @geindex --output-exp (dlltool)
25504
25505
25506 @table @asis
25507
25508 @item @code{--output-exp @emph{exportfile}}
25509
25510 Generate an export file @code{exportfile}. The export file contains the
25511 export table (list of symbols in the DLL) and is used to create the DLL.
25512 @end table
25513
25514 @geindex --output-lib (dlltool)
25515
25516
25517 @table @asis
25518
25519 @item @code{--output-lib @emph{libfile}}
25520
25521 Generate a static import library @code{libfile}.
25522 @end table
25523
25524 @geindex -v (dlltool)
25525
25526
25527 @table @asis
25528
25529 @item @code{-v}
25530
25531 Verbose mode.
25532 @end table
25533
25534 @geindex --as (dlltool)
25535
25536
25537 @table @asis
25538
25539 @item @code{--as @emph{assembler-name}}
25540
25541 Use @code{assembler-name} as the assembler. The default is @code{as}.
25542 @end table
25543
25544 @node GNAT and Windows Resources,Using GNAT DLLs from Microsoft Visual Studio Applications,Creating a Spec for Ada DLLs,Mixed-Language Programming on Windows
25545 @anchor{gnat_ugn/platform_specific_information gnat-and-windows-resources}@anchor{20e}@anchor{gnat_ugn/platform_specific_information id32}@anchor{20f}
25546 @subsubsection GNAT and Windows Resources
25547
25548
25549 @geindex Resources
25550 @geindex windows
25551
25552 Resources are an easy way to add Windows specific objects to your
25553 application. The objects that can be added as resources include:
25554
25555
25556 @itemize *
25557
25558 @item
25559 menus
25560
25561 @item
25562 accelerators
25563
25564 @item
25565 dialog boxes
25566
25567 @item
25568 string tables
25569
25570 @item
25571 bitmaps
25572
25573 @item
25574 cursors
25575
25576 @item
25577 icons
25578
25579 @item
25580 fonts
25581
25582 @item
25583 version information
25584 @end itemize
25585
25586 For example, a version information resource can be defined as follow and
25587 embedded into an executable or DLL:
25588
25589 A version information resource can be used to embed information into an
25590 executable or a DLL. These information can be viewed using the file properties
25591 from the Windows Explorer. Here is an example of a version information
25592 resource:
25593
25594 @quotation
25595
25596 @example
25597 1 VERSIONINFO
25598 FILEVERSION 1,0,0,0
25599 PRODUCTVERSION 1,0,0,0
25600 BEGIN
25601 BLOCK "StringFileInfo"
25602 BEGIN
25603 BLOCK "080904E4"
25604 BEGIN
25605 VALUE "CompanyName", "My Company Name"
25606 VALUE "FileDescription", "My application"
25607 VALUE "FileVersion", "1.0"
25608 VALUE "InternalName", "my_app"
25609 VALUE "LegalCopyright", "My Name"
25610 VALUE "OriginalFilename", "my_app.exe"
25611 VALUE "ProductName", "My App"
25612 VALUE "ProductVersion", "1.0"
25613 END
25614 END
25615
25616 BLOCK "VarFileInfo"
25617 BEGIN
25618 VALUE "Translation", 0x809, 1252
25619 END
25620 END
25621 @end example
25622 @end quotation
25623
25624 The value @code{0809} (langID) is for the U.K English language and
25625 @code{04E4} (charsetID), which is equal to @code{1252} decimal, for
25626 multilingual.
25627
25628 This section explains how to build, compile and use resources. Note that this
25629 section does not cover all resource objects, for a complete description see
25630 the corresponding Microsoft documentation.
25631
25632 @menu
25633 * Building Resources::
25634 * Compiling Resources::
25635 * Using Resources::
25636
25637 @end menu
25638
25639 @node Building Resources,Compiling Resources,,GNAT and Windows Resources
25640 @anchor{gnat_ugn/platform_specific_information building-resources}@anchor{210}@anchor{gnat_ugn/platform_specific_information id33}@anchor{211}
25641 @subsubsection Building Resources
25642
25643
25644 @geindex Resources
25645 @geindex building
25646
25647 A resource file is an ASCII file. By convention resource files have an
25648 @code{.rc} extension.
25649 The easiest way to build a resource file is to use Microsoft tools
25650 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
25651 @code{dlgedit.exe} to build dialogs.
25652 It is always possible to build an @code{.rc} file yourself by writing a
25653 resource script.
25654
25655 It is not our objective to explain how to write a resource file. A
25656 complete description of the resource script language can be found in the
25657 Microsoft documentation.
25658
25659 @node Compiling Resources,Using Resources,Building Resources,GNAT and Windows Resources
25660 @anchor{gnat_ugn/platform_specific_information compiling-resources}@anchor{212}@anchor{gnat_ugn/platform_specific_information id34}@anchor{213}
25661 @subsubsection Compiling Resources
25662
25663
25664 @geindex rc
25665
25666 @geindex windres
25667
25668 @geindex Resources
25669 @geindex compiling
25670
25671 This section describes how to build a GNAT-compatible (COFF) object file
25672 containing the resources. This is done using the Resource Compiler
25673 @code{windres} as follows:
25674
25675 @quotation
25676
25677 @example
25678 $ windres -i myres.rc -o myres.o
25679 @end example
25680 @end quotation
25681
25682 By default @code{windres} will run @code{gcc} to preprocess the @code{.rc}
25683 file. You can specify an alternate preprocessor (usually named
25684 @code{cpp.exe}) using the @code{windres} @code{--preprocessor}
25685 parameter. A list of all possible options may be obtained by entering
25686 the command @code{windres} @code{--help}.
25687
25688 It is also possible to use the Microsoft resource compiler @code{rc.exe}
25689 to produce a @code{.res} file (binary resource file). See the
25690 corresponding Microsoft documentation for further details. In this case
25691 you need to use @code{windres} to translate the @code{.res} file to a
25692 GNAT-compatible object file as follows:
25693
25694 @quotation
25695
25696 @example
25697 $ windres -i myres.res -o myres.o
25698 @end example
25699 @end quotation
25700
25701 @node Using Resources,,Compiling Resources,GNAT and Windows Resources
25702 @anchor{gnat_ugn/platform_specific_information using-resources}@anchor{214}@anchor{gnat_ugn/platform_specific_information id35}@anchor{215}
25703 @subsubsection Using Resources
25704
25705
25706 @geindex Resources
25707 @geindex using
25708
25709 To include the resource file in your program just add the
25710 GNAT-compatible object file for the resource(s) to the linker
25711 arguments. With @code{gnatmake} this is done by using the @code{-largs}
25712 option:
25713
25714 @quotation
25715
25716 @example
25717 $ gnatmake myprog -largs myres.o
25718 @end example
25719 @end quotation
25720
25721 @node Using GNAT DLLs from Microsoft Visual Studio Applications,Debugging a DLL,GNAT and Windows Resources,Mixed-Language Programming on Windows
25722 @anchor{gnat_ugn/platform_specific_information using-gnat-dll-from-msvs}@anchor{216}@anchor{gnat_ugn/platform_specific_information using-gnat-dlls-from-microsoft-visual-studio-applications}@anchor{217}
25723 @subsubsection Using GNAT DLLs from Microsoft Visual Studio Applications
25724
25725
25726 @geindex Microsoft Visual Studio
25727 @geindex use with GNAT DLLs
25728
25729 This section describes a common case of mixed GNAT/Microsoft Visual Studio
25730 application development, where the main program is developed using MSVS, and
25731 is linked with a DLL developed using GNAT. Such a mixed application should
25732 be developed following the general guidelines outlined above; below is the
25733 cookbook-style sequence of steps to follow:
25734
25735
25736 @enumerate
25737
25738 @item
25739 First develop and build the GNAT shared library using a library project
25740 (let's assume the project is @code{mylib.gpr}, producing the library @code{libmylib.dll}):
25741 @end enumerate
25742
25743 @quotation
25744
25745 @example
25746 $ gprbuild -p mylib.gpr
25747 @end example
25748 @end quotation
25749
25750
25751 @enumerate 2
25752
25753 @item
25754 Produce a .def file for the symbols you need to interface with, either by
25755 hand or automatically with possibly some manual adjustments
25756 (see @ref{1f8,,Creating Definition File Automatically}):
25757 @end enumerate
25758
25759 @quotation
25760
25761 @example
25762 $ dlltool libmylib.dll -z libmylib.def --export-all-symbols
25763 @end example
25764 @end quotation
25765
25766
25767 @enumerate 3
25768
25769 @item
25770 Make sure that MSVS command-line tools are accessible on the path.
25771
25772 @item
25773 Create the Microsoft-style import library (see @ref{1fb,,MSVS-Style Import Library}):
25774 @end enumerate
25775
25776 @quotation
25777
25778 @example
25779 $ lib -machine:IX86 -def:libmylib.def -out:libmylib.lib
25780 @end example
25781 @end quotation
25782
25783 If you are using a 64-bit toolchain, the above becomes...
25784
25785 @quotation
25786
25787 @example
25788 $ lib -machine:X64 -def:libmylib.def -out:libmylib.lib
25789 @end example
25790 @end quotation
25791
25792
25793 @enumerate 5
25794
25795 @item
25796 Build the C main
25797 @end enumerate
25798
25799 @quotation
25800
25801 @example
25802 $ cl /O2 /MD main.c libmylib.lib
25803 @end example
25804 @end quotation
25805
25806
25807 @enumerate 6
25808
25809 @item
25810 Before running the executable, make sure you have set the PATH to the DLL,
25811 or copy the DLL into into the directory containing the .exe.
25812 @end enumerate
25813
25814 @node Debugging a DLL,Setting Stack Size from gnatlink,Using GNAT DLLs from Microsoft Visual Studio Applications,Mixed-Language Programming on Windows
25815 @anchor{gnat_ugn/platform_specific_information id36}@anchor{218}@anchor{gnat_ugn/platform_specific_information debugging-a-dll}@anchor{219}
25816 @subsubsection Debugging a DLL
25817
25818
25819 @geindex DLL debugging
25820
25821 Debugging a DLL is similar to debugging a standard program. But
25822 we have to deal with two different executable parts: the DLL and the
25823 program that uses it. We have the following four possibilities:
25824
25825
25826 @itemize *
25827
25828 @item
25829 The program and the DLL are built with GCC/GNAT.
25830
25831 @item
25832 The program is built with foreign tools and the DLL is built with
25833 GCC/GNAT.
25834
25835 @item
25836 The program is built with GCC/GNAT and the DLL is built with
25837 foreign tools.
25838 @end itemize
25839
25840 In this section we address only cases one and two above.
25841 There is no point in trying to debug
25842 a DLL with GNU/GDB, if there is no GDB-compatible debugging
25843 information in it. To do so you must use a debugger compatible with the
25844 tools suite used to build the DLL.
25845
25846 @menu
25847 * Program and DLL Both Built with GCC/GNAT::
25848 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
25849
25850 @end menu
25851
25852 @node Program and DLL Both Built with GCC/GNAT,Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Debugging a DLL
25853 @anchor{gnat_ugn/platform_specific_information id37}@anchor{21a}@anchor{gnat_ugn/platform_specific_information program-and-dll-both-built-with-gcc-gnat}@anchor{21b}
25854 @subsubsection Program and DLL Both Built with GCC/GNAT
25855
25856
25857 This is the simplest case. Both the DLL and the program have @code{GDB}
25858 compatible debugging information. It is then possible to break anywhere in
25859 the process. Let's suppose here that the main procedure is named
25860 @code{ada_main} and that in the DLL there is an entry point named
25861 @code{ada_dll}.
25862
25863 The DLL (@ref{1f1,,Introduction to Dynamic Link Libraries (DLLs)}) and
25864 program must have been built with the debugging information (see GNAT -g
25865 switch). Here are the step-by-step instructions for debugging it:
25866
25867
25868 @itemize *
25869
25870 @item
25871 Launch @code{GDB} on the main program.
25872
25873 @example
25874 $ gdb -nw ada_main
25875 @end example
25876
25877 @item
25878 Start the program and stop at the beginning of the main procedure
25879
25880 @example
25881 (gdb) start
25882 @end example
25883
25884 This step is required to be able to set a breakpoint inside the DLL. As long
25885 as the program is not run, the DLL is not loaded. This has the
25886 consequence that the DLL debugging information is also not loaded, so it is not
25887 possible to set a breakpoint in the DLL.
25888
25889 @item
25890 Set a breakpoint inside the DLL
25891
25892 @example
25893 (gdb) break ada_dll
25894 (gdb) cont
25895 @end example
25896 @end itemize
25897
25898 At this stage a breakpoint is set inside the DLL. From there on
25899 you can use the standard approach to debug the whole program
25900 (@ref{24,,Running and Debugging Ada Programs}).
25901
25902 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Program and DLL Both Built with GCC/GNAT,Debugging a DLL
25903 @anchor{gnat_ugn/platform_specific_information program-built-with-foreign-tools-and-dll-built-with-gcc-gnat}@anchor{21c}@anchor{gnat_ugn/platform_specific_information id38}@anchor{21d}
25904 @subsubsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
25905
25906
25907 In this case things are slightly more complex because it is not possible to
25908 start the main program and then break at the beginning to load the DLL and the
25909 associated DLL debugging information. It is not possible to break at the
25910 beginning of the program because there is no @code{GDB} debugging information,
25911 and therefore there is no direct way of getting initial control. This
25912 section addresses this issue by describing some methods that can be used
25913 to break somewhere in the DLL to debug it.
25914
25915 First suppose that the main procedure is named @code{main} (this is for
25916 example some C code built with Microsoft Visual C) and that there is a
25917 DLL named @code{test.dll} containing an Ada entry point named
25918 @code{ada_dll}.
25919
25920 The DLL (see @ref{1f1,,Introduction to Dynamic Link Libraries (DLLs)}) must have
25921 been built with debugging information (see the GNAT @code{-g} option).
25922
25923 @subsubheading Debugging the DLL Directly
25924
25925
25926
25927 @itemize *
25928
25929 @item
25930 Find out the executable starting address
25931
25932 @example
25933 $ objdump --file-header main.exe
25934 @end example
25935
25936 The starting address is reported on the last line. For example:
25937
25938 @example
25939 main.exe: file format pei-i386
25940 architecture: i386, flags 0x0000010a:
25941 EXEC_P, HAS_DEBUG, D_PAGED
25942 start address 0x00401010
25943 @end example
25944
25945 @item
25946 Launch the debugger on the executable.
25947
25948 @example
25949 $ gdb main.exe
25950 @end example
25951
25952 @item
25953 Set a breakpoint at the starting address, and launch the program.
25954
25955 @example
25956 $ (gdb) break *0x00401010
25957 $ (gdb) run
25958 @end example
25959
25960 The program will stop at the given address.
25961
25962 @item
25963 Set a breakpoint on a DLL subroutine.
25964
25965 @example
25966 (gdb) break ada_dll.adb:45
25967 @end example
25968
25969 Or if you want to break using a symbol on the DLL, you need first to
25970 select the Ada language (language used by the DLL).
25971
25972 @example
25973 (gdb) set language ada
25974 (gdb) break ada_dll
25975 @end example
25976
25977 @item
25978 Continue the program.
25979
25980 @example
25981 (gdb) cont
25982 @end example
25983
25984 This will run the program until it reaches the breakpoint that has been
25985 set. From that point you can use the standard way to debug a program
25986 as described in (@ref{24,,Running and Debugging Ada Programs}).
25987 @end itemize
25988
25989 It is also possible to debug the DLL by attaching to a running process.
25990
25991 @subsubheading Attaching to a Running Process
25992
25993
25994 @geindex DLL debugging
25995 @geindex attach to process
25996
25997 With @code{GDB} it is always possible to debug a running process by
25998 attaching to it. It is possible to debug a DLL this way. The limitation
25999 of this approach is that the DLL must run long enough to perform the
26000 attach operation. It may be useful for instance to insert a time wasting
26001 loop in the code of the DLL to meet this criterion.
26002
26003
26004 @itemize *
26005
26006 @item
26007 Launch the main program @code{main.exe}.
26008
26009 @example
26010 $ main
26011 @end example
26012
26013 @item
26014 Use the Windows @emph{Task Manager} to find the process ID. Let's say
26015 that the process PID for @code{main.exe} is 208.
26016
26017 @item
26018 Launch gdb.
26019
26020 @example
26021 $ gdb
26022 @end example
26023
26024 @item
26025 Attach to the running process to be debugged.
26026
26027 @example
26028 (gdb) attach 208
26029 @end example
26030
26031 @item
26032 Load the process debugging information.
26033
26034 @example
26035 (gdb) symbol-file main.exe
26036 @end example
26037
26038 @item
26039 Break somewhere in the DLL.
26040
26041 @example
26042 (gdb) break ada_dll
26043 @end example
26044
26045 @item
26046 Continue process execution.
26047
26048 @example
26049 (gdb) cont
26050 @end example
26051 @end itemize
26052
26053 This last step will resume the process execution, and stop at
26054 the breakpoint we have set. From there you can use the standard
26055 approach to debug a program as described in
26056 @ref{24,,Running and Debugging Ada Programs}.
26057
26058 @node Setting Stack Size from gnatlink,Setting Heap Size from gnatlink,Debugging a DLL,Mixed-Language Programming on Windows
26059 @anchor{gnat_ugn/platform_specific_information setting-stack-size-from-gnatlink}@anchor{136}@anchor{gnat_ugn/platform_specific_information id39}@anchor{21e}
26060 @subsubsection Setting Stack Size from @code{gnatlink}
26061
26062
26063 It is possible to specify the program stack size at link time. On modern
26064 versions of Windows, starting with XP, this is mostly useful to set the size of
26065 the main stack (environment task). The other task stacks are set with pragma
26066 Storage_Size or with the @emph{gnatbind -d} command.
26067
26068 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
26069 reserve size of individual tasks, the link-time stack size applies to all
26070 tasks, and pragma Storage_Size has no effect.
26071 In particular, Stack Overflow checks are made against this
26072 link-time specified size.
26073
26074 This setting can be done with @code{gnatlink} using either of the following:
26075
26076
26077 @itemize *
26078
26079 @item
26080 @code{-Xlinker} linker option
26081
26082 @example
26083 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
26084 @end example
26085
26086 This sets the stack reserve size to 0x10000 bytes and the stack commit
26087 size to 0x1000 bytes.
26088
26089 @item
26090 @code{-Wl} linker option
26091
26092 @example
26093 $ gnatlink hello -Wl,--stack=0x1000000
26094 @end example
26095
26096 This sets the stack reserve size to 0x1000000 bytes. Note that with
26097 @code{-Wl} option it is not possible to set the stack commit size
26098 because the comma is a separator for this option.
26099 @end itemize
26100
26101 @node Setting Heap Size from gnatlink,,Setting Stack Size from gnatlink,Mixed-Language Programming on Windows
26102 @anchor{gnat_ugn/platform_specific_information setting-heap-size-from-gnatlink}@anchor{137}@anchor{gnat_ugn/platform_specific_information id40}@anchor{21f}
26103 @subsubsection Setting Heap Size from @code{gnatlink}
26104
26105
26106 Under Windows systems, it is possible to specify the program heap size from
26107 @code{gnatlink} using either of the following:
26108
26109
26110 @itemize *
26111
26112 @item
26113 @code{-Xlinker} linker option
26114
26115 @example
26116 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
26117 @end example
26118
26119 This sets the heap reserve size to 0x10000 bytes and the heap commit
26120 size to 0x1000 bytes.
26121
26122 @item
26123 @code{-Wl} linker option
26124
26125 @example
26126 $ gnatlink hello -Wl,--heap=0x1000000
26127 @end example
26128
26129 This sets the heap reserve size to 0x1000000 bytes. Note that with
26130 @code{-Wl} option it is not possible to set the heap commit size
26131 because the comma is a separator for this option.
26132 @end itemize
26133
26134 @node Windows Specific Add-Ons,,Mixed-Language Programming on Windows,Microsoft Windows Topics
26135 @anchor{gnat_ugn/platform_specific_information windows-specific-add-ons}@anchor{220}@anchor{gnat_ugn/platform_specific_information win32-specific-addons}@anchor{221}
26136 @subsection Windows Specific Add-Ons
26137
26138
26139 This section describes the Windows specific add-ons.
26140
26141 @menu
26142 * Win32Ada::
26143 * wPOSIX::
26144
26145 @end menu
26146
26147 @node Win32Ada,wPOSIX,,Windows Specific Add-Ons
26148 @anchor{gnat_ugn/platform_specific_information win32ada}@anchor{222}@anchor{gnat_ugn/platform_specific_information id41}@anchor{223}
26149 @subsubsection Win32Ada
26150
26151
26152 Win32Ada is a binding for the Microsoft Win32 API. This binding can be
26153 easily installed from the provided installer. To use the Win32Ada
26154 binding you need to use a project file, and adding a single with_clause
26155 will give you full access to the Win32Ada binding sources and ensure
26156 that the proper libraries are passed to the linker.
26157
26158 @quotation
26159
26160 @example
26161 with "win32ada";
26162 project P is
26163 for Sources use ...;
26164 end P;
26165 @end example
26166 @end quotation
26167
26168 To build the application you just need to call gprbuild for the
26169 application's project, here p.gpr:
26170
26171 @quotation
26172
26173 @example
26174 gprbuild p.gpr
26175 @end example
26176 @end quotation
26177
26178 @node wPOSIX,,Win32Ada,Windows Specific Add-Ons
26179 @anchor{gnat_ugn/platform_specific_information id42}@anchor{224}@anchor{gnat_ugn/platform_specific_information wposix}@anchor{225}
26180 @subsubsection wPOSIX
26181
26182
26183 wPOSIX is a minimal POSIX binding whose goal is to help with building
26184 cross-platforms applications. This binding is not complete though, as
26185 the Win32 API does not provide the necessary support for all POSIX APIs.
26186
26187 To use the wPOSIX binding you need to use a project file, and adding
26188 a single with_clause will give you full access to the wPOSIX binding
26189 sources and ensure that the proper libraries are passed to the linker.
26190
26191 @quotation
26192
26193 @example
26194 with "wposix";
26195 project P is
26196 for Sources use ...;
26197 end P;
26198 @end example
26199 @end quotation
26200
26201 To build the application you just need to call gprbuild for the
26202 application's project, here p.gpr:
26203
26204 @quotation
26205
26206 @example
26207 gprbuild p.gpr
26208 @end example
26209 @end quotation
26210
26211 @node Mac OS Topics,,Microsoft Windows Topics,Platform-Specific Information
26212 @anchor{gnat_ugn/platform_specific_information mac-os-topics}@anchor{2d}@anchor{gnat_ugn/platform_specific_information id43}@anchor{226}
26213 @section Mac OS Topics
26214
26215
26216 @geindex OS X
26217
26218 This section describes topics that are specific to Apple's OS X
26219 platform.
26220
26221 @menu
26222 * Codesigning the Debugger::
26223
26224 @end menu
26225
26226 @node Codesigning the Debugger,,,Mac OS Topics
26227 @anchor{gnat_ugn/platform_specific_information codesigning-the-debugger}@anchor{227}
26228 @subsection Codesigning the Debugger
26229
26230
26231 The Darwin Kernel requires the debugger to have special permissions
26232 before it is allowed to control other processes. These permissions
26233 are granted by codesigning the GDB executable. Without these
26234 permissions, the debugger will report error messages such as:
26235
26236 @example
26237 Starting program: /x/y/foo
26238 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
26239 (please check gdb is codesigned - see taskgated(8))
26240 @end example
26241
26242 Codesigning requires a certificate. The following procedure explains
26243 how to create one:
26244
26245
26246 @itemize *
26247
26248 @item
26249 Start the Keychain Access application (in
26250 /Applications/Utilities/Keychain Access.app)
26251
26252 @item
26253 Select the Keychain Access -> Certificate Assistant ->
26254 Create a Certificate... menu
26255
26256 @item
26257 Then:
26258
26259
26260 @itemize *
26261
26262 @item
26263 Choose a name for the new certificate (this procedure will use
26264 "gdb-cert" as an example)
26265
26266 @item
26267 Set "Identity Type" to "Self Signed Root"
26268
26269 @item
26270 Set "Certificate Type" to "Code Signing"
26271
26272 @item
26273 Activate the "Let me override defaults" option
26274 @end itemize
26275
26276 @item
26277 Click several times on "Continue" until the "Specify a Location
26278 For The Certificate" screen appears, then set "Keychain" to "System"
26279
26280 @item
26281 Click on "Continue" until the certificate is created
26282
26283 @item
26284 Finally, in the view, double-click on the new certificate,
26285 and set "When using this certificate" to "Always Trust"
26286
26287 @item
26288 Exit the Keychain Access application and restart the computer
26289 (this is unfortunately required)
26290 @end itemize
26291
26292 Once a certificate has been created, the debugger can be codesigned
26293 as follow. In a Terminal, run the following command:
26294
26295 @quotation
26296
26297 @example
26298 $ codesign -f -s "gdb-cert" <gnat_install_prefix>/bin/gdb
26299 @end example
26300 @end quotation
26301
26302 where "gdb-cert" should be replaced by the actual certificate
26303 name chosen above, and <gnat_install_prefix> should be replaced by
26304 the location where you installed GNAT. Also, be sure that users are
26305 in the Unix group @code{_developer}.
26306
26307 @node Example of Binder Output File,Elaboration Order Handling in GNAT,Platform-Specific Information,Top
26308 @anchor{gnat_ugn/example_of_binder_output example-of-binder-output-file}@anchor{e}@anchor{gnat_ugn/example_of_binder_output doc}@anchor{228}@anchor{gnat_ugn/example_of_binder_output id1}@anchor{229}
26309 @chapter Example of Binder Output File
26310
26311
26312 @geindex Binder output (example)
26313
26314 This Appendix displays the source code for the output file
26315 generated by @emph{gnatbind} for a simple 'Hello World' program.
26316 Comments have been added for clarification purposes.
26317
26318 @example
26319 -- The package is called Ada_Main unless this name is actually used
26320 -- as a unit name in the partition, in which case some other unique
26321 -- name is used.
26322
26323 pragma Ada_95;
26324 with System;
26325 package ada_main is
26326 pragma Warnings (Off);
26327
26328 -- The main program saves the parameters (argument count,
26329 -- argument values, environment pointer) in global variables
26330 -- for later access by other units including
26331 -- Ada.Command_Line.
26332
26333 gnat_argc : Integer;
26334 gnat_argv : System.Address;
26335 gnat_envp : System.Address;
26336
26337 -- The actual variables are stored in a library routine. This
26338 -- is useful for some shared library situations, where there
26339 -- are problems if variables are not in the library.
26340
26341 pragma Import (C, gnat_argc);
26342 pragma Import (C, gnat_argv);
26343 pragma Import (C, gnat_envp);
26344
26345 -- The exit status is similarly an external location
26346
26347 gnat_exit_status : Integer;
26348 pragma Import (C, gnat_exit_status);
26349
26350 GNAT_Version : constant String :=
26351 "GNAT Version: Pro 7.4.0w (20141119-49)" & ASCII.NUL;
26352 pragma Export (C, GNAT_Version, "__gnat_version");
26353
26354 Ada_Main_Program_Name : constant String := "_ada_hello" & ASCII.NUL;
26355 pragma Export (C, Ada_Main_Program_Name, "__gnat_ada_main_program_name");
26356
26357 -- This is the generated adainit routine that performs
26358 -- initialization at the start of execution. In the case
26359 -- where Ada is the main program, this main program makes
26360 -- a call to adainit at program startup.
26361
26362 procedure adainit;
26363 pragma Export (C, adainit, "adainit");
26364
26365 -- This is the generated adafinal routine that performs
26366 -- finalization at the end of execution. In the case where
26367 -- Ada is the main program, this main program makes a call
26368 -- to adafinal at program termination.
26369
26370 procedure adafinal;
26371 pragma Export (C, adafinal, "adafinal");
26372
26373 -- This routine is called at the start of execution. It is
26374 -- a dummy routine that is used by the debugger to breakpoint
26375 -- at the start of execution.
26376
26377 -- This is the actual generated main program (it would be
26378 -- suppressed if the no main program switch were used). As
26379 -- required by standard system conventions, this program has
26380 -- the external name main.
26381
26382 function main
26383 (argc : Integer;
26384 argv : System.Address;
26385 envp : System.Address)
26386 return Integer;
26387 pragma Export (C, main, "main");
26388
26389 -- The following set of constants give the version
26390 -- identification values for every unit in the bound
26391 -- partition. This identification is computed from all
26392 -- dependent semantic units, and corresponds to the
26393 -- string that would be returned by use of the
26394 -- Body_Version or Version attributes.
26395
26396 -- The following Export pragmas export the version numbers
26397 -- with symbolic names ending in B (for body) or S
26398 -- (for spec) so that they can be located in a link. The
26399 -- information provided here is sufficient to track down
26400 -- the exact versions of units used in a given build.
26401
26402 type Version_32 is mod 2 ** 32;
26403 u00001 : constant Version_32 := 16#8ad6e54a#;
26404 pragma Export (C, u00001, "helloB");
26405 u00002 : constant Version_32 := 16#fbff4c67#;
26406 pragma Export (C, u00002, "system__standard_libraryB");
26407 u00003 : constant Version_32 := 16#1ec6fd90#;
26408 pragma Export (C, u00003, "system__standard_libraryS");
26409 u00004 : constant Version_32 := 16#3ffc8e18#;
26410 pragma Export (C, u00004, "adaS");
26411 u00005 : constant Version_32 := 16#28f088c2#;
26412 pragma Export (C, u00005, "ada__text_ioB");
26413 u00006 : constant Version_32 := 16#f372c8ac#;
26414 pragma Export (C, u00006, "ada__text_ioS");
26415 u00007 : constant Version_32 := 16#2c143749#;
26416 pragma Export (C, u00007, "ada__exceptionsB");
26417 u00008 : constant Version_32 := 16#f4f0cce8#;
26418 pragma Export (C, u00008, "ada__exceptionsS");
26419 u00009 : constant Version_32 := 16#a46739c0#;
26420 pragma Export (C, u00009, "ada__exceptions__last_chance_handlerB");
26421 u00010 : constant Version_32 := 16#3aac8c92#;
26422 pragma Export (C, u00010, "ada__exceptions__last_chance_handlerS");
26423 u00011 : constant Version_32 := 16#1d274481#;
26424 pragma Export (C, u00011, "systemS");
26425 u00012 : constant Version_32 := 16#a207fefe#;
26426 pragma Export (C, u00012, "system__soft_linksB");
26427 u00013 : constant Version_32 := 16#467d9556#;
26428 pragma Export (C, u00013, "system__soft_linksS");
26429 u00014 : constant Version_32 := 16#b01dad17#;
26430 pragma Export (C, u00014, "system__parametersB");
26431 u00015 : constant Version_32 := 16#630d49fe#;
26432 pragma Export (C, u00015, "system__parametersS");
26433 u00016 : constant Version_32 := 16#b19b6653#;
26434 pragma Export (C, u00016, "system__secondary_stackB");
26435 u00017 : constant Version_32 := 16#b6468be8#;
26436 pragma Export (C, u00017, "system__secondary_stackS");
26437 u00018 : constant Version_32 := 16#39a03df9#;
26438 pragma Export (C, u00018, "system__storage_elementsB");
26439 u00019 : constant Version_32 := 16#30e40e85#;
26440 pragma Export (C, u00019, "system__storage_elementsS");
26441 u00020 : constant Version_32 := 16#41837d1e#;
26442 pragma Export (C, u00020, "system__stack_checkingB");
26443 u00021 : constant Version_32 := 16#93982f69#;
26444 pragma Export (C, u00021, "system__stack_checkingS");
26445 u00022 : constant Version_32 := 16#393398c1#;
26446 pragma Export (C, u00022, "system__exception_tableB");
26447 u00023 : constant Version_32 := 16#b33e2294#;
26448 pragma Export (C, u00023, "system__exception_tableS");
26449 u00024 : constant Version_32 := 16#ce4af020#;
26450 pragma Export (C, u00024, "system__exceptionsB");
26451 u00025 : constant Version_32 := 16#75442977#;
26452 pragma Export (C, u00025, "system__exceptionsS");
26453 u00026 : constant Version_32 := 16#37d758f1#;
26454 pragma Export (C, u00026, "system__exceptions__machineS");
26455 u00027 : constant Version_32 := 16#b895431d#;
26456 pragma Export (C, u00027, "system__exceptions_debugB");
26457 u00028 : constant Version_32 := 16#aec55d3f#;
26458 pragma Export (C, u00028, "system__exceptions_debugS");
26459 u00029 : constant Version_32 := 16#570325c8#;
26460 pragma Export (C, u00029, "system__img_intB");
26461 u00030 : constant Version_32 := 16#1ffca443#;
26462 pragma Export (C, u00030, "system__img_intS");
26463 u00031 : constant Version_32 := 16#b98c3e16#;
26464 pragma Export (C, u00031, "system__tracebackB");
26465 u00032 : constant Version_32 := 16#831a9d5a#;
26466 pragma Export (C, u00032, "system__tracebackS");
26467 u00033 : constant Version_32 := 16#9ed49525#;
26468 pragma Export (C, u00033, "system__traceback_entriesB");
26469 u00034 : constant Version_32 := 16#1d7cb2f1#;
26470 pragma Export (C, u00034, "system__traceback_entriesS");
26471 u00035 : constant Version_32 := 16#8c33a517#;
26472 pragma Export (C, u00035, "system__wch_conB");
26473 u00036 : constant Version_32 := 16#065a6653#;
26474 pragma Export (C, u00036, "system__wch_conS");
26475 u00037 : constant Version_32 := 16#9721e840#;
26476 pragma Export (C, u00037, "system__wch_stwB");
26477 u00038 : constant Version_32 := 16#2b4b4a52#;
26478 pragma Export (C, u00038, "system__wch_stwS");
26479 u00039 : constant Version_32 := 16#92b797cb#;
26480 pragma Export (C, u00039, "system__wch_cnvB");
26481 u00040 : constant Version_32 := 16#09eddca0#;
26482 pragma Export (C, u00040, "system__wch_cnvS");
26483 u00041 : constant Version_32 := 16#6033a23f#;
26484 pragma Export (C, u00041, "interfacesS");
26485 u00042 : constant Version_32 := 16#ece6fdb6#;
26486 pragma Export (C, u00042, "system__wch_jisB");
26487 u00043 : constant Version_32 := 16#899dc581#;
26488 pragma Export (C, u00043, "system__wch_jisS");
26489 u00044 : constant Version_32 := 16#10558b11#;
26490 pragma Export (C, u00044, "ada__streamsB");
26491 u00045 : constant Version_32 := 16#2e6701ab#;
26492 pragma Export (C, u00045, "ada__streamsS");
26493 u00046 : constant Version_32 := 16#db5c917c#;
26494 pragma Export (C, u00046, "ada__io_exceptionsS");
26495 u00047 : constant Version_32 := 16#12c8cd7d#;
26496 pragma Export (C, u00047, "ada__tagsB");
26497 u00048 : constant Version_32 := 16#ce72c228#;
26498 pragma Export (C, u00048, "ada__tagsS");
26499 u00049 : constant Version_32 := 16#c3335bfd#;
26500 pragma Export (C, u00049, "system__htableB");
26501 u00050 : constant Version_32 := 16#99e5f76b#;
26502 pragma Export (C, u00050, "system__htableS");
26503 u00051 : constant Version_32 := 16#089f5cd0#;
26504 pragma Export (C, u00051, "system__string_hashB");
26505 u00052 : constant Version_32 := 16#3bbb9c15#;
26506 pragma Export (C, u00052, "system__string_hashS");
26507 u00053 : constant Version_32 := 16#807fe041#;
26508 pragma Export (C, u00053, "system__unsigned_typesS");
26509 u00054 : constant Version_32 := 16#d27be59e#;
26510 pragma Export (C, u00054, "system__val_lluB");
26511 u00055 : constant Version_32 := 16#fa8db733#;
26512 pragma Export (C, u00055, "system__val_lluS");
26513 u00056 : constant Version_32 := 16#27b600b2#;
26514 pragma Export (C, u00056, "system__val_utilB");
26515 u00057 : constant Version_32 := 16#b187f27f#;
26516 pragma Export (C, u00057, "system__val_utilS");
26517 u00058 : constant Version_32 := 16#d1060688#;
26518 pragma Export (C, u00058, "system__case_utilB");
26519 u00059 : constant Version_32 := 16#392e2d56#;
26520 pragma Export (C, u00059, "system__case_utilS");
26521 u00060 : constant Version_32 := 16#84a27f0d#;
26522 pragma Export (C, u00060, "interfaces__c_streamsB");
26523 u00061 : constant Version_32 := 16#8bb5f2c0#;
26524 pragma Export (C, u00061, "interfaces__c_streamsS");
26525 u00062 : constant Version_32 := 16#6db6928f#;
26526 pragma Export (C, u00062, "system__crtlS");
26527 u00063 : constant Version_32 := 16#4e6a342b#;
26528 pragma Export (C, u00063, "system__file_ioB");
26529 u00064 : constant Version_32 := 16#ba56a5e4#;
26530 pragma Export (C, u00064, "system__file_ioS");
26531 u00065 : constant Version_32 := 16#b7ab275c#;
26532 pragma Export (C, u00065, "ada__finalizationB");
26533 u00066 : constant Version_32 := 16#19f764ca#;
26534 pragma Export (C, u00066, "ada__finalizationS");
26535 u00067 : constant Version_32 := 16#95817ed8#;
26536 pragma Export (C, u00067, "system__finalization_rootB");
26537 u00068 : constant Version_32 := 16#52d53711#;
26538 pragma Export (C, u00068, "system__finalization_rootS");
26539 u00069 : constant Version_32 := 16#769e25e6#;
26540 pragma Export (C, u00069, "interfaces__cB");
26541 u00070 : constant Version_32 := 16#4a38bedb#;
26542 pragma Export (C, u00070, "interfaces__cS");
26543 u00071 : constant Version_32 := 16#07e6ee66#;
26544 pragma Export (C, u00071, "system__os_libB");
26545 u00072 : constant Version_32 := 16#d7b69782#;
26546 pragma Export (C, u00072, "system__os_libS");
26547 u00073 : constant Version_32 := 16#1a817b8e#;
26548 pragma Export (C, u00073, "system__stringsB");
26549 u00074 : constant Version_32 := 16#639855e7#;
26550 pragma Export (C, u00074, "system__stringsS");
26551 u00075 : constant Version_32 := 16#e0b8de29#;
26552 pragma Export (C, u00075, "system__file_control_blockS");
26553 u00076 : constant Version_32 := 16#b5b2aca1#;
26554 pragma Export (C, u00076, "system__finalization_mastersB");
26555 u00077 : constant Version_32 := 16#69316dc1#;
26556 pragma Export (C, u00077, "system__finalization_mastersS");
26557 u00078 : constant Version_32 := 16#57a37a42#;
26558 pragma Export (C, u00078, "system__address_imageB");
26559 u00079 : constant Version_32 := 16#bccbd9bb#;
26560 pragma Export (C, u00079, "system__address_imageS");
26561 u00080 : constant Version_32 := 16#7268f812#;
26562 pragma Export (C, u00080, "system__img_boolB");
26563 u00081 : constant Version_32 := 16#e8fe356a#;
26564 pragma Export (C, u00081, "system__img_boolS");
26565 u00082 : constant Version_32 := 16#d7aac20c#;
26566 pragma Export (C, u00082, "system__ioB");
26567 u00083 : constant Version_32 := 16#8365b3ce#;
26568 pragma Export (C, u00083, "system__ioS");
26569 u00084 : constant Version_32 := 16#6d4d969a#;
26570 pragma Export (C, u00084, "system__storage_poolsB");
26571 u00085 : constant Version_32 := 16#e87cc305#;
26572 pragma Export (C, u00085, "system__storage_poolsS");
26573 u00086 : constant Version_32 := 16#e34550ca#;
26574 pragma Export (C, u00086, "system__pool_globalB");
26575 u00087 : constant Version_32 := 16#c88d2d16#;
26576 pragma Export (C, u00087, "system__pool_globalS");
26577 u00088 : constant Version_32 := 16#9d39c675#;
26578 pragma Export (C, u00088, "system__memoryB");
26579 u00089 : constant Version_32 := 16#445a22b5#;
26580 pragma Export (C, u00089, "system__memoryS");
26581 u00090 : constant Version_32 := 16#6a859064#;
26582 pragma Export (C, u00090, "system__storage_pools__subpoolsB");
26583 u00091 : constant Version_32 := 16#e3b008dc#;
26584 pragma Export (C, u00091, "system__storage_pools__subpoolsS");
26585 u00092 : constant Version_32 := 16#63f11652#;
26586 pragma Export (C, u00092, "system__storage_pools__subpools__finalizationB");
26587 u00093 : constant Version_32 := 16#fe2f4b3a#;
26588 pragma Export (C, u00093, "system__storage_pools__subpools__finalizationS");
26589
26590 -- BEGIN ELABORATION ORDER
26591 -- ada%s
26592 -- interfaces%s
26593 -- system%s
26594 -- system.case_util%s
26595 -- system.case_util%b
26596 -- system.htable%s
26597 -- system.img_bool%s
26598 -- system.img_bool%b
26599 -- system.img_int%s
26600 -- system.img_int%b
26601 -- system.io%s
26602 -- system.io%b
26603 -- system.parameters%s
26604 -- system.parameters%b
26605 -- system.crtl%s
26606 -- interfaces.c_streams%s
26607 -- interfaces.c_streams%b
26608 -- system.standard_library%s
26609 -- system.exceptions_debug%s
26610 -- system.exceptions_debug%b
26611 -- system.storage_elements%s
26612 -- system.storage_elements%b
26613 -- system.stack_checking%s
26614 -- system.stack_checking%b
26615 -- system.string_hash%s
26616 -- system.string_hash%b
26617 -- system.htable%b
26618 -- system.strings%s
26619 -- system.strings%b
26620 -- system.os_lib%s
26621 -- system.traceback_entries%s
26622 -- system.traceback_entries%b
26623 -- ada.exceptions%s
26624 -- system.soft_links%s
26625 -- system.unsigned_types%s
26626 -- system.val_llu%s
26627 -- system.val_util%s
26628 -- system.val_util%b
26629 -- system.val_llu%b
26630 -- system.wch_con%s
26631 -- system.wch_con%b
26632 -- system.wch_cnv%s
26633 -- system.wch_jis%s
26634 -- system.wch_jis%b
26635 -- system.wch_cnv%b
26636 -- system.wch_stw%s
26637 -- system.wch_stw%b
26638 -- ada.exceptions.last_chance_handler%s
26639 -- ada.exceptions.last_chance_handler%b
26640 -- system.address_image%s
26641 -- system.exception_table%s
26642 -- system.exception_table%b
26643 -- ada.io_exceptions%s
26644 -- ada.tags%s
26645 -- ada.streams%s
26646 -- ada.streams%b
26647 -- interfaces.c%s
26648 -- system.exceptions%s
26649 -- system.exceptions%b
26650 -- system.exceptions.machine%s
26651 -- system.finalization_root%s
26652 -- system.finalization_root%b
26653 -- ada.finalization%s
26654 -- ada.finalization%b
26655 -- system.storage_pools%s
26656 -- system.storage_pools%b
26657 -- system.finalization_masters%s
26658 -- system.storage_pools.subpools%s
26659 -- system.storage_pools.subpools.finalization%s
26660 -- system.storage_pools.subpools.finalization%b
26661 -- system.memory%s
26662 -- system.memory%b
26663 -- system.standard_library%b
26664 -- system.pool_global%s
26665 -- system.pool_global%b
26666 -- system.file_control_block%s
26667 -- system.file_io%s
26668 -- system.secondary_stack%s
26669 -- system.file_io%b
26670 -- system.storage_pools.subpools%b
26671 -- system.finalization_masters%b
26672 -- interfaces.c%b
26673 -- ada.tags%b
26674 -- system.soft_links%b
26675 -- system.os_lib%b
26676 -- system.secondary_stack%b
26677 -- system.address_image%b
26678 -- system.traceback%s
26679 -- ada.exceptions%b
26680 -- system.traceback%b
26681 -- ada.text_io%s
26682 -- ada.text_io%b
26683 -- hello%b
26684 -- END ELABORATION ORDER
26685
26686 end ada_main;
26687 @end example
26688
26689 @example
26690 pragma Ada_95;
26691 -- The following source file name pragmas allow the generated file
26692 -- names to be unique for different main programs. They are needed
26693 -- since the package name will always be Ada_Main.
26694
26695 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26696 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26697
26698 pragma Suppress (Overflow_Check);
26699 with Ada.Exceptions;
26700
26701 -- Generated package body for Ada_Main starts here
26702
26703 package body ada_main is
26704 pragma Warnings (Off);
26705
26706 -- These values are reference counter associated to units which have
26707 -- been elaborated. It is also used to avoid elaborating the
26708 -- same unit twice.
26709
26710 E72 : Short_Integer; pragma Import (Ada, E72, "system__os_lib_E");
26711 E13 : Short_Integer; pragma Import (Ada, E13, "system__soft_links_E");
26712 E23 : Short_Integer; pragma Import (Ada, E23, "system__exception_table_E");
26713 E46 : Short_Integer; pragma Import (Ada, E46, "ada__io_exceptions_E");
26714 E48 : Short_Integer; pragma Import (Ada, E48, "ada__tags_E");
26715 E45 : Short_Integer; pragma Import (Ada, E45, "ada__streams_E");
26716 E70 : Short_Integer; pragma Import (Ada, E70, "interfaces__c_E");
26717 E25 : Short_Integer; pragma Import (Ada, E25, "system__exceptions_E");
26718 E68 : Short_Integer; pragma Import (Ada, E68, "system__finalization_root_E");
26719 E66 : Short_Integer; pragma Import (Ada, E66, "ada__finalization_E");
26720 E85 : Short_Integer; pragma Import (Ada, E85, "system__storage_pools_E");
26721 E77 : Short_Integer; pragma Import (Ada, E77, "system__finalization_masters_E");
26722 E91 : Short_Integer; pragma Import (Ada, E91, "system__storage_pools__subpools_E");
26723 E87 : Short_Integer; pragma Import (Ada, E87, "system__pool_global_E");
26724 E75 : Short_Integer; pragma Import (Ada, E75, "system__file_control_block_E");
26725 E64 : Short_Integer; pragma Import (Ada, E64, "system__file_io_E");
26726 E17 : Short_Integer; pragma Import (Ada, E17, "system__secondary_stack_E");
26727 E06 : Short_Integer; pragma Import (Ada, E06, "ada__text_io_E");
26728
26729 Local_Priority_Specific_Dispatching : constant String := "";
26730 Local_Interrupt_States : constant String := "";
26731
26732 Is_Elaborated : Boolean := False;
26733
26734 procedure finalize_library is
26735 begin
26736 E06 := E06 - 1;
26737 declare
26738 procedure F1;
26739 pragma Import (Ada, F1, "ada__text_io__finalize_spec");
26740 begin
26741 F1;
26742 end;
26743 E77 := E77 - 1;
26744 E91 := E91 - 1;
26745 declare
26746 procedure F2;
26747 pragma Import (Ada, F2, "system__file_io__finalize_body");
26748 begin
26749 E64 := E64 - 1;
26750 F2;
26751 end;
26752 declare
26753 procedure F3;
26754 pragma Import (Ada, F3, "system__file_control_block__finalize_spec");
26755 begin
26756 E75 := E75 - 1;
26757 F3;
26758 end;
26759 E87 := E87 - 1;
26760 declare
26761 procedure F4;
26762 pragma Import (Ada, F4, "system__pool_global__finalize_spec");
26763 begin
26764 F4;
26765 end;
26766 declare
26767 procedure F5;
26768 pragma Import (Ada, F5, "system__storage_pools__subpools__finalize_spec");
26769 begin
26770 F5;
26771 end;
26772 declare
26773 procedure F6;
26774 pragma Import (Ada, F6, "system__finalization_masters__finalize_spec");
26775 begin
26776 F6;
26777 end;
26778 declare
26779 procedure Reraise_Library_Exception_If_Any;
26780 pragma Import (Ada, Reraise_Library_Exception_If_Any, "__gnat_reraise_library_exception_if_any");
26781 begin
26782 Reraise_Library_Exception_If_Any;
26783 end;
26784 end finalize_library;
26785
26786 -------------
26787 -- adainit --
26788 -------------
26789
26790 procedure adainit is
26791
26792 Main_Priority : Integer;
26793 pragma Import (C, Main_Priority, "__gl_main_priority");
26794 Time_Slice_Value : Integer;
26795 pragma Import (C, Time_Slice_Value, "__gl_time_slice_val");
26796 WC_Encoding : Character;
26797 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
26798 Locking_Policy : Character;
26799 pragma Import (C, Locking_Policy, "__gl_locking_policy");
26800 Queuing_Policy : Character;
26801 pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
26802 Task_Dispatching_Policy : Character;
26803 pragma Import (C, Task_Dispatching_Policy, "__gl_task_dispatching_policy");
26804 Priority_Specific_Dispatching : System.Address;
26805 pragma Import (C, Priority_Specific_Dispatching, "__gl_priority_specific_dispatching");
26806 Num_Specific_Dispatching : Integer;
26807 pragma Import (C, Num_Specific_Dispatching, "__gl_num_specific_dispatching");
26808 Main_CPU : Integer;
26809 pragma Import (C, Main_CPU, "__gl_main_cpu");
26810 Interrupt_States : System.Address;
26811 pragma Import (C, Interrupt_States, "__gl_interrupt_states");
26812 Num_Interrupt_States : Integer;
26813 pragma Import (C, Num_Interrupt_States, "__gl_num_interrupt_states");
26814 Unreserve_All_Interrupts : Integer;
26815 pragma Import (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
26816 Detect_Blocking : Integer;
26817 pragma Import (C, Detect_Blocking, "__gl_detect_blocking");
26818 Default_Stack_Size : Integer;
26819 pragma Import (C, Default_Stack_Size, "__gl_default_stack_size");
26820 Leap_Seconds_Support : Integer;
26821 pragma Import (C, Leap_Seconds_Support, "__gl_leap_seconds_support");
26822
26823 procedure Runtime_Initialize;
26824 pragma Import (C, Runtime_Initialize, "__gnat_runtime_initialize");
26825
26826 Finalize_Library_Objects : No_Param_Proc;
26827 pragma Import (C, Finalize_Library_Objects, "__gnat_finalize_library_objects");
26828
26829 -- Start of processing for adainit
26830
26831 begin
26832
26833 -- Record various information for this partition. The values
26834 -- are derived by the binder from information stored in the ali
26835 -- files by the compiler.
26836
26837 if Is_Elaborated then
26838 return;
26839 end if;
26840 Is_Elaborated := True;
26841 Main_Priority := -1;
26842 Time_Slice_Value := -1;
26843 WC_Encoding := 'b';
26844 Locking_Policy := ' ';
26845 Queuing_Policy := ' ';
26846 Task_Dispatching_Policy := ' ';
26847 Priority_Specific_Dispatching :=
26848 Local_Priority_Specific_Dispatching'Address;
26849 Num_Specific_Dispatching := 0;
26850 Main_CPU := -1;
26851 Interrupt_States := Local_Interrupt_States'Address;
26852 Num_Interrupt_States := 0;
26853 Unreserve_All_Interrupts := 0;
26854 Detect_Blocking := 0;
26855 Default_Stack_Size := -1;
26856 Leap_Seconds_Support := 0;
26857
26858 Runtime_Initialize;
26859
26860 Finalize_Library_Objects := finalize_library'access;
26861
26862 -- Now we have the elaboration calls for all units in the partition.
26863 -- The Elab_Spec and Elab_Body attributes generate references to the
26864 -- implicit elaboration procedures generated by the compiler for
26865 -- each unit that requires elaboration. Increment a counter of
26866 -- reference for each unit.
26867
26868 System.Soft_Links'Elab_Spec;
26869 System.Exception_Table'Elab_Body;
26870 E23 := E23 + 1;
26871 Ada.Io_Exceptions'Elab_Spec;
26872 E46 := E46 + 1;
26873 Ada.Tags'Elab_Spec;
26874 Ada.Streams'Elab_Spec;
26875 E45 := E45 + 1;
26876 Interfaces.C'Elab_Spec;
26877 System.Exceptions'Elab_Spec;
26878 E25 := E25 + 1;
26879 System.Finalization_Root'Elab_Spec;
26880 E68 := E68 + 1;
26881 Ada.Finalization'Elab_Spec;
26882 E66 := E66 + 1;
26883 System.Storage_Pools'Elab_Spec;
26884 E85 := E85 + 1;
26885 System.Finalization_Masters'Elab_Spec;
26886 System.Storage_Pools.Subpools'Elab_Spec;
26887 System.Pool_Global'Elab_Spec;
26888 E87 := E87 + 1;
26889 System.File_Control_Block'Elab_Spec;
26890 E75 := E75 + 1;
26891 System.File_Io'Elab_Body;
26892 E64 := E64 + 1;
26893 E91 := E91 + 1;
26894 System.Finalization_Masters'Elab_Body;
26895 E77 := E77 + 1;
26896 E70 := E70 + 1;
26897 Ada.Tags'Elab_Body;
26898 E48 := E48 + 1;
26899 System.Soft_Links'Elab_Body;
26900 E13 := E13 + 1;
26901 System.Os_Lib'Elab_Body;
26902 E72 := E72 + 1;
26903 System.Secondary_Stack'Elab_Body;
26904 E17 := E17 + 1;
26905 Ada.Text_Io'Elab_Spec;
26906 Ada.Text_Io'Elab_Body;
26907 E06 := E06 + 1;
26908 end adainit;
26909
26910 --------------
26911 -- adafinal --
26912 --------------
26913
26914 procedure adafinal is
26915 procedure s_stalib_adafinal;
26916 pragma Import (C, s_stalib_adafinal, "system__standard_library__adafinal");
26917
26918 procedure Runtime_Finalize;
26919 pragma Import (C, Runtime_Finalize, "__gnat_runtime_finalize");
26920
26921 begin
26922 if not Is_Elaborated then
26923 return;
26924 end if;
26925 Is_Elaborated := False;
26926 Runtime_Finalize;
26927 s_stalib_adafinal;
26928 end adafinal;
26929
26930 -- We get to the main program of the partition by using
26931 -- pragma Import because if we try to with the unit and
26932 -- call it Ada style, then not only do we waste time
26933 -- recompiling it, but also, we don't really know the right
26934 -- switches (e.g.@@: identifier character set) to be used
26935 -- to compile it.
26936
26937 procedure Ada_Main_Program;
26938 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
26939
26940 ----------
26941 -- main --
26942 ----------
26943
26944 -- main is actually a function, as in the ANSI C standard,
26945 -- defined to return the exit status. The three parameters
26946 -- are the argument count, argument values and environment
26947 -- pointer.
26948
26949 function main
26950 (argc : Integer;
26951 argv : System.Address;
26952 envp : System.Address)
26953 return Integer
26954 is
26955 -- The initialize routine performs low level system
26956 -- initialization using a standard library routine which
26957 -- sets up signal handling and performs any other
26958 -- required setup. The routine can be found in file
26959 -- a-init.c.
26960
26961 procedure initialize;
26962 pragma Import (C, initialize, "__gnat_initialize");
26963
26964 -- The finalize routine performs low level system
26965 -- finalization using a standard library routine. The
26966 -- routine is found in file a-final.c and in the standard
26967 -- distribution is a dummy routine that does nothing, so
26968 -- really this is a hook for special user finalization.
26969
26970 procedure finalize;
26971 pragma Import (C, finalize, "__gnat_finalize");
26972
26973 -- The following is to initialize the SEH exceptions
26974
26975 SEH : aliased array (1 .. 2) of Integer;
26976
26977 Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
26978 pragma Volatile (Ensure_Reference);
26979
26980 -- Start of processing for main
26981
26982 begin
26983 -- Save global variables
26984
26985 gnat_argc := argc;
26986 gnat_argv := argv;
26987 gnat_envp := envp;
26988
26989 -- Call low level system initialization
26990
26991 Initialize (SEH'Address);
26992
26993 -- Call our generated Ada initialization routine
26994
26995 adainit;
26996
26997 -- Now we call the main program of the partition
26998
26999 Ada_Main_Program;
27000
27001 -- Perform Ada finalization
27002
27003 adafinal;
27004
27005 -- Perform low level system finalization
27006
27007 Finalize;
27008
27009 -- Return the proper exit status
27010 return (gnat_exit_status);
27011 end;
27012
27013 -- This section is entirely comments, so it has no effect on the
27014 -- compilation of the Ada_Main package. It provides the list of
27015 -- object files and linker options, as well as some standard
27016 -- libraries needed for the link. The gnatlink utility parses
27017 -- this b~hello.adb file to read these comment lines to generate
27018 -- the appropriate command line arguments for the call to the
27019 -- system linker. The BEGIN/END lines are used for sentinels for
27020 -- this parsing operation.
27021
27022 -- The exact file names will of course depend on the environment,
27023 -- host/target and location of files on the host system.
27024
27025 -- BEGIN Object file/option list
27026 -- ./hello.o
27027 -- -L./
27028 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
27029 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
27030 -- END Object file/option list
27031
27032 end ada_main;
27033 @end example
27034
27035 The Ada code in the above example is exactly what is generated by the
27036 binder. We have added comments to more clearly indicate the function
27037 of each part of the generated @code{Ada_Main} package.
27038
27039 The code is standard Ada in all respects, and can be processed by any
27040 tools that handle Ada. In particular, it is possible to use the debugger
27041 in Ada mode to debug the generated @code{Ada_Main} package. For example,
27042 suppose that for reasons that you do not understand, your program is crashing
27043 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
27044 you can place a breakpoint on the call:
27045
27046 @quotation
27047
27048 @example
27049 Ada.Text_Io'Elab_Body;
27050 @end example
27051 @end quotation
27052
27053 and trace the elaboration routine for this package to find out where
27054 the problem might be (more usually of course you would be debugging
27055 elaboration code in your own application).
27056
27057 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
27058
27059 @node Elaboration Order Handling in GNAT,Inline Assembler,Example of Binder Output File,Top
27060 @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{22a}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id1}@anchor{22b}
27061 @chapter Elaboration Order Handling in GNAT
27062
27063
27064 @geindex Order of elaboration
27065
27066 @geindex Elaboration control
27067
27068 This appendix describes the handling of elaboration code in Ada and GNAT, and
27069 discusses how the order of elaboration of program units can be controlled in
27070 GNAT, either automatically or with explicit programming features.
27071
27072 @menu
27073 * Elaboration Code::
27074 * Elaboration Order::
27075 * Checking the Elaboration Order::
27076 * Controlling the Elaboration Order in Ada::
27077 * Controlling the Elaboration Order in GNAT::
27078 * Common Elaboration-model Traits::
27079 * Dynamic Elaboration Model in GNAT::
27080 * Static Elaboration Model in GNAT::
27081 * SPARK Elaboration Model in GNAT::
27082 * Legacy Elaboration Model in GNAT::
27083 * Mixing Elaboration Models::
27084 * Elaboration Circularities::
27085 * Resolving Elaboration Circularities::
27086 * Resolving Task Issues::
27087 * Elaboration-related Compiler Switches::
27088 * Summary of Procedures for Elaboration Control::
27089 * Inspecting the Chosen Elaboration Order::
27090
27091 @end menu
27092
27093 @node Elaboration Code,Elaboration Order,,Elaboration Order Handling in GNAT
27094 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-code}@anchor{22c}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id2}@anchor{22d}
27095 @section Elaboration Code
27096
27097
27098 Ada defines the term @emph{execution} as the process by which a construct achieves
27099 its run-time effect. This process is also referred to as @strong{elaboration} for
27100 declarations and @emph{evaluation} for expressions.
27101
27102 The execution model in Ada allows for certain sections of an Ada program to be
27103 executed prior to execution of the program itself, primarily with the intent of
27104 initializing data. These sections are referred to as @strong{elaboration code}.
27105 Elaboration code is executed as follows:
27106
27107
27108 @itemize *
27109
27110 @item
27111 All partitions of an Ada program are executed in parallel with one another,
27112 possibly in a separate address space, and possibly on a separate computer.
27113
27114 @item
27115 The execution of a partition involves running the environment task for that
27116 partition.
27117
27118 @item
27119 The environment task executes all elaboration code (if available) for all
27120 units within that partition. This code is said to be executed at
27121 @strong{elaboration time}.
27122
27123 @item
27124 The environment task executes the Ada program (if available) for that
27125 partition.
27126 @end itemize
27127
27128 In addition to the Ada terminology, this appendix defines the following terms:
27129
27130
27131 @itemize *
27132
27133 @item
27134 @emph{Scenario}
27135
27136 A construct that is elaborated or executed by elaboration code is referred to
27137 as an @emph{elaboration scenario} or simply a @strong{scenario}. GNAT recognizes the
27138 following scenarios:
27139
27140
27141 @itemize -
27142
27143 @item
27144 @code{'Access} of entries, operators, and subprograms
27145
27146 @item
27147 Activation of tasks
27148
27149 @item
27150 Calls to entries, operators, and subprograms
27151
27152 @item
27153 Instantiations of generic templates
27154 @end itemize
27155
27156 @item
27157 @emph{Target}
27158
27159 A construct elaborated by a scenario is referred to as @emph{elaboration target}
27160 or simply @strong{target}. GNAT recognizes the following targets:
27161
27162
27163 @itemize -
27164
27165 @item
27166 For @code{'Access} of entries, operators, and subprograms, the target is the
27167 entry, operator, or subprogram being aliased.
27168
27169 @item
27170 For activation of tasks, the target is the task body
27171
27172 @item
27173 For calls to entries, operators, and subprograms, the target is the entry,
27174 operator, or subprogram being invoked.
27175
27176 @item
27177 For instantiations of generic templates, the target is the generic template
27178 being instantiated.
27179 @end itemize
27180 @end itemize
27181
27182 Elaboration code may appear in two distinct contexts:
27183
27184
27185 @itemize *
27186
27187 @item
27188 @emph{Library level}
27189
27190 A scenario appears at the library level when it is encapsulated by a package
27191 [body] compilation unit, ignoring any other package [body] declarations in
27192 between.
27193
27194 @example
27195 with Server;
27196 package Client is
27197 procedure Proc;
27198
27199 package Nested is
27200 Val : ... := Server.Func;
27201 end Nested;
27202 end Client;
27203 @end example
27204
27205 In the example above, the call to @code{Server.Func} is an elaboration scenario
27206 because it appears at the library level of package @code{Client}. Note that the
27207 declaration of package @code{Nested} is ignored according to the definition
27208 given above. As a result, the call to @code{Server.Func} will be executed when
27209 the spec of unit @code{Client} is elaborated.
27210
27211 @item
27212 @emph{Package body statements}
27213
27214 A scenario appears within the statement sequence of a package body when it is
27215 bounded by the region starting from the @code{begin} keyword of the package body
27216 and ending at the @code{end} keyword of the package body.
27217
27218 @example
27219 package body Client is
27220 procedure Proc is
27221 begin
27222 ...
27223 end Proc;
27224 begin
27225 Proc;
27226 end Client;
27227 @end example
27228
27229 In the example above, the call to @code{Proc} is an elaboration scenario because
27230 it appears within the statement sequence of package body @code{Client}. As a
27231 result, the call to @code{Proc} will be executed when the body of @code{Client} is
27232 elaborated.
27233 @end itemize
27234
27235 @node Elaboration Order,Checking the Elaboration Order,Elaboration Code,Elaboration Order Handling in GNAT
27236 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order}@anchor{22e}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id3}@anchor{22f}
27237 @section Elaboration Order
27238
27239
27240 The sequence by which the elaboration code of all units within a partition is
27241 executed is referred to as @strong{elaboration order}.
27242
27243 Within a single unit, elaboration code is executed in sequential order.
27244
27245 @example
27246 package body Client is
27247 Result : ... := Server.Func;
27248
27249 procedure Proc is
27250 package Inst is new Server.Gen;
27251 begin
27252 Inst.Eval (Result);
27253 end Proc;
27254 begin
27255 Proc;
27256 end Client;
27257 @end example
27258
27259 In the example above, the elaboration order within package body @code{Client} is
27260 as follows:
27261
27262
27263 @enumerate
27264
27265 @item
27266 The object declaration of @code{Result} is elaborated.
27267
27268
27269 @itemize *
27270
27271 @item
27272 Function @code{Server.Func} is invoked.
27273 @end itemize
27274
27275 @item
27276 The subprogram body of @code{Proc} is elaborated.
27277
27278 @item
27279 Procedure @code{Proc} is invoked.
27280
27281
27282 @itemize *
27283
27284 @item
27285 Generic unit @code{Server.Gen} is instantiated as @code{Inst}.
27286
27287 @item
27288 Instance @code{Inst} is elaborated.
27289
27290 @item
27291 Procedure @code{Inst.Eval} is invoked.
27292 @end itemize
27293 @end enumerate
27294
27295 The elaboration order of all units within a partition depends on the following
27296 factors:
27297
27298
27299 @itemize *
27300
27301 @item
27302 @emph{with}ed units
27303
27304 @item
27305 purity of units
27306
27307 @item
27308 preelaborability of units
27309
27310 @item
27311 presence of elaboration control pragmas
27312 @end itemize
27313
27314 A program may have several elaboration orders depending on its structure.
27315
27316 @example
27317 package Server is
27318 function Func (Index : Integer) return Integer;
27319 end Server;
27320 @end example
27321
27322 @example
27323 package body Server is
27324 Results : array (1 .. 5) of Integer := (1, 2, 3, 4, 5);
27325
27326 function Func (Index : Integer) return Integer is
27327 begin
27328 return Results (Index);
27329 end Func;
27330 end Server;
27331 @end example
27332
27333 @example
27334 with Server;
27335 package Client is
27336 Val : constant Integer := Server.Func (3);
27337 end Client;
27338 @end example
27339
27340 @example
27341 with Client;
27342 procedure Main is begin null; end Main;
27343 @end example
27344
27345 The following elaboration order exhibits a fundamental problem referred to as
27346 @emph{access-before-elaboration} or simply @strong{ABE}.
27347
27348 @example
27349 spec of Server
27350 spec of Client
27351 body of Server
27352 body of Main
27353 @end example
27354
27355 The elaboration of @code{Server}'s spec materializes function @code{Func}, making it
27356 callable. The elaboration of @code{Client}'s spec elaborates the declaration of
27357 @code{Val}. This invokes function @code{Server.Func}, however the body of
27358 @code{Server.Func} has not been elaborated yet because @code{Server}'s body comes
27359 after @code{Client}'s spec in the elaboration order. As a result, the value of
27360 constant @code{Val} is now undefined.
27361
27362 Without any guarantees from the language, an undetected ABE problem may hinder
27363 proper initialization of data, which in turn may lead to undefined behavior at
27364 run time. To prevent such ABE problems, Ada employs dynamic checks in the same
27365 vein as index or null exclusion checks. A failed ABE check raises exception
27366 @code{Program_Error}.
27367
27368 The following elaboration order avoids the ABE problem and the program can be
27369 successfully elaborated.
27370
27371 @example
27372 spec of Server
27373 body of Server
27374 spec of Client
27375 body of Main
27376 @end example
27377
27378 Ada states that a total elaboration order must exist, but it does not define
27379 what this order is. A compiler is thus tasked with choosing a suitable
27380 elaboration order which satisfies the dependencies imposed by @emph{with} clauses,
27381 unit categorization, and elaboration control pragmas. Ideally an order which
27382 avoids ABE problems should be chosen, however a compiler may not always find
27383 such an order due to complications with respect to control and data flow.
27384
27385 @node Checking the Elaboration Order,Controlling the Elaboration Order in Ada,Elaboration Order,Elaboration Order Handling in GNAT
27386 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id4}@anchor{230}@anchor{gnat_ugn/elaboration_order_handling_in_gnat checking-the-elaboration-order}@anchor{231}
27387 @section Checking the Elaboration Order
27388
27389
27390 To avoid placing the entire elaboration order burden on the programmer, Ada
27391 provides three lines of defense:
27392
27393
27394 @itemize *
27395
27396 @item
27397 @emph{Static semantics}
27398
27399 Static semantic rules restrict the possible choice of elaboration order. For
27400 instance, if unit Client @emph{with}s unit Server, then the spec of Server is
27401 always elaborated prior to Client. The same principle applies to child units
27402 - the spec of a parent unit is always elaborated prior to the child unit.
27403
27404 @item
27405 @emph{Dynamic semantics}
27406
27407 Dynamic checks are performed at run time, to ensure that a target is
27408 elaborated prior to a scenario that executes it, thus avoiding ABE problems.
27409 A failed run-time check raises exception @code{Program_Error}. The following
27410 restrictions apply:
27411
27412
27413 @itemize -
27414
27415 @item
27416 @emph{Restrictions on calls}
27417
27418 An entry, operator, or subprogram can be called from elaboration code only
27419 when the corresponding body has been elaborated.
27420
27421 @item
27422 @emph{Restrictions on instantiations}
27423
27424 A generic unit can be instantiated by elaboration code only when the
27425 corresponding body has been elaborated.
27426
27427 @item
27428 @emph{Restrictions on task activation}
27429
27430 A task can be activated by elaboration code only when the body of the
27431 associated task type has been elaborated.
27432 @end itemize
27433
27434 The restrictions above can be summarized by the following rule:
27435
27436 @emph{If a target has a body, then this body must be elaborated prior to the
27437 execution of the scenario that invokes, instantiates, or activates the
27438 target.}
27439
27440 @item
27441 @emph{Elaboration control}
27442
27443 Pragmas are provided for the programmer to specify the desired elaboration
27444 order.
27445 @end itemize
27446
27447 @node Controlling the Elaboration Order in Ada,Controlling the Elaboration Order in GNAT,Checking the Elaboration Order,Elaboration Order Handling in GNAT
27448 @anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-ada}@anchor{232}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id5}@anchor{233}
27449 @section Controlling the Elaboration Order in Ada
27450
27451
27452 Ada provides several idioms and pragmas to aid the programmer with specifying
27453 the desired elaboration order and avoiding ABE problems altogether.
27454
27455
27456 @itemize *
27457
27458 @item
27459 @emph{Packages without a body}
27460
27461 A library package which does not require a completing body does not suffer
27462 from ABE problems.
27463
27464 @example
27465 package Pack is
27466 generic
27467 type Element is private;
27468 package Containers is
27469 type Element_Array is array (1 .. 10) of Element;
27470 end Containers;
27471 end Pack;
27472 @end example
27473
27474 In the example above, package @code{Pack} does not require a body because it
27475 does not contain any constructs which require completion in a body. As a
27476 result, generic @code{Pack.Containers} can be instantiated without encountering
27477 any ABE problems.
27478 @end itemize
27479
27480 @geindex pragma Pure
27481
27482
27483 @itemize *
27484
27485 @item
27486 @emph{pragma Pure}
27487
27488 Pragma @code{Pure} places sufficient restrictions on a unit to guarantee that no
27489 scenario within the unit can result in an ABE problem.
27490 @end itemize
27491
27492 @geindex pragma Preelaborate
27493
27494
27495 @itemize *
27496
27497 @item
27498 @emph{pragma Preelaborate}
27499
27500 Pragma @code{Preelaborate} is slightly less restrictive than pragma @code{Pure},
27501 but still strong enough to prevent ABE problems within a unit.
27502 @end itemize
27503
27504 @geindex pragma Elaborate_Body
27505
27506
27507 @itemize *
27508
27509 @item
27510 @emph{pragma Elaborate_Body}
27511
27512 Pragma @code{Elaborate_Body} requires that the body of a unit is elaborated
27513 immediately after its spec. This restriction guarantees that no client
27514 scenario can execute a server target before the target body has been
27515 elaborated because the spec and body are effectively "glued" together.
27516
27517 @example
27518 package Server is
27519 pragma Elaborate_Body;
27520
27521 function Func return Integer;
27522 end Server;
27523 @end example
27524
27525 @example
27526 package body Server is
27527 function Func return Integer is
27528 begin
27529 ...
27530 end Func;
27531 end Server;
27532 @end example
27533
27534 @example
27535 with Server;
27536 package Client is
27537 Val : constant Integer := Server.Func;
27538 end Client;
27539 @end example
27540
27541 In the example above, pragma @code{Elaborate_Body} guarantees the following
27542 elaboration order:
27543
27544 @example
27545 spec of Server
27546 body of Server
27547 spec of Client
27548 @end example
27549
27550 because the spec of @code{Server} must be elaborated prior to @code{Client} by
27551 virtue of the @emph{with} clause, and in addition the body of @code{Server} must be
27552 elaborated immediately after the spec of @code{Server}.
27553
27554 Removing pragma @code{Elaborate_Body} could result in the following incorrect
27555 elaboration order:
27556
27557 @example
27558 spec of Server
27559 spec of Client
27560 body of Server
27561 @end example
27562
27563 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func} has
27564 not been elaborated yet.
27565 @end itemize
27566
27567 The pragmas outlined above allow a server unit to guarantee safe elaboration
27568 use by client units. Thus it is a good rule to mark units as @code{Pure} or
27569 @code{Preelaborate}, and if this is not possible, mark them as @code{Elaborate_Body}.
27570
27571 There are however situations where @code{Pure}, @code{Preelaborate}, and
27572 @code{Elaborate_Body} are not applicable. Ada provides another set of pragmas for
27573 use by client units to help ensure the elaboration safety of server units they
27574 depend on.
27575
27576 @geindex pragma Elaborate (Unit)
27577
27578
27579 @itemize *
27580
27581 @item
27582 @emph{pragma Elaborate (Unit)}
27583
27584 Pragma @code{Elaborate} can be placed in the context clauses of a unit, after a
27585 @emph{with} clause. It guarantees that both the spec and body of its argument will
27586 be elaborated prior to the unit with the pragma. Note that other unrelated
27587 units may be elaborated in between the spec and the body.
27588
27589 @example
27590 package Server is
27591 function Func return Integer;
27592 end Server;
27593 @end example
27594
27595 @example
27596 package body Server is
27597 function Func return Integer is
27598 begin
27599 ...
27600 end Func;
27601 end Server;
27602 @end example
27603
27604 @example
27605 with Server;
27606 pragma Elaborate (Server);
27607 package Client is
27608 Val : constant Integer := Server.Func;
27609 end Client;
27610 @end example
27611
27612 In the example above, pragma @code{Elaborate} guarantees the following
27613 elaboration order:
27614
27615 @example
27616 spec of Server
27617 body of Server
27618 spec of Client
27619 @end example
27620
27621 Removing pragma @code{Elaborate} could result in the following incorrect
27622 elaboration order:
27623
27624 @example
27625 spec of Server
27626 spec of Client
27627 body of Server
27628 @end example
27629
27630 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func}
27631 has not been elaborated yet.
27632 @end itemize
27633
27634 @geindex pragma Elaborate_All (Unit)
27635
27636
27637 @itemize *
27638
27639 @item
27640 @emph{pragma Elaborate_All (Unit)}
27641
27642 Pragma @code{Elaborate_All} is placed in the context clauses of a unit, after
27643 a @emph{with} clause. It guarantees that both the spec and body of its argument
27644 will be elaborated prior to the unit with the pragma, as well as all units
27645 @emph{with}ed by the spec and body of the argument, recursively. Note that other
27646 unrelated units may be elaborated in between the spec and the body.
27647
27648 @example
27649 package Math is
27650 function Factorial (Val : Natural) return Natural;
27651 end Math;
27652 @end example
27653
27654 @example
27655 package body Math is
27656 function Factorial (Val : Natural) return Natural is
27657 begin
27658 ...;
27659 end Factorial;
27660 end Math;
27661 @end example
27662
27663 @example
27664 package Computer is
27665 type Operation_Kind is (None, Op_Factorial);
27666
27667 function Compute
27668 (Val : Natural;
27669 Op : Operation_Kind) return Natural;
27670 end Computer;
27671 @end example
27672
27673 @example
27674 with Math;
27675 package body Computer is
27676 function Compute
27677 (Val : Natural;
27678 Op : Operation_Kind) return Natural
27679 is
27680 if Op = Op_Factorial then
27681 return Math.Factorial (Val);
27682 end if;
27683
27684 return 0;
27685 end Compute;
27686 end Computer;
27687 @end example
27688
27689 @example
27690 with Computer;
27691 pragma Elaborate_All (Computer);
27692 package Client is
27693 Val : constant Natural :=
27694 Computer.Compute (123, Computer.Op_Factorial);
27695 end Client;
27696 @end example
27697
27698 In the example above, pragma @code{Elaborate_All} can result in the following
27699 elaboration order:
27700
27701 @example
27702 spec of Math
27703 body of Math
27704 spec of Computer
27705 body of Computer
27706 spec of Client
27707 @end example
27708
27709 Note that there are several allowable suborders for the specs and bodies of
27710 @code{Math} and @code{Computer}, but the point is that these specs and bodies will
27711 be elaborated prior to @code{Client}.
27712
27713 Removing pragma @code{Elaborate_All} could result in the following incorrect
27714 elaboration order
27715
27716 @example
27717 spec of Math
27718 spec of Computer
27719 body of Computer
27720 spec of Client
27721 body of Math
27722 @end example
27723
27724 where @code{Client} invokes @code{Computer.Compute}, which in turn invokes
27725 @code{Math.Factorial}, but the body of @code{Math.Factorial} has not been
27726 elaborated yet.
27727 @end itemize
27728
27729 All pragmas shown above can be summarized by the following rule:
27730
27731 @emph{If a client unit elaborates a server target directly or indirectly, then if
27732 the server unit requires a body and does not have pragma Pure, Preelaborate,
27733 or Elaborate_Body, then the client unit should have pragma Elaborate or
27734 Elaborate_All for the server unit.}
27735
27736 If the rule outlined above is not followed, then a program may fall in one of
27737 the following states:
27738
27739
27740 @itemize *
27741
27742 @item
27743 @emph{No elaboration order exists}
27744
27745 In this case a compiler must diagnose the situation, and refuse to build an
27746 executable program.
27747
27748 @item
27749 @emph{One or more incorrect elaboration orders exist}
27750
27751 In this case a compiler can build an executable program, but
27752 @code{Program_Error} will be raised when the program is run.
27753
27754 @item
27755 @emph{Several elaboration orders exist, some correct, some incorrect}
27756
27757 In this case the programmer has not controlled the elaboration order. As a
27758 result, a compiler may or may not pick one of the correct orders, and the
27759 program may or may not raise @code{Program_Error} when it is run. This is the
27760 worst possible state because the program may fail on another compiler, or
27761 even another version of the same compiler.
27762
27763 @item
27764 @emph{One or more correct orders exist}
27765
27766 In this case a compiler can build an executable program, and the program is
27767 run successfully. This state may be guaranteed by following the outlined
27768 rules, or may be the result of good program architecture.
27769 @end itemize
27770
27771 Note that one additional advantage of using @code{Elaborate} and @code{Elaborate_All}
27772 is that the program continues to stay in the last state (one or more correct
27773 orders exist) even if maintenance changes the bodies of targets.
27774
27775 @node Controlling the Elaboration Order in GNAT,Common Elaboration-model Traits,Controlling the Elaboration Order in Ada,Elaboration Order Handling in GNAT
27776 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id6}@anchor{234}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-gnat}@anchor{235}
27777 @section Controlling the Elaboration Order in GNAT
27778
27779
27780 In addition to Ada semantics and rules synthesized from them, GNAT offers
27781 three elaboration models to aid the programmer with specifying the correct
27782 elaboration order and to diagnose elaboration problems.
27783
27784 @geindex Dynamic elaboration model
27785
27786
27787 @itemize *
27788
27789 @item
27790 @emph{Dynamic elaboration model}
27791
27792 This is the most permissive of the three elaboration models. When the
27793 dynamic model is in effect, GNAT assumes that all code within all units in
27794 a partition is elaboration code. GNAT performs very few diagnostics and
27795 generates run-time checks to verify the elaboration order of a program. This
27796 behavior is identical to that specified by the Ada Reference Manual. The
27797 dynamic model is enabled with compiler switch @code{-gnatE}.
27798 @end itemize
27799
27800 @geindex Static elaboration model
27801
27802
27803 @itemize *
27804
27805 @item
27806 @emph{Static elaboration model}
27807
27808 This is the middle ground of the three models. When the static model is in
27809 effect, GNAT performs extensive diagnostics on a unit-by-unit basis for all
27810 scenarios that elaborate or execute internal targets. GNAT also generates
27811 run-time checks for all external targets and for all scenarios that may
27812 exhibit ABE problems. Finally, GNAT installs implicit @code{Elaborate} and
27813 @code{Elaborate_All} pragmas for server units based on the dependencies of
27814 client units. The static model is the default model in GNAT.
27815 @end itemize
27816
27817 @geindex SPARK elaboration model
27818
27819
27820 @itemize *
27821
27822 @item
27823 @emph{SPARK elaboration model}
27824
27825 This is the most conservative of the three models and enforces the SPARK
27826 rules of elaboration as defined in the SPARK Reference Manual, section 7.7.
27827 The SPARK model is in effect only when a scenario and a target reside in a
27828 region subject to SPARK_Mode On, otherwise the dynamic or static model is in
27829 effect.
27830 @end itemize
27831
27832 @geindex Legacy elaboration model
27833
27834
27835 @itemize *
27836
27837 @item
27838 @emph{Legacy elaboration model}
27839
27840 In addition to the three elaboration models outlined above, GNAT provides the
27841 elaboration model of pre-18.x versions referred to as @cite{legacy elaboration model}. The legacy elaboration model is enabled with compiler switch
27842 @code{-gnatH}.
27843 @end itemize
27844
27845 @geindex Relaxed elaboration mode
27846
27847 The dynamic, legacy, and static models can be relaxed using compiler switch
27848 @code{-gnatJ}, making them more permissive. Note that in this mode, GNAT
27849 may not diagnose certain elaboration issues or install run-time checks.
27850
27851 @node Common Elaboration-model Traits,Dynamic Elaboration Model in GNAT,Controlling the Elaboration Order in GNAT,Elaboration Order Handling in GNAT
27852 @anchor{gnat_ugn/elaboration_order_handling_in_gnat common-elaboration-model-traits}@anchor{236}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id7}@anchor{237}
27853 @section Common Elaboration-model Traits
27854
27855
27856 All three GNAT models are able to detect elaboration problems related to
27857 dispatching calls and a particular kind of ABE referred to as @emph{guaranteed ABE}.
27858
27859
27860 @itemize *
27861
27862 @item
27863 @emph{Dispatching calls}
27864
27865 GNAT installs run-time checks for each primitive subprogram of each tagged
27866 type defined in a partition on the assumption that a dispatching call
27867 invoked at elaboration time will execute one of these primitives. As a
27868 result, a dispatching call that executes a primitive whose body has not
27869 been elaborated yet will raise exception @code{Program_Error} at run time. The
27870 checks can be suppressed using pragma @code{Suppress (Elaboration_Check)}.
27871
27872 @item
27873 @emph{Guaranteed ABE}
27874
27875 A guaranteed ABE arises when the body of a target is not elaborated early
27876 enough, and causes all scenarios that directly execute the target to fail.
27877
27878 @example
27879 package body Guaranteed_ABE is
27880 function ABE return Integer;
27881
27882 Val : constant Integer := ABE;
27883
27884 function ABE return Integer is
27885 begin
27886 ...
27887 end ABE;
27888 end Guaranteed_ABE;
27889 @end example
27890
27891 In the example above, the elaboration of @code{Guaranteed_ABE}'s body elaborates
27892 the declaration of @code{Val}. This invokes function @code{ABE}, however the body
27893 of @code{ABE} has not been elaborated yet. GNAT emits similar diagnostics in all
27894 three models:
27895
27896 @example
27897 1. package body Guaranteed_ABE is
27898 2. function ABE return Integer;
27899 3.
27900 4. Val : constant Integer := ABE;
27901 |
27902 >>> warning: cannot call "ABE" before body seen
27903 >>> warning: Program_Error will be raised at run time
27904
27905 5.
27906 6. function ABE return Integer is
27907 7. begin
27908 8. ...
27909 9. end ABE;
27910 10. end Guaranteed_ABE;
27911 @end example
27912 @end itemize
27913
27914 Note that GNAT emits warnings rather than hard errors whenever it encounters an
27915 elaboration problem. This is because the elaboration model in effect may be too
27916 conservative, or a particular scenario may not be elaborated or executed due to
27917 data and control flow. The warnings can be suppressed selectively with @code{pragma
27918 Warnigns (Off)} or globally with compiler switch @code{-gnatwL}.
27919
27920 @node Dynamic Elaboration Model in GNAT,Static Elaboration Model in GNAT,Common Elaboration-model Traits,Elaboration Order Handling in GNAT
27921 @anchor{gnat_ugn/elaboration_order_handling_in_gnat dynamic-elaboration-model-in-gnat}@anchor{238}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id8}@anchor{239}
27922 @section Dynamic Elaboration Model in GNAT
27923
27924
27925 The dynamic model assumes that all code within all units in a partition is
27926 elaboration code. As a result, run-time checks are installed for each scenario
27927 regardless of whether the target is internal or external. The checks can be
27928 suppressed using pragma @code{Suppress (Elaboration_Check)}. This behavior is
27929 identical to that specified by the Ada Reference Manual. The following example
27930 showcases run-time checks installed by GNAT to verify the elaboration state of
27931 package @code{Dynamic_Model}.
27932
27933 @example
27934 with Server;
27935 package body Dynamic_Model is
27936 procedure API is
27937 begin
27938 ...
27939 end API;
27940
27941 <check that the body of Server.Gen is elaborated>
27942 package Inst is new Server.Gen;
27943
27944 T : Server.Task_Type;
27945
27946 begin
27947 <check that the body of Server.Task_Type is elaborated>
27948
27949 <check that the body of Server.Proc is elaborated>
27950 Server.Proc;
27951 end Dynamic_Model;
27952 @end example
27953
27954 The checks verify that the body of a target has been successfully elaborated
27955 before a scenario activates, calls, or instantiates a target.
27956
27957 Note that no scenario within package @code{Dynamic_Model} calls procedure @code{API}.
27958 In fact, procedure @code{API} may not be invoked by elaboration code within the
27959 partition, however the dynamic model assumes that this can happen.
27960
27961 The dynamic model emits very few diagnostics, but can make suggestions on
27962 missing @code{Elaborate} and @code{Elaborate_All} pragmas for library-level
27963 scenarios. This information is available when compiler switch @code{-gnatel}
27964 is in effect.
27965
27966 @example
27967 1. with Server;
27968 2. package body Dynamic_Model is
27969 3. Val : constant Integer := Server.Func;
27970 |
27971 >>> info: call to "Func" during elaboration
27972 >>> info: missing pragma "Elaborate_All" for unit "Server"
27973
27974 4. end Dynamic_Model;
27975 @end example
27976
27977 @node Static Elaboration Model in GNAT,SPARK Elaboration Model in GNAT,Dynamic Elaboration Model in GNAT,Elaboration Order Handling in GNAT
27978 @anchor{gnat_ugn/elaboration_order_handling_in_gnat static-elaboration-model-in-gnat}@anchor{23a}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id9}@anchor{23b}
27979 @section Static Elaboration Model in GNAT
27980
27981
27982 In contrast to the dynamic model, the static model is more precise in its
27983 analysis of elaboration code. The model makes a clear distinction between
27984 internal and external targets, and resorts to different diagnostics and
27985 run-time checks based on the nature of the target.
27986
27987
27988 @itemize *
27989
27990 @item
27991 @emph{Internal targets}
27992
27993 The static model performs extensive diagnostics on scenarios which elaborate
27994 or execute internal targets. The warnings resulting from these diagnostics
27995 are enabled by default, but can be suppressed selectively with @code{pragma
27996 Warnings (Off)} or globally with compiler switch @code{-gnatwL}.
27997
27998 @example
27999 1. package body Static_Model is
28000 2. generic
28001 3. with function Func return Integer;
28002 4. package Gen is
28003 5. Val : constant Integer := Func;
28004 6. end Gen;
28005 7.
28006 8. function ABE return Integer;
28007 9.
28008 10. function Cause_ABE return Boolean is
28009 11. package Inst is new Gen (ABE);
28010 |
28011 >>> warning: in instantiation at line 5
28012 >>> warning: cannot call "ABE" before body seen
28013 >>> warning: Program_Error may be raised at run time
28014 >>> warning: body of unit "Static_Model" elaborated
28015 >>> warning: function "Cause_ABE" called at line 16
28016 >>> warning: function "ABE" called at line 5, instance at line 11
28017
28018 12. begin
28019 13. ...
28020 14. end Cause_ABE;
28021 15.
28022 16. Val : constant Boolean := Cause_ABE;
28023 17.
28024 18. function ABE return Integer is
28025 19. begin
28026 20. ...
28027 21. end ABE;
28028 22. end Static_Model;
28029 @end example
28030
28031 The example above illustrates an ABE problem within package @code{Static_Model},
28032 which is hidden by several layers of indirection. The elaboration of package
28033 body @code{Static_Model} elaborates the declaration of @code{Val}. This invokes
28034 function @code{Cause_ABE}, which instantiates generic unit @code{Gen} as @code{Inst}.
28035 The elaboration of @code{Inst} invokes function @code{ABE}, however the body of
28036 @code{ABE} has not been elaborated yet.
28037
28038 @item
28039 @emph{External targets}
28040
28041 The static model installs run-time checks to verify the elaboration status
28042 of server targets only when the scenario that elaborates or executes that
28043 target is part of the elaboration code of the client unit. The checks can be
28044 suppressed using pragma @code{Suppress (Elaboration_Check)}.
28045
28046 @example
28047 with Server;
28048 package body Static_Model is
28049 generic
28050 with function Func return Integer;
28051 package Gen is
28052 Val : constant Integer := Func;
28053 end Gen;
28054
28055 function Call_Func return Boolean is
28056 <check that the body of Server.Func is elaborated>
28057 package Inst is new Gen (Server.Func);
28058 begin
28059 ...
28060 end Call_Func;
28061
28062 Val : constant Boolean := Call_Func;
28063 end Static_Model;
28064 @end example
28065
28066 In the example above, the elaboration of package body @code{Static_Model}
28067 elaborates the declaration of @code{Val}. This invokes function @code{Call_Func},
28068 which instantiates generic unit @code{Gen} as @code{Inst}. The elaboration of
28069 @code{Inst} invokes function @code{Server.Func}. Since @code{Server.Func} is an
28070 external target, GNAT installs a run-time check to verify that its body has
28071 been elaborated.
28072
28073 In addition to checks, the static model installs implicit @code{Elaborate} and
28074 @code{Elaborate_All} pragmas to guarantee safe elaboration use of server units.
28075 This information is available when compiler switch @code{-gnatel} is in
28076 effect.
28077
28078 @example
28079 1. with Server;
28080 2. package body Static_Model is
28081 3. generic
28082 4. with function Func return Integer;
28083 5. package Gen is
28084 6. Val : constant Integer := Func;
28085 7. end Gen;
28086 8.
28087 9. function Call_Func return Boolean is
28088 10. package Inst is new Gen (Server.Func);
28089 |
28090 >>> info: instantiation of "Gen" during elaboration
28091 >>> info: in instantiation at line 6
28092 >>> info: call to "Func" during elaboration
28093 >>> info: in instantiation at line 6
28094 >>> info: implicit pragma "Elaborate_All" generated for unit "Server"
28095 >>> info: body of unit "Static_Model" elaborated
28096 >>> info: function "Call_Func" called at line 15
28097 >>> info: function "Func" called at line 6, instance at line 10
28098
28099 11. begin
28100 12. ...
28101 13. end Call_Func;
28102 14.
28103 15. Val : constant Boolean := Call_Func;
28104 |
28105 >>> info: call to "Call_Func" during elaboration
28106
28107 16. end Static_Model;
28108 @end example
28109
28110 In the example above, the elaboration of package body @code{Static_Model}
28111 elaborates the declaration of @code{Val}. This invokes function @code{Call_Func},
28112 which instantiates generic unit @code{Gen} as @code{Inst}. The elaboration of
28113 @code{Inst} invokes function @code{Server.Func}. Since @code{Server.Func} is an
28114 external target, GNAT installs an implicit @code{Elaborate_All} pragma for unit
28115 @code{Server}. The pragma guarantees that both the spec and body of @code{Server},
28116 along with any additional dependencies that @code{Server} may require, are
28117 elaborated prior to the body of @code{Static_Model}.
28118 @end itemize
28119
28120 @node SPARK Elaboration Model in GNAT,Legacy Elaboration Model in GNAT,Static Elaboration Model in GNAT,Elaboration Order Handling in GNAT
28121 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id10}@anchor{23c}@anchor{gnat_ugn/elaboration_order_handling_in_gnat spark-elaboration-model-in-gnat}@anchor{23d}
28122 @section SPARK Elaboration Model in GNAT
28123
28124
28125 The SPARK model is identical to the static model in its handling of internal
28126 targets. The SPARK model, however, requires explicit @code{Elaborate} or
28127 @code{Elaborate_All} pragmas to be present in the program when a target is
28128 external, and compiler switch @code{-gnatd.v} is in effect.
28129
28130 @example
28131 1. with Server;
28132 2. package body SPARK_Model with SPARK_Mode is
28133 3. Val : constant Integer := Server.Func;
28134 |
28135 >>> call to "Func" during elaboration in SPARK
28136 >>> unit "SPARK_Model" requires pragma "Elaborate_All" for "Server"
28137 >>> body of unit "SPARK_Model" elaborated
28138 >>> function "Func" called at line 3
28139
28140 4. end SPARK_Model;
28141 @end example
28142
28143 @node Legacy Elaboration Model in GNAT,Mixing Elaboration Models,SPARK Elaboration Model in GNAT,Elaboration Order Handling in GNAT
28144 @anchor{gnat_ugn/elaboration_order_handling_in_gnat legacy-elaboration-model-in-gnat}@anchor{23e}
28145 @section Legacy Elaboration Model in GNAT
28146
28147
28148 The legacy elaboration model is provided for compatibility with code bases
28149 developed with pre-18.x versions of GNAT. It is similar in functionality to
28150 the dynamic and static models of post-18.x version of GNAT, but may differ
28151 in terms of diagnostics and run-time checks. The legacy elaboration model is
28152 enabled with compiler switch @code{-gnatH}.
28153
28154 @node Mixing Elaboration Models,Elaboration Circularities,Legacy Elaboration Model in GNAT,Elaboration Order Handling in GNAT
28155 @anchor{gnat_ugn/elaboration_order_handling_in_gnat mixing-elaboration-models}@anchor{23f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id11}@anchor{240}
28156 @section Mixing Elaboration Models
28157
28158
28159 It is possible to mix units compiled with a different elaboration model,
28160 however the following rules must be observed:
28161
28162
28163 @itemize *
28164
28165 @item
28166 A client unit compiled with the dynamic model can only @emph{with} a server unit
28167 that meets at least one of the following criteria:
28168
28169
28170 @itemize -
28171
28172 @item
28173 The server unit is compiled with the dynamic model.
28174
28175 @item
28176 The server unit is a GNAT implementation unit from the Ada, GNAT,
28177 Interfaces, or System hierarchies.
28178
28179 @item
28180 The server unit has pragma @code{Pure} or @code{Preelaborate}.
28181
28182 @item
28183 The client unit has an explicit @code{Elaborate_All} pragma for the server
28184 unit.
28185 @end itemize
28186 @end itemize
28187
28188 These rules ensure that elaboration checks are not omitted. If the rules are
28189 violated, the binder emits a warning:
28190
28191 @example
28192 warning: "x.ads" has dynamic elaboration checks and with's
28193 warning: "y.ads" which has static elaboration checks
28194 @end example
28195
28196 The warnings can be suppressed by binder switch @code{-ws}.
28197
28198 @node Elaboration Circularities,Resolving Elaboration Circularities,Mixing Elaboration Models,Elaboration Order Handling in GNAT
28199 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id12}@anchor{241}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-circularities}@anchor{242}
28200 @section Elaboration Circularities
28201
28202
28203 If the binder cannot find an acceptable elaboration order, it outputs detailed
28204 diagnostics describing an @strong{elaboration circularity}.
28205
28206 @example
28207 package Server is
28208 function Func return Integer;
28209 end Server;
28210 @end example
28211
28212 @example
28213 with Client;
28214 package body Server is
28215 function Func return Integer is
28216 begin
28217 ...
28218 end Func;
28219 end Server;
28220 @end example
28221
28222 @example
28223 with Server;
28224 package Client is
28225 Val : constant Integer := Server.Func;
28226 end Client;
28227 @end example
28228
28229 @example
28230 with Client;
28231 procedure Main is begin null; end Main;
28232 @end example
28233
28234 @example
28235 error: elaboration circularity detected
28236 info: "server (body)" must be elaborated before "client (spec)"
28237 info: reason: implicit Elaborate_All in unit "client (spec)"
28238 info: recompile "client (spec)" with -gnatel for full details
28239 info: "server (body)"
28240 info: must be elaborated along with its spec:
28241 info: "server (spec)"
28242 info: which is withed by:
28243 info: "client (spec)"
28244 info: "client (spec)" must be elaborated before "server (body)"
28245 info: reason: with clause
28246 @end example
28247
28248 In the example above, @code{Client} must be elaborated prior to @code{Main} by virtue
28249 of a @emph{with} clause. The elaboration of @code{Client} invokes @code{Server.Func}, and
28250 static model generates an implicit @code{Elaborate_All} pragma for @code{Server}. The
28251 pragma implies that both the spec and body of @code{Server}, along with any units
28252 they @emph{with}, must be elaborated prior to @code{Client}. However, @code{Server}'s body
28253 @emph{with}s @code{Client}, implying that @code{Client} must be elaborated prior to
28254 @code{Server}. The end result is that @code{Client} must be elaborated prior to
28255 @code{Client}, and this leads to a circularity.
28256
28257 @node Resolving Elaboration Circularities,Resolving Task Issues,Elaboration Circularities,Elaboration Order Handling in GNAT
28258 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id13}@anchor{243}@anchor{gnat_ugn/elaboration_order_handling_in_gnat resolving-elaboration-circularities}@anchor{244}
28259 @section Resolving Elaboration Circularities
28260
28261
28262 When faced with an elaboration circularity, a programmer has several options
28263 available.
28264
28265
28266 @itemize *
28267
28268 @item
28269 @emph{Fix the program}
28270
28271 The most desirable option from the point of view of long-term maintenance
28272 is to rearrange the program so that the elaboration problems are avoided.
28273 One useful technique is to place the elaboration code into separate child
28274 packages. Another is to move some of the initialization code to explicitly
28275 invoked subprograms, where the program controls the order of initialization
28276 explicitly. Although this is the most desirable option, it may be impractical
28277 and involve too much modification, especially in the case of complex legacy
28278 code.
28279
28280 @item
28281 @emph{Switch to more permissive elaboration model}
28282
28283 If the compilation was performed using the static model, enable the dynamic
28284 model with compiler switch @code{-gnatE}. GNAT will no longer generate
28285 implicit @code{Elaborate} and @code{Elaborate_All} pragmas, resulting in a behavior
28286 identical to that specified by the Ada Reference Manual. The binder will
28287 generate an executable program that may or may not raise @code{Program_Error},
28288 and it is the programmer's responsibility to ensure that it does not raise
28289 @code{Program_Error}.
28290
28291 If the compilation was performed using a post-18.x version of GNAT, consider
28292 using the legacy elaboration model, in the following order:
28293
28294
28295 @itemize -
28296
28297 @item
28298 Use the legacy static elaboration model, with compiler switch
28299 @code{-gnatH}.
28300
28301 @item
28302 Use the legacy dynamic elaboration model, with compiler switches
28303 @code{-gnatH} @code{-gnatE}.
28304
28305 @item
28306 Use the relaxed legacy static elaboration model, with compiler switches
28307 @code{-gnatH} @code{-gnatJ}.
28308
28309 @item
28310 Use the relaxed legacy dynamic elaboration model, with compiler switches
28311 @code{-gnatH} @code{-gnatJ} @code{-gnatE}.
28312 @end itemize
28313
28314 @item
28315 @emph{Suppress all elaboration checks}
28316
28317 The drawback of run-time checks is that they generate overhead at run time,
28318 both in space and time. If the programmer is absolutely sure that a program
28319 will not raise an elaboration-related @code{Program_Error}, then using the
28320 pragma @code{Suppress (Elaboration_Check)} globally (as a configuration pragma)
28321 will eliminate all run-time checks.
28322
28323 @item
28324 @emph{Suppress elaboration checks selectively}
28325
28326 If a scenario cannot possibly lead to an elaboration @code{Program_Error},
28327 and the binder nevertheless complains about implicit @code{Elaborate} and
28328 @code{Elaborate_All} pragmas that lead to elaboration circularities, it
28329 is possible to suppress the generation of implicit @code{Elaborate} and
28330 @code{Elaborate_All} pragmas, as well as run-time checks. Clearly this can
28331 be unsafe, and it is the responsibility of the programmer to make sure
28332 that the resulting program has no elaboration anomalies. Pragma
28333 @code{Suppress (Elaboration_Check)} can be used with different levels of
28334 granularity to achieve these effects.
28335
28336
28337 @itemize -
28338
28339 @item
28340 @emph{Target suppression}
28341
28342 When the pragma is placed in a declarative part, without a second argument
28343 naming an entity, it will suppress implicit @code{Elaborate} and
28344 @code{Elaborate_All} pragma generation, as well as run-time checks, on all
28345 targets within the region.
28346
28347 @example
28348 package Range_Suppress is
28349 pragma Suppress (Elaboration_Check);
28350
28351 function Func return Integer;
28352
28353 generic
28354 procedure Gen;
28355
28356 pragma Unsuppress (Elaboration_Check);
28357
28358 task type Tsk;
28359 end Range_Suppress;
28360 @end example
28361
28362 In the example above, a pair of Suppress/Unsuppress pragmas define a region
28363 of suppression within package @code{Range_Suppress}. As a result, no implicit
28364 @code{Elaborate} and @code{Elaborate_All} pragmas, nor any run-time checks, will
28365 be generated by callers of @code{Func} and instantiators of @code{Gen}. Note that
28366 task type @code{Tsk} is not within this region.
28367
28368 An alternative to the region-based suppression is to use multiple
28369 @code{Suppress} pragmas with arguments naming specific entities for which
28370 elaboration checks should be suppressed:
28371
28372 @example
28373 package Range_Suppress is
28374 function Func return Integer;
28375 pragma Suppress (Elaboration_Check, Func);
28376
28377 generic
28378 procedure Gen;
28379 pragma Suppress (Elaboration_Check, Gen);
28380
28381 task type Tsk;
28382 end Range_Suppress;
28383 @end example
28384
28385 @item
28386 @emph{Scenario suppression}
28387
28388 When the pragma @code{Suppress} is placed in a declarative or statement
28389 part, without an entity argument, it will suppress implicit @code{Elaborate}
28390 and @code{Elaborate_All} pragma generation, as well as run-time checks, on
28391 all scenarios within the region.
28392
28393 @example
28394 with Server;
28395 package body Range_Suppress is
28396 pragma Suppress (Elaboration_Check);
28397
28398 function Func return Integer is
28399 begin
28400 return Server.Func;
28401 end Func;
28402
28403 procedure Gen is
28404 begin
28405 Server.Proc;
28406 end Gen;
28407
28408 pragma Unsuppress (Elaboration_Check);
28409
28410 task body Tsk is
28411 begin
28412 Server.Proc;
28413 end Tsk;
28414 end Range_Suppress;
28415 @end example
28416
28417 In the example above, a pair of Suppress/Unsuppress pragmas define a region
28418 of suppression within package body @code{Range_Suppress}. As a result, the
28419 calls to @code{Server.Func} in @code{Func} and @code{Server.Proc} in @code{Gen} will
28420 not generate any implicit @code{Elaborate} and @code{Elaborate_All} pragmas or
28421 run-time checks.
28422 @end itemize
28423 @end itemize
28424
28425 @node Resolving Task Issues,Elaboration-related Compiler Switches,Resolving Elaboration Circularities,Elaboration Order Handling in GNAT
28426 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id14}@anchor{245}@anchor{gnat_ugn/elaboration_order_handling_in_gnat resolving-task-issues}@anchor{246}
28427 @section Resolving Task Issues
28428
28429
28430 The model of execution in Ada dictates that elaboration must first take place,
28431 and only then can the main program be started. Tasks which are activated during
28432 elaboration violate this model and may lead to serious concurrent problems at
28433 elaboration time.
28434
28435 A task can be activated in two different ways:
28436
28437
28438 @itemize *
28439
28440 @item
28441 The task is created by an allocator in which case it is activated immediately
28442 after the allocator is evaluated.
28443
28444 @item
28445 The task is declared at the library level or within some nested master in
28446 which case it is activated before starting execution of the statement
28447 sequence of the master defining the task.
28448 @end itemize
28449
28450 Since the elaboration of a partition is performed by the environment task
28451 servicing that partition, any tasks activated during elaboration may be in
28452 a race with the environment task, and lead to unpredictable state and behavior.
28453 The static model seeks to avoid such interactions by assuming that all code in
28454 the task body is executed at elaboration time, if the task was activated by
28455 elaboration code.
28456
28457 @example
28458 package Decls is
28459 task Lib_Task is
28460 entry Start;
28461 end Lib_Task;
28462
28463 type My_Int is new Integer;
28464
28465 function Ident (M : My_Int) return My_Int;
28466 end Decls;
28467 @end example
28468
28469 @example
28470 with Utils;
28471 package body Decls is
28472 task body Lib_Task is
28473 begin
28474 accept Start;
28475 Utils.Put_Val (2);
28476 end Lib_Task;
28477
28478 function Ident (M : My_Int) return My_Int is
28479 begin
28480 return M;
28481 end Ident;
28482 end Decls;
28483 @end example
28484
28485 @example
28486 with Decls;
28487 package Utils is
28488 procedure Put_Val (Arg : Decls.My_Int);
28489 end Utils;
28490 @end example
28491
28492 @example
28493 with Ada.Text_IO; use Ada.Text_IO;
28494 package body Utils is
28495 procedure Put_Val (Arg : Decls.My_Int) is
28496 begin
28497 Put_Line (Arg'Img);
28498 end Put_Val;
28499 end Utils;
28500 @end example
28501
28502 @example
28503 with Decls;
28504 procedure Main is
28505 begin
28506 Decls.Lib_Task.Start;
28507 end Main;
28508 @end example
28509
28510 When the above example is compiled with the static model, an elaboration
28511 circularity arises:
28512
28513 @example
28514 error: elaboration circularity detected
28515 info: "decls (body)" must be elaborated before "decls (body)"
28516 info: reason: implicit Elaborate_All in unit "decls (body)"
28517 info: recompile "decls (body)" with -gnatel for full details
28518 info: "decls (body)"
28519 info: must be elaborated along with its spec:
28520 info: "decls (spec)"
28521 info: which is withed by:
28522 info: "utils (spec)"
28523 info: which is withed by:
28524 info: "decls (body)"
28525 @end example
28526
28527 In the above example, @code{Decls} must be elaborated prior to @code{Main} by virtue
28528 of a with clause. The elaboration of @code{Decls} activates task @code{Lib_Task}. The
28529 static model conservatibely assumes that all code within the body of
28530 @code{Lib_Task} is executed, and generates an implicit @code{Elaborate_All} pragma
28531 for @code{Units} due to the call to @code{Utils.Put_Val}. The pragma implies that
28532 both the spec and body of @code{Utils}, along with any units they @emph{with},
28533 must be elaborated prior to @code{Decls}. However, @code{Utils}'s spec @emph{with}s
28534 @code{Decls}, implying that @code{Decls} must be elaborated before @code{Utils}. The end
28535 result is that @code{Utils} must be elaborated prior to @code{Utils}, and this
28536 leads to a circularity.
28537
28538 In reality, the example above will not exhibit an ABE problem at run time.
28539 When the body of task @code{Lib_Task} is activated, execution will wait for entry
28540 @code{Start} to be accepted, and the call to @code{Utils.Put_Val} will not take place
28541 at elaboration time. Task @code{Lib_Task} will resume its execution after the main
28542 program is executed because @code{Main} performs a rendezvous with
28543 @code{Lib_Task.Start}, and at that point all units have already been elaborated.
28544 As a result, the static model may seem overly conservative, partly because it
28545 does not take control and data flow into account.
28546
28547 When faced with a task elaboration circularity, a programmer has several
28548 options available:
28549
28550
28551 @itemize *
28552
28553 @item
28554 @emph{Use the dynamic model}
28555
28556 The dynamic model does not generate implicit @code{Elaborate} and
28557 @code{Elaborate_All} pragmas. Instead, it will install checks prior to every
28558 call in the example above, thus verifying the successful elaboration of
28559 @code{Utils.Put_Val} in case the call to it takes place at elaboration time.
28560 The dynamic model is enabled with compiler switch @code{-gnatE}.
28561
28562 @item
28563 @emph{Isolate the tasks}
28564
28565 Relocating tasks in their own separate package could decouple them from
28566 dependencies that would otherwise cause an elaboration circularity. The
28567 example above can be rewritten as follows:
28568
28569 @example
28570 package Decls1 is -- new
28571 task Lib_Task is
28572 entry Start;
28573 end Lib_Task;
28574 end Decls1;
28575 @end example
28576
28577 @example
28578 with Utils;
28579 package body Decls1 is -- new
28580 task body Lib_Task is
28581 begin
28582 accept Start;
28583 Utils.Put_Val (2);
28584 end Lib_Task;
28585 end Decls1;
28586 @end example
28587
28588 @example
28589 package Decls2 is -- new
28590 type My_Int is new Integer;
28591 function Ident (M : My_Int) return My_Int;
28592 end Decls2;
28593 @end example
28594
28595 @example
28596 with Utils;
28597 package body Decls2 is -- new
28598 function Ident (M : My_Int) return My_Int is
28599 begin
28600 return M;
28601 end Ident;
28602 end Decls2;
28603 @end example
28604
28605 @example
28606 with Decls2;
28607 package Utils is
28608 procedure Put_Val (Arg : Decls2.My_Int);
28609 end Utils;
28610 @end example
28611
28612 @example
28613 with Ada.Text_IO; use Ada.Text_IO;
28614 package body Utils is
28615 procedure Put_Val (Arg : Decls2.My_Int) is
28616 begin
28617 Put_Line (Arg'Img);
28618 end Put_Val;
28619 end Utils;
28620 @end example
28621
28622 @example
28623 with Decls1;
28624 procedure Main is
28625 begin
28626 Decls1.Lib_Task.Start;
28627 end Main;
28628 @end example
28629
28630 @item
28631 @emph{Declare the tasks}
28632
28633 The original example uses a single task declaration for @code{Lib_Task}. An
28634 explicit task type declaration and a properly placed task object could avoid
28635 the dependencies that would otherwise cause an elaboration circularity. The
28636 example can be rewritten as follows:
28637
28638 @example
28639 package Decls is
28640 task type Lib_Task is -- new
28641 entry Start;
28642 end Lib_Task;
28643
28644 type My_Int is new Integer;
28645
28646 function Ident (M : My_Int) return My_Int;
28647 end Decls;
28648 @end example
28649
28650 @example
28651 with Utils;
28652 package body Decls is
28653 task body Lib_Task is
28654 begin
28655 accept Start;
28656 Utils.Put_Val (2);
28657 end Lib_Task;
28658
28659 function Ident (M : My_Int) return My_Int is
28660 begin
28661 return M;
28662 end Ident;
28663 end Decls;
28664 @end example
28665
28666 @example
28667 with Decls;
28668 package Utils is
28669 procedure Put_Val (Arg : Decls.My_Int);
28670 end Utils;
28671 @end example
28672
28673 @example
28674 with Ada.Text_IO; use Ada.Text_IO;
28675 package body Utils is
28676 procedure Put_Val (Arg : Decls.My_Int) is
28677 begin
28678 Put_Line (Arg'Img);
28679 end Put_Val;
28680 end Utils;
28681 @end example
28682
28683 @example
28684 with Decls;
28685 package Obj_Decls is -- new
28686 Task_Obj : Decls.Lib_Task;
28687 end Obj_Decls;
28688 @end example
28689
28690 @example
28691 with Obj_Decls;
28692 procedure Main is
28693 begin
28694 Obj_Decls.Task_Obj.Start; -- new
28695 end Main;
28696 @end example
28697
28698 @item
28699 @emph{Use restriction No_Entry_Calls_In_Elaboration_Code}
28700
28701 The issue exhibited in the original example under this section revolves
28702 around the body of @code{Lib_Task} blocking on an accept statement. There is
28703 no rule to prevent elaboration code from performing entry calls, however in
28704 practice this is highly unusual. In addition, the pattern of starting tasks
28705 at elaboration time and then immediately blocking on accept or select
28706 statements is quite common.
28707
28708 If a programmer knows that elaboration code will not perform any entry
28709 calls, then the programmer can indicate that the static model should not
28710 process the remainder of a task body once an accept or select statement has
28711 been encountered. This behavior can be specified by a configuration pragma:
28712
28713 @example
28714 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
28715 @end example
28716
28717 In addition to the change in behavior with respect to task bodies, the
28718 static model will verify that no entry calls take place at elaboration time.
28719 @end itemize
28720
28721 @node Elaboration-related Compiler Switches,Summary of Procedures for Elaboration Control,Resolving Task Issues,Elaboration Order Handling in GNAT
28722 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-related-compiler-switches}@anchor{247}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id15}@anchor{248}
28723 @section Elaboration-related Compiler Switches
28724
28725
28726 GNAT has several switches that affect the elaboration model and consequently
28727 the elaboration order chosen by the binder.
28728
28729 @geindex -gnatE (gnat)
28730
28731
28732 @table @asis
28733
28734 @item @code{-gnatE}
28735
28736 Dynamic elaboration checking mode enabled
28737
28738 When this switch is in effect, GNAT activates the dynamic elaboration model.
28739 @end table
28740
28741 @geindex -gnatel (gnat)
28742
28743
28744 @table @asis
28745
28746 @item @code{-gnatel}
28747
28748 Turn on info messages on generated Elaborate[_All] pragmas
28749
28750 When this switch is in effect, GNAT will emit the following supplementary
28751 information depending on the elaboration model in effect.
28752
28753
28754 @itemize -
28755
28756 @item
28757 @emph{Dynamic model}
28758
28759 GNAT will indicate missing @code{Elaborate} and @code{Elaborate_All} pragmas for
28760 all library-level scenarios within the partition.
28761
28762 @item
28763 @emph{Static model}
28764
28765 GNAT will indicate all scenarios executed during elaboration. In addition,
28766 it will provide detailed traceback when an implicit @code{Elaborate} or
28767 @code{Elaborate_All} pragma is generated.
28768
28769 @item
28770 @emph{SPARK model}
28771
28772 GNAT will indicate how an elaboration requirement is met by the context of
28773 a unit. This diagnostic requires compiler switch @code{-gnatd.v}.
28774
28775 @example
28776 1. with Server; pragma Elaborate_All (Server);
28777 2. package Client with SPARK_Mode is
28778 3. Val : constant Integer := Server.Func;
28779 |
28780 >>> info: call to "Func" during elaboration in SPARK
28781 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
28782
28783 4. end Client;
28784 @end example
28785 @end itemize
28786 @end table
28787
28788 @geindex -gnatH (gnat)
28789
28790
28791 @table @asis
28792
28793 @item @code{-gnatH}
28794
28795 Legacy elaboration checking mode enabled
28796
28797 When this switch is in effect, GNAT will utilize the pre-18.x elaboration
28798 model.
28799 @end table
28800
28801 @geindex -gnatJ (gnat)
28802
28803
28804 @table @asis
28805
28806 @item @code{-gnatJ}
28807
28808 Relaxed elaboration checking mode enabled
28809
28810 When this switch is in effect, GNAT will not process certain scenarios,
28811 resulting in a more permissive elaboration model. Note that this may
28812 eliminate some diagnostics and run-time checks.
28813 @end table
28814
28815 @geindex -gnatw.f (gnat)
28816
28817
28818 @table @asis
28819
28820 @item @code{-gnatw.f}
28821
28822 Turn on warnings for suspicious Subp'Access
28823
28824 When this switch is in effect, GNAT will treat @code{'Access} of an entry,
28825 operator, or subprogram as a potential call to the target and issue warnings:
28826
28827 @example
28828 1. package body Attribute_Call is
28829 2. function Func return Integer;
28830 3. type Func_Ptr is access function return Integer;
28831 4.
28832 5. Ptr : constant Func_Ptr := Func'Access;
28833 |
28834 >>> warning: "Access" attribute of "Func" before body seen
28835 >>> warning: possible Program_Error on later references
28836 >>> warning: body of unit "Attribute_Call" elaborated
28837 >>> warning: "Access" of "Func" taken at line 5
28838
28839 6.
28840 7. function Func return Integer is
28841 8. begin
28842 9. ...
28843 10. end Func;
28844 11. end Attribute_Call;
28845 @end example
28846
28847 In the example above, the elaboration of declaration @code{Ptr} is assigned
28848 @code{Func'Access} before the body of @code{Func} has been elaborated.
28849 @end table
28850
28851 @geindex -gnatwl (gnat)
28852
28853
28854 @table @asis
28855
28856 @item @code{-gnatwl}
28857
28858 Turn on warnings for elaboration problems
28859
28860 When this switch is in effect, GNAT emits diagnostics in the form of warnings
28861 concerning various elaboration problems. The warnings are enabled by default.
28862 The switch is provided in case all warnings are suppressed, but elaboration
28863 warnings are still desired.
28864
28865 @item @code{-gnatwL}
28866
28867 Turn off warnings for elaboration problems
28868
28869 When this switch is in effect, GNAT no longer emits any diagnostics in the
28870 form of warnings. Selective suppression of elaboration problems is possible
28871 using @code{pragma Warnings (Off)}.
28872
28873 @example
28874 1. package body Selective_Suppression is
28875 2. function ABE return Integer;
28876 3.
28877 4. Val_1 : constant Integer := ABE;
28878 |
28879 >>> warning: cannot call "ABE" before body seen
28880 >>> warning: Program_Error will be raised at run time
28881
28882 5.
28883 6. pragma Warnings (Off);
28884 7. Val_2 : constant Integer := ABE;
28885 8. pragma Warnings (On);
28886 9.
28887 10. function ABE return Integer is
28888 11. begin
28889 12. ...
28890 13. end ABE;
28891 14. end Selective_Suppression;
28892 @end example
28893
28894 Note that suppressing elaboration warnings does not eliminate run-time
28895 checks. The example above will still fail at run time with an ABE.
28896 @end table
28897
28898 @node Summary of Procedures for Elaboration Control,Inspecting the Chosen Elaboration Order,Elaboration-related Compiler Switches,Elaboration Order Handling in GNAT
28899 @anchor{gnat_ugn/elaboration_order_handling_in_gnat summary-of-procedures-for-elaboration-control}@anchor{249}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id16}@anchor{24a}
28900 @section Summary of Procedures for Elaboration Control
28901
28902
28903 A programmer should first compile the program with the default options, using
28904 none of the binder or compiler switches. If the binder succeeds in finding an
28905 elaboration order, then apart from possible cases involing dispatching calls
28906 and access-to-subprogram types, the program is free of elaboration errors.
28907
28908 If it is important for the program to be portable to compilers other than GNAT,
28909 then the programmer should use compiler switch @code{-gnatel} and consider
28910 the messages about missing or implicitly created @code{Elaborate} and
28911 @code{Elaborate_All} pragmas.
28912
28913 If the binder reports an elaboration circularity, the programmer has several
28914 options:
28915
28916
28917 @itemize *
28918
28919 @item
28920 Ensure that elaboration warnings are enabled. This will allow the static
28921 model to output trace information of elaboration issues. The trace
28922 information could shed light on previously unforeseen dependencies, as well
28923 as their origins. Elaboration warnings are enabled with compiler switch
28924 @code{-gnatwl}.
28925
28926 @item
28927 Use switch @code{-gnatel} to obtain messages on generated implicit
28928 @code{Elaborate} and @code{Elaborate_All} pragmas. The trace information could
28929 indicate why a server unit must be elaborated prior to a client unit.
28930
28931 @item
28932 If the warnings produced by the static model indicate that a task is
28933 involved, consider the options in section @ref{245,,Resolving Task Issues}.
28934
28935 @item
28936 If none of the steps outlined above resolve the circularity, use a more
28937 permissive elaboration model, in the following order:
28938
28939
28940 @itemize -
28941
28942 @item
28943 Use the dynamic elaboration model, with compiler switch @code{-gnatE}.
28944
28945 @item
28946 Use the legacy static elaboration model, with compiler switch
28947 @code{-gnatH}.
28948
28949 @item
28950 Use the legacy dynamic elaboration model, with compiler switches
28951 @code{-gnatH} @code{-gnatE}.
28952
28953 @item
28954 Use the relaxed legacy static elaboration model, with compiler switches
28955 @code{-gnatH} @code{-gnatJ}.
28956
28957 @item
28958 Use the relaxed legacy dynamic elaboration model, with compiler switches
28959 @code{-gnatH} @code{-gnatJ} @code{-gnatE}.
28960 @end itemize
28961 @end itemize
28962
28963 @node Inspecting the Chosen Elaboration Order,,Summary of Procedures for Elaboration Control,Elaboration Order Handling in GNAT
28964 @anchor{gnat_ugn/elaboration_order_handling_in_gnat inspecting-the-chosen-elaboration-order}@anchor{24b}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id17}@anchor{24c}
28965 @section Inspecting the Chosen Elaboration Order
28966
28967
28968 To see the elaboration order chosen by the binder, inspect the contents of file
28969 @cite{b~xxx.adb}. On certain targets, this file appears as @cite{b_xxx.adb}. The
28970 elaboration order appears as a sequence of calls to @code{Elab_Body} and
28971 @code{Elab_Spec}, interspersed with assignments to @cite{Exxx} which indicates that a
28972 particular unit is elaborated. For example:
28973
28974 @example
28975 System.Soft_Links'Elab_Body;
28976 E14 := True;
28977 System.Secondary_Stack'Elab_Body;
28978 E18 := True;
28979 System.Exception_Table'Elab_Body;
28980 E24 := True;
28981 Ada.Io_Exceptions'Elab_Spec;
28982 E67 := True;
28983 Ada.Tags'Elab_Spec;
28984 Ada.Streams'Elab_Spec;
28985 E43 := True;
28986 Interfaces.C'Elab_Spec;
28987 E69 := True;
28988 System.Finalization_Root'Elab_Spec;
28989 E60 := True;
28990 System.Os_Lib'Elab_Body;
28991 E71 := True;
28992 System.Finalization_Implementation'Elab_Spec;
28993 System.Finalization_Implementation'Elab_Body;
28994 E62 := True;
28995 Ada.Finalization'Elab_Spec;
28996 E58 := True;
28997 Ada.Finalization.List_Controller'Elab_Spec;
28998 E76 := True;
28999 System.File_Control_Block'Elab_Spec;
29000 E74 := True;
29001 System.File_Io'Elab_Body;
29002 E56 := True;
29003 Ada.Tags'Elab_Body;
29004 E45 := True;
29005 Ada.Text_Io'Elab_Spec;
29006 Ada.Text_Io'Elab_Body;
29007 E07 := True;
29008 @end example
29009
29010 Note also binder switch @code{-l}, which outputs the chosen elaboration
29011 order and provides a more readable form of the above:
29012
29013 @example
29014 ada (spec)
29015 interfaces (spec)
29016 system (spec)
29017 system.case_util (spec)
29018 system.case_util (body)
29019 system.concat_2 (spec)
29020 system.concat_2 (body)
29021 system.concat_3 (spec)
29022 system.concat_3 (body)
29023 system.htable (spec)
29024 system.parameters (spec)
29025 system.parameters (body)
29026 system.crtl (spec)
29027 interfaces.c_streams (spec)
29028 interfaces.c_streams (body)
29029 system.restrictions (spec)
29030 system.restrictions (body)
29031 system.standard_library (spec)
29032 system.exceptions (spec)
29033 system.exceptions (body)
29034 system.storage_elements (spec)
29035 system.storage_elements (body)
29036 system.secondary_stack (spec)
29037 system.stack_checking (spec)
29038 system.stack_checking (body)
29039 system.string_hash (spec)
29040 system.string_hash (body)
29041 system.htable (body)
29042 system.strings (spec)
29043 system.strings (body)
29044 system.traceback (spec)
29045 system.traceback (body)
29046 system.traceback_entries (spec)
29047 system.traceback_entries (body)
29048 ada.exceptions (spec)
29049 ada.exceptions.last_chance_handler (spec)
29050 system.soft_links (spec)
29051 system.soft_links (body)
29052 ada.exceptions.last_chance_handler (body)
29053 system.secondary_stack (body)
29054 system.exception_table (spec)
29055 system.exception_table (body)
29056 ada.io_exceptions (spec)
29057 ada.tags (spec)
29058 ada.streams (spec)
29059 interfaces.c (spec)
29060 interfaces.c (body)
29061 system.finalization_root (spec)
29062 system.finalization_root (body)
29063 system.memory (spec)
29064 system.memory (body)
29065 system.standard_library (body)
29066 system.os_lib (spec)
29067 system.os_lib (body)
29068 system.unsigned_types (spec)
29069 system.stream_attributes (spec)
29070 system.stream_attributes (body)
29071 system.finalization_implementation (spec)
29072 system.finalization_implementation (body)
29073 ada.finalization (spec)
29074 ada.finalization (body)
29075 ada.finalization.list_controller (spec)
29076 ada.finalization.list_controller (body)
29077 system.file_control_block (spec)
29078 system.file_io (spec)
29079 system.file_io (body)
29080 system.val_uns (spec)
29081 system.val_util (spec)
29082 system.val_util (body)
29083 system.val_uns (body)
29084 system.wch_con (spec)
29085 system.wch_con (body)
29086 system.wch_cnv (spec)
29087 system.wch_jis (spec)
29088 system.wch_jis (body)
29089 system.wch_cnv (body)
29090 system.wch_stw (spec)
29091 system.wch_stw (body)
29092 ada.tags (body)
29093 ada.exceptions (body)
29094 ada.text_io (spec)
29095 ada.text_io (body)
29096 text_io (spec)
29097 gdbstr (body)
29098 @end example
29099
29100 @node Inline Assembler,GNU Free Documentation License,Elaboration Order Handling in GNAT,Top
29101 @anchor{gnat_ugn/inline_assembler inline-assembler}@anchor{10}@anchor{gnat_ugn/inline_assembler doc}@anchor{24d}@anchor{gnat_ugn/inline_assembler id1}@anchor{24e}
29102 @chapter Inline Assembler
29103
29104
29105 @geindex Inline Assembler
29106
29107 If you need to write low-level software that interacts directly
29108 with the hardware, Ada provides two ways to incorporate assembly
29109 language code into your program. First, you can import and invoke
29110 external routines written in assembly language, an Ada feature fully
29111 supported by GNAT. However, for small sections of code it may be simpler
29112 or more efficient to include assembly language statements directly
29113 in your Ada source program, using the facilities of the implementation-defined
29114 package @code{System.Machine_Code}, which incorporates the gcc
29115 Inline Assembler. The Inline Assembler approach offers a number of advantages,
29116 including the following:
29117
29118
29119 @itemize *
29120
29121 @item
29122 No need to use non-Ada tools
29123
29124 @item
29125 Consistent interface over different targets
29126
29127 @item
29128 Automatic usage of the proper calling conventions
29129
29130 @item
29131 Access to Ada constants and variables
29132
29133 @item
29134 Definition of intrinsic routines
29135
29136 @item
29137 Possibility of inlining a subprogram comprising assembler code
29138
29139 @item
29140 Code optimizer can take Inline Assembler code into account
29141 @end itemize
29142
29143 This appendix presents a series of examples to show you how to use
29144 the Inline Assembler. Although it focuses on the Intel x86,
29145 the general approach applies also to other processors.
29146 It is assumed that you are familiar with Ada
29147 and with assembly language programming.
29148
29149 @menu
29150 * Basic Assembler Syntax::
29151 * A Simple Example of Inline Assembler::
29152 * Output Variables in Inline Assembler::
29153 * Input Variables in Inline Assembler::
29154 * Inlining Inline Assembler Code::
29155 * Other Asm Functionality::
29156
29157 @end menu
29158
29159 @node Basic Assembler Syntax,A Simple Example of Inline Assembler,,Inline Assembler
29160 @anchor{gnat_ugn/inline_assembler id2}@anchor{24f}@anchor{gnat_ugn/inline_assembler basic-assembler-syntax}@anchor{250}
29161 @section Basic Assembler Syntax
29162
29163
29164 The assembler used by GNAT and gcc is based not on the Intel assembly
29165 language, but rather on a language that descends from the AT&T Unix
29166 assembler @code{as} (and which is often referred to as 'AT&T syntax').
29167 The following table summarizes the main features of @code{as} syntax
29168 and points out the differences from the Intel conventions.
29169 See the gcc @code{as} and @code{gas} (an @code{as} macro
29170 pre-processor) documentation for further information.
29171
29172
29173 @display
29174 @emph{Register names}@w{ }
29175 @display
29176 gcc / @code{as}: Prefix with '%'; for example @code{%eax}@w{ }
29177 Intel: No extra punctuation; for example @code{eax}@w{ }
29178 @end display
29179 @end display
29180
29181
29182
29183
29184 @display
29185 @emph{Immediate operand}@w{ }
29186 @display
29187 gcc / @code{as}: Prefix with '$'; for example @code{$4}@w{ }
29188 Intel: No extra punctuation; for example @code{4}@w{ }
29189 @end display
29190 @end display
29191
29192
29193
29194
29195 @display
29196 @emph{Address}@w{ }
29197 @display
29198 gcc / @code{as}: Prefix with '$'; for example @code{$loc}@w{ }
29199 Intel: No extra punctuation; for example @code{loc}@w{ }
29200 @end display
29201 @end display
29202
29203
29204
29205
29206 @display
29207 @emph{Memory contents}@w{ }
29208 @display
29209 gcc / @code{as}: No extra punctuation; for example @code{loc}@w{ }
29210 Intel: Square brackets; for example @code{[loc]}@w{ }
29211 @end display
29212 @end display
29213
29214
29215
29216
29217 @display
29218 @emph{Register contents}@w{ }
29219 @display
29220 gcc / @code{as}: Parentheses; for example @code{(%eax)}@w{ }
29221 Intel: Square brackets; for example @code{[eax]}@w{ }
29222 @end display
29223 @end display
29224
29225
29226
29227
29228 @display
29229 @emph{Hexadecimal numbers}@w{ }
29230 @display
29231 gcc / @code{as}: Leading '0x' (C language syntax); for example @code{0xA0}@w{ }
29232 Intel: Trailing 'h'; for example @code{A0h}@w{ }
29233 @end display
29234 @end display
29235
29236
29237
29238
29239 @display
29240 @emph{Operand size}@w{ }
29241 @display
29242 gcc / @code{as}: Explicit in op code; for example @code{movw} to move a 16-bit word@w{ }
29243 Intel: Implicit, deduced by assembler; for example @code{mov}@w{ }
29244 @end display
29245 @end display
29246
29247
29248
29249
29250 @display
29251 @emph{Instruction repetition}@w{ }
29252 @display
29253 gcc / @code{as}: Split into two lines; for example@w{ }
29254 @display
29255 @code{rep}@w{ }
29256 @code{stosl}@w{ }
29257 @end display
29258 Intel: Keep on one line; for example @code{rep stosl}@w{ }
29259 @end display
29260 @end display
29261
29262
29263
29264
29265 @display
29266 @emph{Order of operands}@w{ }
29267 @display
29268 gcc / @code{as}: Source first; for example @code{movw $4, %eax}@w{ }
29269 Intel: Destination first; for example @code{mov eax, 4}@w{ }
29270 @end display
29271 @end display
29272
29273
29274
29275 @node A Simple Example of Inline Assembler,Output Variables in Inline Assembler,Basic Assembler Syntax,Inline Assembler
29276 @anchor{gnat_ugn/inline_assembler a-simple-example-of-inline-assembler}@anchor{251}@anchor{gnat_ugn/inline_assembler id3}@anchor{252}
29277 @section A Simple Example of Inline Assembler
29278
29279
29280 The following example will generate a single assembly language statement,
29281 @code{nop}, which does nothing. Despite its lack of run-time effect,
29282 the example will be useful in illustrating the basics of
29283 the Inline Assembler facility.
29284
29285 @quotation
29286
29287 @example
29288 with System.Machine_Code; use System.Machine_Code;
29289 procedure Nothing is
29290 begin
29291 Asm ("nop");
29292 end Nothing;
29293 @end example
29294 @end quotation
29295
29296 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29297 here it takes one parameter, a @emph{template string} that must be a static
29298 expression and that will form the generated instruction.
29299 @code{Asm} may be regarded as a compile-time procedure that parses
29300 the template string and additional parameters (none here),
29301 from which it generates a sequence of assembly language instructions.
29302
29303 The examples in this chapter will illustrate several of the forms
29304 for invoking @code{Asm}; a complete specification of the syntax
29305 is found in the @code{Machine_Code_Insertions} section of the
29306 @cite{GNAT Reference Manual}.
29307
29308 Under the standard GNAT conventions, the @code{Nothing} procedure
29309 should be in a file named @code{nothing.adb}.
29310 You can build the executable in the usual way:
29311
29312 @quotation
29313
29314 @example
29315 $ gnatmake nothing
29316 @end example
29317 @end quotation
29318
29319 However, the interesting aspect of this example is not its run-time behavior
29320 but rather the generated assembly code.
29321 To see this output, invoke the compiler as follows:
29322
29323 @quotation
29324
29325 @example
29326 $ gcc -c -S -fomit-frame-pointer -gnatp nothing.adb
29327 @end example
29328 @end quotation
29329
29330 where the options are:
29331
29332
29333 @itemize *
29334
29335 @item
29336
29337 @table @asis
29338
29339 @item @code{-c}
29340
29341 compile only (no bind or link)
29342 @end table
29343
29344 @item
29345
29346 @table @asis
29347
29348 @item @code{-S}
29349
29350 generate assembler listing
29351 @end table
29352
29353 @item
29354
29355 @table @asis
29356
29357 @item @code{-fomit-frame-pointer}
29358
29359 do not set up separate stack frames
29360 @end table
29361
29362 @item
29363
29364 @table @asis
29365
29366 @item @code{-gnatp}
29367
29368 do not add runtime checks
29369 @end table
29370 @end itemize
29371
29372 This gives a human-readable assembler version of the code. The resulting
29373 file will have the same name as the Ada source file, but with a @code{.s}
29374 extension. In our example, the file @code{nothing.s} has the following
29375 contents:
29376
29377 @quotation
29378
29379 @example
29380 .file "nothing.adb"
29381 gcc2_compiled.:
29382 ___gnu_compiled_ada:
29383 .text
29384 .align 4
29385 .globl __ada_nothing
29386 __ada_nothing:
29387 #APP
29388 nop
29389 #NO_APP
29390 jmp L1
29391 .align 2,0x90
29392 L1:
29393 ret
29394 @end example
29395 @end quotation
29396
29397 The assembly code you included is clearly indicated by
29398 the compiler, between the @code{#APP} and @code{#NO_APP}
29399 delimiters. The character before the 'APP' and 'NOAPP'
29400 can differ on different targets. For example, GNU/Linux uses '#APP' while
29401 on NT you will see '/APP'.
29402
29403 If you make a mistake in your assembler code (such as using the
29404 wrong size modifier, or using a wrong operand for the instruction) GNAT
29405 will report this error in a temporary file, which will be deleted when
29406 the compilation is finished. Generating an assembler file will help
29407 in such cases, since you can assemble this file separately using the
29408 @code{as} assembler that comes with gcc.
29409
29410 Assembling the file using the command
29411
29412 @quotation
29413
29414 @example
29415 $ as nothing.s
29416 @end example
29417 @end quotation
29418
29419 will give you error messages whose lines correspond to the assembler
29420 input file, so you can easily find and correct any mistakes you made.
29421 If there are no errors, @code{as} will generate an object file
29422 @code{nothing.out}.
29423
29424 @node Output Variables in Inline Assembler,Input Variables in Inline Assembler,A Simple Example of Inline Assembler,Inline Assembler
29425 @anchor{gnat_ugn/inline_assembler id4}@anchor{253}@anchor{gnat_ugn/inline_assembler output-variables-in-inline-assembler}@anchor{254}
29426 @section Output Variables in Inline Assembler
29427
29428
29429 The examples in this section, showing how to access the processor flags,
29430 illustrate how to specify the destination operands for assembly language
29431 statements.
29432
29433 @quotation
29434
29435 @example
29436 with Interfaces; use Interfaces;
29437 with Ada.Text_IO; use Ada.Text_IO;
29438 with System.Machine_Code; use System.Machine_Code;
29439 procedure Get_Flags is
29440 Flags : Unsigned_32;
29441 use ASCII;
29442 begin
29443 Asm ("pushfl" & LF & HT & -- push flags on stack
29444 "popl %%eax" & LF & HT & -- load eax with flags
29445 "movl %%eax, %0", -- store flags in variable
29446 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29447 Put_Line ("Flags register:" & Flags'Img);
29448 end Get_Flags;
29449 @end example
29450 @end quotation
29451
29452 In order to have a nicely aligned assembly listing, we have separated
29453 multiple assembler statements in the Asm template string with linefeed
29454 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29455 The resulting section of the assembly output file is:
29456
29457 @quotation
29458
29459 @example
29460 #APP
29461 pushfl
29462 popl %eax
29463 movl %eax, -40(%ebp)
29464 #NO_APP
29465 @end example
29466 @end quotation
29467
29468 It would have been legal to write the Asm invocation as:
29469
29470 @quotation
29471
29472 @example
29473 Asm ("pushfl popl %%eax movl %%eax, %0")
29474 @end example
29475 @end quotation
29476
29477 but in the generated assembler file, this would come out as:
29478
29479 @quotation
29480
29481 @example
29482 #APP
29483 pushfl popl %eax movl %eax, -40(%ebp)
29484 #NO_APP
29485 @end example
29486 @end quotation
29487
29488 which is not so convenient for the human reader.
29489
29490 We use Ada comments
29491 at the end of each line to explain what the assembler instructions
29492 actually do. This is a useful convention.
29493
29494 When writing Inline Assembler instructions, you need to precede each register
29495 and variable name with a percent sign. Since the assembler already requires
29496 a percent sign at the beginning of a register name, you need two consecutive
29497 percent signs for such names in the Asm template string, thus @code{%%eax}.
29498 In the generated assembly code, one of the percent signs will be stripped off.
29499
29500 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29501 variables: operands you later define using @code{Input} or @code{Output}
29502 parameters to @code{Asm}.
29503 An output variable is illustrated in
29504 the third statement in the Asm template string:
29505
29506 @quotation
29507
29508 @example
29509 movl %%eax, %0
29510 @end example
29511 @end quotation
29512
29513 The intent is to store the contents of the eax register in a variable that can
29514 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29515 necessarily work, since the compiler might optimize by using a register
29516 to hold Flags, and the expansion of the @code{movl} instruction would not be
29517 aware of this optimization. The solution is not to store the result directly
29518 but rather to advise the compiler to choose the correct operand form;
29519 that is the purpose of the @code{%0} output variable.
29520
29521 Information about the output variable is supplied in the @code{Outputs}
29522 parameter to @code{Asm}:
29523
29524 @quotation
29525
29526 @example
29527 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29528 @end example
29529 @end quotation
29530
29531 The output is defined by the @code{Asm_Output} attribute of the target type;
29532 the general format is
29533
29534 @quotation
29535
29536 @example
29537 Type'Asm_Output (constraint_string, variable_name)
29538 @end example
29539 @end quotation
29540
29541 The constraint string directs the compiler how
29542 to store/access the associated variable. In the example
29543
29544 @quotation
29545
29546 @example
29547 Unsigned_32'Asm_Output ("=m", Flags);
29548 @end example
29549 @end quotation
29550
29551 the @code{"m"} (memory) constraint tells the compiler that the variable
29552 @code{Flags} should be stored in a memory variable, thus preventing
29553 the optimizer from keeping it in a register. In contrast,
29554
29555 @quotation
29556
29557 @example
29558 Unsigned_32'Asm_Output ("=r", Flags);
29559 @end example
29560 @end quotation
29561
29562 uses the @code{"r"} (register) constraint, telling the compiler to
29563 store the variable in a register.
29564
29565 If the constraint is preceded by the equal character '=', it tells
29566 the compiler that the variable will be used to store data into it.
29567
29568 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29569 allowing the optimizer to choose whatever it deems best.
29570
29571 There are a fairly large number of constraints, but the ones that are
29572 most useful (for the Intel x86 processor) are the following:
29573
29574 @quotation
29575
29576
29577 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
29578 @item
29579
29580 @emph{=}
29581
29582 @tab
29583
29584 output constraint
29585
29586 @item
29587
29588 @emph{g}
29589
29590 @tab
29591
29592 global (i.e., can be stored anywhere)
29593
29594 @item
29595
29596 @emph{m}
29597
29598 @tab
29599
29600 in memory
29601
29602 @item
29603
29604 @emph{I}
29605
29606 @tab
29607
29608 a constant
29609
29610 @item
29611
29612 @emph{a}
29613
29614 @tab
29615
29616 use eax
29617
29618 @item
29619
29620 @emph{b}
29621
29622 @tab
29623
29624 use ebx
29625
29626 @item
29627
29628 @emph{c}
29629
29630 @tab
29631
29632 use ecx
29633
29634 @item
29635
29636 @emph{d}
29637
29638 @tab
29639
29640 use edx
29641
29642 @item
29643
29644 @emph{S}
29645
29646 @tab
29647
29648 use esi
29649
29650 @item
29651
29652 @emph{D}
29653
29654 @tab
29655
29656 use edi
29657
29658 @item
29659
29660 @emph{r}
29661
29662 @tab
29663
29664 use one of eax, ebx, ecx or edx
29665
29666 @item
29667
29668 @emph{q}
29669
29670 @tab
29671
29672 use one of eax, ebx, ecx, edx, esi or edi
29673
29674 @end multitable
29675
29676 @end quotation
29677
29678 The full set of constraints is described in the gcc and @code{as}
29679 documentation; note that it is possible to combine certain constraints
29680 in one constraint string.
29681
29682 You specify the association of an output variable with an assembler operand
29683 through the @code{%@emph{n}} notation, where @emph{n} is a non-negative
29684 integer. Thus in
29685
29686 @quotation
29687
29688 @example
29689 Asm ("pushfl" & LF & HT & -- push flags on stack
29690 "popl %%eax" & LF & HT & -- load eax with flags
29691 "movl %%eax, %0", -- store flags in variable
29692 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29693 @end example
29694 @end quotation
29695
29696 @code{%0} will be replaced in the expanded code by the appropriate operand,
29697 whatever
29698 the compiler decided for the @code{Flags} variable.
29699
29700 In general, you may have any number of output variables:
29701
29702
29703 @itemize *
29704
29705 @item
29706 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29707
29708 @item
29709 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29710 of @code{Asm_Output} attributes
29711 @end itemize
29712
29713 For example:
29714
29715 @quotation
29716
29717 @example
29718 Asm ("movl %%eax, %0" & LF & HT &
29719 "movl %%ebx, %1" & LF & HT &
29720 "movl %%ecx, %2",
29721 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29722 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29723 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29724 @end example
29725 @end quotation
29726
29727 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29728 in the Ada program.
29729
29730 As a variation on the @code{Get_Flags} example, we can use the constraints
29731 string to direct the compiler to store the eax register into the @code{Flags}
29732 variable, instead of including the store instruction explicitly in the
29733 @code{Asm} template string:
29734
29735 @quotation
29736
29737 @example
29738 with Interfaces; use Interfaces;
29739 with Ada.Text_IO; use Ada.Text_IO;
29740 with System.Machine_Code; use System.Machine_Code;
29741 procedure Get_Flags_2 is
29742 Flags : Unsigned_32;
29743 use ASCII;
29744 begin
29745 Asm ("pushfl" & LF & HT & -- push flags on stack
29746 "popl %%eax", -- save flags in eax
29747 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29748 Put_Line ("Flags register:" & Flags'Img);
29749 end Get_Flags_2;
29750 @end example
29751 @end quotation
29752
29753 The @code{"a"} constraint tells the compiler that the @code{Flags}
29754 variable will come from the eax register. Here is the resulting code:
29755
29756 @quotation
29757
29758 @example
29759 #APP
29760 pushfl
29761 popl %eax
29762 #NO_APP
29763 movl %eax,-40(%ebp)
29764 @end example
29765 @end quotation
29766
29767 The compiler generated the store of eax into Flags after
29768 expanding the assembler code.
29769
29770 Actually, there was no need to pop the flags into the eax register;
29771 more simply, we could just pop the flags directly into the program variable:
29772
29773 @quotation
29774
29775 @example
29776 with Interfaces; use Interfaces;
29777 with Ada.Text_IO; use Ada.Text_IO;
29778 with System.Machine_Code; use System.Machine_Code;
29779 procedure Get_Flags_3 is
29780 Flags : Unsigned_32;
29781 use ASCII;
29782 begin
29783 Asm ("pushfl" & LF & HT & -- push flags on stack
29784 "pop %0", -- save flags in Flags
29785 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29786 Put_Line ("Flags register:" & Flags'Img);
29787 end Get_Flags_3;
29788 @end example
29789 @end quotation
29790
29791 @node Input Variables in Inline Assembler,Inlining Inline Assembler Code,Output Variables in Inline Assembler,Inline Assembler
29792 @anchor{gnat_ugn/inline_assembler id5}@anchor{255}@anchor{gnat_ugn/inline_assembler input-variables-in-inline-assembler}@anchor{256}
29793 @section Input Variables in Inline Assembler
29794
29795
29796 The example in this section illustrates how to specify the source operands
29797 for assembly language statements.
29798 The program simply increments its input value by 1:
29799
29800 @quotation
29801
29802 @example
29803 with Interfaces; use Interfaces;
29804 with Ada.Text_IO; use Ada.Text_IO;
29805 with System.Machine_Code; use System.Machine_Code;
29806 procedure Increment is
29807
29808 function Incr (Value : Unsigned_32) return Unsigned_32 is
29809 Result : Unsigned_32;
29810 begin
29811 Asm ("incl %0",
29812 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29813 Inputs => Unsigned_32'Asm_Input ("a", Value));
29814 return Result;
29815 end Incr;
29816
29817 Value : Unsigned_32;
29818
29819 begin
29820 Value := 5;
29821 Put_Line ("Value before is" & Value'Img);
29822 Value := Incr (Value);
29823 Put_Line ("Value after is" & Value'Img);
29824 end Increment;
29825 @end example
29826 @end quotation
29827
29828 The @code{Outputs} parameter to @code{Asm} specifies
29829 that the result will be in the eax register and that it is to be stored
29830 in the @code{Result} variable.
29831
29832 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29833 but with an @code{Asm_Input} attribute.
29834 The @code{"="} constraint, indicating an output value, is not present.
29835
29836 You can have multiple input variables, in the same way that you can have more
29837 than one output variable.
29838
29839 The parameter count (%0, %1) etc, still starts at the first output statement,
29840 and continues with the input statements.
29841
29842 Just as the @code{Outputs} parameter causes the register to be stored into the
29843 target variable after execution of the assembler statements, so does the
29844 @code{Inputs} parameter cause its variable to be loaded into the register
29845 before execution of the assembler statements.
29846
29847 Thus the effect of the @code{Asm} invocation is:
29848
29849
29850 @itemize *
29851
29852 @item
29853 load the 32-bit value of @code{Value} into eax
29854
29855 @item
29856 execute the @code{incl %eax} instruction
29857
29858 @item
29859 store the contents of eax into the @code{Result} variable
29860 @end itemize
29861
29862 The resulting assembler file (with @code{-O2} optimization) contains:
29863
29864 @quotation
29865
29866 @example
29867 _increment__incr.1:
29868 subl $4,%esp
29869 movl 8(%esp),%eax
29870 #APP
29871 incl %eax
29872 #NO_APP
29873 movl %eax,%edx
29874 movl %ecx,(%esp)
29875 addl $4,%esp
29876 ret
29877 @end example
29878 @end quotation
29879
29880 @node Inlining Inline Assembler Code,Other Asm Functionality,Input Variables in Inline Assembler,Inline Assembler
29881 @anchor{gnat_ugn/inline_assembler id6}@anchor{257}@anchor{gnat_ugn/inline_assembler inlining-inline-assembler-code}@anchor{258}
29882 @section Inlining Inline Assembler Code
29883
29884
29885 For a short subprogram such as the @code{Incr} function in the previous
29886 section, the overhead of the call and return (creating / deleting the stack
29887 frame) can be significant, compared to the amount of code in the subprogram
29888 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29889 which directs the compiler to expand invocations of the subprogram at the
29890 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29891 Here is the resulting program:
29892
29893 @quotation
29894
29895 @example
29896 with Interfaces; use Interfaces;
29897 with Ada.Text_IO; use Ada.Text_IO;
29898 with System.Machine_Code; use System.Machine_Code;
29899 procedure Increment_2 is
29900
29901 function Incr (Value : Unsigned_32) return Unsigned_32 is
29902 Result : Unsigned_32;
29903 begin
29904 Asm ("incl %0",
29905 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29906 Inputs => Unsigned_32'Asm_Input ("a", Value));
29907 return Result;
29908 end Incr;
29909 pragma Inline (Increment);
29910
29911 Value : Unsigned_32;
29912
29913 begin
29914 Value := 5;
29915 Put_Line ("Value before is" & Value'Img);
29916 Value := Increment (Value);
29917 Put_Line ("Value after is" & Value'Img);
29918 end Increment_2;
29919 @end example
29920 @end quotation
29921
29922 Compile the program with both optimization (@code{-O2}) and inlining
29923 (@code{-gnatn}) enabled.
29924
29925 The @code{Incr} function is still compiled as usual, but at the
29926 point in @code{Increment} where our function used to be called:
29927
29928 @quotation
29929
29930 @example
29931 pushl %edi
29932 call _increment__incr.1
29933 @end example
29934 @end quotation
29935
29936 the code for the function body directly appears:
29937
29938 @quotation
29939
29940 @example
29941 movl %esi,%eax
29942 #APP
29943 incl %eax
29944 #NO_APP
29945 movl %eax,%edx
29946 @end example
29947 @end quotation
29948
29949 thus saving the overhead of stack frame setup and an out-of-line call.
29950
29951 @node Other Asm Functionality,,Inlining Inline Assembler Code,Inline Assembler
29952 @anchor{gnat_ugn/inline_assembler other-asm-functionality}@anchor{259}@anchor{gnat_ugn/inline_assembler id7}@anchor{25a}
29953 @section Other @code{Asm} Functionality
29954
29955
29956 This section describes two important parameters to the @code{Asm}
29957 procedure: @code{Clobber}, which identifies register usage;
29958 and @code{Volatile}, which inhibits unwanted optimizations.
29959
29960 @menu
29961 * The Clobber Parameter::
29962 * The Volatile Parameter::
29963
29964 @end menu
29965
29966 @node The Clobber Parameter,The Volatile Parameter,,Other Asm Functionality
29967 @anchor{gnat_ugn/inline_assembler the-clobber-parameter}@anchor{25b}@anchor{gnat_ugn/inline_assembler id8}@anchor{25c}
29968 @subsection The @code{Clobber} Parameter
29969
29970
29971 One of the dangers of intermixing assembly language and a compiled language
29972 such as Ada is that the compiler needs to be aware of which registers are
29973 being used by the assembly code. In some cases, such as the earlier examples,
29974 the constraint string is sufficient to indicate register usage (e.g.,
29975 @code{"a"} for
29976 the eax register). But more generally, the compiler needs an explicit
29977 identification of the registers that are used by the Inline Assembly
29978 statements.
29979
29980 Using a register that the compiler doesn't know about
29981 could be a side effect of an instruction (like @code{mull}
29982 storing its result in both eax and edx).
29983 It can also arise from explicit register usage in your
29984 assembly code; for example:
29985
29986 @quotation
29987
29988 @example
29989 Asm ("movl %0, %%ebx" & LF & HT &
29990 "movl %%ebx, %1",
29991 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29992 Inputs => Unsigned_32'Asm_Input ("g", Var_In));
29993 @end example
29994 @end quotation
29995
29996 where the compiler (since it does not analyze the @code{Asm} template string)
29997 does not know you are using the ebx register.
29998
29999 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
30000 to identify the registers that will be used by your assembly code:
30001
30002 @quotation
30003
30004 @example
30005 Asm ("movl %0, %%ebx" & LF & HT &
30006 "movl %%ebx, %1",
30007 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30008 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30009 Clobber => "ebx");
30010 @end example
30011 @end quotation
30012
30013 The Clobber parameter is a static string expression specifying the
30014 register(s) you are using. Note that register names are @emph{not} prefixed
30015 by a percent sign. Also, if more than one register is used then their names
30016 are separated by commas; e.g., @code{"eax, ebx"}
30017
30018 The @code{Clobber} parameter has several additional uses:
30019
30020
30021 @itemize *
30022
30023 @item
30024 Use 'register' name @code{cc} to indicate that flags might have changed
30025
30026 @item
30027 Use 'register' name @code{memory} if you changed a memory location
30028 @end itemize
30029
30030 @node The Volatile Parameter,,The Clobber Parameter,Other Asm Functionality
30031 @anchor{gnat_ugn/inline_assembler the-volatile-parameter}@anchor{25d}@anchor{gnat_ugn/inline_assembler id9}@anchor{25e}
30032 @subsection The @code{Volatile} Parameter
30033
30034
30035 @geindex Volatile parameter
30036
30037 Compiler optimizations in the presence of Inline Assembler may sometimes have
30038 unwanted effects. For example, when an @code{Asm} invocation with an input
30039 variable is inside a loop, the compiler might move the loading of the input
30040 variable outside the loop, regarding it as a one-time initialization.
30041
30042 If this effect is not desired, you can disable such optimizations by setting
30043 the @code{Volatile} parameter to @code{True}; for example:
30044
30045 @quotation
30046
30047 @example
30048 Asm ("movl %0, %%ebx" & LF & HT &
30049 "movl %%ebx, %1",
30050 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30051 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30052 Clobber => "ebx",
30053 Volatile => True);
30054 @end example
30055 @end quotation
30056
30057 By default, @code{Volatile} is set to @code{False} unless there is no
30058 @code{Outputs} parameter.
30059
30060 Although setting @code{Volatile} to @code{True} prevents unwanted
30061 optimizations, it will also disable other optimizations that might be
30062 important for efficiency. In general, you should set @code{Volatile}
30063 to @code{True} only if the compiler's optimizations have created
30064 problems.
30065
30066 @node GNU Free Documentation License,Index,Inline Assembler,Top
30067 @anchor{share/gnu_free_documentation_license gnu-fdl}@anchor{1}@anchor{share/gnu_free_documentation_license doc}@anchor{25f}@anchor{share/gnu_free_documentation_license gnu-free-documentation-license}@anchor{260}
30068 @chapter GNU Free Documentation License
30069
30070
30071 Version 1.3, 3 November 2008
30072
30073 Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc
30074 @indicateurl{http://fsf.org/}
30075
30076 Everyone is permitted to copy and distribute verbatim copies of this
30077 license document, but changing it is not allowed.
30078
30079 @strong{Preamble}
30080
30081 The purpose of this License is to make a manual, textbook, or other
30082 functional and useful document "free" in the sense of freedom: to
30083 assure everyone the effective freedom to copy and redistribute it,
30084 with or without modifying it, either commercially or noncommercially.
30085 Secondarily, this License preserves for the author and publisher a way
30086 to get credit for their work, while not being considered responsible
30087 for modifications made by others.
30088
30089 This License is a kind of "copyleft", which means that derivative
30090 works of the document must themselves be free in the same sense. It
30091 complements the GNU General Public License, which is a copyleft
30092 license designed for free software.
30093
30094 We have designed this License in order to use it for manuals for free
30095 software, because free software needs free documentation: a free
30096 program should come with manuals providing the same freedoms that the
30097 software does. But this License is not limited to software manuals;
30098 it can be used for any textual work, regardless of subject matter or
30099 whether it is published as a printed book. We recommend this License
30100 principally for works whose purpose is instruction or reference.
30101
30102 @strong{1. APPLICABILITY AND DEFINITIONS}
30103
30104 This License applies to any manual or other work, in any medium, that
30105 contains a notice placed by the copyright holder saying it can be
30106 distributed under the terms of this License. Such a notice grants a
30107 world-wide, royalty-free license, unlimited in duration, to use that
30108 work under the conditions stated herein. The @strong{Document}, below,
30109 refers to any such manual or work. Any member of the public is a
30110 licensee, and is addressed as "@strong{you}". You accept the license if you
30111 copy, modify or distribute the work in a way requiring permission
30112 under copyright law.
30113
30114 A "@strong{Modified Version}" of the Document means any work containing the
30115 Document or a portion of it, either copied verbatim, or with
30116 modifications and/or translated into another language.
30117
30118 A "@strong{Secondary Section}" is a named appendix or a front-matter section of
30119 the Document that deals exclusively with the relationship of the
30120 publishers or authors of the Document to the Document's overall subject
30121 (or to related matters) and contains nothing that could fall directly
30122 within that overall subject. (Thus, if the Document is in part a
30123 textbook of mathematics, a Secondary Section may not explain any
30124 mathematics.) The relationship could be a matter of historical
30125 connection with the subject or with related matters, or of legal,
30126 commercial, philosophical, ethical or political position regarding
30127 them.
30128
30129 The "@strong{Invariant Sections}" are certain Secondary Sections whose titles
30130 are designated, as being those of Invariant Sections, in the notice
30131 that says that the Document is released under this License. If a
30132 section does not fit the above definition of Secondary then it is not
30133 allowed to be designated as Invariant. The Document may contain zero
30134 Invariant Sections. If the Document does not identify any Invariant
30135 Sections then there are none.
30136
30137 The "@strong{Cover Texts}" are certain short passages of text that are listed,
30138 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
30139 the Document is released under this License. A Front-Cover Text may
30140 be at most 5 words, and a Back-Cover Text may be at most 25 words.
30141
30142 A "@strong{Transparent}" copy of the Document means a machine-readable copy,
30143 represented in a format whose specification is available to the
30144 general public, that is suitable for revising the document
30145 straightforwardly with generic text editors or (for images composed of
30146 pixels) generic paint programs or (for drawings) some widely available
30147 drawing editor, and that is suitable for input to text formatters or
30148 for automatic translation to a variety of formats suitable for input
30149 to text formatters. A copy made in an otherwise Transparent file
30150 format whose markup, or absence of markup, has been arranged to thwart
30151 or discourage subsequent modification by readers is not Transparent.
30152 An image format is not Transparent if used for any substantial amount
30153 of text. A copy that is not "Transparent" is called @strong{Opaque}.
30154
30155 Examples of suitable formats for Transparent copies include plain
30156 ASCII without markup, Texinfo input format, LaTeX input format, SGML
30157 or XML using a publicly available DTD, and standard-conforming simple
30158 HTML, PostScript or PDF designed for human modification. Examples of
30159 transparent image formats include PNG, XCF and JPG. Opaque formats
30160 include proprietary formats that can be read and edited only by
30161 proprietary word processors, SGML or XML for which the DTD and/or
30162 processing tools are not generally available, and the
30163 machine-generated HTML, PostScript or PDF produced by some word
30164 processors for output purposes only.
30165
30166 The "@strong{Title Page}" means, for a printed book, the title page itself,
30167 plus such following pages as are needed to hold, legibly, the material
30168 this License requires to appear in the title page. For works in
30169 formats which do not have any title page as such, "Title Page" means
30170 the text near the most prominent appearance of the work's title,
30171 preceding the beginning of the body of the text.
30172
30173 The "@strong{publisher}" means any person or entity that distributes
30174 copies of the Document to the public.
30175
30176 A section "@strong{Entitled XYZ}" means a named subunit of the Document whose
30177 title either is precisely XYZ or contains XYZ in parentheses following
30178 text that translates XYZ in another language. (Here XYZ stands for a
30179 specific section name mentioned below, such as "@strong{Acknowledgements}",
30180 "@strong{Dedications}", "@strong{Endorsements}", or "@strong{History}".)
30181 To "@strong{Preserve the Title}"
30182 of such a section when you modify the Document means that it remains a
30183 section "Entitled XYZ" according to this definition.
30184
30185 The Document may include Warranty Disclaimers next to the notice which
30186 states that this License applies to the Document. These Warranty
30187 Disclaimers are considered to be included by reference in this
30188 License, but only as regards disclaiming warranties: any other
30189 implication that these Warranty Disclaimers may have is void and has
30190 no effect on the meaning of this License.
30191
30192 @strong{2. VERBATIM COPYING}
30193
30194 You may copy and distribute the Document in any medium, either
30195 commercially or noncommercially, provided that this License, the
30196 copyright notices, and the license notice saying this License applies
30197 to the Document are reproduced in all copies, and that you add no other
30198 conditions whatsoever to those of this License. You may not use
30199 technical measures to obstruct or control the reading or further
30200 copying of the copies you make or distribute. However, you may accept
30201 compensation in exchange for copies. If you distribute a large enough
30202 number of copies you must also follow the conditions in section 3.
30203
30204 You may also lend copies, under the same conditions stated above, and
30205 you may publicly display copies.
30206
30207 @strong{3. COPYING IN QUANTITY}
30208
30209 If you publish printed copies (or copies in media that commonly have
30210 printed covers) of the Document, numbering more than 100, and the
30211 Document's license notice requires Cover Texts, you must enclose the
30212 copies in covers that carry, clearly and legibly, all these Cover
30213 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
30214 the back cover. Both covers must also clearly and legibly identify
30215 you as the publisher of these copies. The front cover must present
30216 the full title with all words of the title equally prominent and
30217 visible. You may add other material on the covers in addition.
30218 Copying with changes limited to the covers, as long as they preserve
30219 the title of the Document and satisfy these conditions, can be treated
30220 as verbatim copying in other respects.
30221
30222 If the required texts for either cover are too voluminous to fit
30223 legibly, you should put the first ones listed (as many as fit
30224 reasonably) on the actual cover, and continue the rest onto adjacent
30225 pages.
30226
30227 If you publish or distribute Opaque copies of the Document numbering
30228 more than 100, you must either include a machine-readable Transparent
30229 copy along with each Opaque copy, or state in or with each Opaque copy
30230 a computer-network location from which the general network-using
30231 public has access to download using public-standard network protocols
30232 a complete Transparent copy of the Document, free of added material.
30233 If you use the latter option, you must take reasonably prudent steps,
30234 when you begin distribution of Opaque copies in quantity, to ensure
30235 that this Transparent copy will remain thus accessible at the stated
30236 location until at least one year after the last time you distribute an
30237 Opaque copy (directly or through your agents or retailers) of that
30238 edition to the public.
30239
30240 It is requested, but not required, that you contact the authors of the
30241 Document well before redistributing any large number of copies, to give
30242 them a chance to provide you with an updated version of the Document.
30243
30244 @strong{4. MODIFICATIONS}
30245
30246 You may copy and distribute a Modified Version of the Document under
30247 the conditions of sections 2 and 3 above, provided that you release
30248 the Modified Version under precisely this License, with the Modified
30249 Version filling the role of the Document, thus licensing distribution
30250 and modification of the Modified Version to whoever possesses a copy
30251 of it. In addition, you must do these things in the Modified Version:
30252
30253
30254 @enumerate A
30255
30256 @item
30257 Use in the Title Page (and on the covers, if any) a title distinct
30258 from that of the Document, and from those of previous versions
30259 (which should, if there were any, be listed in the History section
30260 of the Document). You may use the same title as a previous version
30261 if the original publisher of that version gives permission.
30262
30263 @item
30264 List on the Title Page, as authors, one or more persons or entities
30265 responsible for authorship of the modifications in the Modified
30266 Version, together with at least five of the principal authors of the
30267 Document (all of its principal authors, if it has fewer than five),
30268 unless they release you from this requirement.
30269
30270 @item
30271 State on the Title page the name of the publisher of the
30272 Modified Version, as the publisher.
30273
30274 @item
30275 Preserve all the copyright notices of the Document.
30276
30277 @item
30278 Add an appropriate copyright notice for your modifications
30279 adjacent to the other copyright notices.
30280
30281 @item
30282 Include, immediately after the copyright notices, a license notice
30283 giving the public permission to use the Modified Version under the
30284 terms of this License, in the form shown in the Addendum below.
30285
30286 @item
30287 Preserve in that license notice the full lists of Invariant Sections
30288 and required Cover Texts given in the Document's license notice.
30289
30290 @item
30291 Include an unaltered copy of this License.
30292
30293 @item
30294 Preserve the section Entitled "History", Preserve its Title, and add
30295 to it an item stating at least the title, year, new authors, and
30296 publisher of the Modified Version as given on the Title Page. If
30297 there is no section Entitled "History" in the Document, create one
30298 stating the title, year, authors, and publisher of the Document as
30299 given on its Title Page, then add an item describing the Modified
30300 Version as stated in the previous sentence.
30301
30302 @item
30303 Preserve the network location, if any, given in the Document for
30304 public access to a Transparent copy of the Document, and likewise
30305 the network locations given in the Document for previous versions
30306 it was based on. These may be placed in the "History" section.
30307 You may omit a network location for a work that was published at
30308 least four years before the Document itself, or if the original
30309 publisher of the version it refers to gives permission.
30310
30311 @item
30312 For any section Entitled "Acknowledgements" or "Dedications",
30313 Preserve the Title of the section, and preserve in the section all
30314 the substance and tone of each of the contributor acknowledgements
30315 and/or dedications given therein.
30316
30317 @item
30318 Preserve all the Invariant Sections of the Document,
30319 unaltered in their text and in their titles. Section numbers
30320 or the equivalent are not considered part of the section titles.
30321
30322 @item
30323 Delete any section Entitled "Endorsements". Such a section
30324 may not be included in the Modified Version.
30325
30326 @item
30327 Do not retitle any existing section to be Entitled "Endorsements"
30328 or to conflict in title with any Invariant Section.
30329
30330 @item
30331 Preserve any Warranty Disclaimers.
30332 @end enumerate
30333
30334 If the Modified Version includes new front-matter sections or
30335 appendices that qualify as Secondary Sections and contain no material
30336 copied from the Document, you may at your option designate some or all
30337 of these sections as invariant. To do this, add their titles to the
30338 list of Invariant Sections in the Modified Version's license notice.
30339 These titles must be distinct from any other section titles.
30340
30341 You may add a section Entitled "Endorsements", provided it contains
30342 nothing but endorsements of your Modified Version by various
30343 parties---for example, statements of peer review or that the text has
30344 been approved by an organization as the authoritative definition of a
30345 standard.
30346
30347 You may add a passage of up to five words as a Front-Cover Text, and a
30348 passage of up to 25 words as a Back-Cover Text, to the end of the list
30349 of Cover Texts in the Modified Version. Only one passage of
30350 Front-Cover Text and one of Back-Cover Text may be added by (or
30351 through arrangements made by) any one entity. If the Document already
30352 includes a cover text for the same cover, previously added by you or
30353 by arrangement made by the same entity you are acting on behalf of,
30354 you may not add another; but you may replace the old one, on explicit
30355 permission from the previous publisher that added the old one.
30356
30357 The author(s) and publisher(s) of the Document do not by this License
30358 give permission to use their names for publicity for or to assert or
30359 imply endorsement of any Modified Version.
30360
30361 @strong{5. COMBINING DOCUMENTS}
30362
30363 You may combine the Document with other documents released under this
30364 License, under the terms defined in section 4 above for modified
30365 versions, provided that you include in the combination all of the
30366 Invariant Sections of all of the original documents, unmodified, and
30367 list them all as Invariant Sections of your combined work in its
30368 license notice, and that you preserve all their Warranty Disclaimers.
30369
30370 The combined work need only contain one copy of this License, and
30371 multiple identical Invariant Sections may be replaced with a single
30372 copy. If there are multiple Invariant Sections with the same name but
30373 different contents, make the title of each such section unique by
30374 adding at the end of it, in parentheses, the name of the original
30375 author or publisher of that section if known, or else a unique number.
30376 Make the same adjustment to the section titles in the list of
30377 Invariant Sections in the license notice of the combined work.
30378
30379 In the combination, you must combine any sections Entitled "History"
30380 in the various original documents, forming one section Entitled
30381 "History"; likewise combine any sections Entitled "Acknowledgements",
30382 and any sections Entitled "Dedications". You must delete all sections
30383 Entitled "Endorsements".
30384
30385 @strong{6. COLLECTIONS OF DOCUMENTS}
30386
30387 You may make a collection consisting of the Document and other documents
30388 released under this License, and replace the individual copies of this
30389 License in the various documents with a single copy that is included in
30390 the collection, provided that you follow the rules of this License for
30391 verbatim copying of each of the documents in all other respects.
30392
30393 You may extract a single document from such a collection, and distribute
30394 it individually under this License, provided you insert a copy of this
30395 License into the extracted document, and follow this License in all
30396 other respects regarding verbatim copying of that document.
30397
30398 @strong{7. AGGREGATION WITH INDEPENDENT WORKS}
30399
30400 A compilation of the Document or its derivatives with other separate
30401 and independent documents or works, in or on a volume of a storage or
30402 distribution medium, is called an "aggregate" if the copyright
30403 resulting from the compilation is not used to limit the legal rights
30404 of the compilation's users beyond what the individual works permit.
30405 When the Document is included in an aggregate, this License does not
30406 apply to the other works in the aggregate which are not themselves
30407 derivative works of the Document.
30408
30409 If the Cover Text requirement of section 3 is applicable to these
30410 copies of the Document, then if the Document is less than one half of
30411 the entire aggregate, the Document's Cover Texts may be placed on
30412 covers that bracket the Document within the aggregate, or the
30413 electronic equivalent of covers if the Document is in electronic form.
30414 Otherwise they must appear on printed covers that bracket the whole
30415 aggregate.
30416
30417 @strong{8. TRANSLATION}
30418
30419 Translation is considered a kind of modification, so you may
30420 distribute translations of the Document under the terms of section 4.
30421 Replacing Invariant Sections with translations requires special
30422 permission from their copyright holders, but you may include
30423 translations of some or all Invariant Sections in addition to the
30424 original versions of these Invariant Sections. You may include a
30425 translation of this License, and all the license notices in the
30426 Document, and any Warranty Disclaimers, provided that you also include
30427 the original English version of this License and the original versions
30428 of those notices and disclaimers. In case of a disagreement between
30429 the translation and the original version of this License or a notice
30430 or disclaimer, the original version will prevail.
30431
30432 If a section in the Document is Entitled "Acknowledgements",
30433 "Dedications", or "History", the requirement (section 4) to Preserve
30434 its Title (section 1) will typically require changing the actual
30435 title.
30436
30437 @strong{9. TERMINATION}
30438
30439 You may not copy, modify, sublicense, or distribute the Document
30440 except as expressly provided under this License. Any attempt
30441 otherwise to copy, modify, sublicense, or distribute it is void, and
30442 will automatically terminate your rights under this License.
30443
30444 However, if you cease all violation of this License, then your license
30445 from a particular copyright holder is reinstated (a) provisionally,
30446 unless and until the copyright holder explicitly and finally
30447 terminates your license, and (b) permanently, if the copyright holder
30448 fails to notify you of the violation by some reasonable means prior to
30449 60 days after the cessation.
30450
30451 Moreover, your license from a particular copyright holder is
30452 reinstated permanently if the copyright holder notifies you of the
30453 violation by some reasonable means, this is the first time you have
30454 received notice of violation of this License (for any work) from that
30455 copyright holder, and you cure the violation prior to 30 days after
30456 your receipt of the notice.
30457
30458 Termination of your rights under this section does not terminate the
30459 licenses of parties who have received copies or rights from you under
30460 this License. If your rights have been terminated and not permanently
30461 reinstated, receipt of a copy of some or all of the same material does
30462 not give you any rights to use it.
30463
30464 @strong{10. FUTURE REVISIONS OF THIS LICENSE}
30465
30466 The Free Software Foundation may publish new, revised versions
30467 of the GNU Free Documentation License from time to time. Such new
30468 versions will be similar in spirit to the present version, but may
30469 differ in detail to address new problems or concerns. See
30470 @indicateurl{http://www.gnu.org/copyleft/}.
30471
30472 Each version of the License is given a distinguishing version number.
30473 If the Document specifies that a particular numbered version of this
30474 License "or any later version" applies to it, you have the option of
30475 following the terms and conditions either of that specified version or
30476 of any later version that has been published (not as a draft) by the
30477 Free Software Foundation. If the Document does not specify a version
30478 number of this License, you may choose any version ever published (not
30479 as a draft) by the Free Software Foundation. If the Document
30480 specifies that a proxy can decide which future versions of this
30481 License can be used, that proxy's public statement of acceptance of a
30482 version permanently authorizes you to choose that version for the
30483 Document.
30484
30485 @strong{11. RELICENSING}
30486
30487 "Massive Multiauthor Collaboration Site" (or "MMC Site") means any
30488 World Wide Web server that publishes copyrightable works and also
30489 provides prominent facilities for anybody to edit those works. A
30490 public wiki that anybody can edit is an example of such a server. A
30491 "Massive Multiauthor Collaboration" (or "MMC") contained in the
30492 site means any set of copyrightable works thus published on the MMC
30493 site.
30494
30495 "CC-BY-SA" means the Creative Commons Attribution-Share Alike 3.0
30496 license published by Creative Commons Corporation, a not-for-profit
30497 corporation with a principal place of business in San Francisco,
30498 California, as well as future copyleft versions of that license
30499 published by that same organization.
30500
30501 "Incorporate" means to publish or republish a Document, in whole or
30502 in part, as part of another Document.
30503
30504 An MMC is "eligible for relicensing" if it is licensed under this
30505 License, and if all works that were first published under this License
30506 somewhere other than this MMC, and subsequently incorporated in whole
30507 or in part into the MMC, (1) had no cover texts or invariant sections,
30508 and (2) were thus incorporated prior to November 1, 2008.
30509
30510 The operator of an MMC Site may republish an MMC contained in the site
30511 under CC-BY-SA on the same site at any time before August 1, 2009,
30512 provided the MMC is eligible for relicensing.
30513
30514 @strong{ADDENDUM: How to use this License for your documents}
30515
30516 To use this License in a document you have written, include a copy of
30517 the License in the document and put the following copyright and
30518 license notices just after the title page:
30519
30520 @quotation
30521
30522 Copyright © YEAR YOUR NAME.
30523 Permission is granted to copy, distribute and/or modify this document
30524 under the terms of the GNU Free Documentation License, Version 1.3
30525 or any later version published by the Free Software Foundation;
30526 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
30527 A copy of the license is included in the section entitled "GNU
30528 Free Documentation License".
30529 @end quotation
30530
30531 If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
30532 replace the "with ... Texts." line with this:
30533
30534 @quotation
30535
30536 with the Invariant Sections being LIST THEIR TITLES, with the
30537 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
30538 @end quotation
30539
30540 If you have Invariant Sections without Cover Texts, or some other
30541 combination of the three, merge those two alternatives to suit the
30542 situation.
30543
30544 If your document contains nontrivial examples of program code, we
30545 recommend releasing these examples in parallel under your choice of
30546 free software license, such as the GNU General Public License,
30547 to permit their use in free software.
30548
30549 @node Index,,GNU Free Documentation License,Top
30550 @unnumbered Index
30551
30552
30553 @printindex ge
30554
30555 @anchor{de}@w{ }
30556 @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ }
30557
30558 @c %**end of body
30559 @bye
This page took 1.414937 seconds and 4 git commands to generate.