?? cc.pm
字號:
# CC.pm
#
# Copyright (c) 1996, 1997, 1998 Malcolm Beattie
#
# You may distribute under the terms of either the GNU General Public
# License or the Artistic License, as specified in the README file.
#
package B::CC;
use strict;
use B qw(main_start main_root class comppadlist peekop svref_2object
timing_info);
use B::C qw(save_unused_subs objsym init_sections
output_all output_boilerplate output_main);
use B::Bblock qw(find_leaders);
use B::Stackobj qw(:types :flags);
# These should probably be elsewhere
# Flags for $op->flags
sub OPf_LIST () { 1 }
sub OPf_KNOW () { 2 }
sub OPf_MOD () { 32 }
sub OPf_STACKED () { 64 }
sub OPf_SPECIAL () { 128 }
# op-specific flags for $op->private
sub OPpASSIGN_BACKWARDS () { 64 }
sub OPpLVAL_INTRO () { 128 }
sub OPpDEREF_AV () { 32 }
sub OPpDEREF_HV () { 64 }
sub OPpDEREF () { OPpDEREF_AV|OPpDEREF_HV }
sub OPpFLIP_LINENUM () { 64 }
sub G_ARRAY () { 1 }
# cop.h
sub CXt_NULL () { 0 }
sub CXt_SUB () { 1 }
sub CXt_EVAL () { 2 }
sub CXt_LOOP () { 3 }
sub CXt_SUBST () { 4 }
sub CXt_BLOCK () { 5 }
my $module; # module name (when compiled with -m)
my %done; # hash keyed by $$op of leaders of basic blocks
# which have already been done.
my $leaders; # ref to hash of basic block leaders. Keys are $$op
# addresses, values are the $op objects themselves.
my @bblock_todo; # list of leaders of basic blocks that need visiting
# sometime.
my @cc_todo; # list of tuples defining what PP code needs to be
# saved (e.g. CV, main or PMOP repl code). Each tuple
# is [$name, $root, $start, @padlist]. PMOP repl code
# tuples inherit padlist.
my @stack; # shadows perl's stack when contents are known.
# Values are objects derived from class B::Stackobj
my @pad; # Lexicals in current pad as Stackobj-derived objects
my @padlist; # Copy of current padlist so PMOP repl code can find it
my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
my $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufs
my %constobj; # OP_CONST constants as Stackobj-derived objects
# keyed by $$sv.
my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
# block or even to the end of each loop of blocks,
# depending on optimisation options.
my $know_op = 0; # Set when C variable op already holds the right op
# (from an immediately preceding DOOP(ppname)).
my $errors = 0; # Number of errors encountered
my %skip_stack; # Hash of PP names which don't need write_back_stack
my %skip_lexicals; # Hash of PP names which don't need write_back_lexicals
my %skip_invalidate; # Hash of PP names which don't need invalidate_lexicals
my %ignore_op; # Hash of ops which do nothing except returning op_next
BEGIN {
foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) {
$ignore_op{$_} = 1;
}
}
my @unused_sub_packages; # list of packages (given by -u options) to search
# explicitly and save every sub we find there, even
# if apparently unused (could be only referenced from
# an eval "" or from a $SIG{FOO} = "bar").
my ($module_name);
my ($debug_op, $debug_stack, $debug_cxstack, $debug_pad, $debug_runtime,
$debug_shadow, $debug_queue, $debug_lineno, $debug_timings);
# Optimisation options. On the command line, use hyphens instead of
# underscores for compatibility with gcc-style options. We use
# underscores here because they are OK in (strict) barewords.
my ($freetmps_each_bblock, $freetmps_each_loop, $omit_taint);
my %optimise = (freetmps_each_bblock => \$freetmps_each_bblock,
freetmps_each_loop => \$freetmps_each_loop,
omit_taint => \$omit_taint);
# perl patchlevel to generate code for (defaults to current patchlevel)
my $patchlevel = int(0.5 + 1000 * ($] - 5));
# Could rewrite push_runtime() and output_runtime() to use a
# temporary file if memory is at a premium.
my $ppname; # name of current fake PP function
my $runtime_list_ref;
my $declare_ref; # Hash ref keyed by C variable type of declarations.
my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
# tuples to be written out.
my ($init, $decl);
sub init_hash { map { $_ => 1 } @_ }
#
# Initialise the hashes for the default PP functions where we can avoid
# either write_back_stack, write_back_lexicals or invalidate_lexicals.
#
%skip_lexicals = init_hash qw(pp_enter pp_enterloop);
%skip_invalidate = init_hash qw(pp_enter pp_enterloop);
sub debug {
if ($debug_runtime) {
warn(@_);
} else {
runtime(map { chomp; "/* $_ */"} @_);
}
}
sub declare {
my ($type, $var) = @_;
push(@{$declare_ref->{$type}}, $var);
}
sub push_runtime {
push(@$runtime_list_ref, @_);
warn join("\n", @_) . "\n" if $debug_runtime;
}
sub save_runtime {
push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
}
sub output_runtime {
my $ppdata;
print qq(#include "cc_runtime.h"\n);
foreach $ppdata (@pp_list) {
my ($name, $runtime, $declare) = @$ppdata;
print "\nstatic\nPP($name)\n{\n";
my ($type, $varlist, $line);
while (($type, $varlist) = each %$declare) {
print "\t$type ", join(", ", @$varlist), ";\n";
}
foreach $line (@$runtime) {
print $line, "\n";
}
print "}\n";
}
}
sub runtime {
my $line;
foreach $line (@_) {
push_runtime("\t$line");
}
}
sub init_pp {
$ppname = shift;
$runtime_list_ref = [];
$declare_ref = {};
runtime("djSP;");
declare("I32", "oldsave");
declare("SV", "**svp");
map { declare("SV", "*$_") } qw(sv src dst left right);
declare("MAGIC", "*mg");
$decl->add("static OP * $ppname _((ARGSproto));");
debug "init_pp: $ppname\n" if $debug_queue;
}
# Initialise runtime_callback function for Stackobj class
BEGIN { B::Stackobj::set_callback(\&runtime) }
# Initialise saveoptree_callback for B::C class
sub cc_queue {
my ($name, $root, $start, @pl) = @_;
debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
if $debug_queue;
if ($name eq "*ignore*") {
$name = 0;
} else {
push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
}
my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
$start = $fakeop->save;
debug "cc_queue: name $name returns $start\n" if $debug_queue;
return $start;
}
BEGIN { B::C::set_callback(\&cc_queue) }
sub valid_int { $_[0]->{flags} & VALID_INT }
sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
sub valid_sv { $_[0]->{flags} & VALID_SV }
sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
sub top_bool { @stack ? $stack[-1]->as_numeric : "SvTRUE(TOPs)" }
sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
sub pop_bool {
if (@stack) {
return ((pop @stack)->as_numeric);
} else {
# Careful: POPs has an auto-decrement and SvTRUE evaluates
# its argument more than once.
runtime("sv = POPs;");
return "SvTRUE(sv)";
}
}
sub write_back_lexicals {
my $avoid = shift || 0;
debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
if $debug_shadow;
my $lex;
foreach $lex (@pad) {
next unless ref($lex);
$lex->write_back unless $lex->{flags} & $avoid;
}
}
sub write_back_stack {
my $obj;
return unless @stack;
runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
foreach $obj (@stack) {
runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
}
@stack = ();
}
sub invalidate_lexicals {
my $avoid = shift || 0;
debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
if $debug_shadow;
my $lex;
foreach $lex (@pad) {
next unless ref($lex);
$lex->invalidate unless $lex->{flags} & $avoid;
}
}
sub reload_lexicals {
my $lex;
foreach $lex (@pad) {
next unless ref($lex);
my $type = $lex->{type};
if ($type == T_INT) {
$lex->as_int;
} elsif ($type == T_DOUBLE) {
$lex->as_double;
} else {
$lex->as_sv;
}
}
}
{
package B::Pseudoreg;
#
# This class allocates pseudo-registers (OK, so they're C variables).
#
my %alloc; # Keyed by variable name. A value of 1 means the
# variable has been declared. A value of 2 means
# it's in use.
sub new_scope { %alloc = () }
sub new ($$$) {
my ($class, $type, $prefix) = @_;
my ($ptr, $i, $varname, $status, $obj);
$prefix =~ s/^(\**)//;
$ptr = $1;
$i = 0;
do {
$varname = "$prefix$i";
$status = $alloc{$varname};
} while $status == 2;
if ($status != 1) {
# Not declared yet
B::CC::declare($type, "$ptr$varname");
$alloc{$varname} = 2; # declared and in use
}
$obj = bless \$varname, $class;
return $obj;
}
sub DESTROY {
my $obj = shift;
$alloc{$$obj} = 1; # no longer in use but still declared
}
}
{
package B::Shadow;
#
# This class gives a standard API for a perl object to shadow a
# C variable and only generate reloads/write-backs when necessary.
#
# Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
# Use $obj->write_back whenever shadowed_c_var needs to be up to date.
# Use $obj->invalidate whenever an unknown function may have
# set shadow itself.
sub new {
my ($class, $write_back) = @_;
# Object fields are perl shadow variable, validity flag
# (for *C* variable) and callback sub for write_back
# (passed perl shadow variable as argument).
bless [undef, 1, $write_back], $class;
}
sub load {
my ($obj, $newval) = @_;
$obj->[1] = 0; # C variable no longer valid
$obj->[0] = $newval;
}
sub write_back {
my $obj = shift;
if (!($obj->[1])) {
$obj->[1] = 1; # C variable will now be valid
&{$obj->[2]}($obj->[0]);
}
}
sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
}
my $curcop = new B::Shadow (sub {
my $opsym = shift->save;
runtime("PL_curcop = (COP*)$opsym;");
});
#
# Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
#
sub dopoptoloop {
my $cxix = $#cxstack;
while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
$cxix--;
}
debug "dopoptoloop: returning $cxix" if $debug_cxstack;
return $cxix;
}
sub dopoptolabel {
my $label = shift;
my $cxix = $#cxstack;
while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP
&& $cxstack[$cxix]->{label} ne $label) {
$cxix--;
}
debug "dopoptolabel: returning $cxix" if $debug_cxstack;
return $cxix;
}
sub error {
my $format = shift;
my $file = $curcop->[0]->filegv->SV->PV;
my $line = $curcop->[0]->line;
$errors++;
if (@_) {
warn sprintf("%s:%d: $format\n", $file, $line, @_);
} else {
warn sprintf("%s:%d: %s\n", $file, $line, $format);
}
}
#
# Load pad takes (the elements of) a PADLIST as arguments and loads
# up @pad with Stackobj-derived objects which represent those lexicals.
# If/when perl itself can generate type information (my int $foo) then
# we'll take advantage of that here. Until then, we'll use various hacks
# to tell the compiler when we want a lexical to be a particular type
# or to be a register.
#
sub load_pad {
my ($namelistav, $valuelistav) = @_;
@padlist = @_;
my @namelist = $namelistav->ARRAY;
my @valuelist = $valuelistav->ARRAY;
my $ix;
@pad = ();
debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
# Temporary lexicals don't get named so it's possible for @valuelist
# to be strictly longer than @namelist. We count $ix up to the end of
# @valuelist but index into @namelist for the name. Any temporaries which
# run off the end of @namelist will make $namesv undefined and we treat
# that the same as having an explicit SPECIAL sv_undef object in @namelist.
# [XXX If/when @_ becomes a lexical, we must start at 0 here.]
for ($ix = 1; $ix < @valuelist; $ix++) {
my $namesv = $namelist[$ix];
my $type = T_UNKNOWN;
my $flags = 0;
my $name = "tmp$ix";
my $class = class($namesv);
if (!defined($namesv) || $class eq "SPECIAL") {
# temporaries have &PL_sv_undef instead of a PVNV for a name
$flags = VALID_SV|TEMPORARY|REGISTER;
} else {
if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
$name = $1;
if ($2 eq "i") {
$type = T_INT;
$flags = VALID_SV|VALID_INT;
} elsif ($2 eq "d") {
$type = T_DOUBLE;
$flags = VALID_SV|VALID_DOUBLE;
}
$flags |= REGISTER if $3;
}
}
$pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
"i_$name", "d_$name");
declare("IV", $type == T_INT ? "i_$name = 0" : "i_$name");
declare("double", $type == T_DOUBLE ? "d_$name = 0" : "d_$name");
debug sprintf("PL_curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
}
}
#
# Debugging stuff
#
sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }
#
# OP stuff
#
sub label {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -