1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "OptEmitter.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/TableGen/TableGenBackend.h"
16 #include <cstring>
17 #include <map>
18 #include <memory>
19 
20 using namespace llvm;
21 
22 static std::string getOptionName(const Record &R) {
23   // Use the record name unless EnumName is defined.
24   if (isa<UnsetInit>(R.getValueInit("EnumName")))
25     return std::string(R.getName());
26 
27   return std::string(R.getValueAsString("EnumName"));
28 }
29 
30 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
31   OS << '"';
32   OS.write_escaped(Str);
33   OS << '"';
34   return OS;
35 }
36 
37 static std::string getOptionSpelling(const Record &R, size_t &PrefixLength) {
38   std::vector<StringRef> Prefixes = R.getValueAsListOfStrings("Prefixes");
39   StringRef Name = R.getValueAsString("Name");
40 
41   if (Prefixes.empty()) {
42     PrefixLength = 0;
43     return Name.str();
44   }
45 
46   PrefixLength = Prefixes[0].size();
47   return (Twine(Prefixes[0]) + Twine(Name)).str();
48 }
49 
50 static std::string getOptionSpelling(const Record &R) {
51   size_t PrefixLength;
52   return getOptionSpelling(R, PrefixLength);
53 }
54 
55 static void emitNameUsingSpelling(raw_ostream &OS, const Record &R) {
56   size_t PrefixLength;
57   OS << "llvm::StringLiteral(";
58   write_cstring(
59       OS, StringRef(getOptionSpelling(R, PrefixLength)).substr(PrefixLength));
60   OS << ")";
61 }
62 
63 class MarshallingInfo {
64 public:
65   static constexpr const char *MacroName = "OPTION_WITH_MARSHALLING";
66   const Record &R;
67   bool ShouldAlwaysEmit;
68   StringRef MacroPrefix;
69   StringRef KeyPath;
70   StringRef DefaultValue;
71   StringRef NormalizedValuesScope;
72   StringRef ImpliedCheck;
73   StringRef ImpliedValue;
74   StringRef ShouldParse;
75   StringRef Normalizer;
76   StringRef Denormalizer;
77   StringRef ValueMerger;
78   StringRef ValueExtractor;
79   int TableIndex = -1;
80   std::vector<StringRef> Values;
81   std::vector<StringRef> NormalizedValues;
82   std::string ValueTableName;
83 
84   static size_t NextTableIndex;
85 
86   static constexpr const char *ValueTablePreamble = R"(
87 struct SimpleEnumValue {
88   const char *Name;
89   unsigned Value;
90 };
91 
92 struct SimpleEnumValueTable {
93   const SimpleEnumValue *Table;
94   unsigned Size;
95 };
96 )";
97 
98   static constexpr const char *ValueTablesDecl =
99       "static const SimpleEnumValueTable SimpleEnumValueTables[] = ";
100 
101   MarshallingInfo(const Record &R) : R(R) {}
102 
103   std::string getMacroName() const {
104     return (MacroPrefix + MarshallingInfo::MacroName).str();
105   }
106 
107   void emit(raw_ostream &OS) const {
108     write_cstring(OS, StringRef(getOptionSpelling(R)));
109     OS << ", ";
110     OS << ShouldParse;
111     OS << ", ";
112     OS << ShouldAlwaysEmit;
113     OS << ", ";
114     OS << KeyPath;
115     OS << ", ";
116     emitScopedNormalizedValue(OS, DefaultValue);
117     OS << ", ";
118     OS << ImpliedCheck;
119     OS << ", ";
120     emitScopedNormalizedValue(OS, ImpliedValue);
121     OS << ", ";
122     OS << Normalizer;
123     OS << ", ";
124     OS << Denormalizer;
125     OS << ", ";
126     OS << ValueMerger;
127     OS << ", ";
128     OS << ValueExtractor;
129     OS << ", ";
130     OS << TableIndex;
131   }
132 
133   std::optional<StringRef> emitValueTable(raw_ostream &OS) const {
134     if (TableIndex == -1)
135       return {};
136     OS << "static const SimpleEnumValue " << ValueTableName << "[] = {\n";
137     for (unsigned I = 0, E = Values.size(); I != E; ++I) {
138       OS << "{";
139       write_cstring(OS, Values[I]);
140       OS << ",";
141       OS << "static_cast<unsigned>(";
142       emitScopedNormalizedValue(OS, NormalizedValues[I]);
143       OS << ")},";
144     }
145     OS << "};\n";
146     return StringRef(ValueTableName);
147   }
148 
149 private:
150   void emitScopedNormalizedValue(raw_ostream &OS,
151                                  StringRef NormalizedValue) const {
152     if (!NormalizedValuesScope.empty())
153       OS << NormalizedValuesScope << "::";
154     OS << NormalizedValue;
155   }
156 };
157 
158 size_t MarshallingInfo::NextTableIndex = 0;
159 
160 static MarshallingInfo createMarshallingInfo(const Record &R) {
161   assert(!isa<UnsetInit>(R.getValueInit("KeyPath")) &&
162          !isa<UnsetInit>(R.getValueInit("DefaultValue")) &&
163          !isa<UnsetInit>(R.getValueInit("ValueMerger")) &&
164          "MarshallingInfo must have a provide a keypath, default value and a "
165          "value merger");
166 
167   MarshallingInfo Ret(R);
168 
169   Ret.ShouldAlwaysEmit = R.getValueAsBit("ShouldAlwaysEmit");
170   Ret.MacroPrefix = R.getValueAsString("MacroPrefix");
171   Ret.KeyPath = R.getValueAsString("KeyPath");
172   Ret.DefaultValue = R.getValueAsString("DefaultValue");
173   Ret.NormalizedValuesScope = R.getValueAsString("NormalizedValuesScope");
174   Ret.ImpliedCheck = R.getValueAsString("ImpliedCheck");
175   Ret.ImpliedValue =
176       R.getValueAsOptionalString("ImpliedValue").value_or(Ret.DefaultValue);
177 
178   Ret.ShouldParse = R.getValueAsString("ShouldParse");
179   Ret.Normalizer = R.getValueAsString("Normalizer");
180   Ret.Denormalizer = R.getValueAsString("Denormalizer");
181   Ret.ValueMerger = R.getValueAsString("ValueMerger");
182   Ret.ValueExtractor = R.getValueAsString("ValueExtractor");
183 
184   if (!isa<UnsetInit>(R.getValueInit("NormalizedValues"))) {
185     assert(!isa<UnsetInit>(R.getValueInit("Values")) &&
186            "Cannot provide normalized values for value-less options");
187     Ret.TableIndex = MarshallingInfo::NextTableIndex++;
188     Ret.NormalizedValues = R.getValueAsListOfStrings("NormalizedValues");
189     Ret.Values.reserve(Ret.NormalizedValues.size());
190     Ret.ValueTableName = getOptionName(R) + "ValueTable";
191 
192     StringRef ValuesStr = R.getValueAsString("Values");
193     for (;;) {
194       size_t Idx = ValuesStr.find(',');
195       if (Idx == StringRef::npos)
196         break;
197       if (Idx > 0)
198         Ret.Values.push_back(ValuesStr.slice(0, Idx));
199       ValuesStr = ValuesStr.slice(Idx + 1, StringRef::npos);
200     }
201     if (!ValuesStr.empty())
202       Ret.Values.push_back(ValuesStr);
203 
204     assert(Ret.Values.size() == Ret.NormalizedValues.size() &&
205            "The number of normalized values doesn't match the number of "
206            "values");
207   }
208 
209   return Ret;
210 }
211 
212 /// OptParserEmitter - This tablegen backend takes an input .td file
213 /// describing a list of options and emits a data structure for parsing and
214 /// working with those options when given an input command line.
215 namespace llvm {
216 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
217   // Get the option groups and options.
218   const std::vector<Record*> &Groups =
219     Records.getAllDerivedDefinitions("OptionGroup");
220   std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
221 
222   emitSourceFileHeader("Option Parsing Definitions", OS);
223 
224   array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
225   // Generate prefix groups.
226   typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
227   typedef std::map<PrefixKeyT, std::string> PrefixesT;
228   PrefixesT Prefixes;
229   Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
230   unsigned CurPrefix = 0;
231   for (const Record &R : llvm::make_pointee_range(Opts)) {
232     std::vector<StringRef> RPrefixes = R.getValueAsListOfStrings("Prefixes");
233     PrefixKeyT PrefixKey(RPrefixes.begin(), RPrefixes.end());
234     unsigned NewPrefix = CurPrefix + 1;
235     std::string Prefix = (Twine("prefix_") + Twine(NewPrefix)).str();
236     if (Prefixes.insert(std::make_pair(PrefixKey, Prefix)).second)
237       CurPrefix = NewPrefix;
238   }
239 
240   DenseSet<StringRef> PrefixesUnionSet;
241   for (const auto &Prefix : Prefixes)
242     PrefixesUnionSet.insert(Prefix.first.begin(), Prefix.first.end());
243   SmallVector<StringRef> PrefixesUnion(PrefixesUnionSet.begin(),
244                                        PrefixesUnionSet.end());
245   array_pod_sort(PrefixesUnion.begin(), PrefixesUnion.end());
246 
247   // Dump prefixes.
248   OS << "/////////\n";
249   OS << "// Prefixes\n\n";
250   OS << "#ifdef PREFIX\n";
251   OS << "#define COMMA ,\n";
252   for (const auto &Prefix : Prefixes) {
253     OS << "PREFIX(";
254 
255     // Prefix name.
256     OS << Prefix.second;
257 
258     // Prefix values.
259     OS << ", {";
260     for (const auto &PrefixKey : Prefix.first)
261       OS << "llvm::StringLiteral(\"" << PrefixKey << "\") COMMA ";
262     // Append an empty element to avoid ending up with an empty array.
263     OS << "llvm::StringLiteral(\"\")})\n";
264   }
265   OS << "#undef COMMA\n";
266   OS << "#endif // PREFIX\n\n";
267 
268   // Dump prefix unions.
269   OS << "/////////\n";
270   OS << "// Prefix Union\n\n";
271   OS << "#ifdef PREFIX_UNION\n";
272   OS << "#define COMMA ,\n";
273   OS << "PREFIX_UNION({\n";
274   for (const auto &Prefix : PrefixesUnion) {
275     OS << "llvm::StringLiteral(\"" << Prefix << "\") COMMA ";
276   }
277   OS << "llvm::StringLiteral(\"\")})\n";
278   OS << "#undef COMMA\n";
279   OS << "#endif // PREFIX_UNION\n\n";
280 
281   // Dump groups.
282   OS << "/////////\n";
283   OS << "// ValuesCode\n\n";
284   OS << "#ifdef OPTTABLE_VALUES_CODE\n";
285   for (const Record &R : llvm::make_pointee_range(Opts)) {
286     // The option values, if any;
287     if (!isa<UnsetInit>(R.getValueInit("ValuesCode"))) {
288       assert(isa<UnsetInit>(R.getValueInit("Values")) &&
289              "Cannot choose between Values and ValuesCode");
290       OS << "#define VALUES_CODE " << getOptionName(R) << "_Values\n";
291       OS << R.getValueAsString("ValuesCode") << "\n";
292       OS << "#undef VALUES_CODE\n";
293     }
294   }
295   OS << "#endif\n";
296 
297   OS << "/////////\n";
298   OS << "// Groups\n\n";
299   OS << "#ifdef OPTION\n";
300   for (const Record &R : llvm::make_pointee_range(Groups)) {
301     // Start a single option entry.
302     OS << "OPTION(";
303 
304     // The option prefix;
305     OS << "llvm::ArrayRef<llvm::StringLiteral>()";
306 
307     // The option string.
308     OS << ", \"" << R.getValueAsString("Name") << '"';
309 
310     // The option identifier name.
311     OS << ", " << getOptionName(R);
312 
313     // The option kind.
314     OS << ", Group";
315 
316     // The containing option group (if any).
317     OS << ", ";
318     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
319       OS << getOptionName(*DI->getDef());
320     else
321       OS << "INVALID";
322 
323     // The other option arguments (unused for groups).
324     OS << ", INVALID, nullptr, 0, 0";
325 
326     // The option help text.
327     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
328       OS << ",\n";
329       OS << "       ";
330       write_cstring(OS, R.getValueAsString("HelpText"));
331     } else
332       OS << ", nullptr";
333 
334     // The option meta-variable name (unused).
335     OS << ", nullptr";
336 
337     // The option Values (unused for groups).
338     OS << ", nullptr)\n";
339   }
340   OS << "\n";
341 
342   OS << "//////////\n";
343   OS << "// Options\n\n";
344 
345   auto WriteOptRecordFields = [&](raw_ostream &OS, const Record &R) {
346     // The option prefix;
347     std::vector<StringRef> RPrefixes = R.getValueAsListOfStrings("Prefixes");
348     OS << Prefixes[PrefixKeyT(RPrefixes.begin(), RPrefixes.end())] << ", ";
349 
350     // The option string.
351     emitNameUsingSpelling(OS, R);
352 
353     // The option identifier name.
354     OS << ", " << getOptionName(R);
355 
356     // The option kind.
357     OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
358 
359     // The containing option group (if any).
360     OS << ", ";
361     const ListInit *GroupFlags = nullptr;
362     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
363       GroupFlags = DI->getDef()->getValueAsListInit("Flags");
364       OS << getOptionName(*DI->getDef());
365     } else
366       OS << "INVALID";
367 
368     // The option alias (if any).
369     OS << ", ";
370     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
371       OS << getOptionName(*DI->getDef());
372     else
373       OS << "INVALID";
374 
375     // The option alias arguments (if any).
376     // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
377     // would become "foo\0bar\0". Note that the compiler adds an implicit
378     // terminating \0 at the end.
379     OS << ", ";
380     std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
381     if (AliasArgs.size() == 0) {
382       OS << "nullptr";
383     } else {
384       OS << "\"";
385       for (StringRef AliasArg : AliasArgs)
386         OS << AliasArg << "\\0";
387       OS << "\"";
388     }
389 
390     // The option flags.
391     OS << ", ";
392     int NumFlags = 0;
393     const ListInit *LI = R.getValueAsListInit("Flags");
394     for (Init *I : *LI)
395       OS << (NumFlags++ ? " | " : "") << cast<DefInit>(I)->getDef()->getName();
396     if (GroupFlags) {
397       for (Init *I : *GroupFlags)
398         OS << (NumFlags++ ? " | " : "")
399            << cast<DefInit>(I)->getDef()->getName();
400     }
401     if (NumFlags == 0)
402       OS << '0';
403 
404     // The option parameter field.
405     OS << ", " << R.getValueAsInt("NumArgs");
406 
407     // The option help text.
408     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
409       OS << ",\n";
410       OS << "       ";
411       write_cstring(OS, R.getValueAsString("HelpText"));
412     } else
413       OS << ", nullptr";
414 
415     // The option meta-variable name.
416     OS << ", ";
417     if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
418       write_cstring(OS, R.getValueAsString("MetaVarName"));
419     else
420       OS << "nullptr";
421 
422     // The option Values. Used for shell autocompletion.
423     OS << ", ";
424     if (!isa<UnsetInit>(R.getValueInit("Values")))
425       write_cstring(OS, R.getValueAsString("Values"));
426     else if (!isa<UnsetInit>(R.getValueInit("ValuesCode"))) {
427       OS << getOptionName(R) << "_Values";
428     }
429     else
430       OS << "nullptr";
431   };
432 
433   auto IsMarshallingOption = [](const Record &R) {
434     return !isa<UnsetInit>(R.getValueInit("KeyPath")) &&
435            !R.getValueAsString("KeyPath").empty();
436   };
437 
438   std::vector<const Record *> OptsWithMarshalling;
439   for (const Record &R : llvm::make_pointee_range(Opts)) {
440     // Start a single option entry.
441     OS << "OPTION(";
442     WriteOptRecordFields(OS, R);
443     OS << ")\n";
444     if (IsMarshallingOption(R))
445       OptsWithMarshalling.push_back(&R);
446   }
447   OS << "#endif // OPTION\n";
448 
449   auto CmpMarshallingOpts = [](const Record *const *A, const Record *const *B) {
450     unsigned AID = (*A)->getID();
451     unsigned BID = (*B)->getID();
452 
453     if (AID < BID)
454       return -1;
455     if (AID > BID)
456       return 1;
457     return 0;
458   };
459   // The RecordKeeper stores records (options) in lexicographical order, and we
460   // have reordered the options again when generating prefix groups. We need to
461   // restore the original definition order of options with marshalling to honor
462   // the topology of the dependency graph implied by `DefaultAnyOf`.
463   array_pod_sort(OptsWithMarshalling.begin(), OptsWithMarshalling.end(),
464                  CmpMarshallingOpts);
465 
466   std::vector<MarshallingInfo> MarshallingInfos;
467   MarshallingInfos.reserve(OptsWithMarshalling.size());
468   for (const auto *R : OptsWithMarshalling)
469     MarshallingInfos.push_back(createMarshallingInfo(*R));
470 
471   for (const auto &MI : MarshallingInfos) {
472     OS << "#ifdef " << MI.getMacroName() << "\n";
473     OS << MI.getMacroName() << "(";
474     WriteOptRecordFields(OS, MI.R);
475     OS << ", ";
476     MI.emit(OS);
477     OS << ")\n";
478     OS << "#endif // " << MI.getMacroName() << "\n";
479   }
480 
481   OS << "\n";
482   OS << "#ifdef SIMPLE_ENUM_VALUE_TABLE";
483   OS << "\n";
484   OS << MarshallingInfo::ValueTablePreamble;
485   std::vector<StringRef> ValueTableNames;
486   for (const auto &MI : MarshallingInfos)
487     if (auto MaybeValueTableName = MI.emitValueTable(OS))
488       ValueTableNames.push_back(*MaybeValueTableName);
489 
490   OS << MarshallingInfo::ValueTablesDecl << "{";
491   for (auto ValueTableName : ValueTableNames)
492     OS << "{" << ValueTableName << ", std::size(" << ValueTableName << ")},\n";
493   OS << "};\n";
494   OS << "static const unsigned SimpleEnumValueTablesSize = "
495         "std::size(SimpleEnumValueTables);\n";
496 
497   OS << "#endif // SIMPLE_ENUM_VALUE_TABLE\n";
498   OS << "\n";
499 
500   OS << "\n";
501 }
502 } // end namespace llvm
503