home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / gcc.info-9 (.txt) < prev    next >
GNU Info File  |  1995-11-26  |  50KB  |  905 lines

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