📄 ch18_05.htm
字号:
<html><head><title>Code Generators (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Code Generators"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch18_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch18_01.htm">Chapter 18: Compiling</a></td><td align="right" valign="top" width="172"><a href="ch18_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">18.5. Code Generators</h2><p><a name="INDEX-3283"></a>The three current backends that convert Perl opcodes into some otherformat are all emphatically experimental. (Yes, we said this before,but we don't want you to forget.) Even when they happen to produceoutput that runs correctly, the resulting programs may take more diskspace, more memory, and more CPU time than than they would ordinarily.This is an area of ongoing research and development. Things willget better.</p><h3 class="sect2">18.5.1. The Bytecode Generator</h3><p><a name="INDEX-3284"></a><a name="INDEX-3285"></a>The <tt class="literal">B::Bytecode</tt> module writes the parse tree'sopcodes out in a platform-independent encoding. You can take a Perlscript compiled down to bytecodes and copy that to any other machinewith Perl installed on it.</p><p><a name="INDEX-3286"></a>The standard but currently experimental<em class="emphasis">perlcc</em>(1) command knows how to convertPerl source code into a byte-compiled Perl program. All you have todo is:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perlcc -b -o pbyscript srcscript</b></tt></pre></blockquote>And now you should be able to directly "execute" the resulting<em class="emphasis">pbyscript</em>. The start of that file looks somewhat like this:<blockquote><pre class="programlisting">#!/usr/bin/perluse ByteLoader 0.03;^C^@^E^A^C^@^@^@^A^F^@^C^@^@^@^B^F^@^C^@^@^@^C^F^@^C^@^@^@B^@^@^@^H9^A8M-^?M-^?M-^?M-^?7M-^?M-^?M-^?M-^?6^@^@^@^A6^@^G^D^D^@^@^@^KR^@^@^@^HS^@^@^@^HV^@M-2W<^FU^@^@^@^@X^Y@Z^@...</pre></blockquote><a name="INDEX-3287"></a><a name="INDEX-3288"></a>There you find a small script header followed by purely binary data.This may seem like deep magic, but its dweomer, er, dwimmer is at mosta minor one. The <tt class="literal">ByteLoader</tt> module uses atechnique called a <em class="emphasis">source filter</em> to alter thesource code before Perl gets a chance to see it. A source filter is akind of preprocessor that applies to everything below it in thecurrent file. Instead of being limited to simplistic transformationsthe way macro processors like <em class="emphasis">cpp</em>(1) and<em class="emphasis">m4</em>(1) are, here there are no constraints. Sourcefilters have been used to augment Perl's syntax, to compress or encryptsource code, even to write Perl programs in Latin. E perlibusunicode; cogito, ergo substr; carp dbm, et al. Er, caveat scriptor.</p><p>The <tt class="literal">ByteLoader</tt> module is a source filter that knowshow to disassemble the serialized opcodes produced by<tt class="literal">B::Bytecode</tt> to reconstruct the original parse tree.The reconstituted Perl code is spliced into the current parse treewithout using the compiler. When the interpreter hits those opcodes,it just executes them as though they'd been there waiting for it allalong.<a name="INDEX-3289"></a></p><h3 class="sect2">18.5.2. The C Code Generators</h3><a name="INDEX-3290"></a><a name="INDEX-3291"></a><p><a name="INDEX-3292"></a>The remaining code generators, <tt class="literal">B::C</tt> and <tt class="literal">B::CC</tt>, both produce Ccode instead of serialized Perl opcodes. The code they generate is farfrom readable, and if you try to read it you'll just go blind. It'snot something you can use to plug little translated Perl-to-C bitsinto a larger C program. For that, see <a href="ch21_01.htm">Chapter 21, "Internals and Externals"</a>.</p><p>The <tt class="literal">B::C</tt> module just writes out the C datastructures needed to recreate the entire Perl run-time environment.You get a dedicated interpreter with all the compiler-built datastructures pre-initialized. In some senses, the code generated is likewhat <tt class="literal">B::Bytecode</tt> produces. Both are a straighttranslation of the opcode trees that the compiler built, but where<tt class="literal">B::Bytecode</tt> lays them out in symbolic form to berecreated later and plugged into a running Perl interpreter,<tt class="literal">B::C</tt> lays those opcodes down in C. When youcompile this C code with your C compiler and link in the Perl library,the resulting program won't need a Perl interpreter installed on thetarget system. (It might need some shared libraries, though, if youdidn't link everything statically.) However, this program isn'treally any different than the regular Perl interpreter that runs yourscript. It's just precompiled into a standalone executable image.</p><p>The <tt class="literal">B::CC</tt> module, however, tries to do more than that. Thebeginning of the C source file it generates looks pretty much like what<tt class="literal">B::C</tt> produced,<a href="#FOOTNOTE-6">[6]</a> but eventually,any similarity ends. In the <tt class="literal">B::C</tt> code, you have a big opcode tablein C that's manipulated just as the interpreter would do on its own,whereas in the C code generated by <tt class="literal">B::CC</tt> is laid out in the ordercorresponding to the run-time flow of your program. It even has a Cfunction corresponding to each function in your program. Some amountof optimization based on variable types is done; a few benchmarks canrun twice as fast as in the standard interpreter. This is the mostambitious of the current code generators, the one that holds thegreatest promise for the future. By no coincidence, it is also theleast stable of the three.</p><blockquote class="footnote"><a name="FOOTNOTE-6"></a><p>[6]But then, so does everything once you'vegone blind. Didn't we warn you not to peek?</p></blockquote><p>Computer science students looking for graduate thesis projects needlook no further. There are plenty of diamonds in the rough waiting tobe polished off here.</p><a name="INDEX-3293"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch18_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch18_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">18.4. Compiler Backends</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">18.6. Code Development Tools</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -