Function-Basics.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: Function Basics</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Function Basics">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Function Basics">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Functions.html#Functions" rel="up" title="Functions">
  30. <link href="Function-Properties.html#Function-Properties" rel="next" title="Function Properties">
  31. <link href="Functions.html#Functions" rel="prev" title="Functions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Function-Basics"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Function-Properties.html#Function-Properties" accesskey="n" rel="next">Function Properties</a>, Up: <a href="Functions.html#Functions" accesskey="u" rel="up">Functions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Function-Basics-1"></a>
  69. <h4 class="subsection">10.8.1 Function Basics</h4>
  70. <a name="index-DECL_005fNAME-1"></a>
  71. <a name="index-DECL_005fASSEMBLER_005fNAME"></a>
  72. <a name="index-TREE_005fPUBLIC"></a>
  73. <a name="index-DECL_005fARTIFICIAL-1"></a>
  74. <a name="index-DECL_005fFUNCTION_005fSPECIFIC_005fTARGET"></a>
  75. <a name="index-DECL_005fFUNCTION_005fSPECIFIC_005fOPTIMIZATION"></a>
  76. <p>A function has four core parts: the name, the parameters, the result,
  77. and the body. The following macros and functions access these parts
  78. of a <code>FUNCTION_DECL</code> as well as other basic features:
  79. </p><dl compact="compact">
  80. <dt><code>DECL_NAME</code>
  81. <a name="index-DECL_005fNAME-2"></a>
  82. </dt>
  83. <dd><p>This macro returns the unqualified name of the function, as an
  84. <code>IDENTIFIER_NODE</code>. For an instantiation of a function template,
  85. the <code>DECL_NAME</code> is the unqualified name of the template, not
  86. something like <code>f&lt;int&gt;</code>. The value of <code>DECL_NAME</code> is
  87. undefined when used on a constructor, destructor, overloaded operator,
  88. or type-conversion operator, or any function that is implicitly
  89. generated by the compiler. See below for macros that can be used to
  90. distinguish these cases.
  91. </p>
  92. </dd>
  93. <dt><code>DECL_ASSEMBLER_NAME</code>
  94. <a name="index-DECL_005fASSEMBLER_005fNAME-1"></a>
  95. </dt>
  96. <dd><p>This macro returns the mangled name of the function, also an
  97. <code>IDENTIFIER_NODE</code>. This name does not contain leading underscores
  98. on systems that prefix all identifiers with underscores. The mangled
  99. name is computed in the same way on all platforms; if special processing
  100. is required to deal with the object file format used on a particular
  101. platform, it is the responsibility of the back end to perform those
  102. modifications. (Of course, the back end should not modify
  103. <code>DECL_ASSEMBLER_NAME</code> itself.)
  104. </p>
  105. <p>Using <code>DECL_ASSEMBLER_NAME</code> will cause additional memory to be
  106. allocated (for the mangled name of the entity) so it should be used
  107. only when emitting assembly code. It should not be used within the
  108. optimizers to determine whether or not two declarations are the same,
  109. even though some of the existing optimizers do use it in that way.
  110. These uses will be removed over time.
  111. </p>
  112. </dd>
  113. <dt><code>DECL_ARGUMENTS</code>
  114. <a name="index-DECL_005fARGUMENTS"></a>
  115. </dt>
  116. <dd><p>This macro returns the <code>PARM_DECL</code> for the first argument to the
  117. function. Subsequent <code>PARM_DECL</code> nodes can be obtained by
  118. following the <code>TREE_CHAIN</code> links.
  119. </p>
  120. </dd>
  121. <dt><code>DECL_RESULT</code>
  122. <a name="index-DECL_005fRESULT"></a>
  123. </dt>
  124. <dd><p>This macro returns the <code>RESULT_DECL</code> for the function.
  125. </p>
  126. </dd>
  127. <dt><code>DECL_SAVED_TREE</code>
  128. <a name="index-DECL_005fSAVED_005fTREE"></a>
  129. </dt>
  130. <dd><p>This macro returns the complete body of the function.
  131. </p>
  132. </dd>
  133. <dt><code>TREE_TYPE</code>
  134. <a name="index-TREE_005fTYPE-5"></a>
  135. </dt>
  136. <dd><p>This macro returns the <code>FUNCTION_TYPE</code> or <code>METHOD_TYPE</code> for
  137. the function.
  138. </p>
  139. </dd>
  140. <dt><code>DECL_INITIAL</code>
  141. <a name="index-DECL_005fINITIAL-1"></a>
  142. </dt>
  143. <dd><p>A function that has a definition in the current translation unit will
  144. have a non-<code>NULL</code> <code>DECL_INITIAL</code>. However, back ends should not make
  145. use of the particular value given by <code>DECL_INITIAL</code>.
  146. </p>
  147. <p>It should contain a tree of <code>BLOCK</code> nodes that mirrors the scopes
  148. that variables are bound in the function. Each block contains a list
  149. of decls declared in a basic block, a pointer to a chain of blocks at
  150. the next lower scope level, then a pointer to the next block at the
  151. same level and a backpointer to the parent <code>BLOCK</code> or
  152. <code>FUNCTION_DECL</code>. So given a function as follows:
  153. </p>
  154. <div class="smallexample">
  155. <pre class="smallexample">void foo()
  156. {
  157. int a;
  158. {
  159. int b;
  160. }
  161. int c;
  162. }
  163. </pre></div>
  164. <p>you would get the following:
  165. </p>
  166. <div class="smallexample">
  167. <pre class="smallexample">tree foo = FUNCTION_DECL;
  168. tree decl_a = VAR_DECL;
  169. tree decl_b = VAR_DECL;
  170. tree decl_c = VAR_DECL;
  171. tree block_a = BLOCK;
  172. tree block_b = BLOCK;
  173. tree block_c = BLOCK;
  174. BLOCK_VARS(block_a) = decl_a;
  175. BLOCK_SUBBLOCKS(block_a) = block_b;
  176. BLOCK_CHAIN(block_a) = block_c;
  177. BLOCK_SUPERCONTEXT(block_a) = foo;
  178. BLOCK_VARS(block_b) = decl_b;
  179. BLOCK_SUPERCONTEXT(block_b) = block_a;
  180. BLOCK_VARS(block_c) = decl_c;
  181. BLOCK_SUPERCONTEXT(block_c) = foo;
  182. DECL_INITIAL(foo) = block_a;
  183. </pre></div>
  184. </dd>
  185. </dl>
  186. <hr>
  187. <div class="header">
  188. <p>
  189. Next: <a href="Function-Properties.html#Function-Properties" accesskey="n" rel="next">Function Properties</a>, Up: <a href="Functions.html#Functions" accesskey="u" rel="up">Functions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  190. </div>
  191. </body>
  192. </html>