blob: 9fcfd0576505c4e0226affd974d569359cf781b7 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include "llvm/Support/Streams.h"
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000023
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000024#include <algorithm>
25#include <cassert>
26#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000027#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000028#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000029#include <typeinfo>
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000030
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000031using namespace llvm;
32
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000033namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000034
35//===----------------------------------------------------------------------===//
36/// Typedefs
37
38typedef std::vector<Record*> RecordVector;
39typedef std::vector<std::string> StrVector;
40
41//===----------------------------------------------------------------------===//
42/// Constants
43
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000044// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000045const char * Indent1 = " ";
46const char * Indent2 = " ";
47const char * Indent3 = " ";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000049// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
51
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000052// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000053const char * SinkOptionName = "AutoGeneratedSinkOption";
54
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
60 template<typename T>
61 void operator()(const T&) const {
62 }
63};
64
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000065int InitPtrToInt(const Init* ptr) {
66 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000067 return val.getValue();
68}
69
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000070const std::string& InitPtrToString(const Init* ptr) {
71 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
72 return val.getValue();
73}
74
75const ListInit& InitPtrToList(const Init* ptr) {
76 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
77 return val;
78}
79
80const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000081 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000082 return val;
83}
84
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000085// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000086// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000087void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +000088 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkov905a30b2009-05-06 04:54:23 +000089 throw d->getOperator()->getAsString()
90 + ": too few arguments!";
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000091}
92
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000093// isDagEmpty - is this DAG marked with an empty marker?
94bool isDagEmpty (const DagInit* d) {
95 return d->getOperator()->getAsString() == "empty";
96}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000097
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000098// EscapeVariableName - Escape commas and other symbols not allowed
99// in the C++ variable names. Makes it possible to use options named
100// like "Wa," (useful for prefix options).
101std::string EscapeVariableName(const std::string& Var) {
102 std::string ret;
103 for (unsigned i = 0; i != Var.size(); ++i) {
104 char cur_char = Var[i];
105 if (cur_char == ',') {
106 ret += "_comma_";
107 }
108 else if (cur_char == '+') {
109 ret += "_plus_";
110 }
111 else if (cur_char == '-') {
112 ret += "_dash_";
113 }
114 else {
115 ret.push_back(cur_char);
116 }
117 }
118 return ret;
119}
120
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000121/// oneOf - Does the input string contain this character?
122bool oneOf(const char* lst, char c) {
123 while (*lst) {
124 if (*lst++ == c)
125 return true;
126 }
127 return false;
128}
129
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000130template <class I, class S>
131void checkedIncrement(I& P, I E, S ErrorString) {
132 ++P;
133 if (P == E)
134 throw ErrorString;
135}
136
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000137//===----------------------------------------------------------------------===//
138/// Back-end specific code
139
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000140
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000141/// OptionType - One of six different option types. See the
142/// documentation for detailed description of differences.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000143namespace OptionType {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000144 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000145 Prefix, PrefixList};
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000146
147bool IsList (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000148 return (t == ParameterList || t == PrefixList);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000149}
150
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000151bool IsSwitch (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000152 return (t == Switch);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000153}
154
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000155bool IsParameter (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000156 return (t == Parameter || t == Prefix);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000157}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000158
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000159}
160
161OptionType::OptionType stringToOptionType(const std::string& T) {
162 if (T == "alias_option")
163 return OptionType::Alias;
164 else if (T == "switch_option")
165 return OptionType::Switch;
166 else if (T == "parameter_option")
167 return OptionType::Parameter;
168 else if (T == "parameter_list_option")
169 return OptionType::ParameterList;
170 else if (T == "prefix_option")
171 return OptionType::Prefix;
172 else if (T == "prefix_list_option")
173 return OptionType::PrefixList;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000174 else
175 throw "Unknown option type: " + T + '!';
176}
177
178namespace OptionDescriptionFlags {
179 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000180 ReallyHidden = 0x4, Extern = 0x8,
181 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000182}
183
184/// OptionDescription - Represents data contained in a single
185/// OptionList entry.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000186struct OptionDescription {
187 OptionType::OptionType Type;
188 std::string Name;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000189 unsigned Flags;
190 std::string Help;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000191 unsigned MultiVal;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000192
193 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000194 const std::string& n = "",
195 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000196 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000197 {}
198
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000199 /// GenTypeDeclaration - Returns the C++ variable type of this
200 /// option.
201 const char* GenTypeDeclaration() const;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000202
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000203 /// GenVariableName - Returns the variable name used in the
204 /// generated C++ code.
205 std::string GenVariableName() const;
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000206
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000207 /// Merge - Merge two option descriptions.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000208 void Merge (const OptionDescription& other);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000209
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000210 // Misc convenient getters/setters.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000211
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000212 bool isAlias() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000213
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000214 bool isMultiVal() const;
215
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000216 bool isExtern() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000217 void setExtern();
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000218
219 bool isRequired() const;
220 void setRequired();
221
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000222 bool isOneOrMore() const;
223 void setOneOrMore();
224
225 bool isZeroOrOne() const;
226 void setZeroOrOne();
227
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000228 bool isHidden() const;
229 void setHidden();
230
231 bool isReallyHidden() const;
232 void setReallyHidden();
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000233
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000234};
235
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000236void OptionDescription::Merge (const OptionDescription& other)
237{
238 if (other.Type != Type)
239 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000240
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000241 if (Help == other.Help || Help == DefaultHelpString)
242 Help = other.Help;
243 else if (other.Help != DefaultHelpString) {
244 llvm::cerr << "Warning: several different help strings"
245 " defined for option " + Name + "\n";
246 }
247
248 Flags |= other.Flags;
249}
250
251bool OptionDescription::isAlias() const {
252 return Type == OptionType::Alias;
253}
254
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000255bool OptionDescription::isMultiVal() const {
Mikhail Glushenkove3649982009-01-28 03:47:58 +0000256 return MultiVal > 1;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000257}
258
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000259bool OptionDescription::isExtern() const {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000260 return Flags & OptionDescriptionFlags::Extern;
261}
262void OptionDescription::setExtern() {
263 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000264}
265
266bool OptionDescription::isRequired() const {
267 return Flags & OptionDescriptionFlags::Required;
268}
269void OptionDescription::setRequired() {
270 Flags |= OptionDescriptionFlags::Required;
271}
272
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000273bool OptionDescription::isOneOrMore() const {
274 return Flags & OptionDescriptionFlags::OneOrMore;
275}
276void OptionDescription::setOneOrMore() {
277 Flags |= OptionDescriptionFlags::OneOrMore;
278}
279
280bool OptionDescription::isZeroOrOne() const {
281 return Flags & OptionDescriptionFlags::ZeroOrOne;
282}
283void OptionDescription::setZeroOrOne() {
284 Flags |= OptionDescriptionFlags::ZeroOrOne;
285}
286
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000287bool OptionDescription::isHidden() const {
288 return Flags & OptionDescriptionFlags::Hidden;
289}
290void OptionDescription::setHidden() {
291 Flags |= OptionDescriptionFlags::Hidden;
292}
293
294bool OptionDescription::isReallyHidden() const {
295 return Flags & OptionDescriptionFlags::ReallyHidden;
296}
297void OptionDescription::setReallyHidden() {
298 Flags |= OptionDescriptionFlags::ReallyHidden;
299}
300
301const char* OptionDescription::GenTypeDeclaration() const {
302 switch (Type) {
303 case OptionType::Alias:
304 return "cl::alias";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000305 case OptionType::PrefixList:
306 case OptionType::ParameterList:
307 return "cl::list<std::string>";
308 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000309 return "cl::opt<bool>";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000310 case OptionType::Parameter:
311 case OptionType::Prefix:
312 default:
313 return "cl::opt<std::string>";
314 }
315}
316
317std::string OptionDescription::GenVariableName() const {
318 const std::string& EscapedName = EscapeVariableName(Name);
319 switch (Type) {
320 case OptionType::Alias:
321 return "AutoGeneratedAlias_" + EscapedName;
322 case OptionType::PrefixList:
323 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000324 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000325 case OptionType::Switch:
326 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000327 case OptionType::Prefix:
328 case OptionType::Parameter:
329 default:
330 return "AutoGeneratedParameter_" + EscapedName;
331 }
332}
333
334/// OptionDescriptions - An OptionDescription array plus some helper
335/// functions.
336class OptionDescriptions {
337 typedef StringMap<OptionDescription> container_type;
338
339 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000340 container_type Descriptions;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000341
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000342public:
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000343 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000344 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000345
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000346 /// insertDescription - Insert new OptionDescription into
347 /// OptionDescriptions list
348 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000349
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000350 // Support for STL-style iteration
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000351 typedef container_type::const_iterator const_iterator;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000352 const_iterator begin() const { return Descriptions.begin(); }
353 const_iterator end() const { return Descriptions.end(); }
354};
355
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000356const OptionDescription&
357OptionDescriptions::FindOption(const std::string& OptName) const
358{
359 const_iterator I = Descriptions.find(OptName);
360 if (I != Descriptions.end())
361 return I->second;
362 else
363 throw OptName + ": no such option!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000364}
365
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000366void OptionDescriptions::InsertDescription (const OptionDescription& o)
367{
368 container_type::iterator I = Descriptions.find(o.Name);
369 if (I != Descriptions.end()) {
370 OptionDescription& D = I->second;
371 D.Merge(o);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000372 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000373 else {
374 Descriptions[o.Name] = o;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000375 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000376}
377
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000378/// HandlerTable - A base class for function objects implemented as
379/// 'tables of handlers'.
380template <class T>
381class HandlerTable {
382protected:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000383 // Implementation details.
384
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000385 /// Handler -
386 typedef void (T::* Handler) (const DagInit*);
387 /// HandlerMap - A map from property names to property handlers
388 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000389
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000390 static HandlerMap Handlers_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000391 static bool staticMembersInitialized_;
392
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000393 T* childPtr;
394public:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000395
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000396 HandlerTable(T* cp) : childPtr(cp)
397 {}
398
399 /// operator() - Just forwards to the corresponding property
400 /// handler.
401 void operator() (Init* i) {
402 const DagInit& property = InitPtrToDag(i);
403 const std::string& property_name = property.getOperator()->getAsString();
404 typename HandlerMap::iterator method = Handlers_.find(property_name);
405
406 if (method != Handlers_.end()) {
407 Handler h = method->second;
408 (childPtr->*h)(&property);
409 }
410 else {
411 throw "No handler found for property " + property_name + "!";
412 }
413 }
414
415 void AddHandler(const char* Property, Handler Handl) {
416 Handlers_[Property] = Handl;
417 }
418};
419
420template <class T> typename HandlerTable<T>::HandlerMap
421HandlerTable<T>::Handlers_;
422template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
423
424
425/// CollectOptionProperties - Function object for iterating over an
426/// option property list.
427class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
428private:
429
430 /// optDescs_ - OptionDescriptions table. This is where the
431 /// information is stored.
432 OptionDescription& optDesc_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000433
434public:
435
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000436 explicit CollectOptionProperties(OptionDescription& OD)
437 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000438 {
439 if (!staticMembersInitialized_) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000440 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000441 AddHandler("help", &CollectOptionProperties::onHelp);
442 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000443 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
444 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000445 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
446 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000447 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000448
449 staticMembersInitialized_ = true;
450 }
451 }
452
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000453private:
454
455 /// Option property handlers --
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000456 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000457
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000458 void onExtern (const DagInit* d) {
459 checkNumberOfArguments(d, 0);
460 optDesc_.setExtern();
461 }
462
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000463 void onHelp (const DagInit* d) {
464 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000465 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000466 }
467
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000468 void onHidden (const DagInit* d) {
469 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000470 optDesc_.setHidden();
471 }
472
473 void onReallyHidden (const DagInit* d) {
474 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000475 optDesc_.setReallyHidden();
476 }
477
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000478 void onRequired (const DagInit* d) {
479 checkNumberOfArguments(d, 0);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000480 if (optDesc_.isOneOrMore())
481 throw std::string("An option can't have both (required) "
482 "and (one_or_more) properties!");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000483 optDesc_.setRequired();
484 }
485
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000486 void onOneOrMore (const DagInit* d) {
487 checkNumberOfArguments(d, 0);
488 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
489 throw std::string("Only one of (required), (zero_or_one) or "
490 "(one_or_more) properties is allowed!");
491 if (!OptionType::IsList(optDesc_.Type))
492 llvm::cerr << "Warning: specifying the 'one_or_more' property "
493 "on a non-list option will have no effect.\n";
494 optDesc_.setOneOrMore();
495 }
496
497 void onZeroOrOne (const DagInit* d) {
498 checkNumberOfArguments(d, 0);
499 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
500 throw std::string("Only one of (required), (zero_or_one) or "
501 "(one_or_more) properties is allowed!");
502 if (!OptionType::IsList(optDesc_.Type))
503 llvm::cerr << "Warning: specifying the 'zero_or_one' property"
504 "on a non-list option will have no effect.\n";
505 optDesc_.setZeroOrOne();
506 }
507
508 void onMultiVal (const DagInit* d) {
509 checkNumberOfArguments(d, 1);
510 int val = InitPtrToInt(d->getArg(0));
511 if (val < 2)
512 throw std::string("Error in the 'multi_val' property: "
513 "the value must be greater than 1!");
514 if (!OptionType::IsList(optDesc_.Type))
515 throw std::string("The multi_val property is valid only "
516 "on list options!");
517 optDesc_.MultiVal = val;
518 }
519
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000520};
521
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000522/// AddOption - A function object that is applied to every option
523/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000524class AddOption {
525private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000526 OptionDescriptions& OptDescs_;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000527
528public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000529 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000530 {}
531
532 void operator()(const Init* i) {
533 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000534 checkNumberOfArguments(&d, 1);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000535
536 const OptionType::OptionType Type =
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000537 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000538 const std::string& Name = InitPtrToString(d.getArg(0));
539
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000540 OptionDescription OD(Type, Name);
541
542 if (!OD.isExtern())
543 checkNumberOfArguments(&d, 2);
544
545 if (OD.isAlias()) {
546 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000547 OD.Help = InitPtrToString(d.getArg(1));
548 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000549 else if (!OD.isExtern()) {
550 processOptionProperties(&d, OD);
551 }
552 OptDescs_.InsertDescription(OD);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000553 }
554
555private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000556 /// processOptionProperties - Go through the list of option
557 /// properties and call a corresponding handler for each.
558 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
559 checkNumberOfArguments(d, 2);
560 DagInit::const_arg_iterator B = d->arg_begin();
561 // Skip the first argument: it's always the option name.
562 ++B;
563 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000564 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000565
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000566};
567
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000568/// CollectOptionDescriptions - Collects option properties from all
569/// OptionLists.
570void CollectOptionDescriptions (RecordVector::const_iterator B,
571 RecordVector::const_iterator E,
572 OptionDescriptions& OptDescs)
573{
574 // For every OptionList:
575 for (; B!=E; ++B) {
576 RecordVector::value_type T = *B;
577 // Throws an exception if the value does not exist.
578 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000579
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000580 // For every option description in this list:
581 // collect the information and
582 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
583 }
584}
585
586// Tool information record
587
588namespace ToolFlags {
589 enum ToolFlags { Join = 0x1, Sink = 0x2 };
590}
591
592struct ToolDescription : public RefCountedBase<ToolDescription> {
593 std::string Name;
594 Init* CmdLine;
595 Init* Actions;
596 StrVector InLanguage;
597 std::string OutLanguage;
598 std::string OutputSuffix;
599 unsigned Flags;
600
601 // Various boolean properties
602 void setSink() { Flags |= ToolFlags::Sink; }
603 bool isSink() const { return Flags & ToolFlags::Sink; }
604 void setJoin() { Flags |= ToolFlags::Join; }
605 bool isJoin() const { return Flags & ToolFlags::Join; }
606
607 // Default ctor here is needed because StringMap can only store
608 // DefaultConstructible objects
609 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
610 ToolDescription (const std::string& n)
611 : Name(n), CmdLine(0), Actions(0), Flags(0)
612 {}
613};
614
615/// ToolDescriptions - A list of Tool information records.
616typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
617
618
619/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000620/// tool property records.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000621class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000622private:
623
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000624 /// toolDesc_ - Properties of the current Tool. This is where the
625 /// information is stored.
626 ToolDescription& toolDesc_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000627
628public:
629
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000630 explicit CollectToolProperties (ToolDescription& d)
631 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000632 {
633 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000634
635 AddHandler("actions", &CollectToolProperties::onActions);
636 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
637 AddHandler("in_language", &CollectToolProperties::onInLanguage);
638 AddHandler("join", &CollectToolProperties::onJoin);
639 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
640 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
641 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000642
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000643 staticMembersInitialized_ = true;
644 }
645 }
646
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000647private:
648
649 /// Property handlers --
650 /// Functions that extract information about tool properties from
651 /// DAG representation.
652
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000653 void onActions (const DagInit* d) {
654 checkNumberOfArguments(d, 1);
Mikhail Glushenkov8d489a82008-12-07 16:45:12 +0000655 Init* Case = d->getArg(0);
656 if (typeid(*Case) != typeid(DagInit) ||
657 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
658 throw
659 std::string("The argument to (actions) should be a 'case' construct!");
660 toolDesc_.Actions = Case;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000661 }
662
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000663 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000664 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000665 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000666 }
667
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000668 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000669 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000670 Init* arg = d->getArg(0);
671
672 // Find out the argument's type.
673 if (typeid(*arg) == typeid(StringInit)) {
674 // It's a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000675 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000676 }
677 else {
678 // It's a list.
679 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000680 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000681
682 // Copy strings to the output vector.
683 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
684 B != E; ++B) {
685 out.push_back(InitPtrToString(*B));
686 }
687
688 // Remove duplicates.
689 std::sort(out.begin(), out.end());
690 StrVector::iterator newE = std::unique(out.begin(), out.end());
691 out.erase(newE, out.end());
692 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000693 }
694
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000695 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000696 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000697 toolDesc_.setJoin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000698 }
699
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000700 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000701 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000702 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000703 }
704
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000705 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000706 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000707 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000708 }
709
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000710 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000711 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000712 toolDesc_.setSink();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000713 }
714
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000715};
716
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000717/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000718/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000719/// CollectToolProperties function object).
720void CollectToolDescriptions (RecordVector::const_iterator B,
721 RecordVector::const_iterator E,
722 ToolDescriptions& ToolDescs)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000723{
724 // Iterate over a properties list of every Tool definition
725 for (;B!=E;++B) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +0000726 const Record* T = *B;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000727 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000728 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000729
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000730 IntrusiveRefCntPtr<ToolDescription>
731 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000732
733 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000734 CollectToolProperties(*ToolDesc));
735 ToolDescs.push_back(ToolDesc);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000736 }
737}
738
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000739/// FillInEdgeVector - Merge all compilation graph definitions into
740/// one single edge list.
741void FillInEdgeVector(RecordVector::const_iterator B,
742 RecordVector::const_iterator E, RecordVector& Out) {
743 for (; B != E; ++B) {
744 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000745
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000746 for (unsigned i = 0; i < edges->size(); ++i)
747 Out.push_back(edges->getElementAsRecord(i));
748 }
749}
750
751/// CalculatePriority - Calculate the priority of this plugin.
752int CalculatePriority(RecordVector::const_iterator B,
753 RecordVector::const_iterator E) {
754 int total = 0;
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +0000755 for (; B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000756 total += static_cast<int>((*B)->getValueAsInt("priority"));
757 }
758 return total;
759}
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000760
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000761/// NotInGraph - Helper function object for FilterNotInGraph.
762struct NotInGraph {
763private:
764 const llvm::StringSet<>& ToolsInGraph_;
765
766public:
767 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
768 : ToolsInGraph_(ToolsInGraph)
769 {}
770
771 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
772 return (ToolsInGraph_.count(x->Name) == 0);
773 }
774};
775
776/// FilterNotInGraph - Filter out from ToolDescs all Tools not
777/// mentioned in the compilation graph definition.
778void FilterNotInGraph (const RecordVector& EdgeVector,
779 ToolDescriptions& ToolDescs) {
780
781 // List all tools mentioned in the graph.
782 llvm::StringSet<> ToolsInGraph;
783
784 for (RecordVector::const_iterator B = EdgeVector.begin(),
785 E = EdgeVector.end(); B != E; ++B) {
786
787 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000788 const std::string& NodeA = Edge->getValueAsString("a");
789 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000790
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000791 if (NodeA != "root")
792 ToolsInGraph.insert(NodeA);
793 ToolsInGraph.insert(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000794 }
795
796 // Filter ToolPropertiesList.
797 ToolDescriptions::iterator new_end =
798 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
799 NotInGraph(ToolsInGraph));
800 ToolDescs.erase(new_end, ToolDescs.end());
801}
802
803/// FillInToolToLang - Fills in two tables that map tool names to
804/// (input, output) languages. Helper function used by TypecheckGraph().
805void FillInToolToLang (const ToolDescriptions& ToolDescs,
806 StringMap<StringSet<> >& ToolToInLang,
807 StringMap<std::string>& ToolToOutLang) {
808 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
809 E = ToolDescs.end(); B != E; ++B) {
810 const ToolDescription& D = *(*B);
811 for (StrVector::const_iterator B = D.InLanguage.begin(),
812 E = D.InLanguage.end(); B != E; ++B)
813 ToolToInLang[D.Name].insert(*B);
814 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000815 }
816}
817
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000818/// TypecheckGraph - Check that names for output and input languages
819/// on all edges do match. This doesn't do much when the information
820/// about the whole graph is not available (i.e. when compiling most
821/// plugins).
822void TypecheckGraph (const RecordVector& EdgeVector,
823 const ToolDescriptions& ToolDescs) {
824 StringMap<StringSet<> > ToolToInLang;
825 StringMap<std::string> ToolToOutLang;
826
827 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
828 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
829 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
830
831 for (RecordVector::const_iterator B = EdgeVector.begin(),
832 E = EdgeVector.end(); B != E; ++B) {
833 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000834 const std::string& NodeA = Edge->getValueAsString("a");
835 const std::string& NodeB = Edge->getValueAsString("b");
836 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
837 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000838
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000839 if (NodeA != "root") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000840 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000841 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000842 + ": output->input language mismatch";
843 }
844
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000845 if (NodeB == "root")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000846 throw std::string("Edges back to the root are not allowed!");
847 }
848}
849
850/// WalkCase - Walks the 'case' expression DAG and invokes
851/// TestCallback on every test, and StatementCallback on every
852/// statement. Handles 'case' nesting, but not the 'and' and 'or'
853/// combinators.
854// TODO: Re-implement EmitCaseConstructHandler on top of this function?
855template <typename F1, typename F2>
856void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
857 const DagInit& d = InitPtrToDag(Case);
858 bool even = false;
859 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
860 B != E; ++B) {
861 Init* arg = *B;
862 if (even && dynamic_cast<DagInit*>(arg)
863 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
864 WalkCase(arg, TestCallback, StatementCallback);
865 else if (!even)
866 TestCallback(arg);
867 else
868 StatementCallback(arg);
869 even = !even;
870 }
871}
872
873/// ExtractOptionNames - A helper function object used by
874/// CheckForSuperfluousOptions() to walk the 'case' DAG.
875class ExtractOptionNames {
876 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000877
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000878 void processDag(const Init* Statement) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000879 const DagInit& Stmt = InitPtrToDag(Statement);
880 const std::string& ActionName = Stmt.getOperator()->getAsString();
881 if (ActionName == "forward" || ActionName == "forward_as" ||
882 ActionName == "unpack_values" || ActionName == "switch_on" ||
883 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000884 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000885 checkNumberOfArguments(&Stmt, 1);
886 const std::string& Name = InitPtrToString(Stmt.getArg(0));
887 OptionNames_.insert(Name);
888 }
889 else if (ActionName == "and" || ActionName == "or") {
890 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000891 this->processDag(Stmt.getArg(i));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000892 }
893 }
894 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000895
896public:
897 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
898 {}
899
900 void operator()(const Init* Statement) {
901 if (typeid(*Statement) == typeid(ListInit)) {
902 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
903 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
904 B != E; ++B)
905 this->processDag(*B);
906 }
907 else {
908 this->processDag(Statement);
909 }
910 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000911};
912
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000913/// CheckForSuperfluousOptions - Check that there are no side
914/// effect-free options (specified only in the OptionList). Otherwise,
915/// output a warning.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000916void CheckForSuperfluousOptions (const RecordVector& Edges,
917 const ToolDescriptions& ToolDescs,
918 const OptionDescriptions& OptDescs) {
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000919 llvm::StringSet<> nonSuperfluousOptions;
920
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000921 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000922 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000923 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
924 E = ToolDescs.end(); B != E; ++B) {
925 const ToolDescription& TD = *(*B);
926 ExtractOptionNames Callback(nonSuperfluousOptions);
927 if (TD.Actions)
928 WalkCase(TD.Actions, Callback, Callback);
929 }
930
931 // Add all options mentioned in the 'case' clauses of the
932 // OptionalEdges of the compilation graph to the set of
933 // non-superfluous options.
934 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
935 B != E; ++B) {
936 const Record* Edge = *B;
937 DagInit* Weight = Edge->getValueAsDag("weight");
938
939 if (!isDagEmpty(Weight))
940 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000941 }
942
943 // Check that all options in OptDescs belong to the set of
944 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000945 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000946 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000947 const OptionDescription& Val = B->second;
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000948 if (!nonSuperfluousOptions.count(Val.Name)
949 && Val.Type != OptionType::Alias)
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000950 llvm::cerr << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000951 "Probable cause: this option is specified only in the OptionList.\n";
952 }
953}
954
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000955/// EmitCaseTest1Arg - Helper function used by
956/// EmitCaseConstructHandler.
957bool EmitCaseTest1Arg(const std::string& TestName,
958 const DagInit& d,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000959 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000960 std::ostream& O) {
961 checkNumberOfArguments(&d, 1);
962 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000963
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000964 if (TestName == "switch_on") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000965 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
966 if (!OptionType::IsSwitch(OptDesc.Type))
967 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000968 O << OptDesc.GenVariableName();
969 return true;
970 } else if (TestName == "input_languages_contain") {
971 O << "InLangs.count(\"" << OptName << "\") != 0";
972 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000973 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000974 // This works only for single-argument Tool::GenerateAction. Join
975 // tools can process several files in different languages simultaneously.
976
977 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000978 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000979 return true;
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000980 } else if (TestName == "not_empty" || TestName == "empty") {
981 const char* Test = (TestName == "empty") ? "" : "!";
982
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000983 if (OptName == "o") {
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000984 O << Test << "OutputFilename.empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000985 return true;
986 }
987 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000988 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
989 if (OptionType::IsSwitch(OptDesc.Type))
990 throw OptName
991 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000992 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000993 return true;
994 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000995 }
996
997 return false;
998}
999
1000/// EmitCaseTest2Args - Helper function used by
1001/// EmitCaseConstructHandler.
1002bool EmitCaseTest2Args(const std::string& TestName,
1003 const DagInit& d,
1004 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001005 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001006 std::ostream& O) {
1007 checkNumberOfArguments(&d, 2);
1008 const std::string& OptName = InitPtrToString(d.getArg(0));
1009 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001010 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001011
1012 if (TestName == "parameter_equals") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001013 if (!OptionType::IsParameter(OptDesc.Type))
1014 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001015 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1016 return true;
1017 }
1018 else if (TestName == "element_in_list") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001019 if (!OptionType::IsList(OptDesc.Type))
1020 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001021 const std::string& VarName = OptDesc.GenVariableName();
1022 O << "std::find(" << VarName << ".begin(),\n"
1023 << IndentLevel << Indent1 << VarName << ".end(), \""
1024 << OptArg << "\") != " << VarName << ".end()";
1025 return true;
1026 }
1027
1028 return false;
1029}
1030
1031// Forward declaration.
1032// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1033void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001034 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001035 std::ostream& O);
1036
1037/// EmitLogicalOperationTest - Helper function used by
1038/// EmitCaseConstructHandler.
1039void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1040 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001041 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001042 std::ostream& O) {
1043 O << '(';
1044 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001045 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001046 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1047 if (j != NumArgs - 1)
1048 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
1049 else
1050 O << ')';
1051 }
1052}
1053
1054/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1055void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001056 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001057 std::ostream& O) {
1058 const std::string& TestName = d.getOperator()->getAsString();
1059
1060 if (TestName == "and")
1061 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1062 else if (TestName == "or")
1063 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
1064 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1065 return;
1066 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1067 return;
1068 else
1069 throw TestName + ": unknown edge property!";
1070}
1071
1072// Emit code that handles the 'case' construct.
1073// Takes a function object that should emit code for every case clause.
1074// Callback's type is
1075// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
1076template <typename F>
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001077void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +00001078 F Callback, bool EmitElseIf,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001079 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001080 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001081 const DagInit* d = &InitPtrToDag(Dag);
1082 if (d->getOperator()->getAsString() != "case")
1083 throw std::string("EmitCaseConstructHandler should be invoked"
1084 " only on 'case' expressions!");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001085
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001086 unsigned numArgs = d->getNumArgs();
1087 if (d->getNumArgs() < 2)
1088 throw "There should be at least one clause in the 'case' expression:\n"
1089 + d->getAsString();
1090
1091 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001092 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001093
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001094 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001095 if (Test.getOperator()->getAsString() == "default") {
1096 if (i+2 != numArgs)
1097 throw std::string("The 'default' clause should be the last in the"
1098 "'case' construct!");
1099 O << IndentLevel << "else {\n";
1100 }
1101 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001102 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001103 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1104 O << ") {\n";
1105 }
1106
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001107 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001108 ++i;
1109 if (i == numArgs)
1110 throw "Case construct handler: no corresponding action "
1111 "found for the test " + Test.getAsString() + '!';
1112
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001113 Init* arg = d->getArg(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001114 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1115 if (nd && (nd->getOperator()->getAsString() == "case")) {
1116 // Handle the nested 'case'.
1117 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001118 Callback, EmitElseIf, OptDescs, O);
1119 }
1120 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001121 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001122 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001123 O << IndentLevel << "}\n";
1124 }
1125}
1126
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001127/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1128/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1129/// Helper function used by EmitCmdLineVecFill and.
1130void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1131 const char* Delimiters = " \t\n\v\f\r";
1132 enum TokenizerState
1133 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1134 cur_st = Normal;
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001135
1136 if (CmdLine.empty())
1137 return;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001138 Out.push_back("");
1139
1140 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1141 E = CmdLine.size();
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001142
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001143 for (; B != E; ++B) {
1144 char cur_ch = CmdLine[B];
1145
1146 switch (cur_st) {
1147 case Normal:
1148 if (cur_ch == '$') {
1149 cur_st = SpecialCommand;
1150 break;
1151 }
1152 if (oneOf(Delimiters, cur_ch)) {
1153 // Skip whitespace
1154 B = CmdLine.find_first_not_of(Delimiters, B);
1155 if (B == std::string::npos) {
1156 B = E-1;
1157 continue;
1158 }
1159 --B;
1160 Out.push_back("");
1161 continue;
1162 }
1163 break;
1164
1165
1166 case SpecialCommand:
1167 if (oneOf(Delimiters, cur_ch)) {
1168 cur_st = Normal;
1169 Out.push_back("");
1170 continue;
1171 }
1172 if (cur_ch == '(') {
1173 Out.push_back("");
1174 cur_st = InsideSpecialCommand;
1175 continue;
1176 }
1177 break;
1178
1179 case InsideSpecialCommand:
1180 if (oneOf(Delimiters, cur_ch)) {
1181 continue;
1182 }
1183 if (cur_ch == '\'') {
1184 cur_st = InsideQuotationMarks;
1185 Out.push_back("");
1186 continue;
1187 }
1188 if (cur_ch == ')') {
1189 cur_st = Normal;
1190 Out.push_back("");
1191 }
1192 if (cur_ch == ',') {
1193 continue;
1194 }
1195
1196 break;
1197
1198 case InsideQuotationMarks:
1199 if (cur_ch == '\'') {
1200 cur_st = InsideSpecialCommand;
1201 continue;
1202 }
1203 break;
1204 }
1205
1206 Out.back().push_back(cur_ch);
1207 }
1208}
1209
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001210/// SubstituteSpecialCommands - Perform string substitution for $CALL
1211/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001212StrVector::const_iterator SubstituteSpecialCommands
1213(StrVector::const_iterator Pos, StrVector::const_iterator End, std::ostream& O)
1214{
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001215
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001216 const std::string& cmd = *Pos;
1217
1218 if (cmd == "$CALL") {
1219 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1220 const std::string& CmdName = *Pos;
1221
1222 if (CmdName == ")")
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001223 throw std::string("$CALL invocation: empty argument list!");
1224
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001225 O << "hooks::";
1226 O << CmdName << "(";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001227
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001228
1229 bool firstIteration = true;
1230 while (true) {
1231 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1232 const std::string& Arg = *Pos;
1233 assert(Arg.size() != 0);
1234
1235 if (Arg[0] == ')')
1236 break;
1237
1238 if (firstIteration)
1239 firstIteration = false;
1240 else
1241 O << ", ";
1242
1243 O << '"' << Arg << '"';
1244 }
1245
1246 O << ')';
1247
1248 }
1249 else if (cmd == "$ENV") {
1250 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1251 const std::string& EnvName = *Pos;
1252
1253 if (EnvName == ")")
1254 throw "$ENV invocation: empty argument list!";
1255
1256 O << "checkCString(std::getenv(\"";
1257 O << EnvName;
1258 O << "\"))";
1259
1260 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001261 }
1262 else {
1263 throw "Unknown special command: " + cmd;
1264 }
1265
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001266 const std::string& Leftover = *Pos;
1267 assert(Leftover.at(0) == ')');
1268 if (Leftover.size() != 1)
1269 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001270
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001271 return Pos;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001272}
1273
1274/// EmitCmdLineVecFill - Emit code that fills in the command line
1275/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001276void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001277 bool IsJoin, const char* IndentLevel,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001278 std::ostream& O) {
1279 StrVector StrVec;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001280 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1281
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001282 if (StrVec.empty())
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001283 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001284
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001285 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1286
1287 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001288 assert(!StrVec[0].empty());
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001289 if (StrVec[0][0] == '$') {
1290 while (I != E && (*I)[0] != ')' )
1291 ++I;
1292
1293 // Skip the ')' symbol.
1294 ++I;
1295 }
1296 else {
1297 ++I;
1298 }
1299
1300 for (; I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001301 const std::string& cmd = *I;
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001302 assert(!cmd.empty());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001303 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001304 if (cmd.at(0) == '$') {
1305 if (cmd == "$INFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001306 if (IsJoin)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001307 O << "for (PathVector::const_iterator B = inFiles.begin()"
1308 << ", E = inFiles.end();\n"
1309 << IndentLevel << "B != E; ++B)\n"
1310 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1311 else
1312 O << "vec.push_back(inFile.toString());\n";
1313 }
1314 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001315 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001316 }
1317 else {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001318 O << "vec.push_back(";
1319 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001320 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001321 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001322 }
1323 else {
1324 O << "vec.push_back(\"" << cmd << "\");\n";
1325 }
1326 }
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001327 O << IndentLevel << "cmd = ";
1328
1329 if (StrVec[0][0] == '$')
1330 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1331 else
1332 O << '"' << StrVec[0] << '"';
1333 O << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001334}
1335
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001336/// EmitCmdLineVecFillCallback - A function object wrapper around
1337/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1338/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001339class EmitCmdLineVecFillCallback {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001340 bool IsJoin;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001341 const std::string& ToolName;
1342 public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001343 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1344 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001345
1346 void operator()(const Init* Statement, const char* IndentLevel,
1347 std::ostream& O) const
1348 {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001349 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1350 IndentLevel, O);
1351 }
1352};
1353
1354/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1355/// implement EmitActionHandler. Emits code for
1356/// handling the (forward) and (forward_as) option properties.
1357void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1358 const char* Indent,
1359 const std::string& NewName,
1360 std::ostream& O) {
1361 const std::string& Name = NewName.empty()
1362 ? ("-" + D.Name)
1363 : NewName;
1364
1365 switch (D.Type) {
1366 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001367 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1368 break;
1369 case OptionType::Parameter:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001370 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1371 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1372 break;
1373 case OptionType::Prefix:
1374 O << Indent << "vec.push_back(\"" << Name << "\" + "
1375 << D.GenVariableName() << ");\n";
1376 break;
1377 case OptionType::PrefixList:
1378 O << Indent << "for (" << D.GenTypeDeclaration()
1379 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001380 << Indent << "E = " << D.GenVariableName() << ".end(); B != E;) {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001381 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001382 << "*B);\n"
1383 << Indent << Indent1 << "++B;\n";
1384
1385 for (int i = 1, j = D.MultiVal; i < j; ++i) {
1386 O << Indent << Indent1 << "vec.push_back(*B);\n"
1387 << Indent << Indent1 << "++B;\n";
1388 }
1389
1390 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001391 break;
1392 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001393 O << Indent << "for (" << D.GenTypeDeclaration()
1394 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1395 << Indent << "E = " << D.GenVariableName()
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001396 << ".end() ; B != E;) {\n"
1397 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n";
1398
1399 for (int i = 0, j = D.MultiVal; i < j; ++i) {
1400 O << Indent << Indent1 << "vec.push_back(*B);\n"
1401 << Indent << Indent1 << "++B;\n";
1402 }
1403
1404 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001405 break;
1406 case OptionType::Alias:
1407 default:
1408 throw std::string("Aliases are not allowed in tool option descriptions!");
1409 }
1410}
1411
1412/// EmitActionHandler - Emit code that handles actions. Used by
1413/// EmitGenerateActionMethod() as an argument to
1414/// EmitCaseConstructHandler().
1415class EmitActionHandler {
1416 const OptionDescriptions& OptDescs;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001417
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001418 void processActionDag(const Init* Statement, const char* IndentLevel,
1419 std::ostream& O) const
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001420 {
1421 const DagInit& Dag = InitPtrToDag(Statement);
1422 const std::string& ActionName = Dag.getOperator()->getAsString();
1423
1424 if (ActionName == "append_cmd") {
1425 checkNumberOfArguments(&Dag, 1);
1426 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovfa8787d2009-02-27 06:46:55 +00001427 StrVector Out;
1428 llvm::SplitString(Cmd, Out);
1429
1430 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1431 B != E; ++B)
1432 O << IndentLevel << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001433 }
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001434 else if (ActionName == "error") {
1435 O << IndentLevel << "throw std::runtime_error(\"" <<
1436 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1437 : "Unknown error!")
1438 << "\");\n";
1439 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001440 else if (ActionName == "forward") {
1441 checkNumberOfArguments(&Dag, 1);
1442 const std::string& Name = InitPtrToString(Dag.getArg(0));
1443 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1444 IndentLevel, "", O);
1445 }
1446 else if (ActionName == "forward_as") {
1447 checkNumberOfArguments(&Dag, 2);
1448 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkov09699552009-05-06 01:41:19 +00001449 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001450 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1451 IndentLevel, NewName, O);
1452 }
1453 else if (ActionName == "output_suffix") {
1454 checkNumberOfArguments(&Dag, 1);
1455 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1456 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1457 }
1458 else if (ActionName == "stop_compilation") {
1459 O << IndentLevel << "stop_compilation = true;\n";
1460 }
1461 else if (ActionName == "unpack_values") {
1462 checkNumberOfArguments(&Dag, 1);
1463 const std::string& Name = InitPtrToString(Dag.getArg(0));
1464 const OptionDescription& D = OptDescs.FindOption(Name);
1465
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001466 if (D.isMultiVal())
1467 throw std::string("Can't use unpack_values with multi-valued options!");
1468
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001469 if (OptionType::IsList(D.Type)) {
1470 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1471 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1472 << IndentLevel << "E = " << D.GenVariableName()
1473 << ".end(); B != E; ++B)\n"
1474 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1475 }
1476 else if (OptionType::IsParameter(D.Type)){
1477 O << Indent3 << "llvm::SplitString("
1478 << D.GenVariableName() << ", vec, \",\");\n";
1479 }
1480 else {
1481 throw "Option '" + D.Name +
1482 "': switches can't have the 'unpack_values' property!";
1483 }
1484 }
1485 else {
1486 throw "Unknown action name: " + ActionName + "!";
1487 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001488 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001489 public:
1490 EmitActionHandler(const OptionDescriptions& OD)
1491 : OptDescs(OD) {}
1492
1493 void operator()(const Init* Statement, const char* IndentLevel,
1494 std::ostream& O) const
1495 {
1496 if (typeid(*Statement) == typeid(ListInit)) {
1497 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1498 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1499 B != E; ++B)
1500 this->processActionDag(*B, IndentLevel, O);
1501 }
1502 else {
1503 this->processActionDag(Statement, IndentLevel, O);
1504 }
1505 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001506};
1507
1508// EmitGenerateActionMethod - Emit one of two versions of the
1509// Tool::GenerateAction() method.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001510void EmitGenerateActionMethod (const ToolDescription& D,
1511 const OptionDescriptions& OptDescs,
1512 bool IsJoin, std::ostream& O) {
1513 if (IsJoin)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001514 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1515 else
1516 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1517
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001518 O << Indent2 << "bool HasChildren,\n"
1519 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001520 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1521 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001522 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001523 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001524 << Indent2 << "std::vector<std::string> vec;\n"
1525 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1526 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1527 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001528
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001529 // For every understood option, emit handling code.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001530 if (D.Actions)
1531 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1532 false, OptDescs, O);
1533
1534 O << '\n' << Indent2
1535 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
1536 << Indent3 << "TempDir, stop_compilation, output_suffix).toString();\n\n";
1537
1538 // cmd_line is either a string or a 'case' construct.
1539 if (!D.CmdLine)
1540 throw "Tool " + D.Name + " has no cmd_line property!";
1541 else if (typeid(*D.CmdLine) == typeid(StringInit))
1542 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1543 else
1544 EmitCaseConstructHandler(D.CmdLine, Indent2,
1545 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1546 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001547
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001548 // Handle the Sink property.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001549 if (D.isSink()) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001550 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1551 << Indent3 << "vec.insert(vec.end(), "
1552 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1553 << Indent2 << "}\n";
1554 }
1555
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001556 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001557 << Indent1 << "}\n\n";
1558}
1559
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001560/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1561/// a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001562void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1563 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001564 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001565 if (!ToolDesc.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001566 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001567 << Indent2 << "bool HasChildren,\n"
1568 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001569 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1570 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001571 << Indent1 << "{\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001572 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001573 << " is not a Join tool!\");\n"
1574 << Indent1 << "}\n\n";
1575 else
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001576 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001577
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001578 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001579}
1580
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001581/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1582/// methods for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001583void EmitInOutLanguageMethods (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001584 O << Indent1 << "const char** InputLanguages() const {\n"
1585 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001586 << Indent1 << "}\n\n";
1587
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001588 if (D.OutLanguage.empty())
1589 throw "Tool " + D.Name + " has no 'out_language' property!";
1590
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001591 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001592 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001593 << Indent1 << "}\n\n";
1594}
1595
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001596/// EmitNameMethod - Emit the Name() method for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001597void EmitNameMethod (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001598 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001599 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001600 << Indent1 << "}\n\n";
1601}
1602
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001603/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1604/// class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001605void EmitIsJoinMethod (const ToolDescription& D, std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001606 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001607 if (D.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001608 O << Indent2 << "return true;\n";
1609 else
1610 O << Indent2 << "return false;\n";
1611 O << Indent1 << "}\n\n";
1612}
1613
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001614/// EmitStaticMemberDefinitions - Emit static member definitions for a
1615/// given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001616void EmitStaticMemberDefinitions(const ToolDescription& D, std::ostream& O) {
1617 if (D.InLanguage.empty())
1618 throw "Tool " + D.Name + " has no 'in_language' property!";
1619
1620 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1621 for (StrVector::const_iterator B = D.InLanguage.begin(),
1622 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001623 O << '\"' << *B << "\", ";
1624 O << "0};\n\n";
1625}
1626
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001627/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001628void EmitToolClassDefinition (const ToolDescription& D,
1629 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001630 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001631 if (D.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001632 return;
1633
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001634 // Header
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001635 O << "class " << D.Name << " : public ";
1636 if (D.isJoin())
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001637 O << "JoinTool";
1638 else
1639 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001640
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001641 O << "{\nprivate:\n"
1642 << Indent1 << "static const char* InputLanguages_[];\n\n";
1643
1644 O << "public:\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001645 EmitNameMethod(D, O);
1646 EmitInOutLanguageMethods(D, O);
1647 EmitIsJoinMethod(D, O);
1648 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001649
1650 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001651 O << "};\n";
1652
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001653 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001654
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001655}
1656
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001657/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001658/// and emit registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001659void EmitOptionDefinitions (const OptionDescriptions& descs,
1660 bool HasSink, bool HasExterns,
1661 std::ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001662{
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001663 std::vector<OptionDescription> Aliases;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001664
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001665 // Emit static cl::Option variables.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001666 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001667 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001668 const OptionDescription& val = B->second;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001669
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001670 if (val.Type == OptionType::Alias) {
1671 Aliases.push_back(val);
1672 continue;
1673 }
1674
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001675 if (val.isExtern())
1676 O << "extern ";
1677
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001678 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001679 << val.GenVariableName();
1680
1681 if (val.isExtern()) {
1682 O << ";\n";
1683 continue;
1684 }
1685
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001686 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001687
1688 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1689 O << ", cl::Prefix";
1690
1691 if (val.isRequired()) {
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001692 if (OptionType::IsList(val.Type) && !val.isMultiVal())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001693 O << ", cl::OneOrMore";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001694 else
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001695 O << ", cl::Required";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001696 }
1697 else if (val.isOneOrMore() && OptionType::IsList(val.Type)) {
1698 O << ", cl::OneOrMore";
1699 }
1700 else if (val.isZeroOrOne() && OptionType::IsList(val.Type)) {
1701 O << ", cl::ZeroOrOne";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001702 }
1703
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001704 if (val.isReallyHidden()) {
1705 O << ", cl::ReallyHidden";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001706 }
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001707 else if (val.isHidden()) {
1708 O << ", cl::Hidden";
1709 }
1710
1711 if (val.MultiVal > 1)
1712 O << ", cl::multi_val(" << val.MultiVal << ")";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001713
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001714 if (!val.Help.empty())
1715 O << ", cl::desc(\"" << val.Help << "\")";
1716
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001717 O << ");\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001718 }
1719
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001720 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001721 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001722 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001723 const OptionDescription& val = *B;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001724
1725 O << val.GenTypeDeclaration() << ' '
1726 << val.GenVariableName()
1727 << "(\"" << val.Name << '\"';
1728
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001729 const OptionDescription& D = descs.FindOption(val.Help);
1730 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001731
1732 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1733 }
1734
1735 // Emit the sink option.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001736 if (HasSink)
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001737 O << (HasExterns ? "extern cl" : "cl")
1738 << "::list<std::string> " << SinkOptionName
1739 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001740
1741 O << '\n';
1742}
1743
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001744/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001745void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1746{
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001747 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001748 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001749
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001750 // Get the relevant field out of RecordKeeper
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001751 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001752
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001753 // It is allowed for a plugin to have no language map.
1754 if (LangMapRecord) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001755
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001756 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1757 if (!LangsToSuffixesList)
1758 throw std::string("Error in the language map definition!");
1759
1760 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001761 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001762
1763 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1764 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1765
1766 for (unsigned i = 0; i < Suffixes->size(); ++i)
1767 O << Indent1 << "langMap[\""
1768 << InitPtrToString(Suffixes->getElement(i))
1769 << "\"] = \"" << Lang << "\";\n";
1770 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001771 }
1772
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001773 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001774}
1775
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001776/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1777/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001778void IncDecWeight (const Init* i, const char* IndentLevel,
1779 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001780 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001781 const std::string& OpName = d.getOperator()->getAsString();
1782
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001783 if (OpName == "inc_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001784 O << IndentLevel << "ret += ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001785 }
1786 else if (OpName == "dec_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001787 O << IndentLevel << "ret -= ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001788 }
1789 else if (OpName == "error") {
1790 O << IndentLevel << "throw std::runtime_error(\"" <<
1791 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1792 : "Unknown error!")
1793 << "\");\n";
1794 return;
1795 }
1796
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001797 else
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001798 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov55c6bdf2008-12-18 04:06:58 +00001799 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001800
1801 if (d.getNumArgs() > 0)
1802 O << InitPtrToInt(d.getArg(0)) << ";\n";
1803 else
1804 O << "2;\n";
1805
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001806}
1807
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001808/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001809void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001810 DagInit* Case, const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001811 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001812
1813 // Class constructor.
1814 O << "class Edge" << N << ": public Edge {\n"
1815 << "public:\n"
1816 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1817 << "\") {}\n\n"
1818
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001819 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001820 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001821 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001822
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001823 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001824 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001825
1826 O << Indent2 << "return ret;\n"
1827 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001828}
1829
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001830/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001831void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001832 const OptionDescriptions& OptDescs,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001833 std::ostream& O) {
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001834 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001835 for (RecordVector::const_iterator B = EdgeVector.begin(),
1836 E = EdgeVector.end(); B != E; ++B) {
1837 const Record* Edge = *B;
1838 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001839 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001840
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001841 if (!isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001842 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001843 ++i;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001844 }
1845}
1846
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001847/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1848/// function.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001849void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001850 const ToolDescriptions& ToolDescs,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001851 std::ostream& O)
1852{
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001853 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001854
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001855 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1856 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001857 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001858
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001859 O << '\n';
1860
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001861 // Insert edges.
1862
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001863 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001864 for (RecordVector::const_iterator B = EdgeVector.begin(),
1865 E = EdgeVector.end(); B != E; ++B) {
1866 const Record* Edge = *B;
1867 const std::string& NodeA = Edge->getValueAsString("a");
1868 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001869 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001870
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001871 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001872
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001873 if (isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001874 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001875 else
1876 O << "new Edge" << i << "()";
1877
1878 O << ");\n";
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001879 ++i;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001880 }
1881
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001882 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001883}
1884
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001885/// ExtractHookNames - Extract the hook names from all instances of
1886/// $CALL(HookName) in the provided command line string. Helper
1887/// function used by FillInHookNames().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001888class ExtractHookNames {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001889 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001890public:
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001891 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
1892 : HookNames_(HookNames) {}
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001893
1894 void operator()(const Init* CmdLine) {
1895 StrVector cmds;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001896 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001897 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1898 B != E; ++B) {
1899 const std::string& cmd = *B;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001900
1901 if (cmd == "$CALL") {
1902 unsigned NumArgs = 0;
1903 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
1904 const std::string& HookName = *B;
1905
1906
1907 if (HookName.at(0) == ')')
1908 throw "$CALL invoked with no arguments!";
1909
1910 while (++B != E && B->at(0) != ')') {
1911 ++NumArgs;
1912 }
1913
1914 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
1915
1916 if (H != HookNames_.end() && H->second != NumArgs)
1917 throw "Overloading of hooks is not allowed. Overloaded hook: "
1918 + HookName;
1919 else
1920 HookNames_[HookName] = NumArgs;
1921
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001922 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001923 }
1924 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001925};
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001926
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001927/// FillInHookNames - Actually extract the hook names from all command
1928/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001929void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001930 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001931{
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001932 // For all command lines:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001933 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1934 E = ToolDescs.end(); B != E; ++B) {
1935 const ToolDescription& D = *(*B);
1936 if (!D.CmdLine)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001937 continue;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001938 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001939 // This is a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001940 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001941 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001942 // This is a 'case' construct.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001943 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001944 }
1945}
1946
1947/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1948/// property records and emit hook function declaration for each
1949/// instance of $CALL(HookName).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001950void EmitHookDeclarations(const ToolDescriptions& ToolDescs, std::ostream& O) {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001951 llvm::StringMap<unsigned> HookNames;
1952
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001953 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001954 if (HookNames.empty())
1955 return;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001956
1957 O << "namespace hooks {\n";
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001958 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
1959 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkoved765fe2009-01-21 13:04:33 +00001960 O << Indent1 << "std::string " << B->first() << "(";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001961
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001962 for (unsigned i = 0, j = B->second; i < j; ++i) {
1963 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
1964 }
1965
1966 O <<");\n";
1967 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001968 O << "}\n\n";
1969}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001970
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001971/// EmitRegisterPlugin - Emit code to register this plugin.
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001972void EmitRegisterPlugin(int Priority, std::ostream& O) {
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001973 O << "struct Plugin : public llvmc::BasePlugin {\n\n"
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001974 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001975 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
1976 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
1977 << Indent1
1978 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
1979 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
1980 << "};\n\n"
1981
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001982 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001983}
1984
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001985/// EmitIncludes - Emit necessary #include directives and some
1986/// additional declarations.
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001987void EmitIncludes(std::ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001988 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00001989 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001990 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1991 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001992
1993 << "#include \"llvm/ADT/StringExtras.h\"\n"
1994 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1995
1996 << "#include <cstdlib>\n"
1997 << "#include <stdexcept>\n\n"
1998
1999 << "using namespace llvm;\n"
2000 << "using namespace llvmc;\n\n"
2001
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00002002 << "extern cl::opt<std::string> OutputFilename;\n\n"
2003
2004 << "inline const char* checkCString(const char* s)\n"
2005 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00002006}
2007
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002008
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002009/// PluginData - Holds all information about a plugin.
2010struct PluginData {
2011 OptionDescriptions OptDescs;
2012 bool HasSink;
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002013 bool HasExterns;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002014 ToolDescriptions ToolDescs;
2015 RecordVector Edges;
2016 int Priority;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002017};
2018
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002019/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002020/// there are any with the 'sink' property set.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002021bool HasSink(const ToolDescriptions& ToolDescs) {
2022 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2023 E = ToolDescs.end(); B != E; ++B)
2024 if ((*B)->isSink())
2025 return true;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002026
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002027 return false;
2028}
2029
2030/// HasExterns - Go through the list of option descriptions and check
2031/// if there are any external options.
2032bool HasExterns(const OptionDescriptions& OptDescs) {
2033 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2034 E = OptDescs.end(); B != E; ++B)
2035 if (B->second.isExtern())
2036 return true;
2037
2038 return false;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002039}
2040
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002041/// CollectPluginData - Collect tool and option properties,
2042/// compilation graph edges and plugin priority from the parse tree.
2043void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2044 // Collect option properties.
2045 const RecordVector& OptionLists =
2046 Records.getAllDerivedDefinitions("OptionList");
2047 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2048 Data.OptDescs);
2049
2050 // Collect tool properties.
2051 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2052 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2053 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002054 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002055
2056 // Collect compilation graph edges.
2057 const RecordVector& CompilationGraphs =
2058 Records.getAllDerivedDefinitions("CompilationGraph");
2059 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2060 Data.Edges);
2061
2062 // Calculate the priority of this plugin.
2063 const RecordVector& Priorities =
2064 Records.getAllDerivedDefinitions("PluginPriority");
2065 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00002066}
2067
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002068/// CheckPluginData - Perform some sanity checks on the collected data.
2069void CheckPluginData(PluginData& Data) {
2070 // Filter out all tools not mentioned in the compilation graph.
2071 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002072
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002073 // Typecheck the compilation graph.
2074 TypecheckGraph(Data.Edges, Data.ToolDescs);
2075
2076 // Check that there are no options without side effects (specified
2077 // only in the OptionList).
2078 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2079
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002080}
2081
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002082void EmitPluginCode(const PluginData& Data, std::ostream& O) {
2083 // Emit file header.
2084 EmitIncludes(O);
2085
2086 // Emit global option registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00002087 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002088
2089 // Emit hook declarations.
2090 EmitHookDeclarations(Data.ToolDescs, O);
2091
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00002092 O << "namespace {\n\n";
2093
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002094 // Emit PopulateLanguageMap() function
2095 // (a language map maps from file extensions to language names).
2096 EmitPopulateLanguageMap(Records, O);
2097
2098 // Emit Tool classes.
2099 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2100 E = Data.ToolDescs.end(); B!=E; ++B)
2101 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2102
2103 // Emit Edge# classes.
2104 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2105
2106 // Emit PopulateCompilationGraph() function.
2107 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2108
2109 // Emit code for plugin registration.
2110 EmitRegisterPlugin(Data.Priority, O);
2111
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00002112 O << "} // End anonymous namespace.\n\n";
2113
2114 // Force linkage magic.
2115 O << "namespace llvmc {\n";
2116 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2117 O << "}\n";
2118
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002119 // EOF
2120}
2121
2122
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002123// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00002124}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002125
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002126/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00002127void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002128 try {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002129 PluginData Data;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002130
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002131 CollectPluginData(Records, Data);
2132 CheckPluginData(Data);
2133
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00002134 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002135 EmitPluginCode(Data, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002136
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002137 } catch (std::exception& Error) {
2138 throw Error.what() + std::string(" - usually this means a syntax error.");
2139 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002140}