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
11 timing_info init_av sv_undef amagic_generation
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 G_SCALAR
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
96 pp_caller pp_reset pp_rv2cv pp_entereval pp_require pp_dofile
97 pp_entertry pp_enterloop pp_enteriter pp_entersub pp_enter);
100 if ($debug_runtime) {
103 runtime(map { chomp; "/* $_ */"} @_);
108 my ($type, $var) = @_;
109 push(@{$declare_ref->{$type}}, $var);
113 push(@$runtime_list_ref, @_);
114 warn join("\n", @_) . "\n" if $debug_runtime;
118 push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
123 print qq(#include "cc_runtime.h"\n);
124 foreach $ppdata (@pp_list) {
125 my ($name, $runtime, $declare) = @$ppdata;
126 print "\nstatic\nPP($name)\n{\n";
127 my ($type, $varlist, $line);
128 while (($type, $varlist) = each %$declare) {
129 print "\t$type ", join(", ", @$varlist), ";\n";
131 foreach $line (@$runtime) {
141 push_runtime("\t$line");
147 $runtime_list_ref = [];
150 declare("I32", "oldsave");
151 declare("SV", "**svp");
152 map { declare("SV", "*$_") } qw(sv src dst left right);
153 declare("MAGIC", "*mg");
154 $decl->add("static OP * $ppname (pTHX);");
155 debug "init_pp: $ppname\n" if $debug_queue;
158 # Initialise runtime_callback function for Stackobj class
159 BEGIN { B::Stackobj::set_callback(\&runtime) }
161 # Initialise saveoptree_callback for B::C class
163 my ($name, $root, $start, @pl) = @_;
164 debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
166 if ($name eq "*ignore*") {
169 push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
171 my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
172 $start = $fakeop->save;
173 debug "cc_queue: name $name returns $start\n" if $debug_queue;
176 BEGIN { B::C::set_callback(\&cc_queue) }
178 sub valid_int { $_[0]->{flags} & VALID_INT }
179 sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
180 sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
181 sub valid_sv { $_[0]->{flags} & VALID_SV }
183 sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
184 sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
185 sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
186 sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
187 sub top_bool { @stack ? $stack[-1]->as_bool : "SvTRUE(TOPs)" }
189 sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
190 sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
191 sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
192 sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
195 return ((pop @stack)->as_bool);
197 # Careful: POPs has an auto-decrement and SvTRUE evaluates
198 # its argument more than once.
199 runtime("sv = POPs;");
204 sub write_back_lexicals {
205 my $avoid = shift || 0;
206 debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
209 foreach $lex (@pad) {
210 next unless ref($lex);
211 $lex->write_back unless $lex->{flags} & $avoid;
215 sub write_back_stack {
217 return unless @stack;
218 runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
219 foreach $obj (@stack) {
220 runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
225 sub invalidate_lexicals {
226 my $avoid = shift || 0;
227 debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
230 foreach $lex (@pad) {
231 next unless ref($lex);
232 $lex->invalidate unless $lex->{flags} & $avoid;
236 sub reload_lexicals {
238 foreach $lex (@pad) {
239 next unless ref($lex);
240 my $type = $lex->{type};
241 if ($type == T_INT) {
243 } elsif ($type == T_DOUBLE) {
252 package B::Pseudoreg;
254 # This class allocates pseudo-registers (OK, so they're C variables).
256 my %alloc; # Keyed by variable name. A value of 1 means the
257 # variable has been declared. A value of 2 means
260 sub new_scope { %alloc = () }
263 my ($class, $type, $prefix) = @_;
264 my ($ptr, $i, $varname, $status, $obj);
265 $prefix =~ s/^(\**)//;
269 $varname = "$prefix$i";
270 $status = $alloc{$varname};
271 } while $status == 2;
274 B::CC::declare($type, "$ptr$varname");
275 $alloc{$varname} = 2; # declared and in use
277 $obj = bless \$varname, $class;
282 $alloc{$$obj} = 1; # no longer in use but still declared
288 # This class gives a standard API for a perl object to shadow a
289 # C variable and only generate reloads/write-backs when necessary.
291 # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
292 # Use $obj->write_back whenever shadowed_c_var needs to be up to date.
293 # Use $obj->invalidate whenever an unknown function may have
297 my ($class, $write_back) = @_;
298 # Object fields are perl shadow variable, validity flag
299 # (for *C* variable) and callback sub for write_back
300 # (passed perl shadow variable as argument).
301 bless [undef, 1, $write_back], $class;
304 my ($obj, $newval) = @_;
305 $obj->[1] = 0; # C variable no longer valid
311 $obj->[1] = 1; # C variable will now be valid
312 &{$obj->[2]}($obj->[0]);
315 sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
317 my $curcop = new B::Shadow (sub {
318 my $opsym = shift->save;
319 runtime("PL_curcop = (COP*)$opsym;");
323 # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
326 my $cxix = $#cxstack;
327 while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
330 debug "dopoptoloop: returning $cxix" if $debug_cxstack;
336 my $cxix = $#cxstack;
338 ($cxstack[$cxix]->{type} != CXt_LOOP ||
339 $cxstack[$cxix]->{label} ne $label)) {
342 debug "dopoptolabel: returning $cxix" if $debug_cxstack;
348 my $file = $curcop->[0]->filegv->SV->PV;
349 my $line = $curcop->[0]->line;
352 warn sprintf("%s:%d: $format\n", $file, $line, @_);
354 warn sprintf("%s:%d: %s\n", $file, $line, $format);
359 # Load pad takes (the elements of) a PADLIST as arguments and loads
360 # up @pad with Stackobj-derived objects which represent those lexicals.
361 # If/when perl itself can generate type information (my int $foo) then
362 # we'll take advantage of that here. Until then, we'll use various hacks
363 # to tell the compiler when we want a lexical to be a particular type
364 # or to be a register.
367 my ($namelistav, $valuelistav) = @_;
369 my @namelist = $namelistav->ARRAY;
370 my @valuelist = $valuelistav->ARRAY;
373 debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
374 # Temporary lexicals don't get named so it's possible for @valuelist
375 # to be strictly longer than @namelist. We count $ix up to the end of
376 # @valuelist but index into @namelist for the name. Any temporaries which
377 # run off the end of @namelist will make $namesv undefined and we treat
378 # that the same as having an explicit SPECIAL sv_undef object in @namelist.
379 # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
380 for ($ix = 1; $ix < @valuelist; $ix++) {
381 my $namesv = $namelist[$ix];
382 my $type = T_UNKNOWN;
385 my $class = class($namesv);
386 if (!defined($namesv) || $class eq "SPECIAL") {
387 # temporaries have &PL_sv_undef instead of a PVNV for a name
388 $flags = VALID_SV|TEMPORARY|REGISTER;
390 if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
394 $flags = VALID_SV|VALID_INT;
395 } elsif ($2 eq "d") {
397 $flags = VALID_SV|VALID_DOUBLE;
399 $flags |= REGISTER if $3;
402 $pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
403 "i_$name", "d_$name");
405 debug sprintf("PL_curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
411 for ($ix = 1; $ix <= $#pad; $ix++) {
412 my $type = $pad[$ix]->{type};
413 declare("IV", $type == T_INT ?
414 sprintf("%s=0",$pad[$ix]->{iv}):$pad[$ix]->{iv}) if $pad[$ix]->save_int;
415 declare("double", $type == T_DOUBLE ?
416 sprintf("%s = 0",$pad[$ix]->{nv}):$pad[$ix]->{nv} )if $pad[$ix]->save_double;
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("PL_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_WANT) ? (($flags & OPf_WANT)== OPf_WANT_LIST? G_ARRAY:G_SCALAR) : "dowantarray()");
463 # Code generation for PP code
473 my $gimme = gimme($op);
474 if ($gimme != G_ARRAY) {
475 my $obj= new B::Stackobj::Const(sv_undef);
477 # XXX Change to push a constant sv_undef Stackobj onto @stack
479 #runtime("if ($gimme != G_ARRAY) XPUSHs(&PL_sv_undef);");
487 runtime("PP_UNSTACK;");
493 my $next = $op->next;
495 unshift(@bblock_todo, $next);
497 my $bool = pop_bool();
499 runtime(sprintf("if (!$bool) {XPUSHs(&PL_sv_no); goto %s;}", label($next)));
501 runtime(sprintf("if (!%s) goto %s;", top_bool(), label($next)),
509 my $next = $op->next;
511 unshift(@bblock_todo, $next);
513 my $bool = pop_bool @stack;
515 runtime(sprintf("if (%s) { XPUSHs(&PL_sv_yes); goto %s; }",
516 $bool, label($next)));
518 runtime(sprintf("if (%s) goto %s;", top_bool(), label($next)),
526 my $false = $op->false;
527 unshift(@bblock_todo, $false);
529 my $bool = pop_bool();
531 runtime(sprintf("if (!$bool) goto %s;", label($false)));
538 push(@stack, $pad[$ix]);
539 if ($op->flags & OPf_MOD) {
540 my $private = $op->private;
541 if ($private & OPpLVAL_INTRO) {
542 runtime("SAVECLEARSV(PL_curpad[$ix]);");
543 } elsif ($private & OPpDEREF) {
544 runtime(sprintf("vivify_ref(PL_curpad[%d], %d);",
545 $ix, $private & OPpDEREF));
546 $pad[$ix]->invalidate;
555 my $obj = $constobj{$$sv};
556 if (!defined($obj)) {
557 $obj = $constobj{$$sv} = new B::Stackobj::Const ($sv);
567 debug(sprintf("%s:%d\n", $op->filegv->SV->PV, $op->line)) if $debug_lineno;
568 runtime("TAINT_NOT;") unless $omit_taint;
569 runtime("sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;");
570 if ($freetmps_each_bblock || $freetmps_each_loop) {
573 runtime("FREETMPS;");
580 $curcop->invalidate; # XXX?
581 return default_pp($op);
584 #default_pp will handle this:
585 #sub pp_rv2gv { $curcop->write_back; default_pp(@_) }
586 #sub pp_bless { $curcop->write_back; default_pp(@_) }
587 #sub pp_repeat { $curcop->write_back; default_pp(@_) }
588 # The following subs need $curcop->write_back if we decide to support arybase:
589 # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
590 #sub pp_caller { $curcop->write_back; default_pp(@_) }
591 #sub pp_reset { $curcop->write_back; default_pp(@_) }
595 my $ppname = $op->ppaddr;
596 if ($op->flags & OPf_SPECIAL && $op->flags & OPf_STACKED){
597 #this indicates the "sort BLOCK Array" case
598 #ugly optree surgery required.
599 my $root=$op->first->sibling->first;
600 my $start=$root->first;
602 $op->first->sibling->save;
605 my $sym=objsym($start);
606 my $fakeop=cc_queue("pp_sort".$$op,$root,$start);
607 $init->add(sprintf("($sym)->op_next=%s;",$fakeop));
610 write_back_lexicals();
618 my $ppname = $op->ppaddr;
619 write_back_lexicals() unless $skip_lexicals{$ppname};
620 write_back_stack() unless $skip_stack{$ppname};
621 runtime("if (PL_curstackinfo->si_type == PERLSI_SORT) {");
622 runtime("\tPUTBACK;return 0;");
630 my $gvsym = $op->gv->save;
632 runtime("XPUSHs((SV*)$gvsym);");
638 my $gvsym = $op->gv->save;
640 if ($op->private & OPpLVAL_INTRO) {
641 runtime("XPUSHs(save_scalar($gvsym));");
643 runtime("XPUSHs(GvSV($gvsym));");
650 my $gvsym = $op->gv->save;
651 my $ix = $op->private;
652 my $flag = $op->flags & OPf_MOD;
654 runtime("svp = av_fetch(GvAV($gvsym), $ix, $flag);",
655 "PUSHs(svp ? *svp : &PL_sv_undef);");
660 my ($op, $operator) = @_;
661 if ($op->flags & OPf_STACKED) {
662 my $right = pop_int();
664 my $left = top_int();
665 $stack[-1]->set_int(&$operator($left, $right));
667 runtime(sprintf("sv_setiv(TOPs, %s);",&$operator("TOPi", $right)));
670 my $targ = $pad[$op->targ];
671 my $right = new B::Pseudoreg ("IV", "riv");
672 my $left = new B::Pseudoreg ("IV", "liv");
673 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int));
674 $targ->set_int(&$operator($$left, $$right));
680 sub INTS_CLOSED () { 0x1 }
681 sub INT_RESULT () { 0x2 }
682 sub NUMERIC_RESULT () { 0x4 }
685 my ($op, $operator, $flags) = @_;
687 $force_int ||= ($flags & INT_RESULT);
688 $force_int ||= ($flags & INTS_CLOSED && @stack >= 2
689 && valid_int($stack[-2]) && valid_int($stack[-1]));
690 if ($op->flags & OPf_STACKED) {
691 my $right = pop_numeric();
693 my $left = top_numeric();
695 $stack[-1]->set_int(&$operator($left, $right));
697 $stack[-1]->set_numeric(&$operator($left, $right));
701 my $rightruntime = new B::Pseudoreg ("IV", "riv");
702 runtime(sprintf("$$rightruntime = %s;",$right));
703 runtime(sprintf("sv_setiv(TOPs, %s);",
704 &$operator("TOPi", $$rightruntime)));
706 my $rightruntime = new B::Pseudoreg ("double", "rnv");
707 runtime(sprintf("$$rightruntime = %s;",$right));
708 runtime(sprintf("sv_setnv(TOPs, %s);",
709 &$operator("TOPn",$$rightruntime)));
713 my $targ = $pad[$op->targ];
714 $force_int ||= ($targ->{type} == T_INT);
716 my $right = new B::Pseudoreg ("IV", "riv");
717 my $left = new B::Pseudoreg ("IV", "liv");
718 runtime(sprintf("$$right = %s; $$left = %s;",
719 pop_numeric(), pop_numeric));
720 $targ->set_int(&$operator($$left, $$right));
722 my $right = new B::Pseudoreg ("double", "rnv");
723 my $left = new B::Pseudoreg ("double", "lnv");
724 runtime(sprintf("$$right = %s; $$left = %s;",
725 pop_numeric(), pop_numeric));
726 $targ->set_numeric(&$operator($$left, $$right));
735 if ($op->flags & OPf_STACKED) {
736 my $right = pop_numeric();
738 my $left = top_numeric();
739 runtime sprintf("if (%s > %s){",$left,$right);
740 $stack[-1]->set_int(1);
741 $stack[-1]->write_back();
742 runtime sprintf("}else if (%s < %s ) {",$left,$right);
743 $stack[-1]->set_int(-1);
744 $stack[-1]->write_back();
745 runtime sprintf("}else if (%s == %s) {",$left,$right);
746 $stack[-1]->set_int(0);
747 $stack[-1]->write_back();
748 runtime sprintf("}else {");
749 $stack[-1]->set_sv("&PL_sv_undef");
752 my $rightruntime = new B::Pseudoreg ("double", "rnv");
753 runtime(sprintf("$$rightruntime = %s;",$right));
754 runtime sprintf(qq/if ("TOPn" > %s){/,$rightruntime);
755 runtime sprintf("sv_setiv(TOPs,1);");
756 runtime sprintf(qq/}else if ( "TOPn" < %s ) {/,$$rightruntime);
757 runtime sprintf("sv_setiv(TOPs,-1);");
758 runtime sprintf(qq/} else if ("TOPn" == %s) {/,$$rightruntime);
759 runtime sprintf("sv_setiv(TOPs,0);");
760 runtime sprintf(qq/}else {/);
761 runtime sprintf("sv_setiv(TOPs,&PL_sv_undef;");
765 my $targ = $pad[$op->targ];
766 my $right = new B::Pseudoreg ("double", "rnv");
767 my $left = new B::Pseudoreg ("double", "lnv");
768 runtime(sprintf("$$right = %s; $$left = %s;",
769 pop_numeric(), pop_numeric));
770 runtime sprintf("if (%s > %s){",$$left,$$right);
773 runtime sprintf("}else if (%s < %s ) {",$$left,$$right);
776 runtime sprintf("}else if (%s == %s) {",$$left,$$right);
779 runtime sprintf("}else {");
780 $targ->set_sv("&PL_sv_undef");
788 my ($op, $operator, $flags) = @_;
789 if ($op->flags & OPf_STACKED) {
790 my $right = pop_sv();
793 if ($flags & INT_RESULT) {
794 $stack[-1]->set_int(&$operator($left, $right));
795 } elsif ($flags & NUMERIC_RESULT) {
796 $stack[-1]->set_numeric(&$operator($left, $right));
798 # XXX Does this work?
799 runtime(sprintf("sv_setsv($left, %s);",
800 &$operator($left, $right)));
801 $stack[-1]->invalidate;
805 if ($flags & INT_RESULT) {
807 } elsif ($flags & NUMERIC_RESULT) {
812 runtime(sprintf("%s(TOPs, %s);", $f, &$operator("TOPs", $right)));
815 my $targ = $pad[$op->targ];
816 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv));
817 if ($flags & INT_RESULT) {
818 $targ->set_int(&$operator("left", "right"));
819 } elsif ($flags & NUMERIC_RESULT) {
820 $targ->set_numeric(&$operator("left", "right"));
822 # XXX Does this work?
823 runtime(sprintf("sv_setsv(%s, %s);",
824 $targ->as_sv, &$operator("left", "right")));
833 my ($op, $operator) = @_;
834 my $right = new B::Pseudoreg ("IV", "riv");
835 my $left = new B::Pseudoreg ("IV", "liv");
836 runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int()));
837 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
838 $bool->set_int(&$operator($$left, $$right));
843 sub bool_numeric_binop {
844 my ($op, $operator) = @_;
845 my $right = new B::Pseudoreg ("double", "rnv");
846 my $left = new B::Pseudoreg ("double", "lnv");
847 runtime(sprintf("$$right = %s; $$left = %s;",
848 pop_numeric(), pop_numeric()));
849 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
850 $bool->set_numeric(&$operator($$left, $$right));
856 my ($op, $operator) = @_;
857 runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv()));
858 my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
859 $bool->set_numeric(&$operator("left", "right"));
866 return sub { "$_[0] $opname $_[1]" }
871 return sub { sprintf("%s(%s)", $opname, join(", ", @_)) }
875 my $plus_op = infix_op("+");
876 my $minus_op = infix_op("-");
877 my $multiply_op = infix_op("*");
878 my $divide_op = infix_op("/");
879 my $modulo_op = infix_op("%");
880 my $lshift_op = infix_op("<<");
881 my $rshift_op = infix_op(">>");
882 my $scmp_op = prefix_op("sv_cmp");
883 my $seq_op = prefix_op("sv_eq");
884 my $sne_op = prefix_op("!sv_eq");
885 my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
886 my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
887 my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
888 my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
889 my $eq_op = infix_op("==");
890 my $ne_op = infix_op("!=");
891 my $lt_op = infix_op("<");
892 my $gt_op = infix_op(">");
893 my $le_op = infix_op("<=");
894 my $ge_op = infix_op(">=");
897 # XXX The standard perl PP code has extra handling for
898 # some special case arguments of these operators.
900 sub pp_add { numeric_binop($_[0], $plus_op, INTS_CLOSED) }
901 sub pp_subtract { numeric_binop($_[0], $minus_op, INTS_CLOSED) }
902 sub pp_multiply { numeric_binop($_[0], $multiply_op, INTS_CLOSED) }
903 sub pp_divide { numeric_binop($_[0], $divide_op) }
904 sub pp_modulo { int_binop($_[0], $modulo_op) } # differs from perl's
906 sub pp_left_shift { int_binop($_[0], $lshift_op) }
907 sub pp_right_shift { int_binop($_[0], $rshift_op) }
908 sub pp_i_add { int_binop($_[0], $plus_op) }
909 sub pp_i_subtract { int_binop($_[0], $minus_op) }
910 sub pp_i_multiply { int_binop($_[0], $multiply_op) }
911 sub pp_i_divide { int_binop($_[0], $divide_op) }
912 sub pp_i_modulo { int_binop($_[0], $modulo_op) }
914 sub pp_eq { bool_numeric_binop($_[0], $eq_op) }
915 sub pp_ne { bool_numeric_binop($_[0], $ne_op) }
916 sub pp_lt { bool_numeric_binop($_[0], $lt_op) }
917 sub pp_gt { bool_numeric_binop($_[0], $gt_op) }
918 sub pp_le { bool_numeric_binop($_[0], $le_op) }
919 sub pp_ge { bool_numeric_binop($_[0], $ge_op) }
921 sub pp_i_eq { bool_int_binop($_[0], $eq_op) }
922 sub pp_i_ne { bool_int_binop($_[0], $ne_op) }
923 sub pp_i_lt { bool_int_binop($_[0], $lt_op) }
924 sub pp_i_gt { bool_int_binop($_[0], $gt_op) }
925 sub pp_i_le { bool_int_binop($_[0], $le_op) }
926 sub pp_i_ge { bool_int_binop($_[0], $ge_op) }
928 sub pp_scmp { sv_binop($_[0], $scmp_op, INT_RESULT) }
929 sub pp_slt { bool_sv_binop($_[0], $slt_op) }
930 sub pp_sgt { bool_sv_binop($_[0], $sgt_op) }
931 sub pp_sle { bool_sv_binop($_[0], $sle_op) }
932 sub pp_sge { bool_sv_binop($_[0], $sge_op) }
933 sub pp_seq { bool_sv_binop($_[0], $seq_op) }
934 sub pp_sne { bool_sv_binop($_[0], $sne_op) }
940 my $backwards = $op->private & OPpASSIGN_BACKWARDS;
945 ($src, $dst) = ($dst, $src) if $backwards;
946 my $type = $src->{type};
947 if ($type == T_INT) {
948 $dst->set_int($src->as_int);
949 } elsif ($type == T_DOUBLE) {
950 $dst->set_numeric($src->as_numeric);
952 $dst->set_sv($src->as_sv);
955 } elsif (@stack == 1) {
957 my $src = pop @stack;
958 my $type = $src->{type};
959 runtime("if (PL_tainting && PL_tainted) TAINT_NOT;");
960 if ($type == T_INT) {
961 runtime sprintf("sv_setiv(TOPs, %s);", $src->as_int);
962 } elsif ($type == T_DOUBLE) {
963 runtime sprintf("sv_setnv(TOPs, %s);", $src->as_double);
965 runtime sprintf("sv_setsv(TOPs, %s);", $src->as_sv);
967 runtime("SvSETMAGIC(TOPs);");
969 my $dst = $stack[-1];
970 my $type = $dst->{type};
971 runtime("sv = POPs;");
972 runtime("MAYBE_TAINT_SASSIGN_SRC(sv);");
973 if ($type == T_INT) {
974 $dst->set_int("SvIV(sv)");
975 } elsif ($type == T_DOUBLE) {
976 $dst->set_double("SvNV(sv)");
978 runtime("SvSetMagicSV($dst->{sv}, sv);");
984 runtime("src = POPs; dst = TOPs;");
986 runtime("dst = POPs; src = TOPs;");
988 runtime("MAYBE_TAINT_SASSIGN_SRC(src);",
989 "SvSetSV(dst, src);",
999 my $obj = $stack[-1];
1000 my $type = $obj->{type};
1001 if ($type == T_INT || $type == T_DOUBLE) {
1002 $obj->set_int($obj->as_int . " + 1");
1004 runtime sprintf("PP_PREINC(%s);", $obj->as_sv);
1008 runtime sprintf("PP_PREINC(TOPs);");
1016 runtime("PUSHMARK(sp);");
1023 my $gimme = gimme($op);
1024 if ($gimme == G_ARRAY) { # sic
1025 runtime("POPMARK;"); # need this even though not a "full" pp_list
1027 runtime("PP_LIST($gimme);");
1034 $curcop->write_back;
1035 write_back_lexicals(REGISTER|TEMPORARY);
1037 my $sym = doop($op);
1038 runtime("while (PL_op != ($sym)->op_next && PL_op != (OP*)0 ){");
1039 runtime("PL_op = (*PL_op->op_ppaddr)(aTHX);");
1040 runtime("SPAGAIN;}");
1042 invalidate_lexicals(REGISTER|TEMPORARY);
1047 my $ppname = $op->ppaddr;
1048 write_back_lexicals() unless $skip_lexicals{$ppname};
1049 write_back_stack() unless $skip_stack{$ppname};
1051 # See comment in pp_grepwhile to see why!
1052 $init->add("((LISTOP*)$sym)->op_first = $sym;");
1053 runtime("if (PL_op == ((LISTOP*)($sym))->op_first){");
1054 runtime( sprintf("goto %s;",label($op->first)));
1062 my $ppname = $op->ppaddr;
1063 write_back_lexicals() unless $skip_lexicals{$ppname};
1064 write_back_stack() unless $skip_stack{$ppname};
1066 runtime("if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;}");
1067 invalidate_lexicals() unless $skip_invalidate{$ppname};
1077 write_back_lexicals(REGISTER|TEMPORARY);
1079 my $sym = doop($op);
1080 # XXX Is this the right way to distinguish between it returning
1081 # CvSTART(cv) (via doform) and pop_return()?
1082 #runtime("if (PL_op) PL_op = (*PL_op->op_ppaddr)(aTHX);");
1083 runtime("SPAGAIN;");
1085 invalidate_lexicals(REGISTER|TEMPORARY);
1091 $curcop->write_back;
1092 write_back_lexicals(REGISTER|TEMPORARY);
1094 my $sym = loadop($op);
1095 my $ppaddr = $op->ppaddr;
1096 runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
1098 invalidate_lexicals(REGISTER|TEMPORARY);
1102 sub pp_entereval { doeval(@_) }
1103 sub pp_require { doeval(@_) }
1104 sub pp_dofile { doeval(@_) }
1108 $curcop->write_back;
1109 write_back_lexicals(REGISTER|TEMPORARY);
1111 my $sym = doop($op);
1112 my $jmpbuf = sprintf("jmpbuf%d", $jmpbuf_ix++);
1113 declare("JMPENV", $jmpbuf);
1114 runtime(sprintf("PP_ENTERTRY(%s,%s);", $jmpbuf, label($op->other->next)));
1115 invalidate_lexicals(REGISTER|TEMPORARY);
1122 runtime("PP_LEAVETRY;");
1128 if ($need_freetmps && $freetmps_each_loop) {
1129 runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
1136 my $nexttonext=$next->next;
1138 runtime(sprintf("if (PL_op == (($sym)->op_next)->op_next) goto %s;",
1139 label($nexttonext)));
1140 return $op->next->other;
1145 if ($need_freetmps && $freetmps_each_loop) {
1146 runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
1150 # pp_mapstart can return either op_next->op_next or op_next->op_other and
1151 # we need to be able to distinguish the two at runtime.
1155 my $nexttonext=$next->next;
1157 runtime(sprintf("if (PL_op == (($sym)->op_next)->op_next) goto %s;",
1158 label($nexttonext)));
1159 return $op->next->other;
1164 my $next = $op->next;
1165 unshift(@bblock_todo, $next);
1166 write_back_lexicals();
1168 my $sym = doop($op);
1169 # pp_grepwhile can return either op_next or op_other and we need to
1170 # be able to distinguish the two at runtime. Since it's possible for
1171 # both ops to be "inlined", the fields could both be zero. To get
1172 # around that, we hack op_next to be our own op (purely because we
1173 # know it's a non-NULL pointer and can't be the same as op_other).
1174 $init->add("((LOGOP*)$sym)->op_next = $sym;");
1175 runtime(sprintf("if (PL_op == ($sym)->op_next) goto %s;", label($next)));
1186 write_back_lexicals(REGISTER|TEMPORARY);
1189 runtime("PUTBACK;", "return PL_op;");
1196 warn sprintf("%s not yet implemented properly\n", $op->ppaddr);
1197 return default_pp($op);
1202 my $flags = $op->flags;
1203 if (!($flags & OPf_WANT)) {
1204 error("context of range unknown at compile-time");
1206 write_back_lexicals();
1208 unless (($flags & OPf_WANT)== OPf_WANT_LIST) {
1209 # We need to save our UNOP structure since pp_flop uses
1210 # it to find and adjust out targ. We don't need it ourselves.
1212 runtime sprintf("if (SvTRUE(PL_curpad[%d])) goto %s;",
1213 $op->targ, label($op->false));
1214 unshift(@bblock_todo, $op->false);
1221 my $flags = $op->flags;
1222 if (!($flags & OPf_WANT)) {
1223 error("context of flip unknown at compile-time");
1225 if (($flags & OPf_WANT)==OPf_WANT_LIST) {
1226 return $op->first->false;
1228 write_back_lexicals();
1230 # We need to save our UNOP structure since pp_flop uses
1231 # it to find and adjust out targ. We don't need it ourselves.
1234 my $rangeix = $op->first->targ;
1235 runtime(($op->private & OPpFLIP_LINENUM) ?
1236 "if (PL_last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(PL_last_in_gv))) {"
1237 : "if (SvTRUE(TOPs)) {");
1238 runtime("\tsv_setiv(PL_curpad[$rangeix], 1);");
1239 if ($op->flags & OPf_SPECIAL) {
1240 runtime("sv_setiv(PL_curpad[$ix], 1);");
1242 runtime("\tsv_setiv(PL_curpad[$ix], 0);",
1244 sprintf("\tgoto %s;", label($op->first->false)));
1247 qq{sv_setpv(PL_curpad[$ix], "");},
1248 "SETs(PL_curpad[$ix]);");
1262 my $nextop = $op->nextop;
1263 my $lastop = $op->lastop;
1264 my $redoop = $op->redoop;
1265 $curcop->write_back;
1266 debug "enterloop: pushing on cxstack" if $debug_cxstack;
1270 "label" => $curcop->[0]->label,
1278 return default_pp($op);
1281 sub pp_enterloop { enterloop(@_) }
1282 sub pp_enteriter { enterloop(@_) }
1287 die "panic: leaveloop";
1289 debug "leaveloop: popping from cxstack" if $debug_cxstack;
1291 return default_pp($op);
1297 if ($op->flags & OPf_SPECIAL) {
1298 $cxix = dopoptoloop();
1300 error('"next" used outside loop');
1301 return $op->next; # ignore the op
1304 $cxix = dopoptolabel($op->pv);
1306 error('Label not found at compile time for "next %s"', $op->pv);
1307 return $op->next; # ignore the op
1311 my $nextop = $cxstack[$cxix]->{nextop};
1312 push(@bblock_todo, $nextop);
1313 runtime(sprintf("goto %s;", label($nextop)));
1320 if ($op->flags & OPf_SPECIAL) {
1321 $cxix = dopoptoloop();
1323 error('"redo" used outside loop');
1324 return $op->next; # ignore the op
1327 $cxix = dopoptolabel($op->pv);
1329 error('Label not found at compile time for "redo %s"', $op->pv);
1330 return $op->next; # ignore the op
1334 my $redoop = $cxstack[$cxix]->{redoop};
1335 push(@bblock_todo, $redoop);
1336 runtime(sprintf("goto %s;", label($redoop)));
1343 if ($op->flags & OPf_SPECIAL) {
1344 $cxix = dopoptoloop();
1346 error('"last" used outside loop');
1347 return $op->next; # ignore the op
1350 $cxix = dopoptolabel($op->pv);
1352 error('Label not found at compile time for "last %s"', $op->pv);
1353 return $op->next; # ignore the op
1355 # XXX Add support for "last" to leave non-loop blocks
1356 if ($cxstack[$cxix]->{type} != CXt_LOOP) {
1357 error('Use of "last" for non-loop blocks is not yet implemented');
1358 return $op->next; # ignore the op
1362 my $lastop = $cxstack[$cxix]->{lastop}->next;
1363 push(@bblock_todo, $lastop);
1364 runtime(sprintf("goto %s;", label($lastop)));
1370 write_back_lexicals();
1372 my $sym = doop($op);
1373 my $replroot = $op->pmreplroot;
1375 runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplroot) goto %s;",
1376 $sym, label($replroot));
1377 $op->pmreplstart->save;
1378 push(@bblock_todo, $replroot);
1380 invalidate_lexicals();
1386 write_back_lexicals();
1389 my $pmop = $op->other;
1390 # warn sprintf("substcont: op = %s, pmop = %s\n",
1391 # peekop($op), peekop($pmop));#debug
1392 # my $pmopsym = objsym($pmop);
1393 my $pmopsym = $pmop->save; # XXX can this recurse?
1394 # warn "pmopsym = $pmopsym\n";#debug
1395 runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplstart) goto %s;",
1396 $pmopsym, label($pmop->pmreplstart));
1397 invalidate_lexicals();
1403 my $ppname = $op->ppaddr;
1404 if ($curcop and $need_curcop{$ppname}){
1405 $curcop->write_back;
1407 write_back_lexicals() unless $skip_lexicals{$ppname};
1408 write_back_stack() unless $skip_stack{$ppname};
1410 # XXX If the only way that ops can write to a TEMPORARY lexical is
1411 # when it's named in $op->targ then we could call
1412 # invalidate_lexicals(TEMPORARY) and avoid having to write back all
1413 # the temporaries. For now, we'll play it safe and write back the lot.
1414 invalidate_lexicals() unless $skip_invalidate{$ppname};
1420 my $ppname = $op->ppaddr;
1421 if (exists $ignore_op{$ppname}) {
1424 debug peek_stack() if $debug_stack;
1426 debug sprintf("%s [%s]\n",
1428 $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ);
1431 if (defined(&$ppname)) {
1433 return &$ppname($op);
1435 return default_pp($op);
1439 sub compile_bblock {
1441 #warn "compile_bblock: ", peekop($op), "\n"; # debug
1445 $op = compile_op($op);
1446 } while (defined($op) && $$op && !exists($leaders->{$$op}));
1447 write_back_stack(); # boo hoo: big loss
1453 my ($name, $root, $start, @padlist) = @_;
1457 B::Pseudoreg->new_scope;
1459 if ($debug_timings) {
1460 warn sprintf("Basic block analysis at %s\n", timing_info);
1462 $leaders = find_leaders($root, $start);
1463 my @leaders= keys %$leaders;
1464 if ($#leaders > -1) {
1465 @bblock_todo = ($start, values %$leaders) ;
1467 runtime("return PL_op?PL_op->op_next:0;");
1469 if ($debug_timings) {
1470 warn sprintf("Compilation at %s\n", timing_info);
1472 while (@bblock_todo) {
1473 $op = shift @bblock_todo;
1474 #warn sprintf("Considering basic block %s\n", peekop($op)); # debug
1475 next if !defined($op) || !$$op || $done{$$op};
1476 #warn "...compiling it\n"; # debug
1479 $op = compile_bblock($op);
1480 if ($need_freetmps && $freetmps_each_bblock) {
1481 runtime("FREETMPS;");
1484 } while defined($op) && $$op && !$done{$$op};
1485 if ($need_freetmps && $freetmps_each_loop) {
1486 runtime("FREETMPS;");
1490 runtime("PUTBACK;","return PL_op;");
1491 } elsif ($done{$$op}) {
1492 runtime(sprintf("goto %s;", label($op)));
1495 if ($debug_timings) {
1496 warn sprintf("Saving runtime at %s\n", timing_info);
1498 declare_pad(@padlist) ;
1505 $start = cc_queue(@_) if @_;
1506 while ($ccinfo = shift @cc_todo) {
1513 my ($name, $cvref) = @_;
1514 my $cv = svref_2object($cvref);
1515 my @padlist = $cv->PADLIST->ARRAY;
1516 my $curpad_sym = $padlist[1]->save;
1517 cc_recurse($name, $cv->ROOT, $cv->START, @padlist);
1521 my @comppadlist = comppadlist->ARRAY;
1522 my $curpad_nam = $comppadlist[0]->save;
1523 my $curpad_sym = $comppadlist[1]->save;
1524 my $init_av = init_av->save;
1525 my $start = cc_recurse("pp_main", main_root, main_start, @comppadlist);
1526 # Do save_unused_subs before saving inc_hv
1530 my $inc_hv = svref_2object(\%INC)->save;
1531 my $inc_av = svref_2object(\@INC)->save;
1532 my $amagic_generate= amagic_generation;
1534 if (!defined($module)) {
1535 $init->add(sprintf("PL_main_root = s\\_%x;", ${main_root()}),
1536 "PL_main_start = $start;",
1537 "PL_curpad = AvARRAY($curpad_sym);",
1538 "PL_initav = $init_av;",
1539 "GvHV(PL_incgv) = $inc_hv;",
1540 "GvAV(PL_incgv) = $inc_av;",
1541 "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
1542 "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));",
1543 "PL_amagic_generation= $amagic_generate;",
1547 seek(STDOUT,0,0); #prevent print statements from BEGIN{} into the output
1548 output_boilerplate();
1550 output_all("perl_init");
1554 if (defined($module)) {
1555 my $cmodule = $module;
1556 $cmodule =~ s/::/__/g;
1566 SAVESPTR(PL_curpad);
1568 PL_curpad = AvARRAY($curpad_sym);
1578 if ($debug_timings) {
1579 warn sprintf("Done at %s\n", timing_info);
1585 my ($option, $opt, $arg);
1587 while ($option = shift @options) {
1588 if ($option =~ /^-(.)(.*)/) {
1592 unshift @options, $option;
1595 if ($opt eq "-" && $arg eq "-") {
1598 } elsif ($opt eq "o") {
1599 $arg ||= shift @options;
1600 open(STDOUT, ">$arg") or return "open '>$arg': $!\n";
1601 } elsif ($opt eq "n") {
1602 $arg ||= shift @options;
1603 $module_name = $arg;
1604 } elsif ($opt eq "u") {
1605 $arg ||= shift @options;
1606 mark_unused($arg,undef);
1607 } elsif ($opt eq "f") {
1608 $arg ||= shift @options;
1609 my $value = $arg !~ s/^no-//;
1611 my $ref = $optimise{$arg};
1612 if (defined($ref)) {
1615 warn qq(ignoring unknown optimisation option "$arg"\n);
1617 } elsif ($opt eq "O") {
1618 $arg = 1 if $arg eq "";
1620 foreach $ref (values %optimise) {
1624 $freetmps_each_loop = 1;
1627 $freetmps_each_bblock = 1 unless $freetmps_each_loop;
1629 } elsif ($opt eq "m") {
1630 $arg ||= shift @options;
1632 mark_unused($arg,undef);
1633 } elsif ($opt eq "p") {
1634 $arg ||= shift @options;
1636 } elsif ($opt eq "D") {
1637 $arg ||= shift @options;
1638 foreach $arg (split(//, $arg)) {
1641 } elsif ($arg eq "O") {
1643 } elsif ($arg eq "s") {
1645 } elsif ($arg eq "c") {
1647 } elsif ($arg eq "p") {
1649 } elsif ($arg eq "r") {
1651 } elsif ($arg eq "S") {
1653 } elsif ($arg eq "q") {
1655 } elsif ($arg eq "l") {
1657 } elsif ($arg eq "t") {
1664 $init = B::Section->get("init");
1665 $decl = B::Section->get("decl");
1669 my ($objname, $ppname);
1670 foreach $objname (@options) {
1671 $objname = "main::$objname" unless $objname =~ /::/;
1672 ($ppname = $objname) =~ s/^.*?:://;
1673 eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
1674 die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
1677 output_boilerplate();
1679 output_all($module_name || "init_module");
1683 return sub { cc_main() };
1693 B::CC - Perl compiler's optimized C translation backend
1697 perl -MO=CC[,OPTIONS] foo.pl
1701 This compiler backend takes Perl source and generates C source code
1702 corresponding to the flow of your program. In other words, this
1703 backend is somewhat a "real" compiler in the sense that many people
1704 think about compilers. Note however that, currently, it is a very
1705 poor compiler in that although it generates (mostly, or at least
1706 sometimes) correct code, it performs relatively few optimisations.
1707 This will change as the compiler develops. The result is that
1708 running an executable compiled with this backend may start up more
1709 quickly than running the original Perl program (a feature shared
1710 by the B<C> compiler backend--see F<B::C>) and may also execute
1711 slightly faster. This is by no means a good optimising compiler--yet.
1715 If there are any non-option arguments, they are taken to be
1716 names of objects to be saved (probably doesn't work properly yet).
1717 Without extra arguments, it saves the main program.
1723 Output to filename instead of STDOUT
1727 Verbose compilation (currently gives a few compilation statistics).
1731 Force end of options
1735 Force apparently unused subs from package Packname to be compiled.
1736 This allows programs to use eval "foo()" even when sub foo is never
1737 seen to be used at compile time. The down side is that any subs which
1738 really are never used also have code generated. This option is
1739 necessary, for example, if you have a signal handler foo which you
1740 initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just
1741 to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u>
1742 options. The compiler tries to figure out which packages may possibly
1743 have subs in which need compiling but the current version doesn't do
1744 it very well. In particular, it is confused by nested packages (i.e.
1745 of the form C<A::B>) where package C<A> does not contain any subs.
1747 =item B<-mModulename>
1749 Instead of generating source for a runnable executable, generate
1750 source for an XSUB module. The boot_Modulename function (which
1751 DynaLoader can look for) does the appropriate initialisation and runs
1752 the main part of the Perl source that is being compiled.
1757 Debug options (concatenated or separate flags like C<perl -D>).
1761 Writes debugging output to STDERR just as it's about to write to the
1762 program's runtime (otherwise writes debugging info as comments in
1767 Outputs each OP as it's compiled
1771 Outputs the contents of the shadow stack at each OP
1775 Outputs the contents of the shadow pad of lexicals as it's loaded for
1776 each sub or the main program.
1780 Outputs the name of each fake PP function in the queue as it's about
1785 Output the filename and line number of each original line of Perl
1786 code as it's processed (C<pp_nextstate>).
1790 Outputs timing information of compilation stages.
1794 Force optimisations on or off one at a time.
1796 =item B<-ffreetmps-each-bblock>
1798 Delays FREETMPS from the end of each statement to the end of the each
1801 =item B<-ffreetmps-each-loop>
1803 Delays FREETMPS from the end of each statement to the end of the group
1804 of basic blocks forming a loop. At most one of the freetmps-each-*
1805 options can be used.
1807 =item B<-fomit-taint>
1809 Omits generating code for handling perl's tainting mechanism.
1813 Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.
1814 Currently, B<-O1> sets B<-ffreetmps-each-bblock> and B<-O2>
1815 sets B<-ffreetmps-each-loop>.
1821 perl -MO=CC,-O2,-ofoo.c foo.pl
1822 perl cc_harness -o foo foo.c
1824 Note that C<cc_harness> lives in the C<B> subdirectory of your perl
1825 library directory. The utility called C<perlcc> may also be used to
1826 help make use of this compiler.
1828 perl -MO=CC,-mFoo,-oFoo.c Foo.pm
1829 perl cc_harness -shared -c -o Foo.so Foo.c
1833 Plenty. Current status: experimental.
1837 These aren't really bugs but they are constructs which are heavily
1838 tied to perl's compile-and-go implementation and with which this
1839 compiler backend cannot cope.
1843 Standard perl calculates the target of "next", "last", and "redo"
1844 at run-time. The compiler calculates the targets at compile-time.
1845 For example, the program
1847 sub skip_on_odd { next NUMBER if $_[0] % 2 }
1848 NUMBER: for ($i = 0; $i < 5; $i++) {
1857 with standard perl but gives a compile-time error with the compiler.
1859 =head2 Context of ".."
1861 The context (scalar or array) of the ".." operator determines whether
1862 it behaves as a range or a flip/flop. Standard perl delays until
1863 runtime the decision of which context it is in but the compiler needs
1864 to know the context at compile-time. For example,
1867 sub range { (shift @a)..(shift @a) }
1869 while (@a) { print scalar(range()) }
1871 generates the output
1875 with standard Perl but gives a compile-time error with compiled Perl.
1879 Compiled Perl programs use native C arithemtic much more frequently
1880 than standard perl. Operations on large numbers or on boundary
1881 cases may produce different behaviour.
1883 =head2 Deprecated features
1885 Features of standard perl such as C<$[> which have been deprecated
1886 in standard perl since Perl5 was released have not been implemented
1891 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>