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.

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