JSONBackend.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===- JSONBackend.cpp - Generate a JSON dump of all records. -*- C++ -*-=====//
  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. // This TableGen back end generates a machine-readable representation
  10. // of all the classes and records defined by the input, in JSON format.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/Support/Casting.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/JSON.h"
  18. #include "llvm/TableGen/Record.h"
  19. #define DEBUG_TYPE "json-emitter"
  20. using namespace llvm;
  21. namespace {
  22. class JSONEmitter {
  23. private:
  24. RecordKeeper &Records;
  25. json::Value translateInit(const Init &I);
  26. public:
  27. JSONEmitter(RecordKeeper &R);
  28. void run(raw_ostream &OS);
  29. };
  30. } // end anonymous namespace
  31. JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
  32. json::Value JSONEmitter::translateInit(const Init &I) {
  33. // Init subclasses that we return as JSON primitive values of one
  34. // kind or another.
  35. if (isa<UnsetInit>(&I)) {
  36. return nullptr;
  37. } else if (auto *Bit = dyn_cast<BitInit>(&I)) {
  38. return Bit->getValue() ? 1 : 0;
  39. } else if (auto *Bits = dyn_cast<BitsInit>(&I)) {
  40. json::Array array;
  41. for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)
  42. array.push_back(translateInit(*Bits->getBit(i)));
  43. return std::move(array);
  44. } else if (auto *Int = dyn_cast<IntInit>(&I)) {
  45. return Int->getValue();
  46. } else if (auto *Str = dyn_cast<StringInit>(&I)) {
  47. return Str->getValue();
  48. } else if (auto *List = dyn_cast<ListInit>(&I)) {
  49. json::Array array;
  50. for (auto val : *List)
  51. array.push_back(translateInit(*val));
  52. return std::move(array);
  53. }
  54. // Init subclasses that we return as JSON objects containing a
  55. // 'kind' discriminator. For these, we also provide the same
  56. // translation back into TableGen input syntax that -print-records
  57. // would give.
  58. json::Object obj;
  59. obj["printable"] = I.getAsString();
  60. if (auto *Def = dyn_cast<DefInit>(&I)) {
  61. obj["kind"] = "def";
  62. obj["def"] = Def->getDef()->getName();
  63. return std::move(obj);
  64. } else if (auto *Var = dyn_cast<VarInit>(&I)) {
  65. obj["kind"] = "var";
  66. obj["var"] = Var->getName();
  67. return std::move(obj);
  68. } else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {
  69. if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
  70. obj["kind"] = "varbit";
  71. obj["var"] = Var->getName();
  72. obj["index"] = VarBit->getBitNum();
  73. return std::move(obj);
  74. }
  75. } else if (auto *Dag = dyn_cast<DagInit>(&I)) {
  76. obj["kind"] = "dag";
  77. obj["operator"] = translateInit(*Dag->getOperator());
  78. if (auto name = Dag->getName())
  79. obj["name"] = name->getAsUnquotedString();
  80. json::Array args;
  81. for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {
  82. json::Array arg;
  83. arg.push_back(translateInit(*Dag->getArg(i)));
  84. if (auto argname = Dag->getArgName(i))
  85. arg.push_back(argname->getAsUnquotedString());
  86. else
  87. arg.push_back(nullptr);
  88. args.push_back(std::move(arg));
  89. }
  90. obj["args"] = std::move(args);
  91. return std::move(obj);
  92. }
  93. // Final fallback: anything that gets past here is simply given a
  94. // kind field of 'complex', and the only other field is the standard
  95. // 'printable' representation.
  96. assert(!I.isConcrete());
  97. obj["kind"] = "complex";
  98. return std::move(obj);
  99. }
  100. void JSONEmitter::run(raw_ostream &OS) {
  101. json::Object root;
  102. root["!tablegen_json_version"] = 1;
  103. // Prepare the arrays that will list the instances of every class.
  104. // We mostly fill those in by iterating over the superclasses of
  105. // each def, but we also want to ensure we store an empty list for a
  106. // class with no instances at all, so we do a preliminary iteration
  107. // over the classes, invoking std::map::operator[] to default-
  108. // construct the array for each one.
  109. std::map<std::string, json::Array> instance_lists;
  110. for (const auto &C : Records.getClasses()) {
  111. auto &Name = C.second->getNameInitAsString();
  112. (void)instance_lists[Name];
  113. }
  114. // Main iteration over the defs.
  115. for (const auto &D : Records.getDefs()) {
  116. auto &Name = D.second->getNameInitAsString();
  117. auto &Def = *D.second;
  118. json::Object obj;
  119. json::Array fields;
  120. for (const RecordVal &RV : Def.getValues()) {
  121. if (!Def.isTemplateArg(RV.getNameInit())) {
  122. auto Name = RV.getNameInitAsString();
  123. if (RV.isNonconcreteOK())
  124. fields.push_back(Name);
  125. obj[Name] = translateInit(*RV.getValue());
  126. }
  127. }
  128. obj["!fields"] = std::move(fields);
  129. json::Array superclasses;
  130. for (const auto &SuperPair : Def.getSuperClasses())
  131. superclasses.push_back(SuperPair.first->getNameInitAsString());
  132. obj["!superclasses"] = std::move(superclasses);
  133. obj["!name"] = Name;
  134. obj["!anonymous"] = Def.isAnonymous();
  135. root[Name] = std::move(obj);
  136. // Add this def to the instance list for each of its superclasses.
  137. for (const auto &SuperPair : Def.getSuperClasses()) {
  138. auto SuperName = SuperPair.first->getNameInitAsString();
  139. instance_lists[SuperName].push_back(Name);
  140. }
  141. }
  142. // Make a JSON object from the std::map of instance lists.
  143. json::Object instanceof;
  144. for (auto kv: instance_lists)
  145. instanceof[kv.first] = std::move(kv.second);
  146. root["!instanceof"] = std::move(instanceof);
  147. // Done. Write the output.
  148. OS << json::Value(std::move(root)) << "\n";
  149. }
  150. namespace llvm {
  151. void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
  152. } // end namespace llvm