source: trunk/third/gcc/gcc.info-8 @ 11288

Revision 11288, 49.2 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r11287, which included commits to RCS files with non-trunk default branches.
Line 
1This is Info file gcc.info, produced by Makeinfo version 1.67 from the
2input file gcc.texi.
3
4   This file documents the use and the internals of the GNU compiler.
5
6   Published by the Free Software Foundation 59 Temple Place - Suite 330
7Boston, MA 02111-1307 USA
8
9   Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998
10Free Software Foundation, Inc.
11
12   Permission is granted to make and distribute verbatim copies of this
13manual provided the copyright notice and this permission notice are
14preserved on all copies.
15
16   Permission is granted to copy and distribute modified versions of
17this manual under the conditions for verbatim copying, provided also
18that the sections entitled "GNU General Public License," "Funding for
19Free Software," and "Protect Your Freedom--Fight `Look And Feel'" are
20included exactly as in the original, and provided that the entire
21resulting derived work is distributed under the terms of a permission
22notice identical to this one.
23
24   Permission is granted to copy and distribute translations of this
25manual into another language, under the above conditions for modified
26versions, except that the sections entitled "GNU General Public
27License," "Funding for Free Software," and "Protect Your Freedom--Fight
28`Look And Feel'", and this permission notice, may be included in
29translations approved by the Free Software Foundation instead of in the
30original English.
31
32
33File: gcc.info,  Node: Cross Runtime,  Next: Build Cross,  Prev: Cross Headers,  Up: Cross-Compiler
34
35`libgcc.a' and Cross-Compilers
36------------------------------
37
38   Code compiled by GNU CC uses certain runtime support functions
39implicitly.  Some of these functions can be compiled successfully with
40GNU CC itself, but a few cannot be.  These problem functions are in the
41source file `libgcc1.c'; the library made from them is called
42`libgcc1.a'.
43
44   When you build a native compiler, these functions are compiled with
45some other compiler-the one that you use for bootstrapping GNU CC.
46Presumably it knows how to open code these operations, or else knows how
47to call the run-time emulation facilities that the machine comes with.
48But this approach doesn't work for building a cross-compiler.  The
49compiler that you use for building knows about the host system, not the
50target system.
51
52   So, when you build a cross-compiler you have to supply a suitable
53library `libgcc1.a' that does the job it is expected to do.
54
55   To compile `libgcc1.c' with the cross-compiler itself does not work.
56The functions in this file are supposed to implement arithmetic
57operations that GNU CC does not know how to open code for your target
58machine.  If these functions are compiled with GNU CC itself, they will
59compile into infinite recursion.
60
61   On any given target, most of these functions are not needed.  If GNU
62CC can open code an arithmetic operation, it will not call these
63functions to perform the operation.  It is possible that on your target
64machine, none of these functions is needed.  If so, you can supply an
65empty library as `libgcc1.a'.
66
67   Many targets need library support only for multiplication and
68division.  If you are linking with a library that contains functions for
69multiplication and division, you can tell GNU CC to call them directly
70by defining the macros `MULSI3_LIBCALL', and the like.  These macros
71need to be defined in the target description macro file.  For some
72targets, they are defined already.  This may be sufficient to avoid the
73need for libgcc1.a; if so, you can supply an empty library.
74
75   Some targets do not have floating point instructions; they need other
76functions in `libgcc1.a', which do floating arithmetic.  Recent
77versions of GNU CC have a file which emulates floating point.  With a
78certain amount of work, you should be able to construct a floating
79point emulator that can be used as `libgcc1.a'.  Perhaps future
80versions will contain code to do this automatically and conveniently.
81That depends on whether someone wants to implement it.
82
83   Some embedded targets come with all the necessary `libgcc1.a'
84routines written in C or assembler.  These targets build `libgcc1.a'
85automatically and you do not need to do anything special for them.
86Other embedded targets do not need any `libgcc1.a' routines since all
87the necessary operations are supported by the hardware.
88
89   If your target system has another C compiler, you can configure GNU
90CC as a native compiler on that machine, build just `libgcc1.a' with
91`make libgcc1.a' on that machine, and use the resulting file with the
92cross-compiler.  To do this, execute the following on the target
93machine:
94
95     cd TARGET-BUILD-DIR
96     ./configure --host=sparc --target=sun3
97     make libgcc1.a
98
99And then this on the host machine:
100
101     ftp TARGET-MACHINE
102     binary
103     cd TARGET-BUILD-DIR
104     get libgcc1.a
105     quit
106
107   Another way to provide the functions you need in `libgcc1.a' is to
108define the appropriate `perform_...' macros for those functions.  If
109these definitions do not use the C arithmetic operators that they are
110meant to implement, you should be able to compile them with the
111cross-compiler you are building.  (If these definitions already exist
112for your target file, then you are all set.)
113
114   To build `libgcc1.a' using the perform macros, use
115`LIBGCC1=libgcc1.a OLDCC=./xgcc' when building the compiler.
116Otherwise, you should place your replacement library under the name
117`libgcc1.a' in the directory in which you will build the
118cross-compiler, before you run `make'.
119
120
121File: gcc.info,  Node: Cross Headers,  Next: Cross Runtime,  Prev: Tools and Libraries,  Up: Cross-Compiler
122
123Cross-Compilers and Header Files
124--------------------------------
125
126   If you are cross-compiling a standalone program or a program for an
127embedded system, then you may not need any header files except the few
128that are part of GNU CC (and those of your program).  However, if you
129intend to link your program with a standard C library such as `libc.a',
130then you probably need to compile with the header files that go with
131the library you use.
132
133   The GNU C compiler does not come with these files, because (1) they
134are system-specific, and (2) they belong in a C library, not in a
135compiler.
136
137   If the GNU C library supports your target machine, then you can get
138the header files from there (assuming you actually use the GNU library
139when you link your program).
140
141   If your target machine comes with a C compiler, it probably comes
142with suitable header files also.  If you make these files accessible
143from the host machine, the cross-compiler can use them also.
144
145   Otherwise, you're on your own in finding header files to use when
146cross-compiling.
147
148   When you have found suitable header files, put them in the directory
149`/usr/local/TARGET/include', before building the cross compiler.  Then
150installation will run fixincludes properly and install the corrected
151versions of the header files where the compiler will use them.
152
153   Provide the header files before you build the cross-compiler, because
154the build stage actually runs the cross-compiler to produce parts of
155`libgcc.a'.  (These are the parts that *can* be compiled with GNU CC.)
156Some of them need suitable header files.
157
158   Here's an example showing how to copy the header files from a target
159machine.  On the target machine, do this:
160
161     (cd /usr/include; tar cf - .) > tarfile
162
163   Then, on the host machine, do this:
164
165     ftp TARGET-MACHINE
166     lcd /usr/local/TARGET/include
167     get tarfile
168     quit
169     tar xf tarfile
170
171
172File: gcc.info,  Node: Build Cross,  Prev: Cross Runtime,  Up: Cross-Compiler
173
174Actually Building the Cross-Compiler
175------------------------------------
176
177   Now you can proceed just as for compiling a single-machine compiler
178through the step of building stage 1.  If you have not provided some
179sort of `libgcc1.a', then compilation will give up at the point where
180it needs that file, printing a suitable error message.  If you do
181provide `libgcc1.a', then building the compiler will automatically
182compile and link a test program called `libgcc1-test'; if you get
183errors in the linking, it means that not all of the necessary routines
184in `libgcc1.a' are available.
185
186   You must provide the header file `float.h'.  One way to do this is
187to compile `enquire' and run it on your target machine.  The job of
188`enquire' is to run on the target machine and figure out by experiment
189the nature of its floating point representation.  `enquire' records its
190findings in the header file `float.h'.  If you can't produce this file
191by running `enquire' on the target machine, then you will need to come
192up with a suitable `float.h' in some other way (or else, avoid using it
193in your programs).
194
195   Do not try to build stage 2 for a cross-compiler.  It doesn't work to
196rebuild GNU CC as a cross-compiler using the cross-compiler, because
197that would produce a program that runs on the target machine, not on the
198host.  For example, if you compile a 386-to-68030 cross-compiler with
199itself, the result will not be right either for the 386 (because it was
200compiled into 68030 code) or for the 68030 (because it was configured
201for a 386 as the host).  If you want to compile GNU CC into 68030 code,
202whether you compile it on a 68030 or with a cross-compiler on a 386, you
203must specify a 68030 as the host when you configure it.
204
205   To install the cross-compiler, use `make install', as usual.
206
207
208File: gcc.info,  Node: Sun Install,  Next: VMS Install,  Prev: Cross-Compiler,  Up: Installation
209
210Installing GNU CC on the Sun
211============================
212
213   On Solaris, do not use the linker or other tools in `/usr/ucb' to
214build GNU CC.  Use `/usr/ccs/bin'.
215
216   If the assembler reports `Error: misaligned data' when bootstrapping,
217you are probably using an obsolete version of the GNU assembler.
218Upgrade to the latest version of GNU `binutils', or use the Solaris
219assembler.
220
221   Make sure the environment variable `FLOAT_OPTION' is not set when
222you compile `libgcc.a'.  If this option were set to `f68881' when
223`libgcc.a' is compiled, the resulting code would demand to be linked
224with a special startup file and would not link properly without special
225pains.
226
227   There is a bug in `alloca' in certain versions of the Sun library.
228To avoid this bug, install the binaries of GNU CC that were compiled by
229GNU CC.  They use `alloca' as a built-in function and never the one in
230the library.
231
232   Some versions of the Sun compiler crash when compiling GNU CC.  The
233problem is a segmentation fault in cpp.  This problem seems to be due to
234the bulk of data in the environment variables.  You may be able to avoid
235it by using the following command to compile GNU CC with Sun CC:
236
237     make CC="TERMCAP=x OBJS=x LIBFUNCS=x STAGESTUFF=x cc"
238
239   SunOS 4.1.3 and 4.1.3_U1 have bugs that can cause intermittent core
240dumps when compiling GNU CC.  A common symptom is an internal compiler
241error which does not recur if you run it again.  To fix the problem,
242install Sun recommended patch 100726 (for SunOS 4.1.3) or 101508 (for
243SunOS 4.1.3_U1), or upgrade to a later SunOS release.
244
245
246File: gcc.info,  Node: VMS Install,  Next: Collect2,  Prev: Sun Install,  Up: Installation
247
248Installing GNU CC on VMS
249========================
250
251   The VMS version of GNU CC is distributed in a backup saveset
252containing both source code and precompiled binaries.
253
254   To install the `gcc' command so you can use the compiler easily, in
255the same manner as you use the VMS C compiler, you must install the VMS
256CLD file for GNU CC as follows:
257
258  1. Define the VMS logical names `GNU_CC' and `GNU_CC_INCLUDE' to
259     point to the directories where the GNU CC executables
260     (`gcc-cpp.exe', `gcc-cc1.exe', etc.) and the C include files are
261     kept respectively.  This should be done with the commands:
262
263          $ assign /system /translation=concealed -
264            disk:[gcc.] gnu_cc
265          $ assign /system /translation=concealed -
266            disk:[gcc.include.] gnu_cc_include
267
268     with the appropriate disk and directory names.  These commands can
269     be placed in your system startup file so they will be executed
270     whenever the machine is rebooted.  You may, if you choose, do this
271     via the `GCC_INSTALL.COM' script in the `[GCC]' directory.
272
273  2. Install the `GCC' command with the command line:
274
275          $ set command /table=sys$common:[syslib]dcltables -
276            /output=sys$common:[syslib]dcltables gnu_cc:[000000]gcc
277          $ install replace sys$common:[syslib]dcltables
278
279  3. To install the help file, do the following:
280
281          $ library/help sys$library:helplib.hlb gcc.hlp
282
283     Now you can invoke the compiler with a command like `gcc /verbose
284     file.c', which is equivalent to the command `gcc -v -c file.c' in
285     Unix.
286
287   If you wish to use GNU C++ you must first install GNU CC, and then
288perform the following steps:
289
290  1. Define the VMS logical name `GNU_GXX_INCLUDE' to point to the
291     directory where the preprocessor will search for the C++ header
292     files.  This can be done with the command:
293
294          $ assign /system /translation=concealed -
295            disk:[gcc.gxx_include.] gnu_gxx_include
296
297     with the appropriate disk and directory name.  If you are going to
298     be using a C++ runtime library, this is where its install
299     procedure will install its header files.
300
301  2. Obtain the file `gcc-cc1plus.exe', and place this in the same
302     directory that `gcc-cc1.exe' is kept.
303
304     The GNU C++ compiler can be invoked with a command like `gcc /plus
305     /verbose file.cc', which is equivalent to the command `g++ -v -c
306     file.cc' in Unix.
307
308   We try to put corresponding binaries and sources on the VMS
309distribution tape.  But sometimes the binaries will be from an older
310version than the sources, because we don't always have time to update
311them.  (Use the `/version' option to determine the version number of
312the binaries and compare it with the source file `version.c' to tell
313whether this is so.)  In this case, you should use the binaries you get
314to recompile the sources.  If you must recompile, here is how:
315
316  1. Execute the command procedure `vmsconfig.com' to set up the files
317     `tm.h', `config.h', `aux-output.c', and `md.', and to create files
318     `tconfig.h' and `hconfig.h'.  This procedure also creates several
319     linker option files used by `make-cc1.com' and a data file used by
320     `make-l2.com'.
321
322          $ @vmsconfig.com
323
324  2. Setup the logical names and command tables as defined above.  In
325     addition, define the VMS logical name `GNU_BISON' to point at the
326     to the directories where the Bison executable is kept.  This
327     should be done with the command:
328
329          $ assign /system /translation=concealed -
330            disk:[bison.] gnu_bison
331
332     You may, if you choose, use the `INSTALL_BISON.COM' script in the
333     `[BISON]' directory.
334
335  3. Install the `BISON' command with the command line:
336
337          $ set command /table=sys$common:[syslib]dcltables -
338            /output=sys$common:[syslib]dcltables -
339            gnu_bison:[000000]bison
340          $ install replace sys$common:[syslib]dcltables
341
342  4. Type `@make-gcc' to recompile everything (alternatively, submit
343     the file `make-gcc.com' to a batch queue).  If you wish to build
344     the GNU C++ compiler as well as the GNU CC compiler, you must
345     first edit `make-gcc.com' and follow the instructions that appear
346     in the comments.
347
348  5. In order to use GCC, you need a library of functions which GCC
349     compiled code will call to perform certain tasks, and these
350     functions are defined in the file `libgcc2.c'.  To compile this
351     you should use the command procedure `make-l2.com', which will
352     generate the library `libgcc2.olb'.  `libgcc2.olb' should be built
353     using the compiler built from the same distribution that
354     `libgcc2.c' came from, and `make-gcc.com' will automatically do
355     all of this for you.
356
357     To install the library, use the following commands:
358
359          $ library gnu_cc:[000000]gcclib/delete=(new,eprintf)
360          $ library gnu_cc:[000000]gcclib/delete=L_*
361          $ library libgcc2/extract=*/output=libgcc2.obj
362          $ library gnu_cc:[000000]gcclib libgcc2.obj
363
364     The first command simply removes old modules that will be replaced
365     with modules from `libgcc2' under different module names.  The
366     modules `new' and `eprintf' may not actually be present in your
367     `gcclib.olb'--if the VMS librarian complains about those modules
368     not being present, simply ignore the message and continue on with
369     the next command.  The second command removes the modules that
370     came from the previous version of the library `libgcc2.c'.
371
372     Whenever you update the compiler on your system, you should also
373     update the library with the above procedure.
374
375  6. You may wish to build GCC in such a way that no files are written
376     to the directory where the source files reside.  An example would
377     be the when the source files are on a read-only disk.  In these
378     cases, execute the following DCL commands (substituting your
379     actual path names):
380
381          $ assign dua0:[gcc.build_dir.]/translation=concealed, -
382                   dua1:[gcc.source_dir.]/translation=concealed  gcc_build
383          $ set default gcc_build:[000000]
384
385     where the directory `dua1:[gcc.source_dir]' contains the source
386     code, and the directory `dua0:[gcc.build_dir]' is meant to contain
387     all of the generated object files and executables.  Once you have
388     done this, you can proceed building GCC as described above.  (Keep
389     in mind that `gcc_build' is a rooted logical name, and thus the
390     device names in each element of the search list must be an actual
391     physical device name rather than another rooted logical name).
392
393  7. *If you are building GNU CC with a previous version of GNU CC, you
394     also should check to see that you have the newest version of the
395     assembler*.  In particular, GNU CC version 2 treats global constant
396     variables slightly differently from GNU CC version 1, and GAS
397     version 1.38.1 does not have the patches required to work with GCC
398     version 2.  If you use GAS 1.38.1, then `extern const' variables
399     will not have the read-only bit set, and the linker will generate
400     warning messages about mismatched psect attributes for these
401     variables.  These warning messages are merely a nuisance, and can
402     safely be ignored.
403
404     If you are compiling with a version of GNU CC older than 1.33,
405     specify `/DEFINE=("inline=")' as an option in all the
406     compilations.  This requires editing all the `gcc' commands in
407     `make-cc1.com'.  (The older versions had problems supporting
408     `inline'.)  Once you have a working 1.33 or newer GNU CC, you can
409     change this file back.
410
411  8. If you want to build GNU CC with the VAX C compiler, you will need
412     to make minor changes in `make-cccp.com' and `make-cc1.com' to
413     choose alternate definitions of `CC', `CFLAGS', and `LIBS'.  See
414     comments in those files.  However, you must also have a working
415     version of the GNU assembler (GNU as, aka GAS) as it is used as
416     the back-end for GNU CC to produce binary object modules and is
417     not included in the GNU CC sources.  GAS is also needed to compile
418     `libgcc2' in order to build `gcclib' (see above); `make-l2.com'
419     expects to be able to find it operational in
420     `gnu_cc:[000000]gnu-as.exe'.
421
422     To use GNU CC on VMS, you need the VMS driver programs `gcc.exe',
423     `gcc.com', and `gcc.cld'.  They are distributed with the VMS
424     binaries (`gcc-vms') rather than the GNU CC sources.  GAS is also
425     included in `gcc-vms', as is Bison.
426
427     Once you have successfully built GNU CC with VAX C, you should use
428     the resulting compiler to rebuild itself.  Before doing this, be
429     sure to restore the `CC', `CFLAGS', and `LIBS' definitions in
430     `make-cccp.com' and `make-cc1.com'.  The second generation
431     compiler will be able to take advantage of many optimizations that
432     must be suppressed when building with other compilers.
433
434   Under previous versions of GNU CC, the generated code would
435occasionally give strange results when linked with the sharable
436`VAXCRTL' library.  Now this should work.
437
438   Even with this version, however, GNU CC itself should not be linked
439with the sharable `VAXCRTL'.  The version of `qsort' in `VAXCRTL' has a
440bug (known to be present in VMS versions V4.6 through V5.5) which
441causes the compiler to fail.
442
443   The executables are generated by `make-cc1.com' and `make-cccp.com'
444use the object library version of `VAXCRTL' in order to make use of the
445`qsort' routine in `gcclib.olb'.  If you wish to link the compiler
446executables with the shareable image version of `VAXCRTL', you should
447edit the file `tm.h' (created by `vmsconfig.com') to define the macro
448`QSORT_WORKAROUND'.
449
450   `QSORT_WORKAROUND' is always defined when GNU CC is compiled with
451VAX C, to avoid a problem in case `gcclib.olb' is not yet available.
452
453
454File: gcc.info,  Node: Collect2,  Next: Header Dirs,  Prev: VMS Install,  Up: Installation
455
456`collect2'
457==========
458
459   Many target systems do not have support in the assembler and linker
460for "constructors"--initialization functions to be called before the
461official "start" of `main'.  On such systems, GNU CC uses a utility
462called `collect2' to arrange to call these functions at start time.
463
464   The program `collect2' works by linking the program once and looking
465through the linker output file for symbols with particular names
466indicating they are constructor functions.  If it finds any, it creates
467a new temporary `.c' file containing a table of them, compiles it, and
468links the program a second time including that file.
469
470   The actual calls to the constructors are carried out by a subroutine
471called `__main', which is called (automatically) at the beginning of
472the body of `main' (provided `main' was compiled with GNU CC).  Calling
473`__main' is necessary, even when compiling C code, to allow linking C
474and C++ object code together.  (If you use `-nostdlib', you get an
475unresolved reference to `__main', since it's defined in the standard
476GCC library.  Include `-lgcc' at the end of your compiler command line
477to resolve this reference.)
478
479   The program `collect2' is installed as `ld' in the directory where
480the passes of the compiler are installed.  When `collect2' needs to
481find the *real* `ld', it tries the following file names:
482
483   * `real-ld' in the directories listed in the compiler's search
484     directories.
485
486   * `real-ld' in the directories listed in the environment variable
487     `PATH'.
488
489   * The file specified in the `REAL_LD_FILE_NAME' configuration macro,
490     if specified.
491
492   * `ld' in the compiler's search directories, except that `collect2'
493     will not execute itself recursively.
494
495   * `ld' in `PATH'.
496
497   "The compiler's search directories" means all the directories where
498`gcc' searches for passes of the compiler.  This includes directories
499that you specify with `-B'.
500
501   Cross-compilers search a little differently:
502
503   * `real-ld' in the compiler's search directories.
504
505   * `TARGET-real-ld' in `PATH'.
506
507   * The file specified in the `REAL_LD_FILE_NAME' configuration macro,
508     if specified.
509
510   * `ld' in the compiler's search directories.
511
512   * `TARGET-ld' in `PATH'.
513
514   `collect2' explicitly avoids running `ld' using the file name under
515which `collect2' itself was invoked.  In fact, it remembers up a list
516of such names--in case one copy of `collect2' finds another copy (or
517version) of `collect2' installed as `ld' in a second place in the
518search path.
519
520   `collect2' searches for the utilities `nm' and `strip' using the
521same algorithm as above for `ld'.
522
523
524File: gcc.info,  Node: Header Dirs,  Prev: Collect2,  Up: Installation
525
526Standard Header File Directories
527================================
528
529   `GCC_INCLUDE_DIR' means the same thing for native and cross.  It is
530where GNU CC stores its private include files, and also where GNU CC
531stores the fixed include files.  A cross compiled GNU CC runs
532`fixincludes' on the header files in `$(tooldir)/include'.  (If the
533cross compilation header files need to be fixed, they must be installed
534before GNU CC is built.  If the cross compilation header files are
535already suitable for ANSI C and GNU CC, nothing special need be done).
536
537   `GPLUS_INCLUDE_DIR' means the same thing for native and cross.  It
538is where `g++' looks first for header files.  The C++ library installs
539only target independent header files in that directory.
540
541   `LOCAL_INCLUDE_DIR' is used only for a native compiler.  It is
542normally `/usr/local/include'.  GNU CC searches this directory so that
543users can install header files in `/usr/local/include'.
544
545   `CROSS_INCLUDE_DIR' is used only for a cross compiler.  GNU CC
546doesn't install anything there.
547
548   `TOOL_INCLUDE_DIR' is used for both native and cross compilers.  It
549is the place for other packages to install header files that GNU CC will
550use.  For a cross-compiler, this is the equivalent of `/usr/include'.
551When you build a cross-compiler, `fixincludes' processes any header
552files in this directory.
553
554
555File: gcc.info,  Node: C Extensions,  Next: C++ Extensions,  Prev: Installation,  Up: Top
556
557Extensions to the C Language Family
558***********************************
559
560   GNU C provides several language features not found in ANSI standard
561C.  (The `-pedantic' option directs GNU CC to print a warning message if
562any of these features is used.)  To test for the availability of these
563features in conditional compilation, check for a predefined macro
564`__GNUC__', which is always defined under GNU CC.
565
566   These extensions are available in C and Objective C.  Most of them
567are also available in C++.  *Note Extensions to the C++ Language: C++
568Extensions, for extensions that apply *only* to C++.
569
570* Menu:
571
572* Statement Exprs::     Putting statements and declarations inside expressions.
573* Local Labels::        Labels local to a statement-expression.
574* Labels as Values::    Getting pointers to labels, and computed gotos.
575* Nested Functions::    As in Algol and Pascal, lexical scoping of functions.
576* Constructing Calls::  Dispatching a call to another function.
577* Naming Types::        Giving a name to the type of some expression.
578* Typeof::              `typeof': referring to the type of an expression.
579* Lvalues::             Using `?:', `,' and casts in lvalues.
580* Conditionals::        Omitting the middle operand of a `?:' expression.
581* Long Long::           Double-word integers--`long long int'.
582* Complex::             Data types for complex numbers.
583* Zero Length::         Zero-length arrays.
584* Variable Length::     Arrays whose length is computed at run time.
585* Macro Varargs::       Macros with variable number of arguments.
586* Subscripting::        Any array can be subscripted, even if not an lvalue.
587* Pointer Arith::       Arithmetic on `void'-pointers and function pointers.
588* Initializers::        Non-constant initializers.
589* Constructors::        Constructor expressions give structures, unions
590                         or arrays as values.
591* Labeled Elements::    Labeling elements of initializers.
592* Cast to Union::       Casting to union type from any member of the union.
593* Case Ranges::         `case 1 ... 9' and such.
594* Function Attributes:: Declaring that functions have no side effects,
595                         or that they can never return.
596* Function Prototypes:: Prototype declarations and old-style definitions.
597* C++ Comments::        C++ comments are recognized.
598* Dollar Signs::        Dollar sign is allowed in identifiers.
599* Character Escapes::   `\e' stands for the character <ESC>.
600* Variable Attributes:: Specifying attributes of variables.
601* Type Attributes::     Specifying attributes of types.
602* Alignment::           Inquiring about the alignment of a type or variable.
603* Inline::              Defining inline functions (as fast as macros).
604* Extended Asm::        Assembler instructions with C expressions as operands.
605                         (With them you can define "built-in" functions.)
606* Asm Labels::          Specifying the assembler name to use for a C symbol.
607* Explicit Reg Vars::   Defining variables residing in specified registers.
608* Alternate Keywords::  `__const__', `__asm__', etc., for header files.
609* Incomplete Enums::    `enum foo;', with details to follow.
610* Function Names::      Printable strings which are the name of the current
611                         function.
612* Return Address::      Getting the return or frame address of a function.
613
614
615File: gcc.info,  Node: Statement Exprs,  Next: Local Labels,  Up: C Extensions
616
617Statements and Declarations in Expressions
618==========================================
619
620   A compound statement enclosed in parentheses may appear as an
621expression in GNU C.  This allows you to use loops, switches, and local
622variables within an expression.
623
624   Recall that a compound statement is a sequence of statements
625surrounded by braces; in this construct, parentheses go around the
626braces.  For example:
627
628     ({ int y = foo (); int z;
629        if (y > 0) z = y;
630        else z = - y;
631        z; })
632
633is a valid (though slightly more complex than necessary) expression for
634the absolute value of `foo ()'.
635
636   The last thing in the compound statement should be an expression
637followed by a semicolon; the value of this subexpression serves as the
638value of the entire construct.  (If you use some other kind of statement
639last within the braces, the construct has type `void', and thus
640effectively no value.)
641
642   This feature is especially useful in making macro definitions "safe"
643(so that they evaluate each operand exactly once).  For example, the
644"maximum" function is commonly defined as a macro in standard C as
645follows:
646
647     #define max(a,b) ((a) > (b) ? (a) : (b))
648
649But this definition computes either A or B twice, with bad results if
650the operand has side effects.  In GNU C, if you know the type of the
651operands (here let's assume `int'), you can define the macro safely as
652follows:
653
654     #define maxint(a,b) \
655       ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
656
657   Embedded statements are not allowed in constant expressions, such as
658the value of an enumeration constant, the width of a bit field, or the
659initial value of a static variable.
660
661   If you don't know the type of the operand, you can still do this,
662but you must use `typeof' (*note Typeof::.) or type naming (*note
663Naming Types::.).
664
665
666File: gcc.info,  Node: Local Labels,  Next: Labels as Values,  Prev: Statement Exprs,  Up: C Extensions
667
668Locally Declared Labels
669=======================
670
671   Each statement expression is a scope in which "local labels" can be
672declared.  A local label is simply an identifier; you can jump to it
673with an ordinary `goto' statement, but only from within the statement
674expression it belongs to.
675
676   A local label declaration looks like this:
677
678     __label__ LABEL;
679
680or
681
682     __label__ LABEL1, LABEL2, ...;
683
684   Local label declarations must come at the beginning of the statement
685expression, right after the `({', before any ordinary declarations.
686
687   The label declaration defines the label *name*, but does not define
688the label itself.  You must do this in the usual way, with `LABEL:',
689within the statements of the statement expression.
690
691   The local label feature is useful because statement expressions are
692often used in macros.  If the macro contains nested loops, a `goto' can
693be useful for breaking out of them.  However, an ordinary label whose
694scope is the whole function cannot be used: if the macro can be
695expanded several times in one function, the label will be multiply
696defined in that function.  A local label avoids this problem.  For
697example:
698
699     #define SEARCH(array, target)                     \
700     ({                                               \
701       __label__ found;                                \
702       typeof (target) _SEARCH_target = (target);      \
703       typeof (*(array)) *_SEARCH_array = (array);     \
704       int i, j;                                       \
705       int value;                                      \
706       for (i = 0; i < max; i++)                       \
707         for (j = 0; j < max; j++)                     \
708           if (_SEARCH_array[i][j] == _SEARCH_target)  \
709             { value = i; goto found; }              \
710       value = -1;                                     \
711      found:                                           \
712       value;                                          \
713     })
714
715
716File: gcc.info,  Node: Labels as Values,  Next: Nested Functions,  Prev: Local Labels,  Up: C Extensions
717
718Labels as Values
719================
720
721   You can get the address of a label defined in the current function
722(or a containing function) with the unary operator `&&'.  The value has
723type `void *'.  This value is a constant and can be used wherever a
724constant of that type is valid.  For example:
725
726     void *ptr;
727     ...
728     ptr = &&foo;
729
730   To use these values, you need to be able to jump to one.  This is
731done with the computed goto statement(1), `goto *EXP;'.  For example,
732
733     goto *ptr;
734
735Any expression of type `void *' is allowed.
736
737   One way of using these constants is in initializing a static array
738that will serve as a jump table:
739
740     static void *array[] = { &&foo, &&bar, &&hack };
741
742   Then you can select a label with indexing, like this:
743
744     goto *array[i];
745
746Note that this does not check whether the subscript is in bounds--array
747indexing in C never does that.
748
749   Such an array of label values serves a purpose much like that of the
750`switch' statement.  The `switch' statement is cleaner, so use that
751rather than an array unless the problem does not fit a `switch'
752statement very well.
753
754   Another use of label values is in an interpreter for threaded code.
755The labels within the interpreter function can be stored in the
756threaded code for super-fast dispatching.
757
758   You can use this mechanism to jump to code in a different function.
759If you do that, totally unpredictable things will happen.  The best way
760to avoid this is to store the label address only in automatic variables
761and never pass it as an argument.
762
763   ---------- Footnotes ----------
764
765   (1)  The analogous feature in Fortran is called an assigned goto,
766but that name seems inappropriate in C, where one can do more than
767simply store label addresses in label variables.
768
769
770File: gcc.info,  Node: Nested Functions,  Next: Constructing Calls,  Prev: Labels as Values,  Up: C Extensions
771
772Nested Functions
773================
774
775   A "nested function" is a function defined inside another function.
776(Nested functions are not supported for GNU C++.)  The nested function's
777name is local to the block where it is defined.  For example, here we
778define a nested function named `square', and call it twice:
779
780     foo (double a, double b)
781     {
782       double square (double z) { return z * z; }
783     
784       return square (a) + square (b);
785     }
786
787   The nested function can access all the variables of the containing
788function that are visible at the point of its definition.  This is
789called "lexical scoping".  For example, here we show a nested function
790which uses an inherited variable named `offset':
791
792     bar (int *array, int offset, int size)
793     {
794       int access (int *array, int index)
795         { return array[index + offset]; }
796       int i;
797       ...
798       for (i = 0; i < size; i++)
799         ... access (array, i) ...
800     }
801
802   Nested function definitions are permitted within functions in the
803places where variable definitions are allowed; that is, in any block,
804before the first statement in the block.
805
806   It is possible to call the nested function from outside the scope of
807its name by storing its address or passing the address to another
808function:
809
810     hack (int *array, int size)
811     {
812       void store (int index, int value)
813         { array[index] = value; }
814     
815       intermediate (store, size);
816     }
817
818   Here, the function `intermediate' receives the address of `store' as
819an argument.  If `intermediate' calls `store', the arguments given to
820`store' are used to store into `array'.  But this technique works only
821so long as the containing function (`hack', in this example) does not
822exit.
823
824   If you try to call the nested function through its address after the
825containing function has exited, all hell will break loose.  If you try
826to call it after a containing scope level has exited, and if it refers
827to some of the variables that are no longer in scope, you may be lucky,
828but it's not wise to take the risk.  If, however, the nested function
829does not refer to anything that has gone out of scope, you should be
830safe.
831
832   GNU CC implements taking the address of a nested function using a
833technique called "trampolines".
834
835   A nested function can jump to a label inherited from a containing
836function, provided the label was explicitly declared in the containing
837function (*note Local Labels::.).  Such a jump returns instantly to the
838containing function, exiting the nested function which did the `goto'
839and any intermediate functions as well.  Here is an example:
840
841     bar (int *array, int offset, int size)
842     {
843       __label__ failure;
844       int access (int *array, int index)
845         {
846           if (index > size)
847             goto failure;
848           return array[index + offset];
849         }
850       int i;
851       ...
852       for (i = 0; i < size; i++)
853         ... access (array, i) ...
854       ...
855       return 0;
856     
857      /* Control comes here from `access'
858         if it detects an error.  */
859      failure:
860       return -1;
861     }
862
863   A nested function always has internal linkage.  Declaring one with
864`extern' is erroneous.  If you need to declare the nested function
865before its definition, use `auto' (which is otherwise meaningless for
866function declarations).
867
868     bar (int *array, int offset, int size)
869     {
870       __label__ failure;
871       auto int access (int *, int);
872       ...
873       int access (int *array, int index)
874         {
875           if (index > size)
876             goto failure;
877           return array[index + offset];
878         }
879       ...
880     }
881
882
883File: gcc.info,  Node: Constructing Calls,  Next: Naming Types,  Prev: Nested Functions,  Up: C Extensions
884
885Constructing Function Calls
886===========================
887
888   Using the built-in functions described below, you can record the
889arguments a function received, and call another function with the same
890arguments, without knowing the number or types of the arguments.
891
892   You can also record the return value of that function call, and
893later return that value, without knowing what data type the function
894tried to return (as long as your caller expects that data type).
895
896`__builtin_apply_args ()'
897     This built-in function returns a pointer of type `void *' to data
898     describing how to perform a call with the same arguments as were
899     passed to the current function.
900
901     The function saves the arg pointer register, structure value
902     address, and all registers that might be used to pass arguments to
903     a function into a block of memory allocated on the stack.  Then it
904     returns the address of that block.
905
906`__builtin_apply (FUNCTION, ARGUMENTS, SIZE)'
907     This built-in function invokes FUNCTION (type `void (*)()') with a
908     copy of the parameters described by ARGUMENTS (type `void *') and
909     SIZE (type `int').
910
911     The value of ARGUMENTS should be the value returned by
912     `__builtin_apply_args'.  The argument SIZE specifies the size of
913     the stack argument data, in bytes.
914
915     This function returns a pointer of type `void *' to data describing
916     how to return whatever value was returned by FUNCTION.  The data
917     is saved in a block of memory allocated on the stack.
918
919     It is not always simple to compute the proper value for SIZE.  The
920     value is used by `__builtin_apply' to compute the amount of data
921     that should be pushed on the stack and copied from the incoming
922     argument area.
923
924`__builtin_return (RESULT)'
925     This built-in function returns the value described by RESULT from
926     the containing function.  You should specify, for RESULT, a value
927     returned by `__builtin_apply'.
928
929
930File: gcc.info,  Node: Naming Types,  Next: Typeof,  Prev: Constructing Calls,  Up: C Extensions
931
932Naming an Expression's Type
933===========================
934
935   You can give a name to the type of an expression using a `typedef'
936declaration with an initializer.  Here is how to define NAME as a type
937name for the type of EXP:
938
939     typedef NAME = EXP;
940
941   This is useful in conjunction with the statements-within-expressions
942feature.  Here is how the two together can be used to define a safe
943"maximum" macro that operates on any arithmetic type:
944
945     #define max(a,b) \
946       ({typedef _ta = (a), _tb = (b);  \
947         _ta _a = (a); _tb _b = (b);     \
948         _a > _b ? _a : _b; })
949
950   The reason for using names that start with underscores for the local
951variables is to avoid conflicts with variable names that occur within
952the expressions that are substituted for `a' and `b'.  Eventually we
953hope to design a new form of declaration syntax that allows you to
954declare variables whose scopes start only after their initializers;
955this will be a more reliable way to prevent such conflicts.
956
957
958File: gcc.info,  Node: Typeof,  Next: Lvalues,  Prev: Naming Types,  Up: C Extensions
959
960Referring to a Type with `typeof'
961=================================
962
963   Another way to refer to the type of an expression is with `typeof'.
964The syntax of using of this keyword looks like `sizeof', but the
965construct acts semantically like a type name defined with `typedef'.
966
967   There are two ways of writing the argument to `typeof': with an
968expression or with a type.  Here is an example with an expression:
969
970     typeof (x[0](1))
971
972This assumes that `x' is an array of functions; the type described is
973that of the values of the functions.
974
975   Here is an example with a typename as the argument:
976
977     typeof (int *)
978
979Here the type described is that of pointers to `int'.
980
981   If you are writing a header file that must work when included in
982ANSI C programs, write `__typeof__' instead of `typeof'.  *Note
983Alternate Keywords::.
984
985   A `typeof'-construct can be used anywhere a typedef name could be
986used.  For example, you can use it in a declaration, in a cast, or
987inside of `sizeof' or `typeof'.
988
989   * This declares `y' with the type of what `x' points to.
990
991          typeof (*x) y;
992
993   * This declares `y' as an array of such values.
994
995          typeof (*x) y[4];
996
997   * This declares `y' as an array of pointers to characters:
998
999          typeof (typeof (char *)[4]) y;
1000
1001     It is equivalent to the following traditional C declaration:
1002
1003          char *y[4];
1004
1005     To see the meaning of the declaration using `typeof', and why it
1006     might be a useful way to write, let's rewrite it with these macros:
1007
1008          #define pointer(T)  typeof(T *)
1009          #define array(T, N) typeof(T [N])
1010
1011     Now the declaration can be rewritten this way:
1012
1013          array (pointer (char), 4) y;
1014
1015     Thus, `array (pointer (char), 4)' is the type of arrays of 4
1016     pointers to `char'.
1017
1018
1019File: gcc.info,  Node: Lvalues,  Next: Conditionals,  Prev: Typeof,  Up: C Extensions
1020
1021Generalized Lvalues
1022===================
1023
1024   Compound expressions, conditional expressions and casts are allowed
1025as lvalues provided their operands are lvalues.  This means that you
1026can take their addresses or store values into them.
1027
1028   Standard C++ allows compound expressions and conditional expressions
1029as lvalues, and permits casts to reference type, so use of this
1030extension is deprecated for C++ code.
1031
1032   For example, a compound expression can be assigned, provided the last
1033expression in the sequence is an lvalue.  These two expressions are
1034equivalent:
1035
1036     (a, b) += 5
1037     a, (b += 5)
1038
1039   Similarly, the address of the compound expression can be taken.
1040These two expressions are equivalent:
1041
1042     &(a, b)
1043     a, &b
1044
1045   A conditional expression is a valid lvalue if its type is not void
1046and the true and false branches are both valid lvalues.  For example,
1047these two expressions are equivalent:
1048
1049     (a ? b : c) = 5
1050     (a ? b = 5 : (c = 5))
1051
1052   A cast is a valid lvalue if its operand is an lvalue.  A simple
1053assignment whose left-hand side is a cast works by converting the
1054right-hand side first to the specified type, then to the type of the
1055inner left-hand side expression.  After this is stored, the value is
1056converted back to the specified type to become the value of the
1057assignment.  Thus, if `a' has type `char *', the following two
1058expressions are equivalent:
1059
1060     (int)a = 5
1061     (int)(a = (char *)(int)5)
1062
1063   An assignment-with-arithmetic operation such as `+=' applied to a
1064cast performs the arithmetic using the type resulting from the cast,
1065and then continues as in the previous case.  Therefore, these two
1066expressions are equivalent:
1067
1068     (int)a += 5
1069     (int)(a = (char *)(int) ((int)a + 5))
1070
1071   You cannot take the address of an lvalue cast, because the use of its
1072address would not work out coherently.  Suppose that `&(int)f' were
1073permitted, where `f' has type `float'.  Then the following statement
1074would try to store an integer bit-pattern where a floating point number
1075belongs:
1076
1077     *&(int)f = 1;
1078
1079   This is quite different from what `(int)f = 1' would do--that would
1080convert 1 to floating point and store it.  Rather than cause this
1081inconsistency, we think it is better to prohibit use of `&' on a cast.
1082
1083   If you really do want an `int *' pointer with the address of `f',
1084you can simply write `(int *)&f'.
1085
1086
1087File: gcc.info,  Node: Conditionals,  Next: Long Long,  Prev: Lvalues,  Up: C Extensions
1088
1089Conditionals with Omitted Operands
1090==================================
1091
1092   The middle operand in a conditional expression may be omitted.  Then
1093if the first operand is nonzero, its value is the value of the
1094conditional expression.
1095
1096   Therefore, the expression
1097
1098     x ? : y
1099
1100has the value of `x' if that is nonzero; otherwise, the value of `y'.
1101
1102   This example is perfectly equivalent to
1103
1104     x ? x : y
1105
1106In this simple case, the ability to omit the middle operand is not
1107especially useful.  When it becomes useful is when the first operand
1108does, or may (if it is a macro argument), contain a side effect.  Then
1109repeating the operand in the middle would perform the side effect
1110twice.  Omitting the middle operand uses the value already computed
1111without the undesirable effects of recomputing it.
1112
1113
1114File: gcc.info,  Node: Long Long,  Next: Complex,  Prev: Conditionals,  Up: C Extensions
1115
1116Double-Word Integers
1117====================
1118
1119   GNU C supports data types for integers that are twice as long as
1120`int'.  Simply write `long long int' for a signed integer, or `unsigned
1121long long int' for an unsigned integer.  To make an integer constant of
1122type `long long int', add the suffix `LL' to the integer.  To make an
1123integer constant of type `unsigned long long int', add the suffix `ULL'
1124to the integer.
1125
1126   You can use these types in arithmetic like any other integer types.
1127Addition, subtraction, and bitwise boolean operations on these types
1128are open-coded on all types of machines.  Multiplication is open-coded
1129if the machine supports fullword-to-doubleword a widening multiply
1130instruction.  Division and shifts are open-coded only on machines that
1131provide special support.  The operations that are not open-coded use
1132special library routines that come with GNU CC.
1133
1134   There may be pitfalls when you use `long long' types for function
1135arguments, unless you declare function prototypes.  If a function
1136expects type `int' for its argument, and you pass a value of type `long
1137long int', confusion will result because the caller and the subroutine
1138will disagree about the number of bytes for the argument.  Likewise, if
1139the function expects `long long int' and you pass `int'.  The best way
1140to avoid such problems is to use prototypes.
1141
1142
1143File: gcc.info,  Node: Complex,  Next: Zero Length,  Prev: Long Long,  Up: C Extensions
1144
1145Complex Numbers
1146===============
1147
1148   GNU C supports complex data types.  You can declare both complex
1149integer types and complex floating types, using the keyword
1150`__complex__'.
1151
1152   For example, `__complex__ double x;' declares `x' as a variable
1153whose real part and imaginary part are both of type `double'.
1154`__complex__ short int y;' declares `y' to have real and imaginary
1155parts of type `short int'; this is not likely to be useful, but it
1156shows that the set of complex types is complete.
1157
1158   To write a constant with a complex data type, use the suffix `i' or
1159`j' (either one; they are equivalent).  For example, `2.5fi' has type
1160`__complex__ float' and `3i' has type `__complex__ int'.  Such a
1161constant always has a pure imaginary value, but you can form any
1162complex value you like by adding one to a real constant.
1163
1164   To extract the real part of a complex-valued expression EXP, write
1165`__real__ EXP'.  Likewise, use `__imag__' to extract the imaginary part.
1166
1167   The operator `~' performs complex conjugation when used on a value
1168with a complex type.
1169
1170   GNU CC can allocate complex automatic variables in a noncontiguous
1171fashion; it's even possible for the real part to be in a register while
1172the imaginary part is on the stack (or vice-versa).  None of the
1173supported debugging info formats has a way to represent noncontiguous
1174allocation like this, so GNU CC describes a noncontiguous complex
1175variable as if it were two separate variables of noncomplex type.  If
1176the variable's actual name is `foo', the two fictitious variables are
1177named `foo$real' and `foo$imag'.  You can examine and set these two
1178fictitious variables with your debugger.
1179
1180   A future version of GDB will know how to recognize such pairs and
1181treat them as a single variable with a complex type.
1182
1183
1184File: gcc.info,  Node: Zero Length,  Next: Variable Length,  Prev: Complex,  Up: C Extensions
1185
1186Arrays of Length Zero
1187=====================
1188
1189   Zero-length arrays are allowed in GNU C.  They are very useful as
1190the last element of a structure which is really a header for a
1191variable-length object:
1192
1193     struct line {
1194       int length;
1195       char contents[0];
1196     };
1197     
1198     {
1199       struct line *thisline = (struct line *)
1200         malloc (sizeof (struct line) + this_length);
1201       thisline->length = this_length;
1202     }
1203
1204   In standard C, you would have to give `contents' a length of 1, which
1205means either you waste space or complicate the argument to `malloc'.
1206
Note: See TracBrowser for help on using the repository browser.