blob: 42d9d9ce0a35ec5d8a00859e778d8ed3d771f273 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000026#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000028
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000042// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000043const char * Indent1 = " ";
44const char * Indent2 = " ";
45const char * Indent3 = " ";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000046
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000047// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
49
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000050// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000051const char * SinkOptionName = "AutoGeneratedSinkOption";
52
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000056/// Id - An 'identity' function object.
57struct Id {
58 template<typename T>
59 void operator()(const T&) const {
60 }
61};
62
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000063int InitPtrToInt(const Init* ptr) {
64 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000065 return val.getValue();
66}
67
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000068const std::string& InitPtrToString(const Init* ptr) {
69 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
70 return val.getValue();
71}
72
73const ListInit& InitPtrToList(const Init* ptr) {
74 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
75 return val;
76}
77
78const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000079 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000080 return val;
81}
82
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000083// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkova964b032009-07-07 16:07:36 +000084// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000085void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +000086 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkova964b032009-07-07 16:07:36 +000087 throw d->getOperator()->getAsString() + ": too few arguments!";
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000088}
89
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000090// isDagEmpty - is this DAG marked with an empty marker?
91bool isDagEmpty (const DagInit* d) {
92 return d->getOperator()->getAsString() == "empty";
93}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000094
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000095// EscapeVariableName - Escape commas and other symbols not allowed
96// in the C++ variable names. Makes it possible to use options named
97// like "Wa," (useful for prefix options).
98std::string EscapeVariableName(const std::string& Var) {
99 std::string ret;
100 for (unsigned i = 0; i != Var.size(); ++i) {
101 char cur_char = Var[i];
102 if (cur_char == ',') {
103 ret += "_comma_";
104 }
105 else if (cur_char == '+') {
106 ret += "_plus_";
107 }
108 else if (cur_char == '-') {
109 ret += "_dash_";
110 }
111 else {
112 ret.push_back(cur_char);
113 }
114 }
115 return ret;
116}
117
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000118/// oneOf - Does the input string contain this character?
119bool oneOf(const char* lst, char c) {
120 while (*lst) {
121 if (*lst++ == c)
122 return true;
123 }
124 return false;
125}
126
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000127template <class I, class S>
128void checkedIncrement(I& P, I E, S ErrorString) {
129 ++P;
130 if (P == E)
131 throw ErrorString;
132}
133
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000134//===----------------------------------------------------------------------===//
135/// Back-end specific code
136
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000137
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000138/// OptionType - One of six different option types. See the
139/// documentation for detailed description of differences.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000140namespace OptionType {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000141 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000142 Prefix, PrefixList};
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000143
144bool IsList (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000145 return (t == ParameterList || t == PrefixList);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000146}
147
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000148bool IsSwitch (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000149 return (t == Switch);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000150}
151
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000152bool IsParameter (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000153 return (t == Parameter || t == Prefix);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000154}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000155
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000156}
157
158OptionType::OptionType stringToOptionType(const std::string& T) {
159 if (T == "alias_option")
160 return OptionType::Alias;
161 else if (T == "switch_option")
162 return OptionType::Switch;
163 else if (T == "parameter_option")
164 return OptionType::Parameter;
165 else if (T == "parameter_list_option")
166 return OptionType::ParameterList;
167 else if (T == "prefix_option")
168 return OptionType::Prefix;
169 else if (T == "prefix_list_option")
170 return OptionType::PrefixList;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000171 else
172 throw "Unknown option type: " + T + '!';
173}
174
175namespace OptionDescriptionFlags {
176 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000177 ReallyHidden = 0x4, Extern = 0x8,
178 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000179}
180
181/// OptionDescription - Represents data contained in a single
182/// OptionList entry.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000183struct OptionDescription {
184 OptionType::OptionType Type;
185 std::string Name;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000186 unsigned Flags;
187 std::string Help;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000188 unsigned MultiVal;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000189
190 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000191 const std::string& n = "",
192 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000193 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000194 {}
195
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000196 /// GenTypeDeclaration - Returns the C++ variable type of this
197 /// option.
198 const char* GenTypeDeclaration() const;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000199
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000200 /// GenVariableName - Returns the variable name used in the
201 /// generated C++ code.
202 std::string GenVariableName() const;
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000203
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000204 /// Merge - Merge two option descriptions.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000205 void Merge (const OptionDescription& other);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000206
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000207 // Misc convenient getters/setters.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000208
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000209 bool isAlias() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000210
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000211 bool isMultiVal() const;
212
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000213 bool isExtern() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000214 void setExtern();
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000215
216 bool isRequired() const;
217 void setRequired();
218
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000219 bool isOneOrMore() const;
220 void setOneOrMore();
221
222 bool isZeroOrOne() const;
223 void setZeroOrOne();
224
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000225 bool isHidden() const;
226 void setHidden();
227
228 bool isReallyHidden() const;
229 void setReallyHidden();
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000230
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000231};
232
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000233void OptionDescription::Merge (const OptionDescription& other)
234{
235 if (other.Type != Type)
236 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000237
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000238 if (Help == other.Help || Help == DefaultHelpString)
239 Help = other.Help;
240 else if (other.Help != DefaultHelpString) {
Daniel Dunbard4287062009-07-03 00:10:29 +0000241 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000242 " defined for option " + Name + "\n";
243 }
244
245 Flags |= other.Flags;
246}
247
248bool OptionDescription::isAlias() const {
249 return Type == OptionType::Alias;
250}
251
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000252bool OptionDescription::isMultiVal() const {
Mikhail Glushenkove3649982009-01-28 03:47:58 +0000253 return MultiVal > 1;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000254}
255
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000256bool OptionDescription::isExtern() const {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000257 return Flags & OptionDescriptionFlags::Extern;
258}
259void OptionDescription::setExtern() {
260 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000261}
262
263bool OptionDescription::isRequired() const {
264 return Flags & OptionDescriptionFlags::Required;
265}
266void OptionDescription::setRequired() {
267 Flags |= OptionDescriptionFlags::Required;
268}
269
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000270bool OptionDescription::isOneOrMore() const {
271 return Flags & OptionDescriptionFlags::OneOrMore;
272}
273void OptionDescription::setOneOrMore() {
274 Flags |= OptionDescriptionFlags::OneOrMore;
275}
276
277bool OptionDescription::isZeroOrOne() const {
278 return Flags & OptionDescriptionFlags::ZeroOrOne;
279}
280void OptionDescription::setZeroOrOne() {
281 Flags |= OptionDescriptionFlags::ZeroOrOne;
282}
283
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000284bool OptionDescription::isHidden() const {
285 return Flags & OptionDescriptionFlags::Hidden;
286}
287void OptionDescription::setHidden() {
288 Flags |= OptionDescriptionFlags::Hidden;
289}
290
291bool OptionDescription::isReallyHidden() const {
292 return Flags & OptionDescriptionFlags::ReallyHidden;
293}
294void OptionDescription::setReallyHidden() {
295 Flags |= OptionDescriptionFlags::ReallyHidden;
296}
297
298const char* OptionDescription::GenTypeDeclaration() const {
299 switch (Type) {
300 case OptionType::Alias:
301 return "cl::alias";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000302 case OptionType::PrefixList:
303 case OptionType::ParameterList:
304 return "cl::list<std::string>";
305 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000306 return "cl::opt<bool>";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000307 case OptionType::Parameter:
308 case OptionType::Prefix:
309 default:
310 return "cl::opt<std::string>";
311 }
312}
313
314std::string OptionDescription::GenVariableName() const {
315 const std::string& EscapedName = EscapeVariableName(Name);
316 switch (Type) {
317 case OptionType::Alias:
318 return "AutoGeneratedAlias_" + EscapedName;
319 case OptionType::PrefixList:
320 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000321 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000322 case OptionType::Switch:
323 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000324 case OptionType::Prefix:
325 case OptionType::Parameter:
326 default:
327 return "AutoGeneratedParameter_" + EscapedName;
328 }
329}
330
331/// OptionDescriptions - An OptionDescription array plus some helper
332/// functions.
333class OptionDescriptions {
334 typedef StringMap<OptionDescription> container_type;
335
336 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000337 container_type Descriptions;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000338
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000339public:
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000340 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000341 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000342
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000343 /// insertDescription - Insert new OptionDescription into
344 /// OptionDescriptions list
345 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000346
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000347 // Support for STL-style iteration
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000348 typedef container_type::const_iterator const_iterator;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000349 const_iterator begin() const { return Descriptions.begin(); }
350 const_iterator end() const { return Descriptions.end(); }
351};
352
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000353const OptionDescription&
354OptionDescriptions::FindOption(const std::string& OptName) const
355{
356 const_iterator I = Descriptions.find(OptName);
357 if (I != Descriptions.end())
358 return I->second;
359 else
360 throw OptName + ": no such option!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000361}
362
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000363void OptionDescriptions::InsertDescription (const OptionDescription& o)
364{
365 container_type::iterator I = Descriptions.find(o.Name);
366 if (I != Descriptions.end()) {
367 OptionDescription& D = I->second;
368 D.Merge(o);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000369 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000370 else {
371 Descriptions[o.Name] = o;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000372 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000373}
374
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000375/// HandlerTable - A base class for function objects implemented as
376/// 'tables of handlers'.
377template <class T>
378class HandlerTable {
379protected:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000380 // Implementation details.
381
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000382 /// Handler -
383 typedef void (T::* Handler) (const DagInit*);
384 /// HandlerMap - A map from property names to property handlers
385 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000386
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000387 static HandlerMap Handlers_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000388 static bool staticMembersInitialized_;
389
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000390 T* childPtr;
391public:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000392
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000393 HandlerTable(T* cp) : childPtr(cp)
394 {}
395
396 /// operator() - Just forwards to the corresponding property
397 /// handler.
398 void operator() (Init* i) {
399 const DagInit& property = InitPtrToDag(i);
400 const std::string& property_name = property.getOperator()->getAsString();
401 typename HandlerMap::iterator method = Handlers_.find(property_name);
402
403 if (method != Handlers_.end()) {
404 Handler h = method->second;
405 (childPtr->*h)(&property);
406 }
407 else {
408 throw "No handler found for property " + property_name + "!";
409 }
410 }
411
412 void AddHandler(const char* Property, Handler Handl) {
413 Handlers_[Property] = Handl;
414 }
415};
416
417template <class T> typename HandlerTable<T>::HandlerMap
418HandlerTable<T>::Handlers_;
419template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
420
421
422/// CollectOptionProperties - Function object for iterating over an
423/// option property list.
424class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
425private:
426
427 /// optDescs_ - OptionDescriptions table. This is where the
428 /// information is stored.
429 OptionDescription& optDesc_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000430
431public:
432
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000433 explicit CollectOptionProperties(OptionDescription& OD)
434 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000435 {
436 if (!staticMembersInitialized_) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000437 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000438 AddHandler("help", &CollectOptionProperties::onHelp);
439 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000440 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
441 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000442 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
443 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000444 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000445
446 staticMembersInitialized_ = true;
447 }
448 }
449
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000450private:
451
452 /// Option property handlers --
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000453 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000454
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000455 void onExtern (const DagInit* d) {
456 checkNumberOfArguments(d, 0);
457 optDesc_.setExtern();
458 }
459
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000460 void onHelp (const DagInit* d) {
461 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000462 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000463 }
464
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000465 void onHidden (const DagInit* d) {
466 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000467 optDesc_.setHidden();
468 }
469
470 void onReallyHidden (const DagInit* d) {
471 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000472 optDesc_.setReallyHidden();
473 }
474
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000475 void onRequired (const DagInit* d) {
476 checkNumberOfArguments(d, 0);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000477 if (optDesc_.isOneOrMore())
478 throw std::string("An option can't have both (required) "
479 "and (one_or_more) properties!");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000480 optDesc_.setRequired();
481 }
482
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000483 void onOneOrMore (const DagInit* d) {
484 checkNumberOfArguments(d, 0);
485 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
486 throw std::string("Only one of (required), (zero_or_one) or "
487 "(one_or_more) properties is allowed!");
488 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbard4287062009-07-03 00:10:29 +0000489 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000490 "on a non-list option will have no effect.\n";
491 optDesc_.setOneOrMore();
492 }
493
494 void onZeroOrOne (const DagInit* d) {
495 checkNumberOfArguments(d, 0);
496 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
497 throw std::string("Only one of (required), (zero_or_one) or "
498 "(one_or_more) properties is allowed!");
499 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbard4287062009-07-03 00:10:29 +0000500 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000501 "on a non-list option will have no effect.\n";
502 optDesc_.setZeroOrOne();
503 }
504
505 void onMultiVal (const DagInit* d) {
506 checkNumberOfArguments(d, 1);
507 int val = InitPtrToInt(d->getArg(0));
508 if (val < 2)
509 throw std::string("Error in the 'multi_val' property: "
510 "the value must be greater than 1!");
511 if (!OptionType::IsList(optDesc_.Type))
512 throw std::string("The multi_val property is valid only "
513 "on list options!");
514 optDesc_.MultiVal = val;
515 }
516
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000517};
518
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000519/// AddOption - A function object that is applied to every option
520/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000521class AddOption {
522private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000523 OptionDescriptions& OptDescs_;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000524
525public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000526 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000527 {}
528
529 void operator()(const Init* i) {
530 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000531 checkNumberOfArguments(&d, 1);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000532
533 const OptionType::OptionType Type =
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000534 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000535 const std::string& Name = InitPtrToString(d.getArg(0));
536
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000537 OptionDescription OD(Type, Name);
538
539 if (!OD.isExtern())
540 checkNumberOfArguments(&d, 2);
541
542 if (OD.isAlias()) {
543 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000544 OD.Help = InitPtrToString(d.getArg(1));
545 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000546 else if (!OD.isExtern()) {
547 processOptionProperties(&d, OD);
548 }
549 OptDescs_.InsertDescription(OD);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000550 }
551
552private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000553 /// processOptionProperties - Go through the list of option
554 /// properties and call a corresponding handler for each.
555 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
556 checkNumberOfArguments(d, 2);
557 DagInit::const_arg_iterator B = d->arg_begin();
558 // Skip the first argument: it's always the option name.
559 ++B;
560 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000561 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000562
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000563};
564
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000565/// CollectOptionDescriptions - Collects option properties from all
566/// OptionLists.
567void CollectOptionDescriptions (RecordVector::const_iterator B,
568 RecordVector::const_iterator E,
569 OptionDescriptions& OptDescs)
570{
571 // For every OptionList:
572 for (; B!=E; ++B) {
573 RecordVector::value_type T = *B;
574 // Throws an exception if the value does not exist.
575 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000576
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000577 // For every option description in this list:
578 // collect the information and
579 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
580 }
581}
582
583// Tool information record
584
585namespace ToolFlags {
586 enum ToolFlags { Join = 0x1, Sink = 0x2 };
587}
588
589struct ToolDescription : public RefCountedBase<ToolDescription> {
590 std::string Name;
591 Init* CmdLine;
592 Init* Actions;
593 StrVector InLanguage;
594 std::string OutLanguage;
595 std::string OutputSuffix;
596 unsigned Flags;
597
598 // Various boolean properties
599 void setSink() { Flags |= ToolFlags::Sink; }
600 bool isSink() const { return Flags & ToolFlags::Sink; }
601 void setJoin() { Flags |= ToolFlags::Join; }
602 bool isJoin() const { return Flags & ToolFlags::Join; }
603
604 // Default ctor here is needed because StringMap can only store
605 // DefaultConstructible objects
606 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
607 ToolDescription (const std::string& n)
608 : Name(n), CmdLine(0), Actions(0), Flags(0)
609 {}
610};
611
612/// ToolDescriptions - A list of Tool information records.
613typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
614
615
616/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000617/// tool property records.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000618class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000619private:
620
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000621 /// toolDesc_ - Properties of the current Tool. This is where the
622 /// information is stored.
623 ToolDescription& toolDesc_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000624
625public:
626
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000627 explicit CollectToolProperties (ToolDescription& d)
628 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000629 {
630 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000631
632 AddHandler("actions", &CollectToolProperties::onActions);
633 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
634 AddHandler("in_language", &CollectToolProperties::onInLanguage);
635 AddHandler("join", &CollectToolProperties::onJoin);
636 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
637 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
638 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000639
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000640 staticMembersInitialized_ = true;
641 }
642 }
643
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000644private:
645
646 /// Property handlers --
647 /// Functions that extract information about tool properties from
648 /// DAG representation.
649
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000650 void onActions (const DagInit* d) {
651 checkNumberOfArguments(d, 1);
Mikhail Glushenkov8d489a82008-12-07 16:45:12 +0000652 Init* Case = d->getArg(0);
653 if (typeid(*Case) != typeid(DagInit) ||
654 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
655 throw
656 std::string("The argument to (actions) should be a 'case' construct!");
657 toolDesc_.Actions = Case;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000658 }
659
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000660 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000661 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000662 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000663 }
664
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000665 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000666 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000667 Init* arg = d->getArg(0);
668
669 // Find out the argument's type.
670 if (typeid(*arg) == typeid(StringInit)) {
671 // It's a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000672 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000673 }
674 else {
675 // It's a list.
676 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000677 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000678
679 // Copy strings to the output vector.
680 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
681 B != E; ++B) {
682 out.push_back(InitPtrToString(*B));
683 }
684
685 // Remove duplicates.
686 std::sort(out.begin(), out.end());
687 StrVector::iterator newE = std::unique(out.begin(), out.end());
688 out.erase(newE, out.end());
689 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000690 }
691
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000692 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000693 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000694 toolDesc_.setJoin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000695 }
696
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000697 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000698 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000699 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000700 }
701
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000702 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000703 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000704 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000705 }
706
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000707 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000708 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000709 toolDesc_.setSink();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000710 }
711
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000712};
713
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000714/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000715/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000716/// CollectToolProperties function object).
717void CollectToolDescriptions (RecordVector::const_iterator B,
718 RecordVector::const_iterator E,
719 ToolDescriptions& ToolDescs)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000720{
721 // Iterate over a properties list of every Tool definition
722 for (;B!=E;++B) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +0000723 const Record* T = *B;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000724 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000725 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000726
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000727 IntrusiveRefCntPtr<ToolDescription>
728 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000729
730 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000731 CollectToolProperties(*ToolDesc));
732 ToolDescs.push_back(ToolDesc);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000733 }
734}
735
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000736/// FillInEdgeVector - Merge all compilation graph definitions into
737/// one single edge list.
738void FillInEdgeVector(RecordVector::const_iterator B,
739 RecordVector::const_iterator E, RecordVector& Out) {
740 for (; B != E; ++B) {
741 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000742
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000743 for (unsigned i = 0; i < edges->size(); ++i)
744 Out.push_back(edges->getElementAsRecord(i));
745 }
746}
747
748/// CalculatePriority - Calculate the priority of this plugin.
749int CalculatePriority(RecordVector::const_iterator B,
750 RecordVector::const_iterator E) {
751 int total = 0;
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +0000752 for (; B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000753 total += static_cast<int>((*B)->getValueAsInt("priority"));
754 }
755 return total;
756}
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000757
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000758/// NotInGraph - Helper function object for FilterNotInGraph.
759struct NotInGraph {
760private:
761 const llvm::StringSet<>& ToolsInGraph_;
762
763public:
764 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
765 : ToolsInGraph_(ToolsInGraph)
766 {}
767
768 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
769 return (ToolsInGraph_.count(x->Name) == 0);
770 }
771};
772
773/// FilterNotInGraph - Filter out from ToolDescs all Tools not
774/// mentioned in the compilation graph definition.
775void FilterNotInGraph (const RecordVector& EdgeVector,
776 ToolDescriptions& ToolDescs) {
777
778 // List all tools mentioned in the graph.
779 llvm::StringSet<> ToolsInGraph;
780
781 for (RecordVector::const_iterator B = EdgeVector.begin(),
782 E = EdgeVector.end(); B != E; ++B) {
783
784 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000785 const std::string& NodeA = Edge->getValueAsString("a");
786 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000787
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000788 if (NodeA != "root")
789 ToolsInGraph.insert(NodeA);
790 ToolsInGraph.insert(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000791 }
792
793 // Filter ToolPropertiesList.
794 ToolDescriptions::iterator new_end =
795 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
796 NotInGraph(ToolsInGraph));
797 ToolDescs.erase(new_end, ToolDescs.end());
798}
799
800/// FillInToolToLang - Fills in two tables that map tool names to
801/// (input, output) languages. Helper function used by TypecheckGraph().
802void FillInToolToLang (const ToolDescriptions& ToolDescs,
803 StringMap<StringSet<> >& ToolToInLang,
804 StringMap<std::string>& ToolToOutLang) {
805 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
806 E = ToolDescs.end(); B != E; ++B) {
807 const ToolDescription& D = *(*B);
808 for (StrVector::const_iterator B = D.InLanguage.begin(),
809 E = D.InLanguage.end(); B != E; ++B)
810 ToolToInLang[D.Name].insert(*B);
811 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000812 }
813}
814
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000815/// TypecheckGraph - Check that names for output and input languages
816/// on all edges do match. This doesn't do much when the information
817/// about the whole graph is not available (i.e. when compiling most
818/// plugins).
819void TypecheckGraph (const RecordVector& EdgeVector,
820 const ToolDescriptions& ToolDescs) {
821 StringMap<StringSet<> > ToolToInLang;
822 StringMap<std::string> ToolToOutLang;
823
824 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
825 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
826 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
827
828 for (RecordVector::const_iterator B = EdgeVector.begin(),
829 E = EdgeVector.end(); B != E; ++B) {
830 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000831 const std::string& NodeA = Edge->getValueAsString("a");
832 const std::string& NodeB = Edge->getValueAsString("b");
833 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
834 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000835
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000836 if (NodeA != "root") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000837 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000838 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000839 + ": output->input language mismatch";
840 }
841
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000842 if (NodeB == "root")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000843 throw std::string("Edges back to the root are not allowed!");
844 }
845}
846
847/// WalkCase - Walks the 'case' expression DAG and invokes
848/// TestCallback on every test, and StatementCallback on every
849/// statement. Handles 'case' nesting, but not the 'and' and 'or'
850/// combinators.
851// TODO: Re-implement EmitCaseConstructHandler on top of this function?
852template <typename F1, typename F2>
853void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
854 const DagInit& d = InitPtrToDag(Case);
855 bool even = false;
856 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
857 B != E; ++B) {
858 Init* arg = *B;
859 if (even && dynamic_cast<DagInit*>(arg)
860 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
861 WalkCase(arg, TestCallback, StatementCallback);
862 else if (!even)
863 TestCallback(arg);
864 else
865 StatementCallback(arg);
866 even = !even;
867 }
868}
869
870/// ExtractOptionNames - A helper function object used by
871/// CheckForSuperfluousOptions() to walk the 'case' DAG.
872class ExtractOptionNames {
873 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000874
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000875 void processDag(const Init* Statement) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000876 const DagInit& Stmt = InitPtrToDag(Statement);
877 const std::string& ActionName = Stmt.getOperator()->getAsString();
878 if (ActionName == "forward" || ActionName == "forward_as" ||
879 ActionName == "unpack_values" || ActionName == "switch_on" ||
880 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000881 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000882 checkNumberOfArguments(&Stmt, 1);
883 const std::string& Name = InitPtrToString(Stmt.getArg(0));
884 OptionNames_.insert(Name);
885 }
886 else if (ActionName == "and" || ActionName == "or") {
887 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000888 this->processDag(Stmt.getArg(i));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000889 }
890 }
891 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000892
893public:
894 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
895 {}
896
897 void operator()(const Init* Statement) {
898 if (typeid(*Statement) == typeid(ListInit)) {
899 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
900 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
901 B != E; ++B)
902 this->processDag(*B);
903 }
904 else {
905 this->processDag(Statement);
906 }
907 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000908};
909
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000910/// CheckForSuperfluousOptions - Check that there are no side
911/// effect-free options (specified only in the OptionList). Otherwise,
912/// output a warning.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000913void CheckForSuperfluousOptions (const RecordVector& Edges,
914 const ToolDescriptions& ToolDescs,
915 const OptionDescriptions& OptDescs) {
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000916 llvm::StringSet<> nonSuperfluousOptions;
917
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000918 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000919 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000920 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
921 E = ToolDescs.end(); B != E; ++B) {
922 const ToolDescription& TD = *(*B);
923 ExtractOptionNames Callback(nonSuperfluousOptions);
924 if (TD.Actions)
925 WalkCase(TD.Actions, Callback, Callback);
926 }
927
928 // Add all options mentioned in the 'case' clauses of the
929 // OptionalEdges of the compilation graph to the set of
930 // non-superfluous options.
931 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
932 B != E; ++B) {
933 const Record* Edge = *B;
934 DagInit* Weight = Edge->getValueAsDag("weight");
935
936 if (!isDagEmpty(Weight))
937 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000938 }
939
940 // Check that all options in OptDescs belong to the set of
941 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000942 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000943 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000944 const OptionDescription& Val = B->second;
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000945 if (!nonSuperfluousOptions.count(Val.Name)
946 && Val.Type != OptionType::Alias)
Daniel Dunbard4287062009-07-03 00:10:29 +0000947 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000948 "Probable cause: this option is specified only in the OptionList.\n";
949 }
950}
951
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000952/// EmitCaseTest1Arg - Helper function used by
953/// EmitCaseConstructHandler.
954bool EmitCaseTest1Arg(const std::string& TestName,
955 const DagInit& d,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000956 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +0000957 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000958 checkNumberOfArguments(&d, 1);
959 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000960
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000961 if (TestName == "switch_on") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000962 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
963 if (!OptionType::IsSwitch(OptDesc.Type))
964 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000965 O << OptDesc.GenVariableName();
966 return true;
967 } else if (TestName == "input_languages_contain") {
968 O << "InLangs.count(\"" << OptName << "\") != 0";
969 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000970 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000971 // This works only for single-argument Tool::GenerateAction. Join
972 // tools can process several files in different languages simultaneously.
973
974 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000975 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000976 return true;
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000977 } else if (TestName == "not_empty" || TestName == "empty") {
978 const char* Test = (TestName == "empty") ? "" : "!";
979
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000980 if (OptName == "o") {
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000981 O << Test << "OutputFilename.empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000982 return true;
983 }
984 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000985 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
986 if (OptionType::IsSwitch(OptDesc.Type))
987 throw OptName
988 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000989 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000990 return true;
991 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000992 }
993
994 return false;
995}
996
997/// EmitCaseTest2Args - Helper function used by
998/// EmitCaseConstructHandler.
999bool EmitCaseTest2Args(const std::string& TestName,
1000 const DagInit& d,
1001 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001002 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001003 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001004 checkNumberOfArguments(&d, 2);
1005 const std::string& OptName = InitPtrToString(d.getArg(0));
1006 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001007 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001008
1009 if (TestName == "parameter_equals") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001010 if (!OptionType::IsParameter(OptDesc.Type))
1011 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001012 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1013 return true;
1014 }
1015 else if (TestName == "element_in_list") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001016 if (!OptionType::IsList(OptDesc.Type))
1017 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001018 const std::string& VarName = OptDesc.GenVariableName();
1019 O << "std::find(" << VarName << ".begin(),\n"
1020 << IndentLevel << Indent1 << VarName << ".end(), \""
1021 << OptArg << "\") != " << VarName << ".end()";
1022 return true;
1023 }
1024
1025 return false;
1026}
1027
1028// Forward declaration.
1029// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1030void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001031 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001032 raw_ostream& O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001033
1034/// EmitLogicalOperationTest - Helper function used by
1035/// EmitCaseConstructHandler.
1036void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1037 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001038 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001039 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001040 O << '(';
1041 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001042 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001043 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1044 if (j != NumArgs - 1)
1045 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
1046 else
1047 O << ')';
1048 }
1049}
1050
1051/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1052void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001053 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001054 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001055 const std::string& TestName = d.getOperator()->getAsString();
1056
1057 if (TestName == "and")
1058 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1059 else if (TestName == "or")
1060 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
1061 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1062 return;
1063 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1064 return;
1065 else
1066 throw TestName + ": unknown edge property!";
1067}
1068
1069// Emit code that handles the 'case' construct.
1070// Takes a function object that should emit code for every case clause.
1071// Callback's type is
Daniel Dunbard4287062009-07-03 00:10:29 +00001072// void F(Init* Statement, const char* IndentLevel, raw_ostream& O).
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001073template <typename F>
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001074void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +00001075 F Callback, bool EmitElseIf,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001076 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001077 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001078 const DagInit* d = &InitPtrToDag(Dag);
1079 if (d->getOperator()->getAsString() != "case")
1080 throw std::string("EmitCaseConstructHandler should be invoked"
1081 " only on 'case' expressions!");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001082
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001083 unsigned numArgs = d->getNumArgs();
1084 if (d->getNumArgs() < 2)
1085 throw "There should be at least one clause in the 'case' expression:\n"
1086 + d->getAsString();
1087
1088 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001089 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001090
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001091 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001092 if (Test.getOperator()->getAsString() == "default") {
1093 if (i+2 != numArgs)
1094 throw std::string("The 'default' clause should be the last in the"
1095 "'case' construct!");
1096 O << IndentLevel << "else {\n";
1097 }
1098 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001099 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001100 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1101 O << ") {\n";
1102 }
1103
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001104 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001105 ++i;
1106 if (i == numArgs)
1107 throw "Case construct handler: no corresponding action "
1108 "found for the test " + Test.getAsString() + '!';
1109
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001110 Init* arg = d->getArg(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001111 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1112 if (nd && (nd->getOperator()->getAsString() == "case")) {
1113 // Handle the nested 'case'.
1114 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001115 Callback, EmitElseIf, OptDescs, O);
1116 }
1117 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001118 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001119 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001120 O << IndentLevel << "}\n";
1121 }
1122}
1123
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001124/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1125/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1126/// Helper function used by EmitCmdLineVecFill and.
1127void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1128 const char* Delimiters = " \t\n\v\f\r";
1129 enum TokenizerState
1130 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1131 cur_st = Normal;
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001132
1133 if (CmdLine.empty())
1134 return;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001135 Out.push_back("");
1136
1137 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1138 E = CmdLine.size();
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001139
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001140 for (; B != E; ++B) {
1141 char cur_ch = CmdLine[B];
1142
1143 switch (cur_st) {
1144 case Normal:
1145 if (cur_ch == '$') {
1146 cur_st = SpecialCommand;
1147 break;
1148 }
1149 if (oneOf(Delimiters, cur_ch)) {
1150 // Skip whitespace
1151 B = CmdLine.find_first_not_of(Delimiters, B);
1152 if (B == std::string::npos) {
1153 B = E-1;
1154 continue;
1155 }
1156 --B;
1157 Out.push_back("");
1158 continue;
1159 }
1160 break;
1161
1162
1163 case SpecialCommand:
1164 if (oneOf(Delimiters, cur_ch)) {
1165 cur_st = Normal;
1166 Out.push_back("");
1167 continue;
1168 }
1169 if (cur_ch == '(') {
1170 Out.push_back("");
1171 cur_st = InsideSpecialCommand;
1172 continue;
1173 }
1174 break;
1175
1176 case InsideSpecialCommand:
1177 if (oneOf(Delimiters, cur_ch)) {
1178 continue;
1179 }
1180 if (cur_ch == '\'') {
1181 cur_st = InsideQuotationMarks;
1182 Out.push_back("");
1183 continue;
1184 }
1185 if (cur_ch == ')') {
1186 cur_st = Normal;
1187 Out.push_back("");
1188 }
1189 if (cur_ch == ',') {
1190 continue;
1191 }
1192
1193 break;
1194
1195 case InsideQuotationMarks:
1196 if (cur_ch == '\'') {
1197 cur_st = InsideSpecialCommand;
1198 continue;
1199 }
1200 break;
1201 }
1202
1203 Out.back().push_back(cur_ch);
1204 }
1205}
1206
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001207/// SubstituteSpecialCommands - Perform string substitution for $CALL
1208/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001209StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbard4287062009-07-03 00:10:29 +00001210(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001211{
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001212
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001213 const std::string& cmd = *Pos;
1214
1215 if (cmd == "$CALL") {
1216 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1217 const std::string& CmdName = *Pos;
1218
1219 if (CmdName == ")")
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001220 throw std::string("$CALL invocation: empty argument list!");
1221
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001222 O << "hooks::";
1223 O << CmdName << "(";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001224
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001225
1226 bool firstIteration = true;
1227 while (true) {
1228 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1229 const std::string& Arg = *Pos;
1230 assert(Arg.size() != 0);
1231
1232 if (Arg[0] == ')')
1233 break;
1234
1235 if (firstIteration)
1236 firstIteration = false;
1237 else
1238 O << ", ";
1239
1240 O << '"' << Arg << '"';
1241 }
1242
1243 O << ')';
1244
1245 }
1246 else if (cmd == "$ENV") {
1247 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1248 const std::string& EnvName = *Pos;
1249
1250 if (EnvName == ")")
1251 throw "$ENV invocation: empty argument list!";
1252
1253 O << "checkCString(std::getenv(\"";
1254 O << EnvName;
1255 O << "\"))";
1256
1257 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001258 }
1259 else {
1260 throw "Unknown special command: " + cmd;
1261 }
1262
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001263 const std::string& Leftover = *Pos;
1264 assert(Leftover.at(0) == ')');
1265 if (Leftover.size() != 1)
1266 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001267
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001268 return Pos;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001269}
1270
1271/// EmitCmdLineVecFill - Emit code that fills in the command line
1272/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001273void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001274 bool IsJoin, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001275 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001276 StrVector StrVec;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001277 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1278
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001279 if (StrVec.empty())
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001280 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001281
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001282 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1283
1284 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001285 assert(!StrVec[0].empty());
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001286 if (StrVec[0][0] == '$') {
1287 while (I != E && (*I)[0] != ')' )
1288 ++I;
1289
1290 // Skip the ')' symbol.
1291 ++I;
1292 }
1293 else {
1294 ++I;
1295 }
1296
1297 for (; I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001298 const std::string& cmd = *I;
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001299 assert(!cmd.empty());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001300 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001301 if (cmd.at(0) == '$') {
1302 if (cmd == "$INFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001303 if (IsJoin)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001304 O << "for (PathVector::const_iterator B = inFiles.begin()"
1305 << ", E = inFiles.end();\n"
1306 << IndentLevel << "B != E; ++B)\n"
1307 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1308 else
1309 O << "vec.push_back(inFile.toString());\n";
1310 }
1311 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001312 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001313 }
1314 else {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001315 O << "vec.push_back(";
1316 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001317 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001318 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001319 }
1320 else {
1321 O << "vec.push_back(\"" << cmd << "\");\n";
1322 }
1323 }
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001324 O << IndentLevel << "cmd = ";
1325
1326 if (StrVec[0][0] == '$')
1327 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1328 else
1329 O << '"' << StrVec[0] << '"';
1330 O << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001331}
1332
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001333/// EmitCmdLineVecFillCallback - A function object wrapper around
1334/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1335/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001336class EmitCmdLineVecFillCallback {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001337 bool IsJoin;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001338 const std::string& ToolName;
1339 public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001340 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1341 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001342
1343 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001344 raw_ostream& O) const
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001345 {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001346 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1347 IndentLevel, O);
1348 }
1349};
1350
1351/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1352/// implement EmitActionHandler. Emits code for
1353/// handling the (forward) and (forward_as) option properties.
1354void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1355 const char* Indent,
1356 const std::string& NewName,
Daniel Dunbard4287062009-07-03 00:10:29 +00001357 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001358 const std::string& Name = NewName.empty()
1359 ? ("-" + D.Name)
1360 : NewName;
1361
1362 switch (D.Type) {
1363 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001364 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1365 break;
1366 case OptionType::Parameter:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001367 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1368 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1369 break;
1370 case OptionType::Prefix:
1371 O << Indent << "vec.push_back(\"" << Name << "\" + "
1372 << D.GenVariableName() << ");\n";
1373 break;
1374 case OptionType::PrefixList:
1375 O << Indent << "for (" << D.GenTypeDeclaration()
1376 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001377 << Indent << "E = " << D.GenVariableName() << ".end(); B != E;) {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001378 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001379 << "*B);\n"
1380 << Indent << Indent1 << "++B;\n";
1381
1382 for (int i = 1, j = D.MultiVal; i < j; ++i) {
1383 O << Indent << Indent1 << "vec.push_back(*B);\n"
1384 << Indent << Indent1 << "++B;\n";
1385 }
1386
1387 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001388 break;
1389 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001390 O << Indent << "for (" << D.GenTypeDeclaration()
1391 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1392 << Indent << "E = " << D.GenVariableName()
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001393 << ".end() ; B != E;) {\n"
1394 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n";
1395
1396 for (int i = 0, j = D.MultiVal; i < j; ++i) {
1397 O << Indent << Indent1 << "vec.push_back(*B);\n"
1398 << Indent << Indent1 << "++B;\n";
1399 }
1400
1401 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001402 break;
1403 case OptionType::Alias:
1404 default:
1405 throw std::string("Aliases are not allowed in tool option descriptions!");
1406 }
1407}
1408
1409/// EmitActionHandler - Emit code that handles actions. Used by
1410/// EmitGenerateActionMethod() as an argument to
1411/// EmitCaseConstructHandler().
1412class EmitActionHandler {
1413 const OptionDescriptions& OptDescs;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001414
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001415 void processActionDag(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001416 raw_ostream& O) const
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001417 {
1418 const DagInit& Dag = InitPtrToDag(Statement);
1419 const std::string& ActionName = Dag.getOperator()->getAsString();
1420
1421 if (ActionName == "append_cmd") {
1422 checkNumberOfArguments(&Dag, 1);
1423 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovfa8787d2009-02-27 06:46:55 +00001424 StrVector Out;
1425 llvm::SplitString(Cmd, Out);
1426
1427 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1428 B != E; ++B)
1429 O << IndentLevel << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001430 }
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001431 else if (ActionName == "error") {
1432 O << IndentLevel << "throw std::runtime_error(\"" <<
1433 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1434 : "Unknown error!")
1435 << "\");\n";
1436 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001437 else if (ActionName == "forward") {
1438 checkNumberOfArguments(&Dag, 1);
1439 const std::string& Name = InitPtrToString(Dag.getArg(0));
1440 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1441 IndentLevel, "", O);
1442 }
1443 else if (ActionName == "forward_as") {
1444 checkNumberOfArguments(&Dag, 2);
1445 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkov09699552009-05-06 01:41:19 +00001446 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001447 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1448 IndentLevel, NewName, O);
1449 }
1450 else if (ActionName == "output_suffix") {
1451 checkNumberOfArguments(&Dag, 1);
1452 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1453 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1454 }
1455 else if (ActionName == "stop_compilation") {
1456 O << IndentLevel << "stop_compilation = true;\n";
1457 }
1458 else if (ActionName == "unpack_values") {
1459 checkNumberOfArguments(&Dag, 1);
1460 const std::string& Name = InitPtrToString(Dag.getArg(0));
1461 const OptionDescription& D = OptDescs.FindOption(Name);
1462
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001463 if (D.isMultiVal())
1464 throw std::string("Can't use unpack_values with multi-valued options!");
1465
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001466 if (OptionType::IsList(D.Type)) {
1467 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1468 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1469 << IndentLevel << "E = " << D.GenVariableName()
1470 << ".end(); B != E; ++B)\n"
1471 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1472 }
1473 else if (OptionType::IsParameter(D.Type)){
1474 O << Indent3 << "llvm::SplitString("
1475 << D.GenVariableName() << ", vec, \",\");\n";
1476 }
1477 else {
1478 throw "Option '" + D.Name +
1479 "': switches can't have the 'unpack_values' property!";
1480 }
1481 }
1482 else {
1483 throw "Unknown action name: " + ActionName + "!";
1484 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001485 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001486 public:
1487 EmitActionHandler(const OptionDescriptions& OD)
1488 : OptDescs(OD) {}
1489
1490 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001491 raw_ostream& O) const
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001492 {
1493 if (typeid(*Statement) == typeid(ListInit)) {
1494 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1495 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1496 B != E; ++B)
1497 this->processActionDag(*B, IndentLevel, O);
1498 }
1499 else {
1500 this->processActionDag(Statement, IndentLevel, O);
1501 }
1502 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001503};
1504
1505// EmitGenerateActionMethod - Emit one of two versions of the
1506// Tool::GenerateAction() method.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001507void EmitGenerateActionMethod (const ToolDescription& D,
1508 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001509 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001510 if (IsJoin)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001511 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1512 else
1513 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1514
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001515 O << Indent2 << "bool HasChildren,\n"
1516 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001517 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1518 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001519 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001520 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001521 << Indent2 << "std::vector<std::string> vec;\n"
1522 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1523 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1524 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001525
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001526 // For every understood option, emit handling code.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001527 if (D.Actions)
1528 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1529 false, OptDescs, O);
1530
1531 O << '\n' << Indent2
1532 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
1533 << Indent3 << "TempDir, stop_compilation, output_suffix).toString();\n\n";
1534
1535 // cmd_line is either a string or a 'case' construct.
1536 if (!D.CmdLine)
1537 throw "Tool " + D.Name + " has no cmd_line property!";
1538 else if (typeid(*D.CmdLine) == typeid(StringInit))
1539 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1540 else
1541 EmitCaseConstructHandler(D.CmdLine, Indent2,
1542 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1543 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001544
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001545 // Handle the Sink property.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001546 if (D.isSink()) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001547 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1548 << Indent3 << "vec.insert(vec.end(), "
1549 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1550 << Indent2 << "}\n";
1551 }
1552
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001553 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001554 << Indent1 << "}\n\n";
1555}
1556
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001557/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1558/// a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001559void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1560 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001561 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001562 if (!ToolDesc.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001563 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001564 << Indent2 << "bool HasChildren,\n"
1565 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001566 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1567 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001568 << Indent1 << "{\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001569 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001570 << " is not a Join tool!\");\n"
1571 << Indent1 << "}\n\n";
1572 else
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001573 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001574
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001575 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001576}
1577
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001578/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1579/// methods for a given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001580void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001581 O << Indent1 << "const char** InputLanguages() const {\n"
1582 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001583 << Indent1 << "}\n\n";
1584
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001585 if (D.OutLanguage.empty())
1586 throw "Tool " + D.Name + " has no 'out_language' property!";
1587
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001588 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001589 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001590 << Indent1 << "}\n\n";
1591}
1592
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001593/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001594void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001595 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001596 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001597 << Indent1 << "}\n\n";
1598}
1599
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001600/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1601/// class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001602void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001603 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001604 if (D.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001605 O << Indent2 << "return true;\n";
1606 else
1607 O << Indent2 << "return false;\n";
1608 O << Indent1 << "}\n\n";
1609}
1610
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001611/// EmitStaticMemberDefinitions - Emit static member definitions for a
1612/// given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001613void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001614 if (D.InLanguage.empty())
1615 throw "Tool " + D.Name + " has no 'in_language' property!";
1616
1617 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1618 for (StrVector::const_iterator B = D.InLanguage.begin(),
1619 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001620 O << '\"' << *B << "\", ";
1621 O << "0};\n\n";
1622}
1623
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001624/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001625void EmitToolClassDefinition (const ToolDescription& D,
1626 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001627 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001628 if (D.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001629 return;
1630
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001631 // Header
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001632 O << "class " << D.Name << " : public ";
1633 if (D.isJoin())
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001634 O << "JoinTool";
1635 else
1636 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001637
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001638 O << "{\nprivate:\n"
1639 << Indent1 << "static const char* InputLanguages_[];\n\n";
1640
1641 O << "public:\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001642 EmitNameMethod(D, O);
1643 EmitInOutLanguageMethods(D, O);
1644 EmitIsJoinMethod(D, O);
1645 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001646
1647 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001648 O << "};\n";
1649
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001650 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001651
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001652}
1653
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001654/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001655/// and emit registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001656void EmitOptionDefinitions (const OptionDescriptions& descs,
1657 bool HasSink, bool HasExterns,
Daniel Dunbard4287062009-07-03 00:10:29 +00001658 raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001659{
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001660 std::vector<OptionDescription> Aliases;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001661
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001662 // Emit static cl::Option variables.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001663 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001664 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001665 const OptionDescription& val = B->second;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001666
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001667 if (val.Type == OptionType::Alias) {
1668 Aliases.push_back(val);
1669 continue;
1670 }
1671
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001672 if (val.isExtern())
1673 O << "extern ";
1674
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001675 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001676 << val.GenVariableName();
1677
1678 if (val.isExtern()) {
1679 O << ";\n";
1680 continue;
1681 }
1682
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001683 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001684
1685 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1686 O << ", cl::Prefix";
1687
1688 if (val.isRequired()) {
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001689 if (OptionType::IsList(val.Type) && !val.isMultiVal())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001690 O << ", cl::OneOrMore";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001691 else
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001692 O << ", cl::Required";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001693 }
1694 else if (val.isOneOrMore() && OptionType::IsList(val.Type)) {
1695 O << ", cl::OneOrMore";
1696 }
1697 else if (val.isZeroOrOne() && OptionType::IsList(val.Type)) {
1698 O << ", cl::ZeroOrOne";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001699 }
1700
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001701 if (val.isReallyHidden()) {
1702 O << ", cl::ReallyHidden";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001703 }
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001704 else if (val.isHidden()) {
1705 O << ", cl::Hidden";
1706 }
1707
1708 if (val.MultiVal > 1)
1709 O << ", cl::multi_val(" << val.MultiVal << ")";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001710
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001711 if (!val.Help.empty())
1712 O << ", cl::desc(\"" << val.Help << "\")";
1713
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001714 O << ");\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001715 }
1716
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001717 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001718 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001719 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001720 const OptionDescription& val = *B;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001721
1722 O << val.GenTypeDeclaration() << ' '
1723 << val.GenVariableName()
1724 << "(\"" << val.Name << '\"';
1725
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001726 const OptionDescription& D = descs.FindOption(val.Help);
1727 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001728
1729 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1730 }
1731
1732 // Emit the sink option.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001733 if (HasSink)
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001734 O << (HasExterns ? "extern cl" : "cl")
1735 << "::list<std::string> " << SinkOptionName
1736 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001737
1738 O << '\n';
1739}
1740
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001741/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbard4287062009-07-03 00:10:29 +00001742void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001743{
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001744 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001745 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001746
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001747 // Get the relevant field out of RecordKeeper
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001748 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001749
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001750 // It is allowed for a plugin to have no language map.
1751 if (LangMapRecord) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001752
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001753 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1754 if (!LangsToSuffixesList)
1755 throw std::string("Error in the language map definition!");
1756
1757 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001758 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001759
1760 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1761 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1762
1763 for (unsigned i = 0; i < Suffixes->size(); ++i)
1764 O << Indent1 << "langMap[\""
1765 << InitPtrToString(Suffixes->getElement(i))
1766 << "\"] = \"" << Lang << "\";\n";
1767 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001768 }
1769
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001770 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001771}
1772
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001773/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1774/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001775void IncDecWeight (const Init* i, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001776 raw_ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001777 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001778 const std::string& OpName = d.getOperator()->getAsString();
1779
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001780 if (OpName == "inc_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001781 O << IndentLevel << "ret += ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001782 }
1783 else if (OpName == "dec_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001784 O << IndentLevel << "ret -= ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001785 }
1786 else if (OpName == "error") {
1787 O << IndentLevel << "throw std::runtime_error(\"" <<
1788 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1789 : "Unknown error!")
1790 << "\");\n";
1791 return;
1792 }
1793
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001794 else
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001795 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov55c6bdf2008-12-18 04:06:58 +00001796 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001797
1798 if (d.getNumArgs() > 0)
1799 O << InitPtrToInt(d.getArg(0)) << ";\n";
1800 else
1801 O << "2;\n";
1802
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001803}
1804
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001805/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001806void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001807 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001808 raw_ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001809
1810 // Class constructor.
1811 O << "class Edge" << N << ": public Edge {\n"
1812 << "public:\n"
1813 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1814 << "\") {}\n\n"
1815
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001816 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001817 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001818 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001819
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001820 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001821 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001822
1823 O << Indent2 << "return ret;\n"
1824 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001825}
1826
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001827/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001828void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001829 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001830 raw_ostream& O) {
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001831 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001832 for (RecordVector::const_iterator B = EdgeVector.begin(),
1833 E = EdgeVector.end(); B != E; ++B) {
1834 const Record* Edge = *B;
1835 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001836 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001837
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001838 if (!isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001839 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001840 ++i;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001841 }
1842}
1843
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001844/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1845/// function.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001846void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001847 const ToolDescriptions& ToolDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001848 raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001849{
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001850 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001851
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001852 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1853 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001854 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001855
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001856 O << '\n';
1857
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001858 // Insert edges.
1859
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001860 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001861 for (RecordVector::const_iterator B = EdgeVector.begin(),
1862 E = EdgeVector.end(); B != E; ++B) {
1863 const Record* Edge = *B;
1864 const std::string& NodeA = Edge->getValueAsString("a");
1865 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001866 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001867
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001868 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001869
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001870 if (isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001871 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001872 else
1873 O << "new Edge" << i << "()";
1874
1875 O << ");\n";
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001876 ++i;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001877 }
1878
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001879 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001880}
1881
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001882/// ExtractHookNames - Extract the hook names from all instances of
1883/// $CALL(HookName) in the provided command line string. Helper
1884/// function used by FillInHookNames().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001885class ExtractHookNames {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001886 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001887public:
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001888 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
1889 : HookNames_(HookNames) {}
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001890
1891 void operator()(const Init* CmdLine) {
1892 StrVector cmds;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001893 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001894 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1895 B != E; ++B) {
1896 const std::string& cmd = *B;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001897
1898 if (cmd == "$CALL") {
1899 unsigned NumArgs = 0;
1900 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
1901 const std::string& HookName = *B;
1902
1903
1904 if (HookName.at(0) == ')')
1905 throw "$CALL invoked with no arguments!";
1906
1907 while (++B != E && B->at(0) != ')') {
1908 ++NumArgs;
1909 }
1910
1911 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
1912
1913 if (H != HookNames_.end() && H->second != NumArgs)
1914 throw "Overloading of hooks is not allowed. Overloaded hook: "
1915 + HookName;
1916 else
1917 HookNames_[HookName] = NumArgs;
1918
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001919 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001920 }
1921 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001922};
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001923
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001924/// FillInHookNames - Actually extract the hook names from all command
1925/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001926void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001927 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001928{
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001929 // For all command lines:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001930 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1931 E = ToolDescs.end(); B != E; ++B) {
1932 const ToolDescription& D = *(*B);
1933 if (!D.CmdLine)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001934 continue;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001935 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001936 // This is a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001937 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001938 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001939 // This is a 'case' construct.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001940 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001941 }
1942}
1943
1944/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1945/// property records and emit hook function declaration for each
1946/// instance of $CALL(HookName).
Daniel Dunbard4287062009-07-03 00:10:29 +00001947void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001948 llvm::StringMap<unsigned> HookNames;
1949
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001950 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001951 if (HookNames.empty())
1952 return;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001953
1954 O << "namespace hooks {\n";
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001955 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
1956 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkoved765fe2009-01-21 13:04:33 +00001957 O << Indent1 << "std::string " << B->first() << "(";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001958
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001959 for (unsigned i = 0, j = B->second; i < j; ++i) {
1960 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
1961 }
1962
1963 O <<");\n";
1964 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001965 O << "}\n\n";
1966}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001967
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001968/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbard4287062009-07-03 00:10:29 +00001969void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001970 O << "struct Plugin : public llvmc::BasePlugin {\n\n"
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001971 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001972 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
1973 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
1974 << Indent1
1975 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
1976 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
1977 << "};\n\n"
1978
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001979 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001980}
1981
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001982/// EmitIncludes - Emit necessary #include directives and some
1983/// additional declarations.
Daniel Dunbard4287062009-07-03 00:10:29 +00001984void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001985 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00001986 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001987 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1988 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001989
1990 << "#include \"llvm/ADT/StringExtras.h\"\n"
1991 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1992
1993 << "#include <cstdlib>\n"
1994 << "#include <stdexcept>\n\n"
1995
1996 << "using namespace llvm;\n"
1997 << "using namespace llvmc;\n\n"
1998
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001999 << "extern cl::opt<std::string> OutputFilename;\n\n"
2000
2001 << "inline const char* checkCString(const char* s)\n"
2002 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00002003}
2004
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002005
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002006/// PluginData - Holds all information about a plugin.
2007struct PluginData {
2008 OptionDescriptions OptDescs;
2009 bool HasSink;
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002010 bool HasExterns;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002011 ToolDescriptions ToolDescs;
2012 RecordVector Edges;
2013 int Priority;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002014};
2015
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002016/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002017/// there are any with the 'sink' property set.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002018bool HasSink(const ToolDescriptions& ToolDescs) {
2019 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2020 E = ToolDescs.end(); B != E; ++B)
2021 if ((*B)->isSink())
2022 return true;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002023
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002024 return false;
2025}
2026
2027/// HasExterns - Go through the list of option descriptions and check
2028/// if there are any external options.
2029bool HasExterns(const OptionDescriptions& OptDescs) {
2030 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2031 E = OptDescs.end(); B != E; ++B)
2032 if (B->second.isExtern())
2033 return true;
2034
2035 return false;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002036}
2037
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002038/// CollectPluginData - Collect tool and option properties,
2039/// compilation graph edges and plugin priority from the parse tree.
2040void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2041 // Collect option properties.
2042 const RecordVector& OptionLists =
2043 Records.getAllDerivedDefinitions("OptionList");
2044 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2045 Data.OptDescs);
2046
2047 // Collect tool properties.
2048 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2049 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2050 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002051 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002052
2053 // Collect compilation graph edges.
2054 const RecordVector& CompilationGraphs =
2055 Records.getAllDerivedDefinitions("CompilationGraph");
2056 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2057 Data.Edges);
2058
2059 // Calculate the priority of this plugin.
2060 const RecordVector& Priorities =
2061 Records.getAllDerivedDefinitions("PluginPriority");
2062 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00002063}
2064
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002065/// CheckPluginData - Perform some sanity checks on the collected data.
2066void CheckPluginData(PluginData& Data) {
2067 // Filter out all tools not mentioned in the compilation graph.
2068 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002069
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002070 // Typecheck the compilation graph.
2071 TypecheckGraph(Data.Edges, Data.ToolDescs);
2072
2073 // Check that there are no options without side effects (specified
2074 // only in the OptionList).
2075 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2076
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002077}
2078
Daniel Dunbard4287062009-07-03 00:10:29 +00002079void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002080 // Emit file header.
2081 EmitIncludes(O);
2082
2083 // Emit global option registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00002084 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002085
2086 // Emit hook declarations.
2087 EmitHookDeclarations(Data.ToolDescs, O);
2088
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00002089 O << "namespace {\n\n";
2090
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002091 // Emit PopulateLanguageMap() function
2092 // (a language map maps from file extensions to language names).
2093 EmitPopulateLanguageMap(Records, O);
2094
2095 // Emit Tool classes.
2096 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2097 E = Data.ToolDescs.end(); B!=E; ++B)
2098 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2099
2100 // Emit Edge# classes.
2101 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2102
2103 // Emit PopulateCompilationGraph() function.
2104 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2105
2106 // Emit code for plugin registration.
2107 EmitRegisterPlugin(Data.Priority, O);
2108
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00002109 O << "} // End anonymous namespace.\n\n";
2110
2111 // Force linkage magic.
2112 O << "namespace llvmc {\n";
2113 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2114 O << "}\n";
2115
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002116 // EOF
2117}
2118
2119
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002120// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00002121}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002122
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002123/// run - The back-end entry point.
Daniel Dunbard4287062009-07-03 00:10:29 +00002124void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002125 try {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002126 PluginData Data;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002127
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002128 CollectPluginData(Records, Data);
2129 CheckPluginData(Data);
2130
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00002131 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002132 EmitPluginCode(Data, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002133
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002134 } catch (std::exception& Error) {
2135 throw Error.what() + std::string(" - usually this means a syntax error.");
2136 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002137}