Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2002 lines
57 KiB

  1. # CC.pm
  2. #
  3. # Copyright (c) 1996, 1997, 1998 Malcolm Beattie
  4. #
  5. # You may distribute under the terms of either the GNU General Public
  6. # License or the Artistic License, as specified in the README file.
  7. #
  8. package B::CC;
  9. use Config;
  10. use strict;
  11. use B qw(main_start main_root class comppadlist peekop svref_2object
  12. timing_info init_av sv_undef amagic_generation
  13. OPf_WANT_LIST OPf_WANT OPf_MOD OPf_STACKED OPf_SPECIAL
  14. OPpASSIGN_BACKWARDS OPpLVAL_INTRO OPpDEREF_AV OPpDEREF_HV
  15. OPpDEREF OPpFLIP_LINENUM G_ARRAY G_SCALAR
  16. CXt_NULL CXt_SUB CXt_EVAL CXt_LOOP CXt_SUBST CXt_BLOCK
  17. );
  18. use B::C qw(save_unused_subs objsym init_sections mark_unused
  19. output_all output_boilerplate output_main);
  20. use B::Bblock qw(find_leaders);
  21. use B::Stackobj qw(:types :flags);
  22. # These should probably be elsewhere
  23. # Flags for $op->flags
  24. my $module; # module name (when compiled with -m)
  25. my %done; # hash keyed by $$op of leaders of basic blocks
  26. # which have already been done.
  27. my $leaders; # ref to hash of basic block leaders. Keys are $$op
  28. # addresses, values are the $op objects themselves.
  29. my @bblock_todo; # list of leaders of basic blocks that need visiting
  30. # sometime.
  31. my @cc_todo; # list of tuples defining what PP code needs to be
  32. # saved (e.g. CV, main or PMOP repl code). Each tuple
  33. # is [$name, $root, $start, @padlist]. PMOP repl code
  34. # tuples inherit padlist.
  35. my @stack; # shadows perl's stack when contents are known.
  36. # Values are objects derived from class B::Stackobj
  37. my @pad; # Lexicals in current pad as Stackobj-derived objects
  38. my @padlist; # Copy of current padlist so PMOP repl code can find it
  39. my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
  40. my $jmpbuf_ix = 0; # Next free index for dynamically allocated jmpbufs
  41. my %constobj; # OP_CONST constants as Stackobj-derived objects
  42. # keyed by $$sv.
  43. my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
  44. # block or even to the end of each loop of blocks,
  45. # depending on optimisation options.
  46. my $know_op = 0; # Set when C variable op already holds the right op
  47. # (from an immediately preceding DOOP(ppname)).
  48. my $errors = 0; # Number of errors encountered
  49. my %skip_stack; # Hash of PP names which don't need write_back_stack
  50. my %skip_lexicals; # Hash of PP names which don't need write_back_lexicals
  51. my %skip_invalidate; # Hash of PP names which don't need invalidate_lexicals
  52. my %ignore_op; # Hash of ops which do nothing except returning op_next
  53. my %need_curcop; # Hash of ops which need PL_curcop
  54. my %lexstate; #state of padsvs at the start of a bblock
  55. BEGIN {
  56. foreach (qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null)) {
  57. $ignore_op{$_} = 1;
  58. }
  59. }
  60. my ($module_name);
  61. my ($debug_op, $debug_stack, $debug_cxstack, $debug_pad, $debug_runtime,
  62. $debug_shadow, $debug_queue, $debug_lineno, $debug_timings);
  63. # Optimisation options. On the command line, use hyphens instead of
  64. # underscores for compatibility with gcc-style options. We use
  65. # underscores here because they are OK in (strict) barewords.
  66. my ($freetmps_each_bblock, $freetmps_each_loop, $omit_taint);
  67. my %optimise = (freetmps_each_bblock => \$freetmps_each_bblock,
  68. freetmps_each_loop => \$freetmps_each_loop,
  69. omit_taint => \$omit_taint);
  70. # perl patchlevel to generate code for (defaults to current patchlevel)
  71. my $patchlevel = int(0.5 + 1000 * ($] - 5));
  72. # Could rewrite push_runtime() and output_runtime() to use a
  73. # temporary file if memory is at a premium.
  74. my $ppname; # name of current fake PP function
  75. my $runtime_list_ref;
  76. my $declare_ref; # Hash ref keyed by C variable type of declarations.
  77. my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
  78. # tuples to be written out.
  79. my ($init, $decl);
  80. sub init_hash { map { $_ => 1 } @_ }
  81. #
  82. # Initialise the hashes for the default PP functions where we can avoid
  83. # either write_back_stack, write_back_lexicals or invalidate_lexicals.
  84. #
  85. %skip_lexicals = init_hash qw(pp_enter pp_enterloop);
  86. %skip_invalidate = init_hash qw(pp_enter pp_enterloop);
  87. %need_curcop = init_hash qw(pp_rv2gv pp_bless pp_repeat pp_sort pp_caller
  88. pp_reset pp_rv2cv pp_entereval pp_require pp_dofile
  89. pp_entertry pp_enterloop pp_enteriter pp_entersub
  90. pp_enter pp_method);
  91. sub debug {
  92. if ($debug_runtime) {
  93. warn(@_);
  94. } else {
  95. my @tmp=@_;
  96. runtime(map { chomp; "/* $_ */"} @tmp);
  97. }
  98. }
  99. sub declare {
  100. my ($type, $var) = @_;
  101. push(@{$declare_ref->{$type}}, $var);
  102. }
  103. sub push_runtime {
  104. push(@$runtime_list_ref, @_);
  105. warn join("\n", @_) . "\n" if $debug_runtime;
  106. }
  107. sub save_runtime {
  108. push(@pp_list, [$ppname, $runtime_list_ref, $declare_ref]);
  109. }
  110. sub output_runtime {
  111. my $ppdata;
  112. print qq(#include "cc_runtime.h"\n);
  113. foreach $ppdata (@pp_list) {
  114. my ($name, $runtime, $declare) = @$ppdata;
  115. print "\nstatic\nCCPP($name)\n{\n";
  116. my ($type, $varlist, $line);
  117. while (($type, $varlist) = each %$declare) {
  118. print "\t$type ", join(", ", @$varlist), ";\n";
  119. }
  120. foreach $line (@$runtime) {
  121. print $line, "\n";
  122. }
  123. print "}\n";
  124. }
  125. }
  126. sub runtime {
  127. my $line;
  128. foreach $line (@_) {
  129. push_runtime("\t$line");
  130. }
  131. }
  132. sub init_pp {
  133. $ppname = shift;
  134. $runtime_list_ref = [];
  135. $declare_ref = {};
  136. runtime("dSP;");
  137. declare("I32", "oldsave");
  138. declare("SV", "**svp");
  139. map { declare("SV", "*$_") } qw(sv src dst left right);
  140. declare("MAGIC", "*mg");
  141. $decl->add("static OP * $ppname (pTHX);");
  142. debug "init_pp: $ppname\n" if $debug_queue;
  143. }
  144. # Initialise runtime_callback function for Stackobj class
  145. BEGIN { B::Stackobj::set_callback(\&runtime) }
  146. # Initialise saveoptree_callback for B::C class
  147. sub cc_queue {
  148. my ($name, $root, $start, @pl) = @_;
  149. debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
  150. if $debug_queue;
  151. if ($name eq "*ignore*") {
  152. $name = 0;
  153. } else {
  154. push(@cc_todo, [$name, $root, $start, (@pl ? @pl : @padlist)]);
  155. }
  156. my $fakeop = new B::FAKEOP ("next" => 0, sibling => 0, ppaddr => $name);
  157. $start = $fakeop->save;
  158. debug "cc_queue: name $name returns $start\n" if $debug_queue;
  159. return $start;
  160. }
  161. BEGIN { B::C::set_callback(\&cc_queue) }
  162. sub valid_int { $_[0]->{flags} & VALID_INT }
  163. sub valid_double { $_[0]->{flags} & VALID_DOUBLE }
  164. sub valid_numeric { $_[0]->{flags} & (VALID_INT | VALID_DOUBLE) }
  165. sub valid_sv { $_[0]->{flags} & VALID_SV }
  166. sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
  167. sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
  168. sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
  169. sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
  170. sub top_bool { @stack ? $stack[-1]->as_bool : "SvTRUE(TOPs)" }
  171. sub pop_int { @stack ? (pop @stack)->as_int : "POPi" }
  172. sub pop_double { @stack ? (pop @stack)->as_double : "POPn" }
  173. sub pop_numeric { @stack ? (pop @stack)->as_numeric : "POPn" }
  174. sub pop_sv { @stack ? (pop @stack)->as_sv : "POPs" }
  175. sub pop_bool {
  176. if (@stack) {
  177. return ((pop @stack)->as_bool);
  178. } else {
  179. # Careful: POPs has an auto-decrement and SvTRUE evaluates
  180. # its argument more than once.
  181. runtime("sv = POPs;");
  182. return "SvTRUE(sv)";
  183. }
  184. }
  185. sub write_back_lexicals {
  186. my $avoid = shift || 0;
  187. debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
  188. if $debug_shadow;
  189. my $lex;
  190. foreach $lex (@pad) {
  191. next unless ref($lex);
  192. $lex->write_back unless $lex->{flags} & $avoid;
  193. }
  194. }
  195. sub save_or_restore_lexical_state {
  196. my $bblock=shift;
  197. unless( exists $lexstate{$bblock}){
  198. foreach my $lex (@pad) {
  199. next unless ref($lex);
  200. ${$lexstate{$bblock}}{$lex->{iv}} = $lex->{flags} ;
  201. }
  202. }
  203. else {
  204. foreach my $lex (@pad) {
  205. next unless ref($lex);
  206. my $old_flags=${$lexstate{$bblock}}{$lex->{iv}} ;
  207. next if ( $old_flags eq $lex->{flags});
  208. if (($old_flags & VALID_SV) && !($lex->{flags} & VALID_SV)){
  209. $lex->write_back;
  210. }
  211. if (($old_flags & VALID_DOUBLE) && !($lex->{flags} & VALID_DOUBLE)){
  212. $lex->load_double;
  213. }
  214. if (($old_flags & VALID_INT) && !($lex->{flags} & VALID_INT)){
  215. $lex->load_int;
  216. }
  217. }
  218. }
  219. }
  220. sub write_back_stack {
  221. my $obj;
  222. return unless @stack;
  223. runtime(sprintf("EXTEND(sp, %d);", scalar(@stack)));
  224. foreach $obj (@stack) {
  225. runtime(sprintf("PUSHs((SV*)%s);", $obj->as_sv));
  226. }
  227. @stack = ();
  228. }
  229. sub invalidate_lexicals {
  230. my $avoid = shift || 0;
  231. debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
  232. if $debug_shadow;
  233. my $lex;
  234. foreach $lex (@pad) {
  235. next unless ref($lex);
  236. $lex->invalidate unless $lex->{flags} & $avoid;
  237. }
  238. }
  239. sub reload_lexicals {
  240. my $lex;
  241. foreach $lex (@pad) {
  242. next unless ref($lex);
  243. my $type = $lex->{type};
  244. if ($type == T_INT) {
  245. $lex->as_int;
  246. } elsif ($type == T_DOUBLE) {
  247. $lex->as_double;
  248. } else {
  249. $lex->as_sv;
  250. }
  251. }
  252. }
  253. {
  254. package B::Pseudoreg;
  255. #
  256. # This class allocates pseudo-registers (OK, so they're C variables).
  257. #
  258. my %alloc; # Keyed by variable name. A value of 1 means the
  259. # variable has been declared. A value of 2 means
  260. # it's in use.
  261. sub new_scope { %alloc = () }
  262. sub new ($$$) {
  263. my ($class, $type, $prefix) = @_;
  264. my ($ptr, $i, $varname, $status, $obj);
  265. $prefix =~ s/^(\**)//;
  266. $ptr = $1;
  267. $i = 0;
  268. do {
  269. $varname = "$prefix$i";
  270. $status = $alloc{$varname};
  271. } while $status == 2;
  272. if ($status != 1) {
  273. # Not declared yet
  274. B::CC::declare($type, "$ptr$varname");
  275. $alloc{$varname} = 2; # declared and in use
  276. }
  277. $obj = bless \$varname, $class;
  278. return $obj;
  279. }
  280. sub DESTROY {
  281. my $obj = shift;
  282. $alloc{$$obj} = 1; # no longer in use but still declared
  283. }
  284. }
  285. {
  286. package B::Shadow;
  287. #
  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.
  290. #
  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
  294. # set shadow itself.
  295. sub new {
  296. my ($class, $write_back) = @_;
  297. # Object fields are perl shadow variable, validity flag
  298. # (for *C* variable) and callback sub for write_back
  299. # (passed perl shadow variable as argument).
  300. bless [undef, 1, $write_back], $class;
  301. }
  302. sub load {
  303. my ($obj, $newval) = @_;
  304. $obj->[1] = 0; # C variable no longer valid
  305. $obj->[0] = $newval;
  306. }
  307. sub write_back {
  308. my $obj = shift;
  309. if (!($obj->[1])) {
  310. $obj->[1] = 1; # C variable will now be valid
  311. &{$obj->[2]}($obj->[0]);
  312. }
  313. }
  314. sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
  315. }
  316. my $curcop = new B::Shadow (sub {
  317. my $opsym = shift->save;
  318. runtime("PL_curcop = (COP*)$opsym;");
  319. });
  320. #
  321. # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
  322. #
  323. sub dopoptoloop {
  324. my $cxix = $#cxstack;
  325. while ($cxix >= 0 && $cxstack[$cxix]->{type} != CXt_LOOP) {
  326. $cxix--;
  327. }
  328. debug "dopoptoloop: returning $cxix" if $debug_cxstack;
  329. return $cxix;
  330. }
  331. sub dopoptolabel {
  332. my $label = shift;
  333. my $cxix = $#cxstack;
  334. while ($cxix >= 0 &&
  335. ($cxstack[$cxix]->{type} != CXt_LOOP ||
  336. $cxstack[$cxix]->{label} ne $label)) {
  337. $cxix--;
  338. }
  339. debug "dopoptolabel: returning $cxix" if $debug_cxstack;
  340. return $cxix;
  341. }
  342. sub error {
  343. my $format = shift;
  344. my $file = $curcop->[0]->file;
  345. my $line = $curcop->[0]->line;
  346. $errors++;
  347. if (@_) {
  348. warn sprintf("%s:%d: $format\n", $file, $line, @_);
  349. } else {
  350. warn sprintf("%s:%d: %s\n", $file, $line, $format);
  351. }
  352. }
  353. #
  354. # Load pad takes (the elements of) a PADLIST as arguments and loads
  355. # up @pad with Stackobj-derived objects which represent those lexicals.
  356. # If/when perl itself can generate type information (my int $foo) then
  357. # we'll take advantage of that here. Until then, we'll use various hacks
  358. # to tell the compiler when we want a lexical to be a particular type
  359. # or to be a register.
  360. #
  361. sub load_pad {
  362. my ($namelistav, $valuelistav) = @_;
  363. @padlist = @_;
  364. my @namelist = $namelistav->ARRAY;
  365. my @valuelist = $valuelistav->ARRAY;
  366. my $ix;
  367. @pad = ();
  368. debug "load_pad: $#namelist names, $#valuelist values\n" if $debug_pad;
  369. # Temporary lexicals don't get named so it's possible for @valuelist
  370. # to be strictly longer than @namelist. We count $ix up to the end of
  371. # @valuelist but index into @namelist for the name. Any temporaries which
  372. # run off the end of @namelist will make $namesv undefined and we treat
  373. # that the same as having an explicit SPECIAL sv_undef object in @namelist.
  374. # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
  375. for ($ix = 1; $ix < @valuelist; $ix++) {
  376. my $namesv = $namelist[$ix];
  377. my $type = T_UNKNOWN;
  378. my $flags = 0;
  379. my $name = "tmp$ix";
  380. my $class = class($namesv);
  381. if (!defined($namesv) || $class eq "SPECIAL") {
  382. # temporaries have &PL_sv_undef instead of a PVNV for a name
  383. $flags = VALID_SV|TEMPORARY|REGISTER;
  384. } else {
  385. if ($namesv->PV =~ /^\$(.*)_([di])(r?)$/) {
  386. $name = $1;
  387. if ($2 eq "i") {
  388. $type = T_INT;
  389. $flags = VALID_SV|VALID_INT;
  390. } elsif ($2 eq "d") {
  391. $type = T_DOUBLE;
  392. $flags = VALID_SV|VALID_DOUBLE;
  393. }
  394. $flags |= REGISTER if $3;
  395. }
  396. }
  397. $pad[$ix] = new B::Stackobj::Padsv ($type, $flags, $ix,
  398. "i_$name", "d_$name");
  399. debug sprintf("PL_curpad[$ix] = %s\n", $pad[$ix]->peek) if $debug_pad;
  400. }
  401. }
  402. sub declare_pad {
  403. my $ix;
  404. for ($ix = 1; $ix <= $#pad; $ix++) {
  405. my $type = $pad[$ix]->{type};
  406. declare("IV", $type == T_INT ?
  407. sprintf("%s=0",$pad[$ix]->{iv}):$pad[$ix]->{iv}) if $pad[$ix]->save_int;
  408. declare("double", $type == T_DOUBLE ?
  409. sprintf("%s = 0",$pad[$ix]->{nv}):$pad[$ix]->{nv} )if $pad[$ix]->save_double;
  410. }
  411. }
  412. #
  413. # Debugging stuff
  414. #
  415. sub peek_stack { sprintf "stack = %s\n", join(" ", map($_->minipeek, @stack)) }
  416. #
  417. # OP stuff
  418. #
  419. sub label {
  420. my $op = shift;
  421. # XXX Preserve original label name for "real" labels?
  422. return sprintf("lab_%x", $$op);
  423. }
  424. sub write_label {
  425. my $op = shift;
  426. push_runtime(sprintf(" %s:", label($op)));
  427. }
  428. sub loadop {
  429. my $op = shift;
  430. my $opsym = $op->save;
  431. runtime("PL_op = $opsym;") unless $know_op;
  432. return $opsym;
  433. }
  434. sub doop {
  435. my $op = shift;
  436. my $ppname = $op->ppaddr;
  437. my $sym = loadop($op);
  438. runtime("DOOP($ppname);");
  439. $know_op = 1;
  440. return $sym;
  441. }
  442. sub gimme {
  443. my $op = shift;
  444. my $flags = $op->flags;
  445. return (($flags & OPf_WANT) ? (($flags & OPf_WANT)== OPf_WANT_LIST? G_ARRAY:G_SCALAR) : "dowantarray()");
  446. }
  447. #
  448. # Code generation for PP code
  449. #
  450. sub pp_null {
  451. my $op = shift;
  452. return $op->next;
  453. }
  454. sub pp_stub {
  455. my $op = shift;
  456. my $gimme = gimme($op);
  457. if ($gimme != G_ARRAY) {
  458. my $obj= new B::Stackobj::Const(sv_undef);
  459. push(@stack, $obj);
  460. # XXX Change to push a constant sv_undef Stackobj onto @stack
  461. #write_back_stack();
  462. #runtime("if ($gimme != G_ARRAY) XPUSHs(&PL_sv_undef);");
  463. }
  464. return $op->next;
  465. }
  466. sub pp_unstack {
  467. my $op = shift;
  468. @stack = ();
  469. runtime("PP_UNSTACK;");
  470. return $op->next;
  471. }
  472. sub pp_and {
  473. my $op = shift;
  474. my $next = $op->next;
  475. reload_lexicals();
  476. unshift(@bblock_todo, $next);
  477. if (@stack >= 1) {
  478. my $bool = pop_bool();
  479. write_back_stack();
  480. save_or_restore_lexical_state($$next);
  481. runtime(sprintf("if (!$bool) {XPUSHs(&PL_sv_no); goto %s;}", label($next)));
  482. } else {
  483. save_or_restore_lexical_state($$next);
  484. runtime(sprintf("if (!%s) goto %s;", top_bool(), label($next)),
  485. "*sp--;");
  486. }
  487. return $op->other;
  488. }
  489. sub pp_or {
  490. my $op = shift;
  491. my $next = $op->next;
  492. reload_lexicals();
  493. unshift(@bblock_todo, $next);
  494. if (@stack >= 1) {
  495. my $bool = pop_bool @stack;
  496. write_back_stack();
  497. save_or_restore_lexical_state($$next);
  498. runtime(sprintf("if (%s) { XPUSHs(&PL_sv_yes); goto %s; }",
  499. $bool, label($next)));
  500. } else {
  501. save_or_restore_lexical_state($$next);
  502. runtime(sprintf("if (%s) goto %s;", top_bool(), label($next)),
  503. "*sp--;");
  504. }
  505. return $op->other;
  506. }
  507. sub pp_cond_expr {
  508. my $op = shift;
  509. my $false = $op->next;
  510. unshift(@bblock_todo, $false);
  511. reload_lexicals();
  512. my $bool = pop_bool();
  513. write_back_stack();
  514. save_or_restore_lexical_state($$false);
  515. runtime(sprintf("if (!$bool) goto %s;", label($false)));
  516. return $op->other;
  517. }
  518. sub pp_padsv {
  519. my $op = shift;
  520. my $ix = $op->targ;
  521. push(@stack, $pad[$ix]);
  522. if ($op->flags & OPf_MOD) {
  523. my $private = $op->private;
  524. if ($private & OPpLVAL_INTRO) {
  525. runtime("SAVECLEARSV(PL_curpad[$ix]);");
  526. } elsif ($private & OPpDEREF) {
  527. runtime(sprintf("vivify_ref(PL_curpad[%d], %d);",
  528. $ix, $private & OPpDEREF));
  529. $pad[$ix]->invalidate;
  530. }
  531. }
  532. return $op->next;
  533. }
  534. sub pp_const {
  535. my $op = shift;
  536. my $sv = $op->sv;
  537. my $obj;
  538. # constant could be in the pad (under useithreads)
  539. if ($$sv) {
  540. $obj = $constobj{$$sv};
  541. if (!defined($obj)) {
  542. $obj = $constobj{$$sv} = new B::Stackobj::Const ($sv);
  543. }
  544. }
  545. else {
  546. $obj = $pad[$op->targ];
  547. }
  548. push(@stack, $obj);
  549. return $op->next;
  550. }
  551. sub pp_nextstate {
  552. my $op = shift;
  553. $curcop->load($op);
  554. @stack = ();
  555. debug(sprintf("%s:%d\n", $op->file, $op->line)) if $debug_lineno;
  556. runtime("TAINT_NOT;") unless $omit_taint;
  557. runtime("sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;");
  558. if ($freetmps_each_bblock || $freetmps_each_loop) {
  559. $need_freetmps = 1;
  560. } else {
  561. runtime("FREETMPS;");
  562. }
  563. return $op->next;
  564. }
  565. sub pp_dbstate {
  566. my $op = shift;
  567. $curcop->invalidate; # XXX?
  568. return default_pp($op);
  569. }
  570. #default_pp will handle this:
  571. #sub pp_bless { $curcop->write_back; default_pp(@_) }
  572. #sub pp_repeat { $curcop->write_back; default_pp(@_) }
  573. # The following subs need $curcop->write_back if we decide to support arybase:
  574. # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
  575. #sub pp_caller { $curcop->write_back; default_pp(@_) }
  576. #sub pp_reset { $curcop->write_back; default_pp(@_) }
  577. sub pp_rv2gv{
  578. my $op =shift;
  579. $curcop->write_back;
  580. write_back_lexicals() unless $skip_lexicals{$ppname};
  581. write_back_stack() unless $skip_stack{$ppname};
  582. my $sym=doop($op);
  583. if ($op->private & OPpDEREF) {
  584. $init->add(sprintf("((UNOP *)$sym)->op_first = $sym;"));
  585. $init->add(sprintf("((UNOP *)$sym)->op_type = %d;",
  586. $op->first->type));
  587. }
  588. return $op->next;
  589. }
  590. sub pp_sort {
  591. my $op = shift;
  592. my $ppname = $op->ppaddr;
  593. if ( $op->flags & OPf_SPECIAL && $op->flags & OPf_STACKED){
  594. #this indicates the sort BLOCK Array case
  595. #ugly surgery required.
  596. my $root=$op->first->sibling->first;
  597. my $start=$root->first;
  598. $op->first->save;
  599. $op->first->sibling->save;
  600. $root->save;
  601. my $sym=$start->save;
  602. my $fakeop=cc_queue("pp_sort".$$op,$root,$start);
  603. $init->add(sprintf("(%s)->op_next=%s;",$sym,$fakeop));
  604. }
  605. $curcop->write_back;
  606. write_back_lexicals();
  607. write_back_stack();
  608. doop($op);
  609. return $op->next;
  610. }
  611. sub pp_gv {
  612. my $op = shift;
  613. my $gvsym;
  614. if ($Config{useithreads}) {
  615. $gvsym = $pad[$op->padix]->as_sv;
  616. }
  617. else {
  618. $gvsym = $op->gv->save;
  619. }
  620. write_back_stack();
  621. runtime("XPUSHs((SV*)$gvsym);");
  622. return $op->next;
  623. }
  624. sub pp_gvsv {
  625. my $op = shift;
  626. my $gvsym;
  627. if ($Config{useithreads}) {
  628. $gvsym = $pad[$op->padix]->as_sv;
  629. }
  630. else {
  631. $gvsym = $op->gv->save;
  632. }
  633. write_back_stack();
  634. if ($op->private & OPpLVAL_INTRO) {
  635. runtime("XPUSHs(save_scalar($gvsym));");
  636. } else {
  637. runtime("XPUSHs(GvSV($gvsym));");
  638. }
  639. return $op->next;
  640. }
  641. sub pp_aelemfast {
  642. my $op = shift;
  643. my $gvsym;
  644. if ($Config{useithreads}) {
  645. $gvsym = $pad[$op->padix]->as_sv;
  646. }
  647. else {
  648. $gvsym = $op->gv->save;
  649. }
  650. my $ix = $op->private;
  651. my $flag = $op->flags & OPf_MOD;
  652. write_back_stack();
  653. runtime("svp = av_fetch(GvAV($gvsym), $ix, $flag);",
  654. "PUSHs(svp ? *svp : &PL_sv_undef);");
  655. return $op->next;
  656. }
  657. sub int_binop {
  658. my ($op, $operator) = @_;
  659. if ($op->flags & OPf_STACKED) {
  660. my $right = pop_int();
  661. if (@stack >= 1) {
  662. my $left = top_int();
  663. $stack[-1]->set_int(&$operator($left, $right));
  664. } else {
  665. runtime(sprintf("sv_setiv(TOPs, %s);",&$operator("TOPi", $right)));
  666. }
  667. } else {
  668. my $targ = $pad[$op->targ];
  669. my $right = new B::Pseudoreg ("IV", "riv");
  670. my $left = new B::Pseudoreg ("IV", "liv");
  671. runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int));
  672. $targ->set_int(&$operator($$left, $$right));
  673. push(@stack, $targ);
  674. }
  675. return $op->next;
  676. }
  677. sub INTS_CLOSED () { 0x1 }
  678. sub INT_RESULT () { 0x2 }
  679. sub NUMERIC_RESULT () { 0x4 }
  680. sub numeric_binop {
  681. my ($op, $operator, $flags) = @_;
  682. my $force_int = 0;
  683. $force_int ||= ($flags & INT_RESULT);
  684. $force_int ||= ($flags & INTS_CLOSED && @stack >= 2
  685. && valid_int($stack[-2]) && valid_int($stack[-1]));
  686. if ($op->flags & OPf_STACKED) {
  687. my $right = pop_numeric();
  688. if (@stack >= 1) {
  689. my $left = top_numeric();
  690. if ($force_int) {
  691. $stack[-1]->set_int(&$operator($left, $right));
  692. } else {
  693. $stack[-1]->set_numeric(&$operator($left, $right));
  694. }
  695. } else {
  696. if ($force_int) {
  697. my $rightruntime = new B::Pseudoreg ("IV", "riv");
  698. runtime(sprintf("$$rightruntime = %s;",$right));
  699. runtime(sprintf("sv_setiv(TOPs, %s);",
  700. &$operator("TOPi", $$rightruntime)));
  701. } else {
  702. my $rightruntime = new B::Pseudoreg ("double", "rnv");
  703. runtime(sprintf("$$rightruntime = %s;",$right));
  704. runtime(sprintf("sv_setnv(TOPs, %s);",
  705. &$operator("TOPn",$$rightruntime)));
  706. }
  707. }
  708. } else {
  709. my $targ = $pad[$op->targ];
  710. $force_int ||= ($targ->{type} == T_INT);
  711. if ($force_int) {
  712. my $right = new B::Pseudoreg ("IV", "riv");
  713. my $left = new B::Pseudoreg ("IV", "liv");
  714. runtime(sprintf("$$right = %s; $$left = %s;",
  715. pop_numeric(), pop_numeric));
  716. $targ->set_int(&$operator($$left, $$right));
  717. } else {
  718. my $right = new B::Pseudoreg ("double", "rnv");
  719. my $left = new B::Pseudoreg ("double", "lnv");
  720. runtime(sprintf("$$right = %s; $$left = %s;",
  721. pop_numeric(), pop_numeric));
  722. $targ->set_numeric(&$operator($$left, $$right));
  723. }
  724. push(@stack, $targ);
  725. }
  726. return $op->next;
  727. }
  728. sub pp_ncmp {
  729. my ($op) = @_;
  730. if ($op->flags & OPf_STACKED) {
  731. my $right = pop_numeric();
  732. if (@stack >= 1) {
  733. my $left = top_numeric();
  734. runtime sprintf("if (%s > %s){",$left,$right);
  735. $stack[-1]->set_int(1);
  736. $stack[-1]->write_back();
  737. runtime sprintf("}else if (%s < %s ) {",$left,$right);
  738. $stack[-1]->set_int(-1);
  739. $stack[-1]->write_back();
  740. runtime sprintf("}else if (%s == %s) {",$left,$right);
  741. $stack[-1]->set_int(0);
  742. $stack[-1]->write_back();
  743. runtime sprintf("}else {");
  744. $stack[-1]->set_sv("&PL_sv_undef");
  745. runtime "}";
  746. } else {
  747. my $rightruntime = new B::Pseudoreg ("double", "rnv");
  748. runtime(sprintf("$$rightruntime = %s;",$right));
  749. runtime sprintf(qq/if ("TOPn" > %s){/,$rightruntime);
  750. runtime sprintf("sv_setiv(TOPs,1);");
  751. runtime sprintf(qq/}else if ( "TOPn" < %s ) {/,$$rightruntime);
  752. runtime sprintf("sv_setiv(TOPs,-1);");
  753. runtime sprintf(qq/} else if ("TOPn" == %s) {/,$$rightruntime);
  754. runtime sprintf("sv_setiv(TOPs,0);");
  755. runtime sprintf(qq/}else {/);
  756. runtime sprintf("sv_setiv(TOPs,&PL_sv_undef;");
  757. runtime "}";
  758. }
  759. } else {
  760. my $targ = $pad[$op->targ];
  761. my $right = new B::Pseudoreg ("double", "rnv");
  762. my $left = new B::Pseudoreg ("double", "lnv");
  763. runtime(sprintf("$$right = %s; $$left = %s;",
  764. pop_numeric(), pop_numeric));
  765. runtime sprintf("if (%s > %s){",$$left,$$right);
  766. $targ->set_int(1);
  767. $targ->write_back();
  768. runtime sprintf("}else if (%s < %s ) {",$$left,$$right);
  769. $targ->set_int(-1);
  770. $targ->write_back();
  771. runtime sprintf("}else if (%s == %s) {",$$left,$$right);
  772. $targ->set_int(0);
  773. $targ->write_back();
  774. runtime sprintf("}else {");
  775. $targ->set_sv("&PL_sv_undef");
  776. runtime "}";
  777. push(@stack, $targ);
  778. }
  779. return $op->next;
  780. }
  781. sub sv_binop {
  782. my ($op, $operator, $flags) = @_;
  783. if ($op->flags & OPf_STACKED) {
  784. my $right = pop_sv();
  785. if (@stack >= 1) {
  786. my $left = top_sv();
  787. if ($flags & INT_RESULT) {
  788. $stack[-1]->set_int(&$operator($left, $right));
  789. } elsif ($flags & NUMERIC_RESULT) {
  790. $stack[-1]->set_numeric(&$operator($left, $right));
  791. } else {
  792. # XXX Does this work?
  793. runtime(sprintf("sv_setsv($left, %s);",
  794. &$operator($left, $right)));
  795. $stack[-1]->invalidate;
  796. }
  797. } else {
  798. my $f;
  799. if ($flags & INT_RESULT) {
  800. $f = "sv_setiv";
  801. } elsif ($flags & NUMERIC_RESULT) {
  802. $f = "sv_setnv";
  803. } else {
  804. $f = "sv_setsv";
  805. }
  806. runtime(sprintf("%s(TOPs, %s);", $f, &$operator("TOPs", $right)));
  807. }
  808. } else {
  809. my $targ = $pad[$op->targ];
  810. runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv));
  811. if ($flags & INT_RESULT) {
  812. $targ->set_int(&$operator("left", "right"));
  813. } elsif ($flags & NUMERIC_RESULT) {
  814. $targ->set_numeric(&$operator("left", "right"));
  815. } else {
  816. # XXX Does this work?
  817. runtime(sprintf("sv_setsv(%s, %s);",
  818. $targ->as_sv, &$operator("left", "right")));
  819. $targ->invalidate;
  820. }
  821. push(@stack, $targ);
  822. }
  823. return $op->next;
  824. }
  825. sub bool_int_binop {
  826. my ($op, $operator) = @_;
  827. my $right = new B::Pseudoreg ("IV", "riv");
  828. my $left = new B::Pseudoreg ("IV", "liv");
  829. runtime(sprintf("$$right = %s; $$left = %s;", pop_int(), pop_int()));
  830. my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
  831. $bool->set_int(&$operator($$left, $$right));
  832. push(@stack, $bool);
  833. return $op->next;
  834. }
  835. sub bool_numeric_binop {
  836. my ($op, $operator) = @_;
  837. my $right = new B::Pseudoreg ("double", "rnv");
  838. my $left = new B::Pseudoreg ("double", "lnv");
  839. runtime(sprintf("$$right = %s; $$left = %s;",
  840. pop_numeric(), pop_numeric()));
  841. my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
  842. $bool->set_numeric(&$operator($$left, $$right));
  843. push(@stack, $bool);
  844. return $op->next;
  845. }
  846. sub bool_sv_binop {
  847. my ($op, $operator) = @_;
  848. runtime(sprintf("right = %s; left = %s;", pop_sv(), pop_sv()));
  849. my $bool = new B::Stackobj::Bool (new B::Pseudoreg ("int", "b"));
  850. $bool->set_numeric(&$operator("left", "right"));
  851. push(@stack, $bool);
  852. return $op->next;
  853. }
  854. sub infix_op {
  855. my $opname = shift;
  856. return sub { "$_[0] $opname $_[1]" }
  857. }
  858. sub prefix_op {
  859. my $opname = shift;
  860. return sub { sprintf("%s(%s)", $opname, join(", ", @_)) }
  861. }
  862. BEGIN {
  863. my $plus_op = infix_op("+");
  864. my $minus_op = infix_op("-");
  865. my $multiply_op = infix_op("*");
  866. my $divide_op = infix_op("/");
  867. my $modulo_op = infix_op("%");
  868. my $lshift_op = infix_op("<<");
  869. my $rshift_op = infix_op(">>");
  870. my $scmp_op = prefix_op("sv_cmp");
  871. my $seq_op = prefix_op("sv_eq");
  872. my $sne_op = prefix_op("!sv_eq");
  873. my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
  874. my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
  875. my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
  876. my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
  877. my $eq_op = infix_op("==");
  878. my $ne_op = infix_op("!=");
  879. my $lt_op = infix_op("<");
  880. my $gt_op = infix_op(">");
  881. my $le_op = infix_op("<=");
  882. my $ge_op = infix_op(">=");
  883. #
  884. # XXX The standard perl PP code has extra handling for
  885. # some special case arguments of these operators.
  886. #
  887. sub pp_add { numeric_binop($_[0], $plus_op) }
  888. sub pp_subtract { numeric_binop($_[0], $minus_op) }
  889. sub pp_multiply { numeric_binop($_[0], $multiply_op) }
  890. sub pp_divide { numeric_binop($_[0], $divide_op) }
  891. sub pp_modulo { int_binop($_[0], $modulo_op) } # differs from perl's
  892. sub pp_left_shift { int_binop($_[0], $lshift_op) }
  893. sub pp_right_shift { int_binop($_[0], $rshift_op) }
  894. sub pp_i_add { int_binop($_[0], $plus_op) }
  895. sub pp_i_subtract { int_binop($_[0], $minus_op) }
  896. sub pp_i_multiply { int_binop($_[0], $multiply_op) }
  897. sub pp_i_divide { int_binop($_[0], $divide_op) }
  898. sub pp_i_modulo { int_binop($_[0], $modulo_op) }
  899. sub pp_eq { bool_numeric_binop($_[0], $eq_op) }
  900. sub pp_ne { bool_numeric_binop($_[0], $ne_op) }
  901. sub pp_lt { bool_numeric_binop($_[0], $lt_op) }
  902. sub pp_gt { bool_numeric_binop($_[0], $gt_op) }
  903. sub pp_le { bool_numeric_binop($_[0], $le_op) }
  904. sub pp_ge { bool_numeric_binop($_[0], $ge_op) }
  905. sub pp_i_eq { bool_int_binop($_[0], $eq_op) }
  906. sub pp_i_ne { bool_int_binop($_[0], $ne_op) }
  907. sub pp_i_lt { bool_int_binop($_[0], $lt_op) }
  908. sub pp_i_gt { bool_int_binop($_[0], $gt_op) }
  909. sub pp_i_le { bool_int_binop($_[0], $le_op) }
  910. sub pp_i_ge { bool_int_binop($_[0], $ge_op) }
  911. sub pp_scmp { sv_binop($_[0], $scmp_op, INT_RESULT) }
  912. sub pp_slt { bool_sv_binop($_[0], $slt_op) }
  913. sub pp_sgt { bool_sv_binop($_[0], $sgt_op) }
  914. sub pp_sle { bool_sv_binop($_[0], $sle_op) }
  915. sub pp_sge { bool_sv_binop($_[0], $sge_op) }
  916. sub pp_seq { bool_sv_binop($_[0], $seq_op) }
  917. sub pp_sne { bool_sv_binop($_[0], $sne_op) }
  918. }
  919. sub pp_sassign {
  920. my $op = shift;
  921. my $backwards = $op->private & OPpASSIGN_BACKWARDS;
  922. my ($dst, $src);
  923. if (@stack >= 2) {
  924. $dst = pop @stack;
  925. $src = pop @stack;
  926. ($src, $dst) = ($dst, $src) if $backwards;
  927. my $type = $src->{type};
  928. if ($type == T_INT) {
  929. $dst->set_int($src->as_int,$src->{flags} & VALID_UNSIGNED);
  930. } elsif ($type == T_DOUBLE) {
  931. $dst->set_numeric($src->as_numeric);
  932. } else {
  933. $dst->set_sv($src->as_sv);
  934. }
  935. push(@stack, $dst);
  936. } elsif (@stack == 1) {
  937. if ($backwards) {
  938. my $src = pop @stack;
  939. my $type = $src->{type};
  940. runtime("if (PL_tainting && PL_tainted) TAINT_NOT;");
  941. if ($type == T_INT) {
  942. if ($src->{flags} & VALID_UNSIGNED){
  943. runtime sprintf("sv_setuv(TOPs, %s);", $src->as_int);
  944. }else{
  945. runtime sprintf("sv_setiv(TOPs, %s);", $src->as_int);
  946. }
  947. } elsif ($type == T_DOUBLE) {
  948. runtime sprintf("sv_setnv(TOPs, %s);", $src->as_double);
  949. } else {
  950. runtime sprintf("sv_setsv(TOPs, %s);", $src->as_sv);
  951. }
  952. runtime("SvSETMAGIC(TOPs);");
  953. } else {
  954. my $dst = $stack[-1];
  955. my $type = $dst->{type};
  956. runtime("sv = POPs;");
  957. runtime("MAYBE_TAINT_SASSIGN_SRC(sv);");
  958. if ($type == T_INT) {
  959. $dst->set_int("SvIV(sv)");
  960. } elsif ($type == T_DOUBLE) {
  961. $dst->set_double("SvNV(sv)");
  962. } else {
  963. runtime("SvSetMagicSV($dst->{sv}, sv);");
  964. $dst->invalidate;
  965. }
  966. }
  967. } else {
  968. if ($backwards) {
  969. runtime("src = POPs; dst = TOPs;");
  970. } else {
  971. runtime("dst = POPs; src = TOPs;");
  972. }
  973. runtime("MAYBE_TAINT_SASSIGN_SRC(src);",
  974. "SvSetSV(dst, src);",
  975. "SvSETMAGIC(dst);",
  976. "SETs(dst);");
  977. }
  978. return $op->next;
  979. }
  980. sub pp_preinc {
  981. my $op = shift;
  982. if (@stack >= 1) {
  983. my $obj = $stack[-1];
  984. my $type = $obj->{type};
  985. if ($type == T_INT || $type == T_DOUBLE) {
  986. $obj->set_int($obj->as_int . " + 1");
  987. } else {
  988. runtime sprintf("PP_PREINC(%s);", $obj->as_sv);
  989. $obj->invalidate();
  990. }
  991. } else {
  992. runtime sprintf("PP_PREINC(TOPs);");
  993. }
  994. return $op->next;
  995. }
  996. sub pp_pushmark {
  997. my $op = shift;
  998. write_back_stack();
  999. runtime("PUSHMARK(sp);");
  1000. return $op->next;
  1001. }
  1002. sub pp_list {
  1003. my $op = shift;
  1004. write_back_stack();
  1005. my $gimme = gimme($op);
  1006. if ($gimme == G_ARRAY) { # sic
  1007. runtime("POPMARK;"); # need this even though not a "full" pp_list
  1008. } else {
  1009. runtime("PP_LIST($gimme);");
  1010. }
  1011. return $op->next;
  1012. }
  1013. sub pp_entersub {
  1014. my $op = shift;
  1015. $curcop->write_back;
  1016. write_back_lexicals(REGISTER|TEMPORARY);
  1017. write_back_stack();
  1018. my $sym = doop($op);
  1019. runtime("while (PL_op != ($sym)->op_next && PL_op != (OP*)0 ){");
  1020. runtime("PL_op = (*PL_op->op_ppaddr)(aTHX);");
  1021. runtime("SPAGAIN;}");
  1022. $know_op = 0;
  1023. invalidate_lexicals(REGISTER|TEMPORARY);
  1024. return $op->next;
  1025. }
  1026. sub pp_formline {
  1027. my $op = shift;
  1028. my $ppname = $op->ppaddr;
  1029. write_back_lexicals() unless $skip_lexicals{$ppname};
  1030. write_back_stack() unless $skip_stack{$ppname};
  1031. my $sym=doop($op);
  1032. # See comment in pp_grepwhile to see why!
  1033. $init->add("((LISTOP*)$sym)->op_first = $sym;");
  1034. runtime("if (PL_op == ((LISTOP*)($sym))->op_first){");
  1035. save_or_restore_lexical_state(${$op->first});
  1036. runtime( sprintf("goto %s;",label($op->first)));
  1037. runtime("}");
  1038. return $op->next;
  1039. }
  1040. sub pp_goto{
  1041. my $op = shift;
  1042. my $ppname = $op->ppaddr;
  1043. write_back_lexicals() unless $skip_lexicals{$ppname};
  1044. write_back_stack() unless $skip_stack{$ppname};
  1045. my $sym=doop($op);
  1046. runtime("if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;}");
  1047. invalidate_lexicals() unless $skip_invalidate{$ppname};
  1048. return $op->next;
  1049. }
  1050. sub pp_enterwrite {
  1051. my $op = shift;
  1052. pp_entersub($op);
  1053. }
  1054. sub pp_leavesub{
  1055. my $op = shift;
  1056. write_back_lexicals() unless $skip_lexicals{$ppname};
  1057. write_back_stack() unless $skip_stack{$ppname};
  1058. runtime("if (PL_curstackinfo->si_type == PERLSI_SORT){");
  1059. runtime("\tPUTBACK;return 0;");
  1060. runtime("}");
  1061. doop($op);
  1062. return $op->next;
  1063. }
  1064. sub pp_leavewrite {
  1065. my $op = shift;
  1066. write_back_lexicals(REGISTER|TEMPORARY);
  1067. write_back_stack();
  1068. my $sym = doop($op);
  1069. # XXX Is this the right way to distinguish between it returning
  1070. # CvSTART(cv) (via doform) and pop_return()?
  1071. #runtime("if (PL_op) PL_op = (*PL_op->op_ppaddr)(aTHX);");
  1072. runtime("SPAGAIN;");
  1073. $know_op = 0;
  1074. invalidate_lexicals(REGISTER|TEMPORARY);
  1075. return $op->next;
  1076. }
  1077. sub doeval {
  1078. my $op = shift;
  1079. $curcop->write_back;
  1080. write_back_lexicals(REGISTER|TEMPORARY);
  1081. write_back_stack();
  1082. my $sym = loadop($op);
  1083. my $ppaddr = $op->ppaddr;
  1084. #runtime(qq/printf("$ppaddr type eval\n");/);
  1085. runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
  1086. $know_op = 1;
  1087. invalidate_lexicals(REGISTER|TEMPORARY);
  1088. return $op->next;
  1089. }
  1090. sub pp_entereval { doeval(@_) }
  1091. sub pp_dofile { doeval(@_) }
  1092. #pp_require is protected by pp_entertry, so no protection for it.
  1093. sub pp_require {
  1094. my $op = shift;
  1095. $curcop->write_back;
  1096. write_back_lexicals(REGISTER|TEMPORARY);
  1097. write_back_stack();
  1098. my $sym = doop($op);
  1099. runtime("while (PL_op != ($sym)->op_next && PL_op != (OP*)0 ){");
  1100. runtime("PL_op = (*PL_op->op_ppaddr)(ARGS);");
  1101. runtime("SPAGAIN;}");
  1102. $know_op = 1;
  1103. invalidate_lexicals(REGISTER|TEMPORARY);
  1104. return $op->next;
  1105. }
  1106. sub pp_entertry {
  1107. my $op = shift;
  1108. $curcop->write_back;
  1109. write_back_lexicals(REGISTER|TEMPORARY);
  1110. write_back_stack();
  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);
  1116. return $op->next;
  1117. }
  1118. sub pp_leavetry{
  1119. my $op=shift;
  1120. default_pp($op);
  1121. runtime("PP_LEAVETRY;");
  1122. return $op->next;
  1123. }
  1124. sub pp_grepstart {
  1125. my $op = shift;
  1126. if ($need_freetmps && $freetmps_each_loop) {
  1127. runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
  1128. $need_freetmps = 0;
  1129. }
  1130. write_back_stack();
  1131. my $sym= doop($op);
  1132. my $next=$op->next;
  1133. $next->save;
  1134. my $nexttonext=$next->next;
  1135. $nexttonext->save;
  1136. save_or_restore_lexical_state($$nexttonext);
  1137. runtime(sprintf("if (PL_op == (($sym)->op_next)->op_next) goto %s;",
  1138. label($nexttonext)));
  1139. return $op->next->other;
  1140. }
  1141. sub pp_mapstart {
  1142. my $op = shift;
  1143. if ($need_freetmps && $freetmps_each_loop) {
  1144. runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
  1145. $need_freetmps = 0;
  1146. }
  1147. write_back_stack();
  1148. # pp_mapstart can return either op_next->op_next or op_next->op_other and
  1149. # we need to be able to distinguish the two at runtime.
  1150. my $sym= doop($op);
  1151. my $next=$op->next;
  1152. $next->save;
  1153. my $nexttonext=$next->next;
  1154. $nexttonext->save;
  1155. save_or_restore_lexical_state($$nexttonext);
  1156. runtime(sprintf("if (PL_op == (($sym)->op_next)->op_next) goto %s;",
  1157. label($nexttonext)));
  1158. return $op->next->other;
  1159. }
  1160. sub pp_grepwhile {
  1161. my $op = shift;
  1162. my $next = $op->next;
  1163. unshift(@bblock_todo, $next);
  1164. write_back_lexicals();
  1165. write_back_stack();
  1166. my $sym = doop($op);
  1167. # pp_grepwhile can return either op_next or op_other and we need to
  1168. # be able to distinguish the two at runtime. Since it's possible for
  1169. # both ops to be "inlined", the fields could both be zero. To get
  1170. # around that, we hack op_next to be our own op (purely because we
  1171. # know it's a non-NULL pointer and can't be the same as op_other).
  1172. $init->add("((LOGOP*)$sym)->op_next = $sym;");
  1173. save_or_restore_lexical_state($$next);
  1174. runtime(sprintf("if (PL_op == ($sym)->op_next) goto %s;", label($next)));
  1175. $know_op = 0;
  1176. return $op->other;
  1177. }
  1178. sub pp_mapwhile {
  1179. pp_grepwhile(@_);
  1180. }
  1181. sub pp_return {
  1182. my $op = shift;
  1183. write_back_lexicals(REGISTER|TEMPORARY);
  1184. write_back_stack();
  1185. doop($op);
  1186. runtime("PUTBACK;", "return PL_op;");
  1187. $know_op = 0;
  1188. return $op->next;
  1189. }
  1190. sub nyi {
  1191. my $op = shift;
  1192. warn sprintf("%s not yet implemented properly\n", $op->ppaddr);
  1193. return default_pp($op);
  1194. }
  1195. sub pp_range {
  1196. my $op = shift;
  1197. my $flags = $op->flags;
  1198. if (!($flags & OPf_WANT)) {
  1199. error("context of range unknown at compile-time");
  1200. }
  1201. write_back_lexicals();
  1202. write_back_stack();
  1203. unless (($flags & OPf_WANT)== OPf_WANT_LIST) {
  1204. # We need to save our UNOP structure since pp_flop uses
  1205. # it to find and adjust out targ. We don't need it ourselves.
  1206. $op->save;
  1207. save_or_restore_lexical_state(${$op->other});
  1208. runtime sprintf("if (SvTRUE(PL_curpad[%d])) goto %s;",
  1209. $op->targ, label($op->other));
  1210. unshift(@bblock_todo, $op->other);
  1211. }
  1212. return $op->next;
  1213. }
  1214. sub pp_flip {
  1215. my $op = shift;
  1216. my $flags = $op->flags;
  1217. if (!($flags & OPf_WANT)) {
  1218. error("context of flip unknown at compile-time");
  1219. }
  1220. if (($flags & OPf_WANT)==OPf_WANT_LIST) {
  1221. return $op->first->other;
  1222. }
  1223. write_back_lexicals();
  1224. write_back_stack();
  1225. # We need to save our UNOP structure since pp_flop uses
  1226. # it to find and adjust out targ. We don't need it ourselves.
  1227. $op->save;
  1228. my $ix = $op->targ;
  1229. my $rangeix = $op->first->targ;
  1230. runtime(($op->private & OPpFLIP_LINENUM) ?
  1231. "if (PL_last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(PL_last_in_gv))) {"
  1232. : "if (SvTRUE(TOPs)) {");
  1233. runtime("\tsv_setiv(PL_curpad[$rangeix], 1);");
  1234. if ($op->flags & OPf_SPECIAL) {
  1235. runtime("sv_setiv(PL_curpad[$ix], 1);");
  1236. } else {
  1237. save_or_restore_lexical_state(${$op->first->other});
  1238. runtime("\tsv_setiv(PL_curpad[$ix], 0);",
  1239. "\tsp--;",
  1240. sprintf("\tgoto %s;", label($op->first->other)));
  1241. }
  1242. runtime("}",
  1243. qq{sv_setpv(PL_curpad[$ix], "");},
  1244. "SETs(PL_curpad[$ix]);");
  1245. $know_op = 0;
  1246. return $op->next;
  1247. }
  1248. sub pp_flop {
  1249. my $op = shift;
  1250. default_pp($op);
  1251. $know_op = 0;
  1252. return $op->next;
  1253. }
  1254. sub enterloop {
  1255. my $op = shift;
  1256. my $nextop = $op->nextop;
  1257. my $lastop = $op->lastop;
  1258. my $redoop = $op->redoop;
  1259. $curcop->write_back;
  1260. debug "enterloop: pushing on cxstack" if $debug_cxstack;
  1261. push(@cxstack, {
  1262. type => CXt_LOOP,
  1263. op => $op,
  1264. "label" => $curcop->[0]->label,
  1265. nextop => $nextop,
  1266. lastop => $lastop,
  1267. redoop => $redoop
  1268. });
  1269. $nextop->save;
  1270. $lastop->save;
  1271. $redoop->save;
  1272. return default_pp($op);
  1273. }
  1274. sub pp_enterloop { enterloop(@_) }
  1275. sub pp_enteriter { enterloop(@_) }
  1276. sub pp_leaveloop {
  1277. my $op = shift;
  1278. if (!@cxstack) {
  1279. die "panic: leaveloop";
  1280. }
  1281. debug "leaveloop: popping from cxstack" if $debug_cxstack;
  1282. pop(@cxstack);
  1283. return default_pp($op);
  1284. }
  1285. sub pp_next {
  1286. my $op = shift;
  1287. my $cxix;
  1288. if ($op->flags & OPf_SPECIAL) {
  1289. $cxix = dopoptoloop();
  1290. if ($cxix < 0) {
  1291. error('"next" used outside loop');
  1292. return $op->next; # ignore the op
  1293. }
  1294. } else {
  1295. $cxix = dopoptolabel($op->pv);
  1296. if ($cxix < 0) {
  1297. error('Label not found at compile time for "next %s"', $op->pv);
  1298. return $op->next; # ignore the op
  1299. }
  1300. }
  1301. default_pp($op);
  1302. my $nextop = $cxstack[$cxix]->{nextop};
  1303. push(@bblock_todo, $nextop);
  1304. save_or_restore_lexical_state($$nextop);
  1305. runtime(sprintf("goto %s;", label($nextop)));
  1306. return $op->next;
  1307. }
  1308. sub pp_redo {
  1309. my $op = shift;
  1310. my $cxix;
  1311. if ($op->flags & OPf_SPECIAL) {
  1312. $cxix = dopoptoloop();
  1313. if ($cxix < 0) {
  1314. error('"redo" used outside loop');
  1315. return $op->next; # ignore the op
  1316. }
  1317. } else {
  1318. $cxix = dopoptolabel($op->pv);
  1319. if ($cxix < 0) {
  1320. error('Label not found at compile time for "redo %s"', $op->pv);
  1321. return $op->next; # ignore the op
  1322. }
  1323. }
  1324. default_pp($op);
  1325. my $redoop = $cxstack[$cxix]->{redoop};
  1326. push(@bblock_todo, $redoop);
  1327. save_or_restore_lexical_state($$redoop);
  1328. runtime(sprintf("goto %s;", label($redoop)));
  1329. return $op->next;
  1330. }
  1331. sub pp_last {
  1332. my $op = shift;
  1333. my $cxix;
  1334. if ($op->flags & OPf_SPECIAL) {
  1335. $cxix = dopoptoloop();
  1336. if ($cxix < 0) {
  1337. error('"last" used outside loop');
  1338. return $op->next; # ignore the op
  1339. }
  1340. } else {
  1341. $cxix = dopoptolabel($op->pv);
  1342. if ($cxix < 0) {
  1343. error('Label not found at compile time for "last %s"', $op->pv);
  1344. return $op->next; # ignore the op
  1345. }
  1346. # XXX Add support for "last" to leave non-loop blocks
  1347. if ($cxstack[$cxix]->{type} != CXt_LOOP) {
  1348. error('Use of "last" for non-loop blocks is not yet implemented');
  1349. return $op->next; # ignore the op
  1350. }
  1351. }
  1352. default_pp($op);
  1353. my $lastop = $cxstack[$cxix]->{lastop}->next;
  1354. push(@bblock_todo, $lastop);
  1355. save_or_restore_lexical_state($$lastop);
  1356. runtime(sprintf("goto %s;", label($lastop)));
  1357. return $op->next;
  1358. }
  1359. sub pp_subst {
  1360. my $op = shift;
  1361. write_back_lexicals();
  1362. write_back_stack();
  1363. my $sym = doop($op);
  1364. my $replroot = $op->pmreplroot;
  1365. if ($$replroot) {
  1366. save_or_restore_lexical_state($$replroot);
  1367. runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplroot) goto %s;",
  1368. $sym, label($replroot));
  1369. $op->pmreplstart->save;
  1370. push(@bblock_todo, $replroot);
  1371. }
  1372. invalidate_lexicals();
  1373. return $op->next;
  1374. }
  1375. sub pp_substcont {
  1376. my $op = shift;
  1377. write_back_lexicals();
  1378. write_back_stack();
  1379. doop($op);
  1380. my $pmop = $op->other;
  1381. # warn sprintf("substcont: op = %s, pmop = %s\n",
  1382. # peekop($op), peekop($pmop));#debug
  1383. # my $pmopsym = objsym($pmop);
  1384. my $pmopsym = $pmop->save; # XXX can this recurse?
  1385. # warn "pmopsym = $pmopsym\n";#debug
  1386. save_or_restore_lexical_state(${$pmop->pmreplstart});
  1387. runtime sprintf("if (PL_op == ((PMOP*)(%s))->op_pmreplstart) goto %s;",
  1388. $pmopsym, label($pmop->pmreplstart));
  1389. invalidate_lexicals();
  1390. return $pmop->next;
  1391. }
  1392. sub default_pp {
  1393. my $op = shift;
  1394. my $ppname = "pp_" . $op->name;
  1395. if ($curcop and $need_curcop{$ppname}){
  1396. $curcop->write_back;
  1397. }
  1398. write_back_lexicals() unless $skip_lexicals{$ppname};
  1399. write_back_stack() unless $skip_stack{$ppname};
  1400. doop($op);
  1401. # XXX If the only way that ops can write to a TEMPORARY lexical is
  1402. # when it's named in $op->targ then we could call
  1403. # invalidate_lexicals(TEMPORARY) and avoid having to write back all
  1404. # the temporaries. For now, we'll play it safe and write back the lot.
  1405. invalidate_lexicals() unless $skip_invalidate{$ppname};
  1406. return $op->next;
  1407. }
  1408. sub compile_op {
  1409. my $op = shift;
  1410. my $ppname = "pp_" . $op->name;
  1411. if (exists $ignore_op{$ppname}) {
  1412. return $op->next;
  1413. }
  1414. debug peek_stack() if $debug_stack;
  1415. if ($debug_op) {
  1416. debug sprintf("%s [%s]\n",
  1417. peekop($op),
  1418. $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ);
  1419. }
  1420. no strict 'refs';
  1421. if (defined(&$ppname)) {
  1422. $know_op = 0;
  1423. return &$ppname($op);
  1424. } else {
  1425. return default_pp($op);
  1426. }
  1427. }
  1428. sub compile_bblock {
  1429. my $op = shift;
  1430. #warn "compile_bblock: ", peekop($op), "\n"; # debug
  1431. save_or_restore_lexical_state($$op);
  1432. write_label($op);
  1433. $know_op = 0;
  1434. do {
  1435. $op = compile_op($op);
  1436. } while (defined($op) && $$op && !exists($leaders->{$$op}));
  1437. write_back_stack(); # boo hoo: big loss
  1438. reload_lexicals();
  1439. return $op;
  1440. }
  1441. sub cc {
  1442. my ($name, $root, $start, @padlist) = @_;
  1443. my $op;
  1444. if($done{$$start}){
  1445. #warn "repeat=>".ref($start)."$name,\n";#debug
  1446. $decl->add(sprintf("#define $name %s",$done{$$start}));
  1447. return;
  1448. }
  1449. init_pp($name);
  1450. load_pad(@padlist);
  1451. %lexstate=();
  1452. B::Pseudoreg->new_scope;
  1453. @cxstack = ();
  1454. if ($debug_timings) {
  1455. warn sprintf("Basic block analysis at %s\n", timing_info);
  1456. }
  1457. $leaders = find_leaders($root, $start);
  1458. my @leaders= keys %$leaders;
  1459. if ($#leaders > -1) {
  1460. @bblock_todo = ($start, values %$leaders) ;
  1461. } else{
  1462. runtime("return PL_op?PL_op->op_next:0;");
  1463. }
  1464. if ($debug_timings) {
  1465. warn sprintf("Compilation at %s\n", timing_info);
  1466. }
  1467. while (@bblock_todo) {
  1468. $op = shift @bblock_todo;
  1469. #warn sprintf("Considering basic block %s\n", peekop($op)); # debug
  1470. next if !defined($op) || !$$op || $done{$$op};
  1471. #warn "...compiling it\n"; # debug
  1472. do {
  1473. $done{$$op} = $name;
  1474. $op = compile_bblock($op);
  1475. if ($need_freetmps && $freetmps_each_bblock) {
  1476. runtime("FREETMPS;");
  1477. $need_freetmps = 0;
  1478. }
  1479. } while defined($op) && $$op && !$done{$$op};
  1480. if ($need_freetmps && $freetmps_each_loop) {
  1481. runtime("FREETMPS;");
  1482. $need_freetmps = 0;
  1483. }
  1484. if (!$$op) {
  1485. runtime("PUTBACK;","return PL_op;");
  1486. } elsif ($done{$$op}) {
  1487. save_or_restore_lexical_state($$op);
  1488. runtime(sprintf("goto %s;", label($op)));
  1489. }
  1490. }
  1491. if ($debug_timings) {
  1492. warn sprintf("Saving runtime at %s\n", timing_info);
  1493. }
  1494. declare_pad(@padlist) ;
  1495. save_runtime();
  1496. }
  1497. sub cc_recurse {
  1498. my $ccinfo;
  1499. my $start;
  1500. $start = cc_queue(@_) if @_;
  1501. while ($ccinfo = shift @cc_todo) {
  1502. cc(@$ccinfo);
  1503. }
  1504. return $start;
  1505. }
  1506. sub cc_obj {
  1507. my ($name, $cvref) = @_;
  1508. my $cv = svref_2object($cvref);
  1509. my @padlist = $cv->PADLIST->ARRAY;
  1510. my $curpad_sym = $padlist[1]->save;
  1511. cc_recurse($name, $cv->ROOT, $cv->START, @padlist);
  1512. }
  1513. sub cc_main {
  1514. my @comppadlist = comppadlist->ARRAY;
  1515. my $curpad_nam = $comppadlist[0]->save;
  1516. my $curpad_sym = $comppadlist[1]->save;
  1517. my $init_av = init_av->save;
  1518. my $start = cc_recurse("pp_main", main_root, main_start, @comppadlist);
  1519. # Do save_unused_subs before saving inc_hv
  1520. save_unused_subs();
  1521. cc_recurse();
  1522. my $inc_hv = svref_2object(\%INC)->save;
  1523. my $inc_av = svref_2object(\@INC)->save;
  1524. my $amagic_generate= amagic_generation;
  1525. return if $errors;
  1526. if (!defined($module)) {
  1527. $init->add(sprintf("PL_main_root = s\\_%x;", ${main_root()}),
  1528. "PL_main_start = $start;",
  1529. "PL_curpad = AvARRAY($curpad_sym);",
  1530. "PL_initav = (AV *) $init_av;",
  1531. "GvHV(PL_incgv) = $inc_hv;",
  1532. "GvAV(PL_incgv) = $inc_av;",
  1533. "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
  1534. "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));",
  1535. "PL_amagic_generation= $amagic_generate;",
  1536. );
  1537. }
  1538. seek(STDOUT,0,0); #prevent print statements from BEGIN{} into the output
  1539. output_boilerplate();
  1540. print "\n";
  1541. output_all("perl_init");
  1542. output_runtime();
  1543. print "\n";
  1544. output_main();
  1545. if (defined($module)) {
  1546. my $cmodule = $module;
  1547. $cmodule =~ s/::/__/g;
  1548. print <<"EOT";
  1549. #include "XSUB.h"
  1550. XS(boot_$cmodule)
  1551. {
  1552. dXSARGS;
  1553. perl_init();
  1554. ENTER;
  1555. SAVETMPS;
  1556. SAVEVPTR(PL_curpad);
  1557. SAVEVPTR(PL_op);
  1558. PL_curpad = AvARRAY($curpad_sym);
  1559. PL_op = $start;
  1560. pp_main(aTHX);
  1561. FREETMPS;
  1562. LEAVE;
  1563. ST(0) = &PL_sv_yes;
  1564. XSRETURN(1);
  1565. }
  1566. EOT
  1567. }
  1568. if ($debug_timings) {
  1569. warn sprintf("Done at %s\n", timing_info);
  1570. }
  1571. }
  1572. sub compile {
  1573. my @options = @_;
  1574. my ($option, $opt, $arg);
  1575. OPTION:
  1576. while ($option = shift @options) {
  1577. if ($option =~ /^-(.)(.*)/) {
  1578. $opt = $1;
  1579. $arg = $2;
  1580. } else {
  1581. unshift @options, $option;
  1582. last OPTION;
  1583. }
  1584. if ($opt eq "-" && $arg eq "-") {
  1585. shift @options;
  1586. last OPTION;
  1587. } elsif ($opt eq "o") {
  1588. $arg ||= shift @options;
  1589. open(STDOUT, ">$arg") or return "open '>$arg': $!\n";
  1590. } elsif ($opt eq "n") {
  1591. $arg ||= shift @options;
  1592. $module_name = $arg;
  1593. } elsif ($opt eq "u") {
  1594. $arg ||= shift @options;
  1595. mark_unused($arg,undef);
  1596. } elsif ($opt eq "f") {
  1597. $arg ||= shift @options;
  1598. my $value = $arg !~ s/^no-//;
  1599. $arg =~ s/-/_/g;
  1600. my $ref = $optimise{$arg};
  1601. if (defined($ref)) {
  1602. $$ref = $value;
  1603. } else {
  1604. warn qq(ignoring unknown optimisation option "$arg"\n);
  1605. }
  1606. } elsif ($opt eq "O") {
  1607. $arg = 1 if $arg eq "";
  1608. my $ref;
  1609. foreach $ref (values %optimise) {
  1610. $$ref = 0;
  1611. }
  1612. if ($arg >= 2) {
  1613. $freetmps_each_loop = 1;
  1614. }
  1615. if ($arg >= 1) {
  1616. $freetmps_each_bblock = 1 unless $freetmps_each_loop;
  1617. }
  1618. } elsif ($opt eq "m") {
  1619. $arg ||= shift @options;
  1620. $module = $arg;
  1621. mark_unused($arg,undef);
  1622. } elsif ($opt eq "p") {
  1623. $arg ||= shift @options;
  1624. $patchlevel = $arg;
  1625. } elsif ($opt eq "D") {
  1626. $arg ||= shift @options;
  1627. foreach $arg (split(//, $arg)) {
  1628. if ($arg eq "o") {
  1629. B->debug(1);
  1630. } elsif ($arg eq "O") {
  1631. $debug_op = 1;
  1632. } elsif ($arg eq "s") {
  1633. $debug_stack = 1;
  1634. } elsif ($arg eq "c") {
  1635. $debug_cxstack = 1;
  1636. } elsif ($arg eq "p") {
  1637. $debug_pad = 1;
  1638. } elsif ($arg eq "r") {
  1639. $debug_runtime = 1;
  1640. } elsif ($arg eq "S") {
  1641. $debug_shadow = 1;
  1642. } elsif ($arg eq "q") {
  1643. $debug_queue = 1;
  1644. } elsif ($arg eq "l") {
  1645. $debug_lineno = 1;
  1646. } elsif ($arg eq "t") {
  1647. $debug_timings = 1;
  1648. }
  1649. }
  1650. }
  1651. }
  1652. init_sections();
  1653. $init = B::Section->get("init");
  1654. $decl = B::Section->get("decl");
  1655. if (@options) {
  1656. return sub {
  1657. my ($objname, $ppname);
  1658. foreach $objname (@options) {
  1659. $objname = "main::$objname" unless $objname =~ /::/;
  1660. ($ppname = $objname) =~ s/^.*?:://;
  1661. eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
  1662. die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
  1663. return if $errors;
  1664. }
  1665. output_boilerplate();
  1666. print "\n";
  1667. output_all($module_name || "init_module");
  1668. output_runtime();
  1669. }
  1670. } else {
  1671. return sub { cc_main() };
  1672. }
  1673. }
  1674. 1;
  1675. __END__
  1676. =head1 NAME
  1677. B::CC - Perl compiler's optimized C translation backend
  1678. =head1 SYNOPSIS
  1679. perl -MO=CC[,OPTIONS] foo.pl
  1680. =head1 DESCRIPTION
  1681. This compiler backend takes Perl source and generates C source code
  1682. corresponding to the flow of your program. In other words, this
  1683. backend is somewhat a "real" compiler in the sense that many people
  1684. think about compilers. Note however that, currently, it is a very
  1685. poor compiler in that although it generates (mostly, or at least
  1686. sometimes) correct code, it performs relatively few optimisations.
  1687. This will change as the compiler develops. The result is that
  1688. running an executable compiled with this backend may start up more
  1689. quickly than running the original Perl program (a feature shared
  1690. by the B<C> compiler backend--see F<B::C>) and may also execute
  1691. slightly faster. This is by no means a good optimising compiler--yet.
  1692. =head1 OPTIONS
  1693. If there are any non-option arguments, they are taken to be
  1694. names of objects to be saved (probably doesn't work properly yet).
  1695. Without extra arguments, it saves the main program.
  1696. =over 4
  1697. =item B<-ofilename>
  1698. Output to filename instead of STDOUT
  1699. =item B<-v>
  1700. Verbose compilation (currently gives a few compilation statistics).
  1701. =item B<-->
  1702. Force end of options
  1703. =item B<-uPackname>
  1704. Force apparently unused subs from package Packname to be compiled.
  1705. This allows programs to use eval "foo()" even when sub foo is never
  1706. seen to be used at compile time. The down side is that any subs which
  1707. really are never used also have code generated. This option is
  1708. necessary, for example, if you have a signal handler foo which you
  1709. initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just
  1710. to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u>
  1711. options. The compiler tries to figure out which packages may possibly
  1712. have subs in which need compiling but the current version doesn't do
  1713. it very well. In particular, it is confused by nested packages (i.e.
  1714. of the form C<A::B>) where package C<A> does not contain any subs.
  1715. =item B<-mModulename>
  1716. Instead of generating source for a runnable executable, generate
  1717. source for an XSUB module. The boot_Modulename function (which
  1718. DynaLoader can look for) does the appropriate initialisation and runs
  1719. the main part of the Perl source that is being compiled.
  1720. =item B<-D>
  1721. Debug options (concatenated or separate flags like C<perl -D>).
  1722. =item B<-Dr>
  1723. Writes debugging output to STDERR just as it's about to write to the
  1724. program's runtime (otherwise writes debugging info as comments in
  1725. its C output).
  1726. =item B<-DO>
  1727. Outputs each OP as it's compiled
  1728. =item B<-Ds>
  1729. Outputs the contents of the shadow stack at each OP
  1730. =item B<-Dp>
  1731. Outputs the contents of the shadow pad of lexicals as it's loaded for
  1732. each sub or the main program.
  1733. =item B<-Dq>
  1734. Outputs the name of each fake PP function in the queue as it's about
  1735. to process it.
  1736. =item B<-Dl>
  1737. Output the filename and line number of each original line of Perl
  1738. code as it's processed (C<pp_nextstate>).
  1739. =item B<-Dt>
  1740. Outputs timing information of compilation stages.
  1741. =item B<-f>
  1742. Force optimisations on or off one at a time.
  1743. =item B<-ffreetmps-each-bblock>
  1744. Delays FREETMPS from the end of each statement to the end of the each
  1745. basic block.
  1746. =item B<-ffreetmps-each-loop>
  1747. Delays FREETMPS from the end of each statement to the end of the group
  1748. of basic blocks forming a loop. At most one of the freetmps-each-*
  1749. options can be used.
  1750. =item B<-fomit-taint>
  1751. Omits generating code for handling perl's tainting mechanism.
  1752. =item B<-On>
  1753. Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>.
  1754. Currently, B<-O1> sets B<-ffreetmps-each-bblock> and B<-O2>
  1755. sets B<-ffreetmps-each-loop>.
  1756. =back
  1757. =head1 EXAMPLES
  1758. perl -MO=CC,-O2,-ofoo.c foo.pl
  1759. perl cc_harness -o foo foo.c
  1760. Note that C<cc_harness> lives in the C<B> subdirectory of your perl
  1761. library directory. The utility called C<perlcc> may also be used to
  1762. help make use of this compiler.
  1763. perl -MO=CC,-mFoo,-oFoo.c Foo.pm
  1764. perl cc_harness -shared -c -o Foo.so Foo.c
  1765. =head1 BUGS
  1766. Plenty. Current status: experimental.
  1767. =head1 DIFFERENCES
  1768. These aren't really bugs but they are constructs which are heavily
  1769. tied to perl's compile-and-go implementation and with which this
  1770. compiler backend cannot cope.
  1771. =head2 Loops
  1772. Standard perl calculates the target of "next", "last", and "redo"
  1773. at run-time. The compiler calculates the targets at compile-time.
  1774. For example, the program
  1775. sub skip_on_odd { next NUMBER if $_[0] % 2 }
  1776. NUMBER: for ($i = 0; $i < 5; $i++) {
  1777. skip_on_odd($i);
  1778. print $i;
  1779. }
  1780. produces the output
  1781. 024
  1782. with standard perl but gives a compile-time error with the compiler.
  1783. =head2 Context of ".."
  1784. The context (scalar or array) of the ".." operator determines whether
  1785. it behaves as a range or a flip/flop. Standard perl delays until
  1786. runtime the decision of which context it is in but the compiler needs
  1787. to know the context at compile-time. For example,
  1788. @a = (4,6,1,0,0,1);
  1789. sub range { (shift @a)..(shift @a) }
  1790. print range();
  1791. while (@a) { print scalar(range()) }
  1792. generates the output
  1793. 456123E0
  1794. with standard Perl but gives a compile-time error with compiled Perl.
  1795. =head2 Arithmetic
  1796. Compiled Perl programs use native C arithemtic much more frequently
  1797. than standard perl. Operations on large numbers or on boundary
  1798. cases may produce different behaviour.
  1799. =head2 Deprecated features
  1800. Features of standard perl such as C<$[> which have been deprecated
  1801. in standard perl since Perl5 was released have not been implemented
  1802. in the compiler.
  1803. =head1 AUTHOR
  1804. Malcolm Beattie, C<[email protected]>
  1805. =cut