README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. The B::C, B::CC, B::Bytecode Perl Compiler Kit
  2. Copyright (c) 1996, 1997, Malcolm Beattie
  3. Copyright (c) 2008, 2009, 2010, 2011 Reini Urban
  4. Copyright (c) 2012, 2013, 2014 cPanel Inc
  5. Homepage: http://www.perl-compiler.org/
  6. Releases: http://search.cpan.org/dist/B-C/
  7. Code: http://github.com/rurban/perl-compiler/
  8. and mirrored at http://code.google.com/p/perl-compiler/
  9. INSTALL
  10. cpan B::C
  11. On strawberry I needed
  12. perl Makefile.PL FIXIN="perl -S pl2bat.bat"
  13. On Windows and AIX for 5.12 and 5.14 you need to patch and rebuild CORE perl:
  14. ramblings/Export-store_cop_label-for-the-perl-compiler.patch
  15. For 5.14 and 5.15 I recommend also the following patches:
  16. ramblings/revert-B-load-BEGIN.patch (The 5.14.1 version)
  17. ramblings/Carp-wo-B.patch
  18. USAGE
  19. The Bytecode, C and CC backends are now all functional
  20. enough to compile almost the whole of the main perl test
  21. suite and 99-100% of the top100 modules.
  22. In the case of the CC backend, any failures are all
  23. due to differences and/or known bugs documented below.
  24. See the file TESTS.
  25. (1) To compile perl program foo.pl with the C backend, do
  26. perl -MO=C,-ofoo.c foo.pl
  27. Then use the cc_harness perl program to compile the
  28. resulting C source:
  29. perl cc_harness -O2 -o foo foo.c
  30. If you are using a non-ANSI pre-Standard C compiler that
  31. can't handle pre-declaring static arrays, then add
  32. -DBROKEN_STATIC_REDECL to the options you use:
  33. perl cc_harness -O2 -o foo -DBROKEN_STATIC_REDECL foo.c
  34. If you are using a non-ANSI pre-Standard C compiler that
  35. can't handle static initialisation of structures with union
  36. members then add -DBROKEN_UNION_INIT to the options you
  37. use. If you want command line arguments passed to your
  38. executable to be interpreted by perl (e.g. -Dx) then compile
  39. foo.c with -DALLOW_PERL_OPTIONS. Otherwise, all command line
  40. arguments passed to foo will appear directly in @ARGV. The
  41. resulting executable foo is the compiled version of
  42. foo.pl. See the file NOTES for extra options you can pass to
  43. -MO=C.
  44. There are some constraints on the contents on foo.pl if you
  45. want to be able to compile it successfully. Some problems
  46. can be fixed fairly easily by altering foo.pl; some problems
  47. with the compiler are known to be straightforward to solve
  48. and I'll do so soon. The file Todo lists a number of known
  49. problems. See the XSUB section lower down for information
  50. about compiling programs which use XSUBs.
  51. (2) To compile foo.pl with the CC backend (which generates
  52. actual optimised C code for the execution path of your perl
  53. program), use
  54. perl -MO=CC,-ofoo.c foo.pl
  55. and proceed just as with the C backend. You should almost
  56. certainly use an option such as -O2 with the subsequent
  57. cc_harness invocation so that your C compiler uses
  58. optimisation. The C code generated by the Perl compiler's CC
  59. backend looks ugly to humans but is easily optimised by C
  60. compilers.
  61. To make the most of this optimizing compiler backend, you need to tell
  62. the compiler when you're using int or double variables so that it can
  63. optimise appropriately. The old deprecated way do that was by naming
  64. lexical variables ending in "_i" for ints, "_d" for doubles, "_ir" for
  65. int "register" variables or "_dr" for double "register"
  66. variables. Here "register" is a promise that you won't pass a
  67. reference to the variable into a sub which then modifies the variable.
  68. The new way is to declare those lexicals with "my int" and "my
  69. double". The compiler ought to catch attempts to use "\$i" just as C
  70. compilers catch attempts to do "&i" for a register int i, but it
  71. doesn't at the moment. Bugs in the CC backend may make your program
  72. fail in mysterious ways and give wrong answers rather than just crash
  73. in boring ways. CC is still on the experimental level. Please use your
  74. test suite.
  75. If your program uses classes which define methods (or other subs which
  76. are not exported and not apparently used until runtime) then you'll
  77. need to use -u compile-time options (see the NOTES file) to force the
  78. subs to be compiled. Future releases will probably default the other
  79. way, do more auto-detection and provide more fine-grained control.
  80. Since compiled executables need linking with libperl, you
  81. may want to turn libperl.a into a shared library if your
  82. platform supports it, -Duseshrplib.
  83. You'll probably also want to link your main perl executable
  84. against libperl.so; it's nice having an 11K perl executable.
  85. (3) To compile foo.pl into bytecode do
  86. perl -MO=Bytecode,-ofoo.plc foo.pl
  87. To run the resulting bytecode file foo.plc, you use the
  88. ByteLoader module which should have been built along with
  89. the extensions.
  90. perl -MByteLoader foo.plc
  91. Previous Perl releases had ByteLoader in CORE, so you can omit
  92. -MByteLoader there.
  93. You can also do -H to automatically use ByteLoader
  94. perl -MO=Bytecode,-H,-ofoo.plc foo.pl
  95. perl foo.plc
  96. Any extra arguments are passed in as @ARGV; they are not interpreted
  97. as perl options.
  98. See the NOTES file for details of these and other options (including
  99. optimisation options and ways of getting at the intermediate "assembler"
  100. code that the Bytecode backend uses).
  101. (3) There are little Bourne shell scripts and perl programs to aid with
  102. some common operations:
  103. perlcc, assemble, disassemble, cc_harness
  104. XSUBS
  105. The C and CC backends can successfully compile some perl programs which
  106. make use of XSUB extensions. [I'll add more detail to this section in a
  107. later release.] As a prerequisite, such extensions must not need to do
  108. anything in their BOOT: section which needs to be done at runtime rather
  109. than compile time. Normally, the only code in the boot_Foo() function is
  110. a list of newXS() calls which xsubpp puts there and the compiler handles
  111. saving those XS subs itself. For each XSUB used, the C and CC compiler
  112. will generate an initialiser in their C output which refers to the name
  113. of the relevant C function (XS_Foo_somesub). What is not yet automated
  114. is the necessary commands and cc command-line options (e.g. via
  115. "perl cc_harness") which link against the extension libraries. For now,
  116. you need the XSUB extension to have installed files in the right format
  117. for using as C libraries (e.g. Foo.a or Foo.so). As the Foo.so files (or
  118. your platform's version) aren't suitable for linking against, you will
  119. have to reget the extension source and rebuild it as a static extension
  120. to force the generation of a suitable Foo.a file. Then you need to make
  121. a symlink (or copy or rename) of that file into a libFoo.a suitable for
  122. cc linking. Then add the appropriate -L and -l options to your
  123. "perl cc_harness" command line to find and link against those libraries.
  124. You may also need to fix up some platform-dependent environment variable
  125. to ensure that linked-against .so files are found at runtime too.
  126. Read about perlcc --staticxs
  127. DIFFERENCES
  128. The result of running a CC compiled Perl program can sometimes be different
  129. from running the same program with standard perl. Think of the compiler
  130. as having a slightly different implementation of the language Perl.
  131. Unfortunately, since Perl has had a single implementation until now,
  132. there are no formal standards or documents defining what behaviour is
  133. guaranteed of Perl the language and what just "happens to work".
  134. Some of the differences below are almost impossible to change because of
  135. the way the compiler works. Others can be changed to produce "standard"
  136. perl behaviour if it's deemed proper and the resulting performance hit
  137. is accepted. I'll use "standard perl" to mean the result of running a
  138. Perl program using the perl executable from the perl distribution.
  139. I'll use "compiled Perl program" to mean running an executable produced
  140. by this compiler kit ("the compiler") with the CC backend.
  141. Loops
  142. Standard perl calculates the target of "next", "last", and "redo"
  143. at run-time. The compiler calculates the targets at compile-time.
  144. For example, the program
  145. sub skip_on_odd { next NUMBER if $_[0] % 2 }
  146. NUMBER: for ($i = 0; $i < 5; $i++) {
  147. skip_on_odd($i);
  148. print $i;
  149. }
  150. produces the output
  151. 024
  152. with standard perl but gives a compile-time error with the compiler.
  153. See test 21.
  154. Context of ".."
  155. The context (scalar or array) of the ".." operator determines whether
  156. it behaves as a range or a flip/flop. Standard perl delays until
  157. runtime the decision of which context it is in but the compiler needs
  158. to know the context at compile-time. For example,
  159. @a = (4,6,1,0,0,1);
  160. sub range { (shift @a)..(shift @a) }
  161. print range();
  162. while (@a) { print scalar(range()) }
  163. generates the output
  164. 456123E0
  165. with standard Perl but gives a compile-time error with compiled Perl.
  166. See test 30.
  167. Arithmetic
  168. Optimized compiled Perl programs use native C arithmetic
  169. much more frequently than standard perl. So operations on
  170. large numbers or on boundary cases may produce different behaviour.
  171. Deprecated features
  172. Features of standard perl such as $[ which have been deprecated
  173. in standard perl since version 5 was released have not been
  174. implemented in the compiler.
  175. STATUS
  176. C is stable, CC is unstable.
  177. Bytecode stable until 5.16
  178. The Bytecode compiler is disabled for 5.6.2, use the default instead.
  179. See STATUS for details.
  180. BUGS
  181. Here are some things which may cause the compiler problems.
  182. The following render the compiler useless (without serious hacking):
  183. * The following operators are not yet implemented for CC
  184. goto
  185. continue/next/last to a outer LABEL
  186. * You can't use "last" to exit from a non-loop block.
  187. The following may give significant problems:
  188. * BEGIN blocks containing complex initialisation code,
  189. esp. sideeffects. All the BEGIN code is evaluated once at compile-time,
  190. and NOT executed at run-time.
  191. * Code which is only ever referred to at runtime (e.g. via eval "..." or
  192. via method calls): see the -u option for the C and CC backends.
  193. The following may cause problems (not thoroughly tested):
  194. * For the C and CC backends: compile-time strings which are longer than
  195. your C compiler can cope with in a single line or definition.
  196. * Reliance on intimate details of global destruction
  197. * Any "-w" option in the first line of your perl program is seen and
  198. acted on by perl itself before the compiler starts. The compiler
  199. itself then runs with warnings turned on. This may cause perl to
  200. print out warnings about the compiler itself since I haven't tested
  201. it thoroughly with warnings turned on.
  202. There is a terser but more complete list in the Todo file.
  203. LICENSE
  204. This program is free software; you can redistribute it and/or modify
  205. it under the terms of either:
  206. a) the GNU General Public License as published by the Free
  207. Software Foundation; either version 1, or (at your option) any
  208. later version, or
  209. b) the "Artistic License" which comes with this kit.
  210. This program is distributed in the hope that it will be useful,
  211. but WITHOUT ANY WARRANTY; without even the implied warranty of
  212. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
  213. the GNU General Public License or the Artistic License for more details.
  214. You should have received a copy of the Artistic License with this kit,
  215. in the file named "Artistic". If not, you can get one from the Perl
  216. distribution. You should also have received a copy of the GNU General
  217. Public License, in the file named "Copying". If not, you can get one
  218. from the Perl distribution or else write to the Free Software Foundation,
  219. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  220. Reini Urban
  221. 2014-02-11
  222. Malcolm Beattie
  223. 2 September 1996