Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 1 | //===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config --------------===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2 | // |
| 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 Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 10 | // This tablegen backend is responsible for emitting LLVMC configuration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | ecbdcf2 | 2008-05-06 18:09:29 +0000 | [diff] [blame] | 14 | #include "LLVMCConfigurationEmitter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 15 | #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 | |
| 28 | using namespace llvm; |
| 29 | |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 30 | namespace { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | /// Typedefs |
| 34 | |
| 35 | typedef std::vector<Record*> RecordVector; |
| 36 | typedef std::vector<std::string> StrVector; |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | /// Constants |
| 40 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 41 | // Indentation strings. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 42 | const char * Indent1 = " "; |
| 43 | const char * Indent2 = " "; |
| 44 | const char * Indent3 = " "; |
| 45 | const char * Indent4 = " "; |
| 46 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 47 | // Default help string. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 48 | const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED"; |
| 49 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 50 | // Name for the "sink" option. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 51 | const char * SinkOptionName = "AutoGeneratedSinkOption"; |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | /// Helper functions |
| 55 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 56 | const std::string& InitPtrToString(const Init* ptr) { |
| 57 | const StringInit& val = dynamic_cast<const StringInit&>(*ptr); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 58 | return val.getValue(); |
| 59 | } |
| 60 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 61 | int InitPtrToInt(const Init* ptr) { |
| 62 | const IntInit& val = dynamic_cast<const IntInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 63 | return val.getValue(); |
| 64 | } |
| 65 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 66 | const DagInit& InitPtrToDagInitRef(const Init* ptr) { |
| 67 | const DagInit& val = dynamic_cast<const DagInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 68 | return val; |
| 69 | } |
| 70 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 71 | // checkNumberOfArguments - Ensure that the number of args in d is |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 72 | // less than or equal to min_arguments, otherwise throw an exception. |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 73 | void 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 Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 79 | // isDagEmpty - is this DAG marked with an empty marker? |
| 80 | bool isDagEmpty (const DagInit* d) { |
| 81 | return d->getOperator()->getAsString() == "empty"; |
| 82 | } |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 83 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 84 | //===----------------------------------------------------------------------===// |
| 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 | |
| 103 | namespace OptionType { |
| 104 | enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList}; |
| 105 | } |
| 106 | |
| 107 | bool 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 Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 117 | /// OptionDescription - Base class for option descriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 118 | struct 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 Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 141 | // 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 157 | std::string GenVariableName() const { |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 158 | const std::string& EscapedName = EscapeVariableName(Name); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 159 | switch (Type) { |
| 160 | case OptionType::Switch: |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 161 | return "AutoGeneratedSwitch" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 162 | case OptionType::Prefix: |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 163 | return "AutoGeneratedPrefix" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 164 | case OptionType::PrefixList: |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 165 | return "AutoGeneratedPrefixList" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 166 | case OptionType::Parameter: |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 167 | return "AutoGeneratedParameter" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 168 | case OptionType::ParameterList: |
| 169 | default: |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 170 | return "AutoGeneratedParameterList" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | }; |
| 175 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 176 | // Global option description. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 177 | |
| 178 | namespace GlobalOptionDescriptionFlags { |
| 179 | enum GlobalOptionDescriptionFlags { Required = 0x1 }; |
| 180 | } |
| 181 | |
| 182 | struct GlobalOptionDescription : public OptionDescription { |
| 183 | std::string Help; |
| 184 | unsigned Flags; |
| 185 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 186 | // We need to provide a default constructor because |
| 187 | // StringMap can only store DefaultConstructible objects. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 188 | 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 Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 202 | /// Merge - Merge two option descriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 203 | void Merge (const GlobalOptionDescription& other) |
| 204 | { |
| 205 | if (other.Type != Type) |
| 206 | throw "Conflicting definitions for the option " + Name + "!"; |
| 207 | |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 208 | if (Help == DefaultHelpString) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 209 | Help = other.Help; |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 210 | else if (other.Help != DefaultHelpString) { |
| 211 | llvm::cerr << "Warning: more than one help string defined for option " |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 212 | + Name + "\n"; |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 213 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 214 | |
| 215 | Flags |= other.Flags; |
| 216 | } |
| 217 | }; |
| 218 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 219 | /// GlobalOptionDescriptions - A GlobalOptionDescription array |
| 220 | /// together with some flags affecting generation of option |
| 221 | /// declarations. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 222 | struct GlobalOptionDescriptions { |
| 223 | typedef StringMap<GlobalOptionDescription> container_type; |
| 224 | typedef container_type::const_iterator const_iterator; |
| 225 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 226 | /// Descriptions - A list of GlobalOptionDescriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 227 | container_type Descriptions; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 228 | /// HasSink - Should the emitter generate a "cl::sink" option? |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 229 | bool HasSink; |
| 230 | |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 231 | 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 239 | // 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 Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 245 | // Tool-local option description. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 246 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 247 | // Properties without arguments are implemented as flags. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 248 | namespace ToolOptionDescriptionFlags { |
| 249 | enum ToolOptionDescriptionFlags { StopCompilation = 0x1, |
| 250 | Forward = 0x2, UnpackValues = 0x4}; |
| 251 | } |
| 252 | namespace OptionPropertyType { |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 253 | enum OptionPropertyType { AppendCmd, OutputSuffix }; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | typedef std::pair<OptionPropertyType::OptionPropertyType, std::string> |
| 257 | OptionProperty; |
| 258 | typedef SmallVector<OptionProperty, 4> OptionPropertyList; |
| 259 | |
| 260 | struct ToolOptionDescription : public OptionDescription { |
| 261 | unsigned Flags; |
| 262 | OptionPropertyList Props; |
| 263 | |
| 264 | // StringMap can only store DefaultConstructible objects |
Mikhail Glushenkov | 3ee8402 | 2008-03-27 09:53:47 +0000 | [diff] [blame] | 265 | ToolOptionDescription() : OptionDescription(), Flags(0) {} |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 266 | |
| 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 | |
| 300 | typedef StringMap<ToolOptionDescription> ToolOptionDescriptions; |
| 301 | |
| 302 | // Tool information record |
| 303 | |
| 304 | namespace ToolFlags { |
| 305 | enum ToolFlags { Join = 0x1, Sink = 0x2 }; |
| 306 | } |
| 307 | |
| 308 | struct ToolProperties : public RefCountedBase<ToolProperties> { |
| 309 | std::string Name; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 310 | Init* CmdLine; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 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 Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 325 | ToolProperties() : Flags(0) {} |
| 326 | ToolProperties (const std::string& n) : Name(n), Flags(0) {} |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 330 | /// 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 333 | typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList; |
| 334 | |
| 335 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 336 | /// CollectProperties - Function object for iterating over a list of |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 337 | /// tool property records. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 338 | class CollectProperties { |
| 339 | private: |
| 340 | |
| 341 | /// Implementation details |
| 342 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 343 | /// PropertyHandler - a function that extracts information |
| 344 | /// about a given tool property from its DAG representation |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 345 | typedef void (CollectProperties::*PropertyHandler)(const DagInit*); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 346 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 347 | /// PropertyHandlerMap - A map from property names to property |
| 348 | /// handlers. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 349 | typedef StringMap<PropertyHandler> PropertyHandlerMap; |
| 350 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 351 | /// OptionPropertyHandler - a function that extracts information |
| 352 | /// about a given option property from its DAG representation. |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 353 | typedef void (CollectProperties::* OptionPropertyHandler) |
| 354 | (const DagInit*, GlobalOptionDescription &); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 355 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 356 | /// OptionPropertyHandlerMap - A map from option property names to |
| 357 | /// option property handlers |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 358 | 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 Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 368 | /// toolProps_ - Properties of the current Tool. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 369 | ToolProperties& toolProps_; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 370 | /// optDescs_ - OptionDescriptions table (used to register options |
| 371 | /// globally). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 372 | GlobalOptionDescriptions& optDescs_; |
| 373 | |
| 374 | public: |
| 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; |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 400 | optionPropertyHandlers_["output_suffix"] = |
| 401 | &CollectProperties::onOutputSuffixOptionProp; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 402 | optionPropertyHandlers_["required"] = &CollectProperties::onRequired; |
| 403 | optionPropertyHandlers_["stop_compilation"] = |
| 404 | &CollectProperties::onStopCompilation; |
| 405 | optionPropertyHandlers_["unpack_values"] = |
| 406 | &CollectProperties::onUnpackValues; |
| 407 | |
| 408 | staticMembersInitialized_ = true; |
| 409 | } |
| 410 | } |
| 411 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 412 | /// operator() - Gets called for every tool property; Just forwards |
| 413 | /// to the corresponding property handler. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 414 | void operator() (Init* i) { |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 415 | const DagInit& d = InitPtrToDagInitRef(i); |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 416 | const std::string& property_name = d.getOperator()->getAsString(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 417 | PropertyHandlerMap::iterator method |
| 418 | = propertyHandlers_.find(property_name); |
| 419 | |
| 420 | if (method != propertyHandlers_.end()) { |
| 421 | PropertyHandler h = method->second; |
| 422 | (this->*h)(&d); |
| 423 | } |
| 424 | else { |
| 425 | throw "Unknown tool property: " + property_name + "!"; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | private: |
| 430 | |
| 431 | /// Property handlers -- |
| 432 | /// Functions that extract information about tool properties from |
| 433 | /// DAG representation. |
| 434 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 435 | void onCmdLine (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 436 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 437 | toolProps_.CmdLine = d->getArg(0); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 440 | void onInLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 441 | checkNumberOfArguments(d, 1); |
| 442 | toolProps_.InLanguage = InitPtrToString(d->getArg(0)); |
| 443 | } |
| 444 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 445 | void onJoin (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 446 | checkNumberOfArguments(d, 0); |
| 447 | toolProps_.setJoin(); |
| 448 | } |
| 449 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 450 | void onOutLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 451 | checkNumberOfArguments(d, 1); |
| 452 | toolProps_.OutLanguage = InitPtrToString(d->getArg(0)); |
| 453 | } |
| 454 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 455 | void onOutputSuffix (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 456 | checkNumberOfArguments(d, 1); |
| 457 | toolProps_.OutputSuffix = InitPtrToString(d->getArg(0)); |
| 458 | } |
| 459 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 460 | void onSink (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 461 | checkNumberOfArguments(d, 0); |
| 462 | optDescs_.HasSink = true; |
| 463 | toolProps_.setSink(); |
| 464 | } |
| 465 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 466 | 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 485 | |
| 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 Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 490 | void onAppendCmd (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 491 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 492 | const std::string& cmd = InitPtrToString(d->getArg(0)); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 493 | |
| 494 | toolProps_.OptDescs[o.Name].AddProperty(OptionPropertyType::AppendCmd, cmd); |
| 495 | } |
| 496 | |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 497 | void onOutputSuffixOptionProp (const DagInit* d, GlobalOptionDescription& o) { |
| 498 | checkNumberOfArguments(d, 1); |
| 499 | const std::string& suf = InitPtrToString(d->getArg(0)); |
| 500 | |
| 501 | if (toolProps_.OptDescs[o.Name].Type != OptionType::Switch) |
| 502 | throw "Option " + o.Name |
| 503 | + " can't have 'output_suffix' property since it isn't a switch!"; |
| 504 | |
| 505 | toolProps_.OptDescs[o.Name].AddProperty |
| 506 | (OptionPropertyType::OutputSuffix, suf); |
| 507 | } |
| 508 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 509 | void onForward (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 510 | checkNumberOfArguments(d, 0); |
| 511 | toolProps_.OptDescs[o.Name].setForward(); |
| 512 | } |
| 513 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 514 | void onHelp (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 515 | checkNumberOfArguments(d, 1); |
| 516 | const std::string& help_message = InitPtrToString(d->getArg(0)); |
| 517 | |
| 518 | o.Help = help_message; |
| 519 | } |
| 520 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 521 | void onRequired (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 522 | checkNumberOfArguments(d, 0); |
| 523 | o.setRequired(); |
| 524 | } |
| 525 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 526 | void onStopCompilation (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 527 | checkNumberOfArguments(d, 0); |
| 528 | if (o.Type != OptionType::Switch) |
| 529 | throw std::string("Only options of type Switch can stop compilation!"); |
| 530 | toolProps_.OptDescs[o.Name].setStopCompilation(); |
| 531 | } |
| 532 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 533 | void onUnpackValues (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 534 | checkNumberOfArguments(d, 0); |
| 535 | toolProps_.OptDescs[o.Name].setUnpackValues(); |
| 536 | } |
| 537 | |
| 538 | /// Helper functions |
| 539 | |
| 540 | // Add an option of type t |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 541 | void addOption (const DagInit* d, OptionType::OptionType t) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 542 | checkNumberOfArguments(d, 2); |
| 543 | const std::string& name = InitPtrToString(d->getArg(0)); |
| 544 | |
| 545 | GlobalOptionDescription o(t, name); |
| 546 | toolProps_.OptDescs[name].Type = t; |
| 547 | toolProps_.OptDescs[name].Name = name; |
| 548 | processOptionProperties(d, o); |
| 549 | insertDescription(o); |
| 550 | } |
| 551 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 552 | // Insert new GlobalOptionDescription into GlobalOptionDescriptions list |
| 553 | void insertDescription (const GlobalOptionDescription& o) |
| 554 | { |
| 555 | if (optDescs_.Descriptions.count(o.Name)) { |
| 556 | GlobalOptionDescription& D = optDescs_.Descriptions[o.Name]; |
| 557 | D.Merge(o); |
| 558 | } |
| 559 | else { |
| 560 | optDescs_.Descriptions[o.Name] = o; |
| 561 | } |
| 562 | } |
| 563 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 564 | /// processOptionProperties - Go through the list of option |
| 565 | /// properties and call a corresponding handler for each. |
| 566 | /// |
| 567 | /// Parameters: |
| 568 | /// name - option name |
| 569 | /// d - option property list |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 570 | void processOptionProperties (const DagInit* d, GlobalOptionDescription& o) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 571 | // First argument is option name |
| 572 | checkNumberOfArguments(d, 2); |
| 573 | |
| 574 | for (unsigned B = 1, E = d->getNumArgs(); B!=E; ++B) { |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 575 | const DagInit& option_property |
| 576 | = InitPtrToDagInitRef(d->getArg(B)); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 577 | const std::string& option_property_name |
| 578 | = option_property.getOperator()->getAsString(); |
| 579 | OptionPropertyHandlerMap::iterator method |
| 580 | = optionPropertyHandlers_.find(option_property_name); |
| 581 | |
| 582 | if (method != optionPropertyHandlers_.end()) { |
| 583 | OptionPropertyHandler h = method->second; |
| 584 | (this->*h)(&option_property, o); |
| 585 | } |
| 586 | else { |
| 587 | throw "Unknown option property: " + option_property_name + "!"; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | }; |
| 592 | |
| 593 | // Static members of CollectProperties |
| 594 | CollectProperties::PropertyHandlerMap |
| 595 | CollectProperties::propertyHandlers_; |
| 596 | |
| 597 | CollectProperties::OptionPropertyHandlerMap |
| 598 | CollectProperties::optionPropertyHandlers_; |
| 599 | |
| 600 | bool CollectProperties::staticMembersInitialized_ = false; |
| 601 | |
| 602 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 603 | /// CollectToolProperties - Gather information from the parsed |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 604 | /// TableGen data (basically a wrapper for the CollectProperties |
| 605 | /// function object). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 606 | void CollectToolProperties (RecordVector::const_iterator B, |
| 607 | RecordVector::const_iterator E, |
| 608 | ToolPropertiesList& TPList, |
| 609 | GlobalOptionDescriptions& OptDescs) |
| 610 | { |
| 611 | // Iterate over a properties list of every Tool definition |
| 612 | for (;B!=E;++B) { |
| 613 | RecordVector::value_type T = *B; |
| 614 | ListInit* PropList = T->getValueAsListInit("properties"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 615 | |
| 616 | IntrusiveRefCntPtr<ToolProperties> |
| 617 | ToolProps(new ToolProperties(T->getName())); |
| 618 | |
| 619 | std::for_each(PropList->begin(), PropList->end(), |
| 620 | CollectProperties(*ToolProps, OptDescs)); |
| 621 | TPList.push_back(ToolProps); |
| 622 | } |
| 623 | } |
| 624 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 625 | /// EmitCaseTest1Arg - Helper function used by |
| 626 | /// EmitCaseConstructHandler. |
| 627 | bool EmitCaseTest1Arg(const std::string& TestName, |
| 628 | const DagInit& d, |
| 629 | const GlobalOptionDescriptions& OptDescs, |
| 630 | std::ostream& O) { |
| 631 | checkNumberOfArguments(&d, 1); |
| 632 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
| 633 | if (TestName == "switch_on") { |
| 634 | const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName); |
| 635 | if (OptDesc.Type != OptionType::Switch) |
| 636 | throw OptName + ": incorrect option type!"; |
| 637 | O << OptDesc.GenVariableName(); |
| 638 | return true; |
| 639 | } else if (TestName == "input_languages_contain") { |
| 640 | O << "InLangs.count(\"" << OptName << "\") != 0"; |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | /// EmitCaseTest2Args - Helper function used by |
| 648 | /// EmitCaseConstructHandler. |
| 649 | bool EmitCaseTest2Args(const std::string& TestName, |
| 650 | const DagInit& d, |
| 651 | const char* IndentLevel, |
| 652 | const GlobalOptionDescriptions& OptDescs, |
| 653 | std::ostream& O) { |
| 654 | checkNumberOfArguments(&d, 2); |
| 655 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
| 656 | const std::string& OptArg = InitPtrToString(d.getArg(1)); |
| 657 | const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName); |
| 658 | |
| 659 | if (TestName == "parameter_equals") { |
| 660 | if (OptDesc.Type != OptionType::Parameter |
| 661 | && OptDesc.Type != OptionType::Prefix) |
| 662 | throw OptName + ": incorrect option type!"; |
| 663 | O << OptDesc.GenVariableName() << " == \"" << OptArg << "\""; |
| 664 | return true; |
| 665 | } |
| 666 | else if (TestName == "element_in_list") { |
| 667 | if (OptDesc.Type != OptionType::ParameterList |
| 668 | && OptDesc.Type != OptionType::PrefixList) |
| 669 | throw OptName + ": incorrect option type!"; |
| 670 | const std::string& VarName = OptDesc.GenVariableName(); |
| 671 | O << "std::find(" << VarName << ".begin(),\n" |
| 672 | << IndentLevel << Indent1 << VarName << ".end(), \"" |
| 673 | << OptArg << "\") != " << VarName << ".end()"; |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | // Forward declaration. |
| 681 | // EmitLogicalOperationTest and EmitCaseTest are mutually recursive. |
| 682 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
| 683 | const GlobalOptionDescriptions& OptDescs, |
| 684 | std::ostream& O); |
| 685 | |
| 686 | /// EmitLogicalOperationTest - Helper function used by |
| 687 | /// EmitCaseConstructHandler. |
| 688 | void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp, |
| 689 | const char* IndentLevel, |
| 690 | const GlobalOptionDescriptions& OptDescs, |
| 691 | std::ostream& O) { |
| 692 | O << '('; |
| 693 | for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) { |
| 694 | const DagInit& InnerTest = InitPtrToDagInitRef(d.getArg(j)); |
| 695 | EmitCaseTest(InnerTest, IndentLevel, OptDescs, O); |
| 696 | if (j != NumArgs - 1) |
| 697 | O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " ("; |
| 698 | else |
| 699 | O << ')'; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /// EmitCaseTest - Helper function used by EmitCaseConstructHandler. |
| 704 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
| 705 | const GlobalOptionDescriptions& OptDescs, |
| 706 | std::ostream& O) { |
| 707 | const std::string& TestName = d.getOperator()->getAsString(); |
| 708 | |
| 709 | if (TestName == "and") |
| 710 | EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O); |
| 711 | else if (TestName == "or") |
| 712 | EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O); |
| 713 | else if (EmitCaseTest1Arg(TestName, d, OptDescs, O)) |
| 714 | return; |
| 715 | else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O)) |
| 716 | return; |
| 717 | else |
| 718 | throw TestName + ": unknown edge property!"; |
| 719 | } |
| 720 | |
| 721 | // Emit code that handles the 'case' construct. |
| 722 | // Takes a function object that should emit code for every case clause. |
| 723 | // Callback's type is |
| 724 | // void F(Init* Statement, const char* IndentLevel, std::ostream& O). |
| 725 | template <typename F> |
| 726 | void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel, |
| 727 | const F& Callback, |
| 728 | const GlobalOptionDescriptions& OptDescs, |
| 729 | std::ostream& O) { |
| 730 | assert(d->getOperator()->getAsString() == "case"); |
| 731 | |
| 732 | for (unsigned i = 0, numArgs = d->getNumArgs(); i != numArgs; ++i) { |
| 733 | const DagInit& Test = InitPtrToDagInitRef(d->getArg(i)); |
| 734 | |
| 735 | if (Test.getOperator()->getAsString() == "default") { |
| 736 | if (i+2 != numArgs) |
| 737 | throw std::string("The 'default' clause should be the last in the" |
| 738 | "'case' construct!"); |
| 739 | O << IndentLevel << "else {\n"; |
| 740 | } |
| 741 | else { |
| 742 | O << IndentLevel << "if ("; |
| 743 | EmitCaseTest(Test, IndentLevel, OptDescs, O); |
| 744 | O << ") {\n"; |
| 745 | } |
| 746 | |
| 747 | ++i; |
| 748 | if (i == numArgs) |
| 749 | throw "Case construct handler: no corresponding action " |
| 750 | "found for the test " + Test.getAsString() + '!'; |
| 751 | |
| 752 | Callback(d->getArg(i), IndentLevel, O); |
| 753 | O << IndentLevel << "}\n"; |
| 754 | } |
| 755 | } |
| 756 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 757 | /// EmitForwardOptionPropertyHandlingCode - Helper function used to |
| 758 | /// implement EmitOptionPropertyHandlingCode(). Emits code for |
| 759 | /// handling the (forward) option property. |
| 760 | void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D, |
| 761 | std::ostream& O) { |
| 762 | switch (D.Type) { |
| 763 | case OptionType::Switch: |
| 764 | O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n"; |
| 765 | break; |
| 766 | case OptionType::Parameter: |
| 767 | O << Indent3 << "vec.push_back(\"-" << D.Name << "\");\n"; |
| 768 | O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n"; |
| 769 | break; |
| 770 | case OptionType::Prefix: |
| 771 | O << Indent3 << "vec.push_back(\"-" << D.Name << "\" + " |
| 772 | << D.GenVariableName() << ");\n"; |
| 773 | break; |
| 774 | case OptionType::PrefixList: |
| 775 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 776 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 777 | << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n" |
| 778 | << Indent4 << "vec.push_back(\"-" << D.Name << "\" + " |
| 779 | << "*B);\n"; |
| 780 | break; |
| 781 | case OptionType::ParameterList: |
| 782 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 783 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 784 | << Indent3 << "E = " << D.GenVariableName() |
| 785 | << ".end() ; B != E; ++B) {\n" |
| 786 | << Indent4 << "vec.push_back(\"-" << D.Name << "\");\n" |
| 787 | << Indent4 << "vec.push_back(*B);\n" |
| 788 | << Indent3 << "}\n"; |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 793 | // A helper function used by EmitOptionPropertyHandlingCode() that |
| 794 | // tells us whether we should emit any code at all. |
| 795 | bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) { |
| 796 | if (!D.isForward() && !D.isUnpackValues() && D.Props.empty()) |
| 797 | return false; |
| 798 | return true; |
| 799 | } |
| 800 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 801 | /// EmitOptionPropertyHandlingCode - Helper function used by |
| 802 | /// EmitGenerateActionMethod(). Emits code that handles option |
| 803 | /// properties. |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 804 | void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D, |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 805 | std::ostream& O) |
| 806 | { |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 807 | if (!ToolOptionHasInterestingProperties(D)) |
| 808 | return; |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 809 | // Start of the if-clause. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 810 | O << Indent2 << "if ("; |
| 811 | if (D.Type == OptionType::Switch) |
| 812 | O << D.GenVariableName(); |
| 813 | else |
| 814 | O << '!' << D.GenVariableName() << ".empty()"; |
| 815 | |
| 816 | O <<") {\n"; |
| 817 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 818 | // Handle option properties that take an argument. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 819 | for (OptionPropertyList::const_iterator B = D.Props.begin(), |
| 820 | E = D.Props.end(); B!=E; ++B) { |
| 821 | const OptionProperty& val = *B; |
| 822 | |
| 823 | switch (val.first) { |
| 824 | // (append_cmd cmd) property |
| 825 | case OptionPropertyType::AppendCmd: |
| 826 | O << Indent3 << "vec.push_back(\"" << val.second << "\");\n"; |
| 827 | break; |
| 828 | // Other properties with argument |
| 829 | default: |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | // Handle flags |
| 835 | |
| 836 | // (forward) property |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 837 | if (D.isForward()) |
| 838 | EmitForwardOptionPropertyHandlingCode(D, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 839 | |
| 840 | // (unpack_values) property |
| 841 | if (D.isUnpackValues()) { |
| 842 | if (IsListOptionType(D.Type)) { |
| 843 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 844 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 845 | << Indent3 << "E = " << D.GenVariableName() |
| 846 | << ".end(); B != E; ++B)\n" |
Mikhail Glushenkov | d83038c | 2008-05-06 18:13:45 +0000 | [diff] [blame] | 847 | << Indent4 << "llvm::SplitString(*B, vec, \",\");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 848 | } |
| 849 | else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){ |
Mikhail Glushenkov | d83038c | 2008-05-06 18:13:45 +0000 | [diff] [blame] | 850 | O << Indent3 << "llvm::SplitString(" |
| 851 | << D.GenVariableName() << ", vec, \",\");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 852 | } |
| 853 | else { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 854 | throw std::string("Switches can't have unpack_values property!"); |
| 855 | } |
| 856 | } |
| 857 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 858 | // End of the if-clause. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 859 | O << Indent2 << "}\n"; |
| 860 | } |
| 861 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 862 | /// SubstituteSpecialCommands - Perform string substitution for $CALL |
| 863 | /// and $ENV. Helper function used by EmitCmdLineVecFill(). |
| 864 | std::string SubstituteSpecialCommands(const std::string& cmd) { |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame^] | 865 | size_t cparen = cmd.find(")"); |
| 866 | std::string ret; |
| 867 | |
| 868 | if (cmd.find("$CALL(") == 0) { |
| 869 | if (cmd.size() == 6) |
| 870 | throw std::string("$CALL invocation: empty argument list!"); |
| 871 | |
| 872 | ret += "hooks::"; |
| 873 | ret += std::string(cmd.begin() + 6, cmd.begin() + cparen); |
| 874 | ret += "()"; |
| 875 | } |
| 876 | else if (cmd.find("$ENV(") == 0) { |
| 877 | if (cmd.size() == 5) |
| 878 | throw std::string("$ENV invocation: empty argument list!"); |
| 879 | |
| 880 | ret += "std::getenv(\""; |
| 881 | ret += std::string(cmd.begin() + 5, cmd.begin() + cparen); |
| 882 | ret += "\")"; |
| 883 | } |
| 884 | else { |
| 885 | throw "Unknown special command: " + cmd; |
| 886 | } |
| 887 | |
| 888 | if (cmd.begin() + cparen + 1 != cmd.end()) { |
| 889 | ret += " + std::string(\""; |
| 890 | ret += (cmd.c_str() + cparen + 1); |
| 891 | ret += "\")"; |
| 892 | } |
| 893 | |
| 894 | return ret; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /// EmitCmdLineVecFill - Emit code that fills in the command line |
| 898 | /// vector. Helper function used by EmitGenerateActionMethod(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 899 | void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName, |
| 900 | bool Version, const char* IndentLevel, |
| 901 | std::ostream& O) { |
| 902 | StrVector StrVec; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame^] | 903 | SplitString(InitPtrToString(CmdLine), StrVec); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 904 | if (StrVec.empty()) |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 905 | throw "Tool " + ToolName + " has empty command line!"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 906 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 907 | StrVector::const_iterator I = StrVec.begin(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 908 | ++I; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 909 | for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 910 | const std::string& cmd = *I; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 911 | O << IndentLevel; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 912 | if (cmd.at(0) == '$') { |
| 913 | if (cmd == "$INFILE") { |
| 914 | if (Version) |
| 915 | O << "for (PathVector::const_iterator B = inFiles.begin()" |
| 916 | << ", E = inFiles.end();\n" |
| 917 | << IndentLevel << "B != E; ++B)\n" |
| 918 | << IndentLevel << Indent1 << "vec.push_back(B->toString());\n"; |
| 919 | else |
| 920 | O << "vec.push_back(inFile.toString());\n"; |
| 921 | } |
| 922 | else if (cmd == "$OUTFILE") { |
| 923 | O << "vec.push_back(outFile.toString());\n"; |
| 924 | } |
| 925 | else { |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame^] | 926 | O << "vec.push_back(" << SubstituteSpecialCommands(cmd); |
| 927 | O << ");\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 928 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 929 | } |
| 930 | else { |
| 931 | O << "vec.push_back(\"" << cmd << "\");\n"; |
| 932 | } |
| 933 | } |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 934 | O << IndentLevel << "ret = Action(" |
| 935 | << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0]) |
| 936 | : "\"" + StrVec[0] + "\"") |
| 937 | << ", vec);\n"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 940 | /// EmitCmdLineVecFillCallback - A function object wrapper around |
| 941 | /// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an |
| 942 | /// argument to EmitCaseConstructHandler(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 943 | class EmitCmdLineVecFillCallback { |
| 944 | bool Version; |
| 945 | const std::string& ToolName; |
| 946 | public: |
| 947 | EmitCmdLineVecFillCallback(bool Ver, const std::string& TN) |
| 948 | : Version(Ver), ToolName(TN) {} |
| 949 | |
| 950 | void operator()(const Init* Statement, const char* IndentLevel, |
| 951 | std::ostream& O) const |
| 952 | { |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 953 | EmitCmdLineVecFill(Statement, ToolName, Version, |
| 954 | (std::string(IndentLevel) + Indent1).c_str(), O); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 955 | } |
| 956 | }; |
| 957 | |
| 958 | // EmitGenerateActionMethod - Emit one of two versions of the |
| 959 | // Tool::GenerateAction() method. |
| 960 | void EmitGenerateActionMethod (const ToolProperties& P, |
| 961 | const GlobalOptionDescriptions& OptDescs, |
| 962 | bool Version, std::ostream& O) { |
| 963 | if (Version) |
| 964 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"; |
| 965 | else |
| 966 | O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n"; |
| 967 | |
| 968 | O << Indent2 << "const sys::Path& outFile,\n" |
| 969 | << Indent2 << "const InputLanguagesSet& InLangs) const\n" |
| 970 | << Indent1 << "{\n" |
| 971 | << Indent2 << "Action ret;\n" |
| 972 | << Indent2 << "std::vector<std::string> vec;\n"; |
| 973 | |
| 974 | // cmd_line is either a string or a 'case' construct. |
| 975 | if (typeid(*P.CmdLine) == typeid(StringInit)) |
| 976 | EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O); |
| 977 | else |
| 978 | EmitCaseConstructHandler(&InitPtrToDagInitRef(P.CmdLine), Indent2, |
| 979 | EmitCmdLineVecFillCallback(Version, P.Name), |
| 980 | OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 981 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 982 | // For every understood option, emit handling code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 983 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 984 | E = P.OptDescs.end(); B != E; ++B) { |
| 985 | const ToolOptionDescription& val = B->second; |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 986 | EmitOptionPropertyHandlingCode(val, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 989 | // Handle the Sink property. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 990 | if (P.isSink()) { |
| 991 | O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n" |
| 992 | << Indent3 << "vec.insert(vec.end(), " |
| 993 | << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n" |
| 994 | << Indent2 << "}\n"; |
| 995 | } |
| 996 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 997 | O << Indent2 << "return ret;\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 998 | << Indent1 << "}\n\n"; |
| 999 | } |
| 1000 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1001 | /// EmitGenerateActionMethods - Emit two GenerateAction() methods for |
| 1002 | /// a given Tool class. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1003 | void EmitGenerateActionMethods (const ToolProperties& P, |
| 1004 | const GlobalOptionDescriptions& OptDescs, |
| 1005 | std::ostream& O) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1006 | if (!P.isJoin()) |
| 1007 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n" |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1008 | << Indent2 << "const llvm::sys::Path& outFile,\n" |
| 1009 | << Indent2 << "const InputLanguagesSet& InLangs) const\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1010 | << Indent1 << "{\n" |
| 1011 | << Indent2 << "throw std::runtime_error(\"" << P.Name |
| 1012 | << " is not a Join tool!\");\n" |
| 1013 | << Indent1 << "}\n\n"; |
| 1014 | else |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1015 | EmitGenerateActionMethod(P, OptDescs, true, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1016 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1017 | EmitGenerateActionMethod(P, OptDescs, false, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1020 | /// EmitIsLastMethod - Emit the IsLast() method for a given Tool |
| 1021 | /// class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1022 | void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) { |
| 1023 | O << Indent1 << "bool IsLast() const {\n" |
| 1024 | << Indent2 << "bool last = false;\n"; |
| 1025 | |
| 1026 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 1027 | E = P.OptDescs.end(); B != E; ++B) { |
| 1028 | const ToolOptionDescription& val = B->second; |
| 1029 | |
| 1030 | if (val.isStopCompilation()) |
| 1031 | O << Indent2 |
| 1032 | << "if (" << val.GenVariableName() |
| 1033 | << ")\n" << Indent3 << "last = true;\n"; |
| 1034 | } |
| 1035 | |
| 1036 | O << Indent2 << "return last;\n" |
| 1037 | << Indent1 << "}\n\n"; |
| 1038 | } |
| 1039 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1040 | /// EmitInOutLanguageMethods - Emit the [Input,Output]Language() |
| 1041 | /// methods for a given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1042 | void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1043 | O << Indent1 << "const char* InputLanguage() const {\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1044 | << Indent2 << "return \"" << P.InLanguage << "\";\n" |
| 1045 | << Indent1 << "}\n\n"; |
| 1046 | |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1047 | O << Indent1 << "const char* OutputLanguage() const {\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1048 | << Indent2 << "return \"" << P.OutLanguage << "\";\n" |
| 1049 | << Indent1 << "}\n\n"; |
| 1050 | } |
| 1051 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1052 | /// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a |
| 1053 | /// given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1054 | void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1055 | O << Indent1 << "const char* OutputSuffix() const {\n" |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 1056 | << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n"; |
| 1057 | |
| 1058 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 1059 | E = P.OptDescs.end(); B != E; ++B) { |
| 1060 | const ToolOptionDescription& OptDesc = B->second; |
| 1061 | for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(), |
| 1062 | E = OptDesc.Props.end(); B != E; ++B) { |
| 1063 | const OptionProperty& OptProp = *B; |
| 1064 | if (OptProp.first == OptionPropertyType::OutputSuffix) { |
| 1065 | O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n" |
| 1066 | << Indent3 << "ret = \"" << OptProp.second << "\";\n"; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | O << Indent2 << "return ret;\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1072 | << Indent1 << "}\n\n"; |
| 1073 | } |
| 1074 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1075 | /// EmitNameMethod - Emit the Name() method for a given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1076 | void EmitNameMethod (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1077 | O << Indent1 << "const char* Name() const {\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1078 | << Indent2 << "return \"" << P.Name << "\";\n" |
| 1079 | << Indent1 << "}\n\n"; |
| 1080 | } |
| 1081 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1082 | /// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool |
| 1083 | /// class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1084 | void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) { |
| 1085 | O << Indent1 << "bool IsJoin() const {\n"; |
| 1086 | if (P.isJoin()) |
| 1087 | O << Indent2 << "return true;\n"; |
| 1088 | else |
| 1089 | O << Indent2 << "return false;\n"; |
| 1090 | O << Indent1 << "}\n\n"; |
| 1091 | } |
| 1092 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1093 | /// EmitToolClassDefinition - Emit a Tool class definition. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1094 | void EmitToolClassDefinition (const ToolProperties& P, |
| 1095 | const GlobalOptionDescriptions& OptDescs, |
| 1096 | std::ostream& O) { |
| 1097 | if (P.Name == "root") |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1098 | return; |
| 1099 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1100 | // Header |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 1101 | O << "class " << P.Name << " : public "; |
| 1102 | if (P.isJoin()) |
| 1103 | O << "JoinTool"; |
| 1104 | else |
| 1105 | O << "Tool"; |
Mikhail Glushenkov | ee628d9 | 2008-05-06 17:27:15 +0000 | [diff] [blame] | 1106 | O << " {\npublic:\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1107 | |
| 1108 | EmitNameMethod(P, O); |
| 1109 | EmitInOutLanguageMethods(P, O); |
| 1110 | EmitOutputSuffixMethod(P, O); |
| 1111 | EmitIsJoinMethod(P, O); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1112 | EmitGenerateActionMethods(P, OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1113 | EmitIsLastMethod(P, O); |
| 1114 | |
| 1115 | // Close class definition |
| 1116 | O << "};\n\n"; |
| 1117 | } |
| 1118 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1119 | /// EmitOptionDescriptions - Iterate over a list of option |
| 1120 | /// descriptions and emit registration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1121 | void EmitOptionDescriptions (const GlobalOptionDescriptions& descs, |
| 1122 | std::ostream& O) |
| 1123 | { |
| 1124 | // Emit static cl::Option variables |
| 1125 | for (GlobalOptionDescriptions::const_iterator B = descs.begin(), |
| 1126 | E = descs.end(); B!=E; ++B) { |
| 1127 | const GlobalOptionDescription& val = B->second; |
| 1128 | |
| 1129 | O << val.GenTypeDeclaration() << ' ' |
| 1130 | << val.GenVariableName() |
| 1131 | << "(\"" << val.Name << '\"'; |
| 1132 | |
| 1133 | if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList) |
| 1134 | O << ", cl::Prefix"; |
| 1135 | |
| 1136 | if (val.isRequired()) { |
| 1137 | switch (val.Type) { |
| 1138 | case OptionType::PrefixList: |
| 1139 | case OptionType::ParameterList: |
| 1140 | O << ", cl::OneOrMore"; |
| 1141 | break; |
| 1142 | default: |
| 1143 | O << ", cl::Required"; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | O << ", cl::desc(\"" << val.Help << "\"));\n"; |
| 1148 | } |
| 1149 | |
| 1150 | if (descs.HasSink) |
| 1151 | O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n"; |
| 1152 | |
| 1153 | O << '\n'; |
| 1154 | } |
| 1155 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1156 | /// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1157 | void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O) |
| 1158 | { |
| 1159 | // Get the relevant field out of RecordKeeper |
| 1160 | Record* LangMapRecord = Records.getDef("LanguageMap"); |
| 1161 | if (!LangMapRecord) |
| 1162 | throw std::string("Language map definition not found!"); |
| 1163 | |
| 1164 | ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map"); |
| 1165 | if (!LangsToSuffixesList) |
| 1166 | throw std::string("Error in the language map definition!"); |
| 1167 | |
| 1168 | // Generate code |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 1169 | O << "void llvmc::PopulateLanguageMap(LanguageMap& language_map) {\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1170 | |
| 1171 | for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) { |
| 1172 | Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i); |
| 1173 | |
| 1174 | const std::string& Lang = LangToSuffixes->getValueAsString("lang"); |
| 1175 | const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes"); |
| 1176 | |
| 1177 | for (unsigned i = 0; i < Suffixes->size(); ++i) |
| 1178 | O << Indent1 << "language_map[\"" |
| 1179 | << InitPtrToString(Suffixes->getElement(i)) |
| 1180 | << "\"] = \"" << Lang << "\";\n"; |
| 1181 | } |
| 1182 | |
| 1183 | O << "}\n\n"; |
| 1184 | } |
| 1185 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1186 | /// FillInToolToLang - Fills in two tables that map tool names to |
| 1187 | /// (input, output) languages. Used by the typechecker. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1188 | void FillInToolToLang (const ToolPropertiesList& TPList, |
| 1189 | StringMap<std::string>& ToolToInLang, |
| 1190 | StringMap<std::string>& ToolToOutLang) { |
| 1191 | for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end(); |
| 1192 | B != E; ++B) { |
| 1193 | const ToolProperties& P = *(*B); |
| 1194 | ToolToInLang[P.Name] = P.InLanguage; |
| 1195 | ToolToOutLang[P.Name] = P.OutLanguage; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1199 | /// TypecheckGraph - Check that names for output and input languages |
| 1200 | /// on all edges do match. |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1201 | // TOFIX: check for cycles. |
| 1202 | // TOFIX: check for multiple default edges. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1203 | void TypecheckGraph (Record* CompilationGraph, |
| 1204 | const ToolPropertiesList& TPList) { |
| 1205 | StringMap<std::string> ToolToInLang; |
| 1206 | StringMap<std::string> ToolToOutLang; |
| 1207 | |
| 1208 | FillInToolToLang(TPList, ToolToInLang, ToolToOutLang); |
| 1209 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
| 1210 | StringMap<std::string>::iterator IAE = ToolToInLang.end(); |
| 1211 | StringMap<std::string>::iterator IBE = ToolToOutLang.end(); |
| 1212 | |
| 1213 | for (unsigned i = 0; i < edges->size(); ++i) { |
| 1214 | Record* Edge = edges->getElementAsRecord(i); |
| 1215 | Record* A = Edge->getValueAsDef("a"); |
| 1216 | Record* B = Edge->getValueAsDef("b"); |
| 1217 | StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName()); |
| 1218 | StringMap<std::string>::iterator IB = ToolToInLang.find(B->getName()); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1219 | if (IA == IAE) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1220 | throw A->getName() + ": no such tool!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1221 | if (IB == IBE) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1222 | throw B->getName() + ": no such tool!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1223 | if (A->getName() != "root" && IA->second != IB->second) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1224 | throw "Edge " + A->getName() + "->" + B->getName() |
| 1225 | + ": output->input language mismatch"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1226 | if (B->getName() == "root") |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1227 | throw std::string("Edges back to the root are not allowed!"); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1231 | /// IncDecWeight - Helper function passed to EmitCaseConstructHandler() |
| 1232 | /// by EmitEdgeClass(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1233 | void IncDecWeight (const Init* i, const char* IndentLevel, |
| 1234 | std::ostream& O) { |
| 1235 | const DagInit& d = InitPtrToDagInitRef(i); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1236 | const std::string& OpName = d.getOperator()->getAsString(); |
| 1237 | |
| 1238 | if (OpName == "inc_weight") |
| 1239 | O << IndentLevel << Indent1 << "ret += "; |
| 1240 | else if (OpName == "dec_weight") |
| 1241 | O << IndentLevel << Indent1 << "ret -= "; |
| 1242 | else |
| 1243 | throw "Unknown operator in edge properties list: " + OpName + '!'; |
| 1244 | |
| 1245 | if (d.getNumArgs() > 0) |
| 1246 | O << InitPtrToInt(d.getArg(0)) << ";\n"; |
| 1247 | else |
| 1248 | O << "2;\n"; |
| 1249 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1252 | /// EmitEdgeClass - Emit a single Edge# class. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1253 | void EmitEdgeClass (unsigned N, const std::string& Target, |
| 1254 | DagInit* Case, const GlobalOptionDescriptions& OptDescs, |
| 1255 | std::ostream& O) { |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Class constructor. |
| 1258 | O << "class Edge" << N << ": public Edge {\n" |
| 1259 | << "public:\n" |
| 1260 | << Indent1 << "Edge" << N << "() : Edge(\"" << Target |
| 1261 | << "\") {}\n\n" |
| 1262 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1263 | // Function Weight(). |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 1264 | << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n" |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1265 | << Indent2 << "unsigned ret = 0;\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1266 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1267 | // Handle the 'case' construct. |
| 1268 | EmitCaseConstructHandler(Case, Indent2, IncDecWeight, OptDescs, O); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1269 | |
| 1270 | O << Indent2 << "return ret;\n" |
| 1271 | << Indent1 << "};\n\n};\n\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1274 | /// EmitEdgeClasses - Emit Edge* classes that represent graph edges. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1275 | void EmitEdgeClasses (Record* CompilationGraph, |
| 1276 | const GlobalOptionDescriptions& OptDescs, |
| 1277 | std::ostream& O) { |
| 1278 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
| 1279 | |
| 1280 | for (unsigned i = 0; i < edges->size(); ++i) { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1281 | Record* Edge = edges->getElementAsRecord(i); |
| 1282 | Record* B = Edge->getValueAsDef("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1283 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1284 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1285 | if (isDagEmpty(Weight)) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1286 | continue; |
| 1287 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1288 | EmitEdgeClass(i, B->getName(), Weight, OptDescs, O); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1292 | /// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() |
| 1293 | /// function. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1294 | void EmitPopulateCompilationGraph (Record* CompilationGraph, |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1295 | std::ostream& O) |
| 1296 | { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1297 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1298 | |
| 1299 | // Generate code |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 1300 | O << "void llvmc::PopulateCompilationGraph(CompilationGraph& G) {\n" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1301 | << Indent1 << "PopulateLanguageMap(G.ExtsToLangs);\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1302 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1303 | // Insert vertices |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1304 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1305 | RecordVector Tools = Records.getAllDerivedDefinitions("Tool"); |
| 1306 | if (Tools.empty()) |
| 1307 | throw std::string("No tool definitions found!"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1308 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1309 | for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) { |
| 1310 | const std::string& Name = (*B)->getName(); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1311 | if (Name != "root") |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1312 | O << Indent1 << "G.insertNode(new " |
| 1313 | << Name << "());\n"; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | O << '\n'; |
| 1317 | |
| 1318 | // Insert edges |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1319 | for (unsigned i = 0; i < edges->size(); ++i) { |
| 1320 | Record* Edge = edges->getElementAsRecord(i); |
| 1321 | Record* A = Edge->getValueAsDef("a"); |
| 1322 | Record* B = Edge->getValueAsDef("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1323 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1324 | |
| 1325 | O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", "; |
| 1326 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1327 | if (isDagEmpty(Weight)) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1328 | O << "new SimpleEdge(\"" << B->getName() << "\")"; |
| 1329 | else |
| 1330 | O << "new Edge" << i << "()"; |
| 1331 | |
| 1332 | O << ");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | O << "}\n\n"; |
| 1336 | } |
| 1337 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1338 | /// ExtractHookNames - Extract the hook names from all instances of |
| 1339 | /// $CALL(HookName) in the provided command line string. Helper |
| 1340 | /// function used by FillInHookNames(). |
| 1341 | void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) { |
| 1342 | StrVector cmds; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame^] | 1343 | llvm::SplitString(InitPtrToString(CmdLine), cmds); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1344 | for (StrVector::const_iterator B = cmds.begin(), E = cmds.end(); |
| 1345 | B != E; ++B) { |
| 1346 | const std::string& cmd = *B; |
| 1347 | if (cmd.find("$CALL(") == 0) { |
| 1348 | if (cmd.size() == 6) |
| 1349 | throw std::string("$CALL invocation: empty argument list!"); |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame^] | 1350 | HookNames.push_back(std::string(cmd.begin() + 6, |
| 1351 | cmd.begin() + cmd.find(")"))); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | /// FillInHookNames - Actually extract the hook names from all command |
| 1357 | /// line strings. Helper function used by EmitHookDeclarations(). |
| 1358 | void FillInHookNames(const ToolPropertiesList& TPList, |
| 1359 | StrVector& HookNames) { |
| 1360 | for (ToolPropertiesList::const_iterator B = TPList.begin(), |
| 1361 | E = TPList.end(); B != E; ++B) { |
| 1362 | const ToolProperties& P = *(*B); |
| 1363 | if (!P.CmdLine) |
| 1364 | continue; |
| 1365 | if (typeid(*P.CmdLine) == typeid(StringInit)) { |
| 1366 | // This is a string. |
| 1367 | ExtractHookNames(P.CmdLine, HookNames); |
| 1368 | } |
| 1369 | else { |
| 1370 | // This is a 'case' construct. |
| 1371 | const DagInit& d = InitPtrToDagInitRef(P.CmdLine); |
| 1372 | bool even = false; |
| 1373 | for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end(); |
| 1374 | B != E; ++B) { |
| 1375 | if (even) |
| 1376 | ExtractHookNames(*B, HookNames); |
| 1377 | even = !even; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /// EmitHookDeclarations - Parse CmdLine fields of all the tool |
| 1384 | /// property records and emit hook function declaration for each |
| 1385 | /// instance of $CALL(HookName). |
| 1386 | void EmitHookDeclarations(const ToolPropertiesList& ToolProps, |
| 1387 | std::ostream& O) { |
| 1388 | StrVector HookNames; |
| 1389 | FillInHookNames(ToolProps, HookNames); |
| 1390 | if (HookNames.empty()) |
| 1391 | return; |
| 1392 | std::sort(HookNames.begin(), HookNames.end()); |
| 1393 | StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end()); |
| 1394 | |
| 1395 | O << "namespace hooks {\n"; |
| 1396 | for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B) |
| 1397 | O << Indent1 << "std::string " << *B << "();\n"; |
| 1398 | |
| 1399 | O << "}\n\n"; |
| 1400 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1401 | |
| 1402 | // End of anonymous namespace |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 1403 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1404 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1405 | /// run - The back-end entry point. |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 1406 | void LLVMCConfigurationEmitter::run (std::ostream &O) { |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Emit file header. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 1409 | EmitSourceFileHeader("LLVMC Configuration Library", O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1410 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1411 | // Get a list of all defined Tools. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1412 | RecordVector Tools = Records.getAllDerivedDefinitions("Tool"); |
| 1413 | if (Tools.empty()) |
| 1414 | throw std::string("No tool definitions found!"); |
| 1415 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1416 | // Gather information from the Tool description dags. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1417 | ToolPropertiesList tool_props; |
| 1418 | GlobalOptionDescriptions opt_descs; |
| 1419 | CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs); |
| 1420 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1421 | // Emit global option registration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1422 | EmitOptionDescriptions(opt_descs, O); |
| 1423 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1424 | // Emit hook declarations. |
| 1425 | EmitHookDeclarations(tool_props, O); |
| 1426 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1427 | // Emit PopulateLanguageMap() function |
| 1428 | // (a language map maps from file extensions to language names). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1429 | EmitPopulateLanguageMap(Records, O); |
| 1430 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1431 | // Emit Tool classes. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1432 | for (ToolPropertiesList::const_iterator B = tool_props.begin(), |
| 1433 | E = tool_props.end(); B!=E; ++B) |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1434 | EmitToolClassDefinition(*(*B), opt_descs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1435 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1436 | Record* CompilationGraphRecord = Records.getDef("CompilationGraph"); |
| 1437 | if (!CompilationGraphRecord) |
| 1438 | throw std::string("Compilation graph description not found!"); |
| 1439 | |
| 1440 | // Typecheck the compilation graph. |
| 1441 | TypecheckGraph(CompilationGraphRecord, tool_props); |
| 1442 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1443 | // Emit Edge# classes. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1444 | EmitEdgeClasses(CompilationGraphRecord, opt_descs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1445 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1446 | // Emit PopulateCompilationGraph() function. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1447 | EmitPopulateCompilationGraph(CompilationGraphRecord, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1448 | |
| 1449 | // EOF |
| 1450 | } |