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/Support/MemoryBuffer.h" 10 #include "llvm/TableGen/Record.h" 11 #include <algorithm> 12 #include <string> 13 #include <vector> 14 using namespace llvm; 15 16 #define DEBUG_TYPE "attr-enum" 17 18 namespace { 19 20 class Attributes { 21 public: 22 Attributes(RecordKeeper &R) : Records(R) {} 23 void emit(raw_ostream &OS); 24 25 private: 26 void emitTargetIndependentNames(raw_ostream &OS); 27 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr); 28 29 RecordKeeper &Records; 30 }; 31 32 } // End anonymous namespace. 33 34 void Attributes::emitTargetIndependentNames(raw_ostream &OS) { 35 OS << "#ifdef GET_ATTR_NAMES\n"; 36 OS << "#undef GET_ATTR_NAMES\n"; 37 38 OS << "#ifndef ATTRIBUTE_ALL\n"; 39 OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n"; 40 OS << "#endif\n\n"; 41 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"; 46 OS << "#endif\n\n"; 47 for (StringRef KindName : KindNames) { 48 for (auto A : Records.getAllDerivedDefinitions(KindName)) { 49 OS << MacroName << "(" << A->getName() << "," 50 << A->getValueAsString("AttrString") << ")\n"; 51 } 52 } 53 OS << "#undef " << MacroName << "\n\n"; 54 }; 55 56 // Emit attribute enums in the same order llvm::Attribute::operator< expects. 57 Emit({"EnumAttr", "TypeAttr", "IntAttr"}, "ATTRIBUTE_ENUM"); 58 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL"); 59 60 OS << "#undef ATTRIBUTE_ALL\n"; 61 OS << "#endif\n"; 62 } 63 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"; 67 68 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n" 69 << " const Function &Callee) {\n"; 70 OS << " bool Ret = true;\n\n"; 71 72 std::vector<Record *> CompatRules = 73 Records.getAllDerivedDefinitions("CompatRule"); 74 75 for (auto *Rule : CompatRules) { 76 StringRef FuncName = Rule->getValueAsString("CompatFunc"); 77 OS << " Ret &= " << FuncName << "(Caller, Callee);\n"; 78 } 79 80 OS << "\n"; 81 OS << " return Ret;\n"; 82 OS << "}\n\n"; 83 84 std::vector<Record *> MergeRules = 85 Records.getAllDerivedDefinitions("MergeRule"); 86 OS << "static inline void mergeFnAttrs(Function &Caller,\n" 87 << " const Function &Callee) {\n"; 88 89 for (auto *Rule : MergeRules) { 90 StringRef FuncName = Rule->getValueAsString("MergeFunc"); 91 OS << " " << FuncName << "(Caller, Callee);\n"; 92 } 93 94 OS << "}\n\n"; 95 96 OS << "#endif\n"; 97 } 98 99 void Attributes::emit(raw_ostream &OS) { 100 emitTargetIndependentNames(OS); 101 emitFnAttrCompatCheck(OS, false); 102 } 103 104 namespace llvm { 105 106 void EmitAttributes(RecordKeeper &RK, raw_ostream &OS) { 107 Attributes(RK).emit(OS); 108 } 109 110 } // End llvm namespace. 111