blob: a4bd3259c761c5d312c56b90de4a95ca010616c0 [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>
27#include <string>
28
29using namespace llvm;
30
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000031namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
33//===----------------------------------------------------------------------===//
34/// Typedefs
35
36typedef std::vector<Record*> RecordVector;
37typedef std::vector<std::string> StrVector;
38
39//===----------------------------------------------------------------------===//
40/// Constants
41
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000042// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000043const char * Indent1 = " ";
44const char * Indent2 = " ";
45const char * Indent3 = " ";
46const char * Indent4 = " ";
47
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000048// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000049const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
50
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000051// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000052const char * SinkOptionName = "AutoGeneratedSinkOption";
53
54//===----------------------------------------------------------------------===//
55/// Helper functions
56
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000057int InitPtrToInt(const Init* ptr) {
58 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000059 return val.getValue();
60}
61
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000062const std::string& InitPtrToString(const Init* ptr) {
63 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
64 return val.getValue();
65}
66
67const ListInit& InitPtrToList(const Init* ptr) {
68 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
69 return val;
70}
71
72const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000073 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000074 return val;
75}
76
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000077// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000078// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000079void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
80 if (d->getNumArgs() < min_arguments)
81 throw "Property " + d->getOperator()->getAsString()
82 + " has too few arguments!";
83}
84
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000085// isDagEmpty - is this DAG marked with an empty marker?
86bool isDagEmpty (const DagInit* d) {
87 return d->getOperator()->getAsString() == "empty";
88}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000089
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000090//===----------------------------------------------------------------------===//
91/// Back-end specific code
92
93// A command-line option can have one of the following types:
94//
95// Switch - a simple switch w/o arguments, e.g. -O2
96//
97// Parameter - an option that takes one(and only one) argument, e.g. -o file,
98// --output=file
99//
100// ParameterList - same as Parameter, but more than one occurence
101// of the option is allowed, e.g. -lm -lpthread
102//
103// Prefix - argument is everything after the prefix,
104// e.g. -Wa,-foo,-bar, -DNAME=VALUE
105//
106// PrefixList - same as Prefix, but more than one option occurence is
107// allowed
108
109namespace OptionType {
110 enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList};
111}
112
113bool IsListOptionType (OptionType::OptionType t) {
114 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
115}
116
117// Code duplication here is necessary because one option can affect
118// several tools and those tools may have different actions associated
119// with this option. GlobalOptionDescriptions are used to generate
120// the option registration code, while ToolOptionDescriptions are used
121// to generate tool-specific code.
122
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000123/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000124struct OptionDescription {
125 OptionType::OptionType Type;
126 std::string Name;
127
128 OptionDescription(OptionType::OptionType t = OptionType::Switch,
129 const std::string& n = "")
130 : Type(t), Name(n)
131 {}
132
133 const char* GenTypeDeclaration() const {
134 switch (Type) {
135 case OptionType::PrefixList:
136 case OptionType::ParameterList:
137 return "cl::list<std::string>";
138 case OptionType::Switch:
139 return "cl::opt<bool>";
140 case OptionType::Parameter:
141 case OptionType::Prefix:
142 default:
143 return "cl::opt<std::string>";
144 }
145 }
146
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000147 // Escape commas and other symbols not allowed in the C++ variable
148 // names. Makes it possible to use options with names like "Wa,"
149 // (useful for prefix options).
150 std::string EscapeVariableName(const std::string& Var) const {
151 std::string ret;
152 for (unsigned i = 0; i != Var.size(); ++i) {
153 if (Var[i] == ',') {
154 ret += "_comma_";
155 }
156 else {
157 ret.push_back(Var[i]);
158 }
159 }
160 return ret;
161 }
162
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000163 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000164 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000165 switch (Type) {
166 case OptionType::Switch:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000167 return "AutoGeneratedSwitch" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000168 case OptionType::Prefix:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000169 return "AutoGeneratedPrefix" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000170 case OptionType::PrefixList:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000171 return "AutoGeneratedPrefixList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000172 case OptionType::Parameter:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000173 return "AutoGeneratedParameter" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000174 case OptionType::ParameterList:
175 default:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000176 return "AutoGeneratedParameterList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000177 }
178 }
179
180};
181
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000182// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000183
184namespace GlobalOptionDescriptionFlags {
185 enum GlobalOptionDescriptionFlags { Required = 0x1 };
186}
187
188struct GlobalOptionDescription : public OptionDescription {
189 std::string Help;
190 unsigned Flags;
191
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000192 // We need to provide a default constructor because
193 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000194 GlobalOptionDescription() : OptionDescription(), Flags(0)
195 {}
196
197 GlobalOptionDescription (OptionType::OptionType t, const std::string& n)
198 : OptionDescription(t, n), Help(DefaultHelpString), Flags(0)
199 {}
200
201 bool isRequired() const {
202 return Flags & GlobalOptionDescriptionFlags::Required;
203 }
204 void setRequired() {
205 Flags |= GlobalOptionDescriptionFlags::Required;
206 }
207
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000208 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000209 void Merge (const GlobalOptionDescription& other)
210 {
211 if (other.Type != Type)
212 throw "Conflicting definitions for the option " + Name + "!";
213
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000214 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000215 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000216 else if (other.Help != DefaultHelpString) {
217 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000218 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000219 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000220
221 Flags |= other.Flags;
222 }
223};
224
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000225/// GlobalOptionDescriptions - A GlobalOptionDescription array
226/// together with some flags affecting generation of option
227/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000228struct GlobalOptionDescriptions {
229 typedef StringMap<GlobalOptionDescription> container_type;
230 typedef container_type::const_iterator const_iterator;
231
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000232 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000233 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000234 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000235 bool HasSink;
236
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000237 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
238 const_iterator I = Descriptions.find(OptName);
239 if (I != Descriptions.end())
240 return I->second;
241 else
242 throw OptName + ": no such option!";
243 }
244
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000245 // Support for STL-style iteration
246 const_iterator begin() const { return Descriptions.begin(); }
247 const_iterator end() const { return Descriptions.end(); }
248};
249
250
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000251// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000252
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000253// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000254namespace ToolOptionDescriptionFlags {
255 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
256 Forward = 0x2, UnpackValues = 0x4};
257}
258namespace OptionPropertyType {
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000259 enum OptionPropertyType { AppendCmd, OutputSuffix };
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000260}
261
262typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
263OptionProperty;
264typedef SmallVector<OptionProperty, 4> OptionPropertyList;
265
266struct ToolOptionDescription : public OptionDescription {
267 unsigned Flags;
268 OptionPropertyList Props;
269
270 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000271 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000272
273 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
274 : OptionDescription(t, n)
275 {}
276
277 // Various boolean properties
278 bool isStopCompilation() const {
279 return Flags & ToolOptionDescriptionFlags::StopCompilation;
280 }
281 void setStopCompilation() {
282 Flags |= ToolOptionDescriptionFlags::StopCompilation;
283 }
284
285 bool isForward() const {
286 return Flags & ToolOptionDescriptionFlags::Forward;
287 }
288 void setForward() {
289 Flags |= ToolOptionDescriptionFlags::Forward;
290 }
291
292 bool isUnpackValues() const {
293 return Flags & ToolOptionDescriptionFlags::UnpackValues;
294 }
295 void setUnpackValues() {
296 Flags |= ToolOptionDescriptionFlags::UnpackValues;
297 }
298
299 void AddProperty (OptionPropertyType::OptionPropertyType t,
300 const std::string& val)
301 {
302 Props.push_back(std::make_pair(t, val));
303 }
304};
305
306typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
307
308// Tool information record
309
310namespace ToolFlags {
311 enum ToolFlags { Join = 0x1, Sink = 0x2 };
312}
313
314struct ToolProperties : public RefCountedBase<ToolProperties> {
315 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000316 Init* CmdLine;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000317 StrVector InLanguage;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000318 std::string OutLanguage;
319 std::string OutputSuffix;
320 unsigned Flags;
321 ToolOptionDescriptions OptDescs;
322
323 // Various boolean properties
324 void setSink() { Flags |= ToolFlags::Sink; }
325 bool isSink() const { return Flags & ToolFlags::Sink; }
326 void setJoin() { Flags |= ToolFlags::Join; }
327 bool isJoin() const { return Flags & ToolFlags::Join; }
328
329 // Default ctor here is needed because StringMap can only store
330 // DefaultConstructible objects
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000331 ToolProperties() : Flags(0) {}
332 ToolProperties (const std::string& n) : Name(n), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000333};
334
335
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000336/// ToolPropertiesList - A list of Tool information records
337/// IntrusiveRefCntPtrs are used here because StringMap has no copy
338/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000339typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
340
341
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000342/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000343/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000344class CollectProperties {
345private:
346
347 /// Implementation details
348
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000349 /// PropertyHandler - a function that extracts information
350 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000351 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000352
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000353 /// PropertyHandlerMap - A map from property names to property
354 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000355 typedef StringMap<PropertyHandler> PropertyHandlerMap;
356
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000357 /// OptionPropertyHandler - a function that extracts information
358 /// about a given option property from its DAG representation.
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000359 typedef void (CollectProperties::* OptionPropertyHandler)
360 (const DagInit*, GlobalOptionDescription &);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000361
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000362 /// OptionPropertyHandlerMap - A map from option property names to
363 /// option property handlers
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000364 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
365
366 // Static maps from strings to CollectProperties methods("handlers")
367 static PropertyHandlerMap propertyHandlers_;
368 static OptionPropertyHandlerMap optionPropertyHandlers_;
369 static bool staticMembersInitialized_;
370
371
372 /// This is where the information is stored
373
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000374 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000375 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000376 /// optDescs_ - OptionDescriptions table (used to register options
377 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000378 GlobalOptionDescriptions& optDescs_;
379
380public:
381
382 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
383 : toolProps_(p), optDescs_(d)
384 {
385 if (!staticMembersInitialized_) {
386 // Init tool property handlers
387 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
388 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
389 propertyHandlers_["join"] = &CollectProperties::onJoin;
390 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
391 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
392 propertyHandlers_["parameter_option"]
393 = &CollectProperties::onParameter;
394 propertyHandlers_["parameter_list_option"] =
395 &CollectProperties::onParameterList;
396 propertyHandlers_["prefix_option"] = &CollectProperties::onPrefix;
397 propertyHandlers_["prefix_list_option"] =
398 &CollectProperties::onPrefixList;
399 propertyHandlers_["sink"] = &CollectProperties::onSink;
400 propertyHandlers_["switch_option"] = &CollectProperties::onSwitch;
401
402 // Init option property handlers
403 optionPropertyHandlers_["append_cmd"] = &CollectProperties::onAppendCmd;
404 optionPropertyHandlers_["forward"] = &CollectProperties::onForward;
405 optionPropertyHandlers_["help"] = &CollectProperties::onHelp;
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000406 optionPropertyHandlers_["output_suffix"] =
407 &CollectProperties::onOutputSuffixOptionProp;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000408 optionPropertyHandlers_["required"] = &CollectProperties::onRequired;
409 optionPropertyHandlers_["stop_compilation"] =
410 &CollectProperties::onStopCompilation;
411 optionPropertyHandlers_["unpack_values"] =
412 &CollectProperties::onUnpackValues;
413
414 staticMembersInitialized_ = true;
415 }
416 }
417
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000418 /// operator() - Gets called for every tool property; Just forwards
419 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000420 void operator() (Init* i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000421 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000422 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000423 PropertyHandlerMap::iterator method
424 = propertyHandlers_.find(property_name);
425
426 if (method != propertyHandlers_.end()) {
427 PropertyHandler h = method->second;
428 (this->*h)(&d);
429 }
430 else {
431 throw "Unknown tool property: " + property_name + "!";
432 }
433 }
434
435private:
436
437 /// Property handlers --
438 /// Functions that extract information about tool properties from
439 /// DAG representation.
440
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000441 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000442 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000443 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000444 }
445
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000446 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000447 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000448 Init* arg = d->getArg(0);
449
450 // Find out the argument's type.
451 if (typeid(*arg) == typeid(StringInit)) {
452 // It's a string.
453 toolProps_.InLanguage.push_back(InitPtrToString(arg));
454 }
455 else {
456 // It's a list.
457 const ListInit& lst = InitPtrToList(arg);
458 StrVector& out = toolProps_.InLanguage;
459
460 // Copy strings to the output vector.
461 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
462 B != E; ++B) {
463 out.push_back(InitPtrToString(*B));
464 }
465
466 // Remove duplicates.
467 std::sort(out.begin(), out.end());
468 StrVector::iterator newE = std::unique(out.begin(), out.end());
469 out.erase(newE, out.end());
470 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000471 }
472
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000473 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000474 checkNumberOfArguments(d, 0);
475 toolProps_.setJoin();
476 }
477
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000478 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000479 checkNumberOfArguments(d, 1);
480 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
481 }
482
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000483 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000484 checkNumberOfArguments(d, 1);
485 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
486 }
487
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000488 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000489 checkNumberOfArguments(d, 0);
490 optDescs_.HasSink = true;
491 toolProps_.setSink();
492 }
493
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000494 void onSwitch (const DagInit* d) {
495 addOption(d, OptionType::Switch);
496 }
497
498 void onParameter (const DagInit* d) {
499 addOption(d, OptionType::Parameter);
500 }
501
502 void onParameterList (const DagInit* d) {
503 addOption(d, OptionType::ParameterList);
504 }
505
506 void onPrefix (const DagInit* d) {
507 addOption(d, OptionType::Prefix);
508 }
509
510 void onPrefixList (const DagInit* d) {
511 addOption(d, OptionType::PrefixList);
512 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000513
514 /// Option property handlers --
515 /// Methods that handle properties that are common for all types of
516 /// options (like append_cmd, stop_compilation)
517
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000518 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000519 checkNumberOfArguments(d, 1);
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000520 const std::string& cmd = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000521
522 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
523 }
524
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000525 void onOutputSuffixOptionProp (const DagInit* d, GlobalOptionDescription& o) {
526 checkNumberOfArguments(d, 1);
527 const std::string& suf = InitPtrToString(d->getArg(0));
528
529 if (toolProps_.OptDescs[o.Name].Type != OptionType::Switch)
530 throw "Option " + o.Name
531 + " can't have 'output_suffix' property since it isn't a switch!";
532
533 toolProps_.OptDescs[o.Name].AddProperty
534 (OptionPropertyType::OutputSuffix, suf);
535 }
536
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000537 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000538 checkNumberOfArguments(d, 0);
539 toolProps_.OptDescs[o.Name].setForward();
540 }
541
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000542 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000543 checkNumberOfArguments(d, 1);
544 const std::string& help_message = InitPtrToString(d->getArg(0));
545
546 o.Help = help_message;
547 }
548
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000549 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000550 checkNumberOfArguments(d, 0);
551 o.setRequired();
552 }
553
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000554 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000555 checkNumberOfArguments(d, 0);
556 if (o.Type != OptionType::Switch)
557 throw std::string("Only options of type Switch can stop compilation!");
558 toolProps_.OptDescs[o.Name].setStopCompilation();
559 }
560
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000561 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000562 checkNumberOfArguments(d, 0);
563 toolProps_.OptDescs[o.Name].setUnpackValues();
564 }
565
566 /// Helper functions
567
568 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000569 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000570 checkNumberOfArguments(d, 2);
571 const std::string& name = InitPtrToString(d->getArg(0));
572
573 GlobalOptionDescription o(t, name);
574 toolProps_.OptDescs[name].Type = t;
575 toolProps_.OptDescs[name].Name = name;
576 processOptionProperties(d, o);
577 insertDescription(o);
578 }
579
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000580 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
581 void insertDescription (const GlobalOptionDescription& o)
582 {
583 if (optDescs_.Descriptions.count(o.Name)) {
584 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
585 D.Merge(o);
586 }
587 else {
588 optDescs_.Descriptions[o.Name] = o;
589 }
590 }
591
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000592 /// processOptionProperties - Go through the list of option
593 /// properties and call a corresponding handler for each.
594 ///
595 /// Parameters:
596 /// name - option name
597 /// d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000598 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000599 // First argument is option name
600 checkNumberOfArguments(d, 2);
601
602 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000603 const DagInit& option_property
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000604 = InitPtrToDag(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000605 const std::string& option_property_name
606 = option_property.getOperator()->getAsString();
607 OptionPropertyHandlerMap::iterator method
608 = optionPropertyHandlers_.find(option_property_name);
609
610 if (method != optionPropertyHandlers_.end()) {
611 OptionPropertyHandler h = method->second;
612 (this->*h)(&option_property, o);
613 }
614 else {
615 throw "Unknown option property: " + option_property_name + "!";
616 }
617 }
618 }
619};
620
621// Static members of CollectProperties
622CollectProperties::PropertyHandlerMap
623CollectProperties::propertyHandlers_;
624
625CollectProperties::OptionPropertyHandlerMap
626CollectProperties::optionPropertyHandlers_;
627
628bool CollectProperties::staticMembersInitialized_ = false;
629
630
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000631/// CollectToolProperties - Gather information from the parsed
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000632/// TableGen data (basically a wrapper for the CollectProperties
633/// function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000634void CollectToolProperties (RecordVector::const_iterator B,
635 RecordVector::const_iterator E,
636 ToolPropertiesList& TPList,
637 GlobalOptionDescriptions& OptDescs)
638{
639 // Iterate over a properties list of every Tool definition
640 for (;B!=E;++B) {
641 RecordVector::value_type T = *B;
642 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000643
644 IntrusiveRefCntPtr<ToolProperties>
645 ToolProps(new ToolProperties(T->getName()));
646
647 std::for_each(PropList->begin(), PropList->end(),
648 CollectProperties(*ToolProps, OptDescs));
649 TPList.push_back(ToolProps);
650 }
651}
652
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000653/// EmitCaseTest1Arg - Helper function used by
654/// EmitCaseConstructHandler.
655bool EmitCaseTest1Arg(const std::string& TestName,
656 const DagInit& d,
657 const GlobalOptionDescriptions& OptDescs,
658 std::ostream& O) {
659 checkNumberOfArguments(&d, 1);
660 const std::string& OptName = InitPtrToString(d.getArg(0));
661 if (TestName == "switch_on") {
662 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
663 if (OptDesc.Type != OptionType::Switch)
664 throw OptName + ": incorrect option type!";
665 O << OptDesc.GenVariableName();
666 return true;
667 } else if (TestName == "input_languages_contain") {
668 O << "InLangs.count(\"" << OptName << "\") != 0";
669 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000670 } else if (TestName == "in_language") {
671 // Works only for cmd_line!
672 O << "GetLanguage(inFile) == \"" << OptName << '\"';
673 return true;
674 } else if (TestName == "not_empty") {
675 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
676 if (OptDesc.Type == OptionType::Switch)
677 throw OptName + ": incorrect option type!";
678 O << '!' << OptDesc.GenVariableName() << ".empty()";
679 return true;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000680 }
681
682 return false;
683}
684
685/// EmitCaseTest2Args - Helper function used by
686/// EmitCaseConstructHandler.
687bool EmitCaseTest2Args(const std::string& TestName,
688 const DagInit& d,
689 const char* IndentLevel,
690 const GlobalOptionDescriptions& OptDescs,
691 std::ostream& O) {
692 checkNumberOfArguments(&d, 2);
693 const std::string& OptName = InitPtrToString(d.getArg(0));
694 const std::string& OptArg = InitPtrToString(d.getArg(1));
695 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
696
697 if (TestName == "parameter_equals") {
698 if (OptDesc.Type != OptionType::Parameter
699 && OptDesc.Type != OptionType::Prefix)
700 throw OptName + ": incorrect option type!";
701 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
702 return true;
703 }
704 else if (TestName == "element_in_list") {
705 if (OptDesc.Type != OptionType::ParameterList
706 && OptDesc.Type != OptionType::PrefixList)
707 throw OptName + ": incorrect option type!";
708 const std::string& VarName = OptDesc.GenVariableName();
709 O << "std::find(" << VarName << ".begin(),\n"
710 << IndentLevel << Indent1 << VarName << ".end(), \""
711 << OptArg << "\") != " << VarName << ".end()";
712 return true;
713 }
714
715 return false;
716}
717
718// Forward declaration.
719// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
720void EmitCaseTest(const DagInit& d, const char* IndentLevel,
721 const GlobalOptionDescriptions& OptDescs,
722 std::ostream& O);
723
724/// EmitLogicalOperationTest - Helper function used by
725/// EmitCaseConstructHandler.
726void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
727 const char* IndentLevel,
728 const GlobalOptionDescriptions& OptDescs,
729 std::ostream& O) {
730 O << '(';
731 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000732 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000733 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
734 if (j != NumArgs - 1)
735 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
736 else
737 O << ')';
738 }
739}
740
741/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
742void EmitCaseTest(const DagInit& d, const char* IndentLevel,
743 const GlobalOptionDescriptions& OptDescs,
744 std::ostream& O) {
745 const std::string& TestName = d.getOperator()->getAsString();
746
747 if (TestName == "and")
748 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
749 else if (TestName == "or")
750 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
751 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
752 return;
753 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
754 return;
755 else
756 throw TestName + ": unknown edge property!";
757}
758
759// Emit code that handles the 'case' construct.
760// Takes a function object that should emit code for every case clause.
761// Callback's type is
762// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
763template <typename F>
764void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +0000765 const F& Callback,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000766 const GlobalOptionDescriptions& OptDescs,
767 std::ostream& O) {
768 assert(d->getOperator()->getAsString() == "case");
769
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000770 unsigned numArgs = d->getNumArgs();
771 if (d->getNumArgs() < 2)
772 throw "There should be at least one clause in the 'case' expression:\n"
773 + d->getAsString();
774
775 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000776 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000777
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000778 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000779 if (Test.getOperator()->getAsString() == "default") {
780 if (i+2 != numArgs)
781 throw std::string("The 'default' clause should be the last in the"
782 "'case' construct!");
783 O << IndentLevel << "else {\n";
784 }
785 else {
786 O << IndentLevel << "if (";
787 EmitCaseTest(Test, IndentLevel, OptDescs, O);
788 O << ") {\n";
789 }
790
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000791 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000792 ++i;
793 if (i == numArgs)
794 throw "Case construct handler: no corresponding action "
795 "found for the test " + Test.getAsString() + '!';
796
797 Callback(d->getArg(i), IndentLevel, O);
798 O << IndentLevel << "}\n";
799 }
800}
801
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000802/// EmitForwardOptionPropertyHandlingCode - Helper function used to
803/// implement EmitOptionPropertyHandlingCode(). Emits code for
804/// handling the (forward) option property.
805void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
806 std::ostream& O) {
807 switch (D.Type) {
808 case OptionType::Switch:
809 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
810 break;
811 case OptionType::Parameter:
812 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
813 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
814 break;
815 case OptionType::Prefix:
816 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
817 << D.GenVariableName() << ");\n";
818 break;
819 case OptionType::PrefixList:
820 O << Indent3 << "for (" << D.GenTypeDeclaration()
821 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
822 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
823 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
824 << "*B);\n";
825 break;
826 case OptionType::ParameterList:
827 O << Indent3 << "for (" << D.GenTypeDeclaration()
828 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
829 << Indent3 << "E = " << D.GenVariableName()
830 << ".end() ; B != E; ++B) {\n"
831 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
832 << Indent4 << "vec.push_back(*B);\n"
833 << Indent3 << "}\n";
834 break;
835 }
836}
837
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000838// ToolOptionHasInterestingProperties - A helper function used by
839// EmitOptionPropertyHandlingCode() that tells us whether we should
840// emit any property handling code at all.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000841bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000842 bool ret = false;
843 for (OptionPropertyList::const_iterator B = D.Props.begin(),
844 E = D.Props.end(); B != E; ++B) {
845 const OptionProperty& OptProp = *B;
846 if (OptProp.first == OptionPropertyType::AppendCmd)
847 ret = true;
848 }
849 if (D.isForward() || D.isUnpackValues())
850 ret = true;
851 return ret;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000852}
853
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000854/// EmitOptionPropertyHandlingCode - Helper function used by
855/// EmitGenerateActionMethod(). Emits code that handles option
856/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000857void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000858 std::ostream& O)
859{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000860 if (!ToolOptionHasInterestingProperties(D))
861 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000862 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000863 O << Indent2 << "if (";
864 if (D.Type == OptionType::Switch)
865 O << D.GenVariableName();
866 else
867 O << '!' << D.GenVariableName() << ".empty()";
868
869 O <<") {\n";
870
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000871 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000872 for (OptionPropertyList::const_iterator B = D.Props.begin(),
873 E = D.Props.end(); B!=E; ++B) {
874 const OptionProperty& val = *B;
875
876 switch (val.first) {
877 // (append_cmd cmd) property
878 case OptionPropertyType::AppendCmd:
879 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
880 break;
881 // Other properties with argument
882 default:
883 break;
884 }
885 }
886
887 // Handle flags
888
889 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000890 if (D.isForward())
891 EmitForwardOptionPropertyHandlingCode(D, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000892
893 // (unpack_values) property
894 if (D.isUnpackValues()) {
895 if (IsListOptionType(D.Type)) {
896 O << Indent3 << "for (" << D.GenTypeDeclaration()
897 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
898 << Indent3 << "E = " << D.GenVariableName()
899 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000900 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000901 }
902 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000903 O << Indent3 << "llvm::SplitString("
904 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000905 }
906 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000907 throw std::string("Switches can't have unpack_values property!");
908 }
909 }
910
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000911 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000912 O << Indent2 << "}\n";
913}
914
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000915/// SubstituteSpecialCommands - Perform string substitution for $CALL
916/// and $ENV. Helper function used by EmitCmdLineVecFill().
917std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000918 size_t cparen = cmd.find(")");
919 std::string ret;
920
921 if (cmd.find("$CALL(") == 0) {
922 if (cmd.size() == 6)
923 throw std::string("$CALL invocation: empty argument list!");
924
925 ret += "hooks::";
926 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
927 ret += "()";
928 }
929 else if (cmd.find("$ENV(") == 0) {
930 if (cmd.size() == 5)
931 throw std::string("$ENV invocation: empty argument list!");
932
933 ret += "std::getenv(\"";
934 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
935 ret += "\")";
936 }
937 else {
938 throw "Unknown special command: " + cmd;
939 }
940
941 if (cmd.begin() + cparen + 1 != cmd.end()) {
942 ret += " + std::string(\"";
943 ret += (cmd.c_str() + cparen + 1);
944 ret += "\")";
945 }
946
947 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000948}
949
950/// EmitCmdLineVecFill - Emit code that fills in the command line
951/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000952void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
953 bool Version, const char* IndentLevel,
954 std::ostream& O) {
955 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000956 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000957 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000958 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000959
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000960 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000961 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000962 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000963 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000964 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000965 if (cmd.at(0) == '$') {
966 if (cmd == "$INFILE") {
967 if (Version)
968 O << "for (PathVector::const_iterator B = inFiles.begin()"
969 << ", E = inFiles.end();\n"
970 << IndentLevel << "B != E; ++B)\n"
971 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
972 else
973 O << "vec.push_back(inFile.toString());\n";
974 }
975 else if (cmd == "$OUTFILE") {
976 O << "vec.push_back(outFile.toString());\n";
977 }
978 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000979 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
980 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000981 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000982 }
983 else {
984 O << "vec.push_back(\"" << cmd << "\");\n";
985 }
986 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000987 O << IndentLevel << "ret = Action("
988 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
989 : "\"" + StrVec[0] + "\"")
990 << ", vec);\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000991}
992
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000993/// EmitCmdLineVecFillCallback - A function object wrapper around
994/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
995/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000996class EmitCmdLineVecFillCallback {
997 bool Version;
998 const std::string& ToolName;
999 public:
1000 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
1001 : Version(Ver), ToolName(TN) {}
1002
1003 void operator()(const Init* Statement, const char* IndentLevel,
1004 std::ostream& O) const
1005 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001006 EmitCmdLineVecFill(Statement, ToolName, Version,
1007 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001008 }
1009};
1010
1011// EmitGenerateActionMethod - Emit one of two versions of the
1012// Tool::GenerateAction() method.
1013void EmitGenerateActionMethod (const ToolProperties& P,
1014 const GlobalOptionDescriptions& OptDescs,
1015 bool Version, std::ostream& O) {
1016 if (Version)
1017 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1018 else
1019 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1020
1021 O << Indent2 << "const sys::Path& outFile,\n"
1022 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
1023 << Indent1 << "{\n"
1024 << Indent2 << "Action ret;\n"
1025 << Indent2 << "std::vector<std::string> vec;\n";
1026
1027 // cmd_line is either a string or a 'case' construct.
1028 if (typeid(*P.CmdLine) == typeid(StringInit))
1029 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
1030 else
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001031 EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001032 EmitCmdLineVecFillCallback(Version, P.Name),
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +00001033 OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001034
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001035 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001036 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1037 E = P.OptDescs.end(); B != E; ++B) {
1038 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001039 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001040 }
1041
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001042 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001043 if (P.isSink()) {
1044 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1045 << Indent3 << "vec.insert(vec.end(), "
1046 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1047 << Indent2 << "}\n";
1048 }
1049
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001050 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001051 << Indent1 << "}\n\n";
1052}
1053
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001054/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1055/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001056void EmitGenerateActionMethods (const ToolProperties& P,
1057 const GlobalOptionDescriptions& OptDescs,
1058 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001059 if (!P.isJoin())
1060 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001061 << Indent2 << "const llvm::sys::Path& outFile,\n"
1062 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001063 << Indent1 << "{\n"
1064 << Indent2 << "throw std::runtime_error(\"" << P.Name
1065 << " is not a Join tool!\");\n"
1066 << Indent1 << "}\n\n";
1067 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001068 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001069
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001070 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001071}
1072
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001073/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
1074/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001075void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
1076 O << Indent1 << "bool IsLast() const {\n"
1077 << Indent2 << "bool last = false;\n";
1078
1079 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1080 E = P.OptDescs.end(); B != E; ++B) {
1081 const ToolOptionDescription& val = B->second;
1082
1083 if (val.isStopCompilation())
1084 O << Indent2
1085 << "if (" << val.GenVariableName()
1086 << ")\n" << Indent3 << "last = true;\n";
1087 }
1088
1089 O << Indent2 << "return last;\n"
1090 << Indent1 << "}\n\n";
1091}
1092
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001093/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1094/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001095void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001096 O << Indent1 << "StrVector InputLanguages() const {\n"
1097 << Indent2 << "StrVector ret;\n";
1098
1099 for (StrVector::const_iterator B = P.InLanguage.begin(),
1100 E = P.InLanguage.end(); B != E; ++B) {
1101 O << Indent2 << "ret.push_back(\"" << *B << "\");\n";
1102 }
1103
1104 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001105 << Indent1 << "}\n\n";
1106
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001107 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001108 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1109 << Indent1 << "}\n\n";
1110}
1111
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001112/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1113/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001114void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001115 O << Indent1 << "const char* OutputSuffix() const {\n"
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +00001116 << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
1117
1118 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1119 E = P.OptDescs.end(); B != E; ++B) {
1120 const ToolOptionDescription& OptDesc = B->second;
1121 for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
1122 E = OptDesc.Props.end(); B != E; ++B) {
1123 const OptionProperty& OptProp = *B;
1124 if (OptProp.first == OptionPropertyType::OutputSuffix) {
1125 O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
1126 << Indent3 << "ret = \"" << OptProp.second << "\";\n";
1127 }
1128 }
1129 }
1130
1131 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001132 << Indent1 << "}\n\n";
1133}
1134
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001135/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001136void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001137 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001138 << Indent2 << "return \"" << P.Name << "\";\n"
1139 << Indent1 << "}\n\n";
1140}
1141
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001142/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1143/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001144void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1145 O << Indent1 << "bool IsJoin() const {\n";
1146 if (P.isJoin())
1147 O << Indent2 << "return true;\n";
1148 else
1149 O << Indent2 << "return false;\n";
1150 O << Indent1 << "}\n\n";
1151}
1152
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001153/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001154void EmitToolClassDefinition (const ToolProperties& P,
1155 const GlobalOptionDescriptions& OptDescs,
1156 std::ostream& O) {
1157 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001158 return;
1159
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001160 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001161 O << "class " << P.Name << " : public ";
1162 if (P.isJoin())
1163 O << "JoinTool";
1164 else
1165 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +00001166 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001167
1168 EmitNameMethod(P, O);
1169 EmitInOutLanguageMethods(P, O);
1170 EmitOutputSuffixMethod(P, O);
1171 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001172 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001173 EmitIsLastMethod(P, O);
1174
1175 // Close class definition
1176 O << "};\n\n";
1177}
1178
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001179/// EmitOptionDescriptions - Iterate over a list of option
1180/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001181void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1182 std::ostream& O)
1183{
1184 // Emit static cl::Option variables
1185 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1186 E = descs.end(); B!=E; ++B) {
1187 const GlobalOptionDescription& val = B->second;
1188
1189 O << val.GenTypeDeclaration() << ' '
1190 << val.GenVariableName()
1191 << "(\"" << val.Name << '\"';
1192
1193 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1194 O << ", cl::Prefix";
1195
1196 if (val.isRequired()) {
1197 switch (val.Type) {
1198 case OptionType::PrefixList:
1199 case OptionType::ParameterList:
1200 O << ", cl::OneOrMore";
1201 break;
1202 default:
1203 O << ", cl::Required";
1204 }
1205 }
1206
1207 O << ", cl::desc(\"" << val.Help << "\"));\n";
1208 }
1209
1210 if (descs.HasSink)
1211 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1212
1213 O << '\n';
1214}
1215
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001216/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001217void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1218{
1219 // Get the relevant field out of RecordKeeper
1220 Record* LangMapRecord = Records.getDef("LanguageMap");
1221 if (!LangMapRecord)
1222 throw std::string("Language map definition not found!");
1223
1224 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1225 if (!LangsToSuffixesList)
1226 throw std::string("Error in the language map definition!");
1227
1228 // Generate code
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001229 O << "void llvmc::PopulateLanguageMap() {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001230
1231 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1232 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1233
1234 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1235 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1236
1237 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001238 O << Indent1 << "GlobalLanguageMap[\""
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001239 << InitPtrToString(Suffixes->getElement(i))
1240 << "\"] = \"" << Lang << "\";\n";
1241 }
1242
1243 O << "}\n\n";
1244}
1245
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001246/// FillInToolToLang - Fills in two tables that map tool names to
1247/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001248void FillInToolToLang (const ToolPropertiesList& TPList,
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001249 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001250 StringMap<std::string>& ToolToOutLang) {
1251 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1252 B != E; ++B) {
1253 const ToolProperties& P = *(*B);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001254 for (StrVector::const_iterator B = P.InLanguage.begin(),
1255 E = P.InLanguage.end(); B != E; ++B)
1256 ToolToInLang[P.Name].insert(*B);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001257 ToolToOutLang[P.Name] = P.OutLanguage;
1258 }
1259}
1260
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001261/// TypecheckGraph - Check that names for output and input languages
1262/// on all edges do match.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001263// TOFIX: It would be nice if this function also checked for cycles
1264// and multiple default edges in the graph (better error
1265// reporting). Unfortunately, it is awkward to do right now because
1266// our intermediate representation is not sufficiently
1267// sofisticated. Algorithms like these should be run on a real graph
1268// instead of AST.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001269void TypecheckGraph (Record* CompilationGraph,
1270 const ToolPropertiesList& TPList) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001271 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001272 StringMap<std::string> ToolToOutLang;
1273
1274 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1275 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001276 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1277 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001278
1279 for (unsigned i = 0; i < edges->size(); ++i) {
1280 Record* Edge = edges->getElementAsRecord(i);
1281 Record* A = Edge->getValueAsDef("a");
1282 Record* B = Edge->getValueAsDef("b");
1283 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001284 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001285 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001286 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001287 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001288 throw B->getName() + ": no such tool!";
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001289 if (A->getName() != "root" && IB->second.count(IA->second) == 0)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001290 throw "Edge " + A->getName() + "->" + B->getName()
1291 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001292 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001293 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001294 }
1295}
1296
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001297/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1298/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001299void IncDecWeight (const Init* i, const char* IndentLevel,
1300 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001301 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001302 const std::string& OpName = d.getOperator()->getAsString();
1303
1304 if (OpName == "inc_weight")
1305 O << IndentLevel << Indent1 << "ret += ";
1306 else if (OpName == "dec_weight")
1307 O << IndentLevel << Indent1 << "ret -= ";
1308 else
1309 throw "Unknown operator in edge properties list: " + OpName + '!';
1310
1311 if (d.getNumArgs() > 0)
1312 O << InitPtrToInt(d.getArg(0)) << ";\n";
1313 else
1314 O << "2;\n";
1315
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001316}
1317
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001318/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001319void EmitEdgeClass (unsigned N, const std::string& Target,
1320 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1321 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001322
1323 // Class constructor.
1324 O << "class Edge" << N << ": public Edge {\n"
1325 << "public:\n"
1326 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1327 << "\") {}\n\n"
1328
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001329 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001330 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001331 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001332
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001333 // Handle the 'case' construct.
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +00001334 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001335
1336 O << Indent2 << "return ret;\n"
1337 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001338}
1339
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001340/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001341void EmitEdgeClasses (Record* CompilationGraph,
1342 const GlobalOptionDescriptions& OptDescs,
1343 std::ostream& O) {
1344 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1345
1346 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001347 Record* Edge = edges->getElementAsRecord(i);
1348 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001349 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001350
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001351 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001352 continue;
1353
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001354 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001355 }
1356}
1357
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001358/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1359/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001360void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001361 std::ostream& O)
1362{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001363 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001364
1365 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001366 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001367 << Indent1 << "PopulateLanguageMap();\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001368
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001369 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001370
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001371 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1372 if (Tools.empty())
1373 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001374
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001375 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1376 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001377 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001378 O << Indent1 << "G.insertNode(new "
1379 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001380 }
1381
1382 O << '\n';
1383
1384 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001385 for (unsigned i = 0; i < edges->size(); ++i) {
1386 Record* Edge = edges->getElementAsRecord(i);
1387 Record* A = Edge->getValueAsDef("a");
1388 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001389 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001390
1391 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1392
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001393 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001394 O << "new SimpleEdge(\"" << B->getName() << "\")";
1395 else
1396 O << "new Edge" << i << "()";
1397
1398 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001399 }
1400
1401 O << "}\n\n";
1402}
1403
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001404/// ExtractHookNames - Extract the hook names from all instances of
1405/// $CALL(HookName) in the provided command line string. Helper
1406/// function used by FillInHookNames().
1407void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1408 StrVector cmds;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001409 llvm::SplitString(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001410 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1411 B != E; ++B) {
1412 const std::string& cmd = *B;
1413 if (cmd.find("$CALL(") == 0) {
1414 if (cmd.size() == 6)
1415 throw std::string("$CALL invocation: empty argument list!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001416 HookNames.push_back(std::string(cmd.begin() + 6,
1417 cmd.begin() + cmd.find(")")));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001418 }
1419 }
1420}
1421
1422/// FillInHookNames - Actually extract the hook names from all command
1423/// line strings. Helper function used by EmitHookDeclarations().
1424void FillInHookNames(const ToolPropertiesList& TPList,
1425 StrVector& HookNames) {
1426 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1427 E = TPList.end(); B != E; ++B) {
1428 const ToolProperties& P = *(*B);
1429 if (!P.CmdLine)
1430 continue;
1431 if (typeid(*P.CmdLine) == typeid(StringInit)) {
1432 // This is a string.
1433 ExtractHookNames(P.CmdLine, HookNames);
1434 }
1435 else {
1436 // This is a 'case' construct.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001437 const DagInit& d = InitPtrToDag(P.CmdLine);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001438 bool even = false;
1439 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1440 B != E; ++B) {
1441 if (even)
1442 ExtractHookNames(*B, HookNames);
1443 even = !even;
1444 }
1445 }
1446 }
1447}
1448
1449/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1450/// property records and emit hook function declaration for each
1451/// instance of $CALL(HookName).
1452void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1453 std::ostream& O) {
1454 StrVector HookNames;
1455 FillInHookNames(ToolProps, HookNames);
1456 if (HookNames.empty())
1457 return;
1458 std::sort(HookNames.begin(), HookNames.end());
1459 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1460
1461 O << "namespace hooks {\n";
1462 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1463 O << Indent1 << "std::string " << *B << "();\n";
1464
1465 O << "}\n\n";
1466}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001467
1468// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001469}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001470
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001471/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001472void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001473
1474 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001475 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001476
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001477 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001478 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1479 if (Tools.empty())
1480 throw std::string("No tool definitions found!");
1481
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001482 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001483 ToolPropertiesList tool_props;
1484 GlobalOptionDescriptions opt_descs;
1485 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1486
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001487 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001488 EmitOptionDescriptions(opt_descs, O);
1489
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001490 // Emit hook declarations.
1491 EmitHookDeclarations(tool_props, O);
1492
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001493 // Emit PopulateLanguageMap() function
1494 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001495 EmitPopulateLanguageMap(Records, O);
1496
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001497 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001498 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1499 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001500 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001501
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001502 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1503 if (!CompilationGraphRecord)
1504 throw std::string("Compilation graph description not found!");
1505
1506 // Typecheck the compilation graph.
1507 TypecheckGraph(CompilationGraphRecord, tool_props);
1508
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001509 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001510 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001511
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001512 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001513 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001514
1515 // EOF
1516}