3 # Copyright (c) 1996 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 ad
12 use B::C qw(push_decl init_init push_init save_unused_subs objsym
13 output_all output_boilerplate output_main);
14 use B::Bblock qw(find_leaders);
15 use B::Stackobj qw(:types :flags);
17 # These should probably be elsewhere
18 # Flags for $op->flags
22 sub OPf_STACKED () { 64 }
23 sub OPf_SPECIAL () { 128 }
24 # op-specific flags for $op->private
25 sub OPpASSIGN_BACKWARDS () { 64 }
26 sub OPpLVAL_INTRO () { 128 }
27 sub OPpDEREF_AV () { 32 }
28 sub OPpDEREF_HV () { 64 }
29 sub OPpFLIP_LINENUM () { 64 }
36 sub CXt_SUBST () { 4 }
37 sub CXt_BLOCK () { 5 }
39 my %done; # hash keyed by $$op of leaders of basic blocks
40 # which have already been done.
41 my $leaders; # ref to hash of basic block leaders. Keys are $$op
42 # addresses, values are the $op objects themselves.
43 my @bblock_todo; # list of leaders of basic blocks that need visiting
45 my @cc_todo; # list of tuples defining what PP code needs to be
46 # saved (e.g. CV, main or PMOP repl code). Each tuple
47 # is [$name, $root, $start, @padlist]. PMOP repl code
48 # tuples inherit padlist.
49 my @stack; # shadows perl's stack when contents are known.
50 # Values are objects derived from class B::Stackobj
51 my @pad; # Lexicals in current pad as Stackobj-derived objects
52 my @padlist; # Copy of current padlist so PMOP repl code can find it
53 my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
54 my $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufs
55 my %constobj; # OP_CONST constants as Stackobj-derived objects
57 my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
58 # block or even to the end of each loop of blocks,
59 # depending on optimisation options.
60 my $know_op = 0; # Set when C variable op already holds the right op
61 # (from an immediately preceding DOOP(ppname)).
62 my $errors = 0; # Number of errors encountered
63 my %skip_stack; # Hash of PP names which don't need write_back_stack
64 my %skip_lexicals; # Hash of PP names which don't need write_back_lexicals
65 my %skip_invalidate; # Hash of PP names which don't need invalidate_lexicals
66 my %ignore_op; # Hash of ops which do nothing except returning op_next
69 foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) {
74 my @unused_sub_packages; # list of packages (given by -u options) to search
75 # explicitly and save every sub we find there, even
76 # if apparently unused (could be only referenced from
77 # an eval "" or from a $SIG{FOO} = "bar").
80 my ($debug_op, $debug_stack, $debug_cxstack, $debug_pad, $debug_runtime,
81 $debug_shadow, $debug_queue, $debug_lineno, $debug_timings);
83 # Optimisation options. On the command line, use hyphens instead of
84 # underscores for compatibility with gcc-style options. We use
85 # underscores here because they are OK in (strict) barewords.
86 my ($freetmps_each_bblock, $freetmps_each_loop, $omit_taint);
87 my %optimise = (freetmps_each_bblock => \$freetmps_each_bblock,
88 freetmps_each_loop => \$freetmps_each_loop,
89 omit_taint => \$omit_taint);
91 # Could rewrite push_runtime() and output_runtime() to use a
92 # temporary file if memory is at a premium.
93 my $ppname; # name of current fake PP function
95 my $declare_ref; # Hash ref keyed by C variable type of declarations.
97 my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
98 # tuples to be written out.
100 sub init_hash { map { $_ => 1 } @_ }
103 # Initialise the hashes for the default PP functions where we can avoid
104 # either write_back_stack, write_back_lexicals or invalidate_lexicals.
106 %skip_lexicals = init_hash qw(pp_enter pp_enterloop);
107 %skip_invalidate = init_hash qw(pp_enter pp_enterloop);
110 if ($debug_runtime) {
113 runtime(map { chomp; "/* $_ */"} @_);
118 my ($type, $var) = @_;
119 push(@{$declare_ref->{$type}}, $var);
123 push(@$runtime_list_ref, @_);
124 warn join("\n", @_) . "\n" if $debug_runtime;
128 push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
133 print qq(#include "cc_runtime.h"\n);
134 foreach $ppdata (@pp_list) {
135 my ($name, $runtime, $declare) = @$ppdata;
136 print "\nstatic\nPP($name)\n{\n";
137 my ($type, $varlist, $line);
138 while (($type, $varlist) = each %$declare) {
139 print "\t$type ", join(", ", @$varlist), ";\n";
141 foreach $line (@$runtime) {
151 push_runtime("\t$line");
157 $runtime_list_ref = [];
160 declare("I32", "oldsave");
161 declare("SV", "**svp");
162 map { declare("SV", "*$_") } qw(sv src dst left right);
163 declare("MAGIC", "*mg");
164 push_decl("static OP * $ppname _((ARGSproto));");
165 debug "init_pp: $ppname\n" if $debug_queue;
168 # Initialise runtime_callback function for Stackobj class
169 BEGIN { B::Stackobj::set_callback(\&runtime) }
171 # Initialise saveoptree_callback for B::C class
173 my ($name, $root, $start, @pl) = @_;
174 debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
176 if ($name eq "*ignore*") {
179 push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
181 my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
182 $start = $fakeop->save;
183 debug "cc_queue: name $name returns $start\n" if $debug_queue;
186 BEGIN { B::C::set_callback(\&cc_queue) }
188 sub valid_int { $_[0]->{flags} & VALID_INT }
189 sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
190 sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
191 sub valid_sv { $_[0]->{flags} & VALID_SV }
193 sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
194 sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
195 sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
196 sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
197 sub top_bool { @stack ? $stack[-1]->as_numeric : "SvTRUE(TOPs)" }
199 sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
200 sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
201 sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
202 sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
205 return ((pop @stack)->as_numeric);
207 # Careful: POPs has an auto-decrement and SvTRUE evaluates
208 # its argument more than once.
209 runtime("sv = POPs;");
214 sub write_back_lexicals {
215 my $avoid = shift || 0;
216 debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
219 foreach $lex (@pad) {
220 next unless ref($lex);
221 $lex->write_back unless $lex->{flags} & $avoid;
225 sub write_back_stack {
227 return unless @stack;
228 runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
229 foreach $obj (@stack) {
230 runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
235 sub invalidate_lexicals {
236 my $avoid = shift || 0;
237 debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
240 foreach $lex (@pad) {
241 next unless ref($lex);
242 $lex->invalidate unless $lex->{flags} & $avoid;
246 sub reload_lexicals {
248 foreach $lex (@pad) {
249 next unless ref($lex);
250 my $type = $lex->{type};
251 if ($type == T_INT) {
253 } elsif ($type == T_DOUBLE) {
262 package B::Pseudoreg;
264 # This class allocates pseudo-registers (OK, so they're C variables).
266 my %alloc; # Keyed by variable name. A value of 1 means the
267 # variable has been declared. A value of 2 means
270 sub new_scope { %alloc = () }
273 my ($class, $type, $prefix) = @_;
274 my ($ptr, $i, $varname, $status, $obj);
275 $prefix =~ s/^(\**)//;
279 $varname = "$prefix$i";
280 $status = $alloc{$varname};
281 } while $status == 2;
284 B::CC::declare($type, "$ptr$varname");
285 $alloc{$varname} = 2; # declared and in use
287 $obj = bless \$varname, $class;
292 $alloc{$$obj} = 1; # no longer in use but still declared
298 # This class gives a standard API for a perl object to shadow a
299 # C variable and only generate reloads/write-backs when necessary.
301 # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
302 # Use $obj->write_back whenever shadowed_c_var needs to be up to date.
303 # Use $obj->invalidate whenever an unknown function may have
307 my ($class, $write_back) = @_;
308 # Object fields are perl shadow variable, validity flag
309 # (for *C* variable) and callback sub for write_back
310 # (passed perl shadow variable as argument).
311 bless [undef, 1, $write_back], $class;
314 my ($obj, $newval) = @_;
315 $obj->[1] = 0; # C variable no longer valid
321 $obj->[1] = 1; # C variable will now be valid
322 &{$obj->[2]}($obj->[0]);
325 sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
327 my $curcop = new B::Shadow (sub {
328 my $opsym = shift->save;
329 runtime("curcop = (COP*)$opsym;");
333 # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
336 my $cxix = $#cxstack;
337 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
340 debug "dopoptoloop: returning $cxix" if $debug_cxstack;
346 my $cxix = $#cxstack;
347 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP
348 && $cxstack[$cxix]->{label} ne $label) {
351 debug "dopoptolabel: returning $cxix" if $debug_cxstack;
357 my $file = $curcop->[0]->filegv->SV->PV;
358 my $line = $curcop->[0]->line;
361 warn sprintf("%s:%d: $format\n", $file, $line, @_);
363 warn sprintf("%s:%d: %s\n", $file, $line, $format);
368 # Load pad takes (the elements of) a PADLIST as arguments and loads
369 # up @pad with Stackobj-derived objects which represent those lexicals.
370 # If/when perl itself can generate type information (my int $foo) then
371 # we'll take advantage of that here. Until then, we'll use various hacks
372 # to tell the compiler when we want a lexical to be a particular type
373 # or to be a register.
376 my ($namelistav, $valuelistav) = @_;
378 my @namelist = $namelistav->ARRAY;
379 my @valuelist = $valuelistav->ARRAY;
382 debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
383 # Temporary lexicals don't get named so it's possible for @valuelist
384 # to be strictly longer than @namelist. We count $ix up to the end of
385 # @valuelist but index into @namelist for the name. Any temporaries which
386 # run off the end of @namelist will make $namesv undefined and we treat
387 # that the same as having an explicit SPECIAL sv_undef object in @namelist.
388 # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
389 for ($ix = 1; $ix < @valuelist; $ix++) {
390 my $namesv = $namelist[$ix];
391 my $type = T_UNKNOWN;
394 my $class = class($namesv);
395 if (!defined($namesv) || $class eq "SPECIAL") {
396 # temporaries have &sv_undef instead of a PVNV for a name
397 $flags = VALID_SV|TEMPORARY|REGISTER;
399 if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
403 $flags = VALID_SV|VALID_INT;
404 } elsif ($2 eq "d") {
406 $flags = VALID_SV|VALID_DOUBLE;
408 $flags |= REGISTER if $3;
411 $pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
412 "i_$name", "d_$name");
413 declare("IV", $type == T_INT ? "i_$name = 0" : "i_$name");
414 declare("double", $type == T_DOUBLE ? "d_$name = 0" : "d_$name");
415 debug sprintf("curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
422 sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }
430 # XXX Preserve original label name for "real" labels?
431 return sprintf("lab_%x", $$op);
436 push_runtime(sprintf(" %s:", label($op)));
441 my $opsym = $op->save;
442 runtime("op = $opsym;") unless $know_op;
448 my $ppname = $op->ppaddr;
449 my $sym = loadop($op);
450 runtime("DOOP($ppname);");
457 my $flags = $op->flags;
458 return (($flags & OPf_KNOW) ? ($flags & OPf_LIST) : "dowantarray()");
462 # Code generation for PP code
472 my $gimme = gimme($op);
474 # XXX Change to push a constant sv_undef Stackobj onto @stack
476 runtime("if ($gimme != G_ARRAY) XPUSHs(&sv_undef);");
484 runtime("PP_UNSTACK;");
490 my $next = $op->next;
492 unshift(@bblock_todo, $next);
494 my $bool = pop_bool();
496 runtime(sprintf("if (!$bool) goto %s;", label($next)));
498 runtime(sprintf("if (!%s) goto %s;", top_bool(), label($next)),
506 my $next = $op->next;
508 unshift(@bblock_todo, $next);
510 my $obj = pop @stack;
512 runtime(sprintf("if (%s) { XPUSHs(%s); goto %s; }",
513 $obj->as_numeric, $obj->as_sv, label($next)));
515 runtime(sprintf("if (%s) goto %s;", top_bool(), label($next)),
523 my $false = $op->false;
524 unshift(@bblock_todo, $false);
526 my $bool = pop_bool();
528 runtime(sprintf("if (!$bool) goto %s;", label($false)));
536 push(@stack, $pad[$ix]);
537 if ($op->flags & OPf_MOD) {
538 my $private = $op->private;
539 if ($private & OPpLVAL_INTRO) {
540 runtime("SAVECLEARSV(curpad[$ix]);");
541 } elsif ($private & (OPpDEREF_HV|OPpDEREF_AV)) {
543 runtime("provide_ref(op, curpad[$ix]);");
544 $pad[$ix]->invalidate;
553 my $obj = $constobj{ad($sv)};
554 if (!defined($obj)) {
555 $obj = $constobj{ad($sv)} = new B::Stackobj::Const ($sv);
565 debug(sprintf("%s:%d\n", $op->filegv->SV->PV, $op->line)) if $debug_lineno;
566 runtime("TAINT_NOT;") unless $omit_taint;
567 runtime("sp = stack_base + cxstack[cxstack_ix].blk_oldsp;");
568 if ($freetmps_each_bblock || $freetmps_each_loop) {
571 runtime("FREETMPS;");
578 $curcop->invalidate; # XXX?
579 return default_pp($op);
582 sub pp_rv2gv { $curcop->write_back; default_pp(@_) }
583 sub pp_bless { $curcop->write_back; default_pp(@_) }
584 sub pp_repeat { $curcop->write_back; default_pp(@_) }
585 # The following subs need $curcop->write_back if we decide to support arybase:
586 # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
587 sub pp_sort { $curcop->write_back; default_pp(@_) }
588 sub pp_caller { $curcop->write_back; default_pp(@_) }
589 sub pp_reset { $curcop->write_back; default_pp(@_) }
593 my $gvsym = $op->gv->save;
595 runtime("XPUSHs((SV*)$gvsym);");
601 my $gvsym = $op->gv->save;
603 if ($op->private & OPpLVAL_INTRO) {
604 runtime("XPUSHs(save_scalar($gvsym));");
606 runtime("XPUSHs(GvSV($gvsym));");
613 my $gvsym = $op->gv->save;
614 my $ix = $op->private;
615 my $flag = $op->flags & OPf_MOD;
617 runtime("svp = av_fetch(GvAV($gvsym), $ix, $flag);",
618 "PUSHs(svp ? *svp : &sv_undef);");
623 my ($op, $operator) = @_;
624 if ($op->flags & OPf_STACKED) {
625 my $right = pop_int();
627 my $left = top_int();
628 $stack[-1]->set_int(&$operator($left, $right));
630 runtime(sprintf("sv_setiv(TOPs, %s);",&$operator("TOPi", $right)));
633 my $targ = $pad[$op->targ];
634 my $right = new B::Pseudoreg ("IV", "riv");
635 my $left = new B::Pseudoreg ("IV", "liv");
636 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int));
637 $targ->set_int(&$operator($$left, $$right));
643 sub INTS_CLOSED () { 0x1 }
644 sub INT_RESULT () { 0x2 }
645 sub NUMERIC_RESULT () { 0x4 }
648 my ($op, $operator, $flags) = @_;
650 $force_int ||= ($flags & INT_RESULT);
651 $force_int ||= ($flags & INTS_CLOSED && @stack >= 2
652 && valid_int($stack[-2]) && valid_int($stack[-1]));
653 if ($op->flags & OPf_STACKED) {
654 my $right = pop_numeric();
656 my $left = top_numeric();
658 $stack[-1]->set_int(&$operator($left, $right));
660 $stack[-1]->set_numeric(&$operator($left, $right));
664 runtime(sprintf("sv_setiv(TOPs, %s);",
665 &$operator("TOPi", $right)));
667 runtime(sprintf("sv_setnv(TOPs, %s);",
668 &$operator("TOPn", $right)));
672 my $targ = $pad[$op->targ];
673 $force_int ||= ($targ->{type} == T_INT);
675 my $right = new B::Pseudoreg ("IV", "riv");
676 my $left = new B::Pseudoreg ("IV", "liv");
677 runtime(sprintf("$$right = %s; $$left = %s;",
678 pop_numeric(), pop_numeric));
679 $targ->set_int(&$operator($$left, $$right));
681 my $right = new B::Pseudoreg ("double", "rnv");
682 my $left = new B::Pseudoreg ("double", "lnv");
683 runtime(sprintf("$$right = %s; $$left = %s;",
684 pop_numeric(), pop_numeric));
685 $targ->set_numeric(&$operator($$left, $$right));
693 my ($op, $operator, $flags) = @_;
694 if ($op->flags & OPf_STACKED) {
695 my $right = pop_sv();
698 if ($flags & INT_RESULT) {
699 $stack[-1]->set_int(&$operator($left, $right));
700 } elsif ($flags & NUMERIC_RESULT) {
701 $stack[-1]->set_numeric(&$operator($left, $right));
703 # XXX Does this work?
704 runtime(sprintf("sv_setsv($left, %s);",
705 &$operator($left, $right)));
706 $stack[-1]->invalidate;
710 if ($flags & INT_RESULT) {
712 } elsif ($flags & NUMERIC_RESULT) {
717 runtime(sprintf("%s(TOPs, %s);", $f, &$operator("TOPs", $right)));
720 my $targ = $pad[$op->targ];
721 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv));
722 if ($flags & INT_RESULT) {
723 $targ->set_int(&$operator("left", "right"));
724 } elsif ($flags & NUMERIC_RESULT) {
725 $targ->set_numeric(&$operator("left", "right"));
727 # XXX Does this work?
728 runtime(sprintf("sv_setsv(%s, %s);",
729 $targ->as_sv, &$operator("left", "right")));
738 my ($op, $operator) = @_;
739 my $right = new B::Pseudoreg ("IV", "riv");
740 my $left = new B::Pseudoreg ("IV", "liv");
741 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int()));
742 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
743 $bool->set_int(&$operator($$left, $$right));
748 sub bool_numeric_binop {
749 my ($op, $operator) = @_;
750 my $right = new B::Pseudoreg ("double", "rnv");
751 my $left = new B::Pseudoreg ("double", "lnv");
752 runtime(sprintf("$$right = %s; $$left = %s;",
753 pop_numeric(), pop_numeric()));
754 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
755 $bool->set_numeric(&$operator($$left, $$right));
761 my ($op, $operator) = @_;
762 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv()));
763 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
764 $bool->set_numeric(&$operator("left", "right"));
771 return sub { "$_[0] $opname $_[1]" }
776 return sub { sprintf("%s(%s)", $opname, join(", ", @_)) }
780 my $plus_op = infix_op("+");
781 my $minus_op = infix_op("-");
782 my $multiply_op = infix_op("*");
783 my $divide_op = infix_op("/");
784 my $modulo_op = infix_op("%");
785 my $lshift_op = infix_op("<<");
786 my $rshift_op = infix_op("<<");
787 my $ncmp_op = sub { "($_[0] > $_[1] ? 1 : ($_[0] < $_[1]) ? -1 : 0)" };
788 my $scmp_op = prefix_op("sv_cmp");
789 my $seq_op = prefix_op("sv_eq");
790 my $sne_op = prefix_op("!sv_eq");
791 my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
792 my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
793 my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
794 my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
795 my $eq_op = infix_op("==");
796 my $ne_op = infix_op("!=");
797 my $lt_op = infix_op("<");
798 my $gt_op = infix_op(">");
799 my $le_op = infix_op("<=");
800 my $ge_op = infix_op(">=");
803 # XXX The standard perl PP code has extra handling for
804 # some special case arguments of these operators.
806 sub pp_add { numeric_binop($_[0], $plus_op, INTS_CLOSED) }
807 sub pp_subtract { numeric_binop($_[0], $minus_op, INTS_CLOSED) }
808 sub pp_multiply { numeric_binop($_[0], $multiply_op, INTS_CLOSED) }
809 sub pp_divide { numeric_binop($_[0], $divide_op) }
810 sub pp_modulo { int_binop($_[0], $modulo_op) } # differs from perl's
811 sub pp_ncmp { numeric_binop($_[0], $ncmp_op, INT_RESULT) }
813 sub pp_left_shift { int_binop($_[0], $lshift_op) }
814 sub pp_right_shift { int_binop($_[0], $rshift_op) }
815 sub pp_i_add { int_binop($_[0], $plus_op) }
816 sub pp_i_subtract { int_binop($_[0], $minus_op) }
817 sub pp_i_multiply { int_binop($_[0], $multiply_op) }
818 sub pp_i_divide { int_binop($_[0], $divide_op) }
819 sub pp_i_modulo { int_binop($_[0], $modulo_op) }
821 sub pp_eq { bool_numeric_binop($_[0], $eq_op) }
822 sub pp_ne { bool_numeric_binop($_[0], $ne_op) }
823 sub pp_lt { bool_numeric_binop($_[0], $lt_op) }
824 sub pp_gt { bool_numeric_binop($_[0], $gt_op) }
825 sub pp_le { bool_numeric_binop($_[0], $le_op) }
826 sub pp_ge { bool_numeric_binop($_[0], $ge_op) }
828 sub pp_i_eq { bool_int_binop($_[0], $eq_op) }
829 sub pp_i_ne { bool_int_binop($_[0], $ne_op) }
830 sub pp_i_lt { bool_int_binop($_[0], $lt_op) }
831 sub pp_i_gt { bool_int_binop($_[0], $gt_op) }
832 sub pp_i_le { bool_int_binop($_[0], $le_op) }
833 sub pp_i_ge { bool_int_binop($_[0], $ge_op) }
835 sub pp_scmp { sv_binop($_[0], $scmp_op, INT_RESULT) }
836 sub pp_slt { bool_sv_binop($_[0], $slt_op) }
837 sub pp_sgt { bool_sv_binop($_[0], $sgt_op) }
838 sub pp_sle { bool_sv_binop($_[0], $sle_op) }
839 sub pp_sge { bool_sv_binop($_[0], $sge_op) }
840 sub pp_seq { bool_sv_binop($_[0], $seq_op) }
841 sub pp_sne { bool_sv_binop($_[0], $sne_op) }
847 my $backwards = $op->private & OPpASSIGN_BACKWARDS;
852 ($src, $dst) = ($dst, $src) if $backwards;
853 my $type = $src->{type};
854 if ($type == T_INT) {
855 $dst->set_int($src->as_int);
856 } elsif ($type == T_DOUBLE) {
857 $dst->set_numeric($src->as_numeric);
859 $dst->set_sv($src->as_sv);
862 } elsif (@stack == 1) {
864 my $src = pop @stack;
865 my $type = $src->{type};
866 runtime("if (tainting && tainted) TAINT_NOT;");
867 if ($type == T_INT) {
868 runtime sprintf("sv_setiv(TOPs, %s);", $src->as_int);
869 } elsif ($type == T_DOUBLE) {
870 runtime sprintf("sv_setnv(TOPs, %s);", $src->as_double);
872 runtime sprintf("sv_setsv(TOPs, %s);", $src->as_sv);
874 runtime("SvSETMAGIC(TOPs);");
876 my $dst = pop @stack;
877 my $type = $dst->{type};
878 runtime("sv = POPs;");
879 runtime("MAYBE_TAINT_SASSIGN_SRC(sv);");
880 if ($type == T_INT) {
881 $dst->set_int("SvIV(sv)");
882 } elsif ($type == T_DOUBLE) {
883 $dst->set_double("SvNV(sv)");
885 runtime("SvSetSV($dst->{sv}, sv);");
891 runtime("src = POPs; dst = TOPs;");
893 runtime("dst = POPs; src = TOPs;");
895 runtime("MAYBE_TAINT_SASSIGN_SRC(src);",
896 "SvSetSV(dst, src);",
906 my $obj = $stack[-1];
907 my $type = $obj->{type};
908 if ($type == T_INT || $type == T_DOUBLE) {
909 $obj->set_int($obj->as_int . " + 1");
911 runtime sprintf("PP_PREINC(%s);", $obj->as_sv);
915 runtime sprintf("PP_PREINC(TOPs);");
923 runtime("PUSHMARK(sp);");
930 my $gimme = gimme($op);
931 if ($gimme == 1) { # sic
932 runtime("POPMARK;"); # need this even though not a "full" pp_list
934 runtime("PP_LIST($gimme);");
941 write_back_lexicals(REGISTER|TEMPORARY);
944 runtime("if (op != ($sym)->op_next) op = (*op->op_ppaddr)();");
947 invalidate_lexicals(REGISTER|TEMPORARY);
954 write_back_lexicals(REGISTER|TEMPORARY);
956 my $sym = loadop($op);
957 my $ppaddr = $op->ppaddr;
958 runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
960 invalidate_lexicals(REGISTER|TEMPORARY);
964 sub pp_entereval { doeval(@_) }
965 sub pp_require { doeval(@_) }
966 sub pp_dofile { doeval(@_) }
971 write_back_lexicals(REGISTER|TEMPORARY);
974 my $jmpbuf = sprintf("jmpbuf%d", $jmpbuf_ix++);
975 declare("Sigjmp_buf", $jmpbuf);
976 runtime(sprintf("PP_ENTERTRY(%s,%s);", $jmpbuf, label($op->other->next)));
977 invalidate_lexicals(REGISTER|TEMPORARY);
983 if ($need_freetmps && $freetmps_each_loop) {
984 runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
989 return $op->next->other;
994 if ($need_freetmps && $freetmps_each_loop) {
995 runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
1000 return $op->next->other;
1005 my $next = $op->next;
1006 unshift(@bblock_todo, $next);
1007 write_back_lexicals();
1009 my $sym = doop($op);
1010 # pp_grepwhile can return either op_next or op_other and we need to
1011 # be able to distinguish the two at runtime. Since it's possible for
1012 # both ops to be "inlined", the fields could both be zero. To get
1013 # around that, we hack op_next to be our own op (purely because we
1014 # know it's a non-NULL pointer and can't be the same as op_other).
1015 push_init("((LOGOP*)$sym)->op_next = $sym;");
1016 runtime(sprintf("if (op == ($sym)->op_next) goto %s;", label($next)));
1027 write_back_lexicals(REGISTER|TEMPORARY);
1030 runtime("PUTBACK;", "return 0;");
1037 warn sprintf("%s not yet implemented properly\n", $op->ppaddr);
1038 return default_pp($op);
1043 my $flags = $op->flags;
1044 if (!($flags & OPf_KNOW)) {
1045 error("context of range unknown at compile-time");
1047 write_back_lexicals();
1049 if (!($flags & OPf_LIST)) {
1050 # We need to save our UNOP structure since pp_flop uses
1051 # it to find and adjust out targ. We don't need it ourselves.
1053 runtime sprintf("if (SvTRUE(curpad[%d])) goto %s;",
1054 $op->targ, label($op->false));
1055 unshift(@bblock_todo, $op->false);
1062 my $flags = $op->flags;
1063 if (!($flags & OPf_KNOW)) {
1064 error("context of flip unknown at compile-time");
1066 if ($flags & OPf_LIST) {
1067 return $op->first->false;
1069 write_back_lexicals();
1071 # We need to save our UNOP structure since pp_flop uses
1072 # it to find and adjust out targ. We don't need it ourselves.
1075 my $rangeix = $op->first->targ;
1076 runtime(($op->private & OPpFLIP_LINENUM) ?
1077 "if (last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(last_in_gv))) {"
1078 : "if (SvTRUE(TOPs)) {");
1079 runtime("\tsv_setiv(curpad[$rangeix], 1);");
1080 if ($op->flags & OPf_SPECIAL) {
1081 runtime("sv_setiv(curpad[$ix], 1);");
1083 runtime("\tsv_setiv(curpad[$ix], 0);",
1085 sprintf("\tgoto %s;", label($op->first->false)));
1088 qq{sv_setpv(curpad[$ix], "");},
1089 "SETs(curpad[$ix]);");
1103 my $nextop = $op->nextop;
1104 my $lastop = $op->lastop;
1105 my $redoop = $op->redoop;
1106 $curcop->write_back;
1107 debug "enterloop: pushing on cxstack" if $debug_cxstack;
1111 "label" => $curcop->[0]->label,
1119 return default_pp($op);
1122 sub pp_enterloop { enterloop(@_) }
1123 sub pp_enteriter { enterloop(@_) }
1128 die "panic: leaveloop";
1130 debug "leaveloop: popping from cxstack" if $debug_cxstack;
1132 return default_pp($op);
1138 if ($op->flags & OPf_SPECIAL) {
1139 $cxix = dopoptoloop();
1141 error('"next" used outside loop');
1142 return $op->next; # ignore the op
1145 $cxix = dopoptolabel($op->pv);
1147 error('Label not found at compile time for "next %s"', $op->pv);
1148 return $op->next; # ignore the op
1152 my $nextop = $cxstack[$cxix]->{nextop};
1153 push(@bblock_todo, $nextop);
1154 runtime(sprintf("goto %s;", label($nextop)));
1161 if ($op->flags & OPf_SPECIAL) {
1162 $cxix = dopoptoloop();
1164 error('"redo" used outside loop');
1165 return $op->next; # ignore the op
1168 $cxix = dopoptolabel($op->pv);
1170 error('Label not found at compile time for "redo %s"', $op->pv);
1171 return $op->next; # ignore the op
1175 my $redoop = $cxstack[$cxix]->{redoop};
1176 push(@bblock_todo, $redoop);
1177 runtime(sprintf("goto %s;", label($redoop)));
1184 if ($op->flags & OPf_SPECIAL) {
1185 $cxix = dopoptoloop();
1187 error('"last" used outside loop');
1188 return $op->next; # ignore the op
1191 $cxix = dopoptolabel($op->pv);
1193 error('Label not found at compile time for "last %s"', $op->pv);
1194 return $op->next; # ignore the op
1196 # XXX Add support for "last" to leave non-loop blocks
1197 if ($cxstack[$cxix]->{type} != CXt_LOOP) {
1198 error('Use of "last" for non-loop blocks is not yet implemented');
1199 return $op->next; # ignore the op
1203 my $lastop = $cxstack[$cxix]->{lastop}->next;
1204 push(@bblock_todo, $lastop);
1205 runtime(sprintf("goto %s;", label($lastop)));
1211 write_back_lexicals();
1213 my $sym = doop($op);
1214 my $replroot = $op->pmreplroot;
1215 if (ad($replroot)) {
1216 runtime sprintf("if (op == ((PMOP*)(%s))->op_pmreplroot) goto %s;",
1217 $sym, label($replroot));
1218 $op->pmreplstart->save;
1219 push(@bblock_todo, $replroot);
1221 invalidate_lexicals();
1227 write_back_lexicals();
1230 my $pmop = $op->other;
1231 my $pmopsym = objsym($pmop);
1232 runtime sprintf("if (op == ((PMOP*)(%s))->op_pmreplstart) goto %s;",
1233 $pmopsym, label($pmop->pmreplstart));
1234 invalidate_lexicals();
1240 my $ppname = $op->ppaddr;
1241 write_back_lexicals() unless $skip_lexicals{$ppname};
1242 write_back_stack() unless $skip_stack{$ppname};
1244 # XXX If the only way that ops can write to a TEMPORARY lexical is
1245 # when it's named in $op->targ then we could call
1246 # invalidate_lexicals(TEMPORARY) and avoid having to write back all
1247 # the temporaries. For now, we'll play it safe and write back the lot.
1248 invalidate_lexicals() unless $skip_invalidate{$ppname};
1254 my $ppname = $op->ppaddr;
1255 if (exists $ignore_op{$ppname}) {
1258 debug peek_stack() if $debug_stack;
1260 debug sprintf("%s [%s]\n",
1262 $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ);
1265 if (defined(&$ppname)) {
1267 return &$ppname($op);
1269 return default_pp($op);
1273 sub compile_bblock {
1275 #warn "compile_bblock: ", peekop($op), "\n"; # debug
1279 $op = compile_op($op);
1280 } while (defined($op) && $$op && !exists($leaders->{$$op}));
1281 write_back_stack(); # boo hoo: big loss
1287 my ($name, $root, $start, @padlist) = @_;
1291 B::Pseudoreg->new_scope;
1293 if ($debug_timings) {
1294 warn sprintf("Basic block analysis at %s\n", timing_info);
1296 $leaders = find_leaders($root, $start);
1297 @bblock_todo = ($start, values %$leaders);
1298 if ($debug_timings) {
1299 warn sprintf("Compilation at %s\n", timing_info);
1301 while (@bblock_todo) {
1302 $op = shift @bblock_todo;
1303 #warn sprintf("Considering basic block %s\n", peekop($op)); # debug
1304 next if !defined($op) || !$$op || $done{$$op};
1305 #warn "...compiling it\n"; # debug
1308 $op = compile_bblock($op);
1309 if ($need_freetmps && $freetmps_each_bblock) {
1310 runtime("FREETMPS;");
1313 } while defined($op) && $$op && !$done{$$op};
1314 if ($need_freetmps && $freetmps_each_loop) {
1315 runtime("FREETMPS;");
1319 runtime("PUTBACK;", "return 0;");
1320 } elsif ($done{$$op}) {
1321 runtime(sprintf("goto %s;", label($op)));
1324 if ($debug_timings) {
1325 warn sprintf("Saving runtime at %s\n", timing_info);
1333 $start = cc_queue(@_) if @_;
1334 while ($ccinfo = shift @cc_todo) {
1341 my ($name, $cvref) = @_;
1342 my $cv = svref_2object($cvref);
1343 my @padlist = $cv->PADLIST->ARRAY;
1344 my $curpad_sym = $padlist[1]->save;
1345 cc_recurse($name, $cv->ROOT, $cv->START, @padlist);
1349 my @comppadlist = comppadlist->ARRAY;
1350 my $curpad_sym = $comppadlist[1]->save;
1351 my $start = cc_recurse("pp_main", main_root, main_start, @comppadlist);
1352 if (@unused_sub_packages) {
1353 save_unused_subs(@unused_sub_packages);
1354 # That only queues them. Now we need to generate code for them.
1358 push_init(sprintf("main_root = sym_%x;", ad(main_root)),
1359 "main_start = $start;",
1360 "curpad = AvARRAY($curpad_sym);");
1361 output_boilerplate();
1363 output_all("perl_init");
1367 if ($debug_timings) {
1368 warn sprintf("Done at %s\n", timing_info);
1374 my ($option, $opt, $arg);
1376 while ($option = shift @options) {
1377 if ($option =~ /^-(.)(.*)/) {
1381 unshift @options, $option;
1384 if ($opt eq "-" && $arg eq "-") {
1387 } elsif ($opt eq "o") {
1388 $arg ||= shift @options;
1389 open(STDOUT, ">$arg") or return "$arg: $!\n";
1390 } elsif ($opt eq "n") {
1391 $arg ||= shift @options;
1392 $module_name = $arg;
1393 } elsif ($opt eq "u") {
1394 $arg ||= shift @options;
1395 push(@unused_sub_packages, $arg);
1396 } elsif ($opt eq "f") {
1397 $arg ||= shift @options;
1398 my $value = $arg !~ s/^no-//;
1400 my $ref = $optimise{$arg};
1401 if (defined($ref)) {
1404 warn qq(ignoring unknown optimisation option "$arg"\n);
1406 } elsif ($opt eq "O") {
1407 $arg = 1 if $arg eq "";
1409 foreach $ref (values %optimise) {
1413 $freetmps_each_loop = 1;
1416 $freetmps_each_bblock = 1 unless $freetmps_each_loop;
1418 } elsif ($opt eq "D") {
1419 $arg ||= shift @options;
1420 foreach $arg (split(//, $arg)) {
1423 } elsif ($arg eq "O") {
1425 } elsif ($arg eq "s") {
1427 } elsif ($arg eq "c") {
1429 } elsif ($arg eq "p") {
1431 } elsif ($arg eq "r") {
1433 } elsif ($arg eq "S") {
1435 } elsif ($arg eq "q") {
1437 } elsif ($arg eq "l") {
1439 } elsif ($arg eq "t") {
1448 my ($objname, $ppname);
1449 foreach $objname (@options) {
1450 $objname = "main::$objname" unless $objname =~ /::/;
1451 ($ppname = $objname) =~ s/^.*?:://;
1452 eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
1453 die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
1456 output_boilerplate();
1458 output_all($module_name || "init_module");
1462 return sub { cc_main() };