Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
15 KiB

  1. =head1 NAME
  2. perlcompile - Introduction to the Perl Compiler-Translator
  3. =head1 DESCRIPTION
  4. Perl has always had a compiler: your source is compiled into an
  5. internal form (a parse tree) which is then optimized before being
  6. run. Since version 5.005, Perl has shipped with a module
  7. capable of inspecting the optimized parse tree (C<B>), and this has
  8. been used to write many useful utilities, including a module that lets
  9. you turn your Perl into C source code that can be compiled into an
  10. native executable.
  11. The C<B> module provides access to the parse tree, and other modules
  12. ("back ends") do things with the tree. Some write it out as
  13. bytecode, C source code, or a semi-human-readable text. Another
  14. traverses the parse tree to build a cross-reference of which
  15. subroutines, formats, and variables are used where. Another checks
  16. your code for dubious constructs. Yet another back end dumps the
  17. parse tree back out as Perl source, acting as a source code beautifier
  18. or deobfuscator.
  19. Because its original purpose was to be a way to produce C code
  20. corresponding to a Perl program, and in turn a native executable, the
  21. C<B> module and its associated back ends are known as "the
  22. compiler", even though they don't really compile anything.
  23. Different parts of the compiler are more accurately a "translator",
  24. or an "inspector", but people want Perl to have a "compiler
  25. option" not an "inspector gadget". What can you do?
  26. This document covers the use of the Perl compiler: which modules
  27. it comprises, how to use the most important of the back end modules,
  28. what problems there are, and how to work around them.
  29. =head2 Layout
  30. The compiler back ends are in the C<B::> hierarchy, and the front-end
  31. (the module that you, the user of the compiler, will sometimes
  32. interact with) is the O module. Some back ends (e.g., C<B::C>) have
  33. programs (e.g., I<perlcc>) to hide the modules' complexity.
  34. Here are the important back ends to know about, with their status
  35. expressed as a number from 0 (outline for later implementation) to
  36. 10 (if there's a bug in it, we're very surprised):
  37. =over 4
  38. =item B::Bytecode
  39. Stores the parse tree in a machine-independent format, suitable
  40. for later reloading through the ByteLoader module. Status: 5 (some
  41. things work, some things don't, some things are untested).
  42. =item B::C
  43. Creates a C source file containing code to rebuild the parse tree
  44. and resume the interpreter. Status: 6 (many things work adequately,
  45. including programs using Tk).
  46. =item B::CC
  47. Creates a C source file corresponding to the run time code path in
  48. the parse tree. This is the closest to a Perl-to-C translator there
  49. is, but the code it generates is almost incomprehensible because it
  50. translates the parse tree into a giant switch structure that
  51. manipulates Perl structures. Eventual goal is to reduce (given
  52. sufficient type information in the Perl program) some of the
  53. Perl data structure manipulations into manipulations of C-level
  54. ints, floats, etc. Status: 5 (some things work, including
  55. uncomplicated Tk examples).
  56. =item B::Lint
  57. Complains if it finds dubious constructs in your source code. Status:
  58. 6 (it works adequately, but only has a very limited number of areas
  59. that it checks).
  60. =item B::Deparse
  61. Recreates the Perl source, making an attempt to format it coherently.
  62. Status: 8 (it works nicely, but a few obscure things are missing).
  63. =item B::Xref
  64. Reports on the declaration and use of subroutines and variables.
  65. Status: 8 (it works nicely, but still has a few lingering bugs).
  66. =back
  67. =head1 Using The Back Ends
  68. The following sections describe how to use the various compiler back
  69. ends. They're presented roughly in order of maturity, so that the
  70. most stable and proven back ends are described first, and the most
  71. experimental and incomplete back ends are described last.
  72. The O module automatically enabled the B<-c> flag to Perl, which
  73. prevents Perl from executing your code once it has been compiled.
  74. This is why all the back ends print:
  75. myperlprogram syntax OK
  76. before producing any other output.
  77. =head2 The Cross Referencing Back End
  78. The cross referencing back end (B::Xref) produces a report on your program,
  79. breaking down declarations and uses of subroutines and variables (and
  80. formats) by file and subroutine. For instance, here's part of the
  81. report from the I<pod2man> program that comes with Perl:
  82. Subroutine clear_noremap
  83. Package (lexical)
  84. $ready_to_print i1069, 1079
  85. Package main
  86. $& 1086
  87. $. 1086
  88. $0 1086
  89. $1 1087
  90. $2 1085, 1085
  91. $3 1085, 1085
  92. $ARGV 1086
  93. %HTML_Escapes 1085, 1085
  94. This shows the variables used in the subroutine C<clear_noremap>. The
  95. variable C<$ready_to_print> is a my() (lexical) variable,
  96. B<i>ntroduced (first declared with my()) on line 1069, and used on
  97. line 1079. The variable C<$&> from the main package is used on 1086,
  98. and so on.
  99. A line number may be prefixed by a single letter:
  100. =over 4
  101. =item i
  102. Lexical variable introduced (declared with my()) for the first time.
  103. =item &
  104. Subroutine or method call.
  105. =item s
  106. Subroutine defined.
  107. =item r
  108. Format defined.
  109. =back
  110. The most useful option the cross referencer has is to save the report
  111. to a separate file. For instance, to save the report on
  112. I<myperlprogram> to the file I<report>:
  113. $ perl -MO=Xref,-oreport myperlprogram
  114. =head2 The Decompiling Back End
  115. The Deparse back end turns your Perl source back into Perl source. It
  116. can reformat along the way, making it useful as a de-obfuscator. The
  117. most basic way to use it is:
  118. $ perl -MO=Deparse myperlprogram
  119. You'll notice immediately that Perl has no idea of how to paragraph
  120. your code. You'll have to separate chunks of code from each other
  121. with newlines by hand. However, watch what it will do with
  122. one-liners:
  123. $ perl -MO=Deparse -e '$op=shift||die "usage: $0
  124. code [...]";chomp(@ARGV=<>)unless@ARGV; for(@ARGV){$was=$_;eval$op;
  125. die$@ if$@; rename$was,$_ unless$was eq $_}'
  126. -e syntax OK
  127. $op = shift @ARGV || die("usage: $0 code [...]");
  128. chomp(@ARGV = <ARGV>) unless @ARGV;
  129. foreach $_ (@ARGV) {
  130. $was = $_;
  131. eval $op;
  132. die $@ if $@;
  133. rename $was, $_ unless $was eq $_;
  134. }
  135. The decompiler has several options for the code it generates. For
  136. instance, you can set the size of each indent from 4 (as above) to
  137. 2 with:
  138. $ perl -MO=Deparse,-si2 myperlprogram
  139. The B<-p> option adds parentheses where normally they are omitted:
  140. $ perl -MO=Deparse -e 'print "Hello, world\n"'
  141. -e syntax OK
  142. print "Hello, world\n";
  143. $ perl -MO=Deparse,-p -e 'print "Hello, world\n"'
  144. -e syntax OK
  145. print("Hello, world\n");
  146. See L<B::Deparse> for more information on the formatting options.
  147. =head2 The Lint Back End
  148. The lint back end (B::Lint) inspects programs for poor style. One
  149. programmer's bad style is another programmer's useful tool, so options
  150. let you select what is complained about.
  151. To run the style checker across your source code:
  152. $ perl -MO=Lint myperlprogram
  153. To disable context checks and undefined subroutines:
  154. $ perl -MO=Lint,-context,-undefined-subs myperlprogram
  155. See L<B::Lint> for information on the options.
  156. =head2 The Simple C Back End
  157. This module saves the internal compiled state of your Perl program
  158. to a C source file, which can be turned into a native executable
  159. for that particular platform using a C compiler. The resulting
  160. program links against the Perl interpreter library, so it
  161. will not save you disk space (unless you build Perl with a shared
  162. library) or program size. It may, however, save you startup time.
  163. The C<perlcc> tool generates such executables by default.
  164. perlcc myperlprogram.pl
  165. =head2 The Bytecode Back End
  166. This back end is only useful if you also have a way to load and
  167. execute the bytecode that it produces. The ByteLoader module provides
  168. this functionality.
  169. To turn a Perl program into executable byte code, you can use C<perlcc>
  170. with the C<-b> switch:
  171. perlcc -b myperlprogram.pl
  172. The byte code is machine independent, so once you have a compiled
  173. module or program, it is as portable as Perl source (assuming that
  174. the user of the module or program has a modern-enough Perl interpreter
  175. to decode the byte code).
  176. See B<B::Bytecode> for information on options to control the
  177. optimization and nature of the code generated by the Bytecode module.
  178. =head2 The Optimized C Back End
  179. The optimized C back end will turn your Perl program's run time
  180. code-path into an equivalent (but optimized) C program that manipulates
  181. the Perl data structures directly. The program will still link against
  182. the Perl interpreter library, to allow for eval(), C<s///e>,
  183. C<require>, etc.
  184. The C<perlcc> tool generates such executables when using the -opt
  185. switch. To compile a Perl program (ending in C<.pl>
  186. or C<.p>):
  187. perlcc -opt myperlprogram.pl
  188. To produce a shared library from a Perl module (ending in C<.pm>):
  189. perlcc -opt Myperlmodule.pm
  190. For more information, see L<perlcc> and L<B::CC>.
  191. =over 4
  192. =item B
  193. This module is the introspective ("reflective" in Java terms)
  194. module, which allows a Perl program to inspect its innards. The
  195. back end modules all use this module to gain access to the compiled
  196. parse tree. You, the user of a back end module, will not need to
  197. interact with B.
  198. =item O
  199. This module is the front-end to the compiler's back ends. Normally
  200. called something like this:
  201. $ perl -MO=Deparse myperlprogram
  202. This is like saying C<use O 'Deparse'> in your Perl program.
  203. =item B::Asmdata
  204. This module is used by the B::Assembler module, which is in turn used
  205. by the B::Bytecode module, which stores a parse-tree as
  206. bytecode for later loading. It's not a back end itself, but rather a
  207. component of a back end.
  208. =item B::Assembler
  209. This module turns a parse-tree into data suitable for storing
  210. and later decoding back into a parse-tree. It's not a back end
  211. itself, but rather a component of a back end. It's used by the
  212. I<assemble> program that produces bytecode.
  213. =item B::Bblock
  214. This module is used by the B::CC back end. It walks "basic blocks".
  215. A basic block is a series of operations which is known to execute from
  216. start to finish, with no possibility of branching or halting.
  217. =item B::Bytecode
  218. This module is a back end that generates bytecode from a
  219. program's parse tree. This bytecode is written to a file, from where
  220. it can later be reconstructed back into a parse tree. The goal is to
  221. do the expensive program compilation once, save the interpreter's
  222. state into a file, and then restore the state from the file when the
  223. program is to be executed. See L</"The Bytecode Back End">
  224. for details about usage.
  225. =item B::C
  226. This module writes out C code corresponding to the parse tree and
  227. other interpreter internal structures. You compile the corresponding
  228. C file, and get an executable file that will restore the internal
  229. structures and the Perl interpreter will begin running the
  230. program. See L</"The Simple C Back End"> for details about usage.
  231. =item B::CC
  232. This module writes out C code corresponding to your program's
  233. operations. Unlike the B::C module, which merely stores the
  234. interpreter and its state in a C program, the B::CC module makes a
  235. C program that does not involve the interpreter. As a consequence,
  236. programs translated into C by B::CC can execute faster than normal
  237. interpreted programs. See L</"The Optimized C Back End"> for
  238. details about usage.
  239. =item B::Debug
  240. This module dumps the Perl parse tree in verbose detail to STDOUT.
  241. It's useful for people who are writing their own back end, or who
  242. are learning about the Perl internals. It's not useful to the
  243. average programmer.
  244. =item B::Deparse
  245. This module produces Perl source code from the compiled parse tree.
  246. It is useful in debugging and deconstructing other people's code,
  247. also as a pretty-printer for your own source. See
  248. L</"The Decompiling Back End"> for details about usage.
  249. =item B::Disassembler
  250. This module turns bytecode back into a parse tree. It's not a back
  251. end itself, but rather a component of a back end. It's used by the
  252. I<disassemble> program that comes with the bytecode.
  253. =item B::Lint
  254. This module inspects the compiled form of your source code for things
  255. which, while some people frown on them, aren't necessarily bad enough
  256. to justify a warning. For instance, use of an array in scalar context
  257. without explicitly saying C<scalar(@array)> is something that Lint
  258. can identify. See L</"The Lint Back End"> for details about usage.
  259. =item B::Showlex
  260. This module prints out the my() variables used in a function or a
  261. file. To get a list of the my() variables used in the subroutine
  262. mysub() defined in the file myperlprogram:
  263. $ perl -MO=Showlex,mysub myperlprogram
  264. To get a list of the my() variables used in the file myperlprogram:
  265. $ perl -MO=Showlex myperlprogram
  266. [BROKEN]
  267. =item B::Stackobj
  268. This module is used by the B::CC module. It's not a back end itself,
  269. but rather a component of a back end.
  270. =item B::Stash
  271. This module is used by the L<perlcc> program, which compiles a module
  272. into an executable. B::Stash prints the symbol tables in use by a
  273. program, and is used to prevent B::CC from producing C code for the
  274. B::* and O modules. It's not a back end itself, but rather a
  275. component of a back end.
  276. =item B::Terse
  277. This module prints the contents of the parse tree, but without as much
  278. information as B::Debug. For comparison, C<print "Hello, world.">
  279. produced 96 lines of output from B::Debug, but only 6 from B::Terse.
  280. This module is useful for people who are writing their own back end,
  281. or who are learning about the Perl internals. It's not useful to the
  282. average programmer.
  283. =item B::Xref
  284. This module prints a report on where the variables, subroutines, and
  285. formats are defined and used within a program and the modules it
  286. loads. See L</"The Cross Referencing Back End"> for details about
  287. usage.
  288. =back
  289. =head1 KNOWN PROBLEMS
  290. The simple C backend currently only saves typeglobs with alphanumeric
  291. names.
  292. The optimized C backend outputs code for more modules than it should
  293. (e.g., DirHandle). It also has little hope of properly handling
  294. C<goto LABEL> outside the running subroutine (C<goto &sub> is okay).
  295. C<goto LABEL> currently does not work at all in this backend.
  296. It also creates a huge initialization function that gives
  297. C compilers headaches. Splitting the initialization function gives
  298. better results. Other problems include: unsigned math does not
  299. work correctly; some opcodes are handled incorrectly by default
  300. opcode handling mechanism.
  301. BEGIN{} blocks are executed while compiling your code. Any external
  302. state that is initialized in BEGIN{}, such as opening files, initiating
  303. database connections etc., do not behave properly. To work around
  304. this, Perl has an INIT{} block that corresponds to code being executed
  305. before your program begins running but after your program has finished
  306. being compiled. Execution order: BEGIN{}, (possible save of state
  307. through compiler back-end), INIT{}, program runs, END{}.
  308. =head1 AUTHOR
  309. This document was originally written by Nathan Torkington, and is now
  310. maintained by the perl5-porters mailing list
  311. I<[email protected]>.
  312. =cut