xref: /freebsd-src/contrib/llvm-project/llvm/lib/TableGen/JSONBackend.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- JSONBackend.cpp - Generate a JSON dump of all records. -*- C++ -*-=====//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This TableGen back end generates a machine-readable representation
100b57cec5SDimitry Andric // of all the classes and records defined by the input, in JSON format.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
141fd87a68SDimitry Andric #include "llvm/Support/Casting.h"
150b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
161fd87a68SDimitry Andric #include "llvm/Support/ErrorHandling.h"
170b57cec5SDimitry Andric #include "llvm/Support/JSON.h"
18*0fca6ea1SDimitry Andric #include "llvm/TableGen/Error.h"
191fd87a68SDimitry Andric #include "llvm/TableGen/Record.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #define DEBUG_TYPE "json-emitter"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric class JSONEmitter {
280b57cec5SDimitry Andric private:
290b57cec5SDimitry Andric   RecordKeeper &Records;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   json::Value translateInit(const Init &I);
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric public:
340b57cec5SDimitry Andric   JSONEmitter(RecordKeeper &R);
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   void run(raw_ostream &OS);
370b57cec5SDimitry Andric };
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric } // end anonymous namespace
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric json::Value JSONEmitter::translateInit(const Init &I) {
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // Init subclasses that we return as JSON primitive values of one
460b57cec5SDimitry Andric   // kind or another.
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   if (isa<UnsetInit>(&I)) {
490b57cec5SDimitry Andric     return nullptr;
500b57cec5SDimitry Andric   } else if (auto *Bit = dyn_cast<BitInit>(&I)) {
510b57cec5SDimitry Andric     return Bit->getValue() ? 1 : 0;
520b57cec5SDimitry Andric   } else if (auto *Bits = dyn_cast<BitsInit>(&I)) {
530b57cec5SDimitry Andric     json::Array array;
540b57cec5SDimitry Andric     for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)
550b57cec5SDimitry Andric       array.push_back(translateInit(*Bits->getBit(i)));
560b57cec5SDimitry Andric     return std::move(array);
570b57cec5SDimitry Andric   } else if (auto *Int = dyn_cast<IntInit>(&I)) {
580b57cec5SDimitry Andric     return Int->getValue();
590b57cec5SDimitry Andric   } else if (auto *Str = dyn_cast<StringInit>(&I)) {
600b57cec5SDimitry Andric     return Str->getValue();
610b57cec5SDimitry Andric   } else if (auto *List = dyn_cast<ListInit>(&I)) {
620b57cec5SDimitry Andric     json::Array array;
63bdd1243dSDimitry Andric     for (auto *val : *List)
640b57cec5SDimitry Andric       array.push_back(translateInit(*val));
650b57cec5SDimitry Andric     return std::move(array);
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   // Init subclasses that we return as JSON objects containing a
690b57cec5SDimitry Andric   // 'kind' discriminator. For these, we also provide the same
700b57cec5SDimitry Andric   // translation back into TableGen input syntax that -print-records
710b57cec5SDimitry Andric   // would give.
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   json::Object obj;
740b57cec5SDimitry Andric   obj["printable"] = I.getAsString();
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   if (auto *Def = dyn_cast<DefInit>(&I)) {
770b57cec5SDimitry Andric     obj["kind"] = "def";
780b57cec5SDimitry Andric     obj["def"] = Def->getDef()->getName();
790b57cec5SDimitry Andric     return std::move(obj);
800b57cec5SDimitry Andric   } else if (auto *Var = dyn_cast<VarInit>(&I)) {
810b57cec5SDimitry Andric     obj["kind"] = "var";
820b57cec5SDimitry Andric     obj["var"] = Var->getName();
830b57cec5SDimitry Andric     return std::move(obj);
840b57cec5SDimitry Andric   } else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {
850b57cec5SDimitry Andric     if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
860b57cec5SDimitry Andric       obj["kind"] = "varbit";
870b57cec5SDimitry Andric       obj["var"] = Var->getName();
880b57cec5SDimitry Andric       obj["index"] = VarBit->getBitNum();
890b57cec5SDimitry Andric       return std::move(obj);
900b57cec5SDimitry Andric     }
910b57cec5SDimitry Andric   } else if (auto *Dag = dyn_cast<DagInit>(&I)) {
920b57cec5SDimitry Andric     obj["kind"] = "dag";
930b57cec5SDimitry Andric     obj["operator"] = translateInit(*Dag->getOperator());
940b57cec5SDimitry Andric     if (auto name = Dag->getName())
950b57cec5SDimitry Andric       obj["name"] = name->getAsUnquotedString();
960b57cec5SDimitry Andric     json::Array args;
970b57cec5SDimitry Andric     for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {
980b57cec5SDimitry Andric       json::Array arg;
990b57cec5SDimitry Andric       arg.push_back(translateInit(*Dag->getArg(i)));
1000b57cec5SDimitry Andric       if (auto argname = Dag->getArgName(i))
1010b57cec5SDimitry Andric         arg.push_back(argname->getAsUnquotedString());
1020b57cec5SDimitry Andric       else
1030b57cec5SDimitry Andric         arg.push_back(nullptr);
1040b57cec5SDimitry Andric       args.push_back(std::move(arg));
1050b57cec5SDimitry Andric     }
1060b57cec5SDimitry Andric     obj["args"] = std::move(args);
1070b57cec5SDimitry Andric     return std::move(obj);
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   // Final fallback: anything that gets past here is simply given a
1110b57cec5SDimitry Andric   // kind field of 'complex', and the only other field is the standard
1120b57cec5SDimitry Andric   // 'printable' representation.
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   assert(!I.isConcrete());
1150b57cec5SDimitry Andric   obj["kind"] = "complex";
1160b57cec5SDimitry Andric   return std::move(obj);
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric void JSONEmitter::run(raw_ostream &OS) {
1200b57cec5SDimitry Andric   json::Object root;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   root["!tablegen_json_version"] = 1;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   // Prepare the arrays that will list the instances of every class.
1250b57cec5SDimitry Andric   // We mostly fill those in by iterating over the superclasses of
1260b57cec5SDimitry Andric   // each def, but we also want to ensure we store an empty list for a
1270b57cec5SDimitry Andric   // class with no instances at all, so we do a preliminary iteration
1280b57cec5SDimitry Andric   // over the classes, invoking std::map::operator[] to default-
1290b57cec5SDimitry Andric   // construct the array for each one.
1300b57cec5SDimitry Andric   std::map<std::string, json::Array> instance_lists;
1310b57cec5SDimitry Andric   for (const auto &C : Records.getClasses()) {
132fcaf7f86SDimitry Andric     const auto Name = C.second->getNameInitAsString();
1330b57cec5SDimitry Andric     (void)instance_lists[Name];
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   // Main iteration over the defs.
1370b57cec5SDimitry Andric   for (const auto &D : Records.getDefs()) {
138fcaf7f86SDimitry Andric     const auto Name = D.second->getNameInitAsString();
1390b57cec5SDimitry Andric     auto &Def = *D.second;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric     json::Object obj;
1420b57cec5SDimitry Andric     json::Array fields;
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric     for (const RecordVal &RV : Def.getValues()) {
1450b57cec5SDimitry Andric       if (!Def.isTemplateArg(RV.getNameInit())) {
1460b57cec5SDimitry Andric         auto Name = RV.getNameInitAsString();
147e8d8bef9SDimitry Andric         if (RV.isNonconcreteOK())
1480b57cec5SDimitry Andric           fields.push_back(Name);
1490b57cec5SDimitry Andric         obj[Name] = translateInit(*RV.getValue());
1500b57cec5SDimitry Andric       }
1510b57cec5SDimitry Andric     }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     obj["!fields"] = std::move(fields);
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     json::Array superclasses;
1560b57cec5SDimitry Andric     for (const auto &SuperPair : Def.getSuperClasses())
1570b57cec5SDimitry Andric       superclasses.push_back(SuperPair.first->getNameInitAsString());
1580b57cec5SDimitry Andric     obj["!superclasses"] = std::move(superclasses);
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric     obj["!name"] = Name;
1610b57cec5SDimitry Andric     obj["!anonymous"] = Def.isAnonymous();
1620b57cec5SDimitry Andric 
163*0fca6ea1SDimitry Andric     json::Array locs;
164*0fca6ea1SDimitry Andric     for (const SMLoc Loc : Def.getLoc())
165*0fca6ea1SDimitry Andric       locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));
166*0fca6ea1SDimitry Andric     obj["!locs"] = std::move(locs);
167*0fca6ea1SDimitry Andric 
1680b57cec5SDimitry Andric     root[Name] = std::move(obj);
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric     // Add this def to the instance list for each of its superclasses.
1710b57cec5SDimitry Andric     for (const auto &SuperPair : Def.getSuperClasses()) {
1720b57cec5SDimitry Andric       auto SuperName = SuperPair.first->getNameInitAsString();
1730b57cec5SDimitry Andric       instance_lists[SuperName].push_back(Name);
1740b57cec5SDimitry Andric     }
1750b57cec5SDimitry Andric   }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   // Make a JSON object from the std::map of instance lists.
1780b57cec5SDimitry Andric   json::Object instanceof;
1790b57cec5SDimitry Andric   for (auto kv: instance_lists)
1800b57cec5SDimitry Andric     instanceof[kv.first] = std::move(kv.second);
1810b57cec5SDimitry Andric   root["!instanceof"] = std::move(instanceof);
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // Done. Write the output.
1840b57cec5SDimitry Andric   OS << json::Value(std::move(root)) << "\n";
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric namespace llvm {
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
1900b57cec5SDimitry Andric } // end namespace llvm
191