Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

908 lines
22 KiB

  1. # Bytecode.pm
  2. #
  3. # Copyright (c) 1996-1998 Malcolm Beattie
  4. #
  5. # You may distribute under the terms of either the GNU General Public
  6. # License or the Artistic License, as specified in the README file.
  7. #
  8. package B::Bytecode;
  9. use strict;
  10. use Carp;
  11. use IO::File;
  12. use B qw(minus_c main_cv main_root main_start comppadlist
  13. class peekop walkoptree svref_2object cstring walksymtable);
  14. use B::Asmdata qw(@optype @specialsv_name);
  15. use B::Assembler qw(assemble_fh);
  16. my %optype_enum;
  17. my $i;
  18. for ($i = 0; $i < @optype; $i++) {
  19. $optype_enum{$optype[$i]} = $i;
  20. }
  21. # Following is SVf_POK|SVp_POK
  22. # XXX Shouldn't be hardwired
  23. sub POK () { 0x04040000 }
  24. # Following is SVf_IOK|SVp_OK
  25. # XXX Shouldn't be hardwired
  26. sub IOK () { 0x01010000 }
  27. my ($verbose, $module_only, $no_assemble, $debug_bc, $debug_cv);
  28. my $assembler_pid;
  29. # Optimisation options. On the command line, use hyphens instead of
  30. # underscores for compatibility with gcc-style options. We use
  31. # underscores here because they are OK in (strict) barewords.
  32. my ($strip_syntree, $compress_nullops, $omit_seq, $bypass_nullops);
  33. my %optimise = (strip_syntax_tree => \$strip_syntree,
  34. compress_nullops => \$compress_nullops,
  35. omit_sequence_numbers => \$omit_seq,
  36. bypass_nullops => \$bypass_nullops);
  37. my $nextix = 0;
  38. my %symtable; # maps object addresses to object indices.
  39. # Filled in at allocation (newsv/newop) time.
  40. my %saved; # maps object addresses (for SVish classes) to "saved yet?"
  41. # flag. Set at FOO::bytecode time usually by SV::bytecode.
  42. # Manipulated via saved(), mark_saved(), unmark_saved().
  43. my $svix = -1; # we keep track of when the sv register contains an element
  44. # of the object table to avoid unnecessary repeated
  45. # consecutive ldsv instructions.
  46. my $opix = -1; # Ditto for the op register.
  47. sub ldsv {
  48. my $ix = shift;
  49. if ($ix != $svix) {
  50. print "ldsv $ix\n";
  51. $svix = $ix;
  52. }
  53. }
  54. sub stsv {
  55. my $ix = shift;
  56. print "stsv $ix\n";
  57. $svix = $ix;
  58. }
  59. sub set_svix {
  60. $svix = shift;
  61. }
  62. sub ldop {
  63. my $ix = shift;
  64. if ($ix != $opix) {
  65. print "ldop $ix\n";
  66. $opix = $ix;
  67. }
  68. }
  69. sub stop {
  70. my $ix = shift;
  71. print "stop $ix\n";
  72. $opix = $ix;
  73. }
  74. sub set_opix {
  75. $opix = shift;
  76. }
  77. sub pvstring {
  78. my $str = shift;
  79. if (defined($str)) {
  80. return cstring($str . "\0");
  81. } else {
  82. return '""';
  83. }
  84. }
  85. sub saved { $saved{${$_[0]}} }
  86. sub mark_saved { $saved{${$_[0]}} = 1 }
  87. sub unmark_saved { $saved{${$_[0]}} = 0 }
  88. sub debug { $debug_bc = shift }
  89. sub B::OBJECT::nyi {
  90. my $obj = shift;
  91. warn sprintf("bytecode save method for %s (0x%x) not yet implemented\n",
  92. class($obj), $$obj);
  93. }
  94. #
  95. # objix may stomp on the op register (for op objects)
  96. # or the sv register (for SV objects)
  97. #
  98. sub B::OBJECT::objix {
  99. my $obj = shift;
  100. my $ix = $symtable{$$obj};
  101. if (defined($ix)) {
  102. return $ix;
  103. } else {
  104. $obj->newix($nextix);
  105. return $symtable{$$obj} = $nextix++;
  106. }
  107. }
  108. sub B::SV::newix {
  109. my ($sv, $ix) = @_;
  110. printf "newsv %d\t# %s\n", $sv->FLAGS & 0xf, class($sv);
  111. stsv($ix);
  112. }
  113. sub B::GV::newix {
  114. my ($gv, $ix) = @_;
  115. my $gvname = $gv->NAME;
  116. my $name = cstring($gv->STASH->NAME . "::" . $gvname);
  117. print "gv_fetchpv $name\n";
  118. stsv($ix);
  119. }
  120. sub B::HV::newix {
  121. my ($hv, $ix) = @_;
  122. my $name = $hv->NAME;
  123. if ($name) {
  124. # It's a stash
  125. printf "gv_stashpv %s\n", cstring($name);
  126. stsv($ix);
  127. } else {
  128. # It's an ordinary HV. Fall back to ordinary newix method
  129. $hv->B::SV::newix($ix);
  130. }
  131. }
  132. sub B::SPECIAL::newix {
  133. my ($sv, $ix) = @_;
  134. # Special case. $$sv is not the address of the SV but an
  135. # index into svspecialsv_list.
  136. printf "ldspecsv $$sv\t# %s\n", $specialsv_name[$$sv];
  137. stsv($ix);
  138. }
  139. sub B::OP::newix {
  140. my ($op, $ix) = @_;
  141. my $class = class($op);
  142. my $typenum = $optype_enum{$class};
  143. croak "OP::newix: can't understand class $class" unless defined($typenum);
  144. print "newop $typenum\t# $class\n";
  145. stop($ix);
  146. }
  147. sub B::OP::walkoptree_debug {
  148. my $op = shift;
  149. warn(sprintf("walkoptree: %s\n", peekop($op)));
  150. }
  151. sub B::OP::bytecode {
  152. my $op = shift;
  153. my $next = $op->next;
  154. my $nextix;
  155. my $sibix = $op->sibling->objix;
  156. my $ix = $op->objix;
  157. my $type = $op->type;
  158. if ($bypass_nullops) {
  159. $next = $next->next while $$next && $next->type == 0;
  160. }
  161. $nextix = $next->objix;
  162. printf "# %s\n", peekop($op) if $debug_bc;
  163. ldop($ix);
  164. print "op_next $nextix\n";
  165. print "op_sibling $sibix\n" unless $strip_syntree;
  166. printf "op_type %s\t# %d\n", $op->ppaddr, $type;
  167. printf("op_seq %d\n", $op->seq) unless $omit_seq;
  168. if ($type || !$compress_nullops) {
  169. printf "op_targ %d\nop_flags 0x%x\nop_private 0x%x\n",
  170. $op->targ, $op->flags, $op->private;
  171. }
  172. }
  173. sub B::UNOP::bytecode {
  174. my $op = shift;
  175. my $firstix = $op->first->objix;
  176. $op->B::OP::bytecode;
  177. if (($op->type || !$compress_nullops) && !$strip_syntree) {
  178. print "op_first $firstix\n";
  179. }
  180. }
  181. sub B::LOGOP::bytecode {
  182. my $op = shift;
  183. my $otherix = $op->other->objix;
  184. $op->B::UNOP::bytecode;
  185. print "op_other $otherix\n";
  186. }
  187. sub B::SVOP::bytecode {
  188. my $op = shift;
  189. my $sv = $op->sv;
  190. my $svix = $sv->objix;
  191. $op->B::OP::bytecode;
  192. print "op_sv $svix\n";
  193. $sv->bytecode;
  194. }
  195. sub B::GVOP::bytecode {
  196. my $op = shift;
  197. my $gv = $op->gv;
  198. my $gvix = $gv->objix;
  199. $op->B::OP::bytecode;
  200. print "op_gv $gvix\n";
  201. $gv->bytecode;
  202. }
  203. sub B::PVOP::bytecode {
  204. my $op = shift;
  205. my $pv = $op->pv;
  206. $op->B::OP::bytecode;
  207. #
  208. # This would be easy except that OP_TRANS uses a PVOP to store an
  209. # endian-dependent array of 256 shorts instead of a plain string.
  210. #
  211. if ($op->ppaddr eq "pp_trans") {
  212. my @shorts = unpack("s256", $pv); # assembler handles endianness
  213. print "op_pv_tr ", join(",", @shorts), "\n";
  214. } else {
  215. printf "newpv %s\nop_pv\n", pvstring($pv);
  216. }
  217. }
  218. sub B::BINOP::bytecode {
  219. my $op = shift;
  220. my $lastix = $op->last->objix;
  221. $op->B::UNOP::bytecode;
  222. if (($op->type || !$compress_nullops) && !$strip_syntree) {
  223. print "op_last $lastix\n";
  224. }
  225. }
  226. sub B::CONDOP::bytecode {
  227. my $op = shift;
  228. my $trueix = $op->true->objix;
  229. my $falseix = $op->false->objix;
  230. $op->B::UNOP::bytecode;
  231. print "op_true $trueix\nop_false $falseix\n";
  232. }
  233. sub B::LISTOP::bytecode {
  234. my $op = shift;
  235. my $children = $op->children;
  236. $op->B::BINOP::bytecode;
  237. if (($op->type || !$compress_nullops) && !$strip_syntree) {
  238. print "op_children $children\n";
  239. }
  240. }
  241. sub B::LOOP::bytecode {
  242. my $op = shift;
  243. my $redoopix = $op->redoop->objix;
  244. my $nextopix = $op->nextop->objix;
  245. my $lastopix = $op->lastop->objix;
  246. $op->B::LISTOP::bytecode;
  247. print "op_redoop $redoopix\nop_nextop $nextopix\nop_lastop $lastopix\n";
  248. }
  249. sub B::COP::bytecode {
  250. my $op = shift;
  251. my $stash = $op->stash;
  252. my $stashix = $stash->objix;
  253. my $filegv = $op->filegv;
  254. my $filegvix = $filegv->objix;
  255. my $line = $op->line;
  256. if ($debug_bc) {
  257. printf "# line %s:%d\n", $filegv->SV->PV, $line;
  258. }
  259. $op->B::OP::bytecode;
  260. printf <<"EOT", pvstring($op->label), $op->cop_seq, $op->arybase;
  261. newpv %s
  262. cop_label
  263. cop_stash $stashix
  264. cop_seq %d
  265. cop_filegv $filegvix
  266. cop_arybase %d
  267. cop_line $line
  268. EOT
  269. $filegv->bytecode;
  270. $stash->bytecode;
  271. }
  272. sub B::PMOP::bytecode {
  273. my $op = shift;
  274. my $replroot = $op->pmreplroot;
  275. my $replrootix = $replroot->objix;
  276. my $replstartix = $op->pmreplstart->objix;
  277. my $ppaddr = $op->ppaddr;
  278. # pmnext is corrupt in some PMOPs (see misc.t for example)
  279. #my $pmnextix = $op->pmnext->objix;
  280. if ($$replroot) {
  281. # OP_PUSHRE (a mutated version of OP_MATCH for the regexp
  282. # argument to a split) stores a GV in op_pmreplroot instead
  283. # of a substitution syntax tree. We don't want to walk that...
  284. if ($ppaddr eq "pp_pushre") {
  285. $replroot->bytecode;
  286. } else {
  287. walkoptree($replroot, "bytecode");
  288. }
  289. }
  290. $op->B::LISTOP::bytecode;
  291. if ($ppaddr eq "pp_pushre") {
  292. printf "op_pmreplrootgv $replrootix\n";
  293. } else {
  294. print "op_pmreplroot $replrootix\nop_pmreplstart $replstartix\n";
  295. }
  296. my $re = pvstring($op->precomp);
  297. # op_pmnext omitted since a perl bug means it's sometime corrupt
  298. printf <<"EOT", $op->pmflags, $op->pmpermflags;
  299. op_pmflags 0x%x
  300. op_pmpermflags 0x%x
  301. newpv $re
  302. pregcomp
  303. EOT
  304. }
  305. sub B::SV::bytecode {
  306. my $sv = shift;
  307. return if saved($sv);
  308. my $ix = $sv->objix;
  309. my $refcnt = $sv->REFCNT;
  310. my $flags = sprintf("0x%x", $sv->FLAGS);
  311. ldsv($ix);
  312. print "sv_refcnt $refcnt\nsv_flags $flags\n";
  313. mark_saved($sv);
  314. }
  315. sub B::PV::bytecode {
  316. my $sv = shift;
  317. return if saved($sv);
  318. $sv->B::SV::bytecode;
  319. printf("newpv %s\nxpv\n", pvstring($sv->PV)) if $sv->FLAGS & POK;
  320. }
  321. sub B::IV::bytecode {
  322. my $sv = shift;
  323. return if saved($sv);
  324. my $iv = $sv->IVX;
  325. $sv->B::SV::bytecode;
  326. printf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32";
  327. }
  328. sub B::NV::bytecode {
  329. my $sv = shift;
  330. return if saved($sv);
  331. $sv->B::SV::bytecode;
  332. printf "xnv %s\n", $sv->NVX;
  333. }
  334. sub B::RV::bytecode {
  335. my $sv = shift;
  336. return if saved($sv);
  337. my $rv = $sv->RV;
  338. my $rvix = $rv->objix;
  339. $rv->bytecode;
  340. $sv->B::SV::bytecode;
  341. print "xrv $rvix\n";
  342. }
  343. sub B::PVIV::bytecode {
  344. my $sv = shift;
  345. return if saved($sv);
  346. my $iv = $sv->IVX;
  347. $sv->B::PV::bytecode;
  348. printf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32";
  349. }
  350. sub B::PVNV::bytecode {
  351. my ($sv, $flag) = @_;
  352. # The $flag argument is passed through PVMG::bytecode by BM::bytecode
  353. # and AV::bytecode and indicates special handling. $flag = 1 is used by
  354. # BM::bytecode and means that we should ensure we save the whole B-M
  355. # table. It consists of 257 bytes (256 char array plus a final \0)
  356. # which follow the ordinary PV+\0 and the 257 bytes are *not* reflected
  357. # in SvCUR. $flag = 2 is used by AV::bytecode and means that we only
  358. # call SV::bytecode instead of saving PV and calling NV::bytecode since
  359. # PV/NV/IV stuff is different for AVs.
  360. return if saved($sv);
  361. if ($flag == 2) {
  362. $sv->B::SV::bytecode;
  363. } else {
  364. my $pv = $sv->PV;
  365. $sv->B::IV::bytecode;
  366. printf "xnv %s\n", $sv->NVX;
  367. if ($flag == 1) {
  368. $pv .= "\0" . $sv->TABLE;
  369. printf "newpv %s\npv_cur %d\nxpv\n", pvstring($pv),length($pv)-257;
  370. } else {
  371. printf("newpv %s\nxpv\n", pvstring($pv)) if $sv->FLAGS & POK;
  372. }
  373. }
  374. }
  375. sub B::PVMG::bytecode {
  376. my ($sv, $flag) = @_;
  377. # See B::PVNV::bytecode for an explanation of $flag.
  378. return if saved($sv);
  379. # XXX We assume SvSTASH is already saved and don't save it later ourselves
  380. my $stashix = $sv->SvSTASH->objix;
  381. my @mgchain = $sv->MAGIC;
  382. my (@mgobjix, $mg);
  383. #
  384. # We need to traverse the magic chain and get objix for each OBJ
  385. # field *before* we do B::PVNV::bytecode since objix overwrites
  386. # the sv register. However, we need to write the magic-saving
  387. # bytecode *after* B::PVNV::bytecode since sv isn't initialised
  388. # to refer to $sv until then.
  389. #
  390. @mgobjix = map($_->OBJ->objix, @mgchain);
  391. $sv->B::PVNV::bytecode($flag);
  392. print "xmg_stash $stashix\n";
  393. foreach $mg (@mgchain) {
  394. printf "sv_magic %s\nmg_obj %d\nnewpv %s\nmg_pv\n",
  395. cstring($mg->TYPE), shift(@mgobjix), pvstring($mg->PTR);
  396. }
  397. }
  398. sub B::PVLV::bytecode {
  399. my $sv = shift;
  400. return if saved($sv);
  401. $sv->B::PVMG::bytecode;
  402. printf <<'EOT', $sv->TARGOFF, $sv->TARGLEN, cstring($sv->TYPE);
  403. xlv_targoff %d
  404. xlv_targlen %d
  405. xlv_type %s
  406. EOT
  407. }
  408. sub B::BM::bytecode {
  409. my $sv = shift;
  410. return if saved($sv);
  411. # See PVNV::bytecode for an explanation of what the argument does
  412. $sv->B::PVMG::bytecode(1);
  413. printf "xbm_useful %d\nxbm_previous %d\nxbm_rare %d\n",
  414. $sv->USEFUL, $sv->PREVIOUS, $sv->RARE;
  415. }
  416. sub B::GV::bytecode {
  417. my $gv = shift;
  418. return if saved($gv);
  419. my $ix = $gv->objix;
  420. mark_saved($gv);
  421. my $gvname = $gv->NAME;
  422. my $name = cstring($gv->STASH->NAME . "::" . $gvname);
  423. my $egv = $gv->EGV;
  424. my $egvix = $egv->objix;
  425. ldsv($ix);
  426. printf <<"EOT", $gv->FLAGS, $gv->GvFLAGS, $gv->LINE;
  427. sv_flags 0x%x
  428. xgv_flags 0x%x
  429. gp_line %d
  430. EOT
  431. my $refcnt = $gv->REFCNT;
  432. printf("sv_refcnt_add %d\n", $refcnt - 1) if $refcnt > 1;
  433. my $gvrefcnt = $gv->GvREFCNT;
  434. printf("gp_refcnt_add %d\n", $gvrefcnt - 1) if $gvrefcnt > 1;
  435. if ($gvrefcnt > 1 && $ix != $egvix) {
  436. print "gp_share $egvix\n";
  437. } else {
  438. if ($gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/) {
  439. my $i;
  440. my @subfield_names = qw(SV AV HV CV FILEGV FORM IO);
  441. my @subfields = map($gv->$_(), @subfield_names);
  442. my @ixes = map($_->objix, @subfields);
  443. # Reset sv register for $gv
  444. ldsv($ix);
  445. for ($i = 0; $i < @ixes; $i++) {
  446. printf "gp_%s %d\n", lc($subfield_names[$i]), $ixes[$i];
  447. }
  448. # Now save all the subfields
  449. my $sv;
  450. foreach $sv (@subfields) {
  451. $sv->bytecode;
  452. }
  453. }
  454. }
  455. }
  456. sub B::HV::bytecode {
  457. my $hv = shift;
  458. return if saved($hv);
  459. mark_saved($hv);
  460. my $name = $hv->NAME;
  461. my $ix = $hv->objix;
  462. if (!$name) {
  463. # It's an ordinary HV. Stashes have NAME set and need no further
  464. # saving beyond the gv_stashpv that $hv->objix already ensures.
  465. my @contents = $hv->ARRAY;
  466. my ($i, @ixes);
  467. for ($i = 1; $i < @contents; $i += 2) {
  468. push(@ixes, $contents[$i]->objix);
  469. }
  470. for ($i = 1; $i < @contents; $i += 2) {
  471. $contents[$i]->bytecode;
  472. }
  473. ldsv($ix);
  474. for ($i = 0; $i < @contents; $i += 2) {
  475. printf("newpv %s\nhv_store %d\n",
  476. pvstring($contents[$i]), $ixes[$i / 2]);
  477. }
  478. printf "sv_refcnt %d\nsv_flags 0x%x\n", $hv->REFCNT, $hv->FLAGS;
  479. }
  480. }
  481. sub B::AV::bytecode {
  482. my $av = shift;
  483. return if saved($av);
  484. my $ix = $av->objix;
  485. my $fill = $av->FILL;
  486. my $max = $av->MAX;
  487. my (@array, @ixes);
  488. if ($fill > -1) {
  489. @array = $av->ARRAY;
  490. @ixes = map($_->objix, @array);
  491. my $sv;
  492. foreach $sv (@array) {
  493. $sv->bytecode;
  494. }
  495. }
  496. # See PVNV::bytecode for the meaning of the flag argument of 2.
  497. $av->B::PVMG::bytecode(2);
  498. # Recover sv register and set AvMAX and AvFILL to -1 (since we
  499. # create an AV with NEWSV and SvUPGRADE rather than doing newAV
  500. # which is what sets AvMAX and AvFILL.
  501. ldsv($ix);
  502. printf "xav_flags 0x%x\nxav_max -1\nxav_fill -1\n", $av->AvFLAGS;
  503. if ($fill > -1) {
  504. my $elix;
  505. foreach $elix (@ixes) {
  506. print "av_push $elix\n";
  507. }
  508. } else {
  509. if ($max > -1) {
  510. print "av_extend $max\n";
  511. }
  512. }
  513. }
  514. sub B::CV::bytecode {
  515. my $cv = shift;
  516. return if saved($cv);
  517. my $ix = $cv->objix;
  518. $cv->B::PVMG::bytecode;
  519. my $i;
  520. my @subfield_names = qw(ROOT START STASH GV FILEGV PADLIST OUTSIDE);
  521. my @subfields = map($cv->$_(), @subfield_names);
  522. my @ixes = map($_->objix, @subfields);
  523. # Save OP tree from CvROOT (first element of @subfields)
  524. my $root = shift @subfields;
  525. if ($$root) {
  526. walkoptree($root, "bytecode");
  527. }
  528. # Reset sv register for $cv (since above ->objix calls stomped on it)
  529. ldsv($ix);
  530. for ($i = 0; $i < @ixes; $i++) {
  531. printf "xcv_%s %d\n", lc($subfield_names[$i]), $ixes[$i];
  532. }
  533. printf "xcv_depth %d\nxcv_flags 0x%x\n", $cv->DEPTH, $cv->FLAGS;
  534. # Now save all the subfields (except for CvROOT which was handled
  535. # above) and CvSTART (now the initial element of @subfields).
  536. shift @subfields; # bye-bye CvSTART
  537. my $sv;
  538. foreach $sv (@subfields) {
  539. $sv->bytecode;
  540. }
  541. }
  542. sub B::IO::bytecode {
  543. my $io = shift;
  544. return if saved($io);
  545. my $ix = $io->objix;
  546. my $top_gv = $io->TOP_GV;
  547. my $top_gvix = $top_gv->objix;
  548. my $fmt_gv = $io->FMT_GV;
  549. my $fmt_gvix = $fmt_gv->objix;
  550. my $bottom_gv = $io->BOTTOM_GV;
  551. my $bottom_gvix = $bottom_gv->objix;
  552. $io->B::PVMG::bytecode;
  553. ldsv($ix);
  554. print "xio_top_gv $top_gvix\n";
  555. print "xio_fmt_gv $fmt_gvix\n";
  556. print "xio_bottom_gv $bottom_gvix\n";
  557. my $field;
  558. foreach $field (qw(TOP_NAME FMT_NAME BOTTOM_NAME)) {
  559. printf "newpv %s\nxio_%s\n", pvstring($io->$field()), lc($field);
  560. }
  561. foreach $field (qw(LINES PAGE PAGE_LEN LINES_LEFT SUBPROCESS)) {
  562. printf "xio_%s %d\n", lc($field), $io->$field();
  563. }
  564. printf "xio_type %s\nxio_flags 0x%x\n", cstring($io->IoTYPE), $io->IoFLAGS;
  565. $top_gv->bytecode;
  566. $fmt_gv->bytecode;
  567. $bottom_gv->bytecode;
  568. }
  569. sub B::SPECIAL::bytecode {
  570. # nothing extra needs doing
  571. }
  572. sub bytecompile_object {
  573. my $sv;
  574. foreach $sv (@_) {
  575. svref_2object($sv)->bytecode;
  576. }
  577. }
  578. sub B::GV::bytecodecv {
  579. my $gv = shift;
  580. my $cv = $gv->CV;
  581. if ($$cv && !saved($cv)) {
  582. if ($debug_cv) {
  583. warn sprintf("saving extra CV &%s::%s (0x%x) from GV 0x%x\n",
  584. $gv->STASH->NAME, $gv->NAME, $$cv, $$gv);
  585. }
  586. $gv->bytecode;
  587. }
  588. }
  589. sub bytecompile_main {
  590. my $curpad = (comppadlist->ARRAY)[1];
  591. my $curpadix = $curpad->objix;
  592. $curpad->bytecode;
  593. walkoptree(main_root, "bytecode");
  594. warn "done main program, now walking symbol table\n" if $debug_bc;
  595. my ($pack, %exclude);
  596. foreach $pack (qw(B O AutoLoader DynaLoader Config DB VMS strict vars
  597. FileHandle Exporter Carp UNIVERSAL IO Fcntl Symbol
  598. SelectSaver blib Cwd))
  599. {
  600. $exclude{$pack."::"} = 1;
  601. }
  602. no strict qw(vars refs);
  603. walksymtable(\%{"main::"}, "bytecodecv", sub {
  604. warn "considering $_[0]\n" if $debug_bc;
  605. return !defined($exclude{$_[0]});
  606. });
  607. if (!$module_only) {
  608. printf "main_root %d\n", main_root->objix;
  609. printf "main_start %d\n", main_start->objix;
  610. printf "curpad $curpadix\n";
  611. # XXX Do min_intro_pending and max_intro_pending matter?
  612. }
  613. }
  614. sub prepare_assemble {
  615. my $newfh = IO::File->new_tmpfile;
  616. select($newfh);
  617. binmode $newfh;
  618. return $newfh;
  619. }
  620. sub do_assemble {
  621. my $fh = shift;
  622. seek($fh, 0, 0); # rewind the temporary file
  623. assemble_fh($fh, sub { print OUT @_ });
  624. }
  625. sub compile {
  626. my @options = @_;
  627. my ($option, $opt, $arg);
  628. open(OUT, ">&STDOUT");
  629. binmode OUT;
  630. select(OUT);
  631. OPTION:
  632. while ($option = shift @options) {
  633. if ($option =~ /^-(.)(.*)/) {
  634. $opt = $1;
  635. $arg = $2;
  636. } else {
  637. unshift @options, $option;
  638. last OPTION;
  639. }
  640. if ($opt eq "-" && $arg eq "-") {
  641. shift @options;
  642. last OPTION;
  643. } elsif ($opt eq "o") {
  644. $arg ||= shift @options;
  645. open(OUT, ">$arg") or return "$arg: $!\n";
  646. binmode OUT;
  647. } elsif ($opt eq "D") {
  648. $arg ||= shift @options;
  649. foreach $arg (split(//, $arg)) {
  650. if ($arg eq "b") {
  651. $| = 1;
  652. debug(1);
  653. } elsif ($arg eq "o") {
  654. B->debug(1);
  655. } elsif ($arg eq "a") {
  656. B::Assembler::debug(1);
  657. } elsif ($arg eq "C") {
  658. $debug_cv = 1;
  659. }
  660. }
  661. } elsif ($opt eq "v") {
  662. $verbose = 1;
  663. } elsif ($opt eq "m") {
  664. $module_only = 1;
  665. } elsif ($opt eq "S") {
  666. $no_assemble = 1;
  667. } elsif ($opt eq "f") {
  668. $arg ||= shift @options;
  669. my $value = $arg !~ s/^no-//;
  670. $arg =~ s/-/_/g;
  671. my $ref = $optimise{$arg};
  672. if (defined($ref)) {
  673. $$ref = $value;
  674. } else {
  675. warn qq(ignoring unknown optimisation option "$arg"\n);
  676. }
  677. } elsif ($opt eq "O") {
  678. $arg = 1 if $arg eq "";
  679. my $ref;
  680. foreach $ref (values %optimise) {
  681. $$ref = 0;
  682. }
  683. if ($arg >= 6) {
  684. $strip_syntree = 1;
  685. }
  686. if ($arg >= 2) {
  687. $bypass_nullops = 1;
  688. }
  689. if ($arg >= 1) {
  690. $compress_nullops = 1;
  691. $omit_seq = 1;
  692. }
  693. }
  694. }
  695. if (@options) {
  696. return sub {
  697. my $objname;
  698. my $newfh;
  699. $newfh = prepare_assemble() unless $no_assemble;
  700. foreach $objname (@options) {
  701. eval "bytecompile_object(\\$objname)";
  702. }
  703. do_assemble($newfh) unless $no_assemble;
  704. }
  705. } else {
  706. return sub {
  707. my $newfh;
  708. $newfh = prepare_assemble() unless $no_assemble;
  709. bytecompile_main();
  710. do_assemble($newfh) unless $no_assemble;
  711. }
  712. }
  713. }
  714. 1;
  715. __END__
  716. =head1 NAME
  717. B::Bytecode - Perl compiler's bytecode backend
  718. =head1 SYNOPSIS
  719. perl -MO=Bytecode[,OPTIONS] foo.pl
  720. =head1 DESCRIPTION
  721. This compiler backend takes Perl source and generates a
  722. platform-independent bytecode encapsulating code to load the
  723. internal structures perl uses to run your program. When the
  724. generated bytecode is loaded in, your program is ready to run,
  725. reducing the time which perl would have taken to load and parse
  726. your program into its internal semi-compiled form. That means that
  727. compiling with this backend will not help improve the runtime
  728. execution speed of your program but may improve the start-up time.
  729. Depending on the environment in which your program runs this may
  730. or may not be a help.
  731. The resulting bytecode can be run with a special byteperl executable
  732. or (for non-main programs) be loaded via the C<byteload_fh> function
  733. in the F<B> module.
  734. =head1 OPTIONS
  735. If there are any non-option arguments, they are taken to be names of
  736. objects to be saved (probably doesn't work properly yet). Without
  737. extra arguments, it saves the main program.
  738. =over 4
  739. =item B<-ofilename>
  740. Output to filename instead of STDOUT.
  741. =item B<-->
  742. Force end of options.
  743. =item B<-f>
  744. Force optimisations on or off one at a time. Each can be preceded
  745. by B<no-> to turn the option off (e.g. B<-fno-compress-nullops>).
  746. =item B<-fcompress-nullops>
  747. Only fills in the necessary fields of ops which have
  748. been optimised away by perl's internal compiler.
  749. =item B<-fomit-sequence-numbers>
  750. Leaves out code to fill in the op_seq field of all ops
  751. which is only used by perl's internal compiler.
  752. =item B<-fbypass-nullops>
  753. If op->op_next ever points to a NULLOP, replaces the op_next field
  754. with the first non-NULLOP in the path of execution.
  755. =item B<-fstrip-syntax-tree>
  756. Leaves out code to fill in the pointers which link the internal syntax
  757. tree together. They're not needed at run-time but leaving them out
  758. will make it impossible to recompile or disassemble the resulting
  759. program. It will also stop C<goto label> statements from working.
  760. =item B<-On>
  761. Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.
  762. B<-O1> sets B<-fcompress-nullops> B<-fomit-sequence numbers>.
  763. B<-O6> adds B<-fstrip-syntax-tree>.
  764. =item B<-D>
  765. Debug options (concatenated or separate flags like C<perl -D>).
  766. =item B<-Do>
  767. Prints each OP as it's processed.
  768. =item B<-Db>
  769. Print debugging information about bytecompiler progress.
  770. =item B<-Da>
  771. Tells the (bytecode) assembler to include source assembler lines
  772. in its output as bytecode comments.
  773. =item B<-DC>
  774. Prints each CV taken from the final symbol tree walk.
  775. =item B<-S>
  776. Output (bytecode) assembler source rather than piping it
  777. through the assembler and outputting bytecode.
  778. =item B<-m>
  779. Compile as a module rather than a standalone program. Currently this
  780. just means that the bytecodes for initialising C<main_start>,
  781. C<main_root> and C<curpad> are omitted.
  782. =back
  783. =head1 EXAMPLES
  784. perl -MO=Bytecode,-O6,-o,foo.plc foo.pl
  785. perl -MO=Bytecode,-S foo.pl > foo.S
  786. assemble foo.S > foo.plc
  787. byteperl foo.plc
  788. perl -MO=Bytecode,-m,-oFoo.pmc Foo.pm
  789. =head1 BUGS
  790. Plenty. Current status: experimental.
  791. =head1 AUTHOR
  792. Malcolm Beattie, C<[email protected]>
  793. =cut