3 # Copyright (C) 2013-2014 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 # Match the return type when it is "ordinary".
40 $SIMPLE_RETURN_PART = qr,[^\(]+,;
41 # Match the return type when it is a VEC.
42 $VEC_RETURN_PART = qr,VEC\s*\([^\)]+\)[^\(]*,;
44 # Match the TARGET_DEFAULT_* attribute for a method.
45 $TARGET_DEFAULT_PART = qr,TARGET_DEFAULT_(?<style>[A-Z_]+)\s*\((?<default_arg>.*)\),;
47 # Match the arguments and trailing attribute of a method definition.
48 # Note we don't match the trailing ";".
49 $METHOD_TRAILER = qr,\s*${TARGET_DEFAULT_PART}$,;
51 # Match an entire method definition.
52 $METHOD = ($INTRO_PART . "(?<return_type>" . $SIMPLE_RETURN_PART
53 . "|" . $VEC_RETURN_PART . ")"
54 . $NAME_PART . $ARGS_PART
66 # Read from the input files until we find the trigger line.
71 return if m/$TRIGGER/;
74 die "could not find trigger line\n";
77 # Scan target.h and return a list of possible target_ops method entries.
79 my $all_the_text = '';
84 # Skip the open brace.
88 # Just in case somebody ever uses C99.
95 # Now strip out the C comments.
96 $all_the_text =~ s,/\*(.*?)\*/,,g;
98 return split (/;/, $all_the_text);
101 # Parse arguments into a list.
102 sub parse_argtypes($) {
105 $typestr =~ s/^\((.*)\)$/\1/;
107 my (@typelist) = split (/,\s*/, $typestr);
108 my (@result, $iter, $onetype);
110 foreach $iter (@typelist) {
111 if ($iter =~ m/^(enum\s+${SYMBOL}\s*)(${SYMBOL})?$/) {
113 } elsif ($iter =~ m/^(.*(enum\s+)?${SYMBOL}.*(\s|\*))${SYMBOL}+$/) {
115 } elsif ($iter eq 'void') {
120 push @result, trim ($onetype);
128 $name =~ s/to_/delegate_/;
132 # Write function header given name, return type, and argtypes.
133 # Returns a list of actual argument names.
134 sub write_function_header($$@) {
135 my ($name, $return_type, @argtypes) = @_;
137 print "static " . $return_type . "\n";
144 foreach $iter (@argtypes) {
147 if ($iter !~ m,\*$,) {
153 # Just a random nicety.
160 push @argdecls, $val;
161 push @actuals, $vname;
165 print join (', ', @argdecls) . ")\n";
171 # Write out a delegation function.
172 sub write_delegator($$@) {
173 my ($name, $return_type, @argtypes) = @_;
175 my (@names) = write_function_header (dname ($name), $return_type,
178 print " $names[0] = $names[0]->beneath;\n";
180 if ($return_type ne 'void') {
183 print "$names[0]->" . $name . " (";
184 print join (', ', @names);
191 $name =~ s/to_/tdefault_/;
195 # Write out a default function.
196 sub write_tdefault($$$$@) {
197 my ($content, $style, $name, $return_type, @argtypes) = @_;
199 if ($style eq 'FUNC') {
203 write_function_header (tdname ($name), $return_type, @argtypes);
205 if ($style eq 'RETURN') {
206 print " return $content;\n";
207 } elsif ($style eq 'NORETURN') {
208 print " $content;\n";
209 } elsif ($style eq 'IGNORE') {
212 die "unrecognized style: $style\n";
217 return tdname ($name);
220 print "/* THIS FILE IS GENERATED -*- buffer-read-only: t -*- */\n";
221 print "/* vi:set ro: */\n\n";
222 print "/* To regenerate this file, run:*/\n";
223 print "/* make-target-delegates target.h > target-delegates.c */\n";
225 @lines = scan_target_h();
228 %tdefault_names = ();
230 foreach $current_line (@lines) {
231 next unless $current_line =~ m/$METHOD/;
234 $current_line = $+{args};
235 $return_type = trim ($+{return_type});
236 $current_args = $+{args};
237 $tdefault = $+{default_arg};
240 @argtypes = parse_argtypes ($current_args);
242 # The first argument must be "this" to be delegatable.
243 if ($argtypes[0] =~ /\s*struct\s+target_ops\s*\*\s*/) {
244 write_delegator ($name, $return_type, @argtypes);
246 push @delegators, $name;
248 $tdefault_names{$name} = write_tdefault ($tdefault, $style,
254 # Now the delegation code.
255 print "static void\ninstall_delegators (struct target_ops *ops)\n{\n";
257 for $iter (@delegators) {
258 print " if (ops->" . $iter . " == NULL)\n";
259 print " ops->" . $iter . " = " . dname ($iter) . ";\n";
263 # Now the default method code.
264 print "static void\ninstall_dummy_methods (struct target_ops *ops)\n{\n";
266 for $iter (@delegators) {
267 print " ops->" . $iter . " = " . $tdefault_names{$iter} . ";\n";