| 1 | # Bytecode56.pm | ||
| 2 | # | ||
| 3 | # Copyright (c) 1996-1998 Malcolm Beattie | ||
| 4 | # | ||
| 5 | # You may distribute under the terms of either the GNU General Public | ||
| 6 | # License or the Artistic License, as specified in the README file. | ||
| 7 | # | ||
| 8 | package B::Bytecode56; | ||
| 9 | |||
| 10 | # The original 5.6 Bytecode compiler. Unused, not installed. Just for reference. | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use Carp; | ||
| 14 | use B qw(main_cv main_root main_start comppadlist | ||
| 15 | class peekop walkoptree svref_2object cstring walksymtable | ||
| 16 | init_av begin_av end_av | ||
| 17 | SVf_POK SVp_POK SVf_IOK SVp_IOK SVf_NOK SVp_NOK | ||
| 18 | SVf_READONLY GVf_IMPORTED_AV GVf_IMPORTED_CV GVf_IMPORTED_HV | ||
| 19 | GVf_IMPORTED_SV SVTYPEMASK | ||
| 20 | ); | ||
| 21 | use B::Asmdata qw(@optype @specialsv_name); | ||
| 22 | use B::Assembler qw(newasm endasm assemble); | ||
| 23 | |||
| 24 | my %optype_enum; | ||
| 25 | my $i; | ||
| 26 | for ( $i = 0 ; $i < @optype ; $i++ ) { | ||
| 27 | $optype_enum{ $optype[$i] } = $i; | ||
| 28 | } | ||
| 29 | |||
| 30 | # Following is SVf_POK|SVp_POK | ||
| 31 | # XXX Shouldn't be hardwired | ||
| 32 | sub POK () { SVf_POK | SVp_POK } | ||
| 33 | |||
| 34 | # Following is SVf_IOK|SVp_IOK | ||
| 35 | # XXX Shouldn't be hardwired | ||
| 36 | sub IOK () { SVf_IOK | SVp_IOK } | ||
| 37 | |||
| 38 | # Following is SVf_NOK|SVp_NOK | ||
| 39 | # XXX Shouldn't be hardwired | ||
| 40 | sub NOK () { SVf_NOK | SVp_NOK } | ||
| 41 | |||
| 42 | # nonexistant flags (see B::GV::bytecode for usage) | ||
| 43 | sub GVf_IMPORTED_IO () { 0; } | ||
| 44 | sub GVf_IMPORTED_FORM () { 0; } | ||
| 45 | |||
| 46 | my ( $verbose, $no_assemble, $debug_bc, $debug_cv ); | ||
| 47 | my @packages; # list of packages to compile | ||
| 48 | |||
| 49 | sub asm (@) { # print replacement that knows about assembling | ||
| 50 | if ($no_assemble) { | ||
| 51 | print @_; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | my $buf = join '', @_; | ||
| 55 | assemble($_) for ( split /\n/, $buf ); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub asmf (@) { # printf replacement that knows about assembling | ||
| 60 | if ($no_assemble) { | ||
| 61 | printf shift(), @_; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | my $format = shift; | ||
| 65 | my $buf = sprintf $format, @_; | ||
| 66 | assemble($_) for ( split /\n/, $buf ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | # Optimisation options. On the command line, use hyphens instead of | ||
| 71 | # underscores for compatibility with gcc-style options. We use | ||
| 72 | # underscores here because they are OK in (strict) barewords. | ||
| 73 | my ( $compress_nullops, $omit_seq, $bypass_nullops ); | ||
| 74 | my %optimise = ( | ||
| 75 | compress_nullops => \$compress_nullops, | ||
| 76 | omit_sequence_numbers => \$omit_seq, | ||
| 77 | bypass_nullops => \$bypass_nullops | ||
| 78 | ); | ||
| 79 | |||
| 80 | my $strip_syntree; # this is left here in case stripping the | ||
| 81 | # syntree ever becomes safe again | ||
| 82 | # -- BKS, June 2000 | ||
| 83 | |||
| 84 | my $nextix = 0; | ||
| 85 | my %symtable; # maps object addresses to object indices. | ||
| 86 | # Filled in at allocation (newsv/newop) time. | ||
| 87 | |||
| 88 | my %saved; # maps object addresses (for SVish classes) to "saved yet?" | ||
| 89 | # flag. Set at FOO::bytecode time usually by SV::bytecode. | ||
| 90 | # Manipulated via saved(), mark_saved(), unmark_saved(). | ||
| 91 | |||
| 92 | my %strtable; # maps shared strings to object indices | ||
| 93 | # Filled in at allocation (pvix) time | ||
| 94 | |||
| 95 | my $svix = -1; # we keep track of when the sv register contains an element | ||
| 96 | # of the object table to avoid unnecessary repeated | ||
| 97 | # consecutive ldsv instructions. | ||
| 98 | |||
| 99 | my $opix = -1; # Ditto for the op register. | ||
| 100 | |||
| 101 | sub ldsv { | ||
| 102 | my $ix = shift; | ||
| 103 | if ( $ix != $svix ) { | ||
| 104 | asm "ldsv $ix\n"; | ||
| 105 | $svix = $ix; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub stsv { | ||
| 110 | my $ix = shift; | ||
| 111 | asm "stsv $ix\n"; | ||
| 112 | $svix = $ix; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub set_svix { | ||
| 116 | $svix = shift; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub ldop { | ||
| 120 | my $ix = shift; | ||
| 121 | if ( $ix != $opix ) { | ||
| 122 | asm "ldop $ix\n"; | ||
| 123 | $opix = $ix; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub stop { | ||
| 128 | my $ix = shift; | ||
| 129 | asm "stop $ix\n"; | ||
| 130 | $opix = $ix; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub set_opix { | ||
| 134 | $opix = shift; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub pvstring { | ||
| 138 | my $str = shift; | ||
| 139 | if ( defined($str) ) { | ||
| 140 | return cstring( $str . "\0" ); | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | return '""'; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub nv { | ||
| 148 | # print full precision | ||
| 149 | my $str = sprintf "%.40f", $_[0]; | ||
| 150 | $str =~ s/0+$//; # remove trailing zeros | ||
| 151 | $str =~ s/\.$/.0/; | ||
| 152 | return $str; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub saved { $saved{ ${ $_[0] } } } | ||
| 156 | sub mark_saved { $saved{ ${ $_[0] } } = 1 } | ||
| 157 | sub unmark_saved { $saved{ ${ $_[0] } } = 0 } | ||
| 158 | |||
| 159 | sub debug { $debug_bc = shift } | ||
| 160 | |||
| 161 | sub pvix { # save a shared PV (mainly for COPs) | ||
| 162 | return $strtable{ $_[0] } if defined( $strtable{ $_[0] } ); | ||
| 163 | asmf "newpv %s\n", pvstring( $_[0] ); | ||
| 164 | my $ix = $nextix++; | ||
| 165 | $strtable{ $_[0] } = $ix; | ||
| 166 | asmf "stpv %d\n", $ix; | ||
| 167 | return $ix; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub B::OBJECT::nyi { | ||
| 171 | my $obj = shift; | ||
| 172 | warn sprintf( "bytecode save method for %s (0x%x) not yet implemented\n", | ||
| 173 | class($obj), $$obj ); | ||
| 174 | } | ||
| 175 | |||
| 176 | # | ||
| 177 | # objix may stomp on the op register (for op objects) | ||
| 178 | # or the sv register (for SV objects) | ||
| 179 | # | ||
| 180 | sub B::OBJECT::objix { | ||
| 181 | my $obj = shift; | ||
| 182 | my $ix = $symtable{$$obj}; | ||
| 183 | if ( defined($ix) ) { | ||
| 184 | return $ix; | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | $obj->newix($nextix); | ||
| 188 | return $symtable{$$obj} = $nextix++; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub B::SV::newix { | ||
| 193 | my ( $sv, $ix ) = @_; | ||
| 194 | asmf "newsv %d\t# %s\n", $sv->FLAGS & SVTYPEMASK, class($sv); | ||
| 195 | stsv($ix); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub B::GV::newix { | ||
| 199 | my ( $gv, $ix ) = @_; | ||
| 200 | my $gvname = $gv->NAME; | ||
| 201 | my $name = cstring( $gv->STASH->NAME . "::" . $gvname ); | ||
| 202 | asm "gv_fetchpv $name\n"; | ||
| 203 | stsv($ix); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub B::HV::newix { | ||
| 207 | my ( $hv, $ix ) = @_; | ||
| 208 | my $name = $hv->NAME; | ||
| 209 | if ($name) { | ||
| 210 | |||
| 211 | # It's a stash | ||
| 212 | asmf "gv_stashpv %s\n", cstring($name); | ||
| 213 | stsv($ix); | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | |||
| 217 | # It's an ordinary HV. Fall back to ordinary newix method | ||
| 218 | $hv->B::SV::newix($ix); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | sub B::SPECIAL::newix { | ||
| 223 | my ( $sv, $ix ) = @_; | ||
| 224 | |||
| 225 | # Special case. $$sv is not the address of the SV but an | ||
| 226 | # index into svspecialsv_list. | ||
| 227 | asmf "ldspecsv $$sv\t# %s\n", $specialsv_name[$$sv]; | ||
| 228 | stsv($ix); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub B::OP::newix { | ||
| 232 | my ( $op, $ix ) = @_; | ||
| 233 | my $class = class($op); | ||
| 234 | my $typenum = $optype_enum{$class}; | ||
| 235 | croak("OP::newix: can't understand class $class") unless defined($typenum); | ||
| 236 | asm "newop $typenum\t# $class\n"; | ||
| 237 | stop($ix); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub B::OP::walkoptree_debug { | ||
| 241 | my $op = shift; | ||
| 242 | warn( sprintf( "walkoptree: %s\n", peekop($op) ) ); | ||
| 243 | } | ||
| 244 | |||
| 245 | sub B::OP::bytecode { | ||
| 246 | my $op = shift; | ||
| 247 | my $next = $op->next; | ||
| 248 | my $nextix; | ||
| 249 | my $sibix = $op->sibling->objix unless $strip_syntree; | ||
| 250 | my $ix = $op->objix; | ||
| 251 | my $type = $op->type; | ||
| 252 | |||
| 253 | if ($bypass_nullops) { | ||
| 254 | $next = $next->next while $$next && $next->type == 0; | ||
| 255 | } | ||
| 256 | $nextix = $next->objix; | ||
| 257 | |||
| 258 | asmf "# %s\n", peekop($op) if $debug_bc; | ||
| 259 | ldop($ix); | ||
| 260 | asm "op_next $nextix\n"; | ||
| 261 | asm "op_sibling $sibix\n" unless $strip_syntree; | ||
| 262 | asmf "op_type %s\t# %d\n", "pp_" . $op->name, $type; | ||
| 263 | asmf( "op_seq %d\n", $op->seq ) unless $omit_seq; | ||
| 264 | if ( $type || !$compress_nullops ) { | ||
| 265 | asmf "op_targ %d\nop_flags 0x%x\nop_private 0x%x\n", | ||
| 266 | $op->targ, $op->flags, $op->private; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | sub B::UNOP::bytecode { | ||
| 271 | my $op = shift; | ||
| 272 | my $firstix = $op->first->objix unless $strip_syntree; | ||
| 273 | $op->B::OP::bytecode; | ||
| 274 | if ( ( $op->type || !$compress_nullops ) && !$strip_syntree ) { | ||
| 275 | asm "op_first $firstix\n"; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | sub B::LOGOP::bytecode { | ||
| 280 | my $op = shift; | ||
| 281 | my $otherix = $op->other->objix; | ||
| 282 | $op->B::UNOP::bytecode; | ||
| 283 | asm "op_other $otherix\n"; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub B::SVOP::bytecode { | ||
| 287 | my $op = shift; | ||
| 288 | my $sv = $op->sv; | ||
| 289 | my $svix = $sv->objix; | ||
| 290 | $op->B::OP::bytecode; | ||
| 291 | asm "op_sv $svix\n"; | ||
| 292 | $sv->bytecode; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub B::PADOP::bytecode { | ||
| 296 | my $op = shift; | ||
| 297 | my $padix = $op->padix; | ||
| 298 | $op->B::OP::bytecode; | ||
| 299 | asm "op_padix $padix\n"; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub B::PVOP::bytecode { | ||
| 303 | my $op = shift; | ||
| 304 | my $pv = $op->pv; | ||
| 305 | $op->B::OP::bytecode; | ||
| 306 | |||
| 307 | # | ||
| 308 | # This would be easy except that OP_TRANS uses a PVOP to store an | ||
| 309 | # endian-dependent array of 256 shorts instead of a plain string. | ||
| 310 | # | ||
| 311 | if ( $op->name eq "trans" ) { | ||
| 312 | my @shorts = unpack( "s256", $pv ); # assembler handles endianness | ||
| 313 | asm "op_pv_tr ", join( ",", @shorts ), "\n"; | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | asmf "newpv %s\nop_pv\n", pvstring($pv); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | sub B::BINOP::bytecode { | ||
| 321 | my $op = shift; | ||
| 322 | my $lastix = $op->last->objix unless $strip_syntree; | ||
| 323 | $op->B::UNOP::bytecode; | ||
| 324 | if ( ( $op->type || !$compress_nullops ) && !$strip_syntree ) { | ||
| 325 | asm "op_last $lastix\n"; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | sub B::LOOP::bytecode { | ||
| 330 | my $op = shift; | ||
| 331 | my $redoopix = $op->redoop->objix; | ||
| 332 | my $nextopix = $op->nextop->objix; | ||
| 333 | my $lastopix = $op->lastop->objix; | ||
| 334 | $op->B::LISTOP::bytecode; | ||
| 335 | asm "op_redoop $redoopix\nop_nextop $nextopix\nop_lastop $lastopix\n"; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub B::COP::bytecode { | ||
| 339 | my $op = shift; | ||
| 340 | my $file = $op->file; | ||
| 341 | my $line = $op->line; | ||
| 342 | if ($debug_bc) { # do this early to aid debugging | ||
| 343 | asmf "# line %s:%d\n", $file, $line; | ||
| 344 | } | ||
| 345 | my $stashpv = $op->stashpv; | ||
| 346 | my $warnings = $op->warnings; | ||
| 347 | my $warningsix = $warnings->objix; | ||
| 348 | my $labelix = pvix( $op->label ); | ||
| 349 | my $stashix = pvix($stashpv); | ||
| 350 | my $fileix = pvix($file); | ||
| 351 | $warnings->bytecode; | ||
| 352 | $op->B::OP::bytecode; | ||
| 353 | asmf <<"EOT", $labelix, $stashix, $op->cop_seq, $fileix, $op->arybase; | ||
| 354 | cop_label %d | ||
| 355 | cop_stashpv %d | ||
| 356 | cop_seq %d | ||
| 357 | cop_file %d | ||
| 358 | cop_arybase %d | ||
| 359 | cop_line $line | ||
| 360 | cop_warnings $warningsix | ||
| 361 | EOT | ||
| 362 | } | ||
| 363 | |||
| 364 | sub B::PMOP::bytecode { | ||
| 365 | my $op = shift; | ||
| 366 | my $replroot = $op->pmreplroot; | ||
| 367 | my $replrootix = $replroot->objix; | ||
| 368 | my $replstartix = $op->pmreplstart->objix; | ||
| 369 | my $opname = $op->name; | ||
| 370 | |||
| 371 | # pmnext is corrupt in some PMOPs (see misc.t for example) | ||
| 372 | #my $pmnextix = $op->pmnext->objix; | ||
| 373 | |||
| 374 | if ($$replroot) { | ||
| 375 | |||
| 376 | # OP_PUSHRE (a mutated version of OP_MATCH for the regexp | ||
| 377 | # argument to a split) stores a GV in op_pmreplroot instead | ||
| 378 | # of a substitution syntax tree. We don't want to walk that... | ||
| 379 | if ( $opname eq "pushre" ) { | ||
| 380 | $replroot->bytecode; | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | walkoptree( $replroot, "bytecode" ); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | $op->B::LISTOP::bytecode; | ||
| 387 | if ( $opname eq "pushre" ) { | ||
| 388 | asmf "op_pmreplrootgv $replrootix\n"; | ||
| 389 | } | ||
| 390 | else { | ||
| 391 | asm "op_pmreplroot $replrootix\nop_pmreplstart $replstartix\n"; | ||
| 392 | } | ||
| 393 | my $re = pvstring( $op->precomp ); | ||
| 394 | |||
| 395 | # op_pmnext omitted since a perl bug means it's sometime corrupt | ||
| 396 | asmf <<"EOT", $op->pmflags, $op->pmpermflags; | ||
| 397 | op_pmflags 0x%x | ||
| 398 | op_pmpermflags 0x%x | ||
| 399 | newpv $re | ||
| 400 | pregcomp | ||
| 401 | EOT | ||
| 402 | } | ||
| 403 | |||
| 404 | sub B::SV::bytecode { | ||
| 405 | my $sv = shift; | ||
| 406 | return if saved($sv); | ||
| 407 | my $ix = $sv->objix; | ||
| 408 | my $refcnt = $sv->REFCNT; | ||
| 409 | my $flags = sprintf( "0x%x", $sv->FLAGS ); | ||
| 410 | ldsv($ix); | ||
| 411 | asm "sv_refcnt $refcnt\nsv_flags $flags\n"; | ||
| 412 | mark_saved($sv); | ||
| 413 | } | ||
| 414 | |||
| 415 | sub B::PV::bytecode { | ||
| 416 | my $sv = shift; | ||
| 417 | return if saved($sv); | ||
| 418 | $sv->B::SV::bytecode; | ||
| 419 | asmf( "newpv %s\nxpv\n", pvstring( $sv->PV ) ) if $sv->FLAGS & POK; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub B::IV::bytecode { | ||
| 423 | my $sv = shift; | ||
| 424 | return if saved($sv); | ||
| 425 | my $iv = $sv->IVX; | ||
| 426 | $sv->B::SV::bytecode; | ||
| 427 | asmf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32" | ||
| 428 | if $sv->FLAGS & IOK; # could be PVNV | ||
| 429 | } | ||
| 430 | |||
| 431 | sub B::NV::bytecode { | ||
| 432 | my $sv = shift; | ||
| 433 | return if saved($sv); | ||
| 434 | $sv->B::SV::bytecode; | ||
| 435 | asmf "xnv %s\n", nv( $sv->NVX ); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub B::RV::bytecode { | ||
| 439 | my $sv = shift; | ||
| 440 | return if saved($sv); | ||
| 441 | my $rv = $sv->RV; | ||
| 442 | my $rvix = $rv->objix; | ||
| 443 | $rv->bytecode; | ||
| 444 | $sv->B::SV::bytecode; | ||
| 445 | asm "xrv $rvix\n"; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub B::PVIV::bytecode { | ||
| 449 | my $sv = shift; | ||
| 450 | return if saved($sv); | ||
| 451 | my $iv = $sv->IVX; | ||
| 452 | $sv->B::PV::bytecode; | ||
| 453 | asmf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32"; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub B::PVNV::bytecode { | ||
| 457 | my $sv = shift; | ||
| 458 | my $flag = shift || 0; | ||
| 459 | |||
| 460 | # The $flag argument is passed through PVMG::bytecode by BM::bytecode | ||
| 461 | # and AV::bytecode and indicates special handling. $flag = 1 is used by | ||
| 462 | # BM::bytecode and means that we should ensure we save the whole B-M | ||
| 463 | # table. It consists of 257 bytes (256 char array plus a final \0) | ||
| 464 | # which follow the ordinary PV+\0 and the 257 bytes are *not* reflected | ||
| 465 | # in SvCUR. $flag = 2 is used by AV::bytecode and means that we only | ||
| 466 | # call SV::bytecode instead of saving PV and calling NV::bytecode since | ||
| 467 | # PV/NV/IV stuff is different for AVs. | ||
| 468 | return if saved($sv); | ||
| 469 | if ( $flag == 2 ) { | ||
| 470 | $sv->B::SV::bytecode; | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | my $pv = $sv->PV; | ||
| 474 | $sv->B::IV::bytecode; | ||
| 475 | asmf "xnv %s\n", nv( $sv->NVX ); | ||
| 476 | if ( $flag == 1 ) { | ||
| 477 | $pv .= "\0" . $sv->TABLE; | ||
| 478 | asmf "newpv %s\npv_cur %d\nxpv\n", pvstring($pv), length($pv) - 257; | ||
| 479 | } | ||
| 480 | else { | ||
| 481 | asmf( "newpv %s\nxpv\n", pvstring($pv) ) if $sv->FLAGS & POK; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | sub B::PVMG::bytecode { | ||
| 487 | my ( $sv, $flag ) = @_; | ||
| 488 | |||
| 489 | # See B::PVNV::bytecode for an explanation of $flag. | ||
| 490 | return if saved($sv); | ||
| 491 | |||
| 492 | # XXX We assume SvSTASH is already saved and don't save it later ourselves | ||
| 493 | my $stashix = $sv->SvSTASH->objix; | ||
| 494 | my @mgchain = $sv->MAGIC; | ||
| 495 | my ( @mgobjix, $mg ); | ||
| 496 | |||
| 497 | # | ||
| 498 | # We need to traverse the magic chain and get objix for each OBJ | ||
| 499 | # field *before* we do B::PVNV::bytecode since objix overwrites | ||
| 500 | # the sv register. However, we need to write the magic-saving | ||
| 501 | # bytecode *after* B::PVNV::bytecode since sv isn't initialised | ||
| 502 | # to refer to $sv until then. | ||
| 503 | # | ||
| 504 | @mgobjix = map( $_->OBJ->objix, @mgchain ); | ||
| 505 | $sv->B::PVNV::bytecode($flag); | ||
| 506 | asm "xmg_stash $stashix\n"; | ||
| 507 | foreach $mg (@mgchain) { | ||
| 508 | asmf "sv_magic %s\nmg_obj %d\nnewpv %s\nmg_pv\n", | ||
| 509 | cstring( $mg->TYPE ), shift(@mgobjix), pvstring( $mg->PTR ); | ||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | sub B::PVLV::bytecode { | ||
| 514 | my $sv = shift; | ||
| 515 | return if saved($sv); | ||
| 516 | $sv->B::PVMG::bytecode; | ||
| 517 | asmf <<'EOT', $sv->TARGOFF, $sv->TARGLEN, cstring( $sv->TYPE ); | ||
| 518 | xlv_targoff %d | ||
| 519 | xlv_targlen %d | ||
| 520 | xlv_type %s | ||
| 521 | EOT | ||
| 522 | } | ||
| 523 | |||
| 524 | sub B::BM::bytecode { | ||
| 525 | my $sv = shift; | ||
| 526 | return if saved($sv); | ||
| 527 | |||
| 528 | # See PVNV::bytecode for an explanation of what the argument does | ||
| 529 | $sv->B::PVMG::bytecode(1); | ||
| 530 | asmf "xbm_useful %d\nxbm_previous %d\nxbm_rare %d\n", | ||
| 531 | $sv->USEFUL, $sv->PREVIOUS, $sv->RARE; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub empty_gv { # is a GV empty except for imported stuff? | ||
| 535 | my $gv = shift; | ||
| 536 | |||
| 537 | return 0 if ( $gv->SV->FLAGS & SVTYPEMASK ); # sv not SVt_NULL | ||
| 538 | my @subfield_names = qw(AV HV CV FORM IO); | ||
| 539 | @subfield_names = grep { | ||
| 540 | ; | ||
| 541 | no strict 'refs'; | ||
| 542 | !( $gv->GvFLAGS & ${ \"GVf_IMPORTED_$_" }->() ) && ${ $gv->$_() }; | ||
| 543 | } @subfield_names; | ||
| 544 | return scalar @subfield_names; | ||
| 545 | } | ||
| 546 | |||
| 547 | sub B::GV::bytecode { | ||
| 548 | my $gv = shift; | ||
| 549 | return if saved($gv); | ||
| 550 | return unless grep { $_ eq $gv->STASH->NAME; } @packages; | ||
| 551 | return if $gv->NAME =~ m/^\(/; # ignore overloads - they'll be rebuilt | ||
| 552 | my $ix = $gv->objix; | ||
| 553 | mark_saved($gv); | ||
| 554 | ldsv($ix); | ||
| 555 | asmf <<"EOT", $gv->FLAGS, $gv->GvFLAGS; | ||
| 556 | sv_flags 0x%x | ||
| 557 | xgv_flags 0x%x | ||
| 558 | EOT | ||
| 559 | my $refcnt = $gv->REFCNT; | ||
| 560 | asmf( "sv_refcnt_add %d\n", $refcnt - 1 ) if $refcnt > 1; | ||
| 561 | return if $gv->is_empty; | ||
| 562 | asmf <<"EOT", $gv->LINE, pvix( $gv->FILE ); | ||
| 563 | gp_line %d | ||
| 564 | gp_file %d | ||
| 565 | EOT | ||
| 566 | my $gvname = $gv->NAME; | ||
| 567 | my $name = cstring( $gv->STASH->NAME . "::" . $gvname ); | ||
| 568 | my $egv = $gv->EGV; | ||
| 569 | my $egvix = $egv->objix; | ||
| 570 | my $gvrefcnt = $gv->GvREFCNT; | ||
| 571 | asmf( "gp_refcnt_add %d\n", $gvrefcnt - 1 ) if $gvrefcnt > 1; | ||
| 572 | |||
| 573 | if ( $gvrefcnt > 1 && $ix != $egvix ) { | ||
| 574 | asm "gp_share $egvix\n"; | ||
| 575 | } | ||
| 576 | else { | ||
| 577 | if ( $gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/ ) { | ||
| 578 | my $i; | ||
| 579 | my @subfield_names = qw(SV AV HV CV FORM IO); | ||
| 580 | @subfield_names = grep { | ||
| 581 | ; | ||
| 582 | no strict 'refs'; | ||
| 583 | !( $gv->GvFLAGS & ${ \"GVf_IMPORTED_$_" }->() ); | ||
| 584 | } @subfield_names; | ||
| 585 | my @subfields = map( $gv->$_(), @subfield_names ); | ||
| 586 | my @ixes = map( $_->objix, @subfields ); | ||
| 587 | |||
| 588 | # Reset sv register for $gv | ||
| 589 | ldsv($ix); | ||
| 590 | for ( $i = 0 ; $i < @ixes ; $i++ ) { | ||
| 591 | asmf "gp_%s %d\n", lc( $subfield_names[$i] ), $ixes[$i]; | ||
| 592 | } | ||
| 593 | |||
| 594 | # Now save all the subfields | ||
| 595 | my $sv; | ||
| 596 | foreach $sv (@subfields) { | ||
| 597 | $sv->bytecode; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | sub B::HV::bytecode { | ||
| 604 | my $hv = shift; | ||
| 605 | return if saved($hv); | ||
| 606 | mark_saved($hv); | ||
| 607 | my $name = $hv->NAME; | ||
| 608 | my $ix = $hv->objix; | ||
| 609 | if ( !$name ) { | ||
| 610 | |||
| 611 | # It's an ordinary HV. Stashes have NAME set and need no further | ||
| 612 | # saving beyond the gv_stashpv that $hv->objix already ensures. | ||
| 613 | my @contents = $hv->ARRAY; | ||
| 614 | my ( $i, @ixes ); | ||
| 615 | for ( $i = 1 ; $i < @contents ; $i += 2 ) { | ||
| 616 | push( @ixes, $contents[$i]->objix ); | ||
| 617 | } | ||
| 618 | for ( $i = 1 ; $i < @contents ; $i += 2 ) { | ||
| 619 | $contents[$i]->bytecode; | ||
| 620 | } | ||
| 621 | ldsv($ix); | ||
| 622 | for ( $i = 0 ; $i < @contents ; $i += 2 ) { | ||
| 623 | asmf( | ||
| 624 | "newpv %s\nhv_store %d\n", | ||
| 625 | pvstring( $contents[$i] ), | ||
| 626 | $ixes[ $i / 2 ] | ||
| 627 | ); | ||
| 628 | } | ||
| 629 | asmf "sv_refcnt %d\nsv_flags 0x%x\n", $hv->REFCNT, $hv->FLAGS; | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | sub B::AV::bytecode { | ||
| 634 | my $av = shift; | ||
| 635 | return if saved($av); | ||
| 636 | my $ix = $av->objix; | ||
| 637 | my $fill = $av->FILL; | ||
| 638 | my $max = $av->MAX; | ||
| 639 | my ( @array, @ixes ); | ||
| 640 | if ( $fill > -1 ) { | ||
| 641 | @array = $av->ARRAY; | ||
| 642 | @ixes = map( $_->objix, @array ); | ||
| 643 | my $sv; | ||
| 644 | foreach $sv (@array) { | ||
| 645 | $sv->bytecode; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | # See PVNV::bytecode for the meaning of the flag argument of 2. | ||
| 650 | $av->B::PVMG::bytecode(2); | ||
| 651 | |||
| 652 | # Recover sv register and set AvMAX and AvFILL to -1 (since we | ||
| 653 | # create an AV with NEWSV and SvUPGRADE rather than doing newAV | ||
| 654 | # which is what sets AvMAX and AvFILL. | ||
| 655 | ldsv($ix); | ||
| 656 | asmf "sv_flags 0x%x\n", | ||
| 657 | $av->FLAGS & ~SVf_READONLY; # SvREADONLY_off($av) in case PADCONST | ||
| 658 | asmf "xav_flags 0x%x\nxav_max -1\nxav_fill -1\n", $av->AvFLAGS; | ||
| 659 | if ( $fill > -1 ) { | ||
| 660 | my $elix; | ||
| 661 | foreach $elix (@ixes) { | ||
| 662 | asm "av_push $elix\n"; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | else { | ||
| 666 | if ( $max > -1 ) { | ||
| 667 | asm "av_extend $max\n"; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | asmf "sv_flags 0x%x\n", $av->FLAGS; # restore flags from above | ||
| 671 | } | ||
| 672 | |||
| 673 | sub B::CV::bytecode { | ||
| 674 | my $cv = shift; | ||
| 675 | return if saved($cv); | ||
| 676 | return if ${ $cv->GV } && ( $cv->GV->GvFLAGS & GVf_IMPORTED_CV ); | ||
| 677 | my $fileix = pvix( $cv->FILE ); | ||
| 678 | my $ix = $cv->objix; | ||
| 679 | $cv->B::PVMG::bytecode; | ||
| 680 | my $i; | ||
| 681 | my @subfield_names = qw(ROOT START STASH GV PADLIST OUTSIDE); | ||
| 682 | my @subfields = map( $cv->$_(), @subfield_names ); | ||
| 683 | my @ixes = map( $_->objix, @subfields ); | ||
| 684 | |||
| 685 | # Save OP tree from CvROOT (first element of @subfields) | ||
| 686 | my $root = shift @subfields; | ||
| 687 | if ($$root) { | ||
| 688 | walkoptree( $root, "bytecode" ); | ||
| 689 | } | ||
| 690 | |||
| 691 | # Reset sv register for $cv (since above ->objix calls stomped on it) | ||
| 692 | ldsv($ix); | ||
| 693 | for ( $i = 0 ; $i < @ixes ; $i++ ) { | ||
| 694 | asmf "xcv_%s %d\n", lc( $subfield_names[$i] ), $ixes[$i]; | ||
| 695 | } | ||
| 696 | asmf "xcv_depth %d\nxcv_flags 0x%x\n", $cv->DEPTH, $cv->CvFLAGS; | ||
| 697 | asmf "xcv_file %d\n", $fileix; | ||
| 698 | |||
| 699 | # Now save all the subfields (except for CvROOT which was handled | ||
| 700 | # above) and CvSTART (now the initial element of @subfields). | ||
| 701 | shift @subfields; # bye-bye CvSTART | ||
| 702 | my $sv; | ||
| 703 | foreach $sv (@subfields) { | ||
| 704 | $sv->bytecode; | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | sub B::IO::bytecode { | ||
| 709 | my $io = shift; | ||
| 710 | return if saved($io); | ||
| 711 | my $ix = $io->objix; | ||
| 712 | my $top_gv = $io->TOP_GV; | ||
| 713 | my $top_gvix = $top_gv->objix; | ||
| 714 | my $fmt_gv = $io->FMT_GV; | ||
| 715 | my $fmt_gvix = $fmt_gv->objix; | ||
| 716 | my $bottom_gv = $io->BOTTOM_GV; | ||
| 717 | my $bottom_gvix = $bottom_gv->objix; | ||
| 718 | |||
| 719 | $io->B::PVMG::bytecode; | ||
| 720 | ldsv($ix); | ||
| 721 | asm "xio_top_gv $top_gvix\n"; | ||
| 722 | asm "xio_fmt_gv $fmt_gvix\n"; | ||
| 723 | asm "xio_bottom_gv $bottom_gvix\n"; | ||
| 724 | my $field; | ||
| 725 | foreach $field (qw(TOP_NAME FMT_NAME BOTTOM_NAME)) { | ||
| 726 | asmf "newpv %s\nxio_%s\n", pvstring( $io->$field() ), lc($field); | ||
| 727 | } | ||
| 728 | foreach $field (qw(LINES PAGE PAGE_LEN LINES_LEFT SUBPROCESS)) { | ||
| 729 | asmf "xio_%s %d\n", lc($field), $io->$field(); | ||
| 730 | } | ||
| 731 | asmf "xio_type %s\nxio_flags 0x%x\n", cstring( $io->IoTYPE ), $io->IoFLAGS; | ||
| 732 | $top_gv->bytecode; | ||
| 733 | $fmt_gv->bytecode; | ||
| 734 | $bottom_gv->bytecode; | ||
| 735 | } | ||
| 736 | |||
| 737 | sub B::SPECIAL::bytecode { | ||
| 738 | |||
| 739 | # nothing extra needs doing | ||
| 740 | } | ||
| 741 | |||
| 742 | sub bytecompile_object { | ||
| 743 | for my $sv (@_) { | ||
| 744 | svref_2object($sv)->bytecode; | ||
| 745 | } | ||
| 746 | } | ||
| 747 | |||
| 748 | sub B::GV::bytecodecv { | ||
| 749 | my $gv = shift; | ||
| 750 | my $cv = $gv->CV; | ||
| 751 | if ( $$cv && !saved($cv) && !( $gv->FLAGS & GVf_IMPORTED_CV ) ) { | ||
| 752 | if ($debug_cv) { | ||
| 753 | warn sprintf( "saving extra CV &%s::%s (0x%x) from GV 0x%x\n", | ||
| 754 | $gv->STASH->NAME, $gv->NAME, $$cv, $$gv ); | ||
| 755 | } | ||
| 756 | $gv->bytecode; | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | sub save_call_queues { | ||
| 761 | if ( begin_av()->isa("B::AV") ) { # this is just to save 'use Foo;' calls | ||
| 762 | for my $cv ( begin_av()->ARRAY ) { | ||
| 763 | next unless grep { $_ eq $cv->STASH->NAME; } @packages; | ||
| 764 | my $op = $cv->START; | ||
| 765 | OPLOOP: | ||
| 766 | while ($$op) { | ||
| 767 | if ( $op->name eq 'require' ) { # save any BEGIN that does a require | ||
| 768 | $cv->bytecode; | ||
| 769 | asmf "push_begin %d\n", $cv->objix; | ||
| 770 | last OPLOOP; | ||
| 771 | } | ||
| 772 | $op = $op->next; | ||
| 773 | } | ||
| 774 | } | ||
| 775 | } | ||
| 776 | if ( init_av()->isa("B::AV") ) { | ||
| 777 | for my $cv ( init_av()->ARRAY ) { | ||
| 778 | next unless grep { $_ eq $cv->STASH->NAME; } @packages; | ||
| 779 | $cv->bytecode; | ||
| 780 | asmf "push_init %d\n", $cv->objix; | ||
| 781 | } | ||
| 782 | } | ||
| 783 | if ( end_av()->isa("B::AV") ) { | ||
| 784 | for my $cv ( end_av()->ARRAY ) { | ||
| 785 | next unless grep { $_ eq $cv->STASH->NAME; } @packages; | ||
| 786 | $cv->bytecode; | ||
| 787 | asmf "push_end %d\n", $cv->objix; | ||
| 788 | } | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | sub symwalk { | ||
| 793 | no strict 'refs'; | ||
| 794 | my $ok = 1 | ||
| 795 | if grep { ( my $name = $_[0] ) =~ s/::$//; $_ eq $name; } @packages; | ||
| 796 | if ( grep { /^$_[0]/; } @packages ) { | ||
| 797 | walksymtable( \%{"$_[0]"}, "bytecodecv", \&symwalk, $_[0] ); | ||
| 798 | } | ||
| 799 | warn "considering $_[0] ... " . ( $ok ? "accepted\n" : "rejected\n" ) | ||
| 800 | if $debug_bc; | ||
| 801 | $ok; | ||
| 802 | } | ||
| 803 | |||
| 804 | sub bytecompile_main { | ||
| 805 | my $curpad = ( comppadlist->ARRAY )[1]; | ||
| 806 | my $curpadix = $curpad->objix; | ||
| 807 | $curpad->bytecode; | ||
| 808 | save_call_queues(); | ||
| 809 | walkoptree( main_root, "bytecode" ) unless ref(main_root) eq "B::NULL"; | ||
| 810 | warn "done main program, now walking symbol table\n" if $debug_bc; | ||
| 811 | if (@packages) { | ||
| 812 | no strict qw(refs); | ||
| 813 | walksymtable( \%{"main::"}, "bytecodecv", \&symwalk ); | ||
| 814 | } | ||
| 815 | else { | ||
| 816 | die "No packages requested for compilation!\n"; | ||
| 817 | } | ||
| 818 | asmf "main_root %d\n", main_root->objix; | ||
| 819 | asmf "main_start %d\n", main_start->objix; | ||
| 820 | asmf "curpad $curpadix\n"; | ||
| 821 | |||
| 822 | # XXX Do min_intro_pending and max_intro_pending matter? | ||
| 823 | } | ||
| 824 | |||
| 825 | sub compile { | ||
| 826 | my @options = @_; | ||
| 827 | my ( $option, $opt, $arg ); | ||
| 828 | open( OUT, ">&STDOUT" ); | ||
| 829 | binmode OUT; | ||
| 830 | select OUT; | ||
| 831 | OPTION: | ||
| 832 | while ( $option = shift @options ) { | ||
| 833 | if ( $option =~ /^-(.)(.*)/ ) { | ||
| 834 | $opt = $1; | ||
| 835 | $arg = $2; | ||
| 836 | } | ||
| 837 | else { | ||
| 838 | unshift @options, $option; | ||
| 839 | last OPTION; | ||
| 840 | } | ||
| 841 | if ( $opt eq "-" && $arg eq "-" ) { | ||
| 842 | shift @options; | ||
| 843 | last OPTION; | ||
| 844 | } | ||
| 845 | elsif ( $opt eq "o" ) { | ||
| 846 | $arg ||= shift @options; | ||
| 847 | open( OUT, ">$arg" ) or return "$arg: $!\n"; | ||
| 848 | binmode OUT; | ||
| 849 | } | ||
| 850 | elsif ( $opt eq "a" ) { | ||
| 851 | $arg ||= shift @options; | ||
| 852 | open( OUT, ">>$arg" ) or return "$arg: $!\n"; | ||
| 853 | binmode OUT; | ||
| 854 | } | ||
| 855 | elsif ( $opt eq "D" ) { | ||
| 856 | $arg ||= shift @options; | ||
| 857 | foreach $arg ( split( //, $arg ) ) { | ||
| 858 | if ( $arg eq "b" ) { | ||
| 859 | $| = 1; | ||
| 860 | debug(1); | ||
| 861 | } | ||
| 862 | elsif ( $arg eq "o" ) { | ||
| 863 | B->debug(1); | ||
| 864 | } | ||
| 865 | elsif ( $arg eq "a" ) { | ||
| 866 | B::Assembler::debug(1); | ||
| 867 | } | ||
| 868 | elsif ( $arg eq "C" ) { | ||
| 869 | $debug_cv = 1; | ||
| 870 | } | ||
| 871 | } | ||
| 872 | } | ||
| 873 | elsif ( $opt eq "v" ) { | ||
| 874 | $verbose = 1; | ||
| 875 | } | ||
| 876 | elsif ( $opt eq "S" ) { | ||
| 877 | $no_assemble = 1; | ||
| 878 | } | ||
| 879 | elsif ( $opt eq "f" ) { | ||
| 880 | $arg ||= shift @options; | ||
| 881 | my $value = $arg !~ s/^no-//; | ||
| 882 | $arg =~ s/-/_/g; | ||
| 883 | my $ref = $optimise{$arg}; | ||
| 884 | if ( defined($ref) ) { | ||
| 885 | $$ref = $value; | ||
| 886 | } | ||
| 887 | else { | ||
| 888 | warn qq(ignoring unknown optimisation option "$arg"\n); | ||
| 889 | } | ||
| 890 | } | ||
| 891 | elsif ( $opt eq "O" ) { | ||
| 892 | $arg = 1 if $arg eq ""; | ||
| 893 | my $ref; | ||
| 894 | foreach $ref ( values %optimise ) { | ||
| 895 | $$ref = 0; | ||
| 896 | } | ||
| 897 | if ( $arg >= 2 ) { | ||
| 898 | $bypass_nullops = 1; | ||
| 899 | } | ||
| 900 | if ( $arg >= 1 ) { | ||
| 901 | $compress_nullops = 1; | ||
| 902 | $omit_seq = 1; | ||
| 903 | } | ||
| 904 | } | ||
| 905 | elsif ( $opt eq "u" ) { | ||
| 906 | $arg ||= shift @options; | ||
| 907 | push @packages, $arg; | ||
| 908 | } | ||
| 909 | else { | ||
| 910 | warn qq(ignoring unknown option "$opt$arg"\n); | ||
| 911 | } | ||
| 912 | } | ||
| 913 | if ( !@packages ) { | ||
| 914 | warn "No package specified for compilation, assuming main::\n"; | ||
| 915 | @packages = qw(main); | ||
| 916 | } | ||
| 917 | if (@options) { | ||
| 918 | die "Extraneous options left on B::Bytecode commandline: @options\n"; | ||
| 919 | } | ||
| 920 | else { | ||
| 921 | return sub { | ||
| 922 | newasm( \&apr ) unless $no_assemble; | ||
| 923 | bytecompile_main(); | ||
| 924 | endasm() unless $no_assemble; | ||
| 925 | }; | ||
| 926 | } | ||
| 927 | } | ||
| 928 | |||
| 929 | sub apr { print @_; } | ||
| 930 | |||
| 931 | 1; | ||
| 932 | |||
| 933 | __END__ |