================
@@ -0,0 +1,191 @@
+//===- EmitTargetFeature.cpp - Generate CPU Targer feature ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This tablegen backend exports cpu target features
+//  and cpu sub-type for all platform.  
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/TableGenBackend.h"
+#include "llvm/TargetParser/SubtargetFeature.h"
+
+using namespace llvm;
+
+using FeatureMapTy = DenseMap<const Record *, unsigned>;
+using ConstRecVec = std::vector<const Record *>;
+
+struct LessRecordFieldNameAndID {
+  bool operator()(const Record *Rec1, const Record *Rec2) const {
+    return std::tuple(Rec1->getValueAsString("Name"), Rec1->getID()) <
+           std::tuple(Rec2->getValueAsString("Name"), Rec2->getID());
+  }
+};
+
+static StringRef getTargetName(const RecordKeeper &Records) {
+  ArrayRef<const Record *> Targets = 
Records.getAllDerivedDefinitions("Target");
+  if (Targets.size() == 0)
+    PrintFatalError("No 'Target' subclasses defined!");
+  if (Targets.size() != 1)
+    PrintFatalError("Multiple subclasses of Target defined!");
+  return Targets[0]->getName();
+}
+
+static FeatureMapTy enumeration(const RecordKeeper &Records, raw_ostream &OS) {
+  ArrayRef<const Record *> DefList =
+      Records.getAllDerivedDefinitions("SubtargetFeature");
+
+  unsigned N = DefList.size();
+  if (N == 0)
+    return FeatureMapTy();
+
+  if (N + 1 > MAX_SUBTARGET_FEATURES)
+    PrintFatalError(
+        "Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
+
+  StringRef Target = getTargetName(Records);
+
+  OS << "namespace " << Target << " {\n";
+
+  OS << "enum {\n";
+
+  FeatureMapTy FeatureMap;
+  for (unsigned I = 0; I < N; ++I) {
+    const Record *Def = DefList[I];
+    // Print the Feature Name.
+    OS << "  " << Def->getName() << " = " << I << ",\n";
+
+    FeatureMap[Def] = I;
+  }
+
+  OS << "  " << "NumSubtargetFeatures = " << N << "\n";
+
+  // Close enumeration and namespace
+  OS << "};\n";
+  OS << "} // end namespace " << Target << "\n";
+  return FeatureMap;
+}
+
+static void printFeatureMask(raw_ostream &OS,
----------------
rnk wrote:

This tablegen code is duplicated from the subtarget feature emitter. Please 
factor things so that the code is shared, like the subtarget emitter code could 
call this code through a header.

https://github.com/llvm/llvm-project/pull/144594
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to