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