aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/TableGen
diff options
context:
space:
mode:
authorNAKAMURA Takumi <geek4civic@gmail.com>2023-02-17 08:24:07 +0900
committerNAKAMURA Takumi <geek4civic@gmail.com>2023-03-21 16:21:27 +0900
commit4178ef43b2f12171639e3ef2bf02e5b7efd34688 (patch)
treeaa08ded6209a0ff2e4aa8a2f085b49e3e56f6d05 /llvm/lib/TableGen
parentf675ec6165ab6add5e57cd43a2e9fa1a9bc21d81 (diff)
downloadllvm-4178ef43b2f12171639e3ef2bf02e5b7efd34688.zip
llvm-4178ef43b2f12171639e3ef2bf02e5b7efd34688.tar.gz
llvm-4178ef43b2f12171639e3ef2bf02e5b7efd34688.tar.bz2
TableGen: Introduce `llvm::TableGen::Emitter` to register backends
`Opt(flag, func, desc)` registers an option into `Action`. `OptClass<EmitterC>` is also available if `EmitterC(RK).run(OS)` is capable. `Action` is defined as `ManagedStatic<cl::opt>` to guarantee to be created when each registration of emitter is invoked. `llvm::TableGenMain(argv0, MainFn)` invokes `Action` instead of `MainFn` Differential Revision: https://reviews.llvm.org/D144351
Diffstat (limited to 'llvm/lib/TableGen')
-rw-r--r--llvm/lib/TableGen/Main.cpp6
-rw-r--r--llvm/lib/TableGen/TableGenBackend.cpp9
-rw-r--r--llvm/lib/TableGen/TableGenBackendSkeleton.cpp16
3 files changed, 25 insertions, 6 deletions
diff --git a/llvm/lib/TableGen/Main.cpp b/llvm/lib/TableGen/Main.cpp
index ee72b4b..9aee1f8 100644
--- a/llvm/lib/TableGen/Main.cpp
+++ b/llvm/lib/TableGen/Main.cpp
@@ -29,6 +29,7 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
+#include "llvm/TableGen/TableGenBackend.h"
#include <memory>
#include <string>
#include <system_error>
@@ -131,7 +132,10 @@ int llvm::TableGenMain(const char *argv0,
std::string OutString;
raw_string_ostream Out(OutString);
unsigned status = 0;
- if (MainFn)
+ TableGen::Emitter::FnT ActionFn = TableGen::Emitter::Action->getValue();
+ if (ActionFn)
+ ActionFn(Records, Out);
+ else if (MainFn)
status = MainFn(Out, Records);
else
return 1;
diff --git a/llvm/lib/TableGen/TableGenBackend.cpp b/llvm/lib/TableGen/TableGenBackend.cpp
index 252f126..135ec64 100644
--- a/llvm/lib/TableGen/TableGenBackend.cpp
+++ b/llvm/lib/TableGen/TableGenBackend.cpp
@@ -13,12 +13,21 @@
#include "llvm/TableGen/TableGenBackend.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/raw_ostream.h"
+#include <algorithm>
#include <cassert>
+#include <cstddef>
using namespace llvm;
const size_t MAX_LINE_LEN = 80U;
+namespace llvm::TableGen::Emitter {
+ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;
+void *OptCreatorT::call() {
+ return new cl::opt<FnT>(cl::desc("Action to perform:"));
+}
+} // namespace llvm::TableGen::Emitter
+
static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
StringRef Suffix) {
size_t Pos = (size_t)OS.tell();
diff --git a/llvm/lib/TableGen/TableGenBackendSkeleton.cpp b/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
index 0ba00c8..2fde4a6 100644
--- a/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
+++ b/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
@@ -46,14 +46,20 @@ void SkeletonEmitter::run(raw_ostream &OS) {
(void)Records; // To suppress unused variable warning; remove on use.
}
-namespace llvm {
+// Choose either option A or B.
-// The only thing that should be in the llvm namespace is the
-// emitter entry point function.
+//===----------------------------------------------------------------------===//
+// Option A: Register the backed as class <SkeletonEmitter>
+static TableGen::Emitter::OptClass<SkeletonEmitter>
+ X("gen-skeleton-class", "Generate example skeleton class");
-void EmitSkeleton(RecordKeeper &RK, raw_ostream &OS) {
+//===----------------------------------------------------------------------===//
+// Option B: Register "EmitSkeleton" directly
+// The emitter entry may be private scope.
+static void EmitSkeleton(RecordKeeper &RK, raw_ostream &OS) {
// Instantiate the emitter class and invoke run().
SkeletonEmitter(RK).run(OS);
}
-} // namespace llvm
+static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton,
+ "Generate example skeleton entry");