📄 bytecode.pm
字号:
# See PVNV::bytecode for an explanation of what the argument does $sv->B::PVMG::bytecode(1); asmf "xbm_useful %d\nxbm_previous %d\nxbm_rare %d\n", $sv->USEFUL, $sv->PREVIOUS, $sv->RARE;}sub empty_gv { # is a GV empty except for imported stuff? my $gv = shift; return 0 if ($gv->SV->FLAGS & SVTYPEMASK); # sv not SVt_NULL my @subfield_names = qw(AV HV CV FORM IO); @subfield_names = grep {; no strict 'refs'; !($gv->GvFLAGS & ${\"GVf_IMPORTED_$_"}->()) && ${$gv->$_()}; } @subfield_names; return scalar @subfield_names;}sub B::GV::bytecode { my $gv = shift; return if saved($gv); return unless grep { $_ eq $gv->STASH->NAME; } @packages; return if $gv->NAME =~ m/^\(/; # ignore overloads - they'll be rebuilt my $ix = $gv->objix; mark_saved($gv); ldsv($ix); asmf <<"EOT", $gv->FLAGS, $gv->GvFLAGS;sv_flags 0x%xxgv_flags 0x%xEOT my $refcnt = $gv->REFCNT; asmf("sv_refcnt_add %d\n", $refcnt - 1) if $refcnt > 1; return if $gv->is_empty; asmf <<"EOT", $gv->LINE, pvix($gv->FILE);gp_line %dgp_file %dEOT my $gvname = $gv->NAME; my $name = cstring($gv->STASH->NAME . "::" . $gvname); my $egv = $gv->EGV; my $egvix = $egv->objix; my $gvrefcnt = $gv->GvREFCNT; asmf("gp_refcnt_add %d\n", $gvrefcnt - 1) if $gvrefcnt > 1; if ($gvrefcnt > 1 && $ix != $egvix) { asm "gp_share $egvix\n"; } else { if ($gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/) { my $i; my @subfield_names = qw(SV AV HV CV FORM IO); @subfield_names = grep {; no strict 'refs'; !($gv->GvFLAGS & ${\"GVf_IMPORTED_$_"}->()); } @subfield_names; my @subfields = map($gv->$_(), @subfield_names); my @ixes = map($_->objix, @subfields); # Reset sv register for $gv ldsv($ix); for ($i = 0; $i < @ixes; $i++) { asmf "gp_%s %d\n", lc($subfield_names[$i]), $ixes[$i]; } # Now save all the subfields my $sv; foreach $sv (@subfields) { $sv->bytecode; } } }}sub B::HV::bytecode { my $hv = shift; return if saved($hv); mark_saved($hv); my $name = $hv->NAME; my $ix = $hv->objix; if (!$name) { # It's an ordinary HV. Stashes have NAME set and need no further # saving beyond the gv_stashpv that $hv->objix already ensures. my @contents = $hv->ARRAY; my ($i, @ixes); for ($i = 1; $i < @contents; $i += 2) { push(@ixes, $contents[$i]->objix); } for ($i = 1; $i < @contents; $i += 2) { $contents[$i]->bytecode; } ldsv($ix); for ($i = 0; $i < @contents; $i += 2) { asmf("newpv %s\nhv_store %d\n", pvstring($contents[$i]), $ixes[$i / 2]); } asmf "sv_refcnt %d\nsv_flags 0x%x\n", $hv->REFCNT, $hv->FLAGS; }}sub B::AV::bytecode { my $av = shift; return if saved($av); my $ix = $av->objix; my $fill = $av->FILL; my $max = $av->MAX; my (@array, @ixes); if ($fill > -1) { @array = $av->ARRAY; @ixes = map($_->objix, @array); my $sv; foreach $sv (@array) { $sv->bytecode; } } # See PVNV::bytecode for the meaning of the flag argument of 2. $av->B::PVMG::bytecode(2); # Recover sv register and set AvMAX and AvFILL to -1 (since we # create an AV with NEWSV and SvUPGRADE rather than doing newAV # which is what sets AvMAX and AvFILL. ldsv($ix); asmf "sv_flags 0x%x\n", $av->FLAGS & ~SVf_READONLY; # SvREADONLY_off($av) in case PADCONST asmf "xav_flags 0x%x\nxav_max -1\nxav_fill -1\n", $av->AvFLAGS; if ($fill > -1) { my $elix; foreach $elix (@ixes) { asm "av_push $elix\n"; } } else { if ($max > -1) { asm "av_extend $max\n"; } } asmf "sv_flags 0x%x\n", $av->FLAGS; # restore flags from above}sub B::CV::bytecode { my $cv = shift; return if saved($cv); return if ${$cv->GV} && ($cv->GV->GvFLAGS & GVf_IMPORTED_CV); my $fileix = pvix($cv->FILE); my $ix = $cv->objix; $cv->B::PVMG::bytecode; my $i; my @subfield_names = qw(ROOT START STASH GV PADLIST OUTSIDE); my @subfields = map($cv->$_(), @subfield_names); my @ixes = map($_->objix, @subfields); # Save OP tree from CvROOT (first element of @subfields) my $root = shift @subfields; if ($$root) { walkoptree($root, "bytecode"); } # Reset sv register for $cv (since above ->objix calls stomped on it) ldsv($ix); for ($i = 0; $i < @ixes; $i++) { asmf "xcv_%s %d\n", lc($subfield_names[$i]), $ixes[$i]; } asmf "xcv_depth %d\nxcv_flags 0x%x\n", $cv->DEPTH, $cv->CvFLAGS; asmf "xcv_file %d\n", $fileix; # Now save all the subfields (except for CvROOT which was handled # above) and CvSTART (now the initial element of @subfields). shift @subfields; # bye-bye CvSTART my $sv; foreach $sv (@subfields) { $sv->bytecode; }}sub B::IO::bytecode { my $io = shift; return if saved($io); my $ix = $io->objix; my $top_gv = $io->TOP_GV; my $top_gvix = $top_gv->objix; my $fmt_gv = $io->FMT_GV; my $fmt_gvix = $fmt_gv->objix; my $bottom_gv = $io->BOTTOM_GV; my $bottom_gvix = $bottom_gv->objix; $io->B::PVMG::bytecode; ldsv($ix); asm "xio_top_gv $top_gvix\n"; asm "xio_fmt_gv $fmt_gvix\n"; asm "xio_bottom_gv $bottom_gvix\n"; my $field; foreach $field (qw(TOP_NAME FMT_NAME BOTTOM_NAME)) { asmf "newpv %s\nxio_%s\n", pvstring($io->$field()), lc($field); } foreach $field (qw(LINES PAGE PAGE_LEN LINES_LEFT SUBPROCESS)) { asmf "xio_%s %d\n", lc($field), $io->$field(); } asmf "xio_type %s\nxio_flags 0x%x\n", cstring($io->IoTYPE), $io->IoFLAGS; $top_gv->bytecode; $fmt_gv->bytecode; $bottom_gv->bytecode;}sub B::SPECIAL::bytecode { # nothing extra needs doing}sub bytecompile_object { for my $sv (@_) { svref_2object($sv)->bytecode; }}sub B::GV::bytecodecv { my $gv = shift; my $cv = $gv->CV; if ($$cv && !saved($cv) && !($gv->FLAGS & GVf_IMPORTED_CV)) { if ($debug_cv) { warn sprintf("saving extra CV &%s::%s (0x%x) from GV 0x%x\n", $gv->STASH->NAME, $gv->NAME, $$cv, $$gv); } $gv->bytecode; }}sub save_call_queues { if (begin_av()->isa("B::AV")) { # this is just to save 'use Foo;' calls for my $cv (begin_av()->ARRAY) { next unless grep { $_ eq $cv->STASH->NAME; } @packages; my $op = $cv->START;OPLOOP: while ($$op) { if ($op->name eq 'require') { # save any BEGIN that does a require $cv->bytecode; asmf "push_begin %d\n", $cv->objix; last OPLOOP; } $op = $op->next; } } } if (init_av()->isa("B::AV")) { for my $cv (init_av()->ARRAY) { next unless grep { $_ eq $cv->STASH->NAME; } @packages; $cv->bytecode; asmf "push_init %d\n", $cv->objix; } } if (end_av()->isa("B::AV")) { for my $cv (end_av()->ARRAY) { next unless grep { $_ eq $cv->STASH->NAME; } @packages; $cv->bytecode; asmf "push_end %d\n", $cv->objix; } }}sub symwalk { no strict 'refs'; my $ok = 1 if grep { (my $name = $_[0]) =~ s/::$//; $_ eq $name;} @packages; if (grep { /^$_[0]/; } @packages) { walksymtable(\%{"$_[0]"}, "bytecodecv", \&symwalk, $_[0]); } warn "considering $_[0] ... " . ($ok ? "accepted\n" : "rejected\n") if $debug_bc; $ok;}sub bytecompile_main { my $curpad = (comppadlist->ARRAY)[1]; my $curpadix = $curpad->objix; $curpad->bytecode; save_call_queues(); walkoptree(main_root, "bytecode") unless ref(main_root) eq "B::NULL"; warn "done main program, now walking symbol table\n" if $debug_bc; if (@packages) { no strict qw(refs); walksymtable(\%{"main::"}, "bytecodecv", \&symwalk); } else { die "No packages requested for compilation!\n"; } asmf "main_root %d\n", main_root->objix; asmf "main_start %d\n", main_start->objix; asmf "curpad $curpadix\n"; # XXX Do min_intro_pending and max_intro_pending matter?}sub compile { my @options = @_; my ($option, $opt, $arg); open(OUT, ">&STDOUT"); binmode OUT; select OUT; OPTION: while ($option = shift @options) { if ($option =~ /^-(.)(.*)/) { $opt = $1; $arg = $2; } else { unshift @options, $option; last OPTION; } if ($opt eq "-" && $arg eq "-") { shift @options; last OPTION; } elsif ($opt eq "o") { $arg ||= shift @options; open(OUT, ">$arg") or return "$arg: $!\n"; binmode OUT; } elsif ($opt eq "a") { $arg ||= shift @options; open(OUT, ">>$arg") or return "$arg: $!\n"; binmode OUT; } elsif ($opt eq "D") { $arg ||= shift @options; foreach $arg (split(//, $arg)) { if ($arg eq "b") { $| = 1; debug(1); } elsif ($arg eq "o") { B->debug(1); } elsif ($arg eq "a") { B::Assembler::debug(1); } elsif ($arg eq "C") { $debug_cv = 1; } } } elsif ($opt eq "v") { $verbose = 1; } elsif ($opt eq "S") { $no_assemble = 1; } elsif ($opt eq "f") { $arg ||= shift @options; my $value = $arg !~ s/^no-//; $arg =~ s/-/_/g; my $ref = $optimise{$arg}; if (defined($ref)) { $$ref = $value; } else { warn qq(ignoring unknown optimisation option "$arg"\n); } } elsif ($opt eq "O") { $arg = 1 if $arg eq ""; my $ref; foreach $ref (values %optimise) { $$ref = 0; } if ($arg >= 2) { $bypass_nullops = 1; } if ($arg >= 1) { $compress_nullops = 1; $omit_seq = 1; } } elsif ($opt eq "u") { $arg ||= shift @options; push @packages, $arg; } else { warn qq(ignoring unknown option "$opt$arg"\n); } } if (! @packages) { warn "No package specified for compilation, assuming main::\n"; @packages = qw(main); } if (@options) { die "Extraneous options left on B::Bytecode commandline: @options\n"; } else { return sub { newasm(\&apr) unless $no_assemble; bytecompile_main(); endasm() unless $no_assemble; }; }}sub apr { print @_; }1;__END__=head1 NAMEB::Bytecode - Perl compiler's bytecode backend=head1 SYNOPSIS perl -MO=Bytecode[,OPTIONS] foo.pl=head1 DESCRIPTIONThis compiler backend takes Perl source and generates aplatform-independent bytecode encapsulating code to load theinternal structures perl uses to run your program. When thegenerated bytecode is loaded in, your program is ready to run,reducing the time which perl would have taken to load and parseyour program into its internal semi-compiled form. That means thatcompiling with this backend will not help improve the runtimeexecution speed of your program but may improve the start-up time.Depending on the environment in which your program runs this mayor may not be a help.The resulting bytecode can be run with a special byteperl executableor (for non-main programs) be loaded via the C<byteload_fh> functionin the F<B> module.=head1 OPTIONSIf there are any non-option arguments, they are taken to be names ofobjects to be saved (probably doesn't work properly yet). Withoutextra arguments, it saves the main program.=over 4=item B<-ofilename>Output to filename instead of STDOUT.=item B<-afilename>Append output to filename.=item B<-->Force end of options.=item B<-f>Force optimisations on or off one at a time. Each can be precededby B<no-> to turn the option off (e.g. B<-fno-compress-nullops>).=item B<-fcompress-nullops>Only fills in the necessary fields of ops which havebeen optimised away by perl's internal compiler.=item B<-fomit-sequence-numbers>Leaves out code to fill in the op_seq field of all opswhich is only used by perl's internal compiler.=item B<-fbypass-nullops>If op->op_next ever points to a NULLOP, replaces the op_next fieldwith the first non-NULLOP in the path of execution.=item B<-On>Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.B<-O1> sets B<-fcompress-nullops> B<-fomit-sequence numbers>.B<-O2> adds B<-fbypass-nullops>.=item B<-D>Debug options (concatenated or separate flags like C<perl -D>).=item B<-Do>Prints each OP as it's processed.=item B<-Db>Print debugging information about bytecompiler progress.=item B<-Da>Tells the (bytecode) assembler to include source assembler linesin its output as bytecode comments.=item B<-DC>Prints each CV taken from the final symbol tree walk.=item B<-S>Output (bytecode) assembler source rather than piping itthrough the assembler and outputting bytecode.=item B<-upackage> Stores package in the output. =back=head1 EXAMPLES perl -MO=Bytecode,-O6,-ofoo.plc,-umain foo.pl perl -MO=Bytecode,-S,-umain foo.pl > foo.S assemble foo.S > foo.plcNote that C<assemble> lives in the C<B> subdirectory of your perllibrary directory. The utility called perlcc may also be used to help make use of this compiler. perl -MO=Bytecode,-uFoo,-oFoo.pmc Foo.pm=head1 BUGSOutput is still huge and there are still occasional crashes duringeither compilation or ByteLoading. Current status: experimental.=head1 AUTHORSMalcolm Beattie, C<mbeattie@sable.ox.ac.uk>Benjamin Stuhl, C<sho_pi@hotmail.com>=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -