public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@redhat.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@redhat.com>
Subject: [RFC 04/32] add make-target-delegates
Date: Mon, 13 Jan 2014 19:13:00 -0000	[thread overview]
Message-ID: <1389640367-5571-5-git-send-email-tromey@redhat.com> (raw)
In-Reply-To: <1389640367-5571-1-git-send-email-tromey@redhat.com>

This patch adds a new script, call make-target-delegates, which
auto-generates some target delegation code based on annotations in
target.h.  This adds the new delegation macros, the new generated
file, and adds the necessary calls to the new generated functions to
target.c.  It doesn't, however, add any actual annotations to the
target methods, leaving these for separate patches.

2014-01-08  Tom Tromey  <tromey@redhat.com>

	* target-delegates.c: New file.
	* target.c: Include target-delegates.c.
	(init_dummy_target): Call install_dummy_methods.
	(complete_target_initialization): Call install_delegators.
	* target.h (TARGET_DEFAULT_IGNORE, TARGET_DEFAULT_NORETURN)
	(TARGET_DEFAULT_RETURN, TARGET_DEFAULT_FUNC): New defines.
	* make-target-delegates: New file.
---
 gdb/ChangeLog             |  10 ++
 gdb/make-target-delegates | 253 ++++++++++++++++++++++++++++++++++++++++++++++
 gdb/target-delegates.c    |  14 +++
 gdb/target.c              |   9 ++
 gdb/target.h              |  24 +++++
 5 files changed, 310 insertions(+)
 create mode 100755 gdb/make-target-delegates
 create mode 100644 gdb/target-delegates.c

