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