README.alpha 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. B::C Perl Compiler Kit, alpha 6
  2. Copyright (c) 1996, 1997, Malcolm Beattie
  3. Copyright (c) 2008, 2009 Reini Urban
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of either:
  6. a) the GNU General Public License as published by the Free
  7. Software Foundation; either version 1, or (at your option) any
  8. later version, or
  9. b) the "Artistic License" which comes with this kit.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
  13. the GNU General Public License or the Artistic License for more details.
  14. You should have received a copy of the Artistic License with this kit,
  15. in the file named "Artistic". If not, you can get one from the Perl
  16. distribution. You should also have received a copy of the GNU General
  17. Public License, in the file named "Copying". If not, you can get one
  18. from the Perl distribution or else write to the Free Software Foundation,
  19. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20. Releases are at http://search.cpan.org/dist/B-C/
  21. Code is now at http://code.google.com/p/perl-compiler/
  22. CHANGES
  23. Since alpha5 see Changes
  24. New since alpha5
  25. Todo
  26. New since alpha4
  27. Todo
  28. New since alpha3
  29. Anonymous subs work properly with C and CC.
  30. Heuristics for forcing compilation of apparently unused subs/methods.
  31. Subs which use the AutoLoader module are forcibly loaded at compile-time.
  32. Slightly faster compilation.
  33. Handles slightly more complex code within a BEGIN { }.
  34. Minor bug fixes.
  35. New since alpha2
  36. CC backend now supports ".." and s//e.
  37. Xref backend generates cross-reference reports
  38. Cleanups to fix benign but irritating "-w" warnings
  39. Minor cxstack fix
  40. New since alpha1
  41. Working CC backend
  42. Shared globs and pre-initialised hash support
  43. Some XSUB support
  44. Assorted bug fixes
  45. INSTALLATION for alpha4 (outdated)
  46. (1) You need perl5.002 or later.
  47. (2) If you want to compile and run programs with the C or CC backends
  48. which undefine (or redefine) subroutines, then you need to apply a
  49. one-line patch to perl itself. One or two of the programs in perl's
  50. own test suite do this. The patch is in file op.patch. It prevents
  51. perl from calling free() on OPs with the magic sequence number (U16)-1.
  52. The compiler declares all OPs as static structures and uses that magic
  53. sequence number.
  54. (3) Type
  55. perl Makefile.PL
  56. to write a personalised Makefile for your system. If you want the
  57. bytecode modules to support reading bytecode from strings (instead of
  58. just from files) then add the option
  59. -DINDIRECT_BGET_MACROS
  60. into the middle of the definition of the CCCMD macro in the Makefile.
  61. Your C compiler may need to be able to cope with Standard C for this.
  62. I haven't tested this option yet with an old pre-Standard compiler.
  63. (4) If your platform supports dynamic loading then just type
  64. make
  65. and you can then use
  66. perl -Iblib/arch -MO=foo bar
  67. to use the compiler modules (see later for details).
  68. If you need/want instead to make a statically linked perl which
  69. contains the appropriate modules, then type
  70. make perl
  71. make byteperl
  72. and you can then use
  73. ./perl -MO=foo bar
  74. to use the compiler modules.
  75. In both cases, the byteperl executable is required for running standalone
  76. bytecode programs. It is *not* a standard perl+XSUB perl executable.
  77. USAGE
  78. As of the alpha3 release, the Bytecode, C and CC backends are now all
  79. functional enough to compile almost the whole of the main perl test
  80. suite. In the case of the CC backend, any failures are all due to
  81. differences and/or known bugs documented below. See the file TESTS.
  82. In the following examples, you'll need to replace "perl" by
  83. perl -Iblib/arch
  84. if you have built the extensions for a dynamic loading platform but
  85. haven't installed the extensions completely. You'll need to replace
  86. "perl" by
  87. ./perl
  88. if you have built the extensions into a statically linked perl binary.
  89. (1) To compile perl program foo.pl with the C backend, do
  90. perl -MO=C,-ofoo.c foo.pl
  91. Then use the cc_harness perl program to compile the resulting C source:
  92. perl cc_harness -O2 -o foo foo.c
  93. If you are using a non-ANSI pre-Standard C compiler that can't handle
  94. pre-declaring static arrays, then add -DBROKEN_STATIC_REDECL to the
  95. options you use:
  96. perl cc_harness -O2 -o foo -DBROKEN_STATIC_REDECL foo.c
  97. If you are using a non-ANSI pre-Standard C compiler that can't handle
  98. static initialisation of structures with union members then add
  99. -DBROKEN_UNION_INIT to the options you use. If you want command line
  100. arguments passed to your executable to be interpreted by perl (e.g. -Dx)
  101. then compile foo.c with -DALLOW_PERL_OPTIONS. Otherwise, all command line
  102. arguments passed to foo will appear directly in @ARGV. The resulting
  103. executable foo is the compiled version of foo.pl. See the file NOTES for
  104. extra options you can pass to -MO=C.
  105. There are some constraints on the contents on foo.pl if you want to be
  106. able to compile it successfully. Some problems can be fixed fairly easily
  107. by altering foo.pl; some problems with the compiler are known to be
  108. straightforward to solve and I'll do so soon. The file Todo lists a
  109. number of known problems. See the XSUB section lower down for information
  110. about compiling programs which use XSUBs.
  111. (2) To compile foo.pl with the CC backend (which generates actual
  112. optimised C code for the execution path of your perl program), use
  113. perl -MO=CC,-ofoo.c foo.pl
  114. and proceed just as with the C backend. You should almost certainly
  115. use an option such as -O2 with the subsequent cc_harness invocation
  116. so that your C compiler uses optimisation. The C code generated by
  117. the Perl compiler's CC backend looks ugly to humans but is easily
  118. optimised by C compilers.
  119. To make the most of this compiler backend, you need to tell the
  120. compiler when you're using int or double variables so that it can
  121. optimise appropriately (although this part of the compiler is the most
  122. buggy). You currently do that by naming lexical variables ending in
  123. "_i" for ints, "_d" for doubles, "_ir" for int "register" variables or
  124. "_dr" for double "register" variables. Here "register" is a promise
  125. that you won't pass a reference to the variable into a sub which then
  126. modifies the variable. The compiler ought to catch attempts to use
  127. "\$i" just as C compilers catch attempts to do "&i" for a
  128. register int i but it doesn't at the moment. Bugs in the CC backend
  129. may make your program fail in mysterious ways and give wrong answers
  130. rather than just crash in boring ways. But, hey, this is an alpha release
  131. so you knew that anyway. See the XSUB section lower down for information
  132. about compiling programs which use XSUBs.
  133. If your program uses classes which define methods (or other subs which
  134. are not exported and not apparently used until runtime) then you'll
  135. need to use -u compile-time options (see the NOTES file) to force the
  136. subs to be compiled. Future releases will probably default the other
  137. way, do more auto-detection and provide more fine-grained control.
  138. Since compiled executables need linking with libperl, you may want
  139. to turn libperl.a into a shared library if your platform supports
  140. it. For example, with Digital UNIX, do something like
  141. ld -shared -o libperl.so -all libperl.a -none -lc
  142. and with Linux/ELF, rebuild the perl .c files with -fPIC (and I
  143. also suggest -fomit-frame-pointer for Linux on Intel architetcures),
  144. do "make libperl.a" and then do
  145. gcc -shared -Wl,-soname,libperl.so.5 -o libperl.so.5.3 `ar t libperl.a`
  146. and then
  147. # cp libperl.so.5.3 /usr/lib
  148. # cd /usr/lib
  149. # ln -s libperl.so.5.3 libperl.so.5
  150. # ln -s libperl.so.5 libperl.so
  151. # ldconfig
  152. When you compile perl executables with cc_harness, append -L/usr/lib
  153. otherwise the -L for the perl source directory will override it. For
  154. example,
  155. perl -Iblib/arch -MO=CC,-O2,-ofoo3.c foo3.bench
  156. perl cc_harness -o foo3 -O2 foo3.c -L/usr/lib
  157. ls -l foo3
  158. -rwxr-xr-x 1 mbeattie xzdg 11218 Jul 1 15:28 foo3
  159. You'll probably also want to link your main perl executable against
  160. libperl.so; it's nice having an 11K perl executable.
  161. (3) To compile foo.pl into bytecode do
  162. perl -MO=Bytecode,-ofoo foo.pl
  163. To run the resulting bytecode file foo as a standalone program, you
  164. use the program byteperl which should have been built along with the
  165. extensions.
  166. ./byteperl foo
  167. Any extra arguments are passed in as @ARGV; they are not interpreted
  168. as perl options. If you want to load chunks of bytecode into an already
  169. running perl program then use the -m option and investigate the
  170. byteload_fh and byteload_string functions exported by the B module.
  171. See the NOTES file for details of these and other options (including
  172. optimisation options and ways of getting at the intermediate "assembler"
  173. code that the Bytecode backend uses).
  174. (3) There are little Bourne shell scripts and perl programs to aid with
  175. some common operations: assemble, disassemble, run_bytecode_test,
  176. run_test, cc_harness, test_harness, test_harness_bytecode.
  177. (4) Walk the op tree in execution order printing terse info about each op
  178. perl -MO=Terse,exec foo.pl
  179. (5) Walk the op tree in syntax order printing lengthier debug info about
  180. each op. You can also append ",exec" to walk in execution order, but the
  181. formatting is designed to look nice with Terse rather than Debug.
  182. perl -MO=Debug foo.pl
  183. (6) Produce a cross-reference report of the line numbers at which all
  184. variables, subs and formats are defined and used.
  185. perl -MO=Xref foo.pl
  186. XSUBS
  187. The C and CC backends can successfully compile some perl programs which
  188. make use of XSUB extensions. [I'll add more detail to this section in a
  189. later release.] As a prerequisite, such extensions must not need to do
  190. anything in their BOOT: section which needs to be done at runtime rather
  191. than compile time. Normally, the only code in the boot_Foo() function is
  192. a list of newXS() calls which xsubpp puts there and the compiler handles
  193. saving those XS subs itself. For each XSUB used, the C and CC compiler
  194. will generate an initialiser in their C output which refers to the name
  195. of the relevant C function (XS_Foo_somesub). What is not yet automated
  196. is the necessary commands and cc command-line options (e.g. via
  197. "perl cc_harness") which link against the extension libraries. For now,
  198. you need the XSUB extension to have installed files in the right format
  199. for using as C libraries (e.g. Foo.a or Foo.so). As the Foo.so files (or
  200. your platform's version) aren't suitable for linking against, you will
  201. have to reget the extension source and rebuild it as a static extension
  202. to force the generation of a suitable Foo.a file. Then you need to make
  203. a symlink (or copy or rename) of that file into a libFoo.a suitable for
  204. cc linking. Then add the appropriate -L and -l options to your
  205. "perl cc_harness" command line to find and link against those libraries.
  206. You may also need to fix up some platform-dependent environment variable
  207. to ensure that linked-against .so files are found at runtime too.
  208. DIFFERENCES
  209. The result of running a compiled Perl program can sometimes be different
  210. from running the same program with standard perl. Think of the compiler
  211. as having a slightly different implementation of the language Perl.
  212. Unfortunately, since Perl has had a single implementation until now,
  213. there are no formal standards or documents defining what behaviour is
  214. guaranteed of Perl the language and what just "happens to work".
  215. Some of the differences below are almost impossible to change because of
  216. the way the compiler works. Others can be changed to produce "standard"
  217. perl behaviour if it's deemed proper and the resulting performance hit
  218. is accepted. I'll use "standard perl" to mean the result of running a
  219. Perl program using the perl executable from the perl distribution.
  220. I'll use "compiled Perl program" to mean running an executable produced
  221. by this compiler kit ("the compiler") with the CC backend.
  222. Loops
  223. Standard perl calculates the target of "next", "last", and "redo"
  224. at run-time. The compiler calculates the targets at compile-time.
  225. For example, the program
  226. sub skip_on_odd { next NUMBER if $_[0] % 2 }
  227. NUMBER: for ($i = 0; $i < 5; $i++) {
  228. skip_on_odd($i);
  229. print $i;
  230. }
  231. produces the output
  232. 024
  233. with standard perl but gives a compile-time error with the compiler.
  234. Context of ".."
  235. The context (scalar or array) of the ".." operator determines whether
  236. it behaves as a range or a flip/flop. Standard perl delays until
  237. runtime the decision of which context it is in but the compiler needs
  238. to know the context at compile-time. For example,
  239. @a = (4,6,1,0,0,1);
  240. sub range { (shift @a)..(shift @a) }
  241. print range();
  242. while (@a) { print scalar(range()) }
  243. generates the output
  244. 456123E0
  245. with standard Perl but gives a compile-time error with compiled Perl.
  246. Arithmetic
  247. Compiled Perl programs use native C arithemtic much more frequently
  248. than standard perl. Operations on large numbers or on boundary
  249. cases may produce different behaviour.
  250. Deprecated features
  251. Features of standard perl such as $[ which have been deprecated
  252. in standard perl since version 5 was released have not been
  253. implemented in the compiler.
  254. Others
  255. I'll add to this list as I remember what they are.
  256. BUGS
  257. Here are some things which may cause the compiler problems.
  258. The following render the compiler useless (without serious hacking):
  259. * Use of the DATA filehandle (via __END__ or __DATA__ tokens)
  260. * Operator overloading with %OVERLOAD
  261. * The (deprecated) magic array-offset variable $[ does not work
  262. * The following operators are not yet implemented for CC
  263. goto
  264. sort with a non-default comparison (i.e. a named sub or inline block)
  265. * You can't use "last" to exit from a non-loop block.
  266. The following may give significant problems:
  267. * BEGIN blocks containing complex initialisation code
  268. * Code which is only ever referred to at runtime (e.g. via eval "..." or
  269. via method calls): see the -u option for the C and CC backends.
  270. * Run-time lookups of lexical variables in "outside" closures
  271. The following may cause problems (not thoroughly tested):
  272. * Dependencies on whether values of some "magic" Perl variables are
  273. determined at compile-time or runtime.
  274. * For the C and CC backends: compile-time strings which are longer than
  275. your C compiler can cope with in a single line or definition.
  276. * Reliance on intimate details of global destruction
  277. * For the Bytecode backend: high -On optimisation numbers with code
  278. that has complex flow of control.
  279. * Any "-w" option in the first line of your perl program is seen and
  280. acted on by perl itself before the compiler starts. The compiler
  281. itself then runs with warnings turned on. This may cause perl to
  282. print out warnings about the compiler itself since I haven't tested
  283. it thoroughly with warnings turned on.
  284. There is a terser but more complete list in the Todo file.
  285. Malcolm Beattie
  286. 2 September 1996