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