Output-Statement.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: Output Statement</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Output Statement">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Output Statement">
  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="Machine-Desc.html#Machine-Desc" rel="up" title="Machine Desc">
  30. <link href="Predicates.html#Predicates" rel="next" title="Predicates">
  31. <link href="Output-Template.html#Output-Template" rel="prev" title="Output Template">
  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="Output-Statement"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Predicates.html#Predicates" accesskey="n" rel="next">Predicates</a>, Previous: <a href="Output-Template.html#Output-Template" accesskey="p" rel="prev">Output Template</a>, Up: <a href="Machine-Desc.html#Machine-Desc" accesskey="u" rel="up">Machine Desc</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="C-Statements-for-Assembler-Output"></a>
  69. <h3 class="section">16.6 C Statements for Assembler Output</h3>
  70. <a name="index-output-statements"></a>
  71. <a name="index-C-statements-for-assembler-output"></a>
  72. <a name="index-generating-assembler-output"></a>
  73. <p>Often a single fixed template string cannot produce correct and efficient
  74. assembler code for all the cases that are recognized by a single
  75. instruction pattern. For example, the opcodes may depend on the kinds of
  76. operands; or some unfortunate combinations of operands may require extra
  77. machine instructions.
  78. </p>
  79. <p>If the output control string starts with a &lsquo;<samp>@</samp>&rsquo;, then it is actually
  80. a series of templates, each on a separate line. (Blank lines and
  81. leading spaces and tabs are ignored.) The templates correspond to the
  82. pattern&rsquo;s constraint alternatives (see <a href="Multi_002dAlternative.html#Multi_002dAlternative">Multi-Alternative</a>). For example,
  83. if a target machine has a two-address add instruction &lsquo;<samp>addr</samp>&rsquo; to add
  84. into a register and another &lsquo;<samp>addm</samp>&rsquo; to add a register to memory, you
  85. might write this pattern:
  86. </p>
  87. <div class="smallexample">
  88. <pre class="smallexample">(define_insn &quot;addsi3&quot;
  89. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
  90. (plus:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0,0&quot;)
  91. (match_operand:SI 2 &quot;general_operand&quot; &quot;g,r&quot;)))]
  92. &quot;&quot;
  93. &quot;@
  94. addr %2,%0
  95. addm %2,%0&quot;)
  96. </pre></div>
  97. <a name="index-_002a-in-template"></a>
  98. <a name="index-asterisk-in-template"></a>
  99. <p>If the output control string starts with a &lsquo;<samp>*</samp>&rsquo;, then it is not an
  100. output template but rather a piece of C program that should compute a
  101. template. It should execute a <code>return</code> statement to return the
  102. template-string you want. Most such templates use C string literals, which
  103. require doublequote characters to delimit them. To include these
  104. doublequote characters in the string, prefix each one with &lsquo;<samp>\</samp>&rsquo;.
  105. </p>
  106. <p>If the output control string is written as a brace block instead of a
  107. double-quoted string, it is automatically assumed to be C code. In that
  108. case, it is not necessary to put in a leading asterisk, or to escape the
  109. doublequotes surrounding C string literals.
  110. </p>
  111. <p>The operands may be found in the array <code>operands</code>, whose C data type
  112. is <code>rtx []</code>.
  113. </p>
  114. <p>It is very common to select different ways of generating assembler code
  115. based on whether an immediate operand is within a certain range. Be
  116. careful when doing this, because the result of <code>INTVAL</code> is an
  117. integer on the host machine. If the host machine has more bits in an
  118. <code>int</code> than the target machine has in the mode in which the constant
  119. will be used, then some of the bits you get from <code>INTVAL</code> will be
  120. superfluous. For proper results, you must carefully disregard the
  121. values of those bits.
  122. </p>
  123. <a name="index-output_005fasm_005finsn"></a>
  124. <p>It is possible to output an assembler instruction and then go on to output
  125. or compute more of them, using the subroutine <code>output_asm_insn</code>. This
  126. receives two arguments: a template-string and a vector of operands. The
  127. vector may be <code>operands</code>, or it may be another array of <code>rtx</code>
  128. that you declare locally and initialize yourself.
  129. </p>
  130. <a name="index-which_005falternative"></a>
  131. <p>When an insn pattern has multiple alternatives in its constraints, often
  132. the appearance of the assembler code is determined mostly by which alternative
  133. was matched. When this is so, the C code can test the variable
  134. <code>which_alternative</code>, which is the ordinal number of the alternative
  135. that was actually satisfied (0 for the first, 1 for the second alternative,
  136. etc.).
  137. </p>
  138. <p>For example, suppose there are two opcodes for storing zero, &lsquo;<samp>clrreg</samp>&rsquo;
  139. for registers and &lsquo;<samp>clrmem</samp>&rsquo; for memory locations. Here is how
  140. a pattern could use <code>which_alternative</code> to choose between them:
  141. </p>
  142. <div class="smallexample">
  143. <pre class="smallexample">(define_insn &quot;&quot;
  144. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
  145. (const_int 0))]
  146. &quot;&quot;
  147. {
  148. return (which_alternative == 0
  149. ? &quot;clrreg %0&quot; : &quot;clrmem %0&quot;);
  150. })
  151. </pre></div>
  152. <p>The example above, where the assembler code to generate was
  153. <em>solely</em> determined by the alternative, could also have been specified
  154. as follows, having the output control string start with a &lsquo;<samp>@</samp>&rsquo;:
  155. </p>
  156. <div class="smallexample">
  157. <pre class="smallexample">(define_insn &quot;&quot;
  158. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,m&quot;)
  159. (const_int 0))]
  160. &quot;&quot;
  161. &quot;@
  162. clrreg %0
  163. clrmem %0&quot;)
  164. </pre></div>
  165. <p>If you just need a little bit of C code in one (or a few) alternatives,
  166. you can use &lsquo;<samp>*</samp>&rsquo; inside of a &lsquo;<samp>@</samp>&rsquo; multi-alternative template:
  167. </p>
  168. <div class="smallexample">
  169. <pre class="smallexample">(define_insn &quot;&quot;
  170. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=r,&lt;,m&quot;)
  171. (const_int 0))]
  172. &quot;&quot;
  173. &quot;@
  174. clrreg %0
  175. * return stack_mem_p (operands[0]) ? \&quot;push 0\&quot; : \&quot;clrmem %0\&quot;;
  176. clrmem %0&quot;)
  177. </pre></div>
  178. <hr>
  179. <div class="header">
  180. <p>
  181. Next: <a href="Predicates.html#Predicates" accesskey="n" rel="next">Predicates</a>, Previous: <a href="Output-Template.html#Output-Template" accesskey="p" rel="prev">Output Template</a>, Up: <a href="Machine-Desc.html#Machine-Desc" accesskey="u" rel="up">Machine Desc</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>
  182. </div>
  183. </body>
  184. </html>