Change PerlIO::Scalar and Via to scalar and via.
[p5sagit/p5-mst-13.2.git] / ext / B / B / C.pm
index a569f83..8d71bb2 100644 (file)
 #      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::C::Section;
+
+our $VERSION = '1.01';
+
+use B ();
+use base B::Section;
+
+sub new
+{
+ my $class = shift;
+ my $o = $class->SUPER::new(@_);
+ push @$o, { values => [] };
+ return $o;
+}
+
+sub add
+{
+ my $section = shift;
+ push(@{$section->[-1]{values}},@_);
+}
+
+sub index
+{
+ my $section = shift;
+ return scalar(@{$section->[-1]{values}})-1;
+}
+
+sub output
+{
+ my ($section, $fh, $format) = @_;
+ my $sym = $section->symtable || {};
+ my $default = $section->default;
+ my $i;
+ foreach (@{$section->[-1]{values}})
+  {
+   s{(s\\_[0-9a-f]+)}{ exists($sym->{$1}) ? $sym->{$1} : $default; }ge;
+   printf $fh $format, $_, $i;
+   ++$i;
+  }
+}
+
+package B::C::InitSection;
+
+# avoid use vars
+@B::C::InitSection::ISA = qw(B::C::Section);
+
+sub new {
+    my $class = shift;
+    my $max_lines = 10000; #pop;
+    my $section = $class->SUPER::new( @_ );
+
+    $section->[-1]{evals} = [];
+    $section->[-1]{chunks} = [];
+    $section->[-1]{nosplit} = 0;
+    $section->[-1]{current} = [];
+    $section->[-1]{count} = 0;
+    $section->[-1]{max_lines} = $max_lines;
+
+    return $section;
+}
+
+sub split {
+    my $section = shift;
+    $section->[-1]{nosplit}--
+      if $section->[-1]{nosplit} > 0;
+}
+
+sub no_split {
+    shift->[-1]{nosplit}++;
+}
+
+sub inc_count {
+    my $section = shift;
+
+    $section->[-1]{count} += $_[0];
+    # this is cheating
+    $section->add();
+}
+
+sub add {
+    my $section = shift->[-1];
+    my $current = $section->{current};
+    my $nosplit = $section->{nosplit};
+
+    push @$current, @_;
+    $section->{count} += scalar(@_);
+    if( !$nosplit && $section->{count} >= $section->{max_lines} ) {
+        push @{$section->{chunks}}, $current;
+        $section->{current} = [];
+        $section->{count} = 0;
+    }
+}
+
+sub add_eval {
+    my $section = shift;
+    my @strings = @_;
+
+    foreach my $i ( @strings ) {
+        $i =~ s/\"/\\\"/g;
+    }
+    push @{$section->[-1]{evals}}, @strings;
+}
+
+sub output {
+    my( $section, $fh, $format, $init_name ) = @_;
+    my $sym = $section->symtable || {};
+    my $default = $section->default;
+    push @{$section->[-1]{chunks}}, $section->[-1]{current};
+
+    my $name = "aaaa";
+    foreach my $i ( @{$section->[-1]{chunks}} ) {
+        print $fh <<"EOT";
+static int perl_init_${name}()
+{
+       dTARG;
+       dSP;
+EOT
+        foreach my $j ( @$i ) {
+            $j =~ s{(s\\_[0-9a-f]+)}
+                   { exists($sym->{$1}) ? $sym->{$1} : $default; }ge;
+            print $fh "\t$j\n";
+        }
+        print $fh "\treturn 0;\n}\n";
+
+        $section->SUPER::add( "perl_init_${name}();" );
+        ++$name;
+    }
+    foreach my $i ( @{$section->[-1]{evals}} ) {
+        $section->SUPER::add( sprintf q{eval_pv("%s",1);}, $i );
+    }
+
+    print $fh <<"EOT";
+static int ${init_name}()
+{
+       dTARG;
+       dSP;
+EOT
+    $section->SUPER::output( $fh, $format );
+    print $fh "\treturn 0;\n}\n";
+}
+
+
 package B::C;
 use Exporter ();
+our %REGEXP;
+
+{ # block necessary for caller to work
+    my $caller = caller;
+    if( $caller eq 'O' ) {
+        require XSLoader;
+        XSLoader::load( 'B::C' );
+    }
+}
+
 @ISA = qw(Exporter);
-@EXPORT_OK = qw(output_all output_boilerplate output_main
-               init_sections set_callback save_unused_subs objsym);
+@EXPORT_OK = qw(output_all output_boilerplate output_main mark_unused
+               init_sections set_callback save_unused_subs objsym save_context);
 
 use B qw(minus_c sv_undef walkoptree walksymtable main_root main_start peekop
         class cstring cchar svref_2object compile_stats comppadlist hash
-        threadsv_names main_cv );
+        threadsv_names main_cv init_av end_av regex_padav opnumber amagic_generation
+        AVf_REAL HEf_SVKEY SVf_POK SVf_ROK CVf_CONST);
 use B::Asmdata qw(@specialsv_name);
 
 use FileHandle;
 use Carp;
 use strict;
+use Config;
 
 my $hv_index = 0;
 my $gv_index = 0;
 my $re_index = 0;
 my $pv_index = 0;
+my $cv_index = 0;
 my $anonsub_index = 0;
+my $initsub_index = 0;
 
 my %symtable;
+my %xsub;
 my $warn_undefined_syms;
 my $verbose;
-my @unused_sub_packages;
+my %unused_sub_packages;
+my $use_xsloader;
 my $nullop_count;
-my $pv_copy_on_grow;
+my $pv_copy_on_grow = 0;
+my $optimize_ppaddr = 0;
+my $optimize_warn_sv = 0;
+my $use_perl_script_name = 0;
+my $save_data_fh = 0;
+my $save_sig = 0;
 my ($debug_cops, $debug_av, $debug_cv, $debug_mg);
+my $max_string_len;
+
+my $ithreads = $Config{useithreads} eq 'define';
 
 my @threadsv_names;
 BEGIN {
@@ -40,11 +206,14 @@ BEGIN {
 }
 
 # Code sections
-my ($init, $decl, $symsect, $binopsect, $condopsect, $copsect, $cvopsect,
-    $gvopsect, $listopsect, $logopsect, $loopsect, $opsect, $pmopsect,
+my ($init, $decl, $symsect, $binopsect, $condopsect, $copsect, 
+    $padopsect, $listopsect, $logopsect, $loopsect, $opsect, $pmopsect,
     $pvopsect, $svopsect, $unopsect, $svsect, $xpvsect, $xpvavsect,
     $xpvhvsect, $xpvcvsect, $xpvivsect, $xpvnvsect, $xpvmgsect, $xpvlvsect,
-    $xrvsect, $xpvbmsect, $xpviosect);
+    $xrvsect, $xpvbmsect, $xpviosect );
+my @op_sections = \( $binopsect, $condopsect, $copsect, $padopsect, $listopsect,
+                     $logopsect, $loopsect, $opsect, $pmopsect, $pvopsect, $svopsect,
+                     $unopsect );
 
 sub walk_and_save_optree;
 my $saveoptree_callback = \&walk_and_save_optree;
@@ -66,11 +235,9 @@ sub walk_and_save_optree {
 # to "know" that op_seq is a U16 and use 65535. Ugh.
 my $op_seq = 65535;
 
-sub AVf_REAL () { 1 }
-
-# XXX This shouldn't really be hardcoded here but it saves
-# looking up the name of every BASEOP in B::OP
-sub OP_THREADSV () { 345 }
+# Look this up here so we can do just a number compare
+# rather than looking up the name of every BASEOP in B::OP
+my $OP_THREADSV = opnumber('threadsv');
 
 sub savesym {
     my ($obj, $value) = @_;
@@ -97,35 +264,95 @@ sub getsym {
     }
 }
 
+sub savere {
+    my $re = shift;
+    my $sym = sprintf("re%d", $re_index++);
+    $decl->add(sprintf("static char *$sym = %s;", cstring($re)));
+
+    return ($sym,length(pack "a*",$re));
+}
+
 sub savepv {
-    my $pv = shift;
+    my $pv = pack "a*", shift;
     my $pvsym = 0;
     my $pvmax = 0;
     if ($pv_copy_on_grow) {
-       my $cstring = cstring($pv);
-       if ($cstring ne "0") { # sic
-           $pvsym = sprintf("pv%d", $pv_index++);
-           $decl->add(sprintf("static char %s[] = %s;", $pvsym, $cstring));
-       }
+        $pvsym = sprintf("pv%d", $pv_index++);
+
+        if( defined $max_string_len && length($pv) > $max_string_len ) {
+            my $chars = join ', ', map { cchar $_ } split //, $pv;
+            $decl->add(sprintf("static char %s[] = { %s };", $pvsym, $chars));
+        }
+        else {
+            my $cstring = cstring($pv);
+            if ($cstring ne "0") { # sic
+                $decl->add(sprintf("static char %s[] = %s;",
+                                   $pvsym, $cstring));
+           }
+        }
     } else {
-       $pvmax = length($pv) + 1;
+       $pvmax = length(pack "a*",$pv) + 1;
     }
     return ($pvsym, $pvmax);
 }
 
+sub save_rv {
+    my $sv = shift;
+#    confess "Can't save RV: not ROK" unless $sv->FLAGS & SVf_ROK;
+    my $rv = $sv->RV->save;
+
+    $rv =~ s/^\(([AGHS]V|IO)\s*\*\)\s*(\&sv_list.*)$/$2/;
+
+    return $rv;
+}
+
+# savesym, pvmax, len, pv
+sub save_pv_or_rv {
+    my $sv = shift;
+
+    my $rok = $sv->FLAGS & SVf_ROK;
+    my $pok = $sv->FLAGS & SVf_POK;
+    my( $len, $pvmax, $savesym, $pv ) = ( 0, 0 );
+    if( $rok ) {
+       $savesym = '(char*)' . save_rv( $sv );
+    }
+    else {
+       $pv = $pok ? (pack "a*", $sv->PV) : undef;
+       $len = $pok ? length($pv) : 0;
+       ($savesym, $pvmax) = $pok ? savepv($pv) : ( 'NULL', 0 );
+    }
+
+    return ( $savesym, $pvmax, $len, $pv );
+}
+
+# see also init_op_ppaddr below; initializes the ppaddt to the
+# OpTYPE; init_op_ppaddr iterates over the ops and sets
+# op_ppaddr to PL_ppaddr[op_ppaddr]; this avoids an explicit assignmente
+# in perl_init ( ~10 bytes/op with GCC/i386 )
+sub B::OP::fake_ppaddr {
+    return $optimize_ppaddr ?
+      sprintf("INT2PTR(void*,OP_%s)", uc( $_[0]->name ) ) :
+      'NULL';
+}
+
 sub B::OP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     my $type = $op->type;
     $nullop_count++ unless $type;
-    if ($type == OP_THREADSV) {
+    if ($type == $OP_THREADSV) {
        # saves looking up ppaddr but it's a bit naughty to hard code this
        $init->add(sprintf("(void)find_threadsv(%s);",
                           cstring($threadsv_names[$op->targ])));
     }
     $opsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x",
-                        ${$op->next}, ${$op->sibling}, $op->ppaddr, $op->targ,
+                        ${$op->next}, ${$op->sibling}, $op->fake_ppaddr, $op->targ,
                         $type, $op_seq, $op->flags, $op->private));
-    savesym($op, sprintf("&op_list[%d]", $opsect->index));
+    my $ix = $opsect->index;
+    $init->add(sprintf("op_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "&op_list[$ix]");
 }
 
 sub B::FAKEOP::new {
@@ -136,9 +363,12 @@ sub B::FAKEOP::new {
 sub B::FAKEOP::save {
     my ($op, $level) = @_;
     $opsect->add(sprintf("%s, %s, %s, %u, %u, %u, 0x%x, 0x%x",
-                        $op->next, $op->sibling, $op->ppaddr, $op->targ,
+                        $op->next, $op->sibling, $op->fake_ppaddr, $op->targ,
                         $op->type, $op_seq, $op->flags, $op->private));
-    return sprintf("&op_list[%d]", $opsect->index);
+    my $ix = $opsect->index;
+    $init->add(sprintf("op_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    return "&op_list[$ix]";
 }
 
 sub B::FAKEOP::next { $_[0]->{"next"} || 0 }
@@ -151,125 +381,197 @@ sub B::FAKEOP::private { $_[0]->{private} || 0 }
 
 sub B::UNOP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     $unopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x",
-                          ${$op->next}, ${$op->sibling}, $op->ppaddr,
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                           $op->targ, $op->type, $op_seq, $op->flags,
                           $op->private, ${$op->first}));
-    savesym($op, sprintf("(OP*)&unop_list[%d]", $unopsect->index));
+    my $ix = $unopsect->index;
+    $init->add(sprintf("unop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&unop_list[$ix]");
 }
 
 sub B::BINOP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     $binopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
-                           ${$op->next}, ${$op->sibling}, $op->ppaddr,
+                           ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                            $op->targ, $op->type, $op_seq, $op->flags,
                            $op->private, ${$op->first}, ${$op->last}));
-    savesym($op, sprintf("(OP*)&binop_list[%d]", $binopsect->index));
+    my $ix = $binopsect->index;
+    $init->add(sprintf("binop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&binop_list[$ix]");
 }
 
 sub B::LISTOP::save {
     my ($op, $level) = @_;
-    $listopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, %u",
-                            ${$op->next}, ${$op->sibling}, $op->ppaddr,
+    my $sym = objsym($op);
+    return $sym if defined $sym;
+    $listopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
+                            ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                             $op->targ, $op->type, $op_seq, $op->flags,
-                            $op->private, ${$op->first}, ${$op->last},
-                            $op->children));
-    savesym($op, sprintf("(OP*)&listop_list[%d]", $listopsect->index));
+                            $op->private, ${$op->first}, ${$op->last}));
+    my $ix = $listopsect->index;
+    $init->add(sprintf("listop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&listop_list[$ix]");
 }
 
 sub B::LOGOP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     $logopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