diff --git a/gdb/make-target-delegates b/gdb/make-target-delegates
new file mode 100755
index 0000000..47f960c
--- /dev/null
+++ b/gdb/make-target-delegates
@@ -0,0 +1,253 @@
+#!/usr/bin/perl
+
+# Copyright (C) 2013 Free Software Foundation, Inc.
+#
+# This file is part of GDB.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# Usage:
+#    make-target-delegates target.h > target-delegates.c
+
+# The line we search for in target.h that marks where we should start
+# looking for methods.
+$TRIGGER = qr,^struct target_ops$,;
+# The end of the methods part.
+$ENDER = qr,^\s*};$,;
+
+# Match a C symbol.
+$SYMBOL = qr,[a-zA-Z_][a-zA-Z0-9_]*,;
+# Match the name part of a method in struct target_ops.
+$NAME_PART = qr,\(\*(?<name>${SYMBOL}+)\)\s,;
+# Match the start of arguments to a method.
+$ARGS_PART = qr,(?<args>\(.*)$,;
+# Match indentation.
+$INTRO_PART = qr,^\s*,;
+
+# Match the return type when it is "ordinary".
+$SIMPLE_RETURN_PART = qr,[^\(]+,;
+# Match the return type when it is a VEC.
+$VEC_RETURN_PART = qr,VEC\s*\([^\)]+\)[^\(]*,;
+
+# Match the TARGET_DEFAULT_* attribute for a method.
+$TARGET_DEFAULT_PART = qr,TARGET_DEFAULT_(?<style>[A-Z_]+)\s*\((?<default_arg>.*)\),;
+
+# Match the introductory line to a method definition.
+$METHOD = ($INTRO_PART . "(?<return_type>" . $SIMPLE_RETURN_PART
+	   . "|" . $VEC_RETURN_PART . ")"
+	   . $NAME_PART . $ARGS_PART);
+
+# Match the arguments and trailing attribute of a method definition.
+$METHOD_TRAILER = qr,(?<args>\([^\)]+\))\s*${TARGET_DEFAULT_PART};$,;
+
+sub trim($) {
+    my ($result) = @_;
+    $result =~ s,^\s*(\S*)\s*$,\1,;
+    return $result;
+}
+
+# Read from the input files until we find the trigger line.
+# Die if not found.
+sub find_trigger() {
+    while (<>) {
+	chomp;
+	return if m/$TRIGGER/;
+    }
+
+    die "could not find trigger line\n";
+}
+
+# Parse arguments into a list.
+sub parse_argtypes($) {
+    my ($typestr) = @_;
+
+    $typestr =~ s/^\((.*)\)$/\1/;
+
+    my (@typelist) = split (/,\s*/, $typestr);
+    my (@result, $iter, $onetype);
+
+    foreach $iter (@typelist) {
+	if ($iter =~ m/^(enum\s+${SYMBOL}\s*)(${SYMBOL})?$/) {
+	    $onetype = $1;
+	} elsif ($iter =~ m/^(.*(enum\s+)?${SYMBOL}.*(\s|\*))${SYMBOL}+$/) {
+	    $onetype = $1;
+	} elsif ($iter eq 'void') {
+	    next;
+	} else {
+	    $onetype = $iter;
+	}
+	push @result, trim ($onetype);
+    }
+
+    return @result;
+}
+
+sub dname($) {
+    my ($name) = @_;
+    $name =~ s/to_/delegate_/;
+    return $name;
+}
+
+# Write function header given name, return type, and argtypes.
+# Returns a list of actual argument names.
+sub write_function_header($$@) {
+    my ($name, $return_type, @argtypes) = @_;
+
+    print "static " . $return_type . "\n";
+    print $name . ' (';
+
+    my $iter;
+    my @argdecls;
+    my @actuals;
+    my $i = 0;
+    foreach $iter (@argtypes) {
+	my $val = $iter;
+
+	if ($iter !~ m,\*$,) {
+	    $val .= ' ';
+	}
+
+	my $vname;
+	if ($i == 0) {
+	    # Just a random nicety.
+	    $vname = 'self';
+	} else {
+	    $vname .= "arg$i";
+	}
+	$val .= $vname;
+
+	push @argdecls, $val;
+	push @actuals, $vname;
+	++$i;
+    }
+
+    print join (', ', @argdecls) . ")\n";
+    print "{\n";
+
+    return @actuals;
+}
+
+# Write out a delegation function.
+sub write_delegator($$@) {
+    my ($name, $return_type, @argtypes) = @_;
+
+    my (@names) = write_function_header (dname ($name), $return_type,
+					 @argtypes);
+
+    print "  $names[0] = $names[0]->beneath;\n";
+    print "  ";
+    if ($return_type ne 'void') {
+	print "return ";
+    }
+    print "$names[0]->" . $name . " (";
+    print join (', ', @names);
+    print ");\n";
+    print "}\n\n";
+}
+
+sub tdname ($) {
+    my ($name) = @_;
+    $name =~ s/to_/tdefault_/;
+    return $name;
+}
+
+# Write out a default function.
+sub write_tdefault($$$$@) {
+    my ($content, $style, $name, $return_type, @argtypes) = @_;
+
+    if ($style eq 'FUNC') {
+	return $content;
+    }
+
+    write_function_header (tdname ($name), $return_type, @argtypes);
+
+    if ($style eq 'RETURN') {
+	print "  return $content;\n";
+    } elsif ($style eq 'NORETURN') {
+	print "  $content;\n";
+    } elsif ($style eq 'IGNORE') {
+	# Nothing.
+    } else {
+	die "unrecognized style: $style\n";
+    }
+
+    print "}\n\n";
+
+    return tdname ($name);
+}
+
+print "/* THIS FILE IS GENERATED -*- buffer-read-only: t -*- */\n";
+print "/* vi:set ro: */\n\n";
+print "/* To regenerate this file, run:*/\n";
+print "/*      make-target-delegates target.h > target-delegates.c */\n";
+
+find_trigger();
+
+%tdefault_names = ();
+@delegators = ();
+$current_line = '';
+while (<>) {
+    chomp;
+    last if m/$ENDER/;
+
+    if ($current_line ne '') {
+	s/^\s*//;
+	$current_line .= $_;
+    } elsif (m/$METHOD/) {
+	$name = $+{name};
+	$current_line = $+{args};
+	$return_type = trim ($+{return_type});
+    }
+
+    if ($current_line =~ /\);\s*$/) {
+	if ($current_line =~ m,$METHOD_TRAILER,) {
+	    $current_args = $+{args};
+	    $tdefault = $+{default_arg};
+	    $style = $+{style};
+
+	    @argtypes = parse_argtypes ($current_args);
+
+	    # The first argument must be "this" to be delegatable.
+	    if ($argtypes[0] =~ /\s*struct\s+target_ops\s*\*\s*/) {
+		write_delegator ($name, $return_type, @argtypes);
+
+		push @delegators, $name;
+
+		$tdefault_names{$name} = write_tdefault ($tdefault, $style,
+							 $name, $return_type,
+							 @argtypes);
+	    }
+	}
+
+	$current_line = '';
+    }
+}
+
+# Now the delegation code.
+print "static void\ninstall_delegators (struct target_ops *ops)\n{\n";
+
+for $iter (@delegators) {
+    print "  if (ops->" . $iter . " == NULL)\n";
+    print "    ops->" . $iter . " = " . dname ($iter) . ";\n";
+}
+print "}\n\n";
+
+# Now the default method code.
+print "static void\ninstall_dummy_methods (struct target_ops *ops)\n{\n";
+
+for $iter (@delegators) {
+    print "  ops->" . $iter . " = " . $tdefault_names{$iter} . ";\n";
+}
+print "}\n";
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
new file mode 100644
index 0000000..cf6364d
--- /dev/null
+++ b/gdb/target-delegates.c
@@ -0,0 +1,14 @@
+/* THIS FILE IS GENERATED -*- buffer-read-only: t -*- */
+/* vi:set ro: */
+
+/* To regenerate this file, run:*/
+/*      make-target-delegates target.h > target-delegates.c */
+static void
+install_delegators (struct target_ops *ops)
+{
+}
+
+static void
+install_dummy_methods (struct target_ops *ops)
+{
+}
diff --git a/gdb/target.c b/gdb/target.c
index d2a40ee..1fe7ba3 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -85,6 +85,8 @@ static LONGEST current_xfer_partial (struct target_ops *ops,
 static struct gdbarch *default_thread_architecture (struct target_ops *ops,
 						    ptid_t ptid);
 
+#include "target-delegates.c"
+
 static void init_dummy_target (void);
 
 static struct target_ops debug_target;
@@ -352,6 +354,8 @@ complete_target_initialization (struct target_ops *t)
 
   if (t->to_has_execution == NULL)
     t->to_has_execution = (int (*) (struct target_ops *, ptid_t)) return_zero;
+
+  install_delegators (t);
 }
 
 /* Add possible target architecture T to the list and add a new
@@ -559,6 +563,9 @@ update_current_target (void)
   /* First, reset current's contents.  */
   memset (&current_target, 0, sizeof (current_target));
 
