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.

831 lines
14 KiB

  1. # B.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;
  9. require DynaLoader;
  10. require Exporter;
  11. @ISA = qw(Exporter DynaLoader);
  12. @EXPORT_OK = qw(byteload_fh byteload_string minus_c ppname
  13. class peekop cast_I32 cstring cchar hash threadsv_names
  14. main_root main_start main_cv svref_2object
  15. walkoptree walkoptree_slow walkoptree_exec walksymtable
  16. parents comppadlist sv_undef compile_stats timing_info init_av);
  17. use strict;
  18. @B::SV::ISA = 'B::OBJECT';
  19. @B::NULL::ISA = 'B::SV';
  20. @B::PV::ISA = 'B::SV';
  21. @B::IV::ISA = 'B::SV';
  22. @B::NV::ISA = 'B::IV';
  23. @B::RV::ISA = 'B::SV';
  24. @B::PVIV::ISA = qw(B::PV B::IV);
  25. @B::PVNV::ISA = qw(B::PV B::NV);
  26. @B::PVMG::ISA = 'B::PVNV';
  27. @B::PVLV::ISA = 'B::PVMG';
  28. @B::BM::ISA = 'B::PVMG';
  29. @B::AV::ISA = 'B::PVMG';
  30. @B::GV::ISA = 'B::PVMG';
  31. @B::HV::ISA = 'B::PVMG';
  32. @B::CV::ISA = 'B::PVMG';
  33. @B::IO::ISA = 'B::PVMG';
  34. @B::FM::ISA = 'B::CV';
  35. @B::OP::ISA = 'B::OBJECT';
  36. @B::UNOP::ISA = 'B::OP';
  37. @B::BINOP::ISA = 'B::UNOP';
  38. @B::LOGOP::ISA = 'B::UNOP';
  39. @B::CONDOP::ISA = 'B::UNOP';
  40. @B::LISTOP::ISA = 'B::BINOP';
  41. @B::SVOP::ISA = 'B::OP';
  42. @B::GVOP::ISA = 'B::OP';
  43. @B::PVOP::ISA = 'B::OP';
  44. @B::CVOP::ISA = 'B::OP';
  45. @B::LOOP::ISA = 'B::LISTOP';
  46. @B::PMOP::ISA = 'B::LISTOP';
  47. @B::COP::ISA = 'B::OP';
  48. @B::SPECIAL::ISA = 'B::OBJECT';
  49. {
  50. # Stop "-w" from complaining about the lack of a real B::OBJECT class
  51. package B::OBJECT;
  52. }
  53. my $debug;
  54. my $op_count = 0;
  55. my @parents = ();
  56. sub debug {
  57. my ($class, $value) = @_;
  58. $debug = $value;
  59. walkoptree_debug($value);
  60. }
  61. # sub OPf_KIDS;
  62. # add to .xs for perl5.002
  63. sub OPf_KIDS () { 4 }
  64. sub class {
  65. my $obj = shift;
  66. my $name = ref $obj;
  67. $name =~ s/^.*:://;
  68. return $name;
  69. }
  70. sub parents { \@parents }
  71. # For debugging
  72. sub peekop {
  73. my $op = shift;
  74. return sprintf("%s (0x%x) %s", class($op), $$op, $op->ppaddr);
  75. }
  76. sub walkoptree_slow {
  77. my($op, $method, $level) = @_;
  78. $op_count++; # just for statistics
  79. $level ||= 0;
  80. warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
  81. $op->$method($level);
  82. if ($$op && ($op->flags & OPf_KIDS)) {
  83. my $kid;
  84. unshift(@parents, $op);
  85. for ($kid = $op->first; $$kid; $kid = $kid->sibling) {
  86. walkoptree_slow($kid, $method, $level + 1);
  87. }
  88. shift @parents;
  89. }
  90. }
  91. sub compile_stats {
  92. return "Total number of OPs processed: $op_count\n";
  93. }
  94. sub timing_info {
  95. my ($sec, $min, $hr) = localtime;
  96. my ($user, $sys) = times;
  97. sprintf("%02d:%02d:%02d user=$user sys=$sys",
  98. $hr, $min, $sec, $user, $sys);
  99. }
  100. my %symtable;
  101. sub savesym {
  102. my ($obj, $value) = @_;
  103. # warn(sprintf("savesym: sym_%x => %s\n", $$obj, $value)); # debug
  104. $symtable{sprintf("sym_%x", $$obj)} = $value;
  105. }
  106. sub objsym {
  107. my $obj = shift;
  108. return $symtable{sprintf("sym_%x", $$obj)};
  109. }
  110. sub walkoptree_exec {
  111. my ($op, $method, $level) = @_;
  112. my ($sym, $ppname);
  113. my $prefix = " " x $level;
  114. for (; $$op; $op = $op->next) {
  115. $sym = objsym($op);
  116. if (defined($sym)) {
  117. print $prefix, "goto $sym\n";
  118. return;
  119. }
  120. savesym($op, sprintf("%s (0x%lx)", class($op), $$op));
  121. $op->$method($level);
  122. $ppname = $op->ppaddr;
  123. if ($ppname =~ /^pp_(or|and|mapwhile|grepwhile|entertry)$/) {
  124. print $prefix, uc($1), " => {\n";
  125. walkoptree_exec($op->other, $method, $level + 1);
  126. print $prefix, "}\n";
  127. } elsif ($ppname eq "pp_match" || $ppname eq "pp_subst") {
  128. my $pmreplstart = $op->pmreplstart;
  129. if ($$pmreplstart) {
  130. print $prefix, "PMREPLSTART => {\n";
  131. walkoptree_exec($pmreplstart, $method, $level + 1);
  132. print $prefix, "}\n";
  133. }
  134. } elsif ($ppname eq "pp_substcont") {
  135. print $prefix, "SUBSTCONT => {\n";
  136. walkoptree_exec($op->other->pmreplstart, $method, $level + 1);
  137. print $prefix, "}\n";
  138. $op = $op->other;
  139. } elsif ($ppname eq "pp_cond_expr") {
  140. # pp_cond_expr never returns op_next
  141. print $prefix, "TRUE => {\n";
  142. walkoptree_exec($op->true, $method, $level + 1);
  143. print $prefix, "}\n";
  144. $op = $op->false;
  145. redo;
  146. } elsif ($ppname eq "pp_range") {
  147. print $prefix, "TRUE => {\n";
  148. walkoptree_exec($op->true, $method, $level + 1);
  149. print $prefix, "}\n", $prefix, "FALSE => {\n";
  150. walkoptree_exec($op->false, $method, $level + 1);
  151. print $prefix, "}\n";
  152. } elsif ($ppname eq "pp_enterloop") {
  153. print $prefix, "REDO => {\n";
  154. walkoptree_exec($op->redoop, $method, $level + 1);
  155. print $prefix, "}\n", $prefix, "NEXT => {\n";
  156. walkoptree_exec($op->nextop, $method, $level + 1);
  157. print $prefix, "}\n", $prefix, "LAST => {\n";
  158. walkoptree_exec($op->lastop, $method, $level + 1);
  159. print $prefix, "}\n";
  160. } elsif ($ppname eq "pp_subst") {
  161. my $replstart = $op->pmreplstart;
  162. if ($$replstart) {
  163. print $prefix, "SUBST => {\n";
  164. walkoptree_exec($replstart, $method, $level + 1);
  165. print $prefix, "}\n";
  166. }
  167. }
  168. }
  169. }
  170. sub walksymtable {
  171. my ($symref, $method, $recurse, $prefix) = @_;
  172. my $sym;
  173. no strict 'vars';
  174. local(*glob);
  175. while (($sym, *glob) = each %$symref) {
  176. if ($sym =~ /::$/) {
  177. $sym = $prefix . $sym;
  178. if ($sym ne "main::" && &$recurse($sym)) {
  179. walksymtable(\%glob, $method, $recurse, $sym);
  180. }
  181. } else {
  182. svref_2object(\*glob)->EGV->$method();
  183. }
  184. }
  185. }
  186. {
  187. package B::Section;
  188. my $output_fh;
  189. my %sections;
  190. sub new {
  191. my ($class, $section, $symtable, $default) = @_;
  192. $output_fh ||= FileHandle->new_tmpfile;
  193. my $obj = bless [-1, $section, $symtable, $default], $class;
  194. $sections{$section} = $obj;
  195. return $obj;
  196. }
  197. sub get {
  198. my ($class, $section) = @_;
  199. return $sections{$section};
  200. }
  201. sub add {
  202. my $section = shift;
  203. while (defined($_ = shift)) {
  204. print $output_fh "$section->[1]\t$_\n";
  205. $section->[0]++;
  206. }
  207. }
  208. sub index {
  209. my $section = shift;
  210. return $section->[0];
  211. }
  212. sub name {
  213. my $section = shift;
  214. return $section->[1];
  215. }
  216. sub symtable {
  217. my $section = shift;
  218. return $section->[2];
  219. }
  220. sub default {
  221. my $section = shift;
  222. return $section->[3];
  223. }
  224. sub output {
  225. my ($section, $fh, $format) = @_;
  226. my $name = $section->name;
  227. my $sym = $section->symtable || {};
  228. my $default = $section->default;
  229. seek($output_fh, 0, 0);
  230. while (<$output_fh>) {
  231. chomp;
  232. s/^(.*?)\t//;
  233. if ($1 eq $name) {
  234. s{(s\\_[0-9a-f]+)} {
  235. exists($sym->{$1}) ? $sym->{$1} : $default;
  236. }ge;
  237. printf $fh $format, $_;
  238. }
  239. }
  240. }
  241. }
  242. bootstrap B;
  243. 1;
  244. __END__
  245. =head1 NAME
  246. B - The Perl Compiler
  247. =head1 SYNOPSIS
  248. use B;
  249. =head1 DESCRIPTION
  250. The C<B> module supplies classes which allow a Perl program to delve
  251. into its own innards. It is the module used to implement the
  252. "backends" of the Perl compiler. Usage of the compiler does not
  253. require knowledge of this module: see the F<O> module for the
  254. user-visible part. The C<B> module is of use to those who want to
  255. write new compiler backends. This documentation assumes that the
  256. reader knows a fair amount about perl's internals including such
  257. things as SVs, OPs and the internal symbol table and syntax tree
  258. of a program.
  259. =head1 OVERVIEW OF CLASSES
  260. The C structures used by Perl's internals to hold SV and OP
  261. information (PVIV, AV, HV, ..., OP, SVOP, UNOP, ...) are modelled on a
  262. class hierarchy and the C<B> module gives access to them via a true
  263. object hierarchy. Structure fields which point to other objects
  264. (whether types of SV or types of OP) are represented by the C<B>
  265. module as Perl objects of the appropriate class. The bulk of the C<B>
  266. module is the methods for accessing fields of these structures. Note
  267. that all access is read-only: you cannot modify the internals by
  268. using this module.
  269. =head2 SV-RELATED CLASSES
  270. B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM, B::PVLV,
  271. B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes correspond in
  272. the obvious way to the underlying C structures of similar names. The
  273. inheritance hierarchy mimics the underlying C "inheritance". Access
  274. methods correspond to the underlying C macros for field access,
  275. usually with the leading "class indication" prefix removed (Sv, Av,
  276. Hv, ...). The leading prefix is only left in cases where its removal
  277. would cause a clash in method name. For example, C<GvREFCNT> stays
  278. as-is since its abbreviation would clash with the "superclass" method
  279. C<REFCNT> (corresponding to the C function C<SvREFCNT>).
  280. =head2 B::SV METHODS
  281. =over 4
  282. =item REFCNT
  283. =item FLAGS
  284. =back
  285. =head2 B::IV METHODS
  286. =over 4
  287. =item IV
  288. =item IVX
  289. =item needs64bits
  290. =item packiv
  291. =back
  292. =head2 B::NV METHODS
  293. =over 4
  294. =item NV
  295. =item NVX
  296. =back
  297. =head2 B::RV METHODS
  298. =over 4
  299. =item RV
  300. =back
  301. =head2 B::PV METHODS
  302. =over 4
  303. =item PV
  304. =back
  305. =head2 B::PVMG METHODS
  306. =over 4
  307. =item MAGIC
  308. =item SvSTASH
  309. =back
  310. =head2 B::MAGIC METHODS
  311. =over 4
  312. =item MOREMAGIC
  313. =item PRIVATE
  314. =item TYPE
  315. =item FLAGS
  316. =item OBJ
  317. =item PTR
  318. =back
  319. =head2 B::PVLV METHODS
  320. =over 4
  321. =item TARGOFF
  322. =item TARGLEN
  323. =item TYPE
  324. =item TARG
  325. =back
  326. =head2 B::BM METHODS
  327. =over 4
  328. =item USEFUL
  329. =item PREVIOUS
  330. =item RARE
  331. =item TABLE
  332. =back
  333. =head2 B::GV METHODS
  334. =over 4
  335. =item NAME
  336. =item STASH
  337. =item SV
  338. =item IO
  339. =item FORM
  340. =item AV
  341. =item HV
  342. =item EGV
  343. =item CV
  344. =item CVGEN
  345. =item LINE
  346. =item FILEGV
  347. =item GvREFCNT
  348. =item FLAGS
  349. =back
  350. =head2 B::IO METHODS
  351. =over 4
  352. =item LINES
  353. =item PAGE
  354. =item PAGE_LEN
  355. =item LINES_LEFT
  356. =item TOP_NAME
  357. =item TOP_GV
  358. =item FMT_NAME
  359. =item FMT_GV
  360. =item BOTTOM_NAME
  361. =item BOTTOM_GV
  362. =item SUBPROCESS
  363. =item IoTYPE
  364. =item IoFLAGS
  365. =back
  366. =head2 B::AV METHODS
  367. =over 4
  368. =item FILL
  369. =item MAX
  370. =item OFF
  371. =item ARRAY
  372. =item AvFLAGS
  373. =back
  374. =head2 B::CV METHODS
  375. =over 4
  376. =item STASH
  377. =item START
  378. =item ROOT
  379. =item GV
  380. =item FILEGV
  381. =item DEPTH
  382. =item PADLIST
  383. =item OUTSIDE
  384. =item XSUB
  385. =item XSUBANY
  386. =item CvFLAGS
  387. =back
  388. =head2 B::HV METHODS
  389. =over 4
  390. =item FILL
  391. =item MAX
  392. =item KEYS
  393. =item RITER
  394. =item NAME
  395. =item PMROOT
  396. =item ARRAY
  397. =back
  398. =head2 OP-RELATED CLASSES
  399. B::OP, B::UNOP, B::BINOP, B::LOGOP, B::CONDOP, B::LISTOP, B::PMOP,
  400. B::SVOP, B::GVOP, B::PVOP, B::CVOP, B::LOOP, B::COP.
  401. These classes correspond in
  402. the obvious way to the underlying C structures of similar names. The
  403. inheritance hierarchy mimics the underlying C "inheritance". Access
  404. methods correspond to the underlying C structre field names, with the
  405. leading "class indication" prefix removed (op_).
  406. =head2 B::OP METHODS
  407. =over 4
  408. =item next
  409. =item sibling
  410. =item ppaddr
  411. This returns the function name as a string (e.g. pp_add, pp_rv2av).
  412. =item desc
  413. This returns the op description from the global C PL_op_desc array
  414. (e.g. "addition" "array deref").
  415. =item targ
  416. =item type
  417. =item seq
  418. =item flags
  419. =item private
  420. =back
  421. =head2 B::UNOP METHOD
  422. =over 4
  423. =item first
  424. =back
  425. =head2 B::BINOP METHOD
  426. =over 4
  427. =item last
  428. =back
  429. =head2 B::LOGOP METHOD
  430. =over 4
  431. =item other
  432. =back
  433. =head2 B::CONDOP METHODS
  434. =over 4
  435. =item true
  436. =item false
  437. =back
  438. =head2 B::LISTOP METHOD
  439. =over 4
  440. =item children
  441. =back
  442. =head2 B::PMOP METHODS
  443. =over 4
  444. =item pmreplroot
  445. =item pmreplstart
  446. =item pmnext
  447. =item pmregexp
  448. =item pmflags
  449. =item pmpermflags
  450. =item precomp
  451. =back
  452. =head2 B::SVOP METHOD
  453. =over 4
  454. =item sv
  455. =back
  456. =head2 B::GVOP METHOD
  457. =over 4
  458. =item gv
  459. =back
  460. =head2 B::PVOP METHOD
  461. =over 4
  462. =item pv
  463. =back
  464. =head2 B::LOOP METHODS
  465. =over 4
  466. =item redoop
  467. =item nextop
  468. =item lastop
  469. =back
  470. =head2 B::COP METHODS
  471. =over 4
  472. =item label
  473. =item stash
  474. =item filegv
  475. =item cop_seq
  476. =item arybase
  477. =item line
  478. =back
  479. =head1 FUNCTIONS EXPORTED BY C<B>
  480. The C<B> module exports a variety of functions: some are simple
  481. utility functions, others provide a Perl program with a way to
  482. get an initial "handle" on an internal object.
  483. =over 4
  484. =item main_cv
  485. Return the (faked) CV corresponding to the main part of the Perl
  486. program.
  487. =item init_av
  488. Returns the AV object (i.e. in class B::AV) representing INIT blocks.
  489. =item main_root
  490. Returns the root op (i.e. an object in the appropriate B::OP-derived
  491. class) of the main part of the Perl program.
  492. =item main_start
  493. Returns the starting op of the main part of the Perl program.
  494. =item comppadlist
  495. Returns the AV object (i.e. in class B::AV) of the global comppadlist.
  496. =item sv_undef
  497. Returns the SV object corresponding to the C variable C<sv_undef>.
  498. =item sv_yes
  499. Returns the SV object corresponding to the C variable C<sv_yes>.
  500. =item sv_no
  501. Returns the SV object corresponding to the C variable C<sv_no>.
  502. =item walkoptree(OP, METHOD)
  503. Does a tree-walk of the syntax tree based at OP and calls METHOD on
  504. each op it visits. Each node is visited before its children. If
  505. C<walkoptree_debug> (q.v.) has been called to turn debugging on then
  506. the method C<walkoptree_debug> is called on each op before METHOD is
  507. called.
  508. =item walkoptree_debug(DEBUG)
  509. Returns the current debugging flag for C<walkoptree>. If the optional
  510. DEBUG argument is non-zero, it sets the debugging flag to that. See
  511. the description of C<walkoptree> above for what the debugging flag
  512. does.
  513. =item walksymtable(SYMREF, METHOD, RECURSE)
  514. Walk the symbol table starting at SYMREF and call METHOD on each
  515. symbol visited. When the walk reached package symbols "Foo::" it
  516. invokes RECURSE and only recurses into the package if that sub
  517. returns true.
  518. =item svref_2object(SV)
  519. Takes any Perl variable and turns it into an object in the
  520. appropriate B::OP-derived or B::SV-derived class. Apart from functions
  521. such as C<main_root>, this is the primary way to get an initial
  522. "handle" on a internal perl data structure which can then be followed
  523. with the other access methods.
  524. =item ppname(OPNUM)
  525. Return the PP function name (e.g. "pp_add") of op number OPNUM.
  526. =item hash(STR)
  527. Returns a string in the form "0x..." representing the value of the
  528. internal hash function used by perl on string STR.
  529. =item cast_I32(I)
  530. Casts I to the internal I32 type used by that perl.
  531. =item minus_c
  532. Does the equivalent of the C<-c> command-line option. Obviously, this
  533. is only useful in a BEGIN block or else the flag is set too late.
  534. =item cstring(STR)
  535. Returns a double-quote-surrounded escaped version of STR which can
  536. be used as a string in C source code.
  537. =item class(OBJ)
  538. Returns the class of an object without the part of the classname
  539. preceding the first "::". This is used to turn "B::UNOP" into
  540. "UNOP" for example.
  541. =item threadsv_names
  542. In a perl compiled for threads, this returns a list of the special
  543. per-thread threadsv variables.
  544. =item byteload_fh(FILEHANDLE)
  545. Load the contents of FILEHANDLE as bytecode. See documentation for
  546. the B<Bytecode> module in F<B::Backend> for how to generate bytecode.
  547. =back
  548. =head1 AUTHOR
  549. Malcolm Beattie, C<[email protected]>
  550. =cut