-                           ${$op->next}, ${$op->sibling}, $op->ppaddr,
+                           ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                            $op->targ, $op->type, $op_seq, $op->flags,
                            $op->private, ${$op->first}, ${$op->other}));
-    savesym($op, sprintf("(OP*)&logop_list[%d]", $logopsect->index));
-}
-
-sub B::CONDOP::save {
-    my ($op, $level) = @_;
-    $condopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, s\\_%x",
-                            ${$op->next}, ${$op->sibling}, $op->ppaddr,
-                            $op->targ, $op->type, $op_seq, $op->flags,
-                            $op->private, ${$op->first}, ${$op->true},
-                            ${$op->false}));
-    savesym($op, sprintf("(OP*)&condop_list[%d]", $condopsect->index));
+    my $ix = $logopsect->index;
+    $init->add(sprintf("logop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&logop_list[$ix]");
 }
 
 sub B::LOOP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     #warn sprintf("LOOP: redoop %s, nextop %s, lastop %s\n",
     #           peekop($op->redoop), peekop($op->nextop),
     #           peekop($op->lastop)); # debug
-    $loopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, %u, s\\_%x, s\\_%x, s\\_%x",
-                          ${$op->next}, ${$op->sibling}, $op->ppaddr,
+    $loopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, s\\_%x, s\\_%x, s\\_%x",
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                           $op->targ, $op->type, $op_seq, $op->flags,
                           $op->private, ${$op->first}, ${$op->last},
-                          $op->children, ${$op->redoop}, ${$op->nextop},
+                          ${$op->redoop}, ${$op->nextop},
                           ${$op->lastop}));
