blob: b978c2945010d0e26751c5b1e808619ff2165515 [file] [log] [blame]
Mikhail Glushenkov2d3327f2008-05-30 06:20:54 +00001//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Mikhail Glushenkov34307a92008-05-06 18:08:59 +000010// This tablegen backend is responsible for emitting LLVMC configuration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkov41405722008-05-06 18:09:29 +000014#include "LLVMCConfigurationEmitter.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000015#include "Record.h"
16
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000021#include "llvm/ADT/StringSet.h"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000022#include "llvm/Support/Streams.h"
23
24#include <algorithm>
25#include <cassert>
26#include <functional>
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +000027#include <stdexcept>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000028#include <string>
29
30using namespace llvm;
31
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +000032namespace {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000033
34//===----------------------------------------------------------------------===//
35/// Typedefs
36
37typedef std::vector<Record*> RecordVector;
38typedef std::vector<std::string> StrVector;
39
40//===----------------------------------------------------------------------===//
41/// Constants
42
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000043// Indentation strings.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000044const char * Indent1 = " ";
45const char * Indent2 = " ";
46const char * Indent3 = " ";
47const char * Indent4 = " ";
48
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000049// Default help string.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
51
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000052// Name for the "sink" option.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000053const char * SinkOptionName = "AutoGeneratedSinkOption";
54
55//===----------------------------------------------------------------------===//
56/// Helper functions
57
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000058int InitPtrToInt(const Init* ptr) {
59 const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000060 return val.getValue();
61}
62
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +000063const std::string& InitPtrToString(const Init* ptr) {
64 const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
65 return val.getValue();
66}
67
68const ListInit& InitPtrToList(const Init* ptr) {
69 const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
70 return val;
71}
72
73const DagInit& InitPtrToDag(const Init* ptr) {
Mikhail Glushenkov35576b02008-05-30 06:10:19 +000074 const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +000075 return val;
76}
77
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +000078// checkNumberOfArguments - Ensure that the number of args in d is
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000079// less than or equal to min_arguments, otherwise throw an exception.
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000080void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
81 if (d->getNumArgs() < min_arguments)
82 throw "Property " + d->getOperator()->getAsString()
83 + " has too few arguments!";
84}
85
Mikhail Glushenkovdedba642008-05-30 06:08:50 +000086// isDagEmpty - is this DAG marked with an empty marker?
87bool isDagEmpty (const DagInit* d) {
88 return d->getOperator()->getAsString() == "empty";
89}
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +000090
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000091//===----------------------------------------------------------------------===//
92/// Back-end specific code
93
94// A command-line option can have one of the following types:
95//
96// Switch - a simple switch w/o arguments, e.g. -O2
97//
98// Parameter - an option that takes one(and only one) argument, e.g. -o file,
99// --output=file
100//
101// ParameterList - same as Parameter, but more than one occurence
102// of the option is allowed, e.g. -lm -lpthread
103//
104// Prefix - argument is everything after the prefix,
105// e.g. -Wa,-foo,-bar, -DNAME=VALUE
106//
107// PrefixList - same as Prefix, but more than one option occurence is
108// allowed
109
110namespace OptionType {
111 enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList};
112}
113
114bool IsListOptionType (OptionType::OptionType t) {
115 return (t == OptionType::ParameterList || t == OptionType::PrefixList);
116}
117
118// Code duplication here is necessary because one option can affect
119// several tools and those tools may have different actions associated
120// with this option. GlobalOptionDescriptions are used to generate
121// the option registration code, while ToolOptionDescriptions are used
122// to generate tool-specific code.
123
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000124/// OptionDescription - Base class for option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000125struct OptionDescription {
126 OptionType::OptionType Type;
127 std::string Name;
128
129 OptionDescription(OptionType::OptionType t = OptionType::Switch,
130 const std::string& n = "")
131 : Type(t), Name(n)
132 {}
133
134 const char* GenTypeDeclaration() const {
135 switch (Type) {
136 case OptionType::PrefixList:
137 case OptionType::ParameterList:
138 return "cl::list<std::string>";
139 case OptionType::Switch:
140 return "cl::opt<bool>";
141 case OptionType::Parameter:
142 case OptionType::Prefix:
143 default:
144 return "cl::opt<std::string>";
145 }
146 }
147
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000148 // Escape commas and other symbols not allowed in the C++ variable
149 // names. Makes it possible to use options with names like "Wa,"
150 // (useful for prefix options).
151 std::string EscapeVariableName(const std::string& Var) const {
152 std::string ret;
153 for (unsigned i = 0; i != Var.size(); ++i) {
154 if (Var[i] == ',') {
155 ret += "_comma_";
156 }
157 else {
158 ret.push_back(Var[i]);
159 }
160 }
161 return ret;
162 }
163
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000164 std::string GenVariableName() const {
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000165 const std::string& EscapedName = EscapeVariableName(Name);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000166 switch (Type) {
167 case OptionType::Switch:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000168 return "AutoGeneratedSwitch" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000169 case OptionType::Prefix:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000170 return "AutoGeneratedPrefix" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000171 case OptionType::PrefixList:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000172 return "AutoGeneratedPrefixList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000173 case OptionType::Parameter:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000174 return "AutoGeneratedParameter" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000175 case OptionType::ParameterList:
176 default:
Mikhail Glushenkov4019e952008-05-12 16:33:06 +0000177 return "AutoGeneratedParameterList" + EscapedName;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000178 }
179 }
180
181};
182
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000183// Global option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000184
185namespace GlobalOptionDescriptionFlags {
186 enum GlobalOptionDescriptionFlags { Required = 0x1 };
187}
188
189struct GlobalOptionDescription : public OptionDescription {
190 std::string Help;
191 unsigned Flags;
192
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000193 // We need to provide a default constructor because
194 // StringMap can only store DefaultConstructible objects.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000195 GlobalOptionDescription() : OptionDescription(), Flags(0)
196 {}
197
198 GlobalOptionDescription (OptionType::OptionType t, const std::string& n)
199 : OptionDescription(t, n), Help(DefaultHelpString), Flags(0)
200 {}
201
202 bool isRequired() const {
203 return Flags & GlobalOptionDescriptionFlags::Required;
204 }
205 void setRequired() {
206 Flags |= GlobalOptionDescriptionFlags::Required;
207 }
208
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000209 /// Merge - Merge two option descriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000210 void Merge (const GlobalOptionDescription& other)
211 {
212 if (other.Type != Type)
213 throw "Conflicting definitions for the option " + Name + "!";
214
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000215 if (Help == DefaultHelpString)
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000216 Help = other.Help;
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000217 else if (other.Help != DefaultHelpString) {
218 llvm::cerr << "Warning: more than one help string defined for option "
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000219 + Name + "\n";
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000220 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000221
222 Flags |= other.Flags;
223 }
224};
225
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000226/// GlobalOptionDescriptions - A GlobalOptionDescription array
227/// together with some flags affecting generation of option
228/// declarations.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000229struct GlobalOptionDescriptions {
230 typedef StringMap<GlobalOptionDescription> container_type;
231 typedef container_type::const_iterator const_iterator;
232
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000233 /// Descriptions - A list of GlobalOptionDescriptions.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000234 container_type Descriptions;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000235 /// HasSink - Should the emitter generate a "cl::sink" option?
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000236 bool HasSink;
237
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000238 const GlobalOptionDescription& FindOption(const std::string& OptName) const {
239 const_iterator I = Descriptions.find(OptName);
240 if (I != Descriptions.end())
241 return I->second;
242 else
243 throw OptName + ": no such option!";
244 }
245
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000246 // Support for STL-style iteration
247 const_iterator begin() const { return Descriptions.begin(); }
248 const_iterator end() const { return Descriptions.end(); }
249};
250
251
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000252// Tool-local option description.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000253
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000254// Properties without arguments are implemented as flags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000255namespace ToolOptionDescriptionFlags {
256 enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
257 Forward = 0x2, UnpackValues = 0x4};
258}
259namespace OptionPropertyType {
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000260 enum OptionPropertyType { AppendCmd, OutputSuffix };
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000261}
262
263typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
264OptionProperty;
265typedef SmallVector<OptionProperty, 4> OptionPropertyList;
266
267struct ToolOptionDescription : public OptionDescription {
268 unsigned Flags;
269 OptionPropertyList Props;
270
271 // StringMap can only store DefaultConstructible objects
Mikhail Glushenkov18cbe892008-03-27 09:53:47 +0000272 ToolOptionDescription() : OptionDescription(), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000273
274 ToolOptionDescription (OptionType::OptionType t, const std::string& n)
275 : OptionDescription(t, n)
276 {}
277
278 // Various boolean properties
279 bool isStopCompilation() const {
280 return Flags & ToolOptionDescriptionFlags::StopCompilation;
281 }
282 void setStopCompilation() {
283 Flags |= ToolOptionDescriptionFlags::StopCompilation;
284 }
285
286 bool isForward() const {
287 return Flags & ToolOptionDescriptionFlags::Forward;
288 }
289 void setForward() {
290 Flags |= ToolOptionDescriptionFlags::Forward;
291 }
292
293 bool isUnpackValues() const {
294 return Flags & ToolOptionDescriptionFlags::UnpackValues;
295 }
296 void setUnpackValues() {
297 Flags |= ToolOptionDescriptionFlags::UnpackValues;
298 }
299
300 void AddProperty (OptionPropertyType::OptionPropertyType t,
301 const std::string& val)
302 {
303 Props.push_back(std::make_pair(t, val));
304 }
305};
306
307typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
308
309// Tool information record
310
311namespace ToolFlags {
312 enum ToolFlags { Join = 0x1, Sink = 0x2 };
313}
314
315struct ToolProperties : public RefCountedBase<ToolProperties> {
316 std::string Name;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000317 Init* CmdLine;
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000318 StrVector InLanguage;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000319 std::string OutLanguage;
320 std::string OutputSuffix;
321 unsigned Flags;
322 ToolOptionDescriptions OptDescs;
323
324 // Various boolean properties
325 void setSink() { Flags |= ToolFlags::Sink; }
326 bool isSink() const { return Flags & ToolFlags::Sink; }
327 void setJoin() { Flags |= ToolFlags::Join; }
328 bool isJoin() const { return Flags & ToolFlags::Join; }
329
330 // Default ctor here is needed because StringMap can only store
331 // DefaultConstructible objects
Mikhail Glushenkov434816d2008-05-06 18:13:00 +0000332 ToolProperties() : Flags(0) {}
333 ToolProperties (const std::string& n) : Name(n), Flags(0) {}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000334};
335
336
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000337/// ToolPropertiesList - A list of Tool information records
338/// IntrusiveRefCntPtrs are used here because StringMap has no copy
339/// constructor (and we want to avoid copying ToolProperties anyway).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000340typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
341
342
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000343/// CollectProperties - Function object for iterating over a list of
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000344/// tool property records.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000345class CollectProperties {
346private:
347
348 /// Implementation details
349
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000350 /// PropertyHandler - a function that extracts information
351 /// about a given tool property from its DAG representation
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000352 typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000353
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000354 /// PropertyHandlerMap - A map from property names to property
355 /// handlers.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000356 typedef StringMap<PropertyHandler> PropertyHandlerMap;
357
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000358 /// OptionPropertyHandler - a function that extracts information
359 /// about a given option property from its DAG representation.
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000360 typedef void (CollectProperties::* OptionPropertyHandler)
361 (const DagInit*, GlobalOptionDescription &);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000362
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000363 /// OptionPropertyHandlerMap - A map from option property names to
364 /// option property handlers
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000365 typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
366
367 // Static maps from strings to CollectProperties methods("handlers")
368 static PropertyHandlerMap propertyHandlers_;
369 static OptionPropertyHandlerMap optionPropertyHandlers_;
370 static bool staticMembersInitialized_;
371
372
373 /// This is where the information is stored
374
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000375 /// toolProps_ - Properties of the current Tool.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000376 ToolProperties& toolProps_;
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000377 /// optDescs_ - OptionDescriptions table (used to register options
378 /// globally).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000379 GlobalOptionDescriptions& optDescs_;
380
381public:
382
383 explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
384 : toolProps_(p), optDescs_(d)
385 {
386 if (!staticMembersInitialized_) {
387 // Init tool property handlers
388 propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
389 propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
390 propertyHandlers_["join"] = &CollectProperties::onJoin;
391 propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
392 propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
393 propertyHandlers_["parameter_option"]
394 = &CollectProperties::onParameter;
395 propertyHandlers_["parameter_list_option"] =
396 &CollectProperties::onParameterList;
397 propertyHandlers_["prefix_option"] = &CollectProperties::onPrefix;
398 propertyHandlers_["prefix_list_option"] =
399 &CollectProperties::onPrefixList;
400 propertyHandlers_["sink"] = &CollectProperties::onSink;
401 propertyHandlers_["switch_option"] = &CollectProperties::onSwitch;
402
403 // Init option property handlers
404 optionPropertyHandlers_["append_cmd"] = &CollectProperties::onAppendCmd;
405 optionPropertyHandlers_["forward"] = &CollectProperties::onForward;
406 optionPropertyHandlers_["help"] = &CollectProperties::onHelp;
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000407 optionPropertyHandlers_["output_suffix"] =
408 &CollectProperties::onOutputSuffixOptionProp;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000409 optionPropertyHandlers_["required"] = &CollectProperties::onRequired;
410 optionPropertyHandlers_["stop_compilation"] =
411 &CollectProperties::onStopCompilation;
412 optionPropertyHandlers_["unpack_values"] =
413 &CollectProperties::onUnpackValues;
414
415 staticMembersInitialized_ = true;
416 }
417 }
418
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000419 /// operator() - Gets called for every tool property; Just forwards
420 /// to the corresponding property handler.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000421 void operator() (Init* i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000422 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkova5922cc2008-05-06 17:22:03 +0000423 const std::string& property_name = d.getOperator()->getAsString();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000424 PropertyHandlerMap::iterator method
425 = propertyHandlers_.find(property_name);
426
427 if (method != propertyHandlers_.end()) {
428 PropertyHandler h = method->second;
429 (this->*h)(&d);
430 }
431 else {
432 throw "Unknown tool property: " + property_name + "!";
433 }
434 }
435
436private:
437
438 /// Property handlers --
439 /// Functions that extract information about tool properties from
440 /// DAG representation.
441
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000442 void onCmdLine (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000443 checkNumberOfArguments(d, 1);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000444 toolProps_.CmdLine = d->getArg(0);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000445 }
446
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000447 void onInLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000448 checkNumberOfArguments(d, 1);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000449 Init* arg = d->getArg(0);
450
451 // Find out the argument's type.
452 if (typeid(*arg) == typeid(StringInit)) {
453 // It's a string.
454 toolProps_.InLanguage.push_back(InitPtrToString(arg));
455 }
456 else {
457 // It's a list.
458 const ListInit& lst = InitPtrToList(arg);
459 StrVector& out = toolProps_.InLanguage;
460
461 // Copy strings to the output vector.
462 for (ListInit::const_iterator B = lst.begin(), E = lst.end();
463 B != E; ++B) {
464 out.push_back(InitPtrToString(*B));
465 }
466
467 // Remove duplicates.
468 std::sort(out.begin(), out.end());
469 StrVector::iterator newE = std::unique(out.begin(), out.end());
470 out.erase(newE, out.end());
471 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000472 }
473
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000474 void onJoin (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000475 checkNumberOfArguments(d, 0);
476 toolProps_.setJoin();
477 }
478
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000479 void onOutLanguage (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000480 checkNumberOfArguments(d, 1);
481 toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
482 }
483
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000484 void onOutputSuffix (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000485 checkNumberOfArguments(d, 1);
486 toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
487 }
488
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000489 void onSink (const DagInit* d) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000490 checkNumberOfArguments(d, 0);
491 optDescs_.HasSink = true;
492 toolProps_.setSink();
493 }
494
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000495 void onSwitch (const DagInit* d) {
496 addOption(d, OptionType::Switch);
497 }
498
499 void onParameter (const DagInit* d) {
500 addOption(d, OptionType::Parameter);
501 }
502
503 void onParameterList (const DagInit* d) {
504 addOption(d, OptionType::ParameterList);
505 }
506
507 void onPrefix (const DagInit* d) {
508 addOption(d, OptionType::Prefix);
509 }
510
511 void onPrefixList (const DagInit* d) {
512 addOption(d, OptionType::PrefixList);
513 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000514
515 /// Option property handlers --
516 /// Methods that handle properties that are common for all types of
517 /// options (like append_cmd, stop_compilation)
518
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000519 void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000520 checkNumberOfArguments(d, 1);
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000521 const std::string& cmd = InitPtrToString(d->getArg(0));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000522
523 toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd);
524 }
525
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +0000526 void onOutputSuffixOptionProp (const DagInit* d, GlobalOptionDescription& o) {
527 checkNumberOfArguments(d, 1);
528 const std::string& suf = InitPtrToString(d->getArg(0));
529
530 if (toolProps_.OptDescs[o.Name].Type != OptionType::Switch)
531 throw "Option " + o.Name
532 + " can't have 'output_suffix' property since it isn't a switch!";
533
534 toolProps_.OptDescs[o.Name].AddProperty
535 (OptionPropertyType::OutputSuffix, suf);
536 }
537
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000538 void onForward (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000539 checkNumberOfArguments(d, 0);
540 toolProps_.OptDescs[o.Name].setForward();
541 }
542
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000543 void onHelp (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000544 checkNumberOfArguments(d, 1);
545 const std::string& help_message = InitPtrToString(d->getArg(0));
546
547 o.Help = help_message;
548 }
549
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000550 void onRequired (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000551 checkNumberOfArguments(d, 0);
552 o.setRequired();
553 }
554
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000555 void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000556 checkNumberOfArguments(d, 0);
557 if (o.Type != OptionType::Switch)
558 throw std::string("Only options of type Switch can stop compilation!");
559 toolProps_.OptDescs[o.Name].setStopCompilation();
560 }
561
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000562 void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000563 checkNumberOfArguments(d, 0);
564 toolProps_.OptDescs[o.Name].setUnpackValues();
565 }
566
567 /// Helper functions
568
569 // Add an option of type t
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000570 void addOption (const DagInit* d, OptionType::OptionType t) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000571 checkNumberOfArguments(d, 2);
572 const std::string& name = InitPtrToString(d->getArg(0));
573
574 GlobalOptionDescription o(t, name);
575 toolProps_.OptDescs[name].Type = t;
576 toolProps_.OptDescs[name].Name = name;
577 processOptionProperties(d, o);
578 insertDescription(o);
579 }
580
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000581 // Insert new GlobalOptionDescription into GlobalOptionDescriptions list
582 void insertDescription (const GlobalOptionDescription& o)
583 {
584 if (optDescs_.Descriptions.count(o.Name)) {
585 GlobalOptionDescription& D = optDescs_.Descriptions[o.Name];
586 D.Merge(o);
587 }
588 else {
589 optDescs_.Descriptions[o.Name] = o;
590 }
591 }
592
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000593 /// processOptionProperties - Go through the list of option
594 /// properties and call a corresponding handler for each.
595 ///
596 /// Parameters:
597 /// name - option name
598 /// d - option property list
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000599 void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000600 // First argument is option name
601 checkNumberOfArguments(d, 2);
602
603 for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) {
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +0000604 const DagInit& option_property
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000605 = InitPtrToDag(d->getArg(B));
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000606 const std::string& option_property_name
607 = option_property.getOperator()->getAsString();
608 OptionPropertyHandlerMap::iterator method
609 = optionPropertyHandlers_.find(option_property_name);
610
611 if (method != optionPropertyHandlers_.end()) {
612 OptionPropertyHandler h = method->second;
613 (this->*h)(&option_property, o);
614 }
615 else {
616 throw "Unknown option property: " + option_property_name + "!";
617 }
618 }
619 }
620};
621
622// Static members of CollectProperties
623CollectProperties::PropertyHandlerMap
624CollectProperties::propertyHandlers_;
625
626CollectProperties::OptionPropertyHandlerMap
627CollectProperties::optionPropertyHandlers_;
628
629bool CollectProperties::staticMembersInitialized_ = false;
630
631
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +0000632/// CollectToolProperties - Gather information from the parsed
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000633/// TableGen data (basically a wrapper for the CollectProperties
634/// function object).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000635void CollectToolProperties (RecordVector::const_iterator B,
636 RecordVector::const_iterator E,
637 ToolPropertiesList& TPList,
638 GlobalOptionDescriptions& OptDescs)
639{
640 // Iterate over a properties list of every Tool definition
641 for (;B!=E;++B) {
642 RecordVector::value_type T = *B;
643 ListInit* PropList = T->getValueAsListInit("properties");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000644
645 IntrusiveRefCntPtr<ToolProperties>
646 ToolProps(new ToolProperties(T->getName()));
647
648 std::for_each(PropList->begin(), PropList->end(),
649 CollectProperties(*ToolProps, OptDescs));
650 TPList.push_back(ToolProps);
651 }
652}
653
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000654/// EmitCaseTest1Arg - Helper function used by
655/// EmitCaseConstructHandler.
656bool EmitCaseTest1Arg(const std::string& TestName,
657 const DagInit& d,
658 const GlobalOptionDescriptions& OptDescs,
659 std::ostream& O) {
660 checkNumberOfArguments(&d, 1);
661 const std::string& OptName = InitPtrToString(d.getArg(0));
662 if (TestName == "switch_on") {
663 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
664 if (OptDesc.Type != OptionType::Switch)
665 throw OptName + ": incorrect option type!";
666 O << OptDesc.GenVariableName();
667 return true;
668 } else if (TestName == "input_languages_contain") {
669 O << "InLangs.count(\"" << OptName << "\") != 0";
670 return true;
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +0000671 } else if (TestName == "in_language") {
672 // Works only for cmd_line!
673 O << "GetLanguage(inFile) == \"" << OptName << '\"';
674 return true;
675 } else if (TestName == "not_empty") {
676 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
677 if (OptDesc.Type == OptionType::Switch)
678 throw OptName + ": incorrect option type!";
679 O << '!' << OptDesc.GenVariableName() << ".empty()";
680 return true;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000681 }
682
683 return false;
684}
685
686/// EmitCaseTest2Args - Helper function used by
687/// EmitCaseConstructHandler.
688bool EmitCaseTest2Args(const std::string& TestName,
689 const DagInit& d,
690 const char* IndentLevel,
691 const GlobalOptionDescriptions& OptDescs,
692 std::ostream& O) {
693 checkNumberOfArguments(&d, 2);
694 const std::string& OptName = InitPtrToString(d.getArg(0));
695 const std::string& OptArg = InitPtrToString(d.getArg(1));
696 const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
697
698 if (TestName == "parameter_equals") {
699 if (OptDesc.Type != OptionType::Parameter
700 && OptDesc.Type != OptionType::Prefix)
701 throw OptName + ": incorrect option type!";
702 O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
703 return true;
704 }
705 else if (TestName == "element_in_list") {
706 if (OptDesc.Type != OptionType::ParameterList
707 && OptDesc.Type != OptionType::PrefixList)
708 throw OptName + ": incorrect option type!";
709 const std::string& VarName = OptDesc.GenVariableName();
710 O << "std::find(" << VarName << ".begin(),\n"
711 << IndentLevel << Indent1 << VarName << ".end(), \""
712 << OptArg << "\") != " << VarName << ".end()";
713 return true;
714 }
715
716 return false;
717}
718
719// Forward declaration.
720// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
721void EmitCaseTest(const DagInit& d, const char* IndentLevel,
722 const GlobalOptionDescriptions& OptDescs,
723 std::ostream& O);
724
725/// EmitLogicalOperationTest - Helper function used by
726/// EmitCaseConstructHandler.
727void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
728 const char* IndentLevel,
729 const GlobalOptionDescriptions& OptDescs,
730 std::ostream& O) {
731 O << '(';
732 for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000733 const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000734 EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
735 if (j != NumArgs - 1)
736 O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
737 else
738 O << ')';
739 }
740}
741
742/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
743void EmitCaseTest(const DagInit& d, const char* IndentLevel,
744 const GlobalOptionDescriptions& OptDescs,
745 std::ostream& O) {
746 const std::string& TestName = d.getOperator()->getAsString();
747
748 if (TestName == "and")
749 EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
750 else if (TestName == "or")
751 EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
752 else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
753 return;
754 else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
755 return;
756 else
757 throw TestName + ": unknown edge property!";
758}
759
760// Emit code that handles the 'case' construct.
761// Takes a function object that should emit code for every case clause.
762// Callback's type is
763// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
764template <typename F>
765void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +0000766 const F& Callback,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000767 const GlobalOptionDescriptions& OptDescs,
768 std::ostream& O) {
769 assert(d->getOperator()->getAsString() == "case");
770
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000771 unsigned numArgs = d->getNumArgs();
772 if (d->getNumArgs() < 2)
773 throw "There should be at least one clause in the 'case' expression:\n"
774 + d->getAsString();
775
776 for (unsigned i = 0; i != numArgs; ++i) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +0000777 const DagInit& Test = InitPtrToDag(d->getArg(i));
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000778
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000779 // Emit the test.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000780 if (Test.getOperator()->getAsString() == "default") {
781 if (i+2 != numArgs)
782 throw std::string("The 'default' clause should be the last in the"
783 "'case' construct!");
784 O << IndentLevel << "else {\n";
785 }
786 else {
787 O << IndentLevel << "if (";
788 EmitCaseTest(Test, IndentLevel, OptDescs, O);
789 O << ") {\n";
790 }
791
Mikhail Glushenkov31681512008-05-30 06:15:47 +0000792 // Emit the corresponding statement.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000793 ++i;
794 if (i == numArgs)
795 throw "Case construct handler: no corresponding action "
796 "found for the test " + Test.getAsString() + '!';
797
798 Callback(d->getArg(i), IndentLevel, O);
799 O << IndentLevel << "}\n";
800 }
801}
802
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000803/// EmitForwardOptionPropertyHandlingCode - Helper function used to
804/// implement EmitOptionPropertyHandlingCode(). Emits code for
805/// handling the (forward) option property.
806void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
807 std::ostream& O) {
808 switch (D.Type) {
809 case OptionType::Switch:
810 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
811 break;
812 case OptionType::Parameter:
813 O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n";
814 O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
815 break;
816 case OptionType::Prefix:
817 O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + "
818 << D.GenVariableName() << ");\n";
819 break;
820 case OptionType::PrefixList:
821 O << Indent3 << "for (" << D.GenTypeDeclaration()
822 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
823 << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
824 << Indent4 << "vec.push_back(\"-" << D.Name << "\" + "
825 << "*B);\n";
826 break;
827 case OptionType::ParameterList:
828 O << Indent3 << "for (" << D.GenTypeDeclaration()
829 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
830 << Indent3 << "E = " << D.GenVariableName()
831 << ".end() ; B != E; ++B) {\n"
832 << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n"
833 << Indent4 << "vec.push_back(*B);\n"
834 << Indent3 << "}\n";
835 break;
836 }
837}
838
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000839// ToolOptionHasInterestingProperties - A helper function used by
840// EmitOptionPropertyHandlingCode() that tells us whether we should
841// emit any property handling code at all.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000842bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
Mikhail Glushenkovea6ce492008-05-30 06:15:20 +0000843 bool ret = false;
844 for (OptionPropertyList::const_iterator B = D.Props.begin(),
845 E = D.Props.end(); B != E; ++B) {
846 const OptionProperty& OptProp = *B;
847 if (OptProp.first == OptionPropertyType::AppendCmd)
848 ret = true;
849 }
850 if (D.isForward() || D.isUnpackValues())
851 ret = true;
852 return ret;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000853}
854
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000855/// EmitOptionPropertyHandlingCode - Helper function used by
856/// EmitGenerateActionMethod(). Emits code that handles option
857/// properties.
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000858void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000859 std::ostream& O)
860{
Mikhail Glushenkov31f52152008-05-30 06:10:47 +0000861 if (!ToolOptionHasInterestingProperties(D))
862 return;
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000863 // Start of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000864 O << Indent2 << "if (";
865 if (D.Type == OptionType::Switch)
866 O << D.GenVariableName();
867 else
868 O << '!' << D.GenVariableName() << ".empty()";
869
870 O <<") {\n";
871
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000872 // Handle option properties that take an argument.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000873 for (OptionPropertyList::const_iterator B = D.Props.begin(),
874 E = D.Props.end(); B!=E; ++B) {
875 const OptionProperty& val = *B;
876
877 switch (val.first) {
878 // (append_cmd cmd) property
879 case OptionPropertyType::AppendCmd:
880 O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
881 break;
882 // Other properties with argument
883 default:
884 break;
885 }
886 }
887
888 // Handle flags
889
890 // (forward) property
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000891 if (D.isForward())
892 EmitForwardOptionPropertyHandlingCode(D, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000893
894 // (unpack_values) property
895 if (D.isUnpackValues()) {
896 if (IsListOptionType(D.Type)) {
897 O << Indent3 << "for (" << D.GenTypeDeclaration()
898 << "::iterator B = " << D.GenVariableName() << ".begin(),\n"
899 << Indent3 << "E = " << D.GenVariableName()
900 << ".end(); B != E; ++B)\n"
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000901 << Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000902 }
903 else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
Mikhail Glushenkov028f18e2008-05-06 18:13:45 +0000904 O << Indent3 << "llvm::SplitString("
905 << D.GenVariableName() << ", vec, \",\");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000906 }
907 else {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000908 throw std::string("Switches can't have unpack_values property!");
909 }
910 }
911
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +0000912 // End of the if-clause.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000913 O << Indent2 << "}\n";
914}
915
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000916/// SubstituteSpecialCommands - Perform string substitution for $CALL
917/// and $ENV. Helper function used by EmitCmdLineVecFill().
918std::string SubstituteSpecialCommands(const std::string& cmd) {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000919 size_t cparen = cmd.find(")");
920 std::string ret;
921
922 if (cmd.find("$CALL(") == 0) {
923 if (cmd.size() == 6)
924 throw std::string("$CALL invocation: empty argument list!");
925
926 ret += "hooks::";
927 ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
928 ret += "()";
929 }
930 else if (cmd.find("$ENV(") == 0) {
931 if (cmd.size() == 5)
932 throw std::string("$ENV invocation: empty argument list!");
933
934 ret += "std::getenv(\"";
935 ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
936 ret += "\")";
937 }
938 else {
939 throw "Unknown special command: " + cmd;
940 }
941
942 if (cmd.begin() + cparen + 1 != cmd.end()) {
943 ret += " + std::string(\"";
944 ret += (cmd.c_str() + cparen + 1);
945 ret += "\")";
946 }
947
948 return ret;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000949}
950
951/// EmitCmdLineVecFill - Emit code that fills in the command line
952/// vector. Helper function used by EmitGenerateActionMethod().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000953void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
954 bool Version, const char* IndentLevel,
955 std::ostream& O) {
956 StrVector StrVec;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000957 SplitString(InitPtrToString(CmdLine), StrVec);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000958 if (StrVec.empty())
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000959 throw "Tool " + ToolName + " has empty command line!";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000960
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000961 StrVector::const_iterator I = StrVec.begin();
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000962 ++I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000963 for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000964 const std::string& cmd = *I;
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000965 O << IndentLevel;
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000966 if (cmd.at(0) == '$') {
967 if (cmd == "$INFILE") {
968 if (Version)
969 O << "for (PathVector::const_iterator B = inFiles.begin()"
970 << ", E = inFiles.end();\n"
971 << IndentLevel << "B != E; ++B)\n"
972 << IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
973 else
974 O << "vec.push_back(inFile.toString());\n";
975 }
976 else if (cmd == "$OUTFILE") {
977 O << "vec.push_back(outFile.toString());\n";
978 }
979 else {
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +0000980 O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
981 O << ");\n";
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000982 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000983 }
984 else {
985 O << "vec.push_back(\"" << cmd << "\");\n";
986 }
987 }
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000988 O << IndentLevel << "ret = Action("
989 << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
990 : "\"" + StrVec[0] + "\"")
991 << ", vec);\n";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000992}
993
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +0000994/// EmitCmdLineVecFillCallback - A function object wrapper around
995/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
996/// argument to EmitCaseConstructHandler().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +0000997class EmitCmdLineVecFillCallback {
998 bool Version;
999 const std::string& ToolName;
1000 public:
1001 EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
1002 : Version(Ver), ToolName(TN) {}
1003
1004 void operator()(const Init* Statement, const char* IndentLevel,
1005 std::ostream& O) const
1006 {
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001007 EmitCmdLineVecFill(Statement, ToolName, Version,
1008 (std::string(IndentLevel) + Indent1).c_str(), O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001009 }
1010};
1011
1012// EmitGenerateActionMethod - Emit one of two versions of the
1013// Tool::GenerateAction() method.
1014void EmitGenerateActionMethod (const ToolProperties& P,
1015 const GlobalOptionDescriptions& OptDescs,
1016 bool Version, std::ostream& O) {
1017 if (Version)
1018 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
1019 else
1020 O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
1021
1022 O << Indent2 << "const sys::Path& outFile,\n"
1023 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
1024 << Indent1 << "{\n"
1025 << Indent2 << "Action ret;\n"
1026 << Indent2 << "std::vector<std::string> vec;\n";
1027
1028 // cmd_line is either a string or a 'case' construct.
1029 if (typeid(*P.CmdLine) == typeid(StringInit))
1030 EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
1031 else
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001032 EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001033 EmitCmdLineVecFillCallback(Version, P.Name),
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +00001034 OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001035
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001036 // For every understood option, emit handling code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001037 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1038 E = P.OptDescs.end(); B != E; ++B) {
1039 const ToolOptionDescription& val = B->second;
Mikhail Glushenkov31f52152008-05-30 06:10:47 +00001040 EmitOptionPropertyHandlingCode(val, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001041 }
1042
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001043 // Handle the Sink property.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001044 if (P.isSink()) {
1045 O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
1046 << Indent3 << "vec.insert(vec.end(), "
1047 << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
1048 << Indent2 << "}\n";
1049 }
1050
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001051 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001052 << Indent1 << "}\n\n";
1053}
1054
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001055/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
1056/// a given Tool class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001057void EmitGenerateActionMethods (const ToolProperties& P,
1058 const GlobalOptionDescriptions& OptDescs,
1059 std::ostream& O) {
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001060 if (!P.isJoin())
1061 O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001062 << Indent2 << "const llvm::sys::Path& outFile,\n"
1063 << Indent2 << "const InputLanguagesSet& InLangs) const\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001064 << Indent1 << "{\n"
1065 << Indent2 << "throw std::runtime_error(\"" << P.Name
1066 << " is not a Join tool!\");\n"
1067 << Indent1 << "}\n\n";
1068 else
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001069 EmitGenerateActionMethod(P, OptDescs, true, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001070
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001071 EmitGenerateActionMethod(P, OptDescs, false, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001072}
1073
Mikhail Glushenkov7adcf1e2008-05-09 08:27:26 +00001074/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
1075/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001076void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
1077 O << Indent1 << "bool IsLast() const {\n"
1078 << Indent2 << "bool last = false;\n";
1079
1080 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1081 E = P.OptDescs.end(); B != E; ++B) {
1082 const ToolOptionDescription& val = B->second;
1083
1084 if (val.isStopCompilation())
1085 O << Indent2
1086 << "if (" << val.GenVariableName()
1087 << ")\n" << Indent3 << "last = true;\n";
1088 }
1089
1090 O << Indent2 << "return last;\n"
1091 << Indent1 << "}\n\n";
1092}
1093
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001094/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
1095/// methods for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001096void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001097 O << Indent1 << "StrVector InputLanguages() const {\n"
1098 << Indent2 << "StrVector ret;\n";
1099
1100 for (StrVector::const_iterator B = P.InLanguage.begin(),
1101 E = P.InLanguage.end(); B != E; ++B) {
1102 O << Indent2 << "ret.push_back(\"" << *B << "\");\n";
1103 }
1104
1105 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001106 << Indent1 << "}\n\n";
1107
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001108 O << Indent1 << "const char* OutputLanguage() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001109 << Indent2 << "return \"" << P.OutLanguage << "\";\n"
1110 << Indent1 << "}\n\n";
1111}
1112
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001113/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
1114/// given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001115void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001116 O << Indent1 << "const char* OutputSuffix() const {\n"
Mikhail Glushenkovabab33b2008-05-30 06:13:02 +00001117 << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
1118
1119 for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
1120 E = P.OptDescs.end(); B != E; ++B) {
1121 const ToolOptionDescription& OptDesc = B->second;
1122 for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
1123 E = OptDesc.Props.end(); B != E; ++B) {
1124 const OptionProperty& OptProp = *B;
1125 if (OptProp.first == OptionPropertyType::OutputSuffix) {
1126 O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
1127 << Indent3 << "ret = \"" << OptProp.second << "\";\n";
1128 }
1129 }
1130 }
1131
1132 O << Indent2 << "return ret;\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001133 << Indent1 << "}\n\n";
1134}
1135
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001136/// EmitNameMethod - Emit the Name() method for a given Tool class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001137void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
Mikhail Glushenkovd379d162008-05-06 17:24:26 +00001138 O << Indent1 << "const char* Name() const {\n"
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001139 << Indent2 << "return \"" << P.Name << "\";\n"
1140 << Indent1 << "}\n\n";
1141}
1142
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001143/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
1144/// class.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001145void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
1146 O << Indent1 << "bool IsJoin() const {\n";
1147 if (P.isJoin())
1148 O << Indent2 << "return true;\n";
1149 else
1150 O << Indent2 << "return false;\n";
1151 O << Indent1 << "}\n\n";
1152}
1153
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001154/// EmitToolClassDefinition - Emit a Tool class definition.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001155void EmitToolClassDefinition (const ToolProperties& P,
1156 const GlobalOptionDescriptions& OptDescs,
1157 std::ostream& O) {
1158 if (P.Name == "root")
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001159 return;
1160
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001161 // Header
Mikhail Glushenkov121889c2008-05-06 17:26:53 +00001162 O << "class " << P.Name << " : public ";
1163 if (P.isJoin())
1164 O << "JoinTool";
1165 else
1166 O << "Tool";
Mikhail Glushenkovd14857f2008-05-06 17:27:15 +00001167 O << " {\npublic:\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001168
1169 EmitNameMethod(P, O);
1170 EmitInOutLanguageMethods(P, O);
1171 EmitOutputSuffixMethod(P, O);
1172 EmitIsJoinMethod(P, O);
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001173 EmitGenerateActionMethods(P, OptDescs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001174 EmitIsLastMethod(P, O);
1175
1176 // Close class definition
1177 O << "};\n\n";
1178}
1179
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001180/// EmitOptionDescriptions - Iterate over a list of option
1181/// descriptions and emit registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001182void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
1183 std::ostream& O)
1184{
1185 // Emit static cl::Option variables
1186 for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
1187 E = descs.end(); B!=E; ++B) {
1188 const GlobalOptionDescription& val = B->second;
1189
1190 O << val.GenTypeDeclaration() << ' '
1191 << val.GenVariableName()
1192 << "(\"" << val.Name << '\"';
1193
1194 if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
1195 O << ", cl::Prefix";
1196
1197 if (val.isRequired()) {
1198 switch (val.Type) {
1199 case OptionType::PrefixList:
1200 case OptionType::ParameterList:
1201 O << ", cl::OneOrMore";
1202 break;
1203 default:
1204 O << ", cl::Required";
1205 }
1206 }
1207
1208 O << ", cl::desc(\"" << val.Help << "\"));\n";
1209 }
1210
1211 if (descs.HasSink)
1212 O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
1213
1214 O << '\n';
1215}
1216
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001217/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001218void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
1219{
1220 // Get the relevant field out of RecordKeeper
1221 Record* LangMapRecord = Records.getDef("LanguageMap");
1222 if (!LangMapRecord)
1223 throw std::string("Language map definition not found!");
1224
1225 ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
1226 if (!LangsToSuffixesList)
1227 throw std::string("Error in the language map definition!");
1228
1229 // Generate code
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001230 O << "void llvmc::PopulateLanguageMap() {\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001231
1232 for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
1233 Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
1234
1235 const std::string& Lang = LangToSuffixes->getValueAsString("lang");
1236 const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
1237
1238 for (unsigned i = 0; i < Suffixes->size(); ++i)
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001239 O << Indent1 << "GlobalLanguageMap[\""
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001240 << InitPtrToString(Suffixes->getElement(i))
1241 << "\"] = \"" << Lang << "\";\n";
1242 }
1243
1244 O << "}\n\n";
1245}
1246
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001247/// FillInToolToLang - Fills in two tables that map tool names to
1248/// (input, output) languages. Used by the typechecker.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001249void FillInToolToLang (const ToolPropertiesList& TPList,
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001250 StringMap<StringSet<> >& ToolToInLang,
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001251 StringMap<std::string>& ToolToOutLang) {
1252 for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
1253 B != E; ++B) {
1254 const ToolProperties& P = *(*B);
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001255 for (StrVector::const_iterator B = P.InLanguage.begin(),
1256 E = P.InLanguage.end(); B != E; ++B)
1257 ToolToInLang[P.Name].insert(*B);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001258 ToolToOutLang[P.Name] = P.OutLanguage;
1259 }
1260}
1261
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001262/// TypecheckGraph - Check that names for output and input languages
1263/// on all edges do match.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001264// TOFIX: It would be nice if this function also checked for cycles
1265// and multiple default edges in the graph (better error
1266// reporting). Unfortunately, it is awkward to do right now because
1267// our intermediate representation is not sufficiently
1268// sofisticated. Algorithms like these should be run on a real graph
1269// instead of AST.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001270void TypecheckGraph (Record* CompilationGraph,
1271 const ToolPropertiesList& TPList) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001272 StringMap<StringSet<> > ToolToInLang;
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001273 StringMap<std::string> ToolToOutLang;
1274
1275 FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
1276 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001277 StringMap<std::string>::iterator IAE = ToolToOutLang.end();
1278 StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001279
1280 for (unsigned i = 0; i < edges->size(); ++i) {
1281 Record* Edge = edges->getElementAsRecord(i);
1282 Record* A = Edge->getValueAsDef("a");
1283 Record* B = Edge->getValueAsDef("b");
1284 StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001285 StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001286 if (IA == IAE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001287 throw A->getName() + ": no such tool!";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001288 if (IB == IBE)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001289 throw B->getName() + ": no such tool!";
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001290 if (A->getName() != "root" && IB->second.count(IA->second) == 0)
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001291 throw "Edge " + A->getName() + "->" + B->getName()
1292 + ": output->input language mismatch";
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001293 if (B->getName() == "root")
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001294 throw std::string("Edges back to the root are not allowed!");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001295 }
1296}
1297
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001298/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
1299/// by EmitEdgeClass().
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001300void IncDecWeight (const Init* i, const char* IndentLevel,
1301 std::ostream& O) {
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001302 const DagInit& d = InitPtrToDag(i);
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001303 const std::string& OpName = d.getOperator()->getAsString();
1304
1305 if (OpName == "inc_weight")
1306 O << IndentLevel << Indent1 << "ret += ";
1307 else if (OpName == "dec_weight")
1308 O << IndentLevel << Indent1 << "ret -= ";
1309 else
1310 throw "Unknown operator in edge properties list: " + OpName + '!';
1311
1312 if (d.getNumArgs() > 0)
1313 O << InitPtrToInt(d.getArg(0)) << ";\n";
1314 else
1315 O << "2;\n";
1316
Mikhail Glushenkovdfcad6c2008-05-06 18:18:20 +00001317}
1318
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001319/// EmitEdgeClass - Emit a single Edge# class.
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001320void EmitEdgeClass (unsigned N, const std::string& Target,
1321 DagInit* Case, const GlobalOptionDescriptions& OptDescs,
1322 std::ostream& O) {
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001323
1324 // Class constructor.
1325 O << "class Edge" << N << ": public Edge {\n"
1326 << "public:\n"
1327 << Indent1 << "Edge" << N << "() : Edge(\"" << Target
1328 << "\") {}\n\n"
1329
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001330 // Function Weight().
Mikhail Glushenkovd6228882008-05-06 18:15:12 +00001331 << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001332 << Indent2 << "unsigned ret = 0;\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001333
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001334 // Handle the 'case' construct.
Mikhail Glushenkov54c8f462008-05-30 06:21:21 +00001335 EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O);
Mikhail Glushenkov7dbc0ab2008-05-06 18:14:24 +00001336
1337 O << Indent2 << "return ret;\n"
1338 << Indent1 << "};\n\n};\n\n";
Mikhail Glushenkov8d0d5d22008-05-06 17:23:14 +00001339}
1340
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001341/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001342void EmitEdgeClasses (Record* CompilationGraph,
1343 const GlobalOptionDescriptions& OptDescs,
1344 std::ostream& O) {
1345 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
1346
1347 for (unsigned i = 0; i < edges->size(); ++i) {
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001348 Record* Edge = edges->getElementAsRecord(i);
1349 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001350 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001351
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001352 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001353 continue;
1354
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001355 EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001356 }
1357}
1358
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001359/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
1360/// function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001361void EmitPopulateCompilationGraph (Record* CompilationGraph,
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001362 std::ostream& O)
1363{
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001364 ListInit* edges = CompilationGraph->getValueAsListInit("edges");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001365
1366 // Generate code
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001367 O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n"
Mikhail Glushenkov242d0e62008-05-30 06:19:52 +00001368 << Indent1 << "PopulateLanguageMap();\n\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001369
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001370 // Insert vertices
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001371
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001372 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1373 if (Tools.empty())
1374 throw std::string("No tool definitions found!");
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001375
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001376 for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
1377 const std::string& Name = (*B)->getName();
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001378 if (Name != "root")
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001379 O << Indent1 << "G.insertNode(new "
1380 << Name << "());\n";
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001381 }
1382
1383 O << '\n';
1384
1385 // Insert edges
Mikhail Glushenkov2cfd2232008-05-06 16:35:25 +00001386 for (unsigned i = 0; i < edges->size(); ++i) {
1387 Record* Edge = edges->getElementAsRecord(i);
1388 Record* A = Edge->getValueAsDef("a");
1389 Record* B = Edge->getValueAsDef("b");
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001390 DagInit* Weight = Edge->getValueAsDag("weight");
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001391
1392 O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
1393
Mikhail Glushenkovdedba642008-05-30 06:08:50 +00001394 if (isDagEmpty(Weight))
Mikhail Glushenkov761958d2008-05-06 16:36:50 +00001395 O << "new SimpleEdge(\"" << B->getName() << "\")";
1396 else
1397 O << "new Edge" << i << "()";
1398
1399 O << ");\n";
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001400 }
1401
1402 O << "}\n\n";
1403}
1404
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001405/// ExtractHookNames - Extract the hook names from all instances of
1406/// $CALL(HookName) in the provided command line string. Helper
1407/// function used by FillInHookNames().
1408void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
1409 StrVector cmds;
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001410 llvm::SplitString(InitPtrToString(CmdLine), cmds);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001411 for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
1412 B != E; ++B) {
1413 const std::string& cmd = *B;
1414 if (cmd.find("$CALL(") == 0) {
1415 if (cmd.size() == 6)
1416 throw std::string("$CALL invocation: empty argument list!");
Mikhail Glushenkov1e453b02008-05-30 06:13:29 +00001417 HookNames.push_back(std::string(cmd.begin() + 6,
1418 cmd.begin() + cmd.find(")")));
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001419 }
1420 }
1421}
1422
1423/// FillInHookNames - Actually extract the hook names from all command
1424/// line strings. Helper function used by EmitHookDeclarations().
1425void FillInHookNames(const ToolPropertiesList& TPList,
1426 StrVector& HookNames) {
1427 for (ToolPropertiesList::const_iterator B = TPList.begin(),
1428 E = TPList.end(); B != E; ++B) {
1429 const ToolProperties& P = *(*B);
1430 if (!P.CmdLine)
1431 continue;
1432 if (typeid(*P.CmdLine) == typeid(StringInit)) {
1433 // This is a string.
1434 ExtractHookNames(P.CmdLine, HookNames);
1435 }
1436 else {
1437 // This is a 'case' construct.
Mikhail Glushenkov0e92d2f2008-05-30 06:18:16 +00001438 const DagInit& d = InitPtrToDag(P.CmdLine);
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001439 bool even = false;
1440 for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
1441 B != E; ++B) {
1442 if (even)
1443 ExtractHookNames(*B, HookNames);
1444 even = !even;
1445 }
1446 }
1447 }
1448}
1449
1450/// EmitHookDeclarations - Parse CmdLine fields of all the tool
1451/// property records and emit hook function declaration for each
1452/// instance of $CALL(HookName).
1453void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
1454 std::ostream& O) {
1455 StrVector HookNames;
1456 FillInHookNames(ToolProps, HookNames);
1457 if (HookNames.empty())
1458 return;
1459 std::sort(HookNames.begin(), HookNames.end());
1460 StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
1461
1462 O << "namespace hooks {\n";
1463 for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
1464 O << Indent1 << "std::string " << *B << "();\n";
1465
1466 O << "}\n\n";
1467}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001468
1469// End of anonymous namespace
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001470}
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001471
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001472/// run - The back-end entry point.
Mikhail Glushenkovc1f738d2008-05-06 18:12:03 +00001473void LLVMCConfigurationEmitter::run (std::ostream &O) {
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001474 try {
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001475
1476 // Emit file header.
Mikhail Glushenkov34307a92008-05-06 18:08:59 +00001477 EmitSourceFileHeader("LLVMC Configuration Library", O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001478
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001479 // Get a list of all defined Tools.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001480 RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
1481 if (Tools.empty())
1482 throw std::string("No tool definitions found!");
1483
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001484 // Gather information from the Tool description dags.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001485 ToolPropertiesList tool_props;
1486 GlobalOptionDescriptions opt_descs;
1487 CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
1488
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001489 // Emit global option registration code.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001490 EmitOptionDescriptions(opt_descs, O);
1491
Mikhail Glushenkov793f63d2008-05-30 06:12:24 +00001492 // Emit hook declarations.
1493 EmitHookDeclarations(tool_props, O);
1494
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001495 // Emit PopulateLanguageMap() function
1496 // (a language map maps from file extensions to language names).
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001497 EmitPopulateLanguageMap(Records, O);
1498
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001499 // Emit Tool classes.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001500 for (ToolPropertiesList::const_iterator B = tool_props.begin(),
1501 E = tool_props.end(); B!=E; ++B)
Mikhail Glushenkov35576b02008-05-30 06:10:19 +00001502 EmitToolClassDefinition(*(*B), opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001503
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001504 Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
1505 if (!CompilationGraphRecord)
1506 throw std::string("Compilation graph description not found!");
1507
1508 // Typecheck the compilation graph.
1509 TypecheckGraph(CompilationGraphRecord, tool_props);
1510
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001511 // Emit Edge# classes.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001512 EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001513
Mikhail Glushenkovbe46ae12008-05-07 21:50:19 +00001514 // Emit PopulateCompilationGraph() function.
Mikhail Glushenkov46d4e972008-05-06 16:36:06 +00001515 EmitPopulateCompilationGraph(CompilationGraphRecord, O);
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001516
1517 // EOF
Mikhail Glushenkovffe736e2008-05-30 06:21:48 +00001518 } catch (std::exception& Error) {
1519 throw Error.what() + std::string(" - usually this means a syntax error.");
1520 }
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00001521}