[llvm-commits] [llvm] r50725 - in /llvm/trunk: tools/llvmc2/Common.td tools/llvmc2/CompilationGraph.cpp tools/llvmc2/CompilationGraph.h tools/llvmc2/Tools.td utils/TableGen/LLVMCCConfigurationEmitter.cpp

Mikhail Glushenkov foldr at codedgers.com
Tue May 6 09:36:06 PDT 2008


Author: foldr
Date: Tue May  6 11:36:06 2008
New Revision: 50725

URL: http://llvm.org/viewvc/llvm-project?rev=50725&view=rev
Log:
Ongoing work: add an edge typechecker, rudimentary support for edge properties.

Modified:
    llvm/trunk/tools/llvmc2/Common.td
    llvm/trunk/tools/llvmc2/CompilationGraph.cpp
    llvm/trunk/tools/llvmc2/CompilationGraph.h
    llvm/trunk/tools/llvmc2/Tools.td
    llvm/trunk/utils/TableGen/LLVMCCConfigurationEmitter.cpp

Modified: llvm/trunk/tools/llvmc2/Common.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/Common.td?rev=50725&r1=50724&r2=50725&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/Common.td (original)
+++ llvm/trunk/tools/llvmc2/Common.td Tue May  6 11:36:06 2008
@@ -15,7 +15,7 @@
       list<dag> properties = l;
 }
 
-// Special Tool instance - root of all toolchains
+// Special Tool instance - graph root.
 
 def root : Tool<[]>;
 
@@ -45,6 +45,17 @@
 def help;
 def required;
 
