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.

1657 lines
50 KiB

  1. # C.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::C::Section;
  9. use B ();
  10. use base B::Section;
  11. sub new
  12. {
  13. my $class = shift;
  14. my $o = $class->SUPER::new(@_);
  15. push(@$o,[]);
  16. return $o;
  17. }
  18. sub add
  19. {
  20. my $section = shift;
  21. push(@{$section->[-1]},@_);
  22. }
  23. sub index
  24. {
  25. my $section = shift;
  26. return scalar(@{$section->[-1]})-1;
  27. }
  28. sub output
  29. {
  30. my ($section, $fh, $format) = @_;
  31. my $sym = $section->symtable || {};
  32. my $default = $section->default;
  33. foreach (@{$section->[-1]})
  34. {
  35. s{(s\\_[0-9a-f]+)}{ exists($sym->{$1}) ? $sym->{$1} : $default; }ge;
  36. printf $fh $format, $_;
  37. }
  38. }
  39. package B::C;
  40. use Exporter ();
  41. @ISA = qw(Exporter);
  42. @EXPORT_OK = qw(output_all output_boilerplate output_main mark_unused
  43. init_sections set_callback save_unused_subs objsym save_context);
  44. use B qw(minus_c sv_undef walkoptree walksymtable main_root main_start peekop
  45. class cstring cchar svref_2object compile_stats comppadlist hash
  46. threadsv_names main_cv init_av opnumber amagic_generation
  47. AVf_REAL HEf_SVKEY);
  48. use B::Asmdata qw(@specialsv_name);
  49. use FileHandle;
  50. use Carp;
  51. use strict;
  52. use Config;
  53. my $hv_index = 0;
  54. my $gv_index = 0;
  55. my $re_index = 0;
  56. my $pv_index = 0;
  57. my $anonsub_index = 0;
  58. my $initsub_index = 0;
  59. my %symtable;
  60. my %xsub;
  61. my $warn_undefined_syms;
  62. my $verbose;
  63. my %unused_sub_packages;
  64. my $nullop_count;
  65. my $pv_copy_on_grow = 0;
  66. my ($debug_cops, $debug_av, $debug_cv, $debug_mg);
  67. my $max_string_len;
  68. my @threadsv_names;
  69. BEGIN {
  70. @threadsv_names = threadsv_names();
  71. }
  72. # Code sections
  73. my ($init, $decl, $symsect, $binopsect, $condopsect, $copsect,
  74. $padopsect, $listopsect, $logopsect, $loopsect, $opsect, $pmopsect,
  75. $pvopsect, $svopsect, $unopsect, $svsect, $xpvsect, $xpvavsect,
  76. $xpvhvsect, $xpvcvsect, $xpvivsect, $xpvnvsect, $xpvmgsect, $xpvlvsect,
  77. $xrvsect, $xpvbmsect, $xpviosect );
  78. sub walk_and_save_optree;
  79. my $saveoptree_callback = \&walk_and_save_optree;
  80. sub set_callback { $saveoptree_callback = shift }
  81. sub saveoptree { &$saveoptree_callback(@_) }
  82. sub walk_and_save_optree {
  83. my ($name, $root, $start) = @_;
  84. walkoptree($root, "save");
  85. return objsym($start);
  86. }
  87. # Current workaround/fix for op_free() trying to free statically
  88. # defined OPs is to set op_seq = -1 and check for that in op_free().
  89. # Instead of hardwiring -1 in place of $op->seq, we use $op_seq
  90. # so that it can be changed back easily if necessary. In fact, to
  91. # stop compilers from moaning about a U16 being initialised with an
  92. # uncast -1 (the printf format is %d so we can't tweak it), we have
  93. # to "know" that op_seq is a U16 and use 65535. Ugh.
  94. my $op_seq = 65535;
  95. # Look this up here so we can do just a number compare
  96. # rather than looking up the name of every BASEOP in B::OP
  97. my $OP_THREADSV = opnumber('threadsv');
  98. sub savesym {
  99. my ($obj, $value) = @_;
  100. my $sym = sprintf("s\\_%x", $$obj);
  101. $symtable{$sym} = $value;
  102. }
  103. sub objsym {
  104. my $obj = shift;
  105. return $symtable{sprintf("s\\_%x", $$obj)};
  106. }
  107. sub getsym {
  108. my $sym = shift;
  109. my $value;
  110. return 0 if $sym eq "sym_0"; # special case
  111. $value = $symtable{$sym};
  112. if (defined($value)) {
  113. return $value;
  114. } else {
  115. warn "warning: undefined symbol $sym\n" if $warn_undefined_syms;
  116. return "UNUSED";
  117. }
  118. }
  119. sub savepv {
  120. my $pv = shift;
  121. $pv = '' unless defined $pv; # Is this sane ?
  122. my $pvsym = 0;
  123. my $pvmax = 0;
  124. if ($pv_copy_on_grow) {
  125. my $cstring = cstring($pv);
  126. if ($cstring ne "0") { # sic
  127. $pvsym = sprintf("pv%d", $pv_index++);
  128. $decl->add(sprintf("static char %s[] = %s;", $pvsym, $cstring));
  129. }
  130. } else {
  131. $pvmax = length($pv) + 1;
  132. }
  133. return ($pvsym, $pvmax);
  134. }
  135. sub B::OP::save {
  136. my ($op, $level) = @_;
  137. my $sym = objsym($op);
  138. return $sym if defined $sym;
  139. my $type = $op->type;
  140. $nullop_count++ unless $type;
  141. if ($type == $OP_THREADSV) {
  142. # saves looking up ppaddr but it's a bit naughty to hard code this
  143. $init->add(sprintf("(void)find_threadsv(%s);",
  144. cstring($threadsv_names[$op->targ])));
  145. }
  146. $opsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x",
  147. ${$op->next}, ${$op->sibling}, $op->targ,
  148. $type, $op_seq, $op->flags, $op->private));
  149. my $ix = $opsect->index;
  150. $init->add(sprintf("op_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  151. savesym($op, "&op_list[$ix]");
  152. }
  153. sub B::FAKEOP::new {
  154. my ($class, %objdata) = @_;
  155. bless \%objdata, $class;
  156. }
  157. sub B::FAKEOP::save {
  158. my ($op, $level) = @_;
  159. $opsect->add(sprintf("%s, %s, NULL, %u, %u, %u, 0x%x, 0x%x",
  160. $op->next, $op->sibling, $op->targ,
  161. $op->type, $op_seq, $op->flags, $op->private));
  162. my $ix = $opsect->index;
  163. $init->add(sprintf("op_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  164. return "&op_list[$ix]";
  165. }
  166. sub B::FAKEOP::next { $_[0]->{"next"} || 0 }
  167. sub B::FAKEOP::type { $_[0]->{type} || 0}
  168. sub B::FAKEOP::sibling { $_[0]->{sibling} || 0 }
  169. sub B::FAKEOP::ppaddr { $_[0]->{ppaddr} || 0 }
  170. sub B::FAKEOP::targ { $_[0]->{targ} || 0 }
  171. sub B::FAKEOP::flags { $_[0]->{flags} || 0 }
  172. sub B::FAKEOP::private { $_[0]->{private} || 0 }
  173. sub B::UNOP::save {
  174. my ($op, $level) = @_;
  175. my $sym = objsym($op);
  176. return $sym if defined $sym;
  177. $unopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x",
  178. ${$op->next}, ${$op->sibling},
  179. $op->targ, $op->type, $op_seq, $op->flags,
  180. $op->private, ${$op->first}));
  181. my $ix = $unopsect->index;
  182. $init->add(sprintf("unop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  183. savesym($op, "(OP*)&unop_list[$ix]");
  184. }
  185. sub B::BINOP::save {
  186. my ($op, $level) = @_;
  187. my $sym = objsym($op);
  188. return $sym if defined $sym;
  189. $binopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
  190. ${$op->next}, ${$op->sibling},
  191. $op->targ, $op->type, $op_seq, $op->flags,
  192. $op->private, ${$op->first}, ${$op->last}));
  193. my $ix = $binopsect->index;
  194. $init->add(sprintf("binop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  195. savesym($op, "(OP*)&binop_list[$ix]");
  196. }
  197. sub B::LISTOP::save {
  198. my ($op, $level) = @_;
  199. my $sym = objsym($op);
  200. return $sym if defined $sym;
  201. $listopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
  202. ${$op->next}, ${$op->sibling},
  203. $op->targ, $op->type, $op_seq, $op->flags,
  204. $op->private, ${$op->first}, ${$op->last}));
  205. my $ix = $listopsect->index;
  206. $init->add(sprintf("listop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  207. savesym($op, "(OP*)&listop_list[$ix]");
  208. }
  209. sub B::LOGOP::save {
  210. my ($op, $level) = @_;
  211. my $sym = objsym($op);
  212. return $sym if defined $sym;
  213. $logopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x",
  214. ${$op->next}, ${$op->sibling},
  215. $op->targ, $op->type, $op_seq, $op->flags,
  216. $op->private, ${$op->first}, ${$op->other}));
  217. my $ix = $logopsect->index;
  218. $init->add(sprintf("logop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  219. savesym($op, "(OP*)&logop_list[$ix]");
  220. }
  221. sub B::LOOP::save {
  222. my ($op, $level) = @_;
  223. my $sym = objsym($op);
  224. return $sym if defined $sym;
  225. #warn sprintf("LOOP: redoop %s, nextop %s, lastop %s\n",
  226. # peekop($op->redoop), peekop($op->nextop),
  227. # peekop($op->lastop)); # debug
  228. $loopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, s\\_%x, s\\_%x, s\\_%x",
  229. ${$op->next}, ${$op->sibling},
  230. $op->targ, $op->type, $op_seq, $op->flags,
  231. $op->private, ${$op->first}, ${$op->last},
  232. ${$op->redoop}, ${$op->nextop},
  233. ${$op->lastop}));
  234. my $ix = $loopsect->index;
  235. $init->add(sprintf("loop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  236. savesym($op, "(OP*)&loop_list[$ix]");
  237. }
  238. sub B::PVOP::save {
  239. my ($op, $level) = @_;
  240. my $sym = objsym($op);
  241. return $sym if defined $sym;
  242. $pvopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, %s",
  243. ${$op->next}, ${$op->sibling},
  244. $op->targ, $op->type, $op_seq, $op->flags,
  245. $op->private, cstring($op->pv)));
  246. my $ix = $pvopsect->index;
  247. $init->add(sprintf("pvop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  248. savesym($op, "(OP*)&pvop_list[$ix]");
  249. }
  250. sub B::SVOP::save {
  251. my ($op, $level) = @_;
  252. my $sym = objsym($op);
  253. return $sym if defined $sym;
  254. my $svsym = $op->sv->save;
  255. $svopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, Nullsv",
  256. ${$op->next}, ${$op->sibling},
  257. $op->targ, $op->type, $op_seq, $op->flags,
  258. $op->private));
  259. my $ix = $svopsect->index;
  260. $init->add(sprintf("svop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  261. $init->add("svop_list[$ix].op_sv = (SV*)$svsym;");
  262. savesym($op, "(OP*)&svop_list[$ix]");
  263. }
  264. sub B::PADOP::save {
  265. my ($op, $level) = @_;
  266. my $sym = objsym($op);
  267. return $sym if defined $sym;
  268. $padopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, 0",
  269. ${$op->next}, ${$op->sibling},
  270. $op->targ, $op->type, $op_seq, $op->flags,
  271. $op->private));
  272. $init->add(sprintf("padop_list[%d].op_ppaddr = %s;", $padopsect->index, $op->ppaddr));
  273. my $ix = $padopsect->index;
  274. $init->add(sprintf("padop_list[$ix].op_padix = %ld;", $op->padix));
  275. savesym($op, "(OP*)&padop_list[$ix]");
  276. }
  277. sub B::COP::save {
  278. my ($op, $level) = @_;
  279. my $sym = objsym($op);
  280. return $sym if defined $sym;
  281. warn sprintf("COP: line %d file %s\n", $op->line, $op->file)
  282. if $debug_cops;
  283. $copsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, %s, NULL, NULL, %u, %d, %u",
  284. ${$op->next}, ${$op->sibling},
  285. $op->targ, $op->type, $op_seq, $op->flags,
  286. $op->private, cstring($op->label), $op->cop_seq,
  287. $op->arybase, $op->line));
  288. my $ix = $copsect->index;
  289. $init->add(sprintf("cop_list[$ix].op_ppaddr = %s;", $op->ppaddr));
  290. $init->add(sprintf("CopFILE_set(&cop_list[$ix], %s);", cstring($op->file)),
  291. sprintf("CopSTASHPV_set(&cop_list[$ix], %s);", cstring($op->stashpv)));
  292. savesym($op, "(OP*)&cop_list[$ix]");
  293. }
  294. sub B::PMOP::save {
  295. my ($op, $level) = @_;
  296. my $sym = objsym($op);
  297. return $sym if defined $sym;
  298. my $replroot = $op->pmreplroot;
  299. my $replstart = $op->pmreplstart;
  300. my $replrootfield = sprintf("s\\_%x", $$replroot);
  301. my $replstartfield = sprintf("s\\_%x", $$replstart);
  302. my $gvsym;
  303. my $ppaddr = $op->ppaddr;
  304. if ($$replroot) {
  305. # OP_PUSHRE (a mutated version of OP_MATCH for the regexp
  306. # argument to a split) stores a GV in op_pmreplroot instead
  307. # of a substitution syntax tree. We don't want to walk that...
  308. if ($op->name eq "pushre") {
  309. $gvsym = $replroot->save;
  310. # warn "PMOP::save saving a pp_pushre with GV $gvsym\n"; # debug
  311. $replrootfield = 0;
  312. } else {
  313. $replstartfield = saveoptree("*ignore*", $replroot, $replstart);
  314. }
  315. }
  316. # pmnext handling is broken in perl itself, I think. Bad op_pmnext
  317. # fields aren't noticed in perl's runtime (unless you try reset) but we
  318. # segfault when trying to dereference it to find op->op_pmnext->op_type
  319. $pmopsect->add(sprintf("s\\_%x, s\\_%x, NULL, %u, %u, %u, 0x%x, 0x%x, s\\_%x, s\\_%x, %s, %s, 0, 0, 0x%x, 0x%x",
  320. ${$op->next}, ${$op->sibling}, $op->targ,
  321. $op->type, $op_seq, $op->flags, $op->private,
  322. ${$op->first}, ${$op->last},
  323. $replrootfield, $replstartfield,
  324. $op->pmflags, $op->pmpermflags,));
  325. my $pm = sprintf("pmop_list[%d]", $pmopsect->index);
  326. $init->add(sprintf("$pm.op_ppaddr = %s;", $ppaddr));
  327. my $re = $op->precomp;
  328. if (defined($re)) {
  329. my $resym = sprintf("re%d", $re_index++);
  330. $decl->add(sprintf("static char *$resym = %s;", cstring($re)));
  331. $init->add(sprintf("$pm.op_pmregexp = pregcomp($resym, $resym + %u, &$pm);",
  332. length($re)));
  333. }
  334. if ($gvsym) {
  335. $init->add("$pm.op_pmreplroot = (OP*)$gvsym;");
  336. }
  337. savesym($op, "(OP*)&$pm");
  338. }
  339. sub B::SPECIAL::save {
  340. my ($sv) = @_;
  341. # special case: $$sv is not the address but an index into specialsv_list
  342. # warn "SPECIAL::save specialsv $$sv\n"; # debug
  343. my $sym = $specialsv_name[$$sv];
  344. if (!defined($sym)) {
  345. confess "unknown specialsv index $$sv passed to B::SPECIAL::save";
  346. }
  347. return $sym;
  348. }
  349. sub B::OBJECT::save {}
  350. sub B::NULL::save {
  351. my ($sv) = @_;
  352. my $sym = objsym($sv);
  353. return $sym if defined $sym;
  354. # warn "Saving SVt_NULL SV\n"; # debug
  355. # debug
  356. if ($$sv == 0) {
  357. warn "NULL::save for sv = 0 called from @{[(caller(1))[3]]}\n";
  358. return savesym($sv, "Nullsv /* XXX */");
  359. }
  360. $svsect->add(sprintf("0, %u, 0x%x", $sv->REFCNT , $sv->FLAGS));
  361. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  362. }
  363. sub B::IV::save {
  364. my ($sv) = @_;
  365. my $sym = objsym($sv);
  366. return $sym if defined $sym;
  367. $xpvivsect->add(sprintf("0, 0, 0, %d", $sv->IVX));
  368. $svsect->add(sprintf("&xpviv_list[%d], %lu, 0x%x",
  369. $xpvivsect->index, $sv->REFCNT , $sv->FLAGS));
  370. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  371. }
  372. sub B::NV::save {
  373. my ($sv) = @_;
  374. my $sym = objsym($sv);
  375. return $sym if defined $sym;
  376. my $val= $sv->NVX;
  377. $val .= '.00' if $val =~ /^-?\d+$/;
  378. $xpvnvsect->add(sprintf("0, 0, 0, %d, %s", $sv->IVX, $val));
  379. $svsect->add(sprintf("&xpvnv_list[%d], %lu, 0x%x",
  380. $xpvnvsect->index, $sv->REFCNT , $sv->FLAGS));
  381. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  382. }
  383. sub savepvn {
  384. my ($dest,$pv) = @_;
  385. my @res;
  386. if (defined $max_string_len && length($pv) > $max_string_len) {
  387. push @res, sprintf("New(0,%s,%u,char);", $dest, length($pv)+1);
  388. my $offset = 0;
  389. while (length $pv) {
  390. my $str = substr $pv, 0, $max_string_len, '';
  391. push @res, sprintf("Copy(%s,$dest+$offset,%u,char);",
  392. cstring($str), length($str));
  393. $offset += length $str;
  394. }
  395. push @res, sprintf("%s[%u] = '\\0';", $dest, $offset);
  396. }
  397. else {
  398. push @res, sprintf("%s = savepvn(%s, %u);", $dest,
  399. cstring($pv), length($pv));
  400. }
  401. return @res;
  402. }
  403. sub B::PVLV::save {
  404. my ($sv) = @_;
  405. my $sym = objsym($sv);
  406. return $sym if defined $sym;
  407. my $pv = $sv->PV;
  408. my $len = length($pv);
  409. my ($pvsym, $pvmax) = savepv($pv);
  410. my ($lvtarg, $lvtarg_sym);
  411. $xpvlvsect->add(sprintf("%s, %u, %u, %d, %g, 0, 0, %u, %u, 0, %s",
  412. $pvsym, $len, $pvmax, $sv->IVX, $sv->NVX,
  413. $sv->TARGOFF, $sv->TARGLEN, cchar($sv->TYPE)));
  414. $svsect->add(sprintf("&xpvlv_list[%d], %lu, 0x%x",
  415. $xpvlvsect->index, $sv->REFCNT , $sv->FLAGS));
  416. if (!$pv_copy_on_grow) {
  417. $init->add(savepvn(sprintf("xpvlv_list[%d].xpv_pv",
  418. $xpvlvsect->index), $pv));
  419. }
  420. $sv->save_magic;
  421. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  422. }
  423. sub B::PVIV::save {
  424. my ($sv) = @_;
  425. my $sym = objsym($sv);
  426. return $sym if defined $sym;
  427. my $pv = $sv->PV;
  428. my $len = length($pv);
  429. my ($pvsym, $pvmax) = savepv($pv);
  430. $xpvivsect->add(sprintf("%s, %u, %u, %d", $pvsym, $len, $pvmax, $sv->IVX));
  431. $svsect->add(sprintf("&xpviv_list[%d], %u, 0x%x",
  432. $xpvivsect->index, $sv->REFCNT , $sv->FLAGS));
  433. if (!$pv_copy_on_grow) {
  434. $init->add(savepvn(sprintf("xpviv_list[%d].xpv_pv",
  435. $xpvivsect->index), $pv));
  436. }
  437. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  438. }
  439. sub B::PVNV::save {
  440. my ($sv) = @_;
  441. my $sym = objsym($sv);
  442. return $sym if defined $sym;
  443. my $pv = $sv->PV;
  444. $pv = '' unless defined $pv;
  445. my $len = length($pv);
  446. my ($pvsym, $pvmax) = savepv($pv);
  447. my $val= $sv->NVX;
  448. $val .= '.00' if $val =~ /^-?\d+$/;
  449. $xpvnvsect->add(sprintf("%s, %u, %u, %d, %s",
  450. $pvsym, $len, $pvmax, $sv->IVX, $val));
  451. $svsect->add(sprintf("&xpvnv_list[%d], %lu, 0x%x",
  452. $xpvnvsect->index, $sv->REFCNT , $sv->FLAGS));
  453. if (!$pv_copy_on_grow) {
  454. $init->add(savepvn(sprintf("xpvnv_list[%d].xpv_pv",
  455. $xpvnvsect->index), $pv));
  456. }
  457. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  458. }
  459. sub B::BM::save {
  460. my ($sv) = @_;
  461. my $sym = objsym($sv);
  462. return $sym if defined $sym;
  463. my $pv = $sv->PV . "\0" . $sv->TABLE;
  464. my $len = length($pv);
  465. $xpvbmsect->add(sprintf("0, %u, %u, %d, %s, 0, 0, %d, %u, 0x%x",
  466. $len, $len + 258, $sv->IVX, $sv->NVX,
  467. $sv->USEFUL, $sv->PREVIOUS, $sv->RARE));
  468. $svsect->add(sprintf("&xpvbm_list[%d], %lu, 0x%x",
  469. $xpvbmsect->index, $sv->REFCNT , $sv->FLAGS));
  470. $sv->save_magic;
  471. $init->add(savepvn(sprintf("xpvbm_list[%d].xpv_pv",
  472. $xpvbmsect->index), $pv),
  473. sprintf("xpvbm_list[%d].xpv_cur = %u;",
  474. $xpvbmsect->index, $len - 257));
  475. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  476. }
  477. sub B::PV::save {
  478. my ($sv) = @_;
  479. my $sym = objsym($sv);
  480. return $sym if defined $sym;
  481. my $pv = $sv->PV;
  482. my $len = length($pv);
  483. my ($pvsym, $pvmax) = savepv($pv);
  484. $xpvsect->add(sprintf("%s, %u, %u", $pvsym, $len, $pvmax));
  485. $svsect->add(sprintf("&xpv_list[%d], %lu, 0x%x",
  486. $xpvsect->index, $sv->REFCNT , $sv->FLAGS));
  487. if (!$pv_copy_on_grow) {
  488. $init->add(savepvn(sprintf("xpv_list[%d].xpv_pv",
  489. $xpvsect->index), $pv));
  490. }
  491. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  492. }
  493. sub B::PVMG::save {
  494. my ($sv) = @_;
  495. my $sym = objsym($sv);
  496. return $sym if defined $sym;
  497. my $pv = $sv->PV;
  498. my $len = length($pv);
  499. my ($pvsym, $pvmax) = savepv($pv);
  500. $xpvmgsect->add(sprintf("%s, %u, %u, %d, %s, 0, 0",
  501. $pvsym, $len, $pvmax, $sv->IVX, $sv->NVX));
  502. $svsect->add(sprintf("&xpvmg_list[%d], %lu, 0x%x",
  503. $xpvmgsect->index, $sv->REFCNT , $sv->FLAGS));
  504. if (!$pv_copy_on_grow) {
  505. $init->add(savepvn(sprintf("xpvmg_list[%d].xpv_pv",
  506. $xpvmgsect->index), $pv));
  507. }
  508. $sym = savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  509. $sv->save_magic;
  510. return $sym;
  511. }
  512. sub B::PVMG::save_magic {
  513. my ($sv) = @_;
  514. #warn sprintf("saving magic for %s (0x%x)\n", class($sv), $$sv); # debug
  515. my $stash = $sv->SvSTASH;
  516. $stash->save;
  517. if ($$stash) {
  518. warn sprintf("xmg_stash = %s (0x%x)\n", $stash->NAME, $$stash)
  519. if $debug_mg;
  520. # XXX Hope stash is already going to be saved.
  521. $init->add(sprintf("SvSTASH(s\\_%x) = s\\_%x;", $$sv, $$stash));
  522. }
  523. my @mgchain = $sv->MAGIC;
  524. my ($mg, $type, $obj, $ptr,$len,$ptrsv);
  525. foreach $mg (@mgchain) {
  526. $type = $mg->TYPE;
  527. $obj = $mg->OBJ;
  528. $ptr = $mg->PTR;
  529. $len=$mg->LENGTH;
  530. if ($debug_mg) {
  531. warn sprintf("magic %s (0x%x), obj %s (0x%x), type %s, ptr %s\n",
  532. class($sv), $$sv, class($obj), $$obj,
  533. cchar($type), cstring($ptr));
  534. }
  535. $obj->save;
  536. if ($len == HEf_SVKEY){
  537. #The pointer is an SV*
  538. $ptrsv=svref_2object($ptr)->save;
  539. $init->add(sprintf("sv_magic((SV*)s\\_%x, (SV*)s\\_%x, %s,(char *) %s, %d);",
  540. $$sv, $$obj, cchar($type),$ptrsv,$len));
  541. }else{
  542. $init->add(sprintf("sv_magic((SV*)s\\_%x, (SV*)s\\_%x, %s, %s, %d);",
  543. $$sv, $$obj, cchar($type),cstring($ptr),$len));
  544. }
  545. }
  546. }
  547. sub B::RV::save {
  548. my ($sv) = @_;
  549. my $sym = objsym($sv);
  550. return $sym if defined $sym;
  551. my $rv = $sv->RV->save;
  552. $rv =~ s/^\([AGHS]V\s*\*\)\s*(\&sv_list.*)$/$1/;
  553. $xrvsect->add($rv);
  554. $svsect->add(sprintf("&xrv_list[%d], %lu, 0x%x",
  555. $xrvsect->index, $sv->REFCNT , $sv->FLAGS));
  556. return savesym($sv, sprintf("&sv_list[%d]", $svsect->index));
  557. }
  558. sub try_autoload {
  559. my ($cvstashname, $cvname) = @_;
  560. warn sprintf("No definition for sub %s::%s\n", $cvstashname, $cvname);
  561. # Handle AutoLoader classes explicitly. Any more general AUTOLOAD
  562. # use should be handled by the class itself.
  563. no strict 'refs';
  564. my $isa = \@{"$cvstashname\::ISA"};
  565. if (grep($_ eq "AutoLoader", @$isa)) {
  566. warn "Forcing immediate load of sub derived from AutoLoader\n";
  567. # Tweaked version of AutoLoader::AUTOLOAD
  568. my $dir = $cvstashname;
  569. $dir =~ s(::)(/)g;
  570. eval { require "auto/$dir/$cvname.al" };
  571. if ($@) {
  572. warn qq(failed require "auto/$dir/$cvname.al": $@\n);
  573. return 0;
  574. } else {
  575. return 1;
  576. }
  577. }
  578. }
  579. sub Dummy_initxs{};
  580. sub B::CV::save {
  581. my ($cv) = @_;
  582. my $sym = objsym($cv);
  583. if (defined($sym)) {
  584. # warn sprintf("CV 0x%x already saved as $sym\n", $$cv); # debug
  585. return $sym;
  586. }
  587. # Reserve a place in svsect and xpvcvsect and record indices
  588. my $gv = $cv->GV;
  589. my ($cvname, $cvstashname);
  590. if ($$gv){
  591. $cvname = $gv->NAME;
  592. $cvstashname = $gv->STASH->NAME;
  593. }
  594. my $root = $cv->ROOT;
  595. my $cvxsub = $cv->XSUB;
  596. #INIT is removed from the symbol table, so this call must come
  597. # from PL_initav->save. Re-bootstrapping will push INIT back in
  598. # so nullop should be sent.
  599. if ($cvxsub && ($cvname ne "INIT")) {
  600. my $egv = $gv->EGV;
  601. my $stashname = $egv->STASH->NAME;
  602. if ($cvname eq "bootstrap")
  603. {
  604. my $file = $gv->FILE;
  605. $decl->add("/* bootstrap $file */");
  606. warn "Bootstrap $stashname $file\n";
  607. $xsub{$stashname}='Dynamic';
  608. # $xsub{$stashname}='Static' unless $xsub{$stashname};
  609. return qq/NULL/;
  610. }
  611. warn sprintf("stub for XSUB $cvstashname\:\:$cvname CV 0x%x\n", $$cv) if $debug_cv;
  612. return qq/(perl_get_cv("$stashname\:\:$cvname",TRUE))/;
  613. }
  614. if ($cvxsub && $cvname eq "INIT") {
  615. no strict 'refs';
  616. return svref_2object(\&Dummy_initxs)->save;
  617. }
  618. my $sv_ix = $svsect->index + 1;
  619. $svsect->add("svix$sv_ix");
  620. my $xpvcv_ix = $xpvcvsect->index + 1;
  621. $xpvcvsect->add("xpvcvix$xpvcv_ix");
  622. # Save symbol now so that GvCV() doesn't recurse back to us via CvGV()
  623. $sym = savesym($cv, "&sv_list[$sv_ix]");
  624. warn sprintf("saving $cvstashname\:\:$cvname CV 0x%x as $sym\n", $$cv) if $debug_cv;
  625. if (!$$root && !$cvxsub) {
  626. if (try_autoload($cvstashname, $cvname)) {
  627. # Recalculate root and xsub
  628. $root = $cv->ROOT;
  629. $cvxsub = $cv->XSUB;
  630. if ($$root || $cvxsub) {
  631. warn "Successful forced autoload\n";
  632. }
  633. }
  634. }
  635. my $startfield = 0;
  636. my $padlist = $cv->PADLIST;
  637. my $pv = $cv->PV;
  638. my $xsub = 0;
  639. my $xsubany = "Nullany";
  640. if ($$root) {
  641. warn sprintf("saving op tree for CV 0x%x, root = 0x%x\n",
  642. $$cv, $$root) if $debug_cv;
  643. my $ppname = "";
  644. if ($$gv) {
  645. my $stashname = $gv->STASH->NAME;
  646. my $gvname = $gv->NAME;
  647. if ($gvname ne "__ANON__") {
  648. $ppname = (${$gv->FORM} == $$cv) ? "pp_form_" : "pp_sub_";
  649. $ppname .= ($stashname eq "main") ?
  650. $gvname : "$stashname\::$gvname";
  651. $ppname =~ s/::/__/g;
  652. if ($gvname eq "INIT"){
  653. $ppname .= "_$initsub_index";
  654. $initsub_index++;
  655. }
  656. }
  657. }
  658. if (!$ppname) {
  659. $ppname = "pp_anonsub_$anonsub_index";
  660. $anonsub_index++;
  661. }
  662. $startfield = saveoptree($ppname, $root, $cv->START, $padlist->ARRAY);
  663. warn sprintf("done saving op tree for CV 0x%x, name %s, root 0x%x\n",
  664. $$cv, $ppname, $$root) if $debug_cv;
  665. if ($$padlist) {
  666. warn sprintf("saving PADLIST 0x%x for CV 0x%x\n",
  667. $$padlist, $$cv) if $debug_cv;
  668. $padlist->save;
  669. warn sprintf("done saving PADLIST 0x%x for CV 0x%x\n",
  670. $$padlist, $$cv) if $debug_cv;
  671. }
  672. }
  673. else {
  674. warn sprintf("No definition for sub %s::%s (unable to autoload)\n",
  675. $cvstashname, $cvname); # debug
  676. }
  677. $pv = '' unless defined $pv; # Avoid use of undef warnings
  678. $symsect->add(sprintf("xpvcvix%d\t%s, %u, 0, %d, %s, 0, Nullhv, Nullhv, %s, s\\_%x, $xsub, $xsubany, Nullgv, \"\", %d, s\\_%x, (CV*)s\\_%x, 0x%x",
  679. $xpvcv_ix, cstring($pv), length($pv), $cv->IVX,
  680. $cv->NVX, $startfield, ${$cv->ROOT}, $cv->DEPTH,
  681. $$padlist, ${$cv->OUTSIDE}, $cv->CvFLAGS));
  682. if (${$cv->OUTSIDE} == ${main_cv()}){
  683. $init->add(sprintf("CvOUTSIDE(s\\_%x)=PL_main_cv;",$$cv));
  684. $init->add(sprintf("SvREFCNT_inc(PL_main_cv);"));
  685. }
  686. if ($$gv) {
  687. $gv->save;
  688. $init->add(sprintf("CvGV(s\\_%x) = s\\_%x;",$$cv,$$gv));
  689. warn sprintf("done saving GV 0x%x for CV 0x%x\n",
  690. $$gv, $$cv) if $debug_cv;
  691. }
  692. $init->add(sprintf("CvFILE($sym) = %s;", cstring($cv->FILE)));
  693. my $stash = $cv->STASH;
  694. if ($$stash) {
  695. $stash->save;
  696. $init->add(sprintf("CvSTASH(s\\_%x) = s\\_%x;", $$cv, $$stash));
  697. warn sprintf("done saving STASH 0x%x for CV 0x%x\n",
  698. $$stash, $$cv) if $debug_cv;
  699. }
  700. $symsect->add(sprintf("svix%d\t(XPVCV*)&xpvcv_list[%u], %lu, 0x%x",
  701. $sv_ix, $xpvcv_ix, $cv->REFCNT +1 , $cv->FLAGS));
  702. return $sym;
  703. }
  704. sub B::GV::save {
  705. my ($gv) = @_;
  706. my $sym = objsym($gv);
  707. if (defined($sym)) {
  708. #warn sprintf("GV 0x%x already saved as $sym\n", $$gv); # debug
  709. return $sym;
  710. } else {
  711. my $ix = $gv_index++;
  712. $sym = savesym($gv, "gv_list[$ix]");
  713. #warn sprintf("Saving GV 0x%x as $sym\n", $$gv); # debug
  714. }
  715. my $is_empty = $gv->is_empty;
  716. my $gvname = $gv->NAME;
  717. my $name = cstring($gv->STASH->NAME . "::" . $gvname);
  718. #warn "GV name is $name\n"; # debug
  719. my $egvsym;
  720. unless ($is_empty) {
  721. my $egv = $gv->EGV;
  722. if ($$gv != $$egv) {
  723. #warn(sprintf("EGV name is %s, saving it now\n",
  724. # $egv->STASH->NAME . "::" . $egv->NAME)); # debug
  725. $egvsym = $egv->save;
  726. }
  727. }
  728. $init->add(qq[$sym = gv_fetchpv($name, TRUE, SVt_PV);],
  729. sprintf("SvFLAGS($sym) = 0x%x;", $gv->FLAGS),
  730. sprintf("GvFLAGS($sym) = 0x%x;", $gv->GvFLAGS));
  731. $init->add(sprintf("GvLINE($sym) = %u;", $gv->LINE)) unless $is_empty;
  732. # Shouldn't need to do save_magic since gv_fetchpv handles that
  733. #$gv->save_magic;
  734. my $refcnt = $gv->REFCNT + 1;
  735. $init->add(sprintf("SvREFCNT($sym) += %u;", $refcnt - 1)) if $refcnt > 1;
  736. return $sym if $is_empty;
  737. my $gvrefcnt = $gv->GvREFCNT;
  738. if ($gvrefcnt > 1) {
  739. $init->add(sprintf("GvREFCNT($sym) += %u;", $gvrefcnt - 1));
  740. }
  741. if (defined($egvsym)) {
  742. # Shared glob *foo = *bar
  743. $init->add("gp_free($sym);",
  744. "GvGP($sym) = GvGP($egvsym);");
  745. } elsif ($gvname !~ /^([^A-Za-z]|STDIN|STDOUT|STDERR|ARGV|SIG|ENV)$/) {
  746. # Don't save subfields of special GVs (*_, *1, *# and so on)
  747. # warn "GV::save saving subfields\n"; # debug
  748. my $gvsv = $gv->SV;
  749. if ($$gvsv) {
  750. $gvsv->save;
  751. $init->add(sprintf("GvSV($sym) = s\\_%x;", $$gvsv));
  752. # warn "GV::save \$$name\n"; # debug
  753. }
  754. my $gvav = $gv->AV;
  755. if ($$gvav) {
  756. $gvav->save;
  757. $init->add(sprintf("GvAV($sym) = s\\_%x;", $$gvav));
  758. # warn "GV::save \@$name\n"; # debug
  759. }
  760. my $gvhv = $gv->HV;
  761. if ($$gvhv) {
  762. $gvhv->save;
  763. $init->add(sprintf("GvHV($sym) = s\\_%x;", $$gvhv));
  764. # warn "GV::save \%$name\n"; # debug
  765. }
  766. my $gvcv = $gv->CV;
  767. if ($$gvcv) {
  768. my $origname=cstring($gvcv->GV->EGV->STASH->NAME .
  769. "::" . $gvcv->GV->EGV->NAME);
  770. if (0 && $gvcv->XSUB && $name ne $origname) { #XSUB alias
  771. # must save as a 'stub' so newXS() has a CV to populate
  772. $init->add("{ CV *cv;");
  773. $init->add("\tcv=perl_get_cv($origname,TRUE);");
  774. $init->add("\tGvCV($sym)=cv;");
  775. $init->add("\tSvREFCNT_inc((SV *)cv);");
  776. $init->add("}");
  777. } else {
  778. $init->add(sprintf("GvCV($sym) = (CV*)(%s);", $gvcv->save));
  779. # warn "GV::save &$name\n"; # debug
  780. }
  781. }
  782. $init->add(sprintf("GvFILE($sym) = %s;", cstring($gv->FILE)));
  783. # warn "GV::save GvFILE(*$name)\n"; # debug
  784. my $gvform = $gv->FORM;
  785. if ($$gvform) {
  786. $gvform->save;
  787. $init->add(sprintf("GvFORM($sym) = (CV*)s\\_%x;", $$gvform));
  788. # warn "GV::save GvFORM(*$name)\n"; # debug
  789. }
  790. my $gvio = $gv->IO;
  791. if ($$gvio) {
  792. $gvio->save;
  793. $init->add(sprintf("GvIOp($sym) = s\\_%x;", $$gvio));
  794. # warn "GV::save GvIO(*$name)\n"; # debug
  795. }
  796. }
  797. return $sym;
  798. }
  799. sub B::AV::save {
  800. my ($av) = @_;
  801. my $sym = objsym($av);
  802. return $sym if defined $sym;
  803. my $avflags = $av->AvFLAGS;
  804. $xpvavsect->add(sprintf("0, -1, -1, 0, 0.0, 0, Nullhv, 0, 0, 0x%x",
  805. $avflags));
  806. $svsect->add(sprintf("&xpvav_list[%d], %lu, 0x%x",
  807. $xpvavsect->index, $av->REFCNT , $av->FLAGS));
  808. my $sv_list_index = $svsect->index;
  809. my $fill = $av->FILL;
  810. $av->save_magic;
  811. warn sprintf("saving AV 0x%x FILL=$fill AvFLAGS=0x%x", $$av, $avflags)
  812. if $debug_av;
  813. # XXX AVf_REAL is wrong test: need to save comppadlist but not stack
  814. #if ($fill > -1 && ($avflags & AVf_REAL)) {
  815. if ($fill > -1) {
  816. my @array = $av->ARRAY;
  817. if ($debug_av) {
  818. my $el;
  819. my $i = 0;
  820. foreach $el (@array) {
  821. warn sprintf("AV 0x%x[%d] = %s 0x%x\n",
  822. $$av, $i++, class($el), $$el);
  823. }
  824. }
  825. my @names = map($_->save, @array);
  826. # XXX Better ways to write loop?
  827. # Perhaps svp[0] = ...; svp[1] = ...; svp[2] = ...;
  828. # Perhaps I32 i = 0; svp[i++] = ...; svp[i++] = ...; svp[i++] = ...;
  829. $init->add("{",
  830. "\tSV **svp;",
  831. "\tAV *av = (AV*)&sv_list[$sv_list_index];",
  832. "\tav_extend(av, $fill);",
  833. "\tsvp = AvARRAY(av);",
  834. map("\t*svp++ = (SV*)$_;", @names),
  835. "\tAvFILLp(av) = $fill;",
  836. "}");
  837. } else {
  838. my $max = $av->MAX;
  839. $init->add("av_extend((AV*)&sv_list[$sv_list_index], $max);")
  840. if $max > -1;
  841. }
  842. return savesym($av, "(AV*)&sv_list[$sv_list_index]");
  843. }
  844. sub B::HV::save {
  845. my ($hv) = @_;
  846. my $sym = objsym($hv);
  847. return $sym if defined $sym;
  848. my $name = $hv->NAME;
  849. if ($name) {
  850. # It's a stash
  851. # A perl bug means HvPMROOT isn't altered when a PMOP is freed. Usually
  852. # the only symptom is that sv_reset tries to reset the PMf_USED flag of
  853. # a trashed op but we look at the trashed op_type and segfault.
  854. #my $adpmroot = ${$hv->PMROOT};
  855. my $adpmroot = 0;
  856. $decl->add("static HV *hv$hv_index;");
  857. # XXX Beware of weird package names containing double-quotes, \n, ...?
  858. $init->add(qq[hv$hv_index = gv_stashpv("$name", TRUE);]);
  859. if ($adpmroot) {
  860. $init->add(sprintf("HvPMROOT(hv$hv_index) = (PMOP*)s\\_%x;",
  861. $adpmroot));
  862. }
  863. $sym = savesym($hv, "hv$hv_index");
  864. $hv_index++;
  865. return $sym;
  866. }
  867. # It's just an ordinary HV
  868. $xpvhvsect->add(sprintf("0, 0, %d, 0, 0.0, 0, Nullhv, %d, 0, 0, 0",
  869. $hv->MAX, $hv->RITER));
  870. $svsect->add(sprintf("&xpvhv_list[%d], %lu, 0x%x",
  871. $xpvhvsect->index, $hv->REFCNT , $hv->FLAGS));
  872. my $sv_list_index = $svsect->index;
  873. my @contents = $hv->ARRAY;
  874. if (@contents) {
  875. my $i;
  876. for ($i = 1; $i < @contents; $i += 2) {
  877. $contents[$i] = $contents[$i]->save;
  878. }
  879. $init->add("{", "\tHV *hv = (HV*)&sv_list[$sv_list_index];");
  880. while (@contents) {
  881. my ($key, $value) = splice(@contents, 0, 2);
  882. $init->add(sprintf("\thv_store(hv, %s, %u, %s, %s);",
  883. cstring($key),length($key),$value, hash($key)));
  884. # $init->add(sprintf("\thv_store(hv, %s, %u, %s, %s);",
  885. # cstring($key),length($key),$value, 0));
  886. }
  887. $init->add("}");
  888. }
  889. $hv->save_magic();
  890. return savesym($hv, "(HV*)&sv_list[$sv_list_index]");
  891. }
  892. sub B::IO::save {
  893. my ($io) = @_;
  894. my $sym = objsym($io);
  895. return $sym if defined $sym;
  896. my $pv = $io->PV;
  897. $pv = '' unless defined $pv;
  898. my $len = length($pv);
  899. $xpviosect->add(sprintf("0, %u, %u, %d, %s, 0, 0, 0, 0, 0, %d, %d, %d, %d, %s, Nullgv, %s, Nullgv, %s, Nullgv, %d, %s, 0x%x",
  900. $len, $len+1, $io->IVX, $io->NVX, $io->LINES,
  901. $io->PAGE, $io->PAGE_LEN, $io->LINES_LEFT,
  902. cstring($io->TOP_NAME), cstring($io->FMT_NAME),
  903. cstring($io->BOTTOM_NAME), $io->SUBPROCESS,
  904. cchar($io->IoTYPE), $io->IoFLAGS));
  905. $svsect->add(sprintf("&xpvio_list[%d], %lu, 0x%x",
  906. $xpviosect->index, $io->REFCNT , $io->FLAGS));
  907. $sym = savesym($io, sprintf("(IO*)&sv_list[%d]", $svsect->index));
  908. my ($field, $fsym);
  909. foreach $field (qw(TOP_GV FMT_GV BOTTOM_GV)) {
  910. $fsym = $io->$field();
  911. if ($$fsym) {
  912. $init->add(sprintf("Io$field($sym) = (GV*)s\\_%x;", $$fsym));
  913. $fsym->save;
  914. }
  915. }
  916. $io->save_magic;
  917. return $sym;
  918. }
  919. sub B::SV::save {
  920. my $sv = shift;
  921. # This is where we catch an honest-to-goodness Nullsv (which gets
  922. # blessed into B::SV explicitly) and any stray erroneous SVs.
  923. return 0 unless $$sv;
  924. confess sprintf("cannot save that type of SV: %s (0x%x)\n",
  925. class($sv), $$sv);
  926. }
  927. sub output_all {
  928. my $init_name = shift;
  929. my $section;
  930. my @sections = ($opsect, $unopsect, $binopsect, $logopsect, $condopsect,
  931. $listopsect, $pmopsect, $svopsect, $padopsect, $pvopsect,
  932. $loopsect, $copsect, $svsect, $xpvsect,
  933. $xpvavsect, $xpvhvsect, $xpvcvsect, $xpvivsect, $xpvnvsect,
  934. $xpvmgsect, $xpvlvsect, $xrvsect, $xpvbmsect, $xpviosect);
  935. $symsect->output(\*STDOUT, "#define %s\n");
  936. print "\n";
  937. output_declarations();
  938. foreach $section (@sections) {
  939. my $lines = $section->index + 1;
  940. if ($lines) {
  941. my $name = $section->name;
  942. my $typename = ($name eq "xpvcv") ? "XPVCV_or_similar" : uc($name);
  943. print "Static $typename ${name}_list[$lines];\n";
  944. }
  945. }
  946. $decl->output(\*STDOUT, "%s\n");
  947. print "\n";
  948. foreach $section (@sections) {
  949. my $lines = $section->index + 1;
  950. if ($lines) {
  951. my $name = $section->name;
  952. my $typename = ($name eq "xpvcv") ? "XPVCV_or_similar" : uc($name);
  953. printf "static %s %s_list[%u] = {\n", $typename, $name, $lines;
  954. $section->output(\*STDOUT, "\t{ %s },\n");
  955. print "};\n\n";
  956. }
  957. }
  958. print <<"EOT";
  959. static int $init_name()
  960. {
  961. dTARG;
  962. dSP;
  963. EOT
  964. $init->output(\*STDOUT, "\t%s\n");
  965. print "\treturn 0;\n}\n";
  966. if ($verbose) {
  967. warn compile_stats();
  968. warn "NULLOP count: $nullop_count\n";
  969. }
  970. }
  971. sub output_declarations {
  972. print <<'EOT';
  973. #ifdef BROKEN_STATIC_REDECL
  974. #define Static extern
  975. #else
  976. #define Static static
  977. #endif /* BROKEN_STATIC_REDECL */
  978. #ifdef BROKEN_UNION_INIT
  979. /*
  980. * Cribbed from cv.h with ANY (a union) replaced by void*.
  981. * Some pre-Standard compilers can't cope with initialising unions. Ho hum.
  982. */
  983. typedef struct {
  984. char * xpv_pv; /* pointer to malloced string */
  985. STRLEN xpv_cur; /* length of xp_pv as a C string */
  986. STRLEN xpv_len; /* allocated size */
  987. IV xof_off; /* integer value */
  988. NV xnv_nv; /* numeric value, if any */
  989. MAGIC* xmg_magic; /* magic for scalar array */
  990. HV* xmg_stash; /* class package */
  991. HV * xcv_stash;
  992. OP * xcv_start;
  993. OP * xcv_root;
  994. void (*xcv_xsub) (pTHXo_ CV*);
  995. ANY xcv_xsubany;
  996. GV * xcv_gv;
  997. char * xcv_file;
  998. long xcv_depth; /* >= 2 indicates recursive call */
  999. AV * xcv_padlist;
  1000. CV * xcv_outside;
  1001. #ifdef USE_THREADS
  1002. perl_mutex *xcv_mutexp;
  1003. struct perl_thread *xcv_owner; /* current owner thread */
  1004. #endif /* USE_THREADS */
  1005. cv_flags_t xcv_flags;
  1006. } XPVCV_or_similar;
  1007. #define ANYINIT(i) i
  1008. #else
  1009. #define XPVCV_or_similar XPVCV
  1010. #define ANYINIT(i) {i}
  1011. #endif /* BROKEN_UNION_INIT */
  1012. #define Nullany ANYINIT(0)
  1013. #define UNUSED 0
  1014. #define sym_0 0
  1015. EOT
  1016. print "static GV *gv_list[$gv_index];\n" if $gv_index;
  1017. print "\n";
  1018. }
  1019. sub output_boilerplate {
  1020. print <<'EOT';
  1021. #include "EXTERN.h"
  1022. #include "perl.h"
  1023. #include "XSUB.h"
  1024. /* Workaround for mapstart: the only op which needs a different ppaddr */
  1025. #undef Perl_pp_mapstart
  1026. #define Perl_pp_mapstart Perl_pp_grepstart
  1027. #define XS_DynaLoader_boot_DynaLoader boot_DynaLoader
  1028. EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
  1029. static void xs_init (pTHX);
  1030. static void dl_init (pTHX);
  1031. static PerlInterpreter *my_perl;
  1032. EOT
  1033. }
  1034. sub output_main {
  1035. print <<'EOT';
  1036. int
  1037. main(int argc, char **argv, char **env)
  1038. {
  1039. int exitstatus;
  1040. int i;
  1041. char **fakeargv;
  1042. PERL_SYS_INIT3(&argc,&argv,&env);
  1043. if (!PL_do_undump) {
  1044. my_perl = perl_alloc();
  1045. if (!my_perl)
  1046. exit(1);
  1047. perl_construct( my_perl );
  1048. PL_perl_destruct_level = 0;
  1049. }
  1050. #ifdef CSH
  1051. if (!PL_cshlen)
  1052. PL_cshlen = strlen(PL_cshname);
  1053. #endif
  1054. #ifdef ALLOW_PERL_OPTIONS
  1055. #define EXTRA_OPTIONS 2
  1056. #else
  1057. #define EXTRA_OPTIONS 3
  1058. #endif /* ALLOW_PERL_OPTIONS */
  1059. New(666, fakeargv, argc + EXTRA_OPTIONS + 1, char *);
  1060. fakeargv[0] = argv[0];
  1061. fakeargv[1] = "-e";
  1062. fakeargv[2] = "";
  1063. #ifndef ALLOW_PERL_OPTIONS
  1064. fakeargv[3] = "--";
  1065. #endif /* ALLOW_PERL_OPTIONS */
  1066. for (i = 1; i < argc; i++)
  1067. fakeargv[i + EXTRA_OPTIONS] = argv[i];
  1068. fakeargv[argc + EXTRA_OPTIONS] = 0;
  1069. exitstatus = perl_parse(my_perl, xs_init, argc + EXTRA_OPTIONS,
  1070. fakeargv, NULL);
  1071. if (exitstatus)
  1072. exit( exitstatus );
  1073. sv_setpv(GvSV(gv_fetchpv("0", TRUE, SVt_PV)), argv[0]);
  1074. PL_main_cv = PL_compcv;
  1075. PL_compcv = 0;
  1076. exitstatus = perl_init();
  1077. if (exitstatus)
  1078. exit( exitstatus );
  1079. dl_init(aTHX);
  1080. exitstatus = perl_run( my_perl );
  1081. perl_destruct( my_perl );
  1082. perl_free( my_perl );
  1083. PERL_SYS_TERM();
  1084. exit( exitstatus );
  1085. }
  1086. /* yanked from perl.c */
  1087. static void
  1088. xs_init(pTHX)
  1089. {
  1090. char *file = __FILE__;
  1091. dTARG;
  1092. dSP;
  1093. EOT
  1094. print "\n#ifdef USE_DYNAMIC_LOADING";
  1095. print qq/\n\tnewXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);/;
  1096. print "\n#endif\n" ;
  1097. # delete $xsub{'DynaLoader'};
  1098. delete $xsub{'UNIVERSAL'};
  1099. print("/* bootstrapping code*/\n\tSAVETMPS;\n");
  1100. print("\ttarg=sv_newmortal();\n");
  1101. print "#ifdef DYNALOADER_BOOTSTRAP\n";
  1102. print "\tPUSHMARK(sp);\n";
  1103. print qq/\tXPUSHp("DynaLoader",strlen("DynaLoader"));\n/;
  1104. print qq/\tPUTBACK;\n/;
  1105. print "\tboot_DynaLoader(aTHX_ NULL);\n";
  1106. print qq/\tSPAGAIN;\n/;
  1107. print "#endif\n";
  1108. foreach my $stashname (keys %xsub){
  1109. if ($xsub{$stashname} ne 'Dynamic') {
  1110. my $stashxsub=$stashname;
  1111. $stashxsub =~ s/::/__/g;
  1112. print "\tPUSHMARK(sp);\n";
  1113. print qq/\tXPUSHp("$stashname",strlen("$stashname"));\n/;
  1114. print qq/\tPUTBACK;\n/;
  1115. print "\tboot_$stashxsub(aTHX_ NULL);\n";
  1116. print qq/\tSPAGAIN;\n/;
  1117. }
  1118. }
  1119. print("\tFREETMPS;\n/* end bootstrapping code */\n");
  1120. print "}\n";
  1121. print <<'EOT';
  1122. static void
  1123. dl_init(pTHX)
  1124. {
  1125. char *file = __FILE__;
  1126. dTARG;
  1127. dSP;
  1128. EOT
  1129. print("/* Dynamicboot strapping code*/\n\tSAVETMPS;\n");
  1130. print("\ttarg=sv_newmortal();\n");
  1131. foreach my $stashname (@DynaLoader::dl_modules) {
  1132. warn "Loaded $stashname\n";
  1133. if (exists($xsub{$stashname}) && $xsub{$stashname} eq 'Dynamic') {
  1134. my $stashxsub=$stashname;
  1135. $stashxsub =~ s/::/__/g;
  1136. print "\tPUSHMARK(sp);\n";
  1137. print qq/\tXPUSHp("$stashname",/,length($stashname),qq/);\n/;
  1138. print qq/\tPUTBACK;\n/;
  1139. print "#ifdef DYNALOADER_BOOTSTRAP\n";
  1140. warn "bootstrapping $stashname added to xs_init\n";
  1141. print qq/\tperl_call_method("bootstrap",G_DISCARD);\n/;
  1142. print "\n#else\n";
  1143. print "\tboot_$stashxsub(aTHX_ NULL);\n";
  1144. print "#endif\n";
  1145. print qq/\tSPAGAIN;\n/;
  1146. }
  1147. }
  1148. print("\tFREETMPS;\n/* end Dynamic bootstrapping code */\n");
  1149. print "}\n";
  1150. }
  1151. sub dump_symtable {
  1152. # For debugging
  1153. my ($sym, $val);
  1154. warn "----Symbol table:\n";
  1155. while (($sym, $val) = each %symtable) {
  1156. warn "$sym => $val\n";
  1157. }
  1158. warn "---End of symbol table\n";
  1159. }
  1160. sub save_object {
  1161. my $sv;
  1162. foreach $sv (@_) {
  1163. svref_2object($sv)->save;
  1164. }
  1165. }
  1166. sub Dummy_BootStrap { }
  1167. sub B::GV::savecv
  1168. {
  1169. my $gv = shift;
  1170. my $package=$gv->STASH->NAME;
  1171. my $name = $gv->NAME;
  1172. my $cv = $gv->CV;
  1173. my $sv = $gv->SV;
  1174. my $av = $gv->AV;
  1175. my $hv = $gv->HV;
  1176. # We may be looking at this package just because it is a branch in the
  1177. # symbol table which is on the path to a package which we need to save
  1178. # e.g. this is 'Getopt' and we need to save 'Getopt::Long'
  1179. #
  1180. return unless ($unused_sub_packages{$package});
  1181. return unless ($$cv || $$av || $$sv || $$hv);
  1182. $gv->save;
  1183. }
  1184. sub mark_package
  1185. {
  1186. my $package = shift;
  1187. unless ($unused_sub_packages{$package})
  1188. {
  1189. no strict 'refs';
  1190. $unused_sub_packages{$package} = 1;
  1191. if (defined @{$package.'::ISA'})
  1192. {
  1193. foreach my $isa (@{$package.'::ISA'})
  1194. {
  1195. if ($isa eq 'DynaLoader')
  1196. {
  1197. unless (defined(&{$package.'::bootstrap'}))
  1198. {
  1199. warn "Forcing bootstrap of $package\n";
  1200. eval { $package->bootstrap };
  1201. }
  1202. }
  1203. # else
  1204. {
  1205. unless ($unused_sub_packages{$isa})
  1206. {
  1207. warn "$isa saved (it is in $package\'s \@ISA)\n";
  1208. mark_package($isa);
  1209. }
  1210. }
  1211. }
  1212. }
  1213. }
  1214. return 1;
  1215. }
  1216. sub should_save
  1217. {
  1218. no strict qw(vars refs);
  1219. my $package = shift;
  1220. $package =~ s/::$//;
  1221. return $unused_sub_packages{$package} = 0 if ($package =~ /::::/); # skip ::::ISA::CACHE etc.
  1222. # warn "Considering $package\n";#debug
  1223. foreach my $u (grep($unused_sub_packages{$_},keys %unused_sub_packages))
  1224. {
  1225. # If this package is a prefix to something we are saving, traverse it
  1226. # but do not mark it for saving if it is not already
  1227. # e.g. to get to Getopt::Long we need to traverse Getopt but need
  1228. # not save Getopt
  1229. return 1 if ($u =~ /^$package\:\:/);
  1230. }
  1231. if (exists $unused_sub_packages{$package})
  1232. {
  1233. # warn "Cached $package is ".$unused_sub_packages{$package}."\n";
  1234. delete_unsaved_hashINC($package) unless $unused_sub_packages{$package} ;
  1235. return $unused_sub_packages{$package};
  1236. }
  1237. # Omit the packages which we use (and which cause grief
  1238. # because of fancy "goto &$AUTOLOAD" stuff).
  1239. # XXX Surely there must be a nicer way to do this.
  1240. if ($package eq "FileHandle" || $package eq "Config" ||
  1241. $package eq "SelectSaver" || $package =~/^(B|IO)::/)
  1242. {
  1243. delete_unsaved_hashINC($package);
  1244. return $unused_sub_packages{$package} = 0;
  1245. }
  1246. # Now see if current package looks like an OO class this is probably too strong.
  1247. foreach my $m (qw(new DESTROY TIESCALAR TIEARRAY TIEHASH TIEHANDLE))
  1248. {
  1249. if (UNIVERSAL::can($package, $m))
  1250. {
  1251. warn "$package has method $m: saving package\n";#debug
  1252. return mark_package($package);
  1253. }
  1254. }
  1255. delete_unsaved_hashINC($package);
  1256. return $unused_sub_packages{$package} = 0;
  1257. }
  1258. sub delete_unsaved_hashINC{
  1259. my $packname=shift;
  1260. $packname =~ s/\:\:/\//g;
  1261. $packname .= '.pm';
  1262. # warn "deleting $packname" if $INC{$packname} ;# debug
  1263. delete $INC{$packname};
  1264. }
  1265. sub walkpackages
  1266. {
  1267. my ($symref, $recurse, $prefix) = @_;
  1268. my $sym;
  1269. my $ref;
  1270. no strict 'vars';
  1271. local(*glob);
  1272. $prefix = '' unless defined $prefix;
  1273. while (($sym, $ref) = each %$symref)
  1274. {
  1275. *glob = $ref;
  1276. if ($sym =~ /::$/)
  1277. {
  1278. $sym = $prefix . $sym;
  1279. if ($sym ne "main::" && $sym ne "<none>::" && &$recurse($sym))
  1280. {
  1281. walkpackages(\%glob, $recurse, $sym);
  1282. }
  1283. }
  1284. }
  1285. }
  1286. sub save_unused_subs
  1287. {
  1288. no strict qw(refs);
  1289. &descend_marked_unused;
  1290. warn "Prescan\n";
  1291. walkpackages(\%{"main::"}, sub { should_save($_[0]); return 1 });
  1292. warn "Saving methods\n";
  1293. walksymtable(\%{"main::"}, "savecv", \&should_save);
  1294. }
  1295. sub save_context
  1296. {
  1297. my $curpad_nam = (comppadlist->ARRAY)[0]->save;
  1298. my $curpad_sym = (comppadlist->ARRAY)[1]->save;
  1299. my $inc_hv = svref_2object(\%INC)->save;
  1300. my $inc_av = svref_2object(\@INC)->save;
  1301. my $amagic_generate= amagic_generation;
  1302. $init->add( "PL_curpad = AvARRAY($curpad_sym);",
  1303. "GvHV(PL_incgv) = $inc_hv;",
  1304. "GvAV(PL_incgv) = $inc_av;",
  1305. "av_store(CvPADLIST(PL_main_cv),0,SvREFCNT_inc($curpad_nam));",
  1306. "av_store(CvPADLIST(PL_main_cv),1,SvREFCNT_inc($curpad_sym));",
  1307. "PL_amagic_generation= $amagic_generate;" );
  1308. }
  1309. sub descend_marked_unused {
  1310. foreach my $pack (keys %unused_sub_packages)
  1311. {
  1312. mark_package($pack);
  1313. }
  1314. }
  1315. sub save_main {
  1316. warn "Starting compile\n";
  1317. warn "Walking tree\n";
  1318. seek(STDOUT,0,0); #exclude print statements in BEGIN{} into output
  1319. walkoptree(main_root, "save");
  1320. warn "done main optree, walking symtable for extras\n" if $debug_cv;
  1321. save_unused_subs();
  1322. my $init_av = init_av->save;
  1323. $init->add(sprintf("PL_main_root = s\\_%x;", ${main_root()}),
  1324. sprintf("PL_main_start = s\\_%x;", ${main_start()}),
  1325. "PL_initav = (AV *) $init_av;");
  1326. save_context();
  1327. warn "Writing output\n";
  1328. output_boilerplate();
  1329. print "\n";
  1330. output_all("perl_init");
  1331. print "\n";
  1332. output_main();
  1333. }
  1334. sub init_sections {
  1335. my @sections = (init => \$init, decl => \$decl, sym => \$symsect,
  1336. binop => \$binopsect, condop => \$condopsect,
  1337. cop => \$copsect, padop => \$padopsect,
  1338. listop => \$listopsect, logop => \$logopsect,
  1339. loop => \$loopsect, op => \$opsect, pmop => \$pmopsect,
  1340. pvop => \$pvopsect, svop => \$svopsect, unop => \$unopsect,
  1341. sv => \$svsect, xpv => \$xpvsect, xpvav => \$xpvavsect,
  1342. xpvhv => \$xpvhvsect, xpvcv => \$xpvcvsect,
  1343. xpviv => \$xpvivsect, xpvnv => \$xpvnvsect,
  1344. xpvmg => \$xpvmgsect, xpvlv => \$xpvlvsect,
  1345. xrv => \$xrvsect, xpvbm => \$xpvbmsect,
  1346. xpvio => \$xpviosect);
  1347. my ($name, $sectref);
  1348. while (($name, $sectref) = splice(@sections, 0, 2)) {
  1349. $$sectref = new B::C::Section $name, \%symtable, 0;
  1350. }
  1351. }
  1352. sub mark_unused
  1353. {
  1354. my ($arg,$val) = @_;
  1355. $unused_sub_packages{$arg} = $val;
  1356. }
  1357. sub compile {
  1358. my @options = @_;
  1359. my ($option, $opt, $arg);
  1360. OPTION:
  1361. while ($option = shift @options) {
  1362. if ($option =~ /^-(.)(.*)/) {
  1363. $opt = $1;
  1364. $arg = $2;
  1365. } else {
  1366. unshift @options, $option;
  1367. last OPTION;
  1368. }
  1369. if ($opt eq "-" && $arg eq "-") {
  1370. shift @options;
  1371. last OPTION;
  1372. }
  1373. if ($opt eq "w") {
  1374. $warn_undefined_syms = 1;
  1375. } elsif ($opt eq "D") {
  1376. $arg ||= shift @options;
  1377. foreach $arg (split(//, $arg)) {
  1378. if ($arg eq "o") {
  1379. B->debug(1);
  1380. } elsif ($arg eq "c") {
  1381. $debug_cops = 1;
  1382. } elsif ($arg eq "A") {
  1383. $debug_av = 1;
  1384. } elsif ($arg eq "C") {
  1385. $debug_cv = 1;
  1386. } elsif ($arg eq "M") {
  1387. $debug_mg = 1;
  1388. } else {
  1389. warn "ignoring unknown debug option: $arg\n";
  1390. }
  1391. }
  1392. } elsif ($opt eq "o") {
  1393. $arg ||= shift @options;
  1394. open(STDOUT, ">$arg") or return "$arg: $!\n";
  1395. } elsif ($opt eq "v") {
  1396. $verbose = 1;
  1397. } elsif ($opt eq "u") {
  1398. $arg ||= shift @options;
  1399. mark_unused($arg,undef);
  1400. } elsif ($opt eq "f") {
  1401. $arg ||= shift @options;
  1402. if ($arg eq "cog") {
  1403. $pv_copy_on_grow = 1;
  1404. } elsif ($arg eq "no-cog") {
  1405. $pv_copy_on_grow = 0;
  1406. }
  1407. } elsif ($opt eq "O") {
  1408. $arg = 1 if $arg eq "";
  1409. $pv_copy_on_grow = 0;
  1410. if ($arg >= 1) {
  1411. # Optimisations for -O1
  1412. $pv_copy_on_grow = 1;
  1413. }
  1414. } elsif ($opt eq "l") {
  1415. $max_string_len = $arg;
  1416. }
  1417. }
  1418. init_sections();
  1419. if (@options) {
  1420. return sub {
  1421. my $objname;
  1422. foreach $objname (@options) {
  1423. eval "save_object(\\$objname)";
  1424. }
  1425. output_all();
  1426. }
  1427. } else {
  1428. return sub { save_main() };
  1429. }
  1430. }
  1431. 1;
  1432. __END__
  1433. =head1 NAME
  1434. B::C - Perl compiler's C backend
  1435. =head1 SYNOPSIS
  1436. perl -MO=C[,OPTIONS] foo.pl
  1437. =head1 DESCRIPTION
  1438. This compiler backend takes Perl source and generates C source code
  1439. corresponding to the internal structures that perl uses to run
  1440. your program. When the generated C source is compiled and run, it
  1441. cuts out the time which perl would have taken to load and parse
  1442. your program into its internal semi-compiled form. That means that
  1443. compiling with this backend will not help improve the runtime
  1444. execution speed of your program but may improve the start-up time.
  1445. Depending on the environment in which your program runs this may be
  1446. either a help or a hindrance.
  1447. =head1 OPTIONS
  1448. If there are any non-option arguments, they are taken to be
  1449. names of objects to be saved (probably doesn't work properly yet).
  1450. Without extra arguments, it saves the main program.
  1451. =over 4
  1452. =item B<-ofilename>
  1453. Output to filename instead of STDOUT
  1454. =item B<-v>
  1455. Verbose compilation (currently gives a few compilation statistics).
  1456. =item B<-->
  1457. Force end of options
  1458. =item B<-uPackname>
  1459. Force apparently unused subs from package Packname to be compiled.
  1460. This allows programs to use eval "foo()" even when sub foo is never
  1461. seen to be used at compile time. The down side is that any subs which
  1462. really are never used also have code generated. This option is
  1463. necessary, for example, if you have a signal handler foo which you
  1464. initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just
  1465. to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u>
  1466. options. The compiler tries to figure out which packages may possibly
  1467. have subs in which need compiling but the current version doesn't do
  1468. it very well. In particular, it is confused by nested packages (i.e.
  1469. of the form C<A::B>) where package C<A> does not contain any subs.
  1470. =item B<-D>
  1471. Debug options (concatenated or separate flags like C<perl -D>).
  1472. =item B<-Do>
  1473. OPs, prints each OP as it's processed
  1474. =item B<-Dc>
  1475. COPs, prints COPs as processed (incl. file & line num)
  1476. =item B<-DA>
  1477. prints AV information on saving
  1478. =item B<-DC>
  1479. prints CV information on saving
  1480. =item B<-DM>
  1481. prints MAGIC information on saving
  1482. =item B<-f>
  1483. Force optimisations on or off one at a time.
  1484. =item B<-fcog>
  1485. Copy-on-grow: PVs declared and initialised statically.
  1486. =item B<-fno-cog>
  1487. No copy-on-grow.
  1488. =item B<-On>
  1489. Optimisation level (n = 0, 1, 2, ...). B<-O> means B<-O1>. Currently,
  1490. B<-O1> and higher set B<-fcog>.
  1491. =item B<-llimit>
  1492. Some C compilers impose an arbitrary limit on the length of string
  1493. constants (e.g. 2048 characters for Microsoft Visual C++). The
  1494. B<-llimit> options tells the C backend not to generate string literals
  1495. exceeding that limit.
  1496. =back
  1497. =head1 EXAMPLES
  1498. perl -MO=C,-ofoo.c foo.pl
  1499. perl cc_harness -o foo foo.c
  1500. Note that C<cc_harness> lives in the C<B> subdirectory of your perl
  1501. library directory. The utility called C<perlcc> may also be used to
  1502. help make use of this compiler.
  1503. perl -MO=C,-v,-DcA,-l2048 bar.pl > /dev/null
  1504. =head1 BUGS
  1505. Plenty. Current status: experimental.
  1506. =head1 AUTHOR
  1507. Malcolm Beattie, C<[email protected]>
  1508. =cut