README 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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, 2015 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 until 2016 at http://code.google.com/p/perl-compiler/
  9. OVERVIEW
  10. Malcom Beattie's perl compiler ("perlcc") updated to 5.10 - 5.21.3,
  11. with most bugs fixed (also for 5.6. and 5.8) and new features
  12. added. It compiles to C or platform-compatible Bytecode.
  13. Releases are at http://search.cpan.org/dist/B-C/
  14. Development is in the master branch at https://github.com/rurban/perl-compiler,
  15. the stable branch is called 'release', the newest perl releases usually don't work
  16. immediately. I typically need 6 months to catch up.
  17. Known issues:
  18. See https://code.google.com/p/perl-compiler/issues/
  19. (Soon at http://github.com/rurban/perl-compiler/issues)
  20. * run-time attributes (e.g. use open attributes, Attribute::Handlers, ...),
  21. but ok at compile-time.
  22. * compile-time perlio layers are not restored
  23. * some compile-time side-effects can not be reproduced when running a compiled
  24. binary, such as BEGIN { chdir $dir }, i.e. most system and IO calls. use
  25. INIT {} instead.
  26. * Certain XS module using compile-time pointers need special workarounds:
  27. * DBI patched by the compiler (#359)
  28. * Encode > 2.58 (#71, #305, RT 94221), patched by the compiler
  29. * Net::DNS > 0.67 (#305, RT 94069)
  30. * IO::Socket::SSL > 1.995 (#317, RT 95452)
  31. * DBD::mysql > 4.027 (RT 97625)
  32. * Todo: FCGI, Coro, Moose with meta->make_immutable
  33. Mailinglist:
  34. http://groups.google.com/group/perl-compiler perl-compiler@googlegroups.com
  35. INSTALL
  36. cpan B::C
  37. On strawberry I needed
  38. perl Makefile.PL FIXIN="perl -S pl2bat.bat"
  39. On Windows and AIX for 5.12 and 5.14 you need to patch and rebuild CORE perl:
  40. ramblings/Export-store_cop_label-for-the-perl-compiler.patch
  41. For 5.14 and 5.15 I recommend also the following patches:
  42. ramblings/revert-B-load-BEGIN.patch (The 5.14.1 version)
  43. ramblings/Carp-wo-B.patch
  44. We generally discourage the use of 5.16 and 5.18 for unhandled and
  45. unacknowledged security problems with the implementation of "unicode"
  46. symbols and packagenames, where they really implemented binary names
  47. for all symbols without any checks and further support of such binary
  48. names. See the warning at perl Makefile.PL.
  49. 5.20 improved support for binary names for most syscalls, dumpers and
  50. APIs, but TR39 and strict names are still not handled, the problems
  51. are not understood and reactions are generally hostile.
  52. Summarized:
  53. Perl handling of new unicode identifiers - package and symbol names since 5.16 -
  54. without proper TR39 handling is considered a security risc and is not fully supported.
  55. See http://websec.github.io/unicode-security-guide/
  56. Check your code for syntax spoofs, confusables, esp. strip \\0 from package names.
  57. Enable use warnings 'syscalls'. There is no use strict 'names' yet. You cannot disable
  58. binary symbolname support with a configure option.
  59. USAGE
  60. The Bytecode, C and CC backends are now all functional
  61. enough to compile almost the whole of the main perl test
  62. suite and 99-100% of the top100 modules.
  63. In the case of the CC backend, any failures are all
  64. due to differences and/or known bugs documented below.
  65. See the file TESTS.
  66. (1) To compile perl program foo.pl with the C backend, do
  67. perl -MO=C,-ofoo.c foo.pl
  68. Then use the cc_harness perl program to compile the
  69. resulting C source:
  70. perl cc_harness -O2 -o foo foo.c
  71. If you are using a non-ANSI pre-Standard C compiler that
  72. can't handle pre-declaring static arrays, then add
  73. -DBROKEN_STATIC_REDECL to the options you use:
  74. perl cc_harness -O2 -o foo -DBROKEN_STATIC_REDECL foo.c
  75. If you are using a non-ANSI pre-Standard C compiler that
  76. can't handle static initialisation of structures with union
  77. members then add -DBROKEN_UNION_INIT to the options you
  78. use. If you want command line arguments passed to your
  79. executable to be interpreted by perl (e.g. -Dx) then compile
  80. foo.c with -DALLOW_PERL_OPTIONS. Otherwise, all command line
  81. arguments passed to foo will appear directly in @ARGV. The
  82. resulting executable foo is the compiled version of
  83. foo.pl. See the file NOTES for extra options you can pass to
  84. -MO=C.
  85. There are some constraints on the contents on foo.pl if you
  86. want to be able to compile it successfully. Some problems
  87. can be fixed fairly easily by altering foo.pl; some problems
  88. with the compiler are known to be straightforward to solve
  89. and I'll do so soon. The file Todo lists a number of known
  90. problems. See the XSUB section lower down for information
  91. about compiling programs which use XSUBs.
  92. (2) To compile foo.pl with the CC backend (which generates
  93. actual optimised C code for the execution path of your perl
  94. program), use
  95. perl -MO=CC,-ofoo.c foo.pl
  96. and proceed just as with the C backend. You should almost
  97. certainly use an option such as -O2 with the subsequent
  98. cc_harness invocation so that your C compiler uses
  99. optimisation. The C code generated by the Perl compiler's CC
  100. backend looks ugly to humans but is easily optimised by C
  101. compilers.
  102. To make the most of this optimizing compiler backend, you need to tell
  103. the compiler when you're using int or double variables so that it can
  104. optimise appropriately. The old deprecated way do that was by naming
  105. lexical variables ending in "_i" for ints, "_d" for doubles, "_ir" for
  106. int "register" variables or "_dr" for double "register"
  107. variables. Here "register" is a promise that you won't pass a
  108. reference to the variable into a sub which then modifies the variable.
  109. The new way is to declare those lexicals with "my int" and "my
  110. double". The compiler ought to catch attempts to use "\$i" just as C
  111. compilers catch attempts to do "&i" for a register int i, but it
  112. doesn't at the moment. Bugs in the CC backend may make your program
  113. fail in mysterious ways and give wrong answers rather than just crash
  114. in boring ways. CC is still on the experimental level. Please use your
  115. test suite.
  116. If your program uses classes which define methods (or other subs which
  117. are not exported and not apparently used until runtime) then you'll
  118. need to use -u compile-time options (see the NOTES file) to force the
  119. subs to be compiled. Future releases will probably default the other
  120. way, do more auto-detection and provide more fine-grained control.
  121. Since compiled executables need linking with libperl, you
  122. may want to turn libperl.a into a shared library if your
  123. platform supports it, -Duseshrplib.
  124. You'll probably also want to link your main perl executable
  125. against libperl.so; it's nice having an 11K perl executable.
  126. (3) To compile foo.pl into bytecode do
  127. perl -MO=Bytecode,-ofoo.plc foo.pl
  128. To run the resulting bytecode file foo.plc, you use the
  129. ByteLoader module which should have been built along with
  130. the extensions.
  131. perl -MByteLoader foo.plc
  132. Previous Perl releases had ByteLoader in CORE, so you can omit
  133. -MByteLoader there.
  134. You can also do -H to automatically use ByteLoader
  135. perl -MO=Bytecode,-H,-ofoo.plc foo.pl
  136. perl foo.plc
  137. Any extra arguments are passed in as @ARGV; they are not interpreted
  138. as perl options.
  139. See the NOTES file for details of these and other options (including
  140. optimisation options and ways of getting at the intermediate "assembler"
  141. code that the Bytecode backend uses).
  142. (3) There are little Bourne shell scripts and perl programs to aid with
  143. some common operations:
  144. perlcc, assemble, disassemble, cc_harness
  145. XSUBS
  146. The C and CC backends can successfully compile some perl programs which
  147. make use of XSUB extensions. [I'll add more detail to this section in a
  148. later release.] As a prerequisite, such extensions must not need to do
  149. anything in their BOOT: section which needs to be done at runtime rather
  150. than compile time. Normally, the only code in the boot_Foo() function is
  151. a list of newXS() calls which xsubpp puts there and the compiler handles
  152. saving those XS subs itself. For each XSUB used, the C and CC compiler
  153. will generate an initialiser in their C output which refers to the name
  154. of the relevant C function (XS_Foo_somesub). What is not yet automated
  155. is the necessary commands and cc command-line options (e.g. via
  156. "perl cc_harness") which link against the extension libraries. For now,
  157. you need the XSUB extension to have installed files in the right format
  158. for using as C libraries (e.g. Foo.a or Foo.so). As the Foo.so files (or
  159. your platform's version) aren't suitable for linking against, you will
  160. have to reget the extension source and rebuild it as a static extension
  161. to force the generation of a suitable Foo.a file. Then you need to make
  162. a symlink (or copy or rename) of that file into a libFoo.a suitable for
  163. cc linking. Then add the appropriate -L and -l options to your
  164. "perl cc_harness" command line to find and link against those libraries.
  165. You may also need to fix up some platform-dependent environment variable
  166. to ensure that linked-against .so files are found at runtime too.
  167. Read about perlcc --staticxs
  168. DIFFERENCES
  169. The result of running a CC compiled Perl program can sometimes be different
  170. from running the same program with standard perl. Think of the compiler
  171. as having a slightly different implementation of the language Perl.
  172. Unfortunately, since Perl has had a single implementation until now,
  173. there are no formal standards or documents defining what behaviour is
  174. guaranteed of Perl the language and what just "happens to work".
  175. Some of the differences below are almost impossible to change because of
  176. the way the compiler works. Others can be changed to produce "standard"
  177. perl behaviour if it's deemed proper and the resulting performance hit
  178. is accepted. I'll use "standard perl" to mean the result of running a
  179. Perl program using the perl executable from the perl distribution.
  180. I'll use "compiled Perl program" to mean running an executable produced
  181. by this compiler kit ("the compiler") with the CC backend.
  182. Loops
  183. Standard perl calculates the target of "next", "last", and "redo"
  184. at run-time. The compiler calculates the targets at compile-time.
  185. For example, the program
  186. sub skip_on_odd { next NUMBER if $_[0] % 2 }
  187. NUMBER: for ($i = 0; $i < 5; $i++) {
  188. skip_on_odd($i);
  189. print $i;
  190. }
  191. produces the output
  192. 024
  193. with standard perl but gives a compile-time error with the compiler.
  194. See test 21.
  195. Context of ".."
  196. The context (scalar or array) of the ".." operator determines whether
  197. it behaves as a range or a flip/flop. Standard perl delays until
  198. runtime the decision of which context it is in but the compiler needs
  199. to know the context at compile-time. For example,
  200. @a = (4,6,1,0,0,1);
  201. sub range { (shift @a)..(shift @a) }
  202. print range();
  203. while (@a) { print scalar(range()) }
  204. generates the output
  205. 456123E0
  206. with standard Perl but gives a compile-time error with compiled Perl.
  207. See test 30.
  208. Arithmetic
  209. Optimized compiled Perl programs use native C arithmetic
  210. much more frequently than standard perl. So operations on
  211. large numbers or on boundary cases may produce different behaviour.
  212. Deprecated features
  213. Features of standard perl such as $[ which have been deprecated
  214. in standard perl since version 5 was released have not been
  215. implemented in the compiler.
  216. STATUS
  217. C is stable, CC is unstable.
  218. Bytecode stable until 5.16
  219. The Bytecode compiler is disabled for 5.6.2, use the default instead.
  220. See STATUS for details.
  221. BUGS
  222. Here are some things which may cause the compiler problems.
  223. The following render the compiler useless (without serious hacking):
  224. * The following operators are not yet implemented for CC
  225. goto
  226. continue/next/last to a outer LABEL
  227. * You can't use "last" to exit from a non-loop block.
  228. * use Attribute::Handlers, or run-time usage of attributes. Usage of
  229. Attribute::Handlers is discouraged generally for security reasons, as it
  230. evals all attributes. (#169)
  231. * Accessing @- values from compiled regular expressions are currently broken (#281)
  232. The following may give significant problems:
  233. * BEGIN blocks containing complex initialisation code,
  234. esp. sideeffects. All the BEGIN code is evaluated once at compile-time,
  235. and NOT executed at run-time.
  236. * Code which is only ever referred to at runtime (e.g. via eval "..." or
  237. via method calls): see the -u option for the C and CC backends.
  238. * compile-time perlio layers. Use them at run-time instead.
  239. i.e. use open qw(:std :utf8) does not work yet on STDIN.
  240. * run-time loading of DynaLoader packages which use AutoLoad (i.e. BSD::Resource).
  241. Compile them in, e.g. with use package. (#308)
  242. * format STDOUT or STDERR (#285)
  243. The following may cause problems (not thoroughly tested):
  244. * For the C and CC backends: compile-time strings which are longer than
  245. your C compiler can cope with in a single line or definition.
  246. Use perlcc to workaround MSVC problems.
  247. * Reliance on intimate details of global destruction. Implemented only with 1.46
  248. There is a terser but more complete list in the Todo file.
  249. LICENSE
  250. This program is free software; you can redistribute it and/or modify
  251. it under the terms of either:
  252. a) the GNU General Public License as published by the Free
  253. Software Foundation; either version 1, or (at your option) any
  254. later version, or
  255. b) the "Artistic License" which comes with this kit.
  256. This program is distributed in the hope that it will be useful,
  257. but WITHOUT ANY WARRANTY; without even the implied warranty of
  258. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
  259. the GNU General Public License or the Artistic License for more details.
  260. You should have received a copy of the Artistic License with this kit,
  261. in the file named "Artistic". If not, you can get one from the Perl
  262. distribution. You should also have received a copy of the GNU General
  263. Public License, in the file named "Copying". If not, you can get one
  264. from the Perl distribution or else write to the Free Software Foundation,
  265. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  266. Reini Urban
  267. 2015-03-17
  268. Malcolm Beattie
  269. 2 September 1996