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.

1369 lines
36 KiB

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S "%0" %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. goto endofperl
  11. @rem ';
  12. #!perl
  13. #line 14
  14. eval 'exec P:\Apps\ActivePerl\temp\bin\MSWin32-x86-object\perl.exe -S $0 ${1+"$@"}'
  15. if $running_under_some_shell;
  16. #
  17. #
  18. # c2ph (aka pstruct)
  19. # Tom Christiansen, <[email protected]>
  20. #
  21. # As pstruct, dump C structures as generated from 'cc -g -S' stabs.
  22. # As c2ph, do this PLUS generate perl code for getting at the structures.
  23. #
  24. # See the usage message for more. If this isn't enough, read the code.
  25. #
  26. =head1 NAME
  27. c2ph, pstruct - Dump C structures as generated from C<cc -g -S> stabs
  28. =head1 SYNOPSIS
  29. c2ph [-dpnP] [var=val] [files ...]
  30. =head2 OPTIONS
  31. Options:
  32. -w wide; short for: type_width=45 member_width=35 offset_width=8
  33. -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04
  34. -n do not generate perl code (default when invoked as pstruct)
  35. -p generate perl code (default when invoked as c2ph)
  36. -v generate perl code, with C decls as comments
  37. -i do NOT recompute sizes for intrinsic datatypes
  38. -a dump information on intrinsics also
  39. -t trace execution
  40. -d spew reams of debugging output
  41. -slist give comma-separated list a structures to dump
  42. =head1 DESCRIPTION
  43. The following is the old c2ph.doc documentation by Tom Christiansen
  44. <[email protected]>
  45. Date: 25 Jul 91 08:10:21 GMT
  46. Once upon a time, I wrote a program called pstruct. It was a perl
  47. program that tried to parse out C structures and display their member
  48. offsets for you. This was especially useful for people looking at
  49. binary dumps or poking around the kernel.
  50. Pstruct was not a pretty program. Neither was it particularly robust.
  51. The problem, you see, was that the C compiler was much better at parsing
  52. C than I could ever hope to be.
  53. So I got smart: I decided to be lazy and let the C compiler parse the C,
  54. which would spit out debugger stabs for me to read. These were much
  55. easier to parse. It's still not a pretty program, but at least it's more
  56. robust.
  57. Pstruct takes any .c or .h files, or preferably .s ones, since that's
  58. the format it is going to massage them into anyway, and spits out
  59. listings like this:
  60. struct tty {
  61. int tty.t_locker 000 4
  62. int tty.t_mutex_index 004 4
  63. struct tty * tty.t_tp_virt 008 4
  64. struct clist tty.t_rawq 00c 20
  65. int tty.t_rawq.c_cc 00c 4
  66. int tty.t_rawq.c_cmax 010 4
  67. int tty.t_rawq.c_cfx 014 4
  68. int tty.t_rawq.c_clx 018 4
  69. struct tty * tty.t_rawq.c_tp_cpu 01c 4
  70. struct tty * tty.t_rawq.c_tp_iop 020 4
  71. unsigned char * tty.t_rawq.c_buf_cpu 024 4
  72. unsigned char * tty.t_rawq.c_buf_iop 028 4
  73. struct clist tty.t_canq 02c 20
  74. int tty.t_canq.c_cc 02c 4
  75. int tty.t_canq.c_cmax 030 4
  76. int tty.t_canq.c_cfx 034 4
  77. int tty.t_canq.c_clx 038 4
  78. struct tty * tty.t_canq.c_tp_cpu 03c 4
  79. struct tty * tty.t_canq.c_tp_iop 040 4
  80. unsigned char * tty.t_canq.c_buf_cpu 044 4
  81. unsigned char * tty.t_canq.c_buf_iop 048 4
  82. struct clist tty.t_outq 04c 20
  83. int tty.t_outq.c_cc 04c 4
  84. int tty.t_outq.c_cmax 050 4
  85. int tty.t_outq.c_cfx 054 4
  86. int tty.t_outq.c_clx 058 4
  87. struct tty * tty.t_outq.c_tp_cpu 05c 4
  88. struct tty * tty.t_outq.c_tp_iop 060 4
  89. unsigned char * tty.t_outq.c_buf_cpu 064 4
  90. unsigned char * tty.t_outq.c_buf_iop 068 4
  91. (*int)() tty.t_oproc_cpu 06c 4
  92. (*int)() tty.t_oproc_iop 070 4
  93. (*int)() tty.t_stopproc_cpu 074 4
  94. (*int)() tty.t_stopproc_iop 078 4
  95. struct thread * tty.t_rsel 07c 4
  96. etc.
  97. Actually, this was generated by a particular set of options. You can control
  98. the formatting of each column, whether you prefer wide or fat, hex or decimal,
  99. leading zeroes or whatever.
  100. All you need to be able to use this is a C compiler than generates
  101. BSD/GCC-style stabs. The B<-g> option on native BSD compilers and GCC
  102. should get this for you.
  103. To learn more, just type a bogus option, like B<-\?>, and a long usage message
  104. will be provided. There are a fair number of possibilities.
  105. If you're only a C programmer, than this is the end of the message for you.
  106. You can quit right now, and if you care to, save off the source and run it
  107. when you feel like it. Or not.
  108. But if you're a perl programmer, then for you I have something much more
  109. wondrous than just a structure offset printer.
  110. You see, if you call pstruct by its other incybernation, c2ph, you have a code
  111. generator that translates C code into perl code! Well, structure and union
  112. declarations at least, but that's quite a bit.
  113. Prior to this point, anyone programming in perl who wanted to interact
  114. with C programs, like the kernel, was forced to guess the layouts of
  115. the C strutures, and then hardwire these into his program. Of course,
  116. when you took your wonderfully crafted program to a system where the
  117. sgtty structure was laid out differently, you program broke. Which is
  118. a shame.
  119. We've had Larry's h2ph translator, which helped, but that only works on
  120. cpp symbols, not real C, which was also very much needed. What I offer
  121. you is a symbolic way of getting at all the C structures. I've couched
  122. them in terms of packages and functions. Consider the following program:
  123. #!/usr/local/bin/perl
  124. require 'syscall.ph';
  125. require 'sys/time.ph';
  126. require 'sys/resource.ph';
  127. $ru = "\0" x &rusage'sizeof();
  128. syscall(&SYS_getrusage, &RUSAGE_SELF, $ru) && die "getrusage: $!";
  129. @ru = unpack($t = &rusage'typedef(), $ru);
  130. $utime = $ru[ &rusage'ru_utime + &timeval'tv_sec ]
  131. + ($ru[ &rusage'ru_utime + &timeval'tv_usec ]) / 1e6;
  132. $stime = $ru[ &rusage'ru_stime + &timeval'tv_sec ]
  133. + ($ru[ &rusage'ru_stime + &timeval'tv_usec ]) / 1e6;
  134. printf "you have used %8.3fs+%8.3fu seconds.\n", $utime, $stime;
  135. As you see, the name of the package is the name of the structure. Regular
  136. fields are just their own names. Plus the following accessor functions are
  137. provided for your convenience:
  138. struct This takes no arguments, and is merely the number of first-level
  139. elements in the structure. You would use this for indexing
  140. into arrays of structures, perhaps like this
  141. $usec = $u[ &user'u_utimer
  142. + (&ITIMER_VIRTUAL * &itimerval'struct)
  143. + &itimerval'it_value
  144. + &timeval'tv_usec
  145. ];
  146. sizeof Returns the bytes in the structure, or the member if
  147. you pass it an argument, such as
  148. &rusage'sizeof(&rusage'ru_utime)
  149. typedef This is the perl format definition for passing to pack and
  150. unpack. If you ask for the typedef of a nothing, you get
  151. the whole structure, otherwise you get that of the member
  152. you ask for. Padding is taken care of, as is the magic to
  153. guarantee that a union is unpacked into all its aliases.
  154. Bitfields are not quite yet supported however.
  155. offsetof This function is the byte offset into the array of that
  156. member. You may wish to use this for indexing directly
  157. into the packed structure with vec() if you're too lazy
  158. to unpack it.
  159. typeof Not to be confused with the typedef accessor function, this
  160. one returns the C type of that field. This would allow
  161. you to print out a nice structured pretty print of some
  162. structure without knoning anything about it beforehand.
  163. No args to this one is a noop. Someday I'll post such
  164. a thing to dump out your u structure for you.
  165. The way I see this being used is like basically this:
  166. % h2ph <some_include_file.h > /usr/lib/perl/tmp.ph
  167. % c2ph some_include_file.h >> /usr/lib/perl/tmp.ph
  168. % install
  169. It's a little tricker with c2ph because you have to get the includes right.
  170. I can't know this for your system, but it's not usually too terribly difficult.
  171. The code isn't pretty as I mentioned -- I never thought it would be a 1000-
  172. line program when I started, or I might not have begun. :-) But I would have
  173. been less cavalier in how the parts of the program communicated with each
  174. other, etc. It might also have helped if I didn't have to divine the makeup
  175. of the stabs on the fly, and then account for micro differences between my
  176. compiler and gcc.
  177. Anyway, here it is. Should run on perl v4 or greater. Maybe less.
  178. --tom
  179. =cut
  180. $RCSID = '$Id: c2ph,v 1.7 95/10/28 10:41:47 tchrist Exp Locker: tchrist $';
  181. ######################################################################
  182. # some handy data definitions. many of these can be reset later.
  183. $bitorder = 'b'; # ascending; set to B for descending bit fields
  184. %intrinsics =
  185. %template = (
  186. 'char', 'c',
  187. 'unsigned char', 'C',
  188. 'short', 's',
  189. 'short int', 's',
  190. 'unsigned short', 'S',
  191. 'unsigned short int', 'S',
  192. 'short unsigned int', 'S',
  193. 'int', 'i',
  194. 'unsigned int', 'I',
  195. 'long', 'l',
  196. 'long int', 'l',
  197. 'unsigned long', 'L',
  198. 'unsigned long', 'L',
  199. 'long unsigned int', 'L',
  200. 'unsigned long int', 'L',
  201. 'long long', 'q',
  202. 'long long int', 'q',
  203. 'unsigned long long', 'Q',
  204. 'unsigned long long int', 'Q',
  205. 'float', 'f',
  206. 'double', 'd',
  207. 'pointer', 'p',
  208. 'null', 'x',
  209. 'neganull', 'X',
  210. 'bit', $bitorder,
  211. );
  212. &buildscrunchlist;
  213. delete $intrinsics{'neganull'};
  214. delete $intrinsics{'bit'};
  215. delete $intrinsics{'null'};
  216. # use -s to recompute sizes
  217. %sizeof = (
  218. 'char', '1',
  219. 'unsigned char', '1',
  220. 'short', '2',
  221. 'short int', '2',
  222. 'unsigned short', '2',
  223. 'unsigned short int', '2',
  224. 'short unsigned int', '2',
  225. 'int', '4',
  226. 'unsigned int', '4',
  227. 'long', '4',
  228. 'long int', '4',
  229. 'unsigned long', '4',
  230. 'unsigned long int', '4',
  231. 'long unsigned int', '4',
  232. 'long long', '8',
  233. 'long long int', '8',
  234. 'unsigned long long', '8',
  235. 'unsigned long long int', '8',
  236. 'float', '4',
  237. 'double', '8',
  238. 'pointer', '4',
  239. );
  240. ($type_width, $member_width, $offset_width, $size_width) = (20, 20, 6, 5);
  241. ($offset_fmt, $size_fmt) = ('d', 'd');
  242. $indent = 2;
  243. $CC = 'cc';
  244. $CFLAGS = '-g -S';
  245. $DEFINES = '';
  246. $perl++ if $0 =~ m#/?c2ph$#;
  247. require 'getopts.pl';
  248. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  249. &Getopts('aixdpvtnws:') || &usage(0);
  250. $opt_d && $debug++;
  251. $opt_t && $trace++;
  252. $opt_p && $perl++;
  253. $opt_v && $verbose++;
  254. $opt_n && ($perl = 0);
  255. if ($opt_w) {
  256. ($type_width, $member_width, $offset_width) = (45, 35, 8);
  257. }
  258. if ($opt_x) {
  259. ($offset_fmt, $offset_width, $size_fmt, $size_width) = ( 'x', '08', 'x', 04 );
  260. }
  261. eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  262. sub PLUMBER {
  263. select(STDERR);
  264. print "oops, apperent pager foulup\n";
  265. $isatty++;
  266. &usage(1);
  267. }
  268. sub usage {
  269. local($oops) = @_;
  270. unless (-t STDOUT) {
  271. select(STDERR);
  272. } elsif (!$oops) {
  273. $isatty++;
  274. $| = 1;
  275. print "hit <RETURN> for further explanation: ";
  276. <STDIN>;
  277. open (PIPE, "|". ($ENV{PAGER} || 'more'));
  278. $SIG{PIPE} = PLUMBER;
  279. select(PIPE);
  280. }
  281. print "usage: $0 [-dpnP] [var=val] [files ...]\n";
  282. exit unless $isatty;
  283. print <<EOF;
  284. Options:
  285. -w wide; short for: type_width=45 member_width=35 offset_width=8
  286. -x hex; short for: offset_fmt=x offset_width=08 size_fmt=x size_width=04
  287. -n do not generate perl code (default when invoked as pstruct)
  288. -p generate perl code (default when invoked as c2ph)
  289. -v generate perl code, with C decls as comments
  290. -i do NOT recompute sizes for intrinsic datatypes
  291. -a dump information on intrinsics also
  292. -t trace execution
  293. -d spew reams of debugging output
  294. -slist give comma-separated list a structures to dump
  295. Var Name Default Value Meaning
  296. EOF
  297. &defvar('CC', 'which_compiler to call');
  298. &defvar('CFLAGS', 'how to generate *.s files with stabs');
  299. &defvar('DEFINES','any extra cflags or cpp defines, like -I, -D, -U');
  300. print "\n";
  301. &defvar('type_width', 'width of type field (column 1)');
  302. &defvar('member_width', 'width of member field (column 2)');
  303. &defvar('offset_width', 'width of offset field (column 3)');
  304. &defvar('size_width', 'width of size field (column 4)');
  305. print "\n";
  306. &defvar('offset_fmt', 'sprintf format type for offset');
  307. &defvar('size_fmt', 'sprintf format type for size');
  308. print "\n";
  309. &defvar('indent', 'how far to indent each nesting level');
  310. print <<'EOF';
  311. If any *.[ch] files are given, these will be catted together into
  312. a temporary *.c file and sent through:
  313. $CC $CFLAGS $DEFINES
  314. and the resulting *.s groped for stab information. If no files are
  315. supplied, then stdin is read directly with the assumption that it
  316. contains stab information. All other liens will be ignored. At
  317. most one *.s file should be supplied.
  318. EOF
  319. close PIPE;
  320. exit 1;
  321. }
  322. sub defvar {
  323. local($var, $msg) = @_;
  324. printf "%-16s%-15s %s\n", $var, eval "\$$var", $msg;
  325. }
  326. $recurse = 1;
  327. if (@ARGV) {
  328. if (grep(!/\.[csh]$/,@ARGV)) {
  329. warn "Only *.[csh] files expected!\n";
  330. &usage;
  331. }
  332. elsif (grep(/\.s$/,@ARGV)) {
  333. if (@ARGV > 1) {
  334. warn "Only one *.s file allowed!\n";
  335. &usage;
  336. }
  337. }
  338. elsif (@ARGV == 1 && $ARGV[0] =~ /\.c$/) {
  339. local($dir, $file) = $ARGV[0] =~ m#(.*/)?(.*)$#;
  340. $chdir = "cd $dir; " if $dir;
  341. &system("$chdir$CC $CFLAGS $DEFINES $file") && exit 1;
  342. $ARGV[0] =~ s/\.c$/.s/;
  343. }
  344. else {
  345. $TMP = "/tmp/c2ph.$$.c";
  346. &system("cat @ARGV > $TMP") && exit 1;
  347. &system("cd /tmp; $CC $CFLAGS $DEFINES $TMP") && exit 1;
  348. unlink $TMP;
  349. $TMP =~ s/\.c$/.s/;
  350. @ARGV = ($TMP);
  351. }
  352. }
  353. if ($opt_s) {
  354. for (split(/[\s,]+/, $opt_s)) {
  355. $interested{$_}++;
  356. }
  357. }
  358. $| = 1 if $debug;
  359. main: {
  360. if ($trace) {
  361. if (-t && !@ARGV) {
  362. print STDERR "reading from your keyboard: ";
  363. } else {
  364. print STDERR "reading from " . (@ARGV ? "@ARGV" : "<STDIN>").": ";
  365. }
  366. }
  367. STAB: while (<>) {
  368. if ($trace && !($. % 10)) {
  369. $lineno = $..'';
  370. print STDERR $lineno, "\b" x length($lineno);
  371. }
  372. next unless /^\s*\.stabs\s+/;
  373. $line = $_;
  374. s/^\s*\.stabs\s+//;
  375. if (s/\\\\"[d,]+$//) {
  376. $saveline .= $line;
  377. $savebar = $_;
  378. next STAB;
  379. }
  380. if ($saveline) {
  381. s/^"//;
  382. $_ = $savebar . $_;
  383. $line = $saveline;
  384. }
  385. &stab;
  386. $savebar = $saveline = undef;
  387. }
  388. print STDERR "$.\n" if $trace;
  389. unlink $TMP if $TMP;
  390. &compute_intrinsics if $perl && !$opt_i;
  391. print STDERR "resolving types\n" if $trace;
  392. &resolve_types;
  393. &adjust_start_addrs;
  394. $sum = 2 + $type_width + $member_width;
  395. $pmask1 = "%-${type_width}s %-${member_width}s";
  396. $pmask2 = "%-${sum}s %${offset_width}${offset_fmt}%s %${size_width}${size_fmt}%s";
  397. if ($perl) {
  398. # resolve template -- should be in stab define order, but even this isn't enough.
  399. print STDERR "\nbuilding type templates: " if $trace;
  400. for $i (reverse 0..$#type) {
  401. next unless defined($name = $type[$i]);
  402. next unless defined $struct{$name};
  403. ($iname = $name) =~ s/\..*//;
  404. $build_recursed = 0;
  405. &build_template($name) unless defined $template{&psou($name)} ||
  406. $opt_s && !$interested{$iname};
  407. }
  408. print STDERR "\n\n" if $trace;
  409. }
  410. print STDERR "dumping structs: " if $trace;
  411. local($iam);
  412. foreach $name (sort keys %struct) {
  413. ($iname = $name) =~ s/\..*//;
  414. next if $opt_s && !$interested{$iname};
  415. print STDERR "$name " if $trace;
  416. undef @sizeof;
  417. undef @typedef;
  418. undef @offsetof;
  419. undef @indices;
  420. undef @typeof;
  421. undef @fieldnames;
  422. $mname = &munge($name);
  423. $fname = &psou($name);
  424. print "# " if $perl && $verbose;
  425. $pcode = '';
  426. print "$fname {\n" if !$perl || $verbose;
  427. $template{$fname} = &scrunch($template{$fname}) if $perl;
  428. &pstruct($name,$name,0);
  429. print "# " if $perl && $verbose;
  430. print "}\n" if !$perl || $verbose;
  431. print "\n" if $perl && $verbose;
  432. if ($perl) {
  433. print "$pcode";
  434. printf("\nsub %-32s { %4d; }\n\n", "${mname}'struct", $countof{$name});
  435. print <<EOF;
  436. sub ${mname}'typedef {
  437. local(\$${mname}'index) = shift;
  438. defined \$${mname}'index
  439. ? \$${mname}'typedef[\$${mname}'index]
  440. : \$${mname}'typedef;
  441. }
  442. EOF
  443. print <<EOF;
  444. sub ${mname}'sizeof {
  445. local(\$${mname}'index) = shift;
  446. defined \$${mname}'index
  447. ? \$${mname}'sizeof[\$${mname}'index]
  448. : \$${mname}'sizeof;
  449. }
  450. EOF
  451. print <<EOF;
  452. sub ${mname}'offsetof {
  453. local(\$${mname}'index) = shift;
  454. defined \$${mname}index
  455. ? \$${mname}'offsetof[\$${mname}'index]
  456. : \$${mname}'sizeof;
  457. }
  458. EOF
  459. print <<EOF;
  460. sub ${mname}'typeof {
  461. local(\$${mname}'index) = shift;
  462. defined \$${mname}index
  463. ? \$${mname}'typeof[\$${mname}'index]
  464. : '$name';
  465. }
  466. EOF
  467. print <<EOF;
  468. sub ${mname}'fieldnames {
  469. \@${mname}'fieldnames;
  470. }
  471. EOF
  472. $iam = ($isastruct{$name} && 's') || ($isaunion{$name} && 'u');
  473. print <<EOF;
  474. sub ${mname}'isastruct {
  475. '$iam';
  476. }
  477. EOF
  478. print "\$${mname}'typedef = '" . &scrunch($template{$fname})
  479. . "';\n";
  480. print "\$${mname}'sizeof = $sizeof{$name};\n\n";
  481. print "\@${mname}'indices = (", &squishseq(@indices), ");\n";
  482. print "\n";
  483. print "\@${mname}'typedef[\@${mname}'indices] = (",
  484. join("\n\t", '', @typedef), "\n );\n\n";
  485. print "\@${mname}'sizeof[\@${mname}'indices] = (",
  486. join("\n\t", '', @sizeof), "\n );\n\n";
  487. print "\@${mname}'offsetof[\@${mname}'indices] = (",
  488. join("\n\t", '', @offsetof), "\n );\n\n";
  489. print "\@${mname}'typeof[\@${mname}'indices] = (",
  490. join("\n\t", '', @typeof), "\n );\n\n";
  491. print "\@${mname}'fieldnames[\@${mname}'indices] = (",
  492. join("\n\t", '', @fieldnames), "\n );\n\n";
  493. $template_printed{$fname}++;
  494. $size_printed{$fname}++;
  495. }
  496. print "\n";
  497. }
  498. print STDERR "\n" if $trace;
  499. unless ($perl && $opt_a) {
  500. print "\n1;\n" if $perl;
  501. exit;
  502. }
  503. foreach $name (sort bysizevalue keys %intrinsics) {
  504. next if $size_printed{$name};
  505. print '$',&munge($name),"'sizeof = ", $sizeof{$name}, ";\n";
  506. }
  507. print "\n";
  508. sub bysizevalue { $sizeof{$a} <=> $sizeof{$b}; }
  509. foreach $name (sort keys %intrinsics) {
  510. print '$',&munge($name),"'typedef = '", $template{$name}, "';\n";
  511. }
  512. print "\n1;\n" if $perl;
  513. exit;
  514. }
  515. ########################################################################################
  516. sub stab {
  517. next unless $continued || /:[\$\w]+(\(\d+,\d+\))?=[\*\$\w]+/; # (\d+,\d+) is for sun
  518. s/"// || next;
  519. s/",([x\d]+),([x\d]+),([x\d]+),.*// || next;
  520. next if /^\s*$/;
  521. $size = $3 if $3;
  522. $_ = $continued . $_ if length($continued);
  523. if (s/\\\\$//) {
  524. # if last 2 chars of string are '\\' then stab is continued
  525. # in next stab entry
  526. chop;
  527. $continued = $_;
  528. next;
  529. }
  530. $continued = '';
  531. $line = $_;
  532. if (($name, $pdecl) = /^([\$ \w]+):[tT]((\d+)(=[rufs*](\d+))+)$/) {
  533. print "$name is a typedef for some funky pointers: $pdecl\n" if $debug;
  534. &pdecl($pdecl);
  535. next;
  536. }
  537. if (/(([ \w]+):t(\d+|\(\d+,\d+\)))=r?(\d+|\(\d+,\d+\))(;\d+;\d+;)?/) {
  538. local($ident) = $2;
  539. push(@intrinsics, $ident);
  540. $typeno = &typeno($3);
  541. $type[$typeno] = $ident;
  542. print STDERR "intrinsic $ident in new type $typeno\n" if $debug;
  543. next;
  544. }
  545. if (($name, $typeordef, $typeno, $extra, $struct, $_)
  546. = /^([\$ \w]+):([ustT])(\d+|\(\d+,\d+\))(=[rufs*](\d+))?(.*)$/)
  547. {
  548. $typeno = &typeno($typeno); # sun foolery
  549. }
  550. elsif (/^[\$\w]+:/) {
  551. next; # variable
  552. }
  553. else {
  554. warn "can't grok stab: <$_> in: $line " if $_;
  555. next;
  556. }
  557. #warn "got size $size for $name\n";
  558. $sizeof{$name} = $size if $size;
  559. s/;[-\d]*;[-\d]*;$//; # we don't care about ranges
  560. $typenos{$name} = $typeno;
  561. unless (defined $type[$typeno]) {
  562. &panic("type 0??") unless $typeno;
  563. $type[$typeno] = $name unless defined $type[$typeno];
  564. printf "new type $typeno is $name" if $debug;
  565. if ($extra =~ /\*/ && defined $type[$struct]) {
  566. print ", a typedef for a pointer to " , $type[$struct] if $debug;
  567. }
  568. } else {
  569. printf "%s is type %d", $name, $typeno if $debug;
  570. print ", a typedef for " , $type[$typeno] if $debug;
  571. }
  572. print "\n" if $debug;
  573. #next unless $extra =~ /[su*]/;
  574. #$type[$struct] = $name;
  575. if ($extra =~ /[us*]/) {
  576. &sou($name, $extra);
  577. $_ = &sdecl($name, $_, 0);
  578. }
  579. elsif (/^=ar/) {
  580. print "it's a bare array typedef -- that's pretty sick\n" if $debug;
  581. $_ = "$typeno$_";
  582. $scripts = '';
  583. $_ = &adecl($_,1);
  584. }
  585. elsif (s/((\w+):t(\d+|\(\d+,\d+\)))?=r?(;\d+;\d+;)?//) { # the ?'s are for gcc
  586. push(@intrinsics, $2);
  587. $typeno = &typeno($3);
  588. $type[$typeno] = $2;
  589. print STDERR "intrinsic $2 in new type $typeno\n" if $debug;
  590. }
  591. elsif (s/^=e//) { # blessed be thy compiler; mine won't do this
  592. &edecl;
  593. }
  594. else {
  595. warn "Funny remainder for $name on line $_ left in $line " if $_;
  596. }
  597. }
  598. sub typeno { # sun thinks types are (0,27) instead of just 27
  599. local($_) = @_;
  600. s/\(\d+,(\d+)\)/$1/;
  601. $_;
  602. }
  603. sub pstruct {
  604. local($what,$prefix,$base) = @_;
  605. local($field, $fieldname, $typeno, $count, $offset, $entry);
  606. local($fieldtype);
  607. local($type, $tname);
  608. local($mytype, $mycount, $entry2);
  609. local($struct_count) = 0;
  610. local($pad, $revpad, $length, $prepad, $lastoffset, $lastlength, $fmt);
  611. local($bits,$bytes);
  612. local($template);
  613. local($mname) = &munge($name);
  614. sub munge {
  615. local($_) = @_;
  616. s/[\s\$\.]/_/g;
  617. $_;
  618. }
  619. local($sname) = &psou($what);
  620. $nesting++;
  621. for $field (split(/;/, $struct{$what})) {
  622. $pad = $prepad = 0;
  623. $entry = '';
  624. ($fieldname, $typeno, $count, $offset, $length) = split(/,/, $field);
  625. $type = $type[$typeno];
  626. $type =~ /([^[]*)(\[.*\])?/;
  627. $mytype = $1;
  628. $count .= $2;
  629. $fieldtype = &psou($mytype);
  630. local($fname) = &psou($name);
  631. if ($build_templates) {
  632. $pad = ($offset - ($lastoffset + $lastlength))/8
  633. if defined $lastoffset;
  634. if (! $finished_template{$sname}) {
  635. if ($isaunion{$what}) {
  636. $template{$sname} .= 'X' x $revpad . ' ' if $revpad;
  637. } else {
  638. $template{$sname} .= 'x' x $pad . ' ' if $pad;
  639. }
  640. }
  641. $template = &fetch_template($type);
  642. &repeat_template($template,$count);
  643. if (! $finished_template{$sname}) {
  644. $template{$sname} .= $template;
  645. }
  646. $revpad = $length/8 if $isaunion{$what};
  647. ($lastoffset, $lastlength) = ($offset, $length);
  648. } else {
  649. print '# ' if $perl && $verbose;
  650. $entry = sprintf($pmask1,
  651. ' ' x ($nesting * $indent) . $fieldtype,
  652. "$prefix.$fieldname" . $count);
  653. $entry =~ s/(\*+)( )/$2$1/;
  654. printf $pmask2,
  655. $entry,
  656. ($base+$offset)/8,
  657. ($bits = ($base+$offset)%8) ? ".$bits" : " ",
  658. $length/8,
  659. ($bits = $length % 8) ? ".$bits": ""
  660. if !$perl || $verbose;
  661. if ($perl) {
  662. $template = &fetch_template($type);
  663. &repeat_template($template,$count);
  664. }
  665. if ($perl && $nesting == 1) {
  666. push(@sizeof, int($length/8) .",\t# $fieldname");
  667. push(@offsetof, int($offset/8) .",\t# $fieldname");
  668. local($little) = &scrunch($template);
  669. push(@typedef, "'$little', \t# $fieldname");
  670. $type =~ s/(struct|union) //;
  671. push(@typeof, "'$mytype" . ($count ? $count : '') .
  672. "',\t# $fieldname");
  673. push(@fieldnames, "'$fieldname',");
  674. }
  675. print ' ', ' ' x $indent x $nesting, $template
  676. if $perl && $verbose;
  677. print "\n" if !$perl || $verbose;
  678. }
  679. if ($perl) {
  680. local($mycount) = defined $struct{$mytype} ? $countof{$mytype} : 1;
  681. $mycount *= &scripts2count($count) if $count;
  682. if ($nesting==1 && !$build_templates) {
  683. $pcode .= sprintf("sub %-32s { %4d; }\n",
  684. "${mname}'${fieldname}", $struct_count);
  685. push(@indices, $struct_count);
  686. }
  687. $struct_count += $mycount;
  688. }
  689. &pstruct($type, "$prefix.$fieldname", $base+$offset)
  690. if $recurse && defined $struct{$type};
  691. }
  692. $countof{$what} = $struct_count unless defined $countof{$whati};
  693. $template{$sname} .= '$' if $build_templates;
  694. $finished_template{$sname}++;
  695. if ($build_templates && !defined $sizeof{$name}) {
  696. local($fmt) = &scrunch($template{$sname});
  697. print STDERR "no size for $name, punting with $fmt..." if $debug;
  698. eval '$sizeof{$name} = length(pack($fmt, ()))';
  699. if ($@) {
  700. chop $@;
  701. warn "couldn't get size for \$name: $@";
  702. } else {
  703. print STDERR $sizeof{$name}, "\n" if $debUg;
  704. }
  705. }
  706. --$nesting;
  707. }
  708. sub psize {
  709. local($me) = @_;
  710. local($amstruct) = $struct{$me} ? 'struct ' : '';
  711. print '$sizeof{\'', $amstruct, $me, '\'} = ';
  712. printf "%d;\n", $sizeof{$me};
  713. }
  714. sub pdecl {
  715. local($pdecl) = @_;
  716. local(@pdecls);
  717. local($tname);
  718. warn "pdecl: $pdecl\n" if $debug;
  719. $pdecl =~ s/\(\d+,(\d+)\)/$1/g;
  720. $pdecl =~ s/\*//g;
  721. @pdecls = split(/=/, $pdecl);
  722. $typeno = $pdecls[0];
  723. $tname = pop @pdecls;
  724. if ($tname =~ s/^f//) { $tname = "$tname&"; }
  725. #else { $tname = "$tname*"; }
  726. for (reverse @pdecls) {
  727. $tname .= s/^f// ? "&" : "*";
  728. #$tname =~ s/^f(.*)/$1&/;
  729. print "type[$_] is $tname\n" if $debug;
  730. $type[$_] = $tname unless defined $type[$_];
  731. }
  732. }
  733. sub adecl {
  734. ($arraytype, $unknown, $lower, $upper) = ();
  735. #local($typeno);
  736. # global $typeno, @type
  737. local($_, $typedef) = @_;
  738. while (s/^((\d+|\(\d+,\d+\))=)?ar(\d+|\(\d+,\d+\));//) {
  739. ($arraytype, $unknown) = ($2, $3);
  740. $arraytype = &typeno($arraytype);
  741. $unknown = &typeno($unknown);
  742. if (s/^(\d+);(\d+);//) {
  743. ($lower, $upper) = ($1, $2);
  744. $scripts .= '[' . ($upper+1) . ']';
  745. } else {
  746. warn "can't find array bounds: $_";
  747. }
  748. }
  749. if (s/^([(,)\d*f=]*),(\d+),(\d+);//) {
  750. ($start, $length) = ($2, $3);
  751. $whatis = $1;
  752. if ($whatis =~ /^(\d+|\(\d+,\d+\))=/) {
  753. $typeno = &typeno($1);
  754. &pdecl($whatis);
  755. } else {
  756. $typeno = &typeno($whatis);
  757. }
  758. } elsif (s/^(\d+)(=[*suf]\d*)//) {
  759. local($whatis) = $2;
  760. if ($whatis =~ /[f*]/) {
  761. &pdecl($whatis);
  762. } elsif ($whatis =~ /[su]/) { #
  763. print "$prefix.$fieldname is an array$scripts anon structs; disgusting\n"
  764. if $debug;
  765. #$type[$typeno] = $name unless defined $type[$typeno];
  766. ##printf "new type $typeno is $name" if $debug;
  767. $typeno = $1;
  768. $type[$typeno] = "$prefix.$fieldname";
  769. local($name) = $type[$typeno];
  770. &sou($name, $whatis);
  771. $_ = &sdecl($name, $_, $start+$offset);
  772. 1;
  773. $start = $start{$name};
  774. $offset = $sizeof{$name};
  775. $length = $offset;
  776. } else {
  777. warn "what's this? $whatis in $line ";
  778. }
  779. } elsif (/^\d+$/) {
  780. $typeno = $_;
  781. } else {
  782. warn "bad array stab: $_ in $line ";
  783. next STAB;
  784. }
  785. #local($wasdef) = defined($type[$typeno]) && $debug;
  786. #if ($typedef) {
  787. #print "redefining $type[$typeno] to " if $wasdef;
  788. #$type[$typeno] = "$whatis$scripts"; # unless defined $type[$typeno];
  789. #print "$type[$typeno]\n" if $wasdef;
  790. #} else {
  791. #$type[$arraytype] = $type[$typeno] unless defined $type[$arraytype];
  792. #}
  793. $type[$arraytype] = "$type[$typeno]$scripts" if defined $type[$typeno];
  794. print "type[$arraytype] is $type[$arraytype]\n" if $debug;
  795. print "$prefix.$fieldname is an array of $type[$arraytype]\n" if $debug;
  796. $_;
  797. }
  798. sub sdecl {
  799. local($prefix, $_, $offset) = @_;
  800. local($fieldname, $scripts, $type, $arraytype, $unknown,
  801. $whatis, $pdecl, $upper,$lower, $start,$length) = ();
  802. local($typeno,$sou);
  803. SFIELD:
  804. while (/^([^;]+);/) {
  805. $scripts = '';
  806. warn "sdecl $_\n" if $debug;
  807. if (s/^([\$\w]+)://) {
  808. $fieldname = $1;
  809. } elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { #
  810. $typeno = &typeno($1);
  811. $type[$typeno] = "$prefix.$fieldname";
  812. local($name) = "$prefix.$fieldname";
  813. &sou($name,$2);
  814. $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  815. $start = $start{$name};
  816. $offset += $sizeof{$name};
  817. #print "done with anon, start is $start, offset is $offset\n";
  818. #next SFIELD;
  819. } else {
  820. warn "weird field $_ of $line" if $debug;
  821. next STAB;
  822. #$fieldname = &gensym;
  823. #$_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  824. }
  825. if (/^(\d+|\(\d+,\d+\))=ar/) {
  826. $_ = &adecl($_);
  827. }
  828. elsif (s/^(\d+|\(\d+,\d+\))?,(\d+),(\d+);//) {
  829. ($start, $length) = ($2, $3);
  830. &panic("no length?") unless $length;
  831. $typeno = &typeno($1) if $1;
  832. }
  833. elsif (s/^(\d+)=xs\w+:,(\d+),(\d+);//) {
  834. ($start, $length) = ($2, $3);
  835. &panic("no length?") unless $length;
  836. $typeno = &typeno($1) if $1;
  837. }
  838. elsif (s/^((\d+|\(\d+,\d+\))(=[*f](\d+|\(\d+,\d+\)))+),(\d+),(\d+);//) {
  839. ($pdecl, $start, $length) = ($1,$5,$6);
  840. &pdecl($pdecl);
  841. }
  842. elsif (s/(\d+)=([us])(\d+|\(\d+,\d+\))//) { # the dratted anon struct
  843. ($typeno, $sou) = ($1, $2);
  844. $typeno = &typeno($typeno);
  845. if (defined($type[$typeno])) {
  846. warn "now how did we get type $1 in $fieldname of $line?";
  847. } else {
  848. print "anon type $typeno is $prefix.$fieldname\n" if $debug;
  849. $type[$typeno] = "$prefix.$fieldname" unless defined $type[$typeno];
  850. };
  851. local($name) = "$prefix.$fieldname";
  852. &sou($name,$sou);
  853. print "anon ".($isastruct{$name}) ? "struct":"union"." for $prefix.$fieldname\n" if $debug;
  854. $type[$typeno] = "$prefix.$fieldname";
  855. $_ = &sdecl("$prefix.$fieldname", $_, $start+$offset);
  856. $start = $start{$name};
  857. $length = $sizeof{$name};
  858. }
  859. else {
  860. warn "can't grok stab for $name ($_) in line $line ";
  861. next STAB;
  862. }
  863. &panic("no length for $prefix.$fieldname") unless $length;
  864. $struct{$name} .= join(',', $fieldname, $typeno, $scripts, $start, $length) . ';';
  865. }
  866. if (s/;\d*,(\d+),(\d+);//) {
  867. local($start, $size) = ($1, $2);
  868. $sizeof{$prefix} = $size;
  869. print "start of $prefix is $start, size of $sizeof{$prefix}\n" if $debug;
  870. $start{$prefix} = $start;
  871. }
  872. $_;
  873. }
  874. sub edecl {
  875. s/;$//;
  876. $enum{$name} = $_;
  877. $_ = '';
  878. }
  879. sub resolve_types {
  880. local($sou);
  881. for $i (0 .. $#type) {
  882. next unless defined $type[$i];
  883. $_ = $type[$i];
  884. unless (/\d/) {
  885. print "type[$i] $type[$i]\n" if $debug;
  886. next;
  887. }
  888. print "type[$i] $_ ==> " if $debug;
  889. s/^(\d+)(\**)\&\*(\**)/"$2($3".&type($1) . ')()'/e;
  890. s/^(\d+)\&/&type($1)/e;
  891. s/^(\d+)/&type($1)/e;
  892. s/(\*+)([^*]+)(\*+)/$1$3$2/;
  893. s/\((\*+)(\w+)(\*+)\)/$3($1$2)/;
  894. s/^(\d+)([\*\[].*)/&type($1).$2/e;
  895. #s/(\d+)(\*|(\[[\[\]\d\*]+]\])+)/&type($1).$2/ge;
  896. $type[$i] = $_;
  897. print "$_\n" if $debug;
  898. }
  899. }
  900. sub type { &psou($type[$_[0]] || "<UNDEFINED>"); }
  901. sub adjust_start_addrs {
  902. for (sort keys %start) {
  903. ($basename = $_) =~ s/\.[^.]+$//;
  904. $start{$_} += $start{$basename};
  905. print "start: $_ @ $start{$_}\n" if $debug;
  906. }
  907. }
  908. sub sou {
  909. local($what, $_) = @_;
  910. /u/ && $isaunion{$what}++;
  911. /s/ && $isastruct{$what}++;
  912. }
  913. sub psou {
  914. local($what) = @_;
  915. local($prefix) = '';
  916. if ($isaunion{$what}) {
  917. $prefix = 'union ';
  918. } elsif ($isastruct{$what}) {
  919. $prefix = 'struct ';
  920. }
  921. $prefix . $what;
  922. }
  923. sub scrunch {
  924. local($_) = @_;
  925. return '' if $_ eq '';
  926. study;
  927. s/\$//g;
  928. s/ / /g;
  929. 1 while s/(\w) \1/$1$1/g;
  930. # i wanna say this, but perl resists my efforts:
  931. # s/(\w)(\1+)/$2 . length($1)/ge;
  932. &quick_scrunch;
  933. s/ $//;
  934. $_;
  935. }
  936. sub buildscrunchlist {
  937. $scrunch_code = "sub quick_scrunch {\n";
  938. for (values %intrinsics) {
  939. $scrunch_code .= "\ts/(${_}{2,})/'$_' . length(\$1)/ge;\n";
  940. }
  941. $scrunch_code .= "}\n";
  942. print "$scrunch_code" if $debug;
  943. eval $scrunch_code;
  944. &panic("can't eval scrunch_code $@ \nscrunch_code") if $@;
  945. }
  946. sub fetch_template {
  947. local($mytype) = @_;
  948. local($fmt);
  949. local($count) = 1;
  950. &panic("why do you care?") unless $perl;
  951. if ($mytype =~ s/(\[\d+\])+$//) {
  952. $count .= $1;
  953. }
  954. if ($mytype =~ /\*/) {
  955. $fmt = $template{'pointer'};
  956. }
  957. elsif (defined $template{$mytype}) {
  958. $fmt = $template{$mytype};
  959. }
  960. elsif (defined $struct{$mytype}) {
  961. if (!defined $template{&psou($mytype)}) {
  962. &build_template($mytype) unless $mytype eq $name;
  963. }
  964. elsif ($template{&psou($mytype)} !~ /\$$/) {
  965. #warn "incomplete template for $mytype\n";
  966. }
  967. $fmt = $template{&psou($mytype)} || '?';
  968. }
  969. else {
  970. warn "unknown fmt for $mytype\n";
  971. $fmt = '?';
  972. }
  973. $fmt x $count . ' ';
  974. }
  975. sub compute_intrinsics {
  976. local($TMP) = "/tmp/c2ph-i.$$.c";
  977. open (TMP, ">$TMP") || die "can't open $TMP: $!";
  978. select(TMP);
  979. print STDERR "computing intrinsic sizes: " if $trace;
  980. undef %intrinsics;
  981. print <<'EOF';
  982. main() {
  983. char *mask = "%d %s\n";
  984. EOF
  985. for $type (@intrinsics) {
  986. next if !$type || $type eq 'void' || $type =~ /complex/; # sun stuff
  987. print <<"EOF";
  988. printf(mask,sizeof($type), "$type");
  989. EOF
  990. }
  991. print <<'EOF';
  992. printf(mask,sizeof(char *), "pointer");
  993. exit(0);
  994. }
  995. EOF
  996. close TMP;
  997. select(STDOUT);
  998. open(PIPE, "cd /tmp && $CC $TMP && /tmp/a.out|");
  999. while (<PIPE>) {
  1000. chop;
  1001. split(' ',$_,2);;
  1002. print "intrinsic $_[1] is size $_[0]\n" if $debug;
  1003. $sizeof{$_[1]} = $_[0];
  1004. $intrinsics{$_[1]} = $template{$_[0]};
  1005. }
  1006. close(PIPE) || die "couldn't read intrinsics!";
  1007. unlink($TMP, '/tmp/a.out');
  1008. print STDERR "done\n" if $trace;
  1009. }
  1010. sub scripts2count {
  1011. local($_) = @_;
  1012. s/^\[//;
  1013. s/\]$//;
  1014. s/\]\[/*/g;
  1015. $_ = eval;
  1016. &panic("$_: $@") if $@;
  1017. $_;
  1018. }
  1019. sub system {
  1020. print STDERR "@_\n" if $trace;
  1021. system @_;
  1022. }
  1023. sub build_template {
  1024. local($name) = @_;
  1025. &panic("already got a template for $name") if defined $template{$name};
  1026. local($build_templates) = 1;
  1027. local($lparen) = '(' x $build_recursed;
  1028. local($rparen) = ')' x $build_recursed;
  1029. print STDERR "$lparen$name$rparen " if $trace;
  1030. $build_recursed++;
  1031. &pstruct($name,$name,0);
  1032. print STDERR "TEMPLATE for $name is ", $template{&psou($name)}, "\n" if $debug;
  1033. --$build_recursed;
  1034. }
  1035. sub panic {
  1036. select(STDERR);
  1037. print "\npanic: @_\n";
  1038. exit 1 if $] <= 4.003; # caller broken
  1039. local($i,$_);
  1040. local($p,$f,$l,$s,$h,$a,@a,@sub);
  1041. for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  1042. @a = @DB'args;
  1043. for (@a) {
  1044. if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  1045. $_ = sprintf("%s",$_);
  1046. }
  1047. else {
  1048. s/'/\\'/g;
  1049. s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  1050. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1051. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1052. }
  1053. }
  1054. $w = $w ? '@ = ' : '$ = ';
  1055. $a = $h ? '(' . join(', ', @a) . ')' : '';
  1056. push(@sub, "$w&$s$a from file $f line $l\n");
  1057. last if $signal;
  1058. }
  1059. for ($i=0; $i <= $#sub; $i++) {
  1060. last if $signal;
  1061. print $sub[$i];
  1062. }
  1063. exit 1;
  1064. }
  1065. sub squishseq {
  1066. local($num);
  1067. local($last) = -1e8;
  1068. local($string);
  1069. local($seq) = '..';
  1070. while (defined($num = shift)) {
  1071. if ($num == ($last + 1)) {
  1072. $string .= $seq unless $inseq++;
  1073. $last = $num;
  1074. next;
  1075. } elsif ($inseq) {
  1076. $string .= $last unless $last == -1e8;
  1077. }
  1078. $string .= ',' if defined $string;
  1079. $string .= $num;
  1080. $last = $num;
  1081. $inseq = 0;
  1082. }
  1083. $string .= $last if $inseq && $last != -e18;
  1084. $string;
  1085. }
  1086. sub repeat_template {
  1087. # local($template, $scripts) = @_; have to change caller's values
  1088. if ( $_[1] ) {
  1089. local($ncount) = &scripts2count($_[1]);
  1090. if ($_[0] =~ /^\s*c\s*$/i) {
  1091. $_[0] = "A$ncount ";
  1092. $_[1] = '';
  1093. } else {
  1094. $_[0] = $template x $ncount;
  1095. }
  1096. }
  1097. }
  1098. __END__
  1099. :endofperl