+// Possible edge properties
+
+def switch_on;
+def parameter_equals;
+def element_in_list;
+
+// Property combinators
+// TOFIX: implement
+def and;
+def or;
+
 // Map from suffixes to language names
 
 class LangToSuffixes<string str, list<string> lst> {
@@ -58,11 +69,20 @@
 
 // Compilation graph
 
-class Edge<Tool t1, Tool t2> {
+class EdgeBase<Tool t1, Tool t2, list<dag> lst> {
       Tool a = t1;
       Tool b = t2;
+      list<dag> props = lst;
 }
 
-class CompilationGraph<list<Edge> lst> {
-      list<Edge> edges = lst;
+class Edge<Tool t1, Tool t2> : EdgeBase<t1, t2, []>;
+
+// Edge and DefaultEdge are synonyms.
+class DefaultEdge<Tool t1, Tool t2> : EdgeBase<t1, t2, []>;
+
+// Optionally enabled edge.
+class OptionalEdge<Tool t1, Tool t2, list<dag> lst> : EdgeBase<t1, t2, lst>;
+
+class CompilationGraph<list<EdgeBase> lst> {
+      list<EdgeBase> edges = lst;
 }

Modified: llvm/trunk/tools/llvmc2/CompilationGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/CompilationGraph.cpp?rev=50725&r1=50724&r2=50725&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/CompilationGraph.cpp (original)
+++ llvm/trunk/tools/llvmc2/CompilationGraph.cpp Tue May  6 11:36:06 2008
@@ -60,7 +60,7 @@
   return I->second;
 }
 
-void CompilationGraph::insertVertex(const IntrusiveRefCntPtr<Tool> V) {
+void CompilationGraph::insertNode(Tool* V) {
   if (!NodesMap.count(V->Name())) {
     Node N;
     N.OwningGraph = this;
@@ -82,13 +82,13 @@
     ToolsMap[InputLanguage].push_back(B);
 
     // Needed to support iteration via GraphTraits.
-    NodesMap["root"].Children.push_back(B);
+    NodesMap["root"].AddEdge(new DefaultEdge(B));
   }
   else {
-    Node& NA = getNode(A);
+    Node& N = getNode(A);
     // Check that there is a node at B.
     getNode(B);
-    NA.Children.push_back(B);
+    N.AddEdge(new DefaultEdge(B));
   }
 }
 
@@ -123,7 +123,7 @@
       }
 
       // Is this the last tool?
-      if (N->Children.empty() || CurTool->IsLast()) {
+      if (!N->HasChildren() || CurTool->IsLast()) {
         Out.appendComponent(In.getBasename());
         Out.appendSuffix(CurTool->OutputSuffix());
         Last = true;
@@ -139,7 +139,7 @@
       if (CurTool->GenerateAction(In, Out).Execute() != 0)
         throw std::runtime_error("Tool returned error code!");
 
-      N = &getNode(*N->Children.begin());
+      N = &getNode((*N->EdgesBegin())->ToolName());
       In = Out; Out.clear();
     }
   }

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

==============================================================================
--- llvm/trunk/tools/llvmc2/CompilationGraph.h (original)
+++ llvm/trunk/tools/llvmc2/CompilationGraph.h Tue May  6 11:36:06 2008
@@ -18,6 +18,7 @@
 #include "Tool.h"
 
 #include "llvm/ADT/GraphTraits.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/iterator"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
@@ -27,45 +28,59 @@
 
 namespace llvmcc {
 
-  class CompilationGraph;
+  class Edge : public llvm::RefCountedBaseVPTR<Edge> {
+  public:
+    Edge(const std::string& T) : ToolName_(T) {}
+    virtual ~Edge() {};
+
+    const std::string& ToolName() const { return ToolName_; }
+    virtual bool isEnabled() const = 0;
+    virtual bool isDefault() const = 0;
+  private:
+    std::string ToolName_;
+  };
+
+  class DefaultEdge : public Edge {
+  public:
+    DefaultEdge(const std::string& T) : Edge(T) {}
+    bool isEnabled() const { return true;}
+    bool isDefault() const { return true;}
+  };
 
   struct Node {
-    typedef llvm::SmallVector<std::string, 3> sequence_type;
+    typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
+    typedef container_type::iterator iterator;
+    typedef container_type::const_iterator const_iterator;
 
     Node() {}
     Node(CompilationGraph* G) : OwningGraph(G) {}
     Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
 
+    bool HasChildren() const { return OutEdges.empty(); }
+
+    iterator EdgesBegin() { return OutEdges.begin(); }
+    const_iterator EdgesBegin() const { return OutEdges.begin(); }
+    iterator EdgesEnd() { return OutEdges.end(); }
+    const_iterator EdgesEnd() const { return OutEdges.end(); }
+
+    // E is a new-allocated pointer.
+    void AddEdge(Edge* E)
+    { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
+
     // Needed to implement NodeChildIterator/GraphTraits
     CompilationGraph* OwningGraph;
     // The corresponding Tool.
     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
     // Links to children.
-    sequence_type Children;
+    container_type OutEdges;
   };
 
-  // This can be generalised to something like value_iterator for maps
-  class NodesIterator : public llvm::StringMap<Node>::iterator {
-    typedef llvm::StringMap<Node>::iterator super;
-    typedef NodesIterator ThisType;
-    typedef Node* pointer;
-    typedef Node& reference;
-
-  public:
-    NodesIterator(super I) : super(I) {}
-
-    inline reference operator*() const {
-      return super::operator->()->second;
-    }
-    inline pointer operator->() const {
-      return &super::operator->()->second;
-    }
-  };
+  class NodesIterator;
 
   class CompilationGraph {
-    typedef llvm::StringMap<Node> nodes_map_type;
     typedef llvm::SmallVector<std::string, 3> tools_vector_type;
     typedef llvm::StringMap<tools_vector_type> tools_map_type;
+    typedef llvm::StringMap<Node> nodes_map_type;
 
     // Map from file extensions to language names.
     LanguageMap ExtsToLangs;
@@ -79,7 +94,7 @@
     CompilationGraph();
 
     // insertVertex - insert a new node into the graph.
-    void insertVertex(const llvm::IntrusiveRefCntPtr<Tool> T);
+    void insertNode(Tool* T);
 
     // insertEdge - Insert a new edge into the graph. This function
     // assumes that both A and B have been already inserted.
@@ -90,87 +105,108 @@
     // variables.
     int Build(llvm::sys::Path const& tempDir) const;
 
-    /// viewGraph - This function is meant for use from the debugger.
-    /// You can just say 'call G->viewGraph()' and a ghostview window
-    /// should pop up from the program, displaying the compilation
-    /// graph. This depends on there being a 'dot' and 'gv' program
-    /// in your path.
+    // Return a reference to the node correponding to the given tool
+    // name. Throws std::runtime_error.
+    Node& getNode(const std::string& ToolName);
+    const Node& getNode(const std::string& ToolName) const;
+
+    // viewGraph - This function is meant for use from the debugger.
+    // You can just say 'call G->viewGraph()' and a ghostview window
+    // should pop up from the program, displaying the compilation
+    // graph. This depends on there being a 'dot' and 'gv' program
+    // in your path.
     void viewGraph();
 
-    /// Write a CompilationGraph.dot file.
+    // Write a CompilationGraph.dot file.
     void writeGraph();
 
     // GraphTraits support
+    friend NodesIterator GraphBegin(CompilationGraph*);
+    friend NodesIterator GraphEnd(CompilationGraph*);
+    friend void PopulateCompilationGraph(CompilationGraph&);
 
-    typedef NodesIterator nodes_iterator;
+  private:
+    // Helper functions.
 
-    nodes_iterator nodes_begin() {
-      return NodesIterator(NodesMap.begin());
-    }
+    // Find out which language corresponds to the suffix of this file.
+    const std::string& getLanguage(const llvm::sys::Path& File) const;
 
-    nodes_iterator nodes_end() {
-      return NodesIterator(NodesMap.end());
-    }
+    // Return a reference to the list of tool names corresponding to
+    // the given language name. Throws std::runtime_error.
+    const tools_vector_type& getToolsVector(const std::string& LangName) const;
+  };
 
-    // Return a reference to the node correponding to the given tool
-    // name. Throws std::runtime_error in case of error.
-    Node& getNode(const std::string& ToolName);
-    const Node& getNode(const std::string& ToolName) const;
+  /// GraphTraits support code.
 
-    // Auto-generated function.
-    friend void PopulateCompilationGraph(CompilationGraph&);
+  // Auxiliary class needed to implement GraphTraits support.  Can be
+  // generalised to something like value_iterator for map-like
+  // containers.
+  class NodesIterator : public llvm::StringMap<Node>::iterator {
+    typedef llvm::StringMap<Node>::iterator super;
+    typedef NodesIterator ThisType;
+    typedef Node* pointer;
+    typedef Node& reference;
 
-  private:
-    // Helper function - find out which language corresponds to the
-    // suffix of this file
-    const std::string& getLanguage(const llvm::sys::Path& File) const;
+  public:
+    NodesIterator(super I) : super(I) {}
 
-    // Return a reference to the tool names list correponding to the
-    // given language name. Throws std::runtime_error in case of
-    // error.
-    const tools_vector_type& getToolsVector(const std::string& LangName) const;
+    inline reference operator*() const {
+      return super::operator->()->second;
+    }
+    inline pointer operator->() const {
+      return &super::operator->()->second;
+    }
   };
 
-  // Auxiliary class needed to implement GraphTraits support.
+  inline NodesIterator GraphBegin(CompilationGraph* G) {
+    return NodesIterator(G->NodesMap.begin());
+  }
+
+  inline NodesIterator GraphEnd(CompilationGraph* G) {
+    return NodesIterator(G->NodesMap.end());
+  }
+
+
+  // Another auxiliary class needed by GraphTraits.
   class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
     typedef NodeChildIterator ThisType;
-    typedef Node::sequence_type::iterator iterator;
+    typedef Node::container_type::iterator iterator;
 
     CompilationGraph* OwningGraph;
-    iterator KeyIter;
+    iterator EdgeIter;
   public:
     typedef Node* pointer;
     typedef Node& reference;
 
     NodeChildIterator(Node* N, iterator I) :
-      OwningGraph(N->OwningGraph), KeyIter(I) {}
+      OwningGraph(N->OwningGraph), EdgeIter(I) {}
 
     const ThisType& operator=(const ThisType& I) {
       assert(OwningGraph == I.OwningGraph);
-      KeyIter = I.KeyIter;
+      EdgeIter = I.EdgeIter;
       return *this;
     }
 
     inline bool operator==(const ThisType& I) const
-    { return KeyIter == I.KeyIter; }
+    { return EdgeIter == I.EdgeIter; }
     inline bool operator!=(const ThisType& I) const
-    { return KeyIter != I.KeyIter; }
+    { return EdgeIter != I.EdgeIter; }
 
     inline pointer operator*() const {
-      return &OwningGraph->getNode(*KeyIter);
+      return &OwningGraph->getNode((*EdgeIter)->ToolName());
     }
     inline pointer operator->() const {
-      return &OwningGraph->getNode(*KeyIter);
+      return &OwningGraph->getNode((*EdgeIter)->ToolName());
     }
 
-    ThisType& operator++() { ++KeyIter; return *this; } // Preincrement
+    ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
     ThisType operator++(int) { // Postincrement
       ThisType tmp = *this;
       ++*this;
       return tmp;
     }
 
-    inline ThisType& operator--() { --KeyIter; return *this; }  // Predecrement
+    inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
     inline ThisType operator--(int) { // Postdecrement
       ThisType tmp = *this;
       --*this;
@@ -192,18 +228,18 @@
     }
 
     static ChildIteratorType child_begin(NodeType* N) {
-      return ChildIteratorType(N, N->Children.begin());
+      return ChildIteratorType(N, N->OutEdges.begin());
     }
     static ChildIteratorType child_end(NodeType* N) {
-      return ChildIteratorType(N, N->Children.end());
+      return ChildIteratorType(N, N->OutEdges.end());
     }
 
-    typedef GraphType::nodes_iterator nodes_iterator;
+    typedef llvmcc::NodesIterator nodes_iterator;
     static nodes_iterator nodes_begin(GraphType *G) {
-      return G->nodes_begin();
+      return GraphBegin(G);
     }
     static nodes_iterator nodes_end(GraphType *G) {
-      return G->nodes_end();
+      return GraphEnd(G);
     }
   };
 

Modified: llvm/trunk/tools/llvmc2/Tools.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvmc2/Tools.td?rev=50725&r1=50724&r2=50725&view=diff

==============================================================================
--- llvm/trunk/tools/llvmc2/Tools.td (original)
+++ llvm/trunk/tools/llvmc2/Tools.td Tue May  6 11:36:06 2008
@@ -21,7 +21,7 @@
 
 def llvm_gcc_c : Tool<
 [(in_language "c"),
- (out_language "llvm-assembler"),
+ (out_language "llvm-bitcode"),
  (output_suffix "bc"),
  (cmd_line "llvm-gcc -c $INFILE -o $OUTFILE -emit-llvm"),
  (sink)
@@ -29,7 +29,7 @@
 
 def llvm_gcc_cpp : Tool<
 [(in_language "c++"),
- (out_language "llvm-assembler"),
+ (out_language "llvm-bitcode"),
  (output_suffix "bc"),
  (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
  (sink)

Modified: llvm/trunk/utils/TableGen/LLVMCCConfigurationEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/LLVMCCConfigurationEmitter.cpp?rev=50725&r1=50724&r2=50725&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/LLVMCCConfigurationEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/LLVMCCConfigurationEmitter.cpp Tue May  6 11:36:06 2008
@@ -27,7 +27,7 @@
 
 using namespace llvm;
 
-namespace {
+//namespace {
 
 //===----------------------------------------------------------------------===//
 /// Typedefs
@@ -144,6 +144,7 @@
   std::string Help;
   unsigned Flags;
 
+  // We need t provide a default constructor since
   // StringMap can only store DefaultConstructible objects
   GlobalOptionDescription() : OptionDescription(), Flags(0)
   {}
@@ -539,8 +540,6 @@
   for (;B!=E;++B) {
     RecordVector::value_type T = *B;
     ListInit* PropList = T->getValueAsListInit("properties");
-    if (!PropList)
-      throw std::string("Tool has no property list!");
 
     IntrusiveRefCntPtr<ToolProperties>
       ToolProps(new ToolProperties(T->getName()));
@@ -853,17 +852,66 @@
   O << "}\n\n";
 }
 
-void EmitPopulateCompilationGraph (const RecordKeeper& Records,
-                                   StringMap<std::string>& ToolToLang,
+// Fills in two tables that map tool names to (input, output) languages.
+// Used by the typechecker.
+void FillInToolToLang (const ToolPropertiesList& TPList,
+                       StringMap<std::string>& ToolToInLang,
+                       StringMap<std::string>& ToolToOutLang) {
+  for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
+       B != E; ++B) {
+    const ToolProperties& P = *(*B);
+    ToolToInLang[P.Name] = P.InLanguage;
+    ToolToOutLang[P.Name] = P.OutLanguage;
+  }
+}
+
+// Check that all output and input language names match.
+void TypecheckGraph (Record* CompilationGraph,
+                     const ToolPropertiesList& TPList) {
+  StringMap<std::string> ToolToInLang;
+  StringMap<std::string> ToolToOutLang;
+
+  FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
+  ListInit* edges = CompilationGraph->getValueAsListInit("edges");
+  StringMap<std::string>::iterator IAE = ToolToInLang.end();
+  StringMap<std::string>::iterator IBE = ToolToOutLang.end();
+
+  for (unsigned i = 0; i < edges->size(); ++i) {
+    Record* Edge = edges->getElementAsRecord(i);
+    Record* A = Edge->getValueAsDef("a");
+    Record* B = Edge->getValueAsDef("b");
+    StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
+    StringMap<std::string>::iterator IB = ToolToInLang.find(B->getName());
+    if(IA == IAE)
+      throw A->getName() + ": no such tool!";
+    if(IB == IBE)
+      throw B->getName() + ": no such tool!";
+    if(A->getName() != "root" && IA->second != IB->second)
+      throw "Edge " + A->getName() + "->" + B->getName()
+        + ": output->input language mismatch";
+  }
+}
+
+// Emit Edge* classes used for.
+void EmitEdgeClasses (Record* CompilationGraph,
+                      const GlobalOptionDescriptions& OptDescs,
+                      std::ostream& O) {
+  ListInit* edges = CompilationGraph->getValueAsListInit("edges");
+
+  for (unsigned i = 0; i < edges->size(); ++i) {
+    //Record* Edge = edges->getElementAsRecord(i);
+    //Record* A = Edge->getValueAsDef("a");
+    //Record* B = Edge->getValueAsDef("b");
+    //ListInit* Props = Edge->getValueAsListInit("props");
+
+    O << "class Edge" << i << " {};\n\n";
+  }
+}
+
+void EmitPopulateCompilationGraph (Record* CompilationGraph,
                                    std::ostream& O)
 {
-  // Get the relevant field out of RecordKeeper
-  Record* CompilationGraph = Records.getDef("CompilationGraph");
-  if (!CompilationGraph)
-    throw std::string("No CompilationGraph specification found!");
   ListInit* edges = CompilationGraph->getValueAsListInit("edges");
-  if (!edges)
-    throw std::string("Error in compilation graph definition!");
 
   // Generate code
   O << "void llvmcc::PopulateCompilationGraph(CompilationGraph& G) {\n"
@@ -878,14 +926,13 @@
   for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
     const std::string& Name = (*B)->getName();
     if(Name != "root")
-      O << Indent1 << "G.insertVertex(IntrusiveRefCntPtr<Tool>(new "
-        << Name << "()));\n";
+      O << Indent1 << "G.insertNode(new "
+        << Name << "());\n";
   }
 
   O << '\n';
 
   // Insert edges
-
   for (unsigned i = 0; i < edges->size(); ++i) {
     Record* Edge = edges->getElementAsRecord(i);
     Record* A = Edge->getValueAsDef("a");
@@ -897,17 +944,9 @@
   O << "}\n\n";
 }
 
-void FillInToolToLang (const ToolPropertiesList& T,
-                       StringMap<std::string>& M) {
-  for (ToolPropertiesList::const_iterator B = T.begin(), E = T.end();
-       B != E; ++B) {
-    const ToolProperties& P = *(*B);
-    M[P.Name] = P.InLanguage;
-  }
-}
 
 // End of anonymous namespace
-}
+//}
 
 // Back-end entry point
 void LLVMCCConfigurationEmitter::run (std::ostream &O) {
@@ -936,12 +975,18 @@
          E = tool_props.end(); B!=E; ++B)
     EmitToolClassDefinition(*(*B), O);
 
-  // Fill in table that maps tool names to languages
-  StringMap<std::string> ToolToLang;
-  FillInToolToLang(tool_props, ToolToLang);
+  Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
+  if (!CompilationGraphRecord)
+    throw std::string("Compilation graph description not found!");
+
+  // Typecheck the compilation graph.
+  TypecheckGraph(CompilationGraphRecord, tool_props);
+
+  // Emit Edge* classes.
+  EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
 
   // Emit PopulateCompilationGraph function
-  EmitPopulateCompilationGraph(Records, ToolToLang, O);
+  EmitPopulateCompilationGraph(CompilationGraphRecord, O);
 
   // EOF
 }





More information about the llvm-commits mailing list