blob: eda45c7a7b7386188e9f8d0b3fc83450a9a9e1cb [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include "llvm/Support/Streams.h"
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000023
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000024#include <algorithm>
25#include <cassert>
26#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000027#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000028#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000029#include <typeinfo>
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000030
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000031using namespace llvm;
32
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000033namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000034
35//===----------------------------------------------------------------------===//
36/// Typedefs
37
38typedef std::vector<Record*> RecordVector;
39typedef std::vector<std::string> StrVector;
40
41//===----------------------------------------------------------------------===//
42/// Constants
43
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000044// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000045const char * Indent1 = " ";
46const char * Indent2 = " ";
47const char * Indent3 = " ";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000049// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
51
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000052// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000053const char * SinkOptionName = "AutoGeneratedSinkOption";
54
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
60 template<typename T>
61 void operator()(const T&) const {
62 }
63};
64
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000065int InitPtrToInt(const Init* ptr) {
66 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000067 return val.getValue();
68}
69
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000070const std::string& InitPtrToString(const Init* ptr) {
71 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
72 return val.getValue();
73}
74
75const ListInit& InitPtrToList(const Init* ptr) {
76 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
77 return val;
78}
79
80const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000081 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000082 return val;
83}
84
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000085// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000086// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000087void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
88 if (d->getNumArgs() < min_arguments)
89 throw "Property " + d->getOperator()->getAsString()
90 + " has too few arguments!";
91}
92
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000093// isDagEmpty - is this DAG marked with an empty marker?
94bool isDagEmpty (const DagInit* d) {
95 return d->getOperator()->getAsString() == "empty";
96}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000097
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000098// EscapeVariableName - Escape commas and other symbols not allowed
99// in the C++ variable names. Makes it possible to use options named
100// like "Wa," (useful for prefix options).
101std::string EscapeVariableName(const std::string& Var) {
102 std::string ret;
103 for (unsigned i = 0; i != Var.size(); ++i) {
104 char cur_char = Var[i];
105 if (cur_char == ',') {
106 ret += "_comma_";
107 }
108 else if (cur_char == '+') {
109 ret += "_plus_";
110 }
111 else if (cur_char == '-') {
112 ret += "_dash_";
113 }
114 else {
115 ret.push_back(cur_char);
116 }
117 }
118 return ret;
119}
120
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000121//===----------------------------------------------------------------------===//
122/// Back-end specific code
123
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000124
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000125/// OptionType - One of six different option types. See the
126/// documentation for detailed description of differences.
127/// Extern* options are those that are defined in some other plugin.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000128namespace OptionType {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000129 enum OptionType { Alias, Switch, Parameter, ParameterList,
130 Prefix, PrefixList,
131 ExternSwitch, ExternParameter, ExternList };
132
133bool IsList (OptionType t) {
134 return (t == ParameterList || t == PrefixList || t == ExternList);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000135}
136
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000137bool IsSwitch (OptionType t) {
138 return (t == Switch || t == ExternSwitch);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000139}
140
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000141bool IsParameter (OptionType t) {
142 return (t == Parameter || t == Prefix || t == ExternParameter);
143}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000144
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000145}
146
147OptionType::OptionType stringToOptionType(const std::string& T) {
148 if (T == "alias_option")
149 return OptionType::Alias;
150 else if (T == "switch_option")
151 return OptionType::Switch;
152 else if (T == "parameter_option")
153 return OptionType::Parameter;
154 else if (T == "parameter_list_option")
155 return OptionType::ParameterList;
156 else if (T == "prefix_option")
157 return OptionType::Prefix;
158 else if (T == "prefix_list_option")
159 return OptionType::PrefixList;
160 else if (T == "extern_switch")
161 return OptionType::ExternSwitch;
162 else if (T == "extern_parameter")
163 return OptionType::ExternParameter;
164 else if (T == "extern_list")
165 return OptionType::ExternList;
166 else
167 throw "Unknown option type: " + T + '!';
168}
169
170namespace OptionDescriptionFlags {
171 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
172 ReallyHidden = 0x4 };
173}
174
175/// OptionDescription - Represents data contained in a single
176/// OptionList entry.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000177struct OptionDescription {
178 OptionType::OptionType Type;
179 std::string Name;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000180 unsigned Flags;
181 std::string Help;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000182
183 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000184 const std::string& n = "",
185 const std::string& h = DefaultHelpString)
186 : Type(t), Name(n), Flags(0x0), Help(h)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000187 {}
188
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000189 /// GenTypeDeclaration - Returns the C++ variable type of this
190 /// option.
191 const char* GenTypeDeclaration() const;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000192
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000193 /// GenVariableName - Returns the variable name used in the
194 /// generated C++ code.
195 std::string GenVariableName() const;
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000196
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000197 /// Merge - Merge two option descriptions.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000198 void Merge (const OptionDescription& other);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000199
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000200 // Misc convenient getters/setters.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000201
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000202 bool isAlias() const;
203 bool isExtern() const;
204
205 bool isRequired() const;
206 void setRequired();
207
208 bool isHidden() const;
209 void setHidden();
210
211 bool isReallyHidden() const;
212 void setReallyHidden();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000213};
214
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000215void OptionDescription::Merge (const OptionDescription& other)
216{
217 if (other.Type != Type)
218 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000219
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000220 if (Help == other.Help || Help == DefaultHelpString)
221 Help = other.Help;
222 else if (other.Help != DefaultHelpString) {
223 llvm::cerr << "Warning: several different help strings"
224 " defined for option " + Name + "\n";
225 }
226
227 Flags |= other.Flags;
228}
229
230bool OptionDescription::isAlias() const {
231 return Type == OptionType::Alias;
232}
233
234bool OptionDescription::isExtern() const {
235 return (Type == OptionType::ExternList || Type == OptionType::ExternParameter
236 || Type == OptionType::ExternSwitch);
237}
238
239bool OptionDescription::isRequired() const {
240 return Flags & OptionDescriptionFlags::Required;
241}
242void OptionDescription::setRequired() {
243 Flags |= OptionDescriptionFlags::Required;
244}
245
246bool OptionDescription::isHidden() const {
247 return Flags & OptionDescriptionFlags::Hidden;
248}
249void OptionDescription::setHidden() {
250 Flags |= OptionDescriptionFlags::Hidden;
251}
252
253bool OptionDescription::isReallyHidden() const {
254 return Flags & OptionDescriptionFlags::ReallyHidden;
255}
256void OptionDescription::setReallyHidden() {
257 Flags |= OptionDescriptionFlags::ReallyHidden;
258}
259
260const char* OptionDescription::GenTypeDeclaration() const {
261 switch (Type) {
262 case OptionType::Alias:
263 return "cl::alias";
264 case OptionType::ExternList:
265 case OptionType::PrefixList:
266 case OptionType::ParameterList:
267 return "cl::list<std::string>";
268 case OptionType::Switch:
269 case OptionType::ExternSwitch:
270 return "cl::opt<bool>";
271 case OptionType::ExternParameter:
272 case OptionType::Parameter:
273 case OptionType::Prefix:
274 default:
275 return "cl::opt<std::string>";
276 }
277}
278
279std::string OptionDescription::GenVariableName() const {
280 const std::string& EscapedName = EscapeVariableName(Name);
281 switch (Type) {
282 case OptionType::Alias:
283 return "AutoGeneratedAlias_" + EscapedName;
284 case OptionType::PrefixList:
285 case OptionType::ParameterList:
286 case OptionType::ExternList:
287 return "AutoGeneratedList_" + EscapedName;
288 case OptionType::ExternSwitch:
289 case OptionType::Switch:
290 return "AutoGeneratedSwitch_" + EscapedName;
291 case OptionType::ExternParameter:
292 case OptionType::Prefix:
293 case OptionType::Parameter:
294 default:
295 return "AutoGeneratedParameter_" + EscapedName;
296 }
297}
298
299/// OptionDescriptions - An OptionDescription array plus some helper
300/// functions.
301class OptionDescriptions {
302 typedef StringMap<OptionDescription> container_type;
303
304 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000305 container_type Descriptions;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000306
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000307public:
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000308 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000309 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000310
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000311 /// insertDescription - Insert new OptionDescription into
312 /// OptionDescriptions list
313 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000314
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000315 // Support for STL-style iteration
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000316 typedef container_type::const_iterator const_iterator;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000317 const_iterator begin() const { return Descriptions.begin(); }
318 const_iterator end() const { return Descriptions.end(); }
319};
320
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000321const OptionDescription&
322OptionDescriptions::FindOption(const std::string& OptName) const
323{
324 const_iterator I = Descriptions.find(OptName);
325 if (I != Descriptions.end())
326 return I->second;
327 else
328 throw OptName + ": no such option!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000329}
330
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000331void OptionDescriptions::InsertDescription (const OptionDescription& o)
332{
333 container_type::iterator I = Descriptions.find(o.Name);
334 if (I != Descriptions.end()) {
335 OptionDescription& D = I->second;
336 D.Merge(o);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000337 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000338 else {
339 Descriptions[o.Name] = o;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000340 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000341}
342
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000343/// HandlerTable - A base class for function objects implemented as
344/// 'tables of handlers'.
345template <class T>
346class HandlerTable {
347protected:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000348 // Implementation details.
349
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000350 /// Handler -
351 typedef void (T::* Handler) (const DagInit*);
352 /// HandlerMap - A map from property names to property handlers
353 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000354
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000355 static HandlerMap Handlers_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000356 static bool staticMembersInitialized_;
357
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000358 T* childPtr;
359public:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000360
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000361 HandlerTable(T* cp) : childPtr(cp)
362 {}
363
364 /// operator() - Just forwards to the corresponding property
365 /// handler.
366 void operator() (Init* i) {
367 const DagInit& property = InitPtrToDag(i);
368 const std::string& property_name = property.getOperator()->getAsString();
369 typename HandlerMap::iterator method = Handlers_.find(property_name);
370
371 if (method != Handlers_.end()) {
372 Handler h = method->second;
373 (childPtr->*h)(&property);
374 }
375 else {
376 throw "No handler found for property " + property_name + "!";
377 }
378 }
379
380 void AddHandler(const char* Property, Handler Handl) {
381 Handlers_[Property] = Handl;
382 }
383};
384
385template <class T> typename HandlerTable<T>::HandlerMap
386HandlerTable<T>::Handlers_;
387template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
388
389
390/// CollectOptionProperties - Function object for iterating over an
391/// option property list.
392class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
393private:
394
395 /// optDescs_ - OptionDescriptions table. This is where the
396 /// information is stored.
397 OptionDescription& optDesc_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000398
399public:
400
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000401 explicit CollectOptionProperties(OptionDescription& OD)
402 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000403 {
404 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000405 AddHandler("help", &CollectOptionProperties::onHelp);
406 AddHandler("hidden", &CollectOptionProperties::onHidden);
407 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
408 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000409
410 staticMembersInitialized_ = true;
411 }
412 }
413
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000414private:
415
416 /// Option property handlers --
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000417 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000418
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000419 void onHelp (const DagInit* d) {
420 checkNumberOfArguments(d, 1);
421 const std::string& help_message = InitPtrToString(d->getArg(0));
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000422 optDesc_.Help = help_message;
423 }
424
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000425 void onHidden (const DagInit* d) {
426 checkNumberOfArguments(d, 0);
427 checkToolProps(d);
428 optDesc_.setHidden();
429 }
430
431 void onReallyHidden (const DagInit* d) {
432 checkNumberOfArguments(d, 0);
433 checkToolProps(d);
434 optDesc_.setReallyHidden();
435 }
436
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000437 void onRequired (const DagInit* d) {
438 checkNumberOfArguments(d, 0);
439 checkToolProps(d);
440 optDesc_.setRequired();
441 }
442
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000443 // Helper functions
444
445 /// checkToolProps - Throw an error if toolProps_ == 0.
446 void checkToolProps(const DagInit* d) {
447 if (!d)
448 throw "Option property " + d->getOperator()->getAsString()
449 + " can't be used in this context";
450 }
451
452};
453
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000454/// AddOption - A function object that is applied to every option
455/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000456class AddOption {
457private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000458 OptionDescriptions& OptDescs_;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000459
460public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000461 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000462 {}
463
464 void operator()(const Init* i) {
465 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000466 checkNumberOfArguments(&d, 1);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000467
468 const OptionType::OptionType Type =
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000469 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000470 const std::string& Name = InitPtrToString(d.getArg(0));
471
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000472 OptionDescription OD(Type, Name);
473
474 if (!OD.isExtern())
475 checkNumberOfArguments(&d, 2);
476
477 if (OD.isAlias()) {
478 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000479 OD.Help = InitPtrToString(d.getArg(1));
480 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000481 else if (!OD.isExtern()) {
482 processOptionProperties(&d, OD);
483 }
484 OptDescs_.InsertDescription(OD);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000485 }
486
487private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000488 /// processOptionProperties - Go through the list of option
489 /// properties and call a corresponding handler for each.
490 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
491 checkNumberOfArguments(d, 2);
492 DagInit::const_arg_iterator B = d->arg_begin();
493 // Skip the first argument: it's always the option name.
494 ++B;
495 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000496 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000497
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000498};
499
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000500/// CollectOptionDescriptions - Collects option properties from all
501/// OptionLists.
502void CollectOptionDescriptions (RecordVector::const_iterator B,
503 RecordVector::const_iterator E,
504 OptionDescriptions& OptDescs)
505{
506 // For every OptionList:
507 for (; B!=E; ++B) {
508 RecordVector::value_type T = *B;
509 // Throws an exception if the value does not exist.
510 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000511
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000512 // For every option description in this list:
513 // collect the information and
514 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
515 }
516}
517
518// Tool information record
519
520namespace ToolFlags {
521 enum ToolFlags { Join = 0x1, Sink = 0x2 };
522}
523
524struct ToolDescription : public RefCountedBase<ToolDescription> {
525 std::string Name;
526 Init* CmdLine;
527 Init* Actions;
528 StrVector InLanguage;
529 std::string OutLanguage;
530 std::string OutputSuffix;
531 unsigned Flags;
532
533 // Various boolean properties
534 void setSink() { Flags |= ToolFlags::Sink; }
535 bool isSink() const { return Flags & ToolFlags::Sink; }
536 void setJoin() { Flags |= ToolFlags::Join; }
537 bool isJoin() const { return Flags & ToolFlags::Join; }
538
539 // Default ctor here is needed because StringMap can only store
540 // DefaultConstructible objects
541 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
542 ToolDescription (const std::string& n)
543 : Name(n), CmdLine(0), Actions(0), Flags(0)
544 {}
545};
546
547/// ToolDescriptions - A list of Tool information records.
548typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
549
550
551/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000552/// tool property records.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000553class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000554private:
555
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000556 /// toolDesc_ - Properties of the current Tool. This is where the
557 /// information is stored.
558 ToolDescription& toolDesc_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000559
560public:
561
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000562 explicit CollectToolProperties (ToolDescription& d)
563 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000564 {
565 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000566
567 AddHandler("actions", &CollectToolProperties::onActions);
568 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
569 AddHandler("in_language", &CollectToolProperties::onInLanguage);
570 AddHandler("join", &CollectToolProperties::onJoin);
571 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
572 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
573 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000574
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000575 staticMembersInitialized_ = true;
576 }
577 }
578
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000579private:
580
581 /// Property handlers --
582 /// Functions that extract information about tool properties from
583 /// DAG representation.
584
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000585 void onActions (const DagInit* d) {
586 checkNumberOfArguments(d, 1);
Mikhail Glushenkov8d489a82008-12-07 16:45:12 +0000587 Init* Case = d->getArg(0);
588 if (typeid(*Case) != typeid(DagInit) ||
589 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
590 throw
591 std::string("The argument to (actions) should be a 'case' construct!");
592 toolDesc_.Actions = Case;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000593 }
594
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000595 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000596 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000597 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000598 }
599
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000600 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000601 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000602 Init* arg = d->getArg(0);
603
604 // Find out the argument's type.
605 if (typeid(*arg) == typeid(StringInit)) {
606 // It's a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000607 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000608 }
609 else {
610 // It's a list.
611 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000612 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000613
614 // Copy strings to the output vector.
615 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
616 B != E; ++B) {
617 out.push_back(InitPtrToString(*B));
618 }
619
620 // Remove duplicates.
621 std::sort(out.begin(), out.end());
622 StrVector::iterator newE = std::unique(out.begin(), out.end());
623 out.erase(newE, out.end());
624 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000625 }
626
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000627 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000628 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000629 toolDesc_.setJoin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000630 }
631
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000632 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000633 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000634 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000635 }
636
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000637 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000638 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000639 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000640 }
641
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000642 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000643 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000644 toolDesc_.setSink();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000645 }
646
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000647};
648
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000649
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000650/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000651/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000652/// CollectToolProperties function object).
653void CollectToolDescriptions (RecordVector::const_iterator B,
654 RecordVector::const_iterator E,
655 ToolDescriptions& ToolDescs)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000656{
657 // Iterate over a properties list of every Tool definition
658 for (;B!=E;++B) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +0000659 const Record* T = *B;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000660 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000661 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000662
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000663 IntrusiveRefCntPtr<ToolDescription>
664 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000665
666 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000667 CollectToolProperties(*ToolDesc));
668 ToolDescs.push_back(ToolDesc);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000669 }
670}
671
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000672/// FillInEdgeVector - Merge all compilation graph definitions into
673/// one single edge list.
674void FillInEdgeVector(RecordVector::const_iterator B,
675 RecordVector::const_iterator E, RecordVector& Out) {
676 for (; B != E; ++B) {
677 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000678
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000679 for (unsigned i = 0; i < edges->size(); ++i)
680 Out.push_back(edges->getElementAsRecord(i));
681 }
682}
683
684/// CalculatePriority - Calculate the priority of this plugin.
685int CalculatePriority(RecordVector::const_iterator B,
686 RecordVector::const_iterator E) {
687 int total = 0;
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +0000688 for (; B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000689 total += static_cast<int>((*B)->getValueAsInt("priority"));
690 }
691 return total;
692}
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000693
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000694/// NotInGraph - Helper function object for FilterNotInGraph.
695struct NotInGraph {
696private:
697 const llvm::StringSet<>& ToolsInGraph_;
698
699public:
700 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
701 : ToolsInGraph_(ToolsInGraph)
702 {}
703
704 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
705 return (ToolsInGraph_.count(x->Name) == 0);
706 }
707};
708
709/// FilterNotInGraph - Filter out from ToolDescs all Tools not
710/// mentioned in the compilation graph definition.
711void FilterNotInGraph (const RecordVector& EdgeVector,
712 ToolDescriptions& ToolDescs) {
713
714 // List all tools mentioned in the graph.
715 llvm::StringSet<> ToolsInGraph;
716
717 for (RecordVector::const_iterator B = EdgeVector.begin(),
718 E = EdgeVector.end(); B != E; ++B) {
719
720 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000721 const std::string& NodeA = Edge->getValueAsString("a");
722 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000723
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000724 if (NodeA != "root")
725 ToolsInGraph.insert(NodeA);
726 ToolsInGraph.insert(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000727 }
728
729 // Filter ToolPropertiesList.
730 ToolDescriptions::iterator new_end =
731 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
732 NotInGraph(ToolsInGraph));
733 ToolDescs.erase(new_end, ToolDescs.end());
734}
735
736/// FillInToolToLang - Fills in two tables that map tool names to
737/// (input, output) languages. Helper function used by TypecheckGraph().
738void FillInToolToLang (const ToolDescriptions& ToolDescs,
739 StringMap<StringSet<> >& ToolToInLang,
740 StringMap<std::string>& ToolToOutLang) {
741 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
742 E = ToolDescs.end(); B != E; ++B) {
743 const ToolDescription& D = *(*B);
744 for (StrVector::const_iterator B = D.InLanguage.begin(),
745 E = D.InLanguage.end(); B != E; ++B)
746 ToolToInLang[D.Name].insert(*B);
747 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000748 }
749}
750
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000751/// TypecheckGraph - Check that names for output and input languages
752/// on all edges do match. This doesn't do much when the information
753/// about the whole graph is not available (i.e. when compiling most
754/// plugins).
755void TypecheckGraph (const RecordVector& EdgeVector,
756 const ToolDescriptions& ToolDescs) {
757 StringMap<StringSet<> > ToolToInLang;
758 StringMap<std::string> ToolToOutLang;
759
760 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
761 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
762 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
763
764 for (RecordVector::const_iterator B = EdgeVector.begin(),
765 E = EdgeVector.end(); B != E; ++B) {
766 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000767 const std::string& NodeA = Edge->getValueAsString("a");
768 const std::string& NodeB = Edge->getValueAsString("b");
769 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
770 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000771
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000772 if (NodeA != "root") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000773 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000774 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000775 + ": output->input language mismatch";
776 }
777
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000778 if (NodeB == "root")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000779 throw std::string("Edges back to the root are not allowed!");
780 }
781}
782
783/// WalkCase - Walks the 'case' expression DAG and invokes
784/// TestCallback on every test, and StatementCallback on every
785/// statement. Handles 'case' nesting, but not the 'and' and 'or'
786/// combinators.
787// TODO: Re-implement EmitCaseConstructHandler on top of this function?
788template <typename F1, typename F2>
789void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
790 const DagInit& d = InitPtrToDag(Case);
791 bool even = false;
792 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
793 B != E; ++B) {
794 Init* arg = *B;
795 if (even && dynamic_cast<DagInit*>(arg)
796 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
797 WalkCase(arg, TestCallback, StatementCallback);
798 else if (!even)
799 TestCallback(arg);
800 else
801 StatementCallback(arg);
802 even = !even;
803 }
804}
805
806/// ExtractOptionNames - A helper function object used by
807/// CheckForSuperfluousOptions() to walk the 'case' DAG.
808class ExtractOptionNames {
809 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000810
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000811 void processDag(const Init* Statement) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000812 const DagInit& Stmt = InitPtrToDag(Statement);
813 const std::string& ActionName = Stmt.getOperator()->getAsString();
814 if (ActionName == "forward" || ActionName == "forward_as" ||
815 ActionName == "unpack_values" || ActionName == "switch_on" ||
816 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
817 ActionName == "not_empty") {
818 checkNumberOfArguments(&Stmt, 1);
819 const std::string& Name = InitPtrToString(Stmt.getArg(0));
820 OptionNames_.insert(Name);
821 }
822 else if (ActionName == "and" || ActionName == "or") {
823 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000824 this->processDag(Stmt.getArg(i));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000825 }
826 }
827 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000828
829public:
830 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
831 {}
832
833 void operator()(const Init* Statement) {
834 if (typeid(*Statement) == typeid(ListInit)) {
835 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
836 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
837 B != E; ++B)
838 this->processDag(*B);
839 }
840 else {
841 this->processDag(Statement);
842 }
843 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000844};
845
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000846/// CheckForSuperfluousOptions - Check that there are no side
847/// effect-free options (specified only in the OptionList). Otherwise,
848/// output a warning.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000849void CheckForSuperfluousOptions (const RecordVector& Edges,
850 const ToolDescriptions& ToolDescs,
851 const OptionDescriptions& OptDescs) {
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000852 llvm::StringSet<> nonSuperfluousOptions;
853
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000854 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000855 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000856 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
857 E = ToolDescs.end(); B != E; ++B) {
858 const ToolDescription& TD = *(*B);
859 ExtractOptionNames Callback(nonSuperfluousOptions);
860 if (TD.Actions)
861 WalkCase(TD.Actions, Callback, Callback);
862 }
863
864 // Add all options mentioned in the 'case' clauses of the
865 // OptionalEdges of the compilation graph to the set of
866 // non-superfluous options.
867 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
868 B != E; ++B) {
869 const Record* Edge = *B;
870 DagInit* Weight = Edge->getValueAsDag("weight");
871
872 if (!isDagEmpty(Weight))
873 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000874 }
875
876 // Check that all options in OptDescs belong to the set of
877 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000878 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000879 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000880 const OptionDescription& Val = B->second;
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000881 if (!nonSuperfluousOptions.count(Val.Name)
882 && Val.Type != OptionType::Alias)
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000883 llvm::cerr << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000884 "Probable cause: this option is specified only in the OptionList.\n";
885 }
886}
887
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000888/// EmitCaseTest1Arg - Helper function used by
889/// EmitCaseConstructHandler.
890bool EmitCaseTest1Arg(const std::string& TestName,
891 const DagInit& d,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000892 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000893 std::ostream& O) {
894 checkNumberOfArguments(&d, 1);
895 const std::string& OptName = InitPtrToString(d.getArg(0));
896 if (TestName == "switch_on") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000897 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
898 if (!OptionType::IsSwitch(OptDesc.Type))
899 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000900 O << OptDesc.GenVariableName();
901 return true;
902 } else if (TestName == "input_languages_contain") {
903 O << "InLangs.count(\"" << OptName << "\") != 0";
904 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000905 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000906 // This works only for single-argument Tool::GenerateAction. Join
907 // tools can process several files in different languages simultaneously.
908
909 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000910 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000911 return true;
912 } else if (TestName == "not_empty") {
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000913 if (OptName == "o") {
914 O << "!OutputFilename.empty()";
915 return true;
916 }
917 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000918 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
919 if (OptionType::IsSwitch(OptDesc.Type))
920 throw OptName
921 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000922 O << '!' << OptDesc.GenVariableName() << ".empty()";
923 return true;
924 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000925 }
926
927 return false;
928}
929
930/// EmitCaseTest2Args - Helper function used by
931/// EmitCaseConstructHandler.
932bool EmitCaseTest2Args(const std::string& TestName,
933 const DagInit& d,
934 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000935 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000936 std::ostream& O) {
937 checkNumberOfArguments(&d, 2);
938 const std::string& OptName = InitPtrToString(d.getArg(0));
939 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000940 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000941
942 if (TestName == "parameter_equals") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000943 if (!OptionType::IsParameter(OptDesc.Type))
944 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000945 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
946 return true;
947 }
948 else if (TestName == "element_in_list") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000949 if (!OptionType::IsList(OptDesc.Type))
950 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000951 const std::string& VarName = OptDesc.GenVariableName();
952 O << "std::find(" << VarName << ".begin(),\n"
953 << IndentLevel << Indent1 << VarName << ".end(), \""
954 << OptArg << "\") != " << VarName << ".end()";
955 return true;
956 }
957
958 return false;
959}
960
961// Forward declaration.
962// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
963void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000964 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000965 std::ostream& O);
966
967/// EmitLogicalOperationTest - Helper function used by
968/// EmitCaseConstructHandler.
969void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
970 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000971 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000972 std::ostream& O) {
973 O << '(';
974 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000975 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000976 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
977 if (j != NumArgs - 1)
978 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
979 else
980 O << ')';
981 }
982}
983
984/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
985void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000986 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000987 std::ostream& O) {
988 const std::string& TestName = d.getOperator()->getAsString();
989
990 if (TestName == "and")
991 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
992 else if (TestName == "or")
993 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
994 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
995 return;
996 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
997 return;
998 else
999 throw TestName + ": unknown edge property!";
1000}
1001
1002// Emit code that handles the 'case' construct.
1003// Takes a function object that should emit code for every case clause.
1004// Callback's type is
1005// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
1006template <typename F>
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001007void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +00001008 F Callback, bool EmitElseIf,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001009 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001010 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001011 const DagInit* d = &InitPtrToDag(Dag);
1012 if (d->getOperator()->getAsString() != "case")
1013 throw std::string("EmitCaseConstructHandler should be invoked"
1014 " only on 'case' expressions!");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001015
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001016 unsigned numArgs = d->getNumArgs();
1017 if (d->getNumArgs() < 2)
1018 throw "There should be at least one clause in the 'case' expression:\n"
1019 + d->getAsString();
1020
1021 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001022 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001023
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001024 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001025 if (Test.getOperator()->getAsString() == "default") {
1026 if (i+2 != numArgs)
1027 throw std::string("The 'default' clause should be the last in the"
1028 "'case' construct!");
1029 O << IndentLevel << "else {\n";
1030 }
1031 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001032 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001033 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1034 O << ") {\n";
1035 }
1036
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001037 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001038 ++i;
1039 if (i == numArgs)
1040 throw "Case construct handler: no corresponding action "
1041 "found for the test " + Test.getAsString() + '!';
1042
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001043 Init* arg = d->getArg(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001044 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1045 if (nd && (nd->getOperator()->getAsString() == "case")) {
1046 // Handle the nested 'case'.
1047 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001048 Callback, EmitElseIf, OptDescs, O);
1049 }
1050 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001051 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001052 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001053 O << IndentLevel << "}\n";
1054 }
1055}
1056
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001057/// SubstituteSpecialCommands - Perform string substitution for $CALL
1058/// and $ENV. Helper function used by EmitCmdLineVecFill().
1059std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001060 size_t cparen = cmd.find(")");
1061 std::string ret;
1062
1063 if (cmd.find("$CALL(") == 0) {
1064 if (cmd.size() == 6)
1065 throw std::string("$CALL invocation: empty argument list!");
1066
1067 ret += "hooks::";
1068 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
1069 ret += "()";
1070 }
1071 else if (cmd.find("$ENV(") == 0) {
1072 if (cmd.size() == 5)
1073 throw std::string("$ENV invocation: empty argument list!");
1074
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001075 ret += "checkCString(std::getenv(\"";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001076 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001077 ret += "\"))";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001078 }
1079 else {
1080 throw "Unknown special command: " + cmd;
1081 }
1082
1083 if (cmd.begin() + cparen + 1 != cmd.end()) {
1084 ret += " + std::string(\"";
1085 ret += (cmd.c_str() + cparen + 1);
1086 ret += "\")";
1087 }
1088
1089 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001090}
1091
1092/// EmitCmdLineVecFill - Emit code that fills in the command line
1093/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001094void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001095 bool IsJoin, const char* IndentLevel,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001096 std::ostream& O) {
1097 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001098 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001099 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001100 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001101
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001102 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001103 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001104 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001105 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001106 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001107 if (cmd.at(0) == '$') {
1108 if (cmd == "$INFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001109 if (IsJoin)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001110 O << "for (PathVector::const_iterator B = inFiles.begin()"
1111 << ", E = inFiles.end();\n"
1112 << IndentLevel << "B != E; ++B)\n"
1113 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1114 else
1115 O << "vec.push_back(inFile.toString());\n";
1116 }
1117 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001118 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001119 }
1120 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001121 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
1122 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001123 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001124 }
1125 else {
1126 O << "vec.push_back(\"" << cmd << "\");\n";
1127 }
1128 }
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001129 O << IndentLevel << "cmd = "
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001130 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
1131 : "\"" + StrVec[0] + "\"")
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001132 << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001133}
1134
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001135/// EmitCmdLineVecFillCallback - A function object wrapper around
1136/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1137/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001138class EmitCmdLineVecFillCallback {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001139 bool IsJoin;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001140 const std::string& ToolName;
1141 public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001142 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1143 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001144
1145 void operator()(const Init* Statement, const char* IndentLevel,
1146 std::ostream& O) const
1147 {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001148 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1149 IndentLevel, O);
1150 }
1151};
1152
1153/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1154/// implement EmitActionHandler. Emits code for
1155/// handling the (forward) and (forward_as) option properties.
1156void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1157 const char* Indent,
1158 const std::string& NewName,
1159 std::ostream& O) {
1160 const std::string& Name = NewName.empty()
1161 ? ("-" + D.Name)
1162 : NewName;
1163
1164 switch (D.Type) {
1165 case OptionType::Switch:
1166 case OptionType::ExternSwitch:
1167 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1168 break;
1169 case OptionType::Parameter:
1170 case OptionType::ExternParameter:
1171 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1172 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1173 break;
1174 case OptionType::Prefix:
1175 O << Indent << "vec.push_back(\"" << Name << "\" + "
1176 << D.GenVariableName() << ");\n";
1177 break;
1178 case OptionType::PrefixList:
1179 O << Indent << "for (" << D.GenTypeDeclaration()
1180 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1181 << Indent << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
1182 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
1183 << "*B);\n";
1184 break;
1185 case OptionType::ParameterList:
1186 case OptionType::ExternList:
1187 O << Indent << "for (" << D.GenTypeDeclaration()
1188 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1189 << Indent << "E = " << D.GenVariableName()
1190 << ".end() ; B != E; ++B) {\n"
1191 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n"
1192 << Indent << Indent1 << "vec.push_back(*B);\n"
1193 << Indent << "}\n";
1194 break;
1195 case OptionType::Alias:
1196 default:
1197 throw std::string("Aliases are not allowed in tool option descriptions!");
1198 }
1199}
1200
1201/// EmitActionHandler - Emit code that handles actions. Used by
1202/// EmitGenerateActionMethod() as an argument to
1203/// EmitCaseConstructHandler().
1204class EmitActionHandler {
1205 const OptionDescriptions& OptDescs;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001206
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001207 void processActionDag(const Init* Statement, const char* IndentLevel,
1208 std::ostream& O) const
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001209 {
1210 const DagInit& Dag = InitPtrToDag(Statement);
1211 const std::string& ActionName = Dag.getOperator()->getAsString();
1212
1213 if (ActionName == "append_cmd") {
1214 checkNumberOfArguments(&Dag, 1);
1215 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1216 O << IndentLevel << "vec.push_back(\"" << Cmd << "\");\n";
1217 }
1218 else if (ActionName == "forward") {
1219 checkNumberOfArguments(&Dag, 1);
1220 const std::string& Name = InitPtrToString(Dag.getArg(0));
1221 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1222 IndentLevel, "", O);
1223 }
1224 else if (ActionName == "forward_as") {
1225 checkNumberOfArguments(&Dag, 2);
1226 const std::string& Name = InitPtrToString(Dag.getArg(0));
1227 const std::string& NewName = InitPtrToString(Dag.getArg(0));
1228 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1229 IndentLevel, NewName, O);
1230 }
1231 else if (ActionName == "output_suffix") {
1232 checkNumberOfArguments(&Dag, 1);
1233 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1234 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1235 }
1236 else if (ActionName == "stop_compilation") {
1237 O << IndentLevel << "stop_compilation = true;\n";
1238 }
1239 else if (ActionName == "unpack_values") {
1240 checkNumberOfArguments(&Dag, 1);
1241 const std::string& Name = InitPtrToString(Dag.getArg(0));
1242 const OptionDescription& D = OptDescs.FindOption(Name);
1243
1244 if (OptionType::IsList(D.Type)) {
1245 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1246 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1247 << IndentLevel << "E = " << D.GenVariableName()
1248 << ".end(); B != E; ++B)\n"
1249 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1250 }
1251 else if (OptionType::IsParameter(D.Type)){
1252 O << Indent3 << "llvm::SplitString("
1253 << D.GenVariableName() << ", vec, \",\");\n";
1254 }
1255 else {
1256 throw "Option '" + D.Name +
1257 "': switches can't have the 'unpack_values' property!";
1258 }
1259 }
1260 else {
1261 throw "Unknown action name: " + ActionName + "!";
1262 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001263 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001264 public:
1265 EmitActionHandler(const OptionDescriptions& OD)
1266 : OptDescs(OD) {}
1267
1268 void operator()(const Init* Statement, const char* IndentLevel,
1269 std::ostream& O) const
1270 {
1271 if (typeid(*Statement) == typeid(ListInit)) {
1272 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1273 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1274 B != E; ++B)
1275 this->processActionDag(*B, IndentLevel, O);
1276 }
1277 else {
1278 this->processActionDag(Statement, IndentLevel, O);
1279 }
1280 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001281};
1282
1283// EmitGenerateActionMethod - Emit one of two versions of the
1284// Tool::GenerateAction() method.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001285void EmitGenerateActionMethod (const ToolDescription& D,
1286 const OptionDescriptions& OptDescs,
1287 bool IsJoin, std::ostream& O) {
1288 if (IsJoin)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001289 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1290 else
1291 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1292
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001293 O << Indent2 << "bool HasChildren,\n"
1294 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001295 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1296 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001297 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001298 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001299 << Indent2 << "std::vector<std::string> vec;\n"
1300 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1301 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1302 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001303
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001304 // For every understood option, emit handling code.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001305 if (D.Actions)
1306 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1307 false, OptDescs, O);
1308
1309 O << '\n' << Indent2
1310 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
1311 << Indent3 << "TempDir, stop_compilation, output_suffix).toString();\n\n";
1312
1313 // cmd_line is either a string or a 'case' construct.
1314 if (!D.CmdLine)
1315 throw "Tool " + D.Name + " has no cmd_line property!";
1316 else if (typeid(*D.CmdLine) == typeid(StringInit))
1317 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1318 else
1319 EmitCaseConstructHandler(D.CmdLine, Indent2,
1320 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1321 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001322
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001323 // Handle the Sink property.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001324 if (D.isSink()) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001325 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1326 << Indent3 << "vec.insert(vec.end(), "
1327 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1328 << Indent2 << "}\n";
1329 }
1330
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001331 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001332 << Indent1 << "}\n\n";
1333}
1334
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001335/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1336/// a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001337void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1338 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001339 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001340 if (!ToolDesc.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001341 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001342 << Indent2 << "bool HasChildren,\n"
1343 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001344 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1345 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001346 << Indent1 << "{\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001347 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001348 << " is not a Join tool!\");\n"
1349 << Indent1 << "}\n\n";
1350 else
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001351 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001352
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001353 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001354}
1355
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001356/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1357/// methods for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001358void EmitInOutLanguageMethods (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001359 O << Indent1 << "const char** InputLanguages() const {\n"
1360 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001361 << Indent1 << "}\n\n";
1362
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001363 if (D.OutLanguage.empty())
1364 throw "Tool " + D.Name + " has no 'out_language' property!";
1365
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001366 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001367 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001368 << Indent1 << "}\n\n";
1369}
1370
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001371/// EmitNameMethod - Emit the Name() method for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001372void EmitNameMethod (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001373 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001374 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001375 << Indent1 << "}\n\n";
1376}
1377
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001378/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1379/// class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001380void EmitIsJoinMethod (const ToolDescription& D, std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001381 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001382 if (D.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001383 O << Indent2 << "return true;\n";
1384 else
1385 O << Indent2 << "return false;\n";
1386 O << Indent1 << "}\n\n";
1387}
1388
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001389/// EmitStaticMemberDefinitions - Emit static member definitions for a
1390/// given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001391void EmitStaticMemberDefinitions(const ToolDescription& D, std::ostream& O) {
1392 if (D.InLanguage.empty())
1393 throw "Tool " + D.Name + " has no 'in_language' property!";
1394
1395 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1396 for (StrVector::const_iterator B = D.InLanguage.begin(),
1397 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001398 O << '\"' << *B << "\", ";
1399 O << "0};\n\n";
1400}
1401
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001402/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001403void EmitToolClassDefinition (const ToolDescription& D,
1404 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001405 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001406 if (D.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001407 return;
1408
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001409 // Header
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001410 O << "class " << D.Name << " : public ";
1411 if (D.isJoin())
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001412 O << "JoinTool";
1413 else
1414 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001415
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001416 O << "{\nprivate:\n"
1417 << Indent1 << "static const char* InputLanguages_[];\n\n";
1418
1419 O << "public:\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001420 EmitNameMethod(D, O);
1421 EmitInOutLanguageMethods(D, O);
1422 EmitIsJoinMethod(D, O);
1423 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001424
1425 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001426 O << "};\n";
1427
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001428 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001429
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001430}
1431
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001432/// EmitOptionDefintions - Iterate over a list of option descriptions
1433/// and emit registration code.
1434void EmitOptionDefintions (const OptionDescriptions& descs,
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001435 bool HasSink, bool HasExterns,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001436 std::ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001437{
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001438 std::vector<OptionDescription> Aliases;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001439
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001440 // Emit static cl::Option variables.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001441 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001442 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001443 const OptionDescription& val = B->second;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001444
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001445 if (val.Type == OptionType::Alias) {
1446 Aliases.push_back(val);
1447 continue;
1448 }
1449
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001450 if (val.isExtern())
1451 O << "extern ";
1452
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001453 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001454 << val.GenVariableName();
1455
1456 if (val.isExtern()) {
1457 O << ";\n";
1458 continue;
1459 }
1460
1461 O << "(\"" << val.Name << '\"';
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001462
1463 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1464 O << ", cl::Prefix";
1465
1466 if (val.isRequired()) {
1467 switch (val.Type) {
1468 case OptionType::PrefixList:
1469 case OptionType::ParameterList:
1470 O << ", cl::OneOrMore";
1471 break;
1472 default:
1473 O << ", cl::Required";
1474 }
1475 }
1476
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001477 if (val.isReallyHidden() || val.isHidden()) {
1478 if (val.isRequired())
1479 O << " |";
1480 else
1481 O << ",";
1482 if (val.isReallyHidden())
1483 O << " cl::ReallyHidden";
1484 else
1485 O << " cl::Hidden";
1486 }
1487
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001488 if (!val.Help.empty())
1489 O << ", cl::desc(\"" << val.Help << "\")";
1490
1491 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001492 }
1493
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001494 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001495 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001496 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001497 const OptionDescription& val = *B;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001498
1499 O << val.GenTypeDeclaration() << ' '
1500 << val.GenVariableName()
1501 << "(\"" << val.Name << '\"';
1502
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001503 const OptionDescription& D = descs.FindOption(val.Help);
1504 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001505
1506 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1507 }
1508
1509 // Emit the sink option.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001510 if (HasSink)
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001511 O << (HasExterns ? "extern cl" : "cl")
1512 << "::list<std::string> " << SinkOptionName
1513 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001514
1515 O << '\n';
1516}
1517
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001518/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001519void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1520{
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001521 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001522 O << "namespace {\n\n";
1523 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001524
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001525 // Get the relevant field out of RecordKeeper
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001526 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001527
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001528 // It is allowed for a plugin to have no language map.
1529 if (LangMapRecord) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001530
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001531 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1532 if (!LangsToSuffixesList)
1533 throw std::string("Error in the language map definition!");
1534
1535 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001536 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001537
1538 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1539 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1540
1541 for (unsigned i = 0; i < Suffixes->size(); ++i)
1542 O << Indent1 << "langMap[\""
1543 << InitPtrToString(Suffixes->getElement(i))
1544 << "\"] = \"" << Lang << "\";\n";
1545 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001546 }
1547
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001548 O << "}\n\n}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001549}
1550
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001551/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1552/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001553void IncDecWeight (const Init* i, const char* IndentLevel,
1554 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001555 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001556 const std::string& OpName = d.getOperator()->getAsString();
1557
1558 if (OpName == "inc_weight")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001559 O << IndentLevel << "ret += ";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001560 else if (OpName == "dec_weight")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001561 O << IndentLevel << "ret -= ";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001562 else
1563 throw "Unknown operator in edge properties list: " + OpName + '!';
1564
1565 if (d.getNumArgs() > 0)
1566 O << InitPtrToInt(d.getArg(0)) << ";\n";
1567 else
1568 O << "2;\n";
1569
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001570}
1571
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001572/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001573void EmitEdgeClass (unsigned N, const std::string& Target,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001574 DagInit* Case, const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001575 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001576
1577 // Class constructor.
1578 O << "class Edge" << N << ": public Edge {\n"
1579 << "public:\n"
1580 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1581 << "\") {}\n\n"
1582
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001583 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001584 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001585 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001586
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001587 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001588 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001589
1590 O << Indent2 << "return ret;\n"
1591 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001592}
1593
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001594/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001595void EmitEdgeClasses (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001596 const OptionDescriptions& OptDescs,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001597 std::ostream& O) {
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001598 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001599 for (RecordVector::const_iterator B = EdgeVector.begin(),
1600 E = EdgeVector.end(); B != E; ++B) {
1601 const Record* Edge = *B;
1602 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001603 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001604
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001605 if (!isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001606 EmitEdgeClass(i, NodeB, Weight, OptDescs, O);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001607 ++i;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001608 }
1609}
1610
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001611/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1612/// function.
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001613void EmitPopulateCompilationGraph (const RecordVector& EdgeVector,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001614 const ToolDescriptions& ToolDescs,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001615 std::ostream& O)
1616{
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001617 O << "namespace {\n\n";
1618 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001619
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001620 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1621 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001622 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001623
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001624 O << '\n';
1625
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001626 // Insert edges.
1627
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001628 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001629 for (RecordVector::const_iterator B = EdgeVector.begin(),
1630 E = EdgeVector.end(); B != E; ++B) {
1631 const Record* Edge = *B;
1632 const std::string& NodeA = Edge->getValueAsString("a");
1633 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001634 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001635
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001636 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001637
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001638 if (isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001639 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001640 else
1641 O << "new Edge" << i << "()";
1642
1643 O << ");\n";
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001644 ++i;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001645 }
1646
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001647 O << "}\n\n}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001648}
1649
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001650/// ExtractHookNames - Extract the hook names from all instances of
1651/// $CALL(HookName) in the provided command line string. Helper
1652/// function used by FillInHookNames().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001653class ExtractHookNames {
1654 llvm::StringSet<>& HookNames_;
1655public:
1656 ExtractHookNames(llvm::StringSet<>& HookNames)
1657 : HookNames_(HookNames_) {}
1658
1659 void operator()(const Init* CmdLine) {
1660 StrVector cmds;
1661 llvm::SplitString(InitPtrToString(CmdLine), cmds);
1662 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1663 B != E; ++B) {
1664 const std::string& cmd = *B;
1665 if (cmd.find("$CALL(") == 0) {
1666 if (cmd.size() == 6)
1667 throw std::string("$CALL invocation: empty argument list!");
1668 HookNames_.insert(std::string(cmd.begin() + 6,
1669 cmd.begin() + cmd.find(")")));
1670 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001671 }
1672 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001673};
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001674
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001675/// FillInHookNames - Actually extract the hook names from all command
1676/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001677void FillInHookNames(const ToolDescriptions& ToolDescs,
1678 llvm::StringSet<>& HookNames)
1679{
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001680 // For all command lines:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001681 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1682 E = ToolDescs.end(); B != E; ++B) {
1683 const ToolDescription& D = *(*B);
1684 if (!D.CmdLine)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001685 continue;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001686 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001687 // This is a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001688 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001689 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001690 // This is a 'case' construct.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001691 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001692 }
1693}
1694
1695/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1696/// property records and emit hook function declaration for each
1697/// instance of $CALL(HookName).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001698void EmitHookDeclarations(const ToolDescriptions& ToolDescs, std::ostream& O) {
1699 llvm::StringSet<> HookNames;
1700 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001701 if (HookNames.empty())
1702 return;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001703
1704 O << "namespace hooks {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001705 for (StringSet<>::const_iterator B = HookNames.begin(), E = HookNames.end();
1706 B != E; ++B)
1707 O << Indent1 << "std::string " << B->first() << "();\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001708
1709 O << "}\n\n";
1710}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001711
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001712/// EmitRegisterPlugin - Emit code to register this plugin.
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001713void EmitRegisterPlugin(int Priority, std::ostream& O) {
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001714 O << "namespace {\n\n"
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001715 << "struct Plugin : public llvmc::BasePlugin {\n\n"
1716 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001717 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
1718 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
1719 << Indent1
1720 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
1721 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
1722 << "};\n\n"
1723
1724 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n}\n\n";
1725}
1726
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001727/// EmitIncludes - Emit necessary #include directives and some
1728/// additional declarations.
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001729void EmitIncludes(std::ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001730 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
1731 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1732 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001733
1734 << "#include \"llvm/ADT/StringExtras.h\"\n"
1735 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1736
1737 << "#include <cstdlib>\n"
1738 << "#include <stdexcept>\n\n"
1739
1740 << "using namespace llvm;\n"
1741 << "using namespace llvmc;\n\n"
1742
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001743 << "extern cl::opt<std::string> OutputFilename;\n\n"
1744
1745 << "inline const char* checkCString(const char* s)\n"
1746 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001747}
1748
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001749
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001750/// PluginData - Holds all information about a plugin.
1751struct PluginData {
1752 OptionDescriptions OptDescs;
1753 bool HasSink;
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001754 bool HasExterns;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001755 ToolDescriptions ToolDescs;
1756 RecordVector Edges;
1757 int Priority;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001758};
1759
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001760/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001761/// there are any with the 'sink' property set.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001762bool HasSink(const ToolDescriptions& ToolDescs) {
1763 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1764 E = ToolDescs.end(); B != E; ++B)
1765 if ((*B)->isSink())
1766 return true;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001767
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001768 return false;
1769}
1770
1771/// HasExterns - Go through the list of option descriptions and check
1772/// if there are any external options.
1773bool HasExterns(const OptionDescriptions& OptDescs) {
1774 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
1775 E = OptDescs.end(); B != E; ++B)
1776 if (B->second.isExtern())
1777 return true;
1778
1779 return false;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001780}
1781
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001782/// CollectPluginData - Collect tool and option properties,
1783/// compilation graph edges and plugin priority from the parse tree.
1784void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
1785 // Collect option properties.
1786 const RecordVector& OptionLists =
1787 Records.getAllDerivedDefinitions("OptionList");
1788 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
1789 Data.OptDescs);
1790
1791 // Collect tool properties.
1792 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
1793 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
1794 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001795 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001796
1797 // Collect compilation graph edges.
1798 const RecordVector& CompilationGraphs =
1799 Records.getAllDerivedDefinitions("CompilationGraph");
1800 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
1801 Data.Edges);
1802
1803 // Calculate the priority of this plugin.
1804 const RecordVector& Priorities =
1805 Records.getAllDerivedDefinitions("PluginPriority");
1806 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001807}
1808
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001809/// CheckPluginData - Perform some sanity checks on the collected data.
1810void CheckPluginData(PluginData& Data) {
1811 // Filter out all tools not mentioned in the compilation graph.
1812 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001813
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001814 // Typecheck the compilation graph.
1815 TypecheckGraph(Data.Edges, Data.ToolDescs);
1816
1817 // Check that there are no options without side effects (specified
1818 // only in the OptionList).
1819 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
1820
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001821}
1822
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001823void EmitPluginCode(const PluginData& Data, std::ostream& O) {
1824 // Emit file header.
1825 EmitIncludes(O);
1826
1827 // Emit global option registration code.
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001828 EmitOptionDefintions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001829
1830 // Emit hook declarations.
1831 EmitHookDeclarations(Data.ToolDescs, O);
1832
1833 // Emit PopulateLanguageMap() function
1834 // (a language map maps from file extensions to language names).
1835 EmitPopulateLanguageMap(Records, O);
1836
1837 // Emit Tool classes.
1838 for (ToolDescriptions::const_iterator B = Data.ToolDescs.begin(),
1839 E = Data.ToolDescs.end(); B!=E; ++B)
1840 EmitToolClassDefinition(*(*B), Data.OptDescs, O);
1841
1842 // Emit Edge# classes.
1843 EmitEdgeClasses(Data.Edges, Data.OptDescs, O);
1844
1845 // Emit PopulateCompilationGraph() function.
1846 EmitPopulateCompilationGraph(Data.Edges, Data.ToolDescs, O);
1847
1848 // Emit code for plugin registration.
1849 EmitRegisterPlugin(Data.Priority, O);
1850
1851 // EOF
1852}
1853
1854
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001855// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001856}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001857
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001858/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001859void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001860 try {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001861 PluginData Data;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001862
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001863 CollectPluginData(Records, Data);
1864 CheckPluginData(Data);
1865
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001866 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001867 EmitPluginCode(Data, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001868
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001869 } catch (std::exception& Error) {
1870 throw Error.what() + std::string(" - usually this means a syntax error.");
1871 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001872}