-    savesym($op, sprintf("(OP*)&loop_list[%d]", $loopsect->index));
+    my $ix = $loopsect->index;
+    $init->add(sprintf("loop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&loop_list[$ix]");
 }
 
 sub B::PVOP::save {
     my ($op, $level) = @_;
-    $pvopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, %s",
-                          ${$op->next}, ${$op->sibling}, $op->ppaddr,
+    my $sym = objsym($op);
+    return $sym if defined $sym;
+    $pvopsect->add(sprintf("s\\_%x, s\\_%x, %s,  %u, %u, %u, 0x%x, 0x%x, %s",
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                           $op->targ, $op->type, $op_seq, $op->flags,
                           $op->private, cstring($op->pv)));
-    savesym($op, sprintf("(OP*)&pvop_list[%d]", $pvopsect->index));
+    my $ix = $pvopsect->index;
+    $init->add(sprintf("pvop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    savesym($op, "(OP*)&pvop_list[$ix]");
 }
 
 sub B::SVOP::save {
     my ($op, $level) = @_;
-    my $svsym = $op->sv->save;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
+    my $sv = $op->sv;
+    my $svsym = '(SV*)' . $sv->save;
+    my $is_const_addr = $svsym =~ m/Null|\&/;
     $svopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, %s",
-                          ${$op->next}, ${$op->sibling}, $op->ppaddr,
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                           $op->targ, $op->type, $op_seq, $op->flags,
-                          $op->private, "(SV*)$svsym"));
-    savesym($op, sprintf("(OP*)&svop_list[%d]", $svopsect->index));
+                          $op->private,
+                           ( $is_const_addr ? $svsym : 'Nullsv' )));
+    my $ix = $svopsect->index;
+    $init->add(sprintf("svop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    $init->add("svop_list[$ix].op_sv = $svsym;")
+        unless $is_const_addr;
+    savesym($op, "(OP*)&svop_list[$ix]");
 }
 
-sub B::GVOP::save {
+sub B::PADOP::save {
     my ($op, $level) = @_;
-    my $gvsym = $op->gv->save;
-    $gvopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, Nullgv",
-                          ${$op->next}, ${$op->sibling}, $op->ppaddr,
+    my $sym = objsym($op);
+    return $sym if defined $sym;
+    $padopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, %d",
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                           $op->targ, $op->type, $op_seq, $op->flags,
-                          $op->private));
-    $init->add(sprintf("gvop_list[%d].op_gv = %s;", $gvopsect->index, $gvsym));
-    savesym($op, sprintf("(OP*)&gvop_list[%d]", $gvopsect->index));
+                          $op->private,$op->padix));
+    my $ix = $padopsect->index;
+    $init->add(sprintf("padop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+#    $init->add(sprintf("padop_list[$ix].op_padix = %ld;", $op->padix));
+    savesym($op, "(OP*)&padop_list[$ix]");
 }
 
 sub B::COP::save {
     my ($op, $level) = @_;
-    my $gvsym = $op->filegv->save;
-    my $stashsym = $op->stash->save;
-    warn sprintf("COP: line %d file %s\n", $op->line, $op->filegv->SV->PV)
+    my $sym = objsym($op);
+    return $sym if defined $sym;
+    warn sprintf("COP: line %d file %s\n", $op->line, $op->file)
        if $debug_cops;
-    $copsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, %s, Nullhv, Nullgv, %u, %d, %u",
-                         ${$op->next}, ${$op->sibling}, $op->ppaddr,
+    # shameless cut'n'paste from B::Deparse
+    my $warn_sv;
+    my $warnings = $op->warnings;
+    my $is_special = $warnings->isa("B::SPECIAL");
+    if ($is_special && $$warnings == 4) {
+        # use warnings 'all';
+        $warn_sv = $optimize_warn_sv ?
+            'INT2PTR(SV*,1)' :
+            'pWARN_ALL';
+    }
+    elsif ($is_special && $$warnings == 5) {
+        # no warnings 'all';
+        $warn_sv = $optimize_warn_sv ?
+            'INT2PTR(SV*,2)' :
+            'pWARN_NONE';
+    }
+    elsif ($is_special) {
+        # use warnings;
+        $warn_sv = $optimize_warn_sv ?
+            'INT2PTR(SV*,3)' :
+            'pWARN_STD';
+    }
+    else {
+        # something else
+        $warn_sv = $warnings->save;
+    }
+
+    $copsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, %s, NULL, NULL, %u, %d, %u, %s",
+                         ${$op->next}, ${$op->sibling}, $op->fake_ppaddr,
                          $op->targ, $op->type, $op_seq, $op->flags,
                          $op->private, cstring($op->label), $op->cop_seq,
-                         $op->arybase, $op->line));
-    my $copix = $copsect->index;
-    $init->add(sprintf("cop_list[%d].cop_filegv = %s;", $copix, $gvsym),
-              sprintf("cop_list[%d].cop_stash = %s;", $copix, $stashsym));
-    savesym($op, "(OP*)&cop_list[$copix]");
+                         $op->arybase, $op->line,
+                          ( $optimize_warn_sv ? $warn_sv : 'NULL' )));
+    my $ix = $copsect->index;
+    $init->add(sprintf("cop_list[$ix].op_ppaddr = %s;", $op->ppaddr))
+        unless $optimize_ppaddr;
+    $init->add(sprintf("cop_list[$ix].cop_warnings = %s;", $warn_sv ))
+        unless $optimize_warn_sv;
+    $init->add(sprintf("CopFILE_set(&cop_list[$ix], %s);", cstring($op->file)),
+              sprintf("CopSTASHPV_set(&cop_list[$ix], %s);", cstring($op->stashpv)));
+
+    savesym($op, "(OP*)&cop_list[$ix]");
 }
 
 sub B::PMOP::save {
     my ($op, $level) = @_;
+    my $sym = objsym($op);
+    return $sym if defined $sym;
     my $replroot = $op->pmreplroot;
     my $replstart = $op->pmreplstart;
-    my $replrootfield = sprintf("s\\_%x", $$replroot);
+    my $replrootfield;
     my $replstartfield = sprintf("s\\_%x", $$replstart);
     my $gvsym;
     my $ppaddr = $op->ppaddr;
-    if ($$replroot) {
+    # under ithreads, OP_PUSHRE.op_replroot is an integer
+    $replrootfield = sprintf("s\\_%x", $$replroot) if ref $replroot;
+    if($ithreads && $op->name eq "pushre") {
+        $replrootfield = "INT2PTR(OP*,${replroot})";
+    } elsif ($$replroot) {
        # OP_PUSHRE (a mutated version of OP_MATCH for the regexp
        # argument to a split) stores a GV in op_pmreplroot instead
        # of a substitution syntax tree. We don't want to walk that...
-       if ($ppaddr eq "pp_pushre") {
+       if ($op->name eq "pushre") {
            $gvsym = $replroot->save;
 #          warn "PMOP::save saving a pp_pushre with GV $gvsym\n"; # debug
            $replrootfield = 0;
@@ -280,24 +582,26 @@ sub B::PMOP::save {
     # pmnext handling is broken in perl itself, I think. Bad op_pmnext
     # fields aren't noticed in perl's runtime (unless you try reset) but we
     # segfault when trying to dereference it to find op->op_pmnext->op_type
-    $pmopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, %u, %s, %s, 0, 0, 0x%x, 0x%x",
-                          ${$op->next}, ${$op->sibling}, $ppaddr, $op->targ,
+    $pmopsect->add(sprintf("s\\_%x, s\\_%x, %s, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, %s, %s, 0, %u, 0x%x, 0x%x, 0x%x",
+                          ${$op->next}, ${$op->sibling}, $op->fake_ppaddr, $op->targ,
                           $op->type, $op_seq, $op->flags, $op->private,
-                          ${$op->first}, ${$op->last}, $op->children,
+                          ${$op->first}, ${$op->last}, 
                           $replrootfield, $replstartfield,
-                          $op->pmflags, $op->pmpermflags,));
+                           ( $ithreads ? $op->pmoffset : 0 ),
+                          $op->pmflags, $op->pmpermflags, $op->pmdynflags ));
     my $pm = sprintf("pmop_list[%d]", $pmopsect->index);
+    $init->add(sprintf("$pm.op_ppaddr = %s;", $ppaddr))
+        unless $optimize_ppaddr;
     my $re = $op->precomp;
     if (defined($re)) {
-       my $resym = sprintf("re%d", $re_index++);
-       $decl->add(sprintf("static char *$resym = %s;", cstring($re)));
-       $init->add(sprintf("$pm.op_pmregexp = pregcomp($resym, $resym + %u, &$pm);",
-                          length($re)));
+       my( $resym, $relen ) = savere( $re );
+       $init->add(sprintf("PM_SETRE(&$pm,pregcomp($resym, $resym + %u, &$pm));",
+                          $relen));
     }
     if ($gvsym) {
        $init->add("$pm.op_pmreplroot = (OP*)$gvsym;");
     }
-    savesym($op, sprintf("(OP*)&pmop_list[%d]", $pmopsect->index));
+    savesym($op, "(OP*)&$pm");
 }
 
 sub B::SPECIAL::save {
@@ -319,10 +623,11 @@ sub B::NULL::save {
     return $sym if defined $sym;
 #   warn "Saving SVt_NULL SV\n"; # debug
     # debug
-    #if ($$sv == 0) {
-    #  warn "NULL::save for sv = 0 called from @{[(caller(1))[3]]}\n";
-    #}
-    $svsect->add(sprintf("0, %u, 0x%x", $sv->REFCNT + 1, $sv->FLAGS));
+    if ($$sv == 0) {
+       warn "NULL::save for sv = 0 called from @{[(caller(1))[3]]}\n";
+       return savesym($sv, "(void*)Nullsv /* XXX */");
+    }
+    $svsect->add(sprintf("0, %u, 0x%x", $sv->REFCNT , $sv->FLAGS));
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
 
@@ -332,7 +637,7 @@ sub B::IV::save {
     return $sym if defined $sym;
     $xpvivsect->add(sprintf("0, 0, 0, %d", $sv->IVX));
     $svsect->add(sprintf("&xpviv_list[%d], %lu, 0x%x",
-                        $xpvivsect->index, $sv->REFCNT + 1, $sv->FLAGS));
+                        $xpvivsect->index, $sv->REFCNT , $sv->FLAGS));
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
 
@@ -340,12 +645,37 @@ sub B::NV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    $xpvnvsect->add(sprintf("0, 0, 0, %d, %s", $sv->IVX, $sv->NVX));
+    my $val= $sv->NVX;
+    $val .= '.00' if $val =~ /^-?\d+$/;
+    $xpvnvsect->add(sprintf("0, 0, 0, %d, %s", $sv->IVX, $val));
     $svsect->add(sprintf("&xpvnv_list[%d], %lu, 0x%x",
-                        $xpvnvsect->index, $sv->REFCNT + 1, $sv->FLAGS));
+                        $xpvnvsect->index, $sv->REFCNT , $sv->FLAGS));
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
 
+sub savepvn {
+    my ($dest,$pv) = @_;
+    my @res;
+    # work with byte offsets/lengths
+    my $pv = pack "a*", $pv;
+    if (defined $max_string_len && length($pv) > $max_string_len) {
+       push @res, sprintf("New(0,%s,%u,char);", $dest, length($pv)+1);
+       my $offset = 0;
+       while (length $pv) {
+           my $str = substr $pv, 0, $max_string_len, '';
+           push @res, sprintf("Copy(%s,$dest+$offset,%u,char);",
+                              cstring($str), length($str));
+           $offset += length $str;
+       }
+       push @res, sprintf("%s[%u] = '\\0';", $dest, $offset);
+    }
+    else {
+       push @res, sprintf("%s = savepvn(%s, %u);", $dest,
+                          cstring($pv), length($pv));
+    }
+    return @res;
+}
+
 sub B::PVLV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
@@ -358,10 +688,10 @@ sub B::PVLV::save {
                            $pvsym, $len, $pvmax, $sv->IVX, $sv->NVX, 
                            $sv->TARGOFF, $sv->TARGLEN, cchar($sv->TYPE)));
     $svsect->add(sprintf("&xpvlv_list[%d], %lu, 0x%x",
-                        $xpvlvsect->index, $sv->REFCNT + 1, $sv->FLAGS));
+                        $xpvlvsect->index, $sv->REFCNT , $sv->FLAGS));
     if (!$pv_copy_on_grow) {
-       $init->add(sprintf("xpvlv_list[%d].xpv_pv = savepvn(%s, %u);",
-                          $xpvlvsect->index, cstring($pv), $len));
+       $init->add(savepvn(sprintf("xpvlv_list[%d].xpv_pv",
+                                  $xpvlvsect->index), $pv));
     }
     $sv->save_magic;
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
@@ -371,15 +701,13 @@ sub B::PVIV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    my $pv = $sv->PV;
-    my $len = length($pv);
-    my ($pvsym, $pvmax) = savepv($pv);
-    $xpvivsect->add(sprintf("%s, %u, %u, %d", $pvsym, $len, $pvmax, $sv->IVX));
+    my( $savesym, $pvmax, $len, $pv ) = save_pv_or_rv( $sv );
+    $xpvivsect->add(sprintf("%s, %u, %u, %d", $savesym, $len, $pvmax, $sv->IVX));
     $svsect->add(sprintf("&xpviv_list[%d], %u, 0x%x",
-                        $xpvivsect->index, $sv->REFCNT + 1, $sv->FLAGS));
-    if (!$pv_copy_on_grow) {
-       $init->add(sprintf("xpviv_list[%d].xpv_pv = savepvn(%s, %u);",
-                          $xpvivsect->index, cstring($pv), $len));
+                        $xpvivsect->index, $sv->REFCNT , $sv->FLAGS));
+    if (defined($pv) && !$pv_copy_on_grow) {
+       $init->add(savepvn(sprintf("xpviv_list[%d].xpv_pv",
+                                  $xpvivsect->index), $pv));
     }
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
@@ -388,16 +716,16 @@ sub B::PVNV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    my $pv = $sv->PV;
-    my $len = length($pv);
-    my ($pvsym, $pvmax) = savepv($pv);
+    my( $savesym, $pvmax, $len, $pv ) = save_pv_or_rv( $sv );
+    my $val= $sv->NVX;
+    $val .= '.00' if $val =~ /^-?\d+$/;
     $xpvnvsect->add(sprintf("%s, %u, %u, %d, %s",
-                           $pvsym, $len, $pvmax, $sv->IVX, $sv->NVX));
+                           $savesym, $len, $pvmax, $sv->IVX, $val));
     $svsect->add(sprintf("&xpvnv_list[%d], %lu, 0x%x",
-                        $xpvnvsect->index, $sv->REFCNT + 1, $sv->FLAGS));
-    if (!$pv_copy_on_grow) {
-       $init->add(sprintf("xpvnv_list[%d].xpv_pv = savepvn(%s,%u);",
-                          $xpvnvsect->index, cstring($pv), $len));
+                        $xpvnvsect->index, $sv->REFCNT , $sv->FLAGS));
+    if (defined($pv) && !$pv_copy_on_grow) {
+       $init->add(savepvn(sprintf("xpvnv_list[%d].xpv_pv",
+                                  $xpvnvsect->index), $pv));
     }
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
@@ -406,16 +734,16 @@ sub B::BM::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    my $pv = $sv->PV . "\0" . $sv->TABLE;
+    my $pv = pack "a*", ($sv->PV . "\0" . $sv->TABLE);
     my $len = length($pv);
     $xpvbmsect->add(sprintf("0, %u, %u, %d, %s, 0, 0, %d, %u, 0x%x",
                            $len, $len + 258, $sv->IVX, $sv->NVX,
                            $sv->USEFUL, $sv->PREVIOUS, $sv->RARE));
     $svsect->add(sprintf("&xpvbm_list[%d], %lu, 0x%x",
-                        $xpvbmsect->index, $sv->REFCNT + 1, $sv->FLAGS));
+                        $xpvbmsect->index, $sv->REFCNT , $sv->FLAGS));
     $sv->save_magic;
-    $init->add(sprintf("xpvbm_list[%d].xpv_pv = savepvn(%s, %u);",
-                      $xpvbmsect->index, cstring($pv), $len),
+    $init->add(savepvn(sprintf("xpvbm_list[%d].xpv_pv",
+                              $xpvbmsect->index), $pv),
               sprintf("xpvbm_list[%d].xpv_cur = %u;",
                       $xpvbmsect->index, $len - 257));
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
@@ -425,15 +753,13 @@ sub B::PV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    my $pv = $sv->PV;
-    my $len = length($pv);
-    my ($pvsym, $pvmax) = savepv($pv);
-    $xpvsect->add(sprintf("%s, %u, %u", $pvsym, $len, $pvmax));
+    my( $savesym, $pvmax, $len, $pv ) = save_pv_or_rv( $sv );
+    $xpvsect->add(sprintf("%s, %u, %u", $savesym, $len, $pvmax));
     $svsect->add(sprintf("&xpv_list[%d], %lu, 0x%x",
-                        $xpvsect->index, $sv->REFCNT + 1, $sv->FLAGS));
-    if (!$pv_copy_on_grow) {
-       $init->add(sprintf("xpv_list[%d].xpv_pv = savepvn(%s, %u);",
-                          $xpvsect->index, cstring($pv), $len));
+                        $xpvsect->index, $sv->REFCNT , $sv->FLAGS));
+    if (defined($pv) && !$pv_copy_on_grow) {
+       $init->add(savepvn(sprintf("xpv_list[%d].xpv_pv",
+                                  $xpvsect->index), $pv));
     }
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
@@ -442,16 +768,16 @@ sub B::PVMG::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    my $pv = $sv->PV;
-    my $len = length($pv);
-    my ($pvsym, $pvmax) = savepv($pv);
+    my( $savesym, $pvmax, $len, $pv ) = save_pv_or_rv( $sv );
+
     $xpvmgsect->add(sprintf("%s, %u, %u, %d, %s, 0, 0",
-                           $pvsym, $len, $pvmax, $sv->IVX, $sv->NVX));
+                            $savesym, $len, $pvmax,
+                            $sv->IVX, $sv->NVX));
     $svsect->add(sprintf("&xpvmg_list[%d], %lu, 0x%x",
-                        $xpvmgsect->index, $sv->REFCNT + 1, $sv->FLAGS));
-    if (!$pv_copy_on_grow) {
-       $init->add(sprintf("xpvmg_list[%d].xpv_pv = savepvn(%s, %u);",
-                          $xpvmgsect->index, cstring($pv), $len));
+                         $xpvmgsect->index, $sv->REFCNT , $sv->FLAGS));
+    if (defined($pv) && !$pv_copy_on_grow) {
+        $init->add(savepvn(sprintf("xpvmg_list[%d].xpv_pv",
+                                   $xpvmgsect->index), $pv));
     }
     $sym = savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
     $sv->save_magic;
