Commit | Line | Data |
a798dbf2 |
1 | # Bytecode.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::Bytecode; |
9 | use strict; |
10 | use Carp; |
11 | use IO::File; |
12 | |
13 | use B qw(minus_c main_cv main_root main_start comppadlist |
4c1f658f |
14 | class peekop walkoptree svref_2object cstring walksymtable |
15 | SVf_POK SVp_POK SVf_IOK SVp_IOK |
16 | ); |
a798dbf2 |
17 | use B::Asmdata qw(@optype @specialsv_name); |
18 | use B::Assembler qw(assemble_fh); |
19 | |
20 | my %optype_enum; |
21 | my $i; |
22 | for ($i = 0; $i < @optype; $i++) { |
23 | $optype_enum{$optype[$i]} = $i; |
24 | } |
25 | |
26 | # Following is SVf_POK|SVp_POK |
27 | # XXX Shouldn't be hardwired |
4c1f658f |
28 | sub POK () { SVf_POK|SVp_POK } |
a798dbf2 |
29 | |
4c1f658f |
30 | # Following is SVf_IOK|SVp_IOK |
a798dbf2 |
31 | # XXX Shouldn't be hardwired |
4c1f658f |
32 | sub IOK () { SVf_IOK|SVp_IOK } |
a798dbf2 |
33 | |
34 | my ($verbose, $module_only, $no_assemble, $debug_bc, $debug_cv); |
35 | my $assembler_pid; |
36 | |
37 | # Optimisation options. On the command line, use hyphens instead of |
38 | # underscores for compatibility with gcc-style options. We use |
39 | # underscores here because they are OK in (strict) barewords. |
40 | my ($strip_syntree, $compress_nullops, $omit_seq, $bypass_nullops); |
41 | my %optimise = (strip_syntax_tree => \$strip_syntree, |
42 | compress_nullops => \$compress_nullops, |
43 | omit_sequence_numbers => \$omit_seq, |
44 | bypass_nullops => \$bypass_nullops); |
45 | |
46 | my $nextix = 0; |
47 | my %symtable; # maps object addresses to object indices. |
48 | # Filled in at allocation (newsv/newop) time. |
49 | my %saved; # maps object addresses (for SVish classes) to "saved yet?" |
50 | # flag. Set at FOO::bytecode time usually by SV::bytecode. |
51 | # Manipulated via saved(), mark_saved(), unmark_saved(). |
52 | |
53 | my $svix = -1; # we keep track of when the sv register contains an element |
54 | # of the object table to avoid unnecessary repeated |
55 | # consecutive ldsv instructions. |
56 | my $opix = -1; # Ditto for the op register. |
57 | |
58 | sub ldsv { |
59 | my $ix = shift; |
60 | if ($ix != $svix) { |
61 | print "ldsv $ix\n"; |
62 | $svix = $ix; |
63 | } |
64 | } |
65 | |
66 | sub stsv { |
67 | my $ix = shift; |
68 | print "stsv $ix\n"; |
69 | $svix = $ix; |
70 | } |
71 | |
72 | sub set_svix { |
73 | $svix = shift; |
74 | } |
75 | |
76 | sub ldop { |
77 | my $ix = shift; |
78 | if ($ix != $opix) { |
79 | print "ldop $ix\n"; |
80 | $opix = $ix; |
81 | } |
82 | } |
83 | |
84 | sub stop { |
85 | my $ix = shift; |
86 | print "stop $ix\n"; |
87 | $opix = $ix; |
88 | } |
89 | |
90 | sub set_opix { |
91 | $opix = shift; |
92 | } |
93 | |
94 | sub pvstring { |
95 | my $str = shift; |
96 | if (defined($str)) { |
97 | return cstring($str . "\0"); |
98 | } else { |
99 | return '""'; |
100 | } |
101 | } |
102 | |
103 | sub saved { $saved{${$_[0]}} } |
104 | sub mark_saved { $saved{${$_[0]}} = 1 } |
105 | sub unmark_saved { $saved{${$_[0]}} = 0 } |
106 | |
107 | sub debug { $debug_bc = shift } |
108 | |
109 | sub B::OBJECT::nyi { |
110 | my $obj = shift; |
111 | warn sprintf("bytecode save method for %s (0x%x) not yet implemented\n", |
112 | class($obj), $$obj); |
113 | } |
114 | |
115 | # |
116 | # objix may stomp on the op register (for op objects) |
117 | # or the sv register (for SV objects) |
118 | # |
119 | sub B::OBJECT::objix { |
120 | my $obj = shift; |
121 | my $ix = $symtable{$$obj}; |
122 | if (defined($ix)) { |
123 | return $ix; |
124 | } else { |
125 | $obj->newix($nextix); |
126 | return $symtable{$$obj} = $nextix++; |
127 | } |
128 | } |
129 | |
130 | sub B::SV::newix { |
131 | my ($sv, $ix) = @_; |
132 | printf "newsv %d\t# %s\n", $sv->FLAGS & 0xf, class($sv); |
133 | stsv($ix); |
134 | } |
135 | |
136 | sub B::GV::newix { |
137 | my ($gv, $ix) = @_; |
138 | my $gvname = $gv->NAME; |
139 | my $name = cstring($gv->STASH->NAME . "::" . $gvname); |
140 | print "gv_fetchpv $name\n"; |
141 | stsv($ix); |
142 | } |
143 | |
144 | sub B::HV::newix { |
145 | my ($hv, $ix) = @_; |
146 | my $name = $hv->NAME; |
147 | if ($name) { |
148 | # It's a stash |
149 | printf "gv_stashpv %s\n", cstring($name); |
150 | stsv($ix); |
151 | } else { |
152 | # It's an ordinary HV. Fall back to ordinary newix method |
153 | $hv->B::SV::newix($ix); |
154 | } |
155 | } |
156 | |
157 | sub B::SPECIAL::newix { |
158 | my ($sv, $ix) = @_; |
159 | # Special case. $$sv is not the address of the SV but an |
160 | # index into svspecialsv_list. |
161 | printf "ldspecsv $$sv\t# %s\n", $specialsv_name[$$sv]; |
162 | stsv($ix); |
163 | } |
164 | |
165 | sub B::OP::newix { |
166 | my ($op, $ix) = @_; |
167 | my $class = class($op); |
168 | my $typenum = $optype_enum{$class}; |
169 | croak "OP::newix: can't understand class $class" unless defined($typenum); |
170 | print "newop $typenum\t# $class\n"; |
171 | stop($ix); |
172 | } |
173 | |
174 | sub B::OP::walkoptree_debug { |
175 | my $op = shift; |
176 | warn(sprintf("walkoptree: %s\n", peekop($op))); |
177 | } |
178 | |
179 | sub B::OP::bytecode { |
180 | my $op = shift; |
181 | my $next = $op->next; |
182 | my $nextix; |
183 | my $sibix = $op->sibling->objix; |
184 | my $ix = $op->objix; |
185 | my $type = $op->type; |
186 | |
187 | if ($bypass_nullops) { |
188 | $next = $next->next while $$next && $next->type == 0; |
189 | } |
190 | $nextix = $next->objix; |
191 | |
192 | printf "# %s\n", peekop($op) if $debug_bc; |
193 | ldop($ix); |
194 | print "op_next $nextix\n"; |
195 | print "op_sibling $sibix\n" unless $strip_syntree; |
196 | printf "op_type %s\t# %d\n", $op->ppaddr, $type; |
197 | printf("op_seq %d\n", $op->seq) unless $omit_seq; |
198 | if ($type || !$compress_nullops) { |
199 | printf "op_targ %d\nop_flags 0x%x\nop_private 0x%x\n", |
200 | $op->targ, $op->flags, $op->private; |
201 | } |
202 | } |
203 | |
204 | sub B::UNOP::bytecode { |
205 | my $op = shift; |
206 | my $firstix = $op->first->objix; |
207 | $op->B::OP::bytecode; |
208 | if (($op->type || !$compress_nullops) && !$strip_syntree) { |
209 | print "op_first $firstix\n"; |
210 | } |
211 | } |
212 | |
213 | sub B::LOGOP::bytecode { |
214 | my $op = shift; |
215 | my $otherix = $op->other->objix; |
216 | $op->B::UNOP::bytecode; |
217 | print "op_other $otherix\n"; |
218 | } |
219 | |
220 | sub B::SVOP::bytecode { |
221 | my $op = shift; |
222 | my $sv = $op->sv; |
223 | my $svix = $sv->objix; |
224 | $op->B::OP::bytecode; |
225 | print "op_sv $svix\n"; |
226 | $sv->bytecode; |
227 | } |
228 | |
229 | sub B::GVOP::bytecode { |
230 | my $op = shift; |
231 | my $gv = $op->gv; |
232 | my $gvix = $gv->objix; |
233 | $op->B::OP::bytecode; |
234 | print "op_gv $gvix\n"; |
235 | $gv->bytecode; |
236 | } |
237 | |
238 | sub B::PVOP::bytecode { |
239 | my $op = shift; |
240 | my $pv = $op->pv; |
241 | $op->B::OP::bytecode; |
242 | # |
243 | # This would be easy except that OP_TRANS uses a PVOP to store an |
244 | # endian-dependent array of 256 shorts instead of a plain string. |
245 | # |
246 | if ($op->ppaddr eq "pp_trans") { |
247 | my @shorts = unpack("s256", $pv); # assembler handles endianness |
248 | print "op_pv_tr ", join(",", @shorts), "\n"; |
249 | } else { |
250 | printf "newpv %s\nop_pv\n", pvstring($pv); |
251 | } |
252 | } |
253 | |
254 | sub B::BINOP::bytecode { |
255 | my $op = shift; |
256 | my $lastix = $op->last->objix; |
257 | $op->B::UNOP::bytecode; |
258 | if (($op->type || !$compress_nullops) && !$strip_syntree) { |
259 | print "op_last $lastix\n"; |
260 | } |
261 | } |
262 | |
a798dbf2 |
263 | sub B::LISTOP::bytecode { |
264 | my $op = shift; |
265 | my $children = $op->children; |
266 | $op->B::BINOP::bytecode; |
267 | if (($op->type || !$compress_nullops) && !$strip_syntree) { |
268 | print "op_children $children\n"; |
269 | } |
270 | } |
271 | |
272 | sub B::LOOP::bytecode { |
273 | my $op = shift; |
274 | my $redoopix = $op->redoop->objix; |
275 | my $nextopix = $op->nextop->objix; |
276 | my $lastopix = $op->lastop->objix; |
277 | $op->B::LISTOP::bytecode; |
278 | print "op_redoop $redoopix\nop_nextop $nextopix\nop_lastop $lastopix\n"; |
279 | } |
280 | |
281 | sub B::COP::bytecode { |
282 | my $op = shift; |
283 | my $stash = $op->stash; |
284 | my $stashix = $stash->objix; |
285 | my $filegv = $op->filegv; |
286 | my $filegvix = $filegv->objix; |
287 | my $line = $op->line; |
b295d113 |
288 | my $warnings = $op->warnings; |
289 | my $warningsix = $warnings->objix; |
a798dbf2 |
290 | if ($debug_bc) { |
291 | printf "# line %s:%d\n", $filegv->SV->PV, $line; |
292 | } |
293 | $op->B::OP::bytecode; |
294 | printf <<"EOT", pvstring($op->label), $op->cop_seq, $op->arybase; |
295 | newpv %s |
296 | cop_label |
297 | cop_stash $stashix |
298 | cop_seq %d |
299 | cop_filegv $filegvix |
300 | cop_arybase %d |
301 | cop_line $line |
b295d113 |
302 | cop_warnings $warningsix |
a798dbf2 |
303 | EOT |
304 | $filegv->bytecode; |
305 | $stash->bytecode; |
306 | } |
307 | |
308 | sub B::PMOP::bytecode { |
309 | my $op = shift; |
310 | my $replroot = $op->pmreplroot; |
311 | my $replrootix = $replroot->objix; |
312 | my $replstartix = $op->pmreplstart->objix; |
313 | my $ppaddr = $op->ppaddr; |
314 | # pmnext is corrupt in some PMOPs (see misc.t for example) |
315 | #my $pmnextix = $op->pmnext->objix; |
316 | |
317 | if ($$replroot) { |
318 | # OP_PUSHRE (a mutated version of OP_MATCH for the regexp |
319 | # argument to a split) stores a GV in op_pmreplroot instead |
320 | # of a substitution syntax tree. We don't want to walk that... |
321 | if ($ppaddr eq "pp_pushre") { |
322 | $replroot->bytecode; |
323 | } else { |
324 | walkoptree($replroot, "bytecode"); |
325 | } |
326 | } |
327 | $op->B::LISTOP::bytecode; |
328 | if ($ppaddr eq "pp_pushre") { |
329 | printf "op_pmreplrootgv $replrootix\n"; |
330 | } else { |
331 | print "op_pmreplroot $replrootix\nop_pmreplstart $replstartix\n"; |
332 | } |
333 | my $re = pvstring($op->precomp); |
334 | # op_pmnext omitted since a perl bug means it's sometime corrupt |
335 | printf <<"EOT", $op->pmflags, $op->pmpermflags; |
336 | op_pmflags 0x%x |
337 | op_pmpermflags 0x%x |
338 | newpv $re |
339 | pregcomp |
340 | EOT |
341 | } |
342 | |
343 | sub B::SV::bytecode { |
344 | my $sv = shift; |
345 | return if saved($sv); |
346 | my $ix = $sv->objix; |
347 | my $refcnt = $sv->REFCNT; |
348 | my $flags = sprintf("0x%x", $sv->FLAGS); |
349 | ldsv($ix); |
350 | print "sv_refcnt $refcnt\nsv_flags $flags\n"; |
351 | mark_saved($sv); |
352 | } |
353 | |
354 | sub B::PV::bytecode { |
355 | my $sv = shift; |
356 | return if saved($sv); |
357 | $sv->B::SV::bytecode; |
358 | printf("newpv %s\nxpv\n", pvstring($sv->PV)) if $sv->FLAGS & POK; |
359 | } |
360 | |
361 | sub B::IV::bytecode { |
362 | my $sv = shift; |
363 | return if saved($sv); |
364 | my $iv = $sv->IVX; |
365 | $sv->B::SV::bytecode; |
366 | printf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32"; |
367 | } |
368 | |
369 | sub B::NV::bytecode { |
370 | my $sv = shift; |
371 | return if saved($sv); |
372 | $sv->B::SV::bytecode; |
373 | printf "xnv %s\n", $sv->NVX; |
374 | } |
375 | |
376 | sub B::RV::bytecode { |
377 | my $sv = shift; |
378 | return if saved($sv); |
379 | my $rv = $sv->RV; |
380 | my $rvix = $rv->objix; |
381 | $rv->bytecode; |
382 | $sv->B::SV::bytecode; |
383 | print "xrv $rvix\n"; |
384 | } |
385 | |
386 | sub B::PVIV::bytecode { |
387 | my $sv = shift; |
388 | return if saved($sv); |
389 | my $iv = $sv->IVX; |
390 | $sv->B::PV::bytecode; |
391 | printf "%s $iv\n", $sv->needs64bits ? "xiv64" : "xiv32"; |
392 | } |
393 | |
394 | sub B::PVNV::bytecode { |
395 | my ($sv, $flag) = @_; |
396 | # The $flag argument is passed through PVMG::bytecode by BM::bytecode |
397 | # and AV::bytecode and indicates special handling. $flag = 1 is used by |
398 | # BM::bytecode and means that we should ensure we save the whole B-M |
399 | # table. It consists of 257 bytes (256 char array plus a final \0) |
400 | # which follow the ordinary PV+\0 and the 257 bytes are *not* reflected |
401 | # in SvCUR. $flag = 2 is used by AV::bytecode and means that we only |
402 | # call SV::bytecode instead of saving PV and calling NV::bytecode since |
403 | # PV/NV/IV stuff is different for AVs. |
404 | return if saved($sv); |
405 | if ($flag == 2) { |
406 | $sv->B::SV::bytecode; |
407 | } else { |
408 | my $pv = $sv->PV; |
409 | $sv->B::IV::bytecode; |
410 | printf "xnv %s\n", $sv->NVX; |
411 | if ($flag == 1) { |
412 | $pv .= "\0" . $sv->TABLE; |
413 | printf "newpv %s\npv_cur %d\nxpv\n", pvstring($pv),length($pv)-257; |
414 | } else { |
415 | printf("newpv %s\nxpv\n", pvstring($pv)) if $sv->FLAGS & POK; |
416 | } |
417 | } |
418 | } |
419 | |
420 | sub B::PVMG::bytecode { |
421 | my ($sv, $flag) = @_; |
422 | # See B::PVNV::bytecode for an explanation of $flag. |
423 | return if saved($sv); |
424 | # XXX We assume SvSTASH is already saved and don't save it later ourselves |
425 | my $stashix = $sv->SvSTASH->objix; |
426 | my @mgchain = $sv->MAGIC; |
427 | my (@mgobjix, $mg); |
428 | # |
429 | # We need to traverse the magic chain and get objix for each OBJ |
430 | # field *before* we do B::PVNV::bytecode since objix overwrites |
431 | # the sv register. However, we need to write the magic-saving |
432 | # bytecode *after* B::PVNV::bytecode since sv isn't initialised |
433 | # to refer to $sv until then. |
434 | # |
435 | @mgobjix = map($_->OBJ->objix, @mgchain); |
436 | $sv->B::PVNV::bytecode($flag); |
437 | print "xmg_stash $stashix\n"; |
438 | foreach $mg (@mgchain) { |
439 | printf "sv_magic %s\nmg_obj %d\nnewpv %s\nmg_pv\n", |
440 | cstring($mg->TYPE), shift(@mgobjix), pvstring($mg->PTR); |
441 | } |
442 | } |
443 | |
444 | sub B::PVLV::bytecode { |
445 | my $sv = shift; |
446 | return if saved($sv); |
447 | $sv->B::PVMG::bytecode; |
448 | printf <<'EOT', $sv->TARGOFF, $sv->TARGLEN, cstring($sv->TYPE); |
449 | xlv_targoff %d |
450 | xlv_targlen %d |
451 | xlv_type %s |
452 | EOT |
453 | } |
454 | |
455 | sub B::BM::bytecode { |
456 | my $sv = shift; |
457 | return if saved($sv); |
458 | # See PVNV::bytecode for an explanation of what the argument does |
459 | $sv->B::PVMG::bytecode(1); |
460 | printf "xbm_useful %d\nxbm_previous %d\nxbm_rare %d\n", |
461 | $sv->USEFUL, $sv->PREVIOUS, $sv->RARE; |
462 | } |
463 | |
464 | sub B::GV::bytecode { |
465 | my $gv = shift; |
466 | return if saved($gv); |
467 | my $ix = $gv->objix; |
468 | mark_saved($gv); |
469 | my $gvname = $gv->NAME; |
470 | my $name = cstring($gv->STASH->NAME . "::" . $gvname); |
471 | my $egv = $gv->EGV; |
472 | my $egvix = $egv->objix; |
473 | ldsv($ix); |
474 | printf <<"EOT", $gv->FLAGS, $gv->GvFLAGS, $gv->LINE; |
475 | sv_flags 0x%x |
476 | xgv_flags 0x%x |
477 | gp_line %d |
478 | EOT |
479 | my $refcnt = $gv->REFCNT; |
480 | printf("sv_refcnt_add %d\n", $refcnt - 1) if $refcnt > 1; |
481 | my $gvrefcnt = $gv->GvREFCNT; |
482 | printf("gp_refcnt_add %d\n", $gvrefcnt - 1) if $gvrefcnt > 1; |
483 | if ($gvrefcnt > 1 && $ix != $egvix) { |
484 | print "gp_share $egvix\n"; |
485 | } else { |
486 | if ($gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/) { |
487 | my $i; |
488 | my @subfield_names = qw(SV AV HV CV FILEGV FORM IO); |
489 | my @subfields = map($gv->$_(), @subfield_names); |
490 | my @ixes = map($_->objix, @subfields); |
491 | # Reset sv register for $gv |
492 | ldsv($ix); |
493 | for ($i = 0; $i < @ixes; $i++) { |
494 | printf "gp_%s %d\n", lc($subfield_names[$i]), $ixes[$i]; |
495 | } |
496 | # Now save all the subfields |
497 | my $sv; |
498 | foreach $sv (@subfields) { |
499 | $sv->bytecode; |
500 | } |
501 | } |
502 | } |
503 | } |
504 | |
505 | sub B::HV::bytecode { |
506 | my $hv = shift; |
507 | return if saved($hv); |
508 | mark_saved($hv); |
509 | my $name = $hv->NAME; |
510 | my $ix = $hv->objix; |
511 | if (!$name) { |
512 | # It's an ordinary HV. Stashes have NAME set and need no further |
513 | # saving beyond the gv_stashpv that $hv->objix already ensures. |
514 | my @contents = $hv->ARRAY; |
515 | my ($i, @ixes); |
516 | for ($i = 1; $i < @contents; $i += 2) { |
517 | push(@ixes, $contents[$i]->objix); |
518 | } |
519 | for ($i = 1; $i < @contents; $i += 2) { |
520 | $contents[$i]->bytecode; |
521 | } |
522 | ldsv($ix); |
523 | for ($i = 0; $i < @contents; $i += 2) { |
524 | printf("newpv %s\nhv_store %d\n", |
525 | pvstring($contents[$i]), $ixes[$i / 2]); |
526 | } |
527 | printf "sv_refcnt %d\nsv_flags 0x%x\n", $hv->REFCNT, $hv->FLAGS; |
528 | } |
529 | } |
530 | |
531 | sub B::AV::bytecode { |
532 | my $av = shift; |
533 | return if saved($av); |
534 | my $ix = $av->objix; |
535 | my $fill = $av->FILL; |
536 | my $max = $av->MAX; |
537 | my (@array, @ixes); |
538 | if ($fill > -1) { |
539 | @array = $av->ARRAY; |
540 | @ixes = map($_->objix, @array); |
541 | my $sv; |
542 | foreach $sv (@array) { |
543 | $sv->bytecode; |
544 | } |
545 | } |
546 | # See PVNV::bytecode for the meaning of the flag argument of 2. |
547 | $av->B::PVMG::bytecode(2); |
548 | # Recover sv register and set AvMAX and AvFILL to -1 (since we |
549 | # create an AV with NEWSV and SvUPGRADE rather than doing newAV |
550 | # which is what sets AvMAX and AvFILL. |
551 | ldsv($ix); |
552 | printf "xav_flags 0x%x\nxav_max -1\nxav_fill -1\n", $av->AvFLAGS; |
553 | if ($fill > -1) { |
554 | my $elix; |
555 | foreach $elix (@ixes) { |
556 | print "av_push $elix\n"; |
557 | } |
558 | } else { |
559 | if ($max > -1) { |
560 | print "av_extend $max\n"; |
561 | } |
562 | } |
563 | } |
564 | |
565 | sub B::CV::bytecode { |
566 | my $cv = shift; |
567 | return if saved($cv); |
568 | my $ix = $cv->objix; |
569 | $cv->B::PVMG::bytecode; |
570 | my $i; |
571 | my @subfield_names = qw(ROOT START STASH GV FILEGV PADLIST OUTSIDE); |
572 | my @subfields = map($cv->$_(), @subfield_names); |
573 | my @ixes = map($_->objix, @subfields); |
574 | # Save OP tree from CvROOT (first element of @subfields) |
575 | my $root = shift @subfields; |
576 | if ($$root) { |
577 | walkoptree($root, "bytecode"); |
578 | } |
579 | # Reset sv register for $cv (since above ->objix calls stomped on it) |
580 | ldsv($ix); |
581 | for ($i = 0; $i < @ixes; $i++) { |
582 | printf "xcv_%s %d\n", lc($subfield_names[$i]), $ixes[$i]; |
583 | } |
584 | printf "xcv_depth %d\nxcv_flags 0x%x\n", $cv->DEPTH, $cv->FLAGS; |
585 | # Now save all the subfields (except for CvROOT which was handled |
586 | # above) and CvSTART (now the initial element of @subfields). |
587 | shift @subfields; # bye-bye CvSTART |
588 | my $sv; |
589 | foreach $sv (@subfields) { |
590 | $sv->bytecode; |
591 | } |
592 | } |
593 | |
594 | sub B::IO::bytecode { |
595 | my $io = shift; |
596 | return if saved($io); |
597 | my $ix = $io->objix; |
598 | my $top_gv = $io->TOP_GV; |
599 | my $top_gvix = $top_gv->objix; |
600 | my $fmt_gv = $io->FMT_GV; |
601 | my $fmt_gvix = $fmt_gv->objix; |
602 | my $bottom_gv = $io->BOTTOM_GV; |
603 | my $bottom_gvix = $bottom_gv->objix; |
604 | |
605 | $io->B::PVMG::bytecode; |
606 | ldsv($ix); |
607 | print "xio_top_gv $top_gvix\n"; |
608 | print "xio_fmt_gv $fmt_gvix\n"; |
609 | print "xio_bottom_gv $bottom_gvix\n"; |
610 | my $field; |
611 | foreach $field (qw(TOP_NAME FMT_NAME BOTTOM_NAME)) { |
612 | printf "newpv %s\nxio_%s\n", pvstring($io->$field()), lc($field); |
613 | } |
614 | foreach $field (qw(LINES PAGE PAGE_LEN LINES_LEFT SUBPROCESS)) { |
615 | printf "xio_%s %d\n", lc($field), $io->$field(); |
616 | } |
617 | printf "xio_type %s\nxio_flags 0x%x\n", cstring($io->IoTYPE), $io->IoFLAGS; |
618 | $top_gv->bytecode; |
619 | $fmt_gv->bytecode; |
620 | $bottom_gv->bytecode; |
621 | } |
622 | |
623 | sub B::SPECIAL::bytecode { |
624 | # nothing extra needs doing |
625 | } |
626 | |
627 | sub bytecompile_object { |
628 | my $sv; |
629 | foreach $sv (@_) { |
630 | svref_2object($sv)->bytecode; |
631 | } |
632 | } |
633 | |
634 | sub B::GV::bytecodecv { |
635 | my $gv = shift; |
636 | my $cv = $gv->CV; |
637 | if ($$cv && !saved($cv)) { |
638 | if ($debug_cv) { |
639 | warn sprintf("saving extra CV &%s::%s (0x%x) from GV 0x%x\n", |
640 | $gv->STASH->NAME, $gv->NAME, $$cv, $$gv); |
641 | } |
642 | $gv->bytecode; |
643 | } |
644 | } |
645 | |
646 | sub bytecompile_main { |
647 | my $curpad = (comppadlist->ARRAY)[1]; |
648 | my $curpadix = $curpad->objix; |
649 | $curpad->bytecode; |
650 | walkoptree(main_root, "bytecode"); |
651 | warn "done main program, now walking symbol table\n" if $debug_bc; |
652 | my ($pack, %exclude); |
653 | foreach $pack (qw(B O AutoLoader DynaLoader Config DB VMS strict vars |
654 | FileHandle Exporter Carp UNIVERSAL IO Fcntl Symbol |
655 | SelectSaver blib Cwd)) |
656 | { |
657 | $exclude{$pack."::"} = 1; |
658 | } |
659 | no strict qw(vars refs); |
660 | walksymtable(\%{"main::"}, "bytecodecv", sub { |
661 | warn "considering $_[0]\n" if $debug_bc; |
662 | return !defined($exclude{$_[0]}); |
663 | }); |
664 | if (!$module_only) { |
665 | printf "main_root %d\n", main_root->objix; |
666 | printf "main_start %d\n", main_start->objix; |
667 | printf "curpad $curpadix\n"; |
668 | # XXX Do min_intro_pending and max_intro_pending matter? |
669 | } |
670 | } |
671 | |
672 | sub prepare_assemble { |
673 | my $newfh = IO::File->new_tmpfile; |
674 | select($newfh); |
675 | binmode $newfh; |
676 | return $newfh; |
677 | } |
678 | |
679 | sub do_assemble { |
680 | my $fh = shift; |
681 | seek($fh, 0, 0); # rewind the temporary file |
682 | assemble_fh($fh, sub { print OUT @_ }); |
683 | } |
684 | |
685 | sub compile { |
686 | my @options = @_; |
687 | my ($option, $opt, $arg); |
688 | open(OUT, ">&STDOUT"); |
689 | binmode OUT; |
690 | select(OUT); |
691 | OPTION: |
692 | while ($option = shift @options) { |
693 | if ($option =~ /^-(.)(.*)/) { |
694 | $opt = $1; |
695 | $arg = $2; |
696 | } else { |
697 | unshift @options, $option; |
698 | last OPTION; |
699 | } |
700 | if ($opt eq "-" && $arg eq "-") { |
701 | shift @options; |
702 | last OPTION; |
703 | } elsif ($opt eq "o") { |
704 | $arg ||= shift @options; |
705 | open(OUT, ">$arg") or return "$arg: $!\n"; |
706 | binmode OUT; |
707 | } elsif ($opt eq "D") { |
708 | $arg ||= shift @options; |
709 | foreach $arg (split(//, $arg)) { |
710 | if ($arg eq "b") { |
711 | $| = 1; |
712 | debug(1); |
713 | } elsif ($arg eq "o") { |
714 | B->debug(1); |
715 | } elsif ($arg eq "a") { |
716 | B::Assembler::debug(1); |
717 | } elsif ($arg eq "C") { |
718 | $debug_cv = 1; |
719 | } |
720 | } |
721 | } elsif ($opt eq "v") { |
722 | $verbose = 1; |
723 | } elsif ($opt eq "m") { |
724 | $module_only = 1; |
725 | } elsif ($opt eq "S") { |
726 | $no_assemble = 1; |
727 | } elsif ($opt eq "f") { |
728 | $arg ||= shift @options; |
729 | my $value = $arg !~ s/^no-//; |
730 | $arg =~ s/-/_/g; |
731 | my $ref = $optimise{$arg}; |
732 | if (defined($ref)) { |
733 | $$ref = $value; |
734 | } else { |
735 | warn qq(ignoring unknown optimisation option "$arg"\n); |
736 | } |
737 | } elsif ($opt eq "O") { |
738 | $arg = 1 if $arg eq ""; |
739 | my $ref; |
740 | foreach $ref (values %optimise) { |
741 | $$ref = 0; |
742 | } |
743 | if ($arg >= 6) { |
744 | $strip_syntree = 1; |
745 | } |
746 | if ($arg >= 2) { |
747 | $bypass_nullops = 1; |
748 | } |
749 | if ($arg >= 1) { |
750 | $compress_nullops = 1; |
751 | $omit_seq = 1; |
752 | } |
753 | } |
754 | } |
755 | if (@options) { |
756 | return sub { |
757 | my $objname; |
758 | my $newfh; |
759 | $newfh = prepare_assemble() unless $no_assemble; |
760 | foreach $objname (@options) { |
761 | eval "bytecompile_object(\\$objname)"; |
762 | } |
763 | do_assemble($newfh) unless $no_assemble; |
764 | } |
765 | } else { |
766 | return sub { |
767 | my $newfh; |
768 | $newfh = prepare_assemble() unless $no_assemble; |
769 | bytecompile_main(); |
770 | do_assemble($newfh) unless $no_assemble; |
771 | } |
772 | } |
773 | } |
774 | |
775 | 1; |
7f20e9dd |
776 | |
777 | __END__ |
778 | |
779 | =head1 NAME |
780 | |
781 | B::Bytecode - Perl compiler's bytecode backend |
782 | |
783 | =head1 SYNOPSIS |
784 | |
1a52ab62 |
785 | perl -MO=Bytecode[,OPTIONS] foo.pl |
7f20e9dd |
786 | |
787 | =head1 DESCRIPTION |
788 | |
1a52ab62 |
789 | This compiler backend takes Perl source and generates a |
790 | platform-independent bytecode encapsulating code to load the |
791 | internal structures perl uses to run your program. When the |
792 | generated bytecode is loaded in, your program is ready to run, |
793 | reducing the time which perl would have taken to load and parse |
794 | your program into its internal semi-compiled form. That means that |
795 | compiling with this backend will not help improve the runtime |
796 | execution speed of your program but may improve the start-up time. |
797 | Depending on the environment in which your program runs this may |
798 | or may not be a help. |
799 | |
800 | The resulting bytecode can be run with a special byteperl executable |
801 | or (for non-main programs) be loaded via the C<byteload_fh> function |
802 | in the F<B> module. |
803 | |
804 | =head1 OPTIONS |
805 | |
806 | If there are any non-option arguments, they are taken to be names of |
807 | objects to be saved (probably doesn't work properly yet). Without |
808 | extra arguments, it saves the main program. |
809 | |
810 | =over 4 |
811 | |
812 | =item B<-ofilename> |
813 | |
814 | Output to filename instead of STDOUT. |
815 | |
816 | =item B<--> |
817 | |
818 | Force end of options. |
819 | |
820 | =item B<-f> |
821 | |
822 | Force optimisations on or off one at a time. Each can be preceded |
823 | by B<no-> to turn the option off (e.g. B<-fno-compress-nullops>). |
824 | |
825 | =item B<-fcompress-nullops> |
826 | |
827 | Only fills in the necessary fields of ops which have |
828 | been optimised away by perl's internal compiler. |
829 | |
830 | =item B<-fomit-sequence-numbers> |
831 | |
832 | Leaves out code to fill in the op_seq field of all ops |
833 | which is only used by perl's internal compiler. |
834 | |
835 | =item B<-fbypass-nullops> |
836 | |
837 | If op->op_next ever points to a NULLOP, replaces the op_next field |
838 | with the first non-NULLOP in the path of execution. |
839 | |
840 | =item B<-fstrip-syntax-tree> |
841 | |
842 | Leaves out code to fill in the pointers which link the internal syntax |
843 | tree together. They're not needed at run-time but leaving them out |
844 | will make it impossible to recompile or disassemble the resulting |
845 | program. It will also stop C<goto label> statements from working. |
846 | |
847 | =item B<-On> |
848 | |
849 | Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>. |
850 | B<-O1> sets B<-fcompress-nullops> B<-fomit-sequence numbers>. |
851 | B<-O6> adds B<-fstrip-syntax-tree>. |
852 | |
853 | =item B<-D> |
854 | |
855 | Debug options (concatenated or separate flags like C<perl -D>). |
856 | |
857 | =item B<-Do> |
858 | |
859 | Prints each OP as it's processed. |
860 | |
861 | =item B<-Db> |
862 | |
863 | Print debugging information about bytecompiler progress. |
864 | |
865 | =item B<-Da> |
866 | |
867 | Tells the (bytecode) assembler to include source assembler lines |
868 | in its output as bytecode comments. |
869 | |
870 | =item B<-DC> |
871 | |
872 | Prints each CV taken from the final symbol tree walk. |
873 | |
874 | =item B<-S> |
875 | |
876 | Output (bytecode) assembler source rather than piping it |
877 | through the assembler and outputting bytecode. |
878 | |
879 | =item B<-m> |
880 | |
881 | Compile as a module rather than a standalone program. Currently this |
882 | just means that the bytecodes for initialising C<main_start>, |
883 | C<main_root> and C<curpad> are omitted. |
884 | |
885 | =back |
886 | |
707102d0 |
887 | =head1 EXAMPLES |
1a52ab62 |
888 | |
e8edd1e6 |
889 | perl -MO=Bytecode,-O6,-o,foo.plc foo.pl |
1a52ab62 |
890 | |
e8edd1e6 |
891 | perl -MO=Bytecode,-S foo.pl > foo.S |
892 | assemble foo.S > foo.plc |
1a52ab62 |
893 | |
e8edd1e6 |
894 | Note that C<assemble> lives in the C<B> subdirectory of your perl |
895 | library directory. The utility called perlcc may also be used to |
896 | help make use of this compiler. |
897 | |
898 | perl -MO=Bytecode,-m,-oFoo.pmc Foo.pm |
1a52ab62 |
899 | |
900 | =head1 BUGS |
901 | |
902 | Plenty. Current status: experimental. |
7f20e9dd |
903 | |
904 | =head1 AUTHOR |
905 | |
906 | Malcolm Beattie, C<mbeattie@sable.ox.ac.uk> |
907 | |
908 | =cut |