blob: f3f875e8ce0b931aec155eaff27de7d4a2ed9825 [file] [log] [blame]
Akira Hatanakad9326792015-11-11 20:35:42 +00001//===- Attributes.cpp - Generate attributes -------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Akira Hatanakad9326792015-11-11 20:35:42 +00006//
7//===----------------------------------------------------------------------===//
8
Akira Hatanakad9326792015-11-11 20:35:42 +00009#include "llvm/Support/MemoryBuffer.h"
Akira Hatanakad9326792015-11-11 20:35:42 +000010#include "llvm/TableGen/Record.h"
11#include <algorithm>
12#include <string>
13#include <vector>
14using namespace llvm;
15
16#define DEBUG_TYPE "attr-enum"
17
18namespace {
19
20class Attributes {
21public:
22 Attributes(RecordKeeper &R) : Records(R) {}
23 void emit(raw_ostream &OS);
24
25private:
Tykercfe87a42020-02-02 14:46:50 +010026 void emitTargetIndependentNames(raw_ostream &OS);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000027 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
28
Akira Hatanakad9326792015-11-11 20:35:42 +000029 RecordKeeper &Records;
30};
31
32} // End anonymous namespace.
33
Tykercfe87a42020-02-02 14:46:50 +010034void Attributes::emitTargetIndependentNames(raw_ostream &OS) {
35 OS << "#ifdef GET_ATTR_NAMES\n";
36 OS << "#undef GET_ATTR_NAMES\n";
Akira Hatanakad9326792015-11-11 20:35:42 +000037
Tykercfe87a42020-02-02 14:46:50 +010038 OS << "#ifndef ATTRIBUTE_ALL\n";
39 OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
40 OS << "#endif\n\n";
Akira Hatanakad9326792015-11-11 20:35:42 +000041
Benjamin Kramered766f12020-04-26 16:52:53 +020042 auto Emit = [&](ArrayRef<StringRef> KindNames, StringRef MacroName) {
Tykercfe87a42020-02-02 14:46:50 +010043 OS << "#ifndef " << MacroName << "\n";
Benjamin Kramered766f12020-04-26 16:52:53 +020044 OS << "#define " << MacroName
45 << "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, SECOND)\n";
Tykercfe87a42020-02-02 14:46:50 +010046 OS << "#endif\n\n";
Benjamin Kramered766f12020-04-26 16:52:53 +020047 for (StringRef KindName : KindNames) {
48 for (auto A : Records.getAllDerivedDefinitions(KindName)) {
49 OS << MacroName << "(" << A->getName() << ","
50 << A->getValueAsString("AttrString") << ")\n";
51 }
Tykercfe87a42020-02-02 14:46:50 +010052 }
53 OS << "#undef " << MacroName << "\n\n";
54 };
Amaury Sechet60b31452016-04-20 01:02:12 +000055
Benjamin Kramered766f12020-04-26 16:52:53 +020056 // Emit attribute enums in the same order llvm::Attribute::operator< expects.
57 Emit({"EnumAttr", "TypeAttr", "IntAttr"}, "ATTRIBUTE_ENUM");
58 Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");
Amaury Sechet60b31452016-04-20 01:02:12 +000059
Tykercfe87a42020-02-02 14:46:50 +010060 OS << "#undef ATTRIBUTE_ALL\n";
Amaury Sechet60b31452016-04-20 01:02:12 +000061 OS << "#endif\n";
62}
63
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000064void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
65 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
66 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
67
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000068 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) {
Craig Topperbcd3c372017-05-31 21:12:46 +000076 StringRef FuncName = Rule->getValueAsString("CompatFunc");
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000077 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) {
Craig Topperbcd3c372017-05-31 21:12:46 +000090 StringRef FuncName = Rule->getValueAsString("MergeFunc");
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000091 OS << " " << FuncName << "(Caller, Callee);\n";
92 }
93
94 OS << "}\n\n";
95
96 OS << "#endif\n";
97}
98
Akira Hatanakad9326792015-11-11 20:35:42 +000099void Attributes::emit(raw_ostream &OS) {
Tykercfe87a42020-02-02 14:46:50 +0100100 emitTargetIndependentNames(OS);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +0000101 emitFnAttrCompatCheck(OS, false);
Akira Hatanakad9326792015-11-11 20:35:42 +0000102}
103
104namespace llvm {
105
106void EmitAttributes(RecordKeeper &RK, raw_ostream &OS) {
107 Attributes(RK).emit(OS);
108}
109
110} // End llvm namespace.