README 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. Homepage: http://www.perl-compiler.org/
  5. Releases: http://search.cpan.org/dist/B-C/
  6. Code: http://code.google.com/p/perl-compiler/
  7. git clone https://code.google.com/p/perl-compiler/
  8. was previously:
  9. svn checkout http://perl-compiler.googlecode.com/svn/trunk/ perl-compiler
  10. INSTALL
  11. cpan B::C
  12. On strawberry I needed
  13. perl Makefile.PL FIXIN="perl -S pl2bat.bat"
  14. On Windows and AIX for 5.12 and 5.14 you need to patch and rebuild CORE perl:
  15. ramblings/Export-store_cop_label-for-the-perl-compiler.patch
  16. For 5.14 and 5.15 I recommend also the following patches:
  17. ramblings/revert-B-load-BEGIN.patch (The 5.14.1 version)
  18. ramblings/Carp-wo-B.patch
  19. USAGE
  20. The Bytecode, C and CC backends are now all functional
  21. enough to compile almost the whole of the main perl test
  22. suite and 97-100% of the top100 modules.
  23. In the case of the CC backend, any failures are all
  24. due to differences and/or known bugs documented below. See
  25. the file TESTS.
  26. (1) To compile perl program foo.pl with the C backend, do
  27. perl -MO=C,-ofoo.c foo.pl
  28. Then use the cc_harness perl program to compile the
  29. resulting C source:
  30. perl cc_harness -O2 -o foo foo.c
  31. If you are using a non-ANSI pre-Standard C compiler that
  32. can't handle pre-declaring static arrays, then add
  33. -DBROKEN_STATIC_REDECL to the options you use:
  34. perl cc_harness -O2 -o foo -DBROKEN_STATIC_REDECL foo.c
  35. If you are using a non-ANSI pre-Standard C compiler that
  36. can't handle static initialisation of structures with union
  37. members then add -DBROKEN_UNION_INIT to the options you
  38. use. If you want command line arguments passed to your
  39. executable to be interpreted by perl (e.g. -Dx) then compile
  40. foo.c with -DALLOW_PERL_OPTIONS. Otherwise, all command line
  41. arguments passed to foo will appear directly in @ARGV. The
  42. resulting executable foo is the compiled version of
  43. foo.pl. See the file NOTES for extra options you can pass to
  44. -MO=C.
  45. There are some constraints on the contents on foo.pl if you
  46. want to be able to compile it successfully. Some problems
  47. can be fixed fairly easily by altering foo.pl; some problems
  48. with the compiler are known to be straightforward to solve
  49. and I'll do so soon. The file Todo lists a number of known
  50. problems. See the XSUB section lower down for information
  51. about compiling programs which use XSUBs.
  52. (2) To compile foo.pl with the CC backend (which generates
  53. actual optimised C code for the execution path of your perl
  54. program), use
  55. perl -MO=CC,-ofoo.c foo.pl
  56. and proceed just as with the C backend. You should almost
  57. certainly use an option such as -O2 with the subsequent
  58. cc_harness invocation so that your C compiler uses
  59. optimisation. The C code generated by the Perl compiler's CC
  60. backend looks ugly to humans but is easily optimised by C
  61. compilers.
  62. To make the most of this optimizing compiler backend, you need to tell
  63. the compiler when you're using int or double variables so that it can
  64. optimise appropriately. The old deprecated way do that was by naming
  65. lexical variables ending in "_i" for ints, "_d" for doubles, "_ir" for
  66. int "register" variables or "_dr" for double "register"
  67. variables. Here "register" is a promise that you won't pass a
  68. reference to the variable into a sub which then modifies the variable.
  69. The new way is to declare those lexicals with "my int" and "my
  70. double". The compiler ought to catch attempts to use "\$i" just as C
  71. compilers catch attempts to do "&i" for a register int i, but it
  72. doesn't at the moment. Bugs in the CC backend may make your program
  73. fail in mysterious ways and give wrong answers rather than just crash
  74. in boring ways. CC is still on the experimental level. Please use your
  75. test suite.
  76. If your program uses classes which define methods (or other subs which
  77. are not exported and not apparently used until runtime) then you'll
  78. need to use -u compile-time options (see the NOTES file) to force the
  79. subs to be compiled. Future releases will probably default the other
  80. way, do more auto-detection and provide more fine-grained control.
  81. Since compiled executables need linking with libperl, you
  82. may want to turn libperl.a into a shared library if your
  83. platform supports it, -Duseshrplib.
  84. You'll probably also want to link your main perl executable
  85. against libperl.so; it's nice having an 11K perl executable.
  86. (3) To compile foo.pl into bytecode do
  87. perl -MO=Bytecode,-ofoo.plc foo.pl
  88. To run the resulting bytecode file foo.plc, you use the
  89. ByteLoader module which should have been built along with
  90. the extensions.
  91. perl -MByteLoader foo.plc
  92. Previous Perl releases had ByteLoader in CORE, so you can omit
  93. -MByteLoader there.
  94. You can also do -H to automatically use ByteLoader
  95. perl -MO=Bytecode,-H,-ofoo.plc foo.pl
  96. perl foo.plc
  97. Any extra arguments are passed in as @ARGV; they are not interpreted
  98. as perl options.
  99. See the NOTES file for details of these and other options (including
  100. optimisation options and ways of getting at the intermediate "assembler"
  101. code that the Bytecode backend uses).
  102. (3) There are little Bourne shell scripts and perl programs to aid with
  103. some common operations:
  104. perlcc, assemble, disassemble, cc_harness
  105. XSUBS
  106. The C and CC backends can successfully compile some perl programs which
  107. make use of XSUB extensions. [I'll add more detail to this section in a
  108. later release.] As a prerequisite, such extensions must not need to do
  109. anything in their BOOT: section which needs to be done at runtime rather
  110. than compile time. Normally, the only code in the boot_Foo() function is
  111. a list of newXS() calls which xsubpp puts there and the compiler handles
  112. saving those XS subs itself. For each XSUB used, the C and CC compiler
  113. will generate an initialiser in their C output which refers to the name
  114. of the relevant C function (XS_Foo_somesub). What is not yet automated
  115. is the necessary commands and cc command-line options (e.g. via
  116. "perl cc_harness") which link against the extension libraries. For now,
  117. you need the XSUB extension to have installed files in the right format
  118. for using as C libraries (e.g. Foo.a or Foo.so). As the Foo.so files (or
  119. your platform's version) aren't suitable for linking against, you will
  120. have to reget the extension source and rebuild it as a static extension
  121. to force the generation of a suitable Foo.a file. Then you need to make
  122. a symlink (or copy or rename) of that file into a libFoo.a suitable for
  123. cc linking. Then add the appropriate -L and -l options to your
  124. "perl cc_harness" command line to find and link against those libraries.
  125. You may also need to fix up some platform-dependent environment variable
  126. to ensure that linked-against .so files are found at runtime too.
  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 and Bytecode works fine, best with non-threaded perls. 3-4% fails on the top100 modules.
  177. CC fails on some tests. See Todo.
  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. * Use of the DATA filehandle (via __END__ or __DATA__ tokens) on earlier
  184. Perl versions withotu IO::Scalar. See test 15.
  185. * Operator overloading with %OVERLOAD
  186. * The (deprecated) magic array-offset variable $[ does not work
  187. * For -O1 and -O2 copy-on-grow of static pvs and heks since 5.10 is disabled.
  188. Fixes of the destruction failures there is work in progress.
  189. * The following operators are not yet implemented for CC
  190. goto
  191. continue/next/last to a outer LABEL
  192. * You can't use "last" to exit from a non-loop block.
  193. The following may give significant problems:
  194. * BEGIN blocks containing complex initialisation code,
  195. esp. sideeffects. All the BEGIN code is evaluated once at compile-time,
  196. and NOT executed at run-time.
  197. * Code which is only ever referred to at runtime (e.g. via eval "..." or
  198. via method calls): see the -u option for the C and CC backends.
  199. * Run-time lookups of lexical variables in "outside" closures
  200. The following may cause problems (not thoroughly tested):
  201. * Dependencies on whether values of some "magic" Perl variables are
  202. determined at compile-time or runtime.
  203. * For the C and CC backends: compile-time strings which are longer than
  204. your C compiler can cope with in a single line or definition.
  205. * Reliance on intimate details of global destruction
  206. * Any "-w" option in the first line of your perl program is seen and
  207. acted on by perl itself before the compiler starts. The compiler
  208. itself then runs with warnings turned on. This may cause perl to
  209. print out warnings about the compiler itself since I haven't tested
  210. it thoroughly with warnings turned on.
  211. There is a terser but more complete list in the Todo file.
  212. LICENSE
  213. This program is free software; you can redistribute it and/or modify
  214. it under the terms of either:
  215. a) the GNU General Public License as published by the Free
  216. Software Foundation; either version 1, or (at your option) any
  217. later version, or
  218. b) the "Artistic License" which comes with this kit.
  219. This program is distributed in the hope that it will be useful,
  220. but WITHOUT ANY WARRANTY; without even the implied warranty of
  221. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
  222. the GNU General Public License or the Artistic License for more details.
  223. You should have received a copy of the Artistic License with this kit,
  224. in the file named "Artistic". If not, you can get one from the Perl
  225. distribution. You should also have received a copy of the GNU General
  226. Public License, in the file named "Copying". If not, you can get one
  227. from the Perl distribution or else write to the Free Software Foundation,
  228. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  229. Reini Urban
  230. 2011-04-21
  231. Malcolm Beattie
  232. 2 September 1996