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 "ConstantRangeListAttr"},
58 "ATTRIBUTE_ENUM");
59 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
60 Emit({"ComplexStrAttr"}, "ATTRIBUTE_COMPLEXSTR");
61
62 OS << "#undef ATTRIBUTE_ALL\n";
63 OS << "#endif\n\n";
64
65 OS << "#ifdef GET_ATTR_ENUM\n";
66 OS << "#undef GET_ATTR_ENUM\n";
67 unsigned Value = 1; // Leave zero for AttrKind::None.
68 for (StringRef KindName : {"EnumAttr", "TypeAttr", "IntAttr",
69 "ConstantRangeAttr", "ConstantRangeListAttr"}) {
70 OS << "First" << KindName << " = " << Value << ",\n";
71 for (auto *A : Records.getAllDerivedDefinitions(ClassName: KindName)) {
72 OS << A->getName() << " = " << Value << ",\n";
73 Value++;
74 }
75 OS << "Last" << KindName << " = " << (Value - 1) << ",\n";
76 }
77 OS << "#endif\n\n";
78}
79
80void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
81 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
82 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
83
84 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
85 << " const Function &Callee) {\n";
86 OS << " bool Ret = true;\n\n";
87
88 std::vector<Record *> CompatRules =
89 Records.getAllDerivedDefinitions(ClassName: "CompatRule");
90
91 for (auto *Rule : CompatRules) {
92 StringRef FuncName = Rule->getValueAsString(FieldName: "CompatFunc");
93 OS << " Ret &= " << FuncName << "(Caller, Callee";
94 StringRef AttrName = Rule->getValueAsString(FieldName: "AttrName");
95 if (!AttrName.empty())
96 OS << ", \"" << AttrName << "\"";
97 OS << ");\n";
98 }
99
100 OS << "\n";
101 OS << " return Ret;\n";
102 OS << "}\n\n";
103
104 std::vector<Record *> MergeRules =
105 Records.getAllDerivedDefinitions(ClassName: "MergeRule");
106 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
107 << " const Function &Callee) {\n";
108
109 for (auto *Rule : MergeRules) {
110 StringRef FuncName = Rule->getValueAsString(FieldName: "MergeFunc");
111 OS << " " << FuncName << "(Caller, Callee);\n";
112 }
113
114 OS << "}\n\n";
115
116 OS << "#endif\n";
117}
118
119void Attributes::emitAttributeProperties(raw_ostream &OS) {
120 OS << "#ifdef GET_ATTR_PROP_TABLE\n";
121 OS << "#undef GET_ATTR_PROP_TABLE\n";
122 OS << "static const uint8_t AttrPropTable[] = {\n";
123 for (StringRef KindName : {"EnumAttr", "TypeAttr", "IntAttr",
124 "ConstantRangeAttr", "ConstantRangeListAttr"}) {
125 for (auto *A : Records.getAllDerivedDefinitions(ClassName: KindName)) {
126 OS << "0";
127 for (Init *P : *A->getValueAsListInit(FieldName: "Properties"))
128 OS << " | AttributeProperty::" << cast<DefInit>(Val: P)->getDef()->getName();
129 OS << ",\n";
130 }
131 }
132 OS << "};\n";
133 OS << "#endif\n";
134}
135
136void Attributes::run(raw_ostream &OS) {
137 emitTargetIndependentNames(OS);
138 emitFnAttrCompatCheck(OS, IsStringAttr: false);
139 emitAttributeProperties(OS);
140}
141
142static TableGen::Emitter::OptClass<Attributes> X("gen-attrs",
143 "Generate attributes");
144