📄 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 Config;use strict;use B qw(main_start main_root class comppadlist peekop svref_2object timing_info init_av sv_undef amagic_generation OPf_WANT_LIST OPf_WANT OPf_MOD OPf_STACKED OPf_SPECIAL OPpASSIGN_BACKWARDS OPpLVAL_INTRO OPpDEREF_AV OPpDEREF_HV OPpDEREF OPpFLIP_LINENUM G_ARRAY G_SCALAR CXt_NULL CXt_SUB CXt_EVAL CXt_LOOP CXt_SUBST CXt_BLOCK );use B::C qw(save_unused_subs objsym init_sections mark_unused 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->flagsmy $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::Stackobjmy @pad; # Lexicals in current pad as Stackobj-derived objectsmy @padlist; # Copy of current padlist so PMOP repl code can find itmy @cxstack; # Shadows the (compile-time) cxstack for next,last,redomy $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufsmy %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 encounteredmy %skip_stack; # Hash of PP names which don't need write_back_stackmy %skip_lexicals; # Hash of PP names which don't need write_back_lexicalsmy %skip_invalidate; # Hash of PP names which don't need invalidate_lexicalsmy %ignore_op; # Hash of ops which do nothing except returning op_nextmy %need_curcop; # Hash of ops which need PL_curcopmy %lexstate; #state of padsvs at the start of a bblockBEGIN { foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) { $ignore_op{$_} = 1; }}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 functionmy $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);%need_curcop = init_hash qw(pp_rv2gv pp_bless pp_repeat pp_sort pp_caller pp_reset pp_rv2cv pp_entereval pp_require pp_dofile pp_entertry pp_enterloop pp_enteriter pp_entersub pp_enter pp_method);sub debug { if ($debug_runtime) { warn(@_); } else { my @tmp=@_; runtime(map { chomp; "/* $_ */"} @tmp); }}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\nCCPP($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("dSP;"); declare("I32", "oldsave"); declare("SV", "**svp"); map { declare("SV", "*$_") } qw(sv src dst left right); declare("MAGIC", "*mg"); $decl->add("static OP * $ppname (pTHX);"); debug "init_pp: $ppname\n" if $debug_queue;}# Initialise runtime_callback function for Stackobj classBEGIN { B::Stackobj::set_callback(\&runtime) }# Initialise saveoptree_callback for B::C classsub 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_bool : "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_bool); } 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 save_or_restore_lexical_state { my $bblock=shift; unless( exists $lexstate{$bblock}){ foreach my $lex (@pad) { next unless ref($lex); ${$lexstate{$bblock}}{$lex->{iv}} = $lex->{flags} ; } } else { foreach my $lex (@pad) { next unless ref($lex); my $old_flags=${$lexstate{$bblock}}{$lex->{iv}} ; next if ( $old_flags eq $lex->{flags}); if (($old_flags & VALID_SV) && !($lex->{flags} & VALID_SV)){ $lex->write_back; } if (($old_flags & VALID_DOUBLE) && !($lex->{flags} & VALID_DOUBLE)){ $lex->load_double; } if (($old_flags & VALID_INT) && !($lex->{flags} & VALID_INT)){ $lex->load_int; } } }}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]->file; 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"); debug sprintf("PL_curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad; }}sub declare_pad { my $ix; for ($ix = 1; $ix <= $#pad; $ix++) { my $type = $pad[$ix]->{type}; declare("IV", $type == T_INT ? sprintf("%s=0",$pad[$ix]->{iv}):$pad[$ix]->{iv}) if $pad[$ix]->save_int; declare("double", $type == T_DOUBLE ? sprintf("%s = 0",$pad[$ix]->{nv}):$pad[$ix]->{nv} )if $pad[$ix]->save_double; }}## Debugging stuff#sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }## OP stuff#sub label { my $op = shift; # XXX Preserve original label name for "real" labels? return sprintf("lab_%x", $$op);}sub write_label { my $op = shift; push_runtime(sprintf(" %s:", label($op)));}sub loadop { my $op = shift; my $opsym = $op->save; runtime("PL_op = $opsym;") unless $know_op; return $opsym;}sub doop { my $op = shift; my $ppname = $op->ppaddr; my $sym = loadop($op); runtime("DOOP($ppname);"); $know_op = 1; return $sym;}sub gimme { my $op = shift; my $flags = $op->flags; return (($flags & OPf_WANT) ? (($flags & OPf_WANT)== OPf_WANT_LIST? G_ARRAY:G_SCALAR) : "dowantarray()");}## Code generation for PP code#sub pp_null { my $op = shift; return $op->next;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -