PATCH: Registry template

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

PATCH: Registry template

Gordon Henriksen-3
This is a template that takes care of managing plugin registries. I  
wrote it because I got the distinct feeling I was needlessly  
reinventing the wheel as I wrote a collector registry. I haven't  
converted anything else to use it, though. Still, it's orthogonal and  
has become stable in my usage, so I thought I'd submit it in advance.

To get my collector registry going, all I needed was 5 lines:

   // GCRegistry.h
   typedef Registry<TargetCollector> CollectorRegistry;

   // GCRegistry.cpp
   template<> CollectorRegistry::node *CollectorRegistry::Head = 0;
   template<> CollectorRegistry::node *CollectorRegistry::Tail = 0;
   template<> CollectorRegistry::listener  
*CollectorRegistry::ListenerHead = 0;
   template<> CollectorRegistry::listener  
*CollectorRegistry::ListenerTail = 0;

Afterwards, the most important features work more or less as to be  
expected:

   // ShadowStackCollector.cpp
   CollectorRegistry::Add<ShadowStackGC>
   Y("shadow-stack", "Shadow-stack collector, for uncooperative code  
generators");

   // llc.cpp
   static cl::opt<const CollectorRegistry::entry*, false,  
CollectorRegistry::Parser>
   GC("gc", cl::desc("Enable garbage collection."));

   Collector = GC->instantiate();

   // nowhere
   for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
                                    E = CollectorRegistry::end(); I !
= E; ++I)
     cout << I->getName() << ": " << I->getDesc() << "\n";

Granted, my needs are currently simple. The default entry type has  
just the name, description, and use the default constructor. To  
convert, many registries in the system would need to also define:

  1. An custom entry type, particularly where the default constructor  
was insufficient.
  2. A custom traits type.
  3. The RegisterFoo template, to create the constructor wrapper  
springboard.

Thanks,
Gordon



