[llvm-commits] [llvm] r56465 - in /llvm/trunk: tools/llvmc2/AutoGenerated.cpp tools/llvmc2/AutoGenerated.h tools/llvmc2/CompilationGraph.h tools/llvmc2/Plugin.cpp tools/llvmc2/Plugin.h tools/llvmc2/Tool.h tools/llvmc2/llvmc.cpp tools/llvmc2/plugins/ tools/llvmc2/plugins/Hello/ tools/llvmc2/plugins/Hello/Hello.cpp tools/llvmc2/plugins/Hello/Makefile utils/TableGen/LLVMCConfigurationEmitter.cpp

Mikhail Glushenkov foldr at codedgers.com
Mon Sep 22 13:49:35 PDT 2008


Author: foldr
Date: Mon Sep 22 15:49:34 2008
New Revision: 56465

URL: http://llvm.org/viewvc/llvm-project?rev=56465&view=rev
Log:
Plugin support for llvmc2 (a-la opt).

Added:
    llvm/trunk/tools/llvmc2/Plugin.cpp
    llvm/trunk/tools/llvmc2/Plugin.h
    llvm/trunk/tools/llvmc2/plugins/
    llvm/trunk/tools/llvmc2/plugins/Hello/
    llvm/trunk/tools/llvmc2/plugins/Hello/Hello.cpp
    llvm/trunk/tools/llvmc2/plugins/Hello/Makefile
Removed:
    llvm/trunk/tools/llvmc2/AutoGenerated.h
Modified:
    llvm/trunk/tools/llvmc2/AutoGenerated.cpp
    llvm/trunk/tools/llvmc2/CompilationGraph.h
    llvm/trunk/tools/llvmc2/Tool.h
    llvm/trunk/tools/llvmc2/llvmc.cpp
    llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp

Modified: llvm/trunk/tools/llvmc2/AutoGenerated.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/AutoGenerated.cpp?rev=56465&r1=56464&r2=56465&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/AutoGenerated.cpp (original)
+++ llvm/trunk/tools/llvmc2/AutoGenerated.cpp Mon Sep 22 15:49:34 2008
@@ -11,20 +11,5 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "AutoGenerated.h"
-#include "CompilationGraph.h"
-#include "Tool.h"
-
-#include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/CommandLine.h"
-
-#include <cstdlib>
-#include <stdexcept>
-
-using namespace llvm;
-using namespace llvmc;
-
-extern cl::opt<std::string> OutputFilename;
-
 // The auto-generated file
 #include "AutoGenerated.inc"

Removed: llvm/trunk/tools/llvmc2/AutoGenerated.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/AutoGenerated.h?rev=56464&view=auto

==============================================================================
--- llvm/trunk/tools/llvmc2/AutoGenerated.h (original)
+++ llvm/trunk/tools/llvmc2/AutoGenerated.h (removed)
@@ -1,34 +0,0 @@
-//===--- AutoGenerated.h - The LLVM Compiler Driver -------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open
-// Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//  Auto-generated tool descriptions - public interface.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_TOOLS_LLVMC2_AUTOGENERATED_H
-#define LLVM_TOOLS_LLVMC2_AUTOGENERATED_H
-
-#include "llvm/ADT/StringMap.h"
-
-#include <string>
-
-namespace llvmc {
-
-  class LanguageMap;
-  class CompilationGraph;
-
-  /// PopulateLanguageMap - The auto-generated function that fills in
-  /// the language map (map from file extensions to language names).
-  void PopulateLanguageMap(LanguageMap& langMap);
-  /// PopulateCompilationGraph - The auto-generated function that
-  /// populates the compilation graph with nodes and edges.
-  void PopulateCompilationGraph(CompilationGraph& tools);
-}
-
-#endif // LLVM_TOOLS_LLVMC2_AUTOGENERATED_H

Modified: llvm/trunk/tools/llvmc2/CompilationGraph.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/CompilationGraph.h?rev=56465&r1=56464&r2=56465&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/CompilationGraph.h (original)
+++ llvm/trunk/tools/llvmc2/CompilationGraph.h Mon Sep 22 15:49:34 2008
@@ -14,7 +14,6 @@
 #ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
 #define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
 
-#include "AutoGenerated.h"
 #include "Tool.h"
 
 #include "llvm/ADT/GraphTraits.h"
