Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / man / man1 / perlcompile.1
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLCOMPILE 1"
132.TH PERLCOMPILE 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134perlcompile \- Introduction to the Perl Compiler\-Translator
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137Perl has always had a compiler: your source is compiled into an
138internal form (a parse tree) which is then optimized before being
139run. Since version 5.005, Perl has shipped with a module
140capable of inspecting the optimized parse tree (\f(CW\*(C`B\*(C'\fR), and this has
141been used to write many useful utilities, including a module that lets
142you turn your Perl into C source code that can be compiled into a
143native executable.
144.PP
145The \f(CW\*(C`B\*(C'\fR module provides access to the parse tree, and other modules
146(\*(L"back ends\*(R") do things with the tree. Some write it out as
147bytecode, C source code, or a semi-human-readable text. Another
148traverses the parse tree to build a cross-reference of which
149subroutines, formats, and variables are used where. Another checks
150your code for dubious constructs. Yet another back end dumps the
151parse tree back out as Perl source, acting as a source code beautifier
152or deobfuscator.
153.PP
154Because its original purpose was to be a way to produce C code
155corresponding to a Perl program, and in turn a native executable, the
156\&\f(CW\*(C`B\*(C'\fR module and its associated back ends are known as \*(L"the
157compiler\*(R", even though they don't really compile anything.
158Different parts of the compiler are more accurately a \*(L"translator\*(R",
159or an \*(L"inspector\*(R", but people want Perl to have a \*(L"compiler
160option\*(R" not an \*(L"inspector gadget\*(R". What can you do?
161.PP
162This document covers the use of the Perl compiler: which modules
163it comprises, how to use the most important of the back end modules,
164what problems there are, and how to work around them.
165.Sh "Layout"
166.IX Subsection "Layout"
167The compiler back ends are in the \f(CW\*(C`B::\*(C'\fR hierarchy, and the front-end
168(the module that you, the user of the compiler, will sometimes
169interact with) is the O module. Some back ends (e.g., \f(CW\*(C`B::C\*(C'\fR) have
170programs (e.g., \fIperlcc\fR) to hide the modules' complexity.
171.PP
172Here are the important back ends to know about, with their status
173expressed as a number from 0 (outline for later implementation) to
17410 (if there's a bug in it, we're very surprised):
175.IP "B::Bytecode" 4
176.IX Item "B::Bytecode"
177Stores the parse tree in a machine-independent format, suitable
178for later reloading through the ByteLoader module. Status: 5 (some
179things work, some things don't, some things are untested).
180.IP "B::C" 4
181.IX Item "B::C"
182Creates a C source file containing code to rebuild the parse tree
183and resume the interpreter. Status: 6 (many things work adequately,
184including programs using Tk).
185.IP "B::CC" 4
186.IX Item "B::CC"
187Creates a C source file corresponding to the run time code path in
188the parse tree. This is the closest to a Perl-to-C translator there
189is, but the code it generates is almost incomprehensible because it
190translates the parse tree into a giant switch structure that
191manipulates Perl structures. Eventual goal is to reduce (given
192sufficient type information in the Perl program) some of the
193Perl data structure manipulations into manipulations of C\-level
194ints, floats, etc. Status: 5 (some things work, including
195uncomplicated Tk examples).
196.IP "B::Lint" 4
197.IX Item "B::Lint"
198Complains if it finds dubious constructs in your source code. Status:
1996 (it works adequately, but only has a very limited number of areas
200that it checks).
201.IP "B::Deparse" 4
202.IX Item "B::Deparse"
203Recreates the Perl source, making an attempt to format it coherently.
204Status: 8 (it works nicely, but a few obscure things are missing).
205.IP "B::Xref" 4
206.IX Item "B::Xref"
207Reports on the declaration and use of subroutines and variables.
208Status: 8 (it works nicely, but still has a few lingering bugs).
209.SH "Using The Back Ends"
210.IX Header "Using The Back Ends"
211The following sections describe how to use the various compiler back
212ends. They're presented roughly in order of maturity, so that the
213most stable and proven back ends are described first, and the most
214experimental and incomplete back ends are described last.
215.PP
216The O module automatically enabled the \fB\-c\fR flag to Perl, which
217prevents Perl from executing your code once it has been compiled.
218This is why all the back ends print:
219.PP
220.Vb 1
221\& myperlprogram syntax OK
222.Ve
223.PP
224before producing any other output.
225.Sh "The Cross Referencing Back End"
226.IX Subsection "The Cross Referencing Back End"
227The cross referencing back end (B::Xref) produces a report on your program,
228breaking down declarations and uses of subroutines and variables (and
229formats) by file and subroutine. For instance, here's part of the
230report from the \fIpod2man\fR program that comes with Perl:
231.PP
232.Vb 12
233\& Subroutine clear_noremap
234\& Package (lexical)
235\& $ready_to_print i1069, 1079
236\& Package main
237\& $& 1086
238\& $. 1086
239\& $0 1086
240\& $1 1087
241\& $2 1085, 1085
242\& $3 1085, 1085
243\& $ARGV 1086
244\& %HTML_Escapes 1085, 1085
245.Ve
246.PP
247This shows the variables used in the subroutine \f(CW\*(C`clear_noremap\*(C'\fR. The
248variable \f(CW$ready_to_print\fR is a \fImy()\fR (lexical) variable,
249\&\fBi\fRntroduced (first declared with \fImy()\fR) on line 1069, and used on
250line 1079. The variable \f(CW$&\fR from the main package is used on 1086,
251and so on.
252.PP
253A line number may be prefixed by a single letter:
254.IP "i" 4
255.IX Item "i"
256Lexical variable introduced (declared with \fImy()\fR) for the first time.
257.IP "&" 4
258Subroutine or method call.
259.IP "s" 4
260.IX Item "s"
261Subroutine defined.
262.IP "r" 4
263.IX Item "r"
264Format defined.
265.PP
266The most useful option the cross referencer has is to save the report
267to a separate file. For instance, to save the report on
268\&\fImyperlprogram\fR to the file \fIreport\fR:
269.PP
270.Vb 1
271\& $ perl -MO=Xref,-oreport myperlprogram
272.Ve
273.Sh "The Decompiling Back End"
274.IX Subsection "The Decompiling Back End"
275The Deparse back end turns your Perl source back into Perl source. It
276can reformat along the way, making it useful as a de\-obfuscator. The
277most basic way to use it is:
278.PP
279.Vb 1
280\& $ perl -MO=Deparse myperlprogram
281.Ve
282.PP
283You'll notice immediately that Perl has no idea of how to paragraph
284your code. You'll have to separate chunks of code from each other
285with newlines by hand. However, watch what it will do with
286one\-liners:
287.PP
288.Vb 12
289\& $ perl -MO=Deparse -e '$op=shift||die "usage: $0
290\& code [...]";chomp(@ARGV=<>)unless@ARGV; for(@ARGV){$was=$_;eval$op;
291\& die$@ if$@; rename$was,$_ unless$was eq $_}'
292\& -e syntax OK
293\& $op = shift @ARGV || die("usage: $0 code [...]");
294\& chomp(@ARGV = <ARGV>) unless @ARGV;
295\& foreach $_ (@ARGV) {
296\& $was = $_;
297\& eval $op;
298\& die $@ if $@;
299\& rename $was, $_ unless $was eq $_;
300\& }
301.Ve
302.PP
303The decompiler has several options for the code it generates. For
304instance, you can set the size of each indent from 4 (as above) to
3052 with:
306.PP
307.Vb 1
308\& $ perl -MO=Deparse,-si2 myperlprogram
309.Ve
310.PP
311The \fB\-p\fR option adds parentheses where normally they are omitted:
312.PP
313.Vb 6
314\& $ perl -MO=Deparse -e 'print "Hello, world\en"'
315\& -e syntax OK
316\& print "Hello, world\en";
317\& $ perl -MO=Deparse,-p -e 'print "Hello, world\en"'
318\& -e syntax OK
319\& print("Hello, world\en");
320.Ve
321.PP
322See B::Deparse for more information on the formatting options.
323.Sh "The Lint Back End"
324.IX Subsection "The Lint Back End"
325The lint back end (B::Lint) inspects programs for poor style. One
326programmer's bad style is another programmer's useful tool, so options
327let you select what is complained about.
328.PP
329To run the style checker across your source code:
330.PP
331.Vb 1
332\& $ perl -MO=Lint myperlprogram
333.Ve
334.PP
335To disable context checks and undefined subroutines:
336.PP
337.Vb 1
338\& $ perl -MO=Lint,-context,-undefined-subs myperlprogram
339.Ve
340.PP
341See B::Lint for information on the options.
342.Sh "The Simple C Back End"
343.IX Subsection "The Simple C Back End"
344This module saves the internal compiled state of your Perl program
345to a C source file, which can be turned into a native executable
346for that particular platform using a C compiler. The resulting
347program links against the Perl interpreter library, so it
348will not save you disk space (unless you build Perl with a shared
349library) or program size. It may, however, save you startup time.
350.PP
351The \f(CW\*(C`perlcc\*(C'\fR tool generates such executables by default.
352.PP
353.Vb 1
354\& perlcc myperlprogram.pl
355.Ve
356.Sh "The Bytecode Back End"
357.IX Subsection "The Bytecode Back End"
358This back end is only useful if you also have a way to load and
359execute the bytecode that it produces. The ByteLoader module provides
360this functionality.
361.PP
362To turn a Perl program into executable byte code, you can use \f(CW\*(C`perlcc\*(C'\fR
363with the \f(CW\*(C`\-B\*(C'\fR switch:
364.PP
365.Vb 1
366\& perlcc -B myperlprogram.pl
367.Ve
368.PP
369The byte code is machine independent, so once you have a compiled
370module or program, it is as portable as Perl source (assuming that
371the user of the module or program has a modern-enough Perl interpreter
372to decode the byte code).
373.PP
374See \fBB::Bytecode\fR for information on options to control the
375optimization and nature of the code generated by the Bytecode module.
376.Sh "The Optimized C Back End"
377.IX Subsection "The Optimized C Back End"
378The optimized C back end will turn your Perl program's run time
379code-path into an equivalent (but optimized) C program that manipulates
380the Perl data structures directly. The program will still link against
381the Perl interpreter library, to allow for \fIeval()\fR, \f(CW\*(C`s///e\*(C'\fR,
382\&\f(CW\*(C`require\*(C'\fR, etc.
383.PP
384The \f(CW\*(C`perlcc\*(C'\fR tool generates such executables when using the \-O
385switch. To compile a Perl program (ending in \f(CW\*(C`.pl\*(C'\fR
386or \f(CW\*(C`.p\*(C'\fR):
387.PP
388.Vb 1
389\& perlcc -O myperlprogram.pl
390.Ve
391.PP
392To produce a shared library from a Perl module (ending in \f(CW\*(C`.pm\*(C'\fR):
393.PP
394.Vb 1
395\& perlcc -O Myperlmodule.pm
396.Ve
397.PP
398For more information, see perlcc and B::CC.
399.SH "Module List for the Compiler Suite"
400.IX Header "Module List for the Compiler Suite"
401.IP "B" 4
402.IX Item "B"
403This module is the introspective (\*(L"reflective\*(R" in Java terms)
404module, which allows a Perl program to inspect its innards. The
405back end modules all use this module to gain access to the compiled
406parse tree. You, the user of a back end module, will not need to
407interact with B.
408.IP "O" 4
409.IX Item "O"
410This module is the front-end to the compiler's back ends. Normally
411called something like this:
412.Sp
413.Vb 1
414\& $ perl -MO=Deparse myperlprogram
415.Ve
416.Sp
417This is like saying \f(CW\*(C`use O 'Deparse'\*(C'\fR in your Perl program.
418.IP "B::Asmdata" 4
419.IX Item "B::Asmdata"
420This module is used by the B::Assembler module, which is in turn used
421by the B::Bytecode module, which stores a parse-tree as
422bytecode for later loading. It's not a back end itself, but rather a
423component of a back end.
424.IP "B::Assembler" 4
425.IX Item "B::Assembler"
426This module turns a parse-tree into data suitable for storing
427and later decoding back into a parse\-tree. It's not a back end
428itself, but rather a component of a back end. It's used by the
429\&\fIassemble\fR program that produces bytecode.
430.IP "B::Bblock" 4
431.IX Item "B::Bblock"
432This module is used by the B::CC back end. It walks \*(L"basic blocks\*(R".
433A basic block is a series of operations which is known to execute from
434start to finish, with no possibility of branching or halting.
435.IP "B::Bytecode" 4
436.IX Item "B::Bytecode"
437This module is a back end that generates bytecode from a
438program's parse tree. This bytecode is written to a file, from where
439it can later be reconstructed back into a parse tree. The goal is to
440do the expensive program compilation once, save the interpreter's
441state into a file, and then restore the state from the file when the
442program is to be executed. See \*(L"The Bytecode Back End\*(R"
443for details about usage.
444.IP "B::C" 4
445.IX Item "B::C"
446This module writes out C code corresponding to the parse tree and
447other interpreter internal structures. You compile the corresponding
448C file, and get an executable file that will restore the internal
449structures and the Perl interpreter will begin running the
450program. See \*(L"The Simple C Back End\*(R" for details about usage.
451.IP "B::CC" 4
452.IX Item "B::CC"
453This module writes out C code corresponding to your program's
454operations. Unlike the B::C module, which merely stores the
455interpreter and its state in a C program, the B::CC module makes a
456C program that does not involve the interpreter. As a consequence,
457programs translated into C by B::CC can execute faster than normal
458interpreted programs. See \*(L"The Optimized C Back End\*(R" for
459details about usage.
460.IP "B::Concise" 4
461.IX Item "B::Concise"
462This module prints a concise (but complete) version of the Perl parse
463tree. Its output is more customizable than the one of B::Terse or
464B::Debug (and it can emulate them). This module useful for people who
465are writing their own back end, or who are learning about the Perl
466internals. It's not useful to the average programmer.
467.IP "B::Debug" 4
468.IX Item "B::Debug"
469This module dumps the Perl parse tree in verbose detail to \s-1STDOUT\s0.
470It's useful for people who are writing their own back end, or who
471are learning about the Perl internals. It's not useful to the
472average programmer.
473.IP "B::Deparse" 4
474.IX Item "B::Deparse"
475This module produces Perl source code from the compiled parse tree.
476It is useful in debugging and deconstructing other people's code,
477also as a pretty-printer for your own source. See
478\&\*(L"The Decompiling Back End\*(R" for details about usage.
479.IP "B::Disassembler" 4
480.IX Item "B::Disassembler"
481This module turns bytecode back into a parse tree. It's not a back
482end itself, but rather a component of a back end. It's used by the
483\&\fIdisassemble\fR program that comes with the bytecode.
484.IP "B::Lint" 4
485.IX Item "B::Lint"
486This module inspects the compiled form of your source code for things
487which, while some people frown on them, aren't necessarily bad enough
488to justify a warning. For instance, use of an array in scalar context
489without explicitly saying \f(CW\*(C`scalar(@array)\*(C'\fR is something that Lint
490can identify. See \*(L"The Lint Back End\*(R" for details about usage.
491.IP "B::Showlex" 4
492.IX Item "B::Showlex"
493This module prints out the \fImy()\fR variables used in a function or a
494file. To get a list of the \fImy()\fR variables used in the subroutine
495\&\fImysub()\fR defined in the file myperlprogram:
496.Sp
497.Vb 1
498\& $ perl -MO=Showlex,mysub myperlprogram
499.Ve
500.Sp
501To get a list of the \fImy()\fR variables used in the file myperlprogram:
502.Sp
503.Vb 1
504\& $ perl -MO=Showlex myperlprogram
505.Ve
506.Sp
507[\s-1BROKEN\s0]
508.IP "B::Stackobj" 4
509.IX Item "B::Stackobj"
510This module is used by the B::CC module. It's not a back end itself,
511but rather a component of a back end.
512.IP "B::Stash" 4
513.IX Item "B::Stash"
514This module is used by the perlcc program, which compiles a module
515into an executable. B::Stash prints the symbol tables in use by a
516program, and is used to prevent B::CC from producing C code for the
517B::* and O modules. It's not a back end itself, but rather a
518component of a back end.
519.IP "B::Terse" 4
520.IX Item "B::Terse"
521This module prints the contents of the parse tree, but without as much
522information as B::Debug. For comparison, \f(CW\*(C`print "Hello, world."\*(C'\fR
523produced 96 lines of output from B::Debug, but only 6 from B::Terse.
524.Sp
525This module is useful for people who are writing their own back end,
526or who are learning about the Perl internals. It's not useful to the
527average programmer.
528.IP "B::Xref" 4
529.IX Item "B::Xref"
530This module prints a report on where the variables, subroutines, and
531formats are defined and used within a program and the modules it
532loads. See \*(L"The Cross Referencing Back End\*(R" for details about
533usage.
534.SH "KNOWN PROBLEMS"
535.IX Header "KNOWN PROBLEMS"
536The simple C backend currently only saves typeglobs with alphanumeric
537names.
538.PP
539The optimized C backend outputs code for more modules than it should
540(e.g., DirHandle). It also has little hope of properly handling
541\&\f(CW\*(C`goto LABEL\*(C'\fR outside the running subroutine (\f(CW\*(C`goto &sub\*(C'\fR is okay).
542\&\f(CW\*(C`goto LABEL\*(C'\fR currently does not work at all in this backend.
543It also creates a huge initialization function that gives
544C compilers headaches. Splitting the initialization function gives
545better results. Other problems include: unsigned math does not
546work correctly; some opcodes are handled incorrectly by default
547opcode handling mechanism.
548.PP
549BEGIN{} blocks are executed while compiling your code. Any external
550state that is initialized in BEGIN{}, such as opening files, initiating
551database connections etc., do not behave properly. To work around
552this, Perl has an INIT{} block that corresponds to code being executed
553before your program begins running but after your program has finished
554being compiled. Execution order: BEGIN{}, (possible save of state
555through compiler back\-end), INIT{}, program runs, END{}.
556.SH "AUTHOR"
557.IX Header "AUTHOR"
558This document was originally written by Nathan Torkington, and is now
559maintained by the perl5\-porters mailing list
560\&\fIperl5\-porters@perl.org\fR.