General-Bytecode-Design.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2020 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 "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: General Bytecode Design</title>
  16. <meta name="description" content="Debugging with GDB: General Bytecode Design">
  17. <meta name="keywords" content="Debugging with GDB: General Bytecode Design">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Agent-Expressions.html#Agent-Expressions" rel="up" title="Agent Expressions">
  26. <link href="Bytecode-Descriptions.html#Bytecode-Descriptions" rel="next" title="Bytecode Descriptions">
  27. <link href="Agent-Expressions.html#Agent-Expressions" rel="previous" title="Agent Expressions">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="General-Bytecode-Design"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Bytecode-Descriptions.html#Bytecode-Descriptions" accesskey="n" rel="next">Bytecode Descriptions</a>, Up: <a href="Agent-Expressions.html#Agent-Expressions" accesskey="u" rel="up">Agent Expressions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="General-Bytecode-Design-1"></a>
  65. <h3 class="section">F.1 General Bytecode Design</h3>
  66. <p>The agent represents bytecode expressions as an array of bytes. Each
  67. instruction is one byte long (thus the term <em>bytecode</em>). Some
  68. instructions are followed by operand bytes; for example, the <code>goto</code>
  69. instruction is followed by a destination for the jump.
  70. </p>
  71. <p>The bytecode interpreter is a stack-based machine; most instructions pop
  72. their operands off the stack, perform some operation, and push the
  73. result back on the stack for the next instruction to consume. Each
  74. element of the stack may contain either a integer or a floating point
  75. value; these values are as many bits wide as the largest integer that
  76. can be directly manipulated in the source language. Stack elements
  77. carry no record of their type; bytecode could push a value as an
  78. integer, then pop it as a floating point value. However, GDB will not
  79. generate code which does this. In C, one might define the type of a
  80. stack element as follows:
  81. </p><div class="example">
  82. <pre class="example">union agent_val {
  83. LONGEST l;
  84. DOUBLEST d;
  85. };
  86. </pre></div>
  87. <p>where <code>LONGEST</code> and <code>DOUBLEST</code> are <code>typedef</code> names for
  88. the largest integer and floating point types on the machine.
  89. </p>
  90. <p>By the time the bytecode interpreter reaches the end of the expression,
  91. the value of the expression should be the only value left on the stack.
  92. For tracing applications, <code>trace</code> bytecodes in the expression will
  93. have recorded the necessary data, and the value on the stack may be
  94. discarded. For other applications, like conditional breakpoints, the
  95. value may be useful.
  96. </p>
  97. <p>Separate from the stack, the interpreter has two registers:
  98. </p><dl compact="compact">
  99. <dt><code>pc</code></dt>
  100. <dd><p>The address of the next bytecode to execute.
  101. </p>
  102. </dd>
  103. <dt><code>start</code></dt>
  104. <dd><p>The address of the start of the bytecode expression, necessary for
  105. interpreting the <code>goto</code> and <code>if_goto</code> instructions.
  106. </p>
  107. </dd>
  108. </dl>
  109. <p>Neither of these registers is directly visible to the bytecode language
  110. itself, but they are useful for defining the meanings of the bytecode
  111. operations.
  112. </p>
  113. <p>There are no instructions to perform side effects on the running
  114. program, or call the program&rsquo;s functions; we assume that these
  115. expressions are only used for unobtrusive debugging, not for patching
  116. the running code.
  117. </p>
  118. <p>Most bytecode instructions do not distinguish between the various sizes
  119. of values, and operate on full-width values; the upper bits of the
  120. values are simply ignored, since they do not usually make a difference
  121. to the value computed. The exceptions to this rule are:
  122. </p><dl compact="compact">
  123. <dt>memory reference instructions (<code>ref</code><var>n</var>)</dt>
  124. <dd><p>There are distinct instructions to fetch different word sizes from
  125. memory. Once on the stack, however, the values are treated as full-size
  126. integers. They may need to be sign-extended; the <code>ext</code> instruction
  127. exists for this purpose.
  128. </p>
  129. </dd>
  130. <dt>the sign-extension instruction (<code>ext</code> <var>n</var>)</dt>
  131. <dd><p>These clearly need to know which portion of their operand is to be
  132. extended to occupy the full length of the word.
  133. </p>
  134. </dd>
  135. </dl>
  136. <p>If the interpreter is unable to evaluate an expression completely for
  137. some reason (a memory location is inaccessible, or a divisor is zero,
  138. for example), we say that interpretation &ldquo;terminates with an error&rdquo;.
  139. This means that the problem is reported back to the interpreter&rsquo;s caller
  140. in some helpful way. In general, code using agent expressions should
  141. assume that they may attempt to divide by zero, fetch arbitrary memory
  142. locations, and misbehave in other ways.
  143. </p>
  144. <p>Even complicated C expressions compile to a few bytecode instructions;
  145. for example, the expression <code>x + y * z</code> would typically produce
  146. code like the following, assuming that <code>x</code> and <code>y</code> live in
  147. registers, and <code>z</code> is a global variable holding a 32-bit
  148. <code>int</code>:
  149. </p><div class="example">
  150. <pre class="example">reg 1
  151. reg 2
  152. const32 <i>address of z</i>
  153. ref32
  154. ext 32
  155. mul
  156. add
  157. end
  158. </pre></div>
  159. <p>In detail, these mean:
  160. </p><dl compact="compact">
  161. <dt><code>reg 1</code></dt>
  162. <dd><p>Push the value of register 1 (presumably holding <code>x</code>) onto the
  163. stack.
  164. </p>
  165. </dd>
  166. <dt><code>reg 2</code></dt>
  167. <dd><p>Push the value of register 2 (holding <code>y</code>).
  168. </p>
  169. </dd>
  170. <dt><code>const32 <i>address of z</i></code></dt>
  171. <dd><p>Push the address of <code>z</code> onto the stack.
  172. </p>
  173. </dd>
  174. <dt><code>ref32</code></dt>
  175. <dd><p>Fetch a 32-bit word from the address at the top of the stack; replace
  176. the address on the stack with the value. Thus, we replace the address
  177. of <code>z</code> with <code>z</code>&rsquo;s value.
  178. </p>
  179. </dd>
  180. <dt><code>ext 32</code></dt>
  181. <dd><p>Sign-extend the value on the top of the stack from 32 bits to full
  182. length. This is necessary because <code>z</code> is a signed integer.
  183. </p>
  184. </dd>
  185. <dt><code>mul</code></dt>
  186. <dd><p>Pop the top two numbers on the stack, multiply them, and push their
  187. product. Now the top of the stack contains the value of the expression
  188. <code>y * z</code>.
  189. </p>
  190. </dd>
  191. <dt><code>add</code></dt>
  192. <dd><p>Pop the top two numbers, add them, and push the sum. Now the top of the
  193. stack contains the value of <code>x + y * z</code>.
  194. </p>
  195. </dd>
  196. <dt><code>end</code></dt>
  197. <dd><p>Stop executing; the value left on the stack top is the value to be
  198. recorded.
  199. </p>
  200. </dd>
  201. </dl>
  202. <hr>
  203. <div class="header">
  204. <p>
  205. Next: <a href="Bytecode-Descriptions.html#Bytecode-Descriptions" accesskey="n" rel="next">Bytecode Descriptions</a>, Up: <a href="Agent-Expressions.html#Agent-Expressions" accesskey="u" rel="up">Agent Expressions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  206. </div>
  207. </body>
  208. </html>