Leaked source code of windows server 2003
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.

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