[559513]: impnotes / bytecode-design.html Maximize Restore History

Download this file

bytecode-design.html    37 lines (36 with data), 8.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>36.6. Bytecode Design</title><link rel="stylesheet" href="impnotes.css" type="text/css" /><link rev="made" href="mailto:clisp-list@lists.sourceforge.net" /><meta name="generator" content="DocBook XSL Stylesheets V2005-10-07_10:16_snapshot" /><link rel="start" href="index.html" title="Implementation Notes for GNU CLISP" /><link rel="up" href="bytecode.html" title="Chapter 36. The CLISP bytecode specification" /><link rel="prev" href="intr-set.html" title="36.5. The instruction set" /><link rel="next" href="appendices.html" title="Part IV. Appendices" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">36.6. Bytecode Design</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="intr-set.html">Prev</a> </td><th width="60%" align="center">Chapter 36. The <span><strong class="command">CLISP</strong></span> bytecode specification</th><td width="20%" align="right"> <a accesskey="n" href="appendices.html">Next</a></td></tr></table><hr /></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="bytecode-design"></a>36.6. Bytecode Design</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="bytecode-design.html#byte-when-add-q">36.6.1. When to add a new bytecode?</a></span></dt><dt><span class="section"><a href="bytecode-design.html#byte-jmptail-q">36.6.2. Why JMPTAIL?</a></span></dt></dl></div><p>This section offers some insight into bytecode design in the form
of questions and answers.</p><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="byte-when-add-q"></a>36.6.1. When to add a new bytecode?</h3></div></div></div><div class="abstract"><p class="title"><b>Question:</b></p><p>Does it make sense to define a new bytecode instruction for
<a href="http://www.lisp.org/HyperSpec/Body/mac_restart-case.html" target="_top"><code class="function">RESTART-CASE</code></a>? Why? Why not?</p></div><div class="simplesect" lang="en" xml:lang="en"><div class="titlepage"><div><div><h4 class="title"><a id="byte-when-add-a"></a>Answer: Is it speed critical?</h4></div></div></div><p><a href="http://www.lisp.org/HyperSpec/Body/mac_restart-case.html" target="_top"><code class="function">RESTART-CASE</code></a> is a glorified <a href="http://www.lisp.org/HyperSpec/Body/speope_letcm_letst.html" target="_top"><code class="function">LET</code></a> binding
for <code class="varname">SYSTEM::*ACTIVE-RESTARTS*</code> and could well profit
from a separate bytecode: it would make it non-<a href="http://www.lisp.org/HyperSpec/Body/glo_c.html#cons" target="_top">consing[3]</a>.
(Remember that <a href="http://www.lisp.org/HyperSpec/Body/syscla_restart.html" target="_top"><code class="classname">RESTART</code></a>s have dynamic extent and therefore do not
really need to be heap allocated.)</p><p>The reason <a href="http://www.lisp.org/HyperSpec/Body/mac_handler-bind.html" target="_top"><code class="function">HANDLER-BIND</code></a> has its own bytecodes and
<a href="http://www.lisp.org/HyperSpec/Body/mac_restart-case.html" target="_top"><code class="function">RESTART-CASE</code></a> does not is that <a href="http://www.lisp.org/HyperSpec/Body/mac_handler-bind.html" target="_top"><code class="function">HANDLER-BIND</code></a> can occur in inner
computation loops, whereas <a href="http://www.lisp.org/HyperSpec/Body/mac_restart-case.html" target="_top"><code class="function">RESTART-CASE</code></a> occurs only as part of
user-interface programming and therefore not in inner loops where its
consing could hurt much.</p></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="byte-jmptail-q"></a>36.6.2. Why <a href="intr-set.html#instr-jump" title="36.5.5. Instructions for control flow, jumps">JMPTAIL</a>?</h3></div></div></div><div class="abstract"><p class="title"><b>Question:</b></p><p>Consider this function and its disassembly:</p><pre class="programlisting">
(defun foo (x y) (if (or (= x 0) (= y 0)) (+ x y) (foo y (1- x))))
(<a href="http://www.lisp.org/HyperSpec/Body/fun_disassemble.html" target="_top"><code class="function">DISASSEMBLE</code></a> 'foo)
8 (LOAD&amp;PUSH 1)
9 (LOAD&amp;DEC&amp;PUSH 3)
11 (JMPTAIL 2 5 L0)
</pre><p>
Why are the arguments pushed onto the <a href="vm.html" class="olink"><code class="literal">STACK</code></a>, just to be popped off of
it during the <span class="byte"><code class="literal">JMPTAIL</code></span>?
Why not a sequence of <span class="byte"><code class="literal">LOAD</code></span>,
<span class="byte"><code class="literal">STORE</code></span> and
<span class="byte"><code class="literal">SKIP</code></span> instructions
followed by a <span class="byte"><code class="literal">JMP</code></span>?
</p></div><div class="simplesect" lang="en" xml:lang="en"><div class="titlepage"><div><div><h4 class="title"><a id="byte-jmptail-a"></a>Answer: This is a shortcut for the most common use</h4></div></div></div><p>Using <span class="byte"><code class="literal">JMPTAIL</code></span> requires 3
instructions, <span class="byte"><code class="literal">JMP</code></span> requires more.
When <span class="byte"><code class="literal">JMPTAIL</code></span> needs to be called, we
usually have some stuff close to the top of the <a href="vm.html" class="olink"><code class="literal">STACK</code></a> which will
become the new arguments, and some junk between these new arguments
and the closure object. <span class="byte"><code class="literal">JMPTAIL</code></span>
removes the junk. <span class="byte"><code class="literal">JMPTAIL</code></span> is a
convenient shortcut which shortens the bytecode - because typically
one would really have to clean-up the <a href="vm.html" class="olink"><code class="literal">STACK</code></a> by hand or make the
calculations in <a class="clisp-cvs" href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/clisp/clisp/src/compiler.lisp"><code class="filename">src/compiler.lisp</code></a>
more complicated.</p></div></div></div><div class="bookinfo"><hr width="100%" /><table width="100%" summary="impnotes meta info"><th><td align="left">These notes document <a href="http://clisp.cons.org" target="_top"><span><strong class="command">CLISP</strong></span></a> version 2.37</td><td align="right">Last modified: 2006-01-02</td></th></table></div><div class="custom-footer"><hr width="100%" /><table width="100%"><tr><td align="left"><a href="http://clisp.cons.org"><img src="clisp.png" width="48" height="48" alt="[CLISP home]" /></a></td><td align="center"><a href="http://sourceforge.net/donate/index.php?group_id=1355"><img src="http://images.sourceforge.net/images/project-support.jpg" width="88" height="32" border="0" alt="[Support This Project]" /></a></td><td align="right"><a href="http://sourceforge.net"><img width="125" height="37" alt="[SourceForge]" src="http://sflogo.sourceforge.net/sflogo.php?group_id=1355&amp;type=2&amp;page=bytecode-design" /></a></td></tr></table></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="intr-set.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="bytecode.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="appendices.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">36.5. The instruction set </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Part IV. Appendices</td></tr></table></div></body></html>