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/Support/MemoryBuffer.h"
10 #include "llvm/TableGen/Record.h"
16 #define DEBUG_TYPE "attr-enum"
22 Attributes(RecordKeeper &R) : Records(R) {}
23 void emit(raw_ostream &OS);
26 void emitTargetIndependentNames(raw_ostream &OS);
27 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
29 RecordKeeper &Records;
32 } // End anonymous namespace.
34 void Attributes::emitTargetIndependentNames(raw_ostream &OS) {
35 OS << "#ifdef GET_ATTR_NAMES\n";
36 OS << "#undef GET_ATTR_NAMES\n";
38 OS << "#ifndef ATTRIBUTE_ALL\n";
39 OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
42 auto Emit = [&](ArrayRef<StringRef> KindNames, StringRef MacroName) {
43 OS << "#ifndef " << MacroName << "\n";
44 OS << "#define " << MacroName
45 << "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, SECOND)\n";
47 for (StringRef KindName : KindNames) {
48 for (auto A : Records.getAllDerivedDefinitions(KindName)) {
49 OS << MacroName << "(" << A->getName() << ","
50 << A->getValueAsString("AttrString") << ")\n";
53 OS << "#undef " << MacroName << "\n\n";
56 // Emit attribute enums in the same order llvm::Attribute::operator< expects.
57 Emit({"EnumAttr", "TypeAttr", "IntAttr"}, "ATTRIBUTE_ENUM");
58 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
60 OS << "#undef ATTRIBUTE_ALL\n";
64 void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
65 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
66 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
68 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
69 << " const Function &Callee) {\n";
70 OS << " bool Ret = true;\n\n";
72 std::vector<Record *> CompatRules =
73 Records.getAllDerivedDefinitions("CompatRule");
75 for (auto *Rule : CompatRules) {
76 StringRef FuncName = Rule->getValueAsString("CompatFunc");
77 OS << " Ret &= " << FuncName << "(Caller, Callee);\n";
81 OS << " return Ret;\n";
84 std::vector<Record *> MergeRules =
85 Records.getAllDerivedDefinitions("MergeRule");
86 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
87 << " const Function &Callee) {\n";
89 for (auto *Rule : MergeRules) {
90 StringRef FuncName = Rule->getValueAsString("MergeFunc");
91 OS << " " << FuncName << "(Caller, Callee);\n";
99 void Attributes::emit(raw_ostream &OS) {
100 emitTargetIndependentNames(OS);
101 emitFnAttrCompatCheck(OS, false);
106 void EmitAttributes(RecordKeeper &RK, raw_ostream &OS) {
107 Attributes(RK).emit(OS);
110 } // End llvm namespace.