LLVM 20.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 raw_ostream &OS) {
65 FnT Fn = CallbackFunction->getValue();
66 if (!Fn)
67 return true;
68 Fn(Records, OS);
69 return false;
70}
71
72static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
73 StringRef Suffix) {
74 size_t Pos = (size_t)OS.tell();
75 assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) &&
76 "header line exceeds max limit");
77 OS << Prefix;
78 for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size();
79 i < e; ++i)
80 OS << Fill;
81 OS << Suffix << '\n';
82}
83
85 const RecordKeeper &Record) {
86 printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
87 StringRef Prefix("|* ");
88 StringRef Suffix(" *|");
89 printLine(OS, Prefix, ' ', Suffix);
90 size_t PSLen = Prefix.size() + Suffix.size();
91 assert(PSLen < MAX_LINE_LEN);
92 size_t Pos = 0U;
93 do {
94 size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen);
95 printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix);
96 Pos += Length;
97 } while (Pos < Desc.size());
98 printLine(OS, Prefix, ' ', Suffix);
99 printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ',
100 Suffix);
101
102 // Print the filename of source file.
103 if (!Record.getInputFilename().empty())
104 printLine(
105 OS, Prefix + "From: " + sys::path::filename(Record.getInputFilename()),
106 ' ', Suffix);
107 printLine(OS, Prefix, ' ', Suffix);
108 printLine(OS, "\\*===", '-', "===*/");
109 OS << '\n';
110}
std::string Name
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
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...
Definition: ManagedStatic.h:83
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:147
bool ApplyCallback(const RecordKeeper &Records, raw_ostream &OS)
Apply callback for any command line option registered above.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:480
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)
Description of the encoding of one expression Op.
Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault=false)