Mikhail Glushenkov | fb37f39 | 2008-05-30 06:20:54 +0000 | [diff] [blame] | 1 | //===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===// |
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" |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSet.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <cassert> |
| 25 | #include <functional> |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 26 | #include <stdexcept> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 27 | #include <string> |
Chris Lattner | 32a9e7a | 2008-06-04 04:46:14 +0000 | [diff] [blame] | 28 | #include <typeinfo> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 31 | namespace { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | /// Typedefs |
| 35 | |
| 36 | typedef std::vector<Record*> RecordVector; |
| 37 | typedef std::vector<std::string> StrVector; |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | /// Constants |
| 41 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 42 | // Indentation strings. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 43 | const char * Indent1 = " "; |
| 44 | const char * Indent2 = " "; |
| 45 | const char * Indent3 = " "; |
| 46 | const char * Indent4 = " "; |
| 47 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 48 | // Default help string. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 49 | const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED"; |
| 50 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 51 | // Name for the "sink" option. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 52 | const char * SinkOptionName = "AutoGeneratedSinkOption"; |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | /// Helper functions |
| 56 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 57 | int InitPtrToInt(const Init* ptr) { |
| 58 | const IntInit& val = dynamic_cast<const IntInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 59 | return val.getValue(); |
| 60 | } |
| 61 | |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 62 | const std::string& InitPtrToString(const Init* ptr) { |
| 63 | const StringInit& val = dynamic_cast<const StringInit&>(*ptr); |
| 64 | return val.getValue(); |
| 65 | } |
| 66 | |
| 67 | const ListInit& InitPtrToList(const Init* ptr) { |
| 68 | const ListInit& val = dynamic_cast<const ListInit&>(*ptr); |
| 69 | return val; |
| 70 | } |
| 71 | |
| 72 | const DagInit& InitPtrToDag(const Init* ptr) { |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 73 | const DagInit& val = dynamic_cast<const DagInit&>(*ptr); |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 74 | return val; |
| 75 | } |
| 76 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 77 | // checkNumberOfArguments - Ensure that the number of args in d is |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 78 | // less than or equal to min_arguments, otherwise throw an exception. |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 79 | void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) { |
| 80 | if (d->getNumArgs() < min_arguments) |
| 81 | throw "Property " + d->getOperator()->getAsString() |
| 82 | + " has too few arguments!"; |
| 83 | } |
| 84 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 85 | // isDagEmpty - is this DAG marked with an empty marker? |
| 86 | bool isDagEmpty (const DagInit* d) { |
| 87 | return d->getOperator()->getAsString() == "empty"; |
| 88 | } |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 89 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | /// Back-end specific code |
| 92 | |
| 93 | // A command-line option can have one of the following types: |
| 94 | // |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 95 | // Alias - an alias for another option. |
| 96 | // |
| 97 | // Switch - a simple switch without arguments, e.g. -O2 |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 98 | // |
| 99 | // Parameter - an option that takes one(and only one) argument, e.g. -o file, |
| 100 | // --output=file |
| 101 | // |
| 102 | // ParameterList - same as Parameter, but more than one occurence |
| 103 | // of the option is allowed, e.g. -lm -lpthread |
| 104 | // |
| 105 | // Prefix - argument is everything after the prefix, |
| 106 | // e.g. -Wa,-foo,-bar, -DNAME=VALUE |
| 107 | // |
| 108 | // PrefixList - same as Prefix, but more than one option occurence is |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 109 | // allowed. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 110 | |
| 111 | namespace OptionType { |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 112 | enum OptionType { Alias, Switch, |
| 113 | Parameter, ParameterList, Prefix, PrefixList}; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool IsListOptionType (OptionType::OptionType t) { |
| 117 | return (t == OptionType::ParameterList || t == OptionType::PrefixList); |
| 118 | } |
| 119 | |
| 120 | // Code duplication here is necessary because one option can affect |
| 121 | // several tools and those tools may have different actions associated |
| 122 | // with this option. GlobalOptionDescriptions are used to generate |
| 123 | // the option registration code, while ToolOptionDescriptions are used |
| 124 | // to generate tool-specific code. |
| 125 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 126 | /// OptionDescription - Base class for option descriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 127 | struct OptionDescription { |
| 128 | OptionType::OptionType Type; |
| 129 | std::string Name; |
| 130 | |
| 131 | OptionDescription(OptionType::OptionType t = OptionType::Switch, |
| 132 | const std::string& n = "") |
| 133 | : Type(t), Name(n) |
| 134 | {} |
| 135 | |
| 136 | const char* GenTypeDeclaration() const { |
| 137 | switch (Type) { |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 138 | case OptionType::Alias: |
| 139 | return "cl::alias"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 140 | case OptionType::PrefixList: |
| 141 | case OptionType::ParameterList: |
| 142 | return "cl::list<std::string>"; |
| 143 | case OptionType::Switch: |
| 144 | return "cl::opt<bool>"; |
| 145 | case OptionType::Parameter: |
| 146 | case OptionType::Prefix: |
| 147 | default: |
| 148 | return "cl::opt<std::string>"; |
| 149 | } |
| 150 | } |
| 151 | |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 152 | // Escape commas and other symbols not allowed in the C++ variable |
| 153 | // names. Makes it possible to use options with names like "Wa," |
| 154 | // (useful for prefix options). |
| 155 | std::string EscapeVariableName(const std::string& Var) const { |
| 156 | std::string ret; |
| 157 | for (unsigned i = 0; i != Var.size(); ++i) { |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 158 | char cur_char = Var[i]; |
| 159 | if (cur_char == ',') { |
| 160 | ret += "_comma_"; |
| 161 | } |
| 162 | else if (cur_char == '+') { |
| 163 | ret += "_plus_"; |
| 164 | } |
| 165 | else if (cur_char == ',') { |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 166 | ret += "_comma_"; |
| 167 | } |
| 168 | else { |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 169 | ret.push_back(cur_char); |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | return ret; |
| 173 | } |
| 174 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 175 | std::string GenVariableName() const { |
Mikhail Glushenkov | 5c98d82 | 2008-05-12 16:33:06 +0000 | [diff] [blame] | 176 | const std::string& EscapedName = EscapeVariableName(Name); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 177 | switch (Type) { |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 178 | case OptionType::Alias: |
| 179 | return "AutoGeneratedAlias" + EscapedName; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 180 | case OptionType::Switch: |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 181 | return "AutoGeneratedSwitch" + EscapedName; |
| 182 | case OptionType::Prefix: |
| 183 | return "AutoGeneratedPrefix" + EscapedName; |
| 184 | case OptionType::PrefixList: |
| 185 | return "AutoGeneratedPrefixList" + EscapedName; |
| 186 | case OptionType::Parameter: |
| 187 | return "AutoGeneratedParameter" + EscapedName; |
| 188 | case OptionType::ParameterList: |
| 189 | default: |
| 190 | return "AutoGeneratedParameterList" + EscapedName; |
| 191 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | }; |
| 195 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 196 | // Global option description. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 197 | |
| 198 | namespace GlobalOptionDescriptionFlags { |
| 199 | enum GlobalOptionDescriptionFlags { Required = 0x1 }; |
| 200 | } |
| 201 | |
| 202 | struct GlobalOptionDescription : public OptionDescription { |
| 203 | std::string Help; |
| 204 | unsigned Flags; |
| 205 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 206 | // We need to provide a default constructor because |
| 207 | // StringMap can only store DefaultConstructible objects. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 208 | GlobalOptionDescription() : OptionDescription(), Flags(0) |
| 209 | {} |
| 210 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 211 | GlobalOptionDescription (OptionType::OptionType t, const std::string& n, |
| 212 | const std::string& h = DefaultHelpString) |
| 213 | : OptionDescription(t, n), Help(h), Flags(0) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 214 | {} |
| 215 | |
| 216 | bool isRequired() const { |
| 217 | return Flags & GlobalOptionDescriptionFlags::Required; |
| 218 | } |
| 219 | void setRequired() { |
| 220 | Flags |= GlobalOptionDescriptionFlags::Required; |
| 221 | } |
| 222 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 223 | /// Merge - Merge two option descriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 224 | void Merge (const GlobalOptionDescription& other) |
| 225 | { |
| 226 | if (other.Type != Type) |
| 227 | throw "Conflicting definitions for the option " + Name + "!"; |
| 228 | |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 229 | if (Help == DefaultHelpString) |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 230 | Help = other.Help; |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 231 | else if (other.Help != DefaultHelpString) { |
| 232 | llvm::cerr << "Warning: more than one help string defined for option " |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 233 | + Name + "\n"; |
Mikhail Glushenkov | 978d498 | 2008-05-06 18:13:00 +0000 | [diff] [blame] | 234 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 235 | |
| 236 | Flags |= other.Flags; |
| 237 | } |
| 238 | }; |
| 239 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 240 | /// GlobalOptionDescriptions - A GlobalOptionDescription array |
| 241 | /// together with some flags affecting generation of option |
| 242 | /// declarations. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 243 | struct GlobalOptionDescriptions { |
| 244 | typedef StringMap<GlobalOptionDescription> container_type; |
| 245 | typedef container_type::const_iterator const_iterator; |
| 246 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 247 | /// Descriptions - A list of GlobalOptionDescriptions. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 248 | container_type Descriptions; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 249 | /// HasSink - Should the emitter generate a "cl::sink" option? |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 250 | bool HasSink; |
| 251 | |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 252 | /// FindOption - exception-throwing wrapper for find(). |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 253 | const GlobalOptionDescription& FindOption(const std::string& OptName) const { |
| 254 | const_iterator I = Descriptions.find(OptName); |
| 255 | if (I != Descriptions.end()) |
| 256 | return I->second; |
| 257 | else |
| 258 | throw OptName + ": no such option!"; |
| 259 | } |
| 260 | |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 261 | /// insertDescription - Insert new GlobalOptionDescription into |
| 262 | /// GlobalOptionDescriptions list |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 263 | void insertDescription (const GlobalOptionDescription& o) |
| 264 | { |
| 265 | container_type::iterator I = Descriptions.find(o.Name); |
| 266 | if (I != Descriptions.end()) { |
| 267 | GlobalOptionDescription& D = I->second; |
| 268 | D.Merge(o); |
| 269 | } |
| 270 | else { |
| 271 | Descriptions[o.Name] = o; |
| 272 | } |
| 273 | } |
| 274 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 275 | // Support for STL-style iteration |
| 276 | const_iterator begin() const { return Descriptions.begin(); } |
| 277 | const_iterator end() const { return Descriptions.end(); } |
| 278 | }; |
| 279 | |
| 280 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 281 | // Tool-local option description. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 282 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 283 | // Properties without arguments are implemented as flags. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 284 | namespace ToolOptionDescriptionFlags { |
| 285 | enum ToolOptionDescriptionFlags { StopCompilation = 0x1, |
| 286 | Forward = 0x2, UnpackValues = 0x4}; |
| 287 | } |
| 288 | namespace OptionPropertyType { |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 289 | enum OptionPropertyType { AppendCmd, ForwardAs, OutputSuffix }; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | typedef std::pair<OptionPropertyType::OptionPropertyType, std::string> |
| 293 | OptionProperty; |
| 294 | typedef SmallVector<OptionProperty, 4> OptionPropertyList; |
| 295 | |
| 296 | struct ToolOptionDescription : public OptionDescription { |
| 297 | unsigned Flags; |
| 298 | OptionPropertyList Props; |
| 299 | |
| 300 | // StringMap can only store DefaultConstructible objects |
Mikhail Glushenkov | 3ee8402 | 2008-03-27 09:53:47 +0000 | [diff] [blame] | 301 | ToolOptionDescription() : OptionDescription(), Flags(0) {} |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 302 | |
| 303 | ToolOptionDescription (OptionType::OptionType t, const std::string& n) |
| 304 | : OptionDescription(t, n) |
| 305 | {} |
| 306 | |
| 307 | // Various boolean properties |
| 308 | bool isStopCompilation() const { |
| 309 | return Flags & ToolOptionDescriptionFlags::StopCompilation; |
| 310 | } |
| 311 | void setStopCompilation() { |
| 312 | Flags |= ToolOptionDescriptionFlags::StopCompilation; |
| 313 | } |
| 314 | |
| 315 | bool isForward() const { |
| 316 | return Flags & ToolOptionDescriptionFlags::Forward; |
| 317 | } |
| 318 | void setForward() { |
| 319 | Flags |= ToolOptionDescriptionFlags::Forward; |
| 320 | } |
| 321 | |
| 322 | bool isUnpackValues() const { |
| 323 | return Flags & ToolOptionDescriptionFlags::UnpackValues; |
| 324 | } |
| 325 | void setUnpackValues() { |
| 326 | Flags |= ToolOptionDescriptionFlags::UnpackValues; |
| 327 | } |
| 328 | |
| 329 | void AddProperty (OptionPropertyType::OptionPropertyType t, |
| 330 | const std::string& val) |
| 331 | { |
| 332 | Props.push_back(std::make_pair(t, val)); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | typedef StringMap<ToolOptionDescription> ToolOptionDescriptions; |
| 337 | |
| 338 | // Tool information record |
| 339 | |
| 340 | namespace ToolFlags { |
| 341 | enum ToolFlags { Join = 0x1, Sink = 0x2 }; |
| 342 | } |
| 343 | |
| 344 | struct ToolProperties : public RefCountedBase<ToolProperties> { |
| 345 | std::string Name; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 346 | Init* CmdLine; |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 347 | StrVector InLanguage; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 348 | std::string OutLanguage; |
| 349 | std::string OutputSuffix; |
| 350 | unsigned Flags; |
| 351 | ToolOptionDescriptions OptDescs; |
| 352 | |
| 353 | // Various boolean properties |
| 354 | void setSink() { Flags |= ToolFlags::Sink; } |
| 355 | bool isSink() const { return Flags & ToolFlags::Sink; } |
| 356 | void setJoin() { Flags |= ToolFlags::Join; } |
| 357 | bool isJoin() const { return Flags & ToolFlags::Join; } |
| 358 | |
| 359 | // Default ctor here is needed because StringMap can only store |
| 360 | // DefaultConstructible objects |
Anton Korobeynikov | 12a3f83 | 2008-06-01 16:22:49 +0000 | [diff] [blame] | 361 | ToolProperties() : CmdLine(0), Flags(0) {} |
| 362 | ToolProperties (const std::string& n) : Name(n), CmdLine(0), Flags(0) {} |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 366 | /// ToolPropertiesList - A list of Tool information records |
| 367 | /// IntrusiveRefCntPtrs are used here because StringMap has no copy |
| 368 | /// constructor (and we want to avoid copying ToolProperties anyway). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 369 | typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList; |
| 370 | |
| 371 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 372 | /// CollectOptionProperties - Function object for iterating over a |
| 373 | /// list (usually, a DAG) of option property records. |
| 374 | class CollectOptionProperties { |
| 375 | private: |
| 376 | // Implementation details. |
| 377 | |
| 378 | /// OptionPropertyHandler - a function that extracts information |
| 379 | /// about a given option property from its DAG representation. |
| 380 | typedef void (CollectOptionProperties::* OptionPropertyHandler) |
| 381 | (const DagInit*); |
| 382 | |
| 383 | /// OptionPropertyHandlerMap - A map from option property names to |
| 384 | /// option property handlers |
| 385 | typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap; |
| 386 | |
| 387 | static OptionPropertyHandlerMap optionPropertyHandlers_; |
| 388 | static bool staticMembersInitialized_; |
| 389 | |
| 390 | /// This is where the information is stored |
| 391 | |
| 392 | /// toolProps_ - Properties of the current Tool. |
| 393 | ToolProperties* toolProps_; |
| 394 | /// optDescs_ - OptionDescriptions table (used to register options |
| 395 | /// globally). |
| 396 | GlobalOptionDescription& optDesc_; |
| 397 | |
| 398 | public: |
| 399 | |
| 400 | explicit CollectOptionProperties(ToolProperties* TP, |
| 401 | GlobalOptionDescription& OD) |
| 402 | : toolProps_(TP), optDesc_(OD) |
| 403 | { |
| 404 | if (!staticMembersInitialized_) { |
| 405 | optionPropertyHandlers_["append_cmd"] = |
| 406 | &CollectOptionProperties::onAppendCmd; |
| 407 | optionPropertyHandlers_["forward"] = |
| 408 | &CollectOptionProperties::onForward; |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 409 | optionPropertyHandlers_["forward_as"] = |
| 410 | &CollectOptionProperties::onForwardAs; |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 411 | optionPropertyHandlers_["help"] = |
| 412 | &CollectOptionProperties::onHelp; |
| 413 | optionPropertyHandlers_["output_suffix"] = |
| 414 | &CollectOptionProperties::onOutputSuffix; |
| 415 | optionPropertyHandlers_["required"] = |
| 416 | &CollectOptionProperties::onRequired; |
| 417 | optionPropertyHandlers_["stop_compilation"] = |
| 418 | &CollectOptionProperties::onStopCompilation; |
| 419 | optionPropertyHandlers_["unpack_values"] = |
| 420 | &CollectOptionProperties::onUnpackValues; |
| 421 | |
| 422 | staticMembersInitialized_ = true; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// operator() - Gets called for every option property; Just forwards |
| 427 | /// to the corresponding property handler. |
| 428 | void operator() (Init* i) { |
| 429 | const DagInit& option_property = InitPtrToDag(i); |
| 430 | const std::string& option_property_name |
| 431 | = option_property.getOperator()->getAsString(); |
| 432 | OptionPropertyHandlerMap::iterator method |
| 433 | = optionPropertyHandlers_.find(option_property_name); |
| 434 | |
| 435 | if (method != optionPropertyHandlers_.end()) { |
| 436 | OptionPropertyHandler h = method->second; |
| 437 | (this->*h)(&option_property); |
| 438 | } |
| 439 | else { |
| 440 | throw "Unknown option property: " + option_property_name + "!"; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | private: |
| 445 | |
| 446 | /// Option property handlers -- |
| 447 | /// Methods that handle properties that are common for all types of |
| 448 | /// options (like append_cmd, stop_compilation) |
| 449 | |
| 450 | void onAppendCmd (const DagInit* d) { |
| 451 | checkNumberOfArguments(d, 1); |
| 452 | checkToolProps(d); |
| 453 | const std::string& cmd = InitPtrToString(d->getArg(0)); |
| 454 | |
| 455 | toolProps_->OptDescs[optDesc_.Name]. |
| 456 | AddProperty(OptionPropertyType::AppendCmd, cmd); |
| 457 | } |
| 458 | |
| 459 | void onOutputSuffix (const DagInit* d) { |
| 460 | checkNumberOfArguments(d, 1); |
| 461 | checkToolProps(d); |
| 462 | const std::string& suf = InitPtrToString(d->getArg(0)); |
| 463 | |
| 464 | if (toolProps_->OptDescs[optDesc_.Name].Type != OptionType::Switch) |
| 465 | throw "Option " + optDesc_.Name |
| 466 | + " can't have 'output_suffix' property since it isn't a switch!"; |
| 467 | |
| 468 | toolProps_->OptDescs[optDesc_.Name].AddProperty |
| 469 | (OptionPropertyType::OutputSuffix, suf); |
| 470 | } |
| 471 | |
| 472 | void onForward (const DagInit* d) { |
| 473 | checkNumberOfArguments(d, 0); |
| 474 | checkToolProps(d); |
| 475 | toolProps_->OptDescs[optDesc_.Name].setForward(); |
| 476 | } |
| 477 | |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 478 | void onForwardAs (const DagInit* d) { |
| 479 | checkNumberOfArguments(d, 1); |
| 480 | checkToolProps(d); |
| 481 | const std::string& cmd = InitPtrToString(d->getArg(0)); |
| 482 | |
| 483 | toolProps_->OptDescs[optDesc_.Name]. |
| 484 | AddProperty(OptionPropertyType::ForwardAs, cmd); |
| 485 | } |
| 486 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 487 | void onHelp (const DagInit* d) { |
| 488 | checkNumberOfArguments(d, 1); |
| 489 | const std::string& help_message = InitPtrToString(d->getArg(0)); |
| 490 | |
| 491 | optDesc_.Help = help_message; |
| 492 | } |
| 493 | |
| 494 | void onRequired (const DagInit* d) { |
| 495 | checkNumberOfArguments(d, 0); |
| 496 | checkToolProps(d); |
| 497 | optDesc_.setRequired(); |
| 498 | } |
| 499 | |
| 500 | void onStopCompilation (const DagInit* d) { |
| 501 | checkNumberOfArguments(d, 0); |
| 502 | checkToolProps(d); |
| 503 | if (optDesc_.Type != OptionType::Switch) |
| 504 | throw std::string("Only options of type Switch can stop compilation!"); |
| 505 | toolProps_->OptDescs[optDesc_.Name].setStopCompilation(); |
| 506 | } |
| 507 | |
| 508 | void onUnpackValues (const DagInit* d) { |
| 509 | checkNumberOfArguments(d, 0); |
| 510 | checkToolProps(d); |
| 511 | toolProps_->OptDescs[optDesc_.Name].setUnpackValues(); |
| 512 | } |
| 513 | |
| 514 | // Helper functions |
| 515 | |
| 516 | /// checkToolProps - Throw an error if toolProps_ == 0. |
| 517 | void checkToolProps(const DagInit* d) { |
| 518 | if (!d) |
| 519 | throw "Option property " + d->getOperator()->getAsString() |
| 520 | + " can't be used in this context"; |
| 521 | } |
| 522 | |
| 523 | }; |
| 524 | |
| 525 | CollectOptionProperties::OptionPropertyHandlerMap |
| 526 | CollectOptionProperties::optionPropertyHandlers_; |
| 527 | |
| 528 | bool CollectOptionProperties::staticMembersInitialized_ = false; |
| 529 | |
| 530 | |
| 531 | /// processOptionProperties - Go through the list of option |
| 532 | /// properties and call a corresponding handler for each. |
| 533 | void processOptionProperties (const DagInit* d, ToolProperties* t, |
| 534 | GlobalOptionDescription& o) { |
| 535 | checkNumberOfArguments(d, 2); |
| 536 | DagInit::const_arg_iterator B = d->arg_begin(); |
| 537 | // Skip the first argument: it's always the option name. |
| 538 | ++B; |
| 539 | std::for_each(B, d->arg_end(), CollectOptionProperties(t, o)); |
| 540 | } |
| 541 | |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 542 | /// AddOption - A function object wrapper for |
| 543 | /// processOptionProperties. Used by CollectProperties and |
| 544 | /// CollectPropertiesFromOptionList. |
| 545 | class AddOption { |
| 546 | private: |
| 547 | GlobalOptionDescriptions& OptDescs_; |
| 548 | ToolProperties* ToolProps_; |
| 549 | |
| 550 | public: |
| 551 | explicit AddOption(GlobalOptionDescriptions& OD, ToolProperties* TP = 0) |
| 552 | : OptDescs_(OD), ToolProps_(TP) |
| 553 | {} |
| 554 | |
| 555 | void operator()(const Init* i) { |
| 556 | const DagInit& d = InitPtrToDag(i); |
| 557 | checkNumberOfArguments(&d, 2); |
| 558 | |
| 559 | const OptionType::OptionType Type = |
| 560 | getOptionType(d.getOperator()->getAsString()); |
| 561 | const std::string& Name = InitPtrToString(d.getArg(0)); |
| 562 | |
| 563 | GlobalOptionDescription OD(Type, Name); |
| 564 | if (Type != OptionType::Alias) { |
| 565 | processOptionProperties(&d, ToolProps_, OD); |
| 566 | if (ToolProps_) { |
| 567 | ToolProps_->OptDescs[Name].Type = Type; |
| 568 | ToolProps_->OptDescs[Name].Name = Name; |
| 569 | } |
| 570 | } |
| 571 | else { |
| 572 | OD.Help = InitPtrToString(d.getArg(1)); |
| 573 | } |
| 574 | OptDescs_.insertDescription(OD); |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | OptionType::OptionType getOptionType(const std::string& T) const { |
| 579 | if (T == "alias_option") |
| 580 | return OptionType::Alias; |
| 581 | else if (T == "switch_option") |
| 582 | return OptionType::Switch; |
| 583 | else if (T == "parameter_option") |
| 584 | return OptionType::Parameter; |
| 585 | else if (T == "parameter_list_option") |
| 586 | return OptionType::ParameterList; |
| 587 | else if (T == "prefix_option") |
| 588 | return OptionType::Prefix; |
| 589 | else if (T == "prefix_list_option") |
| 590 | return OptionType::PrefixList; |
| 591 | else |
| 592 | throw "Unknown option type: " + T + '!'; |
| 593 | } |
| 594 | }; |
| 595 | |
| 596 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 597 | /// CollectProperties - Function object for iterating over a list of |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 598 | /// tool property records. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 599 | class CollectProperties { |
| 600 | private: |
| 601 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 602 | // Implementation details |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 603 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 604 | /// PropertyHandler - a function that extracts information |
| 605 | /// about a given tool property from its DAG representation |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 606 | typedef void (CollectProperties::*PropertyHandler)(const DagInit*); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 607 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 608 | /// PropertyHandlerMap - A map from property names to property |
| 609 | /// handlers. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 610 | typedef StringMap<PropertyHandler> PropertyHandlerMap; |
| 611 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 612 | // Static maps from strings to CollectProperties methods("handlers") |
| 613 | static PropertyHandlerMap propertyHandlers_; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 614 | static bool staticMembersInitialized_; |
| 615 | |
| 616 | |
| 617 | /// This is where the information is stored |
| 618 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 619 | /// toolProps_ - Properties of the current Tool. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 620 | ToolProperties& toolProps_; |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 621 | /// optDescs_ - OptionDescriptions table (used to register options |
| 622 | /// globally). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 623 | GlobalOptionDescriptions& optDescs_; |
| 624 | |
| 625 | public: |
| 626 | |
| 627 | explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d) |
| 628 | : toolProps_(p), optDescs_(d) |
| 629 | { |
| 630 | if (!staticMembersInitialized_) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 631 | propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine; |
| 632 | propertyHandlers_["in_language"] = &CollectProperties::onInLanguage; |
| 633 | propertyHandlers_["join"] = &CollectProperties::onJoin; |
| 634 | propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage; |
| 635 | propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix; |
| 636 | propertyHandlers_["parameter_option"] |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 637 | = &CollectProperties::addOption; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 638 | propertyHandlers_["parameter_list_option"] = |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 639 | &CollectProperties::addOption; |
| 640 | propertyHandlers_["prefix_option"] = &CollectProperties::addOption; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 641 | propertyHandlers_["prefix_list_option"] = |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 642 | &CollectProperties::addOption; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 643 | propertyHandlers_["sink"] = &CollectProperties::onSink; |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 644 | propertyHandlers_["switch_option"] = &CollectProperties::addOption; |
| 645 | propertyHandlers_["alias_option"] = &CollectProperties::addOption; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 646 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 647 | staticMembersInitialized_ = true; |
| 648 | } |
| 649 | } |
| 650 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 651 | /// operator() - Gets called for every tool property; Just forwards |
| 652 | /// to the corresponding property handler. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 653 | void operator() (Init* i) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 654 | const DagInit& d = InitPtrToDag(i); |
Mikhail Glushenkov | 581936a | 2008-05-06 17:22:03 +0000 | [diff] [blame] | 655 | const std::string& property_name = d.getOperator()->getAsString(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 656 | PropertyHandlerMap::iterator method |
| 657 | = propertyHandlers_.find(property_name); |
| 658 | |
| 659 | if (method != propertyHandlers_.end()) { |
| 660 | PropertyHandler h = method->second; |
| 661 | (this->*h)(&d); |
| 662 | } |
| 663 | else { |
| 664 | throw "Unknown tool property: " + property_name + "!"; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | private: |
| 669 | |
| 670 | /// Property handlers -- |
| 671 | /// Functions that extract information about tool properties from |
| 672 | /// DAG representation. |
| 673 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 674 | void onCmdLine (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 675 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 676 | toolProps_.CmdLine = d->getArg(0); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 679 | void onInLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 680 | checkNumberOfArguments(d, 1); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 681 | Init* arg = d->getArg(0); |
| 682 | |
| 683 | // Find out the argument's type. |
| 684 | if (typeid(*arg) == typeid(StringInit)) { |
| 685 | // It's a string. |
| 686 | toolProps_.InLanguage.push_back(InitPtrToString(arg)); |
| 687 | } |
| 688 | else { |
| 689 | // It's a list. |
| 690 | const ListInit& lst = InitPtrToList(arg); |
| 691 | StrVector& out = toolProps_.InLanguage; |
| 692 | |
| 693 | // Copy strings to the output vector. |
| 694 | for (ListInit::const_iterator B = lst.begin(), E = lst.end(); |
| 695 | B != E; ++B) { |
| 696 | out.push_back(InitPtrToString(*B)); |
| 697 | } |
| 698 | |
| 699 | // Remove duplicates. |
| 700 | std::sort(out.begin(), out.end()); |
| 701 | StrVector::iterator newE = std::unique(out.begin(), out.end()); |
| 702 | out.erase(newE, out.end()); |
| 703 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 706 | void onJoin (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 707 | checkNumberOfArguments(d, 0); |
| 708 | toolProps_.setJoin(); |
| 709 | } |
| 710 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 711 | void onOutLanguage (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 712 | checkNumberOfArguments(d, 1); |
| 713 | toolProps_.OutLanguage = InitPtrToString(d->getArg(0)); |
| 714 | } |
| 715 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 716 | void onOutputSuffix (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 717 | checkNumberOfArguments(d, 1); |
| 718 | toolProps_.OutputSuffix = InitPtrToString(d->getArg(0)); |
| 719 | } |
| 720 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 721 | void onSink (const DagInit* d) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 722 | checkNumberOfArguments(d, 0); |
| 723 | optDescs_.HasSink = true; |
| 724 | toolProps_.setSink(); |
| 725 | } |
| 726 | |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 727 | // Just forwards to the AddOption function object. Somewhat |
| 728 | // non-optimal, but avoids code duplication. |
| 729 | void addOption (const DagInit* d) { |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 730 | checkNumberOfArguments(d, 2); |
Mikhail Glushenkov | 2b7bcb4 | 2008-05-30 06:27:29 +0000 | [diff] [blame] | 731 | AddOption(optDescs_, &toolProps_)(d); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 736 | // Defintions of static members of CollectProperties. |
| 737 | CollectProperties::PropertyHandlerMap CollectProperties::propertyHandlers_; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 738 | bool CollectProperties::staticMembersInitialized_ = false; |
| 739 | |
| 740 | |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 741 | /// CollectToolProperties - Gather information about tool properties |
| 742 | /// from the parsed TableGen data (basically a wrapper for the |
| 743 | /// CollectProperties function object). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 744 | void CollectToolProperties (RecordVector::const_iterator B, |
| 745 | RecordVector::const_iterator E, |
| 746 | ToolPropertiesList& TPList, |
| 747 | GlobalOptionDescriptions& OptDescs) |
| 748 | { |
| 749 | // Iterate over a properties list of every Tool definition |
| 750 | for (;B!=E;++B) { |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 751 | Record* T = *B; |
| 752 | // Throws an exception if the value does not exist. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 753 | ListInit* PropList = T->getValueAsListInit("properties"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 754 | |
| 755 | IntrusiveRefCntPtr<ToolProperties> |
| 756 | ToolProps(new ToolProperties(T->getName())); |
| 757 | |
| 758 | std::for_each(PropList->begin(), PropList->end(), |
| 759 | CollectProperties(*ToolProps, OptDescs)); |
| 760 | TPList.push_back(ToolProps); |
| 761 | } |
| 762 | } |
| 763 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 764 | |
| 765 | /// CollectPropertiesFromOptionList - Gather information about |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 766 | /// *global* option properties from the OptionList. |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 767 | void CollectPropertiesFromOptionList (RecordVector::const_iterator B, |
| 768 | RecordVector::const_iterator E, |
| 769 | GlobalOptionDescriptions& OptDescs) |
| 770 | { |
| 771 | // Iterate over a properties list of every Tool definition |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 772 | for (;B!=E;++B) { |
| 773 | RecordVector::value_type T = *B; |
| 774 | // Throws an exception if the value does not exist. |
| 775 | ListInit* PropList = T->getValueAsListInit("options"); |
| 776 | |
Mikhail Glushenkov | 09b51c3 | 2008-05-30 06:27:02 +0000 | [diff] [blame] | 777 | std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs)); |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 781 | /// CheckForSuperfluousOptions - Check that there are no side |
| 782 | /// effect-free options (specified only in the OptionList). Otherwise, |
| 783 | /// output a warning. |
| 784 | void CheckForSuperfluousOptions (const ToolPropertiesList& TPList, |
| 785 | const GlobalOptionDescriptions& OptDescs) { |
| 786 | llvm::StringSet<> nonSuperfluousOptions; |
| 787 | |
| 788 | // Add all options mentioned in the TPList to the set of |
| 789 | // non-superfluous options. |
| 790 | for (ToolPropertiesList::const_iterator B = TPList.begin(), |
| 791 | E = TPList.end(); B != E; ++B) { |
| 792 | const ToolProperties& TP = *(*B); |
| 793 | for (ToolOptionDescriptions::const_iterator B = TP.OptDescs.begin(), |
| 794 | E = TP.OptDescs.end(); B != E; ++B) { |
| 795 | nonSuperfluousOptions.insert(B->first()); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // Check that all options in OptDescs belong to the set of |
| 800 | // non-superfluous options. |
| 801 | for (GlobalOptionDescriptions::const_iterator B = OptDescs.begin(), |
| 802 | E = OptDescs.end(); B != E; ++B) { |
| 803 | const GlobalOptionDescription& Val = B->second; |
| 804 | if (!nonSuperfluousOptions.count(Val.Name) |
| 805 | && Val.Type != OptionType::Alias) |
| 806 | cerr << "Warning: option '-" << Val.Name << "' has no effect! " |
| 807 | "Probable cause: this option is specified only in the OptionList.\n"; |
| 808 | } |
| 809 | } |
| 810 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 811 | /// EmitCaseTest1Arg - Helper function used by |
| 812 | /// EmitCaseConstructHandler. |
| 813 | bool EmitCaseTest1Arg(const std::string& TestName, |
| 814 | const DagInit& d, |
| 815 | const GlobalOptionDescriptions& OptDescs, |
| 816 | std::ostream& O) { |
| 817 | checkNumberOfArguments(&d, 1); |
| 818 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
| 819 | if (TestName == "switch_on") { |
| 820 | const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName); |
| 821 | if (OptDesc.Type != OptionType::Switch) |
| 822 | throw OptName + ": incorrect option type!"; |
| 823 | O << OptDesc.GenVariableName(); |
| 824 | return true; |
| 825 | } else if (TestName == "input_languages_contain") { |
| 826 | O << "InLangs.count(\"" << OptName << "\") != 0"; |
| 827 | return true; |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 828 | } else if (TestName == "in_language") { |
Mikhail Glushenkov | 0737651 | 2008-09-22 20:48:22 +0000 | [diff] [blame] | 829 | // This works only for single-argument Tool::GenerateAction. Join |
| 830 | // tools can process several files in different languages simultaneously. |
| 831 | |
| 832 | // TODO: make this work with Edge::Weight (if possible). |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 833 | O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"'; |
Mikhail Glushenkov | 2e73e85 | 2008-05-30 06:19:52 +0000 | [diff] [blame] | 834 | return true; |
| 835 | } else if (TestName == "not_empty") { |
Mikhail Glushenkov | 92b8da7 | 2008-05-30 06:24:07 +0000 | [diff] [blame] | 836 | if (OptName == "o") { |
| 837 | O << "!OutputFilename.empty()"; |
| 838 | return true; |
| 839 | } |
| 840 | else { |
| 841 | const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName); |
| 842 | if (OptDesc.Type == OptionType::Switch) |
| 843 | throw OptName + ": incorrect option type!"; |
| 844 | O << '!' << OptDesc.GenVariableName() << ".empty()"; |
| 845 | return true; |
| 846 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | /// EmitCaseTest2Args - Helper function used by |
| 853 | /// EmitCaseConstructHandler. |
| 854 | bool EmitCaseTest2Args(const std::string& TestName, |
| 855 | const DagInit& d, |
| 856 | const char* IndentLevel, |
| 857 | const GlobalOptionDescriptions& OptDescs, |
| 858 | std::ostream& O) { |
| 859 | checkNumberOfArguments(&d, 2); |
| 860 | const std::string& OptName = InitPtrToString(d.getArg(0)); |
| 861 | const std::string& OptArg = InitPtrToString(d.getArg(1)); |
| 862 | const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName); |
| 863 | |
| 864 | if (TestName == "parameter_equals") { |
| 865 | if (OptDesc.Type != OptionType::Parameter |
| 866 | && OptDesc.Type != OptionType::Prefix) |
| 867 | throw OptName + ": incorrect option type!"; |
| 868 | O << OptDesc.GenVariableName() << " == \"" << OptArg << "\""; |
| 869 | return true; |
| 870 | } |
| 871 | else if (TestName == "element_in_list") { |
| 872 | if (OptDesc.Type != OptionType::ParameterList |
| 873 | && OptDesc.Type != OptionType::PrefixList) |
| 874 | throw OptName + ": incorrect option type!"; |
| 875 | const std::string& VarName = OptDesc.GenVariableName(); |
| 876 | O << "std::find(" << VarName << ".begin(),\n" |
| 877 | << IndentLevel << Indent1 << VarName << ".end(), \"" |
| 878 | << OptArg << "\") != " << VarName << ".end()"; |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | return false; |
| 883 | } |
| 884 | |
| 885 | // Forward declaration. |
| 886 | // EmitLogicalOperationTest and EmitCaseTest are mutually recursive. |
| 887 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
| 888 | const GlobalOptionDescriptions& OptDescs, |
| 889 | std::ostream& O); |
| 890 | |
| 891 | /// EmitLogicalOperationTest - Helper function used by |
| 892 | /// EmitCaseConstructHandler. |
| 893 | void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp, |
| 894 | const char* IndentLevel, |
| 895 | const GlobalOptionDescriptions& OptDescs, |
| 896 | std::ostream& O) { |
| 897 | O << '('; |
| 898 | for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 899 | const DagInit& InnerTest = InitPtrToDag(d.getArg(j)); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 900 | EmitCaseTest(InnerTest, IndentLevel, OptDescs, O); |
| 901 | if (j != NumArgs - 1) |
| 902 | O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " ("; |
| 903 | else |
| 904 | O << ')'; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /// EmitCaseTest - Helper function used by EmitCaseConstructHandler. |
| 909 | void EmitCaseTest(const DagInit& d, const char* IndentLevel, |
| 910 | const GlobalOptionDescriptions& OptDescs, |
| 911 | std::ostream& O) { |
| 912 | const std::string& TestName = d.getOperator()->getAsString(); |
| 913 | |
| 914 | if (TestName == "and") |
| 915 | EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O); |
| 916 | else if (TestName == "or") |
| 917 | EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O); |
| 918 | else if (EmitCaseTest1Arg(TestName, d, OptDescs, O)) |
| 919 | return; |
| 920 | else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O)) |
| 921 | return; |
| 922 | else |
| 923 | throw TestName + ": unknown edge property!"; |
| 924 | } |
| 925 | |
| 926 | // Emit code that handles the 'case' construct. |
| 927 | // Takes a function object that should emit code for every case clause. |
| 928 | // Callback's type is |
| 929 | // void F(Init* Statement, const char* IndentLevel, std::ostream& O). |
| 930 | template <typename F> |
| 931 | void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel, |
Mikhail Glushenkov | 310d2eb | 2008-05-31 13:43:21 +0000 | [diff] [blame] | 932 | F Callback, bool EmitElseIf, |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 933 | const GlobalOptionDescriptions& OptDescs, |
| 934 | std::ostream& O) { |
| 935 | assert(d->getOperator()->getAsString() == "case"); |
| 936 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 937 | unsigned numArgs = d->getNumArgs(); |
| 938 | if (d->getNumArgs() < 2) |
| 939 | throw "There should be at least one clause in the 'case' expression:\n" |
| 940 | + d->getAsString(); |
| 941 | |
| 942 | for (unsigned i = 0; i != numArgs; ++i) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 943 | const DagInit& Test = InitPtrToDag(d->getArg(i)); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 944 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 945 | // Emit the test. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 946 | if (Test.getOperator()->getAsString() == "default") { |
| 947 | if (i+2 != numArgs) |
| 948 | throw std::string("The 'default' clause should be the last in the" |
| 949 | "'case' construct!"); |
| 950 | O << IndentLevel << "else {\n"; |
| 951 | } |
| 952 | else { |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 953 | O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if ("); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 954 | EmitCaseTest(Test, IndentLevel, OptDescs, O); |
| 955 | O << ") {\n"; |
| 956 | } |
| 957 | |
Mikhail Glushenkov | cb64f4b | 2008-05-30 06:15:47 +0000 | [diff] [blame] | 958 | // Emit the corresponding statement. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 959 | ++i; |
| 960 | if (i == numArgs) |
| 961 | throw "Case construct handler: no corresponding action " |
| 962 | "found for the test " + Test.getAsString() + '!'; |
| 963 | |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 964 | Init* arg = d->getArg(i); |
| 965 | if (dynamic_cast<DagInit*>(arg) |
| 966 | && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") { |
| 967 | EmitCaseConstructHandler(static_cast<DagInit*>(arg), |
| 968 | (std::string(IndentLevel) + Indent1).c_str(), |
| 969 | Callback, EmitElseIf, OptDescs, O); |
| 970 | } |
| 971 | else { |
| 972 | Callback(arg, IndentLevel, O); |
| 973 | } |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 974 | O << IndentLevel << "}\n"; |
| 975 | } |
| 976 | } |
| 977 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 978 | /// EmitForwardOptionPropertyHandlingCode - Helper function used to |
| 979 | /// implement EmitOptionPropertyHandlingCode(). Emits code for |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 980 | /// handling the (forward) and (forward_as) option properties. |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 981 | void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D, |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 982 | const std::string& NewName, |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 983 | std::ostream& O) { |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 984 | const std::string& Name = NewName.empty() |
| 985 | ? ("-" + D.Name) |
| 986 | : NewName; |
| 987 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 988 | switch (D.Type) { |
| 989 | case OptionType::Switch: |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 990 | O << Indent3 << "vec.push_back(\"" << Name << "\");\n"; |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 991 | break; |
| 992 | case OptionType::Parameter: |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 993 | O << Indent3 << "vec.push_back(\"" << Name << "\");\n"; |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 994 | O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n"; |
| 995 | break; |
| 996 | case OptionType::Prefix: |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 997 | O << Indent3 << "vec.push_back(\"" << Name << "\" + " |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 998 | << D.GenVariableName() << ");\n"; |
| 999 | break; |
| 1000 | case OptionType::PrefixList: |
| 1001 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 1002 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 1003 | << Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n" |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 1004 | << Indent4 << "vec.push_back(\"" << Name << "\" + " |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1005 | << "*B);\n"; |
| 1006 | break; |
| 1007 | case OptionType::ParameterList: |
| 1008 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 1009 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 1010 | << Indent3 << "E = " << D.GenVariableName() |
| 1011 | << ".end() ; B != E; ++B) {\n" |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 1012 | << Indent4 << "vec.push_back(\"" << Name << "\");\n" |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1013 | << Indent4 << "vec.push_back(*B);\n" |
| 1014 | << Indent3 << "}\n"; |
| 1015 | break; |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1016 | case OptionType::Alias: |
| 1017 | default: |
| 1018 | throw std::string("Aliases are not allowed in tool option descriptions!"); |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Mikhail Glushenkov | 37f509a | 2008-05-30 06:15:20 +0000 | [diff] [blame] | 1022 | // ToolOptionHasInterestingProperties - A helper function used by |
| 1023 | // EmitOptionPropertyHandlingCode() that tells us whether we should |
| 1024 | // emit any property handling code at all. |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1025 | bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) { |
Mikhail Glushenkov | 37f509a | 2008-05-30 06:15:20 +0000 | [diff] [blame] | 1026 | bool ret = false; |
| 1027 | for (OptionPropertyList::const_iterator B = D.Props.begin(), |
| 1028 | E = D.Props.end(); B != E; ++B) { |
| 1029 | const OptionProperty& OptProp = *B; |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 1030 | if (OptProp.first == OptionPropertyType::AppendCmd |
| 1031 | || OptProp.first == OptionPropertyType::ForwardAs) |
Mikhail Glushenkov | 37f509a | 2008-05-30 06:15:20 +0000 | [diff] [blame] | 1032 | ret = true; |
| 1033 | } |
| 1034 | if (D.isForward() || D.isUnpackValues()) |
| 1035 | ret = true; |
| 1036 | return ret; |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1039 | /// EmitOptionPropertyHandlingCode - Helper function used by |
| 1040 | /// EmitGenerateActionMethod(). Emits code that handles option |
| 1041 | /// properties. |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1042 | void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D, |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1043 | std::ostream& O) |
| 1044 | { |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1045 | if (!ToolOptionHasInterestingProperties(D)) |
| 1046 | return; |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1047 | // Start of the if-clause. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1048 | O << Indent2 << "if ("; |
| 1049 | if (D.Type == OptionType::Switch) |
| 1050 | O << D.GenVariableName(); |
| 1051 | else |
| 1052 | O << '!' << D.GenVariableName() << ".empty()"; |
| 1053 | |
| 1054 | O <<") {\n"; |
| 1055 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1056 | // Handle option properties that take an argument. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1057 | for (OptionPropertyList::const_iterator B = D.Props.begin(), |
| 1058 | E = D.Props.end(); B!=E; ++B) { |
| 1059 | const OptionProperty& val = *B; |
| 1060 | |
| 1061 | switch (val.first) { |
| 1062 | // (append_cmd cmd) property |
| 1063 | case OptionPropertyType::AppendCmd: |
| 1064 | O << Indent3 << "vec.push_back(\"" << val.second << "\");\n"; |
| 1065 | break; |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 1066 | // (forward_as) property |
| 1067 | case OptionPropertyType::ForwardAs: |
| 1068 | EmitForwardOptionPropertyHandlingCode(D, val.second, O); |
| 1069 | break; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1070 | // Other properties with argument |
| 1071 | default: |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // Handle flags |
| 1077 | |
| 1078 | // (forward) property |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1079 | if (D.isForward()) |
Mikhail Glushenkov | fdee954 | 2008-09-22 20:46:19 +0000 | [diff] [blame] | 1080 | EmitForwardOptionPropertyHandlingCode(D, "", O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1081 | |
| 1082 | // (unpack_values) property |
| 1083 | if (D.isUnpackValues()) { |
| 1084 | if (IsListOptionType(D.Type)) { |
| 1085 | O << Indent3 << "for (" << D.GenTypeDeclaration() |
| 1086 | << "::iterator B = " << D.GenVariableName() << ".begin(),\n" |
| 1087 | << Indent3 << "E = " << D.GenVariableName() |
| 1088 | << ".end(); B != E; ++B)\n" |
Mikhail Glushenkov | d83038c | 2008-05-06 18:13:45 +0000 | [diff] [blame] | 1089 | << Indent4 << "llvm::SplitString(*B, vec, \",\");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1090 | } |
| 1091 | else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){ |
Mikhail Glushenkov | d83038c | 2008-05-06 18:13:45 +0000 | [diff] [blame] | 1092 | O << Indent3 << "llvm::SplitString(" |
| 1093 | << D.GenVariableName() << ", vec, \",\");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1094 | } |
| 1095 | else { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1096 | throw std::string("Switches can't have unpack_values property!"); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1100 | // End of the if-clause. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1101 | O << Indent2 << "}\n"; |
| 1102 | } |
| 1103 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1104 | /// SubstituteSpecialCommands - Perform string substitution for $CALL |
| 1105 | /// and $ENV. Helper function used by EmitCmdLineVecFill(). |
| 1106 | std::string SubstituteSpecialCommands(const std::string& cmd) { |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1107 | size_t cparen = cmd.find(")"); |
| 1108 | std::string ret; |
| 1109 | |
| 1110 | if (cmd.find("$CALL(") == 0) { |
| 1111 | if (cmd.size() == 6) |
| 1112 | throw std::string("$CALL invocation: empty argument list!"); |
| 1113 | |
| 1114 | ret += "hooks::"; |
| 1115 | ret += std::string(cmd.begin() + 6, cmd.begin() + cparen); |
| 1116 | ret += "()"; |
| 1117 | } |
| 1118 | else if (cmd.find("$ENV(") == 0) { |
| 1119 | if (cmd.size() == 5) |
| 1120 | throw std::string("$ENV invocation: empty argument list!"); |
| 1121 | |
| 1122 | ret += "std::getenv(\""; |
| 1123 | ret += std::string(cmd.begin() + 5, cmd.begin() + cparen); |
| 1124 | ret += "\")"; |
| 1125 | } |
| 1126 | else { |
| 1127 | throw "Unknown special command: " + cmd; |
| 1128 | } |
| 1129 | |
| 1130 | if (cmd.begin() + cparen + 1 != cmd.end()) { |
| 1131 | ret += " + std::string(\""; |
| 1132 | ret += (cmd.c_str() + cparen + 1); |
| 1133 | ret += "\")"; |
| 1134 | } |
| 1135 | |
| 1136 | return ret; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /// EmitCmdLineVecFill - Emit code that fills in the command line |
| 1140 | /// vector. Helper function used by EmitGenerateActionMethod(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1141 | void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName, |
| 1142 | bool Version, const char* IndentLevel, |
| 1143 | std::ostream& O) { |
| 1144 | StrVector StrVec; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1145 | SplitString(InitPtrToString(CmdLine), StrVec); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1146 | if (StrVec.empty()) |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1147 | throw "Tool " + ToolName + " has empty command line!"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1148 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1149 | StrVector::const_iterator I = StrVec.begin(); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1150 | ++I; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1151 | for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1152 | const std::string& cmd = *I; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1153 | O << IndentLevel; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1154 | if (cmd.at(0) == '$') { |
| 1155 | if (cmd == "$INFILE") { |
| 1156 | if (Version) |
| 1157 | O << "for (PathVector::const_iterator B = inFiles.begin()" |
| 1158 | << ", E = inFiles.end();\n" |
| 1159 | << IndentLevel << "B != E; ++B)\n" |
| 1160 | << IndentLevel << Indent1 << "vec.push_back(B->toString());\n"; |
| 1161 | else |
| 1162 | O << "vec.push_back(inFile.toString());\n"; |
| 1163 | } |
| 1164 | else if (cmd == "$OUTFILE") { |
| 1165 | O << "vec.push_back(outFile.toString());\n"; |
| 1166 | } |
| 1167 | else { |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1168 | O << "vec.push_back(" << SubstituteSpecialCommands(cmd); |
| 1169 | O << ");\n"; |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1170 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1171 | } |
| 1172 | else { |
| 1173 | O << "vec.push_back(\"" << cmd << "\");\n"; |
| 1174 | } |
| 1175 | } |
Mikhail Glushenkov | 34f3762 | 2008-05-30 06:23:29 +0000 | [diff] [blame] | 1176 | O << IndentLevel << "cmd = " |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1177 | << ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0]) |
| 1178 | : "\"" + StrVec[0] + "\"") |
Mikhail Glushenkov | 34f3762 | 2008-05-30 06:23:29 +0000 | [diff] [blame] | 1179 | << ";\n"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1182 | /// EmitCmdLineVecFillCallback - A function object wrapper around |
| 1183 | /// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an |
| 1184 | /// argument to EmitCaseConstructHandler(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1185 | class EmitCmdLineVecFillCallback { |
| 1186 | bool Version; |
| 1187 | const std::string& ToolName; |
| 1188 | public: |
| 1189 | EmitCmdLineVecFillCallback(bool Ver, const std::string& TN) |
| 1190 | : Version(Ver), ToolName(TN) {} |
| 1191 | |
| 1192 | void operator()(const Init* Statement, const char* IndentLevel, |
| 1193 | std::ostream& O) const |
| 1194 | { |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1195 | EmitCmdLineVecFill(Statement, ToolName, Version, |
| 1196 | (std::string(IndentLevel) + Indent1).c_str(), O); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1197 | } |
| 1198 | }; |
| 1199 | |
| 1200 | // EmitGenerateActionMethod - Emit one of two versions of the |
| 1201 | // Tool::GenerateAction() method. |
| 1202 | void EmitGenerateActionMethod (const ToolProperties& P, |
| 1203 | const GlobalOptionDescriptions& OptDescs, |
| 1204 | bool Version, std::ostream& O) { |
| 1205 | if (Version) |
| 1206 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"; |
| 1207 | else |
| 1208 | O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n"; |
| 1209 | |
| 1210 | O << Indent2 << "const sys::Path& outFile,\n" |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1211 | << Indent2 << "const InputLanguagesSet& InLangs,\n" |
| 1212 | << Indent2 << "const LanguageMap& LangMap) const\n" |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1213 | << Indent1 << "{\n" |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1214 | << Indent2 << "const char* cmd;\n" |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1215 | << Indent2 << "std::vector<std::string> vec;\n"; |
| 1216 | |
| 1217 | // cmd_line is either a string or a 'case' construct. |
| 1218 | if (typeid(*P.CmdLine) == typeid(StringInit)) |
| 1219 | EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O); |
| 1220 | else |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1221 | EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2, |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1222 | EmitCmdLineVecFillCallback(Version, P.Name), |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1223 | true, OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1224 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1225 | // For every understood option, emit handling code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1226 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 1227 | E = P.OptDescs.end(); B != E; ++B) { |
| 1228 | const ToolOptionDescription& val = B->second; |
Mikhail Glushenkov | 14ec27f | 2008-05-30 06:10:47 +0000 | [diff] [blame] | 1229 | EmitOptionPropertyHandlingCode(val, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1232 | // Handle the Sink property. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1233 | if (P.isSink()) { |
| 1234 | O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n" |
| 1235 | << Indent3 << "vec.insert(vec.end(), " |
| 1236 | << SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n" |
| 1237 | << Indent2 << "}\n"; |
| 1238 | } |
| 1239 | |
Mikhail Glushenkov | 34f3762 | 2008-05-30 06:23:29 +0000 | [diff] [blame] | 1240 | O << Indent2 << "return Action(cmd, vec);\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1241 | << Indent1 << "}\n\n"; |
| 1242 | } |
| 1243 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1244 | /// EmitGenerateActionMethods - Emit two GenerateAction() methods for |
| 1245 | /// a given Tool class. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1246 | void EmitGenerateActionMethods (const ToolProperties& P, |
| 1247 | const GlobalOptionDescriptions& OptDescs, |
| 1248 | std::ostream& O) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1249 | if (!P.isJoin()) |
| 1250 | O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n" |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1251 | << Indent2 << "const llvm::sys::Path& outFile,\n" |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1252 | << Indent2 << "const InputLanguagesSet& InLangs,\n" |
| 1253 | << Indent2 << "const LanguageMap& LangMap) const\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1254 | << Indent1 << "{\n" |
| 1255 | << Indent2 << "throw std::runtime_error(\"" << P.Name |
| 1256 | << " is not a Join tool!\");\n" |
| 1257 | << Indent1 << "}\n\n"; |
| 1258 | else |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1259 | EmitGenerateActionMethod(P, OptDescs, true, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1260 | |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1261 | EmitGenerateActionMethod(P, OptDescs, false, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Mikhail Glushenkov | 8e7254c | 2008-05-09 08:27:26 +0000 | [diff] [blame] | 1264 | /// EmitIsLastMethod - Emit the IsLast() method for a given Tool |
| 1265 | /// class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1266 | void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) { |
| 1267 | O << Indent1 << "bool IsLast() const {\n" |
| 1268 | << Indent2 << "bool last = false;\n"; |
| 1269 | |
| 1270 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 1271 | E = P.OptDescs.end(); B != E; ++B) { |
| 1272 | const ToolOptionDescription& val = B->second; |
| 1273 | |
| 1274 | if (val.isStopCompilation()) |
| 1275 | O << Indent2 |
| 1276 | << "if (" << val.GenVariableName() |
| 1277 | << ")\n" << Indent3 << "last = true;\n"; |
| 1278 | } |
| 1279 | |
| 1280 | O << Indent2 << "return last;\n" |
| 1281 | << Indent1 << "}\n\n"; |
| 1282 | } |
| 1283 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1284 | /// EmitInOutLanguageMethods - Emit the [Input,Output]Language() |
| 1285 | /// methods for a given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1286 | void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1287 | O << Indent1 << "const char** InputLanguages() const {\n" |
| 1288 | << Indent2 << "return InputLanguages_;\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1289 | << Indent1 << "}\n\n"; |
| 1290 | |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1291 | O << Indent1 << "const char* OutputLanguage() const {\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1292 | << Indent2 << "return \"" << P.OutLanguage << "\";\n" |
| 1293 | << Indent1 << "}\n\n"; |
| 1294 | } |
| 1295 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1296 | /// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a |
| 1297 | /// given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1298 | void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1299 | O << Indent1 << "const char* OutputSuffix() const {\n" |
Mikhail Glushenkov | 5c7578d | 2008-05-30 06:13:02 +0000 | [diff] [blame] | 1300 | << Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n"; |
| 1301 | |
| 1302 | for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(), |
| 1303 | E = P.OptDescs.end(); B != E; ++B) { |
| 1304 | const ToolOptionDescription& OptDesc = B->second; |
| 1305 | for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(), |
| 1306 | E = OptDesc.Props.end(); B != E; ++B) { |
| 1307 | const OptionProperty& OptProp = *B; |
| 1308 | if (OptProp.first == OptionPropertyType::OutputSuffix) { |
| 1309 | O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n" |
| 1310 | << Indent3 << "ret = \"" << OptProp.second << "\";\n"; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | O << Indent2 << "return ret;\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1316 | << Indent1 << "}\n\n"; |
| 1317 | } |
| 1318 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1319 | /// EmitNameMethod - Emit the Name() method for a given Tool class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1320 | void EmitNameMethod (const ToolProperties& P, std::ostream& O) { |
Mikhail Glushenkov | b96cb60 | 2008-05-06 17:24:26 +0000 | [diff] [blame] | 1321 | O << Indent1 << "const char* Name() const {\n" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1322 | << Indent2 << "return \"" << P.Name << "\";\n" |
| 1323 | << Indent1 << "}\n\n"; |
| 1324 | } |
| 1325 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1326 | /// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool |
| 1327 | /// class. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1328 | void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) { |
| 1329 | O << Indent1 << "bool IsJoin() const {\n"; |
| 1330 | if (P.isJoin()) |
| 1331 | O << Indent2 << "return true;\n"; |
| 1332 | else |
| 1333 | O << Indent2 << "return false;\n"; |
| 1334 | O << Indent1 << "}\n\n"; |
| 1335 | } |
| 1336 | |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1337 | /// EmitStaticMemberDefinitions - Emit static member definitions for a |
| 1338 | /// given Tool class. |
| 1339 | void EmitStaticMemberDefinitions(const ToolProperties& P, std::ostream& O) { |
| 1340 | O << "const char* " << P.Name << "::InputLanguages_[] = {"; |
| 1341 | for (StrVector::const_iterator B = P.InLanguage.begin(), |
| 1342 | E = P.InLanguage.end(); B != E; ++B) |
| 1343 | O << '\"' << *B << "\", "; |
| 1344 | O << "0};\n\n"; |
| 1345 | } |
| 1346 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1347 | /// EmitToolClassDefinition - Emit a Tool class definition. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1348 | void EmitToolClassDefinition (const ToolProperties& P, |
| 1349 | const GlobalOptionDescriptions& OptDescs, |
| 1350 | std::ostream& O) { |
| 1351 | if (P.Name == "root") |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1352 | return; |
| 1353 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1354 | // Header |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 1355 | O << "class " << P.Name << " : public "; |
| 1356 | if (P.isJoin()) |
| 1357 | O << "JoinTool"; |
| 1358 | else |
| 1359 | O << "Tool"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1360 | |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1361 | O << "{\nprivate:\n" |
| 1362 | << Indent1 << "static const char* InputLanguages_[];\n\n"; |
| 1363 | |
| 1364 | O << "public:\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1365 | EmitNameMethod(P, O); |
| 1366 | EmitInOutLanguageMethods(P, O); |
| 1367 | EmitOutputSuffixMethod(P, O); |
| 1368 | EmitIsJoinMethod(P, O); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1369 | EmitGenerateActionMethods(P, OptDescs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1370 | EmitIsLastMethod(P, O); |
| 1371 | |
| 1372 | // Close class definition |
Mikhail Glushenkov | 5fe8475 | 2008-05-30 06:24:49 +0000 | [diff] [blame] | 1373 | O << "};\n"; |
| 1374 | |
| 1375 | EmitStaticMemberDefinitions(P, O); |
| 1376 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1379 | /// EmitOptionDescriptions - Iterate over a list of option |
| 1380 | /// descriptions and emit registration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1381 | void EmitOptionDescriptions (const GlobalOptionDescriptions& descs, |
| 1382 | std::ostream& O) |
| 1383 | { |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1384 | std::vector<GlobalOptionDescription> Aliases; |
| 1385 | |
Mikhail Glushenkov | 34f3762 | 2008-05-30 06:23:29 +0000 | [diff] [blame] | 1386 | // Emit static cl::Option variables. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1387 | for (GlobalOptionDescriptions::const_iterator B = descs.begin(), |
| 1388 | E = descs.end(); B!=E; ++B) { |
| 1389 | const GlobalOptionDescription& val = B->second; |
| 1390 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1391 | if (val.Type == OptionType::Alias) { |
| 1392 | Aliases.push_back(val); |
| 1393 | continue; |
| 1394 | } |
| 1395 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1396 | O << val.GenTypeDeclaration() << ' ' |
| 1397 | << val.GenVariableName() |
| 1398 | << "(\"" << val.Name << '\"'; |
| 1399 | |
| 1400 | if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList) |
| 1401 | O << ", cl::Prefix"; |
| 1402 | |
| 1403 | if (val.isRequired()) { |
| 1404 | switch (val.Type) { |
| 1405 | case OptionType::PrefixList: |
| 1406 | case OptionType::ParameterList: |
| 1407 | O << ", cl::OneOrMore"; |
| 1408 | break; |
| 1409 | default: |
| 1410 | O << ", cl::Required"; |
| 1411 | } |
| 1412 | } |
| 1413 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1414 | if (!val.Help.empty()) |
| 1415 | O << ", cl::desc(\"" << val.Help << "\")"; |
| 1416 | |
| 1417 | O << ");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Mikhail Glushenkov | 6be4ffc | 2008-05-30 06:22:52 +0000 | [diff] [blame] | 1420 | // Emit the aliases (they should go after all the 'proper' options). |
| 1421 | for (std::vector<GlobalOptionDescription>::const_iterator |
| 1422 | B = Aliases.begin(), E = Aliases.end(); B != E; ++B) { |
| 1423 | const GlobalOptionDescription& val = *B; |
| 1424 | |
| 1425 | O << val.GenTypeDeclaration() << ' ' |
| 1426 | << val.GenVariableName() |
| 1427 | << "(\"" << val.Name << '\"'; |
| 1428 | |
| 1429 | GlobalOptionDescriptions::container_type |
| 1430 | ::const_iterator F = descs.Descriptions.find(val.Help); |
| 1431 | if (F != descs.Descriptions.end()) |
| 1432 | O << ", cl::aliasopt(" << F->second.GenVariableName() << ")"; |
| 1433 | else |
| 1434 | throw val.Name + ": alias to an unknown option!"; |
| 1435 | |
| 1436 | O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n"; |
| 1437 | } |
| 1438 | |
| 1439 | // Emit the sink option. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1440 | if (descs.HasSink) |
| 1441 | O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n"; |
| 1442 | |
| 1443 | O << '\n'; |
| 1444 | } |
| 1445 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1446 | /// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1447 | void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O) |
| 1448 | { |
| 1449 | // Get the relevant field out of RecordKeeper |
| 1450 | Record* LangMapRecord = Records.getDef("LanguageMap"); |
| 1451 | if (!LangMapRecord) |
| 1452 | throw std::string("Language map definition not found!"); |
| 1453 | |
| 1454 | ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map"); |
| 1455 | if (!LangsToSuffixesList) |
| 1456 | throw std::string("Error in the language map definition!"); |
| 1457 | |
| 1458 | // Generate code |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1459 | O << "namespace {\n\n"; |
| 1460 | O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1461 | |
| 1462 | for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) { |
| 1463 | Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i); |
| 1464 | |
| 1465 | const std::string& Lang = LangToSuffixes->getValueAsString("lang"); |
| 1466 | const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes"); |
| 1467 | |
| 1468 | for (unsigned i = 0; i < Suffixes->size(); ++i) |
Mikhail Glushenkov | 11a353a | 2008-09-22 20:47:46 +0000 | [diff] [blame] | 1469 | O << Indent1 << "langMap[\"" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1470 | << InitPtrToString(Suffixes->getElement(i)) |
| 1471 | << "\"] = \"" << Lang << "\";\n"; |
| 1472 | } |
| 1473 | |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1474 | O << "}\n\n}\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1477 | /// FillInToolToLang - Fills in two tables that map tool names to |
| 1478 | /// (input, output) languages. Used by the typechecker. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1479 | void FillInToolToLang (const ToolPropertiesList& TPList, |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1480 | StringMap<StringSet<> >& ToolToInLang, |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1481 | StringMap<std::string>& ToolToOutLang) { |
| 1482 | for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end(); |
| 1483 | B != E; ++B) { |
| 1484 | const ToolProperties& P = *(*B); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1485 | for (StrVector::const_iterator B = P.InLanguage.begin(), |
| 1486 | E = P.InLanguage.end(); B != E; ++B) |
| 1487 | ToolToInLang[P.Name].insert(*B); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1488 | ToolToOutLang[P.Name] = P.OutLanguage; |
| 1489 | } |
| 1490 | } |
| 1491 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1492 | /// TypecheckGraph - Check that names for output and input languages |
| 1493 | /// on all edges do match. |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1494 | // TOFIX: It would be nice if this function also checked for cycles |
| 1495 | // and multiple default edges in the graph (better error |
| 1496 | // reporting). Unfortunately, it is awkward to do right now because |
| 1497 | // our intermediate representation is not sufficiently |
Mikhail Glushenkov | 5ccf28f | 2008-09-22 20:45:17 +0000 | [diff] [blame] | 1498 | // sophisticated. Algorithms like these require a real graph instead of |
| 1499 | // an AST. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1500 | void TypecheckGraph (Record* CompilationGraph, |
| 1501 | const ToolPropertiesList& TPList) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1502 | StringMap<StringSet<> > ToolToInLang; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1503 | StringMap<std::string> ToolToOutLang; |
| 1504 | |
| 1505 | FillInToolToLang(TPList, ToolToInLang, ToolToOutLang); |
| 1506 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1507 | StringMap<std::string>::iterator IAE = ToolToOutLang.end(); |
| 1508 | StringMap<StringSet<> >::iterator IBE = ToolToInLang.end(); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1509 | |
| 1510 | for (unsigned i = 0; i < edges->size(); ++i) { |
| 1511 | Record* Edge = edges->getElementAsRecord(i); |
| 1512 | Record* A = Edge->getValueAsDef("a"); |
| 1513 | Record* B = Edge->getValueAsDef("b"); |
| 1514 | StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName()); |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1515 | StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName()); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1516 | if (IA == IAE) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1517 | throw A->getName() + ": no such tool!"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1518 | if (IB == IBE) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1519 | throw B->getName() + ": no such tool!"; |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1520 | if (A->getName() != "root" && IB->second.count(IA->second) == 0) |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1521 | throw "Edge " + A->getName() + "->" + B->getName() |
| 1522 | + ": output->input language mismatch"; |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1523 | if (B->getName() == "root") |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1524 | throw std::string("Edges back to the root are not allowed!"); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1528 | /// IncDecWeight - Helper function passed to EmitCaseConstructHandler() |
| 1529 | /// by EmitEdgeClass(). |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1530 | void IncDecWeight (const Init* i, const char* IndentLevel, |
| 1531 | std::ostream& O) { |
Mikhail Glushenkov | ffcf3a1 | 2008-05-30 06:18:16 +0000 | [diff] [blame] | 1532 | const DagInit& d = InitPtrToDag(i); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1533 | const std::string& OpName = d.getOperator()->getAsString(); |
| 1534 | |
| 1535 | if (OpName == "inc_weight") |
| 1536 | O << IndentLevel << Indent1 << "ret += "; |
| 1537 | else if (OpName == "dec_weight") |
| 1538 | O << IndentLevel << Indent1 << "ret -= "; |
| 1539 | else |
| 1540 | throw "Unknown operator in edge properties list: " + OpName + '!'; |
| 1541 | |
| 1542 | if (d.getNumArgs() > 0) |
| 1543 | O << InitPtrToInt(d.getArg(0)) << ";\n"; |
| 1544 | else |
| 1545 | O << "2;\n"; |
| 1546 | |
Mikhail Glushenkov | 2906355 | 2008-05-06 18:18:20 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1549 | /// EmitEdgeClass - Emit a single Edge# class. |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1550 | void EmitEdgeClass (unsigned N, const std::string& Target, |
| 1551 | DagInit* Case, const GlobalOptionDescriptions& OptDescs, |
| 1552 | std::ostream& O) { |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Class constructor. |
| 1555 | O << "class Edge" << N << ": public Edge {\n" |
| 1556 | << "public:\n" |
| 1557 | << Indent1 << "Edge" << N << "() : Edge(\"" << Target |
| 1558 | << "\") {}\n\n" |
| 1559 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1560 | // Function Weight(). |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame] | 1561 | << Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n" |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1562 | << Indent2 << "unsigned ret = 0;\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1563 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1564 | // Handle the 'case' construct. |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1565 | EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 1566 | |
| 1567 | O << Indent2 << "return ret;\n" |
| 1568 | << Indent1 << "};\n\n};\n\n"; |
Mikhail Glushenkov | 9ef501b | 2008-05-06 17:23:14 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1571 | /// EmitEdgeClasses - Emit Edge* classes that represent graph edges. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1572 | void EmitEdgeClasses (Record* CompilationGraph, |
| 1573 | const GlobalOptionDescriptions& OptDescs, |
| 1574 | std::ostream& O) { |
| 1575 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
| 1576 | |
| 1577 | for (unsigned i = 0; i < edges->size(); ++i) { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1578 | Record* Edge = edges->getElementAsRecord(i); |
| 1579 | Record* B = Edge->getValueAsDef("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1580 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1581 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1582 | if (isDagEmpty(Weight)) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1583 | continue; |
| 1584 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1585 | EmitEdgeClass(i, B->getName(), Weight, OptDescs, O); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1589 | /// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph() |
| 1590 | /// function. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1591 | void EmitPopulateCompilationGraph (Record* CompilationGraph, |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1592 | std::ostream& O) |
| 1593 | { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1594 | ListInit* edges = CompilationGraph->getValueAsListInit("edges"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Generate code |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1597 | O << "namespace {\n\n"; |
| 1598 | O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1599 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1600 | // Insert vertices |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1601 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1602 | RecordVector Tools = Records.getAllDerivedDefinitions("Tool"); |
| 1603 | if (Tools.empty()) |
| 1604 | throw std::string("No tool definitions found!"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1605 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1606 | for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) { |
| 1607 | const std::string& Name = (*B)->getName(); |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1608 | if (Name != "root") |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1609 | O << Indent1 << "G.insertNode(new " |
| 1610 | << Name << "());\n"; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | O << '\n'; |
| 1614 | |
| 1615 | // Insert edges |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 1616 | for (unsigned i = 0; i < edges->size(); ++i) { |
| 1617 | Record* Edge = edges->getElementAsRecord(i); |
| 1618 | Record* A = Edge->getValueAsDef("a"); |
| 1619 | Record* B = Edge->getValueAsDef("b"); |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1620 | DagInit* Weight = Edge->getValueAsDag("weight"); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1621 | |
| 1622 | O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", "; |
| 1623 | |
Mikhail Glushenkov | e5557f4 | 2008-05-30 06:08:50 +0000 | [diff] [blame] | 1624 | if (isDagEmpty(Weight)) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 1625 | O << "new SimpleEdge(\"" << B->getName() << "\")"; |
| 1626 | else |
| 1627 | O << "new Edge" << i << "()"; |
| 1628 | |
| 1629 | O << ");\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1632 | O << "}\n\n}\n\n"; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1635 | /// ExtractHookNames - Extract the hook names from all instances of |
| 1636 | /// $CALL(HookName) in the provided command line string. Helper |
| 1637 | /// function used by FillInHookNames(). |
| 1638 | void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) { |
| 1639 | StrVector cmds; |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1640 | llvm::SplitString(InitPtrToString(CmdLine), cmds); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1641 | for (StrVector::const_iterator B = cmds.begin(), E = cmds.end(); |
| 1642 | B != E; ++B) { |
| 1643 | const std::string& cmd = *B; |
| 1644 | if (cmd.find("$CALL(") == 0) { |
| 1645 | if (cmd.size() == 6) |
| 1646 | throw std::string("$CALL invocation: empty argument list!"); |
Mikhail Glushenkov | 2242456 | 2008-05-30 06:13:29 +0000 | [diff] [blame] | 1647 | HookNames.push_back(std::string(cmd.begin() + 6, |
| 1648 | cmd.begin() + cmd.find(")"))); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1653 | /// ExtractHookNamesFromCaseConstruct - Extract hook names from the |
| 1654 | /// 'case' expression, handle nesting. Helper function used by |
| 1655 | /// FillInHookNames(). |
| 1656 | void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) { |
| 1657 | const DagInit& d = InitPtrToDag(Case); |
| 1658 | bool even = false; |
| 1659 | for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end(); |
| 1660 | B != E; ++B) { |
| 1661 | Init* arg = *B; |
| 1662 | if (even && dynamic_cast<DagInit*>(arg) |
| 1663 | && static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") |
| 1664 | ExtractHookNamesFromCaseConstruct(arg, HookNames); |
| 1665 | else if (even) |
| 1666 | ExtractHookNames(arg, HookNames); |
| 1667 | even = !even; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1671 | /// FillInHookNames - Actually extract the hook names from all command |
| 1672 | /// line strings. Helper function used by EmitHookDeclarations(). |
| 1673 | void FillInHookNames(const ToolPropertiesList& TPList, |
| 1674 | StrVector& HookNames) { |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1675 | // For all command lines: |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1676 | for (ToolPropertiesList::const_iterator B = TPList.begin(), |
| 1677 | E = TPList.end(); B != E; ++B) { |
| 1678 | const ToolProperties& P = *(*B); |
| 1679 | if (!P.CmdLine) |
| 1680 | continue; |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1681 | if (dynamic_cast<StringInit*>(P.CmdLine)) |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1682 | // This is a string. |
| 1683 | ExtractHookNames(P.CmdLine, HookNames); |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1684 | else |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1685 | // This is a 'case' construct. |
Mikhail Glushenkov | 2d0dc9a | 2008-05-30 06:22:15 +0000 | [diff] [blame] | 1686 | ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames); |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /// EmitHookDeclarations - Parse CmdLine fields of all the tool |
| 1691 | /// property records and emit hook function declaration for each |
| 1692 | /// instance of $CALL(HookName). |
| 1693 | void EmitHookDeclarations(const ToolPropertiesList& ToolProps, |
| 1694 | std::ostream& O) { |
| 1695 | StrVector HookNames; |
| 1696 | FillInHookNames(ToolProps, HookNames); |
| 1697 | if (HookNames.empty()) |
| 1698 | return; |
| 1699 | std::sort(HookNames.begin(), HookNames.end()); |
| 1700 | StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end()); |
| 1701 | |
| 1702 | O << "namespace hooks {\n"; |
| 1703 | for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B) |
| 1704 | O << Indent1 << "std::string " << *B << "();\n"; |
| 1705 | |
| 1706 | O << "}\n\n"; |
| 1707 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1708 | |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1709 | /// EmitRegisterPlugin - Emit code to register this plugin. |
| 1710 | void EmitRegisterPlugin(std::ostream& O) { |
| 1711 | O << "namespace {\n\n" |
| 1712 | << "struct Plugin : public llvmc::BasePlugin {\n" |
| 1713 | << Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n" |
| 1714 | << Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n" |
| 1715 | << Indent1 |
| 1716 | << "void PopulateCompilationGraph(CompilationGraph& graph) const\n" |
| 1717 | << Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n" |
| 1718 | << "};\n\n" |
| 1719 | |
| 1720 | << "static llvmc::RegisterPlugin<Plugin> RP;\n\n}\n\n"; |
| 1721 | } |
| 1722 | |
| 1723 | /// EmitInclude - Emit necessary #include directives. |
| 1724 | void EmitIncludes(std::ostream& O) { |
| 1725 | O << "#include \"CompilationGraph.h\"\n" |
| 1726 | << "#include \"Plugin.h\"\n" |
| 1727 | << "#include \"Tool.h\"\n\n" |
| 1728 | |
| 1729 | << "#include \"llvm/ADT/StringExtras.h\"\n" |
| 1730 | << "#include \"llvm/Support/CommandLine.h\"\n\n" |
| 1731 | |
| 1732 | << "#include <cstdlib>\n" |
| 1733 | << "#include <stdexcept>\n\n" |
| 1734 | |
| 1735 | << "using namespace llvm;\n" |
| 1736 | << "using namespace llvmc;\n\n" |
| 1737 | |
| 1738 | << "extern cl::opt<std::string> OutputFilename;\n\n"; |
| 1739 | } |
| 1740 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1741 | // End of anonymous namespace |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 1742 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1743 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1744 | /// run - The back-end entry point. |
Mikhail Glushenkov | 895820d | 2008-05-06 18:12:03 +0000 | [diff] [blame] | 1745 | void LLVMCConfigurationEmitter::run (std::ostream &O) { |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 1746 | try { |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1747 | |
| 1748 | // Emit file header. |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 1749 | EmitSourceFileHeader("LLVMC Configuration Library", O); |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1750 | EmitIncludes(O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1751 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1752 | // Get a list of all defined Tools. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1753 | RecordVector Tools = Records.getAllDerivedDefinitions("Tool"); |
| 1754 | if (Tools.empty()) |
| 1755 | throw std::string("No tool definitions found!"); |
| 1756 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1757 | // Gather information from the Tool description dags. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1758 | ToolPropertiesList tool_props; |
| 1759 | GlobalOptionDescriptions opt_descs; |
| 1760 | CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs); |
| 1761 | |
Mikhail Glushenkov | e4322895 | 2008-05-30 06:26:08 +0000 | [diff] [blame] | 1762 | RecordVector OptionLists = Records.getAllDerivedDefinitions("OptionList"); |
| 1763 | CollectPropertiesFromOptionList(OptionLists.begin(), OptionLists.end(), |
| 1764 | opt_descs); |
| 1765 | |
Mikhail Glushenkov | a7d0ae3 | 2008-05-30 06:28:37 +0000 | [diff] [blame] | 1766 | // Check that there are no options without side effects (specified |
| 1767 | // only in the OptionList). |
| 1768 | CheckForSuperfluousOptions(tool_props, opt_descs); |
| 1769 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1770 | // Emit global option registration code. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1771 | EmitOptionDescriptions(opt_descs, O); |
| 1772 | |
Mikhail Glushenkov | 08bd2e7 | 2008-05-30 06:12:24 +0000 | [diff] [blame] | 1773 | // Emit hook declarations. |
| 1774 | EmitHookDeclarations(tool_props, O); |
| 1775 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1776 | // Emit PopulateLanguageMap() function |
| 1777 | // (a language map maps from file extensions to language names). |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1778 | EmitPopulateLanguageMap(Records, O); |
| 1779 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1780 | // Emit Tool classes. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1781 | for (ToolPropertiesList::const_iterator B = tool_props.begin(), |
| 1782 | E = tool_props.end(); B!=E; ++B) |
Mikhail Glushenkov | b5ccfbf | 2008-05-30 06:10:19 +0000 | [diff] [blame] | 1783 | EmitToolClassDefinition(*(*B), opt_descs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1784 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1785 | Record* CompilationGraphRecord = Records.getDef("CompilationGraph"); |
| 1786 | if (!CompilationGraphRecord) |
| 1787 | throw std::string("Compilation graph description not found!"); |
| 1788 | |
| 1789 | // Typecheck the compilation graph. |
| 1790 | TypecheckGraph(CompilationGraphRecord, tool_props); |
| 1791 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1792 | // Emit Edge# classes. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1793 | EmitEdgeClasses(CompilationGraphRecord, opt_descs, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1794 | |
Mikhail Glushenkov | 4561ab5 | 2008-05-07 21:50:19 +0000 | [diff] [blame] | 1795 | // Emit PopulateCompilationGraph() function. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 1796 | EmitPopulateCompilationGraph(CompilationGraphRecord, O); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1797 | |
Mikhail Glushenkov | c82ce4a | 2008-09-22 20:49:34 +0000 | [diff] [blame^] | 1798 | // Emit code for plugin registration. |
| 1799 | EmitRegisterPlugin(O); |
| 1800 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1801 | // EOF |
Mikhail Glushenkov | 4fb71ea | 2008-05-30 06:21:48 +0000 | [diff] [blame] | 1802 | } catch (std::exception& Error) { |
| 1803 | throw Error.what() + std::string(" - usually this means a syntax error."); |
| 1804 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 1805 | } |