3 # Copyright (C) 2013-2020 Free Software Foundation, Inc.
5 # This file is part of GDB.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # make-target-delegates target.h > target-delegates.c
24 # The line we search for in target.h that marks where we should start
25 # looking for methods.
26 $TRIGGER = qr
,^struct target_ops
$,;
27 # The end of the methods part.
31 $SYMBOL = qr
,[a
-zA
-Z_
][a
-zA
-Z0
-9_
]*,;
32 # Match the name part of a method in struct target_ops.
33 $NAME_PART = qr
,(?
<name
>${SYMBOL
}+)\s
,;
34 # Match the arguments to a method.
35 $ARGS_PART = qr
,(?
<args
>\
(.*\
)),;
36 # We strip the indentation so here we only need the caret.
39 $POINTER_PART = qr
,\s
*(\
*)?\s
*,;
41 # Match a C++ symbol, including scope operators and template
42 # parameters. E.g., 'std::vector<something>'.
43 $CP_SYMBOL = qr
,[a
-zA
-Z_
][a
-zA
-Z0
-9_
<>:]*,;
44 # Match the return type when it is "ordinary".
45 $SIMPLE_RETURN_PART = qr
,((struct
|class|enum
|union
)\s
+)?
${CP_SYMBOL
}+,;
47 # Match a return type.
48 $RETURN_PART = qr
,((const
|volatile
)\s
+)?
(${SIMPLE_RETURN_PART
})${POINTER_PART
},;
51 $VIRTUAL_PART = qr
,virtual\s
,;
53 # Match the TARGET_DEFAULT_* attribute for a method.
54 $TARGET_DEFAULT_PART = qr
,TARGET_DEFAULT_
(?
<style
>[A
-Z_
]+)\s
*\
((?
<default_arg
>.*)\
),;
56 # Match the arguments and trailing attribute of a method definition.
57 # Note we don't match the trailing ";".
58 $METHOD_TRAILER = qr
,\s
*${TARGET_DEFAULT_PART
}$,;
60 # Match an entire method definition.
61 $METHOD = ($INTRO_PART . $VIRTUAL_PART . "(?<return_type>" . $RETURN_PART . ")"
62 . $NAME_PART . $ARGS_PART
65 # Match TARGET_DEBUG_PRINTER in an argument type.
66 # This must match the whole "sub-expression" including the parens.
67 # Reference $1 must refer to the function argument.
68 $TARGET_DEBUG_PRINTER = qr
,\s
*TARGET_DEBUG_PRINTER\s
*\
(([^)]*)\
)\s
*,;
79 # Read from the input files until we find the trigger line.
84 return if m/$TRIGGER/;
87 die "could not find trigger line\n";
90 # Scan target.h and return a list of possible target_ops method entries.
92 my $all_the_text = '';
97 # Skip the open brace.
107 # Now strip out the C comments.
108 $all_the_text =~ s
,/\*(.*?)\*/,,g
;
110 # Replace sequences of tabs and/or whitespace with a single
111 # whitespace character. We need the whitespace because the method
112 # may have been split between multiple lines, like e.g.:
114 # virtual std::vector<long_type_name>
115 # my_long_method_name ()
116 # TARGET_DEFAULT_IGNORE ();
118 # If we didn't preserve the whitespace, then we'd end up with:
120 # virtual std::vector<long_type_name>my_long_method_name ()TARGET_DEFAULT_IGNORE ()
122 # ... which wouldn't later be parsed correctly.
123 $all_the_text =~ s/[\t\s]+/ /g;
125 return split (/;/, $all_the_text);
128 # Parse arguments into a list.
129 sub parse_argtypes
($) {
132 $typestr =~ s/^\((.*)\)$/\1/;
134 my (@typelist) = split (/,\s*/, $typestr);
135 my (@result, $iter, $onetype);
137 foreach $iter (@typelist) {
138 if ($iter =~ m/^(enum\s+${SYMBOL}\s*)(${SYMBOL})?$/) {
140 } elsif ($iter =~ m/^(.*(enum\s+)?${SYMBOL}.*(\s|\*|&))${SYMBOL}+$/) {
142 } elsif ($iter eq 'void') {
147 push @result, trim
($onetype);
155 return "target_ops::" . $name;
158 # Write function header given name, return type, and argtypes.
159 # Returns a list of actual argument names.
160 sub write_function_header
($$$@
) {
161 my ($decl, $name, $return_type, @argtypes) = @_;
166 if ($return_type !~ m
,\
*$,) {
179 foreach $iter (@argtypes) {
182 $val =~ s/$TARGET_DEBUG_PRINTER//;
184 if ($iter !~ m
,(\
*|&)$,) {
192 push @argdecls, $val;
193 push @actuals, $vname;
197 print join (', ', @argdecls) . ")";
200 print " override;\n";
208 # Write out a declaration.
209 sub write_declaration
($$@
) {
210 my ($name, $return_type, @argtypes) = @_;
212 write_function_header
(1, $name, $return_type, @argtypes);
215 # Write out a delegation function.
216 sub write_delegator
($$@
) {
217 my ($name, $return_type, @argtypes) = @_;
219 my (@names) = write_function_header
(0, dname
($name),
220 $return_type, @argtypes);
223 if ($return_type ne 'void') {
226 print "this->beneath ()->" . $name . " (";
227 print join (', ', @names);
234 return "dummy_target::" . $name;
237 # Write out a default function.
238 sub write_tdefault
($$$$@
) {
239 my ($content, $style, $name, $return_type, @argtypes) = @_;
241 my (@names) = write_function_header
(0, tdname
($name),
242 $return_type, @argtypes);
244 if ($style eq 'FUNC') {
246 if ($return_type ne 'void') {
249 print $content . " (this";
253 print join (', ', @names);
255 } elsif ($style eq 'RETURN') {
256 print " return $content;\n";
257 } elsif ($style eq 'NORETURN') {
258 print " $content;\n";
259 } elsif ($style eq 'IGNORE') {
262 die "unrecognized style: $style\n";
267 return tdname
($name);
274 if ($typename =~ m/$TARGET_DEBUG_PRINTER/) {
277 ($result = $typename) =~ s/\s+$//;
278 $result =~ s/[ ()<>:]/_/g;
279 $result =~ s/[*]/p/g;
282 # Identifers with double underscores are reserved to the C++
286 # Avoid ending the function name with underscore, for
287 # cosmetics. Trailing underscores appear after munging types
288 # with template parameters, like e.g. "foo<int>".
291 $result = 'target_debug_print_' . $result;
297 # Write out a debug method.
298 sub write_debugmethod
($$$@
) {
299 my ($content, $name, $return_type, @argtypes) = @_;
301 my ($debugname) = "debug_target::" . $name;
302 my ($targetname) = $name;
304 my (@names) = write_function_header
(0, $debugname, $return_type, @argtypes);
306 if ($return_type ne 'void') {
307 print " $return_type result;\n";
310 print " fprintf_unfiltered (gdb_stdlog, \"-> %s->$name (...)\\n\", this->beneath ()->shortname ());\n";
312 # Delegate to the beneath target.
314 if ($return_type ne 'void') {
317 print "this->beneath ()->" . $name . " (";
318 print join (', ', @names);
321 # Now print the arguments.
322 print " fprintf_unfiltered (gdb_stdlog, \"<- %s->$name (\", this->beneath ()->shortname ());\n";
323 for my $i (0 .. $#argtypes) {
325 print " fputs_unfiltered (\", \", gdb_stdlog);\n"
327 my $printer = munge_type
($argtypes[$i]);
328 print " $printer ($names[$i]);\n";
330 if ($return_type ne 'void') {
331 print " fputs_unfiltered (\") = \", gdb_stdlog);\n";
332 my $printer = munge_type
($return_type);
333 print " $printer (result);\n";
334 print " fputs_unfiltered (\"\\n\", gdb_stdlog);\n";
336 print " fputs_unfiltered (\")\\n\", gdb_stdlog);\n";
339 if ($return_type ne 'void') {
340 print " return result;\n";
348 print "/* THIS FILE IS GENERATED -*- buffer-read-only: t -*- */\n";
349 print "/* vi:set ro: */\n\n";
350 print "/* To regenerate this file, run:*/\n";
351 print "/* make-target-delegates target.h > target-delegates.c */\n";
354 @lines = scan_target_h
();
360 @argtypes_array = ();
362 foreach $current_line (@lines) {
363 # See comments in scan_target_h. Here we strip away the leading
364 # and trailing whitespace.
365 $current_line = trim
($current_line);
367 next unless $current_line =~ m/$METHOD/;
370 my $current_line = $+{args
};
371 my $return_type = trim
($+{return_type
});
372 my $current_args = $+{args
};
373 my $tdefault = $+{default_arg
};
374 my $style = $+{style
};
376 my @argtypes = parse_argtypes
($current_args);
378 push @delegators, $name;
380 $return_types{$name} = $return_type;
381 $tdefaults{$name} = $tdefault;
382 $styles{$name} = $style;
383 $argtypes_array{$name} = \
@argtypes;
389 print "struct " . $name . " : public target_ops\n";
391 print " const target_info &info () const override;\n";
393 print " strata stratum () const override;\n";
396 for $name (@delegators) {
397 my $return_type = $return_types{$name};
398 my @argtypes = @
{$argtypes_array{$name}};
401 write_declaration
($name, $return_type, @argtypes);
407 print_class
("dummy_target");
408 print_class
("debug_target");
410 for $name (@delegators) {
411 my $tdefault = $tdefaults{$name};
412 my $return_type = $return_types{$name};
413 my $style = $styles{$name};
414 my @argtypes = @
{$argtypes_array{$name}};
416 write_delegator
($name, $return_type, @argtypes);
418 write_tdefault
($tdefault, $style, $name, $return_type, @argtypes);
420 write_debugmethod
($tdefault, $name, $return_type, @argtypes);
This page took 0.044874 seconds and 4 git commands to generate.