blob: 06afaf73a02e96aea105778f635dfc23841fcf12 [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-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 Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-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 Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikovac67b7e2008-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 Glushenkovf8349ac2009-09-21 15:53:44 +000042// Indentation.
43unsigned TabWidth = 4;
Mikhail Glushenkov9d7a2dc2009-09-28 01:15:44 +000044unsigned Indent1 = TabWidth*1;
45unsigned Indent2 = TabWidth*2;
46unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
59 template<typename T>
60 void operator()(const T&) const {
61 }
62};
63
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000064int InitPtrToInt(const Init* ptr) {
65 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000066 return val.getValue();
67}
68
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000069const std::string& InitPtrToString(const Init* ptr) {
70 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
71 return val.getValue();
72}
73
74const ListInit& InitPtrToList(const Init* ptr) {
75 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
76 return val;
77}
78
79const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000080 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000081 return val;
82}
83
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000084// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000085// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000086void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +000087 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000088 throw d->getOperator()->getAsString() + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000089}
90
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000091// isDagEmpty - is this DAG marked with an empty marker?
92bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov2d04ec52009-09-10 16:22:02 +000093 return d->getOperator()->getAsString() == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000094}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000095
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000096// EscapeVariableName - Escape commas and other symbols not allowed
97// in the C++ variable names. Makes it possible to use options named
98// like "Wa," (useful for prefix options).
99std::string EscapeVariableName(const std::string& Var) {
100 std::string ret;
101 for (unsigned i = 0; i != Var.size(); ++i) {
102 char cur_char = Var[i];
103 if (cur_char == ',') {
104 ret += "_comma_";
105 }
106 else if (cur_char == '+') {
107 ret += "_plus_";
108 }
109 else if (cur_char == '-') {
110 ret += "_dash_";
111 }
112 else {
113 ret.push_back(cur_char);
114 }
115 }
116 return ret;
117}
118
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000119/// oneOf - Does the input string contain this character?
120bool oneOf(const char* lst, char c) {
121 while (*lst) {
122 if (*lst++ == c)
123 return true;
124 }
125 return false;
126}
127
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000128template <class I, class S>
129void checkedIncrement(I& P, I E, S ErrorString) {
130 ++P;
131 if (P == E)
132 throw ErrorString;
133}
134
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000135//===----------------------------------------------------------------------===//
136/// Back-end specific code
137
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000138
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000139/// OptionType - One of six different option types. See the
140/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000141namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000142
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000143 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000144 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000145
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000146 bool IsList (OptionType t) {
147 return (t == ParameterList || t == PrefixList);
148 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000149
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000150 bool IsSwitch (OptionType t) {
151 return (t == Switch);
152 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000153
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000154 bool IsParameter (OptionType t) {
155 return (t == Parameter || t == Prefix);
156 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000157
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000158}
159
160OptionType::OptionType stringToOptionType(const std::string& T) {
161 if (T == "alias_option")
162 return OptionType::Alias;
163 else if (T == "switch_option")
164 return OptionType::Switch;
165 else if (T == "parameter_option")
166 return OptionType::Parameter;
167 else if (T == "parameter_list_option")
168 return OptionType::ParameterList;
169 else if (T == "prefix_option")
170 return OptionType::Prefix;
171 else if (T == "prefix_list_option")
172 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 else
174 throw "Unknown option type: " + T + '!';
175}
176
177namespace OptionDescriptionFlags {
178 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000179 ReallyHidden = 0x4, Extern = 0x8,
180 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000181}
182
183/// OptionDescription - Represents data contained in a single
184/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185struct OptionDescription {
186 OptionType::OptionType Type;
187 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000188 unsigned Flags;
189 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000190 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000191 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000192
193 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000194 const std::string& n = "",
195 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000196 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000197 {}
198
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000199 /// GenTypeDeclaration - Returns the C++ variable type of this
200 /// option.
201 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000202
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000203 /// GenVariableName - Returns the variable name used in the
204 /// generated C++ code.
205 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000206
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000207 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000211
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000212 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000213
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000214 bool isMultiVal() const;
215
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000217 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218
219 bool isRequired() const;
220 void setRequired();
221
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000222 bool isOneOrMore() const;
223 void setOneOrMore();
224
225 bool isZeroOrOne() const;
226 void setZeroOrOne();
227
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000228 bool isHidden() const;
229 void setHidden();
230
231 bool isReallyHidden() const;
232 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000233
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000234 bool isParameter() const
235 { return OptionType::IsParameter(this->Type); }
236
237 bool isSwitch() const
238 { return OptionType::IsSwitch(this->Type); }
239
240 bool isList() const
241 { return OptionType::IsList(this->Type); }
242
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000243};
244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245void OptionDescription::Merge (const OptionDescription& other)
246{
247 if (other.Type != Type)
248 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000250 if (Help == other.Help || Help == DefaultHelpString)
251 Help = other.Help;
252 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000253 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 " defined for option " + Name + "\n";
255 }
256
257 Flags |= other.Flags;
258}
259
260bool OptionDescription::isAlias() const {
261 return Type == OptionType::Alias;
262}
263
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000264bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000265 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000266}
267
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000269 return Flags & OptionDescriptionFlags::Extern;
270}
271void OptionDescription::setExtern() {
272 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273}
274
275bool OptionDescription::isRequired() const {
276 return Flags & OptionDescriptionFlags::Required;
277}
278void OptionDescription::setRequired() {
279 Flags |= OptionDescriptionFlags::Required;
280}
281
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000282bool OptionDescription::isOneOrMore() const {
283 return Flags & OptionDescriptionFlags::OneOrMore;
284}
285void OptionDescription::setOneOrMore() {
286 Flags |= OptionDescriptionFlags::OneOrMore;
287}
288
289bool OptionDescription::isZeroOrOne() const {
290 return Flags & OptionDescriptionFlags::ZeroOrOne;
291}
292void OptionDescription::setZeroOrOne() {
293 Flags |= OptionDescriptionFlags::ZeroOrOne;
294}
295
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000296bool OptionDescription::isHidden() const {
297 return Flags & OptionDescriptionFlags::Hidden;
298}
299void OptionDescription::setHidden() {
300 Flags |= OptionDescriptionFlags::Hidden;
301}
302
303bool OptionDescription::isReallyHidden() const {
304 return Flags & OptionDescriptionFlags::ReallyHidden;
305}
306void OptionDescription::setReallyHidden() {
307 Flags |= OptionDescriptionFlags::ReallyHidden;
308}
309
310const char* OptionDescription::GenTypeDeclaration() const {
311 switch (Type) {
312 case OptionType::Alias:
313 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000314 case OptionType::PrefixList:
315 case OptionType::ParameterList:
316 return "cl::list<std::string>";
317 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000319 case OptionType::Parameter:
320 case OptionType::Prefix:
321 default:
322 return "cl::opt<std::string>";
323 }
324}
325
326std::string OptionDescription::GenVariableName() const {
327 const std::string& EscapedName = EscapeVariableName(Name);
328 switch (Type) {
329 case OptionType::Alias:
330 return "AutoGeneratedAlias_" + EscapedName;
331 case OptionType::PrefixList:
332 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000333 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 case OptionType::Switch:
335 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000336 case OptionType::Prefix:
337 case OptionType::Parameter:
338 default:
339 return "AutoGeneratedParameter_" + EscapedName;
340 }
341}
342
343/// OptionDescriptions - An OptionDescription array plus some helper
344/// functions.
345class OptionDescriptions {
346 typedef StringMap<OptionDescription> container_type;
347
348 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000349 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000350
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000352 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000353 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000354
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000355 /// insertDescription - Insert new OptionDescription into
356 /// OptionDescriptions list
357 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000358
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000359 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000360 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000361 const_iterator begin() const { return Descriptions.begin(); }
362 const_iterator end() const { return Descriptions.end(); }
363};
364
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000365const OptionDescription&
366OptionDescriptions::FindOption(const std::string& OptName) const
367{
368 const_iterator I = Descriptions.find(OptName);
369 if (I != Descriptions.end())
370 return I->second;
371 else
372 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000373}
374
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000375void OptionDescriptions::InsertDescription (const OptionDescription& o)
376{
377 container_type::iterator I = Descriptions.find(o.Name);
378 if (I != Descriptions.end()) {
379 OptionDescription& D = I->second;
380 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000381 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000382 else {
383 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000384 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000385}
386
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387/// HandlerTable - A base class for function objects implemented as
388/// 'tables of handlers'.
389template <class T>
390class HandlerTable {
391protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000392 // Implementation details.
393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 /// Handler -
395 typedef void (T::* Handler) (const DagInit*);
396 /// HandlerMap - A map from property names to property handlers
397 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000398
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000399 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000400 static bool staticMembersInitialized_;
401
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000402 T* childPtr;
403public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000404
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405 HandlerTable(T* cp) : childPtr(cp)
406 {}
407
408 /// operator() - Just forwards to the corresponding property
409 /// handler.
410 void operator() (Init* i) {
411 const DagInit& property = InitPtrToDag(i);
412 const std::string& property_name = property.getOperator()->getAsString();
413 typename HandlerMap::iterator method = Handlers_.find(property_name);
414
415 if (method != Handlers_.end()) {
416 Handler h = method->second;
417 (childPtr->*h)(&property);
418 }
419 else {
420 throw "No handler found for property " + property_name + "!";
421 }
422 }
423
424 void AddHandler(const char* Property, Handler Handl) {
425 Handlers_[Property] = Handl;
426 }
427};
428
429template <class T> typename HandlerTable<T>::HandlerMap
430HandlerTable<T>::Handlers_;
431template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
432
433
434/// CollectOptionProperties - Function object for iterating over an
435/// option property list.
436class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
437private:
438
439 /// optDescs_ - OptionDescriptions table. This is where the
440 /// information is stored.
441 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000442
443public:
444
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000445 explicit CollectOptionProperties(OptionDescription& OD)
446 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000447 {
448 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000449 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000450 AddHandler("help", &CollectOptionProperties::onHelp);
451 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000452 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000453 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
454 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000455 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
456 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000457 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000458
459 staticMembersInitialized_ = true;
460 }
461 }
462
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000463private:
464
465 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000467
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000468 void onExtern (const DagInit* d) {
469 checkNumberOfArguments(d, 0);
470 optDesc_.setExtern();
471 }
472
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000473 void onHelp (const DagInit* d) {
474 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000475 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000476 }
477
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000478 void onHidden (const DagInit* d) {
479 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000480 optDesc_.setHidden();
481 }
482
483 void onReallyHidden (const DagInit* d) {
484 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000485 optDesc_.setReallyHidden();
486 }
487
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000488 void onRequired (const DagInit* d) {
489 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000490 if (optDesc_.isOneOrMore())
491 throw std::string("An option can't have both (required) "
492 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000493 optDesc_.setRequired();
494 }
495
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000496 void onInit (const DagInit* d) {
497 checkNumberOfArguments(d, 1);
498 Init* i = d->getArg(0);
499 const std::string& str = i->getAsString();
500
501 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
502 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
503
504 if (!correct)
505 throw std::string("Incorrect usage of the 'init' option property!");
506
507 optDesc_.InitVal = i;
508 }
509
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000510 void onOneOrMore (const DagInit* d) {
511 checkNumberOfArguments(d, 0);
512 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
513 throw std::string("Only one of (required), (zero_or_one) or "
514 "(one_or_more) properties is allowed!");
515 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000516 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000517 "on a non-list option will have no effect.\n";
518 optDesc_.setOneOrMore();
519 }
520
521 void onZeroOrOne (const DagInit* d) {
522 checkNumberOfArguments(d, 0);
523 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
524 throw std::string("Only one of (required), (zero_or_one) or "
525 "(one_or_more) properties is allowed!");
526 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000527 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000528 "on a non-list option will have no effect.\n";
529 optDesc_.setZeroOrOne();
530 }
531
532 void onMultiVal (const DagInit* d) {
533 checkNumberOfArguments(d, 1);
534 int val = InitPtrToInt(d->getArg(0));
535 if (val < 2)
536 throw std::string("Error in the 'multi_val' property: "
537 "the value must be greater than 1!");
538 if (!OptionType::IsList(optDesc_.Type))
539 throw std::string("The multi_val property is valid only "
540 "on list options!");
541 optDesc_.MultiVal = val;
542 }
543
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000544};
545
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000546/// AddOption - A function object that is applied to every option
547/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000548class AddOption {
549private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000550 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000551
552public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000553 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000554 {}
555
556 void operator()(const Init* i) {
557 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000558 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000559
560 const OptionType::OptionType Type =
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000561 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000562 const std::string& Name = InitPtrToString(d.getArg(0));
563
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000564 OptionDescription OD(Type, Name);
565
566 if (!OD.isExtern())
567 checkNumberOfArguments(&d, 2);
568
569 if (OD.isAlias()) {
570 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000571 OD.Help = InitPtrToString(d.getArg(1));
572 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000573 else if (!OD.isExtern()) {
574 processOptionProperties(&d, OD);
575 }
576 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000577 }
578
579private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000580 /// processOptionProperties - Go through the list of option
581 /// properties and call a corresponding handler for each.
582 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
583 checkNumberOfArguments(d, 2);
584 DagInit::const_arg_iterator B = d->arg_begin();
585 // Skip the first argument: it's always the option name.
586 ++B;
587 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000588 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000589
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000590};
591
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000592/// CollectOptionDescriptions - Collects option properties from all
593/// OptionLists.
594void CollectOptionDescriptions (RecordVector::const_iterator B,
595 RecordVector::const_iterator E,
596 OptionDescriptions& OptDescs)
597{
598 // For every OptionList:
599 for (; B!=E; ++B) {
600 RecordVector::value_type T = *B;
601 // Throws an exception if the value does not exist.
602 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000603
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000604 // For every option description in this list:
605 // collect the information and
606 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
607 }
608}
609
610// Tool information record
611
612namespace ToolFlags {
613 enum ToolFlags { Join = 0x1, Sink = 0x2 };
614}
615
616struct ToolDescription : public RefCountedBase<ToolDescription> {
617 std::string Name;
618 Init* CmdLine;
619 Init* Actions;
620 StrVector InLanguage;
621 std::string OutLanguage;
622 std::string OutputSuffix;
623 unsigned Flags;
624
625 // Various boolean properties
626 void setSink() { Flags |= ToolFlags::Sink; }
627 bool isSink() const { return Flags & ToolFlags::Sink; }
628 void setJoin() { Flags |= ToolFlags::Join; }
629 bool isJoin() const { return Flags & ToolFlags::Join; }
630
631 // Default ctor here is needed because StringMap can only store
632 // DefaultConstructible objects
633 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
634 ToolDescription (const std::string& n)
635 : Name(n), CmdLine(0), Actions(0), Flags(0)
636 {}
637};
638
639/// ToolDescriptions - A list of Tool information records.
640typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
641
642
643/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000644/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000645class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000646private:
647
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000648 /// toolDesc_ - Properties of the current Tool. This is where the
649 /// information is stored.
650 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000651
652public:
653
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000654 explicit CollectToolProperties (ToolDescription& d)
655 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000656 {
657 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000658
659 AddHandler("actions", &CollectToolProperties::onActions);
660 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
661 AddHandler("in_language", &CollectToolProperties::onInLanguage);
662 AddHandler("join", &CollectToolProperties::onJoin);
663 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
664 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
665 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000666
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000667 staticMembersInitialized_ = true;
668 }
669 }
670
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000671private:
672
673 /// Property handlers --
674 /// Functions that extract information about tool properties from
675 /// DAG representation.
676
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000677 void onActions (const DagInit* d) {
678 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000679 Init* Case = d->getArg(0);
680 if (typeid(*Case) != typeid(DagInit) ||
681 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
682 throw
683 std::string("The argument to (actions) should be a 'case' construct!");
684 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000685 }
686
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000687 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000688 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000689 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000690 }
691
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000692 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000693 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000694 Init* arg = d->getArg(0);
695
696 // Find out the argument's type.
697 if (typeid(*arg) == typeid(StringInit)) {
698 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000699 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000700 }
701 else {
702 // It's a list.
703 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000704 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000705
706 // Copy strings to the output vector.
707 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
708 B != E; ++B) {
709 out.push_back(InitPtrToString(*B));
710 }
711
712 // Remove duplicates.
713 std::sort(out.begin(), out.end());
714 StrVector::iterator newE = std::unique(out.begin(), out.end());
715 out.erase(newE, out.end());
716 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000717 }
718
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000719 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000720 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000721 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000722 }
723
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000724 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000725 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000726 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000727 }
728
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000729 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000730 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000731 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000732 }
733
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000734 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000735 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000736 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000737 }
738
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000739};
740
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000741/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000742/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000743/// CollectToolProperties function object).
744void CollectToolDescriptions (RecordVector::const_iterator B,
745 RecordVector::const_iterator E,
746 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000747{
748 // Iterate over a properties list of every Tool definition
749 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000750 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000751 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000752 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000753
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000754 IntrusiveRefCntPtr<ToolDescription>
755 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000756
757 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000758 CollectToolProperties(*ToolDesc));
759 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000760 }
761}
762
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000763/// FillInEdgeVector - Merge all compilation graph definitions into
764/// one single edge list.
765void FillInEdgeVector(RecordVector::const_iterator B,
766 RecordVector::const_iterator E, RecordVector& Out) {
767 for (; B != E; ++B) {
768 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000769
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000770 for (unsigned i = 0; i < edges->size(); ++i)
771 Out.push_back(edges->getElementAsRecord(i));
772 }
773}
774
775/// CalculatePriority - Calculate the priority of this plugin.
776int CalculatePriority(RecordVector::const_iterator B,
777 RecordVector::const_iterator E) {
778 int total = 0;
Mikhail Glushenkov35fde152008-11-17 17:30:25 +0000779 for (; B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000780 total += static_cast<int>((*B)->getValueAsInt("priority"));
781 }
782 return total;
783}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000784
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000785/// NotInGraph - Helper function object for FilterNotInGraph.
786struct NotInGraph {
787private:
788 const llvm::StringSet<>& ToolsInGraph_;
789
790public:
791 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
792 : ToolsInGraph_(ToolsInGraph)
793 {}
794
795 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
796 return (ToolsInGraph_.count(x->Name) == 0);
797 }
798};
799
800/// FilterNotInGraph - Filter out from ToolDescs all Tools not
801/// mentioned in the compilation graph definition.
802void FilterNotInGraph (const RecordVector& EdgeVector,
803 ToolDescriptions& ToolDescs) {
804
805 // List all tools mentioned in the graph.
806 llvm::StringSet<> ToolsInGraph;
807
808 for (RecordVector::const_iterator B = EdgeVector.begin(),
809 E = EdgeVector.end(); B != E; ++B) {
810
811 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000812 const std::string& NodeA = Edge->getValueAsString("a");
813 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000815 if (NodeA != "root")
816 ToolsInGraph.insert(NodeA);
817 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818 }
819
820 // Filter ToolPropertiesList.
821 ToolDescriptions::iterator new_end =
822 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
823 NotInGraph(ToolsInGraph));
824 ToolDescs.erase(new_end, ToolDescs.end());
825}
826
827/// FillInToolToLang - Fills in two tables that map tool names to
828/// (input, output) languages. Helper function used by TypecheckGraph().
829void FillInToolToLang (const ToolDescriptions& ToolDescs,
830 StringMap<StringSet<> >& ToolToInLang,
831 StringMap<std::string>& ToolToOutLang) {
832 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
833 E = ToolDescs.end(); B != E; ++B) {
834 const ToolDescription& D = *(*B);
835 for (StrVector::const_iterator B = D.InLanguage.begin(),
836 E = D.InLanguage.end(); B != E; ++B)
837 ToolToInLang[D.Name].insert(*B);
838 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000839 }
840}
841
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000842/// TypecheckGraph - Check that names for output and input languages
843/// on all edges do match. This doesn't do much when the information
844/// about the whole graph is not available (i.e. when compiling most
845/// plugins).
846void TypecheckGraph (const RecordVector& EdgeVector,
847 const ToolDescriptions& ToolDescs) {
848 StringMap<StringSet<> > ToolToInLang;
849 StringMap<std::string> ToolToOutLang;
850
851 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
852 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
853 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
854
855 for (RecordVector::const_iterator B = EdgeVector.begin(),
856 E = EdgeVector.end(); B != E; ++B) {
857 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000858 const std::string& NodeA = Edge->getValueAsString("a");
859 const std::string& NodeB = Edge->getValueAsString("b");
860 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
861 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000862
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000863 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000864 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000865 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000866 + ": output->input language mismatch";
867 }
868
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000869 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 throw std::string("Edges back to the root are not allowed!");
871 }
872}
873
874/// WalkCase - Walks the 'case' expression DAG and invokes
875/// TestCallback on every test, and StatementCallback on every
876/// statement. Handles 'case' nesting, but not the 'and' and 'or'
877/// combinators.
878// TODO: Re-implement EmitCaseConstructHandler on top of this function?
879template <typename F1, typename F2>
880void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
881 const DagInit& d = InitPtrToDag(Case);
882 bool even = false;
883 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
884 B != E; ++B) {
885 Init* arg = *B;
886 if (even && dynamic_cast<DagInit*>(arg)
887 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
888 WalkCase(arg, TestCallback, StatementCallback);
889 else if (!even)
890 TestCallback(arg);
891 else
892 StatementCallback(arg);
893 even = !even;
894 }
895}
896
897/// ExtractOptionNames - A helper function object used by
898/// CheckForSuperfluousOptions() to walk the 'case' DAG.
899class ExtractOptionNames {
900 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000901
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000902 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 const DagInit& Stmt = InitPtrToDag(Statement);
904 const std::string& ActionName = Stmt.getOperator()->getAsString();
905 if (ActionName == "forward" || ActionName == "forward_as" ||
906 ActionName == "unpack_values" || ActionName == "switch_on" ||
907 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000908 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000909 checkNumberOfArguments(&Stmt, 1);
910 const std::string& Name = InitPtrToString(Stmt.getArg(0));
911 OptionNames_.insert(Name);
912 }
913 else if (ActionName == "and" || ActionName == "or") {
914 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000915 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000916 }
917 }
918 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000919
920public:
921 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
922 {}
923
924 void operator()(const Init* Statement) {
925 if (typeid(*Statement) == typeid(ListInit)) {
926 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
927 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
928 B != E; ++B)
929 this->processDag(*B);
930 }
931 else {
932 this->processDag(Statement);
933 }
934 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000935};
936
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000937/// CheckForSuperfluousOptions - Check that there are no side
938/// effect-free options (specified only in the OptionList). Otherwise,
939/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940void CheckForSuperfluousOptions (const RecordVector& Edges,
941 const ToolDescriptions& ToolDescs,
942 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000943 llvm::StringSet<> nonSuperfluousOptions;
944
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000945 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000946 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000947 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
948 E = ToolDescs.end(); B != E; ++B) {
949 const ToolDescription& TD = *(*B);
950 ExtractOptionNames Callback(nonSuperfluousOptions);
951 if (TD.Actions)
952 WalkCase(TD.Actions, Callback, Callback);
953 }
954
955 // Add all options mentioned in the 'case' clauses of the
956 // OptionalEdges of the compilation graph to the set of
957 // non-superfluous options.
958 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
959 B != E; ++B) {
960 const Record* Edge = *B;
961 DagInit* Weight = Edge->getValueAsDag("weight");
962
963 if (!isDagEmpty(Weight))
964 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000965 }
966
967 // Check that all options in OptDescs belong to the set of
968 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000969 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000970 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000971 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000972 if (!nonSuperfluousOptions.count(Val.Name)
973 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +0000974 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000975 "Probable cause: this option is specified only in the OptionList.\n";
976 }
977}
978
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +0000979/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
980bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
981 if (TestName == "single_input_file") {
982 O << "InputFilenames.size() == 1";
983 return true;
984 }
985 else if (TestName == "multiple_input_files") {
986 O << "InputFilenames.size() > 1";
987 return true;
988 }
989
990 return false;
991}
992
993
994/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000995bool EmitCaseTest1Arg(const std::string& TestName,
996 const DagInit& d,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000997 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000998 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000999 checkNumberOfArguments(&d, 1);
1000 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001001
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001002 if (TestName == "switch_on") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001003 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001004 if (!OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001005 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001006 O << OptDesc.GenVariableName();
1007 return true;
1008 } else if (TestName == "input_languages_contain") {
1009 O << "InLangs.count(\"" << OptName << "\") != 0";
1010 return true;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001011 } else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001012 // This works only for single-argument Tool::GenerateAction. Join
1013 // tools can process several files in different languages simultaneously.
1014
1015 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001016 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001017 return true;
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001018 } else if (TestName == "not_empty" || TestName == "empty") {
1019 const char* Test = (TestName == "empty") ? "" : "!";
1020
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001021 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001022 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001023 return true;
1024 }
1025 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001026 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001027 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001028 throw OptName
1029 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001030 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001031 return true;
1032 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001033 }
1034
1035 return false;
1036}
1037
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001038/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001039bool EmitCaseTest2Args(const std::string& TestName,
1040 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001041 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001042 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001043 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001044 checkNumberOfArguments(&d, 2);
1045 const std::string& OptName = InitPtrToString(d.getArg(0));
1046 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001047 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001048
1049 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001050 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001051 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001052 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1053 return true;
1054 }
1055 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001056 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001058 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001059 O << "std::find(" << VarName << ".begin(),\n";
1060 O.indent(IndentLevel + Indent1)
1061 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001062 << OptArg << "\") != " << VarName << ".end()";
1063 return true;
1064 }
1065
1066 return false;
1067}
1068
1069// Forward declaration.
1070// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001071void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001072 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001073 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001074
1075/// EmitLogicalOperationTest - Helper function used by
1076/// EmitCaseConstructHandler.
1077void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001078 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001079 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001080 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001081 O << '(';
1082 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001083 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001084 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001085 if (j != NumArgs - 1) {
1086 O << ")\n";
1087 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1088 }
1089 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001090 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001091 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001092 }
1093}
1094
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001095void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001096 const OptionDescriptions& OptDescs, raw_ostream& O)
1097{
1098 checkNumberOfArguments(&d, 1);
1099 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1100 O << "! (";
1101 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1102 O << ")";
1103}
1104
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001105/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001106void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001107 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001108 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001109 const std::string& TestName = d.getOperator()->getAsString();
1110
1111 if (TestName == "and")
1112 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1113 else if (TestName == "or")
1114 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001115 else if (TestName == "not")
1116 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001117 else if (EmitCaseTest0Args(TestName, O))
1118 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001119 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1120 return;
1121 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1122 return;
1123 else
1124 throw TestName + ": unknown edge property!";
1125}
1126
1127// Emit code that handles the 'case' construct.
1128// Takes a function object that should emit code for every case clause.
1129// Callback's type is
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001130// void F(Init* Statement, unsigned IndentLevel, raw_ostream& O).
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001131template <typename F>
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001132void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001133 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001134 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001135 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001136 const DagInit* d = &InitPtrToDag(Dag);
1137 if (d->getOperator()->getAsString() != "case")
1138 throw std::string("EmitCaseConstructHandler should be invoked"
1139 " only on 'case' expressions!");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001140
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001141 unsigned numArgs = d->getNumArgs();
1142 if (d->getNumArgs() < 2)
1143 throw "There should be at least one clause in the 'case' expression:\n"
1144 + d->getAsString();
1145
1146 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001147 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001148
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001149 // Emit the test.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001150 if (Test.getOperator()->getAsString() == "default") {
1151 if (i+2 != numArgs)
1152 throw std::string("The 'default' clause should be the last in the"
1153 "'case' construct!");
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001154 O.indent(IndentLevel) << "else {\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001155 }
1156 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001157 O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001158 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1159 O << ") {\n";
1160 }
1161
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001162 // Emit the corresponding statement.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001163 ++i;
1164 if (i == numArgs)
1165 throw "Case construct handler: no corresponding action "
1166 "found for the test " + Test.getAsString() + '!';
1167
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001168 Init* arg = d->getArg(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001169 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1170 if (nd && (nd->getOperator()->getAsString() == "case")) {
1171 // Handle the nested 'case'.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001172 EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001173 Callback, EmitElseIf, OptDescs, O);
1174 }
1175 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001176 Callback(arg, (IndentLevel + Indent1), O);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001177 }
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001178 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001179 }
1180}
1181
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001182/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1183/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1184/// Helper function used by EmitCmdLineVecFill and.
1185void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1186 const char* Delimiters = " \t\n\v\f\r";
1187 enum TokenizerState
1188 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1189 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001190
1191 if (CmdLine.empty())
1192 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001193 Out.push_back("");
1194
1195 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1196 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001197
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001198 for (; B != E; ++B) {
1199 char cur_ch = CmdLine[B];
1200
1201 switch (cur_st) {
1202 case Normal:
1203 if (cur_ch == '$') {
1204 cur_st = SpecialCommand;
1205 break;
1206 }
1207 if (oneOf(Delimiters, cur_ch)) {
1208 // Skip whitespace
1209 B = CmdLine.find_first_not_of(Delimiters, B);
1210 if (B == std::string::npos) {
1211 B = E-1;
1212 continue;
1213 }
1214 --B;
1215 Out.push_back("");
1216 continue;
1217 }
1218 break;
1219
1220
1221 case SpecialCommand:
1222 if (oneOf(Delimiters, cur_ch)) {
1223 cur_st = Normal;
1224 Out.push_back("");
1225 continue;
1226 }
1227 if (cur_ch == '(') {
1228 Out.push_back("");
1229 cur_st = InsideSpecialCommand;
1230 continue;
1231 }
1232 break;
1233
1234 case InsideSpecialCommand:
1235 if (oneOf(Delimiters, cur_ch)) {
1236 continue;
1237 }
1238 if (cur_ch == '\'') {
1239 cur_st = InsideQuotationMarks;
1240 Out.push_back("");
1241 continue;
1242 }
1243 if (cur_ch == ')') {
1244 cur_st = Normal;
1245 Out.push_back("");
1246 }
1247 if (cur_ch == ',') {
1248 continue;
1249 }
1250
1251 break;
1252
1253 case InsideQuotationMarks:
1254 if (cur_ch == '\'') {
1255 cur_st = InsideSpecialCommand;
1256 continue;
1257 }
1258 break;
1259 }
1260
1261 Out.back().push_back(cur_ch);
1262 }
1263}
1264
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001265/// SubstituteSpecialCommands - Perform string substitution for $CALL
1266/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001267StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001268(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001269{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001270
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001271 const std::string& cmd = *Pos;
1272
1273 if (cmd == "$CALL") {
1274 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1275 const std::string& CmdName = *Pos;
1276
1277 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001278 throw std::string("$CALL invocation: empty argument list!");
1279
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001280 O << "hooks::";
1281 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001282
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001283
1284 bool firstIteration = true;
1285 while (true) {
1286 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1287 const std::string& Arg = *Pos;
1288 assert(Arg.size() != 0);
1289
1290 if (Arg[0] == ')')
1291 break;
1292
1293 if (firstIteration)
1294 firstIteration = false;
1295 else
1296 O << ", ";
1297
1298 O << '"' << Arg << '"';
1299 }
1300
1301 O << ')';
1302
1303 }
1304 else if (cmd == "$ENV") {
1305 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1306 const std::string& EnvName = *Pos;
1307
1308 if (EnvName == ")")
1309 throw "$ENV invocation: empty argument list!";
1310
1311 O << "checkCString(std::getenv(\"";
1312 O << EnvName;
1313 O << "\"))";
1314
1315 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001316 }
1317 else {
1318 throw "Unknown special command: " + cmd;
1319 }
1320
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001321 const std::string& Leftover = *Pos;
1322 assert(Leftover.at(0) == ')');
1323 if (Leftover.size() != 1)
1324 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001325
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001326 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001327}
1328
1329/// EmitCmdLineVecFill - Emit code that fills in the command line
1330/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001331void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001332 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001333 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001334 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001335 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1336
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001337 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001338 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001339
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001340 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1341
1342 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001343 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001344 if (StrVec[0][0] == '$') {
1345 while (I != E && (*I)[0] != ')' )
1346 ++I;
1347
1348 // Skip the ')' symbol.
1349 ++I;
1350 }
1351 else {
1352 ++I;
1353 }
1354
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001355 bool hasINFILE = false;
1356
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001357 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001358 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001359 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001360 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001361 if (cmd.at(0) == '$') {
1362 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001363 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001364 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001365 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001366 << ", E = inFiles.end();\n";
1367 O.indent(IndentLevel) << "B != E; ++B)\n";
1368 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1369 }
1370 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001371 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001372 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001373 }
1374 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001375 O << "vec.push_back(\"\");\n";
1376 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001377 }
1378 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001379 O << "vec.push_back(";
1380 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001381 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001382 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001383 }
1384 else {
1385 O << "vec.push_back(\"" << cmd << "\");\n";
1386 }
1387 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001388 if (!hasINFILE)
1389 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001390
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001391 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001392 if (StrVec[0][0] == '$')
1393 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1394 else
1395 O << '"' << StrVec[0] << '"';
1396 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001397}
1398
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001399/// EmitCmdLineVecFillCallback - A function object wrapper around
1400/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1401/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001402class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001403 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001404 const std::string& ToolName;
1405 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001406 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1407 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001408
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001409 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001410 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001411 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001412 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001413 }
1414};
1415
1416/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1417/// implement EmitActionHandler. Emits code for
1418/// handling the (forward) and (forward_as) option properties.
1419void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001420 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001421 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001422 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001423 const std::string& Name = NewName.empty()
1424 ? ("-" + D.Name)
1425 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001426 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001427
1428 switch (D.Type) {
1429 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001430 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001431 break;
1432 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001433 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1434 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001435 break;
1436 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001437 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1438 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001439 break;
1440 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001441 O.indent(IndentLevel)
1442 << "for (" << D.GenTypeDeclaration()
1443 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1444 O.indent(IndentLevel)
1445 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1446 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1447 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001448
1449 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001450 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1451 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001452 }
1453
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001454 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001455 break;
1456 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001457 O.indent(IndentLevel)
1458 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1459 << D.GenVariableName() << ".begin(),\n";
1460 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1461 << ".end() ; B != E;) {\n";
1462 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001463
1464 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001465 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1466 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001467 }
1468
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001469 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001470 break;
1471 case OptionType::Alias:
1472 default:
1473 throw std::string("Aliases are not allowed in tool option descriptions!");
1474 }
1475}
1476
1477/// EmitActionHandler - Emit code that handles actions. Used by
1478/// EmitGenerateActionMethod() as an argument to
1479/// EmitCaseConstructHandler().
1480class EmitActionHandler {
1481 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001482
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001483 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001484 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001485 {
1486 const DagInit& Dag = InitPtrToDag(Statement);
1487 const std::string& ActionName = Dag.getOperator()->getAsString();
1488
1489 if (ActionName == "append_cmd") {
1490 checkNumberOfArguments(&Dag, 1);
1491 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001492 StrVector Out;
1493 llvm::SplitString(Cmd, Out);
1494
1495 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1496 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001497 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001498 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001499 else if (ActionName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001500 O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001501 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1502 : "Unknown error!")
1503 << "\");\n";
1504 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001505 else if (ActionName == "forward") {
1506 checkNumberOfArguments(&Dag, 1);
1507 const std::string& Name = InitPtrToString(Dag.getArg(0));
1508 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1509 IndentLevel, "", O);
1510 }
1511 else if (ActionName == "forward_as") {
1512 checkNumberOfArguments(&Dag, 2);
1513 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001514 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001515 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1516 IndentLevel, NewName, O);
1517 }
1518 else if (ActionName == "output_suffix") {
1519 checkNumberOfArguments(&Dag, 1);
1520 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001521 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001522 }
1523 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001524 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001525 }
1526 else if (ActionName == "unpack_values") {
1527 checkNumberOfArguments(&Dag, 1);
1528 const std::string& Name = InitPtrToString(Dag.getArg(0));
1529 const OptionDescription& D = OptDescs.FindOption(Name);
1530
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001531 if (D.isMultiVal())
1532 throw std::string("Can't use unpack_values with multi-valued options!");
1533
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001534 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001535 O.indent(IndentLevel)
1536 << "for (" << D.GenTypeDeclaration()
1537 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1538 O.indent(IndentLevel)
1539 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1540 O.indent(IndentLevel + Indent1)
1541 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001542 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001543 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001544 O.indent(IndentLevel) << "llvm::SplitString("
1545 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001546 }
1547 else {
1548 throw "Option '" + D.Name +
1549 "': switches can't have the 'unpack_values' property!";
1550 }
1551 }
1552 else {
1553 throw "Unknown action name: " + ActionName + "!";
1554 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001555 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001556 public:
1557 EmitActionHandler(const OptionDescriptions& OD)
1558 : OptDescs(OD) {}
1559
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001560 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001561 raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001562 {
1563 if (typeid(*Statement) == typeid(ListInit)) {
1564 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1565 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1566 B != E; ++B)
1567 this->processActionDag(*B, IndentLevel, O);
1568 }
1569 else {
1570 this->processActionDag(Statement, IndentLevel, O);
1571 }
1572 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001573};
1574
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001575bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1576 StrVector StrVec;
1577 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1578
1579 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1580 I != E; ++I) {
1581 if (*I == "$OUTFILE")
1582 return false;
1583 }
1584
1585 return true;
1586}
1587
1588class IsOutFileIndexCheckRequiredStrCallback {
1589 bool* ret_;
1590
1591public:
1592 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1593 {}
1594
1595 void operator()(const Init* CmdLine) {
1596 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1597 *ret_ = true;
1598 }
1599};
1600
1601bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1602 bool ret = false;
1603 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1604 return ret;
1605}
1606
1607/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1608/// in EmitGenerateActionMethod() ?
1609bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1610 if (typeid(*CmdLine) == typeid(StringInit))
1611 return IsOutFileIndexCheckRequiredStr(CmdLine);
1612 else
1613 return IsOutFileIndexCheckRequiredCase(CmdLine);
1614}
1615
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001616// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001617// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001618void EmitGenerateActionMethod (const ToolDescription& D,
1619 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001620 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001621 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001622 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001623 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001624 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001625
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001626 O.indent(Indent2) << "bool HasChildren,\n";
1627 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1628 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1629 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1630 O.indent(Indent1) << "{\n";
1631 O.indent(Indent2) << "std::string cmd;\n";
1632 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1633 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1634 O.indent(Indent2) << "const char* output_suffix = \""
1635 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001636
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001637 if (!D.CmdLine)
1638 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001639
1640 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1641 O.indent(Indent2) << "int out_file_index"
1642 << (IndexCheckRequired ? " = -1" : "")
1643 << ";\n\n";
1644
1645 // Process the cmd_line property.
1646 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001647 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1648 else
1649 EmitCaseConstructHandler(D.CmdLine, Indent2,
1650 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1651 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001652
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001653 // For every understood option, emit handling code.
1654 if (D.Actions)
1655 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1656 false, OptDescs, O);
1657
1658 O << '\n';
1659 O.indent(Indent2)
1660 << "std::string out_file = OutFilename("
1661 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1662 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001663
1664 if (IndexCheckRequired)
1665 O.indent(Indent2) << "if (out_file_index != -1)\n";
1666 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1667 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001668
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001669 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001670 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001671 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1672 O.indent(Indent3) << "vec.insert(vec.end(), "
1673 << SinkOptionName << ".begin(), " << SinkOptionName
1674 << ".end());\n";
1675 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001676 }
1677
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001678 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1679 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001680}
1681
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001682/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1683/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001684void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1685 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001686 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001687 if (!ToolDesc.isJoin()) {
1688 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1689 O.indent(Indent2) << "bool HasChildren,\n";
1690 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1691 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1692 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1693 O.indent(Indent1) << "{\n";
1694 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1695 << " is not a Join tool!\");\n";
1696 O.indent(Indent1) << "}\n\n";
1697 }
1698 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001699 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001700 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001701
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001702 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001703}
1704
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001705/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1706/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001707void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001708 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1709 O.indent(Indent2) << "return InputLanguages_;\n";
1710 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001711
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001712 if (D.OutLanguage.empty())
1713 throw "Tool " + D.Name + " has no 'out_language' property!";
1714
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001715 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1716 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1717 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001718}
1719
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001720/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001721void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001722 O.indent(Indent1) << "const char* Name() const {\n";
1723 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1724 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001725}
1726
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001727/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1728/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001729void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001730 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001731 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001732 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001733 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001734 O.indent(Indent2) << "return false;\n";
1735 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001736}
1737
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001738/// EmitStaticMemberDefinitions - Emit static member definitions for a
1739/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001740void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001741 if (D.InLanguage.empty())
1742 throw "Tool " + D.Name + " has no 'in_language' property!";
1743
1744 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1745 for (StrVector::const_iterator B = D.InLanguage.begin(),
1746 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001747 O << '\"' << *B << "\", ";
1748 O << "0};\n\n";
1749}
1750
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001751/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001752void EmitToolClassDefinition (const ToolDescription& D,
1753 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001754 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001755 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001756 return;
1757
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001758 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001759 O << "class " << D.Name << " : public ";
1760 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001761 O << "JoinTool";
1762 else
1763 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001764
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001765 O << "{\nprivate:\n";
1766 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001767
1768 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001769 EmitNameMethod(D, O);
1770 EmitInOutLanguageMethods(D, O);
1771 EmitIsJoinMethod(D, O);
1772 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001773
1774 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001775 O << "};\n";
1776
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001777 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001778
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001779}
1780
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001781/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001782/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001783void EmitOptionDefinitions (const OptionDescriptions& descs,
1784 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001785 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001786{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001787 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001788
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001789 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001790 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001791 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001792 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001793
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001794 if (val.Type == OptionType::Alias) {
1795 Aliases.push_back(val);
1796 continue;
1797 }
1798
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001799 if (val.isExtern())
1800 O << "extern ";
1801
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001802 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001803 << val.GenVariableName();
1804
1805 if (val.isExtern()) {
1806 O << ";\n";
1807 continue;
1808 }
1809
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001810 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001811
1812 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1813 O << ", cl::Prefix";
1814
1815 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001816 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001817 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001818 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001819 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001820 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001821 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001822 O << ", cl::OneOrMore";
1823 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001824 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001825 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001826 }
1827
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001828 if (val.isReallyHidden()) {
1829 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001830 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001831 else if (val.isHidden()) {
1832 O << ", cl::Hidden";
1833 }
1834
1835 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001836 O << ", cl::multi_val(" << val.MultiVal << ')';
1837
1838 if (val.InitVal) {
1839 const std::string& str = val.InitVal->getAsString();
1840 O << ", cl::init(" << str << ')';
1841 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001842
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001843 if (!val.Help.empty())
1844 O << ", cl::desc(\"" << val.Help << "\")";
1845
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001846 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001847 }
1848
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001849 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001851 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001852 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001853
1854 O << val.GenTypeDeclaration() << ' '
1855 << val.GenVariableName()
1856 << "(\"" << val.Name << '\"';
1857
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001858 const OptionDescription& D = descs.FindOption(val.Help);
1859 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001860
1861 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1862 }
1863
1864 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001865 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00001866 O << (HasExterns ? "extern cl" : "cl")
1867 << "::list<std::string> " << SinkOptionName
1868 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001869
1870 O << '\n';
1871}
1872
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001873/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001874void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001875{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001876 // Generate code
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001877 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001878
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001879 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001880 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001881
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001882 // It is allowed for a plugin to have no language map.
1883 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001884
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001885 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1886 if (!LangsToSuffixesList)
1887 throw std::string("Error in the language map definition!");
1888
1889 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001890 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001891
1892 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1893 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1894
1895 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001896 O.indent(Indent1) << "langMap[\""
1897 << InitPtrToString(Suffixes->getElement(i))
1898 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001899 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001900 }
1901
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001902 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001903}
1904
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001905/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1906/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001907void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001908 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001909 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001910 const std::string& OpName = d.getOperator()->getAsString();
1911
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001912 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001913 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001914 }
1915 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001916 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001917 }
1918 else if (OpName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001919 O.indent(IndentLevel)
1920 << "throw std::runtime_error(\"" <<
1921 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1922 : "Unknown error!")
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001923 << "\");\n";
1924 return;
1925 }
1926
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001927 else
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001928 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00001929 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001930
1931 if (d.getNumArgs() > 0)
1932 O << InitPtrToInt(d.getArg(0)) << ";\n";
1933 else
1934 O << "2;\n";
1935
Mikhail Glushenkov29063552008-05-06 18:18:20 +00001936}
1937
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001938/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001939void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001940 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001941 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001942
1943 // Class constructor.
1944 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001945 << "public:\n";
1946 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
1947 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001948
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001949 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001950 O.indent(Indent1)
1951 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
1952 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001953
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001954 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001955 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001956
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001957 O.indent(Indent2) << "return ret;\n";
1958 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001959}
1960
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001961/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001962void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001963 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001964 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001965 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001966 for (RecordVector::const_iterator B = EdgeVector.begin(),
1967 E = EdgeVector.end(); B != E; ++B) {
1968 const Record* Edge = *B;
1969 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001970 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001971
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001972 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001973 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001974 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001975 }
1976}
1977
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001978/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1979/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001980void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001981 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001982 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001983{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001984 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001985
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001986 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1987 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001988 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001989
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001990 O << '\n';
1991
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001992 // Insert edges.
1993
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001994 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001995 for (RecordVector::const_iterator B = EdgeVector.begin(),
1996 E = EdgeVector.end(); B != E; ++B) {
1997 const Record* Edge = *B;
1998 const std::string& NodeA = Edge->getValueAsString("a");
1999 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002000 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002001
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002002 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002003
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002004 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002005 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002006 else
2007 O << "new Edge" << i << "()";
2008
2009 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002010 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002011 }
2012
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002013 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002014}
2015
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002016/// ExtractHookNames - Extract the hook names from all instances of
2017/// $CALL(HookName) in the provided command line string. Helper
2018/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002019class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002020 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002021public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002022 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2023 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002024
2025 void operator()(const Init* CmdLine) {
2026 StrVector cmds;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002027 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002028 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2029 B != E; ++B) {
2030 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002031
2032 if (cmd == "$CALL") {
2033 unsigned NumArgs = 0;
2034 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2035 const std::string& HookName = *B;
2036
2037
2038 if (HookName.at(0) == ')')
2039 throw "$CALL invoked with no arguments!";
2040
2041 while (++B != E && B->at(0) != ')') {
2042 ++NumArgs;
2043 }
2044
2045 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2046
2047 if (H != HookNames_.end() && H->second != NumArgs)
2048 throw "Overloading of hooks is not allowed. Overloaded hook: "
2049 + HookName;
2050 else
2051 HookNames_[HookName] = NumArgs;
2052
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002053 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002054 }
2055 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002056};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002057
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002058/// FillInHookNames - Actually extract the hook names from all command
2059/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002060void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002061 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002062{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002063 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002064 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2065 E = ToolDescs.end(); B != E; ++B) {
2066 const ToolDescription& D = *(*B);
2067 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002068 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002069 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002070 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002071 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002072 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002073 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002074 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002075 }
2076}
2077
2078/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2079/// property records and emit hook function declaration for each
2080/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002081void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002082 llvm::StringMap<unsigned> HookNames;
2083
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002084 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002085 if (HookNames.empty())
2086 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002087
2088 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002089 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2090 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002091 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002092
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002093 for (unsigned i = 0, j = B->second; i < j; ++i) {
2094 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2095 }
2096
2097 O <<");\n";
2098 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002099 O << "}\n\n";
2100}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002101
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002102/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002103void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002104 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2105 O.indent(Indent1) << "int Priority() const { return "
2106 << Priority << "; }\n\n";
2107 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2108 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2109 O.indent(Indent1)
2110 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2111 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2112 << "};\n\n"
2113 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002114}
2115
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002116/// EmitIncludes - Emit necessary #include directives and some
2117/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002118void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002119 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2120 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002121 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002122 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2123 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002124
2125 << "#include \"llvm/ADT/StringExtras.h\"\n"
2126 << "#include \"llvm/Support/CommandLine.h\"\n\n"
2127
2128 << "#include <cstdlib>\n"
2129 << "#include <stdexcept>\n\n"
2130
2131 << "using namespace llvm;\n"
2132 << "using namespace llvmc;\n\n"
2133
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002134 << "extern cl::opt<std::string> OutputFilename;\n\n"
2135
2136 << "inline const char* checkCString(const char* s)\n"
2137 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002138}
2139
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002140
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002141/// PluginData - Holds all information about a plugin.
2142struct PluginData {
2143 OptionDescriptions OptDescs;
2144 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002145 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002146 ToolDescriptions ToolDescs;
2147 RecordVector Edges;
2148 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002149};
2150
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002151/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002152/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002153bool HasSink(const ToolDescriptions& ToolDescs) {
2154 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2155 E = ToolDescs.end(); B != E; ++B)
2156 if ((*B)->isSink())
2157 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002158
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002159 return false;
2160}
2161
2162/// HasExterns - Go through the list of option descriptions and check
2163/// if there are any external options.
2164bool HasExterns(const OptionDescriptions& OptDescs) {
2165 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2166 E = OptDescs.end(); B != E; ++B)
2167 if (B->second.isExtern())
2168 return true;
2169
2170 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002171}
2172
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002173/// CollectPluginData - Collect tool and option properties,
2174/// compilation graph edges and plugin priority from the parse tree.
2175void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2176 // Collect option properties.
2177 const RecordVector& OptionLists =
2178 Records.getAllDerivedDefinitions("OptionList");
2179 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2180 Data.OptDescs);
2181
2182 // Collect tool properties.
2183 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2184 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2185 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002186 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002187
2188 // Collect compilation graph edges.
2189 const RecordVector& CompilationGraphs =
2190 Records.getAllDerivedDefinitions("CompilationGraph");
2191 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2192 Data.Edges);
2193
2194 // Calculate the priority of this plugin.
2195 const RecordVector& Priorities =
2196 Records.getAllDerivedDefinitions("PluginPriority");
2197 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002198}
2199
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002200/// CheckPluginData - Perform some sanity checks on the collected data.
2201void CheckPluginData(PluginData& Data) {
2202 // Filter out all tools not mentioned in the compilation graph.
2203 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002204
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002205 // Typecheck the compilation graph.
2206 TypecheckGraph(Data.Edges, Data.ToolDescs);
2207
2208 // Check that there are no options without side effects (specified
2209 // only in the OptionList).
2210 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2211
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002212}
2213
Daniel Dunbar1a551802009-07-03 00:10:29 +00002214void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002215 // Emit file header.
2216 EmitIncludes(O);
2217
2218 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002219 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002220
2221 // Emit hook declarations.
2222 EmitHookDeclarations(Data.ToolDescs, O);
2223
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002224 O << "namespace {\n\n";
2225
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002226 // Emit PopulateLanguageMap() function
2227 // (a language map maps from file extensions to language names).
2228 EmitPopulateLanguageMap(Records, O);
2229
2230 // Emit Tool classes.
2231 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2232 E = Data.ToolDescs.end(); B!=E; ++B)
2233 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2234
2235 // Emit Edge# classes.
2236 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2237
2238 // Emit PopulateCompilationGraph() function.
2239 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2240
2241 // Emit code for plugin registration.
2242 EmitRegisterPlugin(Data.Priority, O);
2243
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002244 O << "} // End anonymous namespace.\n\n";
2245
2246 // Force linkage magic.
2247 O << "namespace llvmc {\n";
2248 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2249 O << "}\n";
2250
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002251 // EOF
2252}
2253
2254
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002255// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002256}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002257
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002258/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002259void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002260 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002261 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002262
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002263 CollectPluginData(Records, Data);
2264 CheckPluginData(Data);
2265
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002266 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002267 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002268
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002269 } catch (std::exception& Error) {
2270 throw Error.what() + std::string(" - usually this means a syntax error.");
2271 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002272}