LLVM 22.0.0git
TableGenBackend.cpp
Go to the documentation of this file.
1//===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- 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 file provides useful services for TableGen backends...
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Twine.h"
17#include "llvm/Support/Path.h"
19#include <algorithm>
20#include <cassert>
21#include <cstddef>
22
23using namespace llvm;
24using namespace TableGen::Emitter;
25
26const size_t MAX_LINE_LEN = 80U;
27
28// CommandLine options of class type are not directly supported with some
29// specific exceptions like std::string which are safe to copy. In our case,
30// the `FnT` function_ref object is also safe to copy. So provide a
31// specialization of `OptionValue` for `FnT` type that stores it as a copy.
32// This is essentially similar to OptionValue<std::string> specialization for
33// strings.
34template <> struct cl::OptionValue<FnT> final : cl::OptionValueCopy<FnT> {
35 OptionValue() = default;
36
37 OptionValue(const FnT &V) { this->setValue(V); }
38
40 setValue(V);
41 return *this;
42 }
43};
44
45namespace {
46struct OptCreatorT {
47 static void *call() {
48 return new cl::opt<FnT>(cl::desc("Action to perform:"));
49 }
50};
51} // namespace
52
54
55Opt::Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault) {
56 if (ByDefault)
57 CallbackFunction->setInitialValue(CB);
58 CallbackFunction->getParser().addLiteralOption(Name, CB, Desc);
59}
60
61/// Apply callback specified on the command line. Returns true if no callback
62/// was applied.
64 TableGenOutputFiles &OutFiles,
65 StringRef FilenamePrefix) {
66 FnT Fn = CallbackFunction->getValue();
67 if (Fn.SingleFileGenerator) {
68 std::string S;
70 Fn.SingleFileGenerator(Records, OS);
71 OutFiles = {S, {}};
72 return false;
73 }
74 if (Fn.MultiFileGenerator) {
75 OutFiles = Fn.MultiFileGenerator(FilenamePrefix, Records);
76 return false;
77 }
78 return true;
79}
80
81static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
82 StringRef Suffix) {
83 size_t Pos = (size_t)OS.tell();
84 assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
85 "header line exceeds max limit");
86 OS << Prefix;
87 for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
88 i < e; ++i)
89 OS << Fill;
90 OS << Suffix << '\n';
91}
92
94 const RecordKeeper &Record) {
95 printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
96 StringRef Prefix("|* ");
97 StringRef Suffix(" *|");
98 printLine(OS, Prefix, ' ', Suffix);
99 size_t PSLen = Prefix.size() + Suffix.size();
100 assert(PSLen < MAX_LINE_LEN);
101 size_t Pos = 0U;
102 do {
103 size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
104 printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
105 Pos += Length;
106 } while (Pos < Desc.size());
107 printLine(OS, Prefix, ' ', Suffix);
108 printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
109 Suffix);
110
111 // Print the filename of source file.
112 if (!Record.getInputFilename().empty())
113 printLine(
114 OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),
115 ' ', Suffix);
116 printLine(OS, Prefix, ' ', Suffix);
117 printLine(OS, "\\*===", '-', "===*/");
118 OS << '\n';
119}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const size_t MAX_LINE_LEN
static ManagedStatic< cl::opt< FnT >, OptCreatorT > CallbackFunction
static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill, StringRef Suffix)
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
uint64_t tell() const
tell - Return the current offset with the file.
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.
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:577
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:532
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.
OptionValue< FnT > & operator=(const FnT &V)
Represents the emitting function.
MultiFileGeneratorType * MultiFileGenerator
SingleFileGeneratorType * SingleFileGenerator
Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault=false)