blob: 45f6a13d2305bb16f9dba47adb193d695ea4805c [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"
23
24#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>
29
30using namespace llvm;
31
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000032namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000033
34//===----------------------------------------------------------------------===//
35/// Typedefs
36
37typedef std::vector<Record*> RecordVector;
38typedef std::vector<std::string> StrVector;
39
40//===----------------------------------------------------------------------===//
41/// Constants
42
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000043// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000044const char * Indent1 = " ";
45const char * Indent2 = " ";
46const char * Indent3 = " ";
47const char * Indent4 = " ";
48
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 Glushenkov35576b02008-05-30 06:10:19 +000058int InitPtrToInt(const Init* ptr) {
59 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000060 return val.getValue();
61}
62
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000063const std::string& InitPtrToString(const Init* ptr) {
64 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
65 return val.getValue();
66}
67
68const ListInit& InitPtrToList(const Init* ptr) {
69 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
70 return val;
71}
72
73const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000074 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000075 return val;
76}
77
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000078// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000079// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000080void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
81 if (d->getNumArgs() < min_arguments)
82 throw "Property " + d->getOperator()->getAsString()
83 + " has too few arguments!";
84}
85
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000086// isDagEmpty - is this DAG marked with an empty marker?
87bool isDagEmpty (const DagInit* d) {
88 return d->getOperator()->getAsString() == "empty";
89}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000090
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000091//===----------------------------------------------------------------------===//
92/// Back-end specific code
93
94// A command-line option can have one of the following types:
95//
Mikhail Glushenkovb623c322008-05-30 06:22:52 +000096// Alias - an alias for another option.
97//
98// Switch - a simple switch without arguments, e.g. -O2
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000099//
100// Parameter - an option that takes one(and only one) argument, e.g. -o file,
101// --output=file
102//
103// ParameterList - same as Parameter, but more than one occurence
104// of the option is allowed, e.g. -lm -lpthread
105//
106// Prefix - argument is everything after the prefix,
107// e.g. -Wa,-foo,-bar, -DNAME=VALUE
108//
109// PrefixList - same as Prefix, but more than one option occurence is
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000110// allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000111
112namespace OptionType {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000113 enum OptionType { Alias, Switch,
114 Parameter, ParameterList, Prefix, PrefixList};
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000115}
116
117bool IsListOptionType (OptionType::OptionType t) {
118 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
119}
120
121// Code duplication here is necessary because one option can affect
122// several tools and those tools may have different actions associated
123// with this option. GlobalOptionDescriptions are used to generate
124// the option registration code, while ToolOptionDescriptions are used
125// to generate tool-specific code.
126
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000127/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000128struct OptionDescription {
129 OptionType::OptionType Type;
130 std::string Name;
131
132 OptionDescription(OptionType::OptionType t = OptionType::Switch,
133 const std::string& n = "")
134 : Type(t), Name(n)
135 {}
136
137 const char* GenTypeDeclaration() const {
138 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000139 case OptionType::Alias:
140 return "cl::alias";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000141 case OptionType::PrefixList:
142 case OptionType::ParameterList:
143 return "cl::list<std::string>";
144 case OptionType::Switch:
145 return "cl::opt<bool>";
146 case OptionType::Parameter:
147 case OptionType::Prefix:
148 default:
149 return "cl::opt<std::string>";
150 }
151 }
152
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000153 // Escape commas and other symbols not allowed in the C++ variable
154 // names. Makes it possible to use options with names like "Wa,"
155 // (useful for prefix options).
156 std::string EscapeVariableName(const std::string& Var) const {
157 std::string ret;
158 for (unsigned i = 0; i != Var.size(); ++i) {
159 if (Var[i] == ',') {
160 ret += "_comma_";
161 }
162 else {
163 ret.push_back(Var[i]);
164 }
165 }
166 return ret;
167 }
168
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000169 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000170 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000171 switch (Type) {
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000172 case OptionType::Alias:
173 return "AutoGeneratedAlias" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000174 case OptionType::Switch:
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000175 return "AutoGeneratedSwitch" + EscapedName;
176 case OptionType::Prefix:
177 return "AutoGeneratedPrefix" + EscapedName;
178 case OptionType::PrefixList:
179 return "AutoGeneratedPrefixList" + EscapedName;
180 case OptionType::Parameter:
181 return "AutoGeneratedParameter" + EscapedName;
182 case OptionType::ParameterList:
183 default:
184 return "AutoGeneratedParameterList" + EscapedName;
185 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000186 }
187
188};
189
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000190// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000191
192namespace GlobalOptionDescriptionFlags {
193 enum GlobalOptionDescriptionFlags { Required = 0x1 };
194}
195
196struct GlobalOptionDescription : public OptionDescription {
197 std::string Help;
198 unsigned Flags;
199
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000200 // We need to provide a default constructor because
201 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000202 GlobalOptionDescription() : OptionDescription(), Flags(0)
203 {}
204
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000205 GlobalOptionDescription (OptionType::OptionType t, const std::string& n,
206 const std::string& h = DefaultHelpString)
207 : OptionDescription(t, n), Help(h), Flags(0)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000208 {}
209
210 bool isRequired() const {
211 return Flags & GlobalOptionDescriptionFlags::Required;
212 }
213 void setRequired() {
214 Flags |= GlobalOptionDescriptionFlags::Required;
215 }
216
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000217 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000218 void Merge (const GlobalOptionDescription& other)
219 {
220 if (other.Type != Type)
221 throw "Conflicting definitions for the option " + Name + "!";
222
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000223 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000224 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000225 else if (other.Help != DefaultHelpString) {
226 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000227 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000228 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000229
230 Flags |= other.Flags;
231 }
232};
233
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000234/// GlobalOptionDescriptions - A GlobalOptionDescription array
235/// together with some flags affecting generation of option
236/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000237struct GlobalOptionDescriptions {
238 typedef StringMap<GlobalOptionDescription> container_type;
239 typedef container_type::const_iterator const_iterator;
240
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000241 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000242 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000243 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000244 bool HasSink;
245
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000246 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
247 const_iterator I = Descriptions.find(OptName);
248 if (I != Descriptions.end())
249 return I->second;
250 else
251 throw OptName + ": no such option!";
252 }
253
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000254 // Support for STL-style iteration
255 const_iterator begin() const { return Descriptions.begin(); }
256 const_iterator end() const { return Descriptions.end(); }
257};
258
259
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000260// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000261
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000262// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000263namespace ToolOptionDescriptionFlags {
264 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
265 Forward = 0x2, UnpackValues = 0x4};
266}
267namespace OptionPropertyType {
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000268 enum OptionPropertyType { AppendCmd, OutputSuffix };
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000269}
270
271typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
272OptionProperty;
273typedef SmallVector<OptionProperty, 4> OptionPropertyList;
274
275struct ToolOptionDescription : public OptionDescription {
276 unsigned Flags;
277 OptionPropertyList Props;
278
279 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000280 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000281
282 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
283 : OptionDescription(t, n)
284 {}
285
286 // Various boolean properties
287 bool isStopCompilation() const {
288 return Flags & ToolOptionDescriptionFlags::StopCompilation;
289 }
290 void setStopCompilation() {
291 Flags |= ToolOptionDescriptionFlags::StopCompilation;
292 }
293
294 bool isForward() const {
295 return Flags & ToolOptionDescriptionFlags::Forward;
296 }
297 void setForward() {
298 Flags |= ToolOptionDescriptionFlags::Forward;
299 }
300
301 bool isUnpackValues() const {
302 return Flags & ToolOptionDescriptionFlags::UnpackValues;
303 }
304 void setUnpackValues() {
305 Flags |= ToolOptionDescriptionFlags::UnpackValues;
306 }
307
308 void AddProperty (OptionPropertyType::OptionPropertyType t,
309 const std::string& val)
310 {
311 Props.push_back(std::make_pair(t, val));
312 }
313};
314
315typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
316
317// Tool information record
318
319namespace ToolFlags {
320 enum ToolFlags { Join = 0x1, Sink = 0x2 };
321}
322
323struct ToolProperties : public RefCountedBase<ToolProperties> {
324 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000325 Init* CmdLine;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000326 StrVector InLanguage;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000327 std::string OutLanguage;
328 std::string OutputSuffix;
329 unsigned Flags;
330 ToolOptionDescriptions OptDescs;
331
332 // Various boolean properties
333 void setSink() { Flags |= ToolFlags::Sink; }
334 bool isSink() const { return Flags & ToolFlags::Sink; }
335 void setJoin() { Flags |= ToolFlags::Join; }
336 bool isJoin() const { return Flags & ToolFlags::Join; }
337
338 // Default ctor here is needed because StringMap can only store
339 // DefaultConstructible objects
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000340 ToolProperties() : Flags(0) {}
341 ToolProperties (const std::string& n) : Name(n), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000342};
343
344
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000345/// ToolPropertiesList - A list of Tool information records
346/// IntrusiveRefCntPtrs are used here because StringMap has no copy
347/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000348typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
349
350
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000351/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000352/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000353class CollectProperties {
354private:
355
356 /// Implementation details
357
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000358 /// PropertyHandler - a function that extracts information
359 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000360 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000361
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000362 /// PropertyHandlerMap - A map from property names to property
363 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000364 typedef StringMap<PropertyHandler> PropertyHandlerMap;
365
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000366 /// OptionPropertyHandler - a function that extracts information
367 /// about a given option property from its DAG representation.
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000368 typedef void (CollectProperties::* OptionPropertyHandler)
369 (const DagInit*, GlobalOptionDescription &);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000370
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000371 /// OptionPropertyHandlerMap - A map from option property names to
372 /// option property handlers
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000373 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
374
375 // Static maps from strings to CollectProperties methods("handlers")
376 static PropertyHandlerMap propertyHandlers_;
377 static OptionPropertyHandlerMap optionPropertyHandlers_;
378 static bool staticMembersInitialized_;
379
380
381 /// This is where the information is stored
382
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000383 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000384 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000385 /// optDescs_ - OptionDescriptions table (used to register options
386 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000387 GlobalOptionDescriptions& optDescs_;
388
389public:
390
391 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
392 : toolProps_(p), optDescs_(d)
393 {
394 if (!staticMembersInitialized_) {
395 // Init tool property handlers
396 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
397 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
398 propertyHandlers_["join"] = &CollectProperties::onJoin;
399 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
400 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
401 propertyHandlers_["parameter_option"]
402 = &CollectProperties::onParameter;
403 propertyHandlers_["parameter_list_option"] =
404 &CollectProperties::onParameterList;
405 propertyHandlers_["prefix_option"] = &CollectProperties::onPrefix;
406 propertyHandlers_["prefix_list_option"] =
407 &CollectProperties::onPrefixList;
408 propertyHandlers_["sink"] = &CollectProperties::onSink;
409 propertyHandlers_["switch_option"] = &CollectProperties::onSwitch;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000410 propertyHandlers_["alias_option"] = &CollectProperties::onAlias;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000411
412 // Init option property handlers
413 optionPropertyHandlers_["append_cmd"] = &CollectProperties::onAppendCmd;
414 optionPropertyHandlers_["forward"] = &CollectProperties::onForward;
415 optionPropertyHandlers_["help"] = &CollectProperties::onHelp;
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000416 optionPropertyHandlers_["output_suffix"] =
417 &CollectProperties::onOutputSuffixOptionProp;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000418 optionPropertyHandlers_["required"] = &CollectProperties::onRequired;
419 optionPropertyHandlers_["stop_compilation"] =
420 &CollectProperties::onStopCompilation;
421 optionPropertyHandlers_["unpack_values"] =
422 &CollectProperties::onUnpackValues;
423
424 staticMembersInitialized_ = true;
425 }
426 }
427
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000428 /// operator() - Gets called for every tool property; Just forwards
429 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000430 void operator() (Init* i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000431 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000432 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000433 PropertyHandlerMap::iterator method
434 = propertyHandlers_.find(property_name);
435
436 if (method != propertyHandlers_.end()) {
437 PropertyHandler h = method->second;
438 (this->*h)(&d);
439 }
440 else {
441 throw "Unknown tool property: " + property_name + "!";
442 }
443 }
444
445private:
446
447 /// Property handlers --
448 /// Functions that extract information about tool properties from
449 /// DAG representation.
450
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000451 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000452 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000453 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000454 }
455
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000456 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000457 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000458 Init* arg = d->getArg(0);
459
460 // Find out the argument's type.
461 if (typeid(*arg) == typeid(StringInit)) {
462 // It's a string.
463 toolProps_.InLanguage.push_back(InitPtrToString(arg));
464 }
465 else {
466 // It's a list.
467 const ListInit& lst = InitPtrToList(arg);
468 StrVector& out = toolProps_.InLanguage;
469
470 // Copy strings to the output vector.
471 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
472 B != E; ++B) {
473 out.push_back(InitPtrToString(*B));
474 }
475
476 // Remove duplicates.
477 std::sort(out.begin(), out.end());
478 StrVector::iterator newE = std::unique(out.begin(), out.end());
479 out.erase(newE, out.end());
480 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000481 }
482
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000483 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000484 checkNumberOfArguments(d, 0);
485 toolProps_.setJoin();
486 }
487
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000488 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000489 checkNumberOfArguments(d, 1);
490 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
491 }
492
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000493 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000494 checkNumberOfArguments(d, 1);
495 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
496 }
497
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000498 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000499 checkNumberOfArguments(d, 0);
500 optDescs_.HasSink = true;
501 toolProps_.setSink();
502 }
503
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000504 void onAlias (const DagInit* d) {
505 checkNumberOfArguments(d, 2);
506 // We just need a GlobalOptionDescription for the aliases.
507 insertDescription
508 (GlobalOptionDescription(OptionType::Alias,
509 InitPtrToString(d->getArg(0)),
510 InitPtrToString(d->getArg(1))));
511 }
512
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000513 void onSwitch (const DagInit* d) {
514 addOption(d, OptionType::Switch);
515 }
516
517 void onParameter (const DagInit* d) {
518 addOption(d, OptionType::Parameter);
519 }
520
521 void onParameterList (const DagInit* d) {
522 addOption(d, OptionType::ParameterList);
523 }
524
525 void onPrefix (const DagInit* d) {
526 addOption(d, OptionType::Prefix);
527 }
528
529 void onPrefixList (const DagInit* d) {
530 addOption(d, OptionType::PrefixList);
531 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000532
533 /// Option property handlers --
534 /// Methods that handle properties that are common for all types of
535 /// options (like append_cmd, stop_compilation)
536
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000537 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000538 checkNumberOfArguments(d, 1);
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000539 const std::string& cmd = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000540
541 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
542 }
543
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000544 void onOutputSuffixOptionProp (const DagInit* d, GlobalOptionDescription& o) {
545 checkNumberOfArguments(d, 1);
546 const std::string& suf = InitPtrToString(d->getArg(0));
547
548 if (toolProps_.OptDescs[o.Name].Type != OptionType::Switch)
549 throw "Option " + o.Name
550 + " can't have 'output_suffix' property since it isn't a switch!";
551
552 toolProps_.OptDescs[o.Name].AddProperty
553 (OptionPropertyType::OutputSuffix, suf);
554 }
555
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000556 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000557 checkNumberOfArguments(d, 0);
558 toolProps_.OptDescs[o.Name].setForward();
559 }
560
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000561 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000562 checkNumberOfArguments(d, 1);
563 const std::string& help_message = InitPtrToString(d->getArg(0));
564
565 o.Help = help_message;
566 }
567
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000568 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000569 checkNumberOfArguments(d, 0);
570 o.setRequired();
571 }
572
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000573 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000574 checkNumberOfArguments(d, 0);
575 if (o.Type != OptionType::Switch)
576 throw std::string("Only options of type Switch can stop compilation!");
577 toolProps_.OptDescs[o.Name].setStopCompilation();
578 }
579
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000580 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000581 checkNumberOfArguments(d, 0);
582 toolProps_.OptDescs[o.Name].setUnpackValues();
583 }
584
585 /// Helper functions
586
587 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000588 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000589 checkNumberOfArguments(d, 2);
590 const std::string& name = InitPtrToString(d->getArg(0));
591
592 GlobalOptionDescription o(t, name);
593 toolProps_.OptDescs[name].Type = t;
594 toolProps_.OptDescs[name].Name = name;
595 processOptionProperties(d, o);
596 insertDescription(o);
597 }
598
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000599 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
600 void insertDescription (const GlobalOptionDescription& o)
601 {
602 if (optDescs_.Descriptions.count(o.Name)) {
603 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
604 D.Merge(o);
605 }
606 else {
607 optDescs_.Descriptions[o.Name] = o;
608 }
609 }
610
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000611 /// processOptionProperties - Go through the list of option
612 /// properties and call a corresponding handler for each.
613 ///
614 /// Parameters:
615 /// name - option name
616 /// d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000617 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000618 // First argument is option name
619 checkNumberOfArguments(d, 2);
620
621 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000622 const DagInit& option_property
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000623 = InitPtrToDag(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000624 const std::string& option_property_name
625 = option_property.getOperator()->getAsString();
626 OptionPropertyHandlerMap::iterator method
627 = optionPropertyHandlers_.find(option_property_name);
628
629 if (method != optionPropertyHandlers_.end()) {
630 OptionPropertyHandler h = method->second;
631 (this->*h)(&option_property, o);
632 }
633 else {
634 throw "Unknown option property: " + option_property_name + "!";
635 }
636 }
637 }
638};
639
640// Static members of CollectProperties
641CollectProperties::PropertyHandlerMap
642CollectProperties::propertyHandlers_;
643
644CollectProperties::OptionPropertyHandlerMap
645CollectProperties::optionPropertyHandlers_;
646
647bool CollectProperties::staticMembersInitialized_ = false;
648
649
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000650/// CollectToolProperties - Gather information from the parsed
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000651/// TableGen data (basically a wrapper for the CollectProperties
652/// function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000653void CollectToolProperties (RecordVector::const_iterator B,
654 RecordVector::const_iterator E,
655 ToolPropertiesList& TPList,
656 GlobalOptionDescriptions& OptDescs)
657{
658 // Iterate over a properties list of every Tool definition
659 for (;B!=E;++B) {
660 RecordVector::value_type T = *B;
661 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000662
663 IntrusiveRefCntPtr<ToolProperties>
664 ToolProps(new ToolProperties(T->getName()));
665
666 std::for_each(PropList->begin(), PropList->end(),
667 CollectProperties(*ToolProps, OptDescs));
668 TPList.push_back(ToolProps);
669 }
670}
671
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000672/// EmitCaseTest1Arg - Helper function used by
673/// EmitCaseConstructHandler.
674bool EmitCaseTest1Arg(const std::string& TestName,
675 const DagInit& d,
676 const GlobalOptionDescriptions& OptDescs,
677 std::ostream& O) {
678 checkNumberOfArguments(&d, 1);
679 const std::string& OptName = InitPtrToString(d.getArg(0));
680 if (TestName == "switch_on") {
681 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
682 if (OptDesc.Type != OptionType::Switch)
683 throw OptName + ": incorrect option type!";
684 O << OptDesc.GenVariableName();
685 return true;
686 } else if (TestName == "input_languages_contain") {
687 O << "InLangs.count(\"" << OptName << "\") != 0";
688 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000689 } else if (TestName == "in_language") {
690 // Works only for cmd_line!
691 O << "GetLanguage(inFile) == \"" << OptName << '\"';
692 return true;
693 } else if (TestName == "not_empty") {
694 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
695 if (OptDesc.Type == OptionType::Switch)
696 throw OptName + ": incorrect option type!";
697 O << '!' << OptDesc.GenVariableName() << ".empty()";
698 return true;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000699 }
700
701 return false;
702}
703
704/// EmitCaseTest2Args - Helper function used by
705/// EmitCaseConstructHandler.
706bool EmitCaseTest2Args(const std::string& TestName,
707 const DagInit& d,
708 const char* IndentLevel,
709 const GlobalOptionDescriptions& OptDescs,
710 std::ostream& O) {
711 checkNumberOfArguments(&d, 2);
712 const std::string& OptName = InitPtrToString(d.getArg(0));
713 const std::string& OptArg = InitPtrToString(d.getArg(1));
714 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
715
716 if (TestName == "parameter_equals") {
717 if (OptDesc.Type != OptionType::Parameter
718 && OptDesc.Type != OptionType::Prefix)
719 throw OptName + ": incorrect option type!";
720 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
721 return true;
722 }
723 else if (TestName == "element_in_list") {
724 if (OptDesc.Type != OptionType::ParameterList
725 && OptDesc.Type != OptionType::PrefixList)
726 throw OptName + ": incorrect option type!";
727 const std::string& VarName = OptDesc.GenVariableName();
728 O << "std::find(" << VarName << ".begin(),\n"
729 << IndentLevel << Indent1 << VarName << ".end(), \""
730 << OptArg << "\") != " << VarName << ".end()";
731 return true;
732 }
733
734 return false;
735}
736
737// Forward declaration.
738// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
739void EmitCaseTest(const DagInit& d, const char* IndentLevel,
740 const GlobalOptionDescriptions& OptDescs,
741 std::ostream& O);
742
743/// EmitLogicalOperationTest - Helper function used by
744/// EmitCaseConstructHandler.
745void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
746 const char* IndentLevel,
747 const GlobalOptionDescriptions& OptDescs,
748 std::ostream& O) {
749 O << '(';
750 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000751 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000752 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
753 if (j != NumArgs - 1)
754 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
755 else
756 O << ')';
757 }
758}
759
760/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
761void EmitCaseTest(const DagInit& d, const char* IndentLevel,
762 const GlobalOptionDescriptions& OptDescs,
763 std::ostream& O) {
764 const std::string& TestName = d.getOperator()->getAsString();
765
766 if (TestName == "and")
767 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
768 else if (TestName == "or")
769 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
770 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
771 return;
772 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
773 return;
774 else
775 throw TestName + ": unknown edge property!";
776}
777
778// Emit code that handles the 'case' construct.
779// Takes a function object that should emit code for every case clause.
780// Callback's type is
781// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
782template <typename F>
783void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000784 const F& Callback, bool EmitElseIf,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000785 const GlobalOptionDescriptions& OptDescs,
786 std::ostream& O) {
787 assert(d->getOperator()->getAsString() == "case");
788
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000789 unsigned numArgs = d->getNumArgs();
790 if (d->getNumArgs() < 2)
791 throw "There should be at least one clause in the 'case' expression:\n"
792 + d->getAsString();
793
794 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000795 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000796
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000797 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000798 if (Test.getOperator()->getAsString() == "default") {
799 if (i+2 != numArgs)
800 throw std::string("The 'default' clause should be the last in the"
801 "'case' construct!");
802 O << IndentLevel << "else {\n";
803 }
804 else {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000805 O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000806 EmitCaseTest(Test, IndentLevel, OptDescs, O);
807 O << ") {\n";
808 }
809
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000810 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000811 ++i;
812 if (i == numArgs)
813 throw "Case construct handler: no corresponding action "
814 "found for the test " + Test.getAsString() + '!';
815
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +0000816 Init* arg = d->getArg(i);
817 if (dynamic_cast<DagInit*>(arg)
818 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") {
819 EmitCaseConstructHandler(static_cast<DagInit*>(arg),
820 (std::string(IndentLevel) + Indent1).c_str(),
821 Callback, EmitElseIf, OptDescs, O);
822 }
823 else {
824 Callback(arg, IndentLevel, O);
825 }
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000826 O << IndentLevel << "}\n";
827 }
828}
829
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000830/// EmitForwardOptionPropertyHandlingCode - Helper function used to
831/// implement EmitOptionPropertyHandlingCode(). Emits code for
832/// handling the (forward) option property.
833void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
834 std::ostream& O) {
835 switch (D.Type) {
836 case OptionType::Switch:
837 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
838 break;
839 case OptionType::Parameter:
840 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
841 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
842 break;
843 case OptionType::Prefix:
844 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
845 << D.GenVariableName() << ");\n";
846 break;
847 case OptionType::PrefixList:
848 O << Indent3 << "for (" << D.GenTypeDeclaration()
849 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
850 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
851 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
852 << "*B);\n";
853 break;
854 case OptionType::ParameterList:
855 O << Indent3 << "for (" << D.GenTypeDeclaration()
856 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
857 << Indent3 << "E = " << D.GenVariableName()
858 << ".end() ; B != E; ++B) {\n"
859 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
860 << Indent4 << "vec.push_back(*B);\n"
861 << Indent3 << "}\n";
862 break;
Mikhail Glushenkovb623c322008-05-30 06:22:52 +0000863 case OptionType::Alias:
864 default:
865 throw std::string("Aliases are not allowed in tool option descriptions!");
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000866 }
867}
868
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000869// ToolOptionHasInterestingProperties - A helper function used by
870// EmitOptionPropertyHandlingCode() that tells us whether we should
871// emit any property handling code at all.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000872bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000873 bool ret = false;
874 for (OptionPropertyList::const_iterator B = D.Props.begin(),
875 E = D.Props.end(); B != E; ++B) {
876 const OptionProperty& OptProp = *B;
877 if (OptProp.first == OptionPropertyType::AppendCmd)
878 ret = true;
879 }
880 if (D.isForward() || D.isUnpackValues())
881 ret = true;
882 return ret;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000883}
884
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000885/// EmitOptionPropertyHandlingCode - Helper function used by
886/// EmitGenerateActionMethod(). Emits code that handles option
887/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000888void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000889 std::ostream& O)
890{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000891 if (!ToolOptionHasInterestingProperties(D))
892 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000893 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000894 O << Indent2 << "if (";
895 if (D.Type == OptionType::Switch)
896 O << D.GenVariableName();
897 else
898 O << '!' << D.GenVariableName() << ".empty()";
899
900 O <<") {\n";
901
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000902 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000903 for (OptionPropertyList::const_iterator B = D.Props.begin(),
904 E = D.Props.end(); B!=E; ++B) {
905 const OptionProperty& val = *B;
906
907 switch (val.first) {
908 // (append_cmd cmd) property
909 case OptionPropertyType::AppendCmd:
910 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
911 break;
912 // Other properties with argument
913 default:
914 break;
915 }
916 }
917
918 // Handle flags
919
920 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000921 if (D.isForward())
922 EmitForwardOptionPropertyHandlingCode(D, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000923
924 // (unpack_values) property
925 if (D.isUnpackValues()) {
926 if (IsListOptionType(D.Type)) {
927 O << Indent3 << "for (" << D.GenTypeDeclaration()
928 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
929 << Indent3 << "E = " << D.GenVariableName()
930 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000931 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000932 }
933 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000934 O << Indent3 << "llvm::SplitString("
935 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000936 }
937 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000938 throw std::string("Switches can't have unpack_values property!");
939 }
940 }
941
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000942 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000943 O << Indent2 << "}\n";
944}
945
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000946/// SubstituteSpecialCommands - Perform string substitution for $CALL
947/// and $ENV. Helper function used by EmitCmdLineVecFill().
948std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000949 size_t cparen = cmd.find(")");
950 std::string ret;
951
952 if (cmd.find("$CALL(") == 0) {
953 if (cmd.size() == 6)
954 throw std::string("$CALL invocation: empty argument list!");
955
956 ret += "hooks::";
957 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
958 ret += "()";
959 }
960 else if (cmd.find("$ENV(") == 0) {
961 if (cmd.size() == 5)
962 throw std::string("$ENV invocation: empty argument list!");
963
964 ret += "std::getenv(\"";
965 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
966 ret += "\")";
967 }
968 else {
969 throw "Unknown special command: " + cmd;
970 }
971
972 if (cmd.begin() + cparen + 1 != cmd.end()) {
973 ret += " + std::string(\"";
974 ret += (cmd.c_str() + cparen + 1);
975 ret += "\")";
976 }
977
978 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000979}
980
981/// EmitCmdLineVecFill - Emit code that fills in the command line
982/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000983void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
984 bool Version, const char* IndentLevel,
985 std::ostream& O) {
986 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000987 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000988 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000989 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000990
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000991 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000992 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000993 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000994 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000995 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000996 if (cmd.at(0) == '$') {
997 if (cmd == "$INFILE") {
998 if (Version)
999 O << "for (PathVector::const_iterator B = inFiles.begin()"
1000 << ", E = inFiles.end();\n"
1001 << IndentLevel << "B != E; ++B)\n"
1002 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
1003 else
1004 O << "vec.push_back(inFile.toString());\n";
1005 }
1006 else if (cmd == "$OUTFILE") {
1007 O << "vec.push_back(outFile.toString());\n";
1008 }
1009 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001010 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
1011 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001012 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001013 }
1014 else {
1015 O << "vec.push_back(\"" << cmd << "\");\n";
1016 }
1017 }
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001018 O << IndentLevel << "cmd = "
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001019 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
1020 : "\"" + StrVec[0] + "\"")
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001021 << ";\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001022}
1023
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001024/// EmitCmdLineVecFillCallback - A function object wrapper around
1025/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
1026/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001027class EmitCmdLineVecFillCallback {
1028 bool Version;
1029 const std::string& ToolName;
1030 public:
1031 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
1032 : Version(Ver), ToolName(TN) {}
1033
1034 void operator()(const Init* Statement, const char* IndentLevel,
1035 std::ostream& O) const
1036 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001037 EmitCmdLineVecFill(Statement, ToolName, Version,
1038 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001039 }
1040};
1041
1042// EmitGenerateActionMethod - Emit one of two versions of the
1043// Tool::GenerateAction() method.
1044void EmitGenerateActionMethod (const ToolProperties& P,
1045 const GlobalOptionDescriptions& OptDescs,
1046 bool Version, std::ostream& O) {
1047 if (Version)
1048 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1049 else
1050 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1051
1052 O << Indent2 << "const sys::Path& outFile,\n"
1053 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
1054 << Indent1 << "{\n"
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001055 << Indent2 << "std::string cmd;\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001056 << Indent2 << "std::vector<std::string> vec;\n";
1057
1058 // cmd_line is either a string or a 'case' construct.
1059 if (typeid(*P.CmdLine) == typeid(StringInit))
1060 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
1061 else
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001062 EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001063 EmitCmdLineVecFillCallback(Version, P.Name),
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001064 true, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001065
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001066 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001067 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1068 E = P.OptDescs.end(); B != E; ++B) {
1069 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001070 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001071 }
1072
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001073 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001074 if (P.isSink()) {
1075 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1076 << Indent3 << "vec.insert(vec.end(), "
1077 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1078 << Indent2 << "}\n";
1079 }
1080
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001081 O << Indent2 << "return Action(cmd, vec);\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001082 << Indent1 << "}\n\n";
1083}
1084
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001085/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1086/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001087void EmitGenerateActionMethods (const ToolProperties& P,
1088 const GlobalOptionDescriptions& OptDescs,
1089 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001090 if (!P.isJoin())
1091 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001092 << Indent2 << "const llvm::sys::Path& outFile,\n"
1093 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001094 << Indent1 << "{\n"
1095 << Indent2 << "throw std::runtime_error(\"" << P.Name
1096 << " is not a Join tool!\");\n"
1097 << Indent1 << "}\n\n";
1098 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001099 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001100
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001101 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001102}
1103
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001104/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
1105/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001106void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
1107 O << Indent1 << "bool IsLast() const {\n"
1108 << Indent2 << "bool last = false;\n";
1109
1110 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1111 E = P.OptDescs.end(); B != E; ++B) {
1112 const ToolOptionDescription& val = B->second;
1113
1114 if (val.isStopCompilation())
1115 O << Indent2
1116 << "if (" << val.GenVariableName()
1117 << ")\n" << Indent3 << "last = true;\n";
1118 }
1119
1120 O << Indent2 << "return last;\n"
1121 << Indent1 << "}\n\n";
1122}
1123
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001124/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1125/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001126void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001127 O << Indent1 << "StrVector InputLanguages() const {\n"
1128 << Indent2 << "StrVector ret;\n";
1129
1130 for (StrVector::const_iterator B = P.InLanguage.begin(),
1131 E = P.InLanguage.end(); B != E; ++B) {
1132 O << Indent2 << "ret.push_back(\"" << *B << "\");\n";
1133 }
1134
1135 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001136 << Indent1 << "}\n\n";
1137
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001138 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001139 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1140 << Indent1 << "}\n\n";
1141}
1142
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001143/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1144/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001145void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001146 O << Indent1 << "const char* OutputSuffix() const {\n"
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +00001147 << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
1148
1149 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1150 E = P.OptDescs.end(); B != E; ++B) {
1151 const ToolOptionDescription& OptDesc = B->second;
1152 for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
1153 E = OptDesc.Props.end(); B != E; ++B) {
1154 const OptionProperty& OptProp = *B;
1155 if (OptProp.first == OptionPropertyType::OutputSuffix) {
1156 O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
1157 << Indent3 << "ret = \"" << OptProp.second << "\";\n";
1158 }
1159 }
1160 }
1161
1162 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001163 << Indent1 << "}\n\n";
1164}
1165
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001166/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001167void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001168 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001169 << Indent2 << "return \"" << P.Name << "\";\n"
1170 << Indent1 << "}\n\n";
1171}
1172
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001173/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1174/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001175void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1176 O << Indent1 << "bool IsJoin() const {\n";
1177 if (P.isJoin())
1178 O << Indent2 << "return true;\n";
1179 else
1180 O << Indent2 << "return false;\n";
1181 O << Indent1 << "}\n\n";
1182}
1183
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001184/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001185void EmitToolClassDefinition (const ToolProperties& P,
1186 const GlobalOptionDescriptions& OptDescs,
1187 std::ostream& O) {
1188 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001189 return;
1190
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001191 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001192 O << "class " << P.Name << " : public ";
1193 if (P.isJoin())
1194 O << "JoinTool";
1195 else
1196 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +00001197 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001198
1199 EmitNameMethod(P, O);
1200 EmitInOutLanguageMethods(P, O);
1201 EmitOutputSuffixMethod(P, O);
1202 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001203 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001204 EmitIsLastMethod(P, O);
1205
1206 // Close class definition
1207 O << "};\n\n";
1208}
1209
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001210/// EmitOptionDescriptions - Iterate over a list of option
1211/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001212void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1213 std::ostream& O)
1214{
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001215 std::vector<GlobalOptionDescription> Aliases;
1216
Mikhail Glushenkov52a54132008-05-30 06:23:29 +00001217 // Emit static cl::Option variables.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001218 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1219 E = descs.end(); B!=E; ++B) {
1220 const GlobalOptionDescription& val = B->second;
1221
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001222 if (val.Type == OptionType::Alias) {
1223 Aliases.push_back(val);
1224 continue;
1225 }
1226
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001227 O << val.GenTypeDeclaration() << ' '
1228 << val.GenVariableName()
1229 << "(\"" << val.Name << '\"';
1230
1231 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1232 O << ", cl::Prefix";
1233
1234 if (val.isRequired()) {
1235 switch (val.Type) {
1236 case OptionType::PrefixList:
1237 case OptionType::ParameterList:
1238 O << ", cl::OneOrMore";
1239 break;
1240 default:
1241 O << ", cl::Required";
1242 }
1243 }
1244
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001245 if (!val.Help.empty())
1246 O << ", cl::desc(\"" << val.Help << "\")";
1247
1248 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001249 }
1250
Mikhail Glushenkovb623c322008-05-30 06:22:52 +00001251 // Emit the aliases (they should go after all the 'proper' options).
1252 for (std::vector<GlobalOptionDescription>::const_iterator
1253 B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
1254 const GlobalOptionDescription& val = *B;
1255
1256 O << val.GenTypeDeclaration() << ' '
1257 << val.GenVariableName()
1258 << "(\"" << val.Name << '\"';
1259
1260 GlobalOptionDescriptions::container_type
1261 ::const_iterator F = descs.Descriptions.find(val.Help);
1262 if (F != descs.Descriptions.end())
1263 O << ", cl::aliasopt(" << F->second.GenVariableName() << ")";
1264 else
1265 throw val.Name + ": alias to an unknown option!";
1266
1267 O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
1268 }
1269
1270 // Emit the sink option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001271 if (descs.HasSink)
1272 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1273
1274 O << '\n';
1275}
1276
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001277/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001278void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1279{
1280 // Get the relevant field out of RecordKeeper
1281 Record* LangMapRecord = Records.getDef("LanguageMap");
1282 if (!LangMapRecord)
1283 throw std::string("Language map definition not found!");
1284
1285 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1286 if (!LangsToSuffixesList)
1287 throw std::string("Error in the language map definition!");
1288
1289 // Generate code
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001290 O << "void llvmc::PopulateLanguageMap() {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001291
1292 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1293 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1294
1295 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1296 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1297
1298 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001299 O << Indent1 << "GlobalLanguageMap[\""
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001300 << InitPtrToString(Suffixes->getElement(i))
1301 << "\"] = \"" << Lang << "\";\n";
1302 }
1303
1304 O << "}\n\n";
1305}
1306
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001307/// FillInToolToLang - Fills in two tables that map tool names to
1308/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001309void FillInToolToLang (const ToolPropertiesList& TPList,
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001310 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001311 StringMap<std::string>& ToolToOutLang) {
1312 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1313 B != E; ++B) {
1314 const ToolProperties& P = *(*B);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001315 for (StrVector::const_iterator B = P.InLanguage.begin(),
1316 E = P.InLanguage.end(); B != E; ++B)
1317 ToolToInLang[P.Name].insert(*B);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001318 ToolToOutLang[P.Name] = P.OutLanguage;
1319 }
1320}
1321
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001322/// TypecheckGraph - Check that names for output and input languages
1323/// on all edges do match.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001324// TOFIX: It would be nice if this function also checked for cycles
1325// and multiple default edges in the graph (better error
1326// reporting). Unfortunately, it is awkward to do right now because
1327// our intermediate representation is not sufficiently
1328// sofisticated. Algorithms like these should be run on a real graph
1329// instead of AST.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001330void TypecheckGraph (Record* CompilationGraph,
1331 const ToolPropertiesList& TPList) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001332 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001333 StringMap<std::string> ToolToOutLang;
1334
1335 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1336 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001337 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1338 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001339
1340 for (unsigned i = 0; i < edges->size(); ++i) {
1341 Record* Edge = edges->getElementAsRecord(i);
1342 Record* A = Edge->getValueAsDef("a");
1343 Record* B = Edge->getValueAsDef("b");
1344 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001345 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001346 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001347 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001348 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001349 throw B->getName() + ": no such tool!";
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001350 if (A->getName() != "root" && IB->second.count(IA->second) == 0)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001351 throw "Edge " + A->getName() + "->" + B->getName()
1352 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001353 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001354 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001355 }
1356}
1357
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001358/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1359/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001360void IncDecWeight (const Init* i, const char* IndentLevel,
1361 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001362 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001363 const std::string& OpName = d.getOperator()->getAsString();
1364
1365 if (OpName == "inc_weight")
1366 O << IndentLevel << Indent1 << "ret += ";
1367 else if (OpName == "dec_weight")
1368 O << IndentLevel << Indent1 << "ret -= ";
1369 else
1370 throw "Unknown operator in edge properties list: " + OpName + '!';
1371
1372 if (d.getNumArgs() > 0)
1373 O << InitPtrToInt(d.getArg(0)) << ";\n";
1374 else
1375 O << "2;\n";
1376
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001377}
1378
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001379/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001380void EmitEdgeClass (unsigned N, const std::string& Target,
1381 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1382 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001383
1384 // Class constructor.
1385 O << "class Edge" << N << ": public Edge {\n"
1386 << "public:\n"
1387 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1388 << "\") {}\n\n"
1389
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001390 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001391 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001392 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001393
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001394 // Handle the 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001395 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001396
1397 O << Indent2 << "return ret;\n"
1398 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001399}
1400
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001401/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001402void EmitEdgeClasses (Record* CompilationGraph,
1403 const GlobalOptionDescriptions& OptDescs,
1404 std::ostream& O) {
1405 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1406
1407 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001408 Record* Edge = edges->getElementAsRecord(i);
1409 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001410 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001411
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001412 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001413 continue;
1414
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001415 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001416 }
1417}
1418
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001419/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1420/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001421void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001422 std::ostream& O)
1423{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001424 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001425
1426 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001427 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001428 << Indent1 << "PopulateLanguageMap();\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001429
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001430 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001431
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001432 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1433 if (Tools.empty())
1434 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001435
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001436 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1437 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001438 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001439 O << Indent1 << "G.insertNode(new "
1440 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001441 }
1442
1443 O << '\n';
1444
1445 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001446 for (unsigned i = 0; i < edges->size(); ++i) {
1447 Record* Edge = edges->getElementAsRecord(i);
1448 Record* A = Edge->getValueAsDef("a");
1449 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001450 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001451
1452 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1453
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001454 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001455 O << "new SimpleEdge(\"" << B->getName() << "\")";
1456 else
1457 O << "new Edge" << i << "()";
1458
1459 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001460 }
1461
1462 O << "}\n\n";
1463}
1464
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001465/// ExtractHookNames - Extract the hook names from all instances of
1466/// $CALL(HookName) in the provided command line string. Helper
1467/// function used by FillInHookNames().
1468void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1469 StrVector cmds;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001470 llvm::SplitString(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001471 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1472 B != E; ++B) {
1473 const std::string& cmd = *B;
1474 if (cmd.find("$CALL(") == 0) {
1475 if (cmd.size() == 6)
1476 throw std::string("$CALL invocation: empty argument list!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001477 HookNames.push_back(std::string(cmd.begin() + 6,
1478 cmd.begin() + cmd.find(")")));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001479 }
1480 }
1481}
1482
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001483/// ExtractHookNamesFromCaseConstruct - Extract hook names from the
1484/// 'case' expression, handle nesting. Helper function used by
1485/// FillInHookNames().
1486void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) {
1487 const DagInit& d = InitPtrToDag(Case);
1488 bool even = false;
1489 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1490 B != E; ++B) {
1491 Init* arg = *B;
1492 if (even && dynamic_cast<DagInit*>(arg)
1493 && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
1494 ExtractHookNamesFromCaseConstruct(arg, HookNames);
1495 else if (even)
1496 ExtractHookNames(arg, HookNames);
1497 even = !even;
1498 }
1499}
1500
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001501/// FillInHookNames - Actually extract the hook names from all command
1502/// line strings. Helper function used by EmitHookDeclarations().
1503void FillInHookNames(const ToolPropertiesList& TPList,
1504 StrVector& HookNames) {
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001505 // For all command lines:
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001506 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1507 E = TPList.end(); B != E; ++B) {
1508 const ToolProperties& P = *(*B);
1509 if (!P.CmdLine)
1510 continue;
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001511 if (dynamic_cast<StringInit*>(P.CmdLine))
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001512 // This is a string.
1513 ExtractHookNames(P.CmdLine, HookNames);
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001514 else
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001515 // This is a 'case' construct.
Mikhail Glushenkovb24c8b22008-05-30 06:22:15 +00001516 ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001517 }
1518}
1519
1520/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1521/// property records and emit hook function declaration for each
1522/// instance of $CALL(HookName).
1523void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1524 std::ostream& O) {
1525 StrVector HookNames;
1526 FillInHookNames(ToolProps, HookNames);
1527 if (HookNames.empty())
1528 return;
1529 std::sort(HookNames.begin(), HookNames.end());
1530 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1531
1532 O << "namespace hooks {\n";
1533 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1534 O << Indent1 << "std::string " << *B << "();\n";
1535
1536 O << "}\n\n";
1537}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001538
1539// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001540}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001541
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001542/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001543void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001544 try {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001545
1546 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001547 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001548
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001549 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001550 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1551 if (Tools.empty())
1552 throw std::string("No tool definitions found!");
1553
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001554 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001555 ToolPropertiesList tool_props;
1556 GlobalOptionDescriptions opt_descs;
1557 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1558
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001559 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001560 EmitOptionDescriptions(opt_descs, O);
1561
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001562 // Emit hook declarations.
1563 EmitHookDeclarations(tool_props, O);
1564
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001565 // Emit PopulateLanguageMap() function
1566 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001567 EmitPopulateLanguageMap(Records, O);
1568
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001569 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001570 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1571 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001572 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001573
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001574 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1575 if (!CompilationGraphRecord)
1576 throw std::string("Compilation graph description not found!");
1577
1578 // Typecheck the compilation graph.
1579 TypecheckGraph(CompilationGraphRecord, tool_props);
1580
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001581 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001582 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001583
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001584 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001585 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001586
1587 // EOF
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001588 } catch (std::exception& Error) {
1589 throw Error.what() + std::string(" - usually this means a syntax error.");
1590 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001591}