blob: a75e56035d207babffd7ba7f8c7058bf21fa22a2 [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;
44unsigned Indent1 = TabWidth*1;
45unsigned Indent2 = TabWidth*2;
46unsigned Indent3 = TabWidth*3;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000047
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057/// Id - An 'identity' function object.
58struct Id {
59 template<typename T>
60 void operator()(const T&) const {
61 }
62};
63
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000064int InitPtrToInt(const Init* ptr) {
65 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000066 return val.getValue();
67}
68
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000069const std::string& InitPtrToString(const Init* ptr) {
70 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
71 return val.getValue();
72}
73
74const ListInit& InitPtrToList(const Init* ptr) {
75 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
76 return val;
77}
78
79const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +000080 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000081 return val;
82}
83
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000084// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000085// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000086void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +000087 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000088 throw d->getOperator()->getAsString() + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000089}
90
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000091// isDagEmpty - is this DAG marked with an empty marker?
92bool isDagEmpty (const DagInit* d) {
Mikhail Glushenkov2d04ec52009-09-10 16:22:02 +000093 return d->getOperator()->getAsString() == "empty_dag_marker";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000094}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000095
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000096// EscapeVariableName - Escape commas and other symbols not allowed
97// in the C++ variable names. Makes it possible to use options named
98// like "Wa," (useful for prefix options).
99std::string EscapeVariableName(const std::string& Var) {
100 std::string ret;
101 for (unsigned i = 0; i != Var.size(); ++i) {
102 char cur_char = Var[i];
103 if (cur_char == ',') {
104 ret += "_comma_";
105 }
106 else if (cur_char == '+') {
107 ret += "_plus_";
108 }
109 else if (cur_char == '-') {
110 ret += "_dash_";
111 }
112 else {
113 ret.push_back(cur_char);
114 }
115 }
116 return ret;
117}
118
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000119/// oneOf - Does the input string contain this character?
120bool oneOf(const char* lst, char c) {
121 while (*lst) {
122 if (*lst++ == c)
123 return true;
124 }
125 return false;
126}
127
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000128template <class I, class S>
129void checkedIncrement(I& P, I E, S ErrorString) {
130 ++P;
131 if (P == E)
132 throw ErrorString;
133}
134
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000135//===----------------------------------------------------------------------===//
136/// Back-end specific code
137
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000138
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000139/// OptionType - One of six different option types. See the
140/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000141namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000142
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000143 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000144 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000145
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000146 bool IsList (OptionType t) {
147 return (t == ParameterList || t == PrefixList);
148 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000149
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000150 bool IsSwitch (OptionType t) {
151 return (t == Switch);
152 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000153
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000154 bool IsParameter (OptionType t) {
155 return (t == Parameter || t == Prefix);
156 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000157
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000158}
159
160OptionType::OptionType stringToOptionType(const std::string& T) {
161 if (T == "alias_option")
162 return OptionType::Alias;
163 else if (T == "switch_option")
164 return OptionType::Switch;
165 else if (T == "parameter_option")
166 return OptionType::Parameter;
167 else if (T == "parameter_list_option")
168 return OptionType::ParameterList;
169 else if (T == "prefix_option")
170 return OptionType::Prefix;
171 else if (T == "prefix_list_option")
172 return OptionType::PrefixList;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 else
174 throw "Unknown option type: " + T + '!';
175}
176
177namespace OptionDescriptionFlags {
178 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000179 ReallyHidden = 0x4, Extern = 0x8,
180 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000181}
182
183/// OptionDescription - Represents data contained in a single
184/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000185struct OptionDescription {
186 OptionType::OptionType Type;
187 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000188 unsigned Flags;
189 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000190 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000191 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000192
193 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000194 const std::string& n = "",
195 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000196 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000197 {}
198
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000199 /// GenTypeDeclaration - Returns the C++ variable type of this
200 /// option.
201 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000202
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000203 /// GenVariableName - Returns the variable name used in the
204 /// generated C++ code.
205 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000206
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000207 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000210 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000211
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000212 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000213
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000214 bool isMultiVal() const;
215
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000216 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000217 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000218
219 bool isRequired() const;
220 void setRequired();
221
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000222 bool isOneOrMore() const;
223 void setOneOrMore();
224
225 bool isZeroOrOne() const;
226 void setZeroOrOne();
227
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000228 bool isHidden() const;
229 void setHidden();
230
231 bool isReallyHidden() const;
232 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000233
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000234 bool isParameter() const
235 { return OptionType::IsParameter(this->Type); }
236
237 bool isSwitch() const
238 { return OptionType::IsSwitch(this->Type); }
239
240 bool isList() const
241 { return OptionType::IsList(this->Type); }
242
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000243};
244
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000245void OptionDescription::Merge (const OptionDescription& other)
246{
247 if (other.Type != Type)
248 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000250 if (Help == other.Help || Help == DefaultHelpString)
251 Help = other.Help;
252 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000253 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000254 " defined for option " + Name + "\n";
255 }
256
257 Flags |= other.Flags;
258}
259
260bool OptionDescription::isAlias() const {
261 return Type == OptionType::Alias;
262}
263
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000264bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000265 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000266}
267
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000268bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000269 return Flags & OptionDescriptionFlags::Extern;
270}
271void OptionDescription::setExtern() {
272 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000273}
274
275bool OptionDescription::isRequired() const {
276 return Flags & OptionDescriptionFlags::Required;
277}
278void OptionDescription::setRequired() {
279 Flags |= OptionDescriptionFlags::Required;
280}
281
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000282bool OptionDescription::isOneOrMore() const {
283 return Flags & OptionDescriptionFlags::OneOrMore;
284}
285void OptionDescription::setOneOrMore() {
286 Flags |= OptionDescriptionFlags::OneOrMore;
287}
288
289bool OptionDescription::isZeroOrOne() const {
290 return Flags & OptionDescriptionFlags::ZeroOrOne;
291}
292void OptionDescription::setZeroOrOne() {
293 Flags |= OptionDescriptionFlags::ZeroOrOne;
294}
295
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000296bool OptionDescription::isHidden() const {
297 return Flags & OptionDescriptionFlags::Hidden;
298}
299void OptionDescription::setHidden() {
300 Flags |= OptionDescriptionFlags::Hidden;
301}
302
303bool OptionDescription::isReallyHidden() const {
304 return Flags & OptionDescriptionFlags::ReallyHidden;
305}
306void OptionDescription::setReallyHidden() {
307 Flags |= OptionDescriptionFlags::ReallyHidden;
308}
309
310const char* OptionDescription::GenTypeDeclaration() const {
311 switch (Type) {
312 case OptionType::Alias:
313 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000314 case OptionType::PrefixList:
315 case OptionType::ParameterList:
316 return "cl::list<std::string>";
317 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000319 case OptionType::Parameter:
320 case OptionType::Prefix:
321 default:
322 return "cl::opt<std::string>";
323 }
324}
325
326std::string OptionDescription::GenVariableName() const {
327 const std::string& EscapedName = EscapeVariableName(Name);
328 switch (Type) {
329 case OptionType::Alias:
330 return "AutoGeneratedAlias_" + EscapedName;
331 case OptionType::PrefixList:
332 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000333 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000334 case OptionType::Switch:
335 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000336 case OptionType::Prefix:
337 case OptionType::Parameter:
338 default:
339 return "AutoGeneratedParameter_" + EscapedName;
340 }
341}
342
343/// OptionDescriptions - An OptionDescription array plus some helper
344/// functions.
345class OptionDescriptions {
346 typedef StringMap<OptionDescription> container_type;
347
348 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000349 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000350
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000351public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000352 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000353 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000354
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000355 /// insertDescription - Insert new OptionDescription into
356 /// OptionDescriptions list
357 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000358
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000359 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000360 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000361 const_iterator begin() const { return Descriptions.begin(); }
362 const_iterator end() const { return Descriptions.end(); }
363};
364
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000365const OptionDescription&
366OptionDescriptions::FindOption(const std::string& OptName) const
367{
368 const_iterator I = Descriptions.find(OptName);
369 if (I != Descriptions.end())
370 return I->second;
371 else
372 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000373}
374
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000375void OptionDescriptions::InsertDescription (const OptionDescription& o)
376{
377 container_type::iterator I = Descriptions.find(o.Name);
378 if (I != Descriptions.end()) {
379 OptionDescription& D = I->second;
380 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000381 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000382 else {
383 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000384 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000385}
386
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387/// HandlerTable - A base class for function objects implemented as
388/// 'tables of handlers'.
389template <class T>
390class HandlerTable {
391protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000392 // Implementation details.
393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 /// Handler -
395 typedef void (T::* Handler) (const DagInit*);
396 /// HandlerMap - A map from property names to property handlers
397 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000398
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000399 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000400 static bool staticMembersInitialized_;
401
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000402 T* childPtr;
403public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000404
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000405 HandlerTable(T* cp) : childPtr(cp)
406 {}
407
408 /// operator() - Just forwards to the corresponding property
409 /// handler.
410 void operator() (Init* i) {
411 const DagInit& property = InitPtrToDag(i);
412 const std::string& property_name = property.getOperator()->getAsString();
413 typename HandlerMap::iterator method = Handlers_.find(property_name);
414
415 if (method != Handlers_.end()) {
416 Handler h = method->second;
417 (childPtr->*h)(&property);
418 }
419 else {
420 throw "No handler found for property " + property_name + "!";
421 }
422 }
423
424 void AddHandler(const char* Property, Handler Handl) {
425 Handlers_[Property] = Handl;
426 }
427};
428
429template <class T> typename HandlerTable<T>::HandlerMap
430HandlerTable<T>::Handlers_;
431template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
432
433
434/// CollectOptionProperties - Function object for iterating over an
435/// option property list.
436class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
437private:
438
439 /// optDescs_ - OptionDescriptions table. This is where the
440 /// information is stored.
441 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000442
443public:
444
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000445 explicit CollectOptionProperties(OptionDescription& OD)
446 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000447 {
448 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000449 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000450 AddHandler("help", &CollectOptionProperties::onHelp);
451 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000452 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000453 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
454 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000455 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
456 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000457 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000458
459 staticMembersInitialized_ = true;
460 }
461 }
462
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000463private:
464
465 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000466 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000467
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000468 void onExtern (const DagInit* d) {
469 checkNumberOfArguments(d, 0);
470 optDesc_.setExtern();
471 }
472
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000473 void onHelp (const DagInit* d) {
474 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000475 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000476 }
477
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000478 void onHidden (const DagInit* d) {
479 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000480 optDesc_.setHidden();
481 }
482
483 void onReallyHidden (const DagInit* d) {
484 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000485 optDesc_.setReallyHidden();
486 }
487
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000488 void onRequired (const DagInit* d) {
489 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000490 if (optDesc_.isOneOrMore())
491 throw std::string("An option can't have both (required) "
492 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000493 optDesc_.setRequired();
494 }
495
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000496 void onInit (const DagInit* d) {
497 checkNumberOfArguments(d, 1);
498 Init* i = d->getArg(0);
499 const std::string& str = i->getAsString();
500
501 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
502 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
503
504 if (!correct)
505 throw std::string("Incorrect usage of the 'init' option property!");
506
507 optDesc_.InitVal = i;
508 }
509
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000510 void onOneOrMore (const DagInit* d) {
511 checkNumberOfArguments(d, 0);
512 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
513 throw std::string("Only one of (required), (zero_or_one) or "
514 "(one_or_more) properties is allowed!");
515 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000516 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000517 "on a non-list option will have no effect.\n";
518 optDesc_.setOneOrMore();
519 }
520
521 void onZeroOrOne (const DagInit* d) {
522 checkNumberOfArguments(d, 0);
523 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
524 throw std::string("Only one of (required), (zero_or_one) or "
525 "(one_or_more) properties is allowed!");
526 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000527 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000528 "on a non-list option will have no effect.\n";
529 optDesc_.setZeroOrOne();
530 }
531
532 void onMultiVal (const DagInit* d) {
533 checkNumberOfArguments(d, 1);
534 int val = InitPtrToInt(d->getArg(0));
535 if (val < 2)
536 throw std::string("Error in the 'multi_val' property: "
537 "the value must be greater than 1!");
538 if (!OptionType::IsList(optDesc_.Type))
539 throw std::string("The multi_val property is valid only "
540 "on list options!");
541 optDesc_.MultiVal = val;
542 }
543
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000544};
545
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000546/// AddOption - A function object that is applied to every option
547/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000548class AddOption {
549private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000550 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000551
552public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000553 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000554 {}
555
556 void operator()(const Init* i) {
557 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000558 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000559
560 const OptionType::OptionType Type =
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000561 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000562 const std::string& Name = InitPtrToString(d.getArg(0));
563
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000564 OptionDescription OD(Type, Name);
565
566 if (!OD.isExtern())
567 checkNumberOfArguments(&d, 2);
568
569 if (OD.isAlias()) {
570 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000571 OD.Help = InitPtrToString(d.getArg(1));
572 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000573 else if (!OD.isExtern()) {
574 processOptionProperties(&d, OD);
575 }
576 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000577 }
578
579private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000580 /// processOptionProperties - Go through the list of option
581 /// properties and call a corresponding handler for each.
582 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
583 checkNumberOfArguments(d, 2);
584 DagInit::const_arg_iterator B = d->arg_begin();
585 // Skip the first argument: it's always the option name.
586 ++B;
587 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000588 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000589
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000590};
591
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000592/// CollectOptionDescriptions - Collects option properties from all
593/// OptionLists.
594void CollectOptionDescriptions (RecordVector::const_iterator B,
595 RecordVector::const_iterator E,
596 OptionDescriptions& OptDescs)
597{
598 // For every OptionList:
599 for (; B!=E; ++B) {
600 RecordVector::value_type T = *B;
601 // Throws an exception if the value does not exist.
602 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000603
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000604 // For every option description in this list:
605 // collect the information and
606 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
607 }
608}
609
610// Tool information record
611
612namespace ToolFlags {
613 enum ToolFlags { Join = 0x1, Sink = 0x2 };
614}
615
616struct ToolDescription : public RefCountedBase<ToolDescription> {
617 std::string Name;
618 Init* CmdLine;
619 Init* Actions;
620 StrVector InLanguage;
621 std::string OutLanguage;
622 std::string OutputSuffix;
623 unsigned Flags;
624
625 // Various boolean properties
626 void setSink() { Flags |= ToolFlags::Sink; }
627 bool isSink() const { return Flags & ToolFlags::Sink; }
628 void setJoin() { Flags |= ToolFlags::Join; }
629 bool isJoin() const { return Flags & ToolFlags::Join; }
630
631 // Default ctor here is needed because StringMap can only store
632 // DefaultConstructible objects
633 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
634 ToolDescription (const std::string& n)
635 : Name(n), CmdLine(0), Actions(0), Flags(0)
636 {}
637};
638
639/// ToolDescriptions - A list of Tool information records.
640typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
641
642
643/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000644/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000645class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000646private:
647
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000648 /// toolDesc_ - Properties of the current Tool. This is where the
649 /// information is stored.
650 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000651
652public:
653
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000654 explicit CollectToolProperties (ToolDescription& d)
655 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000656 {
657 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000658
659 AddHandler("actions", &CollectToolProperties::onActions);
660 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
661 AddHandler("in_language", &CollectToolProperties::onInLanguage);
662 AddHandler("join", &CollectToolProperties::onJoin);
663 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
664 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
665 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000666
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000667 staticMembersInitialized_ = true;
668 }
669 }
670
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000671private:
672
673 /// Property handlers --
674 /// Functions that extract information about tool properties from
675 /// DAG representation.
676
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000677 void onActions (const DagInit* d) {
678 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000679 Init* Case = d->getArg(0);
680 if (typeid(*Case) != typeid(DagInit) ||
681 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
682 throw
683 std::string("The argument to (actions) should be a 'case' construct!");
684 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000685 }
686
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000687 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000688 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000689 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000690 }
691
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000692 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000693 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000694 Init* arg = d->getArg(0);
695
696 // Find out the argument's type.
697 if (typeid(*arg) == typeid(StringInit)) {
698 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000699 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000700 }
701 else {
702 // It's a list.
703 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000704 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000705
706 // Copy strings to the output vector.
707 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
708 B != E; ++B) {
709 out.push_back(InitPtrToString(*B));
710 }
711
712 // Remove duplicates.
713 std::sort(out.begin(), out.end());
714 StrVector::iterator newE = std::unique(out.begin(), out.end());
715 out.erase(newE, out.end());
716 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000717 }
718
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000719 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000720 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000721 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000722 }
723
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000724 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000725 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000726 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000727 }
728
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000729 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000730 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000731 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000732 }
733
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000734 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000735 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000736 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000737 }
738
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000739};
740
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000741/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000742/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000743/// CollectToolProperties function object).
744void CollectToolDescriptions (RecordVector::const_iterator B,
745 RecordVector::const_iterator E,
746 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000747{
748 // Iterate over a properties list of every Tool definition
749 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000750 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000751 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000752 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000753
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000754 IntrusiveRefCntPtr<ToolDescription>
755 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000756
757 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000758 CollectToolProperties(*ToolDesc));
759 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000760 }
761}
762
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000763/// FillInEdgeVector - Merge all compilation graph definitions into
764/// one single edge list.
765void FillInEdgeVector(RecordVector::const_iterator B,
766 RecordVector::const_iterator E, RecordVector& Out) {
767 for (; B != E; ++B) {
768 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000769
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000770 for (unsigned i = 0; i < edges->size(); ++i)
771 Out.push_back(edges->getElementAsRecord(i));
772 }
773}
774
775/// CalculatePriority - Calculate the priority of this plugin.
776int CalculatePriority(RecordVector::const_iterator B,
777 RecordVector::const_iterator E) {
778 int total = 0;
Mikhail Glushenkov35fde152008-11-17 17:30:25 +0000779 for (; B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000780 total += static_cast<int>((*B)->getValueAsInt("priority"));
781 }
782 return total;
783}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000784
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000785/// NotInGraph - Helper function object for FilterNotInGraph.
786struct NotInGraph {
787private:
788 const llvm::StringSet<>& ToolsInGraph_;
789
790public:
791 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
792 : ToolsInGraph_(ToolsInGraph)
793 {}
794
795 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
796 return (ToolsInGraph_.count(x->Name) == 0);
797 }
798};
799
800/// FilterNotInGraph - Filter out from ToolDescs all Tools not
801/// mentioned in the compilation graph definition.
802void FilterNotInGraph (const RecordVector& EdgeVector,
803 ToolDescriptions& ToolDescs) {
804
805 // List all tools mentioned in the graph.
806 llvm::StringSet<> ToolsInGraph;
807
808 for (RecordVector::const_iterator B = EdgeVector.begin(),
809 E = EdgeVector.end(); B != E; ++B) {
810
811 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000812 const std::string& NodeA = Edge->getValueAsString("a");
813 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000814
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000815 if (NodeA != "root")
816 ToolsInGraph.insert(NodeA);
817 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000818 }
819
820 // Filter ToolPropertiesList.
821 ToolDescriptions::iterator new_end =
822 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
823 NotInGraph(ToolsInGraph));
824 ToolDescs.erase(new_end, ToolDescs.end());
825}
826
827/// FillInToolToLang - Fills in two tables that map tool names to
828/// (input, output) languages. Helper function used by TypecheckGraph().
829void FillInToolToLang (const ToolDescriptions& ToolDescs,
830 StringMap<StringSet<> >& ToolToInLang,
831 StringMap<std::string>& ToolToOutLang) {
832 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
833 E = ToolDescs.end(); B != E; ++B) {
834 const ToolDescription& D = *(*B);
835 for (StrVector::const_iterator B = D.InLanguage.begin(),
836 E = D.InLanguage.end(); B != E; ++B)
837 ToolToInLang[D.Name].insert(*B);
838 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000839 }
840}
841
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000842/// TypecheckGraph - Check that names for output and input languages
843/// on all edges do match. This doesn't do much when the information
844/// about the whole graph is not available (i.e. when compiling most
845/// plugins).
846void TypecheckGraph (const RecordVector& EdgeVector,
847 const ToolDescriptions& ToolDescs) {
848 StringMap<StringSet<> > ToolToInLang;
849 StringMap<std::string> ToolToOutLang;
850
851 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
852 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
853 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
854
855 for (RecordVector::const_iterator B = EdgeVector.begin(),
856 E = EdgeVector.end(); B != E; ++B) {
857 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000858 const std::string& NodeA = Edge->getValueAsString("a");
859 const std::string& NodeB = Edge->getValueAsString("b");
860 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
861 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000862
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000863 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000864 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000865 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000866 + ": output->input language mismatch";
867 }
868
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000869 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000870 throw std::string("Edges back to the root are not allowed!");
871 }
872}
873
874/// WalkCase - Walks the 'case' expression DAG and invokes
875/// TestCallback on every test, and StatementCallback on every
876/// statement. Handles 'case' nesting, but not the 'and' and 'or'
877/// combinators.
878// TODO: Re-implement EmitCaseConstructHandler on top of this function?
879template <typename F1, typename F2>
880void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
881 const DagInit& d = InitPtrToDag(Case);
882 bool even = false;
883 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
884 B != E; ++B) {
885 Init* arg = *B;
886 if (even && dynamic_cast<DagInit*>(arg)
887 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
888 WalkCase(arg, TestCallback, StatementCallback);
889 else if (!even)
890 TestCallback(arg);
891 else
892 StatementCallback(arg);
893 even = !even;
894 }
895}
896
897/// ExtractOptionNames - A helper function object used by
898/// CheckForSuperfluousOptions() to walk the 'case' DAG.
899class ExtractOptionNames {
900 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000901
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000902 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000903 const DagInit& Stmt = InitPtrToDag(Statement);
904 const std::string& ActionName = Stmt.getOperator()->getAsString();
905 if (ActionName == "forward" || ActionName == "forward_as" ||
906 ActionName == "unpack_values" || ActionName == "switch_on" ||
907 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000908 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000909 checkNumberOfArguments(&Stmt, 1);
910 const std::string& Name = InitPtrToString(Stmt.getArg(0));
911 OptionNames_.insert(Name);
912 }
913 else if (ActionName == "and" || ActionName == "or") {
914 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000915 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000916 }
917 }
918 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000919
920public:
921 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
922 {}
923
924 void operator()(const Init* Statement) {
925 if (typeid(*Statement) == typeid(ListInit)) {
926 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
927 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
928 B != E; ++B)
929 this->processDag(*B);
930 }
931 else {
932 this->processDag(Statement);
933 }
934 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000935};
936
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000937/// CheckForSuperfluousOptions - Check that there are no side
938/// effect-free options (specified only in the OptionList). Otherwise,
939/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000940void CheckForSuperfluousOptions (const RecordVector& Edges,
941 const ToolDescriptions& ToolDescs,
942 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000943 llvm::StringSet<> nonSuperfluousOptions;
944
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000945 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000946 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000947 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
948 E = ToolDescs.end(); B != E; ++B) {
949 const ToolDescription& TD = *(*B);
950 ExtractOptionNames Callback(nonSuperfluousOptions);
951 if (TD.Actions)
952 WalkCase(TD.Actions, Callback, Callback);
953 }
954
955 // Add all options mentioned in the 'case' clauses of the
956 // OptionalEdges of the compilation graph to the set of
957 // non-superfluous options.
958 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
959 B != E; ++B) {
960 const Record* Edge = *B;
961 DagInit* Weight = Edge->getValueAsDag("weight");
962
963 if (!isDagEmpty(Weight))
964 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000965 }
966
967 // Check that all options in OptDescs belong to the set of
968 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000969 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000970 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000971 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000972 if (!nonSuperfluousOptions.count(Val.Name)
973 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +0000974 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000975 "Probable cause: this option is specified only in the OptionList.\n";
976 }
977}
978
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000979/// EmitCaseTest1Arg - Helper function used by
980/// EmitCaseConstructHandler.
981bool EmitCaseTest1Arg(const std::string& TestName,
982 const DagInit& d,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000983 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000984 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000985 checkNumberOfArguments(&d, 1);
986 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000987
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000988 if (TestName == "switch_on") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000989 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000990 if (!OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000991 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000992 O << OptDesc.GenVariableName();
993 return true;
994 } else if (TestName == "input_languages_contain") {
995 O << "InLangs.count(\"" << OptName << "\") != 0";
996 return true;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +0000997 } else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000998 // This works only for single-argument Tool::GenerateAction. Join
999 // tools can process several files in different languages simultaneously.
1000
1001 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001002 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001003 return true;
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001004 } else if (TestName == "not_empty" || TestName == "empty") {
1005 const char* Test = (TestName == "empty") ? "" : "!";
1006
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001007 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001008 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001009 return true;
1010 }
1011 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001012 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001013 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001014 throw OptName
1015 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001016 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001017 return true;
1018 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001019 }
1020
1021 return false;
1022}
1023
1024/// EmitCaseTest2Args - Helper function used by
1025/// EmitCaseConstructHandler.
1026bool EmitCaseTest2Args(const std::string& TestName,
1027 const DagInit& d,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001028 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001029 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001030 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001031 checkNumberOfArguments(&d, 2);
1032 const std::string& OptName = InitPtrToString(d.getArg(0));
1033 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001034 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001035
1036 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001037 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001038 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001039 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1040 return true;
1041 }
1042 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001043 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001044 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001045 const std::string& VarName = OptDesc.GenVariableName();
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001046 O << "std::find(" << VarName << ".begin(),\n";
1047 O.indent(IndentLevel + Indent1)
1048 << VarName << ".end(), \""
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001049 << OptArg << "\") != " << VarName << ".end()";
1050 return true;
1051 }
1052
1053 return false;
1054}
1055
1056// Forward declaration.
1057// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001058void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001059 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001060 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001061
1062/// EmitLogicalOperationTest - Helper function used by
1063/// EmitCaseConstructHandler.
1064void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001065 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001066 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001067 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001068 O << '(';
1069 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001070 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001071 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001072 if (j != NumArgs - 1) {
1073 O << ")\n";
1074 O.indent(IndentLevel + Indent1) << ' ' << LogicOp << " (";
1075 }
1076 else {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001077 O << ')';
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001078 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001079 }
1080}
1081
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001082void EmitLogicalNot(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001083 const OptionDescriptions& OptDescs, raw_ostream& O)
1084{
1085 checkNumberOfArguments(&d, 1);
1086 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1087 O << "! (";
1088 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1089 O << ")";
1090}
1091
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001092/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001093void EmitCaseTest(const DagInit& d, unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001094 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001095 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001096 const std::string& TestName = d.getOperator()->getAsString();
1097
1098 if (TestName == "and")
1099 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1100 else if (TestName == "or")
1101 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001102 else if (TestName == "not")
1103 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001104 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1105 return;
1106 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1107 return;
1108 else
1109 throw TestName + ": unknown edge property!";
1110}
1111
1112// Emit code that handles the 'case' construct.
1113// Takes a function object that should emit code for every case clause.
1114// Callback's type is
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001115// void F(Init* Statement, unsigned IndentLevel, raw_ostream& O).
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001116template <typename F>
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001117void EmitCaseConstructHandler(const Init* Dag, unsigned IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001118 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001119 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001120 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001121 const DagInit* d = &InitPtrToDag(Dag);
1122 if (d->getOperator()->getAsString() != "case")
1123 throw std::string("EmitCaseConstructHandler should be invoked"
1124 " only on 'case' expressions!");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001125
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001126 unsigned numArgs = d->getNumArgs();
1127 if (d->getNumArgs() < 2)
1128 throw "There should be at least one clause in the 'case' expression:\n"
1129 + d->getAsString();
1130
1131 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001132 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001133
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001134 // Emit the test.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001135 if (Test.getOperator()->getAsString() == "default") {
1136 if (i+2 != numArgs)
1137 throw std::string("The 'default' clause should be the last in the"
1138 "'case' construct!");
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001139 O.indent(IndentLevel) << "else {\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001140 }
1141 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001142 O.indent(IndentLevel) << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001143 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1144 O << ") {\n";
1145 }
1146
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001147 // Emit the corresponding statement.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001148 ++i;
1149 if (i == numArgs)
1150 throw "Case construct handler: no corresponding action "
1151 "found for the test " + Test.getAsString() + '!';
1152
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001153 Init* arg = d->getArg(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001154 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1155 if (nd && (nd->getOperator()->getAsString() == "case")) {
1156 // Handle the nested 'case'.
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001157 EmitCaseConstructHandler(nd, (IndentLevel + Indent1),
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001158 Callback, EmitElseIf, OptDescs, O);
1159 }
1160 else {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001161 Callback(arg, (IndentLevel + Indent1), O);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001162 }
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001163 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001164 }
1165}
1166
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001167/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1168/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1169/// Helper function used by EmitCmdLineVecFill and.
1170void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1171 const char* Delimiters = " \t\n\v\f\r";
1172 enum TokenizerState
1173 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1174 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001175
1176 if (CmdLine.empty())
1177 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001178 Out.push_back("");
1179
1180 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1181 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001182
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001183 for (; B != E; ++B) {
1184 char cur_ch = CmdLine[B];
1185
1186 switch (cur_st) {
1187 case Normal:
1188 if (cur_ch == '$') {
1189 cur_st = SpecialCommand;
1190 break;
1191 }
1192 if (oneOf(Delimiters, cur_ch)) {
1193 // Skip whitespace
1194 B = CmdLine.find_first_not_of(Delimiters, B);
1195 if (B == std::string::npos) {
1196 B = E-1;
1197 continue;
1198 }
1199 --B;
1200 Out.push_back("");
1201 continue;
1202 }
1203 break;
1204
1205
1206 case SpecialCommand:
1207 if (oneOf(Delimiters, cur_ch)) {
1208 cur_st = Normal;
1209 Out.push_back("");
1210 continue;
1211 }
1212 if (cur_ch == '(') {
1213 Out.push_back("");
1214 cur_st = InsideSpecialCommand;
1215 continue;
1216 }
1217 break;
1218
1219 case InsideSpecialCommand:
1220 if (oneOf(Delimiters, cur_ch)) {
1221 continue;
1222 }
1223 if (cur_ch == '\'') {
1224 cur_st = InsideQuotationMarks;
1225 Out.push_back("");
1226 continue;
1227 }
1228 if (cur_ch == ')') {
1229 cur_st = Normal;
1230 Out.push_back("");
1231 }
1232 if (cur_ch == ',') {
1233 continue;
1234 }
1235
1236 break;
1237
1238 case InsideQuotationMarks:
1239 if (cur_ch == '\'') {
1240 cur_st = InsideSpecialCommand;
1241 continue;
1242 }
1243 break;
1244 }
1245
1246 Out.back().push_back(cur_ch);
1247 }
1248}
1249
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001250/// SubstituteSpecialCommands - Perform string substitution for $CALL
1251/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001252StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001253(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001254{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001255
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001256 const std::string& cmd = *Pos;
1257
1258 if (cmd == "$CALL") {
1259 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1260 const std::string& CmdName = *Pos;
1261
1262 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001263 throw std::string("$CALL invocation: empty argument list!");
1264
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001265 O << "hooks::";
1266 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001267
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001268
1269 bool firstIteration = true;
1270 while (true) {
1271 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1272 const std::string& Arg = *Pos;
1273 assert(Arg.size() != 0);
1274
1275 if (Arg[0] == ')')
1276 break;
1277
1278 if (firstIteration)
1279 firstIteration = false;
1280 else
1281 O << ", ";
1282
1283 O << '"' << Arg << '"';
1284 }
1285
1286 O << ')';
1287
1288 }
1289 else if (cmd == "$ENV") {
1290 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1291 const std::string& EnvName = *Pos;
1292
1293 if (EnvName == ")")
1294 throw "$ENV invocation: empty argument list!";
1295
1296 O << "checkCString(std::getenv(\"";
1297 O << EnvName;
1298 O << "\"))";
1299
1300 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001301 }
1302 else {
1303 throw "Unknown special command: " + cmd;
1304 }
1305
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001306 const std::string& Leftover = *Pos;
1307 assert(Leftover.at(0) == ')');
1308 if (Leftover.size() != 1)
1309 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001310
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001311 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001312}
1313
1314/// EmitCmdLineVecFill - Emit code that fills in the command line
1315/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001316void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001317 bool IsJoin, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001318 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001319 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001320 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1321
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001322 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001323 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001324
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001325 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1326
1327 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001328 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001329 if (StrVec[0][0] == '$') {
1330 while (I != E && (*I)[0] != ')' )
1331 ++I;
1332
1333 // Skip the ')' symbol.
1334 ++I;
1335 }
1336 else {
1337 ++I;
1338 }
1339
1340 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001341 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001342 assert(!cmd.empty());
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001343 O.indent(IndentLevel);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001344 if (cmd.at(0) == '$') {
1345 if (cmd == "$INFILE") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001346 if (IsJoin) {
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001347 O << "for (PathVector::const_iterator B = inFiles.begin()"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001348 << ", E = inFiles.end();\n";
1349 O.indent(IndentLevel) << "B != E; ++B)\n";
1350 O.indent(IndentLevel + Indent1) << "vec.push_back(B->str());\n";
1351 }
1352 else {
Chris Lattner74382b72009-08-23 22:45:37 +00001353 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001354 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001355 }
1356 else if (cmd == "$OUTFILE") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001357 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001358 }
1359 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001360 O << "vec.push_back(";
1361 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001362 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001363 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001364 }
1365 else {
1366 O << "vec.push_back(\"" << cmd << "\");\n";
1367 }
1368 }
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001369 O.indent(IndentLevel) << "cmd = ";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001370
1371 if (StrVec[0][0] == '$')
1372 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1373 else
1374 O << '"' << StrVec[0] << '"';
1375 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001376}
1377
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001378/// EmitCmdLineVecFillCallback - A function object wrapper around
1379/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1380/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001381class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001382 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001383 const std::string& ToolName;
1384 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001385 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1386 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001387
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001388 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001389 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001390 {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001391 EmitCmdLineVecFill(Statement, ToolName, IsJoin, IndentLevel, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001392 }
1393};
1394
1395/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1396/// implement EmitActionHandler. Emits code for
1397/// handling the (forward) and (forward_as) option properties.
1398void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001399 unsigned IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001400 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001401 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001402 const std::string& Name = NewName.empty()
1403 ? ("-" + D.Name)
1404 : NewName;
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001405 unsigned IndentLevel1 = IndentLevel + Indent1;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001406
1407 switch (D.Type) {
1408 case OptionType::Switch:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001409 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001410 break;
1411 case OptionType::Parameter:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001412 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\");\n";
1413 O.indent(IndentLevel) << "vec.push_back(" << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001414 break;
1415 case OptionType::Prefix:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001416 O.indent(IndentLevel) << "vec.push_back(\"" << Name << "\" + "
1417 << D.GenVariableName() << ");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001418 break;
1419 case OptionType::PrefixList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001420 O.indent(IndentLevel)
1421 << "for (" << D.GenTypeDeclaration()
1422 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1423 O.indent(IndentLevel)
1424 << "E = " << D.GenVariableName() << ".end(); B != E;) {\n";
1425 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\" + " << "*B);\n";
1426 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001427
1428 for (int i = 1, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001429 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1430 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001431 }
1432
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001433 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001434 break;
1435 case OptionType::ParameterList:
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001436 O.indent(IndentLevel)
1437 << "for (" << D.GenTypeDeclaration() << "::iterator B = "
1438 << D.GenVariableName() << ".begin(),\n";
1439 O.indent(IndentLevel) << "E = " << D.GenVariableName()
1440 << ".end() ; B != E;) {\n";
1441 O.indent(IndentLevel1) << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001442
1443 for (int i = 0, j = D.MultiVal; i < j; ++i) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001444 O.indent(IndentLevel1) << "vec.push_back(*B);\n";
1445 O.indent(IndentLevel1) << "++B;\n";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001446 }
1447
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001448 O.indent(IndentLevel) << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001449 break;
1450 case OptionType::Alias:
1451 default:
1452 throw std::string("Aliases are not allowed in tool option descriptions!");
1453 }
1454}
1455
1456/// EmitActionHandler - Emit code that handles actions. Used by
1457/// EmitGenerateActionMethod() as an argument to
1458/// EmitCaseConstructHandler().
1459class EmitActionHandler {
1460 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001461
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001462 void processActionDag(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001463 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001464 {
1465 const DagInit& Dag = InitPtrToDag(Statement);
1466 const std::string& ActionName = Dag.getOperator()->getAsString();
1467
1468 if (ActionName == "append_cmd") {
1469 checkNumberOfArguments(&Dag, 1);
1470 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001471 StrVector Out;
1472 llvm::SplitString(Cmd, Out);
1473
1474 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1475 B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001476 O.indent(IndentLevel) << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001477 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001478 else if (ActionName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001479 O.indent(IndentLevel) << "throw std::runtime_error(\"" <<
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001480 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1481 : "Unknown error!")
1482 << "\");\n";
1483 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001484 else if (ActionName == "forward") {
1485 checkNumberOfArguments(&Dag, 1);
1486 const std::string& Name = InitPtrToString(Dag.getArg(0));
1487 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1488 IndentLevel, "", O);
1489 }
1490 else if (ActionName == "forward_as") {
1491 checkNumberOfArguments(&Dag, 2);
1492 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001493 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001494 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1495 IndentLevel, NewName, O);
1496 }
1497 else if (ActionName == "output_suffix") {
1498 checkNumberOfArguments(&Dag, 1);
1499 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001500 O.indent(IndentLevel) << "output_suffix = \"" << OutSuf << "\";\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001501 }
1502 else if (ActionName == "stop_compilation") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001503 O.indent(IndentLevel) << "stop_compilation = true;\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001504 }
1505 else if (ActionName == "unpack_values") {
1506 checkNumberOfArguments(&Dag, 1);
1507 const std::string& Name = InitPtrToString(Dag.getArg(0));
1508 const OptionDescription& D = OptDescs.FindOption(Name);
1509
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001510 if (D.isMultiVal())
1511 throw std::string("Can't use unpack_values with multi-valued options!");
1512
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001513 if (D.isList()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001514 O.indent(IndentLevel)
1515 << "for (" << D.GenTypeDeclaration()
1516 << "::iterator B = " << D.GenVariableName() << ".begin(),\n";
1517 O.indent(IndentLevel)
1518 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n";
1519 O.indent(IndentLevel + Indent1)
1520 << "llvm::SplitString(*B, vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001521 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001522 else if (D.isParameter()){
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001523 O.indent(IndentLevel) << "llvm::SplitString("
1524 << D.GenVariableName() << ", vec, \",\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001525 }
1526 else {
1527 throw "Option '" + D.Name +
1528 "': switches can't have the 'unpack_values' property!";
1529 }
1530 }
1531 else {
1532 throw "Unknown action name: " + ActionName + "!";
1533 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001534 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001535 public:
1536 EmitActionHandler(const OptionDescriptions& OD)
1537 : OptDescs(OD) {}
1538
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001539 void operator()(const Init* Statement, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001540 raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001541 {
1542 if (typeid(*Statement) == typeid(ListInit)) {
1543 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1544 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1545 B != E; ++B)
1546 this->processActionDag(*B, IndentLevel, O);
1547 }
1548 else {
1549 this->processActionDag(Statement, IndentLevel, O);
1550 }
1551 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001552};
1553
1554// EmitGenerateActionMethod - Emit one of two versions of the
1555// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001556void EmitGenerateActionMethod (const ToolDescription& D,
1557 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001558 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001559 if (IsJoin)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001560 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001561 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001562 O.indent(Indent1) << "Action GenerateAction(const sys::Path& inFile,\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001563
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001564 O.indent(Indent2) << "bool HasChildren,\n";
1565 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1566 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1567 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1568 O.indent(Indent1) << "{\n";
1569 O.indent(Indent2) << "std::string cmd;\n";
1570 O.indent(Indent2) << "std::vector<std::string> vec;\n";
1571 O.indent(Indent2) << "bool stop_compilation = !HasChildren;\n";
1572 O.indent(Indent2) << "const char* output_suffix = \""
1573 << D.OutputSuffix << "\";\n";
1574 O.indent(Indent2) << "std::string out_file;\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001575
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001576 // For every understood option, emit handling code.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001577 if (D.Actions)
1578 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1579 false, OptDescs, O);
1580
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001581 O << '\n';
1582 O.indent(Indent2)
1583 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n");
1584 O.indent(Indent3) << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001585
1586 // cmd_line is either a string or a 'case' construct.
1587 if (!D.CmdLine)
1588 throw "Tool " + D.Name + " has no cmd_line property!";
1589 else if (typeid(*D.CmdLine) == typeid(StringInit))
1590 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1591 else
1592 EmitCaseConstructHandler(D.CmdLine, Indent2,
1593 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1594 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001595
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001596 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001597 if (D.isSink()) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001598 O.indent(Indent2) << "if (!" << SinkOptionName << ".empty()) {\n";
1599 O.indent(Indent3) << "vec.insert(vec.end(), "
1600 << SinkOptionName << ".begin(), " << SinkOptionName
1601 << ".end());\n";
1602 O.indent(Indent2) << "}\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001603 }
1604
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001605 O.indent(Indent2) << "return Action(cmd, vec, stop_compilation, out_file);\n";
1606 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001607}
1608
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001609/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1610/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001611void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1612 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001613 raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001614 if (!ToolDesc.isJoin()) {
1615 O.indent(Indent1) << "Action GenerateAction(const PathVector& inFiles,\n";
1616 O.indent(Indent2) << "bool HasChildren,\n";
1617 O.indent(Indent2) << "const llvm::sys::Path& TempDir,\n";
1618 O.indent(Indent2) << "const InputLanguagesSet& InLangs,\n";
1619 O.indent(Indent2) << "const LanguageMap& LangMap) const\n";
1620 O.indent(Indent1) << "{\n";
1621 O.indent(Indent2) << "throw std::runtime_error(\"" << ToolDesc.Name
1622 << " is not a Join tool!\");\n";
1623 O.indent(Indent1) << "}\n\n";
1624 }
1625 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001626 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001627 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001628
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001629 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001630}
1631
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001632/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1633/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001634void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001635 O.indent(Indent1) << "const char** InputLanguages() const {\n";
1636 O.indent(Indent2) << "return InputLanguages_;\n";
1637 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001638
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001639 if (D.OutLanguage.empty())
1640 throw "Tool " + D.Name + " has no 'out_language' property!";
1641
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001642 O.indent(Indent1) << "const char* OutputLanguage() const {\n";
1643 O.indent(Indent2) << "return \"" << D.OutLanguage << "\";\n";
1644 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001645}
1646
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001647/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001648void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001649 O.indent(Indent1) << "const char* Name() const {\n";
1650 O.indent(Indent2) << "return \"" << D.Name << "\";\n";
1651 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001652}
1653
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001654/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1655/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001656void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001657 O.indent(Indent1) << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001658 if (D.isJoin())
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001659 O.indent(Indent2) << "return true;\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001660 else
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001661 O.indent(Indent2) << "return false;\n";
1662 O.indent(Indent1) << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001663}
1664
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001665/// EmitStaticMemberDefinitions - Emit static member definitions for a
1666/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001667void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001668 if (D.InLanguage.empty())
1669 throw "Tool " + D.Name + " has no 'in_language' property!";
1670
1671 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1672 for (StrVector::const_iterator B = D.InLanguage.begin(),
1673 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001674 O << '\"' << *B << "\", ";
1675 O << "0};\n\n";
1676}
1677
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001678/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001679void EmitToolClassDefinition (const ToolDescription& D,
1680 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001681 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001682 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001683 return;
1684
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001685 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001686 O << "class " << D.Name << " : public ";
1687 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001688 O << "JoinTool";
1689 else
1690 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001691
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001692 O << "{\nprivate:\n";
1693 O.indent(Indent1) << "static const char* InputLanguages_[];\n\n";
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001694
1695 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001696 EmitNameMethod(D, O);
1697 EmitInOutLanguageMethods(D, O);
1698 EmitIsJoinMethod(D, O);
1699 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001700
1701 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001702 O << "};\n";
1703
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001704 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001705
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001706}
1707
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001708/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001709/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001710void EmitOptionDefinitions (const OptionDescriptions& descs,
1711 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001712 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001713{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001714 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001715
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001716 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001717 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001718 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001719 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001720
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001721 if (val.Type == OptionType::Alias) {
1722 Aliases.push_back(val);
1723 continue;
1724 }
1725
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001726 if (val.isExtern())
1727 O << "extern ";
1728
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001729 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001730 << val.GenVariableName();
1731
1732 if (val.isExtern()) {
1733 O << ";\n";
1734 continue;
1735 }
1736
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001737 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001738
1739 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1740 O << ", cl::Prefix";
1741
1742 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001743 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001744 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001745 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001746 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001747 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001748 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001749 O << ", cl::OneOrMore";
1750 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001751 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001752 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001753 }
1754
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001755 if (val.isReallyHidden()) {
1756 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001757 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001758 else if (val.isHidden()) {
1759 O << ", cl::Hidden";
1760 }
1761
1762 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001763 O << ", cl::multi_val(" << val.MultiVal << ')';
1764
1765 if (val.InitVal) {
1766 const std::string& str = val.InitVal->getAsString();
1767 O << ", cl::init(" << str << ')';
1768 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001769
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001770 if (!val.Help.empty())
1771 O << ", cl::desc(\"" << val.Help << "\")";
1772
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001773 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001774 }
1775
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001776 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001777 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001778 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001779 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001780
1781 O << val.GenTypeDeclaration() << ' '
1782 << val.GenVariableName()
1783 << "(\"" << val.Name << '\"';
1784
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001785 const OptionDescription& D = descs.FindOption(val.Help);
1786 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001787
1788 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1789 }
1790
1791 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001792 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00001793 O << (HasExterns ? "extern cl" : "cl")
1794 << "::list<std::string> " << SinkOptionName
1795 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001796
1797 O << '\n';
1798}
1799
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001800/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001801void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001802{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001803 // Generate code
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001804 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001805
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001806 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001807 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001808
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001809 // It is allowed for a plugin to have no language map.
1810 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001811
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001812 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1813 if (!LangsToSuffixesList)
1814 throw std::string("Error in the language map definition!");
1815
1816 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001817 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001818
1819 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1820 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1821
1822 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001823 O.indent(Indent1) << "langMap[\""
1824 << InitPtrToString(Suffixes->getElement(i))
1825 << "\"] = \"" << Lang << "\";\n";
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001826 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001827 }
1828
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001829 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001830}
1831
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001832/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1833/// by EmitEdgeClass().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001834void IncDecWeight (const Init* i, unsigned IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001835 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001836 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001837 const std::string& OpName = d.getOperator()->getAsString();
1838
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001839 if (OpName == "inc_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001840 O.indent(IndentLevel) << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001841 }
1842 else if (OpName == "dec_weight") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001843 O.indent(IndentLevel) << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001844 }
1845 else if (OpName == "error") {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001846 O.indent(IndentLevel)
1847 << "throw std::runtime_error(\"" <<
1848 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1849 : "Unknown error!")
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001850 << "\");\n";
1851 return;
1852 }
1853
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001854 else
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001855 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00001856 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001857
1858 if (d.getNumArgs() > 0)
1859 O << InitPtrToInt(d.getArg(0)) << ";\n";
1860 else
1861 O << "2;\n";
1862
Mikhail Glushenkov29063552008-05-06 18:18:20 +00001863}
1864
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001865/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001866void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001867 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001868 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001869
1870 // Class constructor.
1871 O << "class Edge" << N << ": public Edge {\n"
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001872 << "public:\n";
1873 O.indent(Indent1) << "Edge" << N << "() : Edge(\"" << Target
1874 << "\") {}\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001875
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001876 // Function Weight().
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001877 O.indent(Indent1)
1878 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n";
1879 O.indent(Indent2) << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001880
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001881 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001882 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001883
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001884 O.indent(Indent2) << "return ret;\n";
1885 O.indent(Indent1) << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001886}
1887
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001888/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001889void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001890 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001891 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001892 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001893 for (RecordVector::const_iterator B = EdgeVector.begin(),
1894 E = EdgeVector.end(); B != E; ++B) {
1895 const Record* Edge = *B;
1896 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001897 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001898
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001899 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001900 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001901 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001902 }
1903}
1904
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001905/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1906/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001907void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001908 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001909 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001910{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001911 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001912
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001913 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1914 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001915 O.indent(Indent1) << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001916
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001917 O << '\n';
1918
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001919 // Insert edges.
1920
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001921 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001922 for (RecordVector::const_iterator B = EdgeVector.begin(),
1923 E = EdgeVector.end(); B != E; ++B) {
1924 const Record* Edge = *B;
1925 const std::string& NodeA = Edge->getValueAsString("a");
1926 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001927 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001928
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00001929 O.indent(Indent1) << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001930
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001931 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001932 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001933 else
1934 O << "new Edge" << i << "()";
1935
1936 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001937 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001938 }
1939
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001940 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001941}
1942
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001943/// ExtractHookNames - Extract the hook names from all instances of
1944/// $CALL(HookName) in the provided command line string. Helper
1945/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001946class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001947 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001948public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001949 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
1950 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001951
1952 void operator()(const Init* CmdLine) {
1953 StrVector cmds;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001954 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001955 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1956 B != E; ++B) {
1957 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001958
1959 if (cmd == "$CALL") {
1960 unsigned NumArgs = 0;
1961 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
1962 const std::string& HookName = *B;
1963
1964
1965 if (HookName.at(0) == ')')
1966 throw "$CALL invoked with no arguments!";
1967
1968 while (++B != E && B->at(0) != ')') {
1969 ++NumArgs;
1970 }
1971
1972 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
1973
1974 if (H != HookNames_.end() && H->second != NumArgs)
1975 throw "Overloading of hooks is not allowed. Overloaded hook: "
1976 + HookName;
1977 else
1978 HookNames_[HookName] = NumArgs;
1979
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001980 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001981 }
1982 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001983};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001984
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001985/// FillInHookNames - Actually extract the hook names from all command
1986/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001987void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001988 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001989{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001990 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001991 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1992 E = ToolDescs.end(); B != E; ++B) {
1993 const ToolDescription& D = *(*B);
1994 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001995 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001996 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001997 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001998 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001999 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002000 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002001 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002002 }
2003}
2004
2005/// EmitHookDeclarations - Parse CmdLine fields of all the tool
2006/// property records and emit hook function declaration for each
2007/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00002008void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002009 llvm::StringMap<unsigned> HookNames;
2010
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002011 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002012 if (HookNames.empty())
2013 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002014
2015 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002016 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
2017 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002018 O.indent(Indent1) << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002019
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002020 for (unsigned i = 0, j = B->second; i < j; ++i) {
2021 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2022 }
2023
2024 O <<");\n";
2025 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002026 O << "}\n\n";
2027}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002028
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002029/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002030void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkovf8349ac2009-09-21 15:53:44 +00002031 O << "struct Plugin : public llvmc::BasePlugin {\n\n";
2032 O.indent(Indent1) << "int Priority() const { return "
2033 << Priority << "; }\n\n";
2034 O.indent(Indent1) << "void PopulateLanguageMap(LanguageMap& langMap) const\n";
2035 O.indent(Indent1) << "{ PopulateLanguageMapLocal(langMap); }\n\n";
2036 O.indent(Indent1)
2037 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n";
2038 O.indent(Indent1) << "{ PopulateCompilationGraphLocal(graph); }\n"
2039 << "};\n\n"
2040 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002041}
2042
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002043/// EmitIncludes - Emit necessary #include directives and some
2044/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002045void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002046 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002047 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002048 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2049 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002050
2051 << "#include \"llvm/ADT/StringExtras.h\"\n"
2052 << "#include \"llvm/Support/CommandLine.h\"\n\n"
2053
2054 << "#include <cstdlib>\n"
2055 << "#include <stdexcept>\n\n"
2056
2057 << "using namespace llvm;\n"
2058 << "using namespace llvmc;\n\n"
2059
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002060 << "extern cl::opt<std::string> OutputFilename;\n\n"
2061
2062 << "inline const char* checkCString(const char* s)\n"
2063 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002064}
2065
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002066
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002067/// PluginData - Holds all information about a plugin.
2068struct PluginData {
2069 OptionDescriptions OptDescs;
2070 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002071 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002072 ToolDescriptions ToolDescs;
2073 RecordVector Edges;
2074 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002075};
2076
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002077/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002078/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002079bool HasSink(const ToolDescriptions& ToolDescs) {
2080 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2081 E = ToolDescs.end(); B != E; ++B)
2082 if ((*B)->isSink())
2083 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002084
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002085 return false;
2086}
2087
2088/// HasExterns - Go through the list of option descriptions and check
2089/// if there are any external options.
2090bool HasExterns(const OptionDescriptions& OptDescs) {
2091 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2092 E = OptDescs.end(); B != E; ++B)
2093 if (B->second.isExtern())
2094 return true;
2095
2096 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002097}
2098
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002099/// CollectPluginData - Collect tool and option properties,
2100/// compilation graph edges and plugin priority from the parse tree.
2101void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2102 // Collect option properties.
2103 const RecordVector& OptionLists =
2104 Records.getAllDerivedDefinitions("OptionList");
2105 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2106 Data.OptDescs);
2107
2108 // Collect tool properties.
2109 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2110 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2111 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002112 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002113
2114 // Collect compilation graph edges.
2115 const RecordVector& CompilationGraphs =
2116 Records.getAllDerivedDefinitions("CompilationGraph");
2117 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2118 Data.Edges);
2119
2120 // Calculate the priority of this plugin.
2121 const RecordVector& Priorities =
2122 Records.getAllDerivedDefinitions("PluginPriority");
2123 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002124}
2125
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002126/// CheckPluginData - Perform some sanity checks on the collected data.
2127void CheckPluginData(PluginData& Data) {
2128 // Filter out all tools not mentioned in the compilation graph.
2129 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002130
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002131 // Typecheck the compilation graph.
2132 TypecheckGraph(Data.Edges, Data.ToolDescs);
2133
2134 // Check that there are no options without side effects (specified
2135 // only in the OptionList).
2136 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2137
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002138}
2139
Daniel Dunbar1a551802009-07-03 00:10:29 +00002140void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002141 // Emit file header.
2142 EmitIncludes(O);
2143
2144 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002145 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002146
2147 // Emit hook declarations.
2148 EmitHookDeclarations(Data.ToolDescs, O);
2149
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002150 O << "namespace {\n\n";
2151
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002152 // Emit PopulateLanguageMap() function
2153 // (a language map maps from file extensions to language names).
2154 EmitPopulateLanguageMap(Records, O);
2155
2156 // Emit Tool classes.
2157 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2158 E = Data.ToolDescs.end(); B!=E; ++B)
2159 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2160
2161 // Emit Edge# classes.
2162 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2163
2164 // Emit PopulateCompilationGraph() function.
2165 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2166
2167 // Emit code for plugin registration.
2168 EmitRegisterPlugin(Data.Priority, O);
2169
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002170 O << "} // End anonymous namespace.\n\n";
2171
2172 // Force linkage magic.
2173 O << "namespace llvmc {\n";
2174 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2175 O << "}\n";
2176
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002177 // EOF
2178}
2179
2180
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002181// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002182}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002183
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002184/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002185void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002186 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002187 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002188
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002189 CollectPluginData(Records, Data);
2190 CheckPluginData(Data);
2191
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002192 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002193 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002194
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002195 } catch (std::exception& Error) {
2196 throw Error.what() + std::string(" - usually this means a syntax error.");
2197 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002198}