Index: include/llvm/Support/Registry.h
===================================================================
--- include/llvm/Support/Registry.h     (revision 0)
+++ include/llvm/Support/Registry.h     (revision 0)
@@ -0,0 +1,243 @@
+//=== Registry.h - Linker-supported plugin registries -----------*- C
++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Gordon Henriksen and is distributed  
under the
+// University of Illinois Open Source License. See LICENSE.TXT for  
details.
+//
+//
===---------------------------------------------------------------------
-===//
+//
+// Defines a registry template for discovering pluggable modules.
+//
+//
===---------------------------------------------------------------------
-===//
+
+#ifndef LLVM_SUPPORT_REGISTRY_H
+#define LLVM_SUPPORT_REGISTRY_H
+
+#include "llvm/Support/CommandLine.h"
+
+namespace llvm {
+  /// A simple registry entry which provides only a name,  
description, and
+  /// no-argument constructor.
+  template <typename T>
+  class SimpleRegistryEntry {
+    const char *Name, *Desc;
+    T *(*Ctor)();
+
+  public:
+    SimpleRegistryEntry(const char *N, const char *D, T *(*C)())
+      : Name(N), Desc(D), Ctor(C)
+    {}
+
+    const char *getName() const { return Name; }
+    const char *getDesc() const { return Desc; }
+    T *instantiate() const { return Ctor(); }
+  };
+
+
+  /// Traits for registry entries. If using other than  
SimpleRegistryEntry, it
+  /// is necessary to define an alternate traits class.
+  template <typename T>
+  class RegistryTraits {
+    RegistryTraits(); // Do not implement.
+
+  public:
+    typedef SimpleRegistryEntry<T> entry;
+
+    /// Accessors for .
+    ///
+    static const char *nameof(const entry &Entry) { return  
Entry.getName(); }
+    static const char *descof(const entry &Entry) { return  
Entry.getDesc(); }
+  };
+
+
+  /// A global registry used in conjunction with static constructors  
to make
+  /// pluggable components (like targets or garbage collectors)  
"just work" when
+  /// linked with an executable.
+  template <typename T, typename U = RegistryTraits<T> >
+  class Registry {
+  public:
+    typedef U traits;
+    typedef typename U::entry entry;
+
+    class node;
+    class listener;
+    class iterator;
+
+  private:
+    Registry(); // Do not implement.
+
+    static void Announce(node *);
+
+    friend class node;
+    static node *Head, *Tail;
+
+    friend class listener;
+    static listener *ListenerHead, *ListenerTail;
+
+  public:
+    class iterator;
+
+
+    /// Node in linked list of entries.
+    ///
+    class node {
+      friend class iterator;
+
+      node *Next;
+      const entry& Val;
+
+    public:
+      node(const entry& V) : Next(0), Val(V) {
+        if (Tail)
+          Tail->Next = this;
+        else
+          Head = this;
+        Tail = this;
+
+        Announce(V);
+      }
+    };
+
+
+    /// Iterators for registry entries.
+    ///
+    class iterator {
+      const node *Cur;
+
+    public:
+      explicit iterator(const node *N) : Cur(N) {}
+
+      bool operator==(const iterator &That) const { return Cur ==  
That.Cur; }
+      bool operator!=(const iterator &That) const { return Cur !=  
That.Cur; }
+      iterator &operator++() { Cur = Cur->Next; return *this; }
+      const entry &operator*() const { return Cur->Val; }
+      const entry *operator->() const { return &Cur->Val; }
+    };
+
+    static iterator begin() { return iterator(Head); }
+    static iterator end()   { return iterator(0); }
+
+
+    /// Abstract base class for registry listeners, which are  
informed when new
+    /// entries are added to the registry. Simply subclass and  
instantiate:
+    ///
+    ///   class CollectorPrinter : public  
Registry<TargetCollector>::listener {
+    ///   protected:
+    ///     void registered(const Registry<TargetCollector>::entry  
&e) {
+    ///       cerr << "collector now available: " << e->getName() <<  
"\n";
+    ///     }
+    ///
+    ///   public:
+    ///     CollectorPrinter() { init(); }  // Print those already  
registered.
+    ///   };
+    ///
+    ///   CollectorPrinter Printer;
+    ///
+    class listener {
+      listener *Prev, *Next;
+
+      friend void Registry::Announce(const entry &E);
+
+    protected:
+      /// Called when an entry is added to the registry.
+      ///
+      virtual void registered(const entry &) = 0;
+
+      /// Calls 'registered' for each pre-existing entry.
+      ///
+      void init() {
+        for (iterator I = begin(), E = end(); I != E; ++I)
+          registered(*I);
+      }
+
+    public:
+      listener() : Prev(ListenerTail), Next(0) {
+        if (Prev)
+          Prev->Next = this;
+        else
+          ListenerHead = this;
+        ListenerTail = this;
+      }
+
+      virtual ~listener() {
+        if (Next)
+          Next->Prev = Prev;
+        else
+          ListenerTail = Prev;
+        if (Prev)
+          Prev->Next = Next;
+        else
+          ListenerHead = Next;
+      }
+    };
+
+
+    /// A static registration template. Use like such:
+    ///
+    ///   Registry<TargetCollector>::Add<FancyGC>
+    ///   X("fancy-gc", "Newfangled garbage collector.");
+    ///
+    /// Use of this template requires that:
+    ///
+    ///  1. The registered subclass has a default constructor.
+    //
+    ///  2. The registry entry type has a constructor compatible  
with this
+    ///     signature:
+    ///
+    ///       entry(const char *Name, const char *ShortDesc, T *
(*Ctor)());
+    ///
+    /// If you have more elaborate requirements, then copy and modify.
+    ///
+    template <typename V>
+    class Add {
+      entry Entry;
+      node Node;
+
+      static T *CtorFn() { return new V(); }
+
+    public:
+      Add(const char *Name, const char *Desc)
+        : Entry(Name, Desc, CtorFn), Node(Entry) {}
+    };
+
+
+    /// A command-line parser for a registry. Use like such:
+    ///
+    ///   static cl::opt<Registry<TargetCollector>::entry,
+    ///                  false, Registry<TargetCollector>::Parser>
+    ///   TGC("gc", cl::desc("Garbage collector to use."),
+    ///             cl::value_desc());
+    ///
+    /// To make use of the value:
+    ///
+    ///   TargetGC *Collector = TGC->instantiate();
+    ///
+    class Parser : public cl::parser<const typename U::entry*>,  
public listener{
+      typedef U traits;
+      typedef typename U::entry entry;
+
+    protected:
+      void registered(const entry &E) {
+        addLiteralOption(traits::nameof(E), &E, traits::descof(E));
+      }
+
+    public:
+      void initialize(cl::Option &O) {
+        listener::init();
+        cl::parser<const typename U::entry*>::initialize(O);
+      }
+    };
+
+
+  private:
+    static void Announce(const entry &E) {
+      for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
+        Cur->registered(E);
+    }
+
+  };
+
+}
+
+#endif

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PATCH: Registry template

