This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't write bytecode to load the NVX of AVs and HVs
[perl5.git] / ext / B / B / Bytecode.pm
CommitLineData
1df34986
AE
1# B::Bytecode.pm
2# Copyright (c) 2003 Enache Adrian. All rights reserved.
3# This module is free software; you can redistribute and/or modify
4# it under the same terms as Perl itself.
5
6# Based on the original Bytecode.pm module written by Malcolm Beattie.
059a8bb7 7
1df34986 8package B::Bytecode;
28b605d8 9
eaade9ad
JH
10our $VERSION = '1.01';
11
a798dbf2 12use strict;
1df34986
AE
13use Config;
14use B qw(class main_cv main_root main_start cstring comppadlist
15 defstash curstash begin_av init_av end_av inc_gv warnhook diehook
16 dowarn SVt_PVGV SVt_PVHV OPf_SPECIAL OPf_STACKED OPf_MOD
17 OPpLVAL_INTRO SVf_FAKE SVf_READONLY);
18use B::Asmdata qw(@specialsv_name);
19use B::Assembler qw(asm newasm endasm);
1df34986
AE
20
21#################################################
22
f66c782a 23my ($varix, $opix, $savebegins, %walked, %files, @cloop);
1df34986
AE
24my %strtab = (0,0);
25my %svtab = (0,0);
26my %optab = (0,0);
27my %spectab = (0,0);
1df34986
AE
28my $tix = 1;
29sub asm;
30sub nice ($) { }
f66c782a
JH
31
32BEGIN {
33 my $ithreads = $Config{'useithreads'} eq 'define';
34 eval qq{
35 sub ITHREADS() { $ithreads }
36 sub VERSION() { $] }
37 }; die $@ if $@;
38}
1df34986
AE
39
40#################################################
059a8bb7 41
1df34986
AE
42sub pvstring {
43 my $pv = shift;
44 defined($pv) ? cstring ($pv."\0") : "\"\"";
059a8bb7 45}
a798dbf2 46
1df34986
AE
47sub pvix {
48 my $str = pvstring shift;
49 my $ix = $strtab{$str};
50 defined($ix) ? $ix : do {
51 asm "newpv", $str;
52 asm "stpv", $strtab{$str} = $tix;
53 $tix++;
a798dbf2
MB
54 }
55}
56
1df34986
AE
57sub B::OP::ix {
58 my $op = shift;
59 my $ix = $optab{$$op};
60 defined($ix) ? $ix : do {
f66c782a 61 nice "[".$op->name." $tix]";
566ece03
JH
62 asm "newopx", $op->size | $op->type <<7;
63 $optab{$$op} = $opix = $ix = $tix++;
1df34986
AE
64 $op->bsave($ix);
65 $ix;
a798dbf2
MB
66 }
67}
68
1df34986
AE
69sub B::SPECIAL::ix {
70 my $spec = shift;
71 my $ix = $spectab{$$spec};
72 defined($ix) ? $ix : do {
73 nice '['.$specialsv_name[$$spec].']';
566ece03
JH
74 asm "ldspecsvx", $$spec;
75 $spectab{$$spec} = $varix = $tix++;
1df34986 76 }
a798dbf2
MB
77}
78
1df34986
AE
79sub B::SV::ix {
80 my $sv = shift;
81 my $ix = $svtab{$$sv};
82 defined($ix) ? $ix : do {
83 nice '['.class($sv).']';
566ece03
JH
84 asm "newsvx", $sv->FLAGS;
85 $svtab{$$sv} = $varix = $ix = $tix++;
1df34986
AE
86 $sv->bsave($ix);
87 $ix;
88 }
89}
90
91sub B::GV::ix {
92 my ($gv,$desired) = @_;
93 my $ix = $svtab{$$gv};
94 defined($ix) ? $ix : do {
95 if ($gv->GP) {
96 my ($svix, $avix, $hvix, $cvix, $ioix, $formix);
97 nice "[GV]";
98 my $name = $gv->STASH->NAME . "::" . $gv->NAME;
566ece03
JH
99 asm "gv_fetchpvx", cstring $name;
100 $svtab{$$gv} = $varix = $ix = $tix++;
1df34986
AE
101 asm "sv_flags", $gv->FLAGS;
102 asm "sv_refcnt", $gv->REFCNT;
103 asm "xgv_flags", $gv->GvFLAGS;
104
105 asm "gp_refcnt", $gv->GvREFCNT;
106 asm "load_glob", $ix if $name eq "CORE::GLOBAL::glob";
107 return $ix
108 unless $desired || desired $gv;
109 $svix = $gv->SV->ix;
110 $avix = $gv->AV->ix;
111 $hvix = $gv->HV->ix;
112
566ece03 113 # XXX {{{{
1df34986
AE
114 my $cv = $gv->CV;
115 $cvix = $$cv && defined $files{$cv->FILE} ? $cv->ix : 0;
116 my $form = $gv->FORM;
117 $formix = $$form && defined $files{$form->FILE} ? $form->ix : 0;
118
566ece03
JH
119 $ioix = $name !~ /STDOUT$/ ? $gv->IO->ix : 0;
120 # }}}} XXX
1df34986
AE
121
122 nice "-GV-",
123 asm "ldsv", $varix = $ix unless $ix == $varix;
124 asm "gp_sv", $svix;
125 asm "gp_av", $avix;
126 asm "gp_hv", $hvix;
127 asm "gp_cv", $cvix;
128 asm "gp_io", $ioix;
129 asm "gp_cvgen", $gv->CVGEN;
130 asm "gp_form", $formix;
131 asm "gp_file", pvix $gv->FILE;
132 asm "gp_line", $gv->LINE;
133 asm "formfeed", $svix if $name eq "main::\cL";
134 } else {
135 nice "[GV]";
566ece03
JH
136 asm "newsvx", $gv->FLAGS;
137 $svtab{$$gv} = $varix = $ix = $tix++;
1df34986
AE
138 my $stashix = $gv->STASH->ix;
139 $gv->B::PVMG::bsave($ix);
140 asm "xgv_flags", $gv->GvFLAGS;
141 asm "xgv_stash", $stashix;
142 }
143 $ix;
144 }
a798dbf2
MB
145}
146
1df34986
AE
147sub B::HV::ix {
148 my $hv = shift;
149 my $ix = $svtab{$$hv};
150 defined($ix) ? $ix : do {
151 my ($ix,$i,@array);
152 my $name = $hv->NAME;
153 if ($name) {
154 nice "[STASH]";
566ece03
JH
155 asm "gv_stashpvx", cstring $name;
156 asm "sv_flags", $hv->FLAGS;
157 $svtab{$$hv} = $varix = $ix = $tix++;
1df34986
AE
158 asm "xhv_name", pvix $name;
159 # my $pmrootix = $hv->PMROOT->ix; # XXX
160 asm "ldsv", $varix = $ix unless $ix == $varix;
161 # asm "xhv_pmroot", $pmrootix; # XXX
162 } else {
163 nice "[HV]";
566ece03
JH
164 asm "newsvx", $hv->FLAGS;
165 $svtab{$$hv} = $varix = $ix = $tix++;
1df34986
AE
166 my $stashix = $hv->SvSTASH->ix;
167 for (@array = $hv->ARRAY) {
168 next if $i = not $i;
169 $_ = $_->ix;
170 }
171 nice "-HV-",
172 asm "ldsv", $varix = $ix unless $ix == $varix;
173 ($i = not $i) ? asm ("newpv", pvstring $_) : asm("hv_store", $_)
174 for @array;
1df34986 175 asm "xmg_stash", $stashix;
6a1bf161 176 asm "xhv_riter", $hv->RITER;
1df34986
AE
177 }
178 asm "sv_refcnt", $hv->REFCNT;
1df34986 179 $ix;
a798dbf2
MB
180 }
181}
182
1df34986
AE
183sub B::NULL::ix {
184 my $sv = shift;
185 $$sv ? $sv->B::SV::ix : 0;
059a8bb7
JH
186}
187
1df34986 188sub B::NULL::opwalk { 0 }
a798dbf2 189
1df34986 190#################################################
a798dbf2 191
1df34986
AE
192sub B::NULL::bsave {
193 my ($sv,$ix) = @_;
059a8bb7 194
1df34986
AE
195 nice '-'.class($sv).'-',
196 asm "ldsv", $varix = $ix unless $ix == $varix;
197 asm "sv_refcnt", $sv->REFCNT;
a798dbf2
MB
198}
199
1df34986
AE
200sub B::SV::bsave;
201 *B::SV::bsave = *B::NULL::bsave;
a798dbf2 202
1df34986
AE
203sub B::RV::bsave {
204 my ($sv,$ix) = @_;
205 my $rvix = $sv->RV->ix;
206 $sv->B::NULL::bsave($ix);
207 asm "xrv", $rvix;
a798dbf2
MB
208}
209
1df34986
AE
210sub B::PV::bsave {
211 my ($sv,$ix) = @_;
212 $sv->B::NULL::bsave($ix);
213 asm "newpv", pvstring $sv->PVBM;
214 asm "xpv";
a798dbf2
MB
215}
216
1df34986
AE
217sub B::IV::bsave {
218 my ($sv,$ix) = @_;
219 $sv->B::NULL::bsave($ix);
220 asm "xiv", $sv->IVX;
a798dbf2
MB
221}
222
1df34986
AE
223sub B::NV::bsave {
224 my ($sv,$ix) = @_;
225 $sv->B::NULL::bsave($ix);
226 asm "xnv", sprintf "%.40g", $sv->NVX;
a798dbf2
MB
227}
228
1df34986
AE
229sub B::PVIV::bsave {
230 my ($sv,$ix) = @_;
231 $sv->POK ?
232 $sv->B::PV::bsave($ix):
233 $sv->ROK ?
234 $sv->B::RV::bsave($ix):
235 $sv->B::NULL::bsave($ix);
f66c782a 236 asm "xiv", !ITHREADS && $sv->FLAGS & (SVf_FAKE|SVf_READONLY) ?
1df34986 237 "0 but true" : $sv->IVX;
a798dbf2
MB
238}
239
1df34986
AE
240sub B::PVNV::bsave {
241 my ($sv,$ix) = @_;
242 $sv->B::PVIV::bsave($ix);
fefd4cb9
NC
243 # Magical AVs end up here, but AVs now don't have an NV slot actually
244 # allocated. Hence don't write out assembly to store the NV slot if we're
245 # actually an array.
246 return if $sv->isa('B::AV');
247 # Likewise HVs have no NV slot actually allocated.
248 # I don't think that they can get here, but better safe than sorry
249 return if $sv->isa('B::HV');
1df34986 250 asm "xnv", sprintf "%.40g", $sv->NVX;
a798dbf2
MB
251}
252
1df34986
AE
253sub B::PVMG::domagic {
254 my ($sv,$ix) = @_;
255 nice '-MAGICAL-';
256 my @mglist = $sv->MAGIC;
257 my (@mgix, @namix);
258 for (@mglist) {
259 push @mgix, $_->OBJ->ix;
260 push @namix, $_->PTR->ix if $_->LENGTH == B::HEf_SVKEY;
a798dbf2 261 }
a798dbf2 262
1df34986
AE
263 nice '-'.class($sv).'-',
264 asm "ldsv", $varix = $ix unless $ix == $varix;
265 for (@mglist) {
266 asm "sv_magic", cstring $_->TYPE;
267 asm "mg_obj", shift @mgix;
268 my $length = $_->LENGTH;
269 if ($length == B::HEf_SVKEY) {
270 asm "mg_namex", shift @namix;
271 } elsif ($length) {
272 asm "newpv", pvstring $_->PTR;
273 asm "mg_name";
274 }
a798dbf2
MB
275 }
276}
277
1df34986
AE
278sub B::PVMG::bsave {
279 my ($sv,$ix) = @_;
280 my $stashix = $sv->SvSTASH->ix;
281 $sv->B::PVNV::bsave($ix);
282 asm "xmg_stash", $stashix;
283 $sv->domagic($ix) if $sv->MAGICAL;
284}
285
286sub B::PVLV::bsave {
287 my ($sv,$ix) = @_;
288 my $targix = $sv->TARG->ix;
289 $sv->B::PVMG::bsave($ix);
290 asm "xlv_targ", $targix;
291 asm "xlv_targoff", $sv->TARGOFF;
292 asm "xlv_targlen", $sv->TARGLEN;
293 asm "xlv_type", $sv->TYPE;
294
295}
296
297sub B::BM::bsave {
298 my ($sv,$ix) = @_;
299 $sv->B::PVMG::bsave($ix);
300 asm "xpv_cur", $sv->CUR;
301 asm "xbm_useful", $sv->USEFUL;
302 asm "xbm_previous", $sv->PREVIOUS;
303 asm "xbm_rare", $sv->RARE;
304}
305
306sub B::IO::bsave {
307 my ($io,$ix) = @_;
308 my $topix = $io->TOP_GV->ix;
309 my $fmtix = $io->FMT_GV->ix;
310 my $bottomix = $io->BOTTOM_GV->ix;
311 $io->B::PVMG::bsave($ix);
312 asm "xio_lines", $io->LINES;
313 asm "xio_page", $io->PAGE;
314 asm "xio_page_len", $io->PAGE_LEN;
315 asm "xio_lines_left", $io->LINES_LEFT;
316 asm "xio_top_name", pvix $io->TOP_NAME;
317 asm "xio_top_gv", $topix;
318 asm "xio_fmt_name", pvix $io->FMT_NAME;
319 asm "xio_fmt_gv", $fmtix;
320 asm "xio_bottom_name", pvix $io->BOTTOM_NAME;
321 asm "xio_bottom_gv", $bottomix;
322 asm "xio_subprocess", $io->SUBPROCESS;
323 asm "xio_type", ord $io->IoTYPE;
324 # asm "xio_flags", ord($io->IoFLAGS) & ~32; # XXX XXX
325}
326
327sub B::CV::bsave {
328 my ($cv,$ix) = @_;
329 my $stashix = $cv->STASH->ix;
1df34986
AE
330 my $gvix = $cv->GV->ix;
331 my $padlistix = $cv->PADLIST->ix;
332 my $outsideix = $cv->OUTSIDE->ix;
333 my $constix = $cv->CONST ? $cv->XSUBANY->ix : 0;
566ece03
JH
334 my $startix = $cv->START->opwalk;
335 my $rootix = $cv->ROOT->ix;
1df34986
AE
336
337 $cv->B::PVMG::bsave($ix);
338 asm "xcv_stash", $stashix;
339 asm "xcv_start", $startix;
340 asm "xcv_root", $rootix;
341 asm "xcv_xsubany", $constix;
342 asm "xcv_gv", $gvix;
343 asm "xcv_file", pvix $cv->FILE if $cv->FILE; # XXX AD
344 asm "xcv_padlist", $padlistix;
345 asm "xcv_outside", $outsideix;
346 asm "xcv_flags", $cv->CvFLAGS;
347 asm "xcv_outside_seq", $cv->OUTSIDE_SEQ;
348 asm "xcv_depth", $cv->DEPTH;
349}
350
351sub B::FM::bsave {
352 my ($form,$ix) = @_;
353
354 $form->B::CV::bsave($ix);
355 asm "xfm_lines", $form->LINES;
356}
357
358sub B::AV::bsave {
359 my ($av,$ix) = @_;
360 return $av->B::PVMG::bsave($ix) if $av->MAGICAL;
361 my @array = $av->ARRAY;
362 $_ = $_->ix for @array;
363 my $stashix = $av->SvSTASH->ix;
364
365 nice "-AV-",
366 asm "ldsv", $varix = $ix unless $ix == $varix;
ecd04c98 367 asm "av_extend", $av->MAX if $av->MAX >= 0;
1df34986
AE
368 asm "av_pushx", $_ for @array;
369 asm "sv_refcnt", $av->REFCNT;
1df34986
AE
370 asm "xmg_stash", $stashix;
371}
372
373sub B::GV::desired {
374 my $gv = shift;
375 my ($cv, $form);
376 $files{$gv->FILE} && $gv->LINE
377 || ${$cv = $gv->CV} && $files{$cv->FILE}
378 || ${$form = $gv->FORM} && $files{$form->FILE}
a798dbf2
MB
379}
380
1df34986
AE
381sub B::HV::bwalk {
382 my $hv = shift;
383 return if $walked{$$hv}++;
384 my %stash = $hv->ARRAY;
385 while (my($k,$v) = each %stash) {
386 if ($v->SvTYPE == SVt_PVGV) {
387 my $hash = $v->HV;
388 if ($$hash && $hash->NAME) {
389 $hash->bwalk;
390 }
391 $v->ix(1) if desired $v;
392 } else {
393 nice "[prototype]";
566ece03
JH
394 asm "gv_fetchpvx", cstring $hv->NAME . "::$k";
395 $svtab{$$v} = $varix = $tix;
1df34986 396 $v->bsave($tix++);
566ece03 397 asm "sv_flags", $v->FLAGS;
1df34986
AE
398 }
399 }
a798dbf2
MB
400}
401
1df34986 402######################################################
a798dbf2 403
a798dbf2 404
1df34986
AE
405sub B::OP::bsave_thin {
406 my ($op, $ix) = @_;
407 my $next = $op->next;
408 my $nextix = $optab{$$next};
409 $nextix = 0, push @cloop, $op unless defined $nextix;
410 if ($ix != $opix) {
411 nice '-'.$op->name.'-',
412 asm "ldop", $opix = $ix;
a798dbf2 413 }
1df34986
AE
414 asm "op_next", $nextix;
415 asm "op_targ", $op->targ if $op->type; # tricky
416 asm "op_flags", $op->flags;
417 asm "op_private", $op->private;
a798dbf2
MB
418}
419
1df34986
AE
420sub B::OP::bsave;
421 *B::OP::bsave = *B::OP::bsave_thin;
a798dbf2 422
1df34986
AE
423sub B::UNOP::bsave {
424 my ($op, $ix) = @_;
425 my $name = $op->name;
426 my $flags = $op->flags;
427 my $first = $op->first;
428 my $firstix =
429 $name =~ /fl[io]p/
430 # that's just neat
f66c782a 431 || (!ITHREADS && $name eq 'regcomp')
1df34986
AE
432 # trick for /$a/o in pp_regcomp
433 || $name eq 'rv2sv'
434 && $op->flags & OPf_MOD
435 && $op->private & OPpLVAL_INTRO
436 # change #18774 made my life hard
437 ? $first->ix
438 : 0;
439
440 $op->B::OP::bsave($ix);
441 asm "op_first", $firstix;
442}
443
566ece03
JH
444sub B::BINOP::bsave {
445 my ($op, $ix) = @_;
446 if ($op->name eq 'aassign' && $op->private & B::OPpASSIGN_HASH()) {
447 my $last = $op->last;
448 my $lastix = do {
449 local *B::OP::bsave = *B::OP::bsave_fat;
450 local *B::UNOP::bsave = *B::UNOP::bsave_fat;
451 $last->ix;
452 };
453 asm "ldop", $lastix unless $lastix == $opix;
454 asm "op_targ", $last->targ;
455 $op->B::OP::bsave($ix);
456 asm "op_last", $lastix;
457 } else {
458 $op->B::OP::bsave($ix);
459 }
460}
461
462# not needed if no pseudohashes
463
f66c782a 464*B::BINOP::bsave = *B::OP::bsave if VERSION >= 5.009;
1df34986
AE
465
466# deal with sort / formline
467
468sub B::LISTOP::bsave {
469 my ($op, $ix) = @_;
470 my $name = $op->name;
f66c782a
JH
471 sub blocksort() { OPf_SPECIAL|OPf_STACKED }
472 if ($name eq 'sort' && ($op->flags & blocksort) == blocksort) {
1df34986 473 my $first = $op->first;
f66c782a
JH
474 my $pushmark = $first->sibling;
475 my $rvgv = $pushmark->first;
476 my $leave = $rvgv->first;
477
478 my $leaveix = $leave->ix;
479
480 my $rvgvix = $rvgv->ix;
481 asm "ldop", $rvgvix unless $rvgvix == $opix;
482 asm "op_first", $leaveix;
483
484 my $pushmarkix = $pushmark->ix;
485 asm "ldop", $pushmarkix unless $pushmarkix == $opix;
486 asm "op_first", $rvgvix;
487
1df34986 488 my $firstix = $first->ix;
1df34986 489 asm "ldop", $firstix unless $firstix == $opix;
f66c782a
JH
490 asm "op_sibling", $pushmarkix;
491
1df34986
AE
492 $op->B::OP::bsave($ix);
493 asm "op_first", $firstix;
494 } elsif ($name eq 'formline') {
495 $op->B::UNOP::bsave_fat($ix);
a798dbf2 496 } else {
1df34986 497 $op->B::OP::bsave($ix);
a798dbf2 498 }
a798dbf2
MB
499}
500
1df34986 501# fat versions
a798dbf2 502
1df34986
AE
503sub B::OP::bsave_fat {
504 my ($op, $ix) = @_;
505 my $siblix = $op->sibling->ix;
a798dbf2 506
1df34986
AE
507 $op->B::OP::bsave_thin($ix);
508 asm "op_sibling", $siblix;
509 # asm "op_seq", -1; XXX don't allocate OPs piece by piece
a798dbf2
MB
510}
511
1df34986
AE
512sub B::UNOP::bsave_fat {
513 my ($op,$ix) = @_;
514 my $firstix = $op->first->ix;
a798dbf2 515
1df34986
AE
516 $op->B::OP::bsave($ix);
517 asm "op_first", $firstix;
a798dbf2
MB
518}
519
1df34986
AE
520sub B::BINOP::bsave_fat {
521 my ($op,$ix) = @_;
522 my $last = $op->last;
523 my $lastix = $op->last->ix;
f66c782a 524 if (VERSION < 5.009 && $op->name eq 'aassign' && $last->name eq 'null') {
1df34986
AE
525 asm "ldop", $lastix unless $lastix == $opix;
526 asm "op_targ", $last->targ;
a798dbf2 527 }
a798dbf2 528
1df34986
AE
529 $op->B::UNOP::bsave($ix);
530 asm "op_last", $lastix;
a798dbf2
MB
531}
532
1df34986
AE
533sub B::LOGOP::bsave {
534 my ($op,$ix) = @_;
535 my $otherix = $op->other->ix;
a798dbf2 536
1df34986
AE
537 $op->B::UNOP::bsave($ix);
538 asm "op_other", $otherix;
a798dbf2
MB
539}
540
1df34986
AE
541sub B::PMOP::bsave {
542 my ($op,$ix) = @_;
543 my ($rrop, $rrarg, $rstart);
059a8bb7 544
1df34986 545 # my $pmnextix = $op->pmnext->ix; # XXX
059a8bb7 546
f66c782a 547 if (ITHREADS) {
1df34986
AE
548 if ($op->name eq 'subst') {
549 $rrop = "op_pmreplroot";
550 $rrarg = $op->pmreplroot->ix;
551 $rstart = $op->pmreplstart->ix;
552 } elsif ($op->name eq 'pushre') {
553 $rrop = "op_pmreplrootpo";
554 $rrarg = $op->pmreplroot;
a798dbf2 555 }
1df34986
AE
556 $op->B::BINOP::bsave($ix);
557 asm "op_pmstashpv", pvix $op->pmstashpv;
558 } else {
559 $rrop = "op_pmreplrootgv";
560 $rrarg = $op->pmreplroot->ix;
561 $rstart = $op->pmreplstart->ix if $op->name eq 'subst';
562 my $stashix = $op->pmstash->ix;
563 $op->B::BINOP::bsave($ix);
564 asm "op_pmstash", $stashix;
a798dbf2 565 }
1df34986
AE
566
567 asm $rrop, $rrarg if $rrop;
568 asm "op_pmreplstart", $rstart if $rstart;
569
570 asm "op_pmflags", $op->pmflags;
571 asm "op_pmpermflags", $op->pmpermflags;
572 asm "op_pmdynflags", $op->pmdynflags;
573 # asm "op_pmnext", $pmnextix; # XXX
574 asm "newpv", pvstring $op->precomp;
575 asm "pregcomp";
a798dbf2
MB
576}
577
1df34986
AE
578sub B::SVOP::bsave {
579 my ($op,$ix) = @_;
580 my $svix = $op->sv->ix;
581
582 $op->B::OP::bsave($ix);
583 asm "op_sv", $svix;
a798dbf2
MB
584}
585
1df34986
AE
586sub B::PADOP::bsave {
587 my ($op,$ix) = @_;
588
589 $op->B::OP::bsave($ix);
590 asm "op_padix", $op->padix;
a798dbf2
MB
591}
592
1df34986
AE
593sub B::PVOP::bsave {
594 my ($op,$ix) = @_;
595 $op->B::OP::bsave($ix);
596 return unless my $pv = $op->pv;
597
598 if ($op->name eq 'trans') {
599 asm "op_pv_tr", join ',', length($pv)/2, unpack("s*", $pv);
600 } else {
601 asm "newpv", pvstring $pv;
602 asm "op_pv";
a798dbf2 603 }
a798dbf2
MB
604}
605
1df34986
AE
606sub B::LOOP::bsave {
607 my ($op,$ix) = @_;
608 my $nextix = $op->nextop->ix;
609 my $lastix = $op->lastop->ix;
610 my $redoix = $op->redoop->ix;
611
612 $op->B::BINOP::bsave($ix);
613 asm "op_redoop", $redoix;
614 asm "op_nextop", $nextix;
615 asm "op_lastop", $lastix;
a798dbf2
MB
616}
617
1df34986
AE
618sub B::COP::bsave {
619 my ($cop,$ix) = @_;
620 my $warnix = $cop->warnings->ix;
621 my $ioix = $cop->io->ix;
f66c782a 622 if (ITHREADS) {
1df34986
AE
623 $cop->B::OP::bsave($ix);
624 asm "cop_stashpv", pvix $cop->stashpv;
625 asm "cop_file", pvix $cop->file;
626 } else {
627 my $stashix = $cop->stash->ix;
628 my $fileix = $cop->filegv->ix(1);
629 $cop->B::OP::bsave($ix);
630 asm "cop_stash", $stashix;
631 asm "cop_filegv", $fileix;
a798dbf2 632 }
1df34986
AE
633 asm "cop_label", pvix $cop->label if $cop->label; # XXX AD
634 asm "cop_seq", $cop->cop_seq;
635 asm "cop_arybase", $cop->arybase;
636 asm "cop_line", $cop->line;
637 asm "cop_warnings", $warnix;
638 asm "cop_io", $ioix;
a798dbf2
MB
639}
640
1df34986
AE
641sub B::OP::opwalk {
642 my $op = shift;
643 my $ix = $optab{$$op};
644 defined($ix) ? $ix : do {
645 my $ix;
646 my @oplist = $op->oplist;
647 push @cloop, undef;
648 $ix = $_->ix while $_ = pop @oplist;
649 while ($_ = pop @cloop) {
650 asm "ldop", $optab{$$_};
651 asm "op_next", $optab{${$_->next}};
a798dbf2 652 }
1df34986 653 $ix;
a798dbf2
MB
654 }
655}
656
1df34986
AE
657#################################################
658
659sub save_cq {
660 my $av;
661 if (($av=begin_av)->isa("B::AV")) {
662 if ($savebegins) {
663 for ($av->ARRAY) {
664 next unless $_->FILE eq $0;
665 asm "push_begin", $_->ix;
666 }
667 } else {
668 for ($av->ARRAY) {
669 next unless $_->FILE eq $0;
566ece03 670 # XXX BEGIN { goto A while 1; A: }
1df34986 671 for (my $op = $_->START; $$op; $op = $op->next) {
566ece03
JH
672 next unless $op->name eq 'require' ||
673 # this kludge needed for tests
674 $op->name eq 'gv' && do {
675 my $gv = class($op) eq 'SVOP' ?
676 $op->gv :
677 (($_->PADLIST->ARRAY)[1]->ARRAY)[$op->padix];
678 $$gv && $gv->NAME =~ /use_ok|plan/
679 };
1df34986
AE
680 asm "push_begin", $_->ix;
681 last;
059a8bb7 682 }
059a8bb7
JH
683 }
684 }
a798dbf2 685 }
1df34986
AE
686 if (($av=init_av)->isa("B::AV")) {
687 for ($av->ARRAY) {
688 next unless $_->FILE eq $0;
689 asm "push_init", $_->ix;
059a8bb7
JH
690 }
691 }
1df34986
AE
692 if (($av=end_av)->isa("B::AV")) {
693 for ($av->ARRAY) {
694 next unless $_->FILE eq $0;
695 asm "push_end", $_->ix;
059a8bb7 696 }
a798dbf2
MB
697 }
698}
699
a798dbf2 700sub compile {
566ece03 701 my ($head, $scan, $T_inhinc, $keep_syn);
1df34986
AE
702 my $cwd = '';
703 $files{$0} = 1;
704 sub keep_syn {
705 $keep_syn = 1;
706 *B::OP::bsave = *B::OP::bsave_fat;
707 *B::UNOP::bsave = *B::UNOP::bsave_fat;
708 *B::BINOP::bsave = *B::BINOP::bsave_fat;
709 *B::LISTOP::bsave = *B::LISTOP::bsave_fat;
710 }
711 sub bwarn { print STDERR "Bytecode.pm: @_\n" }
712
713 for (@_) {
714 if (/^-S/) {
715 *newasm = *endasm = sub { };
716 *asm = sub { print " @_\n" };
717 *nice = sub ($) { print "\n@_\n" };
718 } elsif (/^-H/) {
719 require ByteLoader;
720 $head = "#! $^X\nuse ByteLoader $ByteLoader::VERSION;\n";
721 } elsif (/^-k/) {
722 keep_syn;
723 } elsif (/^-o(.*)$/) {
566ece03 724 open STDOUT, ">$1" or die "open $1: $!";
1df34986
AE
725 } elsif (/^-f(.*)$/) {
726 $files{$1} = 1;
566ece03
JH
727 } elsif (/^-s(.*)$/) {
728 $scan = length($1) ? $1 : $0;
1df34986
AE
729 } elsif (/^-b/) {
730 $savebegins = 1;
566ece03
JH
731 # this is here for the testsuite
732 } elsif (/^-TI/) {
1df34986 733 $T_inhinc = 1;
566ece03
JH
734 } elsif (/^-TF(.*)/) {
735 my $thatfile = $1;
736 *B::COP::file = sub { $thatfile };
a798dbf2 737 } else {
1df34986 738 bwarn "Ignoring '$_' option";
a798dbf2 739 }
1df34986
AE
740 }
741 if ($scan) {
566ece03 742 my $f;
47682f07
AE
743 if (open $f, $scan) {
744 while (<$f>) {
745 /^#\s*line\s+\d+\s+("?)(.*)\1/ and $files{$2} = 1;
746 /^#/ and next;
747 if (/\bgoto\b\s*[^&]/ && !$keep_syn) {
748 bwarn "keeping the syntax tree: \"goto\" op found";
749 keep_syn;
750 }
a798dbf2 751 }
47682f07
AE
752 } else {
753 bwarn "cannot rescan '$scan'";
1df34986 754 }
566ece03 755 close $f;
1df34986
AE
756 }
757 binmode STDOUT;
758 return sub {
759 print $head if $head;
760 newasm sub { print @_ };
761
762 defstash->bwalk;
763 asm "main_start", main_start->opwalk;
764 asm "main_root", main_root->ix;
765 asm "main_cv", main_cv->ix;
766 asm "curpad", (comppadlist->ARRAY)[1]->ix;
767
768 asm "signal", cstring "__WARN__" # XXX
769 if warnhook->ix;
770 asm "incav", inc_gv->AV->ix if $T_inhinc;
771 save_cq;
772 asm "incav", inc_gv->AV->ix if $T_inhinc;
773 asm "dowarn", dowarn;
774
775 {
776 no strict 'refs';
777 nice "<DATA>";
778 my $dh = *{defstash->NAME."::DATA"};
f66c782a
JH
779 unless (eof $dh) {
780 local undef $/;
1df34986 781 asm "data", ord 'D';
f66c782a 782 print <$dh>;
a798dbf2 783 } else {
1df34986 784 asm "ret";
a798dbf2 785 }
a798dbf2 786 }
1df34986
AE
787
788 endasm;
a798dbf2
MB
789 }
790}
791
7921;
566ece03
JH
793
794=head1 NAME
795
796B::Bytecode - Perl compiler's bytecode backend
797
798=head1 SYNOPSIS
799
800B<perl -MO=Bytecode>[B<,-H>][B<,-o>I<script.plc>] I<script.pl>
801
802=head1 DESCRIPTION
803
804Compiles a Perl script into a bytecode format that could be loaded
805later by the ByteLoader module and executed as a regular Perl script.
806
807=head1 EXAMPLE
808
809 $ perl -MO=Bytecode,-H,-ohi -e 'print "hi!\n"'
810 $ perl hi
811 hi!
812
813=head1 OPTIONS
814
815=over 4
816
817=item B<-b>
818
819Save all the BEGIN blocks. Normally only BEGIN blocks that C<require>
820other files (ex. C<use Foo;>) are saved.
821
822=item B<-H>
823
824prepend a C<use ByteLoader VERSION;> line to the produced bytecode.
825
826=item B<-k>
827
828keep the syntax tree - it is stripped by default.
829
830=item B<-o>I<outfile>
831
832put the bytecode in <outfile> instead of dumping it to STDOUT.
833
834=item B<-s>
835
836scan the script for C<# line ..> directives and for <goto LABEL>
837expressions. When gotos are found keep the syntax tree.
838
839=back
840
841=head1 KNOWN BUGS
842
843=over 4
844
845=item *
846
847C<BEGIN { goto A: while 1; A: }> won't even compile.
848
849=item *
850
851C<?...?> and C<reset> do not work as expected.
852
853=item *
854
855variables in C<(?{ ... })> constructs are not properly scoped.
856
857=item *
858
859scripts that use source filters will fail miserably.
860
861=back
862
863=head1 NOTICE
864
865There are also undocumented bugs and options.
866
867THIS CODE IS HIGHLY EXPERIMENTAL. USE AT YOUR OWN RISK.
868
869=head1 AUTHORS
870
871Originally written by Malcolm Beattie <mbeattie@sable.ox.ac.uk> and
872modified by Benjamin Stuhl <sho_pi@hotmail.com>.
873
874Rewritten by Enache Adrian <enache@rdslink.ro>, 2003 a.d.
875
876=cut