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
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 $module; # module name (when compiled with -m)
40 my %done; # hash keyed by $$op of leaders of basic blocks
41 # which have already been done.
42 my $leaders; # ref to hash of basic block leaders. Keys are $$op
43 # addresses, values are the $op objects themselves.
44 my @bblock_todo; # list of leaders of basic blocks that need visiting
46 my @cc_todo; # list of tuples defining what PP code needs to be
47 # saved (e.g. CV, main or PMOP repl code). Each tuple
48 # is [$name, $root, $start, @padlist]. PMOP repl code
49 # tuples inherit padlist.
50 my @stack; # shadows perl's stack when contents are known.
51 # Values are objects derived from class B::Stackobj
52 my @pad; # Lexicals in current pad as Stackobj-derived objects
53 my @padlist; # Copy of current padlist so PMOP repl code can find it
54 my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
55 my $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufs
56 my %constobj; # OP_CONST constants as Stackobj-derived objects
58 my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
59 # block or even to the end of each loop of blocks,
60 # depending on optimisation options.
61 my $know_op = 0; # Set when C variable op already holds the right op
62 # (from an immediately preceding DOOP(ppname)).
63 my $errors = 0; # Number of errors encountered
64 my %skip_stack; # Hash of PP names which don't need write_back_stack
65 my %skip_lexicals; # Hash of PP names which don't need write_back_lexicals
66 my %skip_invalidate; # Hash of PP names which don't need invalidate_lexicals
67 my %ignore_op; # Hash of ops which do nothing except returning op_next
70 foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) {
75 my @unused_sub_packages; # list of packages (given by -u options) to search
76 # explicitly and save every sub we find there, even
77 # if apparently unused (could be only referenced from
78 # an eval "" or from a $SIG{FOO} = "bar").
81 my ($debug_op, $debug_stack, $debug_cxstack, $debug_pad, $debug_runtime,
82 $debug_shadow, $debug_queue, $debug_lineno, $debug_timings);
84 # Optimisation options. On the command line, use hyphens instead of
85 # underscores for compatibility with gcc-style options. We use
86 # underscores here because they are OK in (strict) barewords.
87 my ($freetmps_each_bblock, $freetmps_each_loop, $omit_taint);
88 my %optimise = (freetmps_each_bblock => \$freetmps_each_bblock,
89 freetmps_each_loop => \$freetmps_each_loop,
90 omit_taint => \$omit_taint);
92 # Could rewrite push_runtime() and output_runtime() to use a
93 # temporary file if memory is at a premium.
94 my $ppname; # name of current fake PP function
96 my $declare_ref; # Hash ref keyed by C variable type of declarations.
98 my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
99 # tuples to be written out.
101 sub init_hash { map { $_ => 1 } @_ }
104 # Initialise the hashes for the default PP functions where we can avoid
105 # either write_back_stack, write_back_lexicals or invalidate_lexicals.
107 %skip_lexicals = init_hash qw(pp_enter pp_enterloop);
108 %skip_invalidate = init_hash qw(pp_enter pp_enterloop);
111 if ($debug_runtime) {
114 runtime(map { chomp; "/* $_ */"} @_);
119 my ($type, $var) = @_;
120 push(@{$declare_ref->{$type}}, $var);
124 push(@$runtime_list_ref, @_);
125 warn join("\n", @_) . "\n" if $debug_runtime;
129 push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
134 print qq(#include "cc_runtime.h"\n);
135 foreach $ppdata (@pp_list) {
136 my ($name, $runtime, $declare) = @$ppdata;
137 print "\nstatic\nPP($name)\n{\n";
138 my ($type, $varlist, $line);
139 while (($type, $varlist) = each %$declare) {
140 print "\t$type ", join(", ", @$varlist), ";\n";
142 foreach $line (@$runtime) {
152 push_runtime("\t$line");
158 $runtime_list_ref = [];
161 declare("I32", "oldsave");
162 declare("SV", "**svp");
163 map { declare("SV", "*$_") } qw(sv src dst left right);
164 declare("MAGIC", "*mg");
165 push_decl("static OP * $ppname _((ARGSproto));");
166 debug "init_pp: $ppname\n" if $debug_queue;
169 # Initialise runtime_callback function for Stackobj class
170 BEGIN { B::Stackobj::set_callback(\&runtime) }
172 # Initialise saveoptree_callback for B::C class
174 my ($name, $root, $start, @pl) = @_;
175 debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
177 if ($name eq "*ignore*") {
180 push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
182 my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
183 $start = $fakeop->save;
184 debug "cc_queue: name $name returns $start\n" if $debug_queue;
187 BEGIN { B::C::set_callback(\&cc_queue) }
189 sub valid_int { $_[0]->{flags} & VALID_INT }
190 sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
191 sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
192 sub valid_sv { $_[0]->{flags} & VALID_SV }
194 sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
195 sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
196 sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
197 sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
198 sub top_bool { @stack ? $stack[-1]->as_numeric : "SvTRUE(TOPs)" }
200 sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
201 sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
202 sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
203 sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
206 return ((pop @stack)->as_numeric);
208 # Careful: POPs has an auto-decrement and SvTRUE evaluates
209 # its argument more than once.
210 runtime("sv = POPs;");
215 sub write_back_lexicals {
216 my $avoid = shift || 0;
217 debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
220 foreach $lex (@pad) {
221 next unless ref($lex);
222 $lex->write_back unless $lex->{flags} & $avoid;
226 sub write_back_stack {
228 return unless @stack;
229 runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
230 foreach $obj (@stack) {
231 runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
236 sub invalidate_lexicals {
237 my $avoid = shift || 0;
238 debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
241 foreach $lex (@pad) {
242 next unless ref($lex);
243 $lex->invalidate unless $lex->{flags} & $avoid;
247 sub reload_lexicals {
249 foreach $lex (@pad) {
250 next unless ref($lex);
251 my $type = $lex->{type};
252 if ($type == T_INT) {
254 } elsif ($type == T_DOUBLE) {
263 package B::Pseudoreg;
265 # This class allocates pseudo-registers (OK, so they're C variables).
267 my %alloc; # Keyed by variable name. A value of 1 means the
268 # variable has been declared. A value of 2 means
271 sub new_scope { %alloc = () }
274 my ($class, $type, $prefix) = @_;
275 my ($ptr, $i, $varname, $status, $obj);
276 $prefix =~ s/^(\**)//;
280 $varname = "$prefix$i";
281 $status = $alloc{$varname};
282 } while $status == 2;
285 B::CC::declare($type, "$ptr$varname");
286 $alloc{$varname} = 2; # declared and in use
288 $obj = bless \$varname, $class;
293 $alloc{$$obj} = 1; # no longer in use but still declared
299 # This class gives a standard API for a perl object to shadow a
300 # C variable and only generate reloads/write-backs when necessary.
302 # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
303 # Use $obj->write_back whenever shadowed_c_var needs to be up to date.
304 # Use $obj->invalidate whenever an unknown function may have
308 my ($class, $write_back) = @_;
309 # Object fields are perl shadow variable, validity flag
310 # (for *C* variable) and callback sub for write_back
311 # (passed perl shadow variable as argument).
312 bless [undef, 1, $write_back], $class;
315 my ($obj, $newval) = @_;
316 $obj->[1] = 0; # C variable no longer valid
322 $obj->[1] = 1; # C variable will now be valid
323 &{$obj->[2]}($obj->[0]);
326 sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
328 my $curcop = new B::Shadow (sub {
329 my $opsym = shift->save;
330 runtime("curcop = (COP*)$opsym;");
334 # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
337 my $cxix = $#cxstack;
338 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
341 debug "dopoptoloop: returning $cxix" if $debug_cxstack;
347 my $cxix = $#cxstack;
348 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP
349 && $cxstack[$cxix]->{label} ne $label) {
352 debug "dopoptolabel: returning $cxix" if $debug_cxstack;
358 my $file = $curcop->[0]->filegv->SV->PV;
359 my $line = $curcop->[0]->line;
362 warn sprintf("%s:%d: $format\n", $file, $line, @_);
364 warn sprintf("%s:%d: %s\n", $file, $line, $format);
369 # Load pad takes (the elements of) a PADLIST as arguments and loads
370 # up @pad with Stackobj-derived objects which represent those lexicals.
371 # If/when perl itself can generate type information (my int $foo) then
372 # we'll take advantage of that here. Until then, we'll use various hacks
373 # to tell the compiler when we want a lexical to be a particular type
374 # or to be a register.
377 my ($namelistav, $valuelistav) = @_;
379 my @namelist = $namelistav->ARRAY;
380 my @valuelist = $valuelistav->ARRAY;
383 debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
384 # Temporary lexicals don't get named so it's possible for @valuelist
385 # to be strictly longer than @namelist. We count $ix up to the end of
386 # @valuelist but index into @namelist for the name. Any temporaries which
387 # run off the end of @namelist will make $namesv undefined and we treat
388 # that the same as having an explicit SPECIAL sv_undef object in @namelist.
389 # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
390 for ($ix = 1; $ix < @valuelist; $ix++) {
391 my $namesv = $namelist[$ix];
392 my $type = T_UNKNOWN;
395 my $class = class($namesv);
396 if (!defined($namesv) || $class eq "SPECIAL") {
397 # temporaries have &sv_undef instead of a PVNV for a name
398 $flags = VALID_SV|TEMPORARY|REGISTER;
400 if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
404 $flags = VALID_SV|VALID_INT;
405 } elsif ($2 eq "d") {
407 $flags = VALID_SV|VALID_DOUBLE;
409 $flags |= REGISTER if $3;
412 $pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
413 "i_$name", "d_$name");
414 declare("IV", $type == T_INT ? "i_$name = 0" : "i_$name");
415 declare("double", $type == T_DOUBLE ? "d_$name = 0" : "d_$name");
416 debug sprintf("curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
423 sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }
431 # XXX Preserve original label name for "real" labels?
432 return sprintf("lab_%x", $$op);
437 push_runtime(sprintf(" %s:", label($op)));
442 my $opsym = $op->save;
443 runtime("op = $opsym;") unless $know_op;
449 my $ppname = $op->ppaddr;
450 my $sym = loadop($op);
451 runtime("DOOP($ppname);");
458 my $flags = $op->flags;
459 return (($flags & OPf_KNOW) ? ($flags & OPf_LIST) : "dowantarray()");
463 # Code generation for PP code
473 my $gimme = gimme($op);
475 # XXX Change to push a constant sv_undef Stackobj onto @stack
477 runtime("if ($gimme != G_ARRAY) XPUSHs(&sv_undef);");
485 runtime("PP_UNSTACK;");
491 my $next = $op->next;
493 unshift(@bblock_todo, $next);
495 my $bool = pop_bool();
497 runtime(sprintf("if (!$bool) goto %s;", label($next)));
499 runtime(sprintf("if (!%s) goto %s;", top_bool(), label($next)),
507 my $next = $op->next;
509 unshift(@bblock_todo, $next);
511 my $obj = pop @stack;
513 runtime(sprintf("if (%s) { XPUSHs(%s); goto %s; }",
514 $obj->as_numeric, $obj->as_sv, label($next)));
516 runtime(sprintf("if (%s) goto %s;", top_bool(), label($next)),
524 my $false = $op->false;
525 unshift(@bblock_todo, $false);
527 my $bool = pop_bool();
529 runtime(sprintf("if (!$bool) goto %s;", label($false)));
537 push(@stack, $pad[$ix]);
538 if ($op->flags & OPf_MOD) {
539 my $private = $op->private;
540 if ($private & OPpLVAL_INTRO) {
541 runtime("SAVECLEARSV(curpad[$ix]);");
542 } elsif ($private & (OPpDEREF_HV|OPpDEREF_AV)) {
544 runtime("provide_ref(op, curpad[$ix]);");
545 $pad[$ix]->invalidate;
554 my $obj = $constobj{$$sv};
555 if (!defined($obj)) {
556 $obj = $constobj{$$sv} = new B::Stackobj::Const ($sv);
566 debug(sprintf("%s:%d\n", $op->filegv->SV->PV, $op->line)) if $debug_lineno;
567 runtime("TAINT_NOT;") unless $omit_taint;
568 runtime("sp = stack_base + cxstack[cxstack_ix].blk_oldsp;");
569 if ($freetmps_each_bblock || $freetmps_each_loop) {
572 runtime("FREETMPS;");
579 $curcop->invalidate; # XXX?
580 return default_pp($op);
583 sub pp_rv2gv { $curcop->write_back; default_pp(@_) }
584 sub pp_bless { $curcop->write_back; default_pp(@_) }
585 sub pp_repeat { $curcop->write_back; default_pp(@_) }
586 # The following subs need $curcop->write_back if we decide to support arybase:
587 # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
588 sub pp_sort { $curcop->write_back; default_pp(@_) }
589 sub pp_caller { $curcop->write_back; default_pp(@_) }
590 sub pp_reset { $curcop->write_back; default_pp(@_) }
594 my $gvsym = $op->gv->save;
596 runtime("XPUSHs((SV*)$gvsym);");
602 my $gvsym = $op->gv->save;
604 if ($op->private & OPpLVAL_INTRO) {
605 runtime("XPUSHs(save_scalar($gvsym));");
607 runtime("XPUSHs(GvSV($gvsym));");
614 my $gvsym = $op->gv->save;
615 my $ix = $op->private;
616 my $flag = $op->flags & OPf_MOD;
618 runtime("svp = av_fetch(GvAV($gvsym), $ix, $flag);",
619 "PUSHs(svp ? *svp : &sv_undef);");
624 my ($op, $operator) = @_;
625 if ($op->flags & OPf_STACKED) {
626 my $right = pop_int();
628 my $left = top_int();
629 $stack[-1]->set_int(&$operator($left, $right));
631 runtime(sprintf("sv_setiv(TOPs, %s);",&$operator("TOPi", $right)));
634 my $targ = $pad[$op->targ];
635 my $right = new B::Pseudoreg ("IV", "riv");
636 my $left = new B::Pseudoreg ("IV", "liv");
637 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int));
638 $targ->set_int(&$operator($$left, $$right));
644 sub INTS_CLOSED () { 0x1 }
645 sub INT_RESULT () { 0x2 }
646 sub NUMERIC_RESULT () { 0x4 }
649 my ($op, $operator, $flags) = @_;
651 $force_int ||= ($flags & INT_RESULT);
652 $force_int ||= ($flags & INTS_CLOSED && @stack >= 2
653 && valid_int($stack[-2]) && valid_int($stack[-1]));
654 if ($op->flags & OPf_STACKED) {
655 my $right = pop_numeric();
657 my $left = top_numeric();
659 $stack[-1]->set_int(&$operator($left, $right));
661 $stack[-1]->set_numeric(&$operator($left, $right));
665 runtime(sprintf("sv_setiv(TOPs, %s);",
666 &$operator("TOPi", $right)));
668 runtime(sprintf("sv_setnv(TOPs, %s);",
669 &$operator("TOPn", $right)));
673 my $targ = $pad[$op->targ];
674 $force_int ||= ($targ->{type} == T_INT);
676 my $right = new B::Pseudoreg ("IV", "riv");
677 my $left = new B::Pseudoreg ("IV", "liv");
678 runtime(sprintf("$$right = %s; $$left = %s;",
679 pop_numeric(), pop_numeric));
680 $targ->set_int(&$operator($$left, $$right));
682 my $right = new B::Pseudoreg ("double", "rnv");
683 my $left = new B::Pseudoreg ("double", "lnv");
684 runtime(sprintf("$$right = %s; $$left = %s;",
685 pop_numeric(), pop_numeric));
686 $targ->set_numeric(&$operator($$left, $$right));
694 my ($op, $operator, $flags) = @_;
695 if ($op->flags & OPf_STACKED) {
696 my $right = pop_sv();
699 if ($flags & INT_RESULT) {
700 $stack[-1]->set_int(&$operator($left, $right));
701 } elsif ($flags & NUMERIC_RESULT) {
702 $stack[-1]->set_numeric(&$operator($left, $right));
704 # XXX Does this work?
705 runtime(sprintf("sv_setsv($left, %s);",
706 &$operator($left, $right)));
707 $stack[-1]->invalidate;
711 if ($flags & INT_RESULT) {
713 } elsif ($flags & NUMERIC_RESULT) {
718 runtime(sprintf("%s(TOPs, %s);", $f, &$operator("TOPs", $right)));
721 my $targ = $pad[$op->targ];
722 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv));
723 if ($flags & INT_RESULT) {
724 $targ->set_int(&$operator("left", "right"));
725 } elsif ($flags & NUMERIC_RESULT) {
726 $targ->set_numeric(&$operator("left", "right"));
728 # XXX Does this work?
729 runtime(sprintf("sv_setsv(%s, %s);",
730 $targ->as_sv, &$operator("left", "right")));
739 my ($op, $operator) = @_;
740 my $right = new B::Pseudoreg ("IV", "riv");
741 my $left = new B::Pseudoreg ("IV", "liv");
742 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int()));
743 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
744 $bool->set_int(&$operator($$left, $$right));
749 sub bool_numeric_binop {
750 my ($op, $operator) = @_;
751 my $right = new B::Pseudoreg ("double", "rnv");
752 my $left = new B::Pseudoreg ("double", "lnv");
753 runtime(sprintf("$$right = %s; $$left = %s;",
754 pop_numeric(), pop_numeric()));
755 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
756 $bool->set_numeric(&$operator($$left, $$right));
762 my ($op, $operator) = @_;
763 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv()));
764 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
765 $bool->set_numeric(&$operator("left", "right"));
772 return sub { "$_[0] $opname $_[1]" }
777 return sub { sprintf("%s(%s)", $opname, join(", ", @_)) }
781 my $plus_op = infix_op("+");
782 my $minus_op = infix_op("-");
783 my $multiply_op = infix_op("*");
784 my $divide_op = infix_op("/");
785 my $modulo_op = infix_op("%");
786 my $lshift_op = infix_op("<<");
787 my $rshift_op = infix_op("<<");
788 my $ncmp_op = sub { "($_[0] > $_[1] ? 1 : ($_[0] < $_[1]) ? -1 : 0)" };
789 my $scmp_op = prefix_op("sv_cmp");
790 my $seq_op = prefix_op("sv_eq");
791 my $sne_op = prefix_op("!sv_eq");
792 my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
793 my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
794 my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
795 my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
796 my $eq_op = infix_op("==");
797 my $ne_op = infix_op("!=");
798 my $lt_op = infix_op("<");
799 my $gt_op = infix_op(">");
800 my $le_op = infix_op("<=");
801 my $ge_op = infix_op(">=");
804 # XXX The standard perl PP code has extra handling for
805 # some special case arguments of these operators.
807 sub pp_add { numeric_binop($_[0], $plus_op, INTS_CLOSED) }
808 sub pp_subtract { numeric_binop($_[0], $minus_op, INTS_CLOSED) }
809 sub pp_multiply { numeric_binop($_[0], $multiply_op, INTS_CLOSED) }
810 sub pp_divide { numeric_binop($_[0], $divide_op) }
811 sub pp_modulo { int_binop($_[0], $modulo_op) } # differs from perl's
812 sub pp_ncmp { numeric_binop($_[0], $ncmp_op, INT_RESULT) }
814 sub pp_left_shift { int_binop($_[0], $lshift_op) }
815 sub pp_right_shift { int_binop($_[0], $rshift_op) }
816 sub pp_i_add { int_binop($_[0], $plus_op) }
817 sub pp_i_subtract { int_binop($_[0], $minus_op) }
818 sub pp_i_multiply { int_binop($_[0], $multiply_op) }
819 sub pp_i_divide { int_binop($_[0], $divide_op) }
820 sub pp_i_modulo { int_binop($_[0], $modulo_op) }
822 sub pp_eq { bool_numeric_binop($_[0], $eq_op) }
823 sub pp_ne { bool_numeric_binop($_[0], $ne_op) }
824 sub pp_lt { bool_numeric_binop($_[0], $lt_op) }
825 sub pp_gt { bool_numeric_binop($_[0], $gt_op) }
826 sub pp_le { bool_numeric_binop($_[0], $le_op) }
827 sub pp_ge { bool_numeric_binop($_[0], $ge_op) }
829 sub pp_i_eq { bool_int_binop($_[0], $eq_op) }
830 sub pp_i_ne { bool_int_binop($_[0], $ne_op) }
831 sub pp_i_lt { bool_int_binop($_[0], $lt_op) }
832 sub pp_i_gt { bool_int_binop($_[0], $gt_op) }
833 sub pp_i_le { bool_int_binop($_[0], $le_op) }
834 sub pp_i_ge { bool_int_binop($_[0], $ge_op) }
836 sub pp_scmp { sv_binop($_[0], $scmp_op, INT_RESULT) }
837 sub pp_slt { bool_sv_binop($_[0], $slt_op) }
838 sub pp_sgt { bool_sv_binop($_[0], $sgt_op) }
839 sub pp_sle { bool_sv_binop($_[0], $sle_op) }
840 sub pp_sge { bool_sv_binop($_[0], $sge_op) }
841 sub pp_seq { bool_sv_binop($_[0], $seq_op) }
842 sub pp_sne { bool_sv_binop($_[0], $sne_op) }
848 my $backwards = $op->private & OPpASSIGN_BACKWARDS;
853 ($src, $dst) = ($dst, $src) if $backwards;
854 my $type = $src->{type};
855 if ($type == T_INT) {
856 $dst->set_int($src->as_int);
857 } elsif ($type == T_DOUBLE) {
858 $dst->set_numeric($src->as_numeric);
860 $dst->set_sv($src->as_sv);
863 } elsif (@stack == 1) {
865 my $src = pop @stack;
866 my $type = $src->{type};
867 runtime("if (tainting && tainted) TAINT_NOT;");
868 if ($type == T_INT) {
869 runtime sprintf("sv_setiv(TOPs, %s);", $src->as_int);
870 } elsif ($type == T_DOUBLE) {
871 runtime sprintf("sv_setnv(TOPs, %s);", $src->as_double);
873 runtime sprintf("sv_setsv(TOPs, %s);", $src->as_sv);
875 runtime("SvSETMAGIC(TOPs);");
877 my $dst = pop @stack;
878 my $type = $dst->{type};
879 runtime("sv = POPs;");
880 runtime("MAYBE_TAINT_SASSIGN_SRC(sv);");
881 if ($type == T_INT) {
882 $dst->set_int("SvIV(sv)");
883 } elsif ($type == T_DOUBLE) {
884 $dst->set_double("SvNV(sv)");
886 runtime("SvSetSV($dst->{sv}, sv);");
892 runtime("src = POPs; dst = TOPs;");
894 runtime("dst = POPs; src = TOPs;");
896 runtime("MAYBE_TAINT_SASSIGN_SRC(src);",
897 "SvSetSV(dst, src);",
907 my $obj = $stack[-1];
908 my $type = $obj->{type};
909 if ($type == T_INT || $type == T_DOUBLE) {
910 $obj->set_int($obj->as_int . " + 1");
912 runtime sprintf("PP_PREINC(%s);", $obj->as_sv);
916 runtime sprintf("PP_PREINC(TOPs);");
924 runtime("PUSHMARK(sp);");
931 my $gimme = gimme($op);
932 if ($gimme == 1) { # sic
933 runtime("POPMARK;"); # need this even though not a "full" pp_list
935 runtime("PP_LIST($gimme);");
942 write_back_lexicals(REGISTER|TEMPORARY);
945 runtime("if (op != ($sym)->op_next) op = (*op->op_ppaddr)();");
948 invalidate_lexicals(REGISTER|TEMPORARY);
959 write_back_lexicals(REGISTER|TEMPORARY);
962 # XXX Is this the right way to distinguish between it returning
963 # CvSTART(cv) (via doform) and pop_return()?
964 runtime("if (op) op = (*op->op_ppaddr)();");
967 invalidate_lexicals(REGISTER|TEMPORARY);
974 write_back_lexicals(REGISTER|TEMPORARY);
976 my $sym = loadop($op);
977 my $ppaddr = $op->ppaddr;
978 runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
980 invalidate_lexicals(REGISTER|TEMPORARY);
984 sub pp_entereval { doeval(@_) }
985 sub pp_require { doeval(@_) }
986 sub pp_dofile { doeval(@_) }
991 write_back_lexicals(REGISTER|TEMPORARY);
994 my $jmpbuf = sprintf("jmpbuf%d", $jmpbuf_ix++);
995 declare("Sigjmp_buf", $jmpbuf);
996 runtime(sprintf("PP_ENTERTRY(%s,%s);", $jmpbuf, label($op->other->next)));
997 invalidate_lexicals(REGISTER|TEMPORARY);
1003 if ($need_freetmps && $freetmps_each_loop) {
1004 runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
1009 return $op->next->other;
1014 if ($need_freetmps && $freetmps_each_loop) {
1015 runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
1020 return $op->next->other;
1025 my $next = $op->next;
1026 unshift(@bblock_todo, $next);
1027 write_back_lexicals();
1029 my $sym = doop($op);
1030 # pp_grepwhile can return either op_next or op_other and we need to
1031 # be able to distinguish the two at runtime. Since it's possible for
1032 # both ops to be "inlined", the fields could both be zero. To get
1033 # around that, we hack op_next to be our own op (purely because we
1034 # know it's a non-NULL pointer and can't be the same as op_other).
1035 push_init("((LOGOP*)$sym)->op_next = $sym;");
1036 runtime(sprintf("if (op == ($sym)->op_next) goto %s;", label($next)));
1047 write_back_lexicals(REGISTER|TEMPORARY);
1050 runtime("PUTBACK;", "return 0;");
1057 warn sprintf("%s not yet implemented properly\n", $op->ppaddr);
1058 return default_pp($op);
1063 my $flags = $op->flags;
1064 if (!($flags & OPf_KNOW)) {
1065 error("context of range unknown at compile-time");
1067 write_back_lexicals();
1069 if (!($flags & OPf_LIST)) {
1070 # We need to save our UNOP structure since pp_flop uses
1071 # it to find and adjust out targ. We don't need it ourselves.
1073 runtime sprintf("if (SvTRUE(curpad[%d])) goto %s;",
1074 $op->targ, label($op->false));
1075 unshift(@bblock_todo, $op->false);
1082 my $flags = $op->flags;
1083 if (!($flags & OPf_KNOW)) {
1084 error("context of flip unknown at compile-time");
1086 if ($flags & OPf_LIST) {
1087 return $op->first->false;
1089 write_back_lexicals();
1091 # We need to save our UNOP structure since pp_flop uses
1092 # it to find and adjust out targ. We don't need it ourselves.
1095 my $rangeix = $op->first->targ;
1096 runtime(($op->private & OPpFLIP_LINENUM) ?
1097 "if (last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(last_in_gv))) {"
1098 : "if (SvTRUE(TOPs)) {");
1099 runtime("\tsv_setiv(curpad[$rangeix], 1);");
1100 if ($op->flags & OPf_SPECIAL) {
1101 runtime("sv_setiv(curpad[$ix], 1);");
1103 runtime("\tsv_setiv(curpad[$ix], 0);",
1105 sprintf("\tgoto %s;", label($op->first->false)));
1108 qq{sv_setpv(curpad[$ix], "");},
1109 "SETs(curpad[$ix]);");
1123 my $nextop = $op->nextop;
1124 my $lastop = $op->lastop;
1125 my $redoop = $op->redoop;
1126 $curcop->write_back;
1127 debug "enterloop: pushing on cxstack" if $debug_cxstack;
1131 "label" => $curcop->[0]->label,
1139 return default_pp($op);
1142 sub pp_enterloop { enterloop(@_) }
1143 sub pp_enteriter { enterloop(@_) }
1148 die "panic: leaveloop";
1150 debug "leaveloop: popping from cxstack" if $debug_cxstack;
1152 return default_pp($op);
1158 if ($op->flags & OPf_SPECIAL) {
1159 $cxix = dopoptoloop();
1161 error('"next" used outside loop');
1162 return $op->next; # ignore the op
1165 $cxix = dopoptolabel($op->pv);
1167 error('Label not found at compile time for "next %s"', $op->pv);
1168 return $op->next; # ignore the op
1172 my $nextop = $cxstack[$cxix]->{nextop};
1173 push(@bblock_todo, $nextop);
1174 runtime(sprintf("goto %s;", label($nextop)));
1181 if ($op->flags & OPf_SPECIAL) {
1182 $cxix = dopoptoloop();
1184 error('"redo" used outside loop');
1185 return $op->next; # ignore the op
1188 $cxix = dopoptolabel($op->pv);
1190 error('Label not found at compile time for "redo %s"', $op->pv);
1191 return $op->next; # ignore the op
1195 my $redoop = $cxstack[$cxix]->{redoop};
1196 push(@bblock_todo, $redoop);
1197 runtime(sprintf("goto %s;", label($redoop)));
1204 if ($op->flags & OPf_SPECIAL) {
1205 $cxix = dopoptoloop();
1207 error('"last" used outside loop');
1208 return $op->next; # ignore the op
1211 $cxix = dopoptolabel($op->pv);
1213 error('Label not found at compile time for "last %s"', $op->pv);
1214 return $op->next; # ignore the op
1216 # XXX Add support for "last" to leave non-loop blocks
1217 if ($cxstack[$cxix]->{type} != CXt_LOOP) {
1218 error('Use of "last" for non-loop blocks is not yet implemented');
1219 return $op->next; # ignore the op
1223 my $lastop = $cxstack[$cxix]->{lastop}->next;
1224 push(@bblock_todo, $lastop);
1225 runtime(sprintf("goto %s;", label($lastop)));
1231 write_back_lexicals();
1233 my $sym = doop($op);
1234 my $replroot = $op->pmreplroot;
1236 runtime sprintf("if (op == ((PMOP*)(%s))->op_pmreplroot) goto %s;",
1237 $sym, label($replroot));
1238 $op->pmreplstart->save;
1239 push(@bblock_todo, $replroot);
1241 invalidate_lexicals();
1247 write_back_lexicals();
1250 my $pmop = $op->other;
1251 warn sprintf("substcont: op = %s, pmop = %s\n",
1252 peekop($op), peekop($pmop));#debug
1253 # my $pmopsym = objsym($pmop);
1254 my $pmopsym = $pmop->save; # XXX can this recurse?
1255 warn "pmopsym = $pmopsym\n";#debug
1256 runtime sprintf("if (op == ((PMOP*)(%s))->op_pmreplstart) goto %s;",
1257 $pmopsym, label($pmop->pmreplstart));
1258 invalidate_lexicals();
1264 my $ppname = $op->ppaddr;
1265 write_back_lexicals() unless $skip_lexicals{$ppname};
1266 write_back_stack() unless $skip_stack{$ppname};
1268 # XXX If the only way that ops can write to a TEMPORARY lexical is
1269 # when it's named in $op->targ then we could call
1270 # invalidate_lexicals(TEMPORARY) and avoid having to write back all
1271 # the temporaries. For now, we'll play it safe and write back the lot.
1272 invalidate_lexicals() unless $skip_invalidate{$ppname};
1278 my $ppname = $op->ppaddr;
1279 if (exists $ignore_op{$ppname}) {
1282 debug peek_stack() if $debug_stack;
1284 debug sprintf("%s [%s]\n",
1286 $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ);
1289 if (defined(&$ppname)) {
1291 return &$ppname($op);
1293 return default_pp($op);
1297 sub compile_bblock {
1299 #warn "compile_bblock: ", peekop($op), "\n"; # debug
1303 $op = compile_op($op);
1304 } while (defined($op) && $$op && !exists($leaders->{$$op}));
1305 write_back_stack(); # boo hoo: big loss
1311 my ($name, $root, $start, @padlist) = @_;
1315 B::Pseudoreg->new_scope;
1317 if ($debug_timings) {
1318 warn sprintf("Basic block analysis at %s\n", timing_info);
1320 $leaders = find_leaders($root, $start);
1321 @bblock_todo = ($start, values %$leaders);
1322 if ($debug_timings) {
1323 warn sprintf("Compilation at %s\n", timing_info);
1325 while (@bblock_todo) {
1326 $op = shift @bblock_todo;
1327 #warn sprintf("Considering basic block %s\n", peekop($op)); # debug
1328 next if !defined($op) || !$$op || $done{$$op};
1329 #warn "...compiling it\n"; # debug
1332 $op = compile_bblock($op);
1333 if ($need_freetmps && $freetmps_each_bblock) {
1334 runtime("FREETMPS;");
1337 } while defined($op) && $$op && !$done{$$op};
1338 if ($need_freetmps && $freetmps_each_loop) {
1339 runtime("FREETMPS;");
1343 runtime("PUTBACK;", "return 0;");
1344 } elsif ($done{$$op}) {
1345 runtime(sprintf("goto %s;", label($op)));
1348 if ($debug_timings) {
1349 warn sprintf("Saving runtime at %s\n", timing_info);
1357 $start = cc_queue(@_) if @_;
1358 while ($ccinfo = shift @cc_todo) {
1365 my ($name, $cvref) = @_;
1366 my $cv = svref_2object($cvref);
1367 my @padlist = $cv->PADLIST->ARRAY;
1368 my $curpad_sym = $padlist[1]->save;
1369 cc_recurse($name, $cv->ROOT, $cv->START, @padlist);
1373 my @comppadlist = comppadlist->ARRAY;
1374 my $curpad_sym = $comppadlist[1]->save;
1375 my $start = cc_recurse("pp_main", main_root, main_start, @comppadlist);
1376 save_unused_subs(@unused_sub_packages);
1380 if (!defined($module)) {
1381 push_init(sprintf("main_root = sym_%x;", ${main_root()}),
1382 "main_start = $start;",
1383 "curpad = AvARRAY($curpad_sym);");
1385 output_boilerplate();
1387 output_all("perl_init");
1391 if (defined($module)) {
1392 my $cmodule = $module;
1393 $cmodule =~ s/::/__/g;
1405 curpad = AvARRAY($curpad_sym);
1415 if ($debug_timings) {
1416 warn sprintf("Done at %s\n", timing_info);
1422 my ($option, $opt, $arg);
1424 while ($option = shift @options) {
1425 if ($option =~ /^-(.)(.*)/) {
1429 unshift @options, $option;
1432 if ($opt eq "-" && $arg eq "-") {
1435 } elsif ($opt eq "o") {
1436 $arg ||= shift @options;
1437 open(STDOUT, ">$arg") or return "$arg: $!\n";
1438 } elsif ($opt eq "n") {
1439 $arg ||= shift @options;
1440 $module_name = $arg;
1441 } elsif ($opt eq "u") {
1442 $arg ||= shift @options;
1443 push(@unused_sub_packages, $arg);
1444 } elsif ($opt eq "f") {
1445 $arg ||= shift @options;
1446 my $value = $arg !~ s/^no-//;
1448 my $ref = $optimise{$arg};
1449 if (defined($ref)) {
1452 warn qq(ignoring unknown optimisation option "$arg"\n);
1454 } elsif ($opt eq "O") {
1455 $arg = 1 if $arg eq "";
1457 foreach $ref (values %optimise) {
1461 $freetmps_each_loop = 1;
1464 $freetmps_each_bblock = 1 unless $freetmps_each_loop;
1466 } elsif ($opt eq "m") {
1468 } elsif ($opt eq "D") {
1469 $arg ||= shift @options;
1470 foreach $arg (split(//, $arg)) {
1473 } elsif ($arg eq "O") {
1475 } elsif ($arg eq "s") {
1477 } elsif ($arg eq "c") {
1479 } elsif ($arg eq "p") {
1481 } elsif ($arg eq "r") {
1483 } elsif ($arg eq "S") {
1485 } elsif ($arg eq "q") {
1487 } elsif ($arg eq "l") {
1489 } elsif ($arg eq "t") {
1498 my ($objname, $ppname);
1499 foreach $objname (@options) {
1500 $objname = "main::$objname" unless $objname =~ /::/;
1501 ($ppname = $objname) =~ s/^.*?:://;
1502 eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
1503 die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
1506 output_boilerplate();
1508 output_all($module_name || "init_module");
1512 return sub { cc_main() };