blob: fc182ce5af8e7dbd748e9200467f62d933556777 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000026#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000028
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000042// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000043const char * Indent1 = " ";
44const char * Indent2 = " ";
45const char * Indent3 = " ";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000046
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000047// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
49
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000050// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000051const char * SinkOptionName = "AutoGeneratedSinkOption";
52
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000056/// Id - An 'identity' function object.
57struct Id {
58 template<typename T>
59 void operator()(const T&) const {
60 }
61};
62
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000063int InitPtrToInt(const Init* ptr) {
64 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000065 return val.getValue();
66}
67
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000068const std::string& InitPtrToString(const Init* ptr) {
69 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
70 return val.getValue();
71}
72
73const ListInit& InitPtrToList(const Init* ptr) {
74 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
75 return val;
76}
77
78const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000079 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000080 return val;
81}
82
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000083// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000084// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000085void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +000086 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkov905a30b2009-05-06 04:54:23 +000087 throw d->getOperator()->getAsString()
88 + ": too few arguments!";
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000089}
90
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000091// isDagEmpty - is this DAG marked with an empty marker?
92bool isDagEmpty (const DagInit* d) {
93 return d->getOperator()->getAsString() == "empty";
94}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000095
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000096// EscapeVariableName - Escape commas and other symbols not allowed
97// in the C++ variable names. Makes it possible to use options named
98// like "Wa," (useful for prefix options).
99std::string EscapeVariableName(const std::string& Var) {
100 std::string ret;
101 for (unsigned i = 0; i != Var.size(); ++i) {
102 char cur_char = Var[i];
103 if (cur_char == ',') {
104 ret += "_comma_";
105 }
106 else if (cur_char == '+') {
107 ret += "_plus_";
108 }
109 else if (cur_char == '-') {
110 ret += "_dash_";
111 }
112 else {
113 ret.push_back(cur_char);
114 }
115 }
116 return ret;
117}
118
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000119/// oneOf - Does the input string contain this character?
120bool oneOf(const char* lst, char c) {
121 while (*lst) {
122 if (*lst++ == c)
123 return true;
124 }
125 return false;
126}
127
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000128template <class I, class S>
129void checkedIncrement(I& P, I E, S ErrorString) {
130 ++P;
131 if (P == E)
132 throw ErrorString;
133}
134
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000135//===----------------------------------------------------------------------===//
136/// Back-end specific code
137
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000138
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000139/// OptionType - One of six different option types. See the
140/// documentation for detailed description of differences.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000141namespace OptionType {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000142 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000143 Prefix, PrefixList};
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000144
145bool IsList (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000146 return (t == ParameterList || t == PrefixList);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000147}
148
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000149bool IsSwitch (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000150 return (t == Switch);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000151}
152
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000153bool IsParameter (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000154 return (t == Parameter || t == Prefix);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000155}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000156
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000157}
158
159OptionType::OptionType stringToOptionType(const std::string& T) {
160 if (T == "alias_option")
161 return OptionType::Alias;
162 else if (T == "switch_option")
163 return OptionType::Switch;
164 else if (T == "parameter_option")
165 return OptionType::Parameter;
166 else if (T == "parameter_list_option")
167 return OptionType::ParameterList;
168 else if (T == "prefix_option")
169 return OptionType::Prefix;
170 else if (T == "prefix_list_option")
171 return OptionType::PrefixList;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000172 else
173 throw "Unknown option type: " + T + '!';
174}
175
176namespace OptionDescriptionFlags {
177 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000178 ReallyHidden = 0x4, Extern = 0x8,
179 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000180}
181
182/// OptionDescription - Represents data contained in a single
183/// OptionList entry.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000184struct OptionDescription {
185 OptionType::OptionType Type;
186 std::string Name;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000187 unsigned Flags;
188 std::string Help;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000189 unsigned MultiVal;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000190
191 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000192 const std::string& n = "",
193 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000194 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000195 {}
196
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000197 /// GenTypeDeclaration - Returns the C++ variable type of this
198 /// option.
199 const char* GenTypeDeclaration() const;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000200
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000201 /// GenVariableName - Returns the variable name used in the
202 /// generated C++ code.
203 std::string GenVariableName() const;
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000204
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000205 /// Merge - Merge two option descriptions.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000206 void Merge (const OptionDescription& other);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000207
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000208 // Misc convenient getters/setters.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000209
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000210 bool isAlias() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000211
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000212 bool isMultiVal() const;
213
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000214 bool isExtern() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000215 void setExtern();
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000216
217 bool isRequired() const;
218 void setRequired();
219
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000220 bool isOneOrMore() const;
221 void setOneOrMore();
222
223 bool isZeroOrOne() const;
224 void setZeroOrOne();
225
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000226 bool isHidden() const;
227 void setHidden();
228
229 bool isReallyHidden() const;
230 void setReallyHidden();
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000231
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000232};
233
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000234void OptionDescription::Merge (const OptionDescription& other)
235{
236 if (other.Type != Type)
237 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000238
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000239 if (Help == other.Help || Help == DefaultHelpString)
240 Help = other.Help;
241 else if (other.Help != DefaultHelpString) {
Daniel Dunbard4287062009-07-03 00:10:29 +0000242 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000243 " defined for option " + Name + "\n";
244 }
245
246 Flags |= other.Flags;
247}
248
249bool OptionDescription::isAlias() const {
250 return Type == OptionType::Alias;
251}
252
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000253bool OptionDescription::isMultiVal() const {
Mikhail Glushenkove3649982009-01-28 03:47:58 +0000254 return MultiVal > 1;
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000255}
256
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000257bool OptionDescription::isExtern() const {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000258 return Flags & OptionDescriptionFlags::Extern;
259}
260void OptionDescription::setExtern() {
261 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000262}
263
264bool OptionDescription::isRequired() const {
265 return Flags & OptionDescriptionFlags::Required;
266}
267void OptionDescription::setRequired() {
268 Flags |= OptionDescriptionFlags::Required;
269}
270
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000271bool OptionDescription::isOneOrMore() const {
272 return Flags & OptionDescriptionFlags::OneOrMore;
273}
274void OptionDescription::setOneOrMore() {
275 Flags |= OptionDescriptionFlags::OneOrMore;
276}
277
278bool OptionDescription::isZeroOrOne() const {
279 return Flags & OptionDescriptionFlags::ZeroOrOne;
280}
281void OptionDescription::setZeroOrOne() {
282 Flags |= OptionDescriptionFlags::ZeroOrOne;
283}
284
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000285bool OptionDescription::isHidden() const {
286 return Flags & OptionDescriptionFlags::Hidden;
287}
288void OptionDescription::setHidden() {
289 Flags |= OptionDescriptionFlags::Hidden;
290}
291
292bool OptionDescription::isReallyHidden() const {
293 return Flags & OptionDescriptionFlags::ReallyHidden;
294}
295void OptionDescription::setReallyHidden() {
296 Flags |= OptionDescriptionFlags::ReallyHidden;
297}
298
299const char* OptionDescription::GenTypeDeclaration() const {
300 switch (Type) {
301 case OptionType::Alias:
302 return "cl::alias";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000303 case OptionType::PrefixList:
304 case OptionType::ParameterList:
305 return "cl::list<std::string>";
306 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000307 return "cl::opt<bool>";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000308 case OptionType::Parameter:
309 case OptionType::Prefix:
310 default:
311 return "cl::opt<std::string>";
312 }
313}
314
315std::string OptionDescription::GenVariableName() const {
316 const std::string& EscapedName = EscapeVariableName(Name);
317 switch (Type) {
318 case OptionType::Alias:
319 return "AutoGeneratedAlias_" + EscapedName;
320 case OptionType::PrefixList:
321 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000322 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000323 case OptionType::Switch:
324 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000325 case OptionType::Prefix:
326 case OptionType::Parameter:
327 default:
328 return "AutoGeneratedParameter_" + EscapedName;
329 }
330}
331
332/// OptionDescriptions - An OptionDescription array plus some helper
333/// functions.
334class OptionDescriptions {
335 typedef StringMap<OptionDescription> container_type;
336
337 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000338 container_type Descriptions;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000339
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000340public:
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000341 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000342 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000343
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000344 /// insertDescription - Insert new OptionDescription into
345 /// OptionDescriptions list
346 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000347
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000348 // Support for STL-style iteration
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000349 typedef container_type::const_iterator const_iterator;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000350 const_iterator begin() const { return Descriptions.begin(); }
351 const_iterator end() const { return Descriptions.end(); }
352};
353
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000354const OptionDescription&
355OptionDescriptions::FindOption(const std::string& OptName) const
356{
357 const_iterator I = Descriptions.find(OptName);
358 if (I != Descriptions.end())
359 return I->second;
360 else
361 throw OptName + ": no such option!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000362}
363
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000364void OptionDescriptions::InsertDescription (const OptionDescription& o)
365{
366 container_type::iterator I = Descriptions.find(o.Name);
367 if (I != Descriptions.end()) {
368 OptionDescription& D = I->second;
369 D.Merge(o);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000370 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000371 else {
372 Descriptions[o.Name] = o;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000373 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000374}
375
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000376/// HandlerTable - A base class for function objects implemented as
377/// 'tables of handlers'.
378template <class T>
379class HandlerTable {
380protected:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000381 // Implementation details.
382
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000383 /// Handler -
384 typedef void (T::* Handler) (const DagInit*);
385 /// HandlerMap - A map from property names to property handlers
386 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000387
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000388 static HandlerMap Handlers_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000389 static bool staticMembersInitialized_;
390
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000391 T* childPtr;
392public:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000393
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000394 HandlerTable(T* cp) : childPtr(cp)
395 {}
396
397 /// operator() - Just forwards to the corresponding property
398 /// handler.
399 void operator() (Init* i) {
400 const DagInit& property = InitPtrToDag(i);
401 const std::string& property_name = property.getOperator()->getAsString();
402 typename HandlerMap::iterator method = Handlers_.find(property_name);
403
404 if (method != Handlers_.end()) {
405 Handler h = method->second;
406 (childPtr->*h)(&property);
407 }
408 else {
409 throw "No handler found for property " + property_name + "!";
410 }
411 }
412
413 void AddHandler(const char* Property, Handler Handl) {
414 Handlers_[Property] = Handl;
415 }
416};
417
418template <class T> typename HandlerTable<T>::HandlerMap
419HandlerTable<T>::Handlers_;
420template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
421
422
423/// CollectOptionProperties - Function object for iterating over an
424/// option property list.
425class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
426private:
427
428 /// optDescs_ - OptionDescriptions table. This is where the
429 /// information is stored.
430 OptionDescription& optDesc_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000431
432public:
433
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000434 explicit CollectOptionProperties(OptionDescription& OD)
435 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000436 {
437 if (!staticMembersInitialized_) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000438 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000439 AddHandler("help", &CollectOptionProperties::onHelp);
440 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000441 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
442 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000443 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
444 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000445 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000446
447 staticMembersInitialized_ = true;
448 }
449 }
450
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000451private:
452
453 /// Option property handlers --
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000454 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000455
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000456 void onExtern (const DagInit* d) {
457 checkNumberOfArguments(d, 0);
458 optDesc_.setExtern();
459 }
460
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000461 void onHelp (const DagInit* d) {
462 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000463 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000464 }
465
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000466 void onHidden (const DagInit* d) {
467 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000468 optDesc_.setHidden();
469 }
470
471 void onReallyHidden (const DagInit* d) {
472 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000473 optDesc_.setReallyHidden();
474 }
475
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000476 void onRequired (const DagInit* d) {
477 checkNumberOfArguments(d, 0);
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000478 if (optDesc_.isOneOrMore())
479 throw std::string("An option can't have both (required) "
480 "and (one_or_more) properties!");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000481 optDesc_.setRequired();
482 }
483
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000484 void onOneOrMore (const DagInit* d) {
485 checkNumberOfArguments(d, 0);
486 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
487 throw std::string("Only one of (required), (zero_or_one) or "
488 "(one_or_more) properties is allowed!");
489 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbard4287062009-07-03 00:10:29 +0000490 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000491 "on a non-list option will have no effect.\n";
492 optDesc_.setOneOrMore();
493 }
494
495 void onZeroOrOne (const DagInit* d) {
496 checkNumberOfArguments(d, 0);
497 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
498 throw std::string("Only one of (required), (zero_or_one) or "
499 "(one_or_more) properties is allowed!");
500 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbard4287062009-07-03 00:10:29 +0000501 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000502 "on a non-list option will have no effect.\n";
503 optDesc_.setZeroOrOne();
504 }
505
506 void onMultiVal (const DagInit* d) {
507 checkNumberOfArguments(d, 1);
508 int val = InitPtrToInt(d->getArg(0));
509 if (val < 2)
510 throw std::string("Error in the 'multi_val' property: "
511 "the value must be greater than 1!");
512 if (!OptionType::IsList(optDesc_.Type))
513 throw std::string("The multi_val property is valid only "
514 "on list options!");
515 optDesc_.MultiVal = val;
516 }
517
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000518};
519
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000520/// AddOption - A function object that is applied to every option
521/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000522class AddOption {
523private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000524 OptionDescriptions& OptDescs_;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000525
526public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000527 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000528 {}
529
530 void operator()(const Init* i) {
531 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000532 checkNumberOfArguments(&d, 1);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000533
534 const OptionType::OptionType Type =
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000535 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000536 const std::string& Name = InitPtrToString(d.getArg(0));
537
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000538 OptionDescription OD(Type, Name);
539
540 if (!OD.isExtern())
541 checkNumberOfArguments(&d, 2);
542
543 if (OD.isAlias()) {
544 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000545 OD.Help = InitPtrToString(d.getArg(1));
546 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000547 else if (!OD.isExtern()) {
548 processOptionProperties(&d, OD);
549 }
550 OptDescs_.InsertDescription(OD);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000551 }
552
553private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000554 /// processOptionProperties - Go through the list of option
555 /// properties and call a corresponding handler for each.
556 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
557 checkNumberOfArguments(d, 2);
558 DagInit::const_arg_iterator B = d->arg_begin();
559 // Skip the first argument: it's always the option name.
560 ++B;
561 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000562 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000563
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000564};
565
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000566/// CollectOptionDescriptions - Collects option properties from all
567/// OptionLists.
568void CollectOptionDescriptions (RecordVector::const_iterator B,
569 RecordVector::const_iterator E,
570 OptionDescriptions& OptDescs)
571{
572 // For every OptionList:
573 for (; B!=E; ++B) {
574 RecordVector::value_type T = *B;
575 // Throws an exception if the value does not exist.
576 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000577
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000578 // For every option description in this list:
579 // collect the information and
580 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
581 }
582}
583
584// Tool information record
585
586namespace ToolFlags {
587 enum ToolFlags { Join = 0x1, Sink = 0x2 };
588}
589
590struct ToolDescription : public RefCountedBase<ToolDescription> {
591 std::string Name;
592 Init* CmdLine;
593 Init* Actions;
594 StrVector InLanguage;
595 std::string OutLanguage;
596 std::string OutputSuffix;
597 unsigned Flags;
598
599 // Various boolean properties
600 void setSink() { Flags |= ToolFlags::Sink; }
601 bool isSink() const { return Flags & ToolFlags::Sink; }
602 void setJoin() { Flags |= ToolFlags::Join; }
603 bool isJoin() const { return Flags & ToolFlags::Join; }
604
605 // Default ctor here is needed because StringMap can only store
606 // DefaultConstructible objects
607 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
608 ToolDescription (const std::string& n)
609 : Name(n), CmdLine(0), Actions(0), Flags(0)
610 {}
611};
612
613/// ToolDescriptions - A list of Tool information records.
614typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
615
616
617/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000618/// tool property records.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000619class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000620private:
621
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000622 /// toolDesc_ - Properties of the current Tool. This is where the
623 /// information is stored.
624 ToolDescription& toolDesc_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000625
626public:
627
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000628 explicit CollectToolProperties (ToolDescription& d)
629 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000630 {
631 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000632
633 AddHandler("actions", &CollectToolProperties::onActions);
634 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
635 AddHandler("in_language", &CollectToolProperties::onInLanguage);
636 AddHandler("join", &CollectToolProperties::onJoin);
637 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
638 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
639 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000640
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000641 staticMembersInitialized_ = true;
642 }
643 }
644
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000645private:
646
647 /// Property handlers --
648 /// Functions that extract information about tool properties from
649 /// DAG representation.
650
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000651 void onActions (const DagInit* d) {
652 checkNumberOfArguments(d, 1);
Mikhail Glushenkov8d489a82008-12-07 16:45:12 +0000653 Init* Case = d->getArg(0);
654 if (typeid(*Case) != typeid(DagInit) ||
655 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
656 throw
657 std::string("The argument to (actions) should be a 'case' construct!");
658 toolDesc_.Actions = Case;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000659 }
660
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000661 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000662 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000663 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000664 }
665
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000666 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000667 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000668 Init* arg = d->getArg(0);
669
670 // Find out the argument's type.
671 if (typeid(*arg) == typeid(StringInit)) {
672 // It's a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000673 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000674 }
675 else {
676 // It's a list.
677 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000678 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000679
680 // Copy strings to the output vector.
681 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
682 B != E; ++B) {
683 out.push_back(InitPtrToString(*B));
684 }
685
686 // Remove duplicates.
687 std::sort(out.begin(), out.end());
688 StrVector::iterator newE = std::unique(out.begin(), out.end());
689 out.erase(newE, out.end());
690 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000691 }
692
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000693 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000694 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000695 toolDesc_.setJoin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000696 }
697
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000698 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000699 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000700 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000701 }
702
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000703 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000704 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000705 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000706 }
707
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000708 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000709 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000710 toolDesc_.setSink();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000711 }
712
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000713};
714
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000715/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000716/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000717/// CollectToolProperties function object).
718void CollectToolDescriptions (RecordVector::const_iterator B,
719 RecordVector::const_iterator E,
720 ToolDescriptions& ToolDescs)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000721{
722 // Iterate over a properties list of every Tool definition
723 for (;B!=E;++B) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +0000724 const Record* T = *B;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000725 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000726 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000727
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000728 IntrusiveRefCntPtr<ToolDescription>
729 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000730
731 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000732 CollectToolProperties(*ToolDesc));
733 ToolDescs.push_back(ToolDesc);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000734 }
735}
736
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000737/// FillInEdgeVector - Merge all compilation graph definitions into
738/// one single edge list.
739void FillInEdgeVector(RecordVector::const_iterator B,
740 RecordVector::const_iterator E, RecordVector& Out) {
741 for (; B != E; ++B) {
742 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000743
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000744 for (unsigned i = 0; i < edges->size(); ++i)
745 Out.push_back(edges->getElementAsRecord(i));
746 }
747}
748
749/// CalculatePriority - Calculate the priority of this plugin.
750int CalculatePriority(RecordVector::const_iterator B,
751 RecordVector::const_iterator E) {
752 int total = 0;
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +0000753 for (; B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000754 total += static_cast<int>((*B)->getValueAsInt("priority"));
755 }
756 return total;
757}
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000758
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000759/// NotInGraph - Helper function object for FilterNotInGraph.
760struct NotInGraph {
761private:
762 const llvm::StringSet<>& ToolsInGraph_;
763
764public:
765 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
766 : ToolsInGraph_(ToolsInGraph)
767 {}
768
769 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
770 return (ToolsInGraph_.count(x->Name) == 0);
771 }
772};
773
774/// FilterNotInGraph - Filter out from ToolDescs all Tools not
775/// mentioned in the compilation graph definition.
776void FilterNotInGraph (const RecordVector& EdgeVector,
777 ToolDescriptions& ToolDescs) {
778
779 // List all tools mentioned in the graph.
780 llvm::StringSet<> ToolsInGraph;
781
782 for (RecordVector::const_iterator B = EdgeVector.begin(),
783 E = EdgeVector.end(); B != E; ++B) {
784
785 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000786 const std::string& NodeA = Edge->getValueAsString("a");
787 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000788
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000789 if (NodeA != "root")
790 ToolsInGraph.insert(NodeA);
791 ToolsInGraph.insert(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000792 }
793
794 // Filter ToolPropertiesList.
795 ToolDescriptions::iterator new_end =
796 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
797 NotInGraph(ToolsInGraph));
798 ToolDescs.erase(new_end, ToolDescs.end());
799}
800
801/// FillInToolToLang - Fills in two tables that map tool names to
802/// (input, output) languages. Helper function used by TypecheckGraph().
803void FillInToolToLang (const ToolDescriptions& ToolDescs,
804 StringMap<StringSet<> >& ToolToInLang,
805 StringMap<std::string>& ToolToOutLang) {
806 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
807 E = ToolDescs.end(); B != E; ++B) {
808 const ToolDescription& D = *(*B);
809 for (StrVector::const_iterator B = D.InLanguage.begin(),
810 E = D.InLanguage.end(); B != E; ++B)
811 ToolToInLang[D.Name].insert(*B);
812 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000813 }
814}
815
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000816/// TypecheckGraph - Check that names for output and input languages
817/// on all edges do match. This doesn't do much when the information
818/// about the whole graph is not available (i.e. when compiling most
819/// plugins).
820void TypecheckGraph (const RecordVector& EdgeVector,
821 const ToolDescriptions& ToolDescs) {
822 StringMap<StringSet<> > ToolToInLang;
823 StringMap<std::string> ToolToOutLang;
824
825 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
826 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
827 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
828
829 for (RecordVector::const_iterator B = EdgeVector.begin(),
830 E = EdgeVector.end(); B != E; ++B) {
831 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000832 const std::string& NodeA = Edge->getValueAsString("a");
833 const std::string& NodeB = Edge->getValueAsString("b");
834 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
835 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000836
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000837 if (NodeA != "root") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000838 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000839 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000840 + ": output->input language mismatch";
841 }
842
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000843 if (NodeB == "root")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000844 throw std::string("Edges back to the root are not allowed!");
845 }
846}
847
848/// WalkCase - Walks the 'case' expression DAG and invokes
849/// TestCallback on every test, and StatementCallback on every
850/// statement. Handles 'case' nesting, but not the 'and' and 'or'
851/// combinators.
852// TODO: Re-implement EmitCaseConstructHandler on top of this function?
853template <typename F1, typename F2>
854void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
855 const DagInit& d = InitPtrToDag(Case);
856 bool even = false;
857 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
858 B != E; ++B) {
859 Init* arg = *B;
860 if (even && dynamic_cast<DagInit*>(arg)
861 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
862 WalkCase(arg, TestCallback, StatementCallback);
863 else if (!even)
864 TestCallback(arg);
865 else
866 StatementCallback(arg);
867 even = !even;
868 }
869}
870
871/// ExtractOptionNames - A helper function object used by
872/// CheckForSuperfluousOptions() to walk the 'case' DAG.
873class ExtractOptionNames {
874 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000875
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000876 void processDag(const Init* Statement) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000877 const DagInit& Stmt = InitPtrToDag(Statement);
878 const std::string& ActionName = Stmt.getOperator()->getAsString();
879 if (ActionName == "forward" || ActionName == "forward_as" ||
880 ActionName == "unpack_values" || ActionName == "switch_on" ||
881 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000882 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000883 checkNumberOfArguments(&Stmt, 1);
884 const std::string& Name = InitPtrToString(Stmt.getArg(0));
885 OptionNames_.insert(Name);
886 }
887 else if (ActionName == "and" || ActionName == "or") {
888 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000889 this->processDag(Stmt.getArg(i));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000890 }
891 }
892 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000893
894public:
895 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
896 {}
897
898 void operator()(const Init* Statement) {
899 if (typeid(*Statement) == typeid(ListInit)) {
900 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
901 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
902 B != E; ++B)
903 this->processDag(*B);
904 }
905 else {
906 this->processDag(Statement);
907 }
908 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000909};
910
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000911/// CheckForSuperfluousOptions - Check that there are no side
912/// effect-free options (specified only in the OptionList). Otherwise,
913/// output a warning.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000914void CheckForSuperfluousOptions (const RecordVector& Edges,
915 const ToolDescriptions& ToolDescs,
916 const OptionDescriptions& OptDescs) {
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000917 llvm::StringSet<> nonSuperfluousOptions;
918
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000919 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000920 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000921 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
922 E = ToolDescs.end(); B != E; ++B) {
923 const ToolDescription& TD = *(*B);
924 ExtractOptionNames Callback(nonSuperfluousOptions);
925 if (TD.Actions)
926 WalkCase(TD.Actions, Callback, Callback);
927 }
928
929 // Add all options mentioned in the 'case' clauses of the
930 // OptionalEdges of the compilation graph to the set of
931 // non-superfluous options.
932 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
933 B != E; ++B) {
934 const Record* Edge = *B;
935 DagInit* Weight = Edge->getValueAsDag("weight");
936
937 if (!isDagEmpty(Weight))
938 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000939 }
940
941 // Check that all options in OptDescs belong to the set of
942 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000943 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000944 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000945 const OptionDescription& Val = B->second;
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000946 if (!nonSuperfluousOptions.count(Val.Name)
947 && Val.Type != OptionType::Alias)
Daniel Dunbard4287062009-07-03 00:10:29 +0000948 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000949 "Probable cause: this option is specified only in the OptionList.\n";
950 }
951}
952
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000953/// EmitCaseTest1Arg - Helper function used by
954/// EmitCaseConstructHandler.
955bool EmitCaseTest1Arg(const std::string& TestName,
956 const DagInit& d,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000957 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +0000958 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000959 checkNumberOfArguments(&d, 1);
960 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000961
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000962 if (TestName == "switch_on") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000963 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
964 if (!OptionType::IsSwitch(OptDesc.Type))
965 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000966 O << OptDesc.GenVariableName();
967 return true;
968 } else if (TestName == "input_languages_contain") {
969 O << "InLangs.count(\"" << OptName << "\") != 0";
970 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000971 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000972 // This works only for single-argument Tool::GenerateAction. Join
973 // tools can process several files in different languages simultaneously.
974
975 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000976 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000977 return true;
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000978 } else if (TestName == "not_empty" || TestName == "empty") {
979 const char* Test = (TestName == "empty") ? "" : "!";
980
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000981 if (OptName == "o") {
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000982 O << Test << "OutputFilename.empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000983 return true;
984 }
985 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000986 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
987 if (OptionType::IsSwitch(OptDesc.Type))
988 throw OptName
989 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000990 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000991 return true;
992 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000993 }
994
995 return false;
996}
997
998/// EmitCaseTest2Args - Helper function used by
999/// EmitCaseConstructHandler.
1000bool EmitCaseTest2Args(const std::string& TestName,
1001 const DagInit& d,
1002 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001003 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001004 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001005 checkNumberOfArguments(&d, 2);
1006 const std::string& OptName = InitPtrToString(d.getArg(0));
1007 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001008 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001009
1010 if (TestName == "parameter_equals") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001011 if (!OptionType::IsParameter(OptDesc.Type))
1012 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001013 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1014 return true;
1015 }
1016 else if (TestName == "element_in_list") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001017 if (!OptionType::IsList(OptDesc.Type))
1018 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001019 const std::string& VarName = OptDesc.GenVariableName();
1020 O << "std::find(" << VarName << ".begin(),\n"
1021 << IndentLevel << Indent1 << VarName << ".end(), \""
1022 << OptArg << "\") != " << VarName << ".end()";
1023 return true;
1024 }
1025
1026 return false;
1027}
1028
1029// Forward declaration.
1030// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1031void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001032 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001033 raw_ostream& O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001034
1035/// EmitLogicalOperationTest - Helper function used by
1036/// EmitCaseConstructHandler.
1037void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1038 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001039 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001040 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001041 O << '(';
1042 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001043 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001044 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1045 if (j != NumArgs - 1)
1046 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
1047 else
1048 O << ')';
1049 }
1050}
1051
1052/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1053void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001054 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001055 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001056 const std::string& TestName = d.getOperator()->getAsString();
1057
1058 if (TestName == "and")
1059 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1060 else if (TestName == "or")
1061 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
1062 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1063 return;
1064 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1065 return;
1066 else
1067 throw TestName + ": unknown edge property!";
1068}
1069
1070// Emit code that handles the 'case' construct.
1071// Takes a function object that should emit code for every case clause.
1072// Callback's type is
Daniel Dunbard4287062009-07-03 00:10:29 +00001073// void F(Init* Statement, const char* IndentLevel, raw_ostream& O).
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001074template <typename F>
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001075void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +00001076 F Callback, bool EmitElseIf,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001077 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001078 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001079 const DagInit* d = &InitPtrToDag(Dag);
1080 if (d->getOperator()->getAsString() != "case")
1081 throw std::string("EmitCaseConstructHandler should be invoked"
1082 " only on 'case' expressions!");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001083
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001084 unsigned numArgs = d->getNumArgs();
1085 if (d->getNumArgs() < 2)
1086 throw "There should be at least one clause in the 'case' expression:\n"
1087 + d->getAsString();
1088
1089 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001090 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001091
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001092 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001093 if (Test.getOperator()->getAsString() == "default") {
1094 if (i+2 != numArgs)
1095 throw std::string("The 'default' clause should be the last in the"
1096 "'case' construct!");
1097 O << IndentLevel << "else {\n";
1098 }
1099 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001100 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001101 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1102 O << ") {\n";
1103 }
1104
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001105 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001106 ++i;
1107 if (i == numArgs)
1108 throw "Case construct handler: no corresponding action "
1109 "found for the test " + Test.getAsString() + '!';
1110
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001111 Init* arg = d->getArg(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001112 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1113 if (nd && (nd->getOperator()->getAsString() == "case")) {
1114 // Handle the nested 'case'.
1115 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001116 Callback, EmitElseIf, OptDescs, O);
1117 }
1118 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001119 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001120 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001121 O << IndentLevel << "}\n";
1122 }
1123}
1124
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001125/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1126/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1127/// Helper function used by EmitCmdLineVecFill and.
1128void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1129 const char* Delimiters = " \t\n\v\f\r";
1130 enum TokenizerState
1131 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1132 cur_st = Normal;
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001133
1134 if (CmdLine.empty())
1135 return;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001136 Out.push_back("");
1137
1138 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1139 E = CmdLine.size();
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001140
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001141 for (; B != E; ++B) {
1142 char cur_ch = CmdLine[B];
1143
1144 switch (cur_st) {
1145 case Normal:
1146 if (cur_ch == '$') {
1147 cur_st = SpecialCommand;
1148 break;
1149 }
1150 if (oneOf(Delimiters, cur_ch)) {
1151 // Skip whitespace
1152 B = CmdLine.find_first_not_of(Delimiters, B);
1153 if (B == std::string::npos) {
1154 B = E-1;
1155 continue;
1156 }
1157 --B;
1158 Out.push_back("");
1159 continue;
1160 }
1161 break;
1162
1163
1164 case SpecialCommand:
1165 if (oneOf(Delimiters, cur_ch)) {
1166 cur_st = Normal;
1167 Out.push_back("");
1168 continue;
1169 }
1170 if (cur_ch == '(') {
1171 Out.push_back("");
1172 cur_st = InsideSpecialCommand;
1173 continue;
1174 }
1175 break;
1176
1177 case InsideSpecialCommand:
1178 if (oneOf(Delimiters, cur_ch)) {
1179 continue;
1180 }
1181 if (cur_ch == '\'') {
1182 cur_st = InsideQuotationMarks;
1183 Out.push_back("");
1184 continue;
1185 }
1186 if (cur_ch == ')') {
1187 cur_st = Normal;
1188 Out.push_back("");
1189 }
1190 if (cur_ch == ',') {
1191 continue;
1192 }
1193
1194 break;
1195
1196 case InsideQuotationMarks:
1197 if (cur_ch == '\'') {
1198 cur_st = InsideSpecialCommand;
1199 continue;
1200 }
1201 break;
1202 }
1203
1204 Out.back().push_back(cur_ch);
1205 }
1206}
1207
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001208/// SubstituteSpecialCommands - Perform string substitution for $CALL
1209/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001210StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbard4287062009-07-03 00:10:29 +00001211(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001212{
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001213
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001214 const std::string& cmd = *Pos;
1215
1216 if (cmd == "$CALL") {
1217 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1218 const std::string& CmdName = *Pos;
1219
1220 if (CmdName == ")")
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001221 throw std::string("$CALL invocation: empty argument list!");
1222
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001223 O << "hooks::";
1224 O << CmdName << "(";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001225
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001226
1227 bool firstIteration = true;
1228 while (true) {
1229 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1230 const std::string& Arg = *Pos;
1231 assert(Arg.size() != 0);
1232
1233 if (Arg[0] == ')')
1234 break;
1235
1236 if (firstIteration)
1237 firstIteration = false;
1238 else
1239 O << ", ";
1240
1241 O << '"' << Arg << '"';
1242 }
1243
1244 O << ')';
1245
1246 }
1247 else if (cmd == "$ENV") {
1248 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1249 const std::string& EnvName = *Pos;
1250
1251 if (EnvName == ")")
1252 throw "$ENV invocation: empty argument list!";
1253
1254 O << "checkCString(std::getenv(\"";
1255 O << EnvName;
1256 O << "\"))";
1257
1258 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001259 }
1260 else {
1261 throw "Unknown special command: " + cmd;
1262 }
1263
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001264 const std::string& Leftover = *Pos;
1265 assert(Leftover.at(0) == ')');
1266 if (Leftover.size() != 1)
1267 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001268
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001269 return Pos;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001270}
1271
1272/// EmitCmdLineVecFill - Emit code that fills in the command line
1273/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001274void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001275 bool IsJoin, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001276 raw_ostream& O) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001277 StrVector StrVec;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001278 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1279
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001280 if (StrVec.empty())
Mikhail Glushenkov688fbb62009-06-25 18:21:34 +00001281 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001282
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001283 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1284
1285 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001286 assert(!StrVec[0].empty());
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001287 if (StrVec[0][0] == '$') {
1288 while (I != E && (*I)[0] != ')' )
1289 ++I;
1290
1291 // Skip the ')' symbol.
1292 ++I;
1293 }
1294 else {
1295 ++I;
1296 }
1297
1298 for (; I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001299 const std::string& cmd = *I;
Mikhail Glushenkov72b128f2009-04-19 00:22:35 +00001300 assert(!cmd.empty());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001301 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001302 if (cmd.at(0) == '$') {
1303 if (cmd == "$INFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001304 if (IsJoin)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001305 O << "for (PathVector::const_iterator B = inFiles.begin()"
1306 << ", E = inFiles.end();\n"
1307 << IndentLevel << "B != E; ++B)\n"
1308 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1309 else
1310 O << "vec.push_back(inFile.toString());\n";
1311 }
1312 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001313 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001314 }
1315 else {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001316 O << "vec.push_back(";
1317 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001318 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001319 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001320 }
1321 else {
1322 O << "vec.push_back(\"" << cmd << "\");\n";
1323 }
1324 }
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001325 O << IndentLevel << "cmd = ";
1326
1327 if (StrVec[0][0] == '$')
1328 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1329 else
1330 O << '"' << StrVec[0] << '"';
1331 O << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001332}
1333
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001334/// EmitCmdLineVecFillCallback - A function object wrapper around
1335/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1336/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001337class EmitCmdLineVecFillCallback {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001338 bool IsJoin;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001339 const std::string& ToolName;
1340 public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001341 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1342 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001343
1344 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001345 raw_ostream& O) const
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001346 {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001347 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1348 IndentLevel, O);
1349 }
1350};
1351
1352/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1353/// implement EmitActionHandler. Emits code for
1354/// handling the (forward) and (forward_as) option properties.
1355void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1356 const char* Indent,
1357 const std::string& NewName,
Daniel Dunbard4287062009-07-03 00:10:29 +00001358 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001359 const std::string& Name = NewName.empty()
1360 ? ("-" + D.Name)
1361 : NewName;
1362
1363 switch (D.Type) {
1364 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001365 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1366 break;
1367 case OptionType::Parameter:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001368 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1369 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1370 break;
1371 case OptionType::Prefix:
1372 O << Indent << "vec.push_back(\"" << Name << "\" + "
1373 << D.GenVariableName() << ");\n";
1374 break;
1375 case OptionType::PrefixList:
1376 O << Indent << "for (" << D.GenTypeDeclaration()
1377 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001378 << Indent << "E = " << D.GenVariableName() << ".end(); B != E;) {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001379 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001380 << "*B);\n"
1381 << Indent << Indent1 << "++B;\n";
1382
1383 for (int i = 1, j = D.MultiVal; i < j; ++i) {
1384 O << Indent << Indent1 << "vec.push_back(*B);\n"
1385 << Indent << Indent1 << "++B;\n";
1386 }
1387
1388 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001389 break;
1390 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001391 O << Indent << "for (" << D.GenTypeDeclaration()
1392 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1393 << Indent << "E = " << D.GenVariableName()
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001394 << ".end() ; B != E;) {\n"
1395 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n";
1396
1397 for (int i = 0, j = D.MultiVal; i < j; ++i) {
1398 O << Indent << Indent1 << "vec.push_back(*B);\n"
1399 << Indent << Indent1 << "++B;\n";
1400 }
1401
1402 O << Indent << "}\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001403 break;
1404 case OptionType::Alias:
1405 default:
1406 throw std::string("Aliases are not allowed in tool option descriptions!");
1407 }
1408}
1409
1410/// EmitActionHandler - Emit code that handles actions. Used by
1411/// EmitGenerateActionMethod() as an argument to
1412/// EmitCaseConstructHandler().
1413class EmitActionHandler {
1414 const OptionDescriptions& OptDescs;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001415
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001416 void processActionDag(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001417 raw_ostream& O) const
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001418 {
1419 const DagInit& Dag = InitPtrToDag(Statement);
1420 const std::string& ActionName = Dag.getOperator()->getAsString();
1421
1422 if (ActionName == "append_cmd") {
1423 checkNumberOfArguments(&Dag, 1);
1424 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovfa8787d2009-02-27 06:46:55 +00001425 StrVector Out;
1426 llvm::SplitString(Cmd, Out);
1427
1428 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1429 B != E; ++B)
1430 O << IndentLevel << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001431 }
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001432 else if (ActionName == "error") {
1433 O << IndentLevel << "throw std::runtime_error(\"" <<
1434 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1435 : "Unknown error!")
1436 << "\");\n";
1437 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001438 else if (ActionName == "forward") {
1439 checkNumberOfArguments(&Dag, 1);
1440 const std::string& Name = InitPtrToString(Dag.getArg(0));
1441 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1442 IndentLevel, "", O);
1443 }
1444 else if (ActionName == "forward_as") {
1445 checkNumberOfArguments(&Dag, 2);
1446 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkov09699552009-05-06 01:41:19 +00001447 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001448 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1449 IndentLevel, NewName, O);
1450 }
1451 else if (ActionName == "output_suffix") {
1452 checkNumberOfArguments(&Dag, 1);
1453 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1454 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1455 }
1456 else if (ActionName == "stop_compilation") {
1457 O << IndentLevel << "stop_compilation = true;\n";
1458 }
1459 else if (ActionName == "unpack_values") {
1460 checkNumberOfArguments(&Dag, 1);
1461 const std::string& Name = InitPtrToString(Dag.getArg(0));
1462 const OptionDescription& D = OptDescs.FindOption(Name);
1463
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001464 if (D.isMultiVal())
1465 throw std::string("Can't use unpack_values with multi-valued options!");
1466
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001467 if (OptionType::IsList(D.Type)) {
1468 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1469 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1470 << IndentLevel << "E = " << D.GenVariableName()
1471 << ".end(); B != E; ++B)\n"
1472 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1473 }
1474 else if (OptionType::IsParameter(D.Type)){
1475 O << Indent3 << "llvm::SplitString("
1476 << D.GenVariableName() << ", vec, \",\");\n";
1477 }
1478 else {
1479 throw "Option '" + D.Name +
1480 "': switches can't have the 'unpack_values' property!";
1481 }
1482 }
1483 else {
1484 throw "Unknown action name: " + ActionName + "!";
1485 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001486 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001487 public:
1488 EmitActionHandler(const OptionDescriptions& OD)
1489 : OptDescs(OD) {}
1490
1491 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001492 raw_ostream& O) const
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001493 {
1494 if (typeid(*Statement) == typeid(ListInit)) {
1495 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1496 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1497 B != E; ++B)
1498 this->processActionDag(*B, IndentLevel, O);
1499 }
1500 else {
1501 this->processActionDag(Statement, IndentLevel, O);
1502 }
1503 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001504};
1505
1506// EmitGenerateActionMethod - Emit one of two versions of the
1507// Tool::GenerateAction() method.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001508void EmitGenerateActionMethod (const ToolDescription& D,
1509 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001510 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001511 if (IsJoin)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001512 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1513 else
1514 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1515
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001516 O << Indent2 << "bool HasChildren,\n"
1517 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001518 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1519 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001520 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001521 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001522 << Indent2 << "std::vector<std::string> vec;\n"
1523 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1524 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1525 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001526
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001527 // For every understood option, emit handling code.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001528 if (D.Actions)
1529 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1530 false, OptDescs, O);
1531
1532 O << '\n' << Indent2
1533 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
1534 << Indent3 << "TempDir, stop_compilation, output_suffix).toString();\n\n";
1535
1536 // cmd_line is either a string or a 'case' construct.
1537 if (!D.CmdLine)
1538 throw "Tool " + D.Name + " has no cmd_line property!";
1539 else if (typeid(*D.CmdLine) == typeid(StringInit))
1540 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1541 else
1542 EmitCaseConstructHandler(D.CmdLine, Indent2,
1543 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1544 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001545
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001546 // Handle the Sink property.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001547 if (D.isSink()) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001548 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1549 << Indent3 << "vec.insert(vec.end(), "
1550 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1551 << Indent2 << "}\n";
1552 }
1553
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001554 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001555 << Indent1 << "}\n\n";
1556}
1557
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001558/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1559/// a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001560void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1561 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001562 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001563 if (!ToolDesc.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001564 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001565 << Indent2 << "bool HasChildren,\n"
1566 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001567 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1568 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001569 << Indent1 << "{\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001570 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001571 << " is not a Join tool!\");\n"
1572 << Indent1 << "}\n\n";
1573 else
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001574 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001575
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001576 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001577}
1578
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001579/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1580/// methods for a given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001581void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001582 O << Indent1 << "const char** InputLanguages() const {\n"
1583 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001584 << Indent1 << "}\n\n";
1585
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001586 if (D.OutLanguage.empty())
1587 throw "Tool " + D.Name + " has no 'out_language' property!";
1588
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001589 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001590 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001591 << Indent1 << "}\n\n";
1592}
1593
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001594/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001595void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001596 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001597 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001598 << Indent1 << "}\n\n";
1599}
1600
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001601/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1602/// class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001603void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001604 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001605 if (D.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001606 O << Indent2 << "return true;\n";
1607 else
1608 O << Indent2 << "return false;\n";
1609 O << Indent1 << "}\n\n";
1610}
1611
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001612/// EmitStaticMemberDefinitions - Emit static member definitions for a
1613/// given Tool class.
Daniel Dunbard4287062009-07-03 00:10:29 +00001614void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001615 if (D.InLanguage.empty())
1616 throw "Tool " + D.Name + " has no 'in_language' property!";
1617
1618 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1619 for (StrVector::const_iterator B = D.InLanguage.begin(),
1620 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001621 O << '\"' << *B << "\", ";
1622 O << "0};\n\n";
1623}
1624
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001625/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001626void EmitToolClassDefinition (const ToolDescription& D,
1627 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001628 raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001629 if (D.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001630 return;
1631
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001632 // Header
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001633 O << "class " << D.Name << " : public ";
1634 if (D.isJoin())
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001635 O << "JoinTool";
1636 else
1637 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001638
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001639 O << "{\nprivate:\n"
1640 << Indent1 << "static const char* InputLanguages_[];\n\n";
1641
1642 O << "public:\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001643 EmitNameMethod(D, O);
1644 EmitInOutLanguageMethods(D, O);
1645 EmitIsJoinMethod(D, O);
1646 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001647
1648 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001649 O << "};\n";
1650
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001651 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001652
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001653}
1654
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001655/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001656/// and emit registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00001657void EmitOptionDefinitions (const OptionDescriptions& descs,
1658 bool HasSink, bool HasExterns,
Daniel Dunbard4287062009-07-03 00:10:29 +00001659 raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001660{
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001661 std::vector<OptionDescription> Aliases;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001662
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001663 // Emit static cl::Option variables.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001664 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001665 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001666 const OptionDescription& val = B->second;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001667
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001668 if (val.Type == OptionType::Alias) {
1669 Aliases.push_back(val);
1670 continue;
1671 }
1672
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001673 if (val.isExtern())
1674 O << "extern ";
1675
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001676 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001677 << val.GenVariableName();
1678
1679 if (val.isExtern()) {
1680 O << ";\n";
1681 continue;
1682 }
1683
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001684 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001685
1686 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1687 O << ", cl::Prefix";
1688
1689 if (val.isRequired()) {
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001690 if (OptionType::IsList(val.Type) && !val.isMultiVal())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001691 O << ", cl::OneOrMore";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001692 else
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001693 O << ", cl::Required";
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001694 }
1695 else if (val.isOneOrMore() && OptionType::IsList(val.Type)) {
1696 O << ", cl::OneOrMore";
1697 }
1698 else if (val.isZeroOrOne() && OptionType::IsList(val.Type)) {
1699 O << ", cl::ZeroOrOne";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001700 }
1701
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001702 if (val.isReallyHidden()) {
1703 O << ", cl::ReallyHidden";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001704 }
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +00001705 else if (val.isHidden()) {
1706 O << ", cl::Hidden";
1707 }
1708
1709 if (val.MultiVal > 1)
1710 O << ", cl::multi_val(" << val.MultiVal << ")";
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001711
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001712 if (!val.Help.empty())
1713 O << ", cl::desc(\"" << val.Help << "\")";
1714
Mikhail Glushenkovacaf35f2009-06-23 20:45:31 +00001715 O << ");\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001716 }
1717
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001718 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001719 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001720 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001721 const OptionDescription& val = *B;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001722
1723 O << val.GenTypeDeclaration() << ' '
1724 << val.GenVariableName()
1725 << "(\"" << val.Name << '\"';
1726
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001727 const OptionDescription& D = descs.FindOption(val.Help);
1728 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001729
1730 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1731 }
1732
1733 // Emit the sink option.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001734 if (HasSink)
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001735 O << (HasExterns ? "extern cl" : "cl")
1736 << "::list<std::string> " << SinkOptionName
1737 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001738
1739 O << '\n';
1740}
1741
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001742/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbard4287062009-07-03 00:10:29 +00001743void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001744{
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001745 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001746 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001747
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001748 // Get the relevant field out of RecordKeeper
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001749 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001750
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001751 // It is allowed for a plugin to have no language map.
1752 if (LangMapRecord) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001753
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001754 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1755 if (!LangsToSuffixesList)
1756 throw std::string("Error in the language map definition!");
1757
1758 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001759 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001760
1761 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1762 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1763
1764 for (unsigned i = 0; i < Suffixes->size(); ++i)
1765 O << Indent1 << "langMap[\""
1766 << InitPtrToString(Suffixes->getElement(i))
1767 << "\"] = \"" << Lang << "\";\n";
1768 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001769 }
1770
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001771 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001772}
1773
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001774/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1775/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001776void IncDecWeight (const Init* i, const char* IndentLevel,
Daniel Dunbard4287062009-07-03 00:10:29 +00001777 raw_ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001778 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001779 const std::string& OpName = d.getOperator()->getAsString();
1780
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001781 if (OpName == "inc_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001782 O << IndentLevel << "ret += ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001783 }
1784 else if (OpName == "dec_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001785 O << IndentLevel << "ret -= ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001786 }
1787 else if (OpName == "error") {
1788 O << IndentLevel << "throw std::runtime_error(\"" <<
1789 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1790 : "Unknown error!")
1791 << "\");\n";
1792 return;
1793 }
1794
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001795 else
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001796 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov55c6bdf2008-12-18 04:06:58 +00001797 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001798
1799 if (d.getNumArgs() > 0)
1800 O << InitPtrToInt(d.getArg(0)) << ";\n";
1801 else
1802 O << "2;\n";
1803
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001804}
1805
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001806/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001807void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001808 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001809 raw_ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001810
1811 // Class constructor.
1812 O << "class Edge" << N << ": public Edge {\n"
1813 << "public:\n"
1814 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1815 << "\") {}\n\n"
1816
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001817 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001818 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001819 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001820
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001821 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001822 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001823
1824 O << Indent2 << "return ret;\n"
1825 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001826}
1827
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001828/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001829void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001830 const OptionDescriptions& OptDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001831 raw_ostream& O) {
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001832 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001833 for (RecordVector::const_iterator B = EdgeVector.begin(),
1834 E = EdgeVector.end(); B != E; ++B) {
1835 const Record* Edge = *B;
1836 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001837 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001838
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001839 if (!isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001840 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001841 ++i;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001842 }
1843}
1844
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001845/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1846/// function.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001847void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001848 const ToolDescriptions& ToolDescs,
Daniel Dunbard4287062009-07-03 00:10:29 +00001849 raw_ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001850{
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001851 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001852
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001853 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1854 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001855 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001856
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001857 O << '\n';
1858
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001859 // Insert edges.
1860
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001861 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001862 for (RecordVector::const_iterator B = EdgeVector.begin(),
1863 E = EdgeVector.end(); B != E; ++B) {
1864 const Record* Edge = *B;
1865 const std::string& NodeA = Edge->getValueAsString("a");
1866 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001867 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001868
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001869 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001870
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001871 if (isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001872 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001873 else
1874 O << "new Edge" << i << "()";
1875
1876 O << ");\n";
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001877 ++i;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001878 }
1879
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001880 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001881}
1882
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001883/// ExtractHookNames - Extract the hook names from all instances of
1884/// $CALL(HookName) in the provided command line string. Helper
1885/// function used by FillInHookNames().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001886class ExtractHookNames {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001887 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001888public:
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001889 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
1890 : HookNames_(HookNames) {}
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001891
1892 void operator()(const Init* CmdLine) {
1893 StrVector cmds;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001894 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001895 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1896 B != E; ++B) {
1897 const std::string& cmd = *B;
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001898
1899 if (cmd == "$CALL") {
1900 unsigned NumArgs = 0;
1901 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
1902 const std::string& HookName = *B;
1903
1904
1905 if (HookName.at(0) == ')')
1906 throw "$CALL invoked with no arguments!";
1907
1908 while (++B != E && B->at(0) != ')') {
1909 ++NumArgs;
1910 }
1911
1912 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
1913
1914 if (H != HookNames_.end() && H->second != NumArgs)
1915 throw "Overloading of hooks is not allowed. Overloaded hook: "
1916 + HookName;
1917 else
1918 HookNames_[HookName] = NumArgs;
1919
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001920 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001921 }
1922 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001923};
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001924
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001925/// FillInHookNames - Actually extract the hook names from all command
1926/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001927void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001928 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001929{
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001930 // For all command lines:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001931 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1932 E = ToolDescs.end(); B != E; ++B) {
1933 const ToolDescription& D = *(*B);
1934 if (!D.CmdLine)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001935 continue;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001936 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001937 // This is a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001938 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001939 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001940 // This is a 'case' construct.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001941 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001942 }
1943}
1944
1945/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1946/// property records and emit hook function declaration for each
1947/// instance of $CALL(HookName).
Daniel Dunbard4287062009-07-03 00:10:29 +00001948void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001949 llvm::StringMap<unsigned> HookNames;
1950
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001951 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001952 if (HookNames.empty())
1953 return;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001954
1955 O << "namespace hooks {\n";
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001956 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
1957 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkoved765fe2009-01-21 13:04:33 +00001958 O << Indent1 << "std::string " << B->first() << "(";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001959
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +00001960 for (unsigned i = 0, j = B->second; i < j; ++i) {
1961 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
1962 }
1963
1964 O <<");\n";
1965 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001966 O << "}\n\n";
1967}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001968
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001969/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbard4287062009-07-03 00:10:29 +00001970void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001971 O << "struct Plugin : public llvmc::BasePlugin {\n\n"
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001972 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001973 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
1974 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
1975 << Indent1
1976 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
1977 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
1978 << "};\n\n"
1979
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001980 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001981}
1982
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001983/// EmitIncludes - Emit necessary #include directives and some
1984/// additional declarations.
Daniel Dunbard4287062009-07-03 00:10:29 +00001985void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001986 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00001987 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001988 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1989 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001990
1991 << "#include \"llvm/ADT/StringExtras.h\"\n"
1992 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1993
1994 << "#include <cstdlib>\n"
1995 << "#include <stdexcept>\n\n"
1996
1997 << "using namespace llvm;\n"
1998 << "using namespace llvmc;\n\n"
1999
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00002000 << "extern cl::opt<std::string> OutputFilename;\n\n"
2001
2002 << "inline const char* checkCString(const char* s)\n"
2003 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00002004}
2005
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002006
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002007/// PluginData - Holds all information about a plugin.
2008struct PluginData {
2009 OptionDescriptions OptDescs;
2010 bool HasSink;
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002011 bool HasExterns;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002012 ToolDescriptions ToolDescs;
2013 RecordVector Edges;
2014 int Priority;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002015};
2016
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002017/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002018/// there are any with the 'sink' property set.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002019bool HasSink(const ToolDescriptions& ToolDescs) {
2020 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2021 E = ToolDescs.end(); B != E; ++B)
2022 if ((*B)->isSink())
2023 return true;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002024
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002025 return false;
2026}
2027
2028/// HasExterns - Go through the list of option descriptions and check
2029/// if there are any external options.
2030bool HasExterns(const OptionDescriptions& OptDescs) {
2031 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2032 E = OptDescs.end(); B != E; ++B)
2033 if (B->second.isExtern())
2034 return true;
2035
2036 return false;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00002037}
2038
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002039/// CollectPluginData - Collect tool and option properties,
2040/// compilation graph edges and plugin priority from the parse tree.
2041void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2042 // Collect option properties.
2043 const RecordVector& OptionLists =
2044 Records.getAllDerivedDefinitions("OptionList");
2045 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2046 Data.OptDescs);
2047
2048 // Collect tool properties.
2049 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2050 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2051 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00002052 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002053
2054 // Collect compilation graph edges.
2055 const RecordVector& CompilationGraphs =
2056 Records.getAllDerivedDefinitions("CompilationGraph");
2057 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2058 Data.Edges);
2059
2060 // Calculate the priority of this plugin.
2061 const RecordVector& Priorities =
2062 Records.getAllDerivedDefinitions("PluginPriority");
2063 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00002064}
2065
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002066/// CheckPluginData - Perform some sanity checks on the collected data.
2067void CheckPluginData(PluginData& Data) {
2068 // Filter out all tools not mentioned in the compilation graph.
2069 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002070
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002071 // Typecheck the compilation graph.
2072 TypecheckGraph(Data.Edges, Data.ToolDescs);
2073
2074 // Check that there are no options without side effects (specified
2075 // only in the OptionList).
2076 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2077
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00002078}
2079
Daniel Dunbard4287062009-07-03 00:10:29 +00002080void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002081 // Emit file header.
2082 EmitIncludes(O);
2083
2084 // Emit global option registration code.
Mikhail Glushenkov087aebe2009-06-23 20:45:07 +00002085 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002086
2087 // Emit hook declarations.
2088 EmitHookDeclarations(Data.ToolDescs, O);
2089
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00002090 O << "namespace {\n\n";
2091
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002092 // Emit PopulateLanguageMap() function
2093 // (a language map maps from file extensions to language names).
2094 EmitPopulateLanguageMap(Records, O);
2095
2096 // Emit Tool classes.
2097 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2098 E = Data.ToolDescs.end(); B!=E; ++B)
2099 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2100
2101 // Emit Edge# classes.
2102 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2103
2104 // Emit PopulateCompilationGraph() function.
2105 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2106
2107 // Emit code for plugin registration.
2108 EmitRegisterPlugin(Data.Priority, O);
2109
Mikhail Glushenkovd500a272009-06-23 20:46:48 +00002110 O << "} // End anonymous namespace.\n\n";
2111
2112 // Force linkage magic.
2113 O << "namespace llvmc {\n";
2114 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2115 O << "}\n";
2116
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002117 // EOF
2118}
2119
2120
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002121// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00002122}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002123
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002124/// run - The back-end entry point.
Daniel Dunbard4287062009-07-03 00:10:29 +00002125void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002126 try {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002127 PluginData Data;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00002128
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002129 CollectPluginData(Records, Data);
2130 CheckPluginData(Data);
2131
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00002132 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00002133 EmitPluginCode(Data, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002134
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00002135 } catch (std::exception& Error) {
2136 throw Error.what() + std::string(" - usually this means a syntax error.");
2137 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002138}