Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 1 | //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/TableGen/Error.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/SmallString.h" |
| 13 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | 7f00f87 | 2013-01-02 10:26:28 +0000 | [diff] [blame] | 14 | #include "llvm/TableGen/Record.h" |
| 15 | #include "llvm/TableGen/TableGenBackend.h" |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 16 | #include <map> |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | static int StrCmpOptionName(const char *A, const char *B) { |
Rui Ueyama | 1997734 | 2013-08-28 00:02:06 +0000 | [diff] [blame^] | 21 | char a = *A, b = *B; |
| 22 | while (a == b) { |
| 23 | if (a == '\0') |
| 24 | return 0; |
| 25 | |
| 26 | a = *++A; |
| 27 | b = *++B; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 28 | } |
Rui Ueyama | 1997734 | 2013-08-28 00:02:06 +0000 | [diff] [blame^] | 29 | |
| 30 | if (a == '\0') // A is a prefix of B. |
| 31 | return 1; |
| 32 | if (b == '\0') // B is a prefix of A. |
| 33 | return -1; |
| 34 | |
| 35 | // Otherwise lexicographic. |
| 36 | return (a < b) ? -1 : 1; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int CompareOptionRecords(const void *Av, const void *Bv) { |
| 40 | const Record *A = *(const Record*const*) Av; |
| 41 | const Record *B = *(const Record*const*) Bv; |
| 42 | |
| 43 | // Sentinel options precede all others and are only ordered by precedence. |
| 44 | bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel"); |
| 45 | bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel"); |
| 46 | if (ASent != BSent) |
| 47 | return ASent ? -1 : 1; |
| 48 | |
| 49 | // Compare options by name, unless they are sentinels. |
| 50 | if (!ASent) |
| 51 | if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(), |
| 52 | B->getValueAsString("Name").c_str())) |
Rui Ueyama | 1997734 | 2013-08-28 00:02:06 +0000 | [diff] [blame^] | 53 | return Cmp; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 54 | |
| 55 | if (!ASent) { |
| 56 | std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes"); |
| 57 | std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes"); |
| 58 | |
| 59 | for (std::vector<std::string>::const_iterator APre = APrefixes.begin(), |
| 60 | AEPre = APrefixes.end(), |
| 61 | BPre = BPrefixes.begin(), |
| 62 | BEPre = BPrefixes.end(); |
| 63 | APre != AEPre && |
| 64 | BPre != BEPre; |
| 65 | ++APre, ++BPre) { |
| 66 | if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str())) |
| 67 | return Cmp; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Then by the kind precedence; |
| 72 | int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
| 73 | int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
| 74 | if (APrec == BPrec && |
| 75 | A->getValueAsListOfStrings("Prefixes") == |
| 76 | B->getValueAsListOfStrings("Prefixes")) { |
| 77 | PrintError(A->getLoc(), Twine("Option is equivilent to")); |
| 78 | PrintError(B->getLoc(), Twine("Other defined here")); |
| 79 | PrintFatalError("Equivalent Options found."); |
| 80 | } |
| 81 | return APrec < BPrec ? -1 : 1; |
| 82 | } |
| 83 | |
| 84 | static const std::string getOptionName(const Record &R) { |
| 85 | // Use the record name unless EnumName is defined. |
| 86 | if (isa<UnsetInit>(R.getValueInit("EnumName"))) |
| 87 | return R.getName(); |
| 88 | |
| 89 | return R.getValueAsString("EnumName"); |
| 90 | } |
| 91 | |
| 92 | static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) { |
| 93 | OS << '"'; |
| 94 | OS.write_escaped(Str); |
| 95 | OS << '"'; |
| 96 | return OS; |
| 97 | } |
| 98 | |
| 99 | /// OptParserEmitter - This tablegen backend takes an input .td file |
| 100 | /// describing a list of options and emits a data structure for parsing and |
| 101 | /// working with those options when given an input command line. |
| 102 | namespace llvm { |
| 103 | void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) { |
| 104 | // Get the option groups and options. |
| 105 | const std::vector<Record*> &Groups = |
| 106 | Records.getAllDerivedDefinitions("OptionGroup"); |
| 107 | std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option"); |
| 108 | |
| 109 | emitSourceFileHeader("Option Parsing Definitions", OS); |
| 110 | |
| 111 | array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords); |
| 112 | // Generate prefix groups. |
| 113 | typedef SmallVector<SmallString<2>, 2> PrefixKeyT; |
| 114 | typedef std::map<PrefixKeyT, std::string> PrefixesT; |
| 115 | PrefixesT Prefixes; |
| 116 | Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0")); |
| 117 | unsigned CurPrefix = 0; |
| 118 | for (unsigned i = 0, e = Opts.size(); i != e; ++i) { |
| 119 | const Record &R = *Opts[i]; |
| 120 | std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes"); |
| 121 | PrefixKeyT prfkey(prf.begin(), prf.end()); |
| 122 | unsigned NewPrefix = CurPrefix + 1; |
| 123 | if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") + |
| 124 | Twine(NewPrefix)).str())).second) |
| 125 | CurPrefix = NewPrefix; |
| 126 | } |
| 127 | |
| 128 | // Dump prefixes. |
| 129 | |
| 130 | OS << "/////////\n"; |
| 131 | OS << "// Prefixes\n\n"; |
| 132 | OS << "#ifdef PREFIX\n"; |
| 133 | OS << "#define COMMA ,\n"; |
| 134 | for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end(); |
| 135 | I != E; ++I) { |
| 136 | OS << "PREFIX("; |
| 137 | |
| 138 | // Prefix name. |
| 139 | OS << I->second; |
| 140 | |
| 141 | // Prefix values. |
| 142 | OS << ", {"; |
| 143 | for (PrefixKeyT::const_iterator PI = I->first.begin(), |
| 144 | PE = I->first.end(); PI != PE; ++PI) { |
| 145 | OS << "\"" << *PI << "\" COMMA "; |
| 146 | } |
| 147 | OS << "0})\n"; |
| 148 | } |
| 149 | OS << "#undef COMMA\n"; |
| 150 | OS << "#endif\n\n"; |
| 151 | |
| 152 | OS << "/////////\n"; |
| 153 | OS << "// Groups\n\n"; |
| 154 | OS << "#ifdef OPTION\n"; |
| 155 | for (unsigned i = 0, e = Groups.size(); i != e; ++i) { |
| 156 | const Record &R = *Groups[i]; |
| 157 | |
| 158 | // Start a single option entry. |
Hans Wennborg | a15d5db | 2013-07-31 23:28:51 +0000 | [diff] [blame] | 159 | OS << "OPTION("; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 160 | |
| 161 | // The option prefix; |
| 162 | OS << "0"; |
| 163 | |
| 164 | // The option string. |
| 165 | OS << ", \"" << R.getValueAsString("Name") << '"'; |
| 166 | |
| 167 | // The option identifier name. |
| 168 | OS << ", "<< getOptionName(R); |
| 169 | |
| 170 | // The option kind. |
| 171 | OS << ", Group"; |
| 172 | |
| 173 | // The containing option group (if any). |
| 174 | OS << ", "; |
| 175 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) |
| 176 | OS << getOptionName(*DI->getDef()); |
| 177 | else |
| 178 | OS << "INVALID"; |
| 179 | |
| 180 | // The other option arguments (unused for groups). |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 181 | OS << ", INVALID, 0, 0, 0"; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 182 | |
| 183 | // The option help text. |
| 184 | if (!isa<UnsetInit>(R.getValueInit("HelpText"))) { |
| 185 | OS << ",\n"; |
| 186 | OS << " "; |
| 187 | write_cstring(OS, R.getValueAsString("HelpText")); |
| 188 | } else |
| 189 | OS << ", 0"; |
| 190 | |
| 191 | // The option meta-variable name (unused). |
| 192 | OS << ", 0)\n"; |
| 193 | } |
| 194 | OS << "\n"; |
| 195 | |
| 196 | OS << "//////////\n"; |
| 197 | OS << "// Options\n\n"; |
| 198 | for (unsigned i = 0, e = Opts.size(); i != e; ++i) { |
| 199 | const Record &R = *Opts[i]; |
| 200 | |
| 201 | // Start a single option entry. |
Hans Wennborg | a15d5db | 2013-07-31 23:28:51 +0000 | [diff] [blame] | 202 | OS << "OPTION("; |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 203 | |
| 204 | // The option prefix; |
| 205 | std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes"); |
| 206 | OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", "; |
| 207 | |
| 208 | // The option string. |
| 209 | write_cstring(OS, R.getValueAsString("Name")); |
| 210 | |
| 211 | // The option identifier name. |
| 212 | OS << ", "<< getOptionName(R); |
| 213 | |
| 214 | // The option kind. |
| 215 | OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name"); |
| 216 | |
| 217 | // The containing option group (if any). |
| 218 | OS << ", "; |
| 219 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) |
| 220 | OS << getOptionName(*DI->getDef()); |
| 221 | else |
| 222 | OS << "INVALID"; |
| 223 | |
| 224 | // The option alias (if any). |
| 225 | OS << ", "; |
| 226 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias"))) |
| 227 | OS << getOptionName(*DI->getDef()); |
| 228 | else |
| 229 | OS << "INVALID"; |
| 230 | |
Hans Wennborg | 9dd8c0c | 2013-07-31 22:44:41 +0000 | [diff] [blame] | 231 | // The option alias arguments (if any). |
| 232 | // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"] |
| 233 | // would become "foo\0bar\0". Note that the compiler adds an implicit |
| 234 | // terminating \0 at the end. |
| 235 | OS << ", "; |
| 236 | std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs"); |
| 237 | if (AliasArgs.size() == 0) { |
| 238 | OS << "0"; |
| 239 | } else { |
| 240 | OS << "\""; |
| 241 | for (size_t i = 0, e = AliasArgs.size(); i != e; ++i) |
| 242 | OS << AliasArgs[i] << "\\0"; |
| 243 | OS << "\""; |
| 244 | } |
| 245 | |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 246 | // The option flags. |
| 247 | const ListInit *LI = R.getValueAsListInit("Flags"); |
| 248 | if (LI->empty()) { |
| 249 | OS << ", 0"; |
| 250 | } else { |
| 251 | OS << ", "; |
| 252 | for (unsigned i = 0, e = LI->size(); i != e; ++i) { |
| 253 | if (i) |
| 254 | OS << " | "; |
| 255 | OS << cast<DefInit>(LI->getElement(i))->getDef()->getName(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // The option parameter field. |
| 260 | OS << ", " << R.getValueAsInt("NumArgs"); |
| 261 | |
| 262 | // The option help text. |
| 263 | if (!isa<UnsetInit>(R.getValueInit("HelpText"))) { |
| 264 | OS << ",\n"; |
| 265 | OS << " "; |
| 266 | write_cstring(OS, R.getValueAsString("HelpText")); |
| 267 | } else |
| 268 | OS << ", 0"; |
| 269 | |
| 270 | // The option meta-variable name. |
| 271 | OS << ", "; |
| 272 | if (!isa<UnsetInit>(R.getValueInit("MetaVarName"))) |
| 273 | write_cstring(OS, R.getValueAsString("MetaVarName")); |
| 274 | else |
| 275 | OS << "0"; |
| 276 | |
| 277 | OS << ")\n"; |
| 278 | } |
Michael J. Spencer | 96a564f | 2012-12-05 00:29:32 +0000 | [diff] [blame] | 279 | OS << "#endif\n"; |
| 280 | } |
| 281 | } // end namespace llvm |