blob: 7836172a44ca389198801b1bdaa241d8351ec6eb [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 = " ";
48const char * Indent4 = " ";
49
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000050// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000051const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
52
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000053// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000054const char * SinkOptionName = "AutoGeneratedSinkOption";
55
56//===----------------------------------------------------------------------===//
57/// Helper functions
58
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000059int InitPtrToInt(const Init* ptr) {
60 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000061 return val.getValue();
62}
63
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000064const std::string& InitPtrToString(const Init* ptr) {
65 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
66 return val.getValue();
67}
68
69const ListInit& InitPtrToList(const Init* ptr) {
70 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
71 return val;
72}
73
74const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000075 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000076 return val;
77}
78
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000079// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000080// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000081void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
82 if (d->getNumArgs() < min_arguments)
83 throw "Property " + d->getOperator()->getAsString()
84 + " has too few arguments!";
85}
86
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000087// isDagEmpty - is this DAG marked with an empty marker?
88bool isDagEmpty (const DagInit* d) {
89 return d->getOperator()->getAsString() == "empty";
90}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000091
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000092//===----------------------------------------------------------------------===//
93/// Back-end specific code
94
95// A command-line option can have one of the following types:
96//
Mikhail Glushenkovb623c322008-05-30 06:22:52 +000097// Alias - an alias for another option.
98//
99// Switch - a simple switch without arguments, e.g. -O2
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000100//
101// Parameter - an option that takes one(and only one) argument, e.g. -o file,
102// --output=file
103//
104// ParameterList - same as Parameter, but more than one occurence
105// of the option is allowed, e.g. -lm -lpthread
106//
107// Prefix - argument is everything after the prefix,
108// e.g. -Wa,-foo,-bar, -DNAME=VALUE
109//
110// PrefixList - same as Prefix, but more than one option occurence is
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000111// allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000112
113namespace OptionType {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000114 enum OptionType { Alias, Switch,
115 Parameter, ParameterList, Prefix, PrefixList};
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000116}
117
118bool IsListOptionType (OptionType::OptionType t) {
119 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
120}
121
122// Code duplication here is necessary because one option can affect
123// several tools and those tools may have different actions associated
124// with this option. GlobalOptionDescriptions are used to generate
125// the option registration code, while ToolOptionDescriptions are used
126// to generate tool-specific code.
127
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000128/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000129struct OptionDescription {
130 OptionType::OptionType Type;
131 std::string Name;
132
133 OptionDescription(OptionType::OptionType t = OptionType::Switch,
134 const std::string& n = "")
135 : Type(t), Name(n)
136 {}
137
138 const char* GenTypeDeclaration() const {
139 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000140 case OptionType::Alias:
141 return "cl::alias";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000142 case OptionType::PrefixList:
143 case OptionType::ParameterList:
144 return "cl::list<std::string>";
145 case OptionType::Switch:
146 return "cl::opt<bool>";
147 case OptionType::Parameter:
148 case OptionType::Prefix:
149 default:
150 return "cl::opt<std::string>";
151 }
152 }
153
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000154 // Escape commas and other symbols not allowed in the C++ variable
155 // names. Makes it possible to use options with names like "Wa,"
156 // (useful for prefix options).
157 std::string EscapeVariableName(const std::string& Var) const {
158 std::string ret;
159 for (unsigned i = 0; i != Var.size(); ++i) {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000160 char cur_char = Var[i];
161 if (cur_char == ',') {
162 ret += "_comma_";
163 }
164 else if (cur_char == '+') {
165 ret += "_plus_";
166 }
Anton Korobeynikov992846d2008-11-08 10:16:21 +0000167 else if (cur_char == '-') {
168 ret += "_dash_";
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000169 }
170 else {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000171 ret.push_back(cur_char);
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000172 }
173 }
174 return ret;
175 }
176
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000177 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000178 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000179 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000180 case OptionType::Alias:
181 return "AutoGeneratedAlias" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000182 case OptionType::Switch:
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000183 return "AutoGeneratedSwitch" + EscapedName;
184 case OptionType::Prefix:
185 return "AutoGeneratedPrefix" + EscapedName;
186 case OptionType::PrefixList:
187 return "AutoGeneratedPrefixList" + EscapedName;
188 case OptionType::Parameter:
189 return "AutoGeneratedParameter" + EscapedName;
190 case OptionType::ParameterList:
191 default:
192 return "AutoGeneratedParameterList" + EscapedName;
193 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000194 }
195
196};
197
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000198// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000199
200namespace GlobalOptionDescriptionFlags {
201 enum GlobalOptionDescriptionFlags { Required = 0x1 };
202}
203
204struct GlobalOptionDescription : public OptionDescription {
205 std::string Help;
206 unsigned Flags;
207
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000208 // We need to provide a default constructor because
209 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000210 GlobalOptionDescription() : OptionDescription(), Flags(0)
211 {}
212
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000213 GlobalOptionDescription (OptionType::OptionType t, const std::string& n,
214 const std::string& h = DefaultHelpString)
215 : OptionDescription(t, n), Help(h), Flags(0)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000216 {}
217
218 bool isRequired() const {
219 return Flags & GlobalOptionDescriptionFlags::Required;
220 }
221 void setRequired() {
222 Flags |= GlobalOptionDescriptionFlags::Required;
223 }
224
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000225 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000226 void Merge (const GlobalOptionDescription& other)
227 {
228 if (other.Type != Type)
229 throw "Conflicting definitions for the option " + Name + "!";
230
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000231 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000232 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000233 else if (other.Help != DefaultHelpString) {
234 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000235 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000236 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000237
238 Flags |= other.Flags;
239 }
240};
241
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000242/// GlobalOptionDescriptions - A GlobalOptionDescription array
243/// together with some flags affecting generation of option
244/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000245struct GlobalOptionDescriptions {
246 typedef StringMap<GlobalOptionDescription> container_type;
247 typedef container_type::const_iterator const_iterator;
248
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000249 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000250 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000251 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000252 bool HasSink;
253
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000254 /// FindOption - exception-throwing wrapper for find().
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000255 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
256 const_iterator I = Descriptions.find(OptName);
257 if (I != Descriptions.end())
258 return I->second;
259 else
260 throw OptName + ": no such option!";
261 }
262
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000263 /// insertDescription - Insert new GlobalOptionDescription into
264 /// GlobalOptionDescriptions list
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000265 void insertDescription (const GlobalOptionDescription& o)
266 {
267 container_type::iterator I = Descriptions.find(o.Name);
268 if (I != Descriptions.end()) {
269 GlobalOptionDescription& D = I->second;
270 D.Merge(o);
271 }
272 else {
273 Descriptions[o.Name] = o;
274 }
275 }
276
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000277 // Support for STL-style iteration
278 const_iterator begin() const { return Descriptions.begin(); }
279 const_iterator end() const { return Descriptions.end(); }
280};
281
282
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000283// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000284
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000285// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000286namespace ToolOptionDescriptionFlags {
287 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
288 Forward = 0x2, UnpackValues = 0x4};
289}
290namespace OptionPropertyType {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000291 enum OptionPropertyType { AppendCmd, ForwardAs, OutputSuffix };
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000292}
293
294typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
295OptionProperty;
296typedef SmallVector<OptionProperty, 4> OptionPropertyList;
297
298struct ToolOptionDescription : public OptionDescription {
299 unsigned Flags;
300 OptionPropertyList Props;
301
302 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000303 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000304
305 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
306 : OptionDescription(t, n)
307 {}
308
309 // Various boolean properties
310 bool isStopCompilation() const {
311 return Flags & ToolOptionDescriptionFlags::StopCompilation;
312 }
313 void setStopCompilation() {
314 Flags |= ToolOptionDescriptionFlags::StopCompilation;
315 }
316
317 bool isForward() const {
318 return Flags & ToolOptionDescriptionFlags::Forward;
319 }
320 void setForward() {
321 Flags |= ToolOptionDescriptionFlags::Forward;
322 }
323
324 bool isUnpackValues() const {
325 return Flags & ToolOptionDescriptionFlags::UnpackValues;
326 }
327 void setUnpackValues() {
328 Flags |= ToolOptionDescriptionFlags::UnpackValues;
329 }
330
331 void AddProperty (OptionPropertyType::OptionPropertyType t,
332 const std::string& val)
333 {
334 Props.push_back(std::make_pair(t, val));
335 }
336};
337
338typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
339
340// Tool information record
341
342namespace ToolFlags {
343 enum ToolFlags { Join = 0x1, Sink = 0x2 };
344}
345
346struct ToolProperties : public RefCountedBase<ToolProperties> {
347 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000348 Init* CmdLine;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000349 StrVector InLanguage;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000350 std::string OutLanguage;
351 std::string OutputSuffix;
352 unsigned Flags;
353 ToolOptionDescriptions OptDescs;
354
355 // Various boolean properties
356 void setSink() { Flags |= ToolFlags::Sink; }
357 bool isSink() const { return Flags & ToolFlags::Sink; }
358 void setJoin() { Flags |= ToolFlags::Join; }
359 bool isJoin() const { return Flags & ToolFlags::Join; }
360
361 // Default ctor here is needed because StringMap can only store
362 // DefaultConstructible objects
Anton Korobeynikov246fced2008-06-01 16:22:49 +0000363 ToolProperties() : CmdLine(0), Flags(0) {}
364 ToolProperties (const std::string& n) : Name(n), CmdLine(0), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000365};
366
367
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000368/// ToolPropertiesList - A list of Tool information records
369/// IntrusiveRefCntPtrs are used here because StringMap has no copy
370/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000371typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
372
373
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000374/// CollectOptionProperties - Function object for iterating over a
375/// list (usually, a DAG) of option property records.
376class CollectOptionProperties {
377private:
378 // Implementation details.
379
380 /// OptionPropertyHandler - a function that extracts information
381 /// about a given option property from its DAG representation.
382 typedef void (CollectOptionProperties::* OptionPropertyHandler)
383 (const DagInit*);
384
385 /// OptionPropertyHandlerMap - A map from option property names to
386 /// option property handlers
387 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
388
389 static OptionPropertyHandlerMap optionPropertyHandlers_;
390 static bool staticMembersInitialized_;
391
392 /// This is where the information is stored
393
394 /// toolProps_ - Properties of the current Tool.
395 ToolProperties* toolProps_;
396 /// optDescs_ - OptionDescriptions table (used to register options
397 /// globally).
398 GlobalOptionDescription& optDesc_;
399
400public:
401
402 explicit CollectOptionProperties(ToolProperties* TP,
403 GlobalOptionDescription& OD)
404 : toolProps_(TP), optDesc_(OD)
405 {
406 if (!staticMembersInitialized_) {
407 optionPropertyHandlers_["append_cmd"] =
408 &CollectOptionProperties::onAppendCmd;
409 optionPropertyHandlers_["forward"] =
410 &CollectOptionProperties::onForward;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000411 optionPropertyHandlers_["forward_as"] =
412 &CollectOptionProperties::onForwardAs;
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000413 optionPropertyHandlers_["help"] =
414 &CollectOptionProperties::onHelp;
415 optionPropertyHandlers_["output_suffix"] =
416 &CollectOptionProperties::onOutputSuffix;
417 optionPropertyHandlers_["required"] =
418 &CollectOptionProperties::onRequired;
419 optionPropertyHandlers_["stop_compilation"] =
420 &CollectOptionProperties::onStopCompilation;
421 optionPropertyHandlers_["unpack_values"] =
422 &CollectOptionProperties::onUnpackValues;
423
424 staticMembersInitialized_ = true;
425 }
426 }
427
428 /// operator() - Gets called for every option property; Just forwards
429 /// to the corresponding property handler.
430 void operator() (Init* i) {
431 const DagInit& option_property = InitPtrToDag(i);
432 const std::string& option_property_name
433 = option_property.getOperator()->getAsString();
434 OptionPropertyHandlerMap::iterator method
435 = optionPropertyHandlers_.find(option_property_name);
436
437 if (method != optionPropertyHandlers_.end()) {
438 OptionPropertyHandler h = method->second;
439 (this->*h)(&option_property);
440 }
441 else {
442 throw "Unknown option property: " + option_property_name + "!";
443 }
444 }
445
446private:
447
448 /// Option property handlers --
449 /// Methods that handle properties that are common for all types of
450 /// options (like append_cmd, stop_compilation)
451
452 void onAppendCmd (const DagInit* d) {
453 checkNumberOfArguments(d, 1);
454 checkToolProps(d);
455 const std::string& cmd = InitPtrToString(d->getArg(0));
456
457 toolProps_->OptDescs[optDesc_.Name].
458 AddProperty(OptionPropertyType::AppendCmd, cmd);
459 }
460
461 void onOutputSuffix (const DagInit* d) {
462 checkNumberOfArguments(d, 1);
463 checkToolProps(d);
464 const std::string& suf = InitPtrToString(d->getArg(0));
465
466 if (toolProps_->OptDescs[optDesc_.Name].Type != OptionType::Switch)
467 throw "Option " + optDesc_.Name
468 + " can't have 'output_suffix' property since it isn't a switch!";
469
470 toolProps_->OptDescs[optDesc_.Name].AddProperty
471 (OptionPropertyType::OutputSuffix, suf);
472 }
473
474 void onForward (const DagInit* d) {
475 checkNumberOfArguments(d, 0);
476 checkToolProps(d);
477 toolProps_->OptDescs[optDesc_.Name].setForward();
478 }
479
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000480 void onForwardAs (const DagInit* d) {
481 checkNumberOfArguments(d, 1);
482 checkToolProps(d);
483 const std::string& cmd = InitPtrToString(d->getArg(0));
484
485 toolProps_->OptDescs[optDesc_.Name].
486 AddProperty(OptionPropertyType::ForwardAs, cmd);
487 }
488
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000489 void onHelp (const DagInit* d) {
490 checkNumberOfArguments(d, 1);
491 const std::string& help_message = InitPtrToString(d->getArg(0));
492
493 optDesc_.Help = help_message;
494 }
495
496 void onRequired (const DagInit* d) {
497 checkNumberOfArguments(d, 0);
498 checkToolProps(d);
499 optDesc_.setRequired();
500 }
501
502 void onStopCompilation (const DagInit* d) {
503 checkNumberOfArguments(d, 0);
504 checkToolProps(d);
505 if (optDesc_.Type != OptionType::Switch)
506 throw std::string("Only options of type Switch can stop compilation!");
507 toolProps_->OptDescs[optDesc_.Name].setStopCompilation();
508 }
509
510 void onUnpackValues (const DagInit* d) {
511 checkNumberOfArguments(d, 0);
512 checkToolProps(d);
513 toolProps_->OptDescs[optDesc_.Name].setUnpackValues();
514 }
515
516 // Helper functions
517
518 /// checkToolProps - Throw an error if toolProps_ == 0.
519 void checkToolProps(const DagInit* d) {
520 if (!d)
521 throw "Option property " + d->getOperator()->getAsString()
522 + " can't be used in this context";
523 }
524
525};
526
527CollectOptionProperties::OptionPropertyHandlerMap
528CollectOptionProperties::optionPropertyHandlers_;
529
530bool CollectOptionProperties::staticMembersInitialized_ = false;
531
532
533/// processOptionProperties - Go through the list of option
534/// properties and call a corresponding handler for each.
535void processOptionProperties (const DagInit* d, ToolProperties* t,
536 GlobalOptionDescription& o) {
537 checkNumberOfArguments(d, 2);
538 DagInit::const_arg_iterator B = d->arg_begin();
539 // Skip the first argument: it's always the option name.
540 ++B;
541 std::for_each(B, d->arg_end(), CollectOptionProperties(t, o));
542}
543
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000544/// AddOption - A function object wrapper for
545/// processOptionProperties. Used by CollectProperties and
546/// CollectPropertiesFromOptionList.
547class AddOption {
548private:
549 GlobalOptionDescriptions& OptDescs_;
550 ToolProperties* ToolProps_;
551
552public:
553 explicit AddOption(GlobalOptionDescriptions& OD, ToolProperties* TP = 0)
554 : OptDescs_(OD), ToolProps_(TP)
555 {}
556
557 void operator()(const Init* i) {
558 const DagInit& d = InitPtrToDag(i);
559 checkNumberOfArguments(&d, 2);
560
561 const OptionType::OptionType Type =
562 getOptionType(d.getOperator()->getAsString());
563 const std::string& Name = InitPtrToString(d.getArg(0));
564
565 GlobalOptionDescription OD(Type, Name);
566 if (Type != OptionType::Alias) {
567 processOptionProperties(&d, ToolProps_, OD);
568 if (ToolProps_) {
569 ToolProps_->OptDescs[Name].Type = Type;
570 ToolProps_->OptDescs[Name].Name = Name;
571 }
572 }
573 else {
574 OD.Help = InitPtrToString(d.getArg(1));
575 }
576 OptDescs_.insertDescription(OD);
577 }
578
579private:
580 OptionType::OptionType getOptionType(const std::string& T) const {
581 if (T == "alias_option")
582 return OptionType::Alias;
583 else if (T == "switch_option")
584 return OptionType::Switch;
585 else if (T == "parameter_option")
586 return OptionType::Parameter;
587 else if (T == "parameter_list_option")
588 return OptionType::ParameterList;
589 else if (T == "prefix_option")
590 return OptionType::Prefix;
591 else if (T == "prefix_list_option")
592 return OptionType::PrefixList;
593 else
594 throw "Unknown option type: " + T + '!';
595 }
596};
597
598
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000599/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000600/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000601class CollectProperties {
602private:
603
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000604 // Implementation details
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000605
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000606 /// PropertyHandler - a function that extracts information
607 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000608 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000609
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000610 /// PropertyHandlerMap - A map from property names to property
611 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000612 typedef StringMap<PropertyHandler> PropertyHandlerMap;
613
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000614 // Static maps from strings to CollectProperties methods("handlers")
615 static PropertyHandlerMap propertyHandlers_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000616 static bool staticMembersInitialized_;
617
618
619 /// This is where the information is stored
620
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000621 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000622 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000623 /// optDescs_ - OptionDescriptions table (used to register options
624 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000625 GlobalOptionDescriptions& optDescs_;
626
627public:
628
629 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
630 : toolProps_(p), optDescs_(d)
631 {
632 if (!staticMembersInitialized_) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000633 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
634 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
635 propertyHandlers_["join"] = &CollectProperties::onJoin;
636 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
637 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
638 propertyHandlers_["parameter_option"]
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000639 = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000640 propertyHandlers_["parameter_list_option"] =
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000641 &CollectProperties::addOption;
642 propertyHandlers_["prefix_option"] = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000643 propertyHandlers_["prefix_list_option"] =
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000644 &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000645 propertyHandlers_["sink"] = &CollectProperties::onSink;
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000646 propertyHandlers_["switch_option"] = &CollectProperties::addOption;
647 propertyHandlers_["alias_option"] = &CollectProperties::addOption;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000648
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000649 staticMembersInitialized_ = true;
650 }
651 }
652
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000653 /// operator() - Gets called for every tool property; Just forwards
654 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000655 void operator() (Init* i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000656 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000657 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000658 PropertyHandlerMap::iterator method
659 = propertyHandlers_.find(property_name);
660
661 if (method != propertyHandlers_.end()) {
662 PropertyHandler h = method->second;
663 (this->*h)(&d);
664 }
665 else {
666 throw "Unknown tool property: " + property_name + "!";
667 }
668 }
669
670private:
671
672 /// Property handlers --
673 /// Functions that extract information about tool properties from
674 /// DAG representation.
675
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000676 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000677 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000678 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000679 }
680
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000681 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000682 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000683 Init* arg = d->getArg(0);
684
685 // Find out the argument's type.
686 if (typeid(*arg) == typeid(StringInit)) {
687 // It's a string.
688 toolProps_.InLanguage.push_back(InitPtrToString(arg));
689 }
690 else {
691 // It's a list.
692 const ListInit& lst = InitPtrToList(arg);
693 StrVector& out = toolProps_.InLanguage;
694
695 // Copy strings to the output vector.
696 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
697 B != E; ++B) {
698 out.push_back(InitPtrToString(*B));
699 }
700
701 // Remove duplicates.
702 std::sort(out.begin(), out.end());
703 StrVector::iterator newE = std::unique(out.begin(), out.end());
704 out.erase(newE, out.end());
705 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000706 }
707
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000708 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000709 checkNumberOfArguments(d, 0);
710 toolProps_.setJoin();
711 }
712
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000713 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000714 checkNumberOfArguments(d, 1);
715 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
716 }
717
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000718 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000719 checkNumberOfArguments(d, 1);
720 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
721 }
722
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000723 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000724 checkNumberOfArguments(d, 0);
725 optDescs_.HasSink = true;
726 toolProps_.setSink();
727 }
728
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000729 // Just forwards to the AddOption function object. Somewhat
730 // non-optimal, but avoids code duplication.
731 void addOption (const DagInit* d) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000732 checkNumberOfArguments(d, 2);
Mikhail Glushenkove62df252008-05-30 06:27:29 +0000733 AddOption(optDescs_, &toolProps_)(d);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000734 }
735
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000736};
737
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000738// Defintions of static members of CollectProperties.
739CollectProperties::PropertyHandlerMap CollectProperties::propertyHandlers_;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000740bool CollectProperties::staticMembersInitialized_ = false;
741
742
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000743/// CollectToolProperties - Gather information about tool properties
744/// from the parsed TableGen data (basically a wrapper for the
745/// CollectProperties function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000746void CollectToolProperties (RecordVector::const_iterator B,
747 RecordVector::const_iterator E,
748 ToolPropertiesList& TPList,
749 GlobalOptionDescriptions& OptDescs)
750{
751 // Iterate over a properties list of every Tool definition
752 for (;B!=E;++B) {
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000753 Record* T = *B;
754 // Throws an exception if the value does not exist.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000755 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000756
757 IntrusiveRefCntPtr<ToolProperties>
758 ToolProps(new ToolProperties(T->getName()));
759
760 std::for_each(PropList->begin(), PropList->end(),
761 CollectProperties(*ToolProps, OptDescs));
762 TPList.push_back(ToolProps);
763 }
764}
765
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000766
767/// CollectPropertiesFromOptionList - Gather information about
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000768/// *global* option properties from the OptionList.
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000769void CollectPropertiesFromOptionList (RecordVector::const_iterator B,
770 RecordVector::const_iterator E,
771 GlobalOptionDescriptions& OptDescs)
772{
773 // Iterate over a properties list of every Tool definition
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000774 for (;B!=E;++B) {
775 RecordVector::value_type T = *B;
776 // Throws an exception if the value does not exist.
777 ListInit* PropList = T->getValueAsListInit("options");
778
Mikhail Glushenkovbf774352008-05-30 06:27:02 +0000779 std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
Mikhail Glushenkovd638e852008-05-30 06:26:08 +0000780 }
781}
782
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +0000783/// CheckForSuperfluousOptions - Check that there are no side
784/// effect-free options (specified only in the OptionList). Otherwise,
785/// output a warning.
786void CheckForSuperfluousOptions (const ToolPropertiesList& TPList,
787 const GlobalOptionDescriptions& OptDescs) {
788 llvm::StringSet<> nonSuperfluousOptions;
789
790 // Add all options mentioned in the TPList to the set of
791 // non-superfluous options.
792 for (ToolPropertiesList::const_iterator B = TPList.begin(),
793 E = TPList.end(); B != E; ++B) {
794 const ToolProperties& TP = *(*B);
795 for (ToolOptionDescriptions::const_iterator B = TP.OptDescs.begin(),
796 E = TP.OptDescs.end(); B != E; ++B) {
797 nonSuperfluousOptions.insert(B->first());
798 }
799 }
800
801 // Check that all options in OptDescs belong to the set of
802 // non-superfluous options.
803 for (GlobalOptionDescriptions::const_iterator B = OptDescs.begin(),
804 E = OptDescs.end(); B != E; ++B) {
805 const GlobalOptionDescription& Val = B->second;
806 if (!nonSuperfluousOptions.count(Val.Name)
807 && Val.Type != OptionType::Alias)
808 cerr << "Warning: option '-" << Val.Name << "' has no effect! "
809 "Probable cause: this option is specified only in the OptionList.\n";
810 }
811}
812
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000813/// EmitCaseTest1Arg - Helper function used by
814/// EmitCaseConstructHandler.
815bool EmitCaseTest1Arg(const std::string& TestName,
816 const DagInit& d,
817 const GlobalOptionDescriptions& OptDescs,
818 std::ostream& O) {
819 checkNumberOfArguments(&d, 1);
820 const std::string& OptName = InitPtrToString(d.getArg(0));
821 if (TestName == "switch_on") {
822 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
823 if (OptDesc.Type != OptionType::Switch)
824 throw OptName + ": incorrect option type!";
825 O << OptDesc.GenVariableName();
826 return true;
827 } else if (TestName == "input_languages_contain") {
828 O << "InLangs.count(\"" << OptName << "\") != 0";
829 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000830 } else if (TestName == "in_language") {
Mikhail Glushenkov56a625a2008-09-22 20:48:22 +0000831 // This works only for single-argument Tool::GenerateAction. Join
832 // tools can process several files in different languages simultaneously.
833
834 // TODO: make this work with Edge::Weight (if possible).
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +0000835 O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000836 return true;
837 } else if (TestName == "not_empty") {
Mikhail Glushenkovb4833872008-05-30 06:24:07 +0000838 if (OptName == "o") {
839 O << "!OutputFilename.empty()";
840 return true;
841 }
842 else {
843 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
844 if (OptDesc.Type == OptionType::Switch)
845 throw OptName + ": incorrect option type!";
846 O << '!' << OptDesc.GenVariableName() << ".empty()";
847 return true;
848 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000849 }
850
851 return false;
852}
853
854/// EmitCaseTest2Args - Helper function used by
855/// EmitCaseConstructHandler.
856bool EmitCaseTest2Args(const std::string& TestName,
857 const DagInit& d,
858 const char* IndentLevel,
859 const GlobalOptionDescriptions& OptDescs,
860 std::ostream& O) {
861 checkNumberOfArguments(&d, 2);
862 const std::string& OptName = InitPtrToString(d.getArg(0));
863 const std::string& OptArg = InitPtrToString(d.getArg(1));
864 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
865
866 if (TestName == "parameter_equals") {
867 if (OptDesc.Type != OptionType::Parameter
868 && OptDesc.Type != OptionType::Prefix)
869 throw OptName + ": incorrect option type!";
870 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
871 return true;
872 }
873 else if (TestName == "element_in_list") {
874 if (OptDesc.Type != OptionType::ParameterList
875 && OptDesc.Type != OptionType::PrefixList)
876 throw OptName + ": incorrect option type!";
877 const std::string& VarName = OptDesc.GenVariableName();
878 O << "std::find(" << VarName << ".begin(),\n"
879 << IndentLevel << Indent1 << VarName << ".end(), \""
880 << OptArg << "\") != " << VarName << ".end()";
881 return true;
882 }
883
884 return false;
885}
886
887// Forward declaration.
888// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
889void EmitCaseTest(const DagInit& d, const char* IndentLevel,
890 const GlobalOptionDescriptions& OptDescs,
891 std::ostream& O);
892
893/// EmitLogicalOperationTest - Helper function used by
894/// EmitCaseConstructHandler.
895void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
896 const char* IndentLevel,
897 const GlobalOptionDescriptions& OptDescs,
898 std::ostream& O) {
899 O << '(';
900 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000901 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000902 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
903 if (j != NumArgs - 1)
904 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
905 else
906 O << ')';
907 }
908}
909
910/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
911void EmitCaseTest(const DagInit& d, const char* IndentLevel,
912 const GlobalOptionDescriptions& OptDescs,
913 std::ostream& O) {
914 const std::string& TestName = d.getOperator()->getAsString();
915
916 if (TestName == "and")
917 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
918 else if (TestName == "or")
919 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
920 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
921 return;
922 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
923 return;
924 else
925 throw TestName + ": unknown edge property!";
926}
927
928// Emit code that handles the 'case' construct.
929// Takes a function object that should emit code for every case clause.
930// Callback's type is
931// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
932template <typename F>
933void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
Mikhail Glushenkov1d95e9f2008-05-31 13:43:21 +0000934 F Callback, bool EmitElseIf,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000935 const GlobalOptionDescriptions& OptDescs,
936 std::ostream& O) {
937 assert(d->getOperator()->getAsString() == "case");
938
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000939 unsigned numArgs = d->getNumArgs();
940 if (d->getNumArgs() < 2)
941 throw "There should be at least one clause in the 'case' expression:\n"
942 + d->getAsString();
943
944 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000945 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000946
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000947 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000948 if (Test.getOperator()->getAsString() == "default") {
949 if (i+2 != numArgs)
950 throw std::string("The 'default' clause should be the last in the"
951 "'case' construct!");
952 O << IndentLevel << "else {\n";
953 }
954 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000955 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000956 EmitCaseTest(Test, IndentLevel, OptDescs, O);
957 O << ") {\n";
958 }
959
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000960 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000961 ++i;
962 if (i == numArgs)
963 throw "Case construct handler: no corresponding action "
964 "found for the test " + Test.getAsString() + '!';
965
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000966 Init* arg = d->getArg(i);
967 if (dynamic_cast<DagInit*>(arg)
968 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") {
969 EmitCaseConstructHandler(static_cast<DagInit*>(arg),
970 (std::string(IndentLevel) + Indent1).c_str(),
971 Callback, EmitElseIf, OptDescs, O);
972 }
973 else {
974 Callback(arg, IndentLevel, O);
975 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000976 O << IndentLevel << "}\n";
977 }
978}
979
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000980/// EmitForwardOptionPropertyHandlingCode - Helper function used to
981/// implement EmitOptionPropertyHandlingCode(). Emits code for
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000982/// handling the (forward) and (forward_as) option properties.
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000983void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000984 const std::string& NewName,
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000985 std::ostream& O) {
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000986 const std::string& Name = NewName.empty()
987 ? ("-" + D.Name)
988 : NewName;
989
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000990 switch (D.Type) {
991 case OptionType::Switch:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000992 O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000993 break;
994 case OptionType::Parameter:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000995 O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000996 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
997 break;
998 case OptionType::Prefix:
Mikhail Glushenkov50084e82008-09-22 20:46:19 +0000999 O << Indent3 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001000 << D.GenVariableName() << ");\n";
1001 break;
1002 case OptionType::PrefixList:
1003 O << Indent3 << "for (" << D.GenTypeDeclaration()
1004 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1005 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001006 << Indent4 << "vec.push_back(\"" << Name << "\" + "
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001007 << "*B);\n";
1008 break;
1009 case OptionType::ParameterList:
1010 O << Indent3 << "for (" << D.GenTypeDeclaration()
1011 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1012 << Indent3 << "E = " << D.GenVariableName()
1013 << ".end() ; B != E; ++B) {\n"
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001014 << Indent4 << "vec.push_back(\"" << Name << "\");\n"
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001015 << Indent4 << "vec.push_back(*B);\n"
1016 << Indent3 << "}\n";
1017 break;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001018 case OptionType::Alias:
1019 default:
1020 throw std::string("Aliases are not allowed in tool option descriptions!");
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001021 }
1022}
1023
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001024// ToolOptionHasInterestingProperties - A helper function used by
1025// EmitOptionPropertyHandlingCode() that tells us whether we should
1026// emit any property handling code at all.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001027bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001028 bool ret = false;
1029 for (OptionPropertyList::const_iterator B = D.Props.begin(),
1030 E = D.Props.end(); B != E; ++B) {
1031 const OptionProperty& OptProp = *B;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001032 if (OptProp.first == OptionPropertyType::AppendCmd
1033 || OptProp.first == OptionPropertyType::ForwardAs)
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +00001034 ret = true;
1035 }
1036 if (D.isForward() || D.isUnpackValues())
1037 ret = true;
1038 return ret;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001039}
1040
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001041/// EmitOptionPropertyHandlingCode - Helper function used by
1042/// EmitGenerateActionMethod(). Emits code that handles option
1043/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001044void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001045 std::ostream& O)
1046{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001047 if (!ToolOptionHasInterestingProperties(D))
1048 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001049 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001050 O << Indent2 << "if (";
1051 if (D.Type == OptionType::Switch)
1052 O << D.GenVariableName();
1053 else
1054 O << '!' << D.GenVariableName() << ".empty()";
1055
1056 O <<") {\n";
1057
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001058 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001059 for (OptionPropertyList::const_iterator B = D.Props.begin(),
1060 E = D.Props.end(); B!=E; ++B) {
1061 const OptionProperty& val = *B;
1062
1063 switch (val.first) {
1064 // (append_cmd cmd) property
1065 case OptionPropertyType::AppendCmd:
1066 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
1067 break;
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001068 // (forward_as) property
1069 case OptionPropertyType::ForwardAs:
1070 EmitForwardOptionPropertyHandlingCode(D, val.second, O);
1071 break;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001072 // Other properties with argument
1073 default:
1074 break;
1075 }
1076 }
1077
1078 // Handle flags
1079
1080 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001081 if (D.isForward())
Mikhail Glushenkov50084e82008-09-22 20:46:19 +00001082 EmitForwardOptionPropertyHandlingCode(D, "", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001083
1084 // (unpack_values) property
1085 if (D.isUnpackValues()) {
1086 if (IsListOptionType(D.Type)) {
1087 O << Indent3 << "for (" << D.GenTypeDeclaration()
1088 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
1089 << Indent3 << "E = " << D.GenVariableName()
1090 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +00001091 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001092 }
1093 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +00001094 O << Indent3 << "llvm::SplitString("
1095 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001096 }
1097 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001098 throw std::string("Switches can't have unpack_values property!");
1099 }
1100 }
1101
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001102 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001103 O << Indent2 << "}\n";
1104}
1105
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001106/// SubstituteSpecialCommands - Perform string substitution for $CALL
1107/// and $ENV. Helper function used by EmitCmdLineVecFill().
1108std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001109 size_t cparen = cmd.find(")");
1110 std::string ret;
1111
1112 if (cmd.find("$CALL(") == 0) {
1113 if (cmd.size() == 6)
1114 throw std::string("$CALL invocation: empty argument list!");
1115
1116 ret += "hooks::";
1117 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
1118 ret += "()";
1119 }
1120 else if (cmd.find("$ENV(") == 0) {
1121 if (cmd.size() == 5)
1122 throw std::string("$ENV invocation: empty argument list!");
1123
1124 ret += "std::getenv(\"";
1125 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
1126 ret += "\")";
1127 }
1128 else {
1129 throw "Unknown special command: " + cmd;
1130 }
1131
1132 if (cmd.begin() + cparen + 1 != cmd.end()) {
1133 ret += " + std::string(\"";
1134 ret += (cmd.c_str() + cparen + 1);
1135 ret += "\")";
1136 }
1137
1138 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001139}
1140
1141/// EmitCmdLineVecFill - Emit code that fills in the command line
1142/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001143void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
1144 bool Version, const char* IndentLevel,
1145 std::ostream& O) {
1146 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001147 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001148 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001149 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001150
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001151 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001152 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001153 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001154 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001155 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001156 if (cmd.at(0) == '$') {
1157 if (cmd == "$INFILE") {
1158 if (Version)
1159 O << "for (PathVector::const_iterator B = inFiles.begin()"
1160 << ", E = inFiles.end();\n"
1161 << IndentLevel << "B != E; ++B)\n"
1162 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1163 else
1164 O << "vec.push_back(inFile.toString());\n";
1165 }
1166 else if (cmd == "$OUTFILE") {
1167 O << "vec.push_back(outFile.toString());\n";
1168 }
1169 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001170 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
1171 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001172 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001173 }
1174 else {
1175 O << "vec.push_back(\"" << cmd << "\");\n";
1176 }
1177 }
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001178 O << IndentLevel << "cmd = "
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001179 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
1180 : "\"" + StrVec[0] + "\"")
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001181 << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001182}
1183
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001184/// EmitCmdLineVecFillCallback - A function object wrapper around
1185/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1186/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001187class EmitCmdLineVecFillCallback {
1188 bool Version;
1189 const std::string& ToolName;
1190 public:
1191 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
1192 : Version(Ver), ToolName(TN) {}
1193
1194 void operator()(const Init* Statement, const char* IndentLevel,
1195 std::ostream& O) const
1196 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001197 EmitCmdLineVecFill(Statement, ToolName, Version,
1198 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001199 }
1200};
1201
1202// EmitGenerateActionMethod - Emit one of two versions of the
1203// Tool::GenerateAction() method.
1204void EmitGenerateActionMethod (const ToolProperties& P,
1205 const GlobalOptionDescriptions& OptDescs,
1206 bool Version, std::ostream& O) {
1207 if (Version)
1208 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1209 else
1210 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1211
1212 O << Indent2 << "const sys::Path& outFile,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001213 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1214 << Indent2 << "const LanguageMap& LangMap) const\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001215 << Indent1 << "{\n"
foldre4a81682008-11-08 19:43:32 +00001216 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001217 << Indent2 << "std::vector<std::string> vec;\n";
1218
1219 // cmd_line is either a string or a 'case' construct.
1220 if (typeid(*P.CmdLine) == typeid(StringInit))
1221 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
1222 else
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001223 EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001224 EmitCmdLineVecFillCallback(Version, P.Name),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001225 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001226
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001227 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001228 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1229 E = P.OptDescs.end(); B != E; ++B) {
1230 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001231 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001232 }
1233
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001234 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001235 if (P.isSink()) {
1236 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1237 << Indent3 << "vec.insert(vec.end(), "
1238 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1239 << Indent2 << "}\n";
1240 }
1241
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001242 O << Indent2 << "return Action(cmd, vec);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001243 << Indent1 << "}\n\n";
1244}
1245
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001246/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1247/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001248void EmitGenerateActionMethods (const ToolProperties& P,
1249 const GlobalOptionDescriptions& OptDescs,
1250 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001251 if (!P.isJoin())
1252 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001253 << Indent2 << "const llvm::sys::Path& outFile,\n"
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001254 << Indent2 << "const InputLanguagesSet& InLangs,\n"
1255 << Indent2 << "const LanguageMap& LangMap) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001256 << Indent1 << "{\n"
1257 << Indent2 << "throw std::runtime_error(\"" << P.Name
1258 << " is not a Join tool!\");\n"
1259 << Indent1 << "}\n\n";
1260 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001261 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001262
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001263 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001264}
1265
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001266/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
1267/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001268void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
1269 O << Indent1 << "bool IsLast() const {\n"
1270 << Indent2 << "bool last = false;\n";
1271
1272 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1273 E = P.OptDescs.end(); B != E; ++B) {
1274 const ToolOptionDescription& val = B->second;
1275
1276 if (val.isStopCompilation())
1277 O << Indent2
1278 << "if (" << val.GenVariableName()
1279 << ")\n" << Indent3 << "last = true;\n";
1280 }
1281
1282 O << Indent2 << "return last;\n"
1283 << Indent1 << "}\n\n";
1284}
1285
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001286/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1287/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001288void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001289 O << Indent1 << "const char** InputLanguages() const {\n"
1290 << Indent2 << "return InputLanguages_;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001291 << Indent1 << "}\n\n";
1292
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001293 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001294 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1295 << Indent1 << "}\n\n";
1296}
1297
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001298/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1299/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001300void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001301 O << Indent1 << "const char* OutputSuffix() const {\n"
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +00001302 << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
1303
1304 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1305 E = P.OptDescs.end(); B != E; ++B) {
1306 const ToolOptionDescription& OptDesc = B->second;
1307 for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
1308 E = OptDesc.Props.end(); B != E; ++B) {
1309 const OptionProperty& OptProp = *B;
1310 if (OptProp.first == OptionPropertyType::OutputSuffix) {
1311 O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
1312 << Indent3 << "ret = \"" << OptProp.second << "\";\n";
1313 }
1314 }
1315 }
1316
1317 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001318 << Indent1 << "}\n\n";
1319}
1320
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001321/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001322void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001323 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001324 << Indent2 << "return \"" << P.Name << "\";\n"
1325 << Indent1 << "}\n\n";
1326}
1327
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001328/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1329/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001330void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1331 O << Indent1 << "bool IsJoin() const {\n";
1332 if (P.isJoin())
1333 O << Indent2 << "return true;\n";
1334 else
1335 O << Indent2 << "return false;\n";
1336 O << Indent1 << "}\n\n";
1337}
1338
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001339/// EmitStaticMemberDefinitions - Emit static member definitions for a
1340/// given Tool class.
1341void EmitStaticMemberDefinitions(const ToolProperties& P, std::ostream& O) {
1342 O << "const char* " << P.Name << "::InputLanguages_[] = {";
1343 for (StrVector::const_iterator B = P.InLanguage.begin(),
1344 E = P.InLanguage.end(); B != E; ++B)
1345 O << '\"' << *B << "\", ";
1346 O << "0};\n\n";
1347}
1348
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001349/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001350void EmitToolClassDefinition (const ToolProperties& P,
1351 const GlobalOptionDescriptions& OptDescs,
1352 std::ostream& O) {
1353 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001354 return;
1355
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001356 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001357 O << "class " << P.Name << " : public ";
1358 if (P.isJoin())
1359 O << "JoinTool";
1360 else
1361 O << "Tool";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001362
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001363 O << "{\nprivate:\n"
1364 << Indent1 << "static const char* InputLanguages_[];\n\n";
1365
1366 O << "public:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001367 EmitNameMethod(P, O);
1368 EmitInOutLanguageMethods(P, O);
1369 EmitOutputSuffixMethod(P, O);
1370 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001371 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001372 EmitIsLastMethod(P, O);
1373
1374 // Close class definition
Mikhail Glushenkov61923cb2008-05-30 06:24:49 +00001375 O << "};\n";
1376
1377 EmitStaticMemberDefinitions(P, O);
1378
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001379}
1380
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001381/// EmitOptionDescriptions - Iterate over a list of option
1382/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001383void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1384 std::ostream& O)
1385{
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001386 std::vector<GlobalOptionDescription> Aliases;
1387
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001388 // Emit static cl::Option variables.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001389 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1390 E = descs.end(); B!=E; ++B) {
1391 const GlobalOptionDescription& val = B->second;
1392
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001393 if (val.Type == OptionType::Alias) {
1394 Aliases.push_back(val);
1395 continue;
1396 }
1397
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001398 O << val.GenTypeDeclaration() << ' '
1399 << val.GenVariableName()
1400 << "(\"" << val.Name << '\"';
1401
1402 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1403 O << ", cl::Prefix";
1404
1405 if (val.isRequired()) {
1406 switch (val.Type) {
1407 case OptionType::PrefixList:
1408 case OptionType::ParameterList:
1409 O << ", cl::OneOrMore";
1410 break;
1411 default:
1412 O << ", cl::Required";
1413 }
1414 }
1415
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001416 if (!val.Help.empty())
1417 O << ", cl::desc(\"" << val.Help << "\")";
1418
1419 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001420 }
1421
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001422 // Emit the aliases (they should go after all the 'proper' options).
1423 for (std::vector<GlobalOptionDescription>::const_iterator
1424 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
1425 const GlobalOptionDescription& val = *B;
1426
1427 O << val.GenTypeDeclaration() << ' '
1428 << val.GenVariableName()
1429 << "(\"" << val.Name << '\"';
1430
1431 GlobalOptionDescriptions::container_type
1432 ::const_iterator F = descs.Descriptions.find(val.Help);
1433 if (F != descs.Descriptions.end())
1434 O << ", cl::aliasopt(" << F->second.GenVariableName() << ")";
1435 else
1436 throw val.Name + ": alias to an unknown option!";
1437
1438 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1439 }
1440
1441 // Emit the sink option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001442 if (descs.HasSink)
1443 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1444
1445 O << '\n';
1446}
1447
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001448/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001449void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1450{
1451 // Get the relevant field out of RecordKeeper
1452 Record* LangMapRecord = Records.getDef("LanguageMap");
1453 if (!LangMapRecord)
1454 throw std::string("Language map definition not found!");
1455
1456 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1457 if (!LangsToSuffixesList)
1458 throw std::string("Error in the language map definition!");
1459
1460 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001461 O << "namespace {\n\n";
1462 O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001463
1464 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1465 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1466
1467 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1468 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1469
1470 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkovcdbfa1a2008-09-22 20:47:46 +00001471 O << Indent1 << "langMap[\""
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001472 << InitPtrToString(Suffixes->getElement(i))
1473 << "\"] = \"" << Lang << "\";\n";
1474 }
1475
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001476 O << "}\n\n}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001477}
1478
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001479/// FillInToolToLang - Fills in two tables that map tool names to
1480/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001481void FillInToolToLang (const ToolPropertiesList& TPList,
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001482 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001483 StringMap<std::string>& ToolToOutLang) {
1484 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1485 B != E; ++B) {
1486 const ToolProperties& P = *(*B);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001487 for (StrVector::const_iterator B = P.InLanguage.begin(),
1488 E = P.InLanguage.end(); B != E; ++B)
1489 ToolToInLang[P.Name].insert(*B);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001490 ToolToOutLang[P.Name] = P.OutLanguage;
1491 }
1492}
1493
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001494/// TypecheckGraph - Check that names for output and input languages
1495/// on all edges do match.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001496// TOFIX: It would be nice if this function also checked for cycles
1497// and multiple default edges in the graph (better error
1498// reporting). Unfortunately, it is awkward to do right now because
1499// our intermediate representation is not sufficiently
Mikhail Glushenkovc178ead2008-09-22 20:45:17 +00001500// sophisticated. Algorithms like these require a real graph instead of
1501// an AST.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001502void TypecheckGraph (Record* CompilationGraph,
1503 const ToolPropertiesList& TPList) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001504 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001505 StringMap<std::string> ToolToOutLang;
1506
1507 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1508 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001509 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1510 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001511
1512 for (unsigned i = 0; i < edges->size(); ++i) {
1513 Record* Edge = edges->getElementAsRecord(i);
1514 Record* A = Edge->getValueAsDef("a");
1515 Record* B = Edge->getValueAsDef("b");
1516 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001517 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001518 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001519 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001520 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001521 throw B->getName() + ": no such tool!";
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001522 if (A->getName() != "root" && IB->second.count(IA->second) == 0)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001523 throw "Edge " + A->getName() + "->" + B->getName()
1524 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001525 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001526 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001527 }
1528}
1529
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001530/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1531/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001532void IncDecWeight (const Init* i, const char* IndentLevel,
1533 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001534 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001535 const std::string& OpName = d.getOperator()->getAsString();
1536
1537 if (OpName == "inc_weight")
1538 O << IndentLevel << Indent1 << "ret += ";
1539 else if (OpName == "dec_weight")
1540 O << IndentLevel << Indent1 << "ret -= ";
1541 else
1542 throw "Unknown operator in edge properties list: " + OpName + '!';
1543
1544 if (d.getNumArgs() > 0)
1545 O << InitPtrToInt(d.getArg(0)) << ";\n";
1546 else
1547 O << "2;\n";
1548
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001549}
1550
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001551/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001552void EmitEdgeClass (unsigned N, const std::string& Target,
1553 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1554 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001555
1556 // Class constructor.
1557 O << "class Edge" << N << ": public Edge {\n"
1558 << "public:\n"
1559 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1560 << "\") {}\n\n"
1561
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001562 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001563 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001564 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001565
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001566 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001567 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001568
1569 O << Indent2 << "return ret;\n"
1570 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001571}
1572
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001573/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001574void EmitEdgeClasses (Record* CompilationGraph,
1575 const GlobalOptionDescriptions& OptDescs,
1576 std::ostream& O) {
1577 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1578
1579 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001580 Record* Edge = edges->getElementAsRecord(i);
1581 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001582 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001583
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001584 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001585 continue;
1586
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001587 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001588 }
1589}
1590
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001591/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1592/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001593void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001594 std::ostream& O)
1595{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001596 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001597
1598 // Generate code
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001599 O << "namespace {\n\n";
1600 O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001601
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001602 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001603
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001604 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1605 if (Tools.empty())
1606 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001607
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001608 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1609 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001610 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001611 O << Indent1 << "G.insertNode(new "
1612 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001613 }
1614
1615 O << '\n';
1616
1617 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001618 for (unsigned i = 0; i < edges->size(); ++i) {
1619 Record* Edge = edges->getElementAsRecord(i);
1620 Record* A = Edge->getValueAsDef("a");
1621 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001622 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001623
1624 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1625
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001626 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001627 O << "new SimpleEdge(\"" << B->getName() << "\")";
1628 else
1629 O << "new Edge" << i << "()";
1630
1631 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001632 }
1633
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001634 O << "}\n\n}\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001635}
1636
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001637/// ExtractHookNames - Extract the hook names from all instances of
1638/// $CALL(HookName) in the provided command line string. Helper
1639/// function used by FillInHookNames().
1640void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1641 StrVector cmds;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001642 llvm::SplitString(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001643 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1644 B != E; ++B) {
1645 const std::string& cmd = *B;
1646 if (cmd.find("$CALL(") == 0) {
1647 if (cmd.size() == 6)
1648 throw std::string("$CALL invocation: empty argument list!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001649 HookNames.push_back(std::string(cmd.begin() + 6,
1650 cmd.begin() + cmd.find(")")));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001651 }
1652 }
1653}
1654
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001655/// ExtractHookNamesFromCaseConstruct - Extract hook names from the
1656/// 'case' expression, handle nesting. Helper function used by
1657/// FillInHookNames().
1658void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) {
1659 const DagInit& d = InitPtrToDag(Case);
1660 bool even = false;
1661 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1662 B != E; ++B) {
1663 Init* arg = *B;
1664 if (even && dynamic_cast<DagInit*>(arg)
1665 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
1666 ExtractHookNamesFromCaseConstruct(arg, HookNames);
1667 else if (even)
1668 ExtractHookNames(arg, HookNames);
1669 even = !even;
1670 }
1671}
1672
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001673/// FillInHookNames - Actually extract the hook names from all command
1674/// line strings. Helper function used by EmitHookDeclarations().
1675void FillInHookNames(const ToolPropertiesList& TPList,
1676 StrVector& HookNames) {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001677 // For all command lines:
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001678 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1679 E = TPList.end(); B != E; ++B) {
1680 const ToolProperties& P = *(*B);
1681 if (!P.CmdLine)
1682 continue;
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001683 if (dynamic_cast<StringInit*>(P.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001684 // This is a string.
1685 ExtractHookNames(P.CmdLine, HookNames);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001686 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001687 // This is a 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001688 ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001689 }
1690}
1691
1692/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1693/// property records and emit hook function declaration for each
1694/// instance of $CALL(HookName).
1695void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1696 std::ostream& O) {
1697 StrVector HookNames;
1698 FillInHookNames(ToolProps, HookNames);
1699 if (HookNames.empty())
1700 return;
1701 std::sort(HookNames.begin(), HookNames.end());
1702 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1703
1704 O << "namespace hooks {\n";
1705 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1706 O << Indent1 << "std::string " << *B << "();\n";
1707
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.
1712void EmitRegisterPlugin(std::ostream& O) {
1713 O << "namespace {\n\n"
1714 << "struct Plugin : public llvmc::BasePlugin {\n"
1715 << 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
1722 << "static llvmc::RegisterPlugin<Plugin> RP;\n\n}\n\n";
1723}
1724
1725/// EmitInclude - Emit necessary #include directives.
1726void EmitIncludes(std::ostream& O) {
Mikhail Glushenkov62ab3112008-09-22 20:50:40 +00001727 O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
1728 << "#include \"llvm/CompilerDriver/Plugin.h\"\n"
1729 << "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001730
1731 << "#include \"llvm/ADT/StringExtras.h\"\n"
1732 << "#include \"llvm/Support/CommandLine.h\"\n\n"
1733
1734 << "#include <cstdlib>\n"
1735 << "#include <stdexcept>\n\n"
1736
1737 << "using namespace llvm;\n"
1738 << "using namespace llvmc;\n\n"
1739
1740 << "extern cl::opt<std::string> OutputFilename;\n\n";
1741}
1742
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001743// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001744}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001745
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001746/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001747void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001748 try {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001749
1750 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001751 EmitSourceFileHeader("LLVMC Configuration Library", O);
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001752 EmitIncludes(O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001753
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001754 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001755 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1756 if (Tools.empty())
1757 throw std::string("No tool definitions found!");
1758
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001759 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001760 ToolPropertiesList tool_props;
1761 GlobalOptionDescriptions opt_descs;
1762 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1763
Mikhail Glushenkovd638e852008-05-30 06:26:08 +00001764 RecordVector OptionLists = Records.getAllDerivedDefinitions("OptionList");
1765 CollectPropertiesFromOptionList(OptionLists.begin(), OptionLists.end(),
1766 opt_descs);
1767
Mikhail Glushenkove5fcb552008-05-30 06:28:37 +00001768 // Check that there are no options without side effects (specified
1769 // only in the OptionList).
1770 CheckForSuperfluousOptions(tool_props, opt_descs);
1771
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001772 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001773 EmitOptionDescriptions(opt_descs, O);
1774
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001775 // Emit hook declarations.
1776 EmitHookDeclarations(tool_props, O);
1777
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001778 // Emit PopulateLanguageMap() function
1779 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001780 EmitPopulateLanguageMap(Records, O);
1781
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001782 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001783 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1784 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001785 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001786
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001787 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1788 if (!CompilationGraphRecord)
1789 throw std::string("Compilation graph description not found!");
1790
1791 // Typecheck the compilation graph.
1792 TypecheckGraph(CompilationGraphRecord, tool_props);
1793
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001794 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001795 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001796
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001797 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001798 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001799
Mikhail Glushenkov945522f2008-09-22 20:49:34 +00001800 // Emit code for plugin registration.
1801 EmitRegisterPlugin(O);
1802
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001803 // EOF
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001804 } catch (std::exception& Error) {
1805 throw Error.what() + std::string(" - usually this means a syntax error.");
1806 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001807}