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 start of arguments to a method.
35 $ARGS_PART = qr,(?<args>\(.*)$,;
37 $INTRO_PART = qr,^\s*,;
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 introductory line to a method definition.
48 $METHOD = ($INTRO_PART . "(?<return_type>" . $SIMPLE_RETURN_PART
49 . "|" . $VEC_RETURN_PART . ")"
50 . $NAME_PART . $ARGS_PART);
52 # Match the arguments and trailing attribute of a method definition.
53 $METHOD_TRAILER = qr,(?<args>\(.+\))\s*${TARGET_DEFAULT_PART};$,;
57 $result =~ s,^\s*(\S*)\s*$,\1,;
61 # Read from the input files until we find the trigger line.
66 return if m/$TRIGGER/;
69 die "could not find trigger line\n";
72 # Parse arguments into a list.
73 sub parse_argtypes($) {
76 $typestr =~ s/^\((.*)\)$/\1/;
78 my (@typelist) = split (/,\s*/, $typestr);
79 my (@result, $iter, $onetype);
81 foreach $iter (@typelist) {
82 if ($iter =~ m/^(enum\s+${SYMBOL}\s*)(${SYMBOL})?$/) {
84 } elsif ($iter =~ m/^(.*(enum\s+)?${SYMBOL}.*(\s|\*))${SYMBOL}+$/) {
86 } elsif ($iter eq 'void') {
91 push @result, trim ($onetype);
99 $name =~ s/to_/delegate_/;
103 # Write function header given name, return type, and argtypes.
104 # Returns a list of actual argument names.
105 sub write_function_header($$@) {
106 my ($name, $return_type, @argtypes) = @_;
108 print "static " . $return_type . "\n";
115 foreach $iter (@argtypes) {
118 if ($iter !~ m,\*$,) {
124 # Just a random nicety.
131 push @argdecls, $val;
132 push @actuals, $vname;
136 print join (', ', @argdecls) . ")\n";
142 # Write out a delegation function.
143 sub write_delegator($$@) {
144 my ($name, $return_type, @argtypes) = @_;
146 my (@names) = write_function_header (dname ($name), $return_type,
149 print " $names[0] = $names[0]->beneath;\n";
151 if ($return_type ne 'void') {
154 print "$names[0]->" . $name . " (";
155 print join (', ', @names);
162 $name =~ s/to_/tdefault_/;
166 # Write out a default function.
167 sub write_tdefault($$$$@) {
168 my ($content, $style, $name, $return_type, @argtypes) = @_;
170 if ($style eq 'FUNC') {
174 write_function_header (tdname ($name), $return_type, @argtypes);
176 if ($style eq 'RETURN') {
177 print " return $content;\n";
178 } elsif ($style eq 'NORETURN') {
179 print " $content;\n";
180 } elsif ($style eq 'IGNORE') {
183 die "unrecognized style: $style\n";
188 return tdname ($name);
191 print "/* THIS FILE IS GENERATED -*- buffer-read-only: t -*- */\n";
192 print "/* vi:set ro: */\n\n";
193 print "/* To regenerate this file, run:*/\n";
194 print "/* make-target-delegates target.h > target-delegates.c */\n";
198 %tdefault_names = ();
205 if ($current_line ne '') {
208 } elsif (m/$METHOD/) {
210 $current_line = $+{args};
211 $return_type = trim ($+{return_type});
214 if ($current_line =~ /\);\s*$/) {
215 if ($current_line =~ m,$METHOD_TRAILER,) {
216 $current_args = $+{args};
217 $tdefault = $+{default_arg};
220 @argtypes = parse_argtypes ($current_args);
222 # The first argument must be "this" to be delegatable.
223 if ($argtypes[0] =~ /\s*struct\s+target_ops\s*\*\s*/) {
224 write_delegator ($name, $return_type, @argtypes);
226 push @delegators, $name;
228 $tdefault_names{$name} = write_tdefault ($tdefault, $style,
238 # Now the delegation code.
239 print "static void\ninstall_delegators (struct target_ops *ops)\n{\n";
241 for $iter (@delegators) {
242 print " if (ops->" . $iter . " == NULL)\n";
243 print " ops->" . $iter . " = " . dname ($iter) . ";\n";
247 # Now the default method code.
248 print "static void\ninstall_dummy_methods (struct target_ops *ops)\n{\n";
250 for $iter (@delegators) {
251 print " ops->" . $iter . " = " . $tdefault_names{$iter} . ";\n";