Varargs.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: Varargs</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Varargs">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Varargs">
  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="Target-Macros.html#Target-Macros" rel="up" title="Target Macros">
  30. <link href="Trampolines.html#Trampolines" rel="next" title="Trampolines">
  31. <link href="Miscellaneous-Register-Hooks.html#Miscellaneous-Register-Hooks" rel="prev" title="Miscellaneous Register Hooks">
  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="Varargs"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Trampolines.html#Trampolines" accesskey="n" rel="next">Trampolines</a>, Previous: <a href="Stack-and-Calling.html#Stack-and-Calling" accesskey="p" rel="prev">Stack and Calling</a>, Up: <a href="Target-Macros.html#Target-Macros" accesskey="u" rel="up">Target Macros</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="Implementing-the-Varargs-Macros"></a>
  69. <h3 class="section">17.10 Implementing the Varargs Macros</h3>
  70. <a name="index-varargs-implementation"></a>
  71. <p>GCC comes with an implementation of <code>&lt;varargs.h&gt;</code> and
  72. <code>&lt;stdarg.h&gt;</code> that work without change on machines that pass arguments
  73. on the stack. Other machines require their own implementations of
  74. varargs, and the two machine independent header files must have
  75. conditionals to include it.
  76. </p>
  77. <p>ISO <code>&lt;stdarg.h&gt;</code> differs from traditional <code>&lt;varargs.h&gt;</code> mainly in
  78. the calling convention for <code>va_start</code>. The traditional
  79. implementation takes just one argument, which is the variable in which
  80. to store the argument pointer. The ISO implementation of
  81. <code>va_start</code> takes an additional second argument. The user is
  82. supposed to write the last named argument of the function here.
  83. </p>
  84. <p>However, <code>va_start</code> should not use this argument. The way to find
  85. the end of the named arguments is with the built-in functions described
  86. below.
  87. </p>
  88. <dl>
  89. <dt><a name="index-_005f_005fbuiltin_005fsaveregs"></a>Macro: <strong>__builtin_saveregs</strong> <em>()</em></dt>
  90. <dd><p>Use this built-in function to save the argument registers in memory so
  91. that the varargs mechanism can access them. Both ISO and traditional
  92. versions of <code>va_start</code> must use <code>__builtin_saveregs</code>, unless
  93. you use <code>TARGET_SETUP_INCOMING_VARARGS</code> (see below) instead.
  94. </p>
  95. <p>On some machines, <code>__builtin_saveregs</code> is open-coded under the
  96. control of the target hook <code>TARGET_EXPAND_BUILTIN_SAVEREGS</code>. On
  97. other machines, it calls a routine written in assembler language,
  98. found in <samp>libgcc2.c</samp>.
  99. </p>
  100. <p>Code generated for the call to <code>__builtin_saveregs</code> appears at the
  101. beginning of the function, as opposed to where the call to
  102. <code>__builtin_saveregs</code> is written, regardless of what the code is.
  103. This is because the registers must be saved before the function starts
  104. to use them for its own purposes.
  105. </p></dd></dl>
  106. <dl>
  107. <dt><a name="index-_005f_005fbuiltin_005fnext_005farg"></a>Macro: <strong>__builtin_next_arg</strong> <em>(<var>lastarg</var>)</em></dt>
  108. <dd><p>This builtin returns the address of the first anonymous stack
  109. argument, as type <code>void *</code>. If <code>ARGS_GROW_DOWNWARD</code>, it
  110. returns the address of the location above the first anonymous stack
  111. argument. Use it in <code>va_start</code> to initialize the pointer for
  112. fetching arguments from the stack. Also use it in <code>va_start</code> to
  113. verify that the second parameter <var>lastarg</var> is the last named argument
  114. of the current function.
  115. </p></dd></dl>
  116. <dl>
  117. <dt><a name="index-_005f_005fbuiltin_005fclassify_005ftype"></a>Macro: <strong>__builtin_classify_type</strong> <em>(<var>object</var>)</em></dt>
  118. <dd><p>Since each machine has its own conventions for which data types are
  119. passed in which kind of register, your implementation of <code>va_arg</code>
  120. has to embody these conventions. The easiest way to categorize the
  121. specified data type is to use <code>__builtin_classify_type</code> together
  122. with <code>sizeof</code> and <code>__alignof__</code>.
  123. </p>
  124. <p><code>__builtin_classify_type</code> ignores the value of <var>object</var>,
  125. considering only its data type. It returns an integer describing what
  126. kind of type that is&mdash;integer, floating, pointer, structure, and so on.
  127. </p>
  128. <p>The file <samp>typeclass.h</samp> defines an enumeration that you can use to
  129. interpret the values of <code>__builtin_classify_type</code>.
  130. </p></dd></dl>
  131. <p>These machine description macros help implement varargs:
  132. </p>
  133. <dl>
  134. <dt><a name="index-TARGET_005fEXPAND_005fBUILTIN_005fSAVEREGS"></a>Target Hook: <em>rtx</em> <strong>TARGET_EXPAND_BUILTIN_SAVEREGS</strong> <em>(void)</em></dt>
  135. <dd><p>If defined, this hook produces the machine-specific code for a call to
  136. <code>__builtin_saveregs</code>. This code will be moved to the very
  137. beginning of the function, before any parameter access are made. The
  138. return value of this function should be an RTX that contains the value
  139. to use as the return of <code>__builtin_saveregs</code>.
  140. </p></dd></dl>
  141. <dl>
  142. <dt><a name="index-TARGET_005fSETUP_005fINCOMING_005fVARARGS"></a>Target Hook: <em>void</em> <strong>TARGET_SETUP_INCOMING_VARARGS</strong> <em>(cumulative_args_t <var>args_so_far</var>, machine_mode <var>mode</var>, tree <var>type</var>, int *<var>pretend_args_size</var>, int <var>second_time</var>)</em></dt>
  143. <dd><p>This target hook offers an alternative to using
  144. <code>__builtin_saveregs</code> and defining the hook
  145. <code>TARGET_EXPAND_BUILTIN_SAVEREGS</code>. Use it to store the anonymous
  146. register arguments into the stack so that all the arguments appear to
  147. have been passed consecutively on the stack. Once this is done, you can
  148. use the standard implementation of varargs that works for machines that
  149. pass all their arguments on the stack.
  150. </p>
  151. <p>The argument <var>args_so_far</var> points to the <code>CUMULATIVE_ARGS</code> data
  152. structure, containing the values that are obtained after processing the
  153. named arguments. The arguments <var>mode</var> and <var>type</var> describe the
  154. last named argument&mdash;its machine mode and its data type as a tree node.
  155. </p>
  156. <p>The target hook should do two things: first, push onto the stack all the
  157. argument registers <em>not</em> used for the named arguments, and second,
  158. store the size of the data thus pushed into the <code>int</code>-valued
  159. variable pointed to by <var>pretend_args_size</var>. The value that you
  160. store here will serve as additional offset for setting up the stack
  161. frame.
  162. </p>
  163. <p>Because you must generate code to push the anonymous arguments at
  164. compile time without knowing their data types,
  165. <code>TARGET_SETUP_INCOMING_VARARGS</code> is only useful on machines that
  166. have just a single category of argument register and use it uniformly
  167. for all data types.
  168. </p>
  169. <p>If the argument <var>second_time</var> is nonzero, it means that the
  170. arguments of the function are being analyzed for the second time. This
  171. happens for an inline function, which is not actually compiled until the
  172. end of the source file. The hook <code>TARGET_SETUP_INCOMING_VARARGS</code> should
  173. not generate any instructions in this case.
  174. </p></dd></dl>
  175. <dl>
  176. <dt><a name="index-TARGET_005fSTRICT_005fARGUMENT_005fNAMING"></a>Target Hook: <em>bool</em> <strong>TARGET_STRICT_ARGUMENT_NAMING</strong> <em>(cumulative_args_t <var>ca</var>)</em></dt>
  177. <dd><p>Define this hook to return <code>true</code> if the location where a function
  178. argument is passed depends on whether or not it is a named argument.
  179. </p>
  180. <p>This hook controls how the <var>named</var> argument to <code>TARGET_FUNCTION_ARG</code>
  181. is set for varargs and stdarg functions. If this hook returns
  182. <code>true</code>, the <var>named</var> argument is always true for named
  183. arguments, and false for unnamed arguments. If it returns <code>false</code>,
  184. but <code>TARGET_PRETEND_OUTGOING_VARARGS_NAMED</code> returns <code>true</code>,
  185. then all arguments are treated as named. Otherwise, all named arguments
  186. except the last are treated as named.
  187. </p>
  188. <p>You need not define this hook if it always returns <code>false</code>.
  189. </p></dd></dl>
  190. <dl>
  191. <dt><a name="index-TARGET_005fCALL_005fARGS"></a>Target Hook: <em>void</em> <strong>TARGET_CALL_ARGS</strong> <em>(rtx, <var>tree</var>)</em></dt>
  192. <dd><p>While generating RTL for a function call, this target hook is invoked once
  193. for each argument passed to the function, either a register returned by
  194. <code>TARGET_FUNCTION_ARG</code> or a memory location. It is called just
  195. before the point where argument registers are stored. The type of the
  196. function to be called is also passed as the second argument; it is
  197. <code>NULL_TREE</code> for libcalls. The <code>TARGET_END_CALL_ARGS</code> hook is
  198. invoked just after the code to copy the return reg has been emitted.
  199. This functionality can be used to perform special setup of call argument
  200. registers if a target needs it.
  201. For functions without arguments, the hook is called once with <code>pc_rtx</code>
  202. passed instead of an argument register.
  203. Most ports do not need to implement anything for this hook.
  204. </p></dd></dl>
  205. <dl>
  206. <dt><a name="index-TARGET_005fEND_005fCALL_005fARGS"></a>Target Hook: <em>void</em> <strong>TARGET_END_CALL_ARGS</strong> <em>(void)</em></dt>
  207. <dd><p>This target hook is invoked while generating RTL for a function call,
  208. just after the point where the return reg is copied into a pseudo. It
  209. signals that all the call argument and return registers for the just
  210. emitted call are now no longer in use.
  211. Most ports do not need to implement anything for this hook.
  212. </p></dd></dl>
  213. <dl>
  214. <dt><a name="index-TARGET_005fPRETEND_005fOUTGOING_005fVARARGS_005fNAMED"></a>Target Hook: <em>bool</em> <strong>TARGET_PRETEND_OUTGOING_VARARGS_NAMED</strong> <em>(cumulative_args_t <var>ca</var>)</em></dt>
  215. <dd><p>If you need to conditionally change ABIs so that one works with
  216. <code>TARGET_SETUP_INCOMING_VARARGS</code>, but the other works like neither
  217. <code>TARGET_SETUP_INCOMING_VARARGS</code> nor <code>TARGET_STRICT_ARGUMENT_NAMING</code> was
  218. defined, then define this hook to return <code>true</code> if
  219. <code>TARGET_SETUP_INCOMING_VARARGS</code> is used, <code>false</code> otherwise.
  220. Otherwise, you should not define this hook.
  221. </p></dd></dl>
  222. <dl>
  223. <dt><a name="index-TARGET_005fLOAD_005fBOUNDS_005fFOR_005fARG"></a>Target Hook: <em>rtx</em> <strong>TARGET_LOAD_BOUNDS_FOR_ARG</strong> <em>(rtx <var>slot</var>, rtx <var>arg</var>, rtx <var>slot_no</var>)</em></dt>
  224. <dd><p>This hook is used by expand pass to emit insn to load bounds of
  225. <var>arg</var> passed in <var>slot</var>. Expand pass uses this hook in case
  226. bounds of <var>arg</var> are not passed in register. If <var>slot</var> is a
  227. memory, then bounds are loaded as for regular pointer loaded from
  228. memory. If <var>slot</var> is not a memory then <var>slot_no</var> is an integer
  229. constant holding number of the target dependent special slot which
  230. should be used to obtain bounds. Hook returns RTX holding loaded bounds.
  231. </p></dd></dl>
  232. <dl>
  233. <dt><a name="index-TARGET_005fSTORE_005fBOUNDS_005fFOR_005fARG"></a>Target Hook: <em>void</em> <strong>TARGET_STORE_BOUNDS_FOR_ARG</strong> <em>(rtx <var>arg</var>, rtx <var>slot</var>, rtx <var>bounds</var>, rtx <var>slot_no</var>)</em></dt>
  234. <dd><p>This hook is used by expand pass to emit insns to store <var>bounds</var> of
  235. <var>arg</var> passed in <var>slot</var>. Expand pass uses this hook in case
  236. <var>bounds</var> of <var>arg</var> are not passed in register. If <var>slot</var> is a
  237. memory, then <var>bounds</var> are stored as for regular pointer stored in
  238. memory. If <var>slot</var> is not a memory then <var>slot_no</var> is an integer
  239. constant holding number of the target dependent special slot which
  240. should be used to store <var>bounds</var>.
  241. </p></dd></dl>
  242. <dl>
  243. <dt><a name="index-TARGET_005fLOAD_005fRETURNED_005fBOUNDS"></a>Target Hook: <em>rtx</em> <strong>TARGET_LOAD_RETURNED_BOUNDS</strong> <em>(rtx <var>slot</var>)</em></dt>
  244. <dd><p>This hook is used by expand pass to emit insn to load bounds
  245. returned by function call in <var>slot</var>. Hook returns RTX holding
  246. loaded bounds.
  247. </p></dd></dl>
  248. <dl>
  249. <dt><a name="index-TARGET_005fSTORE_005fRETURNED_005fBOUNDS"></a>Target Hook: <em>void</em> <strong>TARGET_STORE_RETURNED_BOUNDS</strong> <em>(rtx <var>slot</var>, rtx <var>bounds</var>)</em></dt>
  250. <dd><p>This hook is used by expand pass to emit insn to store <var>bounds</var>
  251. returned by function call into <var>slot</var>.
  252. </p></dd></dl>
  253. <dl>
  254. <dt><a name="index-TARGET_005fCHKP_005fFUNCTION_005fVALUE_005fBOUNDS"></a>Target Hook: <em>rtx</em> <strong>TARGET_CHKP_FUNCTION_VALUE_BOUNDS</strong> <em>(const_tree <var>ret_type</var>, const_tree <var>fn_decl_or_type</var>, bool <var>outgoing</var>)</em></dt>
  255. <dd><p>Define this to return an RTX representing the place where a function
  256. returns bounds for returned pointers. Arguments meaning is similar to
  257. <code>TARGET_FUNCTION_VALUE</code>.
  258. </p></dd></dl>
  259. <dl>
  260. <dt><a name="index-TARGET_005fSETUP_005fINCOMING_005fVARARG_005fBOUNDS"></a>Target Hook: <em>void</em> <strong>TARGET_SETUP_INCOMING_VARARG_BOUNDS</strong> <em>(cumulative_args_t <var>args_so_far</var>, enum machine_mode <var>mode</var>, tree <var>type</var>, int *<var>pretend_args_size</var>, int <var>second_time</var>)</em></dt>
  261. <dd><p>Use it to store bounds for anonymous register arguments stored
  262. into the stack. Arguments meaning is similar to
  263. <code>TARGET_SETUP_INCOMING_VARARGS</code>.
  264. </p></dd></dl>
  265. <hr>
  266. <div class="header">
  267. <p>
  268. Next: <a href="Trampolines.html#Trampolines" accesskey="n" rel="next">Trampolines</a>, Previous: <a href="Stack-and-Calling.html#Stack-and-Calling" accesskey="p" rel="prev">Stack and Calling</a>, Up: <a href="Target-Macros.html#Target-Macros" accesskey="u" rel="up">Target Macros</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>
  269. </div>
  270. </body>
  271. </html>