blob: 122424428e5469f74c84f524f3eed5efa7b0cefe [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
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
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;
310 StrVector CmdLine;
311 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);
435 SplitString(InitPtrToString(d->getArg(0)), toolProps_.CmdLine);
436 if (toolProps_.CmdLine.empty())
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000437 throw "Tool " + toolProps_.Name + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000438 }
439
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000440 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000441 checkNumberOfArguments(d, 1);
442 toolProps_.InLanguage = InitPtrToString(d->getArg(0));
443 }
444
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000445 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000446 checkNumberOfArguments(d, 0);
447 toolProps_.setJoin();
448 }
449
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000450 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000451 checkNumberOfArguments(d, 1);
452 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
453 }
454
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000455 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000456 checkNumberOfArguments(d, 1);
457 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
458 }
459
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000460 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000461 checkNumberOfArguments(d, 0);
462 optDescs_.HasSink = true;
463 toolProps_.setSink();
464 }
465
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000466 void onSwitch (const DagInit* d) {
467 addOption(d, OptionType::Switch);
468 }
469
470 void onParameter (const DagInit* d) {
471 addOption(d, OptionType::Parameter);
472 }
473
474 void onParameterList (const DagInit* d) {
475 addOption(d, OptionType::ParameterList);
476 }
477
478 void onPrefix (const DagInit* d) {
479 addOption(d, OptionType::Prefix);
480 }
481
482 void onPrefixList (const DagInit* d) {
483 addOption(d, OptionType::PrefixList);
484 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000485
486 /// Option property handlers --
487 /// Methods that handle properties that are common for all types of
488 /// options (like append_cmd, stop_compilation)
489
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000490 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000491 checkNumberOfArguments(d, 1);
492 std::string const& cmd = InitPtrToString(d->getArg(0));
493
494 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
495 }
496
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000497 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000498 checkNumberOfArguments(d, 0);
499 toolProps_.OptDescs[o.Name].setForward();
500 }
501
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000502 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000503 checkNumberOfArguments(d, 1);
504 const std::string& help_message = InitPtrToString(d->getArg(0));
505
506 o.Help = help_message;
507 }
508
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000509 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000510 checkNumberOfArguments(d, 0);
511 o.setRequired();
512 }
513
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000514 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000515 checkNumberOfArguments(d, 0);
516 if (o.Type != OptionType::Switch)
517 throw std::string("Only options of type Switch can stop compilation!");
518 toolProps_.OptDescs[o.Name].setStopCompilation();
519 }
520
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000521 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000522 checkNumberOfArguments(d, 0);
523 toolProps_.OptDescs[o.Name].setUnpackValues();
524 }
525
526 /// Helper functions
527
528 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000529 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000530 checkNumberOfArguments(d, 2);
531 const std::string& name = InitPtrToString(d->getArg(0));
532
533 GlobalOptionDescription o(t, name);
534 toolProps_.OptDescs[name].Type = t;
535 toolProps_.OptDescs[name].Name = name;
536 processOptionProperties(d, o);
537 insertDescription(o);
538 }
539
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000540 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
541 void insertDescription (const GlobalOptionDescription& o)
542 {
543 if (optDescs_.Descriptions.count(o.Name)) {
544 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
545 D.Merge(o);
546 }
547 else {
548 optDescs_.Descriptions[o.Name] = o;
549 }
550 }
551
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000552 /// processOptionProperties - Go through the list of option
553 /// properties and call a corresponding handler for each.
554 ///
555 /// Parameters:
556 /// name - option name
557 /// d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000558 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000559 // First argument is option name
560 checkNumberOfArguments(d, 2);
561
562 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000563 const DagInit& option_property
564 = InitPtrToDagInitRef(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000565 const std::string& option_property_name
566 = option_property.getOperator()->getAsString();
567 OptionPropertyHandlerMap::iterator method
568 = optionPropertyHandlers_.find(option_property_name);
569
570 if (method != optionPropertyHandlers_.end()) {
571 OptionPropertyHandler h = method->second;
572 (this->*h)(&option_property, o);
573 }
574 else {
575 throw "Unknown option property: " + option_property_name + "!";
576 }
577 }
578 }
579};
580
581// Static members of CollectProperties
582CollectProperties::PropertyHandlerMap
583CollectProperties::propertyHandlers_;
584
585CollectProperties::OptionPropertyHandlerMap
586CollectProperties::optionPropertyHandlers_;
587
588bool CollectProperties::staticMembersInitialized_ = false;
589
590
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000591/// CollectToolProperties - Gather information from the parsed
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000592/// TableGen data (basically a wrapper for the CollectProperties
593/// function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000594void CollectToolProperties (RecordVector::const_iterator B,
595 RecordVector::const_iterator E,
596 ToolPropertiesList& TPList,
597 GlobalOptionDescriptions& OptDescs)
598{
599 // Iterate over a properties list of every Tool definition
600 for (;B!=E;++B) {
601 RecordVector::value_type T = *B;
602 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000603
604 IntrusiveRefCntPtr<ToolProperties>
605 ToolProps(new ToolProperties(T->getName()));
606
607 std::for_each(PropList->begin(), PropList->end(),
608 CollectProperties(*ToolProps, OptDescs));
609 TPList.push_back(ToolProps);
610 }
611}
612
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000613/// EmitForwardOptionPropertyHandlingCode - Helper function used to
614/// implement EmitOptionPropertyHandlingCode(). Emits code for
615/// handling the (forward) option property.
616void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
617 std::ostream& O) {
618 switch (D.Type) {
619 case OptionType::Switch:
620 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
621 break;
622 case OptionType::Parameter:
623 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
624 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
625 break;
626 case OptionType::Prefix:
627 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
628 << D.GenVariableName() << ");\n";
629 break;
630 case OptionType::PrefixList:
631 O << Indent3 << "for (" << D.GenTypeDeclaration()
632 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
633 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
634 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
635 << "*B);\n";
636 break;
637 case OptionType::ParameterList:
638 O << Indent3 << "for (" << D.GenTypeDeclaration()
639 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
640 << Indent3 << "E = " << D.GenVariableName()
641 << ".end() ; B != E; ++B) {\n"
642 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
643 << Indent4 << "vec.push_back(*B);\n"
644 << Indent3 << "}\n";
645 break;
646 }
647}
648
649/// EmitOptionPropertyHandlingCode - Helper function used by
650/// EmitGenerateActionMethod(). Emits code that handles option
651/// properties.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000652void EmitOptionPropertyHandlingCode (const ToolProperties& P,
653 const ToolOptionDescription& D,
654 std::ostream& O)
655{
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000656 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000657 O << Indent2 << "if (";
658 if (D.Type == OptionType::Switch)
659 O << D.GenVariableName();
660 else
661 O << '!' << D.GenVariableName() << ".empty()";
662
663 O <<") {\n";
664
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000665 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000666 for (OptionPropertyList::const_iterator B = D.Props.begin(),
667 E = D.Props.end(); B!=E; ++B) {
668 const OptionProperty& val = *B;
669
670 switch (val.first) {
671 // (append_cmd cmd) property
672 case OptionPropertyType::AppendCmd:
673 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
674 break;
675 // Other properties with argument
676 default:
677 break;
678 }
679 }
680
681 // Handle flags
682
683 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000684 if (D.isForward())
685 EmitForwardOptionPropertyHandlingCode(D, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000686
687 // (unpack_values) property
688 if (D.isUnpackValues()) {
689 if (IsListOptionType(D.Type)) {
690 O << Indent3 << "for (" << D.GenTypeDeclaration()
691 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
692 << Indent3 << "E = " << D.GenVariableName()
693 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000694 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000695 }
696 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000697 O << Indent3 << "llvm::SplitString("
698 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000699 }
700 else {
701 // TOFIX: move this to the type-checking phase
702 throw std::string("Switches can't have unpack_values property!");
703 }
704 }
705
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000706 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000707 O << Indent2 << "}\n";
708}
709
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000710// EmitGenerateActionMethod - Emit one of two versions of the
711// Tool::GenerateAction() method.
712void EmitGenerateActionMethod (const ToolProperties& P, bool V, std::ostream& O)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000713{
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000714 if (V)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000715 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
716 else
717 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
718
719 O << Indent2 << "const sys::Path& outFile) const\n"
720 << Indent1 << "{\n"
721 << Indent2 << "std::vector<std::string> vec;\n";
722
723 // Parse CmdLine tool property
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000724 if(P.CmdLine.empty())
725 throw "Tool " + P.Name + " has empty command line!";
726
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000727 StrVector::const_iterator I = P.CmdLine.begin();
728 ++I;
729 for (StrVector::const_iterator E = P.CmdLine.end(); I != E; ++I) {
730 const std::string& cmd = *I;
731 O << Indent2;
732 if (cmd == "$INFILE") {
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000733 if (V)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000734 O << "for (PathVector::const_iterator B = inFiles.begin()"
735 << ", E = inFiles.end();\n"
736 << Indent2 << "B != E; ++B)\n"
737 << Indent3 << "vec.push_back(B->toString());\n";
738 else
739 O << "vec.push_back(inFile.toString());\n";
740 }
741 else if (cmd == "$OUTFILE") {
742 O << "vec.push_back(outFile.toString());\n";
743 }
744 else {
745 O << "vec.push_back(\"" << cmd << "\");\n";
746 }
747 }
748
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000749 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000750 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
751 E = P.OptDescs.end(); B != E; ++B) {
752 const ToolOptionDescription& val = B->second;
753 EmitOptionPropertyHandlingCode(P, val, O);
754 }
755
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000756 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000757 if (P.isSink()) {
758 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
759 << Indent3 << "vec.insert(vec.end(), "
760 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
761 << Indent2 << "}\n";
762 }
763
764 O << Indent2 << "return Action(\"" << P.CmdLine.at(0) << "\", vec);\n"
765 << Indent1 << "}\n\n";
766}
767
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000768/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
769/// a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000770void EmitGenerateActionMethods (const ToolProperties& P, std::ostream& O) {
771
772 if (!P.isJoin())
773 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
774 << Indent2 << "const llvm::sys::Path& outFile) const\n"
775 << Indent1 << "{\n"
776 << Indent2 << "throw std::runtime_error(\"" << P.Name
777 << " is not a Join tool!\");\n"
778 << Indent1 << "}\n\n";
779 else
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000780 EmitGenerateActionMethod(P, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000781
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000782 EmitGenerateActionMethod(P, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000783}
784
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000785/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
786/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000787void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
788 O << Indent1 << "bool IsLast() const {\n"
789 << Indent2 << "bool last = false;\n";
790
791 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
792 E = P.OptDescs.end(); B != E; ++B) {
793 const ToolOptionDescription& val = B->second;
794
795 if (val.isStopCompilation())
796 O << Indent2
797 << "if (" << val.GenVariableName()
798 << ")\n" << Indent3 << "last = true;\n";
799 }
800
801 O << Indent2 << "return last;\n"
802 << Indent1 << "}\n\n";
803}
804
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000805/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
806/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000807void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000808 O << Indent1 << "const char* InputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000809 << Indent2 << "return \"" << P.InLanguage << "\";\n"
810 << Indent1 << "}\n\n";
811
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000812 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000813 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
814 << Indent1 << "}\n\n";
815}
816
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000817/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
818/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000819void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000820 O << Indent1 << "const char* OutputSuffix() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000821 << Indent2 << "return \"" << P.OutputSuffix << "\";\n"
822 << Indent1 << "}\n\n";
823}
824
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000825/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000826void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +0000827 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000828 << Indent2 << "return \"" << P.Name << "\";\n"
829 << Indent1 << "}\n\n";
830}
831
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000832/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
833/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000834void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
835 O << Indent1 << "bool IsJoin() const {\n";
836 if (P.isJoin())
837 O << Indent2 << "return true;\n";
838 else
839 O << Indent2 << "return false;\n";
840 O << Indent1 << "}\n\n";
841}
842
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000843/// EmitToolClassDefinition - Emit a Tool class definition.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000844void EmitToolClassDefinition (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +0000845
846 if(P.Name == "root")
847 return;
848
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000849 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +0000850 O << "class " << P.Name << " : public ";
851 if (P.isJoin())
852 O << "JoinTool";
853 else
854 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +0000855 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000856
857 EmitNameMethod(P, O);
858 EmitInOutLanguageMethods(P, O);
859 EmitOutputSuffixMethod(P, O);
860 EmitIsJoinMethod(P, O);
861 EmitGenerateActionMethods(P, O);
862 EmitIsLastMethod(P, O);
863
864 // Close class definition
865 O << "};\n\n";
866}
867
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000868/// EmitOptionDescriptions - Iterate over a list of option
869/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000870void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
871 std::ostream& O)
872{
873 // Emit static cl::Option variables
874 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
875 E = descs.end(); B!=E; ++B) {
876 const GlobalOptionDescription& val = B->second;
877
878 O << val.GenTypeDeclaration() << ' '
879 << val.GenVariableName()
880 << "(\"" << val.Name << '\"';
881
882 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
883 O << ", cl::Prefix";
884
885 if (val.isRequired()) {
886 switch (val.Type) {
887 case OptionType::PrefixList:
888 case OptionType::ParameterList:
889 O << ", cl::OneOrMore";
890 break;
891 default:
892 O << ", cl::Required";
893 }
894 }
895
896 O << ", cl::desc(\"" << val.Help << "\"));\n";
897 }
898
899 if (descs.HasSink)
900 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
901
902 O << '\n';
903}
904
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000905/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000906void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
907{
908 // Get the relevant field out of RecordKeeper
909 Record* LangMapRecord = Records.getDef("LanguageMap");
910 if (!LangMapRecord)
911 throw std::string("Language map definition not found!");
912
913 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
914 if (!LangsToSuffixesList)
915 throw std::string("Error in the language map definition!");
916
917 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +0000918 O << "void llvmc::PopulateLanguageMap(LanguageMap& language_map) {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000919
920 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
921 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
922
923 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
924 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
925
926 for (unsigned i = 0; i < Suffixes->size(); ++i)
927 O << Indent1 << "language_map[\""
928 << InitPtrToString(Suffixes->getElement(i))
929 << "\"] = \"" << Lang << "\";\n";
930 }
931
932 O << "}\n\n";
933}
934
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000935/// FillInToolToLang - Fills in two tables that map tool names to
936/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000937void FillInToolToLang (const ToolPropertiesList& TPList,
938 StringMap<std::string>& ToolToInLang,
939 StringMap<std::string>& ToolToOutLang) {
940 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
941 B != E; ++B) {
942 const ToolProperties& P = *(*B);
943 ToolToInLang[P.Name] = P.InLanguage;
944 ToolToOutLang[P.Name] = P.OutLanguage;
945 }
946}
947
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000948/// TypecheckGraph - Check that names for output and input languages
949/// on all edges do match.
Mikhail Glushenkov761958d2008-05-06 16:36:50 +0000950// TOFIX: check for cycles.
951// TOFIX: check for multiple default edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000952void TypecheckGraph (Record* CompilationGraph,
953 const ToolPropertiesList& TPList) {
954 StringMap<std::string> ToolToInLang;
955 StringMap<std::string> ToolToOutLang;
956
957 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
958 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
959 StringMap<std::string>::iterator IAE = ToolToInLang.end();
960 StringMap<std::string>::iterator IBE = ToolToOutLang.end();
961
962 for (unsigned i = 0; i < edges->size(); ++i) {
963 Record* Edge = edges->getElementAsRecord(i);
964 Record* A = Edge->getValueAsDef("a");
965 Record* B = Edge->getValueAsDef("b");
966 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
967 StringMap<std::string>::iterator IB = ToolToInLang.find(B->getName());
968 if(IA == IAE)
969 throw A->getName() + ": no such tool!";
970 if(IB == IBE)
971 throw B->getName() + ": no such tool!";
972 if(A->getName() != "root" && IA->second != IB->second)
973 throw "Edge " + A->getName() + "->" + B->getName()
974 + ": output->input language mismatch";
Mikhail Glushenkov761958d2008-05-06 16:36:50 +0000975 if(B->getName() == "root")
976 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +0000977 }
978}
979
Mikhail Glushenkovdedba642008-05-30 06:08:50 +0000980/// EmitCaseTest1Arg - Helper function used by
981/// EmitCaseConstructHandler.
982bool EmitCaseTest1Arg(const std::string& TestName,
983 const DagInit& d,
984 const GlobalOptionDescriptions& OptDescs,
985 std::ostream& O) {
986 checkNumberOfArguments(&d, 1);
987 const std::string& OptName = InitPtrToString(d.getArg(0));
988 if (TestName == "switch_on") {
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000989 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
990 if (OptDesc.Type != OptionType::Switch)
991 throw OptName + ": incorrect option type!";
992 O << OptDesc.GenVariableName();
993 return true;
Mikhail Glushenkovdedba642008-05-30 06:08:50 +0000994 } else if (TestName == "input_languages_contain") {
Mikhail Glushenkovd6228882008-05-06 18:15:12 +0000995 O << "InLangs.count(\"" << OptName << "\") != 0";
996 return true;
997 }
998
999 return false;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001000}
1001
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001002/// EmitCaseTest2Args - Helper function used by
1003/// EmitCaseConstructHandler.
1004bool EmitCaseTest2Args(const std::string& TestName,
1005 const DagInit& d,
1006 const char* IndentLevel,
1007 const GlobalOptionDescriptions& OptDescs,
1008 std::ostream& O) {
1009 checkNumberOfArguments(&d, 2);
1010 const std::string& OptName = InitPtrToString(d.getArg(0));
1011 const std::string& OptArg = InitPtrToString(d.getArg(1));
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001012 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
1013
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001014 if (TestName == "parameter_equals") {
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001015 if (OptDesc.Type != OptionType::Parameter
1016 && OptDesc.Type != OptionType::Prefix)
1017 throw OptName + ": incorrect option type!";
1018 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001019 return true;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001020 }
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001021 else if (TestName == "element_in_list") {
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001022 if (OptDesc.Type != OptionType::ParameterList
1023 && OptDesc.Type != OptionType::PrefixList)
1024 throw OptName + ": incorrect option type!";
1025 const std::string& VarName = OptDesc.GenVariableName();
1026 O << "std::find(" << VarName << ".begin(),\n"
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001027 << IndentLevel << Indent1 << VarName << ".end(), \""
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001028 << OptArg << "\") != " << VarName << ".end()";
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001029 return true;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001030 }
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001031
1032 return false;
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001033}
1034
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001035// Forward declaration.
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001036// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
1037void EmitCaseTest(const DagInit& d, const char* IndentLevel,
1038 const GlobalOptionDescriptions& OptDescs,
1039 std::ostream& O);
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001040
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001041/// EmitLogicalOperationTest - Helper function used by
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001042/// EmitCaseConstructHandler.
1043void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
1044 const char* IndentLevel,
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001045 const GlobalOptionDescriptions& OptDescs,
1046 std::ostream& O) {
1047 O << '(';
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001048 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
1049 const DagInit& InnerTest = InitPtrToDagInitRef(d.getArg(j));
1050 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001051 if (j != NumArgs - 1)
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001052 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001053 else
1054 O << ')';
1055 }
Mikhail Glushenkov3f6743e2008-05-06 17:22:47 +00001056}
1057
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001058/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
1059void EmitCaseTest(const DagInit& d, const char* IndentLevel,
1060 const GlobalOptionDescriptions& OptDescs,
1061 std::ostream& O) {
1062 const std::string& TestName = d.getOperator()->getAsString();
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001063
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001064 if (TestName == "and")
1065 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
1066 else if (TestName == "or")
1067 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
1068 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001069 return;
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001070 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001071 return;
1072 else
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001073 throw TestName + ": unknown edge property!";
1074}
1075
1076// Emit code that handles the 'case' construct.
1077// Takes a function object that should emit code for every case clause.
1078template <typename F>
1079void EmitCaseConstructHandler(DagInit* d, const char* IndentLevel,
1080 const F& Callback,
1081 const GlobalOptionDescriptions& OptDescs,
1082 std::ostream& O) {
1083 assert(d->getOperator()->getAsString() == "case");
1084
1085 for (DagInit::arg_iterator B = d->arg_begin(), E = d->arg_end();
1086 B != E; ++B) {
1087 const DagInit& Test = InitPtrToDagInitRef(*B);
1088 O << IndentLevel << "if (";
1089 EmitCaseTest(Test, IndentLevel, OptDescs, O);
1090 O << ") {\n";
1091
1092 ++B;
1093 if (B == E)
1094 throw "Case construct handler: no corresponding action "
1095 "found for the test " + Test.getAsString() + '!';
1096
1097 const DagInit& Action = InitPtrToDagInitRef(*B);
1098 Callback(IndentLevel, Action, O);
1099 O << IndentLevel << "}\n";
1100 }
1101}
1102
1103// Helper function passed to EmitCaseConstructHandler by EmitEdgeClass.
1104void IncDecWeight(const char* IndentLevel,
1105 const DagInit& d, std::ostream& O) {
1106 const std::string& OpName = d.getOperator()->getAsString();
1107
1108 if (OpName == "inc_weight")
1109 O << IndentLevel << Indent1 << "ret += ";
1110 else if (OpName == "dec_weight")
1111 O << IndentLevel << Indent1 << "ret -= ";
1112 else
1113 throw "Unknown operator in edge properties list: " + OpName + '!';
1114
1115 if (d.getNumArgs() > 0)
1116 O << InitPtrToInt(d.getArg(0)) << ";\n";
1117 else
1118 O << "2;\n";
1119
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001120}
1121
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001122/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001123void EmitEdgeClass(unsigned N, const std::string& Target,
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001124 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001125 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001126
1127 // Class constructor.
1128 O << "class Edge" << N << ": public Edge {\n"
1129 << "public:\n"
1130 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1131 << "\") {}\n\n"
1132
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001133 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001134 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001135 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001136
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001137 // Handle the 'case' construct.
1138 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001139
1140 O << Indent2 << "return ret;\n"
1141 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001142}
1143
1144// Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001145void EmitEdgeClasses (Record* CompilationGraph,
1146 const GlobalOptionDescriptions& OptDescs,
1147 std::ostream& O) {
1148 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1149
1150 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001151 Record* Edge = edges->getElementAsRecord(i);
1152 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001153 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001154
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001155 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001156 continue;
1157
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001158 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001159 }
1160}
1161
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001162/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1163/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001164void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001165 std::ostream& O)
1166{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001167 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001168
1169 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001170 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001171 << Indent1 << "PopulateLanguageMap(G.ExtsToLangs);\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001172
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001173 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001174
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001175 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1176 if (Tools.empty())
1177 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001178
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001179 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1180 const std::string& Name = (*B)->getName();
1181 if(Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001182 O << Indent1 << "G.insertNode(new "
1183 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001184 }
1185
1186 O << '\n';
1187
1188 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001189 for (unsigned i = 0; i < edges->size(); ++i) {
1190 Record* Edge = edges->getElementAsRecord(i);
1191 Record* A = Edge->getValueAsDef("a");
1192 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001193 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001194
1195 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1196
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001197 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001198 O << "new SimpleEdge(\"" << B->getName() << "\")";
1199 else
1200 O << "new Edge" << i << "()";
1201
1202 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001203 }
1204
1205 O << "}\n\n";
1206}
1207
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001208
1209// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001210}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001211
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001212/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001213void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001214
1215 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001216 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001217
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001218 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001219 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1220 if (Tools.empty())
1221 throw std::string("No tool definitions found!");
1222
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001223 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001224 ToolPropertiesList tool_props;
1225 GlobalOptionDescriptions opt_descs;
1226 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1227
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001228 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001229 EmitOptionDescriptions(opt_descs, O);
1230
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001231 // Emit PopulateLanguageMap() function
1232 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001233 EmitPopulateLanguageMap(Records, O);
1234
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001235 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001236 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1237 E = tool_props.end(); B!=E; ++B)
1238 EmitToolClassDefinition(*(*B), O);
1239
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001240 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1241 if (!CompilationGraphRecord)
1242 throw std::string("Compilation graph description not found!");
1243
1244 // Typecheck the compilation graph.
1245 TypecheckGraph(CompilationGraphRecord, tool_props);
1246
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001247 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001248 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001249
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001250 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001251 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001252
1253 // EOF
1254}