+  /* Install the delegators.  */
+  install_delegators (&current_target);
+
 #define INHERIT(FIELD, TARGET) \
       if (!current_target.FIELD) \
 	current_target.FIELD = (TARGET)->FIELD
@@ -3783,6 +3790,8 @@ init_dummy_target (void)
   dummy_target.to_stopped_data_address =
     (int (*) (struct target_ops *, CORE_ADDR *)) return_zero;
   dummy_target.to_magic = OPS_MAGIC;
+
+  install_dummy_methods (&dummy_target);
 }
 \f
 static void
diff --git a/gdb/target.h b/gdb/target.h
index 78c9d34..867b2f2 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -345,6 +345,30 @@ struct thread_info;		/* fwd decl for parameter list below: */
 
 typedef void async_callback_ftype (enum inferior_event_type, void *);
 
+/* These defines are used to mark target_ops methods.  The script
+   make-target-delegates scans these and auto-generates the base
+   method implementations.  There are four macros that can be used:
+   
+   1. TARGET_DEFAULT_IGNORE.  There is no argument.  The base method
+   does nothing.  This is only valid if the method return type is
+   'void'.
+   
+   2. TARGET_DEFAULT_NORETURN.  The argument is a function call, like
+   'tcomplain ()'.  The base method simply makes this call, which is
+   assumed not to return.
+   
+   3. TARGET_DEFAULT_RETURN.  The argument is a C expression.  The
+   base method returns this expression's value.
+   
+   4. TARGET_DEFAULT_FUNC.  The argument is the name of a function.
+   make-target-delegates does not generate a base method in this case,
+   but instead uses the argument function as the base method.  */
+
+#define TARGET_DEFAULT_IGNORE()
+#define TARGET_DEFAULT_NORETURN(ARG)
+#define TARGET_DEFAULT_RETURN(ARG)
+#define TARGET_DEFAULT_FUNC(ARG)
+
 struct target_ops
   {
     struct target_ops *beneath;	/* To the target under this one.  */
-- 
1.8.1.4

  parent reply	other threads:[~2014-01-13 19:13 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-13 19:12 [RFC 00/32] clean up target delegation Tom Tromey
2014-01-13 19:12 ` [RFC 01/32] add "this" pointers to more target APIs Tom Tromey
2014-01-14 12:10   ` Pedro Alves
2014-01-14 20:25     ` Tom Tromey
2014-01-13 19:12 ` [RFC 02/32] introduce and use find_target_at Tom Tromey
2014-01-14 11:48   ` [PATCH] Fix "is a record target open" checks Pedro Alves
2014-01-14 15:30     ` Tom Tromey
2014-01-14 18:27       ` Pedro Alves
2014-01-15 16:22     ` Tom Tromey
2014-01-13 19:12 ` [RFC 03/32] introduce async_callback_ftype Tom Tromey
2014-01-14 10:35   ` Pedro Alves
2014-01-14 10:50     ` Joel Brobecker
2014-01-14 15:06       ` Tom Tromey
2014-01-14 17:19         ` Joel Brobecker
2014-01-14 17:27           ` Tom Tromey
2014-01-14 18:30           ` Pedro Alves
2014-01-14 19:45             ` Tom Tromey
2014-01-15 15:25               ` Tom Tromey
2014-01-13 19:13 ` [RFC 32/32] minor cleanups to update_current_target Tom Tromey
2014-01-14 20:10   ` Pedro Alves
2014-01-13 19:13 ` Tom Tromey [this message]
2014-01-14 10:52   ` [RFC 04/32] add make-target-delegates Pedro Alves
2014-01-14 14:46     ` Tom Tromey
2014-01-13 19:13 ` [RFC 31/32] change delegation for to_read_description Tom Tromey
2014-01-14 20:07   ` Pedro Alves
2014-01-14 20:22     ` Tom Tromey
2014-01-13 19:13 ` [RFC 26/32] convert to_static_tracepoint_markers_by_strid Tom Tromey
2014-01-14 18:57   ` Pedro Alves
2014-01-13 19:13 ` [RFC 10/32] Add target_ops argument to to_terminal_init Tom Tromey
2014-01-14 12:51   ` Pedro Alves
2014-01-13 19:13 ` [RFC 25/32] convert to_upload_trace_state_variables Tom Tromey
2014-01-14 19:38   ` Pedro Alves
2014-01-13 19:13 ` [RFC 08/32] remove extended_remote_create_inferior_1 Tom Tromey
2014-01-14 12:41   ` Pedro Alves
2014-01-16 19:20     ` Tom Tromey
2014-01-13 19:13 ` [RFC 09/32] Add target_ops argument to to_close Tom Tromey
2014-01-14 12:48   ` Pedro Alves
2014-01-13 19:13 ` [RFC 11/32] Add target_ops argument to to_insert_vfork_catchpoint Tom Tromey
2014-01-14 12:52   ` Pedro Alves
2014-01-13 19:13 ` [RFC 19/32] convert to_detach Tom Tromey
2014-01-14 13:32   ` Pedro Alves
2014-01-13 19:13 ` [RFC 27/32] convert to_insert_mask_watchpoint Tom Tromey
2014-01-14 19:15   ` Pedro Alves
2014-01-14 19:23     ` Tom Tromey
2014-01-13 19:13 ` [RFC 12/32] Add target_ops argument to to_thread_name Tom Tromey
2014-01-14 13:03   ` Pedro Alves
2014-01-15 16:45     ` Tom Tromey
2014-01-16 17:50       ` Pedro Alves
2014-01-13 19:13 ` [RFC 24/32] convert to_disable_tracepoint Tom Tromey
2014-01-14 18:49   ` Pedro Alves
2014-01-13 19:23 ` [RFC 13/32] Add target_ops argument to to_get_ada_task_ptid Tom Tromey
2014-01-14 13:21   ` Pedro Alves
2014-01-13 19:23 ` [RFC 07/32] introduce remote_load Tom Tromey
2014-01-14 12:39   ` Pedro Alves
2014-01-13 19:23 ` [RFC 20/32] convert to_remove_watchpoint Tom Tromey
2014-01-14 18:39   ` Pedro Alves
2014-01-14 18:55     ` Tom Tromey
2014-01-14 19:07       ` Tom Tromey
2014-01-14 20:38       ` Pedro Alves
2014-01-14 21:47         ` Tom Tromey
2014-01-13 19:23 ` [RFC 17/32] Add target_ops argument to to_static_tracepoint_markers_by_strid Tom Tromey
2014-01-14 13:25   ` Pedro Alves
2014-01-13 19:24 ` [RFC 06/32] convert to_supports_btrace Tom Tromey
2014-01-14 12:37   ` Pedro Alves
2014-01-15 16:55     ` Tom Tromey
2014-01-13 19:24 ` [RFC 16/32] Add target_ops argument to to_upload_trace_state_variables Tom Tromey
2014-01-14 13:24   ` Pedro Alves
2014-01-13 19:24 ` [RFC 30/32] convert to_search_memory Tom Tromey
2014-01-14 19:45   ` Pedro Alves
2014-01-14 20:20     ` Tom Tromey
2014-01-13 19:37 ` [RFC 21/32] convert to_load Tom Tromey
2014-01-14 18:41   ` Pedro Alves
2014-01-13 19:38 ` [RFC 05/32] add target method delegation Tom Tromey
2014-01-14 12:32   ` Pedro Alves
2014-01-20 22:00     ` Tom Tromey
2014-01-13 19:38 ` [RFC 22/32] convert to_extra_thread_info Tom Tromey
2014-01-14 18:43   ` Pedro Alves
2014-01-13 19:38 ` [RFC 28/32] convert to_get_section_table Tom Tromey
2014-01-14 19:23   ` Pedro Alves
2014-01-14 19:29     ` Tom Tromey
2014-01-14 19:30       ` Pedro Alves
2014-01-15 16:43         ` Tom Tromey
2014-01-16 17:51           ` Pedro Alves
2014-01-13 19:38 ` [RFC 23/32] convert to_thread_architecture Tom Tromey
2014-01-14 18:46   ` Pedro Alves
2014-01-13 19:38 ` [RFC 18/32] Add target_ops argument to to_save_record Tom Tromey
2014-01-14 13:26   ` Pedro Alves
2014-01-13 19:40 ` [RFC 29/32] convert to_insn_history Tom Tromey
2014-01-14 19:29   ` Pedro Alves
2014-01-13 19:57 ` [RFC 14/32] Add target_ops argument to to_fileio_pwrite Tom Tromey
2014-01-14 13:22   ` Pedro Alves
2014-01-13 19:57 ` [RFC 15/32] Add target_ops argument to to_disable_tracepoint Tom Tromey
2014-01-14 13:23   ` Pedro Alves
2014-01-14 20:31 ` go32 fix Pedro Alves
2014-01-14 21:58   ` Tom Tromey
2014-01-15 12:55 ` [RFC 00/32] clean up target delegation Pedro Alves
2014-01-15 16:11   ` Tom Tromey
2014-01-15 20:05     ` Tom Tromey
2014-01-16 17:33       ` Pedro Alves
2014-01-16 19:09 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1389640367-5571-5-git-send-email-tromey@redhat.com \
    --to=tromey@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).