3 # Copyright (c) 1996, 1997, 1998 Malcolm Beattie
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.
10 use B qw(main_start main_root class comppadlist peekop svref_2object
12 OPf_WANT_LIST OPf_WANT OPf_MOD OPf_STACKED OPf_SPECIAL
13 OPpASSIGN_BACKWARDS OPpLVAL_INTRO OPpDEREF_AV OPpDEREF_HV
14 OPpDEREF OPpFLIP_LINENUM G_ARRAY
15 CXt_NULL CXt_SUB CXt_EVAL CXt_LOOP CXt_SUBST CXt_BLOCK
17 use B::C qw(save_unused_subs objsym init_sections mark_unused
18 output_all output_boilerplate output_main);
19 use B::Bblock qw(find_leaders);
20 use B::Stackobj qw(:types :flags);
22 # These should probably be elsewhere
23 # Flags for $op->flags
25 my $module; # module name (when compiled with -m)
26 my %done; # hash keyed by $$op of leaders of basic blocks
27 # which have already been done.
28 my $leaders; # ref to hash of basic block leaders. Keys are $$op
29 # addresses, values are the $op objects themselves.
30 my @bblock_todo; # list of leaders of basic blocks that need visiting
32 my @cc_todo; # list of tuples defining what PP code needs to be
33 # saved (e.g. CV, main or PMOP repl code). Each tuple
34 # is [$name, $root, $start, @padlist]. PMOP repl code
35 # tuples inherit padlist.
36 my @stack; # shadows perl's stack when contents are known.
37 # Values are objects derived from class B::Stackobj
38 my @pad; # Lexicals in current pad as Stackobj-derived objects
39 my @padlist; # Copy of current padlist so PMOP repl code can find it
40 my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
41 my $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufs
42 my %constobj; # OP_CONST constants as Stackobj-derived objects
44 my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
45 # block or even to the end of each loop of blocks,
46 # depending on optimisation options.
47 my $know_op = 0; # Set when C variable op already holds the right op
48 # (from an immediately preceding DOOP(ppname)).
49 my $errors = 0; # Number of errors encountered
50 my %skip_stack; # Hash of PP names which don't need write_back_stack
51 my %skip_lexicals; # Hash of PP names which don't need write_back_lexicals
52 my %skip_invalidate; # Hash of PP names which don't need invalidate_lexicals
53 my %ignore_op; # Hash of ops which do nothing except returning op_next
54 my %need_curcop; # Hash of ops which need PL_curcop
57 foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) {
63 my ($debug_op, $debug_stack, $debug_cxstack, $debug_pad, $debug_runtime,
64 $debug_shadow, $debug_queue, $debug_lineno, $debug_timings);
66 # Optimisation options. On the command line, use hyphens instead of
67 # underscores for compatibility with gcc-style options. We use
68 # underscores here because they are OK in (strict) barewords.
69 my ($freetmps_each_bblock, $freetmps_each_loop, $omit_taint);
70 my %optimise = (freetmps_each_bblock => \$freetmps_each_bblock,
71 freetmps_each_loop => \$freetmps_each_loop,
72 omit_taint => \$omit_taint);
73 # perl patchlevel to generate code for (defaults to current patchlevel)
74 my $patchlevel = int(0.5 + 1000 * ($] - 5));
76 # Could rewrite push_runtime() and output_runtime() to use a
77 # temporary file if memory is at a premium.
78 my $ppname; # name of current fake PP function
80 my $declare_ref; # Hash ref keyed by C variable type of declarations.
82 my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
83 # tuples to be written out.
87 sub init_hash { map { $_ => 1 } @_ }
90 # Initialise the hashes for the default PP functions where we can avoid
91 # either write_back_stack, write_back_lexicals or invalidate_lexicals.
93 %skip_lexicals = init_hash qw(pp_enter pp_enterloop);
94 %skip_invalidate = init_hash qw(pp_enter pp_enterloop);
95 %need_curcop = init_hash qw(pp_rv2gv pp_bless pp_repeat pp_sort pp_caller pp_reset pp_rv2cv pp_entereval pp_require pp_dofile pp_entertry pp_enterloop pp_enteriter );
101 runtime(map { chomp; "/* $_ */"} @_);
106 my ($type, $var) = @_;
107 push(@{$declare_ref->{$type}}, $var);
111 push(@$runtime_list_ref, @_);
112 warn join("\n", @_) . "\n" if $debug_runtime;
116 push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
121 print qq(#include "cc_runtime.h"\n);
122 foreach $ppdata (@pp_list) {
123 my ($name, $runtime, $declare) = @$ppdata;
124 print "\nstatic\nPP($name)\n{\n";
125 my ($type, $varlist, $line);
126 while (($type, $varlist) = each %$declare) {
127 print "\t$type ", join(", ", @$varlist), ";\n";
129 foreach $line (@$runtime) {
139 push_runtime("\t$line");
145 $runtime_list_ref = [];
148 declare("I32", "oldsave");
149 declare("SV", "**svp");
150 map { declare("SV", "*$_") } qw(sv src dst left right);
151 declare("MAGIC", "*mg");
152 $decl->add("static OP * $ppname _((ARGSproto));");
153 debug "init_pp: $ppname\n" if $debug_queue;
156 # Initialise runtime_callback function for Stackobj class
157 BEGIN { B::Stackobj::set_callback(\&runtime) }
159 # Initialise saveoptree_callback for B::C class
161 my ($name, $root, $start, @pl) = @_;
162 debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
164 if ($name eq "*ignore*") {
167 push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
169 my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
170 $start = $fakeop->save;
171 debug "cc_queue: name $name returns $start\n" if $debug_queue;
174 BEGIN { B::C::set_callback(\&cc_queue) }
176 sub valid_int { $_[0]->{flags} & VALID_INT }
177 sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
178 sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
179 sub valid_sv { $_[0]->{flags} & VALID_SV }
181 sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
182 sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
183 sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
184 sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
185 sub top_bool { @stack ? $stack[-1]->as_bool : "SvTRUE(TOPs)" }
187 sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
188 sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
189 sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
190 sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
193 return ((pop @stack)->as_bool);
195 # Careful: POPs has an auto-decrement and SvTRUE evaluates
196 # its argument more than once.
197 runtime("sv = POPs;");
202 sub write_back_lexicals {
203 my $avoid = shift || 0;
204 debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
207 foreach $lex (@pad) {
208 next unless ref($lex);
209 $lex->write_back unless $lex->{flags} & $avoid;
213 sub write_back_stack {
215 return unless @stack;
216 runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
217 foreach $obj (@stack) {
218 runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
223 sub invalidate_lexicals {
224 my $avoid = shift || 0;
225 debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
228 foreach $lex (@pad) {
229 next unless ref($lex);
230 $lex->invalidate unless $lex->{flags} & $avoid;
234 sub reload_lexicals {
236 foreach $lex (@pad) {
237 next unless ref($lex);
238 my $type = $lex->{type};
239 if ($type == T_INT) {
241 } elsif ($type == T_DOUBLE) {
250 package B::Pseudoreg;
252 # This class allocates pseudo-registers (OK, so they're C variables).
254 my %alloc; # Keyed by variable name. A value of 1 means the
255 # variable has been declared. A value of 2 means
258 sub new_scope { %alloc = () }
261 my ($class, $type, $prefix) = @_;
262 my ($ptr, $i, $varname, $status, $obj);
263 $prefix =~ s/^(\**)//;
267 $varname = "$prefix$i";
268 $status = $alloc{$varname};
269 } while $status == 2;
272 B::CC::declare($type, "$ptr$varname");
273 $alloc{$varname} = 2; # declared and in use
275 $obj = bless \$varname, $class;
280 $alloc{$$obj} = 1; # no longer in use but still declared
286 # This class gives a standard API for a perl object to shadow a
287 # C variable and only generate reloads/write-backs when necessary.
289 # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
290 # Use $obj->write_back whenever shadowed_c_var needs to be up to date.
291 # Use $obj->invalidate whenever an unknown function may have
295 my ($class, $write_back) = @_;
296 # Object fields are perl shadow variable, validity flag
297 # (for *C* variable) and callback sub for write_back
298 # (passed perl shadow variable as argument).
299 bless [undef, 1, $write_back], $class;
302 my ($obj, $newval) = @_;
303 $obj->[1] = 0; # C variable no longer valid
309 $obj->[1] = 1; # C variable will now be valid
310 &{$obj->[2]}($obj->[0]);
313 sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
315 my $curcop = new B::Shadow (sub {
316 my $opsym = shift->save;
317 runtime("PL_curcop = (COP*)$opsym;");
321 # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
324 my $cxix = $#cxstack;
325 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
328 debug "dopoptoloop: returning $cxix" if $debug_cxstack;
334 my $cxix = $#cxstack;
336 ($cxstack[$cxix]->{type} != CXt_LOOP ||
337 $cxstack[$cxix]->{label} ne $label)) {
340 debug "dopoptolabel: returning $cxix" if $debug_cxstack;
346 my $file = $curcop->[0]->filegv->SV->PV;
347 my $line = $curcop->[0]->line;
350 warn sprintf("%s:%d: $format\n", $file, $line, @_);
352 warn sprintf("%s:%d: %s\n", $file, $line, $format);
357 # Load pad takes (the elements of) a PADLIST as arguments and loads
358 # up @pad with Stackobj-derived objects which represent those lexicals.
359 # If/when perl itself can generate type information (my int $foo) then
360 # we'll take advantage of that here. Until then, we'll use various hacks
361 # to tell the compiler when we want a lexical to be a particular type
362 # or to be a register.
365 my ($namelistav, $valuelistav) = @_;
367 my @namelist = $namelistav->ARRAY;
368 my @valuelist = $valuelistav->ARRAY;
371 debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
372 # Temporary lexicals don't get named so it's possible for @valuelist
373 # to be strictly longer than @namelist. We count $ix up to the end of
374 # @valuelist but index into @namelist for the name. Any temporaries which
375 # run off the end of @namelist will make $namesv undefined and we treat
376 # that the same as having an explicit SPECIAL sv_undef object in @namelist.
377 # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
378 for ($ix = 1; $ix < @valuelist; $ix++) {
379 my $namesv = $namelist[$ix];
380 my $type = T_UNKNOWN;
383 my $class = class($namesv);
384 if (!defined($namesv) || $class eq "SPECIAL") {
385 # temporaries have &PL_sv_undef instead of a PVNV for a name
386 $flags = VALID_SV|TEMPORARY|REGISTER;
388 if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
392 $flags = VALID_SV|VALID_INT;
393 } elsif ($2 eq "d") {
395 $flags = VALID_SV|VALID_DOUBLE;
397 $flags |= REGISTER if $3;
400 $pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
401 "i_$name", "d_$name");
402 declare("IV", $type == T_INT ? "i_$name = 0" : "i_$name");
403 declare("double", $type == T_DOUBLE ? "d_$name = 0" : "d_$name");
404 debug sprintf("PL_curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
411 sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }
419 # XXX Preserve original label name for "real" labels?
420 return sprintf("lab_%x", $$op);
425 push_runtime(sprintf(" %s:", label($op)));
430 my $opsym = $op->save;
431 runtime("PL_op = $opsym;") unless $know_op;
437 my $ppname = $op->ppaddr;
438 my $sym = loadop($op);
439 runtime("DOOP($ppname);");
446 my $flags = $op->flags;
447 return (($flags & OPf_WANT) ? ($flags & OPf_WANT_LIST) : "dowantarray()");
451 # Code generation for PP code
461 my $gimme = gimme($op);
463 # XXX Change to push a constant sv_undef Stackobj onto @stack
465 runtime("if ($gimme != G_ARRAY) XPUSHs(&PL_sv_undef);");
473 runtime("PP_UNSTACK;");
479 my $next = $op->next;
481 unshift(@bblock_todo, $next);
483 my $bool = pop_bool();
485 runtime(sprintf("if (!$bool) {XPUSHs(&PL_sv_no); goto %s;}", label($next)));
487 runtime(sprintf("if (!%s) goto %s;", top_bool(), label($next)),
495 my $next = $op->next;
497 unshift(@bblock_todo, $next);
499 my $bool = pop_bool @stack;
501 runtime(sprintf("if (%s) { XPUSHs(&PL_sv_yes); goto %s; }",
502 $bool, label($next)));
504 runtime(sprintf("if (%s) goto %s;", top_bool(), label($next)),
512 my $false = $op->false;
513 unshift(@bblock_todo, $false);
515 my $bool = pop_bool();
517 runtime(sprintf("if (!$bool) goto %s;", label($false)));
524 push(@stack, $pad[$ix]);
525 if ($op->flags & OPf_MOD) {
526 my $private = $op->private;
527 if ($private & OPpLVAL_INTRO) {
528 runtime("SAVECLEARSV(PL_curpad[$ix]);");
529 } elsif ($private & OPpDEREF) {
530 runtime(sprintf("vivify_ref(PL_curpad[%d], %d);",
531 $ix, $private & OPpDEREF));
532 $pad[$ix]->invalidate;
541 my $obj = $constobj{$$sv};
542 if (!defined($obj)) {
543 $obj = $constobj{$$sv} = new B::Stackobj::Const ($sv);
553 debug(sprintf("%s:%d\n", $op->filegv->SV->PV, $op->line)) if $debug_lineno;
554 runtime("TAINT_NOT;") unless $omit_taint;
555 runtime("sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;");
556 if ($freetmps_each_bblock || $freetmps_each_loop) {
559 runtime("FREETMPS;");
566 $curcop->invalidate; # XXX?
567 return default_pp($op);
570 #default_pp will handle this:
571 #sub pp_rv2gv { $curcop->write_back; default_pp(@_) }
572 #sub pp_bless { $curcop->write_back; default_pp(@_) }
573 #sub pp_repeat { $curcop->write_back; default_pp(@_) }
574 # The following subs need $curcop->write_back if we decide to support arybase:
575 # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
576 #sub pp_sort { $curcop->write_back; default_pp(@_) }
577 #sub pp_caller { $curcop->write_back; default_pp(@_) }
578 #sub pp_reset { $curcop->write_back; default_pp(@_) }
582 my $gvsym = $op->gv->save;
584 runtime("XPUSHs((SV*)$gvsym);");
590 my $gvsym = $op->gv->save;
592 if ($op->private & OPpLVAL_INTRO) {
593 runtime("XPUSHs(save_scalar($gvsym));");
595 runtime("XPUSHs(GvSV($gvsym));");
602 my $gvsym = $op->gv->save;
603 my $ix = $op->private;
604 my $flag = $op->flags & OPf_MOD;
606 runtime("svp = av_fetch(GvAV($gvsym), $ix, $flag);",
607 "PUSHs(svp ? *svp : &PL_sv_undef);");
612 my ($op, $operator) = @_;
613 if ($op->flags & OPf_STACKED) {
614 my $right = pop_int();
616 my $left = top_int();
617 $stack[-1]->set_int(&$operator($left, $right));
619 runtime(sprintf("sv_setiv(TOPs, %s);",&$operator("TOPi", $right)));
622 my $targ = $pad[$op->targ];
623 my $right = new B::Pseudoreg ("IV", "riv");
624 my $left = new B::Pseudoreg ("IV", "liv");
625 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int));
626 $targ->set_int(&$operator($$left, $$right));
632 sub INTS_CLOSED () { 0x1 }
633 sub INT_RESULT () { 0x2 }
634 sub NUMERIC_RESULT () { 0x4 }
637 my ($op, $operator, $flags) = @_;
639 $force_int ||= ($flags & INT_RESULT);
640 $force_int ||= ($flags & INTS_CLOSED && @stack >= 2
641 && valid_int($stack[-2]) && valid_int($stack[-1]));
642 if ($op->flags & OPf_STACKED) {
643 my $right = pop_numeric();
645 my $left = top_numeric();
647 $stack[-1]->set_int(&$operator($left, $right));
649 $stack[-1]->set_numeric(&$operator($left, $right));
653 my $rightruntime = new B::Pseudoreg ("IV", "riv");
654 runtime(sprintf("$$rightruntime = %s;",$right));
655 runtime(sprintf("sv_setiv(TOPs, %s);",
656 &$operator("TOPi", $$rightruntime)));
658 my $rightruntime = new B::Pseudoreg ("double", "rnv");
659 runtime(sprintf("$$rightruntime = %s;",$right));
660 runtime(sprintf("sv_setnv(TOPs, %s);",
661 &$operator("TOPn",$$rightruntime)));
665 my $targ = $pad[$op->targ];
666 $force_int ||= ($targ->{type} == T_INT);
668 my $right = new B::Pseudoreg ("IV", "riv");
669 my $left = new B::Pseudoreg ("IV", "liv");
670 runtime(sprintf("$$right = %s; $$left = %s;",
671 pop_numeric(), pop_numeric));
672 $targ->set_int(&$operator($$left, $$right));
674 my $right = new B::Pseudoreg ("double", "rnv");
675 my $left = new B::Pseudoreg ("double", "lnv");
676 runtime(sprintf("$$right = %s; $$left = %s;",
677 pop_numeric(), pop_numeric));
678 $targ->set_numeric(&$operator($$left, $$right));
686 my ($op, $operator, $flags) = @_;
687 if ($op->flags & OPf_STACKED) {
688 my $right = pop_sv();
691 if ($flags & INT_RESULT) {
692 $stack[-1]->set_int(&$operator($left, $right));
693 } elsif ($flags & NUMERIC_RESULT) {
694 $stack[-1]->set_numeric(&$operator($left, $right));
696 # XXX Does this work?
697 runtime(sprintf("sv_setsv($left, %s);",
698 &$operator($left, $right)));
699 $stack[-1]->invalidate;
703 if ($flags & INT_RESULT) {
705 } elsif ($flags & NUMERIC_RESULT) {
710 runtime(sprintf("%s(TOPs, %s);", $f, &$operator("TOPs", $right)));
713 my $targ = $pad[$op->targ];
714 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv));
715 if ($flags & INT_RESULT) {
716 $targ->set_int(&$operator("left", "right"));
717 } elsif ($flags & NUMERIC_RESULT) {
718 $targ->set_numeric(&$operator("left", "right"));
720 # XXX Does this work?
721 runtime(sprintf("sv_setsv(%s, %s);",
722 $targ->as_sv, &$operator("left", "right")));
731 my ($op, $operator) = @_;
732 my $right = new B::Pseudoreg ("IV", "riv");
733 my $left = new B::Pseudoreg ("IV", "liv");
734 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int()));
735 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
736 $bool->set_int(&$operator($$left, $$right));
741 sub bool_numeric_binop {
742 my ($op, $operator) = @_;
743 my $right = new B::Pseudoreg ("double", "rnv");
744 my $left = new B::Pseudoreg ("double", "lnv");
745 runtime(sprintf("$$right = %s; $$left = %s;",
746 pop_numeric(), pop_numeric()));
747 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
748 $bool->set_numeric(&$operator($$left, $$right));
754 my ($op, $operator) = @_;
755 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv()));
756 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
757 $bool->set_numeric(&$operator("left", "right"));
764 return sub { "$_[0] $opname $_[1]" }
769 return sub { sprintf("%s(%s)", $opname, join(", ", @_)) }
773 my $plus_op = infix_op("+");
774 my $minus_op = infix_op("-");
775 my $multiply_op = infix_op("*");
776 my $divide_op = infix_op("/");
777 my $modulo_op = infix_op("%");
778 my $lshift_op = infix_op("<<");
779 my $rshift_op = infix_op(">>");
780 my $ncmp_op = sub { "($_[0] > $_[1] ? 1 : ($_[0] < $_[1]) ? -1 : 0)" };
781 my $scmp_op = prefix_op("sv_cmp");
782 my $seq_op = prefix_op("sv_eq");
783 my $sne_op = prefix_op("!sv_eq");
784 my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
785 my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
786 my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
787 my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
788 my $eq_op = infix_op("==");
789 my $ne_op = infix_op("!=");
790 my $lt_op = infix_op("<");
791 my $gt_op = infix_op(">");
792 my $le_op = infix_op("<=");
793 my $ge_op = infix_op(">=");
796 # XXX The standard perl PP code has extra handling for
797 # some special case arguments of these operators.
799 sub pp_add { numeric_binop($_[0], $plus_op, INTS_CLOSED) }
800 sub pp_subtract { numeric_binop($_[0], $minus_op, INTS_CLOSED) }
801 sub pp_multiply { numeric_binop($_[0], $multiply_op, INTS_CLOSED) }
802 sub pp_divide { numeric_binop($_[0], $divide_op) }
803 sub pp_modulo { int_binop($_[0], $modulo_op) } # differs from perl's
804 sub pp_ncmp { numeric_binop($_[0], $ncmp_op, INT_RESULT) }
806 sub pp_left_shift { int_binop($_[0], $lshift_op) }
807 sub pp_right_shift { int_binop($_[0], $rshift_op) }
808 sub pp_i_add { int_binop($_[0], $plus_op) }
809 sub pp_i_subtract { int_binop($_[0], $minus_op) }
810 sub pp_i_multiply { int_binop($_[0], $multiply_op) }
811 sub pp_i_divide { int_binop($_[0], $divide_op) }
812 sub pp_i_modulo { int_binop($_[0], $modulo_op) }
814 sub pp_eq { bool_numeric_binop($_[0], $eq_op) }
815 sub pp_ne { bool_numeric_binop($_[0], $ne_op) }
816 sub pp_lt { bool_numeric_binop($_[0], $lt_op) }
817 sub pp_gt { bool_numeric_binop($_[0], $gt_op) }
818 sub pp_le { bool_numeric_binop($_[0], $le_op) }
819 sub pp_ge { bool_numeric_binop($_[0], $ge_op) }
821 sub pp_i_eq { bool_int_binop($_[0], $eq_op) }
822 sub pp_i_ne { bool_int_binop($_[0], $ne_op) }
823 sub pp_i_lt { bool_int_binop($_[0], $lt_op) }
824 sub pp_i_gt { bool_int_binop($_[0], $gt_op) }
825 sub pp_i_le { bool_int_binop($_[0], $le_op) }
826 sub pp_i_ge { bool_int_binop($_[0], $ge_op) }
828 sub pp_scmp { sv_binop($_[0], $scmp_op, INT_RESULT) }
829 sub pp_slt { bool_sv_binop($_[0], $slt_op) }
830 sub pp_sgt { bool_sv_binop($_[0], $sgt_op) }
831 sub pp_sle { bool_sv_binop($_[0], $sle_op) }
832 sub pp_sge { bool_sv_binop($_[0], $sge_op) }
833 sub pp_seq { bool_sv_binop($_[0], $seq_op) }
834 sub pp_sne { bool_sv_binop($_[0], $sne_op) }
840 my $backwards = $op->private & OPpASSIGN_BACKWARDS;
845 ($src, $dst) = ($dst, $src) if $backwards;
846 my $type = $src->{type};
847 if ($type == T_INT) {
848 $dst->set_int($src->as_int);
849 } elsif ($type == T_DOUBLE) {
850 $dst->set_numeric($src->as_numeric);
852 $dst->set_sv($src->as_sv);
855 } elsif (@stack == 1) {
857 my $src = pop @stack;
858 my $type = $src->{type};
859 runtime("if (PL_tainting && PL_tainted) TAINT_NOT;");
860 if ($type == T_INT) {
861 runtime sprintf("sv_setiv(TOPs, %s);", $src->as_int);
862 } elsif ($type == T_DOUBLE) {
863 runtime sprintf("sv_setnv(TOPs, %s);", $src->as_double);
865 runtime sprintf("sv_setsv(TOPs, %s);", $src->as_sv);
867 runtime("SvSETMAGIC(TOPs);");
869 my $dst = $stack[-1];
870 my $type = $dst->{type};
871 runtime("sv = POPs;");
872 runtime("MAYBE_TAINT_SASSIGN_SRC(sv);");
873 if ($type == T_INT) {
874 $dst->set_int("SvIV(sv)");
875 } elsif ($type == T_DOUBLE) {
876 $dst->set_double("SvNV(sv)");
878 runtime("SvSetSV($dst->{sv}, sv);");
884 runtime("src = POPs; dst = TOPs;");
886 runtime("dst = POPs; src = TOPs;");
888 runtime("MAYBE_TAINT_SASSIGN_SRC(src);",
889 "SvSetSV(dst, src);",
899 my $obj = $stack[-1];
900 my $type = $obj->{type};
901 if ($type == T_INT || $type == T_DOUBLE) {
902 $obj->set_int($obj->as_int . " + 1");
904 runtime sprintf("PP_PREINC(%s);", $obj->as_sv);
908 runtime sprintf("PP_PREINC(TOPs);");
916 runtime("PUSHMARK(sp);");
923 my $gimme = gimme($op);
924 if ($gimme == 1) { # sic
925 runtime("POPMARK;"); # need this even though not a "full" pp_list
927 runtime("PP_LIST($gimme);");
934 write_back_lexicals(REGISTER|TEMPORARY);
937 runtime("while (PL_op != ($sym)->op_next && PL_op != (OP*)0 ){");
938 runtime("PL_op = (*PL_op->op_ppaddr)(ARGS);");
939 runtime("SPAGAIN;}");
941 invalidate_lexicals(REGISTER|TEMPORARY);
948 my $ppname = $op->ppaddr;
949 write_back_lexicals() unless $skip_lexicals{$ppname};
950 write_back_stack() unless $skip_stack{$ppname};
952 runtime("if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;}");
953 invalidate_lexicals() unless $skip_invalidate{$ppname};
963 write_back_lexicals(REGISTER|TEMPORARY);
966 # XXX Is this the right way to distinguish between it returning
967 # CvSTART(cv) (via doform) and pop_return()?
968 runtime("if (PL_op) PL_op = (*PL_op->op_ppaddr)(ARGS);");
971 invalidate_lexicals(REGISTER|TEMPORARY);
978 write_back_lexicals(REGISTER|TEMPORARY);
980 my $sym = loadop($op);
981 my $ppaddr = $op->ppaddr;
982 runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
984 invalidate_lexicals(REGISTER|TEMPORARY);
988 sub pp_entereval { doeval(@_) }
989 sub pp_require { doeval(@_) }
990 sub pp_dofile { doeval(@_) }
995 write_back_lexicals(REGISTER|TEMPORARY);
998 my $jmpbuf = sprintf("jmpbuf%d", $jmpbuf_ix++);
999 declare("Sigjmp_buf", $jmpbuf);
1000 runtime(sprintf("PP_ENTERTRY(%s,%s);", $jmpbuf, label($op->other->next)));
1001 invalidate_lexicals(REGISTER|TEMPORARY);
1007 if ($need_freetmps && $freetmps_each_loop) {
1008 runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
1013 return $op->next->other;
1018 if ($need_freetmps && $freetmps_each_loop) {
1019 runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
1023 # pp_mapstart can return either op_next->op_next or op_next->op_other and
1024 # we need to be able to distinguish the two at runtime.
1028 my $nexttonext=$next->next;
1030 runtime(sprintf("if (PL_op == (($sym)->op_next)->op_next) goto %s;", label($nexttonext)));
1031 return $op->next->other;
1036 my $next = $op->next;
1037 unshift(@bblock_todo, $next);
1038 write_back_lexicals();
1040 my $sym = doop($op);
1041 # pp_grepwhile can return either op_next or op_other and we need to
1042 # be able to distinguish the two at runtime. Since it's possible for
1043 # both ops to be "inlined", the fields could both be zero. To get
1044 # around that, we hack op_next to be our own op (purely because we
1045 # know it's a non-NULL pointer and can't be the same as op_other).
1046 $init->add("((LOGOP*)$sym)->op_next = $sym;");
1047 runtime(sprintf("if (PL_op == ($sym)->op_next) goto %s;", label($next)));
1058 write_back_lexicals(REGISTER|TEMPORARY);
1061 runtime("PUTBACK;", "return PL_op;");
1068 warn sprintf("%s not yet implemented properly\n", $op->ppaddr);
1069 return default_pp($op);
1074 my $flags = $op->flags;
1075 if (!($flags & OPf_WANT)) {
1076 error("context of range unknown at compile-time");
1078 write_back_lexicals();
1080 if (!($flags & OPf_WANT_LIST)) {
1081 # We need to save our UNOP structure since pp_flop uses
1082 # it to find and adjust out targ. We don't need it ourselves.
1084 runtime sprintf("if (SvTRUE(PL_curpad[%d])) goto %s;",
1085 $op->targ, label($op->false));
1086 unshift(@bblock_todo, $op->false);
1093 my $flags = $op->flags;
1094 if (!($flags & OPf_WANT)) {
1095 error("context of flip unknown at compile-time");
1097 if ($flags & OPf_WANT_LIST) {
1098 return $op->first->false;
1100 write_back_lexicals();
1102 # We need to save our UNOP structure since pp_flop uses
1103 # it to find and adjust out targ. We don't need it ourselves.
1106 my $rangeix = $op->first->targ;
1107 runtime(($op->private & OPpFLIP_LINENUM) ?
1108 "if (PL_last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(PL_last_in_gv))) {"
1109 : "if (SvTRUE(TOPs)) {");
1110 runtime("\tsv_setiv(PL_curpad[$rangeix], 1);");
1111 if ($op->flags & OPf_SPECIAL) {
1112 runtime("sv_setiv(PL_curpad[$ix], 1);");
1114 runtime("\tsv_setiv(PL_curpad[$ix], 0);",
1116 sprintf("\tgoto %s;", label($op->first->false)));
1119 qq{sv_setpv(PL_curpad[$ix], "");},
1120 "SETs(PL_curpad[$ix]);");
1134 my $nextop = $op->nextop;
1135 my $lastop = $op->lastop;
1136 my $redoop = $op->redoop;
1137 $curcop->write_back;
1138 debug "enterloop: pushing on cxstack" if $debug_cxstack;
1142 "label" => $curcop->[0]->label,
1150 return default_pp($op);
1153 sub pp_enterloop { enterloop(@_) }
1154 sub pp_enteriter { enterloop(@_) }
1159 die "panic: leaveloop";
1161 debug "leaveloop: popping from cxstack" if $debug_cxstack;
1163 return default_pp($op);
1169 if ($op->flags & OPf_SPECIAL) {
1170 $cxix = dopoptoloop();
1172 error('"next" used outside loop');
1173 return $op->next; # ignore the op
1176 $cxix = dopoptolabel($op->pv);
1178 error('Label not found at compile time for "next %s"', $op->pv);
1179 return $op->next; # ignore the op
1183 my $nextop = $cxstack[$cxix]->{nextop};
1184 push(@bblock_todo, $nextop);
1185 runtime(sprintf("goto %s;", label($nextop)));
1192 if ($op->flags & OPf_SPECIAL) {
1193 $cxix = dopoptoloop();
1195 error('"redo" used outside loop');
1196 return $op->next; # ignore the op
1199 $cxix = dopoptolabel($op->pv);
1201 error('Label not found at compile time for "redo %s"', $op->pv);
1202 return $op->next; # ignore the op
1206 my $redoop = $cxstack[$cxix]->{redoop};
1207 push(@bblock_todo, $redoop);
1208 runtime(sprintf("goto %s;", label($redoop)));
1215 if ($op->flags & OPf_SPECIAL) {
1216 $cxix = dopoptoloop();
1218 error('"last" used outside loop');
1219 return $op->next; # ignore the op
1222 $cxix = dopoptolabel($op->pv);
1224 error('Label not found at compile time for "last %s"', $op->pv);
1225 return $op->next; # ignore the op
1227 # XXX Add support for "last" to leave non-loop blocks
1228 if ($cxstack[$cxix]->{type} != CXt_LOOP) {
1229 error('Use of "last" for non-loop blocks is not yet implemented');
1230 return $op->next; # ignore the op
1234 my $lastop = $cxstack[$cxix]->{lastop}->next;
1235 push(@bblock_todo, $lastop);
1236 runtime(sprintf("goto %s;", label($lastop)));
1242 write_back_lexicals();
1244 my $sym = doop($op);
1245 my $replroot = $op->pmreplroot;
1247 runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplroot) goto %s;",
1248 $sym, label($replroot));
1249 $op->pmreplstart->save;
1250 push(@bblock_todo, $replroot);
1252 invalidate_lexicals();
1258 write_back_lexicals();
1261 my $pmop = $op->other;
1262 # warn sprintf("substcont: op = %s, pmop = %s\n",
1263 # peekop($op), peekop($pmop));#debug
1264 # my $pmopsym = objsym($pmop);
1265 my $pmopsym = $pmop->save; # XXX can this recurse?
1266 # warn "pmopsym = $pmopsym\n";#debug
1267 runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplstart) goto %s;",
1268 $pmopsym, label($pmop->pmreplstart));
1269 invalidate_lexicals();
1275 my $ppname = $op->ppaddr;
1276 if ($curcop and $need_curcop{$ppname}){
1277 $curcop->write_back;
1279 write_back_lexicals() unless $skip_lexicals{$ppname};
1280 write_back_stack() unless $skip_stack{$ppname};
1282 # XXX If the only way that ops can write to a TEMPORARY lexical is
1283 # when it's named in $op->targ then we could call
1284 # invalidate_lexicals(TEMPORARY) and avoid having to write back all
1285 # the temporaries. For now, we'll play it safe and write back the lot.
1286 invalidate_lexicals() unless $skip_invalidate{$ppname};
1292 my $ppname = $op->ppaddr;
1293 if (exists $ignore_op{$ppname}) {
1296 debug peek_stack() if $debug_stack;
1298 debug sprintf("%s [%s]\n",
1300 $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ);
1303 if (defined(&$ppname)) {
1305 return &$ppname($op);
1307 return default_pp($op);
1311 sub compile_bblock {
1313 #warn "compile_bblock: ", peekop($op), "\n"; # debug
1317 $op = compile_op($op);
1318 } while (defined($op) && $$op && !exists($leaders->{$$op}));
1319 write_back_stack(); # boo hoo: big loss
1325 my ($name, $root, $start, @padlist) = @_;
1329 B::Pseudoreg->new_scope;
1331 if ($debug_timings) {
1332 warn sprintf("Basic block analysis at %s\n", timing_info);
1334 $leaders = find_leaders($root, $start);
1335 @bblock_todo = ($start, values %$leaders);
1336 if ($debug_timings) {
1337 warn sprintf("Compilation at %s\n", timing_info);
1339 while (@bblock_todo) {
1340 $op = shift @bblock_todo;
1341 #warn sprintf("Considering basic block %s\n", peekop($op)); # debug
1342 next if !defined($op) || !$$op || $done{$$op};
1343 #warn "...compiling it\n"; # debug
1346 $op = compile_bblock($op);
1347 if ($need_freetmps && $freetmps_each_bblock) {
1348 runtime("FREETMPS;");
1351 } while defined($op) && $$op && !$done{$$op};
1352 if ($need_freetmps && $freetmps_each_loop) {
1353 runtime("FREETMPS;");
1357 runtime("PUTBACK;","return PL_op;");
1358 } elsif ($done{$$op}) {
1359 runtime(sprintf("goto %s;", label($op)));
1362 if ($debug_timings) {
1363 warn sprintf("Saving runtime at %s\n", timing_info);
1371 $start = cc_queue(@_) if @_;
1372 while ($ccinfo = shift @cc_todo) {
1379 my ($name, $cvref) = @_;
1380 my $cv = svref_2object($cvref);
1381 my @padlist = $cv->PADLIST->ARRAY;
1382 my $curpad_sym = $padlist[1]->save;
1383 cc_recurse($name, $cv->ROOT, $cv->START, @padlist);
1387 my @comppadlist = comppadlist->ARRAY;
1388 my $curpad_nam = $comppadlist[0]->save;
1389 my $curpad_sym = $comppadlist[1]->save;
1390 my $init_av = init_av->save;
1391 my $inc_hv = svref_2object(\%INC)->save;
1392 my $inc_av = svref_2object(\@INC)->save;
1393 my $start = cc_recurse("pp_main", main_root, main_start, @comppadlist);
1398 if (!defined($module)) {
1399 $init->add(sprintf("PL_main_root = s\\_%x;", ${main_root()}),
1400 "PL_main_start = $start;",
1401 "PL_curpad = AvARRAY($curpad_sym);",
1402 "PL_initav = $init_av;",
1403 "GvHV(PL_incgv) = $inc_hv;",
1404 "GvAV(PL_incgv) = $inc_av;",
1405 "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
1406 "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));",
1410 seek(STDOUT,0,0); #prevent print statements from BEGIN{} into the output
1411 output_boilerplate();
1413 output_all("perl_init");
1417 if (defined($module)) {
1418 my $cmodule = $module;
1419 $cmodule =~ s/::/__/g;
1429 SAVESPTR(PL_curpad);
1431 PL_curpad = AvARRAY($curpad_sym);
1441 if ($debug_timings) {
1442 warn sprintf("Done at %s\n", timing_info);
1448 my ($option, $opt, $arg);
1450 while ($option = shift @options) {
1451 if ($option =~ /^-(.)(.*)/) {
1455 unshift @options, $option;
1458 if ($opt eq "-" && $arg eq "-") {
1461 } elsif ($opt eq "o") {
1462 $arg ||= shift @options;
1463 open(STDOUT, ">$arg") or return "open '>$arg': $!\n";
1464 } elsif ($opt eq "n") {
1465 $arg ||= shift @options;
1466 $module_name = $arg;
1467 } elsif ($opt eq "u") {
1468 $arg ||= shift @options;
1469 mark_unused($arg,undef);
1470 } elsif ($opt eq "f") {
1471 $arg ||= shift @options;
1472 my $value = $arg !~ s/^no-//;
1474 my $ref = $optimise{$arg};
1475 if (defined($ref)) {
1478 warn qq(ignoring unknown optimisation option "$arg"\n);
1480 } elsif ($opt eq "O") {
1481 $arg = 1 if $arg eq "";
1483 foreach $ref (values %optimise) {
1487 $freetmps_each_loop = 1;
1490 $freetmps_each_bblock = 1 unless $freetmps_each_loop;
1492 } elsif ($opt eq "m") {
1493 $arg ||= shift @options;
1495 mark_unused($arg,undef);
1496 } elsif ($opt eq "p") {
1497 $arg ||= shift @options;
1499 } elsif ($opt eq "D") {
1500 $arg ||= shift @options;
1501 foreach $arg (split(//, $arg)) {
1504 } elsif ($arg eq "O") {
1506 } elsif ($arg eq "s") {
1508 } elsif ($arg eq "c") {
1510 } elsif ($arg eq "p") {
1512 } elsif ($arg eq "r") {
1514 } elsif ($arg eq "S") {
1516 } elsif ($arg eq "q") {
1518 } elsif ($arg eq "l") {
1520 } elsif ($arg eq "t") {
1527 $init = B::Section->get("init");
1528 $decl = B::Section->get("decl");
1532 my ($objname, $ppname);
1533 foreach $objname (@options) {
1534 $objname = "main::$objname" unless $objname =~ /::/;
1535 ($ppname = $objname) =~ s/^.*?:://;
1536 eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
1537 die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
1540 output_boilerplate();
1542 output_all($module_name || "init_module");
1546 return sub { cc_main() };
1556 B::CC - Perl compiler's optimized C translation backend
1560 perl -MO=CC[,OPTIONS] foo.pl
1564 This compiler backend takes Perl source and generates C source code
1565 corresponding to the flow of your program. In other words, this
1566 backend is somewhat a "real" compiler in the sense that many people
1567 think about compilers. Note however that, currently, it is a very
1568 poor compiler in that although it generates (mostly, or at least
1569 sometimes) correct code, it performs relatively few optimisations.
1570 This will change as the compiler develops. The result is that
1571 running an executable compiled with this backend may start up more
1572 quickly than running the original Perl program (a feature shared
1573 by the B<C> compiler backend--see F<B::C>) and may also execute
1574 slightly faster. This is by no means a good optimising compiler--yet.
1578 If there are any non-option arguments, they are taken to be
1579 names of objects to be saved (probably doesn't work properly yet).
1580 Without extra arguments, it saves the main program.
1586 Output to filename instead of STDOUT
1590 Verbose compilation (currently gives a few compilation statistics).
1594 Force end of options
1598 Force apparently unused subs from package Packname to be compiled.
1599 This allows programs to use eval "foo()" even when sub foo is never
1600 seen to be used at compile time. The down side is that any subs which
1601 really are never used also have code generated. This option is
1602 necessary, for example, if you have a signal handler foo which you
1603 initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just
1604 to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u>
1605 options. The compiler tries to figure out which packages may possibly
1606 have subs in which need compiling but the current version doesn't do
1607 it very well. In particular, it is confused by nested packages (i.e.
1608 of the form C<A::B>) where package C<A> does not contain any subs.
1610 =item B<-mModulename>
1612 Instead of generating source for a runnable executable, generate
1613 source for an XSUB module. The boot_Modulename function (which
1614 DynaLoader can look for) does the appropriate initialisation and runs
1615 the main part of the Perl source that is being compiled.
1620 Debug options (concatenated or separate flags like C<perl -D>).
1624 Writes debugging output to STDERR just as it's about to write to the
1625 program's runtime (otherwise writes debugging info as comments in
1630 Outputs each OP as it's compiled
1634 Outputs the contents of the shadow stack at each OP
1638 Outputs the contents of the shadow pad of lexicals as it's loaded for
1639 each sub or the main program.
1643 Outputs the name of each fake PP function in the queue as it's about
1648 Output the filename and line number of each original line of Perl
1649 code as it's processed (C<pp_nextstate>).
1653 Outputs timing information of compilation stages.
1657 Force optimisations on or off one at a time.
1659 =item B<-ffreetmps-each-bblock>
1661 Delays FREETMPS from the end of each statement to the end of the each
1664 =item B<-ffreetmps-each-loop>
1666 Delays FREETMPS from the end of each statement to the end of the group
1667 of basic blocks forming a loop. At most one of the freetmps-each-*
1668 options can be used.
1670 =item B<-fomit-taint>
1672 Omits generating code for handling perl's tainting mechanism.
1676 Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.
1677 Currently, B<-O1> sets B<-ffreetmps-each-bblock> and B<-O2>
1678 sets B<-ffreetmps-each-loop>.
1684 perl -MO=CC,-O2,-ofoo.c foo.pl
1685 perl cc_harness -o foo foo.c
1687 Note that C<cc_harness> lives in the C<B> subdirectory of your perl
1688 library directory. The utility called C<perlcc> may also be used to
1689 help make use of this compiler.
1691 perl -MO=CC,-mFoo,-oFoo.c Foo.pm
1692 perl cc_harness -shared -c -o Foo.so Foo.c
1696 Plenty. Current status: experimental.
1700 These aren't really bugs but they are constructs which are heavily
1701 tied to perl's compile-and-go implementation and with which this
1702 compiler backend cannot cope.
1706 Standard perl calculates the target of "next", "last", and "redo"
1707 at run-time. The compiler calculates the targets at compile-time.
1708 For example, the program
1710 sub skip_on_odd { next NUMBER if $_[0] % 2 }
1711 NUMBER: for ($i = 0; $i < 5; $i++) {
1720 with standard perl but gives a compile-time error with the compiler.
1722 =head2 Context of ".."
1724 The context (scalar or array) of the ".." operator determines whether
1725 it behaves as a range or a flip/flop. Standard perl delays until
1726 runtime the decision of which context it is in but the compiler needs
1727 to know the context at compile-time. For example,
1730 sub range { (shift @a)..(shift @a) }
1732 while (@a) { print scalar(range()) }
1734 generates the output
1738 with standard Perl but gives a compile-time error with compiled Perl.
1742 Compiled Perl programs use native C arithemtic much more frequently
1743 than standard perl. Operations on large numbers or on boundary
1744 cases may produce different behaviour.
1746 =head2 Deprecated features
1748 Features of standard perl such as C<$[> which have been deprecated
1749 in standard perl since Perl5 was released have not been implemented
1754 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>