blob: 1aa0ec6bb1f4b60767412e491d7dd0b252afb7f0 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include "llvm/Support/Streams.h"
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000023
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000024#include <algorithm>
25#include <cassert>
26#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000027#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000028#include <string>
Chris Lattner52aa6862008-06-04 04:46:14 +000029#include <typeinfo>
Mikhail Glushenkovb08aafd2008-11-12 00:04:46 +000030
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000031using namespace llvm;
32
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000033namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000034
35//===----------------------------------------------------------------------===//
36/// Typedefs
37
38typedef std::vector<Record*> RecordVector;
39typedef std::vector<std::string> StrVector;
40
41//===----------------------------------------------------------------------===//
42/// Constants
43
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000044// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000045const char * Indent1 = " ";
46const char * Indent2 = " ";
47const char * Indent3 = " ";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000049// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
51
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000052// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000053const char * SinkOptionName = "AutoGeneratedSinkOption";
54
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +000058/// Id - An 'identity' function object.
59struct Id {
60 template<typename T>
61 void operator()(const T&) const {
62 }
63};
64
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000065int InitPtrToInt(const Init* ptr) {
66 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000067 return val.getValue();
68}
69
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000070const std::string& InitPtrToString(const Init* ptr) {
71 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
72 return val.getValue();
73}
74
75const ListInit& InitPtrToList(const Init* ptr) {
76 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
77 return val;
78}
79
80const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000081 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000082 return val;
83}
84
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000085// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000086// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000087void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +000088 if (!d || d->getNumArgs() < min_arguments)
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000089 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,
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000130 Prefix, PrefixList};
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000131
132bool IsList (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000133 return (t == ParameterList || t == PrefixList);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000134}
135
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000136bool IsSwitch (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000137 return (t == Switch);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000138}
139
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000140bool IsParameter (OptionType t) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000141 return (t == Parameter || t == Prefix);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000142}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000143
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000144}
145
146OptionType::OptionType stringToOptionType(const std::string& T) {
147 if (T == "alias_option")
148 return OptionType::Alias;
149 else if (T == "switch_option")
150 return OptionType::Switch;
151 else if (T == "parameter_option")
152 return OptionType::Parameter;
153 else if (T == "parameter_list_option")
154 return OptionType::ParameterList;
155 else if (T == "prefix_option")
156 return OptionType::Prefix;
157 else if (T == "prefix_list_option")
158 return OptionType::PrefixList;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000159 else
160 throw "Unknown option type: " + T + '!';
161}
162
163namespace OptionDescriptionFlags {
164 enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000165 ReallyHidden = 0x4, Extern = 0x8 };
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000166}
167
168/// OptionDescription - Represents data contained in a single
169/// OptionList entry.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000170struct OptionDescription {
171 OptionType::OptionType Type;
172 std::string Name;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000173 unsigned Flags;
174 std::string Help;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000175
176 OptionDescription(OptionType::OptionType t = OptionType::Switch,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000177 const std::string& n = "",
178 const std::string& h = DefaultHelpString)
179 : Type(t), Name(n), Flags(0x0), Help(h)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000180 {}
181
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000182 /// GenTypeDeclaration - Returns the C++ variable type of this
183 /// option.
184 const char* GenTypeDeclaration() const;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000185
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000186 /// GenVariableName - Returns the variable name used in the
187 /// generated C++ code.
188 std::string GenVariableName() const;
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000189
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000190 /// Merge - Merge two option descriptions.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000191 void Merge (const OptionDescription& other);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000192
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000193 // Misc convenient getters/setters.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000194
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000195 bool isAlias() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000196
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000197 bool isExtern() const;
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000198 void setExtern();
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000199
200 bool isRequired() const;
201 void setRequired();
202
203 bool isHidden() const;
204 void setHidden();
205
206 bool isReallyHidden() const;
207 void setReallyHidden();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000208};
209
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000210void OptionDescription::Merge (const OptionDescription& other)
211{
212 if (other.Type != Type)
213 throw "Conflicting definitions for the option " + Name + "!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000214
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000215 if (Help == other.Help || Help == DefaultHelpString)
216 Help = other.Help;
217 else if (other.Help != DefaultHelpString) {
218 llvm::cerr << "Warning: several different help strings"
219 " defined for option " + Name + "\n";
220 }
221
222 Flags |= other.Flags;
223}
224
225bool OptionDescription::isAlias() const {
226 return Type == OptionType::Alias;
227}
228
229bool OptionDescription::isExtern() const {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000230 return Flags & OptionDescriptionFlags::Extern;
231}
232void OptionDescription::setExtern() {
233 Flags |= OptionDescriptionFlags::Extern;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000234}
235
236bool OptionDescription::isRequired() const {
237 return Flags & OptionDescriptionFlags::Required;
238}
239void OptionDescription::setRequired() {
240 Flags |= OptionDescriptionFlags::Required;
241}
242
243bool OptionDescription::isHidden() const {
244 return Flags & OptionDescriptionFlags::Hidden;
245}
246void OptionDescription::setHidden() {
247 Flags |= OptionDescriptionFlags::Hidden;
248}
249
250bool OptionDescription::isReallyHidden() const {
251 return Flags & OptionDescriptionFlags::ReallyHidden;
252}
253void OptionDescription::setReallyHidden() {
254 Flags |= OptionDescriptionFlags::ReallyHidden;
255}
256
257const char* OptionDescription::GenTypeDeclaration() const {
258 switch (Type) {
259 case OptionType::Alias:
260 return "cl::alias";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000261 case OptionType::PrefixList:
262 case OptionType::ParameterList:
263 return "cl::list<std::string>";
264 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000265 return "cl::opt<bool>";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000266 case OptionType::Parameter:
267 case OptionType::Prefix:
268 default:
269 return "cl::opt<std::string>";
270 }
271}
272
273std::string OptionDescription::GenVariableName() const {
274 const std::string& EscapedName = EscapeVariableName(Name);
275 switch (Type) {
276 case OptionType::Alias:
277 return "AutoGeneratedAlias_" + EscapedName;
278 case OptionType::PrefixList:
279 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000280 return "AutoGeneratedList_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000281 case OptionType::Switch:
282 return "AutoGeneratedSwitch_" + EscapedName;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000283 case OptionType::Prefix:
284 case OptionType::Parameter:
285 default:
286 return "AutoGeneratedParameter_" + EscapedName;
287 }
288}
289
290/// OptionDescriptions - An OptionDescription array plus some helper
291/// functions.
292class OptionDescriptions {
293 typedef StringMap<OptionDescription> container_type;
294
295 /// Descriptions - A list of OptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000296 container_type Descriptions;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000297
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000298public:
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000299 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000300 const OptionDescription& FindOption(const std::string& OptName) const;
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000301
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000302 /// insertDescription - Insert new OptionDescription into
303 /// OptionDescriptions list
304 void InsertDescription (const OptionDescription& o);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000305
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000306 // Support for STL-style iteration
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000307 typedef container_type::const_iterator const_iterator;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000308 const_iterator begin() const { return Descriptions.begin(); }
309 const_iterator end() const { return Descriptions.end(); }
310};
311
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000312const OptionDescription&
313OptionDescriptions::FindOption(const std::string& OptName) const
314{
315 const_iterator I = Descriptions.find(OptName);
316 if (I != Descriptions.end())
317 return I->second;
318 else
319 throw OptName + ": no such option!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000320}
321
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000322void OptionDescriptions::InsertDescription (const OptionDescription& o)
323{
324 container_type::iterator I = Descriptions.find(o.Name);
325 if (I != Descriptions.end()) {
326 OptionDescription& D = I->second;
327 D.Merge(o);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000328 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000329 else {
330 Descriptions[o.Name] = o;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000331 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000332}
333
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000334/// HandlerTable - A base class for function objects implemented as
335/// 'tables of handlers'.
336template <class T>
337class HandlerTable {
338protected:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000339 // Implementation details.
340
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000341 /// Handler -
342 typedef void (T::* Handler) (const DagInit*);
343 /// HandlerMap - A map from property names to property handlers
344 typedef StringMap<Handler> HandlerMap;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000345
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000346 static HandlerMap Handlers_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000347 static bool staticMembersInitialized_;
348
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000349 T* childPtr;
350public:
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000351
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000352 HandlerTable(T* cp) : childPtr(cp)
353 {}
354
355 /// operator() - Just forwards to the corresponding property
356 /// handler.
357 void operator() (Init* i) {
358 const DagInit& property = InitPtrToDag(i);
359 const std::string& property_name = property.getOperator()->getAsString();
360 typename HandlerMap::iterator method = Handlers_.find(property_name);
361
362 if (method != Handlers_.end()) {
363 Handler h = method->second;
364 (childPtr->*h)(&property);
365 }
366 else {
367 throw "No handler found for property " + property_name + "!";
368 }
369 }
370
371 void AddHandler(const char* Property, Handler Handl) {
372 Handlers_[Property] = Handl;
373 }
374};
375
376template <class T> typename HandlerTable<T>::HandlerMap
377HandlerTable<T>::Handlers_;
378template <class T> bool HandlerTable<T>::staticMembersInitialized_ = false;
379
380
381/// CollectOptionProperties - Function object for iterating over an
382/// option property list.
383class CollectOptionProperties : public HandlerTable<CollectOptionProperties> {
384private:
385
386 /// optDescs_ - OptionDescriptions table. This is where the
387 /// information is stored.
388 OptionDescription& optDesc_;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000389
390public:
391
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000392 explicit CollectOptionProperties(OptionDescription& OD)
393 : HandlerTable<CollectOptionProperties>(this), optDesc_(OD)
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000394 {
395 if (!staticMembersInitialized_) {
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000396 AddHandler("extern", &CollectOptionProperties::onExtern);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000397 AddHandler("help", &CollectOptionProperties::onHelp);
398 AddHandler("hidden", &CollectOptionProperties::onHidden);
399 AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
400 AddHandler("required", &CollectOptionProperties::onRequired);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000401
402 staticMembersInitialized_ = true;
403 }
404 }
405
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000406private:
407
408 /// Option property handlers --
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000409 /// Methods that handle option properties such as (help) or (hidden).
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000410
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000411 void onExtern (const DagInit* d) {
412 checkNumberOfArguments(d, 0);
413 optDesc_.setExtern();
414 }
415
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000416 void onHelp (const DagInit* d) {
417 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e70fb52008-12-07 16:47:12 +0000418 optDesc_.Help = InitPtrToString(d->getArg(0));
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000419 }
420
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000421 void onHidden (const DagInit* d) {
422 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000423 optDesc_.setHidden();
424 }
425
426 void onReallyHidden (const DagInit* d) {
427 checkNumberOfArguments(d, 0);
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000428 optDesc_.setReallyHidden();
429 }
430
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000431 void onRequired (const DagInit* d) {
432 checkNumberOfArguments(d, 0);
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000433 optDesc_.setRequired();
434 }
435
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000436};
437
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000438/// AddOption - A function object that is applied to every option
439/// description. Used by CollectOptionDescriptions.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000440class AddOption {
441private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000442 OptionDescriptions& OptDescs_;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000443
444public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000445 explicit AddOption(OptionDescriptions& OD) : OptDescs_(OD)
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000446 {}
447
448 void operator()(const Init* i) {
449 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000450 checkNumberOfArguments(&d, 1);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000451
452 const OptionType::OptionType Type =
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000453 stringToOptionType(d.getOperator()->getAsString());
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000454 const std::string& Name = InitPtrToString(d.getArg(0));
455
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000456 OptionDescription OD(Type, Name);
457
458 if (!OD.isExtern())
459 checkNumberOfArguments(&d, 2);
460
461 if (OD.isAlias()) {
462 // Aliases store the aliased option name in the 'Help' field.
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000463 OD.Help = InitPtrToString(d.getArg(1));
464 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000465 else if (!OD.isExtern()) {
466 processOptionProperties(&d, OD);
467 }
468 OptDescs_.InsertDescription(OD);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000469 }
470
471private:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000472 /// processOptionProperties - Go through the list of option
473 /// properties and call a corresponding handler for each.
474 static void processOptionProperties (const DagInit* d, OptionDescription& o) {
475 checkNumberOfArguments(d, 2);
476 DagInit::const_arg_iterator B = d->arg_begin();
477 // Skip the first argument: it's always the option name.
478 ++B;
479 std::for_each(B, d->arg_end(), CollectOptionProperties(o));
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000480 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000481
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000482};
483
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000484/// CollectOptionDescriptions - Collects option properties from all
485/// OptionLists.
486void CollectOptionDescriptions (RecordVector::const_iterator B,
487 RecordVector::const_iterator E,
488 OptionDescriptions& OptDescs)
489{
490 // For every OptionList:
491 for (; B!=E; ++B) {
492 RecordVector::value_type T = *B;
493 // Throws an exception if the value does not exist.
494 ListInit* PropList = T->getValueAsListInit("options");
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000495
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000496 // For every option description in this list:
497 // collect the information and
498 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
499 }
500}
501
502// Tool information record
503
504namespace ToolFlags {
505 enum ToolFlags { Join = 0x1, Sink = 0x2 };
506}
507
508struct ToolDescription : public RefCountedBase<ToolDescription> {
509 std::string Name;
510 Init* CmdLine;
511 Init* Actions;
512 StrVector InLanguage;
513 std::string OutLanguage;
514 std::string OutputSuffix;
515 unsigned Flags;
516
517 // Various boolean properties
518 void setSink() { Flags |= ToolFlags::Sink; }
519 bool isSink() const { return Flags & ToolFlags::Sink; }
520 void setJoin() { Flags |= ToolFlags::Join; }
521 bool isJoin() const { return Flags & ToolFlags::Join; }
522
523 // Default ctor here is needed because StringMap can only store
524 // DefaultConstructible objects
525 ToolDescription() : CmdLine(0), Actions(0), Flags(0) {}
526 ToolDescription (const std::string& n)
527 : Name(n), CmdLine(0), Actions(0), Flags(0)
528 {}
529};
530
531/// ToolDescriptions - A list of Tool information records.
532typedef std::vector<IntrusiveRefCntPtr<ToolDescription> > ToolDescriptions;
533
534
535/// CollectToolProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000536/// tool property records.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000537class CollectToolProperties : public HandlerTable<CollectToolProperties> {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000538private:
539
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000540 /// toolDesc_ - Properties of the current Tool. This is where the
541 /// information is stored.
542 ToolDescription& toolDesc_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000543
544public:
545
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000546 explicit CollectToolProperties (ToolDescription& d)
547 : HandlerTable<CollectToolProperties>(this) , toolDesc_(d)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000548 {
549 if (!staticMembersInitialized_) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000550
551 AddHandler("actions", &CollectToolProperties::onActions);
552 AddHandler("cmd_line", &CollectToolProperties::onCmdLine);
553 AddHandler("in_language", &CollectToolProperties::onInLanguage);
554 AddHandler("join", &CollectToolProperties::onJoin);
555 AddHandler("out_language", &CollectToolProperties::onOutLanguage);
556 AddHandler("output_suffix", &CollectToolProperties::onOutputSuffix);
557 AddHandler("sink", &CollectToolProperties::onSink);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000558
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000559 staticMembersInitialized_ = true;
560 }
561 }
562
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000563private:
564
565 /// Property handlers --
566 /// Functions that extract information about tool properties from
567 /// DAG representation.
568
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000569 void onActions (const DagInit* d) {
570 checkNumberOfArguments(d, 1);
Mikhail Glushenkov8d489a82008-12-07 16:45:12 +0000571 Init* Case = d->getArg(0);
572 if (typeid(*Case) != typeid(DagInit) ||
573 static_cast<DagInit*>(Case)->getOperator()->getAsString() != "case")
574 throw
575 std::string("The argument to (actions) should be a 'case' construct!");
576 toolDesc_.Actions = Case;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000577 }
578
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000579 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000580 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000581 toolDesc_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000582 }
583
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000584 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000585 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000586 Init* arg = d->getArg(0);
587
588 // Find out the argument's type.
589 if (typeid(*arg) == typeid(StringInit)) {
590 // It's a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000591 toolDesc_.InLanguage.push_back(InitPtrToString(arg));
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000592 }
593 else {
594 // It's a list.
595 const ListInit& lst = InitPtrToList(arg);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000596 StrVector& out = toolDesc_.InLanguage;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000597
598 // Copy strings to the output vector.
599 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
600 B != E; ++B) {
601 out.push_back(InitPtrToString(*B));
602 }
603
604 // Remove duplicates.
605 std::sort(out.begin(), out.end());
606 StrVector::iterator newE = std::unique(out.begin(), out.end());
607 out.erase(newE, out.end());
608 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000609 }
610
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000611 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000612 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000613 toolDesc_.setJoin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000614 }
615
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000616 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000617 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000618 toolDesc_.OutLanguage = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000619 }
620
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000621 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000622 checkNumberOfArguments(d, 1);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000623 toolDesc_.OutputSuffix = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000624 }
625
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000626 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000627 checkNumberOfArguments(d, 0);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000628 toolDesc_.setSink();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000629 }
630
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000631};
632
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000633
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000634/// CollectToolDescriptions - Gather information about tool properties
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000635/// from the parsed TableGen data (basically a wrapper for the
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000636/// CollectToolProperties function object).
637void CollectToolDescriptions (RecordVector::const_iterator B,
638 RecordVector::const_iterator E,
639 ToolDescriptions& ToolDescs)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000640{
641 // Iterate over a properties list of every Tool definition
642 for (;B!=E;++B) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +0000643 const Record* T = *B;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000644 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000645 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000646
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000647 IntrusiveRefCntPtr<ToolDescription>
648 ToolDesc(new ToolDescription(T->getName()));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000649
650 std::for_each(PropList->begin(), PropList->end(),
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000651 CollectToolProperties(*ToolDesc));
652 ToolDescs.push_back(ToolDesc);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000653 }
654}
655
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000656/// FillInEdgeVector - Merge all compilation graph definitions into
657/// one single edge list.
658void FillInEdgeVector(RecordVector::const_iterator B,
659 RecordVector::const_iterator E, RecordVector& Out) {
660 for (; B != E; ++B) {
661 const ListInit* edges = (*B)->getValueAsListInit("edges");
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000662
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000663 for (unsigned i = 0; i < edges->size(); ++i)
664 Out.push_back(edges->getElementAsRecord(i));
665 }
666}
667
668/// CalculatePriority - Calculate the priority of this plugin.
669int CalculatePriority(RecordVector::const_iterator B,
670 RecordVector::const_iterator E) {
671 int total = 0;
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +0000672 for (; B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000673 total += static_cast<int>((*B)->getValueAsInt("priority"));
674 }
675 return total;
676}
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000677
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000678/// NotInGraph - Helper function object for FilterNotInGraph.
679struct NotInGraph {
680private:
681 const llvm::StringSet<>& ToolsInGraph_;
682
683public:
684 NotInGraph(const llvm::StringSet<>& ToolsInGraph)
685 : ToolsInGraph_(ToolsInGraph)
686 {}
687
688 bool operator()(const IntrusiveRefCntPtr<ToolDescription>& x) {
689 return (ToolsInGraph_.count(x->Name) == 0);
690 }
691};
692
693/// FilterNotInGraph - Filter out from ToolDescs all Tools not
694/// mentioned in the compilation graph definition.
695void FilterNotInGraph (const RecordVector& EdgeVector,
696 ToolDescriptions& ToolDescs) {
697
698 // List all tools mentioned in the graph.
699 llvm::StringSet<> ToolsInGraph;
700
701 for (RecordVector::const_iterator B = EdgeVector.begin(),
702 E = EdgeVector.end(); B != E; ++B) {
703
704 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000705 const std::string& NodeA = Edge->getValueAsString("a");
706 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000707
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000708 if (NodeA != "root")
709 ToolsInGraph.insert(NodeA);
710 ToolsInGraph.insert(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000711 }
712
713 // Filter ToolPropertiesList.
714 ToolDescriptions::iterator new_end =
715 std::remove_if(ToolDescs.begin(), ToolDescs.end(),
716 NotInGraph(ToolsInGraph));
717 ToolDescs.erase(new_end, ToolDescs.end());
718}
719
720/// FillInToolToLang - Fills in two tables that map tool names to
721/// (input, output) languages. Helper function used by TypecheckGraph().
722void FillInToolToLang (const ToolDescriptions& ToolDescs,
723 StringMap<StringSet<> >& ToolToInLang,
724 StringMap<std::string>& ToolToOutLang) {
725 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
726 E = ToolDescs.end(); B != E; ++B) {
727 const ToolDescription& D = *(*B);
728 for (StrVector::const_iterator B = D.InLanguage.begin(),
729 E = D.InLanguage.end(); B != E; ++B)
730 ToolToInLang[D.Name].insert(*B);
731 ToolToOutLang[D.Name] = D.OutLanguage;
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000732 }
733}
734
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000735/// TypecheckGraph - Check that names for output and input languages
736/// on all edges do match. This doesn't do much when the information
737/// about the whole graph is not available (i.e. when compiling most
738/// plugins).
739void TypecheckGraph (const RecordVector& EdgeVector,
740 const ToolDescriptions& ToolDescs) {
741 StringMap<StringSet<> > ToolToInLang;
742 StringMap<std::string> ToolToOutLang;
743
744 FillInToolToLang(ToolDescs, ToolToInLang, ToolToOutLang);
745 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
746 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
747
748 for (RecordVector::const_iterator B = EdgeVector.begin(),
749 E = EdgeVector.end(); B != E; ++B) {
750 const Record* Edge = *B;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000751 const std::string& NodeA = Edge->getValueAsString("a");
752 const std::string& NodeB = Edge->getValueAsString("b");
753 StringMap<std::string>::iterator IA = ToolToOutLang.find(NodeA);
754 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(NodeB);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000755
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000756 if (NodeA != "root") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000757 if (IA != IAE && IB != IBE && IB->second.count(IA->second) == 0)
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000758 throw "Edge " + NodeA + "->" + NodeB
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000759 + ": output->input language mismatch";
760 }
761
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +0000762 if (NodeB == "root")
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000763 throw std::string("Edges back to the root are not allowed!");
764 }
765}
766
767/// WalkCase - Walks the 'case' expression DAG and invokes
768/// TestCallback on every test, and StatementCallback on every
769/// statement. Handles 'case' nesting, but not the 'and' and 'or'
770/// combinators.
771// TODO: Re-implement EmitCaseConstructHandler on top of this function?
772template <typename F1, typename F2>
773void WalkCase(Init* Case, F1 TestCallback, F2 StatementCallback) {
774 const DagInit& d = InitPtrToDag(Case);
775 bool even = false;
776 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
777 B != E; ++B) {
778 Init* arg = *B;
779 if (even && dynamic_cast<DagInit*>(arg)
780 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
781 WalkCase(arg, TestCallback, StatementCallback);
782 else if (!even)
783 TestCallback(arg);
784 else
785 StatementCallback(arg);
786 even = !even;
787 }
788}
789
790/// ExtractOptionNames - A helper function object used by
791/// CheckForSuperfluousOptions() to walk the 'case' DAG.
792class ExtractOptionNames {
793 llvm::StringSet<>& OptionNames_;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000794
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000795 void processDag(const Init* Statement) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000796 const DagInit& Stmt = InitPtrToDag(Statement);
797 const std::string& ActionName = Stmt.getOperator()->getAsString();
798 if (ActionName == "forward" || ActionName == "forward_as" ||
799 ActionName == "unpack_values" || ActionName == "switch_on" ||
800 ActionName == "parameter_equals" || ActionName == "element_in_list" ||
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000801 ActionName == "not_empty" || ActionName == "empty") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000802 checkNumberOfArguments(&Stmt, 1);
803 const std::string& Name = InitPtrToString(Stmt.getArg(0));
804 OptionNames_.insert(Name);
805 }
806 else if (ActionName == "and" || ActionName == "or") {
807 for (unsigned i = 0, NumArgs = Stmt.getNumArgs(); i < NumArgs; ++i) {
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000808 this->processDag(Stmt.getArg(i));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000809 }
810 }
811 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +0000812
813public:
814 ExtractOptionNames(llvm::StringSet<>& OptionNames) : OptionNames_(OptionNames)
815 {}
816
817 void operator()(const Init* Statement) {
818 if (typeid(*Statement) == typeid(ListInit)) {
819 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
820 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
821 B != E; ++B)
822 this->processDag(*B);
823 }
824 else {
825 this->processDag(Statement);
826 }
827 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000828};
829
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000830/// CheckForSuperfluousOptions - Check that there are no side
831/// effect-free options (specified only in the OptionList). Otherwise,
832/// output a warning.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000833void CheckForSuperfluousOptions (const RecordVector& Edges,
834 const ToolDescriptions& ToolDescs,
835 const OptionDescriptions& OptDescs) {
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000836 llvm::StringSet<> nonSuperfluousOptions;
837
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000838 // Add all options mentioned in the ToolDesc.Actions to the set of
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000839 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000840 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
841 E = ToolDescs.end(); B != E; ++B) {
842 const ToolDescription& TD = *(*B);
843 ExtractOptionNames Callback(nonSuperfluousOptions);
844 if (TD.Actions)
845 WalkCase(TD.Actions, Callback, Callback);
846 }
847
848 // Add all options mentioned in the 'case' clauses of the
849 // OptionalEdges of the compilation graph to the set of
850 // non-superfluous options.
851 for (RecordVector::const_iterator B = Edges.begin(), E = Edges.end();
852 B != E; ++B) {
853 const Record* Edge = *B;
854 DagInit* Weight = Edge->getValueAsDag("weight");
855
856 if (!isDagEmpty(Weight))
857 WalkCase(Weight, ExtractOptionNames(nonSuperfluousOptions), Id());
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000858 }
859
860 // Check that all options in OptDescs belong to the set of
861 // non-superfluous options.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000862 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000863 E = OptDescs.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000864 const OptionDescription& Val = B->second;
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000865 if (!nonSuperfluousOptions.count(Val.Name)
866 && Val.Type != OptionType::Alias)
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000867 llvm::cerr << "Warning: option '-" << Val.Name << "' has no effect! "
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000868 "Probable cause: this option is specified only in the OptionList.\n";
869 }
870}
871
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000872/// EmitCaseTest1Arg - Helper function used by
873/// EmitCaseConstructHandler.
874bool EmitCaseTest1Arg(const std::string& TestName,
875 const DagInit& d,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000876 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000877 std::ostream& O) {
878 checkNumberOfArguments(&d, 1);
879 const std::string& OptName = InitPtrToString(d.getArg(0));
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000880
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000881 if (TestName == "switch_on") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000882 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
883 if (!OptionType::IsSwitch(OptDesc.Type))
884 throw OptName + ": incorrect option type - should be a switch!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000885 O << OptDesc.GenVariableName();
886 return true;
887 } else if (TestName == "input_languages_contain") {
888 O << "InLangs.count(\"" << OptName << "\") != 0";
889 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000890 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000891 // This works only for single-argument Tool::GenerateAction. Join
892 // tools can process several files in different languages simultaneously.
893
894 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000895 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000896 return true;
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000897 } else if (TestName == "not_empty" || TestName == "empty") {
898 const char* Test = (TestName == "empty") ? "" : "!";
899
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000900 if (OptName == "o") {
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000901 O << Test << "OutputFilename.empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000902 return true;
903 }
904 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000905 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
906 if (OptionType::IsSwitch(OptDesc.Type))
907 throw OptName
908 + ": incorrect option type - should be a list or parameter!";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000909 O << Test << OptDesc.GenVariableName() << ".empty()";
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000910 return true;
911 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000912 }
913
914 return false;
915}
916
917/// EmitCaseTest2Args - Helper function used by
918/// EmitCaseConstructHandler.
919bool EmitCaseTest2Args(const std::string& TestName,
920 const DagInit& d,
921 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000922 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000923 std::ostream& O) {
924 checkNumberOfArguments(&d, 2);
925 const std::string& OptName = InitPtrToString(d.getArg(0));
926 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000927 const OptionDescription& OptDesc = OptDescs.FindOption(OptName);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000928
929 if (TestName == "parameter_equals") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000930 if (!OptionType::IsParameter(OptDesc.Type))
931 throw OptName + ": incorrect option type - should be a parameter!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000932 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
933 return true;
934 }
935 else if (TestName == "element_in_list") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000936 if (!OptionType::IsList(OptDesc.Type))
937 throw OptName + ": incorrect option type - should be a list!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000938 const std::string& VarName = OptDesc.GenVariableName();
939 O << "std::find(" << VarName << ".begin(),\n"
940 << IndentLevel << Indent1 << VarName << ".end(), \""
941 << OptArg << "\") != " << VarName << ".end()";
942 return true;
943 }
944
945 return false;
946}
947
948// Forward declaration.
949// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
950void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000951 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000952 std::ostream& O);
953
954/// EmitLogicalOperationTest - Helper function used by
955/// EmitCaseConstructHandler.
956void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
957 const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000958 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000959 std::ostream& O) {
960 O << '(';
961 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000962 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000963 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
964 if (j != NumArgs - 1)
965 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
966 else
967 O << ')';
968 }
969}
970
971/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
972void EmitCaseTest(const DagInit& d, const char* IndentLevel,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000973 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000974 std::ostream& O) {
975 const std::string& TestName = d.getOperator()->getAsString();
976
977 if (TestName == "and")
978 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
979 else if (TestName == "or")
980 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
981 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
982 return;
983 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
984 return;
985 else
986 throw TestName + ": unknown edge property!";
987}
988
989// Emit code that handles the 'case' construct.
990// Takes a function object that should emit code for every case clause.
991// Callback's type is
992// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
993template <typename F>
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000994void EmitCaseConstructHandler(const Init* Dag, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +0000995 F Callback, bool EmitElseIf,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000996 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000997 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +0000998 const DagInit* d = &InitPtrToDag(Dag);
999 if (d->getOperator()->getAsString() != "case")
1000 throw std::string("EmitCaseConstructHandler should be invoked"
1001 " only on 'case' expressions!");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001002
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001003 unsigned numArgs = d->getNumArgs();
1004 if (d->getNumArgs() < 2)
1005 throw "There should be at least one clause in the 'case' expression:\n"
1006 + d->getAsString();
1007
1008 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001009 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001010
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001011 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001012 if (Test.getOperator()->getAsString() == "default") {
1013 if (i+2 != numArgs)
1014 throw std::string("The 'default' clause should be the last in the"
1015 "'case' construct!");
1016 O << IndentLevel << "else {\n";
1017 }
1018 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001019 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001020 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1021 O << ") {\n";
1022 }
1023
Mikhail Glushenkov31681512008-05-30 06:15:47 +00001024 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001025 ++i;
1026 if (i == numArgs)
1027 throw "Case construct handler: no corresponding action "
1028 "found for the test " + Test.getAsString() + '!';
1029
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001030 Init* arg = d->getArg(i);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001031 const DagInit* nd = dynamic_cast<DagInit*>(arg);
1032 if (nd && (nd->getOperator()->getAsString() == "case")) {
1033 // Handle the nested 'case'.
1034 EmitCaseConstructHandler(nd, (std::string(IndentLevel) + Indent1).c_str(),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001035 Callback, EmitElseIf, OptDescs, O);
1036 }
1037 else {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001038 Callback(arg, (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001039 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001040 O << IndentLevel << "}\n";
1041 }
1042}
1043
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001044/// SubstituteSpecialCommands - Perform string substitution for $CALL
1045/// and $ENV. Helper function used by EmitCmdLineVecFill().
1046std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001047 size_t cparen = cmd.find(")");
1048 std::string ret;
1049
1050 if (cmd.find("$CALL(") == 0) {
1051 if (cmd.size() == 6)
1052 throw std::string("$CALL invocation: empty argument list!");
1053
1054 ret += "hooks::";
1055 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
1056 ret += "()";
1057 }
1058 else if (cmd.find("$ENV(") == 0) {
1059 if (cmd.size() == 5)
1060 throw std::string("$ENV invocation: empty argument list!");
1061
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001062 ret += "checkCString(std::getenv(\"";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001063 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001064 ret += "\"))";
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001065 }
1066 else {
1067 throw "Unknown special command: " + cmd;
1068 }
1069
1070 if (cmd.begin() + cparen + 1 != cmd.end()) {
1071 ret += " + std::string(\"";
1072 ret += (cmd.c_str() + cparen + 1);
1073 ret += "\")";
1074 }
1075
1076 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001077}
1078
1079/// EmitCmdLineVecFill - Emit code that fills in the command line
1080/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001081void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001082 bool IsJoin, const char* IndentLevel,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001083 std::ostream& O) {
1084 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001085 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001086 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001087 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001088
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001089 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001090 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001091 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001092 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001093 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001094 if (cmd.at(0) == '$') {
1095 if (cmd == "$INFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001096 if (IsJoin)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001097 O << "for (PathVector::const_iterator B = inFiles.begin()"
1098 << ", E = inFiles.end();\n"
1099 << IndentLevel << "B != E; ++B)\n"
1100 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1101 else
1102 O << "vec.push_back(inFile.toString());\n";
1103 }
1104 else if (cmd == "$OUTFILE") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001105 O << "vec.push_back(out_file);\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001106 }
1107 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001108 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
1109 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001110 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001111 }
1112 else {
1113 O << "vec.push_back(\"" << cmd << "\");\n";
1114 }
1115 }
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001116 O << IndentLevel << "cmd = "
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001117 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
1118 : "\"" + StrVec[0] + "\"")
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001119 << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001120}
1121
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001122/// EmitCmdLineVecFillCallback - A function object wrapper around
1123/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1124/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001125class EmitCmdLineVecFillCallback {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001126 bool IsJoin;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001127 const std::string& ToolName;
1128 public:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001129 EmitCmdLineVecFillCallback(bool J, const std::string& TN)
1130 : IsJoin(J), ToolName(TN) {}
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001131
1132 void operator()(const Init* Statement, const char* IndentLevel,
1133 std::ostream& O) const
1134 {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001135 EmitCmdLineVecFill(Statement, ToolName, IsJoin,
1136 IndentLevel, O);
1137 }
1138};
1139
1140/// EmitForwardOptionPropertyHandlingCode - Helper function used to
1141/// implement EmitActionHandler. Emits code for
1142/// handling the (forward) and (forward_as) option properties.
1143void EmitForwardOptionPropertyHandlingCode (const OptionDescription& D,
1144 const char* Indent,
1145 const std::string& NewName,
1146 std::ostream& O) {
1147 const std::string& Name = NewName.empty()
1148 ? ("-" + D.Name)
1149 : NewName;
1150
1151 switch (D.Type) {
1152 case OptionType::Switch:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001153 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1154 break;
1155 case OptionType::Parameter:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001156 O << Indent << "vec.push_back(\"" << Name << "\");\n";
1157 O << Indent << "vec.push_back(" << D.GenVariableName() << ");\n";
1158 break;
1159 case OptionType::Prefix:
1160 O << Indent << "vec.push_back(\"" << Name << "\" + "
1161 << D.GenVariableName() << ");\n";
1162 break;
1163 case OptionType::PrefixList:
1164 O << Indent << "for (" << D.GenTypeDeclaration()
1165 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1166 << Indent << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
1167 << Indent << Indent1 << "vec.push_back(\"" << Name << "\" + "
1168 << "*B);\n";
1169 break;
1170 case OptionType::ParameterList:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001171 O << Indent << "for (" << D.GenTypeDeclaration()
1172 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1173 << Indent << "E = " << D.GenVariableName()
1174 << ".end() ; B != E; ++B) {\n"
1175 << Indent << Indent1 << "vec.push_back(\"" << Name << "\");\n"
1176 << Indent << Indent1 << "vec.push_back(*B);\n"
1177 << Indent << "}\n";
1178 break;
1179 case OptionType::Alias:
1180 default:
1181 throw std::string("Aliases are not allowed in tool option descriptions!");
1182 }
1183}
1184
1185/// EmitActionHandler - Emit code that handles actions. Used by
1186/// EmitGenerateActionMethod() as an argument to
1187/// EmitCaseConstructHandler().
1188class EmitActionHandler {
1189 const OptionDescriptions& OptDescs;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001190
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001191 void processActionDag(const Init* Statement, const char* IndentLevel,
1192 std::ostream& O) const
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001193 {
1194 const DagInit& Dag = InitPtrToDag(Statement);
1195 const std::string& ActionName = Dag.getOperator()->getAsString();
1196
1197 if (ActionName == "append_cmd") {
1198 checkNumberOfArguments(&Dag, 1);
1199 const std::string& Cmd = InitPtrToString(Dag.getArg(0));
1200 O << IndentLevel << "vec.push_back(\"" << Cmd << "\");\n";
1201 }
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001202 else if (ActionName == "error") {
1203 O << IndentLevel << "throw std::runtime_error(\"" <<
1204 (Dag.getNumArgs() >= 1 ? InitPtrToString(Dag.getArg(0))
1205 : "Unknown error!")
1206 << "\");\n";
1207 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001208 else if (ActionName == "forward") {
1209 checkNumberOfArguments(&Dag, 1);
1210 const std::string& Name = InitPtrToString(Dag.getArg(0));
1211 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1212 IndentLevel, "", O);
1213 }
1214 else if (ActionName == "forward_as") {
1215 checkNumberOfArguments(&Dag, 2);
1216 const std::string& Name = InitPtrToString(Dag.getArg(0));
1217 const std::string& NewName = InitPtrToString(Dag.getArg(0));
1218 EmitForwardOptionPropertyHandlingCode(OptDescs.FindOption(Name),
1219 IndentLevel, NewName, O);
1220 }
1221 else if (ActionName == "output_suffix") {
1222 checkNumberOfArguments(&Dag, 1);
1223 const std::string& OutSuf = InitPtrToString(Dag.getArg(0));
1224 O << IndentLevel << "output_suffix = \"" << OutSuf << "\";\n";
1225 }
1226 else if (ActionName == "stop_compilation") {
1227 O << IndentLevel << "stop_compilation = true;\n";
1228 }
1229 else if (ActionName == "unpack_values") {
1230 checkNumberOfArguments(&Dag, 1);
1231 const std::string& Name = InitPtrToString(Dag.getArg(0));
1232 const OptionDescription& D = OptDescs.FindOption(Name);
1233
1234 if (OptionType::IsList(D.Type)) {
1235 O << IndentLevel << "for (" << D.GenTypeDeclaration()
1236 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1237 << IndentLevel << "E = " << D.GenVariableName()
1238 << ".end(); B != E; ++B)\n"
1239 << IndentLevel << Indent1 << "llvm::SplitString(*B, vec, \",\");\n";
1240 }
1241 else if (OptionType::IsParameter(D.Type)){
1242 O << Indent3 << "llvm::SplitString("
1243 << D.GenVariableName() << ", vec, \",\");\n";
1244 }
1245 else {
1246 throw "Option '" + D.Name +
1247 "': switches can't have the 'unpack_values' property!";
1248 }
1249 }
1250 else {
1251 throw "Unknown action name: " + ActionName + "!";
1252 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001253 }
Mikhail Glushenkovccce1922008-12-07 16:42:22 +00001254 public:
1255 EmitActionHandler(const OptionDescriptions& OD)
1256 : OptDescs(OD) {}
1257
1258 void operator()(const Init* Statement, const char* IndentLevel,
1259 std::ostream& O) const
1260 {
1261 if (typeid(*Statement) == typeid(ListInit)) {
1262 const ListInit& DagList = *static_cast<const ListInit*>(Statement);
1263 for (ListInit::const_iterator B = DagList.begin(), E = DagList.end();
1264 B != E; ++B)
1265 this->processActionDag(*B, IndentLevel, O);
1266 }
1267 else {
1268 this->processActionDag(Statement, IndentLevel, O);
1269 }
1270 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001271};
1272
1273// EmitGenerateActionMethod - Emit one of two versions of the
1274// Tool::GenerateAction() method.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001275void EmitGenerateActionMethod (const ToolDescription& D,
1276 const OptionDescriptions& OptDescs,
1277 bool IsJoin, std::ostream& O) {
1278 if (IsJoin)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001279 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1280 else
1281 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1282
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001283 O << Indent2 << "bool HasChildren,\n"
1284 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001285 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1286 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001287 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001288 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001289 << Indent2 << "std::vector<std::string> vec;\n"
1290 << Indent2 << "bool stop_compilation = !HasChildren;\n"
1291 << Indent2 << "const char* output_suffix = \"" << D.OutputSuffix << "\";\n"
1292 << Indent2 << "std::string out_file;\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001293
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001294 // For every understood option, emit handling code.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001295 if (D.Actions)
1296 EmitCaseConstructHandler(D.Actions, Indent2, EmitActionHandler(OptDescs),
1297 false, OptDescs, O);
1298
1299 O << '\n' << Indent2
1300 << "out_file = OutFilename(" << (IsJoin ? "sys::Path(),\n" : "inFile,\n")
1301 << Indent3 << "TempDir, stop_compilation, output_suffix).toString();\n\n";
1302
1303 // cmd_line is either a string or a 'case' construct.
1304 if (!D.CmdLine)
1305 throw "Tool " + D.Name + " has no cmd_line property!";
1306 else if (typeid(*D.CmdLine) == typeid(StringInit))
1307 EmitCmdLineVecFill(D.CmdLine, D.Name, IsJoin, Indent2, O);
1308 else
1309 EmitCaseConstructHandler(D.CmdLine, Indent2,
1310 EmitCmdLineVecFillCallback(IsJoin, D.Name),
1311 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001312
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001313 // Handle the Sink property.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001314 if (D.isSink()) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001315 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1316 << Indent3 << "vec.insert(vec.end(), "
1317 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1318 << Indent2 << "}\n";
1319 }
1320
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001321 O << Indent2 << "return Action(cmd, vec, stop_compilation, out_file);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001322 << Indent1 << "}\n\n";
1323}
1324
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001325/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1326/// a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001327void EmitGenerateActionMethods (const ToolDescription& ToolDesc,
1328 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001329 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001330 if (!ToolDesc.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001331 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001332 << Indent2 << "bool HasChildren,\n"
1333 << Indent2 << "const llvm::sys::Path& TempDir,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001334 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1335 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001336 << Indent1 << "{\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001337 << Indent2 << "throw std::runtime_error(\"" << ToolDesc.Name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001338 << " is not a Join tool!\");\n"
1339 << Indent1 << "}\n\n";
1340 else
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001341 EmitGenerateActionMethod(ToolDesc, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001342
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001343 EmitGenerateActionMethod(ToolDesc, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001344}
1345
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001346/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1347/// methods for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001348void EmitInOutLanguageMethods (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001349 O << Indent1 << "const char** InputLanguages() const {\n"
1350 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001351 << Indent1 << "}\n\n";
1352
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001353 if (D.OutLanguage.empty())
1354 throw "Tool " + D.Name + " has no 'out_language' property!";
1355
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001356 O << Indent1 << "const char* OutputLanguage() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001357 << Indent2 << "return \"" << D.OutLanguage << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001358 << Indent1 << "}\n\n";
1359}
1360
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001361/// EmitNameMethod - Emit the Name() method for a given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001362void EmitNameMethod (const ToolDescription& D, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001363 O << Indent1 << "const char* Name() const {\n"
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001364 << Indent2 << "return \"" << D.Name << "\";\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001365 << Indent1 << "}\n\n";
1366}
1367
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001368/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1369/// class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001370void EmitIsJoinMethod (const ToolDescription& D, std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001371 O << Indent1 << "bool IsJoin() const {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001372 if (D.isJoin())
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001373 O << Indent2 << "return true;\n";
1374 else
1375 O << Indent2 << "return false;\n";
1376 O << Indent1 << "}\n\n";
1377}
1378
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001379/// EmitStaticMemberDefinitions - Emit static member definitions for a
1380/// given Tool class.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001381void EmitStaticMemberDefinitions(const ToolDescription& D, std::ostream& O) {
1382 if (D.InLanguage.empty())
1383 throw "Tool " + D.Name + " has no 'in_language' property!";
1384
1385 O << "const char* " << D.Name << "::InputLanguages_[] = {";
1386 for (StrVector::const_iterator B = D.InLanguage.begin(),
1387 E = D.InLanguage.end(); B != E; ++B)
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001388 O << '\"' << *B << "\", ";
1389 O << "0};\n\n";
1390}
1391
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001392/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001393void EmitToolClassDefinition (const ToolDescription& D,
1394 const OptionDescriptions& OptDescs,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001395 std::ostream& O) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001396 if (D.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001397 return;
1398
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001399 // Header
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001400 O << "class " << D.Name << " : public ";
1401 if (D.isJoin())
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001402 O << "JoinTool";
1403 else
1404 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001405
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001406 O << "{\nprivate:\n"
1407 << Indent1 << "static const char* InputLanguages_[];\n\n";
1408
1409 O << "public:\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001410 EmitNameMethod(D, O);
1411 EmitInOutLanguageMethods(D, O);
1412 EmitIsJoinMethod(D, O);
1413 EmitGenerateActionMethods(D, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001414
1415 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001416 O << "};\n";
1417
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001418 EmitStaticMemberDefinitions(D, O);
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001419
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001420}
1421
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001422/// EmitOptionDefintions - Iterate over a list of option descriptions
1423/// and emit registration code.
1424void EmitOptionDefintions (const OptionDescriptions& descs,
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001425 bool HasSink, bool HasExterns,
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001426 std::ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001427{
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001428 std::vector<OptionDescription> Aliases;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001429
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001430 // Emit static cl::Option variables.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001431 for (OptionDescriptions::const_iterator B = descs.begin(),
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001432 E = descs.end(); B!=E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001433 const OptionDescription& val = B->second;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001434
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001435 if (val.Type == OptionType::Alias) {
1436 Aliases.push_back(val);
1437 continue;
1438 }
1439
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001440 if (val.isExtern())
1441 O << "extern ";
1442
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001443 O << val.GenTypeDeclaration() << ' '
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001444 << val.GenVariableName();
1445
1446 if (val.isExtern()) {
1447 O << ";\n";
1448 continue;
1449 }
1450
1451 O << "(\"" << val.Name << '\"';
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001452
1453 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1454 O << ", cl::Prefix";
1455
1456 if (val.isRequired()) {
1457 switch (val.Type) {
1458 case OptionType::PrefixList:
1459 case OptionType::ParameterList:
1460 O << ", cl::OneOrMore";
1461 break;
1462 default:
1463 O << ", cl::Required";
1464 }
1465 }
1466
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +00001467 if (val.isReallyHidden() || val.isHidden()) {
1468 if (val.isRequired())
1469 O << " |";
1470 else
1471 O << ",";
1472 if (val.isReallyHidden())
1473 O << " cl::ReallyHidden";
1474 else
1475 O << " cl::Hidden";
1476 }
1477
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001478 if (!val.Help.empty())
1479 O << ", cl::desc(\"" << val.Help << "\")";
1480
1481 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001482 }
1483
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001484 // Emit the aliases (they should go after all the 'proper' options).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001485 for (std::vector<OptionDescription>::const_iterator
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001486 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001487 const OptionDescription& val = *B;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001488
1489 O << val.GenTypeDeclaration() << ' '
1490 << val.GenVariableName()
1491 << "(\"" << val.Name << '\"';
1492
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001493 const OptionDescription& D = descs.FindOption(val.Help);
1494 O << ", cl::aliasopt(" << D.GenVariableName() << ")";
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001495
1496 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1497 }
1498
1499 // Emit the sink option.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001500 if (HasSink)
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001501 O << (HasExterns ? "extern cl" : "cl")
1502 << "::list<std::string> " << SinkOptionName
1503 << (HasExterns ? ";\n" : "(cl::Sink);\n");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001504
1505 O << '\n';
1506}
1507
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001508/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001509void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1510{
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001511 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001512 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001513
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001514 // Get the relevant field out of RecordKeeper
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001515 const Record* LangMapRecord = Records.getDef("LanguageMap");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001516
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001517 // It is allowed for a plugin to have no language map.
1518 if (LangMapRecord) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001519
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001520 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1521 if (!LangsToSuffixesList)
1522 throw std::string("Error in the language map definition!");
1523
1524 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001525 const Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
Mikhail Glushenkovfa990682008-11-17 17:29:18 +00001526
1527 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1528 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1529
1530 for (unsigned i = 0; i < Suffixes->size(); ++i)
1531 O << Indent1 << "langMap[\""
1532 << InitPtrToString(Suffixes->getElement(i))
1533 << "\"] = \"" << Lang << "\";\n";
1534 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001535 }
1536
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001537 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001538}
1539
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001540/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1541/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001542void IncDecWeight (const Init* i, const char* IndentLevel,
1543 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001544 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001545 const std::string& OpName = d.getOperator()->getAsString();
1546
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001547 if (OpName == "inc_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001548 O << IndentLevel << "ret += ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001549 }
1550 else if (OpName == "dec_weight") {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001551 O << IndentLevel << "ret -= ";
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001552 }
1553 else if (OpName == "error") {
1554 O << IndentLevel << "throw std::runtime_error(\"" <<
1555 (d.getNumArgs() >= 1 ? InitPtrToString(d.getArg(0))
1556 : "Unknown error!")
1557 << "\");\n";
1558 return;
1559 }
1560
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001561 else
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +00001562 throw "Unknown operator in edge properties list: " + OpName + '!' +
1563 "Only 'inc_weight', 'dec_weight' and 'error' are allowed.";
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001564
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 << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001618
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001619 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1620 E = ToolDescs.end(); B != E; ++B)
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001621 O << Indent1 << "G.insertNode(new " << (*B)->Name << "());\n";
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001622
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001623 O << '\n';
1624
Mikhail Glushenkov719c25812008-11-12 00:05:17 +00001625 // Insert edges.
1626
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001627 int i = 0;
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001628 for (RecordVector::const_iterator B = EdgeVector.begin(),
1629 E = EdgeVector.end(); B != E; ++B) {
1630 const Record* Edge = *B;
1631 const std::string& NodeA = Edge->getValueAsString("a");
1632 const std::string& NodeB = Edge->getValueAsString("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001633 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001634
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001635 O << Indent1 << "G.insertEdge(\"" << NodeA << "\", ";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001636
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001637 if (isDagEmpty(Weight))
Mikhail Glushenkov02ec3a52008-12-07 16:44:47 +00001638 O << "new SimpleEdge(\"" << NodeB << "\")";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001639 else
1640 O << "new Edge" << i << "()";
1641
1642 O << ");\n";
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001643 ++i;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001644 }
1645
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001646 O << "}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001647}
1648
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001649/// ExtractHookNames - Extract the hook names from all instances of
1650/// $CALL(HookName) in the provided command line string. Helper
1651/// function used by FillInHookNames().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001652class ExtractHookNames {
1653 llvm::StringSet<>& HookNames_;
1654public:
1655 ExtractHookNames(llvm::StringSet<>& HookNames)
1656 : HookNames_(HookNames_) {}
1657
1658 void operator()(const Init* CmdLine) {
1659 StrVector cmds;
1660 llvm::SplitString(InitPtrToString(CmdLine), cmds);
1661 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1662 B != E; ++B) {
1663 const std::string& cmd = *B;
1664 if (cmd.find("$CALL(") == 0) {
1665 if (cmd.size() == 6)
1666 throw std::string("$CALL invocation: empty argument list!");
1667 HookNames_.insert(std::string(cmd.begin() + 6,
1668 cmd.begin() + cmd.find(")")));
1669 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001670 }
1671 }
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001672};
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001673
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001674/// FillInHookNames - Actually extract the hook names from all command
1675/// line strings. Helper function used by EmitHookDeclarations().
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001676void FillInHookNames(const ToolDescriptions& ToolDescs,
1677 llvm::StringSet<>& HookNames)
1678{
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001679 // For all command lines:
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001680 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1681 E = ToolDescs.end(); B != E; ++B) {
1682 const ToolDescription& D = *(*B);
1683 if (!D.CmdLine)
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001684 continue;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001685 if (dynamic_cast<StringInit*>(D.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001686 // This is a string.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001687 ExtractHookNames(HookNames).operator()(D.CmdLine);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001688 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001689 // This is a 'case' construct.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001690 WalkCase(D.CmdLine, Id(), ExtractHookNames(HookNames));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001691 }
1692}
1693
1694/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1695/// property records and emit hook function declaration for each
1696/// instance of $CALL(HookName).
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001697void EmitHookDeclarations(const ToolDescriptions& ToolDescs, std::ostream& O) {
1698 llvm::StringSet<> HookNames;
1699 FillInHookNames(ToolDescs, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001700 if (HookNames.empty())
1701 return;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001702
1703 O << "namespace hooks {\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001704 for (StringSet<>::const_iterator B = HookNames.begin(), E = HookNames.end();
1705 B != E; ++B)
1706 O << Indent1 << "std::string " << B->first() << "();\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001707
1708 O << "}\n\n";
1709}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001710
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001711/// EmitRegisterPlugin - Emit code to register this plugin.
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001712void EmitRegisterPlugin(int Priority, std::ostream& O) {
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001713 O << "struct Plugin : public llvmc::BasePlugin {\n\n"
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001714 << Indent1 << "int Priority() const { return " << Priority << "; }\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001715 << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
1716 << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
1717 << Indent1
1718 << "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
1719 << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
1720 << "};\n\n"
1721
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001722 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001723}
1724
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001725/// EmitIncludes - Emit necessary #include directives and some
1726/// additional declarations.
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001727void EmitIncludes(std::ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001728 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
1729 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1730 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001731
1732 << "#include \"llvm/ADT/StringExtras.h\"\n"
1733 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1734
1735 << "#include <cstdlib>\n"
1736 << "#include <stdexcept>\n\n"
1737
1738 << "using namespace llvm;\n"
1739 << "using namespace llvmc;\n\n"
1740
Mikhail Glushenkovb4890702008-11-12 12:41:18 +00001741 << "extern cl::opt<std::string> OutputFilename;\n\n"
1742
1743 << "inline const char* checkCString(const char* s)\n"
1744 << "{ return s == NULL ? \"\" : s; }\n\n";
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001745}
1746
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001747
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001748/// PluginData - Holds all information about a plugin.
1749struct PluginData {
1750 OptionDescriptions OptDescs;
1751 bool HasSink;
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001752 bool HasExterns;
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001753 ToolDescriptions ToolDescs;
1754 RecordVector Edges;
1755 int Priority;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001756};
1757
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001758/// HasSink - Go through the list of tool descriptions and check if
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001759/// there are any with the 'sink' property set.
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001760bool HasSink(const ToolDescriptions& ToolDescs) {
1761 for (ToolDescriptions::const_iterator B = ToolDescs.begin(),
1762 E = ToolDescs.end(); B != E; ++B)
1763 if ((*B)->isSink())
1764 return true;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001765
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001766 return false;
1767}
1768
1769/// HasExterns - Go through the list of option descriptions and check
1770/// if there are any external options.
1771bool HasExterns(const OptionDescriptions& OptDescs) {
1772 for (OptionDescriptions::const_iterator B = OptDescs.begin(),
1773 E = OptDescs.end(); B != E; ++B)
1774 if (B->second.isExtern())
1775 return true;
1776
1777 return false;
Mikhail Glushenkov973b3a32008-11-17 17:29:42 +00001778}
1779
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001780/// CollectPluginData - Collect tool and option properties,
1781/// compilation graph edges and plugin priority from the parse tree.
1782void CollectPluginData (const RecordKeeper& Records, PluginData& Data) {
1783 // Collect option properties.
1784 const RecordVector& OptionLists =
1785 Records.getAllDerivedDefinitions("OptionList");
1786 CollectOptionDescriptions(OptionLists.begin(), OptionLists.end(),
1787 Data.OptDescs);
1788
1789 // Collect tool properties.
1790 const RecordVector& Tools = Records.getAllDerivedDefinitions("Tool");
1791 CollectToolDescriptions(Tools.begin(), Tools.end(), Data.ToolDescs);
1792 Data.HasSink = HasSink(Data.ToolDescs);
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001793 Data.HasExterns = HasExterns(Data.OptDescs);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001794
1795 // Collect compilation graph edges.
1796 const RecordVector& CompilationGraphs =
1797 Records.getAllDerivedDefinitions("CompilationGraph");
1798 FillInEdgeVector(CompilationGraphs.begin(), CompilationGraphs.end(),
1799 Data.Edges);
1800
1801 // Calculate the priority of this plugin.
1802 const RecordVector& Priorities =
1803 Records.getAllDerivedDefinitions("PluginPriority");
1804 Data.Priority = CalculatePriority(Priorities.begin(), Priorities.end());
Mikhail Glushenkoveb71ecf2008-11-17 17:30:25 +00001805}
1806
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001807/// CheckPluginData - Perform some sanity checks on the collected data.
1808void CheckPluginData(PluginData& Data) {
1809 // Filter out all tools not mentioned in the compilation graph.
1810 FilterNotInGraph(Data.Edges, Data.ToolDescs);
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001811
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001812 // Typecheck the compilation graph.
1813 TypecheckGraph(Data.Edges, Data.ToolDescs);
1814
1815 // Check that there are no options without side effects (specified
1816 // only in the OptionList).
1817 CheckForSuperfluousOptions(Data.Edges, Data.ToolDescs, Data.OptDescs);
1818
Mikhail Glushenkovd24c1292008-11-28 00:13:47 +00001819}
1820
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001821void EmitPluginCode(const PluginData& Data, std::ostream& O) {
1822 // Emit file header.
1823 EmitIncludes(O);
1824
1825 // Emit global option registration code.
Mikhail Glushenkov9111ba22008-12-07 16:42:47 +00001826 EmitOptionDefintions(Data.OptDescs, Data.HasSink, Data.HasExterns, O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001827
1828 // Emit hook declarations.
1829 EmitHookDeclarations(Data.ToolDescs, O);
1830
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001831 O << "namespace {\n\n";
1832
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001833 // 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
Mikhail Glushenkov64046a72008-12-11 10:34:18 +00001851 O << "} // End anonymous namespace.\n";
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001852 // EOF
1853}
1854
1855
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001856// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001857}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001858
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001859/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001860void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001861 try {
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001862 PluginData Data;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001863
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001864 CollectPluginData(Records, Data);
1865 CheckPluginData(Data);
1866
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001867 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov4840b4c2008-12-07 16:41:11 +00001868 EmitPluginCode(Data, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001869
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001870 } catch (std::exception& Error) {
1871 throw Error.what() + std::string(" - usually this means a syntax error.");
1872 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001873}