blob: 841396a906cd218944af3deeef57bd2a512fd093 [file] [log] [blame]
thughesd64e87d2004-11-02 09:45:21 +00001#!/usr/bin/perl -w
fitzhardinge7e343cd2003-12-16 02:14:00 +00002
jseward2886b0e2004-01-04 03:46:11 +00003# This file is part of Valgrind, an extensible x86 protected-mode
4# emulator for monitoring program execution on x86-Unixes.
5#
6# Copyright (C) 2000-2004 Julian Seward
7# jseward@acm.org
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; either version 2 of the
12# License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22# 02111-1307, USA.
23#
24# The GNU General Public License is contained in the file COPYING.
25
nethercoted1c71402004-01-04 13:08:56 +000026use strict;
jseward2886b0e2004-01-04 03:46:11 +000027
fitzhardinge7e343cd2003-12-16 02:14:00 +000028my $output = shift @ARGV;
29my $indent = "";
30my $headerguard;
31my $include;
32my $passcomment = 1;
nethercoted1c71402004-01-04 13:08:56 +000033my $pre;
34my $post;
35my $generate;
fitzhardinge7e343cd2003-12-16 02:14:00 +000036
37my $struct = "VG_(tool_interface)";
38
njn26f02512004-11-22 18:33:15 +000039my %pfxmap = ("track" => "TL_",
40 "tool" => "TL_",
41 "malloc"=> "TL_",
fitzhardinge7e343cd2003-12-16 02:14:00 +000042 );
43
44sub getargnames(@) {
45 my @args = @_;
46 my @ret;
47
48 foreach my $a (@args) {
49 my @pieces = split /\s+/, $a;
50 my $name = pop @pieces;
51 push @ret, $name unless $name eq "void";
52 }
53 return @ret;
54}
55
56sub getargtypes(@) {
57 my @args = @_;
58 my @ret;
59
60 foreach my $a (@args) {
61 my @pieces = split /\s+/, $a;
62 pop @pieces;
63 push @ret, (join " ", @pieces);
64 }
65 @ret = "void" if ($#ret == -1);
66 return @ret;
67}
68
69# Different output modes
70if ($output eq "callwrap") {
nethercotef1e5e152004-09-01 23:58:16 +000071 $include = "core.h";
fitzhardinge7e343cd2003-12-16 02:14:00 +000072 $generate = sub ($$$@) {
73 my ($pfx, $ret, $func, @args) = @_;
74 my $args = join ", ", @args;
75 my $argnames = join ", ", getargnames(@args);
76 print "$ret $pfxmap{$pfx}($func)($args)\n{\n";
77 print " return (*$struct.${pfx}_$func)($argnames);\n";
78 print "}\n";
79 }
80} elsif ($output eq "proto") {
nethercotef1e5e152004-09-01 23:58:16 +000081 $include = "core.h";
fitzhardinge7e343cd2003-12-16 02:14:00 +000082 $generate = sub ($$$@) {
83 my ($pfx, $ret, $func, @args) = @_;
84 my $args = join ', ', @args;
85
86 print "$ret $pfxmap{$pfx}($func)($args);\n";
87 print "Bool VG_(defined_$func)(void);\n";
88 }
89} elsif ($output eq "toolproto") {
90 $generate = sub ($$$@) {
91 my ($pfx, $ret, $func, @args) = @_;
92 my $args = join ', ', @args;
93
94 print "$ret $pfxmap{$pfx}($func)($args);\n";
95 }
96} elsif ($output eq "missingfuncs") {
nethercotef1e5e152004-09-01 23:58:16 +000097 $include = "core.h";
fitzhardinge7e343cd2003-12-16 02:14:00 +000098 $generate = sub ($$$@) {
99 my ($pfx, $ret, $func, @args) = @_;
100 my $args = join ", ", @args;
101
102 print "static $ret missing_${pfx}_$func($args) {\n";
103 print " VG_(missing_tool_func)(\"${pfx}_$func\");\n";
104 print "}\n";
105 print "Bool VG_(defined_$func)(void) {\n";
106 print " return $struct.${pfx}_$func != missing_${pfx}_$func;\n";
107 print "}\n\n";
108 };
109 $indent = " ";
110} elsif ($output eq "struct") {
nethercotef1e5e152004-09-01 23:58:16 +0000111 $include = "core.h";
fitzhardinge7e343cd2003-12-16 02:14:00 +0000112 $pre = sub () {
113 print "typedef struct {\n";
114 };
115 $post = sub () {
116 print "} VgToolInterface;\n\n";
117 print "extern VgToolInterface $struct;\n"
118 };
119 $generate = sub ($$$@) {
120 my ($pfx, $ret, $func, @args) = @_;
121 my $args = join ", ", @args;
122
123 print "$indent$ret (*${pfx}_$func)($args);\n";
124 };
125 $indent = " ";
126 $headerguard=$output;
127} elsif ($output eq "structdef") {
128 $include = "vg_toolint.h";
129 $pre = sub () {
130 print "VgToolInterface $struct = {\n";
131 };
132 $post = sub () {
133 print "};\n";
134 };
135 $generate = sub ($$$@) {
136 my ($pfx, $ret, $func, @args) = @_;
137
138 print "$indent.${pfx}_$func = missing_${pfx}_$func,\n"
139 };
140 $indent = " ";
141} elsif ($output eq "initfunc") {
nethercote46063202004-09-02 08:51:43 +0000142 $include = "tool.h";
fitzhardinge7e343cd2003-12-16 02:14:00 +0000143 $generate = sub ($$$@) {
144 my ($pfx, $ret, $func, @args) = @_;
145 my $args = join ", ", @args;
146 my $argnames = join ", ", getargnames(@args);
147
148 print <<EOF;
149void VG_(init_$func)($ret (*func)($args))
150{
151 if (func == NULL)
152 func = missing_${pfx}_$func;
153 if (VG_(defined_$func)())
154 VG_(printf)("Warning tool is redefining $func\\n");
njn26f02512004-11-22 18:33:15 +0000155 if (func == TL_($func))
fitzhardinge7e343cd2003-12-16 02:14:00 +0000156 VG_(printf)("Warning tool is defining $func recursively\\n");
157 $struct.${pfx}_$func = func;
158}
159EOF
160 }
161} elsif ($output eq "initproto") {
162 $generate = sub ($$$@) {
163 my ($pfx, $ret, $func, @args) = @_;
164 my $args = join ', ', @args;
165 print "void VG_(init_$func)($ret (*func)($args));\n";
166 };
167 $headerguard=$output;
168} elsif ($output eq "initdlsym") {
169 $pre = sub () {
170 print <<EOF;
171#include <dlfcn.h>
172void VG_(tool_init_dlsym)(void *dlhandle)
173{
174 void *ret;
175
176EOF
177 };
178 $post = sub () {
179 print "}\n";
180 };
181 $generate = sub ($$$@) {
182 my ($pfx, $ret, $func, @args) = @_;
183 my $args = join ", ", getargtypes(@args);
184
185 print <<EOF;
njn26f02512004-11-22 18:33:15 +0000186 ret = dlsym(dlhandle, "vgTool_$func");
fitzhardinge7e343cd2003-12-16 02:14:00 +0000187 if (ret != NULL)
188 VG_(init_$func)(($ret (*)($args))ret);
189
190EOF
191 };
192
193 $passcomment = 0;
194}
195
196die "Unknown output format \"$output\"" unless defined $generate;
197
198print "/* Generated by \"gen_toolint.pl $output\" */\n";
199
200print <<EOF if defined $headerguard;
201
202#ifndef VG_toolint_$headerguard
203#define VG_toolint_$headerguard
204
205EOF
206
207print <<EOF if defined $include;
208#include \"$include\"
209EOF
210
211&$pre() if defined $pre; # preamble
212
213my $state = "idle";
214
215my $buf;
216my $lines;
nethercoted1c71402004-01-04 13:08:56 +0000217my $prefix;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000218
219while(<STDIN>) {
220 # skip simple comments
221 next if (/^#[^#]/);
222
223 if (/^:/) {
224 s/^://;
225 chomp;
226 $prefix=$_;
227 next;
228 }
229
230 # look for inserted comments
231 if (/^##/) {
232 if ($state eq "idle") {
233 $state = "comment";
234 $lines = 1;
235 $_ =~ s,^## ,/* ,;
236 $buf = $_;
237 next;
238 } elsif ($state eq "comment") {
239 $lines++;
240 $_ =~ s,^## , ,;
241 print $indent.$buf if $passcomment;
242 $buf = $_;
243 next;
244 }
245 next;
246 }
247
248 # blank lines in a comment are part of the comment
249 if (/^\s*$/) {
250 if ($state eq "comment") {
251 $lines++;
252 print $indent.$buf if $passcomment;
253 $buf = "\n";
254 } else {
255 print "\n" if $passcomment;
256 }
257 next;
258 }
259
260 # coming out of a comment
261 if ($state eq "comment") {
262 chomp $buf;
263
264 if ($passcomment) {
265 if ($lines == 1) {
266 print "$indent$buf */\n";
267 } else {
268 print "$indent$buf\n$indent */\n";
269 }
270 }
271 $buf = "";
272 $state = "idle";
273 }
274
275 chomp;
276 my @func = split /,\s*/;
277
278 my $rettype = shift @func;
279 my $funcname = shift @func;
280
281 @func = "void" if scalar @func == 0;
282
283 &$generate ($prefix, $rettype, $funcname, @func);
284}
285
286&$post() if defined $post; # postamble
287
288print <<EOF if defined $headerguard;
289
290#endif /* VG_toolint_$headerguard */
291EOF