Source code of Windows XP (NT5)
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.

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