blob: d9d9fda8c842f60cfba60dcb4582fa0066aa4aa7 [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
41// Indentation strings
42const char * Indent1 = " ";
43const char * Indent2 = " ";
44const char * Indent3 = " ";
45const char * Indent4 = " ";
46
47// Default help string
48const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
49
50// Name for the "sink" option
51const char * SinkOptionName = "AutoGeneratedSinkOption";
52
53//===----------------------------------------------------------------------===//
54/// Helper functions
55
56std::string InitPtrToString(Init* ptr) {
57 StringInit& val = dynamic_cast<StringInit&>(*ptr);
58 return val.getValue();
59}
60
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000061int InitPtrToInt(Init* ptr) {
62 IntInit& val = dynamic_cast<IntInit&>(*ptr);
63 return val.getValue();
64}
65
66const DagInit& InitPtrToDagInitRef(Init* ptr) {
67 DagInit& val = dynamic_cast<DagInit&>(*ptr);
68 return val;
69}
70
71
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000072// Ensure that the number of args in d is <= min_arguments,
73// throw exception otherwise
74void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
75 if (d->getNumArgs() < min_arguments)
76 throw "Property " + d->getOperator()->getAsString()
77 + " has too few arguments!";
78}
79
80
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000081//===----------------------------------------------------------------------===//
82/// Back-end specific code
83
84// A command-line option can have one of the following types:
85//
86// Switch - a simple switch w/o arguments, e.g. -O2
87//
88// Parameter - an option that takes one(and only one) argument, e.g. -o file,
89// --output=file
90//
91// ParameterList - same as Parameter, but more than one occurence
92// of the option is allowed, e.g. -lm -lpthread
93//
94// Prefix - argument is everything after the prefix,
95// e.g. -Wa,-foo,-bar, -DNAME=VALUE
96//
97// PrefixList - same as Prefix, but more than one option occurence is
98// allowed
99
100namespace OptionType {
101 enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList};
102}
103
104bool IsListOptionType (OptionType::OptionType t) {
105 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
106}
107
108// Code duplication here is necessary because one option can affect
109// several tools and those tools may have different actions associated
110// with this option. GlobalOptionDescriptions are used to generate
111// the option registration code, while ToolOptionDescriptions are used
112// to generate tool-specific code.
113
114// Base class for option descriptions
115
116struct OptionDescription {
117 OptionType::OptionType Type;
118 std::string Name;
119
120 OptionDescription(OptionType::OptionType t = OptionType::Switch,
121 const std::string& n = "")
122 : Type(t), Name(n)
123 {}
124
125 const char* GenTypeDeclaration() const {
126 switch (Type) {
127 case OptionType::PrefixList:
128 case OptionType::ParameterList:
129 return "cl::list<std::string>";
130 case OptionType::Switch:
131 return "cl::opt<bool>";
132 case OptionType::Parameter:
133 case OptionType::Prefix:
134 default:
135 return "cl::opt<std::string>";
136 }
137 }
138
139 std::string GenVariableName() const {
140 switch (Type) {
141 case OptionType::Switch:
142 return "AutoGeneratedSwitch" + Name;
143 case OptionType::Prefix:
144 return "AutoGeneratedPrefix" + Name;
145 case OptionType::PrefixList:
146 return "AutoGeneratedPrefixList" + Name;
147 case OptionType::Parameter:
148 return "AutoGeneratedParameter" + Name;
149 case OptionType::ParameterList:
150 default:
151 return "AutoGeneratedParameterList" + Name;
152 }
153 }
154
155};
156
157// Global option description
158
159namespace GlobalOptionDescriptionFlags {
160 enum GlobalOptionDescriptionFlags { Required = 0x1 };
161}
162
163struct GlobalOptionDescription : public OptionDescription {
164 std::string Help;
165 unsigned Flags;
166
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000167 // We need t provide a default constructor since
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000168 // StringMap can only store DefaultConstructible objects
169 GlobalOptionDescription() : OptionDescription(), Flags(0)
170 {}
171
172 GlobalOptionDescription (OptionType::OptionType t, const std::string& n)
173 : OptionDescription(t, n), Help(DefaultHelpString), Flags(0)
174 {}
175
176 bool isRequired() const {
177 return Flags & GlobalOptionDescriptionFlags::Required;
178 }
179 void setRequired() {
180 Flags |= GlobalOptionDescriptionFlags::Required;
181 }
182
183 // Merge two option descriptions
184 void Merge (const GlobalOptionDescription& other)
185 {
186 if (other.Type != Type)
187 throw "Conflicting definitions for the option " + Name + "!";
188
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000189 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000190 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000191 else if (other.Help != DefaultHelpString) {
192 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000193 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000194 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000195
196 Flags |= other.Flags;
197 }
198};
199
200// A GlobalOptionDescription array
201// + some flags affecting generation of option declarations
202struct GlobalOptionDescriptions {
203 typedef StringMap<GlobalOptionDescription> container_type;
204 typedef container_type::const_iterator const_iterator;
205
206 // A list of GlobalOptionDescriptions
207 container_type Descriptions;
208 // Should the emitter generate a "cl::sink" option?
209 bool HasSink;
210
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000211 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
212 const_iterator I = Descriptions.find(OptName);
213 if (I != Descriptions.end())
214 return I->second;
215 else
216 throw OptName + ": no such option!";
217 }
218
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000219 // Support for STL-style iteration
220 const_iterator begin() const { return Descriptions.begin(); }
221 const_iterator end() const { return Descriptions.end(); }
222};
223
224
225// Tool-local option description
226
227// Properties without arguments are implemented as flags
228namespace ToolOptionDescriptionFlags {
229 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
230 Forward = 0x2, UnpackValues = 0x4};
231}
232namespace OptionPropertyType {
233 enum OptionPropertyType { AppendCmd };
234}
235
236typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
237OptionProperty;
238typedef SmallVector<OptionProperty, 4> OptionPropertyList;
239
240struct ToolOptionDescription : public OptionDescription {
241 unsigned Flags;
242 OptionPropertyList Props;
243
244 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000245 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000246
247 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
248 : OptionDescription(t, n)
249 {}
250
251 // Various boolean properties
252 bool isStopCompilation() const {
253 return Flags & ToolOptionDescriptionFlags::StopCompilation;
254 }
255 void setStopCompilation() {
256 Flags |= ToolOptionDescriptionFlags::StopCompilation;
257 }
258
259 bool isForward() const {
260 return Flags & ToolOptionDescriptionFlags::Forward;
261 }
262 void setForward() {
263 Flags |= ToolOptionDescriptionFlags::Forward;
264 }
265
266 bool isUnpackValues() const {
267 return Flags & ToolOptionDescriptionFlags::UnpackValues;
268 }
269 void setUnpackValues() {
270 Flags |= ToolOptionDescriptionFlags::UnpackValues;
271 }
272
273 void AddProperty (OptionPropertyType::OptionPropertyType t,
274 const std::string& val)
275 {
276 Props.push_back(std::make_pair(t, val));
277 }
278};
279
280typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
281
282// Tool information record
283
284namespace ToolFlags {
285 enum ToolFlags { Join = 0x1, Sink = 0x2 };
286}
287
288struct ToolProperties : public RefCountedBase<ToolProperties> {
289 std::string Name;
290 StrVector CmdLine;
291 std::string InLanguage;
292 std::string OutLanguage;
293 std::string OutputSuffix;
294 unsigned Flags;
295 ToolOptionDescriptions OptDescs;
296
297 // Various boolean properties
298 void setSink() { Flags |= ToolFlags::Sink; }
299 bool isSink() const { return Flags & ToolFlags::Sink; }
300 void setJoin() { Flags |= ToolFlags::Join; }
301 bool isJoin() const { return Flags & ToolFlags::Join; }
302
303 // Default ctor here is needed because StringMap can only store
304 // DefaultConstructible objects
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000305 ToolProperties() : Flags(0) {}
306 ToolProperties (const std::string& n) : Name(n), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000307};
308
309
310// A list of Tool information records
311// IntrusiveRefCntPtrs are used because StringMap has no copy constructor
312// (and we want to avoid copying ToolProperties anyway)
313typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
314
315
316// Function object for iterating over a list of tool property records
317class CollectProperties {
318private:
319
320 /// Implementation details
321
322 // "Property handler" - a function that extracts information
323 // about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000324 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000325
326 // Map from property names -> property handlers
327 typedef StringMap<PropertyHandler> PropertyHandlerMap;
328
329 // "Option property handler" - a function that extracts information
330 // about a given option property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000331 typedef void (CollectProperties::* OptionPropertyHandler)
332 (const DagInit*, GlobalOptionDescription &);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000333
334 // Map from option property names -> option property handlers
335 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
336
337 // Static maps from strings to CollectProperties methods("handlers")
338 static PropertyHandlerMap propertyHandlers_;
339 static OptionPropertyHandlerMap optionPropertyHandlers_;
340 static bool staticMembersInitialized_;
341
342
343 /// This is where the information is stored
344
345 // Current Tool properties
346 ToolProperties& toolProps_;
347 // OptionDescriptions table(used to register options globally)
348 GlobalOptionDescriptions& optDescs_;
349
350public:
351
352 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
353 : toolProps_(p), optDescs_(d)
354 {
355 if (!staticMembersInitialized_) {
356 // Init tool property handlers
357 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
358 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
359 propertyHandlers_["join"] = &CollectProperties::onJoin;
360 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
361 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
362 propertyHandlers_["parameter_option"]
363 = &CollectProperties::onParameter;
364 propertyHandlers_["parameter_list_option"] =
365 &CollectProperties::onParameterList;
366 propertyHandlers_["prefix_option"] = &CollectProperties::onPrefix;
367 propertyHandlers_["prefix_list_option"] =
368 &CollectProperties::onPrefixList;
369 propertyHandlers_["sink"] = &CollectProperties::onSink;
370 propertyHandlers_["switch_option"] = &CollectProperties::onSwitch;
371
372 // Init option property handlers
373 optionPropertyHandlers_["append_cmd"] = &CollectProperties::onAppendCmd;
374 optionPropertyHandlers_["forward"] = &CollectProperties::onForward;
375 optionPropertyHandlers_["help"] = &CollectProperties::onHelp;
376 optionPropertyHandlers_["required"] = &CollectProperties::onRequired;
377 optionPropertyHandlers_["stop_compilation"] =
378 &CollectProperties::onStopCompilation;
379 optionPropertyHandlers_["unpack_values"] =
380 &CollectProperties::onUnpackValues;
381
382 staticMembersInitialized_ = true;
383 }
384 }
385
386 // Gets called for every tool property;
387 // Just forwards to the corresponding property handler.
388 void operator() (Init* i) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000389 const DagInit& d = InitPtrToDagInitRef(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000390 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000391 PropertyHandlerMap::iterator method
392 = propertyHandlers_.find(property_name);
393
394 if (method != propertyHandlers_.end()) {
395 PropertyHandler h = method->second;
396 (this->*h)(&d);
397 }
398 else {
399 throw "Unknown tool property: " + property_name + "!";
400 }
401 }
402
403private:
404
405 /// Property handlers --
406 /// Functions that extract information about tool properties from
407 /// DAG representation.
408
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000409 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000410 checkNumberOfArguments(d, 1);
411 SplitString(InitPtrToString(d->getArg(0)), toolProps_.CmdLine);
412 if (toolProps_.CmdLine.empty())
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000413 throw "Tool " + toolProps_.Name + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000414 }
415
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000416 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000417 checkNumberOfArguments(d, 1);
418 toolProps_.InLanguage = InitPtrToString(d->getArg(0));
419 }
420
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000421 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000422 checkNumberOfArguments(d, 0);
423 toolProps_.setJoin();
424 }
425
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000426 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000427 checkNumberOfArguments(d, 1);
428 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
429 }
430
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000431 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000432 checkNumberOfArguments(d, 1);
433 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
434 }
435
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000436 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000437 checkNumberOfArguments(d, 0);
438 optDescs_.HasSink = true;
439 toolProps_.setSink();
440 }
441
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000442 void onSwitch (const DagInit* d) {
443 addOption(d, OptionType::Switch);
444 }
445
446 void onParameter (const DagInit* d) {
447 addOption(d, OptionType::Parameter);
448 }
449
450 void onParameterList (const DagInit* d) {
451 addOption(d, OptionType::ParameterList);
452 }
453
454 void onPrefix (const DagInit* d) {
455 addOption(d, OptionType::Prefix);
456 }
457
458 void onPrefixList (const DagInit* d) {
459 addOption(d, OptionType::PrefixList);
460 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000461
462 /// Option property handlers --
463 /// Methods that handle properties that are common for all types of
464 /// options (like append_cmd, stop_compilation)
465
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000466 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000467 checkNumberOfArguments(d, 1);
468 std::string const& cmd = InitPtrToString(d->getArg(0));
469
470 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
471 }
472
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000473 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000474 checkNumberOfArguments(d, 0);
475 toolProps_.OptDescs[o.Name].setForward();
476 }
477
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000478 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000479 checkNumberOfArguments(d, 1);
480 const std::string& help_message = InitPtrToString(d->getArg(0));
481
482 o.Help = help_message;
483 }
484
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000485 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000486 checkNumberOfArguments(d, 0);
487 o.setRequired();
488 }
489
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000490 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000491 checkNumberOfArguments(d, 0);
492 if (o.Type != OptionType::Switch)
493 throw std::string("Only options of type Switch can stop compilation!");
494 toolProps_.OptDescs[o.Name].setStopCompilation();
495 }
496
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000497 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000498 checkNumberOfArguments(d, 0);
499 toolProps_.OptDescs[o.Name].setUnpackValues();
500 }
501
502 /// Helper functions
503
504 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000505 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000506 checkNumberOfArguments(d, 2);
507 const std::string& name = InitPtrToString(d->getArg(0));
508
509 GlobalOptionDescription o(t, name);
510 toolProps_.OptDescs[name].Type = t;
511 toolProps_.OptDescs[name].Name = name;
512 processOptionProperties(d, o);
513 insertDescription(o);
514 }
515
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000516 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
517 void insertDescription (const GlobalOptionDescription& o)
518 {
519 if (optDescs_.Descriptions.count(o.Name)) {
520 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
521 D.Merge(o);
522 }
523 else {
524 optDescs_.Descriptions[o.Name] = o;
525 }
526 }
527
528 // Go through the list of option properties and call a corresponding
529 // handler for each.
530 //
531 // Parameters:
532 // name - option name
533 // d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000534 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000535 // First argument is option name
536 checkNumberOfArguments(d, 2);
537
538 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000539 const DagInit& option_property
540 = InitPtrToDagInitRef(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000541 const std::string& option_property_name
542 = option_property.getOperator()->getAsString();
543 OptionPropertyHandlerMap::iterator method
544 = optionPropertyHandlers_.find(option_property_name);
545
546 if (method != optionPropertyHandlers_.end()) {
547 OptionPropertyHandler h = method->second;
548 (this->*h)(&option_property, o);
549 }
550 else {
551 throw "Unknown option property: " + option_property_name + "!";
552 }
553 }
554 }
555};
556
557// Static members of CollectProperties
558CollectProperties::PropertyHandlerMap
559CollectProperties::propertyHandlers_;
560
561CollectProperties::OptionPropertyHandlerMap
562CollectProperties::optionPropertyHandlers_;
563
564bool CollectProperties::staticMembersInitialized_ = false;
565
566
567// Gather information from the parsed TableGen data
568// (Basically a wrapper for CollectProperties)
569void CollectToolProperties (RecordVector::const_iterator B,
570 RecordVector::const_iterator E,
571 ToolPropertiesList& TPList,
572 GlobalOptionDescriptions& OptDescs)
573{
574 // Iterate over a properties list of every Tool definition
575 for (;B!=E;++B) {
576 RecordVector::value_type T = *B;
577 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000578
579 IntrusiveRefCntPtr<ToolProperties>
580 ToolProps(new ToolProperties(T->getName()));
581
582 std::for_each(PropList->begin(), PropList->end(),
583 CollectProperties(*ToolProps, OptDescs));
584 TPList.push_back(ToolProps);
585 }
586}
587
588// Used by EmitGenerateActionMethod
589void EmitOptionPropertyHandlingCode (const ToolProperties& P,
590 const ToolOptionDescription& D,
591 std::ostream& O)
592{
593 // if clause
594 O << Indent2 << "if (";
595 if (D.Type == OptionType::Switch)
596 O << D.GenVariableName();
597 else
598 O << '!' << D.GenVariableName() << ".empty()";
599
600 O <<") {\n";
601
602 // Handle option properties that take an argument
603 for (OptionPropertyList::const_iterator B = D.Props.begin(),
604 E = D.Props.end(); B!=E; ++B) {
605 const OptionProperty& val = *B;
606
607 switch (val.first) {
608 // (append_cmd cmd) property
609 case OptionPropertyType::AppendCmd:
610 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
611 break;
612 // Other properties with argument
613 default:
614 break;
615 }
616 }
617
618 // Handle flags
619
620 // (forward) property
621 if (D.isForward()) {
622 switch (D.Type) {
623 case OptionType::Switch:
624 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
625 break;
626 case OptionType::Parameter:
627 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
628 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
629 break;
630 case OptionType::Prefix:
631 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
632 << D.GenVariableName() << ");\n";
633 break;
634 case OptionType::PrefixList:
635 O << Indent3 << "for (" << D.GenTypeDeclaration()
636 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
637 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
638 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
639 << "*B);\n";
640 break;
641 case OptionType::ParameterList:
642 O << Indent3 << "for (" << D.GenTypeDeclaration()
643 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
644 << Indent3 << "E = " << D.GenVariableName()
645 << ".end() ; B != E; ++B) {\n"
646 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
647 << Indent4 << "vec.push_back(*B);\n"
648 << Indent3 << "}\n";
649 break;
650 }
651 }
652
653 // (unpack_values) property
654 if (D.isUnpackValues()) {
655 if (IsListOptionType(D.Type)) {
656 O << Indent3 << "for (" << D.GenTypeDeclaration()
657 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
658 << Indent3 << "E = " << D.GenVariableName()
659 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000660 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000661 }
662 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000663 O << Indent3 << "llvm::SplitString("
664 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000665 }
666 else {
667 // TOFIX: move this to the type-checking phase
668 throw std::string("Switches can't have unpack_values property!");
669 }
670 }
671
672 // close if clause
673 O << Indent2 << "}\n";
674}
675
676// Emite one of two versions of GenerateAction method
677void EmitGenerateActionMethod (const ToolProperties& P, int V, std::ostream& O)
678{
679 assert(V==1 || V==2);
680 if (V==1)
681 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
682 else
683 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
684
685 O << Indent2 << "const sys::Path& outFile) const\n"
686 << Indent1 << "{\n"
687 << Indent2 << "std::vector<std::string> vec;\n";
688
689 // Parse CmdLine tool property
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000690 if(P.CmdLine.empty())
691 throw "Tool " + P.Name + " has empty command line!";
692
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000693 StrVector::const_iterator I = P.CmdLine.begin();
694 ++I;
695 for (StrVector::const_iterator E = P.CmdLine.end(); I != E; ++I) {
696 const std::string& cmd = *I;
697 O << Indent2;
698 if (cmd == "$INFILE") {
699 if (V==1)
700 O << "for (PathVector::const_iterator B = inFiles.begin()"
701 << ", E = inFiles.end();\n"
702 << Indent2 << "B != E; ++B)\n"
703 << Indent3 << "vec.push_back(B->toString());\n";
704 else
705 O << "vec.push_back(inFile.toString());\n";
706 }
707 else if (cmd == "$OUTFILE") {
708 O << "vec.push_back(outFile.toString());\n";
709 }
710 else {
711 O << "vec.push_back(\"" << cmd << "\");\n";
712 }
713 }
714
715 // For every understood option, emit handling code
716 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
717 E = P.OptDescs.end(); B != E; ++B) {
718 const ToolOptionDescription& val = B->second;
719 EmitOptionPropertyHandlingCode(P, val, O);
720 }
721
722 // Handle Sink property
723 if (P.isSink()) {
724 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
725 << Indent3 << "vec.insert(vec.end(), "
726 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
727 << Indent2 << "}\n";
728 }
729
730 O << Indent2 << "return Action(\"" << P.CmdLine.at(0) << "\", vec);\n"
731 << Indent1 << "}\n\n";
732}
733
734// Emit GenerateAction methods for Tool classes
735void EmitGenerateActionMethods (const ToolProperties& P, std::ostream& O) {
736
737 if (!P.isJoin())
738 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
739 << Indent2 << "const llvm::sys::Path& outFile) const\n"
740 << Indent1 << "{\n"
741 << Indent2 << "throw std::runtime_error(\"" << P.Name
742 << " is not a Join tool!\");\n"
743 << Indent1 << "}\n\n";
744 else
745 EmitGenerateActionMethod(P, 1, O);
746
747 EmitGenerateActionMethod(P, 2, O);
748}
749
750// Emit IsLast() method for Tool classes
751void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
752 O << Indent1 << "bool IsLast() const {\n"
753 << Indent2 << "bool last = false;\n";
754
755 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
756 E = P.OptDescs.end(); B != E; ++B) {
757 const ToolOptionDescription& val = B->second;
758
759 if (val.isStopCompilation())
760 O << Indent2
761 << "if (" << val.GenVariableName()
762 << ")\n" << Indent3 << "last = true;\n";
763 }
764
765 O << Indent2 << "return last;\n"
766 << Indent1 << "}\n\n";
767}
768
769// Emit static [Input,Output]Language() methods for Tool classes
770void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000771 O << Indent1 << "const char* InputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000772 << Indent2 << "return \"" << P.InLanguage << "\";\n"
773 << Indent1 << "}\n\n";
774
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000775 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000776 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
777 << Indent1 << "}\n\n";
778}
779
780// Emit static [Input,Output]Language() methods for Tool classes
781void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000782 O << Indent1 << "const char* OutputSuffix() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000783 << Indent2 << "return \"" << P.OutputSuffix << "\";\n"
784 << Indent1 << "}\n\n";
785}
786
787// Emit static Name() method for Tool classes
788void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000789 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000790 << Indent2 << "return \"" << P.Name << "\";\n"
791 << Indent1 << "}\n\n";
792}
793
794// Emit static Name() method for Tool classes
795void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
796 O << Indent1 << "bool IsJoin() const {\n";
797 if (P.isJoin())
798 O << Indent2 << "return true;\n";
799 else
800 O << Indent2 << "return false;\n";
801 O << Indent1 << "}\n\n";
802}
803
804// Emit a Tool class definition
805void EmitToolClassDefinition (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000806
807 if(P.Name == "root")
808 return;
809
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000810 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +0000811 O << "class " << P.Name << " : public ";
812 if (P.isJoin())
813 O << "JoinTool";
814 else
815 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +0000816 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000817
818 EmitNameMethod(P, O);
819 EmitInOutLanguageMethods(P, O);
820 EmitOutputSuffixMethod(P, O);
821 EmitIsJoinMethod(P, O);
822 EmitGenerateActionMethods(P, O);
823 EmitIsLastMethod(P, O);
824
825 // Close class definition
826 O << "};\n\n";
827}
828
829// Iterate over a list of option descriptions and emit registration code
830void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
831 std::ostream& O)
832{
833 // Emit static cl::Option variables
834 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
835 E = descs.end(); B!=E; ++B) {
836 const GlobalOptionDescription& val = B->second;
837
838 O << val.GenTypeDeclaration() << ' '
839 << val.GenVariableName()
840 << "(\"" << val.Name << '\"';
841
842 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
843 O << ", cl::Prefix";
844
845 if (val.isRequired()) {
846 switch (val.Type) {
847 case OptionType::PrefixList:
848 case OptionType::ParameterList:
849 O << ", cl::OneOrMore";
850 break;
851 default:
852 O << ", cl::Required";
853 }
854 }
855
856 O << ", cl::desc(\"" << val.Help << "\"));\n";
857 }
858
859 if (descs.HasSink)
860 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
861
862 O << '\n';
863}
864
865void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
866{
867 // Get the relevant field out of RecordKeeper
868 Record* LangMapRecord = Records.getDef("LanguageMap");
869 if (!LangMapRecord)
870 throw std::string("Language map definition not found!");
871
872 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
873 if (!LangsToSuffixesList)
874 throw std::string("Error in the language map definition!");
875
876 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +0000877 O << "void llvmc::PopulateLanguageMap(LanguageMap& language_map) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000878
879 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
880 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
881
882 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
883 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
884
885 for (unsigned i = 0; i < Suffixes->size(); ++i)
886 O << Indent1 << "language_map[\""
887 << InitPtrToString(Suffixes->getElement(i))
888 << "\"] = \"" << Lang << "\";\n";
889 }
890
891 O << "}\n\n";
892}
893
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000894// Fills in two tables that map tool names to (input, output) languages.
895// Used by the typechecker.
896void FillInToolToLang (const ToolPropertiesList& TPList,
897 StringMap<std::string>& ToolToInLang,
898 StringMap<std::string>& ToolToOutLang) {
899 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
900 B != E; ++B) {
901 const ToolProperties& P = *(*B);
902 ToolToInLang[P.Name] = P.InLanguage;
903 ToolToOutLang[P.Name] = P.OutLanguage;
904 }
905}
906
907// Check that all output and input language names match.
Mikhail Glushenkov761958d2008-05-06 16:36:50 +0000908// TOFIX: check for cycles.
909// TOFIX: check for multiple default edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000910void TypecheckGraph (Record* CompilationGraph,
911 const ToolPropertiesList& TPList) {
912 StringMap<std::string> ToolToInLang;
913 StringMap<std::string> ToolToOutLang;
914
915 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
916 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
917 StringMap<std::string>::iterator IAE = ToolToInLang.end();
918 StringMap<std::string>::iterator IBE = ToolToOutLang.end();
919
920 for (unsigned i = 0; i < edges->size(); ++i) {
921 Record* Edge = edges->getElementAsRecord(i);
922 Record* A = Edge->getValueAsDef("a");
923 Record* B = Edge->getValueAsDef("b");
924 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
925 StringMap<std::string>::iterator IB = ToolToInLang.find(B->getName());
926 if(IA == IAE)
927 throw A->getName() + ": no such tool!";
928 if(IB == IBE)
929 throw B->getName() + ": no such tool!";
930 if(A->getName() != "root" && IA->second != IB->second)
931 throw "Edge " + A->getName() + "->" + B->getName()
932 + ": output->input language mismatch";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +0000933 if(B->getName() == "root")
934 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000935 }
936}
937
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000938// Helper function used by EmitEdgePropertyTest.
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000939bool EmitEdgePropertyTest1Arg(const std::string& PropName,
940 const DagInit& Prop,
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000941 const GlobalOptionDescriptions& OptDescs,
942 std::ostream& O) {
943 checkNumberOfArguments(&Prop, 1);
944 const std::string& OptName = InitPtrToString(Prop.getArg(0));
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000945 if (PropName == "switch_on") {
946 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
947 if (OptDesc.Type != OptionType::Switch)
948 throw OptName + ": incorrect option type!";
949 O << OptDesc.GenVariableName();
950 return true;
Mikhail Glushenkovb0387302008-05-06 18:18:58 +0000951 } else if (PropName == "if_input_languages_contain") {
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000952 O << "InLangs.count(\"" << OptName << "\") != 0";
953 return true;
954 }
955
956 return false;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000957}
958
959// Helper function used by EmitEdgePropertyTest.
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000960bool EmitEdgePropertyTest2Args(const std::string& PropName,
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000961 const DagInit& Prop,
962 const GlobalOptionDescriptions& OptDescs,
963 std::ostream& O) {
964 checkNumberOfArguments(&Prop, 2);
965 const std::string& OptName = InitPtrToString(Prop.getArg(0));
966 const std::string& OptArg = InitPtrToString(Prop.getArg(1));
967 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
968
969 if (PropName == "parameter_equals") {
970 if (OptDesc.Type != OptionType::Parameter
971 && OptDesc.Type != OptionType::Prefix)
972 throw OptName + ": incorrect option type!";
973 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000974 return true;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000975 }
976 else if (PropName == "element_in_list") {
977 if (OptDesc.Type != OptionType::ParameterList
978 && OptDesc.Type != OptionType::PrefixList)
979 throw OptName + ": incorrect option type!";
980 const std::string& VarName = OptDesc.GenVariableName();
981 O << "std::find(" << VarName << ".begin(),\n"
982 << Indent3 << VarName << ".end(), \""
983 << OptArg << "\") != " << VarName << ".end()";
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000984 return true;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000985 }
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000986
987 return false;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000988}
989
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000990// Forward declaration.
991void EmitEdgePropertyTest(const DagInit& Prop,
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +0000992 const GlobalOptionDescriptions& OptDescs,
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000993 std::ostream& O);
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000994
995// Helper function used by EmitEdgeClass.
996void EmitLogicalOperationTest(const DagInit& Prop, const char* LogicOp,
997 const GlobalOptionDescriptions& OptDescs,
998 std::ostream& O) {
999 O << '(';
1000 for (unsigned j = 0, NumArgs = Prop.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001001 const DagInit& InnerProp = InitPtrToDagInitRef(Prop.getArg(j));
1002 EmitEdgePropertyTest(InnerProp, OptDescs, O);
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001003 if (j != NumArgs - 1)
1004 O << ")\n" << Indent3 << ' ' << LogicOp << " (";
1005 else
1006 O << ')';
1007 }
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001008}
1009
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001010// Helper function used by EmitEdgeClass.
1011void EmitEdgePropertyTest(const DagInit& Prop,
1012 const GlobalOptionDescriptions& OptDescs,
1013 std::ostream& O) {
1014 const std::string& PropName = Prop.getOperator()->getAsString();
1015
1016 if (PropName == "and")
1017 EmitLogicalOperationTest(Prop, "&&", OptDescs, O);
1018 else if (PropName == "or")
1019 EmitLogicalOperationTest(Prop, "||", OptDescs, O);
1020 else if (EmitEdgePropertyTest1Arg(PropName, Prop, OptDescs, O))
1021 return;
1022 else if (EmitEdgePropertyTest2Args(PropName, Prop, OptDescs, O))
1023 return;
1024 else
1025 throw PropName + ": unknown edge property!";
1026}
1027
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001028// Emit a single Edge* class.
1029void EmitEdgeClass(unsigned N, const std::string& Target,
1030 ListInit* Props, const GlobalOptionDescriptions& OptDescs,
1031 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001032
1033 // Class constructor.
1034 O << "class Edge" << N << ": public Edge {\n"
1035 << "public:\n"
1036 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1037 << "\") {}\n\n"
1038
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001039 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001040 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001041 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001042
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001043 // Emit tests for every edge property.
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001044 for (size_t i = 0, PropsSize = Props->size(); i < PropsSize; ++i) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001045 const DagInit& Prop = InitPtrToDagInitRef(Props->getElement(i));
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001046 const std::string& PropName = Prop.getOperator()->getAsString();
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001047 unsigned N = 2;
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001048
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001049 O << Indent2 << "if (";
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001050
1051 if (PropName == "weight") {
1052 checkNumberOfArguments(&Prop, 2);
1053 N = InitPtrToInt(Prop.getArg(0));
1054 const DagInit& InnerProp = InitPtrToDagInitRef(Prop.getArg(1));
1055 EmitEdgePropertyTest(InnerProp, OptDescs, O);
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001056 }
1057 else {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001058 EmitEdgePropertyTest(Prop, OptDescs, O);
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001059 }
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001060
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001061 O << ")\n" << Indent3 << "ret += " << N << ";\n";
1062 }
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001063
1064 O << Indent2 << "return ret;\n"
1065 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001066}
1067
1068// Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001069void EmitEdgeClasses (Record* CompilationGraph,
1070 const GlobalOptionDescriptions& OptDescs,
1071 std::ostream& O) {
1072 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1073
1074 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001075 Record* Edge = edges->getElementAsRecord(i);
1076 Record* B = Edge->getValueAsDef("b");
1077 ListInit* Props = Edge->getValueAsListInit("props");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001078
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001079 if (Props->empty())
1080 continue;
1081
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001082 EmitEdgeClass(i, B->getName(), Props, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001083 }
1084}
1085
1086void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001087 std::ostream& O)
1088{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001089 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001090
1091 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001092 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001093 << Indent1 << "PopulateLanguageMap(G.ExtsToLangs);\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001094
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001095 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001096
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001097 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1098 if (Tools.empty())
1099 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001100
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001101 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1102 const std::string& Name = (*B)->getName();
1103 if(Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001104 O << Indent1 << "G.insertNode(new "
1105 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001106 }
1107
1108 O << '\n';
1109
1110 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001111 for (unsigned i = 0; i < edges->size(); ++i) {
1112 Record* Edge = edges->getElementAsRecord(i);
1113 Record* A = Edge->getValueAsDef("a");
1114 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001115 ListInit* Props = Edge->getValueAsListInit("props");
1116
1117 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1118
1119 if (Props->empty())
1120 O << "new SimpleEdge(\"" << B->getName() << "\")";
1121 else
1122 O << "new Edge" << i << "()";
1123
1124 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001125 }
1126
1127 O << "}\n\n";
1128}
1129
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001130
1131// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001132}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001133
1134// Back-end entry point
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001135void LLVMCConfigurationEmitter::run (std::ostream &O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001136 // Emit file header
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001137 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001138
1139 // Get a list of all defined Tools
1140 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1141 if (Tools.empty())
1142 throw std::string("No tool definitions found!");
1143
1144 // Gather information from the Tool descriptions
1145 ToolPropertiesList tool_props;
1146 GlobalOptionDescriptions opt_descs;
1147 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1148
1149 // Emit global option registration code
1150 EmitOptionDescriptions(opt_descs, O);
1151
1152 // Emit PopulateLanguageMap function
1153 // (a language map maps from file extensions to language names)
1154 EmitPopulateLanguageMap(Records, O);
1155
1156 // Emit Tool classes
1157 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1158 E = tool_props.end(); B!=E; ++B)
1159 EmitToolClassDefinition(*(*B), O);
1160
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001161 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1162 if (!CompilationGraphRecord)
1163 throw std::string("Compilation graph description not found!");
1164
1165 // Typecheck the compilation graph.
1166 TypecheckGraph(CompilationGraphRecord, tool_props);
1167
1168 // Emit Edge* classes.
1169 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001170
1171 // Emit PopulateCompilationGraph function
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001172 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001173
1174 // EOF
1175}