| 1 | # CC.pm | ||||||||||
| 2 | # | ||||||||||
| 3 | # Copyright (c) 1996, 1997, 1998 Malcolm Beattie | ||||||||||
| 4 | # Copyright (c) 2009, 2010, 2011 Reini Urban | ||||||||||
| 5 | # Copyright (c) 2010 Heinz Knutzen | ||||||||||
| 6 | # Copyright (c) 2012-2017 cPanel Inc | ||||||||||
| 7 | # | ||||||||||
| 8 | # You may distribute under the terms of either the GNU General Public | ||||||||||
| 9 | # License or the Artistic License, as specified in the README file. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | B::CC - Perl compiler's optimized C translation backend | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | perl -MO=CC[,OPTIONS] foo.pl | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | This compiler backend takes Perl source and generates C source code | ||||||||||
| 22 | corresponding to the flow of your program with unrolled ops and optimised | ||||||||||
| 23 | stack handling and lexicals variable types. In other words, this backend is | ||||||||||
| 24 | somewhat a "real" compiler in the sense that many people think about | ||||||||||
| 25 | compilers. Note however that, currently, it is a very poor compiler in that | ||||||||||
| 26 | although it generates (mostly, or at least sometimes) correct code, it | ||||||||||
| 27 | performs relatively few optimisations. This will change as the compiler and | ||||||||||
| 28 | the types develops. The result is that running an executable compiled with | ||||||||||
| 29 | this backend may start up more quickly than running the original Perl program | ||||||||||
| 30 | (a feature shared by the B<C> compiler backend--see L<B::C>) and may also | ||||||||||
| 31 | execute slightly faster. This is by no means a good optimising compiler--yet. | ||||||||||
| 32 | |||||||||||
| 33 | =head1 OPTIONS | ||||||||||
| 34 | |||||||||||
| 35 | If there are any non-option arguments, they are taken to be | ||||||||||
| 36 | names of objects to be saved (probably doesn't work properly yet). | ||||||||||
| 37 | Without extra arguments, it saves the main program. | ||||||||||
| 38 | |||||||||||
| 39 | =over 4 | ||||||||||
| 40 | |||||||||||
| 41 | =item B<-ofilename> | ||||||||||
| 42 | |||||||||||
| 43 | Output to filename instead of STDOUT | ||||||||||
| 44 | |||||||||||
| 45 | =item B<-c> | ||||||||||
| 46 | |||||||||||
| 47 | Check and abort. | ||||||||||
| 48 | |||||||||||
| 49 | Compiles and prints only warnings, but does not emit C code. | ||||||||||
| 50 | |||||||||||
| 51 | =item B<-v> | ||||||||||
| 52 | |||||||||||
| 53 | Verbose compilation (prints a few compilation stages). | ||||||||||
| 54 | |||||||||||
| 55 | =item B<--> | ||||||||||
| 56 | |||||||||||
| 57 | Force end of options | ||||||||||
| 58 | |||||||||||
| 59 | =item B<-uPackname> | ||||||||||
| 60 | |||||||||||
| 61 | Force apparently unused subs from package Packname to be compiled. | ||||||||||
| 62 | This allows programs to use eval "foo()" even when sub foo is never | ||||||||||
| 63 | seen to be used at compile time. The down side is that any subs which | ||||||||||
| 64 | really are never used also have code generated. This option is | ||||||||||
| 65 | necessary, for example, if you have a signal handler foo which you | ||||||||||
| 66 | initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just | ||||||||||
| 67 | to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u> | ||||||||||
| 68 | options. The compiler tries to figure out which packages may possibly | ||||||||||
| 69 | have subs in which need compiling but the current version doesn't do | ||||||||||
| 70 | it very well. In particular, it is confused by nested packages (i.e. | ||||||||||
| 71 | of the form C<A::B>) where package C<A> does not contain any subs. | ||||||||||
| 72 | |||||||||||
| 73 | =item B<-UPackname> "unuse" skip Package | ||||||||||
| 74 | |||||||||||
| 75 | Ignore all subs from Package to be compiled. | ||||||||||
| 76 | |||||||||||
| 77 | Certain packages might not be needed at run-time, even if the pessimistic | ||||||||||
| 78 | walker detects it. | ||||||||||
| 79 | |||||||||||
| 80 | =item B<-mModulename> | ||||||||||
| 81 | |||||||||||
| 82 | Instead of generating source for a runnable executable, generate | ||||||||||
| 83 | source for an XSUB module. The boot_Modulename function (which | ||||||||||
| 84 | DynaLoader can look for) does the appropriate initialisation and runs | ||||||||||
| 85 | the main part of the Perl source that is being compiled. | ||||||||||
| 86 | |||||||||||
| 87 | =item B<-nInitname> | ||||||||||
| 88 | |||||||||||
| 89 | Provide a different init name for additional objects added via cmdline. | ||||||||||
| 90 | |||||||||||
| 91 | =item B<-strict> | ||||||||||
| 92 | |||||||||||
| 93 | With a DEBUGGING perl compile-time errors for range and flip without | ||||||||||
| 94 | compile-time context are only warnings. | ||||||||||
| 95 | With C<-strict> these warnings are fatal, otherwise only run-time errors occur. | ||||||||||
| 96 | |||||||||||
| 97 | =item B<-On> | ||||||||||
| 98 | |||||||||||
| 99 | Optimisation level (n = 0, 1, 2). B<-O> means B<-O1>. | ||||||||||
| 100 | |||||||||||
| 101 | The following L<B::C> optimisations are applied automatically: | ||||||||||
| 102 | |||||||||||
| 103 | optimize_warn_sv save_data_fh av-init2|av_init save_sig destruct | ||||||||||
| 104 | pv_copy_on_grow | ||||||||||
| 105 | |||||||||||
| 106 | B<-O1> sets B<-ffreetmps-each-bblock>. | ||||||||||
| 107 | |||||||||||
| 108 | B<-O2> adds B<-ffreetmps-each-loop>, C<-faelem> and B<-fno-destruct> from L<B::C>. | ||||||||||
| 109 | |||||||||||
| 110 | The following options must be set explicitly: | ||||||||||
| 111 | |||||||||||
| 112 | B<-fno-taint> or B<-fomit-taint>, | ||||||||||
| 113 | |||||||||||
| 114 | B<-fslow-signals>, | ||||||||||
| 115 | |||||||||||
| 116 | B<-no-autovivify>, | ||||||||||
| 117 | |||||||||||
| 118 | B<-fno-magic>. | ||||||||||
| 119 | |||||||||||
| 120 | =item B<-f>C<OPTIM> | ||||||||||
| 121 | |||||||||||
| 122 | Force optimisations on or off one at a time. | ||||||||||
| 123 | Unknown optimizations are passed down to L<B::C>. | ||||||||||
| 124 | |||||||||||
| 125 | =item B<-ffreetmps-each-bblock> | ||||||||||
| 126 | |||||||||||
| 127 | Delays FREETMPS from the end of each statement to the end of the each | ||||||||||
| 128 | basic block. | ||||||||||
| 129 | |||||||||||
| 130 | Enabled with B<-O1>. | ||||||||||
| 131 | |||||||||||
| 132 | =item B<-ffreetmps-each-loop> | ||||||||||
| 133 | |||||||||||
| 134 | Delays FREETMPS from the end of each statement to the end of the group | ||||||||||
| 135 | of basic blocks forming a loop. At most one of the freetmps-each-* | ||||||||||
| 136 | options can be used. | ||||||||||
| 137 | |||||||||||
| 138 | Enabled with B<-O2>. | ||||||||||
| 139 | |||||||||||
| 140 | =item B<-faelem> | ||||||||||
| 141 | |||||||||||
| 142 | Enable array element access optimizations, allowing unchecked | ||||||||||
| 143 | fast access under certain circumstances. | ||||||||||
| 144 | |||||||||||
| 145 | Enabled with B<-O2> and not-threaded perls only. | ||||||||||
| 146 | |||||||||||
| 147 | =item B<-fno-inline-ops> | ||||||||||
| 148 | |||||||||||
| 149 | Do not inline calls to certain small pp ops. | ||||||||||
| 150 | |||||||||||
| 151 | Most of the inlinable ops were already inlined. | ||||||||||
| 152 | Turns off inlining for some new ops. | ||||||||||
| 153 | |||||||||||
| 154 | AUTOMATICALLY inlined: | ||||||||||
| 155 | |||||||||||
| 156 | pp_null pp_stub pp_unstack pp_and pp_andassign pp_or pp_orassign pp_cond_expr | ||||||||||
| 157 | pp_padsv pp_const pp_nextstate pp_dbstate pp_rv2gv pp_sort pp_gv pp_gvsv | ||||||||||
| 158 | pp_aelemfast pp_ncmp pp_add pp_subtract pp_multiply pp_divide pp_modulo | ||||||||||
| 159 | pp_left_shift pp_right_shift pp_i_add pp_i_subtract pp_i_multiply pp_i_divide | ||||||||||
| 160 | pp_i_modulo pp_eq pp_ne pp_lt pp_gt pp_le pp_ge pp_i_eq pp_i_ne pp_i_lt | ||||||||||
| 161 | pp_i_gt pp_i_le pp_i_ge pp_scmp pp_slt pp_sgt pp_sle pp_sge pp_seq pp_sne | ||||||||||
| 162 | pp_sassign pp_preinc pp_pushmark pp_list pp_entersub pp_formline pp_goto | ||||||||||
| 163 | pp_enterwrite pp_leavesub pp_leavewrite pp_entergiven pp_leavegiven | ||||||||||
| 164 | pp_entereval pp_dofile pp_require pp_entertry pp_leavetry pp_grepstart | ||||||||||
| 165 | pp_mapstart pp_grepwhile pp_mapwhile pp_return pp_range pp_flip pp_flop | ||||||||||
| 166 | pp_enterloop pp_enteriter pp_leaveloop pp_next pp_redo pp_last pp_subst | ||||||||||
| 167 | pp_substcont | ||||||||||
| 168 | |||||||||||
| 169 | DONE with -finline-ops: | ||||||||||
| 170 | |||||||||||
| 171 | pp_enter pp_reset pp_regcreset pp_stringify | ||||||||||
| 172 | |||||||||||
| 173 | TODO with -finline-ops: | ||||||||||
| 174 | |||||||||||
| 175 | pp_anoncode pp_wantarray pp_srefgen pp_refgen pp_ref pp_trans pp_schop pp_chop | ||||||||||
| 176 | pp_schomp pp_chomp pp_not pp_sprintf pp_anonlist pp_shift pp_once pp_lock | ||||||||||
| 177 | pp_rcatline pp_close pp_time pp_alarm pp_av2arylen: no lvalue, pp_length: no | ||||||||||
| 178 | magic | ||||||||||
| 179 | |||||||||||
| 180 | =item B<-fomit-taint> | ||||||||||
| 181 | |||||||||||
| 182 | Omits generating code for handling perl's tainting mechanism. | ||||||||||
| 183 | |||||||||||
| 184 | =item B<-fslow-signals> | ||||||||||
| 185 | |||||||||||
| 186 | Add PERL_ASYNC_CHECK after every op as in the old Perl runloop before 5.13. | ||||||||||
| 187 | |||||||||||
| 188 | perl "Safe signals" check the state of incoming signals after every op. | ||||||||||
| 189 | See L<http://perldoc.perl.org/perlipc.html#Deferred-Signals-(Safe-Signals)> | ||||||||||
| 190 | We trade safety for more speed and delay the execution of non-IO signals | ||||||||||
| 191 | (IO signals are already handled in PerlIO) from after every single Perl op | ||||||||||
| 192 | to the same ops as used in 5.14. | ||||||||||
| 193 | |||||||||||
| 194 | Only with -fslow-signals we get the old slow and safe behaviour. | ||||||||||
| 195 | |||||||||||
| 196 | =item B<-fno-name-magic> | ||||||||||
| 197 | |||||||||||
| 198 | With the default C<-fname-magic> we infer the SCALAR type for specially named | ||||||||||
| 199 | locals vars and most ops use C vars then, not the perl vars. | ||||||||||
| 200 | Arithmetic and comparison is inlined. Scalar magic is bypassed. | ||||||||||
| 201 | |||||||||||
| 202 | With C<-fno-name-magic> do not infer a local variable type from its name: | ||||||||||
| 203 | |||||||||||
| 204 | B<_i> suffix for int, B<_d> for double/num, B<_ir> for register int | ||||||||||
| 205 | |||||||||||
| 206 | See the experimental C<-ftype-attr> type attributes. | ||||||||||
| 207 | Currently supported are B<int> and B<num> only. See </load_pad>. | ||||||||||
| 208 | |||||||||||
| 209 | =item B<-ftype-attr> (DOES NOT WORK YET) | ||||||||||
| 210 | |||||||||||
| 211 | Experimentally support B<type attributes> for B<int> and B<num>, | ||||||||||
| 212 | SCALAR only so far. | ||||||||||
| 213 | For most ops new C vars are used then, not the fat perl vars. | ||||||||||
| 214 | Very awkward to use until the basic type classes are supported from | ||||||||||
| 215 | within core or use types. | ||||||||||
| 216 | |||||||||||
| 217 | Enabled with B<-O2>. See L<TYPES> and </load_pad>. | ||||||||||
| 218 | |||||||||||
| 219 | =item B<-fno-autovivify> | ||||||||||
| 220 | |||||||||||
| 221 | Do not vivify array and soon also hash elements when accessing them. | ||||||||||
| 222 | Beware: Vivified elements default to undef, unvivified elements are | ||||||||||
| 223 | invalid. | ||||||||||
| 224 | |||||||||||
| 225 | This is the same as the pragma "no autovivification" and allows | ||||||||||
| 226 | very fast array accesses, 4-6 times faster, without the overhead of | ||||||||||
| 227 | L<autovivification>. | ||||||||||
| 228 | |||||||||||
| 229 | =item B<-fno-magic> | ||||||||||
| 230 | |||||||||||
| 231 | Assume certain data being optimized is never tied or is holding other magic. | ||||||||||
| 232 | This mainly holds for arrays being optimized, but in the future hashes also. | ||||||||||
| 233 | |||||||||||
| 234 | =item B<-D> | ||||||||||
| 235 | |||||||||||
| 236 | Debug options (concatenated or separate flags like C<perl -D>). | ||||||||||
| 237 | Verbose debugging options are crucial, because the interactive | ||||||||||
| 238 | debugger L<Od> adds a lot of ballast to the resulting code. | ||||||||||
| 239 | |||||||||||
| 240 | =item B<-Dr> | ||||||||||
| 241 | |||||||||||
| 242 | Writes debugging output to STDERR just as it's about to write to the | ||||||||||
| 243 | program's runtime (otherwise writes debugging info as comments in | ||||||||||
| 244 | its C output). | ||||||||||
| 245 | |||||||||||
| 246 | =item B<-DO> | ||||||||||
| 247 | |||||||||||
| 248 | Outputs each OP as it's compiled | ||||||||||
| 249 | |||||||||||
| 250 | =item B<-DT> | ||||||||||
| 251 | |||||||||||
| 252 | Outputs the contents of the B<stack> at each OP. | ||||||||||
| 253 | Values are B::Stackobj objects. | ||||||||||
| 254 | |||||||||||
| 255 | =item B<-Dc> | ||||||||||
| 256 | |||||||||||
| 257 | Outputs the contents of the loop B<context stack>, the @cxstack. | ||||||||||
| 258 | |||||||||||
| 259 | =item B<-Dw> | ||||||||||
| 260 | |||||||||||
| 261 | Outputs the contents of the B<shadow> stack at each OP. | ||||||||||
| 262 | |||||||||||
| 263 | =item B<-Da> | ||||||||||
| 264 | |||||||||||
| 265 | Outputs the contents of the shadow pad of lexicals as it's loaded for | ||||||||||
| 266 | each sub or the main program. | ||||||||||
| 267 | |||||||||||
| 268 | =item B<-Dq> | ||||||||||
| 269 | |||||||||||
| 270 | Outputs the name of each fake PP function in the queue as it's about | ||||||||||
| 271 | to process it. | ||||||||||
| 272 | |||||||||||
| 273 | =item B<-Dl> | ||||||||||
| 274 | |||||||||||
| 275 | Output the filename and line number of each original line of Perl | ||||||||||
| 276 | code as it's processed (C<pp_nextstate>). | ||||||||||
| 277 | |||||||||||
| 278 | =item B<-Dt> | ||||||||||
| 279 | |||||||||||
| 280 | Outputs timing information of compilation stages. | ||||||||||
| 281 | |||||||||||
| 282 | =item B<-DF> | ||||||||||
| 283 | |||||||||||
| 284 | Add Flags info to the code. | ||||||||||
| 285 | |||||||||||
| 286 | =back | ||||||||||
| 287 | |||||||||||
| 288 | =head1 NOTABLE FUNCTIONS | ||||||||||
| 289 | |||||||||||
| 290 | =cut | ||||||||||
| 291 | |||||||||||
| 292 | |||||||||||
| 293 | package B::CC; | ||||||||||
| 294 | |||||||||||
| 295 | our $VERSION = '1.16_03'; | ||||||||||
| 296 | |||||||||||
| 297 | # Start registering the L<types> namespaces. | ||||||||||
| 298 | 14 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 299 | our %Config; | ||||||||||
| 300 | 14 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
| |||||||||||
| 301 | 14 | require B::C::Config; | |||||||||
| 302 | 14 | *Config = \%B::C::Config::Config; | |||||||||
| 303 | # make it a restricted hash | ||||||||||
| 304 | 14 | ▶ | Internals::SvREADONLY(%Config, 1) if $] >= 5.008004; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 305 | } | ||||||||||
| 306 | unless ($Config{usecperl}) { | ||||||||||
| 307 | eval '$main::int::B_CC = $main::num::B_CC = $main::str::B_CC = $main::double::B_CC = $main::string::B_CC = $VERSION;'; | ||||||||||
| 308 | } | ||||||||||
| 309 | |||||||||||
| 310 | #use 5.008; | ||||||||||
| 311 | 14 | ▶ | use B qw(main_start main_root comppadlist peekop svref_2object | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 312 | timing_info init_av end_av sv_undef | ||||||||||
| 313 | OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST OPf_WANT | ||||||||||
| 314 | OPf_MOD OPf_STACKED OPf_SPECIAL OPpLVAL_DEFER OPpLVAL_INTRO | ||||||||||
| 315 | OPpASSIGN_BACKWARDS OPpLVAL_INTRO OPpDEREF_AV OPpDEREF_HV | ||||||||||
| 316 | 14 | OPpDEREF OPpFLIP_LINENUM G_VOID G_SCALAR G_ARRAY); | |||||||||
| 317 | #CXt_NULL CXt_SUB CXt_EVAL CXt_SUBST CXt_BLOCK | ||||||||||
| 318 | use B::C qw(save_unused_subs objsym init_sections mark_unused mark_skip | ||||||||||
| 319 | output_all output_boilerplate output_main output_main_rest fixup_ppaddr | ||||||||||
| 320 | save_sig svop_or_padop_pv inc_cleanup curcv set_curcv cross_config); | ||||||||||
| 321 | use B::Bblock qw(find_leaders); | ||||||||||
| 322 | use B::Stackobj qw(:types :flags); | ||||||||||
| 323 | use B::C::Config; | ||||||||||
| 324 | # use attributes qw(get reftype); | ||||||||||
| 325 | |||||||||||
| 326 | @B::OP::ISA = qw(B); # support -Do | ||||||||||
| 327 | @B::LISTOP::ISA = qw(B::BINOP B); # support -Do | ||||||||||
| 328 | push @B::OP::ISA, 'B::NULLOP' if exists $main::B::{'NULLOP'}; | ||||||||||
| 329 | |||||||||||
| 330 | # These should probably be elsewhere | ||||||||||
| 331 | # Flags for $op->flags | ||||||||||
| 332 | |||||||||||
| 333 | my $module; # module name (when compiled with -m) | ||||||||||
| 334 | my $cross; # cross config.sh path | ||||||||||
| 335 | my %done; # hash keyed by $$op of leaders of basic blocks | ||||||||||
| 336 | # which have already been done. | ||||||||||
| 337 | my $leaders; # ref to hash of basic block leaders. Keys are $$op | ||||||||||
| 338 | # addresses, values are the $op objects themselves. | ||||||||||
| 339 | my @bblock_todo; # list of leaders of basic blocks that need visiting | ||||||||||
| 340 | # sometime. | ||||||||||
| 341 | my @cc_todo; # list of tuples defining what PP code needs to be | ||||||||||
| 342 | # saved (e.g. CV, main or PMOP repl code). Each tuple | ||||||||||
| 343 | # is [$name, $root, $start, @padlist]. PMOP repl code | ||||||||||
| 344 | # tuples inherit padlist. | ||||||||||
| 345 | my %cc_pp_sub; # hashed names of pp_sub functions already saved | ||||||||||
| 346 | my @stack; # shadows perl's stack when contents are known. | ||||||||||
| 347 | # Values are objects derived from class B::Stackobj | ||||||||||
| 348 | my @pad; # Lexicals in current pad as Stackobj-derived objects | ||||||||||
| 349 | my @padlist; # Copy of current padlist so PMOP repl code can find it | ||||||||||
| 350 | my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo | ||||||||||
| 351 | # This covers only a small part of the perl cxstack | ||||||||||
| 352 | my $labels; # hashref to array of op labels | ||||||||||
| 353 | my %constobj; # OP_CONST constants as Stackobj-derived objects | ||||||||||
| 354 | # keyed by $$sv. | ||||||||||
| 355 | my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic | ||||||||||
| 356 | # block or even to the end of each loop of blocks, | ||||||||||
| 357 | # depending on optimisation options. | ||||||||||
| 358 | my $know_op = 0; # Set when C variable op already holds the right op | ||||||||||
| 359 | # (from an immediately preceding DOOP(ppname)). | ||||||||||
| 360 | my $errors = 0; # Number of errors encountered | ||||||||||
| 361 | my $op_count = 0; # for B::compile_stats on verbose | ||||||||||
| 362 | my %no_stack; # PP names which don't need save pp restore stack | ||||||||||
| 363 | my %skip_stack; # PP names which don't need write_back_stack (empty) | ||||||||||
| 364 | my %skip_lexicals; # PP names which don't need write_back_lexicals | ||||||||||
| 365 | my %skip_invalidate; # PP names which don't need invalidate_lexicals | ||||||||||
| 366 | my %ignore_op; # ops which do nothing except returning op_next | ||||||||||
| 367 | my %need_curcop; # ops which need PL_curcop | ||||||||||
| 368 | my $package_pv; # sv->pv of previous op for method_named | ||||||||||
| 369 | |||||||||||
| 370 | my %lexstate; # state of padsvs at the start of a bblock | ||||||||||
| 371 | my ( $verbose, $check ); | ||||||||||
| 372 | my ( $entertry_defined, $vivify_ref_defined ); | ||||||||||
| 373 | my ( $init_name, %debug, $strict ); | ||||||||||
| 374 | |||||||||||
| 375 | # Optimisation options. On the command line, use hyphens instead of | ||||||||||
| 376 | # underscores for compatibility with gcc-style options. We use | ||||||||||
| 377 | # underscores here because they are OK in (strict) barewords. | ||||||||||
| 378 | # Disable with -fno- | ||||||||||
| 379 | my ( $freetmps_each_bblock, $freetmps_each_loop, $inline_ops, $opt_taint, $opt_omit_taint, | ||||||||||
| 380 | $opt_slow_signals, $opt_name_magic, $opt_type_attr, $opt_autovivify, $opt_magic, | ||||||||||
| 381 | $opt_aelem, %c_optimise ); | ||||||||||
| 382 | $inline_ops = 1 unless $^O eq 'MSWin32'; # Win32 cannot link to unexported pp_op() XXX | ||||||||||
| 383 | $opt_name_magic = 1; | ||||||||||
| 384 | my %optimise = ( | ||||||||||
| 385 | freetmps_each_bblock => \$freetmps_each_bblock, # -O1 | ||||||||||
| 386 | freetmps_each_loop => \$freetmps_each_loop, # -O2 | ||||||||||
| 387 | aelem => \$opt_aelem, # -O2 | ||||||||||
| 388 | inline_ops => \$inline_ops, # not on Win32 | ||||||||||
| 389 | omit_taint => \$opt_omit_taint, | ||||||||||
| 390 | taint => \$opt_taint, | ||||||||||
| 391 | slow_signals => \$opt_slow_signals, | ||||||||||
| 392 | name_magic => \$opt_name_magic, | ||||||||||
| 393 | type_attr => \$opt_type_attr, | ||||||||||
| 394 | autovivify => \$opt_autovivify, | ||||||||||
| 395 | magic => \$opt_magic, | ||||||||||
| 396 | ); | ||||||||||
| 397 | my %async_signals = map { $_ => 1 } # 5.14 ops which do PERL_ASYNC_CHECK | ||||||||||
| 398 | qw(wait waitpid nextstate and cond_expr unstack or subst dorassign); | ||||||||||
| 399 | $async_signals{$_} = 1 for # more 5.16 ops which do PERL_ASYNC_CHECK | ||||||||||
| 400 | qw(substcont next redo goto leavewhen); | ||||||||||
| 401 | # perl patchlevel to generate code for (defaults to current patchlevel) | ||||||||||
| 402 | my $patchlevel = int( 0.5 + 1000 * ( $] - 5 ) ); # XXX unused? | ||||||||||
| 403 | my $MULTI = $Config{usemultiplicity}; | ||||||||||
| 404 | my $ITHREADS = $Config{useithreads}; | ||||||||||
| 405 | my $PERL510 = ( $] >= 5.009005 ); | ||||||||||
| 406 | my $PERL512 = ( $] >= 5.011 ); | ||||||||||
| 407 | |||||||||||
| 408 | my $SVt_PVLV = $PERL510 ? 10 : 9; | ||||||||||
| 409 | my $SVt_PVAV = $PERL510 ? 11 : 10; | ||||||||||
| 410 | # use sub qw(CXt_LOOP_PLAIN CXt_LOOP); | ||||||||||
| 411 | BEGIN { | ||||||||||
| 412 | if ($PERL512) { | ||||||||||
| 413 | sub CXt_LOOP_PLAIN {5} # CXt_LOOP_FOR CXt_LOOP_LAZYSV CXt_LOOP_LAZYIV | ||||||||||
| 414 | } else { | ||||||||||
| 415 | sub CXt_LOOP {3} | ||||||||||
| 416 | } | ||||||||||
| 417 | sub CxTYPE_no_LOOP { | ||||||||||
| 418 | $PERL512 | ||||||||||
| 419 | ? ( $_[0]->{type} < 4 or $_[0]->{type} > 7 ) | ||||||||||
| 420 | : $_[0]->{type} != 3 | ||||||||||
| 421 | } | ||||||||||
| 422 | if ($] < 5.008) { | ||||||||||
| 423 | eval "sub SVs_RMG {0x8000};"; | ||||||||||
| 424 | } else { | ||||||||||
| 425 | B->import('SVs_RMG'); | ||||||||||
| 426 | } | ||||||||||
| 427 | if ($] <= 5.010) { | ||||||||||
| 428 | eval "sub PMf_ONCE() {0xff}; # unused"; | ||||||||||
| 429 | } elsif ($] >= 5.018) { # PMf_ONCE not exported | ||||||||||
| 430 | eval q[sub PMf_ONCE(){ 0x10000 }]; | ||||||||||
| 431 | } elsif ($] >= 5.014) { | ||||||||||
| 432 | eval q[sub PMf_ONCE(){ 0x8000 }]; | ||||||||||
| 433 | } elsif ($] >= 5.012) { | ||||||||||
| 434 | eval q[sub PMf_ONCE(){ 0x0080 }]; | ||||||||||
| 435 | } else { # 5.10. not used with <= 5.8 | ||||||||||
| 436 | eval q[sub PMf_ONCE(){ 0x0002 }]; | ||||||||||
| 437 | } | ||||||||||
| 438 | } | ||||||||||
| 439 | |||||||||||
| 440 | # Could rewrite push_runtime() and output_runtime() to use a | ||||||||||
| 441 | # temporary file if memory is at a premium. | ||||||||||
| 442 | my $ppname; # name of current fake PP function | ||||||||||
| 443 | my $runtime_list_ref; | ||||||||||
| 444 | my $declare_ref; # Hash ref keyed by C variable type of declarations. | ||||||||||
| 445 | |||||||||||
| 446 | my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref] | ||||||||||
| 447 | # tuples to be written out. | ||||||||||
| 448 | |||||||||||
| 449 | my ( $init, $decl ); | ||||||||||
| 450 | |||||||||||
| 451 | sub init_hash { | ||||||||||
| 452 | map { $_ => 1 } @_; | ||||||||||
| 453 | } | ||||||||||
| 454 | |||||||||||
| 455 | # Initialise the hashes for the default PP functions where we can avoid | ||||||||||
| 456 | # either stack save/restore,write_back_stack, write_back_lexicals or invalidate_lexicals. | ||||||||||
| 457 | # XXX We should really take some of this info from Opcodes (was: CORE opcode.pl) | ||||||||||
| 458 | # | ||||||||||
| 459 | # no args and no return value = Opcodes::argnum 0 | ||||||||||
| 460 | %no_stack = init_hash qw(pp_unstack pp_break pp_continue); | ||||||||||
| 461 | # pp_enter pp_leave, use/change global stack. | ||||||||||
| 462 | #skip write_back_stack (no args) | ||||||||||
| 463 | %skip_stack = init_hash qw(pp_enter pp_leave pp_nextstate pp_dbstate); | ||||||||||
| 464 | # which ops do not read pad vars | ||||||||||
| 465 | %skip_lexicals = init_hash qw(pp_enter pp_enterloop pp_leave pp_nextstate pp_dbstate); | ||||||||||
| 466 | # which ops no not write to pad vars | ||||||||||
| 467 | %skip_invalidate = init_hash qw(pp_enter pp_enterloop pp_leave pp_nextstate pp_dbstate | ||||||||||
| 468 | pp_return pp_leavesub pp_list pp_pushmark | ||||||||||
| 469 | pp_anonlist | ||||||||||
| 470 | ); | ||||||||||
| 471 | |||||||||||
| 472 | %need_curcop = init_hash qw(pp_rv2gv pp_bless pp_repeat pp_sort pp_caller | ||||||||||
| 473 | pp_reset pp_rv2cv pp_entereval pp_require pp_dofile | ||||||||||
| 474 | pp_entertry pp_enterloop pp_enteriter pp_entersub pp_entergiven | ||||||||||
| 475 | pp_enter pp_method); | ||||||||||
| 476 | %ignore_op = init_hash qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null); | ||||||||||
| 477 | |||||||||||
| 478 | { # block necessary for caller to work | ||||||||||
| 479 | my $caller = caller; | ||||||||||
| 480 | if ( $caller eq 'O' ) { | ||||||||||
| 481 | require XSLoader; | ||||||||||
| 482 | XSLoader::load('B::C'); # for r-magic only | ||||||||||
| 483 | } | ||||||||||
| 484 | } | ||||||||||
| 485 | |||||||||||
| 486 | sub debug { | ||||||||||
| 487 | if ( $debug{runtime} ) { | ||||||||||
| 488 | # TODO: fix COP to callers line number | ||||||||||
| 489 | warn(@_) if $verbose; | ||||||||||
| 490 | } | ||||||||||
| 491 | else { | ||||||||||
| 492 | my @tmp = @_; | ||||||||||
| 493 | runtime( map { chomp; "/* $_ */" } @tmp ); | ||||||||||
| 494 | } | ||||||||||
| 495 | } | ||||||||||
| 496 | |||||||||||
| 497 | sub declare { | ||||||||||
| 498 | my ( $type, $var ) = @_; | ||||||||||
| 499 | push( @{ $declare_ref->{$type} }, $var ); | ||||||||||
| 500 | } | ||||||||||
| 501 | |||||||||||
| 502 | sub push_runtime { | ||||||||||
| 503 | push( @$runtime_list_ref, @_ ); | ||||||||||
| 504 | warn join( "\n", @_ ) . "\n" if $debug{runtime}; | ||||||||||
| 505 | } | ||||||||||
| 506 | |||||||||||
| 507 | sub save_runtime { | ||||||||||
| 508 | push( @pp_list, [ $ppname, $runtime_list_ref, $declare_ref ] ); | ||||||||||
| 509 | } | ||||||||||
| 510 | |||||||||||
| 511 | sub output_runtime { | ||||||||||
| 512 | my $ppdata; | ||||||||||
| 513 | print qq(\n#include "cc_runtime.h"\n); | ||||||||||
| 514 | # CC coverage: 12, 32 | ||||||||||
| 515 | |||||||||||
| 516 | # Perls >=5.8.9 have a broken PP_ENTERTRY. See PERL_FLEXIBLE_EXCEPTIONS in cop.h | ||||||||||
| 517 | # Fixed in CORE with 5.11.4 | ||||||||||
| 518 | print' | ||||||||||
| 519 | #undef PP_ENTERTRY | ||||||||||
| 520 | #define PP_ENTERTRY(label) \ | ||||||||||
| 521 | STMT_START { \ | ||||||||||
| 522 | int ret; \ | ||||||||||
| 523 | JMPENV_PUSH(ret); \ | ||||||||||
| 524 | switch (ret) { \ | ||||||||||
| 525 | case 1: JMPENV_POP; JMPENV_JUMP(1);\ | ||||||||||
| 526 | case 2: JMPENV_POP; JMPENV_JUMP(2);\ | ||||||||||
| 527 | case 3: JMPENV_POP; SPAGAIN; goto label;\ | ||||||||||
| 528 | } \ | ||||||||||
| 529 | } STMT_END' | ||||||||||
| 530 | if $entertry_defined and $] < 5.011004; | ||||||||||
| 531 | # XXX need to find out when PERL_FLEXIBLE_EXCEPTIONS were actually active. | ||||||||||
| 532 | # 5.6.2 not, 5.8.9 not. coverage 32 | ||||||||||
| 533 | |||||||||||
| 534 | # test 12. Used by entereval + dofile | ||||||||||
| 535 | if ($PERL510 or $MULTI) { | ||||||||||
| 536 | # Threads error Bug#55302: too few arguments to function | ||||||||||
| 537 | # CALLRUNOPS()=>CALLRUNOPS(aTHX) | ||||||||||
| 538 | # fixed with 5.11.4 | ||||||||||
| 539 | print ' | ||||||||||
| 540 | #undef PP_EVAL | ||||||||||
| 541 | #define PP_EVAL(ppaddr, nxt) do { \ | ||||||||||
| 542 | int ret; \ | ||||||||||
| 543 | PUTBACK; \ | ||||||||||
| 544 | JMPENV_PUSH(ret); \ | ||||||||||
| 545 | switch (ret) { \ | ||||||||||
| 546 | case 0: \ | ||||||||||
| 547 | PL_op = ppaddr(aTHX); \\'; | ||||||||||
| 548 | if ($PERL510) { | ||||||||||
| 549 | # pp_leaveeval sets: retop = cx->blk_eval.retop | ||||||||||
| 550 | print ' | ||||||||||
| 551 | cxstack[cxstack_ix].blk_eval.retop = Nullop; \\'; | ||||||||||
| 552 | } else { | ||||||||||
| 553 | # up to 5.8 pp_entereval did set the retstack to next. | ||||||||||
| 554 | # nullify that so that we can now exec the rest of this bblock. | ||||||||||
| 555 | # (nextstate .. leaveeval) | ||||||||||
| 556 | print ' | ||||||||||
| 557 | PL_retstack[PL_retstack_ix - 1] = Nullop; \\'; | ||||||||||
| 558 | } | ||||||||||
| 559 | print ' | ||||||||||
| 560 | if (PL_op != nxt) CALLRUNOPS(aTHX); \ | ||||||||||
| 561 | JMPENV_POP; \ | ||||||||||
| 562 | break; \ | ||||||||||
| 563 | case 1: JMPENV_POP; JMPENV_JUMP(1); \ | ||||||||||
| 564 | case 2: JMPENV_POP; JMPENV_JUMP(2); \ | ||||||||||
| 565 | case 3: \ | ||||||||||
| 566 | JMPENV_POP; \ | ||||||||||
| 567 | if (PL_restartop && PL_restartop != nxt) \ | ||||||||||
| 568 | JMPENV_JUMP(3); \ | ||||||||||
| 569 | } \ | ||||||||||
| 570 | PL_op = nxt; \ | ||||||||||
| 571 | SPAGAIN; \ | ||||||||||
| 572 | } while (0) | ||||||||||
| 573 | '; | ||||||||||
| 574 | } | ||||||||||
| 575 | |||||||||||
| 576 | # Perl_vivify_ref not exported on MSWin32 | ||||||||||
| 577 | # coverage: 18 | ||||||||||
| 578 | if ($PERL510 and $^O eq 'MSWin32') { | ||||||||||
| 579 | # CC coverage: 18, 29 | ||||||||||
| 580 | print << '__EOV' if $vivify_ref_defined; | ||||||||||
| 581 | |||||||||||
| 582 | /* Code to take a scalar and ready it to hold a reference */ | ||||||||||
| 583 | # ifndef SVt_RV | ||||||||||
| 584 | # define SVt_RV SVt_IV | ||||||||||
| 585 | # endif | ||||||||||
| 586 | # define prepare_SV_for_RV(sv) \ | ||||||||||
| 587 | STMT_START { \ | ||||||||||
| 588 | if (SvTYPE(sv) < SVt_RV) \ | ||||||||||
| 589 | sv_upgrade(sv, SVt_RV); \ | ||||||||||
| 590 | else if (SvPVX_const(sv)) { \ | ||||||||||
| 591 | SvPV_free(sv); \ | ||||||||||
| 592 | SvLEN_set(sv, 0); \ | ||||||||||
| 593 | SvCUR_set(sv, 0); \ | ||||||||||
| 594 | } \ | ||||||||||
| 595 | } STMT_END | ||||||||||
| 596 | |||||||||||
| 597 | #if (PERL_VERSION > 15) || ((PERL_VERSION == 15) && (PERL_SUBVERSION >= 2)) | ||||||||||
| 598 | SV* | ||||||||||
| 599 | #else | ||||||||||
| 600 | void | ||||||||||
| 601 | #endif | ||||||||||
| 602 | Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) | ||||||||||
| 603 | { | ||||||||||
| 604 | SvGETMAGIC(sv); | ||||||||||
| 605 | if (!SvOK(sv)) { | ||||||||||
| 606 | if (SvREADONLY(sv)) | ||||||||||
| 607 | Perl_croak(aTHX_ "%s", PL_no_modify); | ||||||||||
| 608 | prepare_SV_for_RV(sv); | ||||||||||
| 609 | switch (to_what) { | ||||||||||
| 610 | case OPpDEREF_SV: | ||||||||||
| 611 | SvRV_set(sv, newSV(0)); | ||||||||||
| 612 | break; | ||||||||||
| 613 | case OPpDEREF_AV: | ||||||||||
| 614 | SvRV_set(sv, newAV()); | ||||||||||
| 615 | break; | ||||||||||
| 616 | case OPpDEREF_HV: | ||||||||||
| 617 | SvRV_set(sv, newHV()); | ||||||||||
| 618 | break; | ||||||||||
| 619 | } | ||||||||||
| 620 | SvROK_on(sv); | ||||||||||
| 621 | SvSETMAGIC(sv); | ||||||||||
| 622 | } | ||||||||||
| 623 | } | ||||||||||
| 624 | |||||||||||
| 625 | __EOV | ||||||||||
| 626 | |||||||||||
| 627 | } | ||||||||||
| 628 | |||||||||||
| 629 | print ' | ||||||||||
| 630 | |||||||||||
| 631 | OP *Perl_pp_aelem_nolval(pTHXx); | ||||||||||
| 632 | #ifndef SVfARG | ||||||||||
| 633 | # define SVfARG(x) (void *)x | ||||||||||
| 634 | #endif | ||||||||||
| 635 | #ifndef MUTABLE_AV | ||||||||||
| 636 | # define MUTABLE_AV(av) av | ||||||||||
| 637 | #endif | ||||||||||
| 638 | PP(pp_aelem_nolval) | ||||||||||
| 639 | { | ||||||||||
| 640 | dSP; | ||||||||||
| 641 | SV** svp; | ||||||||||
| 642 | SV* const elemsv = POPs; | ||||||||||
| 643 | IV elem = SvIV(elemsv); | ||||||||||
| 644 | AV *const av = MUTABLE_AV(POPs); | ||||||||||
| 645 | SV *sv; | ||||||||||
| 646 | |||||||||||
| 647 | #if PERL_VERSION > 6 | ||||||||||
| 648 | if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC)) | ||||||||||
| 649 | Perl_warner(aTHX_ packWARN(WARN_MISC), | ||||||||||
| 650 | "Use of reference \"%"SVf"\" as array index", | ||||||||||
| 651 | SVfARG(elemsv)); | ||||||||||
| 652 | #endif | ||||||||||
| 653 | if (SvTYPE(av) != SVt_PVAV) RETPUSHUNDEF; | ||||||||||
| 654 | svp = av_fetch(av, elem, 0); | ||||||||||
| 655 | sv = (svp ? *svp : &PL_sv_undef); | ||||||||||
| 656 | if (SvRMAGICAL(av) && SvGMAGICAL(sv)) mg_get(sv); | ||||||||||
| 657 | PUSHs(sv); | ||||||||||
| 658 | RETURN; | ||||||||||
| 659 | } | ||||||||||
| 660 | ' if 0; | ||||||||||
| 661 | |||||||||||
| 662 | foreach $ppdata (@pp_list) { | ||||||||||
| 663 | my ( $name, $runtime, $declare ) = @$ppdata; | ||||||||||
| 664 | print "\nstatic\nCCPP($name)\n{\n"; | ||||||||||
| 665 | my ( $type, $varlist, $line ); | ||||||||||
| 666 | while ( ( $type, $varlist ) = each %$declare ) { | ||||||||||
| 667 | $varlist = $declare->{$type}; | ||||||||||
| 668 | print "\t$type ", join( ", ", @$varlist ), ";\n"; | ||||||||||
| 669 | } | ||||||||||
| 670 | foreach $line (@$runtime) { | ||||||||||
| 671 | print $line, "\n"; | ||||||||||
| 672 | } | ||||||||||
| 673 | print "}\n"; | ||||||||||
| 674 | } | ||||||||||
| 675 | } | ||||||||||
| 676 | |||||||||||
| 677 | sub runtime { | ||||||||||
| 678 | foreach (@_) { | ||||||||||
| 679 | if (/ goto lab_0;/) { | ||||||||||
| 680 | my $line = $_; # readonly $_ needs a copy | ||||||||||
| 681 | $line =~ s/ goto lab_0;/ PUTBACK; return NULL;/; | ||||||||||
| 682 | push_runtime("\t$line"); | ||||||||||
| 683 | } else { | ||||||||||
| 684 | push_runtime("\t$_"); | ||||||||||
| 685 | } | ||||||||||
| 686 | } | ||||||||||
| 687 | } | ||||||||||
| 688 | |||||||||||
| 689 | sub init_pp { | ||||||||||
| 690 | $ppname = shift; | ||||||||||
| 691 | $runtime_list_ref = []; | ||||||||||
| 692 | $declare_ref = {}; | ||||||||||
| 693 | runtime("dVAR; dSP;"); | ||||||||||
| 694 | declare( "I32", "oldsave" ); | ||||||||||
| 695 | map { declare( "SV", "*$_" ) } qw(sv src dst left right); | ||||||||||
| 696 | declare( "MAGIC", "*mg" ); | ||||||||||
| 697 | $decl->add( "#undef cxinc", "#define cxinc() Perl_cxinc(aTHX)") | ||||||||||
| 698 | if $] < 5.011001 and $inline_ops; | ||||||||||
| 699 | declare( "PERL_CONTEXT", "*cx" ); | ||||||||||
| 700 | declare( "I32", "gimme"); | ||||||||||
| 701 | $decl->add("static OP * $ppname (pTHX);"); | ||||||||||
| 702 | debug "init_pp: $ppname\n" if $debug{queue}; | ||||||||||
| 703 | } | ||||||||||
| 704 | |||||||||||
| 705 | # Initialise runtime_callback function for Stackobj class | ||||||||||
| 706 | BEGIN { B::Stackobj::set_callback( \&runtime ) } | ||||||||||
| 707 | |||||||||||
| 708 | =head2 cc_queue | ||||||||||
| 709 | |||||||||||
| 710 | Creates a new ccpp optree. | ||||||||||
| 711 | |||||||||||
| 712 | Initialised by saveoptree_callback in L<B::C>, replaces B::C::walk_and_save_optree. | ||||||||||
| 713 | Called by every C<CV::save> if ROOT. | ||||||||||
| 714 | B<blocksort> also creates its block closure with cc_queue. | ||||||||||
| 715 | |||||||||||
| 716 | =cut | ||||||||||
| 717 | |||||||||||
| 718 | # coverage: test 18, 28 (fixed with B-C-1.30 r971) | ||||||||||
| 719 | sub cc_queue { | ||||||||||
| 720 | my ( $name, $root, $start, @pl ) = @_; | ||||||||||
| 721 | debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n" | ||||||||||
| 722 | if $debug{queue}; | ||||||||||
| 723 | if ( $name eq "*ignore*" or $name =~ /^pp_sub_.*(FETCH|MODIFY)_SCALAR_ATTRIBUTES$/) { | ||||||||||
| 724 | $name = ''; | ||||||||||
| 725 | } else { | ||||||||||
| 726 | push( @cc_todo, [ $name, $root, $start, ( @pl ? @pl : @padlist ) ] ); | ||||||||||
| 727 | } | ||||||||||
| 728 | my $fakeop_next = 0; | ||||||||||
| 729 | if ($name =~ /^pp_sub_.*DESTROY$/) { | ||||||||||
| 730 | # curse in sv_clean_objs() checks for ->op_next->op_type | ||||||||||
| 731 | $fakeop_next = $start->next->save; | ||||||||||
| 732 | } | ||||||||||
| 733 | my $fakeop = B::FAKEOP->new( "next" => $fakeop_next, ppaddr => $name ); | ||||||||||
| 734 | $start = $fakeop->save; | ||||||||||
| 735 | debug "cc_queue: name $name returns $start\n" if $debug{queue}; | ||||||||||
| 736 | return $start; | ||||||||||
| 737 | } | ||||||||||
| 738 | BEGIN { B::C::set_callback( \&cc_queue ) } | ||||||||||
| 739 | |||||||||||
| 740 | sub valid_int { $_[0]->{flags} & VALID_INT } | ||||||||||
| 741 | sub valid_double { $_[0]->{flags} & VALID_NUM } | ||||||||||
| 742 | sub valid_numeric { $_[0]->{flags} & ( VALID_INT | VALID_NUM ) } | ||||||||||
| 743 | sub valid_str { $_[0]->{flags} & VALID_STR } | ||||||||||
| 744 | sub valid_sv { $_[0]->{flags} & VALID_SV } | ||||||||||
| 745 | |||||||||||
| 746 | sub top_int { @stack ? $stack[-1]->as_int : "TOPi" } | ||||||||||
| 747 | sub top_double { @stack ? $stack[-1]->as_double : "TOPn" } | ||||||||||
| 748 | sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" } | ||||||||||
| 749 | sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" } | ||||||||||
| 750 | sub top_str { @stack ? $stack[-1]->as_str : "TOPs" } | ||||||||||
| 751 | sub top_bool { @stack ? $stack[-1]->as_bool : "SvTRUE(TOPs)" } | ||||||||||
| 752 | |||||||||||
| 753 | sub pop_int { @stack ? ( pop @stack )->as_int : "POPi" } | ||||||||||
| 754 | sub pop_double { @stack ? ( pop @stack )->as_double : "POPn" } | ||||||||||
| 755 | sub pop_numeric { @stack ? ( pop @stack )->as_numeric : "POPn" } | ||||||||||
| 756 | sub pop_str { @stack ? ( pop @stack )->as_str : "POPs" } | ||||||||||
| 757 | sub pop_sv { @stack ? ( pop @stack )->as_sv : "POPs" } | ||||||||||
| 758 | |||||||||||
| 759 | sub pop_bool { | ||||||||||
| 760 | if (@stack) { | ||||||||||
| 761 | return ( ( pop @stack )->as_bool ); | ||||||||||
| 762 | } | ||||||||||
| 763 | else { | ||||||||||
| 764 | # Careful: POPs has an auto-decrement and SvTRUE evaluates | ||||||||||
| 765 | # its argument more than once. | ||||||||||
| 766 | runtime("sv = POPs;"); | ||||||||||
| 767 | return "SvTRUE(sv)"; | ||||||||||
| 768 | } | ||||||||||
| 769 | } | ||||||||||
| 770 | |||||||||||
| 771 | sub write_back_lexicals { | ||||||||||
| 772 | my $avoid = shift || 0; | ||||||||||
| 773 | debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n" | ||||||||||
| 774 | if $debug{shadow}; | ||||||||||
| 775 | my $lex; | ||||||||||
| 776 | foreach $lex (@pad) { | ||||||||||
| 777 | next unless ref($lex); | ||||||||||
| 778 | $lex->write_back unless $lex->{flags} & $avoid; | ||||||||||
| 779 | } | ||||||||||
| 780 | } | ||||||||||
| 781 | |||||||||||
| 782 | =head1 save_or_restore_lexical_state | ||||||||||
| 783 | |||||||||||
| 784 | The compiler tracks state of lexical variables in @pad to generate optimised | ||||||||||
| 785 | code. But multiple execution paths lead to the entry point of a basic block. | ||||||||||
| 786 | The state of the first execution path is saved and all other execution | ||||||||||
| 787 | paths are restored to the state of the first one. | ||||||||||
| 788 | |||||||||||
| 789 | Missing flags are regenerated by loading values. | ||||||||||
| 790 | |||||||||||
| 791 | Added flags must are removed; otherwise the compiler would be too optimistic, | ||||||||||
| 792 | hence generating code which doesn't match state of the other execution paths. | ||||||||||
| 793 | |||||||||||
| 794 | =cut | ||||||||||
| 795 | |||||||||||
| 796 | sub save_or_restore_lexical_state { | ||||||||||
| 797 | my $bblock = shift; | ||||||||||
| 798 | unless ( exists $lexstate{$bblock} ) { | ||||||||||
| 799 | foreach my $lex (@pad) { | ||||||||||
| 800 | next unless ref($lex); | ||||||||||
| 801 | ${ $lexstate{$bblock} }{ $lex->{iv} } = $lex->{flags}; | ||||||||||
| 802 | } | ||||||||||
| 803 | } | ||||||||||
| 804 | else { | ||||||||||
| 805 | foreach my $lex (@pad) { | ||||||||||
| 806 | next unless ref($lex); | ||||||||||
| 807 | my $old_flags = ${ $lexstate{$bblock} }{ $lex->{iv} }; | ||||||||||
| 808 | next if ( $old_flags eq $lex->{flags} ); | ||||||||||
| 809 | my $changed = $old_flags ^ $lex->{flags}; | ||||||||||
| 810 | if ( $changed & VALID_SV ) { | ||||||||||
| 811 | ( $old_flags & VALID_SV ) ? $lex->write_back : $lex->invalidate; | ||||||||||
| 812 | } | ||||||||||
| 813 | if ( $changed & VALID_NUM ) { | ||||||||||
| 814 | ( $old_flags & VALID_NUM ) ? $lex->load_double : $lex->invalidate_double; | ||||||||||
| 815 | } | ||||||||||
| 816 | if ( $changed & VALID_INT ) { | ||||||||||
| 817 | ( $old_flags & VALID_INT ) ? $lex->load_int : $lex->invalidate_int; | ||||||||||
| 818 | } | ||||||||||
| 819 | if ( $changed & VALID_STR ) { | ||||||||||
| 820 | ( $old_flags & VALID_STR ) ? $lex->load_str : $lex->invalidate_str; | ||||||||||
| 821 | } | ||||||||||
| 822 | } | ||||||||||
| 823 | } | ||||||||||
| 824 | } | ||||||||||
| 825 | |||||||||||
| 826 | sub write_back_stack { | ||||||||||
| 827 | debug "write_back_stack() ".scalar(@stack)." called from @{[(caller(1))[3]]}\n" | ||||||||||
| 828 | if $debug{shadow}; | ||||||||||
| 829 | return unless @stack; | ||||||||||
| 830 | runtime( sprintf( "EXTEND(sp, %d);", scalar(@stack) ) ); | ||||||||||
| 831 | foreach my $obj (@stack) { | ||||||||||
| 832 | runtime( sprintf( "PUSHs((SV*)%s);", $obj->as_sv ) ); | ||||||||||
| 833 | } | ||||||||||
| 834 | @stack = (); | ||||||||||
| 835 | } | ||||||||||
| 836 | |||||||||||
| 837 | sub invalidate_lexicals { | ||||||||||
| 838 | my $avoid = shift || 0; | ||||||||||
| 839 | debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n" | ||||||||||
| 840 | if $debug{shadow}; | ||||||||||
| 841 | my $lex; | ||||||||||
| 842 | foreach $lex (@pad) { | ||||||||||
| 843 | next unless ref($lex); | ||||||||||
| 844 | $lex->invalidate unless $lex->{flags} & $avoid; | ||||||||||
| 845 | } | ||||||||||
| 846 | } | ||||||||||
| 847 | |||||||||||
| 848 | sub reload_lexicals { | ||||||||||
| 849 | my $lex; | ||||||||||
| 850 | foreach $lex (@pad) { | ||||||||||
| 851 | next unless ref($lex); | ||||||||||
| 852 | my $type = $lex->{type}; | ||||||||||
| 853 | if ( $type == T_INT ) { | ||||||||||
| 854 | $lex->as_int; | ||||||||||
| 855 | } | ||||||||||
| 856 | elsif ( $type == T_NUM ) { | ||||||||||
| 857 | $lex->as_double; | ||||||||||
| 858 | } | ||||||||||
| 859 | elsif ( $type == T_STR ) { | ||||||||||
| 860 | $lex->as_str; | ||||||||||
| 861 | } | ||||||||||
| 862 | else { | ||||||||||
| 863 | $lex->as_sv; | ||||||||||
| 864 | } | ||||||||||
| 865 | } | ||||||||||
| 866 | } | ||||||||||
| 867 | |||||||||||
| 868 | { | ||||||||||
| 869 | |||||||||||
| 870 | package B::Pseudoreg; | ||||||||||
| 871 | |||||||||||
| 872 | # | ||||||||||
| 873 | # This class allocates pseudo-registers (OK, so they're C variables). | ||||||||||
| 874 | # | ||||||||||
| 875 | my %alloc; # Keyed by variable name. A value of 1 means the | ||||||||||
| 876 | # variable has been declared. A value of 2 means | ||||||||||
| 877 | # it's in use. | ||||||||||
| 878 | |||||||||||
| 879 | sub new_scope { %alloc = () } | ||||||||||
| 880 | |||||||||||
| 881 | sub new ($$$) { | ||||||||||
| 882 | my ( $class, $type, $prefix ) = @_; | ||||||||||
| 883 | my ( $ptr, $i, $varname, $status, $obj ); | ||||||||||
| 884 | $prefix =~ s/^(\**)//; | ||||||||||
| 885 | $ptr = $1; | ||||||||||
| 886 | $i = 0; | ||||||||||
| 887 | do { | ||||||||||
| 888 | $varname = "$prefix$i"; | ||||||||||
| 889 | $status = exists $alloc{$varname} ? $alloc{$varname} : 0; | ||||||||||
| 890 | } while $status == 2; | ||||||||||
| 891 | |||||||||||
| 892 | if ( $status != 1 ) { | ||||||||||
| 893 | # Not declared yet | ||||||||||
| 894 | B::CC::declare( $type, "$ptr$varname" ); | ||||||||||
| 895 | $alloc{$varname} = 2; # declared and in use | ||||||||||
| 896 | } | ||||||||||
| 897 | $obj = bless \$varname, $class; | ||||||||||
| 898 | return $obj; | ||||||||||
| 899 | } | ||||||||||
| 900 | |||||||||||
| 901 | sub DESTROY { | ||||||||||
| 902 | my $obj = shift; | ||||||||||
| 903 | $alloc{$$obj} = 1; # no longer in use but still declared | ||||||||||
| 904 | } | ||||||||||
| 905 | } | ||||||||||
| 906 | { | ||||||||||
| 907 | |||||||||||
| 908 | package B::Shadow; | ||||||||||
| 909 | |||||||||||
| 910 | # | ||||||||||
| 911 | # This class gives a standard API for a perl object to shadow a | ||||||||||
| 912 | # C variable and only generate reloads/write-backs when necessary. | ||||||||||
| 913 | # | ||||||||||
| 914 | # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo"). | ||||||||||
| 915 | # Use $obj->write_back whenever shadowed_c_var needs to be up to date. | ||||||||||
| 916 | # Use $obj->invalidate whenever an unknown function may have | ||||||||||
| 917 | # set shadow itself. | ||||||||||
| 918 | |||||||||||
| 919 | sub new { | ||||||||||
| 920 | my ( $class, $write_back ) = @_; | ||||||||||
| 921 | |||||||||||
| 922 | # Object fields are perl shadow variable, validity flag | ||||||||||
| 923 | # (for *C* variable) and callback sub for write_back | ||||||||||
| 924 | # (passed perl shadow variable as argument). | ||||||||||
| 925 | bless [ undef, 1, $write_back ], $class; | ||||||||||
| 926 | } | ||||||||||
| 927 | |||||||||||
| 928 | sub load { | ||||||||||
| 929 | my ( $obj, $newval ) = @_; | ||||||||||
| 930 | $obj->[1] = 0; # C variable no longer valid | ||||||||||
| 931 | $obj->[0] = $newval; | ||||||||||
| 932 | } | ||||||||||
| 933 | |||||||||||
| 934 | sub value { | ||||||||||
| 935 | return $_[0]->[0]; | ||||||||||
| 936 | } | ||||||||||
| 937 | |||||||||||
| 938 | sub write_back { | ||||||||||
| 939 | my $obj = shift; | ||||||||||
| 940 | if ( !( $obj->[1] ) ) { | ||||||||||
| 941 | $obj->[1] = 1; # C variable will now be valid | ||||||||||
| 942 | &{ $obj->[2] }( $obj->[0] ); | ||||||||||
| 943 | } | ||||||||||
| 944 | } | ||||||||||
| 945 | sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid | ||||||||||
| 946 | } | ||||||||||
| 947 | |||||||||||
| 948 | my $curcop = B::Shadow->new( | ||||||||||
| 949 | sub { | ||||||||||
| 950 | my $op = shift; | ||||||||||
| 951 | my $opsym = $op->save; | ||||||||||
| 952 | runtime("PL_curcop = (COP*)$opsym;"); | ||||||||||
| 953 | } | ||||||||||
| 954 | ); | ||||||||||
| 955 | |||||||||||
| 956 | # | ||||||||||
| 957 | # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on. | ||||||||||
| 958 | # | ||||||||||
| 959 | sub dopoptoloop { | ||||||||||
| 960 | my $cxix = $#cxstack; | ||||||||||
| 961 | while ( $cxix >= 0 && CxTYPE_no_LOOP( $cxstack[$cxix] ) ) { | ||||||||||
| 962 | $cxix--; | ||||||||||
| 963 | } | ||||||||||
| 964 | debug "dopoptoloop: returning $cxix" if $debug{cxstack}; | ||||||||||
| 965 | return $cxix; | ||||||||||
| 966 | } | ||||||||||
| 967 | |||||||||||
| 968 | sub dopoptolabel { | ||||||||||
| 969 | my $label = shift; | ||||||||||
| 970 | my $cxix = $#cxstack; | ||||||||||
| 971 | while ( | ||||||||||
| 972 | $cxix >= 0 | ||||||||||
| 973 | && ( CxTYPE_no_LOOP( $cxstack[$cxix] ) | ||||||||||
| 974 | || $cxstack[$cxix]->{label} ne $label ) | ||||||||||
| 975 | ) | ||||||||||
| 976 | { | ||||||||||
| 977 | $cxix--; | ||||||||||
| 978 | } | ||||||||||
| 979 | debug "dopoptolabel: returning $cxix\n" if $debug{cxstack}; | ||||||||||
| 980 | if ($cxix < 0 and $debug{cxstack}) { | ||||||||||
| 981 | for my $cx (0 .. $#cxstack) { | ||||||||||
| 982 | debug "$cx: ",$cxstack[$cx]->{label},"\n"; | ||||||||||
| 983 | } | ||||||||||
| 984 | for my $op (keys %{$labels->{label}}) { | ||||||||||
| 985 | debug $labels->{label}->{$op},"\n"; | ||||||||||
| 986 | } | ||||||||||
| 987 | } | ||||||||||
| 988 | return $cxix; | ||||||||||
| 989 | } | ||||||||||
| 990 | |||||||||||
| 991 | sub push_label { | ||||||||||
| 992 | my $op = shift; | ||||||||||
| 993 | my $type = shift; | ||||||||||
| 994 | push @{$labels->{$type}}, ( $op ); | ||||||||||
| 995 | } | ||||||||||
| 996 | |||||||||||
| 997 | sub pop_label { | ||||||||||
| 998 | my $type = shift; | ||||||||||
| 999 | my $op = pop @{$labels->{$type}}; | ||||||||||
| 1000 | write_label ($op); # avoids duplicate labels | ||||||||||
| 1001 | } | ||||||||||
| 1002 | |||||||||||
| 1003 | sub error { | ||||||||||
| 1004 | my $format = shift; | ||||||||||
| 1005 | my $file = $curcop->[0]->file; | ||||||||||
| 1006 | my $line = $curcop->[0]->line; | ||||||||||
| 1007 | $errors++; | ||||||||||
| 1008 | if (@_) { | ||||||||||
| 1009 | warn sprintf( "ERROR at %s:%d: $format\n", $file, $line, @_ ); | ||||||||||
| 1010 | } | ||||||||||
| 1011 | else { | ||||||||||
| 1012 | warn sprintf( "ERROR at %s:%d: %s\n", $file, $line, $format ); | ||||||||||
| 1013 | } | ||||||||||
| 1014 | } | ||||||||||
| 1015 | |||||||||||
| 1016 | # run-time eval is too late for attrs being checked by perlcore. BEGIN does not help. | ||||||||||
| 1017 | # use types is the right approach. But until types is fixed we use this hack. | ||||||||||
| 1018 | # Note that we also need a new CHECK_SCALAR_ATTRIBUTES hook, starting with v5.22. | ||||||||||
| 1019 | sub init_type_attrs { | ||||||||||
| 1020 | eval q[ | ||||||||||
| 1021 | |||||||||||
| 1022 | our $valid_attr = '^(int|num|str|double|string|unsigned|register|temporary|ro|readonly|const)$'; | ||||||||||
| 1023 | sub MODIFY_SCALAR_ATTRIBUTES { | ||||||||||
| 1024 | my $pkg = shift; | ||||||||||
| 1025 | my $v = shift; | ||||||||||
| 1026 | my $attr = $B::CC::valid_attr; | ||||||||||
| 1027 | $attr =~ s/\b$pkg\b//; | ||||||||||
| 1028 | if (my @bad = grep !/$attr/, @_) { | ||||||||||
| 1029 | return @bad; | ||||||||||
| 1030 | } else { | ||||||||||
| 1031 | no strict 'refs'; | ||||||||||
| 1032 | push @{"$pkg\::$v\::attributes"}, @_; # create a magic glob | ||||||||||
| 1033 | return (); | ||||||||||
| 1034 | } | ||||||||||
| 1035 | } | ||||||||||
| 1036 | sub FETCH_SCALAR_ATTRIBUTES { | ||||||||||
| 1037 | my ($pkg, $v) = @_; | ||||||||||
| 1038 | no strict 'refs'; | ||||||||||
| 1039 | return @{"$pkg\::$v\::attributes"}; | ||||||||||
| 1040 | } | ||||||||||
| 1041 | |||||||||||
| 1042 | # pollute our callers namespace for attributes to be accepted with -MB::CC | ||||||||||
| 1043 | *main::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1044 | *main::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1045 | |||||||||||
| 1046 | # my int $i : register : ro; | ||||||||||
| 1047 | *int::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1048 | *int::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1049 | |||||||||||
| 1050 | # my double $d : ro; | ||||||||||
| 1051 | *num::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1052 | *num::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1053 | *str::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1054 | *str::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1055 | |||||||||||
| 1056 | # deprecated: | ||||||||||
| 1057 | *double::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1058 | *double::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1059 | *string::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES; | ||||||||||
| 1060 | *string::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES; | ||||||||||
| 1061 | ]; | ||||||||||
| 1062 | |||||||||||
| 1063 | } | ||||||||||
| 1064 | |||||||||||
| 1065 | =head2 load_pad | ||||||||||
| 1066 | |||||||||||
| 1067 | Load pad takes (the elements of) a PADLIST as arguments and loads up @pad | ||||||||||
| 1068 | with Stackobj-derived objects which represent those lexicals. | ||||||||||
| 1069 | |||||||||||
| 1070 | If/when perl itself can generate type information C<(my int $foo; my $foo : int)> then we'll | ||||||||||
| 1071 | take advantage of that here. Until then, we'll use the L<-fname-magic/-fno-name-magic> | ||||||||||
| 1072 | hack to tell the compiler when we want a lexical to be a particular type or to be a register. | ||||||||||
| 1073 | |||||||||||
| 1074 | =cut | ||||||||||
| 1075 | |||||||||||
| 1076 | sub load_pad { | ||||||||||
| 1077 | my ( $namelistav, $valuelistav ) = @_; | ||||||||||
| 1078 | @padlist = @_; | ||||||||||
| 1079 | my @namelist = $namelistav->ARRAY; | ||||||||||
| 1080 | my @valuelist = $valuelistav->ARRAY; | ||||||||||
| 1081 | my $ix; | ||||||||||
| 1082 | @pad = (); | ||||||||||
| 1083 | debug "load_pad: $#namelist names, $#valuelist values\n" if $debug{pad}; | ||||||||||
| 1084 | |||||||||||
| 1085 | # Temporary lexicals don't get named so it's possible for @valuelist | ||||||||||
| 1086 | # to be strictly longer than @namelist. We count $ix up to the end of | ||||||||||
| 1087 | # @valuelist but index into @namelist for the name. Any temporaries which | ||||||||||
| 1088 | # run off the end of @namelist will make $namesv undefined and we treat | ||||||||||
| 1089 | # that the same as having an explicit SPECIAL sv_undef object in @namelist. | ||||||||||
| 1090 | # [XXX If/when @_ becomes a lexical, we must start at 0 here.] | ||||||||||
| 1091 | for ( $ix = 1 ; $ix < @valuelist ; $ix++ ) { | ||||||||||
| 1092 | my $namesv = $namelist[$ix]; | ||||||||||
| 1093 | my $type = T_UNKNOWN; | ||||||||||
| 1094 | my $flags = 0; | ||||||||||
| 1095 | my $name = "tmp"; | ||||||||||
| 1096 | my $class = B::class($namesv); | ||||||||||
| 1097 | if ( !defined($namesv) || $class eq "SPECIAL" ) { | ||||||||||
| 1098 | # temporaries have &PL_sv_undef instead of a PVNV for a name | ||||||||||
| 1099 | $flags = VALID_SV | TEMPORARY | REGISTER; | ||||||||||
| 1100 | } | ||||||||||
| 1101 | else { | ||||||||||
| 1102 | my ($nametry) = $namesv->PV =~ /^\$(.+)$/ if $namesv->PV; | ||||||||||
| 1103 | $name = $nametry if $nametry; | ||||||||||
| 1104 | |||||||||||
| 1105 | # my int $i; my num $d; compiled code only, unless the source provides the int and num packages. | ||||||||||
| 1106 | # With Ctypes it is easier. my c_int $i; defines an external Ctypes int, which can be efficiently | ||||||||||
| 1107 | # compiled in Perl also. | ||||||||||
| 1108 | # XXX Better use attributes, like my $i:int; my $d:num; which works un-compiled also. | ||||||||||
| 1109 | if (ref($namesv) eq 'B::PVMG' and ref($namesv->SvSTASH) eq 'B::HV') { # my int | ||||||||||
| 1110 | $class = $namesv->SvSTASH->NAME; | ||||||||||
| 1111 | if ($class eq 'int') { | ||||||||||
| 1112 | $type = T_INT; | ||||||||||
| 1113 | $flags = VALID_SV | VALID_INT; | ||||||||||
| 1114 | } | ||||||||||
| 1115 | elsif ($class eq 'num' or $class eq 'double') { # my num | ||||||||||
| 1116 | $type = T_NUM; | ||||||||||
| 1117 | $flags = VALID_SV | VALID_NUM; | ||||||||||
| 1118 | } | ||||||||||
| 1119 | elsif ($class eq 'str' or $class eq 'string') { # my str | ||||||||||
| 1120 | $type = T_STR; | ||||||||||
| 1121 | $flags = VALID_SV | VALID_STR; | ||||||||||
| 1122 | } | ||||||||||
| 1123 | #elsif ($class eq 'c_int') { # use Ctypes; | ||||||||||
| 1124 | # $type = T_INT; | ||||||||||
| 1125 | # $flags = VALID_SV | VALID_INT; | ||||||||||
| 1126 | #} | ||||||||||
| 1127 | #elsif ($class eq 'c_double') { | ||||||||||
| 1128 | # $type = T_NUM; | ||||||||||
| 1129 | # $flags = VALID_SV | VALID_NUM; | ||||||||||
| 1130 | #} | ||||||||||
| 1131 | # TODO: MooseX::Types | ||||||||||
| 1132 | } | ||||||||||
| 1133 | |||||||||||
| 1134 | # Valid scalar type attributes: | ||||||||||
| 1135 | # int num str ro readonly const unsigned | ||||||||||
| 1136 | # Note: PVMG from above also. | ||||||||||
| 1137 | # Typed arrays and hashes later. | ||||||||||
| 1138 | if (0 and $class =~ /^(I|P|S|N)V/ | ||||||||||
| 1139 | and $opt_type_attr | ||||||||||
| 1140 | and UNIVERSAL::can($class,"CHECK_SCALAR_ATTRIBUTES")) # with 5.18 | ||||||||||
| 1141 | { | ||||||||||
| 1142 | require attributes; | ||||||||||
| 1143 | #my $svtype = uc reftype ($namesv); | ||||||||||
| 1144 | # test 105 | ||||||||||
| 1145 | my @attr = attributes::get(\$namesv); # how to get em from B? see optimize | ||||||||||
| 1146 | warn "\$$name attrs: ".@attr if $verbose or $debug{pad}; | ||||||||||
| 1147 | #my $valid_types = ${"$class\::valid_attr"}; # They ARE valid, parser checked already. | ||||||||||
| 1148 | } | ||||||||||
| 1149 | |||||||||||
| 1150 | # XXX We should try Devel::TypeCheck for type inference also | ||||||||||
| 1151 | |||||||||||
| 1152 | # magic names: my $i_ir, my $d_d. without -fno-name-magic cmdline option only | ||||||||||
| 1153 | if ( $type == T_UNKNOWN and $opt_name_magic and $name =~ /^(.*)_([dis])(r?)$/ ) { | ||||||||||
| 1154 | $name = $1; | ||||||||||
| 1155 | if ( $2 eq "i" ) { | ||||||||||
| 1156 | $type = T_INT; | ||||||||||
| 1157 | $flags = VALID_SV | VALID_INT; | ||||||||||
| 1158 | } | ||||||||||
| 1159 | elsif ( $2 eq "d" ) { | ||||||||||
| 1160 | $type = T_NUM; | ||||||||||
| 1161 | $flags = VALID_SV | VALID_NUM; | ||||||||||
| 1162 | } | ||||||||||
| 1163 | elsif ( $2 eq "s" ) { | ||||||||||
| 1164 | $type = T_STR; | ||||||||||
| 1165 | $flags = VALID_SV | VALID_STR; | ||||||||||
| 1166 | } | ||||||||||
| 1167 | $flags |= REGISTER if $3; | ||||||||||
| 1168 | } | ||||||||||
| 1169 | } | ||||||||||
| 1170 | $name = "${ix}_$name"; | ||||||||||
| 1171 | # comppadname bug with overlong strings | ||||||||||
| 1172 | if ($] < 5.008008 and length($name) > 100 and $name =~ /\0\0/) { | ||||||||||
| 1173 | my $i = index($name,"\0"); | ||||||||||
| 1174 | $name = substr($name,0,$i) if $i > -1; | ||||||||||
| 1175 | } | ||||||||||
| 1176 | $pad[$ix] = | ||||||||||
| 1177 | B::Stackobj::Padsv->new( $type, $flags, $ix, "i$name", "d$name" ); | ||||||||||
| 1178 | |||||||||||
| 1179 | debug sprintf( "PL_curpad[$ix] = %s\n", $pad[$ix]->peek ) if $debug{pad}; | ||||||||||
| 1180 | } | ||||||||||
| 1181 | } | ||||||||||
| 1182 | |||||||||||
| 1183 | sub declare_pad { | ||||||||||
| 1184 | my $ix; | ||||||||||
| 1185 | for ( $ix = 1 ; $ix <= $#pad ; $ix++ ) { | ||||||||||
| 1186 | my $type = $pad[$ix]->{type}; | ||||||||||
| 1187 | declare( "IV", | ||||||||||
| 1188 | $type == T_INT ? sprintf( "%s=0", $pad[$ix]->{iv} ) : $pad[$ix]->{iv} ) | ||||||||||
| 1189 | if $pad[$ix]->save_int; | ||||||||||
| 1190 | declare( "NV", | ||||||||||
| 1191 | $type == T_NUM | ||||||||||
| 1192 | ? sprintf( "%s = 0", $pad[$ix]->{nv} ) | ||||||||||
| 1193 | : $pad[$ix]->{nv} ) | ||||||||||
| 1194 | if $pad[$ix]->save_double; | ||||||||||
| 1195 | declare( "PV", | ||||||||||
| 1196 | $type == T_STR | ||||||||||
| 1197 | ? sprintf( "%s = 0", $pad[$ix]->{sv} ) | ||||||||||
| 1198 | : $pad[$ix]->{sv} ) | ||||||||||
| 1199 | if $pad[$ix]->save_str; | ||||||||||
| 1200 | } | ||||||||||
| 1201 | } | ||||||||||
| 1202 | |||||||||||
| 1203 | # for cc: unique ascii representation of an utf8 string, for labels | ||||||||||
| 1204 | sub encode_utf8($) { | ||||||||||
| 1205 | my $l = shift; | ||||||||||
| 1206 | if ($] > 5.007 and utf8::is_utf8($l)) { | ||||||||||
| 1207 | # utf8::encode($l); | ||||||||||
| 1208 | # $l =~ s/([\x{0100}-\x{ffff}])/sprintf("u%x", $1)/ge; | ||||||||||
| 1209 | #$l = substr(B::cstring($l), 1, -1); | ||||||||||
| 1210 | #$l =~ s/\\/u/g; | ||||||||||
| 1211 | $l = join('', map { $_ < 127 ? $_ : sprintf("u_%x_", $_) } unpack("U*", $l)); | ||||||||||
| 1212 | } | ||||||||||
| 1213 | return $l; | ||||||||||
| 1214 | } | ||||||||||
| 1215 | |||||||||||
| 1216 | # | ||||||||||
| 1217 | # Debugging stuff | ||||||||||
| 1218 | # | ||||||||||
| 1219 | sub peek_stack { | ||||||||||
| 1220 | sprintf "stack = %s\n", join( " ", map( $_->minipeek, @stack ) ); | ||||||||||
| 1221 | } | ||||||||||
| 1222 | |||||||||||
| 1223 | # | ||||||||||
| 1224 | # OP stuff | ||||||||||
| 1225 | # | ||||||||||
| 1226 | |||||||||||
| 1227 | =head2 label | ||||||||||
| 1228 | |||||||||||
| 1229 | We not only mark named labels in C as such - with prefix "label_". | ||||||||||
| 1230 | |||||||||||
| 1231 | We also have to mark each known (back jumps) and yet unknown branch targets | ||||||||||
| 1232 | (forward jumps) for compile-time generated branch points, with the "lab_" | ||||||||||
| 1233 | prefix. | ||||||||||
| 1234 | |||||||||||
| 1235 | =cut | ||||||||||
| 1236 | |||||||||||
| 1237 | sub label { | ||||||||||
| 1238 | my $op = shift; | ||||||||||
| 1239 | # Preserve original label name for "real" labels | ||||||||||
| 1240 | if ($op->can("label") and $op->label) { | ||||||||||
| 1241 | my $l = encode_utf8 $op->label; | ||||||||||
| 1242 | # cc should error on duplicate named labels | ||||||||||
| 1243 | return sprintf( "label_%s_%x", $l, $$op); | ||||||||||
| 1244 | } else { | ||||||||||
| 1245 | return sprintf( "lab_%x", $$op ); | ||||||||||
| 1246 | } | ||||||||||
| 1247 | } | ||||||||||
| 1248 | |||||||||||
| 1249 | sub write_label { | ||||||||||
| 1250 | my $op = shift; | ||||||||||
| 1251 | $op->save if $$op; | ||||||||||
| 1252 | # debug sprintf("lab_%x:?\n", $$op) if $debug{cxstack}; | ||||||||||
| 1253 | unless ($labels->{label}->{$$op}) { | ||||||||||
| 1254 | my $l = label($op); | ||||||||||
| 1255 | # named label but op not yet known? | ||||||||||
| 1256 | if ( $op->can("label") and $op->label ) { | ||||||||||
| 1257 | $l = "label_" . encode_utf8 $op->label; | ||||||||||
| 1258 | # only print first such label. test 21 | ||||||||||
| 1259 | push_runtime(sprintf( " %s:", $l)) | ||||||||||
| 1260 | unless $labels->{label}->{$l}; | ||||||||||
| 1261 | $labels->{label}->{$l} = $$op; | ||||||||||
| 1262 | } | ||||||||||
| 1263 | if ($verbose) { | ||||||||||
| 1264 | push_runtime(sprintf( " %s:\t/* %s */", label($op), $op->name )); | ||||||||||
| 1265 | } else { | ||||||||||
| 1266 | push_runtime(sprintf( " %s:", label($op) )); | ||||||||||
| 1267 | } | ||||||||||
| 1268 | # avoid printing duplicate jump labels | ||||||||||
| 1269 | $labels->{label}->{$$op} = $l; | ||||||||||
| 1270 | if ($op->can("label") and $op->label ) { | ||||||||||
| 1271 | push(@cxstack, { | ||||||||||
| 1272 | type => 0, | ||||||||||
| 1273 | op => $op, | ||||||||||
| 1274 | nextop => ((ref($op) eq 'B::LOOP') && $op->nextop) ? $op->nextop : $op, | ||||||||||
| 1275 | redoop => ((ref($op) eq 'B::LOOP') && $op->redoop) ? $op->redoop : $op, | ||||||||||
| 1276 | lastop => ((ref($op) eq 'B::LOOP') && $op->lastop) ? $op->lastop : $op, | ||||||||||
| 1277 | 'label' => $op->can("label") && $op->label ? $op->label : $l | ||||||||||
| 1278 | }); | ||||||||||
| 1279 | } | ||||||||||
| 1280 | } | ||||||||||
| 1281 | } | ||||||||||
| 1282 | |||||||||||
| 1283 | sub loadop { | ||||||||||
| 1284 | my $op = shift; | ||||||||||
| 1285 | my $opsym = $op->save; | ||||||||||
| 1286 | $op_count++; # for statistics | ||||||||||
| 1287 | runtime("PL_op = $opsym;") unless $know_op; | ||||||||||
| 1288 | return $opsym; | ||||||||||
| 1289 | } | ||||||||||
| 1290 | |||||||||||
| 1291 | sub doop { | ||||||||||
| 1292 | my $op = shift; | ||||||||||
| 1293 | my $ppaddr = $op->ppaddr; | ||||||||||
| 1294 | my $sym = loadop($op); | ||||||||||
| 1295 | my $ppname = "pp_" . $op->name; | ||||||||||
| 1296 | if ($inline_ops) { | ||||||||||
| 1297 | # inlining direct calls is safe, just CALLRUNOPS for macros not | ||||||||||
| 1298 | $ppaddr = "Perl_".$ppname; | ||||||||||
| 1299 | $no_stack{$ppname} | ||||||||||
| 1300 | ? runtime("PL_op = $ppaddr(aTHX);") | ||||||||||
| 1301 | : runtime("PUTBACK; PL_op = $ppaddr(aTHX); SPAGAIN;"); | ||||||||||
| 1302 | } else { | ||||||||||
| 1303 | $no_stack{$ppname} | ||||||||||
| 1304 | ? runtime("PL_op = $ppaddr(aTHX);") | ||||||||||
| 1305 | : runtime("DOOP($ppaddr);"); | ||||||||||
| 1306 | } | ||||||||||
| 1307 | $know_op = 1; | ||||||||||
| 1308 | return $sym; | ||||||||||
| 1309 | } | ||||||||||
| 1310 | |||||||||||
| 1311 | sub gimme { | ||||||||||
| 1312 | my $op = shift; | ||||||||||
| 1313 | my $want = $op->flags & OPf_WANT; | ||||||||||
| 1314 | return ( $want == OPf_WANT_VOID ? G_VOID : | ||||||||||
| 1315 | $want == OPf_WANT_SCALAR ? G_SCALAR : | ||||||||||
| 1316 | $want == OPf_WANT_LIST ? G_ARRAY : | ||||||||||
| 1317 | undef ); | ||||||||||
| 1318 | } | ||||||||||
| 1319 | |||||||||||
| 1320 | # | ||||||||||
| 1321 | # Code generation for PP code | ||||||||||
| 1322 | # | ||||||||||
| 1323 | |||||||||||
| 1324 | # coverage: 18,19,25,... | ||||||||||
| 1325 | sub pp_null { | ||||||||||
| 1326 | my $op = shift; | ||||||||||
| 1327 | $B::C::nullop_count++; | ||||||||||
| 1328 | return $op->next; | ||||||||||
| 1329 | } | ||||||||||
| 1330 | |||||||||||
| 1331 | # coverage: 102 | ||||||||||
| 1332 | sub pp_stub { | ||||||||||
| 1333 | my $op = shift; | ||||||||||
| 1334 | my $gimme = gimme($op); | ||||||||||
| 1335 | if ( not defined $gimme ) { | ||||||||||
| 1336 | write_back_stack(); | ||||||||||
| 1337 | runtime("if (block_gimme() == G_SCALAR)", | ||||||||||
| 1338 | "\tXPUSHs(&PL_sv_undef);"); | ||||||||||
| 1339 | } elsif ( $gimme == G_SCALAR ) { | ||||||||||
| 1340 | my $obj = B::Stackobj::Const->new(sv_undef); | ||||||||||
| 1341 | push( @stack, $obj ); | ||||||||||
| 1342 | } | ||||||||||
| 1343 | return $op->next; | ||||||||||
| 1344 | } | ||||||||||
| 1345 | |||||||||||
| 1346 | # coverage: 2,21,28,30 | ||||||||||
| 1347 | sub pp_unstack { | ||||||||||
| 1348 | my $op = shift; | ||||||||||
| 1349 | @stack = (); | ||||||||||
| 1350 | runtime("PP_UNSTACK;"); | ||||||||||
| 1351 | return $op->next; | ||||||||||
| 1352 | } | ||||||||||
| 1353 | |||||||||||
| 1354 | # coverage: 2,21,27,28,30 | ||||||||||
| 1355 | sub pp_and { | ||||||||||
| 1356 | my $op = shift; | ||||||||||
| 1357 | my $next = $op->next; | ||||||||||
| 1358 | reload_lexicals(); | ||||||||||
| 1359 | unshift( @bblock_todo, $next ); | ||||||||||
| 1360 | if ( @stack >= 1 ) { | ||||||||||
| 1361 | my $obj = pop @stack; | ||||||||||
| 1362 | my $bool = $obj->as_bool; | ||||||||||
| 1363 | write_back_stack(); | ||||||||||
| 1364 | save_or_restore_lexical_state($$next); | ||||||||||
| 1365 | if ($bool =~ /POPs/) { | ||||||||||
| 1366 | runtime("sv = $bool;", | ||||||||||
| 1367 | sprintf("if (!sv) { PUSHs(sv); goto %s;}", label($next))); | ||||||||||
| 1368 | } else { | ||||||||||
| 1369 | runtime(sprintf( | ||||||||||
| 1370 | "if (!$bool) { PUSHs((SV*)%s); goto %s;}", $obj->as_sv, label($next) | ||||||||||
| 1371 | )); | ||||||||||
| 1372 | } | ||||||||||
| 1373 | } | ||||||||||
| 1374 | else { | ||||||||||
| 1375 | save_or_restore_lexical_state($$next); | ||||||||||
| 1376 | runtime( sprintf( "if (!%s) goto %s;", top_bool(), label($next) ), | ||||||||||
| 1377 | "sp--;" ); | ||||||||||
| 1378 | } | ||||||||||
| 1379 | return $op->other; | ||||||||||
| 1380 | } | ||||||||||
| 1381 | |||||||||||
| 1382 | # Nearly identical to pp_and, but leaves stack unchanged. | ||||||||||
| 1383 | sub pp_andassign { | ||||||||||
| 1384 | my $op = shift; | ||||||||||
| 1385 | my $next = $op->next; | ||||||||||
| 1386 | reload_lexicals(); | ||||||||||
| 1387 | unshift( @bblock_todo, $next ); | ||||||||||
| 1388 | if ( @stack >= 1 ) { | ||||||||||
| 1389 | my $obj = pop @stack; | ||||||||||
| 1390 | my $bool = $obj->as_bool; | ||||||||||
| 1391 | write_back_stack(); | ||||||||||
| 1392 | save_or_restore_lexical_state($$next); | ||||||||||
| 1393 | if ($bool =~ /POPs/) { | ||||||||||
| 1394 | runtime("sv = $bool;", | ||||||||||
| 1395 | sprintf("PUSHs((SV*)%s); if (!$bool) { goto %s;}", | ||||||||||
| 1396 | $obj->as_sv, label($next))); | ||||||||||
| 1397 | } else { | ||||||||||
| 1398 | runtime( | ||||||||||
| 1399 | sprintf("PUSHs((SV*)%s); if (!$bool) { goto %s;}", | ||||||||||
| 1400 | $obj->as_sv, label($next))); | ||||||||||
| 1401 | } | ||||||||||
| 1402 | } | ||||||||||
| 1403 | else { | ||||||||||
| 1404 | save_or_restore_lexical_state($$next); | ||||||||||
| 1405 | runtime( sprintf( "if (!%s) goto %s;", top_bool(), label($next) ) ); | ||||||||||
| 1406 | } | ||||||||||
| 1407 | return $op->other; | ||||||||||
| 1408 | } | ||||||||||
| 1409 | |||||||||||
| 1410 | # coverage: 28 | ||||||||||
| 1411 | sub pp_or { | ||||||||||
| 1412 | my $op = shift; | ||||||||||
| 1413 | my $next = $op->next; | ||||||||||
| 1414 | reload_lexicals(); | ||||||||||
| 1415 | unshift( @bblock_todo, $next ); | ||||||||||
| 1416 | if ( @stack >= 1 ) { | ||||||||||
| 1417 | my $obj = pop @stack; | ||||||||||
| 1418 | my $bool = $obj->as_bool; | ||||||||||
| 1419 | write_back_stack(); | ||||||||||
| 1420 | save_or_restore_lexical_state($$next); | ||||||||||
| 1421 | if ($bool =~ /POPs/) { | ||||||||||
| 1422 | runtime("sv = $bool;", | ||||||||||
| 1423 | sprintf("if (sv) { PUSHs(sv); goto %s;}", label($next))); | ||||||||||
| 1424 | } else { | ||||||||||
| 1425 | runtime( | ||||||||||
| 1426 | sprintf("if ($bool) { PUSHs((SV*)%s); goto %s; }", $obj->as_sv, label($next))); | ||||||||||
| 1427 | } | ||||||||||
| 1428 | } | ||||||||||
| 1429 | else { | ||||||||||
| 1430 | save_or_restore_lexical_state($$next); | ||||||||||
| 1431 | runtime( sprintf( "if (%s) goto %s;", top_bool(), label($next) ), | ||||||||||
| 1432 | "sp--;" ); | ||||||||||
| 1433 | } | ||||||||||
| 1434 | return $op->other; | ||||||||||
| 1435 | } | ||||||||||
| 1436 | |||||||||||
| 1437 | # Nearly identical to pp_or, but leaves stack unchanged. | ||||||||||
| 1438 | sub pp_orassign { | ||||||||||
| 1439 | my $op = shift; | ||||||||||
| 1440 | my $next = $op->next; | ||||||||||
| 1441 | reload_lexicals(); | ||||||||||
| 1442 | unshift( @bblock_todo, $next ); | ||||||||||
| 1443 | if ( @stack >= 1 ) { | ||||||||||
| 1444 | my $obj = pop @stack; | ||||||||||
| 1445 | my $bool = $obj->as_bool; | ||||||||||
| 1446 | write_back_stack(); | ||||||||||
| 1447 | save_or_restore_lexical_state($$next); | ||||||||||
| 1448 | runtime( | ||||||||||
| 1449 | sprintf( | ||||||||||
| 1450 | "PUSHs((SV*)%s); if ($bool) { goto %s; }", $obj->as_sv, label($next) | ||||||||||
| 1451 | ) | ||||||||||
| 1452 | ); | ||||||||||
| 1453 | } | ||||||||||
| 1454 | else { | ||||||||||
| 1455 | save_or_restore_lexical_state($$next); | ||||||||||
| 1456 | runtime( sprintf( "if (%s) goto %s;", top_bool(), label($next) ) ); | ||||||||||
| 1457 | } | ||||||||||
| 1458 | return $op->other; | ||||||||||
| 1459 | } | ||||||||||
| 1460 | |||||||||||
| 1461 | # coverage: issue 45 (1,2) | ||||||||||
| 1462 | # in CORE aliased to pp_defined | ||||||||||
| 1463 | # default dor is okay issue 45 (3,4) | ||||||||||
| 1464 | sub pp_dorassign { | ||||||||||
| 1465 | my $op = shift; | ||||||||||
| 1466 | my $next = $op->next; | ||||||||||
| 1467 | reload_lexicals(); | ||||||||||
| 1468 | unshift( @bblock_todo, $next ); | ||||||||||
| 1469 | my $sv = pop @stack; | ||||||||||
| 1470 | write_back_stack(); | ||||||||||
| 1471 | save_or_restore_lexical_state($$next); | ||||||||||
| 1472 | runtime( sprintf( "PUSHs(%s); if (%s && SvANY(%s)) goto %s;\t/* dorassign */", | ||||||||||
| 1473 | $sv->as_sv, $sv->as_sv, $sv->as_sv, label($next)) ) if $sv; | ||||||||||
| 1474 | return $op->other; | ||||||||||
| 1475 | } | ||||||||||
| 1476 | |||||||||||
| 1477 | # coverage: 102 | ||||||||||
| 1478 | sub pp_cond_expr { | ||||||||||
| 1479 | my $op = shift; | ||||||||||
| 1480 | my $false = $op->next; | ||||||||||
| 1481 | unshift( @bblock_todo, $false ); | ||||||||||
| 1482 | reload_lexicals(); | ||||||||||
| 1483 | my $bool = pop_bool(); | ||||||||||
| 1484 | write_back_stack(); | ||||||||||
| 1485 | save_or_restore_lexical_state($$false); | ||||||||||
| 1486 | runtime( sprintf( "if (!$bool) goto %s;\t/* cond_expr */", label($false) ) ); | ||||||||||
| 1487 | return $op->other; | ||||||||||
| 1488 | } | ||||||||||
| 1489 | |||||||||||
| 1490 | # coverage: 9,10,12,17,18,22,28,32 | ||||||||||
| 1491 | sub pp_padsv { | ||||||||||
| 1492 | my $op = shift; | ||||||||||
| 1493 | my $ix = $op->targ; | ||||||||||
| 1494 | push( @stack, $pad[$ix] ) if $pad[$ix]; | ||||||||||
| 1495 | if ( $op->flags & OPf_MOD ) { | ||||||||||
| 1496 | my $private = $op->private; | ||||||||||
| 1497 | if ( $private & OPpLVAL_INTRO ) { | ||||||||||
| 1498 | # coverage: 9,10,12,17,18,19,20,22,27,28,31,32 | ||||||||||
| 1499 | runtime("SAVECLEARSV(PL_curpad[$ix]);"); | ||||||||||
| 1500 | } | ||||||||||
| 1501 | elsif ( $private & OPpDEREF ) { | ||||||||||
| 1502 | # coverage: 18 | ||||||||||
| 1503 | if ($] >= 5.015002) { | ||||||||||
| 1504 | runtime(sprintf( "PL_curpad[%d] = Perl_vivify_ref(aTHX_ PL_curpad[%d], %d);", | ||||||||||
| 1505 | $ix, $ix, $private & OPpDEREF )); | ||||||||||
| 1506 | } else { | ||||||||||
| 1507 | runtime(sprintf( "Perl_vivify_ref(aTHX_ PL_curpad[%d], %d);", | ||||||||||
| 1508 | $ix, $private & OPpDEREF )); | ||||||||||
| 1509 | } | ||||||||||
| 1510 | $vivify_ref_defined++; | ||||||||||
| 1511 | $pad[$ix]->invalidate if $pad[$ix]; | ||||||||||
| 1512 | } | ||||||||||
| 1513 | } | ||||||||||
| 1514 | return $op->next; | ||||||||||
| 1515 | } | ||||||||||
| 1516 | |||||||||||
| 1517 | # coverage: 1-5,7-14,18-23,25,27-32 | ||||||||||
| 1518 | sub pp_const { | ||||||||||
| 1519 | my $op = shift; | ||||||||||
| 1520 | my $sv = $op->sv; | ||||||||||
| 1521 | my $obj; | ||||||||||
| 1522 | |||||||||||
| 1523 | # constant could be in the pad (under useithreads) | ||||||||||
| 1524 | if ($$sv) { | ||||||||||
| 1525 | $obj = $constobj{$$sv}; | ||||||||||
| 1526 | if ( !defined($obj) ) { | ||||||||||
| 1527 | $obj = $constobj{$$sv} = B::Stackobj::Const->new($sv); | ||||||||||
| 1528 | } | ||||||||||
| 1529 | } | ||||||||||
| 1530 | else { | ||||||||||
| 1531 | $obj = $pad[ $op->targ ]; | ||||||||||
| 1532 | } | ||||||||||
| 1533 | # XXX looks like method_named has only const as prev op | ||||||||||
| 1534 | if ($op->next | ||||||||||
| 1535 | and $op->next->can('name') | ||||||||||
| 1536 | and $op->next->name eq 'method_named' | ||||||||||
| 1537 | ) { | ||||||||||
| 1538 | $package_pv = svop_or_padop_pv($op); | ||||||||||
| 1539 | debug "save package_pv \"$package_pv\" for method_name\n" if $debug{op}; | ||||||||||
| 1540 | } | ||||||||||
| 1541 | push( @stack, $obj ); | ||||||||||
| 1542 | return $op->next; | ||||||||||
| 1543 | } | ||||||||||
| 1544 | |||||||||||
| 1545 | # coverage: 1-39, fails in 33 | ||||||||||
| 1546 | sub pp_nextstate { | ||||||||||
| 1547 | my $op = shift; | ||||||||||
| 1548 | if ($labels->{'nextstate'}->[-1] and $labels->{'nextstate'}->[-1] == $op) { | ||||||||||
| 1549 | debug sprintf("pop_label nextstate: cxstack label %s\n", $curcop->[0]->label) if $debug{cxstack}; | ||||||||||
| 1550 | pop_label 'nextstate'; | ||||||||||
| 1551 | } else { | ||||||||||
| 1552 | write_label($op); | ||||||||||
| 1553 | } | ||||||||||
| 1554 | $curcop->load($op); | ||||||||||
| 1555 | loadop($op); | ||||||||||
| 1556 | #testcc 48: protect CopFILE_free and CopSTASH_free in END block (#296) | ||||||||||
| 1557 | if ($ppname =~ /^pp_sub_END(_\d+)?$/ and $ITHREADS) { | ||||||||||
| 1558 | runtime("#ifdef USE_ITHREADS", | ||||||||||
| 1559 | "CopFILE((COP*)PL_op) = NULL;"); | ||||||||||
| 1560 | if ($] >= 5.018) { | ||||||||||
| 1561 | runtime("CopSTASH_set((COP*)PL_op, NULL);"); | ||||||||||
| 1562 | } elsif ($] >= 5.016 and $] <= 5.017) { | ||||||||||
| 1563 | runtime("CopSTASHPV_set((COP*)PL_op, NULL, 0);"); | ||||||||||
| 1564 | } else { | ||||||||||
| 1565 | runtime("CopSTASHPV_set((COP*)PL_op, NULL);"); | ||||||||||
| 1566 | } | ||||||||||
| 1567 | runtime("#endif"); | ||||||||||
| 1568 | } | ||||||||||
| 1569 | @stack = (); | ||||||||||
| 1570 | debug( sprintf( "%s:%d\n", $op->file, $op->line ) ) if $debug{lineno}; | ||||||||||
| 1571 | debug( sprintf( "CopLABEL %s\n", $op->label ) ) if $op->label and $debug{cxstack}; | ||||||||||
| 1572 | runtime("TAINT_NOT;") if $opt_taint; # TODO Not always needed (resets PL_taint = 0) | ||||||||||
| 1573 | runtime("sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;"); # TODO reset sp not needed always | ||||||||||
| 1574 | if ( $freetmps_each_bblock || $freetmps_each_loop ) { | ||||||||||
| 1575 | $need_freetmps = 1; | ||||||||||
| 1576 | } | ||||||||||
| 1577 | else { | ||||||||||
| 1578 | runtime("FREETMPS;"); # TODO Not always needed | ||||||||||
| 1579 | } | ||||||||||
| 1580 | return $op->next; | ||||||||||
| 1581 | } | ||||||||||
| 1582 | |||||||||||
| 1583 | # Like pp_nextstate, but used instead when the debugger is active. | ||||||||||
| 1584 | sub pp_dbstate { pp_nextstate(@_) } | ||||||||||
| 1585 | |||||||||||
| 1586 | #default_pp will handle this: | ||||||||||
| 1587 | #sub pp_bless { $curcop->write_back; default_pp(@_) } | ||||||||||
| 1588 | #sub pp_repeat { $curcop->write_back; default_pp(@_) } | ||||||||||
| 1589 | # The following subs need $curcop->write_back if we decide to support arybase: | ||||||||||
| 1590 | # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice | ||||||||||
| 1591 | #sub pp_caller { $curcop->write_back; default_pp(@_) } | ||||||||||
| 1592 | |||||||||||
| 1593 | # coverage: ny | ||||||||||
| 1594 | sub bad_pp_reset { | ||||||||||
| 1595 | if ($inline_ops) { | ||||||||||
| 1596 | my $op = shift; | ||||||||||
| 1597 | warn "inlining reset\n" if $debug{op}; | ||||||||||
| 1598 | $curcop->write_back if $curcop; | ||||||||||
| 1599 | runtime '{ /* pp_reset */'; | ||||||||||
| 1600 | runtime ' const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;'; | ||||||||||
| 1601 | runtime ' sv_reset(tmps, CopSTASH(PL_curcop));}'; | ||||||||||
| 1602 | runtime 'PUSHs(&PL_sv_yes);'; | ||||||||||
| 1603 | return $op->next; | ||||||||||
| 1604 | } else { | ||||||||||
| 1605 | default_pp(@_); | ||||||||||
| 1606 | } | ||||||||||
| 1607 | } | ||||||||||
| 1608 | |||||||||||
| 1609 | # coverage: 20 | ||||||||||
| 1610 | sub pp_regcreset { | ||||||||||
| 1611 | if ($inline_ops) { | ||||||||||
| 1612 | my $op = shift; | ||||||||||
| 1613 | warn "inlining regcreset\n" if $debug{op}; | ||||||||||
| 1614 | $curcop->write_back if $curcop; | ||||||||||
| 1615 | runtime 'PL_reginterp_cnt = 0; /* pp_regcreset */'; | ||||||||||
| 1616 | runtime 'TAINT_NOT;' if $opt_taint; | ||||||||||
| 1617 | return $op->next; | ||||||||||
| 1618 | } else { | ||||||||||
| 1619 | default_pp(@_); | ||||||||||
| 1620 | } | ||||||||||
| 1621 | } | ||||||||||
| 1622 | |||||||||||
| 1623 | # coverage: 103 | ||||||||||
| 1624 | sub pp_stringify { | ||||||||||
| 1625 | if ($inline_ops and $] >= 5.008) { | ||||||||||
| 1626 | my $op = shift; | ||||||||||
| 1627 | warn "inlining stringify\n" if $debug{op}; | ||||||||||
| 1628 | my $sv = top_sv(); | ||||||||||
| 1629 | my $ix = $op->targ; | ||||||||||
| 1630 | my $targ = $pad[$ix]; | ||||||||||
| 1631 | runtime "sv_copypv(PL_curpad[$ix], $sv);\t/* pp_stringify */"; | ||||||||||
| 1632 | $stack[-1] = $targ if @stack; | ||||||||||
| 1633 | return $op->next; | ||||||||||
| 1634 | } else { | ||||||||||
| 1635 | default_pp(@_); | ||||||||||
| 1636 | } | ||||||||||
| 1637 | } | ||||||||||
| 1638 | |||||||||||
| 1639 | # coverage: 9,10,27 | ||||||||||
| 1640 | sub bad_pp_anoncode { | ||||||||||
| 1641 | if ($inline_ops) { | ||||||||||
| 1642 | my $op = shift; | ||||||||||
| 1643 | warn "inlining anoncode\n" if $debug{op}; | ||||||||||
| 1644 | my $ix = $op->targ; | ||||||||||
| 1645 | my $ppname = "pp_" . $op->name; | ||||||||||
| 1646 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 1647 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 1648 | # XXX finish me. this works only with >= 5.10 | ||||||||||
| 1649 | runtime '{ /* pp_anoncode */', | ||||||||||
| 1650 | ' CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));', | ||||||||||
| 1651 | ' if (CvCLONE(cv))', | ||||||||||
| 1652 | ' cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(Perl_cv_clone(aTHX_ cv))));', | ||||||||||
| 1653 | ' EXTEND(SP,1);', | ||||||||||
| 1654 | ' PUSHs(MUTABLE_SV(cv));', | ||||||||||
| 1655 | '}'; | ||||||||||
| 1656 | invalidate_lexicals() unless $skip_invalidate{$ppname}; | ||||||||||
| 1657 | return $op->next; | ||||||||||
| 1658 | } else { | ||||||||||
| 1659 | default_pp(@_); | ||||||||||
| 1660 | } | ||||||||||
| 1661 | } | ||||||||||
| 1662 | |||||||||||
| 1663 | # coverage: 35 | ||||||||||
| 1664 | # XXX TODO get prev op. For now saved in pp_const. | ||||||||||
| 1665 | sub pp_method_named { | ||||||||||
| 1666 | my ( $op ) = @_; | ||||||||||
| 1667 | my $name = svop_or_padop_pv($op); | ||||||||||
| 1668 | # The pkg PV is at [PL_stack_base+TOPMARK+1], the previous op->sv->PV. | ||||||||||
| 1669 | my $stash = $package_pv ? $package_pv."::" : "main::"; | ||||||||||
| 1670 | $name = $stash . $name; | ||||||||||
| 1671 | if (exists &$name) { | ||||||||||
| 1672 | debug "save method_name \"$name\"\n" if $debug{op}; | ||||||||||
| 1673 | svref_2object( \&{$name} )->save; | ||||||||||
| 1674 | } else { | ||||||||||
| 1675 | debug "skip saving non-existing method_name \"$name\"\n" if $debug{op}; #CC 50 | ||||||||||
| 1676 | } | ||||||||||
| 1677 | default_pp(@_); | ||||||||||
| 1678 | } | ||||||||||
| 1679 | |||||||||||
| 1680 | # inconsequence: gvs are not passed around on the stack | ||||||||||
| 1681 | # coverage: 26,103 | ||||||||||
| 1682 | sub bad_pp_srefgen { | ||||||||||
| 1683 | if ($inline_ops) { | ||||||||||
| 1684 | my $op = shift; | ||||||||||
| 1685 | warn "inlining srefgen\n" if $debug{op}; | ||||||||||
| 1686 | #my $ppname = "pp_" . $op->name; | ||||||||||
| 1687 | #$curcop->write_back; | ||||||||||
| 1688 | #write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 1689 | #write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 1690 | my $svobj = $stack[-1]->as_sv; | ||||||||||
| 1691 | my $sv = pop_sv(); | ||||||||||
| 1692 | # XXX fix me | ||||||||||
| 1693 | runtime "{ /* pp_srefgen */ | ||||||||||
| 1694 | SV* rv; | ||||||||||
| 1695 | SV* sv = $sv;"; | ||||||||||
| 1696 | # sv = POPs | ||||||||||
| 1697 | #B::svref_2object(\$sv); | ||||||||||
| 1698 | if (($svobj->flags & 0xff) == $SVt_PVLV | ||||||||||
| 1699 | and B::PVLV::LvTYPE($svobj) eq ord('y')) | ||||||||||
| 1700 | { | ||||||||||
| 1701 | runtime 'if (LvTARGLEN(sv)) | ||||||||||
| 1702 | vivify_defelem(sv); | ||||||||||
| 1703 | if (!(sv = LvTARG(sv))) | ||||||||||
| 1704 | sv = &PL_sv_undef; | ||||||||||
| 1705 | else | ||||||||||
| 1706 | SvREFCNT_inc_void_NN(sv);'; | ||||||||||
| 1707 | } | ||||||||||
| 1708 | elsif (($svobj->flags & 0xff) == $SVt_PVAV) { | ||||||||||
| 1709 | runtime 'if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv)) | ||||||||||
| 1710 | av_reify(MUTABLE_AV(sv)); | ||||||||||
| 1711 | SvTEMP_off(sv); | ||||||||||
| 1712 | SvREFCNT_inc_void_NN(sv);'; | ||||||||||
| 1713 | } | ||||||||||
| 1714 | #elsif ($sv->SvPADTMP && !IS_PADGV(sv)) { | ||||||||||
| 1715 | # runtime 'sv = newSVsv(sv);'; | ||||||||||
| 1716 | #} | ||||||||||
| 1717 | else { | ||||||||||
| 1718 | runtime 'SvTEMP_off(sv); | ||||||||||
| 1719 | SvREFCNT_inc_void_NN(sv);'; | ||||||||||
| 1720 | } | ||||||||||
| 1721 | runtime 'rv = sv_newmortal(); | ||||||||||
| 1722 | sv_upgrade(rv, SVt_IV); | ||||||||||
| 1723 | SvRV_set(rv, sv); | ||||||||||
| 1724 | SvROK_on(rv); | ||||||||||
| 1725 | PUSHBACK; | ||||||||||
| 1726 | }'; | ||||||||||
| 1727 | return $op->next; | ||||||||||
| 1728 | } else { | ||||||||||
| 1729 | default_pp(@_); | ||||||||||
| 1730 | } | ||||||||||
| 1731 | } | ||||||||||
| 1732 | |||||||||||
| 1733 | # coverage: 9,10,27 | ||||||||||
| 1734 | #sub pp_refgen | ||||||||||
| 1735 | |||||||||||
| 1736 | # coverage: 28, 14 | ||||||||||
| 1737 | sub pp_rv2gv { | ||||||||||
| 1738 | my $op = shift; | ||||||||||
| 1739 | $curcop->write_back if $curcop; | ||||||||||
| 1740 | my $ppname = "pp_" . $op->name; | ||||||||||
| 1741 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 1742 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 1743 | my $sym = doop($op); | ||||||||||
| 1744 | if ( $op->private & OPpDEREF ) { | ||||||||||
| 1745 | $init->add( sprintf("((UNOP *)$sym)->op_first = $sym;") ); | ||||||||||
| 1746 | $init->add( sprintf( "((UNOP *)$sym)->op_type = %d;", $op->first->type ) ); | ||||||||||
| 1747 | } | ||||||||||
| 1748 | return $op->next; | ||||||||||
| 1749 | } | ||||||||||
| 1750 | |||||||||||
| 1751 | # coverage: 18,19,25 | ||||||||||
| 1752 | sub pp_sort { | ||||||||||
| 1753 | my $op = shift; | ||||||||||
| 1754 | #my $ppname = $op->ppaddr; | ||||||||||
| 1755 | if ( $op->flags & OPf_SPECIAL && $op->flags & OPf_STACKED ) { | ||||||||||
| 1756 | # blocksort is awful. E.g. we need to the leading NULL op, invalidates -fcop | ||||||||||
| 1757 | # Ugly surgery required. sort expects as block: pushmark rv2gv leave => enter | ||||||||||
| 1758 | # pp_sort() OP *kid = cLISTOP->op_first->op_sibling;/* skip over pushmark 4 to null */ | ||||||||||
| 1759 | # kid = cUNOPx(kid)->op_first; /* pass rv2gv (null'ed) */ | ||||||||||
| 1760 | # kid = cUNOPx(kid)->op_first; /* pass leave */ | ||||||||||
| 1761 | # | ||||||||||
| 1762 | #3 <0> pushmark s ->4 | ||||||||||
| 1763 | #8 <@> sort lKS* ->9 | ||||||||||
| 1764 | #4 <0> pushmark s ->5 | ||||||||||
| 1765 | #- <1> null sK/1 ->5 | ||||||||||
| 1766 | #- <1> ex-leave sKP ->- | ||||||||||
| 1767 | #- <0> enter s ->- | ||||||||||
| 1768 | # some code doing cmp or ncmp | ||||||||||
| 1769 | # Example with 3 const args: print sort { bla; $b <=> $a } 1,4,3 | ||||||||||
| 1770 | #5 <$> const[IV 1] s ->6 | ||||||||||
| 1771 | #6 <$> const[IV 4] s ->7 | ||||||||||
| 1772 | #7 <$> const[IV 3] s ->8 => sort | ||||||||||
| 1773 | # | ||||||||||
| 1774 | my $root = $op->first->sibling->first; #leave or null | ||||||||||
| 1775 | my $start = $root->first; #enter | ||||||||||
| 1776 | warn "blocksort: root=",$root->name,", start=",$start->name,"\n" if $debug{op}; | ||||||||||
| 1777 | my $pushmark = $op->first->save; #pushmark sibling to null | ||||||||||
| 1778 | $op->first->sibling->save; #null->first to leave | ||||||||||
| 1779 | $root->save; #ex-leave | ||||||||||
| 1780 | my $sym = $start->save; #enter | ||||||||||
| 1781 | my $fakeop = cc_queue( "pp_sort" . sprintf("%x",abs($$op)), $root, $start ); | ||||||||||
| 1782 | $init->add( sprintf( "(%s)->op_next = %s;", $sym, $fakeop ) ); | ||||||||||
| 1783 | } | ||||||||||
| 1784 | $curcop->write_back; | ||||||||||
| 1785 | write_back_lexicals(); | ||||||||||
| 1786 | write_back_stack(); | ||||||||||
| 1787 | doop($op); | ||||||||||
| 1788 | return $op->next; | ||||||||||
| 1789 | } | ||||||||||
| 1790 | |||||||||||
| 1791 | # coverage: 2-4,6,7,13,15,21,24,26,27,30,31 | ||||||||||
| 1792 | sub pp_gv { | ||||||||||
| 1793 | my $op = shift; | ||||||||||
| 1794 | my $gvsym; | ||||||||||
| 1795 | if ($ITHREADS) { | ||||||||||
| 1796 | $gvsym = $pad[ $op->padix ]->as_sv; | ||||||||||
| 1797 | #push @stack, ($pad[$op->padix]); | ||||||||||
| 1798 | } | ||||||||||
| 1799 | else { | ||||||||||
| 1800 | $gvsym = $op->gv->save; | ||||||||||
| 1801 | # XXX | ||||||||||
| 1802 | #my $obj = new B::Stackobj::Const($op->gv); | ||||||||||
| 1803 | #push( @stack, $obj ); | ||||||||||
| 1804 | } | ||||||||||
| 1805 | write_back_stack(); | ||||||||||
| 1806 | runtime("XPUSHs((SV*)$gvsym);"); | ||||||||||
| 1807 | return $op->next; | ||||||||||
| 1808 | } | ||||||||||
| 1809 | |||||||||||
| 1810 | # coverage: 2,3,4,9,11,14,20,21,23,28 | ||||||||||
| 1811 | sub pp_gvsv { | ||||||||||
| 1812 | my $op = shift; | ||||||||||
| 1813 | my $gvsym; | ||||||||||
| 1814 | if ($ITHREADS) { | ||||||||||
| 1815 | #debug(sprintf("OP name=%s, class=%s\n",$op->name,B::class($op))) if $debug{pad}; | ||||||||||
| 1816 | debug( sprintf( "GVSV->padix = %d\n", $op->padix ) ) if $debug{pad}; | ||||||||||
| 1817 | $gvsym = $pad[ $op->padix ]->as_sv; | ||||||||||
| 1818 | debug( sprintf( "GVSV->private = 0x%x\n", $op->private ) ) if $debug{pad}; | ||||||||||
| 1819 | } | ||||||||||
| 1820 | else { | ||||||||||
| 1821 | $gvsym = $op->gv->save; | ||||||||||
| 1822 | } | ||||||||||
| 1823 | write_back_stack(); | ||||||||||
| 1824 | # Expects GV*, not SV* PL_curpad | ||||||||||
| 1825 | $gvsym = "(GV*)$gvsym" if $gvsym =~ /PL_curpad/; | ||||||||||
| 1826 | if ($gvsym eq '(SV*)&PL_sv_undef') { | ||||||||||
| 1827 | runtime("XPUSHs($gvsym);"); | ||||||||||
| 1828 | } | ||||||||||
| 1829 | elsif ( $op->private & OPpLVAL_INTRO ) { | ||||||||||
| 1830 | runtime("XPUSHs(save_scalar($gvsym));"); | ||||||||||
| 1831 | #my $obj = new B::Stackobj::Const($op->gv); | ||||||||||
| 1832 | #push( @stack, $obj ); | ||||||||||
| 1833 | } | ||||||||||
| 1834 | else { | ||||||||||
| 1835 | $PERL510 | ||||||||||
| 1836 | ? runtime("XPUSHs(GvSVn($gvsym));") | ||||||||||
| 1837 | : runtime("XPUSHs(GvSV($gvsym));"); | ||||||||||
| 1838 | } | ||||||||||
| 1839 | return $op->next; | ||||||||||
| 1840 | } | ||||||||||
| 1841 | |||||||||||
| 1842 | # Check for faster fetch calls. Returns 0 if the fast 'no' is in effect. | ||||||||||
| 1843 | sub autovivification { | ||||||||||
| 1844 | if (!$opt_autovivify) { | ||||||||||
| 1845 | return 0; | ||||||||||
| 1846 | } elsif ($INC{'autovivification.pm'}) { | ||||||||||
| 1847 | return _autovivification($curcop->[0]); | ||||||||||
| 1848 | } else { | ||||||||||
| 1849 | return 1; | ||||||||||
| 1850 | } | ||||||||||
| 1851 | } | ||||||||||
| 1852 | |||||||||||
| 1853 | # coverage: 16, issue44 | ||||||||||
| 1854 | sub pp_aelemfast { | ||||||||||
| 1855 | my $op = shift; | ||||||||||
| 1856 | my ($av, $rmg); | ||||||||||
| 1857 | if ($op->flags & OPf_SPECIAL) { | ||||||||||
| 1858 | my $sv = $pad[ $op->targ ]->as_sv; | ||||||||||
| 1859 | my @c = comppadlist->ARRAY; | ||||||||||
| 1860 | my @p = $c[1]->ARRAY; | ||||||||||
| 1861 | my $lex = $p[ $op->targ ]; | ||||||||||
| 1862 | $rmg = ($lex and ref $lex eq 'B::AV' and ($lex->MAGICAL & SVs_RMG or !$lex->ARRAY)) ? 1 : 0; | ||||||||||
| 1863 | # MUTABLE_AV is only needed to catch compiler const loss | ||||||||||
| 1864 | # $av = $] > 5.01000 ? "MUTABLE_AV($sv)" : $sv; | ||||||||||
| 1865 | $av = "(AV*)$sv"; | ||||||||||
| 1866 | } else { | ||||||||||
| 1867 | my $gvsym; | ||||||||||
| 1868 | if ($ITHREADS) { #padop XXX if it's only a OP, no PADOP? t/CORE/op/ref.t test 36 | ||||||||||
| 1869 | if ($op->can('padix')) { | ||||||||||
| 1870 | #warn "padix\n"; | ||||||||||
| 1871 | $gvsym = $pad[ $op->padix ]->as_sv; | ||||||||||
| 1872 | my @c = comppadlist->ARRAY; # XXX curpad, not comppad!! | ||||||||||
| 1873 | my @p = $c[1]->ARRAY; | ||||||||||
| 1874 | my $lex = $p[ $op->padix ]; | ||||||||||
| 1875 | $rmg = ($lex and ref $lex eq 'B::AV' and ($lex->MAGICAL & SVs_RMG or !$lex->ARRAY)) ? 1 : 0; | ||||||||||
| 1876 | } else { | ||||||||||
| 1877 | $gvsym = 'PL_incgv'; # XXX passes, but need to investigate why. cc test 43 5.10.1 | ||||||||||
| 1878 | #write_back_stack(); | ||||||||||
| 1879 | #runtime("PUSHs(&PL_sv_undef);"); | ||||||||||
| 1880 | #return $op->next; | ||||||||||
| 1881 | } | ||||||||||
| 1882 | } | ||||||||||
| 1883 | else { #svop | ||||||||||
| 1884 | my $gv = $op->gv; | ||||||||||
| 1885 | $gvsym = $gv->save; | ||||||||||
| 1886 | my $gvav = $gv->AV; # test 16, tied gvav | ||||||||||
| 1887 | $rmg = $] < 5.007 ? 0 : ($gvav and ($gvav->MAGICAL & SVs_RMG or !$gvav->ARRAY)) ? 1 : 0; | ||||||||||
| 1888 | } | ||||||||||
| 1889 | $av = "GvAV($gvsym)"; | ||||||||||
| 1890 | } | ||||||||||
| 1891 | my $ix = $op->private; | ||||||||||
| 1892 | my $lval = $op->flags & OPf_MOD; | ||||||||||
| 1893 | my $vivify = !$rmg ? autovivification() : 1; # no need to call if $rmg | ||||||||||
| 1894 | debug "aelemfast: vivify=$vivify, rmg=$rmg, lval=$lval, -fautovivify=$opt_autovivify -faelem=$opt_aelem\n" if $debug{pad}; | ||||||||||
| 1895 | return _aelem($op, $av, $ix, $lval, $rmg, $vivify); | ||||||||||
| 1896 | } | ||||||||||
| 1897 | |||||||||||
| 1898 | sub _aelem { | ||||||||||
| 1899 | my ($op, $av, $ix, $lval, $rmg, $vivify) = @_; | ||||||||||
| 1900 | if ($opt_aelem and !$rmg and !$vivify and $ix >= 0) { | ||||||||||
| 1901 | push @stack, B::Stackobj::Aelem->new($av, $ix, $lval); | ||||||||||
| 1902 | } else { | ||||||||||
| 1903 | write_back_stack(); | ||||||||||
| 1904 | runtime( | ||||||||||
| 1905 | "{ AV* av = (AV*)$av;", | ||||||||||
| 1906 | " SV** const svp = av_fetch(av, $ix, $lval);", | ||||||||||
| 1907 | " SV *sv = (svp ? *svp : &PL_sv_undef);", | ||||||||||
| 1908 | (!$lval and $rmg) ? " if (SvRMAGICAL(av) && SvGMAGICAL(sv)) mg_get(sv);" : "", | ||||||||||
| 1909 | " PUSHs(sv);", | ||||||||||
| 1910 | "}" | ||||||||||
| 1911 | ); | ||||||||||
| 1912 | } | ||||||||||
| 1913 | return $op->next; | ||||||||||
| 1914 | } | ||||||||||
| 1915 | |||||||||||
| 1916 | # coverage: ? | ||||||||||
| 1917 | sub pp_aelem { | ||||||||||
| 1918 | my $op = shift; | ||||||||||
| 1919 | my ($ix, $av); | ||||||||||
| 1920 | my $lval = ($op->flags & OPf_MOD or $op->private & (OPpLVAL_DEFER || OPpLVAL_INTRO)) ? 1 : 0; | ||||||||||
| 1921 | my $vivify = autovivification(); | ||||||||||
| 1922 | my $rmg = $opt_magic; # use -fno-magic for the av (2nd stack arg) | ||||||||||
| 1923 | if (@stack >= 1) { # at least ix | ||||||||||
| 1924 | $ix = pop_int(); # TODO: substract CopARYBASE from ix | ||||||||||
| 1925 | if (@stack >= 1) { | ||||||||||
| 1926 | my $avobj = $stack[-1]->as_obj; | ||||||||||
| 1927 | $rmg = ($avobj and $avobj->MAGICAL & SVs_RMG) ? 1 : 0; | ||||||||||
| 1928 | } | ||||||||||
| 1929 | $av = pop_sv(); | ||||||||||
| 1930 | debug "aelem: vivify = $vivify, rmg = $rmg, lval = $lval\n" if $debug{pad}; | ||||||||||
| 1931 | return _aelem($op, $av, $ix, $lval, $rmg, $vivify); | ||||||||||
| 1932 | } else { | ||||||||||
| 1933 | if ($lval or $rmg) { # always | ||||||||||
| 1934 | return default_pp($op); | ||||||||||
| 1935 | } else { | ||||||||||
| 1936 | $ix = pop_int(); # TODO: substract CopARYBASE from ix | ||||||||||
| 1937 | $av = pop_sv(); | ||||||||||
| 1938 | debug "aelem: vivify = $vivify, rmg = $rmg, lval = $lval\n" if $debug{pad}; | ||||||||||
| 1939 | return _aelem($op, $av, $ix, $lval, $rmg, $vivify); | ||||||||||
| 1940 | } | ||||||||||
| 1941 | } | ||||||||||
| 1942 | } | ||||||||||
| 1943 | |||||||||||
| 1944 | # coverage: ? | ||||||||||
| 1945 | sub int_binop { | ||||||||||
| 1946 | my ( $op, $operator, $unsigned ) = @_; | ||||||||||
| 1947 | if ( $op->flags & OPf_STACKED ) { | ||||||||||
| 1948 | my $right = pop_int(); | ||||||||||
| 1949 | if ( @stack >= 1 ) { | ||||||||||
| 1950 | my $left = top_int(); | ||||||||||
| 1951 | $stack[-1]->set_int( &$operator( $left, $right ), $unsigned ); | ||||||||||
| 1952 | } | ||||||||||
| 1953 | else { | ||||||||||
| 1954 | my $sv_setxv = $unsigned ? 'sv_setuv' : 'sv_setiv'; | ||||||||||
| 1955 | runtime( sprintf( "$sv_setxv(TOPs, %s);", &$operator( "TOPi", $right ) ) ); | ||||||||||
| 1956 | } | ||||||||||
| 1957 | } | ||||||||||
| 1958 | else { | ||||||||||
| 1959 | my $targ = $pad[ $op->targ ]; | ||||||||||
| 1960 | my $right = B::Pseudoreg->new( "IV", "riv" ); | ||||||||||
| 1961 | my $left = B::Pseudoreg->new( "IV", "liv" ); | ||||||||||
| 1962 | runtime( sprintf( "$$right = %s; $$left = %s;", pop_int(), pop_int ) ); | ||||||||||
| 1963 | $targ->set_int( &$operator( $$left, $$right ), $unsigned ); | ||||||||||
| 1964 | push( @stack, $targ ); | ||||||||||
| 1965 | } | ||||||||||
| 1966 | return $op->next; | ||||||||||
| 1967 | } | ||||||||||
| 1968 | |||||||||||
| 1969 | sub INTS_CLOSED () { 0x1 } | ||||||||||
| 1970 | sub INT_RESULT () { 0x2 } | ||||||||||
| 1971 | sub NUMERIC_RESULT () { 0x4 } | ||||||||||
| 1972 | |||||||||||
| 1973 | # coverage: 101 | ||||||||||
| 1974 | sub numeric_binop { | ||||||||||
| 1975 | my ( $op, $operator, $flags ) = @_; | ||||||||||
| 1976 | my $force_int = 0; | ||||||||||
| 1977 | $flags = 0 unless $flags; | ||||||||||
| 1978 | $force_int ||= ( $flags & INT_RESULT ); | ||||||||||
| 1979 | $force_int ||= | ||||||||||
| 1980 | ( $flags & INTS_CLOSED | ||||||||||
| 1981 | && @stack >= 2 | ||||||||||
| 1982 | && valid_int( $stack[-2] ) | ||||||||||
| 1983 | && valid_int( $stack[-1] ) ); | ||||||||||
| 1984 | if ( $op->flags & OPf_STACKED ) { | ||||||||||
| 1985 | runtime(sprintf("/* %s */", $op->name)) if $verbose; | ||||||||||
| 1986 | my $right = pop_numeric(); | ||||||||||
| 1987 | if ( @stack >= 1 ) { | ||||||||||
| 1988 | my $left = top_numeric(); | ||||||||||
| 1989 | if ($force_int) { | ||||||||||
| 1990 | $stack[-1]->set_int( &$operator( $left, $right ) ); | ||||||||||
| 1991 | } | ||||||||||
| 1992 | else { | ||||||||||
| 1993 | $stack[-1]->set_numeric( &$operator( $left, $right ) ); | ||||||||||
| 1994 | } | ||||||||||
| 1995 | } | ||||||||||
| 1996 | else { | ||||||||||
| 1997 | if ($force_int) { | ||||||||||
| 1998 | my $rightruntime = B::Pseudoreg->new( "IV", "riv" ); | ||||||||||
| 1999 | runtime( sprintf( "$$rightruntime = %s;", $right ) ); | ||||||||||
| 2000 | runtime( | ||||||||||
| 2001 | sprintf( | ||||||||||
| 2002 | "sv_setiv(TOPs, %s);", &$operator( "TOPi", $$rightruntime ) | ||||||||||
| 2003 | ) | ||||||||||
| 2004 | ); | ||||||||||
| 2005 | } | ||||||||||
| 2006 | else { | ||||||||||
| 2007 | my $rightruntime = B::Pseudoreg->new( "NV", "rnv" ); | ||||||||||
| 2008 | runtime( sprintf( "$$rightruntime = %s;\t/* %s */", $right, $op->name ) ); | ||||||||||
| 2009 | runtime( | ||||||||||
| 2010 | sprintf( | ||||||||||
| 2011 | "sv_setnv(TOPs, %s);", &$operator( "TOPn", $$rightruntime ) | ||||||||||
| 2012 | ) | ||||||||||
| 2013 | ); | ||||||||||
| 2014 | } | ||||||||||
| 2015 | } | ||||||||||
| 2016 | } | ||||||||||
| 2017 | else { | ||||||||||
| 2018 | my $targ = $pad[ $op->targ ]; | ||||||||||
| 2019 | $force_int ||= ( $targ->{type} == T_INT ); | ||||||||||
| 2020 | if ($force_int) { | ||||||||||
| 2021 | my $right = B::Pseudoreg->new( "IV", "riv" ); | ||||||||||
| 2022 | my $left = B::Pseudoreg->new( "IV", "liv" ); | ||||||||||
| 2023 | runtime( | ||||||||||
| 2024 | sprintf( "$$right = %s;", pop_numeric()), | ||||||||||
| 2025 | sprintf( "$$left = %s;\t/* %s */", pop_numeric(), pop_numeric(), $op->name ) ); | ||||||||||
| 2026 | $targ->set_int( &$operator( $$left, $$right ) ); | ||||||||||
| 2027 | } | ||||||||||
| 2028 | else { | ||||||||||
| 2029 | my $right = B::Pseudoreg->new( "NV", "rnv" ); | ||||||||||
| 2030 | my $left = B::Pseudoreg->new( "NV", "lnv" ); | ||||||||||
| 2031 | runtime( | ||||||||||
| 2032 | sprintf( "$$right = %s;", pop_numeric()), | ||||||||||
| 2033 | sprintf( "$$left = %s;\t/* %s */", pop_numeric(), $op->name ) ); | ||||||||||
| 2034 | $targ->set_numeric( &$operator( $$left, $$right ) ); | ||||||||||
| 2035 | } | ||||||||||
| 2036 | push( @stack, $targ ); | ||||||||||
| 2037 | } | ||||||||||
| 2038 | return $op->next; | ||||||||||
| 2039 | } | ||||||||||
| 2040 | |||||||||||
| 2041 | sub numeric_unop { | ||||||||||
| 2042 | my ( $op, $operator, $flags ) = @_; | ||||||||||
| 2043 | my $force_int = 0; | ||||||||||
| 2044 | $force_int ||= ( $flags & INT_RESULT ); | ||||||||||
| 2045 | $force_int ||= | ||||||||||
| 2046 | ( $flags & INTS_CLOSED | ||||||||||
| 2047 | && @stack >= 1 | ||||||||||
| 2048 | && valid_int( $stack[-1] ) ); | ||||||||||
| 2049 | my $targ = $pad[ $op->targ ]; | ||||||||||
| 2050 | $force_int ||= ( $targ->{type} == T_INT ); | ||||||||||
| 2051 | if ($force_int) { | ||||||||||
| 2052 | my $arg = B::Pseudoreg->new( "IV", "liv" ); | ||||||||||
| 2053 | runtime(sprintf( "$$arg = %s;\t/* %s */", | ||||||||||
| 2054 | pop_numeric, $op->name ) ); | ||||||||||
| 2055 | # XXX set targ? | ||||||||||
| 2056 | $targ->set_int( &$operator( $$arg ) ); | ||||||||||
| 2057 | } | ||||||||||
| 2058 | else { | ||||||||||
| 2059 | my $arg = B::Pseudoreg->new( "NV", "lnv" ); | ||||||||||
| 2060 | runtime(sprintf( "$$arg = %s;\t/* %s */", | ||||||||||
| 2061 | pop_numeric, $op->name ) ); | ||||||||||
| 2062 | # XXX set targ? | ||||||||||
| 2063 | $targ->set_numeric( &$operator( $$arg ) ); | ||||||||||
| 2064 | } | ||||||||||
| 2065 | push( @stack, $targ ); | ||||||||||
| 2066 | return $op->next; | ||||||||||
| 2067 | } | ||||||||||
| 2068 | |||||||||||
| 2069 | # coverage: 18 | ||||||||||
| 2070 | sub pp_ncmp { | ||||||||||
| 2071 | my ($op) = @_; | ||||||||||
| 2072 | if ( $op->flags & OPf_STACKED ) { | ||||||||||
| 2073 | my $right = pop_numeric(); | ||||||||||
| 2074 | if ( @stack >= 1 ) { | ||||||||||
| 2075 | my $left = top_numeric(); | ||||||||||
| 2076 | runtime sprintf( "if (%s > %s){\t/* %s */", $left, $right, $op->name ); | ||||||||||
| 2077 | $stack[-1]->set_int(1); | ||||||||||
| 2078 | $stack[-1]->write_back(); | ||||||||||
| 2079 | runtime sprintf( "}else if (%s < %s ) {", $left, $right ); | ||||||||||
| 2080 | $stack[-1]->set_int(-1); | ||||||||||
| 2081 | $stack[-1]->write_back(); | ||||||||||
| 2082 | runtime sprintf( "}else if (%s == %s) {", $left, $right ); | ||||||||||
| 2083 | $stack[-1]->set_int(0); | ||||||||||
| 2084 | $stack[-1]->write_back(); | ||||||||||
| 2085 | runtime sprintf("}else {"); | ||||||||||
| 2086 | $stack[-1]->set_sv("&PL_sv_undef"); | ||||||||||
| 2087 | runtime "}"; | ||||||||||
| 2088 | } | ||||||||||
| 2089 | else { | ||||||||||
| 2090 | my $rightruntime = B::Pseudoreg->new( "NV", "rnv" ); | ||||||||||
| 2091 | runtime( sprintf( "$$rightruntime = %s;\t/* %s */", $right, $op->name ) ); | ||||||||||
| 2092 | runtime sprintf( qq/if ("TOPn" > %s){/, $rightruntime ); | ||||||||||
| 2093 | runtime sprintf(" sv_setiv(TOPs,1);"); | ||||||||||
| 2094 | runtime sprintf( qq/}else if ( "TOPn" < %s ) {/, $$rightruntime ); | ||||||||||
| 2095 | runtime sprintf(" sv_setiv(TOPs,-1);"); | ||||||||||
| 2096 | runtime sprintf( qq/} else if ("TOPn" == %s) {/, $$rightruntime ); | ||||||||||
| 2097 | runtime sprintf(" sv_setiv(TOPs,0);"); | ||||||||||
| 2098 | runtime sprintf(qq/}else {/); | ||||||||||
| 2099 | runtime sprintf(" sv_setiv(TOPs,&PL_sv_undef;"); | ||||||||||
| 2100 | runtime "}"; | ||||||||||
| 2101 | } | ||||||||||
| 2102 | } | ||||||||||
| 2103 | else { | ||||||||||
| 2104 | my $targ = $pad[ $op->targ ]; | ||||||||||
| 2105 | my $right = B::Pseudoreg->new( "NV", "rnv" ); | ||||||||||
| 2106 | my $left = B::Pseudoreg->new( "NV", "lnv" ); | ||||||||||
| 2107 | runtime( | ||||||||||
| 2108 | sprintf( "$$right = %s; $$left = %s;\t/* %s */", | ||||||||||
| 2109 | pop_numeric(), pop_numeric, $op->name ) ); | ||||||||||
| 2110 | runtime sprintf( "if (%s > %s){ /*targ*/", $$left, $$right ); | ||||||||||
| 2111 | $targ->set_int(1); | ||||||||||
| 2112 | $targ->write_back(); | ||||||||||
| 2113 | runtime sprintf( "}else if (%s < %s ) {", $$left, $$right ); | ||||||||||
| 2114 | $targ->set_int(-1); | ||||||||||
| 2115 | $targ->write_back(); | ||||||||||
| 2116 | runtime sprintf( "}else if (%s == %s) {", $$left, $$right ); | ||||||||||
| 2117 | $targ->set_int(0); | ||||||||||
| 2118 | $targ->write_back(); | ||||||||||
| 2119 | runtime sprintf("}else {"); | ||||||||||
| 2120 | $targ->set_sv("&PL_sv_undef"); | ||||||||||
| 2121 | runtime "}"; | ||||||||||
| 2122 | push( @stack, $targ ); | ||||||||||
| 2123 | } | ||||||||||
| 2124 | #runtime "return NULL;"; | ||||||||||
| 2125 | return $op->next; | ||||||||||
| 2126 | } | ||||||||||
| 2127 | |||||||||||
| 2128 | # coverage: ? | ||||||||||
| 2129 | sub sv_binop { | ||||||||||
| 2130 | my ( $op, $operator, $flags ) = @_; | ||||||||||
| 2131 | if ( $op->flags & OPf_STACKED ) { | ||||||||||
| 2132 | my $right = pop_sv(); | ||||||||||
| 2133 | if ( @stack >= 1 ) { | ||||||||||
| 2134 | my $left = top_sv(); | ||||||||||
| 2135 | if ( $flags & INT_RESULT ) { | ||||||||||
| 2136 | $stack[-1]->set_int( &$operator( $left, $right ) ); | ||||||||||
| 2137 | } | ||||||||||
| 2138 | elsif ( $flags & NUMERIC_RESULT ) { | ||||||||||
| 2139 | $stack[-1]->set_numeric( &$operator( $left, $right ) ); | ||||||||||
| 2140 | } | ||||||||||
| 2141 | else { | ||||||||||
| 2142 | # XXX Does this work? | ||||||||||
| 2143 | runtime( | ||||||||||
| 2144 | sprintf( "sv_setsv($left, %s);\t/* %s */", | ||||||||||
| 2145 | &$operator( $left, $right ), $op->name ) ); | ||||||||||
| 2146 | $stack[-1]->invalidate; | ||||||||||
| 2147 | } | ||||||||||
| 2148 | } | ||||||||||
| 2149 | else { | ||||||||||
| 2150 | my $f; | ||||||||||
| 2151 | if ( $flags & INT_RESULT ) { | ||||||||||
| 2152 | $f = "sv_setiv"; | ||||||||||
| 2153 | } | ||||||||||
| 2154 | elsif ( $flags & NUMERIC_RESULT ) { | ||||||||||
| 2155 | $f = "sv_setnv"; | ||||||||||
| 2156 | } | ||||||||||
| 2157 | else { | ||||||||||
| 2158 | $f = "sv_setsv"; | ||||||||||
| 2159 | } | ||||||||||
| 2160 | runtime( sprintf( "%s(TOPs, %s);\t/* %s */", | ||||||||||
| 2161 | $f, &$operator( "TOPs", $right ), $op->name ) ); | ||||||||||
| 2162 | } | ||||||||||
| 2163 | } | ||||||||||
| 2164 | else { | ||||||||||
| 2165 | my $targ = $pad[ $op->targ ]; | ||||||||||
| 2166 | runtime( sprintf( "right = %s; left = %s;\t/* %s */", | ||||||||||
| 2167 | pop_sv(), pop_sv, $op->name ) ); | ||||||||||
| 2168 | if ( $flags & INT_RESULT ) { | ||||||||||
| 2169 | $targ->set_int( &$operator( "left", "right" ) ); | ||||||||||
| 2170 | } | ||||||||||
| 2171 | elsif ( $flags & NUMERIC_RESULT ) { | ||||||||||
| 2172 | $targ->set_numeric( &$operator( "left", "right" ) ); | ||||||||||
| 2173 | } | ||||||||||
| 2174 | else { | ||||||||||
| 2175 | # XXX Does this work? | ||||||||||
| 2176 | runtime(sprintf("sv_setsv(%s, %s);", | ||||||||||
| 2177 | $targ->as_sv, &$operator( "left", "right" ) )); | ||||||||||
| 2178 | $targ->invalidate; | ||||||||||
| 2179 | } | ||||||||||
| 2180 | push( @stack, $targ ); | ||||||||||
| 2181 | } | ||||||||||
| 2182 | return $op->next; | ||||||||||
| 2183 | } | ||||||||||
| 2184 | |||||||||||
| 2185 | # coverage: ? | ||||||||||
| 2186 | sub bool_int_binop { | ||||||||||
| 2187 | my ( $op, $operator ) = @_; | ||||||||||
| 2188 | my $right = B::Pseudoreg->new( "IV", "riv" ); | ||||||||||
| 2189 | my $left = B::Pseudoreg->new( "IV", "liv" ); | ||||||||||
| 2190 | runtime( sprintf( "$$right = %s; $$left = %s;\t/* %s */", | ||||||||||
| 2191 | pop_int(), pop_int(), $op->name ) ); | ||||||||||
| 2192 | my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) ); | ||||||||||
| 2193 | $bool->set_int( &$operator( $$left, $$right ) ); | ||||||||||
| 2194 | push( @stack, $bool ); | ||||||||||
| 2195 | return $op->next; | ||||||||||
| 2196 | } | ||||||||||
| 2197 | |||||||||||
| 2198 | # coverage: ? | ||||||||||
| 2199 | sub bool_numeric_binop { | ||||||||||
| 2200 | my ( $op, $operator ) = @_; | ||||||||||
| 2201 | my $right = B::Pseudoreg->new( "NV", "rnv" ); | ||||||||||
| 2202 | my $left = B::Pseudoreg->new( "NV", "lnv" ); | ||||||||||
| 2203 | runtime( | ||||||||||
| 2204 | sprintf( "$$right = %s; $$left = %s;\t/* %s */", | ||||||||||
| 2205 | pop_numeric(), pop_numeric(), $op->name ) ); | ||||||||||
| 2206 | my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) ); | ||||||||||
| 2207 | $bool->set_numeric( &$operator( $$left, $$right ) ); | ||||||||||
| 2208 | push( @stack, $bool ); | ||||||||||
| 2209 | return $op->next; | ||||||||||
| 2210 | } | ||||||||||
| 2211 | |||||||||||
| 2212 | # coverage: ? | ||||||||||
| 2213 | sub bool_sv_binop { | ||||||||||
| 2214 | my ( $op, $operator ) = @_; | ||||||||||
| 2215 | runtime( sprintf( "right = %s; left = %s;\t/* %s */", | ||||||||||
| 2216 | pop_sv(), pop_sv(), $op->name ) ); | ||||||||||
| 2217 | my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) ); | ||||||||||
| 2218 | $bool->set_numeric( &$operator( "left", "right" ) ); | ||||||||||
| 2219 | push( @stack, $bool ); | ||||||||||
| 2220 | return $op->next; | ||||||||||
| 2221 | } | ||||||||||
| 2222 | |||||||||||
| 2223 | # coverage: ? | ||||||||||
| 2224 | sub infix_op { | ||||||||||
| 2225 | my $opname = shift; | ||||||||||
| 2226 | return sub { "$_[0] $opname $_[1]" } | ||||||||||
| 2227 | } | ||||||||||
| 2228 | |||||||||||
| 2229 | # coverage: ? | ||||||||||
| 2230 | sub prefix_op { | ||||||||||
| 2231 | my $opname = shift; | ||||||||||
| 2232 | return sub { sprintf( "%s(%s)", $opname, join( ", ", @_ ) ) } | ||||||||||
| 2233 | } | ||||||||||
| 2234 | |||||||||||
| 2235 | BEGIN { | ||||||||||
| 2236 | my $plus_op = infix_op("+"); | ||||||||||
| 2237 | my $minus_op = infix_op("-"); | ||||||||||
| 2238 | my $multiply_op = infix_op("*"); | ||||||||||
| 2239 | my $divide_op = infix_op("/"); | ||||||||||
| 2240 | my $modulo_op = infix_op("%"); | ||||||||||
| 2241 | my $lshift_op = infix_op("<<"); | ||||||||||
| 2242 | my $rshift_op = infix_op(">>"); | ||||||||||
| 2243 | my $scmp_op = prefix_op("sv_cmp"); | ||||||||||
| 2244 | my $seq_op = prefix_op("sv_eq"); | ||||||||||
| 2245 | my $sne_op = prefix_op("!sv_eq"); | ||||||||||
| 2246 | my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" }; | ||||||||||
| 2247 | my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" }; | ||||||||||
| 2248 | my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" }; | ||||||||||
| 2249 | my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" }; | ||||||||||
| 2250 | my $eq_op = infix_op("=="); | ||||||||||
| 2251 | my $ne_op = infix_op("!="); | ||||||||||
| 2252 | my $lt_op = infix_op("<"); | ||||||||||
| 2253 | my $gt_op = infix_op(">"); | ||||||||||
| 2254 | my $le_op = infix_op("<="); | ||||||||||
| 2255 | my $ge_op = infix_op(">="); | ||||||||||
| 2256 | |||||||||||
| 2257 | # | ||||||||||
| 2258 | # XXX The standard perl PP code has extra handling for | ||||||||||
| 2259 | # some special case arguments of these operators. | ||||||||||
| 2260 | # | ||||||||||
| 2261 | sub pp_add { numeric_binop( $_[0], $plus_op ) } | ||||||||||
| 2262 | sub pp_subtract { numeric_binop( $_[0], $minus_op ) } | ||||||||||
| 2263 | sub pp_multiply { numeric_binop( $_[0], $multiply_op ) } | ||||||||||
| 2264 | sub pp_divide { numeric_binop( $_[0], $divide_op ) } | ||||||||||
| 2265 | |||||||||||
| 2266 | sub pp_modulo { int_binop( $_[0], $modulo_op ) } # differs from perl's | ||||||||||
| 2267 | # http://perldoc.perl.org/perlop.html#Shift-Operators: | ||||||||||
| 2268 | # If use integer is in force then signed C integers are used, | ||||||||||
| 2269 | # else unsigned C integers are used. | ||||||||||
| 2270 | sub pp_left_shift { int_binop( $_[0], $lshift_op, VALID_UNSIGNED ) } | ||||||||||
| 2271 | sub pp_right_shift { int_binop( $_[0], $rshift_op, VALID_UNSIGNED ) } | ||||||||||
| 2272 | sub pp_i_add { int_binop( $_[0], $plus_op ) } | ||||||||||
| 2273 | sub pp_i_subtract { int_binop( $_[0], $minus_op ) } | ||||||||||
| 2274 | sub pp_i_multiply { int_binop( $_[0], $multiply_op ) } | ||||||||||
| 2275 | sub pp_i_divide { int_binop( $_[0], $divide_op ) } | ||||||||||
| 2276 | sub pp_i_modulo { int_binop( $_[0], $modulo_op ) } | ||||||||||
| 2277 | |||||||||||
| 2278 | sub pp_eq { bool_numeric_binop( $_[0], $eq_op ) } | ||||||||||
| 2279 | sub pp_ne { bool_numeric_binop( $_[0], $ne_op ) } | ||||||||||
| 2280 | # coverage: 21 | ||||||||||
| 2281 | sub pp_lt { bool_numeric_binop( $_[0], $lt_op ) } | ||||||||||
| 2282 | # coverage: 28 | ||||||||||
| 2283 | sub pp_gt { bool_numeric_binop( $_[0], $gt_op ) } | ||||||||||
| 2284 | sub pp_le { bool_numeric_binop( $_[0], $le_op ) } | ||||||||||
| 2285 | sub pp_ge { bool_numeric_binop( $_[0], $ge_op ) } | ||||||||||
| 2286 | |||||||||||
| 2287 | sub pp_i_eq { bool_int_binop( $_[0], $eq_op ) } | ||||||||||
| 2288 | sub pp_i_ne { bool_int_binop( $_[0], $ne_op ) } | ||||||||||
| 2289 | sub pp_i_lt { bool_int_binop( $_[0], $lt_op ) } | ||||||||||
| 2290 | sub pp_i_gt { bool_int_binop( $_[0], $gt_op ) } | ||||||||||
| 2291 | sub pp_i_le { bool_int_binop( $_[0], $le_op ) } | ||||||||||
| 2292 | sub pp_i_ge { bool_int_binop( $_[0], $ge_op ) } | ||||||||||
| 2293 | |||||||||||
| 2294 | sub pp_scmp { sv_binop( $_[0], $scmp_op, INT_RESULT ) } | ||||||||||
| 2295 | sub pp_slt { bool_sv_binop( $_[0], $slt_op ) } | ||||||||||
| 2296 | sub pp_sgt { bool_sv_binop( $_[0], $sgt_op ) } | ||||||||||
| 2297 | sub pp_sle { bool_sv_binop( $_[0], $sle_op ) } | ||||||||||
| 2298 | sub pp_sge { bool_sv_binop( $_[0], $sge_op ) } | ||||||||||
| 2299 | sub pp_seq { bool_sv_binop( $_[0], $seq_op ) } | ||||||||||
| 2300 | sub pp_sne { bool_sv_binop( $_[0], $sne_op ) } | ||||||||||
| 2301 | |||||||||||
| 2302 | # sub pp_sin { numeric_unop( $_[0], prefix_op("Perl_sin"), NUMERIC_RESULT ) } | ||||||||||
| 2303 | # sub pp_cos { numeric_unop( $_[0], prefix_op("Perl_cos"), NUMERIC_RESULT ) } | ||||||||||
| 2304 | # sub pp_exp { numeric_unop( $_[0], prefix_op("Perl_exp"), NUMERIC_RESULT ) } | ||||||||||
| 2305 | # sub pp_abs { numeric_unop( $_[0], prefix_op("abs") ) } | ||||||||||
| 2306 | # sub pp_negate { numeric_unop( $_[0], sub { "- $_[0]" }; ) } | ||||||||||
| 2307 | |||||||||||
| 2308 | # pow has special perl logic | ||||||||||
| 2309 | ## sub pp_pow { numeric_binop( $_[0], prefix_op("Perl_pow"), NUMERIC_RESULT ) } | ||||||||||
| 2310 | #XXX log and sqrt need to check negative args | ||||||||||
| 2311 | # sub pp_sqrt { numeric_unop( $_[0], prefix_op("Perl_sqrt"), NUMERIC_RESULT ) } | ||||||||||
| 2312 | # sub pp_log { numeric_unop( $_[0], prefix_op("Perl_log"), NUMERIC_RESULT ) } | ||||||||||
| 2313 | # sub pp_atan2 { numeric_binop( $_[0], prefix_op("Perl_atan2"), NUMERIC_RESULT ) } | ||||||||||
| 2314 | |||||||||||
| 2315 | } | ||||||||||
| 2316 | |||||||||||
| 2317 | # coverage: 3,4,9,10,11,12,17,18,20,21,23 | ||||||||||
| 2318 | sub pp_sassign { | ||||||||||
| 2319 | my $op = shift; | ||||||||||
| 2320 | my $backwards = $op->private & OPpASSIGN_BACKWARDS; | ||||||||||
| 2321 | debug( sprintf( "sassign->private=0x%x\n", $op->private ) ) if $debug{op}; | ||||||||||
| 2322 | my ( $dst, $src ); | ||||||||||
| 2323 | runtime("/* pp_sassign */") if $verbose; | ||||||||||
| 2324 | if ( @stack >= 2 ) { | ||||||||||
| 2325 | $dst = pop @stack; | ||||||||||
| 2326 | $src = pop @stack; | ||||||||||
| 2327 | ( $src, $dst ) = ( $dst, $src ) if $backwards; | ||||||||||
| 2328 | my $type = $src->{type}; | ||||||||||
| 2329 | if ( $type == T_INT ) { | ||||||||||
| 2330 | $dst->set_int( $src->as_int, $src->{flags} & VALID_UNSIGNED ); | ||||||||||
| 2331 | } | ||||||||||
| 2332 | elsif ( $type == T_NUM ) { | ||||||||||
| 2333 | $dst->set_numeric( $src->as_numeric ); | ||||||||||
| 2334 | } | ||||||||||
| 2335 | else { | ||||||||||
| 2336 | $dst->set_sv( $src->as_sv ); | ||||||||||
| 2337 | } | ||||||||||
| 2338 | push( @stack, $dst ); | ||||||||||
| 2339 | } | ||||||||||
| 2340 | elsif ( @stack == 1 ) { | ||||||||||
| 2341 | if ($backwards) { | ||||||||||
| 2342 | my $src = pop @stack; | ||||||||||
| 2343 | my $type = $src->{type}; | ||||||||||
| 2344 | runtime("if (PL_tainting && PL_tainted) TAINT_NOT;") if $opt_taint; | ||||||||||
| 2345 | if ( $type == T_INT ) { | ||||||||||
| 2346 | if ( $src->{flags} & VALID_UNSIGNED ) { | ||||||||||
| 2347 | runtime sprintf( "sv_setuv(TOPs, %s);", $src->as_int ); | ||||||||||
| 2348 | } | ||||||||||
| 2349 | else { | ||||||||||
| 2350 | runtime sprintf( "sv_setiv(TOPs, %s);", $src->as_int ); | ||||||||||
| 2351 | } | ||||||||||
| 2352 | } | ||||||||||
| 2353 | elsif ( $type == T_NUM ) { | ||||||||||
| 2354 | runtime sprintf( "sv_setnv(TOPs, %s);", $src->as_double ); | ||||||||||
| 2355 | } | ||||||||||
| 2356 | else { | ||||||||||
| 2357 | runtime sprintf( "sv_setsv(TOPs, %s);", $src->as_sv ); | ||||||||||
| 2358 | } | ||||||||||
| 2359 | runtime("SvSETMAGIC(TOPs);") if $opt_magic; | ||||||||||
| 2360 | } | ||||||||||
| 2361 | else { | ||||||||||
| 2362 | my $dst = $stack[-1]; | ||||||||||
| 2363 | my $type = $dst->{type}; | ||||||||||
| 2364 | runtime("sv = POPs;"); | ||||||||||
| 2365 | runtime("MAYBE_TAINT_SASSIGN_SRC(sv);") if $opt_taint; | ||||||||||
| 2366 | if ( $type == T_INT ) { | ||||||||||
| 2367 | $dst->set_int("SvIV(sv)"); | ||||||||||
| 2368 | } | ||||||||||
| 2369 | elsif ( $type == T_NUM ) { | ||||||||||
| 2370 | $dst->set_double("SvNV(sv)"); | ||||||||||
| 2371 | } | ||||||||||
| 2372 | else { | ||||||||||
| 2373 | $opt_magic | ||||||||||
| 2374 | ? runtime("SvSetMagicSV($dst->{sv}, sv);") | ||||||||||
| 2375 | : runtime("SvSetSV($dst->{sv}, sv);"); | ||||||||||
| 2376 | $dst->invalidate; | ||||||||||
| 2377 | } | ||||||||||
| 2378 | } | ||||||||||
| 2379 | } | ||||||||||
| 2380 | else { | ||||||||||
| 2381 | # empty perl stack, both at run-time | ||||||||||
| 2382 | if ($backwards) { | ||||||||||
| 2383 | runtime("src = POPs; dst = TOPs;"); | ||||||||||
| 2384 | } | ||||||||||
| 2385 | else { | ||||||||||
| 2386 | runtime("dst = POPs; src = TOPs;"); | ||||||||||
| 2387 | } | ||||||||||
| 2388 | runtime( | ||||||||||
| 2389 | $opt_taint ? "MAYBE_TAINT_SASSIGN_SRC(src);" : "", | ||||||||||
| 2390 | "SvSetSV(dst, src);", | ||||||||||
| 2391 | $opt_magic ? "SvSETMAGIC(dst);" : "", | ||||||||||
| 2392 | "SETs(dst);" | ||||||||||
| 2393 | ); | ||||||||||
| 2394 | } | ||||||||||
| 2395 | return $op->next; | ||||||||||
| 2396 | } | ||||||||||
| 2397 | |||||||||||
| 2398 | # coverage: ny | ||||||||||
| 2399 | sub pp_preinc { | ||||||||||
| 2400 | my $op = shift; | ||||||||||
| 2401 | if ( @stack >= 1 ) { | ||||||||||
| 2402 | my $obj = $stack[-1]; | ||||||||||
| 2403 | my $type = $obj->{type}; | ||||||||||
| 2404 | if ( $type == T_INT || $type == T_NUM ) { | ||||||||||
| 2405 | $obj->set_int( $obj->as_int . " + 1" ); | ||||||||||
| 2406 | } | ||||||||||
| 2407 | else { | ||||||||||
| 2408 | runtime sprintf( "PP_PREINC(%s);", $obj->as_sv ); | ||||||||||
| 2409 | $obj->invalidate(); | ||||||||||
| 2410 | } | ||||||||||
| 2411 | } | ||||||||||
| 2412 | else { | ||||||||||
| 2413 | runtime sprintf("PP_PREINC(TOPs);"); | ||||||||||
| 2414 | } | ||||||||||
| 2415 | return $op->next; | ||||||||||
| 2416 | } | ||||||||||
| 2417 | |||||||||||
| 2418 | # coverage: 1-32,35 | ||||||||||
| 2419 | sub pp_pushmark { | ||||||||||
| 2420 | my $op = shift; | ||||||||||
| 2421 | # runtime(sprintf("/* %s */", $op->name)) if $verbose; | ||||||||||
| 2422 | write_back_stack(); | ||||||||||
| 2423 | runtime("PUSHMARK(sp);"); | ||||||||||
| 2424 | return $op->next; | ||||||||||
| 2425 | } | ||||||||||
| 2426 | |||||||||||
| 2427 | # coverage: 28 | ||||||||||
| 2428 | sub pp_list { | ||||||||||
| 2429 | my $op = shift; | ||||||||||
| 2430 | runtime(sprintf("/* %s */", $op->name)) if $verbose; | ||||||||||
| 2431 | write_back_stack(); | ||||||||||
| 2432 | my $gimme = gimme($op); | ||||||||||
| 2433 | if ( not defined $gimme ) { | ||||||||||
| 2434 | runtime("PP_LIST(block_gimme());"); | ||||||||||
| 2435 | } elsif ( $gimme == G_ARRAY ) { # sic | ||||||||||
| 2436 | runtime("POPMARK;"); # need this even though not a "full" pp_list | ||||||||||
| 2437 | } | ||||||||||
| 2438 | else { | ||||||||||
| 2439 | runtime("PP_LIST($gimme);"); | ||||||||||
| 2440 | } | ||||||||||
| 2441 | return $op->next; | ||||||||||
| 2442 | } | ||||||||||
| 2443 | |||||||||||
| 2444 | # coverage: 6,8,9,10,24,26,27,31,35 | ||||||||||
| 2445 | sub pp_entersub { | ||||||||||
| 2446 | my $op = shift; | ||||||||||
| 2447 | runtime(sprintf("/* %s */", $op->name)) if $verbose; | ||||||||||
| 2448 | $curcop->write_back if $curcop; | ||||||||||
| 2449 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2450 | write_back_stack(); | ||||||||||
| 2451 | my $sym = doop($op); | ||||||||||
| 2452 | $op->next->save if ${$op->next}; | ||||||||||
| 2453 | $op->first->save if ${$op->first} and $op->first->type; | ||||||||||
| 2454 | # sometimes needs an additional check | ||||||||||
| 2455 | my $ck_next = ${$op->next} ? "PL_op != ($sym)->op_next && " : ""; | ||||||||||
| 2456 | runtime("while ($ck_next PL_op != (OP*)0 ){", | ||||||||||
| 2457 | "\tPL_op = (*PL_op->op_ppaddr)(aTHX);", | ||||||||||
| 2458 | "\tSPAGAIN;}"); | ||||||||||
| 2459 | $know_op = 0; | ||||||||||
| 2460 | invalidate_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2461 | # B::C::check_entersub($op); | ||||||||||
| 2462 | return $op->next; | ||||||||||
| 2463 | } | ||||||||||
| 2464 | |||||||||||
| 2465 | # coverage: 16,26,35,51,72,73 | ||||||||||
| 2466 | sub pp_bless { | ||||||||||
| 2467 | my $op = shift; | ||||||||||
| 2468 | $curcop->write_back if $curcop; | ||||||||||
| 2469 | # B::C::check_bless($op); | ||||||||||
| 2470 | default_pp($op); | ||||||||||
| 2471 | } | ||||||||||
| 2472 | |||||||||||
| 2473 | |||||||||||
| 2474 | # coverage: ny | ||||||||||
| 2475 | sub pp_formline { | ||||||||||
| 2476 | my $op = shift; | ||||||||||
| 2477 | my $ppname = "pp_" . $op->name; | ||||||||||
| 2478 | runtime(sprintf("/* %s */", $ppname)) if $verbose; | ||||||||||
| 2479 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 2480 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 2481 | my $sym = doop($op); | ||||||||||
| 2482 | |||||||||||
| 2483 | # See comment in pp_grepwhile to see why! | ||||||||||
| 2484 | $init->add("((LISTOP*)$sym)->op_first = $sym;"); | ||||||||||
| 2485 | runtime("if (PL_op == ((LISTOP*)($sym))->op_first) {"); | ||||||||||
| 2486 | save_or_restore_lexical_state( ${ $op->first } ); | ||||||||||
| 2487 | runtime( sprintf( "goto %s;", label( $op->first ) ), | ||||||||||
| 2488 | "}"); | ||||||||||
| 2489 | return $op->next; | ||||||||||
| 2490 | } | ||||||||||
| 2491 | |||||||||||
| 2492 | # coverage: 2,17,21,28,30 | ||||||||||
| 2493 | sub pp_goto { | ||||||||||
| 2494 | my $op = shift; | ||||||||||
| 2495 | my $ppname = "pp_" . $op->name; | ||||||||||
| 2496 | runtime(sprintf("/* %s */", $ppname)) if $verbose; | ||||||||||
| 2497 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 2498 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 2499 | my $sym = doop($op); | ||||||||||
| 2500 | runtime("if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;}"); | ||||||||||
| 2501 | invalidate_lexicals() unless $skip_invalidate{$ppname}; | ||||||||||
| 2502 | return $op->next; | ||||||||||
| 2503 | } | ||||||||||
| 2504 | |||||||||||
| 2505 | # coverage: 1-39, c_argv.t 2 | ||||||||||
| 2506 | sub pp_enter { | ||||||||||
| 2507 | # XXX fails with simple c_argv.t 2. no cxix. Disabled for now | ||||||||||
| 2508 | if (0 and $inline_ops) { | ||||||||||
| 2509 | my $op = shift; | ||||||||||
| 2510 | runtime(sprintf("/* %s */", $op->name)) if $verbose; | ||||||||||
| 2511 | warn "inlining enter\n" if $debug{op}; | ||||||||||
| 2512 | $curcop->write_back if $curcop; | ||||||||||
| 2513 | if (!($op->flags & OPf_WANT)) { | ||||||||||
| 2514 | my $cxix = $#cxstack; | ||||||||||
| 2515 | if ( $cxix >= 0 ) { | ||||||||||
| 2516 | if ( $op->flags & OPf_SPECIAL ) { | ||||||||||
| 2517 | runtime "gimme = block_gimme();"; | ||||||||||
| 2518 | } else { | ||||||||||
| 2519 | runtime "gimme = cxstack[cxstack_ix].blk_gimme;"; | ||||||||||
| 2520 | } | ||||||||||
| 2521 | } else { | ||||||||||
| 2522 | runtime "gimme = G_SCALAR;"; | ||||||||||
| 2523 | } | ||||||||||
| 2524 | } else { | ||||||||||
| 2525 | runtime "gimme = OP_GIMME(PL_op, -1);"; | ||||||||||
| 2526 | } | ||||||||||
| 2527 | runtime($] >= 5.011001 and $] < 5.011004 | ||||||||||
| 2528 | ? 'ENTER_with_name("block");' : 'ENTER;', | ||||||||||
| 2529 | "SAVETMPS;", | ||||||||||
| 2530 | "PUSHBLOCK(cx, CXt_BLOCK, SP);"); | ||||||||||
| 2531 | return $op->next; | ||||||||||
| 2532 | } else { | ||||||||||
| 2533 | return default_pp(@_); | ||||||||||
| 2534 | } | ||||||||||
| 2535 | } | ||||||||||
| 2536 | |||||||||||
| 2537 | # coverage: ny | ||||||||||
| 2538 | sub pp_enterwrite { pp_entersub(@_) } | ||||||||||
| 2539 | |||||||||||
| 2540 | # coverage: 6,8,9,10,24,26,27,31 | ||||||||||
| 2541 | sub pp_leavesub { | ||||||||||
| 2542 | my $op = shift; | ||||||||||
| 2543 | my $ppname = "pp_" . $op->name; | ||||||||||
| 2544 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 2545 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 2546 | runtime("if (PL_curstackinfo->si_type == PERLSI_SORT){", | ||||||||||
| 2547 | "\tPUTBACK;return 0;", | ||||||||||
| 2548 | "}"); | ||||||||||
| 2549 | doop($op); | ||||||||||
| 2550 | return $op->next; | ||||||||||
| 2551 | } | ||||||||||
| 2552 | |||||||||||
| 2553 | # coverage: ny | ||||||||||
| 2554 | sub pp_leavewrite { | ||||||||||
| 2555 | my $op = shift; | ||||||||||
| 2556 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2557 | write_back_stack(); | ||||||||||
| 2558 | my $sym = doop($op); | ||||||||||
| 2559 | |||||||||||
| 2560 | # XXX Is this the right way to distinguish between it returning | ||||||||||
| 2561 | # CvSTART(cv) (via doform) and pop_return()? | ||||||||||
| 2562 | #runtime("if (PL_op) PL_op = (*PL_op->op_ppaddr)(aTHX);"); | ||||||||||
| 2563 | runtime("SPAGAIN;"); | ||||||||||
| 2564 | $know_op = 0; | ||||||||||
| 2565 | invalidate_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2566 | return $op->next; | ||||||||||
| 2567 | } | ||||||||||
| 2568 | |||||||||||
| 2569 | # coverage: ny | ||||||||||
| 2570 | sub pp_entergiven { pp_enterwrite(@_) } | ||||||||||
| 2571 | # coverage: ny | ||||||||||
| 2572 | sub pp_leavegiven { pp_leavewrite(@_) } | ||||||||||
| 2573 | |||||||||||
| 2574 | sub doeval { | ||||||||||
| 2575 | my $op = shift; | ||||||||||
| 2576 | $curcop->write_back; | ||||||||||
| 2577 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2578 | write_back_stack(); | ||||||||||
| 2579 | my $sym = loadop($op); | ||||||||||
| 2580 | my $ppaddr = $op->ppaddr; | ||||||||||
| 2581 | runtime("PP_EVAL($ppaddr, ($sym)->op_next);"); | ||||||||||
| 2582 | $know_op = 1; | ||||||||||
| 2583 | invalidate_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2584 | return $op->next; | ||||||||||
| 2585 | } | ||||||||||
| 2586 | |||||||||||
| 2587 | # coverage: 12 | ||||||||||
| 2588 | sub pp_entereval { doeval(@_) } | ||||||||||
| 2589 | # coverage: ny | ||||||||||
| 2590 | sub pp_dofile { doeval(@_) } | ||||||||||
| 2591 | |||||||||||
| 2592 | # coverage: 28 | ||||||||||
| 2593 | #pp_require is protected by pp_entertry, so no protection for it. | ||||||||||
| 2594 | sub pp_require { | ||||||||||
| 2595 | my $op = shift; | ||||||||||
| 2596 | $curcop->write_back; | ||||||||||
| 2597 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2598 | write_back_stack(); | ||||||||||
| 2599 | my $sym = doop($op); | ||||||||||
| 2600 | # sometimes needs an additional check | ||||||||||
| 2601 | my $ck_next = ${$op->next} ? "PL_op != ($sym)->op_next && " : ""; | ||||||||||
| 2602 | runtime("while ($ck_next PL_op != (OP*)0 ) {", #(test 28). | ||||||||||
| 2603 | " PL_op = (*PL_op->op_ppaddr)(aTHX);", | ||||||||||
| 2604 | " SPAGAIN;", | ||||||||||
| 2605 | "}"); | ||||||||||
| 2606 | $know_op = 1; | ||||||||||
| 2607 | invalidate_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2608 | # B::C::check_require($op); # mark package | ||||||||||
| 2609 | return $op->next; | ||||||||||
| 2610 | } | ||||||||||
| 2611 | |||||||||||
| 2612 | # coverage: 32 | ||||||||||
| 2613 | sub pp_entertry { | ||||||||||
| 2614 | my $op = shift; | ||||||||||
| 2615 | $curcop->write_back; | ||||||||||
| 2616 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2617 | write_back_stack(); | ||||||||||
| 2618 | my $sym = doop($op); | ||||||||||
| 2619 | $entertry_defined = 1; | ||||||||||
| 2620 | my $next = $op->next; # broken in 5.12, fixed in B::C by upgrading BASEOP | ||||||||||
| 2621 | # jump past leavetry | ||||||||||
| 2622 | $next = $op->other->next if $op->can("other"); # before 5.11.4 and after 5.13.8 | ||||||||||
| 2623 | my $l = label( $next ); | ||||||||||
| 2624 | debug "ENTERTRY label=$l (".ref($op).") ->".$next->name."(".ref($next).")\n"; | ||||||||||
| 2625 | runtime(sprintf( "PP_ENTERTRY(%s);", $l)); | ||||||||||
| 2626 | if ($next->isa('B::COP')) { | ||||||||||
| 2627 | push_label($next, 'nextstate'); | ||||||||||
| 2628 | } else { | ||||||||||
| 2629 | push_label($op->other, 'leavetry') if $op->can("other"); | ||||||||||
| 2630 | } | ||||||||||
| 2631 | invalidate_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2632 | return $op->next; | ||||||||||
| 2633 | } | ||||||||||
| 2634 | |||||||||||
| 2635 | # coverage: 32 | ||||||||||
| 2636 | sub pp_leavetry { | ||||||||||
| 2637 | my $op = shift; | ||||||||||
| 2638 | pop_label 'leavetry' if $labels->{'leavetry'}->[-1] and $labels->{'leavetry'}->[-1] == $op; | ||||||||||
| 2639 | default_pp($op); | ||||||||||
| 2640 | runtime("PP_LEAVETRY;"); | ||||||||||
| 2641 | write_label($op->next); | ||||||||||
| 2642 | return $op->next; | ||||||||||
| 2643 | } | ||||||||||
| 2644 | |||||||||||
| 2645 | # coverage: ny | ||||||||||
| 2646 | sub pp_grepstart { | ||||||||||
| 2647 | my $op = shift; | ||||||||||
| 2648 | if ( $need_freetmps && $freetmps_each_loop ) { | ||||||||||
| 2649 | runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up | ||||||||||
| 2650 | $need_freetmps = 0; | ||||||||||
| 2651 | } | ||||||||||
| 2652 | write_back_stack(); | ||||||||||
| 2653 | my $sym = doop($op); | ||||||||||
| 2654 | my $next = $op->next; | ||||||||||
| 2655 | $next->save; | ||||||||||
| 2656 | my $nexttonext = $next->next; | ||||||||||
| 2657 | $nexttonext->save; | ||||||||||
| 2658 | save_or_restore_lexical_state($$nexttonext); | ||||||||||
| 2659 | runtime( | ||||||||||
| 2660 | sprintf( "if (PL_op == (($sym)->op_next)->op_next) goto %s;", | ||||||||||
| 2661 | label($nexttonext) ) | ||||||||||
| 2662 | ); | ||||||||||
| 2663 | return $op->next->other; | ||||||||||
| 2664 | } | ||||||||||
| 2665 | |||||||||||
| 2666 | # coverage: ny | ||||||||||
| 2667 | sub pp_mapstart { | ||||||||||
| 2668 | my $op = shift; | ||||||||||
| 2669 | if ( $need_freetmps && $freetmps_each_loop ) { | ||||||||||
| 2670 | runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up | ||||||||||
| 2671 | $need_freetmps = 0; | ||||||||||
| 2672 | } | ||||||||||
| 2673 | write_back_stack(); | ||||||||||
| 2674 | |||||||||||
| 2675 | # pp_mapstart can return either op_next->op_next or op_next->op_other and | ||||||||||
| 2676 | # we need to be able to distinguish the two at runtime. | ||||||||||
| 2677 | my $sym = doop($op); | ||||||||||
| 2678 | my $next = $op->next; | ||||||||||
| 2679 | $next->save; | ||||||||||
| 2680 | my $nexttonext = $next->next; | ||||||||||
| 2681 | $nexttonext->save; | ||||||||||
| 2682 | save_or_restore_lexical_state($$nexttonext); | ||||||||||
| 2683 | runtime( | ||||||||||
| 2684 | sprintf( "if (PL_op == (($sym)->op_next)->op_next) goto %s;", | ||||||||||
| 2685 | label($nexttonext) ) | ||||||||||
| 2686 | ); | ||||||||||
| 2687 | return $op->next->other; | ||||||||||
| 2688 | } | ||||||||||
| 2689 | |||||||||||
| 2690 | # coverage: ny | ||||||||||
| 2691 | sub pp_grepwhile { | ||||||||||
| 2692 | my $op = shift; | ||||||||||
| 2693 | my $next = $op->next; | ||||||||||
| 2694 | unshift( @bblock_todo, $next ); | ||||||||||
| 2695 | write_back_lexicals(); | ||||||||||
| 2696 | write_back_stack(); | ||||||||||
| 2697 | my $sym = doop($op); | ||||||||||
| 2698 | |||||||||||
| 2699 | # pp_grepwhile can return either op_next or op_other and we need to | ||||||||||
| 2700 | # be able to distinguish the two at runtime. Since it's possible for | ||||||||||
| 2701 | # both ops to be "inlined", the fields could both be zero. To get | ||||||||||
| 2702 | # around that, we hack op_next to be our own op (purely because we | ||||||||||
| 2703 | # know it's a non-NULL pointer and can't be the same as op_other). | ||||||||||
| 2704 | $init->add("((LOGOP*)$sym)->op_next = $sym;"); | ||||||||||
| 2705 | save_or_restore_lexical_state($$next); | ||||||||||
| 2706 | runtime( sprintf( "if (PL_op == ($sym)->op_next) goto %s;", label($next) ) ); | ||||||||||
| 2707 | $know_op = 0; | ||||||||||
| 2708 | return $op->other; | ||||||||||
| 2709 | } | ||||||||||
| 2710 | |||||||||||
| 2711 | # coverage: ny | ||||||||||
| 2712 | sub pp_mapwhile { pp_grepwhile(@_) } | ||||||||||
| 2713 | |||||||||||
| 2714 | # coverage: 24 | ||||||||||
| 2715 | sub pp_return { | ||||||||||
| 2716 | my $op = shift; | ||||||||||
| 2717 | write_back_lexicals( REGISTER | TEMPORARY ); | ||||||||||
| 2718 | write_back_stack(); | ||||||||||
| 2719 | doop($op); | ||||||||||
| 2720 | runtime( "PUTBACK;", "return PL_op;" ); | ||||||||||
| 2721 | $know_op = 0; | ||||||||||
| 2722 | return $op->next; | ||||||||||
| 2723 | } | ||||||||||
| 2724 | |||||||||||
| 2725 | sub nyi { | ||||||||||
| 2726 | my $op = shift; | ||||||||||
| 2727 | warn sprintf( "Warning: %s not yet implemented properly\n", $op->ppaddr ); | ||||||||||
| 2728 | return default_pp($op); | ||||||||||
| 2729 | } | ||||||||||
| 2730 | |||||||||||
| 2731 | # coverage: 17 | ||||||||||
| 2732 | sub pp_range { | ||||||||||
| 2733 | my $op = shift; | ||||||||||
| 2734 | my $flags = $op->flags; | ||||||||||
| 2735 | if ( !( $flags & OPf_WANT ) ) { | ||||||||||
| 2736 | if ($strict) { | ||||||||||
| 2737 | error("context of range unknown at compile-time\n"); | ||||||||||
| 2738 | } else { | ||||||||||
| 2739 | warn("Warning: context of range unknown at compile-time\n"); | ||||||||||
| 2740 | runtime('warn("context of range unknown at compile-time");'); | ||||||||||
| 2741 | } | ||||||||||
| 2742 | return default_pp($op); | ||||||||||
| 2743 | } | ||||||||||
| 2744 | write_back_lexicals(); | ||||||||||
| 2745 | write_back_stack(); | ||||||||||
| 2746 | unless ( ( $flags & OPf_WANT ) == OPf_WANT_LIST ) { | ||||||||||
| 2747 | # We need to save our UNOP structure since pp_flop uses | ||||||||||
| 2748 | # it to find and adjust out targ. We don't need it ourselves. | ||||||||||
| 2749 | $op->save; | ||||||||||
| 2750 | save_or_restore_lexical_state( ${ $op->other } ); | ||||||||||
| 2751 | runtime sprintf( "if (SvTRUE(PL_curpad[%d])) goto %s;", | ||||||||||
| 2752 | $op->targ, label( $op->other ) ); | ||||||||||
| 2753 | unshift( @bblock_todo, $op->other ); | ||||||||||
| 2754 | } | ||||||||||
| 2755 | return $op->next; | ||||||||||
| 2756 | } | ||||||||||
| 2757 | |||||||||||
| 2758 | # coverage: 17, 30 | ||||||||||
| 2759 | sub pp_flip { | ||||||||||
| 2760 | my $op = shift; | ||||||||||
| 2761 | my $flags = $op->flags; | ||||||||||
| 2762 | if ( !( $flags & OPf_WANT ) ) { | ||||||||||
| 2763 | if ($strict) { | ||||||||||
| 2764 | error("context of flip unknown at compile-time\n"); | ||||||||||
| 2765 | } else { | ||||||||||
| 2766 | warn("Warning: context of flip unknown at compile-time\n"); | ||||||||||
| 2767 | runtime('warn("context of flip unknown at compile-time");'); | ||||||||||
| 2768 | } | ||||||||||
| 2769 | return default_pp($op); | ||||||||||
| 2770 | } | ||||||||||
| 2771 | if ( ( $flags & OPf_WANT ) == OPf_WANT_LIST ) { | ||||||||||
| 2772 | return $op->first->other; | ||||||||||
| 2773 | } | ||||||||||
| 2774 | write_back_lexicals(); | ||||||||||
| 2775 | write_back_stack(); | ||||||||||
| 2776 | # We need to save our UNOP structure since pp_flop uses | ||||||||||
| 2777 | # it to find and adjust out targ. We don't need it ourselves. | ||||||||||
| 2778 | $op->save; | ||||||||||
| 2779 | my $ix = $op->targ; | ||||||||||
| 2780 | my $rangeix = $op->first->targ; | ||||||||||
| 2781 | runtime( | ||||||||||
| 2782 | ( $op->private & OPpFLIP_LINENUM ) | ||||||||||
| 2783 | ? "if (PL_last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(PL_last_in_gv))) {" | ||||||||||
| 2784 | : "if (SvTRUE(TOPs)) {" | ||||||||||
| 2785 | ); | ||||||||||
| 2786 | runtime("\tsv_setiv(PL_curpad[$rangeix], 1);"); | ||||||||||
| 2787 | if ( $op->flags & OPf_SPECIAL ) { | ||||||||||
| 2788 | runtime("sv_setiv(PL_curpad[$ix], 1);"); | ||||||||||
| 2789 | } | ||||||||||
| 2790 | else { | ||||||||||
| 2791 | save_or_restore_lexical_state( ${ $op->first->other } ); | ||||||||||
| 2792 | runtime( "\tsv_setiv(PL_curpad[$ix], 0);", | ||||||||||
| 2793 | "\tsp--;", sprintf( "\tgoto %s;", label( $op->first->other ) ) ); | ||||||||||
| 2794 | } | ||||||||||
| 2795 | runtime( "}", qq{sv_setpv(PL_curpad[$ix], "");}, "SETs(PL_curpad[$ix]);" ); | ||||||||||
| 2796 | $know_op = 0; | ||||||||||
| 2797 | return $op->next; | ||||||||||
| 2798 | } | ||||||||||
| 2799 | |||||||||||
| 2800 | # coverage: 17 | ||||||||||
| 2801 | sub pp_flop { | ||||||||||
| 2802 | my $op = shift; | ||||||||||
| 2803 | default_pp($op); | ||||||||||
| 2804 | $know_op = 0; | ||||||||||
| 2805 | return $op->next; | ||||||||||
| 2806 | } | ||||||||||
| 2807 | |||||||||||
| 2808 | sub enterloop { | ||||||||||
| 2809 | my $op = shift; | ||||||||||
| 2810 | my $nextop = $op->nextop; | ||||||||||
| 2811 | my $lastop = $op->lastop; | ||||||||||
| 2812 | my $redoop = $op->redoop; | ||||||||||
| 2813 | $curcop->write_back if $curcop; | ||||||||||
| 2814 | debug "enterloop: pushing on cxstack\n" if $debug{cxstack}; | ||||||||||
| 2815 | push( | ||||||||||
| 2816 | @cxstack, | ||||||||||
| 2817 | { | ||||||||||
| 2818 | type => $PERL512 ? CXt_LOOP_PLAIN : CXt_LOOP, | ||||||||||
| 2819 | op => $op, | ||||||||||
| 2820 | "label" => $curcop->[0]->label, | ||||||||||
| 2821 | nextop => $nextop, | ||||||||||
| 2822 | lastop => $lastop, | ||||||||||
| 2823 | redoop => $redoop | ||||||||||
| 2824 | } | ||||||||||
| 2825 | ); | ||||||||||
| 2826 | debug sprintf("enterloop: cxstack label %s\n", $curcop->[0]->label) if $debug{cxstack}; | ||||||||||
| 2827 | $nextop->save; | ||||||||||
| 2828 | $lastop->save; | ||||||||||
| 2829 | $redoop->save; | ||||||||||
| 2830 | # We need to compile the corresponding pp_leaveloop even if it's | ||||||||||
| 2831 | # never executed. This is needed to get @cxstack right. | ||||||||||
| 2832 | # Use case: while(1) { .. } | ||||||||||
| 2833 | unshift @bblock_todo, ($lastop); | ||||||||||
| 2834 | if (0 and $inline_ops and $op->name eq 'enterloop') { | ||||||||||
| 2835 | warn "inlining enterloop\n" if $debug{op}; | ||||||||||
| 2836 | # XXX = GIMME_V fails on freebsd7 5.8.8 (28) | ||||||||||
| 2837 | # = block_gimme() fails on the rest, but passes on freebsd7 | ||||||||||
| 2838 | runtime "gimme = GIMME_V;"; # XXX | ||||||||||
| 2839 | if ($PERL512) { | ||||||||||
| 2840 | runtime('ENTER_with_name("loop1");', | ||||||||||
| 2841 | 'SAVETMPS;', | ||||||||||
| 2842 | 'ENTER_with_name("loop2");', | ||||||||||
| 2843 | 'PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);', | ||||||||||
| 2844 | 'PUSHLOOP_PLAIN(cx, SP);'); | ||||||||||
| 2845 | } else { | ||||||||||
| 2846 | runtime('ENTER;', | ||||||||||
| 2847 | 'SAVETMPS;', | ||||||||||
| 2848 | 'ENTER;', | ||||||||||
| 2849 | 'PUSHBLOCK(cx, CXt_LOOP, SP);', | ||||||||||
| 2850 | 'PUSHLOOP(cx, 0, SP);'); | ||||||||||
| 2851 | } | ||||||||||
| 2852 | return $op->next; | ||||||||||
| 2853 | } else { | ||||||||||
| 2854 | return default_pp($op); | ||||||||||
| 2855 | } | ||||||||||
| 2856 | } | ||||||||||
| 2857 | |||||||||||
| 2858 | # coverage: 6,21,28,30 | ||||||||||
| 2859 | sub pp_enterloop { enterloop(@_) } | ||||||||||
| 2860 | # coverage: 2 | ||||||||||
| 2861 | sub pp_enteriter { enterloop(@_) } | ||||||||||
| 2862 | |||||||||||
| 2863 | # coverage: 6,21,28,30 | ||||||||||
| 2864 | sub pp_leaveloop { | ||||||||||
| 2865 | my $op = shift; | ||||||||||
| 2866 | if ( !@cxstack ) { | ||||||||||
| 2867 | die "panic: leaveloop, no cxstack"; | ||||||||||
| 2868 | } | ||||||||||
| 2869 | debug "leaveloop: popping from cxstack\n" if $debug{cxstack}; | ||||||||||
| 2870 | pop(@cxstack); | ||||||||||
| 2871 | return default_pp($op); | ||||||||||
| 2872 | } | ||||||||||
| 2873 | |||||||||||
| 2874 | # coverage: ? | ||||||||||
| 2875 | sub pp_next { | ||||||||||
| 2876 | my $op = shift; | ||||||||||
| 2877 | my $cxix; | ||||||||||
| 2878 | if ( $op->flags & OPf_SPECIAL ) { | ||||||||||
| 2879 | $cxix = dopoptoloop(); | ||||||||||
| 2880 | if ( $cxix < 0 ) { | ||||||||||
| 2881 | warn "Warning: \"next\" used outside loop\n"; | ||||||||||
| 2882 | return default_pp($op); # no optimization | ||||||||||
| 2883 | } | ||||||||||
| 2884 | } | ||||||||||
| 2885 | else { | ||||||||||
| 2886 | my $label = $op->pv; | ||||||||||
| 2887 | if ($label) { | ||||||||||
| 2888 | $cxix = dopoptolabel( $label ); | ||||||||||
| 2889 | if ( $cxix < 0 ) { | ||||||||||
| 2890 | # coverage: t/testcc 21 | ||||||||||
| 2891 | warn(sprintf("Warning: Label not found at compile time for \"next %s\"\n", $label )); | ||||||||||
| 2892 | $labels->{nlabel}->{$label} = $$op; | ||||||||||
| 2893 | return $op->next; | ||||||||||
| 2894 | } | ||||||||||
| 2895 | } | ||||||||||
| 2896 | # Add support to leave non-loop blocks. | ||||||||||
| 2897 | if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) { | ||||||||||
| 2898 | if (!$cxstack[$cxix]->{'nextop'} or !$cxstack[$cxix]->{'label'}) { | ||||||||||
| 2899 | error("Use of \"next\" for non-loop and non-label blocks not yet implemented\n"); | ||||||||||
| 2900 | } | ||||||||||
| 2901 | } | ||||||||||
| 2902 | } | ||||||||||
| 2903 | default_pp($op); | ||||||||||
| 2904 | my $nextop = $cxstack[$cxix]->{nextop}; | ||||||||||
| 2905 | if ($nextop) { | ||||||||||
| 2906 | push( @bblock_todo, $nextop ); | ||||||||||
| 2907 | save_or_restore_lexical_state($$nextop); | ||||||||||
| 2908 | runtime( sprintf( "goto %s;", label($nextop) ) ); | ||||||||||
| 2909 | } | ||||||||||
| 2910 | return $op->next; | ||||||||||
| 2911 | } | ||||||||||
| 2912 | |||||||||||
| 2913 | # coverage: ? | ||||||||||
| 2914 | sub pp_redo { | ||||||||||
| 2915 | my $op = shift; | ||||||||||
| 2916 | my $cxix; | ||||||||||
| 2917 | if ( $op->flags & OPf_SPECIAL ) { | ||||||||||
| 2918 | $cxix = dopoptoloop(); | ||||||||||
| 2919 | if ( $cxix < 0 ) { | ||||||||||
| 2920 | #warn("Warning: \"redo\" used outside loop\n"); | ||||||||||
| 2921 | return default_pp($op); # no optimization | ||||||||||
| 2922 | } | ||||||||||
| 2923 | } | ||||||||||
| 2924 | else { | ||||||||||
| 2925 | my $label = $op->pv; | ||||||||||
| 2926 | if ($label) { | ||||||||||
| 2927 | $cxix = dopoptolabel( $label ); | ||||||||||
| 2928 | if ( $cxix < 0 ) { | ||||||||||
| 2929 | warn(sprintf("Warning: Label not found at compile time for \"redo %s\"\n", $label )); | ||||||||||
| 2930 | $labels->{nlabel}->{$label} = $$op; | ||||||||||
| 2931 | return $op->next; | ||||||||||
| 2932 | } | ||||||||||
| 2933 | } | ||||||||||
| 2934 | # Add support to leave non-loop blocks. | ||||||||||
| 2935 | if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) { | ||||||||||
| 2936 | if (!$cxstack[$cxix]->{'redoop'} or !$cxstack[$cxix]->{'label'}) { | ||||||||||
| 2937 | error("Use of \"redo\" for non-loop and non-label blocks not yet implemented\n"); | ||||||||||
| 2938 | } | ||||||||||
| 2939 | } | ||||||||||
| 2940 | } | ||||||||||
| 2941 | default_pp($op); | ||||||||||
| 2942 | my $redoop = $cxstack[$cxix]->{redoop}; | ||||||||||
| 2943 | if ($redoop) { | ||||||||||
| 2944 | push( @bblock_todo, $redoop ); | ||||||||||
| 2945 | save_or_restore_lexical_state($$redoop); | ||||||||||
| 2946 | runtime( sprintf( "goto %s;", label($redoop) ) ); | ||||||||||
| 2947 | } | ||||||||||
| 2948 | return $op->next; | ||||||||||
| 2949 | } | ||||||||||
| 2950 | |||||||||||
| 2951 | # coverage: issue36, cc_last.t | ||||||||||
| 2952 | sub pp_last { | ||||||||||
| 2953 | my $op = shift; | ||||||||||
| 2954 | my $cxix; | ||||||||||
| 2955 | if ( $op->flags & OPf_SPECIAL ) { | ||||||||||
| 2956 | $cxix = dopoptoloop(); | ||||||||||
| 2957 | if ( $cxix < 0 ) { | ||||||||||
| 2958 | #warn("Warning: \"last\" used outside loop\n"); | ||||||||||
| 2959 | return default_pp($op); # no optimization | ||||||||||
| 2960 | } | ||||||||||
| 2961 | } | ||||||||||
| 2962 | elsif (ref($op) eq 'B::PVOP') { # !OPf_STACKED | ||||||||||
| 2963 | my $label = $op->pv; | ||||||||||
| 2964 | if ($label) { | ||||||||||
| 2965 | $cxix = dopoptolabel( $label ); | ||||||||||
| 2966 | if ( $cxix < 0 ) { | ||||||||||
| 2967 | # coverage: cc_last.t 2 (ok) 4 (ok) | ||||||||||
| 2968 | warn( sprintf("Warning: Label not found at compile time for \"last %s\"\n", $label )); | ||||||||||
| 2969 | # last does not jump into the future, by name without $$op | ||||||||||
| 2970 | # instead it should jump to the block afterwards | ||||||||||
| 2971 | $labels->{nlabel}->{$label} = $$op; | ||||||||||
| 2972 | return $op->next; | ||||||||||
| 2973 | } | ||||||||||
| 2974 | } | ||||||||||
| 2975 | # Add support to leave non-loop blocks. label fixed with 1.11 | ||||||||||
| 2976 | if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) { | ||||||||||
| 2977 | if (!$cxstack[$cxix]->{'lastop'} or !$cxstack[$cxix]->{'label'}) { | ||||||||||
| 2978 | error("Use of \"last\" for non-loop and non-label blocks not yet implemented\n"); | ||||||||||
| 2979 | } | ||||||||||
| 2980 | } | ||||||||||
| 2981 | } | ||||||||||
| 2982 | default_pp($op); | ||||||||||
| 2983 | if ($cxstack[$cxix]->{lastop} and $cxstack[$cxix]->{lastop}->next) { | ||||||||||
| 2984 | my $lastop = $cxstack[$cxix]->{lastop}->next; | ||||||||||
| 2985 | push( @bblock_todo, $lastop ); | ||||||||||
| 2986 | save_or_restore_lexical_state($$lastop); | ||||||||||
| 2987 | runtime( sprintf( "goto %s;", label($lastop) ) ); | ||||||||||
| 2988 | } | ||||||||||
| 2989 | return $op->next; | ||||||||||
| 2990 | } | ||||||||||
| 2991 | |||||||||||
| 2992 | # coverage: 3,4 | ||||||||||
| 2993 | sub pp_subst { | ||||||||||
| 2994 | my $op = shift; | ||||||||||
| 2995 | write_back_lexicals(); | ||||||||||
| 2996 | write_back_stack(); | ||||||||||
| 2997 | my $sym = doop($op); | ||||||||||
| 2998 | my $replroot = $op->pmreplroot; | ||||||||||
| 2999 | if ($$replroot) { | ||||||||||
| 3000 | save_or_restore_lexical_state($$replroot); | ||||||||||
| 3001 | runtime sprintf( | ||||||||||
| 3002 | "if (PL_op == ((PMOP*)(%s))%s) goto %s;", | ||||||||||
| 3003 | $sym, $PERL510 ? "->op_pmreplrootu.op_pmreplroot" : "->op_pmreplroot", | ||||||||||
| 3004 | label($replroot) | ||||||||||
| 3005 | ); | ||||||||||
| 3006 | $op->pmreplstart->save; | ||||||||||
| 3007 | push( @bblock_todo, $replroot ); | ||||||||||
| 3008 | } | ||||||||||
| 3009 | invalidate_lexicals(); | ||||||||||
| 3010 | return $op->next; | ||||||||||
| 3011 | } | ||||||||||
| 3012 | |||||||||||
| 3013 | # coverage: 3 | ||||||||||
| 3014 | sub pp_substcont { | ||||||||||
| 3015 | my $op = shift; | ||||||||||
| 3016 | write_back_lexicals(); | ||||||||||
| 3017 | write_back_stack(); | ||||||||||
| 3018 | doop($op); | ||||||||||
| 3019 | my $pmop = $op->other; | ||||||||||
| 3020 | #warn sprintf( "substcont: op = %s, pmop = %s\n", peekop($op), peekop($pmop) ) if $verbose; | ||||||||||
| 3021 | |||||||||||
| 3022 | # my $pmopsym = objsym($pmop); | ||||||||||
| 3023 | my $pmopsym = $pmop->save; # XXX can this recurse? | ||||||||||
| 3024 | # warn "pmopsym = $pmopsym\n" if $verbose; | ||||||||||
| 3025 | save_or_restore_lexical_state( ${ $pmop->pmreplstart } ); | ||||||||||
| 3026 | runtime sprintf( | ||||||||||
| 3027 | "if (PL_op == ((PMOP*)(%s))%s) goto %s;", | ||||||||||
| 3028 | $pmopsym, | ||||||||||
| 3029 | $PERL510 ? "->op_pmstashstartu.op_pmreplstart" : "->op_pmreplstart", | ||||||||||
| 3030 | label( $pmop->pmreplstart ) | ||||||||||
| 3031 | ); | ||||||||||
| 3032 | push( @bblock_todo, $pmop->pmreplstart ); | ||||||||||
| 3033 | invalidate_lexicals(); | ||||||||||
| 3034 | return $pmop->next; | ||||||||||
| 3035 | } | ||||||||||
| 3036 | |||||||||||
| 3037 | # coverage: issue24 | ||||||||||
| 3038 | # resolve the DBM library at compile-time, not at run-time | ||||||||||
| 3039 | sub pp_dbmopen { | ||||||||||
| 3040 | my $op = shift; | ||||||||||
| 3041 | require AnyDBM_File; | ||||||||||
| 3042 | my $dbm = $AnyDBM_File::ISA[0]; | ||||||||||
| 3043 | svref_2object( \&{"$dbm\::bootstrap"} )->save; | ||||||||||
| 3044 | return default_pp($op); | ||||||||||
| 3045 | } | ||||||||||
| 3046 | |||||||||||
| 3047 | sub default_pp { | ||||||||||
| 3048 | my $op = shift; | ||||||||||
| 3049 | my $ppname = "pp_" . $op->name; | ||||||||||
| 3050 | # runtime(sprintf("/* %s */", $ppname)) if $verbose; | ||||||||||
| 3051 | if ( $curcop and $need_curcop{$ppname} ) { | ||||||||||
| 3052 | $curcop->write_back; | ||||||||||
| 3053 | } | ||||||||||
| 3054 | write_back_lexicals() unless $skip_lexicals{$ppname}; | ||||||||||
| 3055 | write_back_stack() unless $skip_stack{$ppname}; | ||||||||||
| 3056 | doop($op); | ||||||||||
| 3057 | |||||||||||
| 3058 | # XXX If the only way that ops can write to a TEMPORARY lexical is | ||||||||||
| 3059 | # when it's named in $op->targ then we could call | ||||||||||
| 3060 | # invalidate_lexicals(TEMPORARY) and avoid having to write back all | ||||||||||
| 3061 | # the temporaries. For now, we'll play it safe and write back the lot. | ||||||||||
| 3062 | invalidate_lexicals() unless $skip_invalidate{$ppname}; | ||||||||||
| 3063 | return $op->next; | ||||||||||
| 3064 | } | ||||||||||
| 3065 | |||||||||||
| 3066 | sub compile_op { | ||||||||||
| 3067 | my $op = shift; | ||||||||||
| 3068 | my $ppname = "pp_" . $op->name; | ||||||||||
| 3069 | if ( exists $ignore_op{$ppname} ) { | ||||||||||
| 3070 | return $op->next; | ||||||||||
| 3071 | } | ||||||||||
| 3072 | debug peek_stack() if $debug{stack}; | ||||||||||
| 3073 | if ( $debug{op} ) { | ||||||||||
| 3074 | debug sprintf( "%s [%s]\n", | ||||||||||
| 3075 | peekop($op), $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ ); | ||||||||||
| 3076 | } | ||||||||||
| 3077 | no strict 'refs'; | ||||||||||
| 3078 | if ( defined(&$ppname) ) { | ||||||||||
| 3079 | $know_op = 0; | ||||||||||
| 3080 | return &$ppname($op); | ||||||||||
| 3081 | } | ||||||||||
| 3082 | else { | ||||||||||
| 3083 | return default_pp($op); | ||||||||||
| 3084 | } | ||||||||||
| 3085 | } | ||||||||||
| 3086 | |||||||||||
| 3087 | sub compile_bblock { | ||||||||||
| 3088 | my $op = shift; | ||||||||||
| 3089 | warn "compile_bblock: ", peekop($op), "\n" if $debug{bblock}; | ||||||||||
| 3090 | save_or_restore_lexical_state($$op); | ||||||||||
| 3091 | write_label($op); | ||||||||||
| 3092 | $know_op = 0; | ||||||||||
| 3093 | do { | ||||||||||
| 3094 | $op = compile_op($op); | ||||||||||
| 3095 | if ($] < 5.013 and ($opt_slow_signals or ($$op and $async_signals{$op->name}))) { | ||||||||||
| 3096 | runtime("PERL_ASYNC_CHECK();"); | ||||||||||
| 3097 | } | ||||||||||
| 3098 | } while ( defined($op) && $$op && !exists( $leaders->{$$op} ) ); | ||||||||||
| 3099 | write_back_stack(); # boo hoo: big loss | ||||||||||
| 3100 | reload_lexicals(); | ||||||||||
| 3101 | return $op; | ||||||||||
| 3102 | } | ||||||||||
| 3103 | |||||||||||
| 3104 | sub cc { | ||||||||||
| 3105 | my ( $name, $root, $start, @padlist ) = @_; | ||||||||||
| 3106 | my $op; | ||||||||||
| 3107 | if ( $done{$$start} ) { | ||||||||||
| 3108 | warn "repeat=>" . ref($start) . " $name,\n" if $verbose; | ||||||||||
| 3109 | $decl->add( sprintf( "#define $name %s", $done{$$start} ) ); | ||||||||||
| 3110 | return; | ||||||||||
| 3111 | } | ||||||||||
| 3112 | return if ref($padlist[0]) !~ /^B::(AV|PADNAMELIST)$/ or ref($padlist[1]) ne 'B::AV'; | ||||||||||
| 3113 | warn "cc $name\n" if $verbose; | ||||||||||
| 3114 | init_pp($name); | ||||||||||
| 3115 | load_pad(@padlist); | ||||||||||
| 3116 | %lexstate = (); | ||||||||||
| 3117 | B::Pseudoreg->new_scope; | ||||||||||
| 3118 | @cxstack = (); | ||||||||||
| 3119 | if ( $debug{timings} ) { | ||||||||||
| 3120 | warn sprintf( "Basic block analysis at %s\n", timing_info ); | ||||||||||
| 3121 | } | ||||||||||
| 3122 | $leaders = find_leaders( $root, $start ); | ||||||||||
| 3123 | my @leaders = keys %$leaders; | ||||||||||
| 3124 | if ( $#leaders > -1 ) { | ||||||||||
| 3125 | # Don't add basic blocks of dead code. | ||||||||||
| 3126 | # It would produce errors when processing $cxstack. | ||||||||||
| 3127 | # @bblock_todo = ( values %$leaders ); | ||||||||||
| 3128 | # Instead, save $root (pp_leavesub) separately, | ||||||||||
| 3129 | # because it will not get compiled if located in dead code. | ||||||||||
| 3130 | $root->save; | ||||||||||
| 3131 | unshift @bblock_todo, ($start) if $$start; | ||||||||||
| 3132 | } | ||||||||||
| 3133 | else { | ||||||||||
| 3134 | runtime("return PL_op?PL_op->op_next:0;"); | ||||||||||
| 3135 | } | ||||||||||
| 3136 | if ( $debug{timings} ) { | ||||||||||
| 3137 | warn sprintf( "Compilation at %s\n", timing_info ); | ||||||||||
| 3138 | } | ||||||||||
| 3139 | while (@bblock_todo) { | ||||||||||
| 3140 | $op = shift @bblock_todo; | ||||||||||
| 3141 | warn sprintf( "Considering basic block %s\n", peekop($op) ) if $debug{bblock}; | ||||||||||
| 3142 | next if !defined($op) || !$$op || $done{$$op}; | ||||||||||
| 3143 | warn "...compiling it\n" if $debug{bblock}; | ||||||||||
| 3144 | do { | ||||||||||
| 3145 | $done{$$op} = $name; | ||||||||||
| 3146 | $op = compile_bblock($op); | ||||||||||
| 3147 | if ( $need_freetmps && $freetmps_each_bblock ) { | ||||||||||
| 3148 | runtime("FREETMPS;"); | ||||||||||
| 3149 | $need_freetmps = 0; | ||||||||||
| 3150 | } | ||||||||||
| 3151 | } while defined($op) && $$op && !$done{$$op}; | ||||||||||
| 3152 | if ( $need_freetmps && $freetmps_each_loop ) { | ||||||||||
| 3153 | runtime("FREETMPS;"); | ||||||||||
| 3154 | $need_freetmps = 0; | ||||||||||
| 3155 | } | ||||||||||
| 3156 | if ( !$$op ) { | ||||||||||
| 3157 | runtime( "PUTBACK;", | ||||||||||
| 3158 | "return NULL;" ); | ||||||||||
| 3159 | } | ||||||||||
| 3160 | elsif ( $done{$$op} ) { | ||||||||||
| 3161 | save_or_restore_lexical_state($$op); | ||||||||||
| 3162 | runtime( sprintf( "goto %s;", label($op) ) ); | ||||||||||
| 3163 | } | ||||||||||
| 3164 | } | ||||||||||
| 3165 | if ( $debug{timings} ) { | ||||||||||
| 3166 | warn sprintf( "Saving runtime at %s\n", timing_info ); | ||||||||||
| 3167 | } | ||||||||||
| 3168 | declare_pad(@padlist); | ||||||||||
| 3169 | save_runtime(); | ||||||||||
| 3170 | } | ||||||||||
| 3171 | |||||||||||
| 3172 | sub cc_recurse { | ||||||||||
| 3173 | my ($ccinfo); | ||||||||||
| 3174 | my $start = cc_queue(@_) if @_; | ||||||||||
| 3175 | |||||||||||
| 3176 | while ( $ccinfo = shift @cc_todo ) { | ||||||||||
| 3177 | if ($ccinfo->[0] eq 'pp_sub_warnings__register_categories') { | ||||||||||
| 3178 | # patch broken PADLIST | ||||||||||
| 3179 | #warn "cc $ccinfo->[0] patch broken PADLIST (inc-i340)\n" if $verbose; | ||||||||||
| 3180 | #debug "cc(ccinfo): @$ccinfo skipped (inc-i340)\n" if $debug{queue}; | ||||||||||
| 3181 | #$ccinfo->[0] = 'NULL'; | ||||||||||
| 3182 | my @empty = (); | ||||||||||
| 3183 | #$ccinfo->[3] = $ccinfo->[4] = svref_2object(\@empty); | ||||||||||
| 3184 | } | ||||||||||
| 3185 | if ($DB::deep and $ccinfo->[0] =~ /^pp_sub_(DB|Term__ReadLine)_/) { | ||||||||||
| 3186 | warn "cc $ccinfo->[0] skipped (debugging)\n" if $verbose; | ||||||||||
| 3187 | debug "cc(ccinfo): @$ccinfo skipped (debugging)\n" if $debug{queue}; | ||||||||||
| 3188 | } | ||||||||||
| 3189 | elsif (exists $cc_pp_sub{$ccinfo->[0]}) { # skip duplicates | ||||||||||
| 3190 | warn "cc $ccinfo->[0] already defined\n" if $verbose; | ||||||||||
| 3191 | debug "cc(ccinfo): @$ccinfo already defined\n" if $debug{queue}; | ||||||||||
| 3192 | while (exists $cc_pp_sub{$ccinfo->[0]}) { | ||||||||||
| 3193 | if ($ccinfo->[0] =~ /^(pp_(?:lex)?sub_.*_)(\d*)$/) { | ||||||||||
| 3194 | my $s = $2; | ||||||||||
| 3195 | $s++; | ||||||||||
| 3196 | $ccinfo->[0] = $1 . $s; | ||||||||||
| 3197 | } else { | ||||||||||
| 3198 | $ccinfo->[0] .= '_0'; | ||||||||||
| 3199 | } | ||||||||||
| 3200 | } | ||||||||||
| 3201 | warn "cc renamed to $ccinfo->[0]\n" if $verbose; | ||||||||||
| 3202 | cc(@$ccinfo); | ||||||||||
| 3203 | $cc_pp_sub{$ccinfo->[0]}++; | ||||||||||
| 3204 | } else { | ||||||||||
| 3205 | debug "cc(ccinfo): @$ccinfo\n" if $debug{queue}; | ||||||||||
| 3206 | cc(@$ccinfo); | ||||||||||
| 3207 | $cc_pp_sub{$ccinfo->[0]}++; | ||||||||||
| 3208 | } | ||||||||||
| 3209 | } | ||||||||||
| 3210 | return $start; | ||||||||||
| 3211 | } | ||||||||||
| 3212 | |||||||||||
| 3213 | sub cc_obj { | ||||||||||
| 3214 | my ( $name, $cvref ) = @_; | ||||||||||
| 3215 | my $cv = svref_2object($cvref); | ||||||||||
| 3216 | my @padlist = $cv->PADLIST->ARRAY; | ||||||||||
| 3217 | my $curpad_sym = $padlist[1]->save; | ||||||||||
| 3218 | set_curcv $cv; | ||||||||||
| 3219 | cc_recurse( $name, $cv->ROOT, $cv->START, @padlist ); | ||||||||||
| 3220 | } | ||||||||||
| 3221 | |||||||||||
| 3222 | sub cc_main { | ||||||||||
| 3223 | my @comppadlist = comppadlist->ARRAY; | ||||||||||
| 3224 | my $curpad_nam = $comppadlist[0]->save('curpad_name'); | ||||||||||
| 3225 | my $curpad_sym = $comppadlist[1]->save('curpad_syms');; | ||||||||||
| 3226 | my $init_av = init_av->save('INIT'); | ||||||||||
| 3227 | set_curcv B::main_cv; | ||||||||||
| 3228 | my $start = cc_recurse( "pp_main", main_root, main_start, @comppadlist ); | ||||||||||
| 3229 | |||||||||||
| 3230 | # Do save_unused_subs before saving inc_hv | ||||||||||
| 3231 | B::C::module($module) if $module; | ||||||||||
| 3232 | save_unused_subs(); | ||||||||||
| 3233 | |||||||||||
| 3234 | my $warner = $SIG{__WARN__}; | ||||||||||
| 3235 | save_sig($warner); | ||||||||||
| 3236 | |||||||||||
| 3237 | my($inc_hv, $inc_av, $end_av); | ||||||||||
| 3238 | if ( !defined($module) ) { | ||||||||||
| 3239 | # forbid run-time extends of curpad syms, names and INC | ||||||||||
| 3240 | warn "save context:\n" if $verbose; | ||||||||||
| 3241 | $init->add("/* save context */"); | ||||||||||
| 3242 | $init->add('/* %INC */'); | ||||||||||
| 3243 | inc_cleanup(); | ||||||||||
| 3244 | my $inc_gv = svref_2object( \*main::INC ); | ||||||||||
| 3245 | $inc_hv = $inc_gv->HV->save('main::INC'); | ||||||||||
| 3246 | $init->add( sprintf( "GvHV(%s) = s\\_%x;", | ||||||||||
| 3247 | $inc_gv->save('main::INC'), $inc_hv ) ); | ||||||||||
| 3248 | local ($B::C::const_strings); | ||||||||||
| 3249 | $B::C::const_strings = 1 if $B::C::ro_inc; | ||||||||||
| 3250 | if ($cross) { | ||||||||||
| 3251 | $init->add('/* cross @INC */'); | ||||||||||
| 3252 | my @crossinc = ($Config{archlib}); | ||||||||||
| 3253 | if ($Config{archlib} ne $Config{privlib}) { | ||||||||||
| 3254 | push @crossinc, $Config{privlib}; | ||||||||||
| 3255 | } | ||||||||||
| 3256 | if (exists $Config{sitearch} and $Config{sitearch}) { | ||||||||||
| 3257 | unshift @crossinc, $Config{sitearch}; | ||||||||||
| 3258 | unshift @crossinc, $Config{sitelib} | ||||||||||
| 3259 | if $Config{sitearch} ne $Config{sitelib}; | ||||||||||
| 3260 | } | ||||||||||
| 3261 | if (exists $Config{vendorarch} and $Config{vendorarch}) { | ||||||||||
| 3262 | push @crossinc, $Config{vendorarch}; | ||||||||||
| 3263 | push @crossinc, $Config{vendorlib} | ||||||||||
| 3264 | if $Config{vendorarch} ne $Config{vendorlib}; | ||||||||||
| 3265 | } | ||||||||||
| 3266 | if ($] < 5.026 and !$Config{usecperl}) { | ||||||||||
| 3267 | push @crossinc, '.'; | ||||||||||
| 3268 | } | ||||||||||
| 3269 | $inc_av = svref_2object(\@crossinc)->save('main::INC'); | ||||||||||
| 3270 | } else { | ||||||||||
| 3271 | $inc_av = $inc_gv->AV->save('main::INC'); | ||||||||||
| 3272 | } | ||||||||||
| 3273 | $init->add( sprintf( "GvAV(%s) = s\\_%x;", | ||||||||||
| 3274 | $inc_gv->save('main::INC'), $inc_av ) ); | ||||||||||
| 3275 | } | ||||||||||
| 3276 | { | ||||||||||
| 3277 | # >=5.10 needs to defer nullifying of all vars in END, not only new ones. | ||||||||||
| 3278 | local ($B::C::const_strings); | ||||||||||
| 3279 | $B::C::in_endav = 1; | ||||||||||
| 3280 | $end_av = end_av->save('END'); | ||||||||||
| 3281 | } | ||||||||||
| 3282 | cc_recurse(); | ||||||||||
| 3283 | return if $errors or $check; | ||||||||||
| 3284 | |||||||||||
| 3285 | if ( !defined($module) ) { | ||||||||||
| 3286 | # XXX TODO push BEGIN/END blocks to modules code. | ||||||||||
| 3287 | $init->add( | ||||||||||
| 3288 | sprintf( "PL_main_root = s\\_%x;", ${ main_root() } ), | ||||||||||
| 3289 | "PL_main_start = $start;", | ||||||||||
| 3290 | "PL_curpad = AvARRAY($curpad_sym);", | ||||||||||
| 3291 | "PL_comppad = $curpad_sym;"); | ||||||||||
| 3292 | if ($] < 5.017005) { | ||||||||||
| 3293 | $init->add( | ||||||||||
| 3294 | "av_store((AV*)CvPADLIST(PL_main_cv), 0, SvREFCNT_inc($curpad_nam)); /* namepad */", | ||||||||||
| 3295 | "av_store((AV*)CvPADLIST(PL_main_cv), 1, SvREFCNT_inc($curpad_sym)); /* curpad */"); | ||||||||||
| 3296 | } else { | ||||||||||
| 3297 | $init->add( | ||||||||||
| 3298 | "PadlistARRAY(CvPADLIST(PL_main_cv))[0] = (PAD*)SvREFCNT_inc($curpad_nam); /* namepad */", | ||||||||||
| 3299 | "PadlistARRAY(CvPADLIST(PL_main_cv))[1] = (PAD*)SvREFCNT_inc($curpad_sym); /* curpad */"); | ||||||||||
| 3300 | } | ||||||||||
| 3301 | $init->add( | ||||||||||
| 3302 | "GvHV(PL_incgv) = $inc_hv;", | ||||||||||
| 3303 | "GvAV(PL_incgv) = $inc_av;", | ||||||||||
| 3304 | "PL_initav = (AV*)$init_av;", | ||||||||||
| 3305 | "PL_endav = (AV*)$end_av;"); | ||||||||||
| 3306 | if ($] < 5.017) { | ||||||||||
| 3307 | my $amagic_generate = B::amagic_generation; | ||||||||||
| 3308 | $init->add("PL_amagic_generation = $amagic_generate;"); | ||||||||||
| 3309 | }; | ||||||||||
| 3310 | } | ||||||||||
| 3311 | |||||||||||
| 3312 | seek( STDOUT, 0, 0 ); #prevent print statements from BEGIN{} into the output | ||||||||||
| 3313 | fixup_ppaddr(); | ||||||||||
| 3314 | print "/* using B::CC $B::CC::VERSION backend */\n"; | ||||||||||
| 3315 | output_boilerplate(); | ||||||||||
| 3316 | print "\n"; | ||||||||||
| 3317 | output_all("perl_init"); | ||||||||||
| 3318 | output_runtime(); | ||||||||||
| 3319 | print "\n"; | ||||||||||
| 3320 | output_main_rest(); | ||||||||||
| 3321 | |||||||||||
| 3322 | if ( defined($module) ) { | ||||||||||
| 3323 | my $cmodule = $module ||= 'main'; | ||||||||||
| 3324 | $cmodule =~ s/::/__/g; | ||||||||||
| 3325 | print <<"EOT"; | ||||||||||
| 3326 | |||||||||||
| 3327 | #include "XSUB.h" | ||||||||||
| 3328 | XS(boot_$cmodule) | ||||||||||
| 3329 | { | ||||||||||
| 3330 | dVAR; | ||||||||||
| 3331 | dXSARGS; | ||||||||||
| 3332 | perl_init(); | ||||||||||
| 3333 | ENTER; | ||||||||||
| 3334 | SAVETMPS; | ||||||||||
| 3335 | SAVEVPTR(PL_curpad); | ||||||||||
| 3336 | SAVEVPTR(PL_op); | ||||||||||
| 3337 | PL_curpad = AvARRAY($curpad_sym); | ||||||||||
| 3338 | PL_op = $start; | ||||||||||
| 3339 | pp_main(aTHX); | ||||||||||
| 3340 | FREETMPS; | ||||||||||
| 3341 | LEAVE; | ||||||||||
| 3342 | ST(0) = &PL_sv_yes; | ||||||||||
| 3343 | XSRETURN(1); | ||||||||||
| 3344 | } | ||||||||||
| 3345 | EOT | ||||||||||
| 3346 | } else { | ||||||||||
| 3347 | output_main(); | ||||||||||
| 3348 | } | ||||||||||
| 3349 | if ( $debug{timings} ) { | ||||||||||
| 3350 | warn sprintf( "Done at %s\n", timing_info ); | ||||||||||
| 3351 | } | ||||||||||
| 3352 | } | ||||||||||
| 3353 | |||||||||||
| 3354 | sub compile_stats { | ||||||||||
| 3355 | my $s = "Total number of OPs processed: $op_count\n"; | ||||||||||
| 3356 | $s .= "Total number of unresolved symbols: $B::C::unresolved_count\n" | ||||||||||
| 3357 | if $B::C::unresolved_count; | ||||||||||
| 3358 | return $s; | ||||||||||
| 3359 | } | ||||||||||
| 3360 | |||||||||||
| 3361 | # Accessible via use B::CC '-ftype-attr'; in user code, or -MB::CC=-O2 on the cmdline | ||||||||||
| 3362 | sub import { | ||||||||||
| 3363 | my @options = @_; | ||||||||||
| 3364 | # Allow debugging in CHECK blocks without Od | ||||||||||
| 3365 | $DB::single = 1 if defined &DB::DB; | ||||||||||
| 3366 | my ( $option, $opt, $arg ); | ||||||||||
| 3367 | # init with -O0 | ||||||||||
| 3368 | foreach my $ref ( values %optimise ) { | ||||||||||
| 3369 | $$ref = 0; | ||||||||||
| 3370 | } | ||||||||||
| 3371 | $B::C::fold = 0 if $] >= 5.013009; # utf8::Cased tables | ||||||||||
| 3372 | $B::C::warnings = 0 if $] >= 5.013005; # Carp warnings categories and B | ||||||||||
| 3373 | $B::C::destruct = 0 unless $] < 5.008; # fast_destruct | ||||||||||
| 3374 | $opt_taint = 1; | ||||||||||
| 3375 | $opt_magic = 1; # only makes sense with -fno-magic | ||||||||||
| 3376 | $opt_autovivify = 1; # only makes sense with -fno-autovivify | ||||||||||
| 3377 | OPTION: | ||||||||||
| 3378 | while ( $option = shift @options ) { | ||||||||||
| 3379 | if ( $option =~ /^-(cross)=(.*)/ ) { | ||||||||||
| 3380 | $opt = $1; | ||||||||||
| 3381 | $arg = $2; | ||||||||||
| 3382 | } | ||||||||||
| 3383 | elsif ( $option =~ /^-(.)(.*)/ ) { | ||||||||||
| 3384 | $opt = $1; | ||||||||||
| 3385 | $arg = $2; | ||||||||||
| 3386 | } | ||||||||||
| 3387 | else { | ||||||||||
| 3388 | unshift @options, $option; | ||||||||||
| 3389 | last OPTION; | ||||||||||
| 3390 | } | ||||||||||
| 3391 | if ( $opt eq "-" && $arg eq "-" ) { | ||||||||||
| 3392 | shift @options; | ||||||||||
| 3393 | last OPTION; | ||||||||||
| 3394 | } | ||||||||||
| 3395 | elsif ( $opt eq "o" ) { | ||||||||||
| 3396 | $arg ||= shift @options; | ||||||||||
| 3397 | open( STDOUT, ">$arg" ) or return "open '>$arg': $!\n"; | ||||||||||
| 3398 | } | ||||||||||
| 3399 | elsif ( $opt eq "c" ) { | ||||||||||
| 3400 | $check = 1; | ||||||||||
| 3401 | $B::C::check = 1; | ||||||||||
| 3402 | } | ||||||||||
| 3403 | elsif ( $opt eq "v" ) { | ||||||||||
| 3404 | $verbose = 1; | ||||||||||
| 3405 | B::C::verbose(1); # crashed in C _save_common_middle(B::FAKEOP) | ||||||||||
| 3406 | } | ||||||||||
| 3407 | elsif ( $opt eq "u" ) { | ||||||||||
| 3408 | $arg ||= shift @options; | ||||||||||
| 3409 | eval "require $arg;"; | ||||||||||
| 3410 | mark_unused( $arg, 1 ); | ||||||||||
| 3411 | } | ||||||||||
| 3412 | elsif ( $opt eq "U" ) { | ||||||||||
| 3413 | $arg ||= shift @options; | ||||||||||
| 3414 | mark_skip( $arg ); | ||||||||||
| 3415 | } | ||||||||||
| 3416 | elsif ( $opt eq "strict" ) { | ||||||||||
| 3417 | $arg ||= shift @options; | ||||||||||
| 3418 | $strict++; | ||||||||||
| 3419 | } | ||||||||||
| 3420 | elsif ( $opt eq "f" ) { | ||||||||||
| 3421 | $arg ||= shift @options; | ||||||||||
| 3422 | my $value = $arg !~ s/^no-//; | ||||||||||
| 3423 | $arg =~ s/-/_/g; | ||||||||||
| 3424 | my $ref = $optimise{$arg}; | ||||||||||
| 3425 | if ( defined($ref) ) { | ||||||||||
| 3426 | $$ref = $value; | ||||||||||
| 3427 | } | ||||||||||
| 3428 | else { | ||||||||||
| 3429 | # Pass down to B::C | ||||||||||
| 3430 | my $ref = $B::C::option_map{$arg}; | ||||||||||
| 3431 | if ( defined($ref) ) { | ||||||||||
| 3432 | $$ref = $value; | ||||||||||
| 3433 | $c_optimise{$ref}++; | ||||||||||
| 3434 | } | ||||||||||
| 3435 | else { | ||||||||||
| 3436 | warn qq(Warning: ignoring unknown optimisation "$arg"\n); | ||||||||||
| 3437 | } | ||||||||||
| 3438 | } | ||||||||||
| 3439 | } | ||||||||||
| 3440 | elsif ( $opt eq "O" ) { | ||||||||||
| 3441 | $arg = 1 if $arg eq ""; | ||||||||||
| 3442 | foreach my $ref ( values %optimise ) { | ||||||||||
| 3443 | $$ref = 0; | ||||||||||
| 3444 | } | ||||||||||
| 3445 | if ($arg >= 2) { | ||||||||||
| 3446 | $freetmps_each_loop = 1; | ||||||||||
| 3447 | if (!$ITHREADS) { | ||||||||||
| 3448 | #warn qq(Warning: ignoring -faelem with threaded perl\n); | ||||||||||
| 3449 | $opt_aelem = 1; # unstable, test: 68 pp_padhv targ assert | ||||||||||
| 3450 | } | ||||||||||
| 3451 | } | ||||||||||
| 3452 | if ( $arg >= 1 ) { | ||||||||||
| 3453 | $opt_type_attr = 1; | ||||||||||
| 3454 | $freetmps_each_bblock = 1 unless $freetmps_each_loop; | ||||||||||
| 3455 | } | ||||||||||
| 3456 | } | ||||||||||
| 3457 | elsif ( $opt eq "n" ) { | ||||||||||
| 3458 | $arg ||= shift @options; | ||||||||||
| 3459 | $init_name = $arg; | ||||||||||
| 3460 | } | ||||||||||
| 3461 | elsif ( $opt eq "m" ) { | ||||||||||
| 3462 | $module = $arg; | ||||||||||
| 3463 | mark_unused( $arg, undef ); | ||||||||||
| 3464 | } | ||||||||||
| 3465 | elsif ( $opt eq "cross" ) { | ||||||||||
| 3466 | $cross = $arg; | ||||||||||
| 3467 | cross_config($cross); # overrides %B::C::Config::Config | ||||||||||
| 3468 | } | ||||||||||
| 3469 | #elsif ( $opt eq "p" ) { | ||||||||||
| 3470 | # $arg ||= shift @options; | ||||||||||
| 3471 | # $patchlevel = $arg; | ||||||||||
| 3472 | #} | ||||||||||
| 3473 | elsif ( $opt eq "D" ) { | ||||||||||
| 3474 | $arg ||= shift @options; | ||||||||||
| 3475 | $verbose++; | ||||||||||
| 3476 | # note that we should not clash too much with the B::C debug map | ||||||||||
| 3477 | # because we set theirs also | ||||||||||
| 3478 | my %debug_map = (O => 'op', | ||||||||||
| 3479 | T => 'stack', # was S | ||||||||||
| 3480 | c => 'cxstack', | ||||||||||
| 3481 | a => 'pad', # was p | ||||||||||
| 3482 | r => 'runtime', | ||||||||||
| 3483 | w => 'shadow', # was s | ||||||||||
| 3484 | q => 'queue', | ||||||||||
| 3485 | l => 'lineno', | ||||||||||
| 3486 | t => 'timings', | ||||||||||
| 3487 | b => 'bblock'); | ||||||||||
| 3488 | $arg = join('',keys %debug_map).'Fsp' if $arg eq 'full'; | ||||||||||
| 3489 | foreach $arg ( split( //, $arg ) ) { | ||||||||||
| 3490 | if ( $arg eq "o" ) { | ||||||||||
| 3491 | B->debug(1); | ||||||||||
| 3492 | } | ||||||||||
| 3493 | elsif ( $debug_map{$arg} ) { | ||||||||||
| 3494 | $debug{ $debug_map{$arg} }++; | ||||||||||
| 3495 | } | ||||||||||
| 3496 | elsif ( $arg eq "F" and eval "require B::Flags;" ) { | ||||||||||
| 3497 | $debug{flags}++; | ||||||||||
| 3498 | $B::C::debug{flags}++; | ||||||||||
| 3499 | } | ||||||||||
| 3500 | elsif ( exists $B::C::debug_map{$arg} ) { | ||||||||||
| 3501 | $B::C::verbose++; | ||||||||||
| 3502 | $B::C::debug{ $B::C::debug_map{$arg} }++; | ||||||||||
| 3503 | } | ||||||||||
| 3504 | else { | ||||||||||
| 3505 | warn qq(Warning: ignoring unknown -D option "$arg"\n); | ||||||||||
| 3506 | } | ||||||||||
| 3507 | } | ||||||||||
| 3508 | } | ||||||||||
| 3509 | } | ||||||||||
| 3510 | $strict++ if !$strict and $Config{ccflags} !~ m/-DDEBUGGING/; | ||||||||||
| 3511 | if ($opt_omit_taint) { | ||||||||||
| 3512 | $opt_taint = 0; | ||||||||||
| 3513 | warn "Warning: -fomit_taint is deprecated. Use -fno-taint instead.\n"; | ||||||||||
| 3514 | } | ||||||||||
| 3515 | |||||||||||
| 3516 | # rgs didn't want opcodes to be added to Opcode. So I had to add it to a | ||||||||||
| 3517 | # seperate Opcodes package. | ||||||||||
| 3518 | eval { require Opcodes; }; | ||||||||||
| 3519 | if (!$@ and $Opcodes::VERSION) { | ||||||||||
| 3520 | my $MAXO = Opcodes::opcodes(); | ||||||||||
| 3521 | for (0..$MAXO-1) { | ||||||||||
| 3522 | no strict 'refs'; | ||||||||||
| 3523 | my $ppname = "pp_".Opcodes::opname($_); | ||||||||||
| 3524 | # opflags n: no args, no return values. don't need save/restore stack | ||||||||||
| 3525 | # But pp_enter, pp_leave use/change global stack. | ||||||||||
| 3526 | next if $ppname eq 'pp_enter' || $ppname eq 'pp_leave'; | ||||||||||
| 3527 | $no_stack{$ppname} = 1 | ||||||||||
| 3528 | if Opcodes::opflags($_) & 512; | ||||||||||
| 3529 | # XXX More Opcodes options to be added later | ||||||||||
| 3530 | } | ||||||||||
| 3531 | } | ||||||||||
| 3532 | #if ($debug{op}) { | ||||||||||
| 3533 | # warn "no_stack: ",join(" ",sort keys %no_stack),"\n"; | ||||||||||
| 3534 | #} | ||||||||||
| 3535 | |||||||||||
| 3536 | mark_skip(qw(B::C B::C::Config B::CC B::Asmdata B::FAKEOP | ||||||||||
| 3537 | B::Pseudoreg B::Shadow B::C::InitSection | ||||||||||
| 3538 | O B::Stackobj B::Stackobj::Bool B::Stackobj::Padsv | ||||||||||
| 3539 | B::Stackobj::Const B::Stackobj::Aelem B::Bblock)); | ||||||||||
| 3540 | $B::C::all_bc_deps{$_}++ for qw(Opcodes Opcode B::Concise attributes double int num str string subs); | ||||||||||
| 3541 | mark_skip(qw(DB Term::ReadLine)) if defined &DB::DB; | ||||||||||
| 3542 | |||||||||||
| 3543 | # Set some B::C optimizations. | ||||||||||
| 3544 | # optimize_ppaddr is not needed with B::CC as CC does it even better. | ||||||||||
| 3545 | for (qw(optimize_warn_sv save_data_fh av_init save_sig destruct const_strings)) { | ||||||||||
| 3546 | no strict 'refs'; | ||||||||||
| 3547 | ${"B::C::$_"} = 1 unless $c_optimise{$_}; | ||||||||||
| 3548 | } | ||||||||||
| 3549 | $B::C::destruct = 0 unless $c_optimise{destruct} and $] > 5.008; | ||||||||||
| 3550 | $B::C::stash = 0 unless $c_optimise{stash}; | ||||||||||
| 3551 | if (!$B::C::Config::have_independent_comalloc) { | ||||||||||
| 3552 | $B::C::av_init = 1 unless $c_optimise{av_init}; | ||||||||||
| 3553 | $B::C::av_init2 = 0 unless $c_optimise{av_init2}; | ||||||||||
| 3554 | } else { | ||||||||||
| 3555 | $B::C::av_init = 0 unless $c_optimise{av_init}; | ||||||||||
| 3556 | $B::C::av_init2 = 1 unless $c_optimise{av_init2}; | ||||||||||
| 3557 | } | ||||||||||
| 3558 | init_type_attrs() if $opt_type_attr; # but too late for -MB::CC=-O2 on import. attrs are checked before | ||||||||||
| 3559 | @options; | ||||||||||
| 3560 | } | ||||||||||
| 3561 | |||||||||||
| 3562 | # -MO=CC entry point | ||||||||||
| 3563 | sub compile { | ||||||||||
| 3564 | my @options = @_; | ||||||||||
| 3565 | @options = import(@options); | ||||||||||
| 3566 | |||||||||||
| 3567 | init_sections(); | ||||||||||
| 3568 | $init = B::C::Section->get("init"); | ||||||||||
| 3569 | $decl = B::C::Section->get("decl"); | ||||||||||
| 3570 | |||||||||||
| 3571 | # just some subs or main? | ||||||||||
| 3572 | if (@options) { | ||||||||||
| 3573 | return sub { | ||||||||||
| 3574 | my ( $objname, $ppname ); | ||||||||||
| 3575 | foreach $objname (@options) { | ||||||||||
| 3576 | $objname = "main::$objname" unless $objname =~ /::/; | ||||||||||
| 3577 | ( $ppname = $objname ) =~ s/^.*?:://; | ||||||||||
| 3578 | eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)"; | ||||||||||
| 3579 | die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@; | ||||||||||
| 3580 | return if $errors; | ||||||||||
| 3581 | } | ||||||||||
| 3582 | my $warner = $SIG{__WARN__}; | ||||||||||
| 3583 | save_sig($warner); | ||||||||||
| 3584 | fixup_ppaddr(); | ||||||||||
| 3585 | return if $check; | ||||||||||
| 3586 | output_boilerplate(); | ||||||||||
| 3587 | print "\n"; | ||||||||||
| 3588 | output_all( $init_name || "init_module" ); | ||||||||||
| 3589 | output_runtime(); | ||||||||||
| 3590 | output_main_rest(); | ||||||||||
| 3591 | } | ||||||||||
| 3592 | } | ||||||||||
| 3593 | else { | ||||||||||
| 3594 | return sub { cc_main() }; | ||||||||||
| 3595 | } | ||||||||||
| 3596 | } | ||||||||||
| 3597 | |||||||||||
| 3598 | 1; | ||||||||||
| 3599 | |||||||||||
| 3600 | __END__ | ||||||||||