@@ -462,6 +788,7 @@ sub B::PVMG::save_magic {
     my ($sv) = @_;
     #warn sprintf("saving magic for %s (0x%x)\n", class($sv), $$sv); # debug
     my $stash = $sv->SvSTASH;
+    $stash->save;
     if ($$stash) {
        warn sprintf("xmg_stash = %s (0x%x)\n", $stash->NAME, $$stash)
            if $debug_mg;
@@ -469,19 +796,45 @@ sub B::PVMG::save_magic {
        $init->add(sprintf("SvSTASH(s\\_%x) = s\\_%x;", $$sv, $$stash));
     }
     my @mgchain = $sv->MAGIC;
-    my ($mg, $type, $obj, $ptr);
+    my ($mg, $type, $obj, $ptr,$len,$ptrsv);
     foreach $mg (@mgchain) {
        $type = $mg->TYPE;
-       $obj = $mg->OBJ;
        $ptr = $mg->PTR;
-       my $len = defined($ptr) ? length($ptr) : 0;
+       $len=$mg->LENGTH;
        if ($debug_mg) {
            warn sprintf("magic %s (0x%x), obj %s (0x%x), type %s, ptr %s\n",
                         class($sv), $$sv, class($obj), $$obj,
                         cchar($type), cstring($ptr));
        }
-       $init->add(sprintf("sv_magic((SV*)s\\_%x, (SV*)s\\_%x, %s, %s, %d);",
+
+        unless( $type eq 'r' ) {
+          $obj = $mg->OBJ;
+          $obj->save;
+        }
+
+       if ($len == HEf_SVKEY){
+               #The pointer is an SV*
+               $ptrsv=svref_2object($ptr)->save;
+               $init->add(sprintf("sv_magic((SV*)s\\_%x, (SV*)s\\_%x, %s,(char *) %s, %d);",
+                          $$sv, $$obj, cchar($type),$ptrsv,$len));
+        }elsif( $type eq 'r' ){
+            my $rx = $mg->REGEX;
+            my $pmop = $REGEXP{$rx};
+
+            confess "PMOP not found for REGEXP $rx" unless $pmop;
+
+            my( $resym, $relen ) = savere( $mg->precomp );
+            my $pmsym = $pmop->save;
+            $init->add( split /\n/, sprintf <<CODE, $$sv, cchar($type), cstring($ptr) );
+{
+    REGEXP* rx = pregcomp($resym, $resym + $relen, (PMOP*)$pmsym);
+    sv_magic((SV*)s\\_%x, (SV*)rx, %s, %s, %d);
+}
+CODE
+        }else{
+               $init->add(sprintf("sv_magic((SV*)s\\_%x, (SV*)s\\_%x, %s, %s, %d);",
                           $$sv, $$obj, cchar($type),cstring($ptr),$len));
+       }
     }
 }
 
@@ -489,9 +842,22 @@ sub B::RV::save {
     my ($sv) = @_;
     my $sym = objsym($sv);
     return $sym if defined $sym;
-    $xrvsect->add($sv->RV->save);
+    my $rv = save_rv( $sv );
+    # GVs need to be handled at runtime
+    if( ref( $sv->RV ) eq 'B::GV' ) {
+        $xrvsect->add( "(SV*)Nullgv" );
+        $init->add(sprintf("xrv_list[%d].xrv_rv = (SV*)%s;\n", $xrvsect->index, $rv));
+    }
+    # and stashes, too
+    elsif( $sv->RV->isa( 'B::HV' ) && $sv->RV->NAME ) {
+        $xrvsect->add( "(SV*)Nullhv" );
+        $init->add(sprintf("xrv_list[%d].xrv_rv = (SV*)%s;\n", $xrvsect->index, $rv));
+    }
+    else {
+        $xrvsect->add($rv);
+    }
     $svsect->add(sprintf("&xrv_list[%d], %lu, 0x%x",
-                        $xrvsect->index, $sv->REFCNT + 1, $sv->FLAGS));
+                        $xrvsect->index, $sv->REFCNT , $sv->FLAGS));
     return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
 }
 
@@ -516,7 +882,7 @@ sub try_autoload {
        }
     }
 }
