source: trunk/third/gcc/gcc.info-9 @ 8834

Revision 8834, 49.2 KB checked in by ghudson, 28 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r8833, which included commits to RCS files with non-trunk default branches.
Line 
1This is Info file gcc.info, produced by Makeinfo-1.55 from the input
2file 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 Free Software
10Foundation, 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: Constructors,  Next: Labeled Elements,  Prev: Initializers,  Up: C Extensions
34
35Constructor Expressions
36=======================
37
38   GNU C supports constructor expressions.  A constructor looks like a
39cast containing an initializer.  Its value is an object of the type
40specified in the cast, containing the elements specified in the
41initializer.
42
43   Usually, the specified type is a structure.  Assume that `struct
44foo' and `structure' are declared as shown:
45
46     struct foo {int a; char b[2];} structure;
47
48Here is an example of constructing a `struct foo' with a constructor:
49
50     structure = ((struct foo) {x + y, 'a', 0});
51
52This is equivalent to writing the following:
53
54     {
55       struct foo temp = {x + y, 'a', 0};
56       structure = temp;
57     }
58
59   You can also construct an array.  If all the elements of the
60constructor are (made up of) simple constant expressions, suitable for
61use in initializers, then the constructor is an lvalue and can be
62coerced to a pointer to its first element, as shown here:
63
64     char **foo = (char *[]) { "x", "y", "z" };
65
66   Array constructors whose elements are not simple constants are not
67very useful, because the constructor is not an lvalue.  There are only
68two valid ways to use it: to subscript it, or initialize an array
69variable with it.  The former is probably slower than a `switch'
70statement, while the latter does the same thing an ordinary C
71initializer would do.  Here is an example of subscripting an array
72constructor:
73
74     output = ((int[]) { 2, x, 28 }) [input];
75
76   Constructor expressions for scalar types and union types are is also
77allowed, but then the constructor expression is equivalent to a cast.
78
79
80File: gcc.info,  Node: Labeled Elements,  Next: Cast to Union,  Prev: Constructors,  Up: C Extensions
81
82Labeled Elements in Initializers
83================================
84
85   Standard C requires the elements of an initializer to appear in a
86fixed order, the same as the order of the elements in the array or
87structure being initialized.
88
89   In GNU C you can give the elements in any order, specifying the array
90indices or structure field names they apply to.  This extension is not
91implemented in GNU C++.
92
93   To specify an array index, write `[INDEX]' or `[INDEX] =' before the
94element value.  For example,
95
96     int a[6] = { [4] 29, [2] = 15 };
97
98is equivalent to
99
100     int a[6] = { 0, 0, 15, 0, 29, 0 };
101
102The index values must be constant expressions, even if the array being
103initialized is automatic.
104
105   To initialize a range of elements to the same value, write `[FIRST
106... LAST] = VALUE'.  For example,
107
108     int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
109
110Note that the length of the array is the highest value specified plus
111one.
112
113   In a structure initializer, specify the name of a field to initialize
114with `FIELDNAME:' before the element value.  For example, given the
115following structure,
116
117     struct point { int x, y; };
118
119the following initialization
120
121     struct point p = { y: yvalue, x: xvalue };
122
123is equivalent to
124
125     struct point p = { xvalue, yvalue };
126
127   Another syntax which has the same meaning is `.FIELDNAME ='., as
128shown here:
129
130     struct point p = { .y = yvalue, .x = xvalue };
131
132   You can also use an element label (with either the colon syntax or
133the period-equal syntax) when initializing a union, to specify which
134element of the union should be used.  For example,
135
136     union foo { int i; double d; };
137     
138     union foo f = { d: 4 };
139
140will convert 4 to a `double' to store it in the union using the second
141element.  By contrast, casting 4 to type `union foo' would store it
142into the union as the integer `i', since it is an integer.  (*Note Cast
143to Union::.)
144
145   You can combine this technique of naming elements with ordinary C
146initialization of successive elements.  Each initializer element that
147does not have a label applies to the next consecutive element of the
148array or structure.  For example,
149
150     int a[6] = { [1] = v1, v2, [4] = v4 };
151
152is equivalent to
153
154     int a[6] = { 0, v1, v2, 0, v4, 0 };
155
156   Labeling the elements of an array initializer is especially useful
157when the indices are characters or belong to an `enum' type.  For
158example:
159
160     int whitespace[256]
161       = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
162           ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
163
164
165File: gcc.info,  Node: Case Ranges,  Next: Function Attributes,  Prev: Cast to Union,  Up: C Extensions
166
167Case Ranges
168===========
169
170   You can specify a range of consecutive values in a single `case'
171label, like this:
172
173     case LOW ... HIGH:
174
175This has the same effect as the proper number of individual `case'
176labels, one for each integer value from LOW to HIGH, inclusive.
177
178   This feature is especially useful for ranges of ASCII character
179codes:
180
181     case 'A' ... 'Z':
182
183   *Be careful:* Write spaces around the `...', for otherwise it may be
184parsed wrong when you use it with integer values.  For example, write
185this:
186
187     case 1 ... 5:
188
189rather than this:
190
191     case 1...5:
192
193
194File: gcc.info,  Node: Cast to Union,  Next: Case Ranges,  Prev: Labeled Elements,  Up: C Extensions
195
196Cast to a Union Type
197====================
198
199   A cast to union type is similar to other casts, except that the type
200specified is a union type.  You can specify the type either with `union
201TAG' or with a typedef name.  A cast to union is actually a constructor
202though, not a cast, and hence does not yield an lvalue like normal
203casts.  (*Note Constructors::.)
204
205   The types that may be cast to the union type are those of the members
206of the union.  Thus, given the following union and variables:
207
208     union foo { int i; double d; };
209     int x;
210     double y;
211
212both `x' and `y' can be cast to type `union' foo.
213
214   Using the cast as the right-hand side of an assignment to a variable
215of union type is equivalent to storing in a member of the union:
216
217     union foo u;
218     ...
219     u = (union foo) x  ==  u.i = x
220     u = (union foo) y  ==  u.d = y
221
222   You can also use the union cast as a function argument:
223
224     void hack (union foo);
225     ...
226     hack ((union foo) x);
227
228
229File: gcc.info,  Node: Function Attributes,  Next: Function Prototypes,  Prev: Case Ranges,  Up: C Extensions
230
231Declaring Attributes of Functions
232=================================
233
234   In GNU C, you declare certain things about functions called in your
235program which help the compiler optimize function calls and check your
236code more carefully.
237
238   The keyword `__attribute__' allows you to specify special attributes
239when making a declaration.  This keyword is followed by an attribute
240specification inside double parentheses.  Eight attributes, `noreturn',
241`const', `format', `section', `constructor', `destructor', `unused' and
242`weak' are currently defined for functions.  Other attributes, including
243`section' are supported for variables declarations (*note Variable
244Attributes::.) and for types (*note Type Attributes::.).
245
246   You may also specify attributes with `__' preceding and following
247each keyword.  This allows you to use them in header files without
248being concerned about a possible macro of the same name.  For example,
249you may use `__noreturn__' instead of `noreturn'.
250
251`noreturn'
252     A few standard library functions, such as `abort' and `exit',
253     cannot return.  GNU CC knows this automatically.  Some programs
254     define their own functions that never return.  You can declare them
255     `noreturn' to tell the compiler this fact.  For example,
256
257          void fatal () __attribute__ ((noreturn));
258         
259          void
260          fatal (...)
261          {
262            ... /* Print error message. */ ...
263            exit (1);
264          }
265
266     The `noreturn' keyword tells the compiler to assume that `fatal'
267     cannot return.  It can then optimize without regard to what would
268     happen if `fatal' ever did return.  This makes slightly better
269     code.  More importantly, it helps avoid spurious warnings of
270     uninitialized variables.
271
272     Do not assume that registers saved by the calling function are
273     restored before calling the `noreturn' function.
274
275     It does not make sense for a `noreturn' function to have a return
276     type other than `void'.
277
278     The attribute `noreturn' is not implemented in GNU C versions
279     earlier than 2.5.  An alternative way to declare that a function
280     does not return, which works in the current version and in some
281     older versions, is as follows:
282
283          typedef void voidfn ();
284         
285          volatile voidfn fatal;
286
287`const'
288     Many functions do not examine any values except their arguments,
289     and have no effects except the return value.  Such a function can
290     be subject to common subexpression elimination and loop
291     optimization just as an arithmetic operator would be.  These
292     functions should be declared with the attribute `const'.  For
293     example,
294
295          int square (int) __attribute__ ((const));
296
297     says that the hypothetical function `square' is safe to call fewer
298     times than the program says.
299
300     The attribute `const' is not implemented in GNU C versions earlier
301     than 2.5.  An alternative way to declare that a function has no
302     side effects, which works in the current version and in some older
303     versions, is as follows:
304
305          typedef int intfn ();
306         
307          extern const intfn square;
308
309     This approach does not work in GNU C++ from 2.6.0 on, since the
310     language specifies that the `const' must be attached to the return
311     value.
312
313     Note that a function that has pointer arguments and examines the
314     data pointed to must *not* be declared `const'.  Likewise, a
315     function that calls a non-`const' function usually must not be
316     `const'.  It does not make sense for a `const' function to return
317     `void'.
318
319`format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)'
320     The `format' attribute specifies that a function takes `printf' or
321     `scanf' style arguments which should be type-checked against a
322     format string.  For example, the declaration:
323
324          extern int
325          my_printf (void *my_object, const char *my_format, ...)
326                __attribute__ ((format (printf, 2, 3)));
327
328     causes the compiler to check the arguments in calls to `my_printf'
329     for consistency with the `printf' style format string argument
330     `my_format'.
331
332     The parameter ARCHETYPE determines how the format string is
333     interpreted, and should be either `printf' or `scanf'.  The
334     parameter STRING-INDEX specifies which argument is the format
335     string argument (starting from 1), while FIRST-TO-CHECK is the
336     number of the first argument to check against the format string.
337     For functions where the arguments are not available to be checked
338     (such as `vprintf'), specify the third parameter as zero.  In this
339     case the compiler only checks the format string for consistency.
340
341     In the example above, the format string (`my_format') is the second
342     argument of the function `my_print', and the arguments to check
343     start with the third argument, so the correct parameters for the
344     format attribute are 2 and 3.
345
346     The `format' attribute allows you to identify your own functions
347     which take format strings as arguments, so that GNU CC can check
348     the calls to these functions for errors.  The compiler always
349     checks formats for the ANSI library functions `printf', `fprintf',
350     `sprintf', `scanf', `fscanf', `sscanf', `vprintf', `vfprintf' and
351     `vsprintf' whenever such warnings are requested (using
352     `-Wformat'), so there is no need to modify the header file
353     `stdio.h'.
354
355`section ("section-name")'
356     Normally, the compiler places the code it generates in the `text'
357     section.  Sometimes, however, you need additional sections, or you
358     need certain particular functions to appear in special sections.
359     The `section' attribute specifies that a function lives in a
360     particular section.  For example, the declaration:
361
362          extern void foobar (void) __attribute__ ((section ("bar")));
363
364     puts the function `foobar' in the `bar' section.
365
366     Some file formats do not support arbitrary sections so the
367     `section' attribute is not available on all platforms.  If you
368     need to map the entire contents of a module to a particular
369     section, consider using the facilities of the linker instead.
370
371`constructor'
372`destructor'
373     The `constructor' attribute causes the function to be called
374     automatically before execution enters `main ()'.  Similarly, the
375     `destructor' attribute causes the function to be called
376     automatically after `main ()' has completed or `exit ()' has been
377     called.  Functions with these attributes are useful for
378     initializing data that will be used implicitly during the
379     execution of the program.
380
381     These attributes are not currently implemented for Objective C.
382
383`unused'
384     This attribute, attached to a function, means that the function is
385     meant to be possibly unused.  GNU CC will not produce a warning
386     for this function.
387
388`weak'
389     The `weak' attribute causes the declaration to be emitted as a weak
390     symbol rather than a global.  This is primarily useful in defining
391     library functions which can be overridden in user code, though it
392     can also be used with non-function declarations.  Weak symbols are
393     supported for ELF targets, and also for a.out targets when using
394     the GNU assembler and linker.
395
396`alias ("target")'
397     The `alias' attribute causes the declaration to be emitted as an
398     alias for another symbol, which must be specified.  For instance,
399
400          void __f () { /* do something */; }
401          void f () __attribute__ ((weak, alias ("__f")));
402
403     declares `f' to be a weak alias for `__f'.  In C++, the mangled
404     name for the target must be used.
405
406`regparm (NUMBER)'
407     On the Intel 386, the `regparm' attribute causes the compiler to
408     pass up to NUMBER integer arguments in registers EAX, EDX, and ECX
409     instead of on the stack.  Functions that take a variable number of
410     arguments will continue to be passed all of their arguments on the
411     stack.
412
413`stdcall'
414     On the Intel 386, the `stdcall' attribute causes the compiler to
415     assume that the called function will pop off the stack space used
416     to pass arguments, unless it takes a variable number of arguments.
417
418`cdecl'
419     On the Intel 386, the `cdecl' attribute causes the compiler to
420     assume that the called function will pop off the stack space used
421     to pass arguments, unless it takes a variable number of arguments.
422     This is useful to override the effects of the `-mrtd' switch.
423
424   You can specify multiple attributes in a declaration by separating
425them by commas within the double parentheses or by immediately
426following an attribute declaration with another attribute declaration.
427
428   Some people object to the `__attribute__' feature, suggesting that
429ANSI C's `#pragma' should be used instead.  There are two reasons for
430not doing this.
431
432  1. It is impossible to generate `#pragma' commands from a macro.
433
434  2. There is no telling what the same `#pragma' might mean in another
435     compiler.
436
437   These two reasons apply to almost any application that might be
438proposed for `#pragma'.  It is basically a mistake to use `#pragma' for
439*anything*.
440
441
442File: gcc.info,  Node: Function Prototypes,  Next: C++ Comments,  Prev: Function Attributes,  Up: C Extensions
443
444Prototypes and Old-Style Function Definitions
445=============================================
446
447   GNU C extends ANSI C to allow a function prototype to override a
448later old-style non-prototype definition.  Consider the following
449example:
450
451     /* Use prototypes unless the compiler is old-fashioned.  */
452     #if __STDC__
453     #define P(x) x
454     #else
455     #define P(x) ()
456     #endif
457     
458     /* Prototype function declaration.  */
459     int isroot P((uid_t));
460     
461     /* Old-style function definition.  */
462     int
463     isroot (x)   /* ??? lossage here ??? */
464          uid_t x;
465     {
466       return x == 0;
467     }
468
469   Suppose the type `uid_t' happens to be `short'.  ANSI C does not
470allow this example, because subword arguments in old-style
471non-prototype definitions are promoted.  Therefore in this example the
472function definition's argument is really an `int', which does not match
473the prototype argument type of `short'.
474
475   This restriction of ANSI C makes it hard to write code that is
476portable to traditional C compilers, because the programmer does not
477know whether the `uid_t' type is `short', `int', or `long'.  Therefore,
478in cases like these GNU C allows a prototype to override a later
479old-style definition.  More precisely, in GNU C, a function prototype
480argument type overrides the argument type specified by a later
481old-style definition if the former type is the same as the latter type
482before promotion.  Thus in GNU C the above example is equivalent to the
483following:
484
485     int isroot (uid_t);
486     
487     int
488     isroot (uid_t x)
489     {
490       return x == 0;
491     }
492
493   GNU C++ does not support old-style function definitions, so this
494extension is irrelevant.
495
496
497File: gcc.info,  Node: C++ Comments,  Next: Dollar Signs,  Prev: Function Prototypes,  Up: C Extensions
498
499C++ Style Comments
500==================
501
502   In GNU C, you may use C++ style comments, which start with `//' and
503continue until the end of the line.  Many other C implementations allow
504such comments, and they are likely to be in a future C standard.
505However, C++ style comments are not recognized if you specify `-ansi'
506or `-traditional', since they are incompatible with traditional
507constructs like `dividend//*comment*/divisor'.
508
509
510File: gcc.info,  Node: Dollar Signs,  Next: Character Escapes,  Prev: C++ Comments,  Up: C Extensions
511
512Dollar Signs in Identifier Names
513================================
514
515   In GNU C, you may use dollar signs in identifier names.  This is
516because many traditional C implementations allow such identifiers.
517
518   On some machines, dollar signs are allowed in identifiers if you
519specify `-traditional'.  On a few systems they are allowed by default,
520even if you do not use `-traditional'.  But they are never allowed if
521you specify `-ansi'.
522
523   There are certain ANSI C programs (obscure, to be sure) that would
524compile incorrectly if dollar signs were permitted in identifiers.  For
525example:
526
527     #define foo(a) #a
528     #define lose(b) foo (b)
529     #define test$
530     lose (test)
531
532
533File: gcc.info,  Node: Character Escapes,  Next: Variable Attributes,  Prev: Dollar Signs,  Up: C Extensions
534
535The Character ESC in Constants
536==============================
537
538   You can use the sequence `\e' in a string or character constant to
539stand for the ASCII character ESC.
540
541
542File: gcc.info,  Node: Alignment,  Next: Inline,  Prev: Type Attributes,  Up: C Extensions
543
544Inquiring on Alignment of Types or Variables
545============================================
546
547   The keyword `__alignof__' allows you to inquire about how an object
548is aligned, or the minimum alignment usually required by a type.  Its
549syntax is just like `sizeof'.
550
551   For example, if the target machine requires a `double' value to be
552aligned on an 8-byte boundary, then `__alignof__ (double)' is 8.  This
553is true on many RISC machines.  On more traditional machine designs,
554`__alignof__ (double)' is 4 or even 2.
555
556   Some machines never actually require alignment; they allow reference
557to any data type even at an odd addresses.  For these machines,
558`__alignof__' reports the *recommended* alignment of a type.
559
560   When the operand of `__alignof__' is an lvalue rather than a type,
561the value is the largest alignment that the lvalue is known to have.
562It may have this alignment as a result of its data type, or because it
563is part of a structure and inherits alignment from that structure.  For
564example, after this declaration:
565
566     struct foo { int x; char y; } foo1;
567
568the value of `__alignof__ (foo1.y)' is probably 2 or 4, the same as
569`__alignof__ (int)', even though the data type of `foo1.y' does not
570itself demand any alignment.
571
572   A related feature which lets you specify the alignment of an object
573is `__attribute__ ((aligned (ALIGNMENT)))'; see the following section.
574
575
576File: gcc.info,  Node: Variable Attributes,  Next: Type Attributes,  Prev: Character Escapes,  Up: C Extensions
577
578Specifying Attributes of Variables
579==================================
580
581   The keyword `__attribute__' allows you to specify special attributes
582of variables or structure fields.  This keyword is followed by an
583attribute specification inside double parentheses.  Eight attributes
584are currently defined for variables: `aligned', `mode', `nocommon',
585`packed', `section', `transparent_union', `unused', and `weak'.  Other
586attributes are available for functions (*note Function Attributes::.)
587and for types (*note Type Attributes::.).
588
589   You may also specify attributes with `__' preceding and following
590each keyword.  This allows you to use them in header files without
591being concerned about a possible macro of the same name.  For example,
592you may use `__aligned__' instead of `aligned'.
593
594`aligned (ALIGNMENT)'
595     This attribute specifies a minimum alignment for the variable or
596     structure field, measured in bytes.  For example, the declaration:
597
598          int x __attribute__ ((aligned (16))) = 0;
599
600     causes the compiler to allocate the global variable `x' on a
601     16-byte boundary.  On a 68040, this could be used in conjunction
602     with an `asm' expression to access the `move16' instruction which
603     requires 16-byte aligned operands.
604
605     You can also specify the alignment of structure fields.  For
606     example, to create a double-word aligned `int' pair, you could
607     write:
608
609          struct foo { int x[2] __attribute__ ((aligned (8))); };
610
611     This is an alternative to creating a union with a `double' member
612     that forces the union to be double-word aligned.
613
614     It is not possible to specify the alignment of functions; the
615     alignment of functions is determined by the machine's requirements
616     and cannot be changed.  You cannot specify alignment for a typedef
617     name because such a name is just an alias, not a distinct type.
618
619     As in the preceding examples, you can explicitly specify the
620     alignment (in bytes) that you wish the compiler to use for a given
621     variable or structure field.  Alternatively, you can leave out the
622     alignment factor and just ask the compiler to align a variable or
623     field to the maximum useful alignment for the target machine you
624     are compiling for.  For example, you could write:
625
626          short array[3] __attribute__ ((aligned));
627
628     Whenever you leave out the alignment factor in an `aligned'
629     attribute specification, the compiler automatically sets the
630     alignment for the declared variable or field to the largest
631     alignment which is ever used for any data type on the target
632     machine you are compiling for.  Doing this can often make copy
633     operations more efficient, because the compiler can use whatever
634     instructions copy the biggest chunks of memory when performing
635     copies to or from the variables or fields that you have aligned
636     this way.
637
638     The `aligned' attribute can only increase the alignment; but you
639     can decrease it by specifying `packed' as well.  See below.
640
641     Note that the effectiveness of `aligned' attributes may be limited
642     by inherent limitations in your linker.  On many systems, the
643     linker is only able to arrange for variables to be aligned up to a
644     certain maximum alignment.  (For some linkers, the maximum
645     supported alignment may be very very small.)  If your linker is
646     only able to align variables up to a maximum of 8 byte alignment,
647     then specifying `aligned(16)' in an `__attribute__' will still
648     only provide you with 8 byte alignment.  See your linker
649     documentation for further information.
650
651`mode (MODE)'
652     This attribute specifies the data type for the
653     declaration--whichever type corresponds to the mode MODE.  This in
654     effect lets you request an integer or floating point type
655     according to its width.
656
657     You may also specify a mode of `byte' or `__byte__' to indicate
658     the mode corresponding to a one-byte integer, `word' or `__word__'
659     for the mode of a one-word integer, and `pointer' or `__pointer__'
660     for the mode used to represent pointers.
661
662`nocommon'
663     This attribute specifies requests GNU CC not to place a variable
664     "common" but instead to allocate space for it directly.  If you
665     specify the `-fno-common' flag, GNU CC will do this for all
666     variables.
667
668     Specifying the `nocommon' attribute for a variable provides an
669     initialization of zeros.  A variable may only be initialized in one
670     source file.
671
672`packed'
673     The `packed' attribute specifies that a variable or structure field
674     should have the smallest possible alignment--one byte for a
675     variable, and one bit for a field, unless you specify a larger
676     value with the `aligned' attribute.
677
678     Here is a structure in which the field `x' is packed, so that it
679     immediately follows `a':
680
681          struct foo
682          {
683            char a;
684            int x[2] __attribute__ ((packed));
685          };
686
687`section ("section-name")'
688     Normally, the compiler places the objects it generates in sections
689     like `data' and `bss'.  Sometimes, however, you need additional
690     sections, or you need certain particular variables to appear in
691     special sections, for example to map to special hardware.  The
692     `section' attribute specifies that a variable (or function) lives
693     in a particular section.  For example, this small program uses
694     several specific section names:
695
696          struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
697          struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
698          char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
699          int init_data_copy __attribute__ ((section ("INITDATACOPY"))) = 0;
700         
701          main()
702          {
703            /* Initialize stack pointer */
704            init_sp (stack + sizeof (stack));
705         
706            /* Initialize initialized data */
707            memcpy (&init_data_copy, &data, &edata - &data);
708         
709            /* Turn on the serial ports */
710            init_duart (&a);
711            init_duart (&b);
712          }
713
714     Use the `section' attribute with an *initialized* definition of a
715     *global* variable, as shown in the example.  GNU CC issues a
716     warning and otherwise ignores the `section' attribute in
717     uninitialized variable declarations.
718
719     You may only use the `section' attribute with a fully initialized
720     global definition because of the way linkers work.  The linker
721     requires each object be defined once, with the exception that
722     uninitialized variables tentatively go in the `common' (or `bss')
723     section and can be multiply "defined".  You can force a variable
724     to be initialized with the `-fno-common' flag or the `nocommon'
725     attribute.
726
727     Some file formats do not support arbitrary sections so the
728     `section' attribute is not available on all platforms.  If you
729     need to map the entire contents of a module to a particular
730     section, consider using the facilities of the linker instead.
731
732`transparent_union'
733     This attribute, attached to a function argument variable which is a
734     union, means to pass the argument in the same way that the first
735     union member would be passed.  You can also use this attribute on a
736     `typedef' for a union data type; then it applies to all function
737     arguments with that type.
738
739`unused'
740     This attribute, attached to a variable, means that the variable is
741     meant to be possibly unused.  GNU CC will not produce a warning
742     for this variable.
743
744`weak'
745     The `weak' attribute is described in *Note Function Attributes::.
746
747   To specify multiple attributes, separate them by commas within the
748double parentheses: for example, `__attribute__ ((aligned (16),
749packed))'.
750
751
752File: gcc.info,  Node: Type Attributes,  Next: Alignment,  Prev: Variable Attributes,  Up: C Extensions
753
754Specifying Attributes of Types
755==============================
756
757   The keyword `__attribute__' allows you to specify special attributes
758of `struct' and `union' types when you define such types.  This keyword
759is followed by an attribute specification inside double parentheses.
760Three attributes are currently defined for types: `aligned', `packed',
761and `transparent_union'.  Other attributes are defined for functions
762(*note Function Attributes::.) and for variables (*note Variable
763Attributes::.).
764
765   You may also specify any one of these attributes with `__' preceding
766and following its keyword.  This allows you to use these attributes in
767header files without being concerned about a possible macro of the same
768name.  For example, you may use `__aligned__' instead of `aligned'.
769
770   You may specify the `aligned' and `transparent_union' attributes
771either in a `typedef' declaration or just past the closing curly brace
772of a complete enum, struct or union type *definition* and the `packed'
773attribute only past the closing brace of a definition.
774
775`aligned (ALIGNMENT)'
776     This attribute specifies a minimum alignment (in bytes) for
777     variables of the specified type.  For example, the declarations:
778
779          struct S { short f[3]; } __attribute__ ((aligned (8));
780          typedef int more_aligned_int __attribute__ ((aligned (8));
781
782     force the compiler to insure (as fas as it can) that each variable
783     whose type is `struct S' or `more_aligned_int' will be allocated
784     and aligned *at least* on a 8-byte boundary.  On a Sparc, having
785     all variables of type `struct S' aligned to 8-byte boundaries
786     allows the compiler to use the `ldd' and `std' (doubleword load and
787     store) instructions when copying one variable of type `struct S' to
788     another, thus improving run-time efficiency.
789
790     Note that the alignment of any given `struct' or `union' type is
791     required by the ANSI C standard to be at least a perfect multiple
792     of the lowest common multiple of the alignments of all of the
793     members of the `struct' or `union' in question.  This means that
794     you *can* effectively adjust the alignment of a `struct' or `union'
795     type by attaching an `aligned' attribute to any one of the members
796     of such a type, but the notation illustrated in the example above
797     is a more obvious, intuitive, and readable way to request the
798     compiler to adjust the alignment of an entire `struct' or `union'
799     type.
800
801     As in the preceding example, you can explicitly specify the
802     alignment (in bytes) that you wish the compiler to use for a given
803     `struct' or `union' type.  Alternatively, you can leave out the
804     alignment factor and just ask the compiler to align a type to the
805     maximum useful alignment for the target machine you are compiling
806     for.  For example, you could write:
807
808          struct S { short f[3]; } __attribute__ ((aligned));
809
810     Whenever you leave out the alignment factor in an `aligned'
811     attribute specification, the compiler automatically sets the
812     alignment for the type to the largest alignment which is ever used
813     for any data type on the target machine you are compiling for.
814     Doing this can often make copy operations more efficient, because
815     the compiler can use whatever instructions copy the biggest chunks
816     of memory when performing copies to or from the variables which
817     have types that you have aligned this way.
818
819     In the example above, if the size of each `short' is 2 bytes, then
820     the size of the entire `struct S' type is 6 bytes.  The smallest
821     power of two which is greater than or equal to that is 8, so the
822     compiler sets the alignment for the entire `struct S' type to 8
823     bytes.
824
825     Note that although you can ask the compiler to select a
826     time-efficient alignment for a given type and then declare only
827     individual stand-alone objects of that type, the compiler's
828     ability to select a time-efficient alignment is primarily useful
829     only when you plan to create arrays of variables having the
830     relevant (efficiently aligned) type.  If you declare or use arrays
831     of variables of an efficiently-aligned type, then it is likely
832     that your program will also be doing pointer arithmetic (or
833     subscripting, which amounts to the same thing) on pointers to the
834     relevant type, and the code that the compiler generates for these
835     pointer arithmetic operations will often be more efficient for
836     efficiently-aligned types than for other types.
837
838     The `aligned' attribute can only increase the alignment; but you
839     can decrease it by specifying `packed' as well.  See below.
840
841     Note that the effectiveness of `aligned' attributes may be limited
842     by inherent limitations in your linker.  On many systems, the
843     linker is only able to arrange for variables to be aligned up to a
844     certain maximum alignment.  (For some linkers, the maximum
845     supported alignment may be very very small.)  If your linker is
846     only able to align variables up to a maximum of 8 byte alignment,
847     then specifying `aligned(16)' in an `__attribute__' will still
848     only provide you with 8 byte alignment.  See your linker
849     documentation for further information.
850
851`packed'
852     This attribute, attached to an `enum', `struct', or `union' type
853     definition, specified that the minimum required memory be used to
854     represent the type.
855
856     Specifying this attribute for `struct' and `union' types is
857     equivalent to specifying the `packed' attribute on each of the
858     structure or union members.  Specifying the `-fshort-enums' flag
859     on the line is equivalent to specifying the `packed' attribute on
860     all `enum' definitions.
861
862     You may only specify this attribute after a closing curly brace on
863     an `enum' definition, not in a `typedef' declaration.
864
865`transparent_union'
866     This attribute, attached to a `union' type definition, indicates
867     that any variable having that union type should, if passed to a
868     function, be passed in the same way that the first union member
869     would be passed.  For example:
870
871          union foo
872          {
873            char a;
874            int x[2];
875          } __attribute__ ((transparent_union));
876
877   To specify multiple attributes, separate them by commas within the
878double parentheses: for example, `__attribute__ ((aligned (16),
879packed))'.
880
881
882File: gcc.info,  Node: Inline,  Next: Extended Asm,  Prev: Alignment,  Up: C Extensions
883
884An Inline Function is As Fast As a Macro
885========================================
886
887   By declaring a function `inline', you can direct GNU CC to integrate
888that function's code into the code for its callers.  This makes
889execution faster by eliminating the function-call overhead; in
890addition, if any of the actual argument values are constant, their known
891values may permit simplifications at compile time so that not all of the
892inline function's code needs to be included.  The effect on code size is
893less predictable; object code may be larger or smaller with function
894inlining, depending on the particular case.  Inlining of functions is an
895optimization and it really "works" only in optimizing compilation.  If
896you don't use `-O', no function is really inline.
897
898   To declare a function inline, use the `inline' keyword in its
899declaration, like this:
900
901     inline int
902     inc (int *a)
903     {
904       (*a)++;
905     }
906
907   (If you are writing a header file to be included in ANSI C programs,
908write `__inline__' instead of `inline'.  *Note Alternate Keywords::.)
909
910   You can also make all "simple enough" functions inline with the
911option `-finline-functions'.  Note that certain usages in a function
912definition can make it unsuitable for inline substitution.
913
914   Note that in C and Objective C, unlike C++, the `inline' keyword
915does not affect the linkage of the function.
916
917   GNU CC automatically inlines member functions defined within the
918class body of C++ programs even if they are not explicitly declared
919`inline'.  (You can override this with `-fno-default-inline'; *note
920Options Controlling C++ Dialect: C++ Dialect Options..)
921
922   When a function is both inline and `static', if all calls to the
923function are integrated into the caller, and the function's address is
924never used, then the function's own assembler code is never referenced.
925In this case, GNU CC does not actually output assembler code for the
926function, unless you specify the option `-fkeep-inline-functions'.
927Some calls cannot be integrated for various reasons (in particular,
928calls that precede the function's definition cannot be integrated, and
929neither can recursive calls within the definition).  If there is a
930nonintegrated call, then the function is compiled to assembler code as
931usual.  The function must also be compiled as usual if the program
932refers to its address, because that can't be inlined.
933
934   When an inline function is not `static', then the compiler must
935assume that there may be calls from other source files; since a global
936symbol can be defined only once in any program, the function must not
937be defined in the other source files, so the calls therein cannot be
938integrated.  Therefore, a non-`static' inline function is always
939compiled on its own in the usual fashion.
940
941   If you specify both `inline' and `extern' in the function
942definition, then the definition is used only for inlining.  In no case
943is the function compiled on its own, not even if you refer to its
944address explicitly.  Such an address becomes an external reference, as
945if you had only declared the function, and had not defined it.
946
947   This combination of `inline' and `extern' has almost the effect of a
948macro.  The way to use it is to put a function definition in a header
949file with these keywords, and put another copy of the definition
950(lacking `inline' and `extern') in a library file.  The definition in
951the header file will cause most calls to the function to be inlined.
952If any uses of the function remain, they will refer to the single copy
953in the library.
954
955   GNU C does not inline any functions when not optimizing.  It is not
956clear whether it is better to inline or not, in this case, but we found
957that a correct implementation when not optimizing was difficult.  So we
958did the easy thing, and turned it off.
959
960
961File: gcc.info,  Node: Extended Asm,  Next: Asm Labels,  Prev: Inline,  Up: C Extensions
962
963Assembler Instructions with C Expression Operands
964=================================================
965
966   In an assembler instruction using `asm', you can now specify the
967operands of the instruction using C expressions.  This means no more
968guessing which registers or memory locations will contain the data you
969want to use.
970
971   You must specify an assembler instruction template much like what
972appears in a machine description, plus an operand constraint string for
973each operand.
974
975   For example, here is how to use the 68881's `fsinx' instruction:
976
977     asm ("fsinx %1,%0" : "=f" (result) : "f" (angle));
978
979Here `angle' is the C expression for the input operand while `result'
980is that of the output operand.  Each has `"f"' as its operand
981constraint, saying that a floating point register is required.  The `='
982in `=f' indicates that the operand is an output; all output operands'
983constraints must use `='.  The constraints use the same language used
984in the machine description (*note Constraints::.).
985
986   Each operand is described by an operand-constraint string followed
987by the C expression in parentheses.  A colon separates the assembler
988template from the first output operand, and another separates the last
989output operand from the first input, if any.  Commas separate output
990operands and separate inputs.  The total number of operands is limited
991to ten or to the maximum number of operands in any instruction pattern
992in the machine description, whichever is greater.
993
994   If there are no output operands, and there are input operands, then
995there must be two consecutive colons surrounding the place where the
996output operands would go.
997
998   Output operand expressions must be lvalues; the compiler can check
999this.  The input operands need not be lvalues.  The compiler cannot
1000check whether the operands have data types that are reasonable for the
1001instruction being executed.  It does not parse the assembler
1002instruction template and does not know what it means, or whether it is
1003valid assembler input.  The extended `asm' feature is most often used
1004for machine instructions that the compiler itself does not know exist.
1005If the output expression cannot be directly addressed (for example, it
1006is a bit field), your constraint must allow a register.  In that case,
1007GNU CC will use the register as the output of the `asm', and then store
1008that register into the output.
1009
1010   The output operands must be write-only; GNU CC will assume that the
1011values in these operands before the instruction are dead and need not be
1012generated.  Extended asm does not support input-output or read-write
1013operands.  For this reason, the constraint character `+', which
1014indicates such an operand, may not be used.
1015
1016   When the assembler instruction has a read-write operand, or an
1017operand in which only some of the bits are to be changed, you must
1018logically split its function into two separate operands, one input
1019operand and one write-only output operand.  The connection between them
1020is expressed by constraints which say they need to be in the same
1021location when the instruction executes.  You can use the same C
1022expression for both operands, or different expressions.  For example,
1023here we write the (fictitious) `combine' instruction with `bar' as its
1024read-only source operand and `foo' as its read-write destination:
1025
1026     asm ("combine %2,%0" : "=r" (foo) : "0" (foo), "g" (bar));
1027
1028The constraint `"0"' for operand 1 says that it must occupy the same
1029location as operand 0.  A digit in constraint is allowed only in an
1030input operand, and it must refer to an output operand.
1031
1032   Only a digit in the constraint can guarantee that one operand will
1033be in the same place as another.  The mere fact that `foo' is the value
1034of both operands is not enough to guarantee that they will be in the
1035same place in the generated assembler code.  The following would not
1036work:
1037
1038     asm ("combine %2,%0" : "=r" (foo) : "r" (foo), "g" (bar));
1039
1040   Various optimizations or reloading could cause operands 0 and 1 to
1041be in different registers; GNU CC knows no reason not to do so.  For
1042example, the compiler might find a copy of the value of `foo' in one
1043register and use it for operand 1, but generate the output operand 0 in
1044a different register (copying it afterward to `foo''s own address).  Of
1045course, since the register for operand 1 is not even mentioned in the
1046assembler code, the result will not work, but GNU CC can't tell that.
1047
1048   Some instructions clobber specific hard registers.  To describe
1049this, write a third colon after the input operands, followed by the
1050names of the clobbered hard registers (given as strings).  Here is a
1051realistic example for the Vax:
1052
1053     asm volatile ("movc3 %0,%1,%2"
1054                   : /* no outputs */
1055                   : "g" (from), "g" (to), "g" (count)
1056                   : "r0", "r1", "r2", "r3", "r4", "r5");
1057
1058   If you refer to a particular hardware register from the assembler
1059code, then you will probably have to list the register after the third
1060colon to tell the compiler that the register's value is modified.  In
1061many assemblers, the register names begin with `%'; to produce one `%'
1062in the assembler code, you must write `%%' in the input.
1063
1064   If your assembler instruction can alter the condition code register,
1065add `cc' to the list of clobbered registers.  GNU CC on some machines
1066represents the condition codes as a specific hardware register; `cc'
1067serves to name this register.  On other machines, the condition code is
1068handled differently, and specifying `cc' has no effect.  But it is
1069valid no matter what the machine.
1070
1071   If your assembler instruction modifies memory in an unpredictable
1072fashion, add `memory' to the list of clobbered registers.  This will
1073cause GNU CC to not keep memory values cached in registers across the
1074assembler instruction.
1075
1076   You can put multiple assembler instructions together in a single
1077`asm' template, separated either with newlines (written as `\n') or with
1078semicolons if the assembler allows such semicolons.  The GNU assembler
1079allows semicolons and all Unix assemblers seem to do so.  The input
1080operands are guaranteed not to use any of the clobbered registers, and
1081neither will the output operands' addresses, so you can read and write
1082the clobbered registers as many times as you like.  Here is an example
1083of multiple instructions in a template; it assumes that the subroutine
1084`_foo' accepts arguments in registers 9 and 10:
1085
1086     asm ("movl %0,r9;movl %1,r10;call _foo"
1087          : /* no outputs */
1088          : "g" (from), "g" (to)
1089          : "r9", "r10");
1090
1091   Unless an output operand has the `&' constraint modifier, GNU CC may
1092allocate it in the same register as an unrelated input operand, on the
1093assumption that the inputs are consumed before the outputs are produced.
1094This assumption may be false if the assembler code actually consists of
1095more than one instruction.  In such a case, use `&' for each output
1096operand that may not overlap an input.  *Note Modifiers::.
1097
1098   If you want to test the condition code produced by an assembler
1099instruction, you must include a branch and a label in the `asm'
1100construct, as follows:
1101
1102     asm ("clr %0;frob %1;beq 0f;mov #1,%0;0:"
1103          : "g" (result)
1104          : "g" (input));
1105
1106This assumes your assembler supports local labels, as the GNU assembler
1107and most Unix assemblers do.
1108
1109   Speaking of labels, jumps from one `asm' to another are not
1110supported.  The compiler's optimizers do not know about these jumps,
1111and therefore they cannot take account of them when deciding how to
1112optimize.
1113
1114   Usually the most convenient way to use these `asm' instructions is to
1115encapsulate them in macros that look like functions.  For example,
1116
1117     #define sin(x)       \
1118     ({ double __value, __arg = (x);   \
1119        asm ("fsinx %1,%0": "=f" (__value): "f" (__arg));  \
1120        __value; })
1121
1122Here the variable `__arg' is used to make sure that the instruction
1123operates on a proper `double' value, and to accept only those arguments
1124`x' which can convert automatically to a `double'.
1125
1126   Another way to make sure the instruction operates on the correct
1127data type is to use a cast in the `asm'.  This is different from using a
1128variable `__arg' in that it converts more different types.  For
1129example, if the desired type were `int', casting the argument to `int'
1130would accept a pointer with no complaint, while assigning the argument
1131to an `int' variable named `__arg' would warn about using a pointer
1132unless the caller explicitly casts it.
1133
1134   If an `asm' has output operands, GNU CC assumes for optimization
1135purposes that the instruction has no side effects except to change the
1136output operands.  This does not mean that instructions with a side
1137effect cannot be used, but you must be careful, because the compiler
1138may eliminate them if the output operands aren't used, or move them out
1139of loops, or replace two with one if they constitute a common
1140subexpression.  Also, if your instruction does have a side effect on a
1141variable that otherwise appears not to change, the old value of the
1142variable may be reused later if it happens to be found in a register.
1143
1144   You can prevent an `asm' instruction from being deleted, moved
1145significantly, or combined, by writing the keyword `volatile' after the
1146`asm'.  For example:
1147
1148     #define set_priority(x)  \
1149     asm volatile ("set_priority %0": /* no outputs */ : "g" (x))
1150
1151An instruction without output operands will not be deleted or moved
1152significantly, regardless, unless it is unreachable.
1153
1154   Note that even a volatile `asm' instruction can be moved in ways
1155that appear insignificant to the compiler, such as across jump
1156instructions.  You can't expect a sequence of volatile `asm'
1157instructions to remain perfectly consecutive.  If you want consecutive
1158output, use a single `asm'.
1159
1160   It is a natural idea to look for a way to give access to the
1161condition code left by the assembler instruction.  However, when we
1162attempted to implement this, we found no way to make it work reliably.
1163The problem is that output operands might need reloading, which would
1164result in additional following "store" instructions.  On most machines,
1165these instructions would alter the condition code before there was time
1166to test it.  This problem doesn't arise for ordinary "test" and
1167"compare" instructions because they don't have any output operands.
1168
1169   If you are writing a header file that should be includable in ANSI C
1170programs, write `__asm__' instead of `asm'.  *Note Alternate Keywords::.
1171
Note: See TracBrowser for help on using the repository browser.