blob: 6dfd1b362d5c8476d20895e26e0492efd8d53705 [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) {
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000778 int priority = 0;
779
780 if (B != E) {
781 priority = static_cast<int>((*B)->getValueAsInt("priority"));
782
783 if (++B != E)
784 throw std::string("More than one 'PluginPriority' instance found: "
785 "most probably an error!");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000786 }
Mikhail Glushenkov2cea7bd2009-10-17 20:08:30 +0000787
788 return priority;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000789}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000790
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000791/// NotInGraph - Helper function object for FilterNotInGraph.
792struct NotInGraph {
793private:
794 const llvm::StringSet<>& ToolsInGraph_;
795
796public:
797 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
798 : ToolsInGraph_(ToolsInGraph)
799 {}
800
801 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
802 return (ToolsInGraph_.count(x->Name) == 0);
803 }
804};
805
806/// FilterNotInGraph - Filter out from ToolDescs all Tools not
807/// mentioned in the compilation graph definition.
808void FilterNotInGraph (const RecordVector& EdgeVector,
809 ToolDescriptions& ToolDescs) {
810
811 // List all tools mentioned in the graph.
812 llvm::StringSet<> ToolsInGraph;
813
814 for (RecordVector::const_iterator B = EdgeVector.begin(),
815 E = EdgeVector.end(); B != E; ++B) {
816
817 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000818 const std::string& NodeA = Edge->getValueAsString("a");
819 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000820
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000821 if (NodeA != "root")
822 ToolsInGraph.insert(NodeA);
823 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000824 }
825
826 // Filter ToolPropertiesList.
827 ToolDescriptions::iterator new_end =
828 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
829 NotInGraph(ToolsInGraph));
830 ToolDescs.erase(new_end, ToolDescs.end());
831}
832
833/// FillInToolToLang - Fills in two tables that map tool names to
834/// (input, output) languages. Helper function used by TypecheckGraph().
835void FillInToolToLang (const ToolDescriptions& ToolDescs,
836 StringMap<StringSet<> >& ToolToInLang,
837 StringMap<std::string>& ToolToOutLang) {
838 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
839 E = ToolDescs.end(); B != E; ++B) {
840 const ToolDescription& D = *(*B);
841 for (StrVector::const_iterator B = D.InLanguage.begin(),
842 E = D.InLanguage.end(); B != E; ++B)
843 ToolToInLang[D.Name].insert(*B);
844 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000845 }
846}
847
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000848/// TypecheckGraph - Check that names for output and input languages
849/// on all edges do match. This doesn't do much when the information
850/// about the whole graph is not available (i.e. when compiling most
851/// plugins).
852void TypecheckGraph (const RecordVector& EdgeVector,
853 const ToolDescriptions& ToolDescs) {
854 StringMap<StringSet<> > ToolToInLang;
855 StringMap<std::string> ToolToOutLang;
856
857 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
858 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
859 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
860
861 for (RecordVector::const_iterator B = EdgeVector.begin(),
862 E = EdgeVector.end(); B != E; ++B) {
863 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000864 const std::string& NodeA = Edge->getValueAsString("a");
865 const std::string& NodeB = Edge->getValueAsString("b");
866 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
867 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000868
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000869 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000871 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000872 + ": output->input language mismatch";
873 }
874
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000875 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000876 throw std::string("Edges back to the root are not allowed!");
877 }
878}
879
880/// WalkCase - Walks the 'case' expression DAG and invokes
881/// TestCallback on every test, and StatementCallback on every
882/// statement. Handles 'case' nesting, but not the 'and' and 'or'
883/// combinators.
884// TODO: Re-implement EmitCaseConstructHandler on top of this function?
885template <typename F1, typename F2>
886void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
887 const DagInit& d = InitPtrToDag(Case);
888 bool even = false;
889 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
890 B != E; ++B) {
891 Init* arg = *B;
892 if (even && dynamic_cast<DagInit*>(arg)
893 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
894 WalkCase(arg, TestCallback, StatementCallback);
895 else if (!even)
896 TestCallback(arg);
897 else
898 StatementCallback(arg);
899 even = !even;
900 }
901}
902
903/// ExtractOptionNames - A helper function object used by
904/// CheckForSuperfluousOptions() to walk the 'case' DAG.
905class ExtractOptionNames {
906 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000907
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000908 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000909 const DagInit& Stmt = InitPtrToDag(Statement);
910 const std::string& ActionName = Stmt.getOperator()->getAsString();
911 if (ActionName == "forward" || ActionName == "forward_as" ||
912 ActionName == "unpack_values" || ActionName == "switch_on" ||
913 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000914 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000915 checkNumberOfArguments(&Stmt, 1);
916 const std::string& Name = InitPtrToString(Stmt.getArg(0));
917 OptionNames_.insert(Name);
918 }
919 else if (ActionName == "and" || ActionName == "or") {
920 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000921 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000922 }
923 }
924 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000925
926public:
927 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
928 {}
929
930 void operator()(const Init* Statement) {
931 if (typeid(*Statement) == typeid(ListInit)) {
932 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
933 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
934 B != E; ++B)
935 this->processDag(*B);
936 }
937 else {
938 this->processDag(Statement);
939 }
940 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000941};
942
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000943/// CheckForSuperfluousOptions - Check that there are no side
944/// effect-free options (specified only in the OptionList). Otherwise,
945/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000946void CheckForSuperfluousOptions (const RecordVector& Edges,
947 const ToolDescriptions& ToolDescs,
948 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000949 llvm::StringSet<> nonSuperfluousOptions;
950
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000951 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000952 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000953 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
954 E = ToolDescs.end(); B != E; ++B) {
955 const ToolDescription& TD = *(*B);
956 ExtractOptionNames Callback(nonSuperfluousOptions);
957 if (TD.Actions)
958 WalkCase(TD.Actions, Callback, Callback);
959 }
960
961 // Add all options mentioned in the 'case' clauses of the
962 // OptionalEdges of the compilation graph to the set of
963 // non-superfluous options.
964 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
965 B != E; ++B) {
966 const Record* Edge = *B;
967 DagInit* Weight = Edge->getValueAsDag("weight");
968
969 if (!isDagEmpty(Weight))
970 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000971 }
972
973 // Check that all options in OptDescs belong to the set of
974 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000975 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000976 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000977 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000978 if (!nonSuperfluousOptions.count(Val.Name)
979 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +0000980 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000981 "Probable cause: this option is specified only in the OptionList.\n";
982 }
983}
984
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +0000985/// EmitCaseTest0Args - Helper function used by EmitCaseConstructHandler().
986bool EmitCaseTest0Args(const std::string& TestName, raw_ostream& O) {
987 if (TestName == "single_input_file") {
988 O << "InputFilenames.size() == 1";
989 return true;
990 }
991 else if (TestName == "multiple_input_files") {
992 O << "InputFilenames.size() > 1";
993 return true;
994 }
995
996 return false;
997}
998
999
1000/// EmitCaseTest1Arg - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001001bool EmitCaseTest1Arg(const std::string& TestName,
1002 const DagInit& d,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001003 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001004 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001005 checkNumberOfArguments(&d, 1);
1006 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001007
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001008 if (TestName == "switch_on") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001009 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001010 if (!OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001011 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001012 O << OptDesc.GenVariableName();
1013 return true;
1014 } else if (TestName == "input_languages_contain") {
1015 O << "InLangs.count(\"" << OptName << "\") != 0";
1016 return true;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001017 } else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +00001018 // This works only for single-argument Tool::GenerateAction. Join
1019 // tools can process several files in different languages simultaneously.
1020
1021 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001022 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001023 return true;
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001024 } else if (TestName == "not_empty" || TestName == "empty") {
1025 const char* Test = (TestName == "empty") ? "" : "!";
1026
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001027 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001028 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001029 return true;
1030 }
1031 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001032 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001033 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001034 throw OptName
1035 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001036 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001037 return true;
1038 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001039 }
1040
1041 return false;
1042}
1043
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001044/// EmitCaseTest2Args - Helper function used by EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001045bool EmitCaseTest2Args(const std::string& TestName,
1046 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001047 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001048 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001049 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001050 checkNumberOfArguments(&d, 2);
1051 const std::string& OptName = InitPtrToString(d.getArg(0));
1052 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001053 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001054
1055 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001056 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001058 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1059 return true;
1060 }
1061 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001062 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001063 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001064 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001065 O << "std::find(" << VarName << ".begin(),\n";
1066 O.indent(IndentLevel + Indent1)
1067 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001068 << OptArg << "\") != " << VarName << ".end()";
1069 return true;
1070 }
1071
1072 return false;
1073}
1074
1075// Forward declaration.
1076// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001077void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001078 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001079 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001080
1081/// EmitLogicalOperationTest - Helper function used by
1082/// EmitCaseConstructHandler.
1083void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001084 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001085 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001086 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001087 O << '(';
1088 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001089 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001090 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001091 if (j != NumArgs - 1) {
1092 O << ")\n";
1093 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1094 }
1095 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001096 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001097 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001098 }
1099}
1100
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001101void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001102 const OptionDescriptions& OptDescs, raw_ostream& O)
1103{
1104 checkNumberOfArguments(&d, 1);
1105 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1106 O << "! (";
1107 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1108 O << ")";
1109}
1110
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001111/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001112void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001113 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001114 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001115 const std::string& TestName = d.getOperator()->getAsString();
1116
1117 if (TestName == "and")
1118 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1119 else if (TestName == "or")
1120 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001121 else if (TestName == "not")
1122 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00001123 else if (EmitCaseTest0Args(TestName, O))
1124 return;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001125 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1126 return;
1127 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1128 return;
1129 else
1130 throw TestName + ": unknown edge property!";
1131}
1132
1133// Emit code that handles the 'case' construct.
1134// Takes a function object that should emit code for every case clause.
1135// Callback's type is
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001136// void F(Init* Statement, unsigned IndentLevel, raw_ostream& O).
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001137template <typename F>
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001138void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001139 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001140 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001141 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001142 const DagInit* d = &InitPtrToDag(Dag);
1143 if (d->getOperator()->getAsString() != "case")
1144 throw std::string("EmitCaseConstructHandler should be invoked"
1145 " only on 'case' expressions!");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001146
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001147 unsigned numArgs = d->getNumArgs();
1148 if (d->getNumArgs() < 2)
1149 throw "There should be at least one clause in the 'case' expression:\n"
1150 + d->getAsString();
1151
1152 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001153 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001154
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001155 // Emit the test.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001156 if (Test.getOperator()->getAsString() == "default") {
1157 if (i+2 != numArgs)
1158 throw std::string("The 'default' clause should be the last in the"
1159 "'case' construct!");
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001160 O.indent(IndentLevel) << "else {\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001161 }
1162 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001163 O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001164 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1165 O << ") {\n";
1166 }
1167
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001168 // Emit the corresponding statement.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001169 ++i;
1170 if (i == numArgs)
1171 throw "Case construct handler: no corresponding action "
1172 "found for the test " + Test.getAsString() + '!';
1173
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001174 Init* arg = d->getArg(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001175 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1176 if (nd && (nd->getOperator()->getAsString() == "case")) {
1177 // Handle the nested 'case'.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001178 EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001179 Callback, EmitElseIf, OptDescs, O);
1180 }
1181 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001182 Callback(arg, (IndentLevel + Indent1), O);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001183 }
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001184 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001185 }
1186}
1187
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001188/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1189/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1190/// Helper function used by EmitCmdLineVecFill and.
1191void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1192 const char* Delimiters = " \t\n\v\f\r";
1193 enum TokenizerState
1194 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1195 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001196
1197 if (CmdLine.empty())
1198 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001199 Out.push_back("");
1200
1201 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1202 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001203
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001204 for (; B != E; ++B) {
1205 char cur_ch = CmdLine[B];
1206
1207 switch (cur_st) {
1208 case Normal:
1209 if (cur_ch == '$') {
1210 cur_st = SpecialCommand;
1211 break;
1212 }
1213 if (oneOf(Delimiters, cur_ch)) {
1214 // Skip whitespace
1215 B = CmdLine.find_first_not_of(Delimiters, B);
1216 if (B == std::string::npos) {
1217 B = E-1;
1218 continue;
1219 }
1220 --B;
1221 Out.push_back("");
1222 continue;
1223 }
1224 break;
1225
1226
1227 case SpecialCommand:
1228 if (oneOf(Delimiters, cur_ch)) {
1229 cur_st = Normal;
1230 Out.push_back("");
1231 continue;
1232 }
1233 if (cur_ch == '(') {
1234 Out.push_back("");
1235 cur_st = InsideSpecialCommand;
1236 continue;
1237 }
1238 break;
1239
1240 case InsideSpecialCommand:
1241 if (oneOf(Delimiters, cur_ch)) {
1242 continue;
1243 }
1244 if (cur_ch == '\'') {
1245 cur_st = InsideQuotationMarks;
1246 Out.push_back("");
1247 continue;
1248 }
1249 if (cur_ch == ')') {
1250 cur_st = Normal;
1251 Out.push_back("");
1252 }
1253 if (cur_ch == ',') {
1254 continue;
1255 }
1256
1257 break;
1258
1259 case InsideQuotationMarks:
1260 if (cur_ch == '\'') {
1261 cur_st = InsideSpecialCommand;
1262 continue;
1263 }
1264 break;
1265 }
1266
1267 Out.back().push_back(cur_ch);
1268 }
1269}
1270
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001271/// SubstituteSpecialCommands - Perform string substitution for $CALL
1272/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001273StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001274(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001275{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001276
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001277 const std::string& cmd = *Pos;
1278
1279 if (cmd == "$CALL") {
1280 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1281 const std::string& CmdName = *Pos;
1282
1283 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001284 throw std::string("$CALL invocation: empty argument list!");
1285
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001286 O << "hooks::";
1287 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001288
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001289
1290 bool firstIteration = true;
1291 while (true) {
1292 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1293 const std::string& Arg = *Pos;
1294 assert(Arg.size() != 0);
1295
1296 if (Arg[0] == ')')
1297 break;
1298
1299 if (firstIteration)
1300 firstIteration = false;
1301 else
1302 O << ", ";
1303
1304 O << '"' << Arg << '"';
1305 }
1306
1307 O << ')';
1308
1309 }
1310 else if (cmd == "$ENV") {
1311 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1312 const std::string& EnvName = *Pos;
1313
1314 if (EnvName == ")")
1315 throw "$ENV invocation: empty argument list!";
1316
1317 O << "checkCString(std::getenv(\"";
1318 O << EnvName;
1319 O << "\"))";
1320
1321 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001322 }
1323 else {
1324 throw "Unknown special command: " + cmd;
1325 }
1326
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001327 const std::string& Leftover = *Pos;
1328 assert(Leftover.at(0) == ')');
1329 if (Leftover.size() != 1)
1330 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001331
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001332 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001333}
1334
1335/// EmitCmdLineVecFill - Emit code that fills in the command line
1336/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001337void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001338 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001339 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001340 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001341 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1342
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001343 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001344 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001345
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001346 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1347
1348 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001349 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001350 if (StrVec[0][0] == '$') {
1351 while (I != E && (*I)[0] != ')' )
1352 ++I;
1353
1354 // Skip the ')' symbol.
1355 ++I;
1356 }
1357 else {
1358 ++I;
1359 }
1360
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001361 bool hasINFILE = false;
1362
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001363 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001364 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001365 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001366 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001367 if (cmd.at(0) == '$') {
1368 if (cmd == "$INFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001369 hasINFILE = true;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001370 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001371 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001372 << ", E = inFiles.end();\n";
1373 O.indent(IndentLevel) << "B != E; ++B)\n";
1374 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1375 }
1376 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001377 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001378 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001379 }
1380 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001381 O << "vec.push_back(\"\");\n";
1382 O.indent(IndentLevel) << "out_file_index = vec.size()-1;\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001383 }
1384 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001385 O << "vec.push_back(";
1386 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001387 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001388 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001389 }
1390 else {
1391 O << "vec.push_back(\"" << cmd << "\");\n";
1392 }
1393 }
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001394 if (!hasINFILE)
1395 throw "Tool '" + ToolName + "' doesn't take any input!";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001396
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001397 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001398 if (StrVec[0][0] == '$')
1399 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1400 else
1401 O << '"' << StrVec[0] << '"';
1402 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001403}
1404
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001405/// EmitCmdLineVecFillCallback - A function object wrapper around
1406/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1407/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001408class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001409 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001410 const std::string& ToolName;
1411 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001412 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1413 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001414
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001415 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001416 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001417 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001418 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001419 }
1420};
1421
1422/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1423/// implement EmitActionHandler. Emits code for
1424/// handling the (forward) and (forward_as) option properties.
1425void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001426 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001427 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001428 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001429 const std::string& Name = NewName.empty()
1430 ? ("-" + D.Name)
1431 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001432 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001433
1434 switch (D.Type) {
1435 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001436 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001437 break;
1438 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001439 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1440 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001441 break;
1442 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001443 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1444 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001445 break;
1446 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001447 O.indent(IndentLevel)
1448 << "for (" << D.GenTypeDeclaration()
1449 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1450 O.indent(IndentLevel)
1451 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1452 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1453 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001454
1455 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001456 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1457 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001458 }
1459
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001460 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001461 break;
1462 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001463 O.indent(IndentLevel)
1464 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1465 << D.GenVariableName() << ".begin(),\n";
1466 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1467 << ".end() ; B != E;) {\n";
1468 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001469
1470 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001471 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1472 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001473 }
1474
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001475 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001476 break;
1477 case OptionType::Alias:
1478 default:
1479 throw std::string("Aliases are not allowed in tool option descriptions!");
1480 }
1481}
1482
1483/// EmitActionHandler - Emit code that handles actions. Used by
1484/// EmitGenerateActionMethod() as an argument to
1485/// EmitCaseConstructHandler().
1486class EmitActionHandler {
1487 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001488
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001489 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001490 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001491 {
1492 const DagInit& Dag = InitPtrToDag(Statement);
1493 const std::string& ActionName = Dag.getOperator()->getAsString();
1494
1495 if (ActionName == "append_cmd") {
1496 checkNumberOfArguments(&Dag, 1);
1497 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001498 StrVector Out;
1499 llvm::SplitString(Cmd, Out);
1500
1501 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1502 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001503 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001504 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001505 else if (ActionName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001506 O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001507 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1508 : "Unknown error!")
1509 << "\");\n";
1510 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001511 else if (ActionName == "forward") {
1512 checkNumberOfArguments(&Dag, 1);
1513 const std::string& Name = InitPtrToString(Dag.getArg(0));
1514 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1515 IndentLevel, "", O);
1516 }
1517 else if (ActionName == "forward_as") {
1518 checkNumberOfArguments(&Dag, 2);
1519 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001520 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001521 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1522 IndentLevel, NewName, O);
1523 }
1524 else if (ActionName == "output_suffix") {
1525 checkNumberOfArguments(&Dag, 1);
1526 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001527 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001528 }
1529 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001530 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001531 }
1532 else if (ActionName == "unpack_values") {
1533 checkNumberOfArguments(&Dag, 1);
1534 const std::string& Name = InitPtrToString(Dag.getArg(0));
1535 const OptionDescription& D = OptDescs.FindOption(Name);
1536
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001537 if (D.isMultiVal())
1538 throw std::string("Can't use unpack_values with multi-valued options!");
1539
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001540 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001541 O.indent(IndentLevel)
1542 << "for (" << D.GenTypeDeclaration()
1543 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1544 O.indent(IndentLevel)
1545 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1546 O.indent(IndentLevel + Indent1)
1547 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001548 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001549 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001550 O.indent(IndentLevel) << "llvm::SplitString("
1551 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001552 }
1553 else {
1554 throw "Option '" + D.Name +
1555 "': switches can't have the 'unpack_values' property!";
1556 }
1557 }
1558 else {
1559 throw "Unknown action name: " + ActionName + "!";
1560 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001561 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001562 public:
1563 EmitActionHandler(const OptionDescriptions& OD)
1564 : OptDescs(OD) {}
1565
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001566 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001567 raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001568 {
1569 if (typeid(*Statement) == typeid(ListInit)) {
1570 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1571 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1572 B != E; ++B)
1573 this->processActionDag(*B, IndentLevel, O);
1574 }
1575 else {
1576 this->processActionDag(Statement, IndentLevel, O);
1577 }
1578 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001579};
1580
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001581bool IsOutFileIndexCheckRequiredStr (const Init* CmdLine) {
1582 StrVector StrVec;
1583 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1584
1585 for (StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1586 I != E; ++I) {
1587 if (*I == "$OUTFILE")
1588 return false;
1589 }
1590
1591 return true;
1592}
1593
1594class IsOutFileIndexCheckRequiredStrCallback {
1595 bool* ret_;
1596
1597public:
1598 IsOutFileIndexCheckRequiredStrCallback(bool* ret) : ret_(ret)
1599 {}
1600
1601 void operator()(const Init* CmdLine) {
1602 if (IsOutFileIndexCheckRequiredStr(CmdLine))
1603 *ret_ = true;
1604 }
1605};
1606
1607bool IsOutFileIndexCheckRequiredCase (Init* CmdLine) {
1608 bool ret = false;
1609 WalkCase(CmdLine, Id(), IsOutFileIndexCheckRequiredStrCallback(&ret));
1610 return ret;
1611}
1612
1613/// IsOutFileIndexCheckRequired - Should we emit an "out_file_index != -1" check
1614/// in EmitGenerateActionMethod() ?
1615bool IsOutFileIndexCheckRequired (Init* CmdLine) {
1616 if (typeid(*CmdLine) == typeid(StringInit))
1617 return IsOutFileIndexCheckRequiredStr(CmdLine);
1618 else
1619 return IsOutFileIndexCheckRequiredCase(CmdLine);
1620}
1621
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001622// EmitGenerateActionMethod - Emit either a normal or a "join" version of the
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001623// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001624void EmitGenerateActionMethod (const ToolDescription& D,
1625 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001626 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001627 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001628 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001629 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001630 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001631
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001632 O.indent(Indent2) << "bool HasChildren,\n";
1633 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1634 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1635 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1636 O.indent(Indent1) << "{\n";
1637 O.indent(Indent2) << "std::string cmd;\n";
1638 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1639 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1640 O.indent(Indent2) << "const char* output_suffix = \""
1641 << D.OutputSuffix << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001642
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001643 if (!D.CmdLine)
1644 throw "Tool " + D.Name + " has no cmd_line property!";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001645
1646 bool IndexCheckRequired = IsOutFileIndexCheckRequired(D.CmdLine);
1647 O.indent(Indent2) << "int out_file_index"
1648 << (IndexCheckRequired ? " = -1" : "")
1649 << ";\n\n";
1650
1651 // Process the cmd_line property.
1652 if (typeid(*D.CmdLine) == typeid(StringInit))
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001653 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1654 else
1655 EmitCaseConstructHandler(D.CmdLine, Indent2,
1656 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1657 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001658
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001659 // For every understood option, emit handling code.
1660 if (D.Actions)
1661 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1662 false, OptDescs, O);
1663
1664 O << '\n';
1665 O.indent(Indent2)
1666 << "std::string out_file = OutFilename("
1667 << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1668 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkov0b599392009-10-09 05:45:21 +00001669
1670 if (IndexCheckRequired)
1671 O.indent(Indent2) << "if (out_file_index != -1)\n";
1672 O.indent(IndexCheckRequired ? Indent3 : Indent2)
1673 << "vec[out_file_index] = out_file;\n";
Mikhail Glushenkov39482dd2009-10-08 04:40:08 +00001674
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001675 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001676 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001677 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1678 O.indent(Indent3) << "vec.insert(vec.end(), "
1679 << SinkOptionName << ".begin(), " << SinkOptionName
1680 << ".end());\n";
1681 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001682 }
1683
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001684 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1685 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001686}
1687
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001688/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1689/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001690void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1691 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001692 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001693 if (!ToolDesc.isJoin()) {
1694 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1695 O.indent(Indent2) << "bool HasChildren,\n";
1696 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1697 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1698 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1699 O.indent(Indent1) << "{\n";
1700 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1701 << " is not a Join tool!\");\n";
1702 O.indent(Indent1) << "}\n\n";
1703 }
1704 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001705 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001706 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001707
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001708 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001709}
1710
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001711/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1712/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001713void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001714 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1715 O.indent(Indent2) << "return InputLanguages_;\n";
1716 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001717
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001718 if (D.OutLanguage.empty())
1719 throw "Tool " + D.Name + " has no 'out_language' property!";
1720
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001721 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1722 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1723 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001724}
1725
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001726/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001727void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001728 O.indent(Indent1) << "const char* Name() const {\n";
1729 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1730 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001731}
1732
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001733/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1734/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001735void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001736 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001737 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001738 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001739 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001740 O.indent(Indent2) << "return false;\n";
1741 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001742}
1743
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001744/// EmitStaticMemberDefinitions - Emit static member definitions for a
1745/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001746void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001747 if (D.InLanguage.empty())
1748 throw "Tool " + D.Name + " has no 'in_language' property!";
1749
1750 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1751 for (StrVector::const_iterator B = D.InLanguage.begin(),
1752 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001753 O << '\"' << *B << "\", ";
1754 O << "0};\n\n";
1755}
1756
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001757/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001758void EmitToolClassDefinition (const ToolDescription& D,
1759 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001760 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001761 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001762 return;
1763
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001764 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001765 O << "class " << D.Name << " : public ";
1766 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001767 O << "JoinTool";
1768 else
1769 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001770
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001771 O << "{\nprivate:\n";
1772 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001773
1774 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001775 EmitNameMethod(D, O);
1776 EmitInOutLanguageMethods(D, O);
1777 EmitIsJoinMethod(D, O);
1778 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001779
1780 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001781 O << "};\n";
1782
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001783 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001784
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001785}
1786
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001787/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001788/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001789void EmitOptionDefinitions (const OptionDescriptions& descs,
1790 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001791 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001792{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001793 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001794
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001795 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001796 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001797 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001798 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001799
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001800 if (val.Type == OptionType::Alias) {
1801 Aliases.push_back(val);
1802 continue;
1803 }
1804
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001805 if (val.isExtern())
1806 O << "extern ";
1807
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001808 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001809 << val.GenVariableName();
1810
1811 if (val.isExtern()) {
1812 O << ";\n";
1813 continue;
1814 }
1815
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001816 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001817
1818 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1819 O << ", cl::Prefix";
1820
1821 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001822 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001823 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001824 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001825 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001826 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001827 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001828 O << ", cl::OneOrMore";
1829 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001830 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001831 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001832 }
1833
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001834 if (val.isReallyHidden()) {
1835 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001836 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001837 else if (val.isHidden()) {
1838 O << ", cl::Hidden";
1839 }
1840
1841 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001842 O << ", cl::multi_val(" << val.MultiVal << ')';
1843
1844 if (val.InitVal) {
1845 const std::string& str = val.InitVal->getAsString();
1846 O << ", cl::init(" << str << ')';
1847 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001848
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001849 if (!val.Help.empty())
1850 O << ", cl::desc(\"" << val.Help << "\")";
1851
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001852 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001853 }
1854
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001855 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001856 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001857 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001858 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001859
1860 O << val.GenTypeDeclaration() << ' '
1861 << val.GenVariableName()
1862 << "(\"" << val.Name << '\"';
1863
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001864 const OptionDescription& D = descs.FindOption(val.Help);
1865 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001866
1867 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1868 }
1869
1870 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001871 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00001872 O << (HasExterns ? "extern cl" : "cl")
1873 << "::list<std::string> " << SinkOptionName
1874 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001875
1876 O << '\n';
1877}
1878
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001879/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001880void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001881{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001882 // Generate code
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001883 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001884
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001885 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001886 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001887
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001888 // It is allowed for a plugin to have no language map.
1889 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001890
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001891 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1892 if (!LangsToSuffixesList)
1893 throw std::string("Error in the language map definition!");
1894
1895 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001896 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001897
1898 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1899 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1900
1901 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001902 O.indent(Indent1) << "langMap[\""
1903 << InitPtrToString(Suffixes->getElement(i))
1904 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001905 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001906 }
1907
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001908 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001909}
1910
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001911/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1912/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001913void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001914 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001915 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001916 const std::string& OpName = d.getOperator()->getAsString();
1917
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001918 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001919 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001920 }
1921 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001922 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001923 }
1924 else if (OpName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001925 O.indent(IndentLevel)
1926 << "throw std::runtime_error(\"" <<
1927 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1928 : "Unknown error!")
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001929 << "\");\n";
1930 return;
1931 }
1932
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001933 else
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001934 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00001935 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001936
1937 if (d.getNumArgs() > 0)
1938 O << InitPtrToInt(d.getArg(0)) << ";\n";
1939 else
1940 O << "2;\n";
1941
Mikhail Glushenkov29063552008-05-06 18:18:20 +00001942}
1943
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001944/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001945void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001946 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001947 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001948
1949 // Class constructor.
1950 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001951 << "public:\n";
1952 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
1953 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001954
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001955 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001956 O.indent(Indent1)
1957 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
1958 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001959
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001960 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001961 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001962
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001963 O.indent(Indent2) << "return ret;\n";
1964 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001965}
1966
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001967/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001968void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001969 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001970 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001971 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001972 for (RecordVector::const_iterator B = EdgeVector.begin(),
1973 E = EdgeVector.end(); B != E; ++B) {
1974 const Record* Edge = *B;
1975 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001976 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001977
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001978 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001979 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001980 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001981 }
1982}
1983
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001984/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1985/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001986void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001987 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001988 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001989{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001990 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001991
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001992 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1993 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001994 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001995
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001996 O << '\n';
1997
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001998 // Insert edges.
1999
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002000 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002001 for (RecordVector::const_iterator B = EdgeVector.begin(),
2002 E = EdgeVector.end(); B != E; ++B) {
2003 const Record* Edge = *B;
2004 const std::string& NodeA = Edge->getValueAsString("a");
2005 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002006 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002007
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002008 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002009
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00002010 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00002011 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00002012 else
2013 O << "new Edge" << i << "()";
2014
2015 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002016 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002017 }
2018
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002019 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002020}
2021
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002022/// ExtractHookNames - Extract the hook names from all instances of
2023/// $CALL(HookName) in the provided command line string. Helper
2024/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002025class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002026 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002027public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002028 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
2029 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002030
2031 void operator()(const Init* CmdLine) {
2032 StrVector cmds;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002033 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002034 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
2035 B != E; ++B) {
2036 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002037
2038 if (cmd == "$CALL") {
2039 unsigned NumArgs = 0;
2040 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
2041 const std::string& HookName = *B;
2042
2043
2044 if (HookName.at(0) == ')')
2045 throw "$CALL invoked with no arguments!";
2046
2047 while (++B != E && B->at(0) != ')') {
2048 ++NumArgs;
2049 }
2050
2051 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
2052
2053 if (H != HookNames_.end() && H->second != NumArgs)
2054 throw "Overloading of hooks is not allowed. Overloaded hook: "
2055 + HookName;
2056 else
2057 HookNames_[HookName] = NumArgs;
2058
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002059 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002060 }
2061 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002062};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002063
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002064/// FillInHookNames - Actually extract the hook names from all command
2065/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002066void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002067 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002068{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002069 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002070 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2071 E = ToolDescs.end(); B != E; ++B) {
2072 const ToolDescription& D = *(*B);
2073 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002074 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002075 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002076 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002077 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00002078 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002079 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002080 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002081 }
2082}
2083
2084/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2085/// property records and emit hook function declaration for each
2086/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002087void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002088 llvm::StringMap<unsigned> HookNames;
2089
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002090 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002091 if (HookNames.empty())
2092 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002093
2094 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002095 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2096 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002097 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002098
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002099 for (unsigned i = 0, j = B->second; i < j; ++i) {
2100 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2101 }
2102
2103 O <<");\n";
2104 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002105 O << "}\n\n";
2106}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002107
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002108/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002109void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002110 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2111 O.indent(Indent1) << "int Priority() const { return "
2112 << Priority << "; }\n\n";
2113 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2114 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2115 O.indent(Indent1)
2116 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2117 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2118 << "};\n\n"
2119 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002120}
2121
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002122/// EmitIncludes - Emit necessary #include directives and some
2123/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002124void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkovad981bf2009-09-28 01:16:42 +00002125 O << "#include \"llvm/CompilerDriver/BuiltinOptions.h\"\n"
2126 << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002127 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002128 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2129 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002130
2131 << "#include \"llvm/ADT/StringExtras.h\"\n"
2132 << "#include \"llvm/Support/CommandLine.h\"\n\n"
2133
2134 << "#include <cstdlib>\n"
2135 << "#include <stdexcept>\n\n"
2136
2137 << "using namespace llvm;\n"
2138 << "using namespace llvmc;\n\n"
2139
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002140 << "extern cl::opt<std::string> OutputFilename;\n\n"
2141
2142 << "inline const char* checkCString(const char* s)\n"
2143 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002144}
2145
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002146
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002147/// PluginData - Holds all information about a plugin.
2148struct PluginData {
2149 OptionDescriptions OptDescs;
2150 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002151 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002152 ToolDescriptions ToolDescs;
2153 RecordVector Edges;
2154 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002155};
2156
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002157/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002158/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002159bool HasSink(const ToolDescriptions& ToolDescs) {
2160 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2161 E = ToolDescs.end(); B != E; ++B)
2162 if ((*B)->isSink())
2163 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002164
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002165 return false;
2166}
2167
2168/// HasExterns - Go through the list of option descriptions and check
2169/// if there are any external options.
2170bool HasExterns(const OptionDescriptions& OptDescs) {
2171 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2172 E = OptDescs.end(); B != E; ++B)
2173 if (B->second.isExtern())
2174 return true;
2175
2176 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002177}
2178
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002179/// CollectPluginData - Collect tool and option properties,
2180/// compilation graph edges and plugin priority from the parse tree.
2181void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2182 // Collect option properties.
2183 const RecordVector& OptionLists =
2184 Records.getAllDerivedDefinitions("OptionList");
2185 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2186 Data.OptDescs);
2187
2188 // Collect tool properties.
2189 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2190 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2191 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002192 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193
2194 // Collect compilation graph edges.
2195 const RecordVector& CompilationGraphs =
2196 Records.getAllDerivedDefinitions("CompilationGraph");
2197 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2198 Data.Edges);
2199
2200 // Calculate the priority of this plugin.
2201 const RecordVector& Priorities =
2202 Records.getAllDerivedDefinitions("PluginPriority");
2203 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002204}
2205
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002206/// CheckPluginData - Perform some sanity checks on the collected data.
2207void CheckPluginData(PluginData& Data) {
2208 // Filter out all tools not mentioned in the compilation graph.
2209 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002210
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002211 // Typecheck the compilation graph.
2212 TypecheckGraph(Data.Edges, Data.ToolDescs);
2213
2214 // Check that there are no options without side effects (specified
2215 // only in the OptionList).
2216 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2217
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002218}
2219
Daniel Dunbar1a551802009-07-03 00:10:29 +00002220void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002221 // Emit file header.
2222 EmitIncludes(O);
2223
2224 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002225 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002226
2227 // Emit hook declarations.
2228 EmitHookDeclarations(Data.ToolDescs, O);
2229
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002230 O << "namespace {\n\n";
2231
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002232 // Emit PopulateLanguageMap() function
2233 // (a language map maps from file extensions to language names).
2234 EmitPopulateLanguageMap(Records, O);
2235
2236 // Emit Tool classes.
2237 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2238 E = Data.ToolDescs.end(); B!=E; ++B)
2239 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2240
2241 // Emit Edge# classes.
2242 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2243
2244 // Emit PopulateCompilationGraph() function.
2245 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2246
2247 // Emit code for plugin registration.
2248 EmitRegisterPlugin(Data.Priority, O);
2249
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002250 O << "} // End anonymous namespace.\n\n";
2251
2252 // Force linkage magic.
2253 O << "namespace llvmc {\n";
2254 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2255 O << "}\n";
2256
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002257 // EOF
2258}
2259
2260
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002261// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002262}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002263
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002264/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002265void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002266 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002267 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002268
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002269 CollectPluginData(Records, Data);
2270 CheckPluginData(Data);
2271
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002272 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002273 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002274
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002275 } catch (std::exception& Error) {
2276 throw Error.what() + std::string(" - usually this means a syntax error.");
2277 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002278}