@@ -30,6 +29,7 @@
 
 namespace llvmc {
 
+  class CompilationGraph;
   typedef llvm::StringSet<> InputLanguagesSet;
 
   /// LanguageMap - Maps from extensions to language names.

Added: llvm/trunk/tools/llvmc2/Plugin.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/Plugin.cpp?rev=56465&view=auto

==============================================================================
--- llvm/trunk/tools/llvmc2/Plugin.cpp (added)
+++ llvm/trunk/tools/llvmc2/Plugin.cpp Mon Sep 22 15:49:34 2008
@@ -0,0 +1,41 @@
+//===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open
+// Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Plugin support for llvmc2.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Plugin.h"
+
+#include <vector>
+
+namespace {
+  typedef std::vector<llvmc::BasePlugin*> PluginRegistry;
+  static PluginRegistry GlobalPluginRegistry;
+}
+
+namespace llvmc {
+
+  RegisterPluginImpl::RegisterPluginImpl(BasePlugin* plugin) {
+    GlobalPluginRegistry.push_back(plugin);
+  }
+
+  void PopulateLanguageMap(LanguageMap& langMap) {
+    for (PluginRegistry::const_iterator B = GlobalPluginRegistry.begin(),
+           E = GlobalPluginRegistry.end(); B != E; ++B)
+      (*B)->PopulateLanguageMap(langMap);
+  }
+
+  void PopulateCompilationGraph(CompilationGraph& graph) {
+    for (PluginRegistry::const_iterator B = GlobalPluginRegistry.begin(),
+           E = GlobalPluginRegistry.end(); B != E; ++B)
+      (*B)->PopulateCompilationGraph(graph);
+  }
+
+}

Added: llvm/trunk/tools/llvmc2/Plugin.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/Plugin.h?rev=56465&view=auto

==============================================================================
--- llvm/trunk/tools/llvmc2/Plugin.h (added)
+++ llvm/trunk/tools/llvmc2/Plugin.h Mon Sep 22 15:49:34 2008
@@ -0,0 +1,56 @@
+//===--- Plugin.h - The LLVM Compiler Driver --------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open
+// Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Plugin support for llvmc2.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_LLVMC2_PLUGIN_H
+#define LLVM_TOOLS_LLVMC2_PLUGIN_H
+
+namespace llvmc {
+
+  class LanguageMap;
+  class CompilationGraph;
+
+  /// BasePlugin - An abstract base class for all LLVMC plugins.
+  struct BasePlugin {
+
+    /// PopulateLanguageMap - The auto-generated function that fills in
+    /// the language map (map from file extensions to language names).
+    virtual void PopulateLanguageMap(LanguageMap&) const = 0;
+
+    /// PopulateCompilationGraph - The auto-generated function that
+    /// populates the compilation graph with nodes and edges.
+    virtual void PopulateCompilationGraph(CompilationGraph&) const = 0;
+  };
+
+  // Helper class for RegisterPlugin.
+  class RegisterPluginImpl {
+  protected:
+    RegisterPluginImpl(BasePlugin*);
+  };
+
+  /// RegisterPlugin<T> template - Used to register LLVMC plugins.
+  template <class T>
+  struct RegisterPlugin : RegisterPluginImpl {
+    RegisterPlugin() : RegisterPluginImpl (new T()) {}
+  };
+
+  /// PopulateLanguageMap - Fills in the language map by calling
+  /// PopulateLanguageMap methods of all plugins.
+  void PopulateLanguageMap(LanguageMap& langMap);
+
+  /// PopulateCompilationGraph - Populates the compilation graph by
+  /// calling PopulateCompilationGraph methods of all plugins.
+  void PopulateCompilationGraph(CompilationGraph& tools);
+
+}
+
+#endif // LLVM_TOOLS_LLVMC2_PLUGIN_H

Modified: llvm/trunk/tools/llvmc2/Tool.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/Tool.h?rev=56465&r1=56464&r2=56465&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/Tool.h (original)
+++ llvm/trunk/tools/llvmc2/Tool.h Mon Sep 22 15:49:34 2008
@@ -25,6 +25,7 @@
 
 namespace llvmc {
 
+  class LanguageMap;
   typedef std::vector<llvm::sys::Path> PathVector;
   typedef llvm::StringSet<> InputLanguagesSet;
 

Modified: llvm/trunk/tools/llvmc2/llvmc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/llvmc.cpp?rev=56465&r1=56464&r2=56465&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/llvmc.cpp (original)
+++ llvm/trunk/tools/llvmc2/llvmc.cpp Mon Sep 22 15:49:34 2008
@@ -16,10 +16,11 @@
 
 #include "CompilationGraph.h"
 #include "Error.h"
-#include "Tool.h"
+#include "Plugin.h"
 
 #include "llvm/System/Path.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/PluginLoader.h"
 
 #include <iostream>
 #include <stdexcept>

Added: llvm/trunk/tools/llvmc2/plugins/Hello/Hello.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/plugins/Hello/Hello.cpp?rev=56465&view=auto

==============================================================================
--- llvm/trunk/tools/llvmc2/plugins/Hello/Hello.cpp (added)
+++ llvm/trunk/tools/llvmc2/plugins/Hello/Hello.cpp Mon Sep 22 15:49:34 2008
@@ -0,0 +1,35 @@
+//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Test plugin for LLVMC.
+//
+//===----------------------------------------------------------------------===//
+
+// TODO: Since llvmc2 has now gained support for plugins, its header
+// files should be probably moved into LLVM include dir.
+
+#include "../../CompilationGraph.h"
+#include "../../Plugin.h"
+
+#include <iostream>
+
+namespace {
+struct MyPlugin : public llvmc::BasePlugin {
+  void PopulateLanguageMap(llvmc::LanguageMap&) const
+  { std::cout << "Hello!\n"; }
+
+  void PopulateCompilationGraph(llvmc::CompilationGraph&) const
+  {}
+};
+
+static llvmc::RegisterPlugin<MyPlugin> RP;
+
+}
+
+

Added: llvm/trunk/tools/llvmc2/plugins/Hello/Makefile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/plugins/Hello/Makefile?rev=56465&view=auto

==============================================================================
--- llvm/trunk/tools/llvmc2/plugins/Hello/Makefile (added)
+++ llvm/trunk/tools/llvmc2/plugins/Hello/Makefile Mon Sep 22 15:49:34 2008
@@ -0,0 +1,17 @@
+##===- lib/Transforms/Hello/Makefile -----------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../../../..
+LIBRARYNAME = LLVMCHello
+LOADABLE_MODULE = 1
+REQUIRES_EH = 1
+USEDLIBS =
+
+include $(LEVEL)/Makefile.common
+

Modified: llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp?rev=56465&r1=56464&r2=56465&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/LLVMCConfigurationEmitter.cpp Mon Sep 22 15:49:34 2008
@@ -1456,7 +1456,8 @@
     throw std::string("Error in the language map definition!");
 
   // Generate code
-  O << "void llvmc::PopulateLanguageMap(LanguageMap& langMap) {\n";
+  O << "namespace {\n\n";
+  O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
 
   for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
     Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
@@ -1470,7 +1471,7 @@
         << "\"] = \"" << Lang << "\";\n";
   }
 
-  O << "}\n\n";
+  O << "}\n\n}\n\n";
 }
 
 /// FillInToolToLang - Fills in two tables that map tool names to
