README 14 KB

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