llvm.org GIT mirror llvm / 0a17493
Ongoing work: add an edge typechecker, rudimentary support for edge properties. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50725 91177308-0d34-0410-b5e6-96231b3b80d8 Mikhail Glushenkov 11 years ago
5 changed file(s) with 213 addition(s) and 112 deletion(s). Raw diff Collapse all Expand all
1414 list properties = l;
1515 }
1616
17 // Special Tool instance - root of all toolchains
17 // Special Tool instance - graph root.
1818
1919 def root : Tool<[]>;
2020
4444 def help;
4545 def required;
4646
47 // Possible edge properties
48
49 def switch_on;
50 def parameter_equals;
51 def element_in_list;
52
53 // Property combinators
54 // TOFIX: implement
55 def and;
56 def or;
57
4758 // Map from suffixes to language names
4859
4960 class LangToSuffixes lst> {
5768
5869 // Compilation graph
5970
60 class Edge> {
71 class EdgeBase lst> {
6172 Tool a = t1;
6273 Tool b = t2;
74 list props = lst;
6375 }
6476
65 class CompilationGraph lst> {
66 list edges = lst;
77 class Edge : EdgeBase;
78
79 // Edge and DefaultEdge are synonyms.
80 class DefaultEdge : EdgeBase;
81
82 // Optionally enabled edge.
83 class OptionalEdge lst> : EdgeBase;
84
85 class CompilationGraph lst> {
86 list edges = lst;
6787 }
5959 return I->second;
6060 }
6161
62 void CompilationGraph::insertVertex(const IntrusiveRefCntPtr V) {
62 void CompilationGraph::insertNode(Tool* V) {
6363 if (!NodesMap.count(V->Name())) {
6464 Node N;
6565 N.OwningGraph = this;
8181 ToolsMap[InputLanguage].push_back(B);
8282
8383 // Needed to support iteration via GraphTraits.
84 NodesMap["root"].Children.push_back(B);
84 NodesMap["root"].AddEdge(new DefaultEdge(B));
8585 }
8686 else {
87 Node& NA = getNode(A);
87 Node& N = getNode(A);
8888 // Check that there is a node at B.
8989 getNode(B);
90 NA.Children.push_back(B);
90 N.AddEdge(new DefaultEdge(B));
9191 }
9292 }
9393
122122 }
123123
124124 // Is this the last tool?
125 if (N->Children.empty() || CurTool->IsLast()) {
125 if (!N->HasChildren() || CurTool->IsLast()) {
126126 Out.appendComponent(In.getBasename());
127127 Out.appendSuffix(CurTool->OutputSuffix());
128128 Last = true;
138138 if (CurTool->GenerateAction(In, Out).Execute() != 0)
139139 throw std::runtime_error("Tool returned error code!");
140140
141 N = &getNode(*N->Children.begin());
141 N = &getNode((*N->EdgesBegin())->ToolName());
142142 In = Out; Out.clear();
143143 }
144144 }
1717 #include "Tool.h"
1818
1919 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
2021 #include "llvm/ADT/iterator"
2122 #include "llvm/ADT/SmallVector.h"
2223 #include "llvm/ADT/StringMap.h"
2627
2728 namespace llvmcc {
2829
29 class CompilationGraph;
30 class Edge : public llvm::RefCountedBaseVPTR {
31 public:
32 Edge(const std::string& T) : ToolName_(T) {}
33 virtual ~Edge() {};
34
35 const std::string& ToolName() const { return ToolName_; }
36 virtual bool isEnabled() const = 0;
37 virtual bool isDefault() const = 0;
38 private:
39 std::string ToolName_;
40 };
41
42 class DefaultEdge : public Edge {
43 public:
44 DefaultEdge(const std::string& T) : Edge(T) {}
45 bool isEnabled() const { return true;}
46 bool isDefault() const { return true;}
47 };
3048
3149 struct Node {
32 typedef llvm::SmallVector<std::string, 3> sequence_type;
50 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr, 3> container_type;
51 typedef container_type::iterator iterator;
52 typedef container_type::const_iterator const_iterator;
3353
3454 Node() {}
3555 Node(CompilationGraph* G) : OwningGraph(G) {}
3656 Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
57
58 bool HasChildren() const { return OutEdges.empty(); }
59
60 iterator EdgesBegin() { return OutEdges.begin(); }
61 const_iterator EdgesBegin() const { return OutEdges.begin(); }
62 iterator EdgesEnd() { return OutEdges.end(); }
63 const_iterator EdgesEnd() const { return OutEdges.end(); }
64
65 // E is a new-allocated pointer.
66 void AddEdge(Edge* E)
67 { OutEdges.push_back(llvm::IntrusiveRefCntPtr(E)); }
3768
3869 // Needed to implement NodeChildIterator/GraphTraits
3970 CompilationGraph* OwningGraph;
4071 // The corresponding Tool.
4172 llvm::IntrusiveRefCntPtr ToolPtr;
4273 // Links to children.
43 sequence_type Children;
44 };
45
46 // This can be generalised to something like value_iterator for maps
47 class NodesIterator : public llvm::StringMap::iterator {
48 typedef llvm::StringMap::iterator super;
49 typedef NodesIterator ThisType;
50 typedef Node* pointer;
51 typedef Node& reference;
52
53 public:
54 NodesIterator(super I) : super(I) {}
55
56 inline reference operator*() const {
57 return super::operator->()->second;
58 }
59 inline pointer operator->() const {
60 return &super::operator->()->second;
61 }
62 };
74 container_type OutEdges;
75 };
76
77 class NodesIterator;
6378
6479 class CompilationGraph {
65 typedef llvm::StringMap nodes_map_type;
6680 typedef llvm::SmallVector tools_vector_type;
6781 typedef llvm::StringMap tools_map_type;
82 typedef llvm::StringMap nodes_map_type;
6883
6984 // Map from file extensions to language names.
7085 LanguageMap ExtsToLangs;
7893 CompilationGraph();
7994
8095 // insertVertex - insert a new node into the graph.
81 void insertVertex(const llvm::IntrusiveRefCntPtr T);
96 void insertNode(Tool* T);
8297
8398 // insertEdge - Insert a new edge into the graph. This function
8499 // assumes that both A and B have been already inserted.
89104 // variables.
90105 int Build(llvm::sys::Path const& tempDir) const;
91106
92 /// viewGraph - This function is meant for use from the debugger.
93 /// You can just say 'call G->viewGraph()' and a ghostview window
94 /// should pop up from the program, displaying the compilation
95 /// graph. This depends on there being a 'dot' and 'gv' program
96 /// in your path.
97 void viewGraph();
98
99 /// Write a CompilationGraph.dot file.
100 void writeGraph();
101
102 // GraphTraits support
103
104 typedef NodesIterator nodes_iterator;
105
106 nodes_iterator nodes_begin() {
107 return NodesIterator(NodesMap.begin());
108 }
109
110 nodes_iterator nodes_end() {
111 return NodesIterator(NodesMap.end());
112 }
113
114107 // Return a reference to the node correponding to the given tool
115 // name. Throws std::runtime_error in case of error.
108 // name. Throws std::runtime_error.
116109 Node& getNode(const std::string& ToolName);
117110 const Node& getNode(const std::string& ToolName) const;
118111
119 // Auto-generated function.
112 // viewGraph - This function is meant for use from the debugger.
113 // You can just say 'call G->viewGraph()' and a ghostview window
114 // should pop up from the program, displaying the compilation
115 // graph. This depends on there being a 'dot' and 'gv' program
116 // in your path.
117 void viewGraph();
118
119 // Write a CompilationGraph.dot file.
120 void writeGraph();
121
122 // GraphTraits support
123 friend NodesIterator GraphBegin(CompilationGraph*);
124 friend NodesIterator GraphEnd(CompilationGraph*);
120125 friend void PopulateCompilationGraph(CompilationGraph&);
121126
122127 private:
123 // Helper function - find out which language corresponds to the
124 // suffix of this file
128 // Helper functions.
129
130 // Find out which language corresponds to the suffix of this file.
125131 const std::string& getLanguage(const llvm::sys::Path& File) const;
126132
127 // Return a reference to the tool names list correponding to the
128 // given language name. Throws std::runtime_error in case of
129 // error.
133 // Return a reference to the list of tool names corresponding to
134 // the given language name. Throws std::runtime_error.
130135 const tools_vector_type& getToolsVector(const std::string& LangName) const;
131136 };
132137
133 // Auxiliary class needed to implement GraphTraits support.
138 /// GraphTraits support code.
139
140 // Auxiliary class needed to implement GraphTraits support. Can be
141 // generalised to something like value_iterator for map-like
142 // containers.
143 class NodesIterator : public llvm::StringMap::iterator {
144 typedef llvm::StringMap::iterator super;
145 typedef NodesIterator ThisType;
146 typedef Node* pointer;
147 typedef Node& reference;
148
149 public:
150 NodesIterator(super I) : super(I) {}
151
152 inline reference operator*() const {
153 return super::operator->()->second;
154 }
155 inline pointer operator->() const {
156 return &super::operator->()->second;
157 }
158 };
159
160 inline NodesIterator GraphBegin(CompilationGraph* G) {
161 return NodesIterator(G->NodesMap.begin());
162 }
163
164 inline NodesIterator GraphEnd(CompilationGraph* G) {
165 return NodesIterator(G->NodesMap.end());
166 }
167
168
169 // Another auxiliary class needed by GraphTraits.
134170 class NodeChildIterator : public bidirectional_iterator {
135171 typedef NodeChildIterator ThisType;
136 typedef Node::sequence_type::iterator iterator;
172 typedef Node::container_type::iterator iterator;
137173
138174 CompilationGraph* OwningGraph;
139 iterator KeyIter;
175 iterator EdgeIter;
140176 public:
141177 typedef Node* pointer;
142178 typedef Node& reference;
143179
144180 NodeChildIterator(Node* N, iterator I) :
145 OwningGraph(N->OwningGraph), KeyIter(I) {}
181 OwningGraph(N->OwningGraph), EdgeIter(I) {}
146182
147183 const ThisType& operator=(const ThisType& I) {
148184 assert(OwningGraph == I.OwningGraph);
149 KeyIter = I.KeyIter;
185 EdgeIter = I.EdgeIter;
150186 return *this;
151187 }
152188
153189 inline bool operator==(const ThisType& I) const
154 { return KeyIter == I.KeyIter; }
190 { return EdgeIter == I.EdgeIter; }
155191 inline bool operator!=(const ThisType& I) const
156 { return KeyIter != I.KeyIter; }
192 { return EdgeIter != I.EdgeIter; }
157193
158194 inline pointer operator*() const {
159 return &OwningGraph->getNode(*KeyIter);
195 return &OwningGraph->getNode((*EdgeIter)->ToolName());
160196 }
161197 inline pointer operator->() const {
162 return &OwningGraph->getNode(*KeyIter);
163 }
164
165 ThisType& operator++() { ++KeyIter; return *this; } // Preincrement
198 return &OwningGraph->getNode((*EdgeIter)->ToolName());
199 }
200
201 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
166202 ThisType operator++(int) { // Postincrement
167203 ThisType tmp = *this;
168204 ++*this;
169205 return tmp;
170206 }
171207
172 inline ThisType& operator--() { --KeyIter; return *this; } // Predecrement
208 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
173209 inline ThisType operator--(int) { // Postdecrement
174210 ThisType tmp = *this;
175211 --*this;
191227 }
192228
193229 static ChildIteratorType child_begin(NodeType* N) {
194 return ChildIteratorType(N, N->Children.begin());
230 return ChildIteratorType(N, N->OutEdges.begin());
195231 }
196232 static ChildIteratorType child_end(NodeType* N) {
197 return ChildIteratorType(N, N->Children.end());
198 }
199
200 typedef GraphType::nodes_iterator nodes_iterator;
233 return ChildIteratorType(N, N->OutEdges.end());
234 }
235
236 typedef llvmcc::NodesIterator nodes_iterator;
201237 static nodes_iterator nodes_begin(GraphType *G) {
202 return G->nodes_begin();
238 return GraphBegin(G);
203239 }
204240 static nodes_iterator nodes_end(GraphType *G) {
205 return G->nodes_end();
241 return GraphEnd(G);
206242 }
207243 };
208244
2020
2121 def llvm_gcc_c : Tool<
2222 [(in_language "c"),
23 (out_language "llvm-assembler"),
23 (out_language "llvm-bitcode"),
2424 (output_suffix "bc"),
2525 (cmd_line "llvm-gcc -c $INFILE -o $OUTFILE -emit-llvm"),
2626 (sink)
2828
2929 def llvm_gcc_cpp : Tool<
3030 [(in_language "c++"),
31 (out_language "llvm-assembler"),
31 (out_language "llvm-bitcode"),
3232 (output_suffix "bc"),
3333 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
3434 (sink)
2626
2727 using namespace llvm;
2828
29 namespace {
29 //namespace {
3030
3131 //===----------------------------------------------------------------------===//
3232 /// Typedefs
143143 std::string Help;
144144 unsigned Flags;
145145
146 // We need t provide a default constructor since
146147 // StringMap can only store DefaultConstructible objects
147148 GlobalOptionDescription() : OptionDescription(), Flags(0)
148149 {}
538539 for (;B!=E;++B) {
539540 RecordVector::value_type T = *B;
540541 ListInit* PropList = T->getValueAsListInit("properties");
541 if (!PropList)
542 throw std::string("Tool has no property list!");
543542
544543 IntrusiveRefCntPtr
545544 ToolProps(new ToolProperties(T->getName()));
852851 O << "}\n\n";
853852 }
854853
855 void EmitPopulateCompilationGraph (const RecordKeeper& Records,
856 StringMap& ToolToLang,
854 // Fills in two tables that map tool names to (input, output) languages.
855 // Used by the typechecker.
856 void FillInToolToLang (const ToolPropertiesList& TPList,
857 StringMap& ToolToInLang,
858 StringMap& ToolToOutLang) {
859 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
860 B != E; ++B) {
861 const ToolProperties& P = *(*B);
862 ToolToInLang[P.Name] = P.InLanguage;
863 ToolToOutLang[P.Name] = P.OutLanguage;
864 }
865 }
866
867 // Check that all output and input language names match.
868 void TypecheckGraph (Record* CompilationGraph,
869 const ToolPropertiesList& TPList) {
870 StringMap ToolToInLang;
871 StringMap ToolToOutLang;
872
873 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
874 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
875 StringMap::iterator IAE = ToolToInLang.end();
876 StringMap::iterator IBE = ToolToOutLang.end();
877
878 for (unsigned i = 0; i < edges->size(); ++i) {
879 Record* Edge = edges->getElementAsRecord(i);
880 Record* A = Edge->getValueAsDef("a");
881 Record* B = Edge->getValueAsDef("b");
882 StringMap::iterator IA = ToolToOutLang.find(A->getName());
883 StringMap::iterator IB = ToolToInLang.find(B->getName());
884 if(IA == IAE)
885 throw A->getName() + ": no such tool!";
886 if(IB == IBE)
887 throw B->getName() + ": no such tool!";
888 if(A->getName() != "root" && IA->second != IB->second)
889 throw "Edge " + A->getName() + "->" + B->getName()
890 + ": output->input language mismatch";
891 }
892 }
893
894 // Emit Edge* classes used for.
895 void EmitEdgeClasses (Record* CompilationGraph,
896 const GlobalOptionDescriptions& OptDescs,
897 std::ostream& O) {
898 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
899
900 for (unsigned i = 0; i < edges->size(); ++i) {
901 //Record* Edge = edges->getElementAsRecord(i);
902 //Record* A = Edge->getValueAsDef("a");
903 //Record* B = Edge->getValueAsDef("b");
904 //ListInit* Props = Edge->getValueAsListInit("props");
905
906 O << "class Edge" << i << " {};\n\n";
907 }
908 }
909
910 void EmitPopulateCompilationGraph (Record* CompilationGraph,
857911 std::ostream& O)
858912 {
859 // Get the relevant field out of RecordKeeper
860 Record* CompilationGraph = Records.getDef("CompilationGraph");
861 if (!CompilationGraph)
862 throw std::string("No CompilationGraph specification found!");
863913 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
864 if (!edges)
865 throw std::string("Error in compilation graph definition!");
866914
867915 // Generate code
868916 O << "void llvmcc::PopulateCompilationGraph(CompilationGraph& G) {\n"
877925 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
878926 const std::string& Name = (*B)->getName();
879927 if(Name != "root")
880 O << Indent1 << "G.insertVertex(IntrusiveRefCntPtr(new "
881 << Name << "()));\n";
928 O << Indent1 << "G.insertNode(new "
929 << Name << "());\n";
882930 }
883931
884932 O << '\n';
885933
886934 // Insert edges
887
888935 for (unsigned i = 0; i < edges->size(); ++i) {
889936 Record* Edge = edges->getElementAsRecord(i);
890937 Record* A = Edge->getValueAsDef("a");
896943 O << "}\n\n";
897944 }
898945
899 void FillInToolToLang (const ToolPropertiesList& T,
900 StringMap& M) {
901 for (ToolPropertiesList::const_iterator B = T.begin(), E = T.end();
902 B != E; ++B) {
903 const ToolProperties& P = *(*B);
904 M[P.Name] = P.InLanguage;
905 }
906 }
907946
908947 // End of anonymous namespace
909 }
948 //}
910949
911950 // Back-end entry point
912951 void LLVMCCConfigurationEmitter::run (std::ostream &O) {
935974 E = tool_props.end(); B!=E; ++B)
936975 EmitToolClassDefinition(*(*B), O);
937976
938 // Fill in table that maps tool names to languages
939 StringMap ToolToLang;
940 FillInToolToLang(tool_props, ToolToLang);
977 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
978 if (!CompilationGraphRecord)
979 throw std::string("Compilation graph description not found!");
980
981 // Typecheck the compilation graph.
982 TypecheckGraph(CompilationGraphRecord, tool_props);
983
984 // Emit Edge* classes.
985 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
941986
942987 // Emit PopulateCompilationGraph function
943 EmitPopulateCompilationGraph(Records, ToolToLang, O);
988 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
944989
945990 // EOF
946991 }