LLVM 22.0.0git
TableGenBackend.h
Go to the documentation of this file.
1//===- llvm/TableGen/TableGenBackend.h - Backend utilities ------*- 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// Useful utilities for TableGen backends.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TABLEGEN_TABLEGENBACKEND_H
14#define LLVM_TABLEGEN_TABLEGENBACKEND_H
15
17#include "llvm/ADT/StringRef.h"
18#include "llvm/TableGen/Main.h"
20
21namespace llvm {
22
23class RecordKeeper;
24class raw_ostream;
25
27
28/// Represents the emitting function. Can produce a single or multple output
29/// files.
30struct FnT {
31 using SingleFileGeneratorType = void(const RecordKeeper &Records,
32 raw_ostream &OS);
34 StringRef FilenamePrefix, const RecordKeeper &Records);
35
38
39 FnT() = default;
42
43 bool operator==(const FnT &Other) const {
44 return SingleFileGenerator == Other.SingleFileGenerator &&
45 MultiFileGenerator == Other.MultiFileGenerator;
46 }
47};
48
49/// Creating an `Opt` object registers the command line option \p Name with
50/// TableGen backend and associates the callback \p CB with that option. If
51/// \p ByDefault is true, then that callback is applied by default if no
52/// command line option was specified.
53struct Opt {
54 Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault = false);
55};
56
57/// Convienence wrapper around `Opt` that registers `EmitterClass::run` as the
58/// callback.
59template <class EmitterC> class OptClass : Opt {
60 static TableGenOutputFiles run(StringRef /*FilenamePrefix*/,
61 const RecordKeeper &RK) {
62 std::string S;
64 EmitterC(RK).run(OS);
65 return {S, {}};
66 }
67
68public:
69 OptClass(StringRef Name, StringRef Desc) : Opt(Name, run, Desc) {}
70};
71
72/// A version of the wrapper for backends emitting multiple files.
73template <class EmitterC> class MultiFileOptClass : Opt {
74 static TableGenOutputFiles run(StringRef FilenamePrefix,
75 const RecordKeeper &RK) {
76 return EmitterC(RK).run(FilenamePrefix);
77 }
78
79public:
81};
82
83/// Apply callback for any command line option registered above. Returns false
84/// is no callback was applied.
85bool ApplyCallback(const RecordKeeper &Records, TableGenOutputFiles &OutFiles,
86 StringRef FilenamePrefix);
87
88} // namespace TableGen::Emitter
89
90/// emitSourceFileHeader - Output an LLVM style file header to the specified
91/// raw_ostream.
94
95} // namespace llvm
96
97#endif // LLVM_TABLEGEN_TABLEGENBACKEND_H
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
MultiFileOptClass(StringRef Name, StringRef Desc)
OptClass(StringRef Name, StringRef Desc)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
bool ApplyCallback(const RecordKeeper &Records, TableGenOutputFiles &OutFiles, StringRef FilenamePrefix)
Apply callback for any command line option registered above.
This is an optimization pass for GlobalISel generic memory operations.
Op::Description Desc
void emitSourceFileHeader(StringRef Desc, raw_ostream &OS, const RecordKeeper &Record=RecordKeeper())
emitSourceFileHeader - Output an LLVM style file header to the specified raw_ostream.
@ Other
Any other memory.
Definition ModRef.h:68
Represents the emitting function.
FnT(MultiFileGeneratorType *Gen)
bool operator==(const FnT &Other) const
void(const RecordKeeper &Records, raw_ostream &OS) SingleFileGeneratorType
FnT(SingleFileGeneratorType *Gen)
TableGenOutputFiles( StringRef FilenamePrefix, const RecordKeeper &Records) MultiFileGeneratorType
MultiFileGeneratorType * MultiFileGenerator
SingleFileGeneratorType * SingleFileGenerator
Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault=false)