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