1 //===- Attributes.cpp - Generate attributes -------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/TableGen/Error.h"
10 #include "llvm/TableGen/Record.h"
11 #include "llvm/TableGen/TableGenBackend.h"
14 #define DEBUG_TYPE "attr-enum"
20 Attributes(const RecordKeeper
&R
) : Records(R
) {}
21 void run(raw_ostream
&OS
);
24 void emitTargetIndependentNames(raw_ostream
&OS
);
25 void emitFnAttrCompatCheck(raw_ostream
&OS
, bool IsStringAttr
);
26 void emitAttributeProperties(raw_ostream
&OF
);
28 const RecordKeeper
&Records
;
31 } // End anonymous namespace.
33 void Attributes::emitTargetIndependentNames(raw_ostream
&OS
) {
34 OS
<< "#ifdef GET_ATTR_NAMES\n";
35 OS
<< "#undef GET_ATTR_NAMES\n";
37 OS
<< "#ifndef ATTRIBUTE_ALL\n";
38 OS
<< "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
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";
46 for (StringRef KindName
: KindNames
) {
47 for (auto *A
: Records
.getAllDerivedDefinitions(KindName
)) {
48 OS
<< MacroName
<< "(" << A
->getName() << ","
49 << A
->getValueAsString("AttrString") << ")\n";
52 OS
<< "#undef " << MacroName
<< "\n\n";
55 // Emit attribute enums in the same order llvm::Attribute::operator< expects.
56 Emit({"EnumAttr", "TypeAttr", "IntAttr", "ConstantRangeAttr",
57 "ConstantRangeListAttr"},
59 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
60 Emit({"ComplexStrAttr"}, "ATTRIBUTE_COMPLEXSTR");
62 OS
<< "#undef ATTRIBUTE_ALL\n";
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(KindName
)) {
72 OS
<< A
->getName() << " = " << Value
<< ",\n";
75 OS
<< "Last" << KindName
<< " = " << (Value
- 1) << ",\n";
80 void Attributes::emitFnAttrCompatCheck(raw_ostream
&OS
, bool IsStringAttr
) {
81 OS
<< "#ifdef GET_ATTR_COMPAT_FUNC\n";
82 OS
<< "#undef GET_ATTR_COMPAT_FUNC\n";
84 OS
<< "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
85 << " const Function &Callee) {\n";
86 OS
<< " bool Ret = true;\n\n";
88 for (const Record
*Rule
: Records
.getAllDerivedDefinitions("CompatRule")) {
89 StringRef FuncName
= Rule
->getValueAsString("CompatFunc");
90 OS
<< " Ret &= " << FuncName
<< "(Caller, Callee";
91 StringRef AttrName
= Rule
->getValueAsString("AttrName");
92 if (!AttrName
.empty())
93 OS
<< ", \"" << AttrName
<< "\"";
98 OS
<< " return Ret;\n";
101 OS
<< "static inline void mergeFnAttrs(Function &Caller,\n"
102 << " const Function &Callee) {\n";
104 for (const Record
*Rule
: Records
.getAllDerivedDefinitions("MergeRule")) {
105 StringRef FuncName
= Rule
->getValueAsString("MergeFunc");
106 OS
<< " " << FuncName
<< "(Caller, Callee);\n";
114 void Attributes::emitAttributeProperties(raw_ostream
&OS
) {
115 OS
<< "#ifdef GET_ATTR_PROP_TABLE\n";
116 OS
<< "#undef GET_ATTR_PROP_TABLE\n";
117 OS
<< "static const uint8_t AttrPropTable[] = {\n";
118 for (StringRef KindName
: {"EnumAttr", "TypeAttr", "IntAttr",
119 "ConstantRangeAttr", "ConstantRangeListAttr"}) {
120 bool AllowIntersectAnd
= KindName
== "EnumAttr";
121 bool AllowIntersectMin
= KindName
== "IntAttr";
122 for (auto *A
: Records
.getAllDerivedDefinitions(KindName
)) {
124 for (const Init
*P
: *A
->getValueAsListInit("Properties")) {
125 if (!AllowIntersectAnd
&&
126 cast
<DefInit
>(P
)->getDef()->getName() == "IntersectAnd")
127 PrintFatalError("'IntersectAnd' only compatible with 'EnumAttr'");
128 if (!AllowIntersectMin
&&
129 cast
<DefInit
>(P
)->getDef()->getName() == "IntersectMin")
130 PrintFatalError("'IntersectMin' only compatible with 'IntAttr'");
132 OS
<< " | AttributeProperty::" << cast
<DefInit
>(P
)->getDef()->getName();
141 void Attributes::run(raw_ostream
&OS
) {
142 emitTargetIndependentNames(OS
);
143 emitFnAttrCompatCheck(OS
, false);
144 emitAttributeProperties(OS
);
147 static TableGen::Emitter::OptClass
<Attributes
> X("gen-attrs",
148 "Generate attributes");