@@ -1593,7 +1594,8 @@
   ListInit* edges = CompilationGraph->getValueAsListInit("edges");
 
   // Generate code
-  O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n";
+  O << "namespace {\n\n";
+  O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
 
   // Insert vertices
 
@@ -1627,7 +1629,7 @@
     O << ");\n";
   }
 
-  O << "}\n\n";
+  O << "}\n\n}\n\n";
 }
 
 /// ExtractHookNames - Extract the hook names from all instances of
@@ -1704,6 +1706,38 @@
   O << "}\n\n";
 }
 
+/// EmitRegisterPlugin - Emit code to register this plugin.
+void EmitRegisterPlugin(std::ostream& O) {
+  O << "namespace {\n\n"
+    << "struct Plugin : public llvmc::BasePlugin {\n"
+    << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
+    << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
+    << Indent1
+    << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
+    << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
+    << "};\n\n"
+
+    << "static llvmc::RegisterPlugin<Plugin> RP;\n\n}\n\n";
+}
+
+/// EmitInclude - Emit necessary #include directives.
+void EmitIncludes(std::ostream& O) {
+  O << "#include \"CompilationGraph.h\"\n"
+    << "#include \"Plugin.h\"\n"
+    << "#include \"Tool.h\"\n\n"
+
+    << "#include \"llvm/ADT/StringExtras.h\"\n"
+    << "#include \"llvm/Support/CommandLine.h\"\n\n"
+
+    << "#include <cstdlib>\n"
+    << "#include <stdexcept>\n\n"
+
+    << "using namespace llvm;\n"
+    << "using namespace llvmc;\n\n"
+
+    << "extern cl::opt<std::string> OutputFilename;\n\n";
+}
+
 // End of anonymous namespace
 }
 
@@ -1713,6 +1747,7 @@
 
   // Emit file header.
   EmitSourceFileHeader("LLVMC Configuration Library", O);
+  EmitIncludes(O);
 
   // Get a list of all defined Tools.
   RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
@@ -1760,6 +1795,9 @@
   // Emit PopulateCompilationGraph() function.
   EmitPopulateCompilationGraph(CompilationGraphRecord, O);
 
+  // Emit code for plugin registration.
+  EmitRegisterPlugin(O);
+
   // EOF
   } catch (std::exception& Error) {
     throw Error.what() + std::string(" - usually this means a syntax error.");





More information about the llvm-commits mailing list