1//===- Attributes.cpp - Generate attributes -------------------------------===//
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#include "llvm/TableGen/Record.h"
10#include "llvm/TableGen/TableGenBackend.h"
11#include <vector>
12using namespace llvm;
13
14#define DEBUG_TYPE "attr-enum"
15
16namespace {
17
18class Attributes {
19public:
20 Attributes(RecordKeeper &R) : Records(R) {}
21 void run(raw_ostream &OS);
22
23private:
24 void emitTargetIndependentNames(raw_ostream &OS);
25 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
26 void emitAttributeProperties(raw_ostream &OF);
27
28 RecordKeeper &Records;
29};
30
31} // End anonymous namespace.
32
33void Attributes::emitTargetIndependentNames(raw_ostream &OS) {
34 OS << "#ifdef GET_ATTR_NAMES\n";
35 OS << "#undef GET_ATTR_NAMES\n";
36
37 OS << "#ifndef ATTRIBUTE_ALL\n";
38 OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
39 OS << "#endif\n\n";
40
41 auto Emit = [&](ArrayRef<StringRef> KindNames, StringRef MacroName) {
42 OS << "#ifndef " << MacroName << "\n";
43 OS << "#define " << MacroName
44 << "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, SECOND)\n";
45 OS << "#endif\n\n";
46 for (StringRef KindName : KindNames) {
47 for (auto *A : Records.getAllDerivedDefinitions(ClassName: KindName)) {
48 OS << MacroName << "(" << A->getName() << ","
49 << A->getValueAsString(FieldName: "AttrString") << ")\n";
50 }
51 }
52 OS << "#undef " << MacroName << "\n\n";
53 };
54
55 // Emit attribute enums in the same order llvm::Attribute::operator< expects.
56 Emit({"EnumAttr", "TypeAttr", "IntAttr", "ConstantRangeAttr"},
57 "ATTRIBUTE_ENUM");
58 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
59 Emit({"ComplexStrAttr"}, "ATTRIBUTE_COMPLEXSTR");
60
61 OS << "#undef ATTRIBUTE_ALL\n";
62 OS << "#endif\n\n";
63
64 OS << "#ifdef GET_ATTR_ENUM\n";
65 OS << "#undef GET_ATTR_ENUM\n";
66 unsigned Value = 1; // Leave zero for AttrKind::None.
67 for (StringRef KindName :
68 {"EnumAttr", "TypeAttr", "IntAttr", "ConstantRangeAttr"}) {
69 OS << "First" << KindName << " = " << Value << ",\n";
70 for (auto *A : Records.getAllDerivedDefinitions(ClassName: KindName)) {
71 OS << A->getName() << " = " << Value << ",\n";
72 Value++;
73 }
74 OS << "Last" << KindName << " = " << (Value - 1) << ",\n";
75 }
76 OS << "#endif\n\n";
77}
78
79void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
80 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
81 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
82
83 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
84 << " const Function &Callee) {\n";
85 OS << " bool Ret = true;\n\n";
86
87 std::vector<Record *> CompatRules =
88 Records.getAllDerivedDefinitions(ClassName: "CompatRule");
89
90 for (auto *Rule : CompatRules) {
91 StringRef FuncName = Rule->getValueAsString(FieldName: "CompatFunc");
92 OS << " Ret &= " << FuncName << "(Caller, Callee";
93 StringRef AttrName = Rule->getValueAsString(FieldName: "AttrName");
94 if (!AttrName.empty())
95 OS << ", \"" << AttrName << "\"";
96 OS << ");\n";
97 }
98
99 OS << "\n";
100 OS << " return Ret;\n";
101 OS << "}\n\n";
102
103 std::vector<Record *> MergeRules =
104 Records.getAllDerivedDefinitions(ClassName: "MergeRule");
105 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
106 << " const Function &Callee) {\n";
107
108 for (auto *Rule : MergeRules) {
109 StringRef FuncName = Rule->getValueAsString(FieldName: "MergeFunc");
110 OS << " " << FuncName << "(Caller, Callee);\n";
111 }
112
113 OS << "}\n\n";
114
115 OS << "#endif\n";
116}
117
118void Attributes::emitAttributeProperties(raw_ostream &OS) {
119 OS << "#ifdef GET_ATTR_PROP_TABLE\n";
120 OS << "#undef GET_ATTR_PROP_TABLE\n";
121 OS << "static const uint8_t AttrPropTable[] = {\n";
122 for (StringRef KindName :
123 {"EnumAttr", "TypeAttr", "IntAttr", "ConstantRangeAttr"}) {
124 for (auto *A : Records.getAllDerivedDefinitions(ClassName: KindName)) {
125 OS << "0";
126 for (Init *P : *A->getValueAsListInit(FieldName: "Properties"))
127 OS << " | AttributeProperty::" << cast<DefInit>(Val: P)->getDef()->getName();
128 OS << ",\n";
129 }
130 }
131 OS << "};\n";
132 OS << "#endif\n";
133}
134
135void Attributes::run(raw_ostream &OS) {
136 emitTargetIndependentNames(OS);
137 emitFnAttrCompatCheck(OS, IsStringAttr: false);
138 emitAttributeProperties(OS);
139}
140
141static TableGen::Emitter::OptClass<Attributes> X("gen-attrs",
142 "Generate attributes");
143

source code of llvm/utils/TableGen/Attributes.cpp