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