-
+sub Dummy_initxs{};
 sub B::CV::save {
     my ($cv) = @_;
     my $sym = objsym($cv);
@@ -525,18 +891,74 @@ sub B::CV::save {
        return $sym;
     }
     # Reserve a place in svsect and xpvcvsect and record indices
+    my $gv = $cv->GV;
+    my ($cvname, $cvstashname);
+    if ($$gv){
+       $cvname = $gv->NAME;
+       $cvstashname = $gv->STASH->NAME;
+    }
+    my $root = $cv->ROOT;
+    my $cvxsub = $cv->XSUB;
+    my $isconst = $cv->CvFLAGS & CVf_CONST;
+    if( $isconst ) {
+        my $value = $cv->XSUBANY;
+        my $stash = $gv->STASH;
+        my $vsym = $value->save;
+        my $stsym = $stash->save;
+        my $name = cstring($cvname);
+        $decl->add( "static CV* cv$cv_index;" );
+        $init->add( "cv$cv_index = newCONSTSUB( $stsym, NULL, $vsym );" );
+        my $sym = savesym( $cv, "cv$cv_index" );
+        $cv_index++;
+        return $sym;
+    }
+    #INIT is removed from the symbol table, so this call must come
+    # from PL_initav->save. Re-bootstrapping  will push INIT back in
+    # so nullop should be sent.
+    if (!$isconst && $cvxsub && ($cvname ne "INIT")) {
+       my $egv = $gv->EGV;
+       my $stashname = $egv->STASH->NAME;
+         if ($cvname eq "bootstrap")
+          { 
+           my $file = $gv->FILE;
+           $decl->add("/* bootstrap $file */"); 
+           warn "Bootstrap $stashname $file\n";
+           # if it not isa('DynaLoader'), it should hopefully be XSLoaded
+           # ( attributes being an exception, of course )
+           if( $stashname ne 'attributes' &&
+               !UNIVERSAL::isa($stashname,'DynaLoader') ) {
+            $xsub{$stashname}='Dynamic-XSLoaded';
+            $use_xsloader = 1;
+           }
+           else {
+            $xsub{$stashname}='Dynamic';
+           }
+          # $xsub{$stashname}='Static' unless  $xsub{$stashname};
+           return qq/NULL/;
+          }
+         else
+          {
+           # XSUBs for IO::File, IO::Handle, IO::Socket,
+           # IO::Seekable and IO::Poll
+           # are defined in IO.xs, so let's bootstrap it
+           svref_2object( \&IO::bootstrap )->save
+            if grep { $stashname eq $_ } qw(IO::File IO::Handle IO::Socket
+                                              IO::Seekable IO::Poll);
+          }
+        warn sprintf("stub for XSUB $cvstashname\:\:$cvname CV 0x%x\n", $$cv) if $debug_cv;
+       return qq/(perl_get_cv("$stashname\:\:$cvname",TRUE))/;
+    }
+    if ($cvxsub && $cvname eq "INIT") {
+        no strict 'refs';
+        return svref_2object(\&Dummy_initxs)->save;
+    }
     my $sv_ix = $svsect->index + 1;
     $svsect->add("svix$sv_ix");
     my $xpvcv_ix = $xpvcvsect->index + 1;
     $xpvcvsect->add("xpvcvix$xpvcv_ix");
     # Save symbol now so that GvCV() doesn't recurse back to us via CvGV()
     $sym = savesym($cv, "&sv_list[$sv_ix]");
-    warn sprintf("saving CV 0x%x as $sym\n", $$cv) if $debug_cv;
-    my $gv = $cv->GV;
-    my $cvstashname = $gv->STASH->NAME;
-    my $cvname = $gv->NAME;
-    my $root = $cv->ROOT;
-    my $cvxsub = $cv->XSUB;
+    warn sprintf("saving $cvstashname\:\:$cvname CV 0x%x as $sym\n", $$cv) if $debug_cv;
     if (!$$root && !$cvxsub) {
        if (try_autoload($cvstashname, $cvname)) {
            # Recalculate root and xsub
@@ -564,6 +986,10 @@ sub B::CV::save {
                $ppname .= ($stashname eq "main") ?
                            $gvname : "$stashname\::$gvname";
                $ppname =~ s/::/__/g;
+               if ($gvname eq "INIT"){
+                      $ppname .= "_$initsub_index";
+                      $initsub_index++;
+                   }
            }
        }
        if (!$ppname) {
@@ -581,28 +1007,19 @@ sub B::CV::save {
                         $$padlist, $$cv) if $debug_cv;
        }
     }
-    elsif ($cvxsub) {
-       $xsubany = sprintf("ANYINIT((void*)0x%x)", $cv->XSUBANY);
-       # Try to find out canonical name of XSUB function from EGV.
-       # XXX Doesn't work for XSUBs with PREFIX set (or anyone who
-       # calls newXS() manually with weird arguments).
-       my $egv = $gv->EGV;
-       my $stashname = $egv->STASH->NAME;
-       $stashname =~ s/::/__/g;
-       $xsub = sprintf("XS_%s_%s", $stashname, $egv->NAME);
-       $decl->add("void $xsub _((CV*));");
-    }
     else {
        warn sprintf("No definition for sub %s::%s (unable to autoload)\n",
                     $cvstashname, $cvname); # debug
-    }
-    $symsect->add(sprintf("xpvcvix%d\t%s, %u, 0, %d, %s, 0, Nullhv, Nullhv, %s, s\\_%x, $xsub, $xsubany, Nullgv, Nullgv, %d, s\\_%x, (CV*)s\\_%x, 0x%x",
+    }              
+    $pv = '' unless defined $pv; # Avoid use of undef warnings
+    $symsect->add(sprintf("xpvcvix%d\t%s, %u, 0, %d, %s, 0, Nullhv, Nullhv, %s, s\\_%x, $xsub, $xsubany, Nullgv, \"\", %d, s\\_%x, (CV*)s\\_%x, 0x%x",
                          $xpvcv_ix, cstring($pv), length($pv), $cv->IVX,
                          $cv->NVX, $startfield, ${$cv->ROOT}, $cv->DEPTH,
                         $$padlist, ${$cv->OUTSIDE}, $cv->CvFLAGS));
 
     if (${$cv->OUTSIDE} == ${main_cv()}){
        $init->add(sprintf("CvOUTSIDE(s\\_%x)=PL_main_cv;",$$cv));
+       $init->add(sprintf("SvREFCNT_inc(PL_main_cv);"));
     }
 
     if ($$gv) {
@@ -611,12 +1028,11 @@ sub B::CV::save {
        warn sprintf("done saving GV 0x%x for CV 0x%x\n",
                     $$gv, $$cv) if $debug_cv;
     }
-    my $filegv = $cv->FILEGV;
-    if ($$filegv) {
-       $filegv->save;
-       $init->add(sprintf("CvFILEGV(s\\_%x) = s\\_%x;", $$cv, $$filegv));
-       warn sprintf("done saving FILEGV 0x%x for CV 0x%x\n",
-                    $$filegv, $$cv) if $debug_cv;
+    if( $ithreads ) {
+        $init->add( savepvn( "CvFILE($sym)", $cv->FILE) );
+    }
+    else {
+        $init->add(sprintf("CvFILE($sym) = %s;", cstring($cv->FILE)));
     }
     my $stash = $cv->STASH;
     if ($$stash) {
@@ -626,7 +1042,7 @@ sub B::CV::save {
                     $$stash, $$cv) if $debug_cv;
     }
     $symsect->add(sprintf("svix%d\t(XPVCV*)&xpvcv_list[%u], %lu, 0x%x",
-                         $sv_ix, $xpvcv_ix, $cv->REFCNT + 1, $cv->FLAGS));
+                         $sv_ix, $xpvcv_ix, $cv->REFCNT +1*0 , $cv->FLAGS));
     return $sym;
 }
 
@@ -641,80 +1057,126 @@ sub B::GV::save {
        $sym = savesym($gv, "gv_list[$ix]");
        #warn sprintf("Saving GV 0x%x as $sym\n", $$gv); # debug
     }
+    my $is_empty = $gv->is_empty;
     my $gvname = $gv->NAME;
-    my $name = cstring($gv->STASH->NAME . "::" . $gvname);
+    my $fullname = $gv->STASH->NAME . "::" . $gvname;
+    my $name = cstring($fullname);
     #warn "GV name is $name\n"; # debug
-    my $egv = $gv->EGV;
     my $egvsym;
-    if ($$gv != $$egv) {
-       #warn(sprintf("EGV name is %s, saving it now\n",
-       #            $egv->STASH->NAME . "::" . $egv->NAME)); # debug
-       $egvsym = $egv->save;
+    unless ($is_empty) {
+       my $egv = $gv->EGV;
+       if ($$gv != $$egv) {
+           #warn(sprintf("EGV name is %s, saving it now\n",
+           #        $egv->STASH->NAME . "::" . $egv->NAME)); # debug
+           $egvsym = $egv->save;
+       }
     }
     $init->add(qq[$sym = gv_fetchpv($name, TRUE, SVt_PV);],
-              sprintf("SvFLAGS($sym) = 0x%x;", $gv->FLAGS),
-              sprintf("GvFLAGS($sym) = 0x%x;", $gv->GvFLAGS),
-              sprintf("GvLINE($sym) = %u;", $gv->LINE));
+              sprintf("SvFLAGS($sym) = 0x%x;", $gv->FLAGS ),
+              sprintf("GvFLAGS($sym) = 0x%x;", $gv->GvFLAGS));
+    $init->add(sprintf("GvLINE($sym) = %u;", $gv->LINE)) unless $is_empty;
+    # XXX hack for when Perl accesses PVX of GVs
+    $init->add("SvPVX($sym) = emptystring;\n");
     # Shouldn't need to do save_magic since gv_fetchpv handles that
     #$gv->save_magic;
+    # XXX will always be > 1!!!
     my $refcnt = $gv->REFCNT + 1;
-    $init->add(sprintf("SvREFCNT($sym) += %u;", $refcnt - 1)) if $refcnt > 1;
+    $init->add(sprintf("SvREFCNT($sym) += %u;", $refcnt - 1 )) if $refcnt > 1;
+
+    return $sym if $is_empty;
+
+    # XXX B::walksymtable creates an extra reference to the GV
     my $gvrefcnt = $gv->GvREFCNT;
     if ($gvrefcnt > 1) {
        $init->add(sprintf("GvREFCNT($sym) += %u;", $gvrefcnt - 1));
     }
-    if (defined($egvsym)) {
+    # some non-alphavetic globs require some parts to be saved
+    # ( ex. %!, but not $! )
+    sub Save_HV() { 1 }
+    sub Save_AV() { 2 }
+    sub Save_SV() { 4 }
+    sub Save_CV() { 8 }
+    sub Save_FORM() { 16 }
+    sub Save_IO() { 32 }
+    my $savefields = 0;
+    if( $gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/ ) {
+        $savefields = Save_HV|Save_AV|Save_SV|Save_CV|Save_FORM|Save_IO;
+    }
+    elsif( $gvname eq '!' ) {
+        $savefields = Save_HV;
+    }
+    # attributes::bootstrap is created in perl_parse
+    # saving it would overwrite it, because perl_init() is
+    # called after perl_parse()
+    $savefields&=~Save_CV if $fullname eq 'attributes::bootstrap';
+
+    # save it
+    # XXX is that correct?
+    if (defined($egvsym) && $egvsym !~ m/Null/ ) {
        # Shared glob *foo = *bar
        $init->add("gp_free($sym);",
                   "GvGP($sym) = GvGP($egvsym);");
-    } elsif ($gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/) {
+    } elsif ($savefields) {
        # Don't save subfields of special GVs (*_, *1, *# and so on)
 #      warn "GV::save saving subfields\n"; # debug
        my $gvsv = $gv->SV;
-       if ($$gvsv) {
+       if ($$gvsv && $savefields&Save_SV) {
+           $gvsv->save;
            $init->add(sprintf("GvSV($sym) = s\\_%x;", $$gvsv));
 #          warn "GV::save \$$name\n"; # debug
-           $gvsv->save;
        }
        my $gvav = $gv->AV;
-       if ($$gvav) {
+       if ($$gvav && $savefields&Save_AV) {
+           $gvav->save;
            $init->add(sprintf("GvAV($sym) = s\\_%x;", $$gvav));
 #          warn "GV::save \@$name\n"; # debug
-           $gvav->save;
        }
        my $gvhv = $gv->HV;
-       if ($$gvhv) {
+       if ($$gvhv && $savefields&Save_HV) {
+           $gvhv->save;
            $init->add(sprintf("GvHV($sym) = s\\_%x;", $$gvhv));
 #          warn "GV::save \%$name\n"; # debug
-           $gvhv->save;
        }
        my $gvcv = $gv->CV;
-       if ($$gvcv) {
-           $init->add(sprintf("GvCV($sym) = (CV*)s\\_%x;", $$gvcv));
-#          warn "GV::save &$name\n"; # debug
-           $gvcv->save;
-       }
-       my $gvfilegv = $gv->FILEGV;
-       if ($$gvfilegv) {
-           $init->add(sprintf("GvFILEGV($sym) = (GV*)s\\_%x;",$$gvfilegv));
-#          warn "GV::save GvFILEGV(*$name)\n"; # debug
-           $gvfilegv->save;
-       }
+       if ($$gvcv && $savefields&Save_CV) {
+           my $origname=cstring($gvcv->GV->EGV->STASH->NAME .
+                "::" . $gvcv->GV->EGV->NAME);  
+           if (0 && $gvcv->XSUB && $name ne $origname) { #XSUB alias
+               # must save as a 'stub' so newXS() has a CV to populate
+                $init->add("{ CV *cv;");
+                $init->add("\tcv=perl_get_cv($origname,TRUE);");
+                $init->add("\tGvCV($sym)=cv;");
+                $init->add("\tSvREFCNT_inc((SV *)cv);");
+                $init->add("}");    
+           } else {
+               $init->add(sprintf("GvCV($sym) = (CV*)(%s);", $gvcv->save));
+#              warn "GV::save &$name\n"; # debug
+           } 
+        }     
+       $init->add(sprintf("GvFILE($sym) = %s;", cstring($gv->FILE)));
+#      warn "GV::save GvFILE(*$name)\n"; # debug
        my $gvform = $gv->FORM;
-       if ($$gvform) {
+       if ($$gvform && $savefields&Save_FORM) {
+           $gvform->save;
            $init->add(sprintf("GvFORM($sym) = (CV*)s\\_%x;", $$gvform));
 #          warn "GV::save GvFORM(*$name)\n"; # debug
-           $gvform->save;
        }
        my $gvio = $gv->IO;
-       if ($$gvio) {
+       if ($$gvio && $savefields&Save_IO) {
+           $gvio->save;
            $init->add(sprintf("GvIOp($sym) = s\\_%x;", $$gvio));
+            if( $fullname =~ m/::DATA$/ && $save_data_fh ) {
+                no strict 'refs';
+                my $fh = *{$fullname}{IO};
+                use strict 'refs';
+                $gvio->save_data( $fullname, <$fh> ) if $fh->opened;
+            }
 #          warn "GV::save GvIO(*$name)\n"; # debug
-           $gvio->save;
        }
     }
     return $sym;
 }
+
 sub B::AV::save {
     my ($av) = @_;
     my $sym = objsym($av);
@@ -723,7 +1185,7 @@ sub B::AV::save {
     $xpvavsect->add(sprintf("0, -1, -1, 0, 0.0, 0, Nullhv, 0, 0, 0x%x",
                            $avflags));
     $svsect->add(sprintf("&xpvav_list[%d], %lu, 0x%x",
-                        $xpvavsect->index, $av->REFCNT + 1, $av->FLAGS));
+                        $xpvavsect->index, $av->REFCNT  , $av->FLAGS));
     my $sv_list_index = $svsect->index;
     my $fill = $av->FILL;
     $av->save_magic;
@@ -741,18 +1203,38 @@ sub B::AV::save {
                             $$av, $i++, class($el), $$el);
            }
        }
-       my @names = map($_->save, @array);
+#      my @names = map($_->save, @array);
        # XXX Better ways to write loop?
        # Perhaps svp[0] = ...; svp[1] = ...; svp[2] = ...;
        # Perhaps I32 i = 0; svp[i++] = ...; svp[i++] = ...; svp[i++] = ...;
+
+        # micro optimization: op/pat.t ( and other code probably )
+        # has very large pads ( 20k/30k elements ) passing them to
+        # ->add is a performance bottleneck: passing them as a
+        # single string cuts runtime from 6min20sec to 40sec
+
+        # you want to keep this out of the no_split/split
+        # map("\t*svp++ = (SV*)$_;", @names),
+        my $acc = '';
+        foreach my $i ( 0..$#array ) {
+              $acc .= "\t*svp++ = (SV*)" . $array[$i]->save . ";\n\t";
+        }
+        $acc .= "\n";
+
+        $init->no_split;
        $init->add("{",
                   "\tSV **svp;",
                   "\tAV *av = (AV*)&sv_list[$sv_list_index];",
                   "\tav_extend(av, $fill);",
-                  "\tsvp = AvARRAY(av);",
-              map("\t*svp++ = (SV*)$_;", @names),
-                  "\tAvFILLp(av) = $fill;",
+                  "\tsvp = AvARRAY(av);" );
+        $init->add($acc);
+       $init->add("\tAvFILLp(av) = $fill;",
                   "}");
+        $init->split;
+        # we really added a lot of lines ( B::C::InitSection->add
+        # should really scan for \n, but that would slow
+        # it down
+        $init->inc_count( $#array );
     } else {
        my $max = $av->MAX;
        $init->add("av_extend((AV*)&sv_list[$sv_list_index], $max);")
@@ -789,7 +1271,7 @@ sub B::HV::save {
     $xpvhvsect->add(sprintf("0, 0, %d, 0, 0.0, 0, Nullhv, %d, 0, 0, 0",
                            $hv->MAX, $hv->RITER));
     $svsect->add(sprintf("&xpvhv_list[%d], %lu, 0x%x",
-                        $xpvhvsect->index, $hv->REFCNT + 1, $hv->FLAGS));
+                        $xpvhvsect->index, $hv->REFCNT  , $hv->FLAGS));
     my $sv_list_index = $svsect->index;
     my @contents = $hv->ARRAY;
     if (@contents) {
@@ -797,22 +1279,47 @@ sub B::HV::save {
        for ($i = 1; $i < @contents; $i += 2) {
            $contents[$i] = $contents[$i]->save;
        }
+        $init->no_split;
        $init->add("{", "\tHV *hv = (HV*)&sv_list[$sv_list_index];");
        while (@contents) {
            my ($key, $value) = splice(@contents, 0, 2);
            $init->add(sprintf("\thv_store(hv, %s, %u, %s, %s);",
-                              cstring($key),length($key),$value, hash($key)));
+                              cstring($key),length(pack "a*",$key),
+                               $value, hash($key)));
+#          $init->add(sprintf("\thv_store(hv, %s, %u, %s, %s);",
+#                             cstring($key),length($key),$value, 0));
        }
        $init->add("}");
+        $init->split;
     }
+    $hv->save_magic();
     return savesym($hv, "(HV*)&sv_list[$sv_list_index]");
 }
 
+sub B::IO::save_data {
+    my( $io, $globname, @data ) = @_;
+    my $data = join '', @data;
+
+    # XXX using $DATA might clobber it!
+    my $sym = svref_2object( \\$data )->save;
+    $init->add( split /\n/, <<CODE );
+    {
+        GV* gv = (GV*)gv_fetchpv( "$globname", TRUE, SVt_PV );
+        SV* sv = $sym;
+        GvSV( gv ) = sv;
+    }
+CODE
+    # for PerlIO::scalar
+    $use_xsloader = 1;
+    $init->add_eval( sprintf 'open(%s, "<", $%s)', $globname, $globname );
+}
+
 sub B::IO::save {
     my ($io) = @_;
     my $sym = objsym($io);
     return $sym if defined $sym;
     my $pv = $io->PV;
+    $pv = '' unless defined $pv;
     my $len = length($pv);
     $xpviosect->add(sprintf("0, %u, %u, %d, %s, 0, 0, 0, 0, 0, %d, %d, %d, %d, %s, Nullgv, %s, Nullgv, %s, Nullgv, %d, %s, 0x%x",
                            $len, $len+1, $io->IVX, $io->NVX, $io->LINES,
@@ -821,8 +1328,18 @@ sub B::IO::save {
                            cstring($io->BOTTOM_NAME), $io->SUBPROCESS,
                            cchar($io->IoTYPE), $io->IoFLAGS));
     $svsect->add(sprintf("&xpvio_list[%d], %lu, 0x%x",
-                        $xpviosect->index, $io->REFCNT + 1, $io->FLAGS));
+                        $xpviosect->index, $io->REFCNT , $io->FLAGS));
     $sym = savesym($io, sprintf("(IO*)&sv_list[%d]", $svsect->index));
+    # deal with $x = *STDIN/STDOUT/STDERR{IO}
+    my $perlio_func;
+    foreach ( qw(stdin stdout stderr) ) {
+        $io->IsSTD($_) and $perlio_func = $_;
+    }
+    if( $perlio_func ) {
+        $init->add( "IoIFP(${sym})=PerlIO_${perlio_func}();" );
+        $init->add( "IoOFP(${sym})=PerlIO_${perlio_func}();" );
+    }
+
     my ($field, $fsym);
     foreach $field (qw(TOP_GV FMT_GV BOTTOM_GV)) {
        $fsym = $io->$field();
@@ -848,8 +1365,8 @@ sub output_all {
     my $init_name = shift;
     my $section;
     my @sections = ($opsect, $unopsect, $binopsect, $logopsect, $condopsect,
-                   $listopsect, $pmopsect, $svopsect, $gvopsect, $pvopsect,
-                   $cvopsect, $loopsect, $copsect, $svsect, $xpvsect,
+                   $listopsect, $pmopsect, $svopsect, $padopsect, $pvopsect,
+                   $loopsect, $copsect, $svsect, $xpvsect,
                    $xpvavsect, $xpvhvsect, $xpvcvsect, $xpvivsect, $xpvnvsect,
                    $xpvmgsect, $xpvlvsect, $xrvsect, $xpvbmsect, $xpviosect);
     $symsect->output(\*STDOUT, "#define %s\n");
@@ -863,6 +1380,9 @@ sub output_all {
            print "Static $typename ${name}_list[$lines];\n";
        }
     }
+    # XXX hack for when Perl accesses PVX of GVs
+    print 'Static char emptystring[] = "\0";';
+
     $decl->output(\*STDOUT, "%s\n");
     print "\n";
     foreach $section (@sections) {
@@ -871,18 +1391,12 @@ sub output_all {
            my $name = $section->name;
            my $typename = ($name eq "xpvcv") ? "XPVCV_or_similar" : uc($name);
            printf "static %s %s_list[%u] = {\n", $typename, $name, $lines;
-           $section->output(\*STDOUT, "\t{ %s },\n");
+           $section->output(\*STDOUT, "\t{ %s }, /* %d */\n");
            print "};\n\n";
        }
     }
 
-    print <<"EOT";
-static int $init_name()
-{
-       dTHR;
-EOT
-    $init->output(\*STDOUT, "\t%s\n");
-    print "\treturn 0;\n}\n";
+    $init->output(\*STDOUT, "\t%s\n", $init_name );
     if ($verbose) {
        warn compile_stats();
        warn "NULLOP count: $nullop_count\n";
@@ -907,25 +1421,25 @@ typedef struct {
     STRLEN     xpv_cur;        /* length of xp_pv as a C string */
     STRLEN     xpv_len;        /* allocated size */
     IV         xof_off;        /* integer value */
-    double     xnv_nv;         /* numeric value, if any */
+    NV         xnv_nv;         /* numeric value, if any */
     MAGIC*     xmg_magic;      /* magic for scalar array */
     HV*                xmg_stash;      /* class package */
 
     HV *       xcv_stash;
     OP *       xcv_start;
     OP *       xcv_root;
-    void      (*xcv_xsub) _((CV*));
-    void *     xcv_xsubany;
+    void      (*xcv_xsub) (pTHX_ CV*);
+    ANY                xcv_xsubany;
     GV *       xcv_gv;
-    GV *       xcv_filegv;
-    long       xcv_depth;              /* >= 2 indicates recursive call */
+    char *     xcv_file;
+    long       xcv_depth;      /* >= 2 indicates recursive call */
     AV *       xcv_padlist;
     CV *       xcv_outside;
-#ifdef USE_THREADS
+#ifdef USE_5005THREADS
     perl_mutex *xcv_mutexp;
     struct perl_thread *xcv_owner;     /* current owner thread */
-#endif /* USE_THREADS */
-    U8         xcv_flags;
+#endif /* USE_5005THREADS */
+    cv_flags_t xcv_flags;
 } XPVCV_or_similar;
 #define ANYINIT(i) i
 #else
@@ -936,7 +1450,6 @@ typedef struct {
 
 #define UNUSED 0
 #define sym_0 0
-
 EOT
     print "static GV *gv_list[$gv_index];\n" if $gv_index;
     print "\n";
@@ -947,95 +1460,268 @@ sub output_boilerplate {
     print <<'EOT';
 #include "EXTERN.h"
 #include "perl.h"
-#ifndef PATCHLEVEL
-#include "patchlevel.h"
-#endif
+#include "XSUB.h"
 
 /* Workaround for mapstart: the only op which needs a different ppaddr */
-#undef pp_mapstart
-#define pp_mapstart pp_grepstart
-
-static void xs_init _((void));
+#undef Perl_pp_mapstart
+#define Perl_pp_mapstart Perl_pp_grepstart
+#undef OP_MAPSTART
+#define OP_MAPSTART OP_GREPSTART
+#define XS_DynaLoader_boot_DynaLoader boot_DynaLoader
+EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
+
+static void xs_init (pTHX);
+static void dl_init (pTHX);
 static PerlInterpreter *my_perl;
 EOT
 }
 
+sub init_op_addr {
+    my( $op_type, $num ) = @_;
+    my $op_list = $op_type."_list";
+
+    $init->add( split /\n/, <<EOT );
+    {
+        int i;
+
+        for( i = 0; i < ${num}; ++i )
+        {
+            ${op_list}\[i].op_ppaddr = PL_ppaddr[INT2PTR(int,${op_list}\[i].op_ppaddr)];
+        }
+    }
+EOT
+}
+
+sub init_op_warn {
+    my( $op_type, $num ) = @_;
+    my $op_list = $op_type."_list";
+
+    # for resons beyond imagination, MSVC5 considers pWARN_ALL non-const
+    $init->add( split /\n/, <<EOT );
+    {
+        int i;
+
+        for( i = 0; i < ${num}; ++i )
+        {
+            switch( (int)(${op_list}\[i].cop_warnings) )
+            {
+            case 1:
+                ${op_list}\[i].cop_warnings = pWARN_ALL;
+                break;
+            case 2:
+                ${op_list}\[i].cop_warnings = pWARN_NONE;
+                break;
+            case 3:
+                ${op_list}\[i].cop_warnings = pWARN_STD;
+                break;
+            default:
+                break;
+            }
+        }
+    }
+EOT
+}
+
 sub output_main {
     print <<'EOT';
+/* if USE_IMPLICIT_SYS, we need a 'real' exit */
+#if defined(exit)
+#undef exit
+#endif
+
 int
-#ifndef CAN_PROTOTYPE
-main(argc, argv, env)
-int argc;
-char **argv;
-char **env;
-#else  /* def(CAN_PROTOTYPE) */
 main(int argc, char **argv, char **env)
-#endif  /* def(CAN_PROTOTYPE) */
 {
     int exitstatus;
     int i;
     char **fakeargv;
+    GV* tmpgv;
+    SV* tmpsv;
+    int options_count;
 
-    PERL_SYS_INIT(&argc,&argv);
-    perl_init_i18nl10n(1);
+    PERL_SYS_INIT3(&argc,&argv,&env);
 
     if (!PL_do_undump) {
        my_perl = perl_alloc();
        if (!my_perl)
            exit(1);
        perl_construct( my_perl );
+       PL_perl_destruct_level = 0;
+    }
+EOT
+    if( $ithreads ) {
+        # XXX init free elems!
+        my $pad_len = regex_padav->FILL + 1 - 1; # first is an avref
+
+        print <<EOT;
+#ifdef USE_ITHREADS
+    for( i = 0; i < $pad_len; ++i ) {
+        av_push( PL_regex_padav, newSViv(0) );
+    }
+    PL_regex_pad = AvARRAY( PL_regex_padav );
+#endif
+EOT
     }
 
+    print <<'EOT';
 #ifdef CSH
     if (!PL_cshlen) 
       PL_cshlen = strlen(PL_cshname);
 #endif
 
 #ifdef ALLOW_PERL_OPTIONS
-#define EXTRA_OPTIONS 2
-#else
 #define EXTRA_OPTIONS 3
+#else
+#define EXTRA_OPTIONS 4
 #endif /* ALLOW_PERL_OPTIONS */
     New(666, fakeargv, argc + EXTRA_OPTIONS + 1, char *);
+
     fakeargv[0] = argv[0];
     fakeargv[1] = "-e";
     fakeargv[2] = "";
+    options_count = 3;
+EOT
+    # honour -T
+    print <<EOT;
+    if( ${^TAINT} ) {
+        fakeargv[options_count] = "-T";
+        ++options_count;
+    }
+EOT
+    print <<'EOT';
 #ifndef ALLOW_PERL_OPTIONS
-    fakeargv[3] = "--";
+    fakeargv[options_count] = "--";
+    ++options_count;
 #endif /* ALLOW_PERL_OPTIONS */
     for (i = 1; i < argc; i++)
-       fakeargv[i + EXTRA_OPTIONS] = argv[i];
-    fakeargv[argc + EXTRA_OPTIONS] = 0;
-    
-    exitstatus = perl_parse(my_perl, xs_init, argc + EXTRA_OPTIONS,
+       fakeargv[i + options_count - 1] = argv[i];
+    fakeargv[argc + options_count - 1] = 0;
+
+    exitstatus = perl_parse(my_perl, xs_init, argc + options_count - 1,
                            fakeargv, NULL);
+
     if (exitstatus)
        exit( exitstatus );
 
-    sv_setpv(GvSV(gv_fetchpv("0", TRUE, SVt_PV)), argv[0]);
-    PL_main_cv = PL_compcv;
+    TAINT;
+EOT
+
+    if( $use_perl_script_name ) {
+        my $dollar_0 = $0;
+        $dollar_0 =~ s/\\/\\\\/g;
+        $dollar_0 = '"' . $dollar_0 . '"';
+
+        print <<EOT;
+    if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {/* $0 */
+        tmpsv = GvSV(tmpgv);
+        sv_setpv(tmpsv, ${dollar_0});
+        SvSETMAGIC(tmpsv);
+    }
+EOT
+    }
+
+    print <<'EOT';
+    if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
+        tmpsv = GvSV(tmpgv);
+#ifdef WIN32
+        sv_setpv(tmpsv,"perl.exe");
+#else
+        sv_setpv(tmpsv,"perl");
+#endif
+        SvSETMAGIC(tmpsv);
+    }
+
+    TAINT_NOT;
+
+    /* PL_main_cv = PL_compcv; */
     PL_compcv = 0;
 
     exitstatus = perl_init();
     if (exitstatus)
        exit( exitstatus );
+    dl_init(aTHX);
 
     exitstatus = perl_run( my_perl );
 
     perl_destruct( my_perl );
     perl_free( my_perl );
 
+    PERL_SYS_TERM();
+
     exit( exitstatus );
 }
 
+/* yanked from perl.c */
 static void
-xs_init()
+xs_init(pTHX)
 {
-}
+    char *file = __FILE__;
+    dTARG;
+    dSP;
+EOT
+    print "\n#ifdef USE_DYNAMIC_LOADING";
+    print qq/\n\tnewXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);/;
+    print "\n#endif\n" ;
+    # delete $xsub{'DynaLoader'}; 
+    delete $xsub{'UNIVERSAL'}; 
+    print("/* bootstrapping code*/\n\tSAVETMPS;\n");
+    print("\ttarg=sv_newmortal();\n");
+    print "#ifdef USE_DYNAMIC_LOADING\n";
+    print "\tPUSHMARK(sp);\n";
+    print qq/\tXPUSHp("DynaLoader",strlen("DynaLoader"));\n/;
+    print qq/\tPUTBACK;\n/;
+    print "\tboot_DynaLoader(aTHX_ NULL);\n";
+    print qq/\tSPAGAIN;\n/;
+    print "#endif\n";
+    foreach my $stashname (keys %xsub){
+       if ($xsub{$stashname} !~ m/Dynamic/ ) {
+          my $stashxsub=$stashname;
+          $stashxsub  =~ s/::/__/g; 
+          print "\tPUSHMARK(sp);\n";
+          print qq/\tXPUSHp("$stashname",strlen("$stashname"));\n/;
+          print qq/\tPUTBACK;\n/;
+          print "\tboot_$stashxsub(aTHX_ NULL);\n";
+          print qq/\tSPAGAIN;\n/;
+       }   
+    }
+    print("\tFREETMPS;\n/* end bootstrapping code */\n");
+    print "}\n";
+    
+print <<'EOT';
+static void
+dl_init(pTHX)
+{
+    char *file = __FILE__;
+    dTARG;
+    dSP;
 EOT
+    print("/* Dynamicboot strapping code*/\n\tSAVETMPS;\n");
+    print("\ttarg=sv_newmortal();\n");
+    foreach my $stashname (@DynaLoader::dl_modules) {
+       warn "Loaded $stashname\n";
+       if (exists($xsub{$stashname}) && $xsub{$stashname} =~ m/Dynamic/) {
+          my $stashxsub=$stashname;
+          $stashxsub  =~ s/::/__/g; 
+          print "\tPUSHMARK(sp);\n";
+          print qq/\tXPUSHp("$stashname",/,length($stashname),qq/);\n/;
+          print qq/\tPUTBACK;\n/;
+           print "#ifdef USE_DYNAMIC_LOADING\n";
+          warn "bootstrapping $stashname added to xs_init\n";
+           if( $xsub{$stashname} eq 'Dynamic' ) {
+              print qq/\tperl_call_method("bootstrap",G_DISCARD);\n/;
+           }
+           else {
+              print qq/\tperl_call_pv("XSLoader::load",G_DISCARD);\n/;
+           }
+           print "#else\n";
+          print "\tboot_$stashxsub(aTHX_ NULL);\n";
+           print "#endif\n";
+          print qq/\tSPAGAIN;\n/;
+       }   
+    }
+    print("\tFREETMPS;\n/* end Dynamic bootstrapping code */\n");
+    print "}\n";
 }
-
 sub dump_symtable {
     # For debugging
     my ($sym, $val);
@@ -1051,90 +1737,223 @@ sub save_object {
     foreach $sv (@_) {
        svref_2object($sv)->save;
     }
+}       
+
+sub Dummy_BootStrap { }            
+
+sub B::GV::savecv 
+{
+ my $gv = shift;
+ my $package=$gv->STASH->NAME;
+ my $name = $gv->NAME;
+ my $cv = $gv->CV;
+ my $sv = $gv->SV;
+ my $av = $gv->AV;
+ my $hv = $gv->HV;
+
+ my $fullname = $gv->STASH->NAME . "::" . $gv->NAME;
+
+ # We may be looking at this package just because it is a branch in the 
+ # symbol table which is on the path to a package which we need to save
+ # e.g. this is 'Getopt' and we need to save 'Getopt::Long'
+ # 
+ return unless ($unused_sub_packages{$package});
+ return unless ($$cv || $$av || $$sv || $$hv);
+ $gv->save;
 }
 
-sub B::GV::savecv {
-    my $gv = shift;
-    my $cv = $gv->CV;
-    my $name = $gv->NAME;
-    if ($$cv && !objsym($cv) && !($name eq "bootstrap" && $cv->XSUB)) {
-       if ($debug_cv) {
-           warn sprintf("saving extra CV &%s::%s (0x%x) from GV 0x%x\n",
-                        $gv->STASH->NAME, $name, $$cv, $$gv);
-       }
-      my $package=$gv->STASH->NAME;
-      if ( ! grep(/^$package$/,@unused_sub_packages)){
-          warn sprintf("omitting cv in superclass %s", $gv->STASH->NAME) 
-              if $debug_cv;
-          return ;
+sub mark_package
+{    
+ my $package = shift;
+ unless ($unused_sub_packages{$package})
+  {    
+   no strict 'refs';
+   $unused_sub_packages{$package} = 1;
+   if (defined @{$package.'::ISA'})
+    {
+     foreach my $isa (@{$package.'::ISA'}) 
+      {
+       if ($isa eq 'DynaLoader')
+        {
+         unless (defined(&{$package.'::bootstrap'}))
+          {                    
+           warn "Forcing bootstrap of $package\n";
+           eval { $package->bootstrap }; 
+          }
+        }
+#      else
+        {
+         unless ($unused_sub_packages{$isa})
+          {
+           warn "$isa saved (it is in $package\'s \@ISA)\n";
+           mark_package($isa);
+          }
+        }
       }
-       $gv->save;
     }
-    elsif ($name eq 'ISA')
-     {
-      $gv->save;
-     }
-
-}
-
-sub save_unused_subs {
-    my %search_pack;
-    map { $search_pack{$_} = 1 } @_;
-    @unused_sub_packages=@_;
-    no strict qw(vars refs);
-    walksymtable(\%{"main::"}, "savecv", sub {
-       my $package = shift;
-       $package =~ s/::$//;
-       return 0 if ($package =~ /::::/);  # skip ::::ISA::CACHE etc.
-       #warn "Considering $package\n";#debug
-       return 1 if exists $search_pack{$package};
-      #sub try for a partial match
-      if (grep(/^$package\:\:/,@unused_sub_packages)){ 
-          return 1;   
-      }       
-       #warn "    (nothing explicit)\n";#debug
-       # Omit the packages which we use (and which cause grief
-       # because of fancy "goto &$AUTOLOAD" stuff).
-       # XXX Surely there must be a nicer way to do this.
-       if ($package eq "FileHandle"
-           || $package eq "Config"
-           || $package eq "SelectSaver") {
-           return 0;
-       }
-       foreach my $u (keys %search_pack) {
-           if ($package =~ /^${u}::/) {
-               warn "$package starts with $u\n";
-               return 1
-           }
-           if ($package->isa($u)) {
-               warn "$package isa $u\n";
-               return 1
-           }
-           return 1 if $package->isa($u);
-       }
-       foreach my $m (qw(new DESTROY TIESCALAR TIEARRAY TIEHASH)) {
-           if (defined(&{$package."::$m"})) {
-               warn "$package has method $m: -u$package assumed\n";#debug
-              push @unused_sub_package, $package;
-               return 1;
-           }
-       }
-       return 0;
-    });
+  }
+ return 1;
+}
+     
+sub should_save
+{
+ no strict qw(vars refs);
+ my $package = shift;
+ $package =~ s/::$//;
+ return $unused_sub_packages{$package} = 0 if ($package =~ /::::/);  # skip ::::ISA::CACHE etc.
+ # warn "Considering $package\n";#debug
+ foreach my $u (grep($unused_sub_packages{$_},keys %unused_sub_packages)) 
+  {  
+   # If this package is a prefix to something we are saving, traverse it 
+   # but do not mark it for saving if it is not already
+   # e.g. to get to Getopt::Long we need to traverse Getopt but need
+   # not save Getopt
+   return 1 if ($u =~ /^$package\:\:/);
+  }
+ if (exists $unused_sub_packages{$package})
+  {
+   # warn "Cached $package is ".$unused_sub_packages{$package}."\n"; 
+   delete_unsaved_hashINC($package) unless  $unused_sub_packages{$package} ;
+   return $unused_sub_packages{$package}; 
+  }
+ # Omit the packages which we use (and which cause grief
+ # because of fancy "goto &$AUTOLOAD" stuff).
+ # XXX Surely there must be a nicer way to do this.
+ if ($package eq "FileHandle" || $package eq "Config" || 
+     $package eq "SelectSaver" || $package =~/^(B|IO)::/) 
+  {
+   delete_unsaved_hashINC($package);
+   return $unused_sub_packages{$package} = 0;
+  }
+ # Now see if current package looks like an OO class this is probably too strong.
+ foreach my $m (qw(new DESTROY TIESCALAR TIEARRAY TIEHASH TIEHANDLE)) 
+  {
+   if (UNIVERSAL::can($package, $m))
+    {
+     warn "$package has method $m: saving package\n";#debug
+     return mark_package($package);
+    }
+  }
+ delete_unsaved_hashINC($package);
+ return $unused_sub_packages{$package} = 0;
+}
+sub delete_unsaved_hashINC{
+       my $packname=shift;
+       $packname =~ s/\:\:/\//g;
+       $packname .= '.pm';
+#      warn "deleting $packname" if $INC{$packname} ;# debug
+       delete $INC{$packname};
+}
+sub walkpackages 
+{
+ my ($symref, $recurse, $prefix) = @_;
+ my $sym;
+ my $ref;
+ no strict 'vars';
+ local(*glob);
+ $prefix = '' unless defined $prefix;
+ while (($sym, $ref) = each %$symref) 
+  {             
+   *glob = $ref;
+   if ($sym =~ /::$/) 
+    {
+     $sym = $prefix . $sym;
+     if ($sym ne "main::" && $sym ne "<none>::" && &$recurse($sym)) 
+      {
+       walkpackages(\%glob, $recurse, $sym);
+      }
+    } 
+  }
+}
+
+
+sub save_unused_subs 
+{
+ no strict qw(refs);
+ &descend_marked_unused;
+ warn "Prescan\n";
+ walkpackages(\%{"main::"}, sub { should_save($_[0]); return 1 });
+ warn "Saving methods\n";
+ walksymtable(\%{"main::"}, "savecv", \&should_save);
 }
 
+sub save_context
+{
+ my $curpad_nam = (comppadlist->ARRAY)[0]->save;
+ my $curpad_sym = (comppadlist->ARRAY)[1]->save;
+ my $inc_hv     = svref_2object(\%INC)->save;
+ my $inc_av     = svref_2object(\@INC)->save;
+ my $amagic_generate= amagic_generation;          
+ $init->add(   "PL_curpad = AvARRAY($curpad_sym);",
+              "GvHV(PL_incgv) = $inc_hv;",
+              "GvAV(PL_incgv) = $inc_av;",
+               "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
+               "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));",
+               "PL_amagic_generation= $amagic_generate;" );
+}
+
+sub descend_marked_unused {
+    foreach my $pack (keys %unused_sub_packages)
+    {
+       mark_package($pack);
+    }
+}
 sub save_main {
-    my $curpad_nam = (comppadlist->ARRAY)[0]->save;
-    my $curpad_sym = (comppadlist->ARRAY)[1]->save;
+    # this is mainly for the test suite
+    my $warner = $SIG{__WARN__};
+    local $SIG{__WARN__} = sub { print STDERR @_ };
+
+    warn "Starting compile\n";
+    warn "Walking tree\n";
+    seek(STDOUT,0,0); #exclude print statements in BEGIN{} into output
     walkoptree(main_root, "save");
     warn "done main optree, walking symtable for extras\n" if $debug_cv;
-    save_unused_subs(@unused_sub_packages);
-
+    save_unused_subs();
+    # XSLoader was used, force saving of XSLoader::load
+    if( $use_xsloader ) {
+        my $cv = svref_2object( \&XSLoader::load );
+        $cv->save;
+    }
+    # save %SIG ( in case it was set in a BEGIN block )
+    if( $save_sig ) {
+        local $SIG{__WARN__} = $warner;
+        $init->no_split;
+        $init->add("{", "\tHV* hv = get_hv(\"main::SIG\",1);" );
+        foreach my $k ( keys %SIG ) {
+            next unless ref $SIG{$k};
+            my $cv = svref_2object( \$SIG{$k} );
+            my $sv = $cv->save;
+            $init->add('{',sprintf 'SV* sv = (SV*)%s;', $sv );
+            $init->add(sprintf("\thv_store(hv, %s, %u, %s, %s);",
+                               cstring($k),length(pack "a*",$k),
+                               'sv', hash($k)));
+            $init->add('mg_set(sv);','}');
+        }
+        $init->add('}');
+        $init->split;
+    }
+    # honour -w
+    $init->add( sprintf "    PL_dowarn = ( %s ) ? G_WARN_ON : G_WARN_OFF;", $^W );
+    #
+    my $init_av = init_av->save;
+    my $end_av = end_av->save;
     $init->add(sprintf("PL_main_root = s\\_%x;", ${main_root()}),
               sprintf("PL_main_start = s\\_%x;", ${main_start()}),
-              "PL_curpad = AvARRAY($curpad_sym);",
-               "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
-               "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));");
+              "PL_initav = (AV *) $init_av;",
+              "PL_endav = (AV*) $end_av;");
+    save_context();
+    # init op addrs ( must be the last action, otherwise
+    # some ops might not be initialized
+    if( $optimize_ppaddr ) {
+        foreach my $i ( @op_sections ) {
+            my $section = $$i;
+            next unless $section->index >= 0;
+            init_op_addr( $section->name, $section->index + 1);
+        }
+    }
+    init_op_warn( $copsect->name, $copsect->index + 1)
+      if $optimize_warn_sv && $copsect->index >= 0;
 
     warn "Writing output\n";
     output_boilerplate();
@@ -1145,9 +1964,9 @@ sub save_main {
 }
 
 sub init_sections {
-    my @sections = (init => \$init, decl => \$decl, sym => \$symsect,
+    my @sections = (decl => \$decl, sym => \$symsect,
                    binop => \$binopsect, condop => \$condopsect,
-                   cop => \$copsect, cvop => \$cvopsect, gvop => \$gvopsect,
+                   cop => \$copsect, padop => \$padopsect,
                    listop => \$listopsect, logop => \$logopsect,
                    loop => \$loopsect, op => \$opsect, pmop => \$pmopsect,
                    pvop => \$pvopsect, svop => \$svopsect, unop => \$unopsect,
@@ -1159,13 +1978,32 @@ sub init_sections {
                    xpvio => \$xpviosect);
     my ($name, $sectref);
     while (($name, $sectref) = splice(@sections, 0, 2)) {
-       $$sectref = new B::Section $name, \%symtable, 0;
+       $$sectref = new B::C::Section $name, \%symtable, 0;
     }
+    $init = new B::C::InitSection 'init', \%symtable, 0;
+}
+
+sub mark_unused
+{
+ my ($arg,$val) = @_;
+ $unused_sub_packages{$arg} = $val;
 }
 
 sub compile {
     my @options = @_;
     my ($option, $opt, $arg);
+    my @eval_at_startup;
+    my %option_map = ( 'cog' => \$pv_copy_on_grow,
+                       'save-data' => \$save_data_fh,
+                       'ppaddr' => \$optimize_ppaddr,
+                       'warn-sv' => \$optimize_warn_sv,
+                       'use-script-name' => \$use_perl_script_name,
+                       'save-sig-hash' => \$save_sig,
+                     );
+    my %optimization_map = ( 0 => [ qw() ], # special case
+                             1 => [ qw(-fcog) ],
+                             2 => [ qw(-fwarn-sv -fppaddr) ],
+                           );
   OPTION:
     while ($option = shift @options) {
        if ($option =~ /^-(.)(.*)/) {
@@ -1205,24 +2043,35 @@ sub compile {
            $verbose = 1;
        } elsif ($opt eq "u") {
            $arg ||= shift @options;
-           push(@unused_sub_packages, $arg);
+           mark_unused($arg,undef);
        } elsif ($opt eq "f") {
            $arg ||= shift @options;
-           if ($arg eq "cog") {
-               $pv_copy_on_grow = 1;
-           } elsif ($arg eq "no-cog") {
-               $pv_copy_on_grow = 0;
-           }
+            $arg =~ m/(no-)?(.*)/;
+            my $no = defined($1) && $1 eq 'no-';
+            $arg = $no ? $2 : $arg;
+            if( exists $option_map{$arg} ) {
+                ${$option_map{$arg}} = !$no;
+            } else {
+                die "Invalid optimization '$arg'";
+            }
        } elsif ($opt eq "O") {
            $arg = 1 if $arg eq "";
-           $pv_copy_on_grow = 0;
-           if ($arg >= 1) {
-               # Optimisations for -O1
-               $pv_copy_on_grow = 1;
-           }
+            my @opt;
+            foreach my $i ( 1 .. $arg ) {
+                push @opt, @{$optimization_map{$i}}
+                    if exists $optimization_map{$i};
+            }
+            unshift @options, @opt;
+        } elsif ($opt eq "e") {
+            push @eval_at_startup, $arg;
+       } elsif ($opt eq "l") {
+           $max_string_len = $arg;
        }
     }
     init_sections();
+    foreach my $i ( @eval_at_startup ) {
+        $init->add_eval( $i );
+    }
     if (@options) {
        return sub {
            my $objname;
@@ -1320,20 +2169,66 @@ prints MAGIC information on saving
 
 =item B<-f>
 
-Force optimisations on or off one at a time.
+Force options/optimisations on or off one at a time. You can explicitly
+disable an option using B<-fno-option>. All options default to
+B<disabled>.
+
+=over 4
 
 =item B<-fcog>
 
 Copy-on-grow: PVs declared and initialised statically.
 
-=item B<-fno-cog>
+=item B<-fsave-data>
+
+Save package::DATA filehandles ( only available with PerlIO ).
+
+=item B<-fppaddr>
+
+Optimize the initialization of op_ppaddr.
 
-No copy-on-grow.
+=item B<-fwarn-sv>
+
+Optimize the initialization of cop_warnings.
+
+=item B<-fuse-script-name>
+
+Use the script name instead of the program name as $0.
+
+=item B<-fsave-sig-hash>
+
+Save compile-time modifications to the %SIG hash.
+
+=back
 
 =item B<-On>
 
-Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.  Currently,
-B<-O1> and higher set B<-fcog>.
+Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.
+
+=over 4
+
+=item B<-O0>
+
+Disable all optimizations.
+
+=item B<-O1>
+
+Enable B<-fcog>.
+
+=item B<-O2>
+
+Enable B<-fppaddr>, B<-fwarn-sv>.
+
+=back
+
+=item B<-llimit>
+
+Some C compilers impose an arbitrary limit on the length of string
+constants (e.g. 2048 characters for Microsoft Visual C++).  The
+B<-llimit> options tells the C backend not to generate string literals
+exceeding that limit.
+
+=back
 
 =head1 EXAMPLES
 
@@ -1344,7 +2239,7 @@ Note that C<cc_harness> lives in the C<B> subdirectory of your perl
 library directory. The utility called C<perlcc> may also be used to
 help make use of this compiler.
 
-    perl -MO=C,-v,-DcA bar.pl > /dev/null
+    perl -MO=C,-v,-DcA,-l2048 bar.pl > /dev/null
 
 =head1 BUGS