blob: d160412cfaa718e916a351e1a74c7fd771c46ae4 [file] [log] [blame]
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config --------------===//
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"
21#include "llvm/Support/Streams.h"
22
23#include <algorithm>
24#include <cassert>
25#include <functional>
26#include <string>
27
28using namespace llvm;
29
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000030namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000031
32//===----------------------------------------------------------------------===//
33/// Typedefs
34
35typedef std::vector<Record*> RecordVector;
36typedef std::vector<std::string> StrVector;
37
38//===----------------------------------------------------------------------===//
39/// Constants
40
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000041// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000042const char * Indent1 = " ";
43const char * Indent2 = " ";
44const char * Indent3 = " ";
45const char * Indent4 = " ";
46
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000047// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000048const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
49
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000050// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000051const char * SinkOptionName = "AutoGeneratedSinkOption";
52
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000056const std::string& InitPtrToString(const Init* ptr) {
57 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000058 return val.getValue();
59}
60
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000061int InitPtrToInt(const Init* ptr) {
62 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000063 return val.getValue();
64}
65
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000066const DagInit& InitPtrToDagInitRef(const Init* ptr) {
67 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000068 return val;
69}
70
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000071// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000072// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000073void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
74 if (d->getNumArgs() < min_arguments)
75 throw "Property " + d->getOperator()->getAsString()
76 + " has too few arguments!";
77}
78
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000079// isDagEmpty - is this DAG marked with an empty marker?
80bool isDagEmpty (const DagInit* d) {
81 return d->getOperator()->getAsString() == "empty";
82}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000083
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000084//===----------------------------------------------------------------------===//
85/// Back-end specific code
86
87// A command-line option can have one of the following types:
88//
89// Switch - a simple switch w/o arguments, e.g. -O2
90//
91// Parameter - an option that takes one(and only one) argument, e.g. -o file,
92// --output=file
93//
94// ParameterList - same as Parameter, but more than one occurence
95// of the option is allowed, e.g. -lm -lpthread
96//
97// Prefix - argument is everything after the prefix,
98// e.g. -Wa,-foo,-bar, -DNAME=VALUE
99//
100// PrefixList - same as Prefix, but more than one option occurence is
101// allowed
102
103namespace OptionType {
104 enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList};
105}
106
107bool IsListOptionType (OptionType::OptionType t) {
108 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
109}
110
111// Code duplication here is necessary because one option can affect
112// several tools and those tools may have different actions associated
113// with this option. GlobalOptionDescriptions are used to generate
114// the option registration code, while ToolOptionDescriptions are used
115// to generate tool-specific code.
116
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000117/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000118struct OptionDescription {
119 OptionType::OptionType Type;
120 std::string Name;
121
122 OptionDescription(OptionType::OptionType t = OptionType::Switch,
123 const std::string& n = "")
124 : Type(t), Name(n)
125 {}
126
127 const char* GenTypeDeclaration() const {
128 switch (Type) {
129 case OptionType::PrefixList:
130 case OptionType::ParameterList:
131 return "cl::list<std::string>";
132 case OptionType::Switch:
133 return "cl::opt<bool>";
134 case OptionType::Parameter:
135 case OptionType::Prefix:
136 default:
137 return "cl::opt<std::string>";
138 }
139 }
140
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000141 // Escape commas and other symbols not allowed in the C++ variable
142 // names. Makes it possible to use options with names like "Wa,"
143 // (useful for prefix options).
144 std::string EscapeVariableName(const std::string& Var) const {
145 std::string ret;
146 for (unsigned i = 0; i != Var.size(); ++i) {
147 if (Var[i] == ',') {
148 ret += "_comma_";
149 }
150 else {
151 ret.push_back(Var[i]);
152 }
153 }
154 return ret;
155 }
156
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000157 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000158 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000159 switch (Type) {
160 case OptionType::Switch:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000161 return "AutoGeneratedSwitch" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000162 case OptionType::Prefix:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000163 return "AutoGeneratedPrefix" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000164 case OptionType::PrefixList:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000165 return "AutoGeneratedPrefixList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000166 case OptionType::Parameter:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000167 return "AutoGeneratedParameter" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000168 case OptionType::ParameterList:
169 default:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000170 return "AutoGeneratedParameterList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000171 }
172 }
173
174};
175
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000176// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000177
178namespace GlobalOptionDescriptionFlags {
179 enum GlobalOptionDescriptionFlags { Required = 0x1 };
180}
181
182struct GlobalOptionDescription : public OptionDescription {
183 std::string Help;
184 unsigned Flags;
185
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000186 // We need to provide a default constructor because
187 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000188 GlobalOptionDescription() : OptionDescription(), Flags(0)
189 {}
190
191 GlobalOptionDescription (OptionType::OptionType t, const std::string& n)
192 : OptionDescription(t, n), Help(DefaultHelpString), Flags(0)
193 {}
194
195 bool isRequired() const {
196 return Flags & GlobalOptionDescriptionFlags::Required;
197 }
198 void setRequired() {
199 Flags |= GlobalOptionDescriptionFlags::Required;
200 }
201
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000202 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000203 void Merge (const GlobalOptionDescription& other)
204 {
205 if (other.Type != Type)
206 throw "Conflicting definitions for the option " + Name + "!";
207
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000208 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000209 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000210 else if (other.Help != DefaultHelpString) {
211 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000212 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000213 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000214
215 Flags |= other.Flags;
216 }
217};
218
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000219/// GlobalOptionDescriptions - A GlobalOptionDescription array
220/// together with some flags affecting generation of option
221/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000222struct GlobalOptionDescriptions {
223 typedef StringMap<GlobalOptionDescription> container_type;
224 typedef container_type::const_iterator const_iterator;
225
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000226 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000227 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000228 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000229 bool HasSink;
230
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000231 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
232 const_iterator I = Descriptions.find(OptName);
233 if (I != Descriptions.end())
234 return I->second;
235 else
236 throw OptName + ": no such option!";
237 }
238
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000239 // Support for STL-style iteration
240 const_iterator begin() const { return Descriptions.begin(); }
241 const_iterator end() const { return Descriptions.end(); }
242};
243
244
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000245// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000246
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000247// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000248namespace ToolOptionDescriptionFlags {
249 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
250 Forward = 0x2, UnpackValues = 0x4};
251}
252namespace OptionPropertyType {
253 enum OptionPropertyType { AppendCmd };
254}
255
256typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
257OptionProperty;
258typedef SmallVector<OptionProperty, 4> OptionPropertyList;
259
260struct ToolOptionDescription : public OptionDescription {
261 unsigned Flags;
262 OptionPropertyList Props;
263
264 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000265 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000266
267 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
268 : OptionDescription(t, n)
269 {}
270
271 // Various boolean properties
272 bool isStopCompilation() const {
273 return Flags & ToolOptionDescriptionFlags::StopCompilation;
274 }
275 void setStopCompilation() {
276 Flags |= ToolOptionDescriptionFlags::StopCompilation;
277 }
278
279 bool isForward() const {
280 return Flags & ToolOptionDescriptionFlags::Forward;
281 }
282 void setForward() {
283 Flags |= ToolOptionDescriptionFlags::Forward;
284 }
285
286 bool isUnpackValues() const {
287 return Flags & ToolOptionDescriptionFlags::UnpackValues;
288 }
289 void setUnpackValues() {
290 Flags |= ToolOptionDescriptionFlags::UnpackValues;
291 }
292
293 void AddProperty (OptionPropertyType::OptionPropertyType t,
294 const std::string& val)
295 {
296 Props.push_back(std::make_pair(t, val));
297 }
298};
299
300typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
301
302// Tool information record
303
304namespace ToolFlags {
305 enum ToolFlags { Join = 0x1, Sink = 0x2 };
306}
307
308struct ToolProperties : public RefCountedBase<ToolProperties> {
309 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000310 Init* CmdLine;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000311 std::string InLanguage;
312 std::string OutLanguage;
313 std::string OutputSuffix;
314 unsigned Flags;
315 ToolOptionDescriptions OptDescs;
316
317 // Various boolean properties
318 void setSink() { Flags |= ToolFlags::Sink; }
319 bool isSink() const { return Flags & ToolFlags::Sink; }
320 void setJoin() { Flags |= ToolFlags::Join; }
321 bool isJoin() const { return Flags & ToolFlags::Join; }
322
323 // Default ctor here is needed because StringMap can only store
324 // DefaultConstructible objects
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000325 ToolProperties() : Flags(0) {}
326 ToolProperties (const std::string& n) : Name(n), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000327};
328
329
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000330/// ToolPropertiesList - A list of Tool information records
331/// IntrusiveRefCntPtrs are used here because StringMap has no copy
332/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000333typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
334
335
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000336/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000337/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000338class CollectProperties {
339private:
340
341 /// Implementation details
342
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000343 /// PropertyHandler - a function that extracts information
344 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000345 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000346
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000347 /// PropertyHandlerMap - A map from property names to property
348 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000349 typedef StringMap<PropertyHandler> PropertyHandlerMap;
350
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000351 /// OptionPropertyHandler - a function that extracts information
352 /// about a given option property from its DAG representation.
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000353 typedef void (CollectProperties::* OptionPropertyHandler)
354 (const DagInit*, GlobalOptionDescription &);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000355
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000356 /// OptionPropertyHandlerMap - A map from option property names to
357 /// option property handlers
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000358 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
359
360 // Static maps from strings to CollectProperties methods("handlers")
361 static PropertyHandlerMap propertyHandlers_;
362 static OptionPropertyHandlerMap optionPropertyHandlers_;
363 static bool staticMembersInitialized_;
364
365
366 /// This is where the information is stored
367
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000368 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000369 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000370 /// optDescs_ - OptionDescriptions table (used to register options
371 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000372 GlobalOptionDescriptions& optDescs_;
373
374public:
375
376 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
377 : toolProps_(p), optDescs_(d)
378 {
379 if (!staticMembersInitialized_) {
380 // Init tool property handlers
381 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
382 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
383 propertyHandlers_["join"] = &CollectProperties::onJoin;
384 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
385 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
386 propertyHandlers_["parameter_option"]
387 = &CollectProperties::onParameter;
388 propertyHandlers_["parameter_list_option"] =
389 &CollectProperties::onParameterList;
390 propertyHandlers_["prefix_option"] = &CollectProperties::onPrefix;
391 propertyHandlers_["prefix_list_option"] =
392 &CollectProperties::onPrefixList;
393 propertyHandlers_["sink"] = &CollectProperties::onSink;
394 propertyHandlers_["switch_option"] = &CollectProperties::onSwitch;
395
396 // Init option property handlers
397 optionPropertyHandlers_["append_cmd"] = &CollectProperties::onAppendCmd;
398 optionPropertyHandlers_["forward"] = &CollectProperties::onForward;
399 optionPropertyHandlers_["help"] = &CollectProperties::onHelp;
400 optionPropertyHandlers_["required"] = &CollectProperties::onRequired;
401 optionPropertyHandlers_["stop_compilation"] =
402 &CollectProperties::onStopCompilation;
403 optionPropertyHandlers_["unpack_values"] =
404 &CollectProperties::onUnpackValues;
405
406 staticMembersInitialized_ = true;
407 }
408 }
409
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000410 /// operator() - Gets called for every tool property; Just forwards
411 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000412 void operator() (Init* i) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000413 const DagInit& d = InitPtrToDagInitRef(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000414 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000415 PropertyHandlerMap::iterator method
416 = propertyHandlers_.find(property_name);
417
418 if (method != propertyHandlers_.end()) {
419 PropertyHandler h = method->second;
420 (this->*h)(&d);
421 }
422 else {
423 throw "Unknown tool property: " + property_name + "!";
424 }
425 }
426
427private:
428
429 /// Property handlers --
430 /// Functions that extract information about tool properties from
431 /// DAG representation.
432
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000433 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000434 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000435 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000436 }
437
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000438 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000439 checkNumberOfArguments(d, 1);
440 toolProps_.InLanguage = InitPtrToString(d->getArg(0));
441 }
442
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000443 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000444 checkNumberOfArguments(d, 0);
445 toolProps_.setJoin();
446 }
447
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000448 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000449 checkNumberOfArguments(d, 1);
450 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
451 }
452
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000453 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000454 checkNumberOfArguments(d, 1);
455 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
456 }
457
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000458 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000459 checkNumberOfArguments(d, 0);
460 optDescs_.HasSink = true;
461 toolProps_.setSink();
462 }
463
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000464 void onSwitch (const DagInit* d) {
465 addOption(d, OptionType::Switch);
466 }
467
468 void onParameter (const DagInit* d) {
469 addOption(d, OptionType::Parameter);
470 }
471
472 void onParameterList (const DagInit* d) {
473 addOption(d, OptionType::ParameterList);
474 }
475
476 void onPrefix (const DagInit* d) {
477 addOption(d, OptionType::Prefix);
478 }
479
480 void onPrefixList (const DagInit* d) {
481 addOption(d, OptionType::PrefixList);
482 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000483
484 /// Option property handlers --
485 /// Methods that handle properties that are common for all types of
486 /// options (like append_cmd, stop_compilation)
487
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000488 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000489 checkNumberOfArguments(d, 1);
490 std::string const& cmd = InitPtrToString(d->getArg(0));
491
492 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
493 }
494
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000495 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000496 checkNumberOfArguments(d, 0);
497 toolProps_.OptDescs[o.Name].setForward();
498 }
499
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000500 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000501 checkNumberOfArguments(d, 1);
502 const std::string& help_message = InitPtrToString(d->getArg(0));
503
504 o.Help = help_message;
505 }
506
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000507 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000508 checkNumberOfArguments(d, 0);
509 o.setRequired();
510 }
511
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000512 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000513 checkNumberOfArguments(d, 0);
514 if (o.Type != OptionType::Switch)
515 throw std::string("Only options of type Switch can stop compilation!");
516 toolProps_.OptDescs[o.Name].setStopCompilation();
517 }
518
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000519 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000520 checkNumberOfArguments(d, 0);
521 toolProps_.OptDescs[o.Name].setUnpackValues();
522 }
523
524 /// Helper functions
525
526 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000527 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000528 checkNumberOfArguments(d, 2);
529 const std::string& name = InitPtrToString(d->getArg(0));
530
531 GlobalOptionDescription o(t, name);
532 toolProps_.OptDescs[name].Type = t;
533 toolProps_.OptDescs[name].Name = name;
534 processOptionProperties(d, o);
535 insertDescription(o);
536 }
537
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000538 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
539 void insertDescription (const GlobalOptionDescription& o)
540 {
541 if (optDescs_.Descriptions.count(o.Name)) {
542 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
543 D.Merge(o);
544 }
545 else {
546 optDescs_.Descriptions[o.Name] = o;
547 }
548 }
549
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000550 /// processOptionProperties - Go through the list of option
551 /// properties and call a corresponding handler for each.
552 ///
553 /// Parameters:
554 /// name - option name
555 /// d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000556 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000557 // First argument is option name
558 checkNumberOfArguments(d, 2);
559
560 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000561 const DagInit& option_property
562 = InitPtrToDagInitRef(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000563 const std::string& option_property_name
564 = option_property.getOperator()->getAsString();
565 OptionPropertyHandlerMap::iterator method
566 = optionPropertyHandlers_.find(option_property_name);
567
568 if (method != optionPropertyHandlers_.end()) {
569 OptionPropertyHandler h = method->second;
570 (this->*h)(&option_property, o);
571 }
572 else {
573 throw "Unknown option property: " + option_property_name + "!";
574 }
575 }
576 }
577};
578
579// Static members of CollectProperties
580CollectProperties::PropertyHandlerMap
581CollectProperties::propertyHandlers_;
582
583CollectProperties::OptionPropertyHandlerMap
584CollectProperties::optionPropertyHandlers_;
585
586bool CollectProperties::staticMembersInitialized_ = false;
587
588
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000589/// CollectToolProperties - Gather information from the parsed
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000590/// TableGen data (basically a wrapper for the CollectProperties
591/// function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000592void CollectToolProperties (RecordVector::const_iterator B,
593 RecordVector::const_iterator E,
594 ToolPropertiesList& TPList,
595 GlobalOptionDescriptions& OptDescs)
596{
597 // Iterate over a properties list of every Tool definition
598 for (;B!=E;++B) {
599 RecordVector::value_type T = *B;
600 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000601
602 IntrusiveRefCntPtr<ToolProperties>
603 ToolProps(new ToolProperties(T->getName()));
604
605 std::for_each(PropList->begin(), PropList->end(),
606 CollectProperties(*ToolProps, OptDescs));
607 TPList.push_back(ToolProps);
608 }
609}
610
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000611/// EmitCaseTest1Arg - Helper function used by
612/// EmitCaseConstructHandler.
613bool EmitCaseTest1Arg(const std::string& TestName,
614 const DagInit& d,
615 const GlobalOptionDescriptions& OptDescs,
616 std::ostream& O) {
617 checkNumberOfArguments(&d, 1);
618 const std::string& OptName = InitPtrToString(d.getArg(0));
619 if (TestName == "switch_on") {
620 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
621 if (OptDesc.Type != OptionType::Switch)
622 throw OptName + ": incorrect option type!";
623 O << OptDesc.GenVariableName();
624 return true;
625 } else if (TestName == "input_languages_contain") {
626 O << "InLangs.count(\"" << OptName << "\") != 0";
627 return true;
628 }
629
630 return false;
631}
632
633/// EmitCaseTest2Args - Helper function used by
634/// EmitCaseConstructHandler.
635bool EmitCaseTest2Args(const std::string& TestName,
636 const DagInit& d,
637 const char* IndentLevel,
638 const GlobalOptionDescriptions& OptDescs,
639 std::ostream& O) {
640 checkNumberOfArguments(&d, 2);
641 const std::string& OptName = InitPtrToString(d.getArg(0));
642 const std::string& OptArg = InitPtrToString(d.getArg(1));
643 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
644
645 if (TestName == "parameter_equals") {
646 if (OptDesc.Type != OptionType::Parameter
647 && OptDesc.Type != OptionType::Prefix)
648 throw OptName + ": incorrect option type!";
649 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
650 return true;
651 }
652 else if (TestName == "element_in_list") {
653 if (OptDesc.Type != OptionType::ParameterList
654 && OptDesc.Type != OptionType::PrefixList)
655 throw OptName + ": incorrect option type!";
656 const std::string& VarName = OptDesc.GenVariableName();
657 O << "std::find(" << VarName << ".begin(),\n"
658 << IndentLevel << Indent1 << VarName << ".end(), \""
659 << OptArg << "\") != " << VarName << ".end()";
660 return true;
661 }
662
663 return false;
664}
665
666// Forward declaration.
667// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
668void EmitCaseTest(const DagInit& d, const char* IndentLevel,
669 const GlobalOptionDescriptions& OptDescs,
670 std::ostream& O);
671
672/// EmitLogicalOperationTest - Helper function used by
673/// EmitCaseConstructHandler.
674void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
675 const char* IndentLevel,
676 const GlobalOptionDescriptions& OptDescs,
677 std::ostream& O) {
678 O << '(';
679 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
680 const DagInit& InnerTest = InitPtrToDagInitRef(d.getArg(j));
681 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
682 if (j != NumArgs - 1)
683 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
684 else
685 O << ')';
686 }
687}
688
689/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
690void EmitCaseTest(const DagInit& d, const char* IndentLevel,
691 const GlobalOptionDescriptions& OptDescs,
692 std::ostream& O) {
693 const std::string& TestName = d.getOperator()->getAsString();
694
695 if (TestName == "and")
696 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
697 else if (TestName == "or")
698 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
699 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
700 return;
701 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
702 return;
703 else
704 throw TestName + ": unknown edge property!";
705}
706
707// Emit code that handles the 'case' construct.
708// Takes a function object that should emit code for every case clause.
709// Callback's type is
710// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
711template <typename F>
712void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
713 const F& Callback,
714 const GlobalOptionDescriptions& OptDescs,
715 std::ostream& O) {
716 assert(d->getOperator()->getAsString() == "case");
717
718 for (unsigned i = 0, numArgs = d->getNumArgs(); i != numArgs; ++i) {
719 const DagInit& Test = InitPtrToDagInitRef(d->getArg(i));
720
721 if (Test.getOperator()->getAsString() == "default") {
722 if (i+2 != numArgs)
723 throw std::string("The 'default' clause should be the last in the"
724 "'case' construct!");
725 O << IndentLevel << "else {\n";
726 }
727 else {
728 O << IndentLevel << "if (";
729 EmitCaseTest(Test, IndentLevel, OptDescs, O);
730 O << ") {\n";
731 }
732
733 ++i;
734 if (i == numArgs)
735 throw "Case construct handler: no corresponding action "
736 "found for the test " + Test.getAsString() + '!';
737
738 Callback(d->getArg(i), IndentLevel, O);
739 O << IndentLevel << "}\n";
740 }
741}
742
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000743/// EmitForwardOptionPropertyHandlingCode - Helper function used to
744/// implement EmitOptionPropertyHandlingCode(). Emits code for
745/// handling the (forward) option property.
746void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
747 std::ostream& O) {
748 switch (D.Type) {
749 case OptionType::Switch:
750 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
751 break;
752 case OptionType::Parameter:
753 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
754 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
755 break;
756 case OptionType::Prefix:
757 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
758 << D.GenVariableName() << ");\n";
759 break;
760 case OptionType::PrefixList:
761 O << Indent3 << "for (" << D.GenTypeDeclaration()
762 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
763 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
764 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
765 << "*B);\n";
766 break;
767 case OptionType::ParameterList:
768 O << Indent3 << "for (" << D.GenTypeDeclaration()
769 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
770 << Indent3 << "E = " << D.GenVariableName()
771 << ".end() ; B != E; ++B) {\n"
772 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
773 << Indent4 << "vec.push_back(*B);\n"
774 << Indent3 << "}\n";
775 break;
776 }
777}
778
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000779// A helper function used by EmitOptionPropertyHandlingCode() that
780// tells us whether we should emit any code at all.
781bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
782 if (!D.isForward() && !D.isUnpackValues() && D.Props.empty())
783 return false;
784 return true;
785}
786
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000787/// EmitOptionPropertyHandlingCode - Helper function used by
788/// EmitGenerateActionMethod(). Emits code that handles option
789/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000790void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000791 std::ostream& O)
792{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000793 if (!ToolOptionHasInterestingProperties(D))
794 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000795 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000796 O << Indent2 << "if (";
797 if (D.Type == OptionType::Switch)
798 O << D.GenVariableName();
799 else
800 O << '!' << D.GenVariableName() << ".empty()";
801
802 O <<") {\n";
803
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000804 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000805 for (OptionPropertyList::const_iterator B = D.Props.begin(),
806 E = D.Props.end(); B!=E; ++B) {
807 const OptionProperty& val = *B;
808
809 switch (val.first) {
810 // (append_cmd cmd) property
811 case OptionPropertyType::AppendCmd:
812 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
813 break;
814 // Other properties with argument
815 default:
816 break;
817 }
818 }
819
820 // Handle flags
821
822 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000823 if (D.isForward())
824 EmitForwardOptionPropertyHandlingCode(D, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000825
826 // (unpack_values) property
827 if (D.isUnpackValues()) {
828 if (IsListOptionType(D.Type)) {
829 O << Indent3 << "for (" << D.GenTypeDeclaration()
830 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
831 << Indent3 << "E = " << D.GenVariableName()
832 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000833 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000834 }
835 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000836 O << Indent3 << "llvm::SplitString("
837 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000838 }
839 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000840 throw std::string("Switches can't have unpack_values property!");
841 }
842 }
843
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000844 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000845 O << Indent2 << "}\n";
846}
847
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000848/// SubstituteSpecialCommands - Perform string substitution for $CALL
849/// and $ENV. Helper function used by EmitCmdLineVecFill().
850std::string SubstituteSpecialCommands(const std::string& cmd) {
851 if (cmd.find("$CALL(") == 0) {
852 if (cmd.size() == 6)
853 throw std::string("$CALL invocation: empty argument list!");
854 return std::string("hooks::") + (cmd.c_str() + 6) + "()";
855 }
856 else if (cmd.find("$ENV(") == 0) {
857 if (cmd.size() == 5)
858 throw std::string("$ENV invocation: empty argument list!");
859 return std::string("std::getenv(\"") + (cmd.c_str() + 5) + "\")";
860 }
861 else {
862 throw "Unknown special command: " + cmd;
863 }
864}
865
866/// EmitCmdLineVecFill - Emit code that fills in the command line
867/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000868void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
869 bool Version, const char* IndentLevel,
870 std::ostream& O) {
871 StrVector StrVec;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000872 SplitString(InitPtrToString(CmdLine), StrVec, ") ");
873 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000874 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000875
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000876 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000877 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000878 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000879 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000880 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000881 if (cmd.at(0) == '$') {
882 if (cmd == "$INFILE") {
883 if (Version)
884 O << "for (PathVector::const_iterator B = inFiles.begin()"
885 << ", E = inFiles.end();\n"
886 << IndentLevel << "B != E; ++B)\n"
887 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
888 else
889 O << "vec.push_back(inFile.toString());\n";
890 }
891 else if (cmd == "$OUTFILE") {
892 O << "vec.push_back(outFile.toString());\n";
893 }
894 else {
895 O << "vec.push_back(" << SubstituteSpecialCommands(cmd) << ");\n";
896 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000897 }
898 else {
899 O << "vec.push_back(\"" << cmd << "\");\n";
900 }
901 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000902 O << IndentLevel << "ret = Action("
903 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
904 : "\"" + StrVec[0] + "\"")
905 << ", vec);\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000906}
907
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000908/// EmitCmdLineVecFillCallback - A function object wrapper around
909/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
910/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000911class EmitCmdLineVecFillCallback {
912 bool Version;
913 const std::string& ToolName;
914 public:
915 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
916 : Version(Ver), ToolName(TN) {}
917
918 void operator()(const Init* Statement, const char* IndentLevel,
919 std::ostream& O) const
920 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000921 EmitCmdLineVecFill(Statement, ToolName, Version,
922 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000923 }
924};
925
926// EmitGenerateActionMethod - Emit one of two versions of the
927// Tool::GenerateAction() method.
928void EmitGenerateActionMethod (const ToolProperties& P,
929 const GlobalOptionDescriptions& OptDescs,
930 bool Version, std::ostream& O) {
931 if (Version)
932 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
933 else
934 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
935
936 O << Indent2 << "const sys::Path& outFile,\n"
937 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
938 << Indent1 << "{\n"
939 << Indent2 << "Action ret;\n"
940 << Indent2 << "std::vector<std::string> vec;\n";
941
942 // cmd_line is either a string or a 'case' construct.
943 if (typeid(*P.CmdLine) == typeid(StringInit))
944 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
945 else
946 EmitCaseConstructHandler(&InitPtrToDagInitRef(P.CmdLine), Indent2,
947 EmitCmdLineVecFillCallback(Version, P.Name),
948 OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000949
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000950 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000951 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
952 E = P.OptDescs.end(); B != E; ++B) {
953 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000954 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000955 }
956
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000957 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000958 if (P.isSink()) {
959 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
960 << Indent3 << "vec.insert(vec.end(), "
961 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
962 << Indent2 << "}\n";
963 }
964
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000965 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000966 << Indent1 << "}\n\n";
967}
968
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000969/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
970/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000971void EmitGenerateActionMethods (const ToolProperties& P,
972 const GlobalOptionDescriptions& OptDescs,
973 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000974 if (!P.isJoin())
975 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000976 << Indent2 << "const llvm::sys::Path& outFile,\n"
977 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000978 << Indent1 << "{\n"
979 << Indent2 << "throw std::runtime_error(\"" << P.Name
980 << " is not a Join tool!\");\n"
981 << Indent1 << "}\n\n";
982 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000983 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000984
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000985 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000986}
987
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000988/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
989/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000990void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
991 O << Indent1 << "bool IsLast() const {\n"
992 << Indent2 << "bool last = false;\n";
993
994 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
995 E = P.OptDescs.end(); B != E; ++B) {
996 const ToolOptionDescription& val = B->second;
997
998 if (val.isStopCompilation())
999 O << Indent2
1000 << "if (" << val.GenVariableName()
1001 << ")\n" << Indent3 << "last = true;\n";
1002 }
1003
1004 O << Indent2 << "return last;\n"
1005 << Indent1 << "}\n\n";
1006}
1007
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001008/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1009/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001010void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001011 O << Indent1 << "const char* InputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001012 << Indent2 << "return \"" << P.InLanguage << "\";\n"
1013 << Indent1 << "}\n\n";
1014
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001015 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001016 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1017 << Indent1 << "}\n\n";
1018}
1019
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001020/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1021/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001022void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001023 O << Indent1 << "const char* OutputSuffix() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001024 << Indent2 << "return \"" << P.OutputSuffix << "\";\n"
1025 << Indent1 << "}\n\n";
1026}
1027
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001028/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001029void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001030 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001031 << Indent2 << "return \"" << P.Name << "\";\n"
1032 << Indent1 << "}\n\n";
1033}
1034
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001035/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1036/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001037void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1038 O << Indent1 << "bool IsJoin() const {\n";
1039 if (P.isJoin())
1040 O << Indent2 << "return true;\n";
1041 else
1042 O << Indent2 << "return false;\n";
1043 O << Indent1 << "}\n\n";
1044}
1045
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001046/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001047void EmitToolClassDefinition (const ToolProperties& P,
1048 const GlobalOptionDescriptions& OptDescs,
1049 std::ostream& O) {
1050 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001051 return;
1052
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001053 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001054 O << "class " << P.Name << " : public ";
1055 if (P.isJoin())
1056 O << "JoinTool";
1057 else
1058 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +00001059 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001060
1061 EmitNameMethod(P, O);
1062 EmitInOutLanguageMethods(P, O);
1063 EmitOutputSuffixMethod(P, O);
1064 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001065 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001066 EmitIsLastMethod(P, O);
1067
1068 // Close class definition
1069 O << "};\n\n";
1070}
1071
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001072/// EmitOptionDescriptions - Iterate over a list of option
1073/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001074void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1075 std::ostream& O)
1076{
1077 // Emit static cl::Option variables
1078 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1079 E = descs.end(); B!=E; ++B) {
1080 const GlobalOptionDescription& val = B->second;
1081
1082 O << val.GenTypeDeclaration() << ' '
1083 << val.GenVariableName()
1084 << "(\"" << val.Name << '\"';
1085
1086 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1087 O << ", cl::Prefix";
1088
1089 if (val.isRequired()) {
1090 switch (val.Type) {
1091 case OptionType::PrefixList:
1092 case OptionType::ParameterList:
1093 O << ", cl::OneOrMore";
1094 break;
1095 default:
1096 O << ", cl::Required";
1097 }
1098 }
1099
1100 O << ", cl::desc(\"" << val.Help << "\"));\n";
1101 }
1102
1103 if (descs.HasSink)
1104 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1105
1106 O << '\n';
1107}
1108
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001109/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001110void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1111{
1112 // Get the relevant field out of RecordKeeper
1113 Record* LangMapRecord = Records.getDef("LanguageMap");
1114 if (!LangMapRecord)
1115 throw std::string("Language map definition not found!");
1116
1117 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1118 if (!LangsToSuffixesList)
1119 throw std::string("Error in the language map definition!");
1120
1121 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001122 O << "void llvmc::PopulateLanguageMap(LanguageMap& language_map) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001123
1124 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1125 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1126
1127 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1128 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1129
1130 for (unsigned i = 0; i < Suffixes->size(); ++i)
1131 O << Indent1 << "language_map[\""
1132 << InitPtrToString(Suffixes->getElement(i))
1133 << "\"] = \"" << Lang << "\";\n";
1134 }
1135
1136 O << "}\n\n";
1137}
1138
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001139/// FillInToolToLang - Fills in two tables that map tool names to
1140/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001141void FillInToolToLang (const ToolPropertiesList& TPList,
1142 StringMap<std::string>& ToolToInLang,
1143 StringMap<std::string>& ToolToOutLang) {
1144 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1145 B != E; ++B) {
1146 const ToolProperties& P = *(*B);
1147 ToolToInLang[P.Name] = P.InLanguage;
1148 ToolToOutLang[P.Name] = P.OutLanguage;
1149 }
1150}
1151
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001152/// TypecheckGraph - Check that names for output and input languages
1153/// on all edges do match.
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001154// TOFIX: check for cycles.
1155// TOFIX: check for multiple default edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001156void TypecheckGraph (Record* CompilationGraph,
1157 const ToolPropertiesList& TPList) {
1158 StringMap<std::string> ToolToInLang;
1159 StringMap<std::string> ToolToOutLang;
1160
1161 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1162 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1163 StringMap<std::string>::iterator IAE = ToolToInLang.end();
1164 StringMap<std::string>::iterator IBE = ToolToOutLang.end();
1165
1166 for (unsigned i = 0; i < edges->size(); ++i) {
1167 Record* Edge = edges->getElementAsRecord(i);
1168 Record* A = Edge->getValueAsDef("a");
1169 Record* B = Edge->getValueAsDef("b");
1170 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
1171 StringMap<std::string>::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001172 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001173 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001174 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001175 throw B->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001176 if (A->getName() != "root" && IA->second != IB->second)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001177 throw "Edge " + A->getName() + "->" + B->getName()
1178 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001179 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001180 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001181 }
1182}
1183
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001184/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1185/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001186void IncDecWeight (const Init* i, const char* IndentLevel,
1187 std::ostream& O) {
1188 const DagInit& d = InitPtrToDagInitRef(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001189 const std::string& OpName = d.getOperator()->getAsString();
1190
1191 if (OpName == "inc_weight")
1192 O << IndentLevel << Indent1 << "ret += ";
1193 else if (OpName == "dec_weight")
1194 O << IndentLevel << Indent1 << "ret -= ";
1195 else
1196 throw "Unknown operator in edge properties list: " + OpName + '!';
1197
1198 if (d.getNumArgs() > 0)
1199 O << InitPtrToInt(d.getArg(0)) << ";\n";
1200 else
1201 O << "2;\n";
1202
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001203}
1204
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001205/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001206void EmitEdgeClass (unsigned N, const std::string& Target,
1207 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1208 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001209
1210 // Class constructor.
1211 O << "class Edge" << N << ": public Edge {\n"
1212 << "public:\n"
1213 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1214 << "\") {}\n\n"
1215
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001216 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001217 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001218 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001219
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001220 // Handle the 'case' construct.
1221 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001222
1223 O << Indent2 << "return ret;\n"
1224 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001225}
1226
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001227/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001228void EmitEdgeClasses (Record* CompilationGraph,
1229 const GlobalOptionDescriptions& OptDescs,
1230 std::ostream& O) {
1231 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1232
1233 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001234 Record* Edge = edges->getElementAsRecord(i);
1235 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001236 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001237
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001238 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001239 continue;
1240
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001241 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001242 }
1243}
1244
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001245/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1246/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001247void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001248 std::ostream& O)
1249{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001250 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001251
1252 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001253 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001254 << Indent1 << "PopulateLanguageMap(G.ExtsToLangs);\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001255
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001256 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001257
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001258 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1259 if (Tools.empty())
1260 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001261
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001262 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1263 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001264 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001265 O << Indent1 << "G.insertNode(new "
1266 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001267 }
1268
1269 O << '\n';
1270
1271 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001272 for (unsigned i = 0; i < edges->size(); ++i) {
1273 Record* Edge = edges->getElementAsRecord(i);
1274 Record* A = Edge->getValueAsDef("a");
1275 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001276 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001277
1278 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1279
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001280 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001281 O << "new SimpleEdge(\"" << B->getName() << "\")";
1282 else
1283 O << "new Edge" << i << "()";
1284
1285 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001286 }
1287
1288 O << "}\n\n";
1289}
1290
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001291/// ExtractHookNames - Extract the hook names from all instances of
1292/// $CALL(HookName) in the provided command line string. Helper
1293/// function used by FillInHookNames().
1294void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1295 StrVector cmds;
1296 llvm::SplitString(InitPtrToString(CmdLine), cmds, ") ");
1297 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1298 B != E; ++B) {
1299 const std::string& cmd = *B;
1300 if (cmd.find("$CALL(") == 0) {
1301 if (cmd.size() == 6)
1302 throw std::string("$CALL invocation: empty argument list!");
1303 HookNames.push_back(std::string(cmd.c_str() + 6));
1304 }
1305 }
1306}
1307
1308/// FillInHookNames - Actually extract the hook names from all command
1309/// line strings. Helper function used by EmitHookDeclarations().
1310void FillInHookNames(const ToolPropertiesList& TPList,
1311 StrVector& HookNames) {
1312 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1313 E = TPList.end(); B != E; ++B) {
1314 const ToolProperties& P = *(*B);
1315 if (!P.CmdLine)
1316 continue;
1317 if (typeid(*P.CmdLine) == typeid(StringInit)) {
1318 // This is a string.
1319 ExtractHookNames(P.CmdLine, HookNames);
1320 }
1321 else {
1322 // This is a 'case' construct.
1323 const DagInit& d = InitPtrToDagInitRef(P.CmdLine);
1324 bool even = false;
1325 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1326 B != E; ++B) {
1327 if (even)
1328 ExtractHookNames(*B, HookNames);
1329 even = !even;
1330 }
1331 }
1332 }
1333}
1334
1335/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1336/// property records and emit hook function declaration for each
1337/// instance of $CALL(HookName).
1338void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1339 std::ostream& O) {
1340 StrVector HookNames;
1341 FillInHookNames(ToolProps, HookNames);
1342 if (HookNames.empty())
1343 return;
1344 std::sort(HookNames.begin(), HookNames.end());
1345 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1346
1347 O << "namespace hooks {\n";
1348 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1349 O << Indent1 << "std::string " << *B << "();\n";
1350
1351 O << "}\n\n";
1352}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001353
1354// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001355}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001356
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001357/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001358void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001359
1360 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001361 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001362
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001363 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001364 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1365 if (Tools.empty())
1366 throw std::string("No tool definitions found!");
1367
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001368 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001369 ToolPropertiesList tool_props;
1370 GlobalOptionDescriptions opt_descs;
1371 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1372
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001373 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001374 EmitOptionDescriptions(opt_descs, O);
1375
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001376 // Emit hook declarations.
1377 EmitHookDeclarations(tool_props, O);
1378
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001379 // Emit PopulateLanguageMap() function
1380 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001381 EmitPopulateLanguageMap(Records, O);
1382
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001383 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001384 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1385 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001386 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001387
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001388 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1389 if (!CompilationGraphRecord)
1390 throw std::string("Compilation graph description not found!");
1391
1392 // Typecheck the compilation graph.
1393 TypecheckGraph(CompilationGraphRecord, tool_props);
1394
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001395 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001396 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001397
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001398 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001399 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001400
1401 // EOF
1402}