OSDN Git Service

2004-08-06 Steven G. Kargl <kargls@comcast.net>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / gfortran.texi
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename gfortran.info
4 @set last-update 13 May 2004
5 @set copyrights-gfortran 1999-2004
6
7 @include gcc-common.texi
8
9 @settitle The GNU Fortran 95 Compiler
10
11 @c Create a separate index for command line options
12 @defcodeindex op
13 @c Merge the standard indexes into a single one.
14 @syncodeindex fn cp
15 @syncodeindex vr cp
16 @syncodeindex ky cp
17 @syncodeindex pg cp
18 @syncodeindex tp cp
19
20 @c %**end of header
21
22 @c Use with @@smallbook.
23
24 @c %** start of document
25
26 @c Cause even numbered pages to be printed on the left hand side of
27 @c the page and odd numbered pages to be printed on the right hand
28 @c side of the page.  Using this, you can print on both sides of a
29 @c sheet of paper and have the text on the same part of the sheet.
30
31 @c The text on right hand pages is pushed towards the right hand
32 @c margin and the text on left hand pages is pushed toward the left
33 @c hand margin.
34 @c (To provide the reverse effect, set bindingoffset to -0.75in.)
35
36 @c @tex
37 @c \global\bindingoffset=0.75in
38 @c \global\normaloffset =0.75in
39 @c @end tex
40
41 @copying
42 Copyright @copyright{} @value{copyrights-gfortran} Free Software Foundation, Inc.
43
44 Permission is granted to copy, distribute and/or modify this document
45 under the terms of the GNU Free Documentation License, Version 1.1 or
46 any later version published by the Free Software Foundation; with the
47 Invariant Sections being ``GNU General Public License'' and ``Funding
48 Free Software'', the Front-Cover
49 texts being (a) (see below), and with the Back-Cover Texts being (b)
50 (see below).  A copy of the license is included in the section entitled
51 ``GNU Free Documentation License''.
52
53 (a) The FSF's Front-Cover Text is:
54
55      A GNU Manual
56
57 (b) The FSF's Back-Cover Text is:
58
59      You have freedom to copy and modify this GNU Manual, like GNU
60      software.  Copies published by the Free Software Foundation raise
61      funds for GNU development.
62 @end copying
63
64 @ifinfo
65 @dircategory Programming
66 @direntry
67 * gfortran: (gfortran).                  The GNU Fortran 95 Compiler.
68 @end direntry
69 This file documents the use and the internals of
70 the GNU Fortran 95 compiler, (@command{gfortran}).
71
72 Published by the Free Software Foundation
73 59 Temple Place - Suite 330
74 Boston, MA 02111-1307 USA
75
76 @insertcopying
77 @end ifinfo
78
79 Contributed by Steven Bosscher (@email{s.bosscher@@gcc.gnu.org}).
80
81 @setchapternewpage odd
82 @titlepage
83 @title Using GNU Fortran 95
84 @sp 2
85 @center Steven Bosscher
86 @sp 3 
87 @center Last updated @value{last-update}
88 @sp 1
89 @center for version @value {version-GCC}
90 @page
91 @vskip 0pt plus 1filll
92 For the @value{version-GCC} Version*
93 @sp 1
94 Published by the Free Software Foundation @*
95 59 Temple Place - Suite 330@*
96 Boston, MA 02111-1307, USA@*
97 @c Last printed ??ber, 19??.@*
98 @c Printed copies are available for $? each.@*
99 @c ISBN ???
100 @sp 1
101 @insertcopying
102 @end titlepage
103 @summarycontents
104 @contents
105 @page
106
107 @node Top, Copying,, (DIR)
108 @top Introduction
109 @cindex Introduction
110
111 This manual documents the use of @command{gfortran}, 
112 the GNU Fortran 95 compiler. You can find in this manual how to invoke
113 @command{gfortran}, as well as its features and incompatibilities.
114
115 @ifset DEVELOPMENT
116 @emph{Warning:} This document, and the compiler it describes, are still
117 under development.  While efforts are made too keep it up-to-date it might
118 not accurately reflect the status of the most recent @command{gfortran}.
119 @end ifset
120
121 @menu
122 * Copying::         GNU General Public License says
123                     how you can copy and share GNU Fortran.
124 * GNU Free Documentation License::
125                     How you can copy and share this manual.
126 * Funding::         How to help assure continued work for free software.
127 * Getting Started:: What you should know about @command{gfortran}.
128 * GFORTRAN and GCC::        You can compile Fortran, C, or other programs.
129 * GFORTRAN and G77::     Why we choose to start from scratch.
130 * Invoking GFORTRAN::    Command options supported by @command{gfortran}.
131 * Project Status::  Status of GFORTRAN, Roadmap, proposed extensions.
132 * Contributing::    Helping you can help.
133 * Standards::       Standards supported by GFORTRAN.
134 * Index::           Index of this documentation.
135 @end menu
136
137
138
139 @c ---------------------------------------------------------------------
140 @c GNU General Public License
141 @c ---------------------------------------------------------------------
142
143 @include gpl.texi
144
145
146
147 @c ---------------------------------------------------------------------
148 @c GNU Free Documentation License
149 @c ---------------------------------------------------------------------
150
151 @include fdl.texi
152
153
154
155 @c ---------------------------------------------------------------------
156 @c Funding Free Software
157 @c ---------------------------------------------------------------------
158
159 @include funding.texi
160
161
162
163 @c ---------------------------------------------------------------------
164 @c Getting Started
165 @c ---------------------------------------------------------------------
166
167 @node Getting Started
168 @chapter Getting Started
169
170 Gfortran is the GNU Fortran 95 compiler front end,
171 designed initially as a free replacement for,
172 or alternative to, the unix @command{f95} command;
173 @command{gfortran} is command you'll use to invoke the compiler.
174
175 Gfortran is still in an early state of development.
176 @command{gfortran} can generate code for most constructs and expressions,
177 but much work remains to be done.
178
179 When @command{gfortran} is finished,
180 it will do everything you expect from any decent compiler: 
181
182 @itemize @bullet
183 @item
184 Read a user's program,
185 stored in a file and containing instructions written
186 in Fortran 77, Fortran 90 or Fortran 95.
187 This file contains @dfn{source code}.
188
189 @item
190 Translate the user's program into instructions a computer
191 can carry out more quickly than it takes to translate the
192 instructions in the first
193 place.  The result after compilation of a program is
194 @dfn{machine code},
195 code designed to be efficiently translated and processed
196 by a machine such as your computer.
197 Humans usually aren't as good writing machine code
198 as they are at writing Fortran (or C++, Ada, or Java),
199 because is easy to make tiny mistakes writing machine code.
200
201 @item
202 Provide the user with information about the reasons why
203 the compiler is unable to create a binary from the source code.
204 Usually this will be the case if the source code is flawed.
205 When writing Fortran, it is easy to make big mistakes.
206 The Fortran 90 requires that the compiler can point out
207 mistakes to the user.
208 An incorrect usage of the language causes an @dfn{error message}.
209
210 The compiler will also attempt to diagnose cases where the
211 user's program contains a correct usage of the language,
212 but instructs the computer to do something questionable.
213 This kind of diagnostics message is called a @dfn{warning message}.
214
215 @item
216 Provide optional information about the translation passes
217 from the source code to machine code.
218 This can help a user of the compiler to find the cause of
219 certain bugs which may not be obvious in the source code,
220 but may be more easily found at a lower level compiler output.
221 It also helps developers to find bugs in the compiler itself.
222
223 @item
224 Provide information in the generated machine code that can
225 make it easier to find bugs in the program (using a debugging tool,
226 called a @dfn{debugger}, such as the GNU Debugger @command{gdb}). 
227
228 @item
229 Locate and gather machine code already generated to
230 perform actions requested by statements in the user's program.
231 This machine code is organized into @dfn{modules} and is located
232 and @dfn{linked} to the user program. 
233 @end itemize
234
235 Gfortran consists of several components:
236
237 @itemize @bullet
238 @item
239 A version of the @command{gcc} command
240 (which also might be installed as the system's @command{cc} command)
241 that also understands and accepts Fortran source code.
242 The @command{gcc} command is the @dfn{driver} program for
243 all the languages in the GNU Compiler Collection (GCC);
244 With @command{gcc},
245 you can compiler the source code of any language for
246 which a front end is available in GCC.
247
248 @item
249 The @command{gfortran} command itself,
250 which also might be installed as the
251 system's @command{f95} command.
252 @command{gfortran} is just another driver program,
253 but specifically for the Fortran 95 compiler only.
254 The difference with @command{gcc} is that @command{gfortran}
255 will automatically link the correct libraries to your program.
256
257 @item
258 A collection of run-time libraries.
259 These libraries contains the machine code needed to support
260 capabilities of the Fortran language that are not directly
261 provided by the machine code generated by the
262 @command{gfortran} compilation phase,
263 such as intrinsic functions and subroutines,
264 and routines for interaction with files and the operating system.
265 @c and mechanisms to spawn,
266 @c unleash and pause threads in parallelized code.
267
268 @item
269 The Fortran compiler itself, (@command{f951}).
270 This is the gfortran parser and code generator,
271 linked to and interfaced with the GCC backend library.
272 @command{f951} ``translates'' the source code to
273 assembler code.  You would typically not use this
274 program directly;
275 instead, the @command{gcc} or @command{gfortran} driver
276 programs will call it for you.
277 @end itemize
278
279
280
281 @c ---------------------------------------------------------------------
282 @c GFORTRAN and GCC
283 @c ---------------------------------------------------------------------
284
285 @node GFORTRAN and GCC
286 @chapter GFORTRAN and GCC
287 @cindex GNU Compiler Collection
288
289 GCC used to be the GNU ``C'' Compiler,
290 but is now known as the @dfn{GNU Compiler Collection}.
291 GCC provides the GNU system with a very versatile
292 compiler middle end (shared optimization passes),
293 and with back ends (code generators) for many different
294 computer architectures and operating systems.
295 The code of the middle end and back end are shared by all
296 compiler front ends that are in the GNU Compiler Collection.
297
298 A GCC front end is essentially a source code parser
299 and a pass to generate a representation of the semantics
300 of the program in the source code in the GCC language
301 independent intermediate language,
302 called @dfn{GENERIC}.
303
304 The parser takes a source file written in a
305 particular computer language, reads and parses it,
306 and tries to make sure that the source code conforms to
307 the language rules.
308 Once the correctness of a program has been established,
309 the compiler will build a data structure known as the
310 @dfn{Abstract Syntax tree},
311 or just @dfn{AST} or ``tree'' for short.
312 This data structure represents the whole program
313 or a subroutine or a function.
314 The ``tree'' is passed to the GCC middle end,
315 which will perform optimization passes on it,
316 pass the optimized AST and generate assembly
317 for the program unit.
318
319 Different phases in this translation process can be,
320 and in fact @emph{are} merged in many compiler front ends.
321 GNU Fortran 95 has a strict separation between the
322 parser and code generator.
323
324 The goal of the gfortran project is to build a new front end for GCC:
325 A Fortran 95 front end.
326 In a non-gfortran installation,
327 @command{gcc} will not be able to compile Fortran 95 source code
328 (only the ``C'' front end has to be compiled if you want to build GCC,
329 all other languages are optional).
330 If you build GCC with gfortran, @command{gcc} will recognize
331 @file{.f/.f90/.f95} source files and accepts Fortran 95 specific
332 command line options.
333
334
335
336 @c ---------------------------------------------------------------------
337 @c GFORTRAN and G77
338 @c ---------------------------------------------------------------------
339
340 @node GFORTRAN and G77
341 @chapter GFORTRAN and G77
342 @cindex Fortran 77
343 @cindex G77
344
345 Why do we write a compiler front end from scratch? 
346 There's a fine Fortran 77 compiler in the
347 GNU Compiler Collection that accepts some features
348 of the Fortran 90 standard as extensions.
349 Why not start from there and revamp it?
350
351 One of the reasons is that Craig Burley, the author of G77,
352 has decided to stop working on the G77 front end.
353 On @uref{http://world.std.com/~burley/g77-why.html,
354 Craig explains the reasons for his decision to stop working on G77}
355 in one of the pages in his homepage.
356 Among the reasons is a lack of interest in improvements to
357 @command{g77}.
358 Users appear to be quite satisfied with @command{g77} as it is.
359 While @command{g77} is still being maintained (by Toon Moene),
360 it is unlikely that sufficient people will be willing
361 to completely rewrite the existing code. 
362
363 But there are other reasons to start from scratch.
364 Many people, including Craig Burley,
365 no longer agreed with certain design decisions in the G77 front end.
366 Also, the interface of @command{g77} to the back end is written in
367 a style which is confusing and not up to date on recommended practice.
368 In fact, a full rewrite had already been planned for GCC 3.0.
369
370 When Craig decided to stop,
371 it just seemed to be a better idea to start a new project from scratch,
372 because it was expected to be easier to maintain code we
373 develop ourselves than to do a major overhaul of @command{g77} first,
374 and then build a Fortran 95 compiler out of it.
375
376
377 @include invoke.texi
378
379 @c ---------------------------------------------------------------------
380 @c Project Status
381 @c ---------------------------------------------------------------------
382
383 @node Project Status
384 @chapter Project Status
385
386 @quotation
387 As soon as gfortran can parse all of the statements correctly,
388 it will be in the ``larva'' state.
389 When we generate code, the ``puppa'' state.
390 When gfortran is done,
391 we'll see if it will be a beautiful butterfly,
392 or just a big bug....
393
394 --Andy Vaught, April 2000
395 @end quotation
396
397 The start of the GNU Fortran 95 project was announced on
398 the GCC homepage in March 18, 2000
399 (even though Andy had already been working on it for a while,
400 or course).
401
402 Gfortran is currently reaching the stage where is is able to compile real
403 world programs.  However it is still under development and has many rough
404 edges.
405
406 @menu
407 * Compiler Status::
408 * Library Status::
409 * Proposed Extensions::
410 @end menu
411
412 @node Compiler Status
413 @section Compiler Status
414
415 @table @emph
416 @item Front end
417 This is the part of gfortran which parses a source file, verifies that it
418 is valid Fortran 95, performs compile time replacement of constants
419 (PARAMETER variables) and reads and generate module files. This is
420 almost complete. Every Fortran 95 source should be accepted, and most
421 none-Fortran 95 source should be rejected. If you find a source file where
422 this is not true, please tell us. You can use the -fsyntax-only switch to
423 make gfortran quit after running the front end, effectively reducing it to
424 a syntax checker.
425
426 @item Middle end interface
427 These are the parts of gfortran that take the parse tree generated by the
428 front end and translate it to the GENERIC form required by the GCC back
429 end. Work is ongoing in these parts of gfortran, but a large part has
430 already been completed.
431 @end table
432
433 @node Library Status
434 @section Library Status
435
436 Some intrinsic functions map directly to library functions, and in most
437 cases the name of the library function used depends on the type of the
438 arguments.  For some intrinsics we generate inline code, and for others,
439 such as sin, cos and sqrt, we rely on the backend to use special
440 instructions in the floating point unit of the CPU if available, or to
441 fall back to a call to libm if these are not available.
442
443 Implementation of some non-elemental intrinsic functions (eg. DOT_PRODUCT,
444 AVERAGE) is not yet optimal. This is hard because we have to make decisions
445 whether to use inline code (good for small arrays as no function call
446 overhead occurs) or generate function calls (good for large arrays as it
447 allows use of hand-optimized assembly routines, SIMD instructions, etc.)
448
449 The IO library is still under development.  The following features should be
450 usable for real programs:
451
452 @itemize @minus
453 @item List directed
454 @item Unformatted sequential
455 @end itemize
456
457 Usable with bugs:
458
459 @itemize @minus
460 @item Formatted sequential ('T' edit descriptor, and others)
461 @item Namelist (can read a namelist that it writes, but not free-form)
462 @end itemize
463
464 Not recommended:
465
466 @itemize @minus
467 @item Unformatted direct access
468 @item Formatted direct access
469 @end itemize
470
471 Many Fortran programs only use a small subset of the available IO
472 capabilities, so your milage may vary.
473
474 @node Proposed Extensions
475 @section Proposed Extensions
476
477 Here's a list of proposed extensions for @command{gfortran}, in no particular
478 order.  Most of these are necessary to be fully compatible with
479 existing Fortran compilers, but they are not part of the official
480 J3 Fortran 95 standard.
481
482 @subsection Compiler extensions: 
483 @itemize @bullet
484 @item
485 Flag for defining the kind number for default logicals.
486
487 @item
488 User-specified alignment rules for structures.
489 @item
490 Flag to generate a @code{Makefile} info.
491
492 @item
493 Automatically extend single precision constants to double.
494
495 @item
496 Cray pointers (this was high on the @command{g77} wishlist).
497
498 @item
499 Compile code that conserves memory by dynamically allocating common and
500 module storage either on stack or heap.
501
502 @item
503 Flag to cause the compiler to distinguish between upper and lower case
504 names.  The Fortran 95 standard does not distinguish them.
505
506 @item
507 Compile switch for changing the interpretation of a backslash from a
508 character to ``C''-style escape characters.
509
510 @item
511 Compile flag to generate code for array conformance checking (suggest -CC).
512
513 @item
514 User control of symbol names (underscores, etc).
515
516 @item
517 Compile setting for maximum size of stack frame size before spilling
518 parts to static or heap.
519
520 @item
521 Flag to force local variables into static space.
522
523 @item
524 Flag to force local variables onto stack.
525
526 @item
527 Flag to compile lines beginning with ``D''.
528
529 @item
530 Flag to ignore lines beginning with ``D''.
531
532 @item
533 Flag for maximum errors before ending compile.
534
535 @item
536 Generate code to check for null pointer dereferences -- prints locus of
537 dereference instead of segfaulting.  There was some discussion about this
538 option in the g95 development mailing list.
539
540 @item
541 Allow setting default unit number.
542
543 @item
544 Option to initialize of otherwise uninitialized integer and floating
545 point variables.
546
547 @item
548 Support for OpenMP directives.  This also requires support from the runtime
549 library and the rest of the compiler.
550
551 @item
552 Support for Fortran 200x. This includes several new features including
553 floating point exceptions, extended use of allocatable arrays, C
554 interoperability, Parameterizer data types and function pointers.
555 @end itemize
556
557
558 @subsection Environment Options
559 @itemize @bullet
560 @item
561 Pluggable library modules for random numbers, linear algebra.
562 LA should use BLAS calling conventions.
563
564 @item
565 Environment variables controlling actions on arithmetic exceptions like
566 overflow, underflow, precision loss -- Generate NaN, abort, default.
567 action.
568
569 @item
570 Set precision for fp units that support it (i387).
571
572 @item
573 Variables for setting fp rounding mode.
574
575 @item
576 Support old style namelists ending in $end or &end.
577
578 @item
579 Variable to fill uninitialized variables with a user-defined bit
580 pattern.
581
582 @item
583 Environment variable controlling filename that is opened for that unit
584 number.
585
586 @item
587 Environment variable to clear/trash memory being freed.
588
589 @item
590 Environment variable to control tracing of allocations and frees.
591
592 @item
593 Environment variable to display allocated memory at normal program end.
594
595 @item
596 Environment variable for filename for * IO-unit.
597
598 @item
599 Environment variable for temporary file directory.
600
601 @item
602 Environment variable forcing standard output to be line buffered (unix).
603
604 @item
605 Variable for swapping endianness during unformatted read.
606
607 @item
608 Variable for swapping Endianness during unformatted write.
609 @end itemize
610
611
612
613 @c ---------------------------------------------------------------------
614 @c Contributing
615 @c ---------------------------------------------------------------------
616
617 @node Contributing
618 @chapter Contributing
619 @cindex Contributing
620
621 Free software is only possible if people contribute to efforts
622 to create it.
623 We're always in need of more people helping out with ideas
624 and comments, writing documentation and contributing code.
625
626 If you want to contribute to GNU Fortran 95,
627 have a look at the long lists of projects you can take on.
628 Some of these projects are small,
629 some of them are large;
630 some are completely orthogonal to the rest of what is
631 happening on @command{gfortran},
632 but others are ``mainstream'' projects in need of enthusiastic hackers.
633 All of these projects are important!
634 We'll eventually get around to the things here,
635 but they are also things doable by someone who is willing and able.
636
637 @menu
638 * Contributors::
639 * Projects::
640 @end menu
641
642
643 @node Contributors
644 @section Contributors to GNU Fortran 95
645 @cindex Contributors
646 @cindex Credits
647 @cindex Authors
648
649 Most of the parser was hand-crafted by @emph{Andy Vaught}, who is
650 also the initiator of the whole project.  Thanks Andy!
651 Most of the interface with GCC was written by @emph{Paul Brook}.
652
653 The following individuals have contributed code and/or
654 ideas and significant help to the gfortran project
655 (in no particular order): 
656
657 @itemize @minus
658 @item Andy Vaught
659 @item Katherine Holcomb
660 @item Tobias Schlüter
661 @item Steven Bosscher
662 @item Toon Moene
663 @item Tim Prince
664 @item Niels Kristian Bech Jensen
665 @item Steven Johnson
666 @item Paul Brook
667 @item Feng Wang
668 @item Bud Davis
669 @end itemize
670
671 The following people have contributed bug reports,
672 smaller or larger patches,
673 and much needed feedback and encouragement for the
674 @command{gfortran} project: 
675
676 @itemize @minus
677 @item Erik Schnetter
678 @item Bill Clodius
679 @item Kate Hedstrom
680 @end itemize
681
682 Many other individuals have helped debug,
683 test and improve @command{gfortran} over the past two years,
684 and we welcome you to do the same!
685 If you already have done so,
686 and you would like to see your name listed in the
687 list above, please contact us.
688
689
690 @node Projects
691 @section Projects
692
693 @table @emph
694
695 @item Help build the test suite
696 Solicit more code for donation to the test suite.
697 We can keep code private on request.
698
699 @item Bug hunting/squishing
700 Find bugs and write more test cases!
701 Test cases are especially very welcome,
702 because it allows us to concentrate on fixing bugs
703 instead of isolating them.
704
705 @item Smaller projects (``bug'' fixes):
706   @itemize @minus
707   @item Allow init exprs to be numbers raised to integer powers.
708   @item Implement correct rounding.
709   @item Implement F restrictions on Fortran 95 syntax.
710   @item See about making Emacs-parsable error messages.
711   @end itemize
712 @end table
713
714 If you wish to work on the runtime libraries,
715 please contact a project maintainer.
716 @c TODO: email!
717
718
719 @c ---------------------------------------------------------------------
720 @c Standards
721 @c ---------------------------------------------------------------------
722
723 @node Standards
724 @chapter Standards
725 @cindex Standards
726
727 The GNU Fortran 95 Compiler aims to be a conforming implementation of
728 ISO/IEC 1539:1997 (Fortran 95).
729
730 In the future it may also support other variants and extensions to the Fortran
731 language.  This includes ANSI Fortran 77, Fortran 90, Fortran 2000 (not yet
732 finalized), and OpenMP.
733
734 @node Index
735 @unnumbered Index
736
737 @printindex cp
738
739 @bye