Gordon Henriksen-3
Woo, that got mangled by wrapping. Here's an attachment.

On 2007-08-31, at 01:32, Gordon Henriksen wrote:

> This is a template that takes care of managing plugin registries. I
> wrote it because I got the distinct feeling I was needlessly
> reinventing the wheel as I wrote a collector registry. I haven't
> converted anything else to use it, though. Still, it's orthogonal
> and has become stable in my usage, so I thought I'd submit it in
> advance.
>
> To get my collector registry going, all I needed was 5 lines:
>
>    // GCRegistry.h
>    typedef Registry<TargetCollector> CollectorRegistry;
>
>    // GCRegistry.cpp
>    template<> CollectorRegistry::node *CollectorRegistry::Head = 0;
>    template<> CollectorRegistry::node *CollectorRegistry::Tail = 0;
>    template<> CollectorRegistry::listener *CollectorRegistry::
>    ListenerHead = 0;
>    template<> CollectorRegistry::listener *CollectorRegistry::
>    ListenerTail = 0;
>
> Afterwards, the most important features work more or less as to be
> expected:
>
>    // ShadowStackCollector.cpp
>    CollectorRegistry::Add<ShadowStackGC>
>    Y("shadow-stack",
>      "Shadow stack collector, for uncooperative code generators");
>
>    // llc.cpp
>    static cl::opt<const CollectorRegistry::entry*, false,
>                   CollectorRegistry::Parser>
>    GC("gc", cl::desc("Enable garbage collection."));
>
>    Collector = GC->instantiate();
>
>    // nowhere
>    for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
>                                     E = CollectorRegistry::end();
>                                     I ! = E; ++I)
>      cout << I->getName() << ": " << I->getDesc() << "\n";
>
> Granted, my needs are currently simple. The default entry type has
> just the name, description, and use the default constructor. To
> convert, many registries in the system would need to also define:
>
>   1. An custom entry type, particularly where the default
>      constructor was insufficient.
>   2. A custom traits type.
>   3. The RegisterFoo template, to create the constructor wrapper
>      springboard.
>
> Thanks,
> Gordon

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

registry-template.patch (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: PATCH: Registry template

Gordon Henriksen-3
In reply to this post by Gordon Henriksen-3
On 2007-08-31, at 01:32, Gordon Henriksen wrote:

> This is a template that takes care of managing plugin registries. I  
> haven't converted anything else to use it, though.

-50 LOC for TargetMachineRegistry.

— Gordon




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

targetmachineregistry.patch (12K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: PATCH: Registry template

dag-7
In reply to this post by Gordon Henriksen-3
On Friday 31 August 2007 00:32, Gordon Henriksen wrote:
> This is a template that takes care of managing plugin registries.

Woot!  I relized the need for such a thing when I did the pluggable
register coalescer work.

In particular, register allocators should use this.  They use a special
regalloc-only thing that as far as I can tell only exists for historical
reasons (perhaps it was the first one?).

                                            -Dave
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev