blob: 8a5b36acd58b9e18bf6dbbdb44bbc0e9b79f57dc [file] [log] [blame]
Mikhail Glushenkovfb37f392008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovecbdcf22008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022#include <algorithm>
23#include <cassert>
24#include <functional>
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +000025#include <stdexcept>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <string>
Chris Lattner32a9e7a2008-06-04 04:46:14 +000027#include <typeinfo>
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +000028
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029using namespace llvm;
30
Mikhail Glushenkov895820d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000042// Indentation strings.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000043const char * Indent1 = " ";
44const char * Indent2 = " ";
45const char * Indent3 = " ";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047// Default help string.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
49
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000050// Name for the "sink" option.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000051const char * SinkOptionName = "AutoGeneratedSinkOption";
52
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkovf9152532008-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 Glushenkovb5ccfbf2008-05-30 06:10:19 +000063int InitPtrToInt(const Init* ptr) {
64 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000065 return val.getValue();
66}
67
Mikhail Glushenkovffcf3a12008-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 Glushenkovb5ccfbf2008-05-30 06:10:19 +000079 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkov29063552008-05-06 18:18:20 +000080 return val;
81}
82
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000083// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000084// greater than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000085void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +000086 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkovb7970002009-07-07 16:07:36 +000087 throw d->getOperator()->getAsString() + ": too few arguments!";
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000088}
89
Mikhail Glushenkove5557f42008-05-30 06:08:50 +000090// isDagEmpty - is this DAG marked with an empty marker?
91bool isDagEmpty (const DagInit* d) {
92 return d->getOperator()->getAsString() == "empty";
93}
Mikhail Glushenkov581936a2008-05-06 17:22:03 +000094
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000095// EscapeVariableName - Escape commas and other symbols not allowed
96// in the C++ variable names. Makes it possible to use options named
97// like "Wa," (useful for prefix options).
98std::string EscapeVariableName(const std::string& Var) {
99 std::string ret;
100 for (unsigned i = 0; i != Var.size(); ++i) {
101 char cur_char = Var[i];
102 if (cur_char == ',') {
103 ret += "_comma_";
104 }
105 else if (cur_char == '+') {
106 ret += "_plus_";
107 }
108 else if (cur_char == '-') {
109 ret += "_dash_";
110 }
111 else {
112 ret.push_back(cur_char);
113 }
114 }
115 return ret;
116}
117
Mikhail Glushenkova298bb72009-01-21 13:04:00 +0000118/// oneOf - Does the input string contain this character?
119bool oneOf(const char* lst, char c) {
120 while (*lst) {
121 if (*lst++ == c)
122 return true;
123 }
124 return false;
125}
126
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000127template <class I, class S>
128void checkedIncrement(I& P, I E, S ErrorString) {
129 ++P;
130 if (P == E)
131 throw ErrorString;
132}
133
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000134//===----------------------------------------------------------------------===//
135/// Back-end specific code
136
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000137
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000138/// OptionType - One of six different option types. See the
139/// documentation for detailed description of differences.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000140namespace OptionType {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000141
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000142 enum OptionType { Alias, Switch, Parameter, ParameterList,
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000143 Prefix, PrefixList};
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000144
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000145 bool IsList (OptionType t) {
146 return (t == ParameterList || t == PrefixList);
147 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000148
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000149 bool IsSwitch (OptionType t) {
150 return (t == Switch);
151 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000152
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000153 bool IsParameter (OptionType t) {
154 return (t == Parameter || t == Prefix);
155 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000156
Mikhail Glushenkovf9152532008-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 Glushenkovf9152532008-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 Glushenkov19d3e822009-01-28 03:47:20 +0000178 ReallyHidden = 0x4, Extern = 0x8,
179 OneOrMore = 0x10, ZeroOrOne = 0x20 };
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000180}
181
182/// OptionDescription - Represents data contained in a single
183/// OptionList entry.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000184struct OptionDescription {
185 OptionType::OptionType Type;
186 std::string Name;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000187 unsigned Flags;
188 std::string Help;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000189 unsigned MultiVal;
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000190 Init* InitVal;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000191
192 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000193 const std::string& n = "",
194 const std::string& h = DefaultHelpString)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000195 : Type(t), Name(n), Flags(0x0), Help(h), MultiVal(1), InitVal(0)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000196 {}
197
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000198 /// GenTypeDeclaration - Returns the C++ variable type of this
199 /// option.
200 const char* GenTypeDeclaration() const;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000201
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000202 /// GenVariableName - Returns the variable name used in the
203 /// generated C++ code.
204 std::string GenVariableName() const;
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000205
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000206 /// Merge - Merge two option descriptions.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000207 void Merge (const OptionDescription& other);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000208
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000209 // Misc convenient getters/setters.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000210
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000211 bool isAlias() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000212
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000213 bool isMultiVal() const;
214
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000215 bool isExtern() const;
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000216 void setExtern();
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000217
218 bool isRequired() const;
219 void setRequired();
220
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000221 bool isOneOrMore() const;
222 void setOneOrMore();
223
224 bool isZeroOrOne() const;
225 void setZeroOrOne();
226
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000227 bool isHidden() const;
228 void setHidden();
229
230 bool isReallyHidden() const;
231 void setReallyHidden();
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000232
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000233 bool isParameter() const
234 { return OptionType::IsParameter(this->Type); }
235
236 bool isSwitch() const
237 { return OptionType::IsSwitch(this->Type); }
238
239 bool isList() const
240 { return OptionType::IsList(this->Type); }
241
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242};
243
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000244void OptionDescription::Merge (const OptionDescription& other)
245{
246 if (other.Type != Type)
247 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000248
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000249 if (Help == other.Help || Help == DefaultHelpString)
250 Help = other.Help;
251 else if (other.Help != DefaultHelpString) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000252 llvm::errs() << "Warning: several different help strings"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000253 " defined for option " + Name + "\n";
254 }
255
256 Flags |= other.Flags;
257}
258
259bool OptionDescription::isAlias() const {
260 return Type == OptionType::Alias;
261}
262
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000263bool OptionDescription::isMultiVal() const {
Mikhail Glushenkov57cd67f2009-01-28 03:47:58 +0000264 return MultiVal > 1;
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000265}
266
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000267bool OptionDescription::isExtern() const {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000268 return Flags & OptionDescriptionFlags::Extern;
269}
270void OptionDescription::setExtern() {
271 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000272}
273
274bool OptionDescription::isRequired() const {
275 return Flags & OptionDescriptionFlags::Required;
276}
277void OptionDescription::setRequired() {
278 Flags |= OptionDescriptionFlags::Required;
279}
280
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000281bool OptionDescription::isOneOrMore() const {
282 return Flags & OptionDescriptionFlags::OneOrMore;
283}
284void OptionDescription::setOneOrMore() {
285 Flags |= OptionDescriptionFlags::OneOrMore;
286}
287
288bool OptionDescription::isZeroOrOne() const {
289 return Flags & OptionDescriptionFlags::ZeroOrOne;
290}
291void OptionDescription::setZeroOrOne() {
292 Flags |= OptionDescriptionFlags::ZeroOrOne;
293}
294
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000295bool OptionDescription::isHidden() const {
296 return Flags & OptionDescriptionFlags::Hidden;
297}
298void OptionDescription::setHidden() {
299 Flags |= OptionDescriptionFlags::Hidden;
300}
301
302bool OptionDescription::isReallyHidden() const {
303 return Flags & OptionDescriptionFlags::ReallyHidden;
304}
305void OptionDescription::setReallyHidden() {
306 Flags |= OptionDescriptionFlags::ReallyHidden;
307}
308
309const char* OptionDescription::GenTypeDeclaration() const {
310 switch (Type) {
311 case OptionType::Alias:
312 return "cl::alias";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000313 case OptionType::PrefixList:
314 case OptionType::ParameterList:
315 return "cl::list<std::string>";
316 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000317 return "cl::opt<bool>";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000318 case OptionType::Parameter:
319 case OptionType::Prefix:
320 default:
321 return "cl::opt<std::string>";
322 }
323}
324
325std::string OptionDescription::GenVariableName() const {
326 const std::string& EscapedName = EscapeVariableName(Name);
327 switch (Type) {
328 case OptionType::Alias:
329 return "AutoGeneratedAlias_" + EscapedName;
330 case OptionType::PrefixList:
331 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000332 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000333 case OptionType::Switch:
334 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000335 case OptionType::Prefix:
336 case OptionType::Parameter:
337 default:
338 return "AutoGeneratedParameter_" + EscapedName;
339 }
340}
341
342/// OptionDescriptions - An OptionDescription array plus some helper
343/// functions.
344class OptionDescriptions {
345 typedef StringMap<OptionDescription> container_type;
346
347 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000348 container_type Descriptions;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000349
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000350public:
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000351 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000352 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkov581936a2008-05-06 17:22:03 +0000353
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000354 /// insertDescription - Insert new OptionDescription into
355 /// OptionDescriptions list
356 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000357
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000358 // Support for STL-style iteration
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000359 typedef container_type::const_iterator const_iterator;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000360 const_iterator begin() const { return Descriptions.begin(); }
361 const_iterator end() const { return Descriptions.end(); }
362};
363
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000364const OptionDescription&
365OptionDescriptions::FindOption(const std::string& OptName) const
366{
367 const_iterator I = Descriptions.find(OptName);
368 if (I != Descriptions.end())
369 return I->second;
370 else
371 throw OptName + ": no such option!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000372}
373
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000374void OptionDescriptions::InsertDescription (const OptionDescription& o)
375{
376 container_type::iterator I = Descriptions.find(o.Name);
377 if (I != Descriptions.end()) {
378 OptionDescription& D = I->second;
379 D.Merge(o);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000380 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000381 else {
382 Descriptions[o.Name] = o;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000383 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000384}
385
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000386/// HandlerTable - A base class for function objects implemented as
387/// 'tables of handlers'.
388template <class T>
389class HandlerTable {
390protected:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000391 // Implementation details.
392
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000393 /// Handler -
394 typedef void (T::* Handler) (const DagInit*);
395 /// HandlerMap - A map from property names to property handlers
396 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000397
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000398 static HandlerMap Handlers_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000399 static bool staticMembersInitialized_;
400
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000401 T* childPtr;
402public:
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000403
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000404 HandlerTable(T* cp) : childPtr(cp)
405 {}
406
407 /// operator() - Just forwards to the corresponding property
408 /// handler.
409 void operator() (Init* i) {
410 const DagInit& property = InitPtrToDag(i);
411 const std::string& property_name = property.getOperator()->getAsString();
412 typename HandlerMap::iterator method = Handlers_.find(property_name);
413
414 if (method != Handlers_.end()) {
415 Handler h = method->second;
416 (childPtr->*h)(&property);
417 }
418 else {
419 throw "No handler found for property " + property_name + "!";
420 }
421 }
422
423 void AddHandler(const char* Property, Handler Handl) {
424 Handlers_[Property] = Handl;
425 }
426};
427
428template <class T> typename HandlerTable<T>::HandlerMap
429HandlerTable<T>::Handlers_;
430template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
431
432
433/// CollectOptionProperties - Function object for iterating over an
434/// option property list.
435class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
436private:
437
438 /// optDescs_ - OptionDescriptions table. This is where the
439 /// information is stored.
440 OptionDescription& optDesc_;
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000441
442public:
443
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000444 explicit CollectOptionProperties(OptionDescription& OD)
445 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000446 {
447 if (!staticMembersInitialized_) {
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000448 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000449 AddHandler("help", &CollectOptionProperties::onHelp);
450 AddHandler("hidden", &CollectOptionProperties::onHidden);
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000451 AddHandler("init", &CollectOptionProperties::onInit);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000452 AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
453 AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000454 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
455 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000456 AddHandler("zero_or_one", &CollectOptionProperties::onZeroOrOne);
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000457
458 staticMembersInitialized_ = true;
459 }
460 }
461
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000462private:
463
464 /// Option property handlers --
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000465 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000466
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000467 void onExtern (const DagInit* d) {
468 checkNumberOfArguments(d, 0);
469 optDesc_.setExtern();
470 }
471
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000472 void onHelp (const DagInit* d) {
473 checkNumberOfArguments(d, 1);
Mikhail Glushenkovb4ced5a2008-12-07 16:47:12 +0000474 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000475 }
476
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000477 void onHidden (const DagInit* d) {
478 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000479 optDesc_.setHidden();
480 }
481
482 void onReallyHidden (const DagInit* d) {
483 checkNumberOfArguments(d, 0);
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000484 optDesc_.setReallyHidden();
485 }
486
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000487 void onRequired (const DagInit* d) {
488 checkNumberOfArguments(d, 0);
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000489 if (optDesc_.isOneOrMore())
490 throw std::string("An option can't have both (required) "
491 "and (one_or_more) properties!");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000492 optDesc_.setRequired();
493 }
494
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +0000495 void onInit (const DagInit* d) {
496 checkNumberOfArguments(d, 1);
497 Init* i = d->getArg(0);
498 const std::string& str = i->getAsString();
499
500 bool correct = optDesc_.isParameter() && dynamic_cast<StringInit*>(i);
501 correct |= (optDesc_.isSwitch() && (str == "true" || str == "false"));
502
503 if (!correct)
504 throw std::string("Incorrect usage of the 'init' option property!");
505
506 optDesc_.InitVal = i;
507 }
508
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000509 void onOneOrMore (const DagInit* d) {
510 checkNumberOfArguments(d, 0);
511 if (optDesc_.isRequired() || optDesc_.isZeroOrOne())
512 throw std::string("Only one of (required), (zero_or_one) or "
513 "(one_or_more) properties is allowed!");
514 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000515 llvm::errs() << "Warning: specifying the 'one_or_more' property "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000516 "on a non-list option will have no effect.\n";
517 optDesc_.setOneOrMore();
518 }
519
520 void onZeroOrOne (const DagInit* d) {
521 checkNumberOfArguments(d, 0);
522 if (optDesc_.isRequired() || optDesc_.isOneOrMore())
523 throw std::string("Only one of (required), (zero_or_one) or "
524 "(one_or_more) properties is allowed!");
525 if (!OptionType::IsList(optDesc_.Type))
Daniel Dunbar1a551802009-07-03 00:10:29 +0000526 llvm::errs() << "Warning: specifying the 'zero_or_one' property"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +0000527 "on a non-list option will have no effect.\n";
528 optDesc_.setZeroOrOne();
529 }
530
531 void onMultiVal (const DagInit* d) {
532 checkNumberOfArguments(d, 1);
533 int val = InitPtrToInt(d->getArg(0));
534 if (val < 2)
535 throw std::string("Error in the 'multi_val' property: "
536 "the value must be greater than 1!");
537 if (!OptionType::IsList(optDesc_.Type))
538 throw std::string("The multi_val property is valid only "
539 "on list options!");
540 optDesc_.MultiVal = val;
541 }
542
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000543};
544
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000545/// AddOption - A function object that is applied to every option
546/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000547class AddOption {
548private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000549 OptionDescriptions& OptDescs_;
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000550
551public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000552 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000553 {}
554
555 void operator()(const Init* i) {
556 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000557 checkNumberOfArguments(&d, 1);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000558
559 const OptionType::OptionType Type =
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000560 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000561 const std::string& Name = InitPtrToString(d.getArg(0));
562
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000563 OptionDescription OD(Type, Name);
564
565 if (!OD.isExtern())
566 checkNumberOfArguments(&d, 2);
567
568 if (OD.isAlias()) {
569 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000570 OD.Help = InitPtrToString(d.getArg(1));
571 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000572 else if (!OD.isExtern()) {
573 processOptionProperties(&d, OD);
574 }
575 OptDescs_.InsertDescription(OD);
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000576 }
577
578private:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000579 /// processOptionProperties - Go through the list of option
580 /// properties and call a corresponding handler for each.
581 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
582 checkNumberOfArguments(d, 2);
583 DagInit::const_arg_iterator B = d->arg_begin();
584 // Skip the first argument: it's always the option name.
585 ++B;
586 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000587 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000588
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000589};
590
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000591/// CollectOptionDescriptions - Collects option properties from all
592/// OptionLists.
593void CollectOptionDescriptions (RecordVector::const_iterator B,
594 RecordVector::const_iterator E,
595 OptionDescriptions& OptDescs)
596{
597 // For every OptionList:
598 for (; B!=E; ++B) {
599 RecordVector::value_type T = *B;
600 // Throws an exception if the value does not exist.
601 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkov2b7bcb42008-05-30 06:27:29 +0000602
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000603 // For every option description in this list:
604 // collect the information and
605 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
606 }
607}
608
609// Tool information record
610
611namespace ToolFlags {
612 enum ToolFlags { Join = 0x1, Sink = 0x2 };
613}
614
615struct ToolDescription : public RefCountedBase<ToolDescription> {
616 std::string Name;
617 Init* CmdLine;
618 Init* Actions;
619 StrVector InLanguage;
620 std::string OutLanguage;
621 std::string OutputSuffix;
622 unsigned Flags;
623
624 // Various boolean properties
625 void setSink() { Flags |= ToolFlags::Sink; }
626 bool isSink() const { return Flags & ToolFlags::Sink; }
627 void setJoin() { Flags |= ToolFlags::Join; }
628 bool isJoin() const { return Flags & ToolFlags::Join; }
629
630 // Default ctor here is needed because StringMap can only store
631 // DefaultConstructible objects
632 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
633 ToolDescription (const std::string& n)
634 : Name(n), CmdLine(0), Actions(0), Flags(0)
635 {}
636};
637
638/// ToolDescriptions - A list of Tool information records.
639typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
640
641
642/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +0000643/// tool property records.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000644class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000645private:
646
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000647 /// toolDesc_ - Properties of the current Tool. This is where the
648 /// information is stored.
649 ToolDescription& toolDesc_;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000650
651public:
652
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000653 explicit CollectToolProperties (ToolDescription& d)
654 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000655 {
656 if (!staticMembersInitialized_) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000657
658 AddHandler("actions", &CollectToolProperties::onActions);
659 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
660 AddHandler("in_language", &CollectToolProperties::onInLanguage);
661 AddHandler("join", &CollectToolProperties::onJoin);
662 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
663 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
664 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000665
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000666 staticMembersInitialized_ = true;
667 }
668 }
669
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000670private:
671
672 /// Property handlers --
673 /// Functions that extract information about tool properties from
674 /// DAG representation.
675
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000676 void onActions (const DagInit* d) {
677 checkNumberOfArguments(d, 1);
Mikhail Glushenkovad889a72008-12-07 16:45:12 +0000678 Init* Case = d->getArg(0);
679 if (typeid(*Case) != typeid(DagInit) ||
680 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
681 throw
682 std::string("The argument to (actions) should be a 'case' construct!");
683 toolDesc_.Actions = Case;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000684 }
685
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000686 void onCmdLine (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000687 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000688 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000689 }
690
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000691 void onInLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000692 checkNumberOfArguments(d, 1);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000693 Init* arg = d->getArg(0);
694
695 // Find out the argument's type.
696 if (typeid(*arg) == typeid(StringInit)) {
697 // It's a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000698 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000699 }
700 else {
701 // It's a list.
702 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000703 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000704
705 // Copy strings to the output vector.
706 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
707 B != E; ++B) {
708 out.push_back(InitPtrToString(*B));
709 }
710
711 // Remove duplicates.
712 std::sort(out.begin(), out.end());
713 StrVector::iterator newE = std::unique(out.begin(), out.end());
714 out.erase(newE, out.end());
715 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000716 }
717
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000718 void onJoin (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000719 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000720 toolDesc_.setJoin();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000721 }
722
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000723 void onOutLanguage (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000724 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000725 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000726 }
727
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000728 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000729 checkNumberOfArguments(d, 1);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000730 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000731 }
732
Mikhail Glushenkov29063552008-05-06 18:18:20 +0000733 void onSink (const DagInit* d) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000734 checkNumberOfArguments(d, 0);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000735 toolDesc_.setSink();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000736 }
737
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000738};
739
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000740/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000741/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000742/// CollectToolProperties function object).
743void CollectToolDescriptions (RecordVector::const_iterator B,
744 RecordVector::const_iterator E,
745 ToolDescriptions& ToolDescs)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000746{
747 // Iterate over a properties list of every Tool definition
748 for (;B!=E;++B) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +0000749 const Record* T = *B;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000750 // Throws an exception if the value does not exist.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000751 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000752
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000753 IntrusiveRefCntPtr<ToolDescription>
754 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000755
756 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000757 CollectToolProperties(*ToolDesc));
758 ToolDescs.push_back(ToolDesc);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000759 }
760}
761
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000762/// FillInEdgeVector - Merge all compilation graph definitions into
763/// one single edge list.
764void FillInEdgeVector(RecordVector::const_iterator B,
765 RecordVector::const_iterator E, RecordVector& Out) {
766 for (; B != E; ++B) {
767 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkov09b51c32008-05-30 06:27:02 +0000768
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000769 for (unsigned i = 0; i < edges->size(); ++i)
770 Out.push_back(edges->getElementAsRecord(i));
771 }
772}
773
774/// CalculatePriority - Calculate the priority of this plugin.
775int CalculatePriority(RecordVector::const_iterator B,
776 RecordVector::const_iterator E) {
777 int total = 0;
Mikhail Glushenkov35fde152008-11-17 17:30:25 +0000778 for (; B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000779 total += static_cast<int>((*B)->getValueAsInt("priority"));
780 }
781 return total;
782}
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000783
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000784/// NotInGraph - Helper function object for FilterNotInGraph.
785struct NotInGraph {
786private:
787 const llvm::StringSet<>& ToolsInGraph_;
788
789public:
790 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
791 : ToolsInGraph_(ToolsInGraph)
792 {}
793
794 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
795 return (ToolsInGraph_.count(x->Name) == 0);
796 }
797};
798
799/// FilterNotInGraph - Filter out from ToolDescs all Tools not
800/// mentioned in the compilation graph definition.
801void FilterNotInGraph (const RecordVector& EdgeVector,
802 ToolDescriptions& ToolDescs) {
803
804 // List all tools mentioned in the graph.
805 llvm::StringSet<> ToolsInGraph;
806
807 for (RecordVector::const_iterator B = EdgeVector.begin(),
808 E = EdgeVector.end(); B != E; ++B) {
809
810 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000811 const std::string& NodeA = Edge->getValueAsString("a");
812 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000813
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000814 if (NodeA != "root")
815 ToolsInGraph.insert(NodeA);
816 ToolsInGraph.insert(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000817 }
818
819 // Filter ToolPropertiesList.
820 ToolDescriptions::iterator new_end =
821 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
822 NotInGraph(ToolsInGraph));
823 ToolDescs.erase(new_end, ToolDescs.end());
824}
825
826/// FillInToolToLang - Fills in two tables that map tool names to
827/// (input, output) languages. Helper function used by TypecheckGraph().
828void FillInToolToLang (const ToolDescriptions& ToolDescs,
829 StringMap<StringSet<> >& ToolToInLang,
830 StringMap<std::string>& ToolToOutLang) {
831 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
832 E = ToolDescs.end(); B != E; ++B) {
833 const ToolDescription& D = *(*B);
834 for (StrVector::const_iterator B = D.InLanguage.begin(),
835 E = D.InLanguage.end(); B != E; ++B)
836 ToolToInLang[D.Name].insert(*B);
837 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkove43228952008-05-30 06:26:08 +0000838 }
839}
840
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000841/// TypecheckGraph - Check that names for output and input languages
842/// on all edges do match. This doesn't do much when the information
843/// about the whole graph is not available (i.e. when compiling most
844/// plugins).
845void TypecheckGraph (const RecordVector& EdgeVector,
846 const ToolDescriptions& ToolDescs) {
847 StringMap<StringSet<> > ToolToInLang;
848 StringMap<std::string> ToolToOutLang;
849
850 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
851 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
852 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
853
854 for (RecordVector::const_iterator B = EdgeVector.begin(),
855 E = EdgeVector.end(); B != E; ++B) {
856 const Record* Edge = *B;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000857 const std::string& NodeA = Edge->getValueAsString("a");
858 const std::string& NodeB = Edge->getValueAsString("b");
859 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
860 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000861
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000862 if (NodeA != "root") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000863 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000864 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000865 + ": output->input language mismatch";
866 }
867
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +0000868 if (NodeB == "root")
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000869 throw std::string("Edges back to the root are not allowed!");
870 }
871}
872
873/// WalkCase - Walks the 'case' expression DAG and invokes
874/// TestCallback on every test, and StatementCallback on every
875/// statement. Handles 'case' nesting, but not the 'and' and 'or'
876/// combinators.
877// TODO: Re-implement EmitCaseConstructHandler on top of this function?
878template <typename F1, typename F2>
879void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
880 const DagInit& d = InitPtrToDag(Case);
881 bool even = false;
882 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
883 B != E; ++B) {
884 Init* arg = *B;
885 if (even && dynamic_cast<DagInit*>(arg)
886 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
887 WalkCase(arg, TestCallback, StatementCallback);
888 else if (!even)
889 TestCallback(arg);
890 else
891 StatementCallback(arg);
892 even = !even;
893 }
894}
895
896/// ExtractOptionNames - A helper function object used by
897/// CheckForSuperfluousOptions() to walk the 'case' DAG.
898class ExtractOptionNames {
899 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000900
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000901 void processDag(const Init* Statement) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000902 const DagInit& Stmt = InitPtrToDag(Statement);
903 const std::string& ActionName = Stmt.getOperator()->getAsString();
904 if (ActionName == "forward" || ActionName == "forward_as" ||
905 ActionName == "unpack_values" || ActionName == "switch_on" ||
906 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000907 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000908 checkNumberOfArguments(&Stmt, 1);
909 const std::string& Name = InitPtrToString(Stmt.getArg(0));
910 OptionNames_.insert(Name);
911 }
912 else if (ActionName == "and" || ActionName == "or") {
913 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000914 this->processDag(Stmt.getArg(i));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000915 }
916 }
917 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +0000918
919public:
920 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
921 {}
922
923 void operator()(const Init* Statement) {
924 if (typeid(*Statement) == typeid(ListInit)) {
925 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
926 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
927 B != E; ++B)
928 this->processDag(*B);
929 }
930 else {
931 this->processDag(Statement);
932 }
933 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000934};
935
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000936/// CheckForSuperfluousOptions - Check that there are no side
937/// effect-free options (specified only in the OptionList). Otherwise,
938/// output a warning.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000939void CheckForSuperfluousOptions (const RecordVector& Edges,
940 const ToolDescriptions& ToolDescs,
941 const OptionDescriptions& OptDescs) {
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000942 llvm::StringSet<> nonSuperfluousOptions;
943
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000944 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000945 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000946 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
947 E = ToolDescs.end(); B != E; ++B) {
948 const ToolDescription& TD = *(*B);
949 ExtractOptionNames Callback(nonSuperfluousOptions);
950 if (TD.Actions)
951 WalkCase(TD.Actions, Callback, Callback);
952 }
953
954 // Add all options mentioned in the 'case' clauses of the
955 // OptionalEdges of the compilation graph to the set of
956 // non-superfluous options.
957 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
958 B != E; ++B) {
959 const Record* Edge = *B;
960 DagInit* Weight = Edge->getValueAsDag("weight");
961
962 if (!isDagEmpty(Weight))
963 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000964 }
965
966 // Check that all options in OptDescs belong to the set of
967 // non-superfluous options.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000968 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000969 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000970 const OptionDescription& Val = B->second;
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000971 if (!nonSuperfluousOptions.count(Val.Name)
972 && Val.Type != OptionType::Alias)
Daniel Dunbar1a551802009-07-03 00:10:29 +0000973 llvm::errs() << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkova7d0ae32008-05-30 06:28:37 +0000974 "Probable cause: this option is specified only in the OptionList.\n";
975 }
976}
977
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000978/// EmitCaseTest1Arg - Helper function used by
979/// EmitCaseConstructHandler.
980bool EmitCaseTest1Arg(const std::string& TestName,
981 const DagInit& d,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000982 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000983 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000984 checkNumberOfArguments(&d, 1);
985 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000986
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000987 if (TestName == "switch_on") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000988 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +0000989 if (!OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000990 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +0000991 O << OptDesc.GenVariableName();
992 return true;
993 } else if (TestName == "input_languages_contain") {
994 O << "InLangs.count(\"" << OptName << "\") != 0";
995 return true;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +0000996 } else if (TestName == "in_language") {
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000997 // This works only for single-argument Tool::GenerateAction. Join
998 // tools can process several files in different languages simultaneously.
999
1000 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001001 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +00001002 return true;
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001003 } else if (TestName == "not_empty" || TestName == "empty") {
1004 const char* Test = (TestName == "empty") ? "" : "!";
1005
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001006 if (OptName == "o") {
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001007 O << Test << "OutputFilename.empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001008 return true;
1009 }
1010 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001011 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001012 if (OptDesc.isSwitch())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001013 throw OptName
1014 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001015 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkov92b8da72008-05-30 06:24:07 +00001016 return true;
1017 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001018 }
1019
1020 return false;
1021}
1022
1023/// EmitCaseTest2Args - Helper function used by
1024/// EmitCaseConstructHandler.
1025bool EmitCaseTest2Args(const std::string& TestName,
1026 const DagInit& d,
1027 const char* IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001028 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001029 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001030 checkNumberOfArguments(&d, 2);
1031 const std::string& OptName = InitPtrToString(d.getArg(0));
1032 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001033 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001034
1035 if (TestName == "parameter_equals") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001036 if (!OptDesc.isParameter())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001037 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001038 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
1039 return true;
1040 }
1041 else if (TestName == "element_in_list") {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001042 if (!OptDesc.isList())
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001043 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001044 const std::string& VarName = OptDesc.GenVariableName();
1045 O << "std::find(" << VarName << ".begin(),\n"
1046 << IndentLevel << Indent1 << VarName << ".end(), \""
1047 << OptArg << "\") != " << VarName << ".end()";
1048 return true;
1049 }
1050
1051 return false;
1052}
1053
1054// Forward declaration.
1055// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1056void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001057 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001058 raw_ostream& O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001059
1060/// EmitLogicalOperationTest - Helper function used by
1061/// EmitCaseConstructHandler.
1062void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1063 const char* IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001064 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001065 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001066 O << '(';
1067 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001068 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001069 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1070 if (j != NumArgs - 1)
1071 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
1072 else
1073 O << ')';
1074 }
1075}
1076
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001077void EmitLogicalNot(const DagInit& d, const char* IndentLevel,
1078 const OptionDescriptions& OptDescs, raw_ostream& O)
1079{
1080 checkNumberOfArguments(&d, 1);
1081 const DagInit& InnerTest = InitPtrToDag(d.getArg(0));
1082 O << "! (";
1083 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
1084 O << ")";
1085}
1086
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001087/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1088void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001089 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001090 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001091 const std::string& TestName = d.getOperator()->getAsString();
1092
1093 if (TestName == "and")
1094 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1095 else if (TestName == "or")
1096 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
Mikhail Glushenkov684a8b02009-09-10 16:21:38 +00001097 else if (TestName == "not")
1098 EmitLogicalNot(d, IndentLevel, OptDescs, O);
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001099 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
1100 return;
1101 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
1102 return;
1103 else
1104 throw TestName + ": unknown edge property!";
1105}
1106
1107// Emit code that handles the 'case' construct.
1108// Takes a function object that should emit code for every case clause.
1109// Callback's type is
Daniel Dunbar1a551802009-07-03 00:10:29 +00001110// void F(Init* Statement, const char* IndentLevel, raw_ostream& O).
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001111template <typename F>
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001112void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov310d2eb2008-05-31 13:43:21 +00001113 F Callback, bool EmitElseIf,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001114 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001115 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001116 const DagInit* d = &InitPtrToDag(Dag);
1117 if (d->getOperator()->getAsString() != "case")
1118 throw std::string("EmitCaseConstructHandler should be invoked"
1119 " only on 'case' expressions!");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001120
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001121 unsigned numArgs = d->getNumArgs();
1122 if (d->getNumArgs() < 2)
1123 throw "There should be at least one clause in the 'case' expression:\n"
1124 + d->getAsString();
1125
1126 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001127 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001128
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001129 // Emit the test.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001130 if (Test.getOperator()->getAsString() == "default") {
1131 if (i+2 != numArgs)
1132 throw std::string("The 'default' clause should be the last in the"
1133 "'case' construct!");
1134 O << IndentLevel << "else {\n";
1135 }
1136 else {
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001137 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001138 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1139 O << ") {\n";
1140 }
1141
Mikhail Glushenkovcb64f4b2008-05-30 06:15:47 +00001142 // Emit the corresponding statement.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001143 ++i;
1144 if (i == numArgs)
1145 throw "Case construct handler: no corresponding action "
1146 "found for the test " + Test.getAsString() + '!';
1147
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001148 Init* arg = d->getArg(i);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001149 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1150 if (nd && (nd->getOperator()->getAsString() == "case")) {
1151 // Handle the nested 'case'.
1152 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001153 Callback, EmitElseIf, OptDescs, O);
1154 }
1155 else {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001156 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001157 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001158 O << IndentLevel << "}\n";
1159 }
1160}
1161
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001162/// TokenizeCmdline - converts from "$CALL(HookName, 'Arg1', 'Arg2')/path" to
1163/// ["$CALL(", "HookName", "Arg1", "Arg2", ")/path"] .
1164/// Helper function used by EmitCmdLineVecFill and.
1165void TokenizeCmdline(const std::string& CmdLine, StrVector& Out) {
1166 const char* Delimiters = " \t\n\v\f\r";
1167 enum TokenizerState
1168 { Normal, SpecialCommand, InsideSpecialCommand, InsideQuotationMarks }
1169 cur_st = Normal;
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001170
1171 if (CmdLine.empty())
1172 return;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001173 Out.push_back("");
1174
1175 std::string::size_type B = CmdLine.find_first_not_of(Delimiters),
1176 E = CmdLine.size();
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001177
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001178 for (; B != E; ++B) {
1179 char cur_ch = CmdLine[B];
1180
1181 switch (cur_st) {
1182 case Normal:
1183 if (cur_ch == '$') {
1184 cur_st = SpecialCommand;
1185 break;
1186 }
1187 if (oneOf(Delimiters, cur_ch)) {
1188 // Skip whitespace
1189 B = CmdLine.find_first_not_of(Delimiters, B);
1190 if (B == std::string::npos) {
1191 B = E-1;
1192 continue;
1193 }
1194 --B;
1195 Out.push_back("");
1196 continue;
1197 }
1198 break;
1199
1200
1201 case SpecialCommand:
1202 if (oneOf(Delimiters, cur_ch)) {
1203 cur_st = Normal;
1204 Out.push_back("");
1205 continue;
1206 }
1207 if (cur_ch == '(') {
1208 Out.push_back("");
1209 cur_st = InsideSpecialCommand;
1210 continue;
1211 }
1212 break;
1213
1214 case InsideSpecialCommand:
1215 if (oneOf(Delimiters, cur_ch)) {
1216 continue;
1217 }
1218 if (cur_ch == '\'') {
1219 cur_st = InsideQuotationMarks;
1220 Out.push_back("");
1221 continue;
1222 }
1223 if (cur_ch == ')') {
1224 cur_st = Normal;
1225 Out.push_back("");
1226 }
1227 if (cur_ch == ',') {
1228 continue;
1229 }
1230
1231 break;
1232
1233 case InsideQuotationMarks:
1234 if (cur_ch == '\'') {
1235 cur_st = InsideSpecialCommand;
1236 continue;
1237 }
1238 break;
1239 }
1240
1241 Out.back().push_back(cur_ch);
1242 }
1243}
1244
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001245/// SubstituteSpecialCommands - Perform string substitution for $CALL
1246/// and $ENV. Helper function used by EmitCmdLineVecFill().
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001247StrVector::const_iterator SubstituteSpecialCommands
Daniel Dunbar1a551802009-07-03 00:10:29 +00001248(StrVector::const_iterator Pos, StrVector::const_iterator End, raw_ostream& O)
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001249{
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001250
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001251 const std::string& cmd = *Pos;
1252
1253 if (cmd == "$CALL") {
1254 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1255 const std::string& CmdName = *Pos;
1256
1257 if (CmdName == ")")
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001258 throw std::string("$CALL invocation: empty argument list!");
1259
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001260 O << "hooks::";
1261 O << CmdName << "(";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001262
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001263
1264 bool firstIteration = true;
1265 while (true) {
1266 checkedIncrement(Pos, End, "Syntax error in $CALL invocation!");
1267 const std::string& Arg = *Pos;
1268 assert(Arg.size() != 0);
1269
1270 if (Arg[0] == ')')
1271 break;
1272
1273 if (firstIteration)
1274 firstIteration = false;
1275 else
1276 O << ", ";
1277
1278 O << '"' << Arg << '"';
1279 }
1280
1281 O << ')';
1282
1283 }
1284 else if (cmd == "$ENV") {
1285 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
1286 const std::string& EnvName = *Pos;
1287
1288 if (EnvName == ")")
1289 throw "$ENV invocation: empty argument list!";
1290
1291 O << "checkCString(std::getenv(\"";
1292 O << EnvName;
1293 O << "\"))";
1294
1295 checkedIncrement(Pos, End, "Syntax error in $ENV invocation!");
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001296 }
1297 else {
1298 throw "Unknown special command: " + cmd;
1299 }
1300
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001301 const std::string& Leftover = *Pos;
1302 assert(Leftover.at(0) == ')');
1303 if (Leftover.size() != 1)
1304 O << " + std::string(\"" << (Leftover.c_str() + 1) << "\")";
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001305
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001306 return Pos;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001307}
1308
1309/// EmitCmdLineVecFill - Emit code that fills in the command line
1310/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001311void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001312 bool IsJoin, const char* IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001313 raw_ostream& O) {
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001314 StrVector StrVec;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001315 TokenizeCmdline(InitPtrToString(CmdLine), StrVec);
1316
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001317 if (StrVec.empty())
Mikhail Glushenkov7bba2332009-06-25 18:21:34 +00001318 throw "Tool '" + ToolName + "' has empty command line!";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001319
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001320 StrVector::const_iterator I = StrVec.begin(), E = StrVec.end();
1321
1322 // If there is a hook invocation on the place of the first command, skip it.
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001323 assert(!StrVec[0].empty());
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001324 if (StrVec[0][0] == '$') {
1325 while (I != E && (*I)[0] != ')' )
1326 ++I;
1327
1328 // Skip the ')' symbol.
1329 ++I;
1330 }
1331 else {
1332 ++I;
1333 }
1334
1335 for (; I != E; ++I) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001336 const std::string& cmd = *I;
Mikhail Glushenkov0941b0f2009-04-19 00:22:35 +00001337 assert(!cmd.empty());
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001338 O << IndentLevel;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001339 if (cmd.at(0) == '$') {
1340 if (cmd == "$INFILE") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001341 if (IsJoin)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001342 O << "for (PathVector::const_iterator B = inFiles.begin()"
1343 << ", E = inFiles.end();\n"
1344 << IndentLevel << "B != E; ++B)\n"
Chris Lattner74382b72009-08-23 22:45:37 +00001345 << IndentLevel << Indent1 << "vec.push_back(B->str());\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001346 else
Chris Lattner74382b72009-08-23 22:45:37 +00001347 O << "vec.push_back(inFile.str());\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001348 }
1349 else if (cmd == "$OUTFILE") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001350 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001351 }
1352 else {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001353 O << "vec.push_back(";
1354 I = SubstituteSpecialCommands(I, E, O);
Mikhail Glushenkov22424562008-05-30 06:13:29 +00001355 O << ");\n";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001356 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001357 }
1358 else {
1359 O << "vec.push_back(\"" << cmd << "\");\n";
1360 }
1361 }
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001362 O << IndentLevel << "cmd = ";
1363
1364 if (StrVec[0][0] == '$')
1365 SubstituteSpecialCommands(StrVec.begin(), StrVec.end(), O);
1366 else
1367 O << '"' << StrVec[0] << '"';
1368 O << ";\n";
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001369}
1370
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001371/// EmitCmdLineVecFillCallback - A function object wrapper around
1372/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1373/// argument to EmitCaseConstructHandler().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001374class EmitCmdLineVecFillCallback {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001375 bool IsJoin;
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001376 const std::string& ToolName;
1377 public:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001378 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1379 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001380
1381 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001382 raw_ostream& O) const
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001383 {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001384 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1385 IndentLevel, O);
1386 }
1387};
1388
1389/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1390/// implement EmitActionHandler. Emits code for
1391/// handling the (forward) and (forward_as) option properties.
1392void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1393 const char* Indent,
1394 const std::string& NewName,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001395 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001396 const std::string& Name = NewName.empty()
1397 ? ("-" + D.Name)
1398 : NewName;
1399
1400 switch (D.Type) {
1401 case OptionType::Switch:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001402 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1403 break;
1404 case OptionType::Parameter:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001405 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1406 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1407 break;
1408 case OptionType::Prefix:
1409 O << Indent << "vec.push_back(\"" << Name << "\" + "
1410 << D.GenVariableName() << ");\n";
1411 break;
1412 case OptionType::PrefixList:
1413 O << Indent << "for (" << D.GenTypeDeclaration()
1414 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001415 << Indent << "E = " << D.GenVariableName() << ".end(); B != E;) {\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001416 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001417 << "*B);\n"
1418 << Indent << Indent1 << "++B;\n";
1419
1420 for (int i = 1, j = D.MultiVal; i < j; ++i) {
1421 O << Indent << Indent1 << "vec.push_back(*B);\n"
1422 << Indent << Indent1 << "++B;\n";
1423 }
1424
1425 O << Indent << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001426 break;
1427 case OptionType::ParameterList:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001428 O << Indent << "for (" << D.GenTypeDeclaration()
1429 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1430 << Indent << "E = " << D.GenVariableName()
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001431 << ".end() ; B != E;) {\n"
1432 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n";
1433
1434 for (int i = 0, j = D.MultiVal; i < j; ++i) {
1435 O << Indent << Indent1 << "vec.push_back(*B);\n"
1436 << Indent << Indent1 << "++B;\n";
1437 }
1438
1439 O << Indent << "}\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001440 break;
1441 case OptionType::Alias:
1442 default:
1443 throw std::string("Aliases are not allowed in tool option descriptions!");
1444 }
1445}
1446
1447/// EmitActionHandler - Emit code that handles actions. Used by
1448/// EmitGenerateActionMethod() as an argument to
1449/// EmitCaseConstructHandler().
1450class EmitActionHandler {
1451 const OptionDescriptions& OptDescs;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001452
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001453 void processActionDag(const Init* Statement, const char* IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001454 raw_ostream& O) const
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001455 {
1456 const DagInit& Dag = InitPtrToDag(Statement);
1457 const std::string& ActionName = Dag.getOperator()->getAsString();
1458
1459 if (ActionName == "append_cmd") {
1460 checkNumberOfArguments(&Dag, 1);
1461 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkovc52551d2009-02-27 06:46:55 +00001462 StrVector Out;
1463 llvm::SplitString(Cmd, Out);
1464
1465 for (StrVector::const_iterator B = Out.begin(), E = Out.end();
1466 B != E; ++B)
1467 O << IndentLevel << "vec.push_back(\"" << *B << "\");\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001468 }
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001469 else if (ActionName == "error") {
1470 O << IndentLevel << "throw std::runtime_error(\"" <<
1471 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1472 : "Unknown error!")
1473 << "\");\n";
1474 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001475 else if (ActionName == "forward") {
1476 checkNumberOfArguments(&Dag, 1);
1477 const std::string& Name = InitPtrToString(Dag.getArg(0));
1478 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1479 IndentLevel, "", O);
1480 }
1481 else if (ActionName == "forward_as") {
1482 checkNumberOfArguments(&Dag, 2);
1483 const std::string& Name = InitPtrToString(Dag.getArg(0));
Mikhail Glushenkove89331b2009-05-06 01:41:19 +00001484 const std::string& NewName = InitPtrToString(Dag.getArg(1));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001485 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1486 IndentLevel, NewName, O);
1487 }
1488 else if (ActionName == "output_suffix") {
1489 checkNumberOfArguments(&Dag, 1);
1490 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1491 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1492 }
1493 else if (ActionName == "stop_compilation") {
1494 O << IndentLevel << "stop_compilation = true;\n";
1495 }
1496 else if (ActionName == "unpack_values") {
1497 checkNumberOfArguments(&Dag, 1);
1498 const std::string& Name = InitPtrToString(Dag.getArg(0));
1499 const OptionDescription& D = OptDescs.FindOption(Name);
1500
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001501 if (D.isMultiVal())
1502 throw std::string("Can't use unpack_values with multi-valued options!");
1503
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001504 if (D.isList()) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001505 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1506 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1507 << IndentLevel << "E = " << D.GenVariableName()
1508 << ".end(); B != E; ++B)\n"
1509 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1510 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001511 else if (D.isParameter()){
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001512 O << Indent3 << "llvm::SplitString("
1513 << D.GenVariableName() << ", vec, \",\");\n";
1514 }
1515 else {
1516 throw "Option '" + D.Name +
1517 "': switches can't have the 'unpack_values' property!";
1518 }
1519 }
1520 else {
1521 throw "Unknown action name: " + ActionName + "!";
1522 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001523 }
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001524 public:
1525 EmitActionHandler(const OptionDescriptions& OD)
1526 : OptDescs(OD) {}
1527
1528 void operator()(const Init* Statement, const char* IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001529 raw_ostream& O) const
Mikhail Glushenkov08509492008-12-07 16:42:22 +00001530 {
1531 if (typeid(*Statement) == typeid(ListInit)) {
1532 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1533 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1534 B != E; ++B)
1535 this->processActionDag(*B, IndentLevel, O);
1536 }
1537 else {
1538 this->processActionDag(Statement, IndentLevel, O);
1539 }
1540 }
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001541};
1542
1543// EmitGenerateActionMethod - Emit one of two versions of the
1544// Tool::GenerateAction() method.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001545void EmitGenerateActionMethod (const ToolDescription& D,
1546 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001547 bool IsJoin, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001548 if (IsJoin)
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001549 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1550 else
1551 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1552
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001553 O << Indent2 << "bool HasChildren,\n"
1554 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001555 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1556 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001557 << Indent1 << "{\n"
Mikhail Glushenkovc389e942008-11-08 19:43:32 +00001558 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001559 << Indent2 << "std::vector<std::string> vec;\n"
1560 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1561 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1562 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001563
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001564 // For every understood option, emit handling code.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001565 if (D.Actions)
1566 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1567 false, OptDescs, O);
1568
1569 O << '\n' << Indent2
1570 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
Chris Lattner74382b72009-08-23 22:45:37 +00001571 << Indent3 << "TempDir, stop_compilation, output_suffix).str();\n\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001572
1573 // cmd_line is either a string or a 'case' construct.
1574 if (!D.CmdLine)
1575 throw "Tool " + D.Name + " has no cmd_line property!";
1576 else if (typeid(*D.CmdLine) == typeid(StringInit))
1577 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1578 else
1579 EmitCaseConstructHandler(D.CmdLine, Indent2,
1580 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1581 true, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001582
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001583 // Handle the Sink property.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001584 if (D.isSink()) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001585 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1586 << Indent3 << "vec.insert(vec.end(), "
1587 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1588 << Indent2 << "}\n";
1589 }
1590
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001591 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001592 << Indent1 << "}\n\n";
1593}
1594
Mikhail Glushenkov8e7254c2008-05-09 08:27:26 +00001595/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1596/// a given Tool class.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001597void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1598 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001599 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001600 if (!ToolDesc.isJoin())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001601 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001602 << Indent2 << "bool HasChildren,\n"
1603 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +00001604 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1605 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001606 << Indent1 << "{\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001607 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001608 << " is not a Join tool!\");\n"
1609 << Indent1 << "}\n\n";
1610 else
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001611 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001612
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001613 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001614}
1615
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001616/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1617/// methods for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001618void EmitInOutLanguageMethods (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001619 O << Indent1 << "const char** InputLanguages() const {\n"
1620 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001621 << Indent1 << "}\n\n";
1622
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001623 if (D.OutLanguage.empty())
1624 throw "Tool " + D.Name + " has no 'out_language' property!";
1625
Mikhail Glushenkovb96cb602008-05-06 17:24:26 +00001626 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001627 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001628 << Indent1 << "}\n\n";
1629}
1630
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001631/// EmitNameMethod - Emit the Name() method for a given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001632void EmitNameMethod (const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovb96cb602008-05-06 17:24:26 +00001633 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001634 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001635 << Indent1 << "}\n\n";
1636}
1637
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001638/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1639/// class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001640void EmitIsJoinMethod (const ToolDescription& D, raw_ostream& O) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001641 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001642 if (D.isJoin())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001643 O << Indent2 << "return true;\n";
1644 else
1645 O << Indent2 << "return false;\n";
1646 O << Indent1 << "}\n\n";
1647}
1648
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001649/// EmitStaticMemberDefinitions - Emit static member definitions for a
1650/// given Tool class.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001651void EmitStaticMemberDefinitions(const ToolDescription& D, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001652 if (D.InLanguage.empty())
1653 throw "Tool " + D.Name + " has no 'in_language' property!";
1654
1655 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1656 for (StrVector::const_iterator B = D.InLanguage.begin(),
1657 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001658 O << '\"' << *B << "\", ";
1659 O << "0};\n\n";
1660}
1661
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001662/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001663void EmitToolClassDefinition (const ToolDescription& D,
1664 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001665 raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001666 if (D.Name == "root")
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001667 return;
1668
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001669 // Header
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001670 O << "class " << D.Name << " : public ";
1671 if (D.isJoin())
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +00001672 O << "JoinTool";
1673 else
1674 O << "Tool";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001675
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001676 O << "{\nprivate:\n"
1677 << Indent1 << "static const char* InputLanguages_[];\n\n";
1678
1679 O << "public:\n";
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001680 EmitNameMethod(D, O);
1681 EmitInOutLanguageMethods(D, O);
1682 EmitIsJoinMethod(D, O);
1683 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001684
1685 // Close class definition
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001686 O << "};\n";
1687
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001688 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +00001689
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001690}
1691
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001692/// EmitOptionDefinitions - Iterate over a list of option descriptions
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001693/// and emit registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00001694void EmitOptionDefinitions (const OptionDescriptions& descs,
1695 bool HasSink, bool HasExterns,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001696 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001697{
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001698 std::vector<OptionDescription> Aliases;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001699
Mikhail Glushenkov34f37622008-05-30 06:23:29 +00001700 // Emit static cl::Option variables.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001701 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001702 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001703 const OptionDescription& val = B->second;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001704
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001705 if (val.Type == OptionType::Alias) {
1706 Aliases.push_back(val);
1707 continue;
1708 }
1709
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001710 if (val.isExtern())
1711 O << "extern ";
1712
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001713 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001714 << val.GenVariableName();
1715
1716 if (val.isExtern()) {
1717 O << ";\n";
1718 continue;
1719 }
1720
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001721 O << "(\"" << val.Name << "\"\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001722
1723 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1724 O << ", cl::Prefix";
1725
1726 if (val.isRequired()) {
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001727 if (val.isList() && !val.isMultiVal())
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001728 O << ", cl::OneOrMore";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001729 else
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001730 O << ", cl::Required";
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001731 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001732 else if (val.isOneOrMore() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001733 O << ", cl::OneOrMore";
1734 }
Mikhail Glushenkovcbc360d2009-07-07 16:08:11 +00001735 else if (val.isZeroOrOne() && val.isList()) {
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001736 O << ", cl::ZeroOrOne";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001737 }
1738
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001739 if (val.isReallyHidden()) {
1740 O << ", cl::ReallyHidden";
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001741 }
Mikhail Glushenkov19d3e822009-01-28 03:47:20 +00001742 else if (val.isHidden()) {
1743 O << ", cl::Hidden";
1744 }
1745
1746 if (val.MultiVal > 1)
Mikhail Glushenkov8fe44472009-07-07 16:08:41 +00001747 O << ", cl::multi_val(" << val.MultiVal << ')';
1748
1749 if (val.InitVal) {
1750 const std::string& str = val.InitVal->getAsString();
1751 O << ", cl::init(" << str << ')';
1752 }
Mikhail Glushenkov739c7202008-11-28 00:13:25 +00001753
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001754 if (!val.Help.empty())
1755 O << ", cl::desc(\"" << val.Help << "\")";
1756
Mikhail Glushenkov0cbb5902009-06-23 20:45:31 +00001757 O << ");\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001758 }
1759
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001760 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001761 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001762 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001763 const OptionDescription& val = *B;
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001764
1765 O << val.GenTypeDeclaration() << ' '
1766 << val.GenVariableName()
1767 << "(\"" << val.Name << '\"';
1768
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001769 const OptionDescription& D = descs.FindOption(val.Help);
1770 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkov6be4ffc2008-05-30 06:22:52 +00001771
1772 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1773 }
1774
1775 // Emit the sink option.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001776 if (HasSink)
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00001777 O << (HasExterns ? "extern cl" : "cl")
1778 << "::list<std::string> " << SinkOptionName
1779 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001780
1781 O << '\n';
1782}
1783
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001784/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Daniel Dunbar1a551802009-07-03 00:10:29 +00001785void EmitPopulateLanguageMap (const RecordKeeper& Records, raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001786{
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001787 // Generate code
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001788 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001789
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001790 // Get the relevant field out of RecordKeeper
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001791 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001792
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001793 // It is allowed for a plugin to have no language map.
1794 if (LangMapRecord) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001795
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001796 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1797 if (!LangsToSuffixesList)
1798 throw std::string("Error in the language map definition!");
1799
1800 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001801 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkov01088772008-11-17 17:29:18 +00001802
1803 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1804 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1805
1806 for (unsigned i = 0; i < Suffixes->size(); ++i)
1807 O << Indent1 << "langMap[\""
1808 << InitPtrToString(Suffixes->getElement(i))
1809 << "\"] = \"" << Lang << "\";\n";
1810 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001811 }
1812
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001813 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001814}
1815
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001816/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1817/// by EmitEdgeClass().
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001818void IncDecWeight (const Init* i, const char* IndentLevel,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001819 raw_ostream& O) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +00001820 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001821 const std::string& OpName = d.getOperator()->getAsString();
1822
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001823 if (OpName == "inc_weight") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001824 O << IndentLevel << "ret += ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001825 }
1826 else if (OpName == "dec_weight") {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001827 O << IndentLevel << "ret -= ";
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001828 }
1829 else if (OpName == "error") {
1830 O << IndentLevel << "throw std::runtime_error(\"" <<
1831 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1832 : "Unknown error!")
1833 << "\");\n";
1834 return;
1835 }
1836
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001837 else
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +00001838 throw "Unknown operator in edge properties list: " + OpName + '!' +
Mikhail Glushenkov65ee1e62008-12-18 04:06:58 +00001839 "\nOnly 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001840
1841 if (d.getNumArgs() > 0)
1842 O << InitPtrToInt(d.getArg(0)) << ";\n";
1843 else
1844 O << "2;\n";
1845
Mikhail Glushenkov29063552008-05-06 18:18:20 +00001846}
1847
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001848/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkovb5ccfbf2008-05-30 06:10:19 +00001849void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001850 DagInit* Case, const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001851 raw_ostream& O) {
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001852
1853 // Class constructor.
1854 O << "class Edge" << N << ": public Edge {\n"
1855 << "public:\n"
1856 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1857 << "\") {}\n\n"
1858
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001859 // Function Weight().
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +00001860 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001861 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001862
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001863 // Handle the 'case' construct.
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001864 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +00001865
1866 O << Indent2 << "return ret;\n"
1867 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov9ef501b2008-05-06 17:23:14 +00001868}
1869
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001870/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001871void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001872 const OptionDescriptions& OptDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001873 raw_ostream& O) {
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001874 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001875 for (RecordVector::const_iterator B = EdgeVector.begin(),
1876 E = EdgeVector.end(); B != E; ++B) {
1877 const Record* Edge = *B;
1878 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001879 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001880
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001881 if (!isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001882 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001883 ++i;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +00001884 }
1885}
1886
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00001887/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1888/// function.
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001889void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001890 const ToolDescriptions& ToolDescs,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001891 raw_ostream& O)
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001892{
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00001893 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001894
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001895 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1896 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00001897 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001898
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +00001899 O << '\n';
1900
Mikhail Glushenkov262d2482008-11-12 00:05:17 +00001901 // Insert edges.
1902
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001903 int i = 0;
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001904 for (RecordVector::const_iterator B = EdgeVector.begin(),
1905 E = EdgeVector.end(); B != E; ++B) {
1906 const Record* Edge = *B;
1907 const std::string& NodeA = Edge->getValueAsString("a");
1908 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001909 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001910
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001911 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001912
Mikhail Glushenkove5557f42008-05-30 06:08:50 +00001913 if (isDagEmpty(Weight))
Mikhail Glushenkov15b71ba2008-12-07 16:44:47 +00001914 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +00001915 else
1916 O << "new Edge" << i << "()";
1917
1918 O << ");\n";
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00001919 ++i;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001920 }
1921
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00001922 O << "}\n\n";
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00001923}
1924
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001925/// ExtractHookNames - Extract the hook names from all instances of
1926/// $CALL(HookName) in the provided command line string. Helper
1927/// function used by FillInHookNames().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001928class ExtractHookNames {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001929 llvm::StringMap<unsigned>& HookNames_;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001930public:
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001931 ExtractHookNames(llvm::StringMap<unsigned>& HookNames)
1932 : HookNames_(HookNames) {}
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001933
1934 void operator()(const Init* CmdLine) {
1935 StrVector cmds;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001936 TokenizeCmdline(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001937 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1938 B != E; ++B) {
1939 const std::string& cmd = *B;
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001940
1941 if (cmd == "$CALL") {
1942 unsigned NumArgs = 0;
1943 checkedIncrement(B, E, "Syntax error in $CALL invocation!");
1944 const std::string& HookName = *B;
1945
1946
1947 if (HookName.at(0) == ')')
1948 throw "$CALL invoked with no arguments!";
1949
1950 while (++B != E && B->at(0) != ')') {
1951 ++NumArgs;
1952 }
1953
1954 StringMap<unsigned>::const_iterator H = HookNames_.find(HookName);
1955
1956 if (H != HookNames_.end() && H->second != NumArgs)
1957 throw "Overloading of hooks is not allowed. Overloaded hook: "
1958 + HookName;
1959 else
1960 HookNames_[HookName] = NumArgs;
1961
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001962 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001963 }
1964 }
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001965};
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001966
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001967/// FillInHookNames - Actually extract the hook names from all command
1968/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001969void FillInHookNames(const ToolDescriptions& ToolDescs,
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001970 llvm::StringMap<unsigned>& HookNames)
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001971{
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001972 // For all command lines:
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001973 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1974 E = ToolDescs.end(); B != E; ++B) {
1975 const ToolDescription& D = *(*B);
1976 if (!D.CmdLine)
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001977 continue;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001978 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001979 // This is a string.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001980 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkov2d0dc9a2008-05-30 06:22:15 +00001981 else
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001982 // This is a 'case' construct.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001983 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001984 }
1985}
1986
1987/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1988/// property records and emit hook function declaration for each
1989/// instance of $CALL(HookName).
Daniel Dunbar1a551802009-07-03 00:10:29 +00001990void EmitHookDeclarations(const ToolDescriptions& ToolDescs, raw_ostream& O) {
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001991 llvm::StringMap<unsigned> HookNames;
1992
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001993 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001994 if (HookNames.empty())
1995 return;
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00001996
1997 O << "namespace hooks {\n";
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00001998 for (StringMap<unsigned>::const_iterator B = HookNames.begin(),
1999 E = HookNames.end(); B != E; ++B) {
Mikhail Glushenkovb6b51412009-01-21 13:04:33 +00002000 O << Indent1 << "std::string " << B->first() << "(";
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002001
Mikhail Glushenkova298bb72009-01-21 13:04:00 +00002002 for (unsigned i = 0, j = B->second; i < j; ++i) {
2003 O << "const char* Arg" << i << (i+1 == j ? "" : ", ");
2004 }
2005
2006 O <<");\n";
2007 }
Mikhail Glushenkov08bd2e72008-05-30 06:12:24 +00002008 O << "}\n\n";
2009}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002010
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002011/// EmitRegisterPlugin - Emit code to register this plugin.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002012void EmitRegisterPlugin(int Priority, raw_ostream& O) {
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002013 O << "struct Plugin : public llvmc::BasePlugin {\n\n"
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002014 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002015 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
2016 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
2017 << Indent1
2018 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
2019 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
2020 << "};\n\n"
2021
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002022 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002023}
2024
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002025/// EmitIncludes - Emit necessary #include directives and some
2026/// additional declarations.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002027void EmitIncludes(raw_ostream& O) {
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002028 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002029 << "#include \"llvm/CompilerDriver/ForceLinkageMacros.h\"\n"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +00002030 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
2031 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002032
2033 << "#include \"llvm/ADT/StringExtras.h\"\n"
2034 << "#include \"llvm/Support/CommandLine.h\"\n\n"
2035
2036 << "#include <cstdlib>\n"
2037 << "#include <stdexcept>\n\n"
2038
2039 << "using namespace llvm;\n"
2040 << "using namespace llvmc;\n\n"
2041
Mikhail Glushenkov67665722008-11-12 12:41:18 +00002042 << "extern cl::opt<std::string> OutputFilename;\n\n"
2043
2044 << "inline const char* checkCString(const char* s)\n"
2045 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkovc82ce4a2008-09-22 20:49:34 +00002046}
2047
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002048
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002049/// PluginData - Holds all information about a plugin.
2050struct PluginData {
2051 OptionDescriptions OptDescs;
2052 bool HasSink;
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002053 bool HasExterns;
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002054 ToolDescriptions ToolDescs;
2055 RecordVector Edges;
2056 int Priority;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002057};
2058
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002059/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002060/// there are any with the 'sink' property set.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002061bool HasSink(const ToolDescriptions& ToolDescs) {
2062 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
2063 E = ToolDescs.end(); B != E; ++B)
2064 if ((*B)->isSink())
2065 return true;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002066
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002067 return false;
2068}
2069
2070/// HasExterns - Go through the list of option descriptions and check
2071/// if there are any external options.
2072bool HasExterns(const OptionDescriptions& OptDescs) {
2073 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
2074 E = OptDescs.end(); B != E; ++B)
2075 if (B->second.isExtern())
2076 return true;
2077
2078 return false;
Mikhail Glushenkovfa270772008-11-17 17:29:42 +00002079}
2080
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002081/// CollectPluginData - Collect tool and option properties,
2082/// compilation graph edges and plugin priority from the parse tree.
2083void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
2084 // Collect option properties.
2085 const RecordVector& OptionLists =
2086 Records.getAllDerivedDefinitions("OptionList");
2087 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
2088 Data.OptDescs);
2089
2090 // Collect tool properties.
2091 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
2092 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
2093 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkovb59dbad2008-12-07 16:42:47 +00002094 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002095
2096 // Collect compilation graph edges.
2097 const RecordVector& CompilationGraphs =
2098 Records.getAllDerivedDefinitions("CompilationGraph");
2099 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
2100 Data.Edges);
2101
2102 // Calculate the priority of this plugin.
2103 const RecordVector& Priorities =
2104 Records.getAllDerivedDefinitions("PluginPriority");
2105 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkov35fde152008-11-17 17:30:25 +00002106}
2107
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002108/// CheckPluginData - Perform some sanity checks on the collected data.
2109void CheckPluginData(PluginData& Data) {
2110 // Filter out all tools not mentioned in the compilation graph.
2111 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002112
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002113 // Typecheck the compilation graph.
2114 TypecheckGraph(Data.Edges, Data.ToolDescs);
2115
2116 // Check that there are no options without side effects (specified
2117 // only in the OptionList).
2118 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
2119
Mikhail Glushenkovad746be2008-11-28 00:13:47 +00002120}
2121
Daniel Dunbar1a551802009-07-03 00:10:29 +00002122void EmitPluginCode(const PluginData& Data, raw_ostream& O) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002123 // Emit file header.
2124 EmitIncludes(O);
2125
2126 // Emit global option registration code.
Mikhail Glushenkov7c8deb32009-06-23 20:45:07 +00002127 EmitOptionDefinitions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002128
2129 // Emit hook declarations.
2130 EmitHookDeclarations(Data.ToolDescs, O);
2131
Mikhail Glushenkove1d44b52008-12-11 10:34:18 +00002132 O << "namespace {\n\n";
2133
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002134 // Emit PopulateLanguageMap() function
2135 // (a language map maps from file extensions to language names).
2136 EmitPopulateLanguageMap(Records, O);
2137
2138 // Emit Tool classes.
2139 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
2140 E = Data.ToolDescs.end(); B!=E; ++B)
2141 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
2142
2143 // Emit Edge# classes.
2144 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
2145
2146 // Emit PopulateCompilationGraph() function.
2147 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
2148
2149 // Emit code for plugin registration.
2150 EmitRegisterPlugin(Data.Priority, O);
2151
Mikhail Glushenkovd80d8692009-06-23 20:46:48 +00002152 O << "} // End anonymous namespace.\n\n";
2153
2154 // Force linkage magic.
2155 O << "namespace llvmc {\n";
2156 O << "LLVMC_FORCE_LINKAGE_DECL(LLVMC_PLUGIN_NAME) {}\n";
2157 O << "}\n";
2158
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002159 // EOF
2160}
2161
2162
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002163// End of anonymous namespace
Mikhail Glushenkov895820d2008-05-06 18:12:03 +00002164}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002165
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002166/// run - The back-end entry point.
Daniel Dunbar1a551802009-07-03 00:10:29 +00002167void LLVMCConfigurationEmitter::run (raw_ostream &O) {
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002168 try {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002169 PluginData Data;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +00002170
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002171 CollectPluginData(Records, Data);
2172 CheckPluginData(Data);
2173
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +00002174 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00002175 EmitPluginCode(Data, O);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002176
Mikhail Glushenkov4fb71ea2008-05-30 06:21:48 +00002177 } catch (std::exception& Error) {
2178 throw Error.what() + std::string(" - usually this means a syntax error.");
2179 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002180}