Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 1 | //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the debug info Metadata classes. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/IR/DebugInfoMetadata.h" |
| 14 | #include "LLVMContextImpl.h" |
| 15 | #include "MetadataImpl.h" |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallSet.h" |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DIBuilder.h" |
Duncan P. N. Exon Smith | df52349 | 2015-02-18 20:32:57 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Instructions.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 21 | |
Mircea Trofin | b53eeb6 | 2018-12-21 22:48:50 +0000 | [diff] [blame] | 22 | #include <numeric> |
| 23 | |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 26 | DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line, |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 27 | unsigned Column, ArrayRef<Metadata *> MDs, |
| 28 | bool ImplicitCode) |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 29 | : MDNode(C, DILocationKind, Storage, MDs) { |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 30 | assert((MDs.size() == 1 || MDs.size() == 2) && |
| 31 | "Expected a scope and optional inlined-at"); |
| 32 | |
| 33 | // Set line and column. |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 34 | assert(Column < (1u << 16) && "Expected 16-bit column"); |
| 35 | |
| 36 | SubclassData32 = Line; |
| 37 | SubclassData16 = Column; |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 38 | |
| 39 | setImplicitCode(ImplicitCode); |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 42 | static void adjustColumn(unsigned &Column) { |
| 43 | // Set to unknown on overflow. We only have 16 bits to play with here. |
| 44 | if (Column >= (1u << 16)) |
| 45 | Column = 0; |
| 46 | } |
| 47 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 48 | DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line, |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 49 | unsigned Column, Metadata *Scope, |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 50 | Metadata *InlinedAt, bool ImplicitCode, |
| 51 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | af677eb | 2015-02-06 22:50:13 +0000 | [diff] [blame] | 52 | // Fixup column. |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 53 | adjustColumn(Column); |
| 54 | |
| 55 | if (Storage == Uniqued) { |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 56 | if (auto *N = getUniqued(Context.pImpl->DILocations, |
| 57 | DILocationInfo::KeyTy(Line, Column, Scope, |
| 58 | InlinedAt, ImplicitCode))) |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 59 | return N; |
| 60 | if (!ShouldCreate) |
| 61 | return nullptr; |
| 62 | } else { |
| 63 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); |
| 64 | } |
| 65 | |
| 66 | SmallVector<Metadata *, 2> Ops; |
| 67 | Ops.push_back(Scope); |
| 68 | if (InlinedAt) |
| 69 | Ops.push_back(InlinedAt); |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 70 | return storeImpl(new (Ops.size()) DILocation(Context, Storage, Line, Column, |
| 71 | Ops, ImplicitCode), |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 72 | Storage, Context.pImpl->DILocations); |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Vedant Kumar | 65b0d4d | 2018-04-12 20:58:24 +0000 | [diff] [blame] | 75 | const DILocation *DILocation::getMergedLocation(const DILocation *LocA, |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 76 | const DILocation *LocB) { |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 77 | if (!LocA || !LocB) |
| 78 | return nullptr; |
| 79 | |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 80 | if (LocA == LocB) |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 81 | return LocA; |
| 82 | |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 83 | SmallPtrSet<DILocation *, 5> InlinedLocationsA; |
| 84 | for (DILocation *L = LocA->getInlinedAt(); L; L = L->getInlinedAt()) |
| 85 | InlinedLocationsA.insert(L); |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 86 | SmallSet<std::pair<DIScope *, DILocation *>, 5> Locations; |
| 87 | DIScope *S = LocA->getScope(); |
| 88 | DILocation *L = LocA->getInlinedAt(); |
| 89 | while (S) { |
| 90 | Locations.insert(std::make_pair(S, L)); |
| 91 | S = S->getScope().resolve(); |
| 92 | if (!S && L) { |
| 93 | S = L->getScope(); |
| 94 | L = L->getInlinedAt(); |
| 95 | } |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 96 | } |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 97 | const DILocation *Result = LocB; |
| 98 | S = LocB->getScope(); |
| 99 | L = LocB->getInlinedAt(); |
| 100 | while (S) { |
| 101 | if (Locations.count(std::make_pair(S, L))) |
| 102 | break; |
| 103 | S = S->getScope().resolve(); |
| 104 | if (!S && L) { |
| 105 | S = L->getScope(); |
| 106 | L = L->getInlinedAt(); |
| 107 | } |
| 108 | } |
Adrian Prantl | 4ddd059 | 2018-08-24 23:30:57 +0000 | [diff] [blame] | 109 | |
| 110 | // If the two locations are irreconsilable, just pick one. This is misleading, |
| 111 | // but on the other hand, it's a "line 0" location. |
| 112 | if (!S || !isa<DILocalScope>(S)) |
| 113 | S = LocA->getScope(); |
David Blaikie | 2a813ef | 2018-08-23 22:35:58 +0000 | [diff] [blame] | 114 | return DILocation::get(Result->getContext(), 0, 0, S, L); |
Vedant Kumar | 2b881f5 | 2017-11-06 23:15:21 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Mircea Trofin | b53eeb6 | 2018-12-21 22:48:50 +0000 | [diff] [blame] | 117 | Optional<unsigned> DILocation::encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI) { |
| 118 | SmallVector<unsigned, 3> Components = {BD, DF, CI}; |
| 119 | uint64_t RemainingWork = 0U; |
| 120 | // We use RemainingWork to figure out if we have no remaining components to |
| 121 | // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to |
| 122 | // encode anything for the latter 2. |
| 123 | // Since any of the input components is at most 32 bits, their sum will be |
| 124 | // less than 34 bits, and thus RemainingWork won't overflow. |
| 125 | RemainingWork = std::accumulate(Components.begin(), Components.end(), RemainingWork); |
| 126 | |
| 127 | int I = 0; |
| 128 | unsigned Ret = 0; |
| 129 | unsigned NextBitInsertionIndex = 0; |
| 130 | while (RemainingWork > 0) { |
| 131 | unsigned C = Components[I++]; |
| 132 | RemainingWork -= C; |
| 133 | unsigned EC = encodeComponent(C); |
| 134 | Ret |= (EC << NextBitInsertionIndex); |
| 135 | NextBitInsertionIndex += encodingBits(C); |
| 136 | } |
| 137 | |
| 138 | // Encoding may be unsuccessful because of overflow. We determine success by |
| 139 | // checking equivalence of components before & after encoding. Alternatively, |
| 140 | // we could determine Success during encoding, but the current alternative is |
| 141 | // simpler. |
| 142 | unsigned TBD, TDF, TCI = 0; |
| 143 | decodeDiscriminator(Ret, TBD, TDF, TCI); |
| 144 | if (TBD == BD && TDF == DF && TCI == CI) |
| 145 | return Ret; |
| 146 | return None; |
| 147 | } |
| 148 | |
| 149 | void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, |
| 150 | unsigned &CI) { |
| 151 | BD = getUnsignedFromPrefixEncoding(D); |
| 152 | DF = getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D)); |
| 153 | CI = getUnsignedFromPrefixEncoding( |
| 154 | getNextComponentInDiscriminator(getNextComponentInDiscriminator(D))); |
| 155 | } |
| 156 | |
| 157 | |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 158 | DINode::DIFlags DINode::getFlag(StringRef Flag) { |
| 159 | return StringSwitch<DIFlags>(Flag) |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 160 | #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) |
| 161 | #include "llvm/IR/DebugInfoFlags.def" |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 162 | .Default(DINode::FlagZero); |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Mehdi Amini | f42ec79 | 2016-10-01 05:57:50 +0000 | [diff] [blame] | 165 | StringRef DINode::getFlagString(DIFlags Flag) { |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 166 | switch (Flag) { |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 167 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 168 | case Flag##NAME: \ |
| 169 | return "DIFlag" #NAME; |
| 170 | #include "llvm/IR/DebugInfoFlags.def" |
| 171 | } |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 172 | return ""; |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 175 | DINode::DIFlags DINode::splitFlags(DIFlags Flags, |
Leny Kholodov | 40c6235 | 2016-09-06 17:03:02 +0000 | [diff] [blame] | 176 | SmallVectorImpl<DIFlags> &SplitFlags) { |
Bob Haarman | 26a87bd | 2016-10-25 22:11:52 +0000 | [diff] [blame] | 177 | // Flags that are packed together need to be specially handled, so |
| 178 | // that, for example, we emit "DIFlagPublic" and not |
| 179 | // "DIFlagPrivate | DIFlagProtected". |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 180 | if (DIFlags A = Flags & FlagAccessibility) { |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 181 | if (A == FlagPrivate) |
| 182 | SplitFlags.push_back(FlagPrivate); |
| 183 | else if (A == FlagProtected) |
| 184 | SplitFlags.push_back(FlagProtected); |
| 185 | else |
| 186 | SplitFlags.push_back(FlagPublic); |
| 187 | Flags &= ~A; |
| 188 | } |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 189 | if (DIFlags R = Flags & FlagPtrToMemberRep) { |
Reid Kleckner | 604105b | 2016-06-17 21:31:33 +0000 | [diff] [blame] | 190 | if (R == FlagSingleInheritance) |
| 191 | SplitFlags.push_back(FlagSingleInheritance); |
| 192 | else if (R == FlagMultipleInheritance) |
| 193 | SplitFlags.push_back(FlagMultipleInheritance); |
| 194 | else |
| 195 | SplitFlags.push_back(FlagVirtualInheritance); |
| 196 | Flags &= ~R; |
| 197 | } |
Bob Haarman | 26a87bd | 2016-10-25 22:11:52 +0000 | [diff] [blame] | 198 | if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) { |
| 199 | Flags &= ~FlagIndirectVirtualBase; |
| 200 | SplitFlags.push_back(FlagIndirectVirtualBase); |
| 201 | } |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 202 | |
| 203 | #define HANDLE_DI_FLAG(ID, NAME) \ |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 204 | if (DIFlags Bit = Flags & Flag##NAME) { \ |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 205 | SplitFlags.push_back(Bit); \ |
| 206 | Flags &= ~Bit; \ |
| 207 | } |
| 208 | #include "llvm/IR/DebugInfoFlags.def" |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 209 | return Flags; |
| 210 | } |
| 211 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 212 | DIScopeRef DIScope::getScope() const { |
| 213 | if (auto *T = dyn_cast<DIType>(this)) |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 214 | return T->getScope(); |
| 215 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 216 | if (auto *SP = dyn_cast<DISubprogram>(this)) |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 217 | return SP->getScope(); |
| 218 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 219 | if (auto *LB = dyn_cast<DILexicalBlockBase>(this)) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 220 | return LB->getScope(); |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 221 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 222 | if (auto *NS = dyn_cast<DINamespace>(this)) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 223 | return NS->getScope(); |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 224 | |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 225 | if (auto *CB = dyn_cast<DICommonBlock>(this)) |
| 226 | return CB->getScope(); |
| 227 | |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 228 | if (auto *M = dyn_cast<DIModule>(this)) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 229 | return M->getScope(); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 230 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 231 | assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) && |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 232 | "Unhandled type of scope."); |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 236 | StringRef DIScope::getName() const { |
| 237 | if (auto *T = dyn_cast<DIType>(this)) |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 238 | return T->getName(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 239 | if (auto *SP = dyn_cast<DISubprogram>(this)) |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 240 | return SP->getName(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 241 | if (auto *NS = dyn_cast<DINamespace>(this)) |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 242 | return NS->getName(); |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 243 | if (auto *CB = dyn_cast<DICommonBlock>(this)) |
| 244 | return CB->getName(); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 245 | if (auto *M = dyn_cast<DIModule>(this)) |
| 246 | return M->getName(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 247 | assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) || |
| 248 | isa<DICompileUnit>(this)) && |
Duncan P. N. Exon Smith | f0d81a5 | 2015-04-11 17:37:23 +0000 | [diff] [blame] | 249 | "Unhandled type of scope."); |
| 250 | return ""; |
| 251 | } |
Duncan P. N. Exon Smith | 5261e4b | 2015-04-07 01:21:40 +0000 | [diff] [blame] | 252 | |
Duncan P. N. Exon Smith | c7e0813 | 2015-02-02 20:20:56 +0000 | [diff] [blame] | 253 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 9146fc8 | 2015-02-02 20:01:03 +0000 | [diff] [blame] | 254 | static bool isCanonical(const MDString *S) { |
| 255 | return !S || !S->getString().empty(); |
Duncan P. N. Exon Smith | 442ec02 | 2015-02-02 19:54:05 +0000 | [diff] [blame] | 256 | } |
Duncan P. N. Exon Smith | c7e0813 | 2015-02-02 20:20:56 +0000 | [diff] [blame] | 257 | #endif |
Duncan P. N. Exon Smith | 442ec02 | 2015-02-02 19:54:05 +0000 | [diff] [blame] | 258 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 259 | GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag, |
| 260 | MDString *Header, |
| 261 | ArrayRef<Metadata *> DwarfOps, |
| 262 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 263 | unsigned Hash = 0; |
| 264 | if (Storage == Uniqued) { |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 265 | GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 266 | if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key)) |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 267 | return N; |
| 268 | if (!ShouldCreate) |
| 269 | return nullptr; |
| 270 | Hash = Key.getHash(); |
| 271 | } else { |
| 272 | assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); |
| 273 | } |
| 274 | |
| 275 | // Use a nullptr for empty headers. |
Duncan P. N. Exon Smith | 9146fc8 | 2015-02-02 20:01:03 +0000 | [diff] [blame] | 276 | assert(isCanonical(Header) && "Expected canonical MDString"); |
| 277 | Metadata *PreOps[] = {Header}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 278 | return storeImpl(new (DwarfOps.size() + 1) GenericDINode( |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 279 | Context, Storage, Hash, Tag, PreOps, DwarfOps), |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 280 | Storage, Context.pImpl->GenericDINodes); |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 283 | void GenericDINode::recalculateHash() { |
| 284 | setHash(GenericDINodeInfo::KeyTy::calculateHash(this)); |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 285 | } |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 286 | |
| 287 | #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ |
| 288 | #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS |
| 289 | #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ |
| 290 | do { \ |
| 291 | if (Storage == Uniqued) { \ |
| 292 | if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ |
| 293 | CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ |
| 294 | return N; \ |
| 295 | if (!ShouldCreate) \ |
| 296 | return nullptr; \ |
| 297 | } else { \ |
| 298 | assert(ShouldCreate && \ |
| 299 | "Expected non-uniqued nodes to always be created"); \ |
| 300 | } \ |
| 301 | } while (false) |
| 302 | #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \ |
David Blaikie | 6662d6a | 2016-04-13 17:42:56 +0000 | [diff] [blame] | 303 | return storeImpl(new (array_lengthof(OPS)) \ |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 304 | CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ |
| 305 | Storage, Context.pImpl->CLASS##s) |
| 306 | #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \ |
| 307 | return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \ |
| 308 | Storage, Context.pImpl->CLASS##s) |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 309 | #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \ |
David Blaikie | 6662d6a | 2016-04-13 17:42:56 +0000 | [diff] [blame] | 310 | return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \ |
Duncan P. N. Exon Smith | bd33d37 | 2015-02-10 01:59:57 +0000 | [diff] [blame] | 311 | Storage, Context.pImpl->CLASS##s) |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 312 | #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \ |
| 313 | return storeImpl(new (NUM_OPS) \ |
| 314 | CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ |
| 315 | Storage, Context.pImpl->CLASS##s) |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 316 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 317 | DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 318 | StorageType Storage, bool ShouldCreate) { |
Sander de Smalen | fdf4091 | 2018-01-24 09:56:07 +0000 | [diff] [blame] | 319 | auto *CountNode = ConstantAsMetadata::get( |
| 320 | ConstantInt::getSigned(Type::getInt64Ty(Context), Count)); |
| 321 | return getImpl(Context, CountNode, Lo, Storage, ShouldCreate); |
| 322 | } |
| 323 | |
| 324 | DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, |
| 325 | int64_t Lo, StorageType Storage, |
| 326 | bool ShouldCreate) { |
| 327 | DEFINE_GETIMPL_LOOKUP(DISubrange, (CountNode, Lo)); |
| 328 | Metadata *Ops[] = { CountNode }; |
| 329 | DEFINE_GETIMPL_STORE(DISubrange, (CountNode, Lo), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 332 | DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value, |
Momchil Velikov | 08dc66e | 2018-02-12 16:10:09 +0000 | [diff] [blame] | 333 | bool IsUnsigned, MDString *Name, |
| 334 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 335 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Momchil Velikov | 08dc66e | 2018-02-12 16:10:09 +0000 | [diff] [blame] | 336 | DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, IsUnsigned, Name)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 337 | Metadata *Ops[] = {Name}; |
Momchil Velikov | 08dc66e | 2018-02-12 16:10:09 +0000 | [diff] [blame] | 338 | DEFINE_GETIMPL_STORE(DIEnumerator, (Value, IsUnsigned), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 341 | DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag, |
Duncan P. N. Exon Smith | d34db17 | 2015-02-19 23:56:07 +0000 | [diff] [blame] | 342 | MDString *Name, uint64_t SizeInBits, |
Victor Leschuk | 197aa31 | 2016-10-18 14:31:22 +0000 | [diff] [blame] | 343 | uint32_t AlignInBits, unsigned Encoding, |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 344 | DIFlags Flags, StorageType Storage, |
| 345 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 346 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 347 | DEFINE_GETIMPL_LOOKUP(DIBasicType, |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 348 | (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 349 | Metadata *Ops[] = {nullptr, nullptr, Name}; |
Adrian Prantl | 55f4262 | 2018-08-14 19:35:34 +0000 | [diff] [blame] | 350 | DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding, |
| 351 | Flags), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 354 | Optional<DIBasicType::Signedness> DIBasicType::getSignedness() const { |
| 355 | switch (getEncoding()) { |
| 356 | case dwarf::DW_ATE_signed: |
| 357 | case dwarf::DW_ATE_signed_char: |
| 358 | return Signedness::Signed; |
| 359 | case dwarf::DW_ATE_unsigned: |
| 360 | case dwarf::DW_ATE_unsigned_char: |
| 361 | return Signedness::Unsigned; |
| 362 | default: |
| 363 | return None; |
| 364 | } |
| 365 | } |
| 366 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 367 | DIDerivedType *DIDerivedType::getImpl( |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 368 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, |
Duncan P. N. Exon Smith | d34db17 | 2015-02-19 23:56:07 +0000 | [diff] [blame] | 369 | unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 370 | uint32_t AlignInBits, uint64_t OffsetInBits, |
| 371 | Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData, |
| 372 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 373 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 374 | DEFINE_GETIMPL_LOOKUP(DIDerivedType, |
| 375 | (Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 376 | AlignInBits, OffsetInBits, DWARFAddressSpace, Flags, |
| 377 | ExtraData)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 378 | Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData}; |
| 379 | DEFINE_GETIMPL_STORE( |
Konstantin Zhuravlyov | d5561e0 | 2017-03-08 23:55:44 +0000 | [diff] [blame] | 380 | DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, |
| 381 | DWARFAddressSpace, Flags), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 384 | DICompositeType *DICompositeType::getImpl( |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 385 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, |
Duncan P. N. Exon Smith | d34db17 | 2015-02-19 23:56:07 +0000 | [diff] [blame] | 386 | unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, |
Victor Leschuk | 197aa31 | 2016-10-18 14:31:22 +0000 | [diff] [blame] | 387 | uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 388 | Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 389 | Metadata *TemplateParams, MDString *Identifier, Metadata *Discriminator, |
| 390 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 391 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Duncan P. N. Exon Smith | 5ab2be0 | 2016-04-17 03:58:21 +0000 | [diff] [blame] | 392 | |
Duncan P. N. Exon Smith | 9738602 | 2016-04-19 18:00:19 +0000 | [diff] [blame] | 393 | // Keep this in sync with buildODRType. |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 394 | DEFINE_GETIMPL_LOOKUP( |
| 395 | DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 396 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 397 | VTableHolder, TemplateParams, Identifier, Discriminator)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 398 | Metadata *Ops[] = {File, Scope, Name, BaseType, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 399 | Elements, VTableHolder, TemplateParams, Identifier, |
| 400 | Discriminator}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 401 | DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 402 | AlignInBits, OffsetInBits, Flags), |
| 403 | Ops); |
| 404 | } |
| 405 | |
Duncan P. N. Exon Smith | 9738602 | 2016-04-19 18:00:19 +0000 | [diff] [blame] | 406 | DICompositeType *DICompositeType::buildODRType( |
| 407 | LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, |
| 408 | Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, |
Victor Leschuk | 197aa31 | 2016-10-18 14:31:22 +0000 | [diff] [blame] | 409 | uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 410 | DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 411 | Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) { |
Duncan P. N. Exon Smith | 9738602 | 2016-04-19 18:00:19 +0000 | [diff] [blame] | 412 | assert(!Identifier.getString().empty() && "Expected valid identifier"); |
| 413 | if (!Context.isODRUniquingDebugTypes()) |
| 414 | return nullptr; |
| 415 | auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; |
| 416 | if (!CT) |
| 417 | return CT = DICompositeType::getDistinct( |
| 418 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 419 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 420 | VTableHolder, TemplateParams, &Identifier, Discriminator); |
Duncan P. N. Exon Smith | 9738602 | 2016-04-19 18:00:19 +0000 | [diff] [blame] | 421 | |
| 422 | // Only mutate CT if it's a forward declaration and the new operands aren't. |
| 423 | assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?"); |
| 424 | if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl)) |
| 425 | return CT; |
| 426 | |
| 427 | // Mutate CT in place. Keep this in sync with getImpl. |
| 428 | CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits, |
| 429 | Flags); |
| 430 | Metadata *Ops[] = {File, Scope, Name, BaseType, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 431 | Elements, VTableHolder, TemplateParams, &Identifier, |
| 432 | Discriminator}; |
Simon Pilgrim | 1ec7dc7 | 2016-05-02 16:45:02 +0000 | [diff] [blame] | 433 | assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() && |
Duncan P. N. Exon Smith | 9738602 | 2016-04-19 18:00:19 +0000 | [diff] [blame] | 434 | "Mismatched number of operands"); |
| 435 | for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I) |
| 436 | if (Ops[I] != CT->getOperand(I)) |
| 437 | CT->setOperand(I, Ops[I]); |
| 438 | return CT; |
| 439 | } |
| 440 | |
Duncan P. N. Exon Smith | 0b0271e | 2016-04-19 14:55:09 +0000 | [diff] [blame] | 441 | DICompositeType *DICompositeType::getODRType( |
| 442 | LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, |
| 443 | Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, |
Victor Leschuk | 197aa31 | 2016-10-18 14:31:22 +0000 | [diff] [blame] | 444 | uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 445 | DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 446 | Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator) { |
Duncan P. N. Exon Smith | 0b0271e | 2016-04-19 14:55:09 +0000 | [diff] [blame] | 447 | assert(!Identifier.getString().empty() && "Expected valid identifier"); |
| 448 | if (!Context.isODRUniquingDebugTypes()) |
| 449 | return nullptr; |
| 450 | auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; |
| 451 | if (!CT) |
| 452 | CT = DICompositeType::getDistinct( |
| 453 | Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, |
| 454 | AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, |
Adrian Prantl | 8c59921 | 2018-02-06 23:45:59 +0000 | [diff] [blame] | 455 | TemplateParams, &Identifier, Discriminator); |
Duncan P. N. Exon Smith | 0b0271e | 2016-04-19 14:55:09 +0000 | [diff] [blame] | 456 | return CT; |
| 457 | } |
| 458 | |
| 459 | DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context, |
| 460 | MDString &Identifier) { |
| 461 | assert(!Identifier.getString().empty() && "Expected valid identifier"); |
| 462 | if (!Context.isODRUniquingDebugTypes()) |
| 463 | return nullptr; |
| 464 | return Context.pImpl->DITypeMap->lookup(&Identifier); |
| 465 | } |
| 466 | |
Leny Kholodov | 40c6235 | 2016-09-06 17:03:02 +0000 | [diff] [blame] | 467 | DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags, |
| 468 | uint8_t CC, Metadata *TypeArray, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 469 | StorageType Storage, |
| 470 | bool ShouldCreate) { |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 471 | DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray)); |
Duncan P. N. Exon Smith | b9e045a | 2015-07-24 20:56:36 +0000 | [diff] [blame] | 472 | Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray}; |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 473 | DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Reid Kleckner | 26fa1bf | 2017-09-19 18:14:45 +0000 | [diff] [blame] | 476 | // FIXME: Implement this string-enum correspondence with a .def file and macros, |
| 477 | // so that the association is explicit rather than implied. |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 478 | static const char *ChecksumKindName[DIFile::CSK_Last] = { |
Amjad Aboud | 7faeecc | 2016-12-25 10:12:09 +0000 | [diff] [blame] | 479 | "CSK_MD5", |
| 480 | "CSK_SHA1" |
| 481 | }; |
| 482 | |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 483 | StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) { |
| 484 | assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind"); |
| 485 | // The first space was originally the CSK_None variant, which is now |
| 486 | // obsolete, but the space is still reserved in ChecksumKind, so we account |
| 487 | // for it here. |
| 488 | return ChecksumKindName[CSKind - 1]; |
Amjad Aboud | 7faeecc | 2016-12-25 10:12:09 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 491 | Optional<DIFile::ChecksumKind> DIFile::getChecksumKind(StringRef CSKindStr) { |
| 492 | return StringSwitch<Optional<DIFile::ChecksumKind>>(CSKindStr) |
| 493 | .Case("CSK_MD5", DIFile::CSK_MD5) |
| 494 | .Case("CSK_SHA1", DIFile::CSK_SHA1) |
| 495 | .Default(None); |
Amjad Aboud | 7faeecc | 2016-12-25 10:12:09 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 498 | DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename, |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 499 | MDString *Directory, |
| 500 | Optional<DIFile::ChecksumInfo<MDString *>> CS, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 501 | Optional<MDString *> Source, StorageType Storage, |
| 502 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 503 | assert(isCanonical(Filename) && "Expected canonical MDString"); |
| 504 | assert(isCanonical(Directory) && "Expected canonical MDString"); |
Scott Linder | 7160384 | 2018-02-12 19:45:54 +0000 | [diff] [blame] | 505 | assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString"); |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 506 | assert((!Source || isCanonical(*Source)) && "Expected canonical MDString"); |
| 507 | DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS, Source)); |
| 508 | Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr, |
| 509 | Source.getValueOr(nullptr)}; |
| 510 | DEFINE_GETIMPL_STORE(DIFile, (CS, Source), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 513 | DICompileUnit *DICompileUnit::getImpl( |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 514 | LLVMContext &Context, unsigned SourceLanguage, Metadata *File, |
| 515 | MDString *Producer, bool IsOptimized, MDString *Flags, |
| 516 | unsigned RuntimeVersion, MDString *SplitDebugFilename, |
| 517 | unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 518 | Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros, |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 519 | uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, |
David Blaikie | bb27911 | 2018-11-13 20:08:10 +0000 | [diff] [blame] | 520 | unsigned NameTableKind, bool RangesBaseAddress, StorageType Storage, |
| 521 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 522 | assert(Storage != Uniqued && "Cannot unique DICompileUnit"); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 523 | assert(isCanonical(Producer) && "Expected canonical MDString"); |
| 524 | assert(isCanonical(Flags) && "Expected canonical MDString"); |
| 525 | assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString"); |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 526 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 527 | Metadata *Ops[] = { |
| 528 | File, Producer, Flags, SplitDebugFilename, |
| 529 | EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities, |
| 530 | Macros}; |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 531 | return storeImpl(new (array_lengthof(Ops)) DICompileUnit( |
| 532 | Context, Storage, SourceLanguage, IsOptimized, |
| 533 | RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining, |
David Blaikie | bb27911 | 2018-11-13 20:08:10 +0000 | [diff] [blame] | 534 | DebugInfoForProfiling, NameTableKind, RangesBaseAddress, |
| 535 | Ops), |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 536 | Storage); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 539 | Optional<DICompileUnit::DebugEmissionKind> |
| 540 | DICompileUnit::getEmissionKind(StringRef Str) { |
| 541 | return StringSwitch<Optional<DebugEmissionKind>>(Str) |
| 542 | .Case("NoDebug", NoDebug) |
| 543 | .Case("FullDebug", FullDebug) |
| 544 | .Case("LineTablesOnly", LineTablesOnly) |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 545 | .Case("DebugDirectivesOnly", DebugDirectivesOnly) |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 546 | .Default(None); |
| 547 | } |
| 548 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 549 | Optional<DICompileUnit::DebugNameTableKind> |
| 550 | DICompileUnit::getNameTableKind(StringRef Str) { |
| 551 | return StringSwitch<Optional<DebugNameTableKind>>(Str) |
| 552 | .Case("Default", DebugNameTableKind::Default) |
| 553 | .Case("GNU", DebugNameTableKind::GNU) |
| 554 | .Case("None", DebugNameTableKind::None) |
| 555 | .Default(None); |
| 556 | } |
| 557 | |
Fangrui Song | 3c1b5db | 2018-07-06 19:26:00 +0000 | [diff] [blame] | 558 | const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) { |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 559 | switch (EK) { |
| 560 | case NoDebug: return "NoDebug"; |
| 561 | case FullDebug: return "FullDebug"; |
| 562 | case LineTablesOnly: return "LineTablesOnly"; |
Alexey Bataev | 075412d | 2018-08-23 17:43:40 +0000 | [diff] [blame] | 563 | case DebugDirectivesOnly: return "DebugDirectivesOnly"; |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 564 | } |
| 565 | return nullptr; |
| 566 | } |
| 567 | |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 568 | const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) { |
| 569 | switch (NTK) { |
| 570 | case DebugNameTableKind::Default: |
| 571 | return nullptr; |
| 572 | case DebugNameTableKind::GNU: |
| 573 | return "GNU"; |
| 574 | case DebugNameTableKind::None: |
| 575 | return "None"; |
| 576 | } |
| 577 | return nullptr; |
| 578 | } |
| 579 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 580 | DISubprogram *DILocalScope::getSubprogram() const { |
| 581 | if (auto *Block = dyn_cast<DILexicalBlockBase>(this)) |
Duncan P. N. Exon Smith | fd07a2a | 2015-03-30 21:32:28 +0000 | [diff] [blame] | 582 | return Block->getScope()->getSubprogram(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 583 | return const_cast<DISubprogram *>(cast<DISubprogram>(this)); |
Duncan P. N. Exon Smith | fd07a2a | 2015-03-30 21:32:28 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Amjad Aboud | a5ba991 | 2016-04-21 16:58:49 +0000 | [diff] [blame] | 586 | DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const { |
| 587 | if (auto *File = dyn_cast<DILexicalBlockFile>(this)) |
| 588 | return File->getScope()->getNonLexicalBlockFileScope(); |
| 589 | return const_cast<DILocalScope *>(this); |
| 590 | } |
| 591 | |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 592 | DISubprogram::DISPFlags DISubprogram::getFlag(StringRef Flag) { |
| 593 | return StringSwitch<DISPFlags>(Flag) |
| 594 | #define HANDLE_DISP_FLAG(ID, NAME) .Case("DISPFlag" #NAME, SPFlag##NAME) |
| 595 | #include "llvm/IR/DebugInfoFlags.def" |
| 596 | .Default(SPFlagZero); |
| 597 | } |
| 598 | |
| 599 | StringRef DISubprogram::getFlagString(DISPFlags Flag) { |
| 600 | switch (Flag) { |
Paul Robinson | 49f51bc | 2018-11-29 21:13:51 +0000 | [diff] [blame] | 601 | // Appease a warning. |
| 602 | case SPFlagVirtuality: |
Paul Robinson | adcdc1b | 2018-11-28 21:14:32 +0000 | [diff] [blame] | 603 | return ""; |
| 604 | #define HANDLE_DISP_FLAG(ID, NAME) \ |
| 605 | case SPFlag##NAME: \ |
| 606 | return "DISPFlag" #NAME; |
| 607 | #include "llvm/IR/DebugInfoFlags.def" |
| 608 | } |
| 609 | return ""; |
| 610 | } |
| 611 | |
| 612 | DISubprogram::DISPFlags |
| 613 | DISubprogram::splitFlags(DISPFlags Flags, |
| 614 | SmallVectorImpl<DISPFlags> &SplitFlags) { |
| 615 | // Multi-bit fields can require special handling. In our case, however, the |
| 616 | // only multi-bit field is virtuality, and all its values happen to be |
| 617 | // single-bit values, so the right behavior just falls out. |
| 618 | #define HANDLE_DISP_FLAG(ID, NAME) \ |
| 619 | if (DISPFlags Bit = Flags & SPFlag##NAME) { \ |
| 620 | SplitFlags.push_back(Bit); \ |
| 621 | Flags &= ~Bit; \ |
| 622 | } |
| 623 | #include "llvm/IR/DebugInfoFlags.def" |
| 624 | return Flags; |
| 625 | } |
| 626 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 627 | DISubprogram *DISubprogram::getImpl( |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 628 | LLVMContext &Context, Metadata *Scope, MDString *Name, |
| 629 | MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 630 | unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex, |
| 631 | int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 632 | Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes, |
Adrian Prantl | 1d12b88 | 2017-04-26 22:56:44 +0000 | [diff] [blame] | 633 | Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 634 | assert(isCanonical(Name) && "Expected canonical MDString"); |
| 635 | assert(isCanonical(LinkageName) && "Expected canonical MDString"); |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 636 | DEFINE_GETIMPL_LOOKUP(DISubprogram, |
| 637 | (Scope, Name, LinkageName, File, Line, Type, ScopeLine, |
| 638 | ContainingType, VirtualIndex, ThisAdjustment, Flags, |
| 639 | SPFlags, Unit, TemplateParams, Declaration, |
| 640 | RetainedNodes, ThrownTypes)); |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 641 | SmallVector<Metadata *, 11> Ops = { |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 642 | File, Scope, Name, LinkageName, Type, Unit, |
| 643 | Declaration, RetainedNodes, ContainingType, TemplateParams, ThrownTypes}; |
Adrian Prantl | 9d2f019 | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 644 | if (!ThrownTypes) { |
| 645 | Ops.pop_back(); |
| 646 | if (!TemplateParams) { |
| 647 | Ops.pop_back(); |
| 648 | if (!ContainingType) |
| 649 | Ops.pop_back(); |
| 650 | } |
| 651 | } |
Paul Robinson | cda5421 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 652 | DEFINE_GETIMPL_STORE_N( |
| 653 | DISubprogram, |
| 654 | (Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, SPFlags), Ops, |
| 655 | Ops.size()); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 658 | bool DISubprogram::describes(const Function *F) const { |
Duncan P. N. Exon Smith | 3c2d704 | 2015-04-13 19:07:27 +0000 | [diff] [blame] | 659 | assert(F && "Invalid function"); |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 660 | if (F->getSubprogram() == this) |
Duncan P. N. Exon Smith | 3c2d704 | 2015-04-13 19:07:27 +0000 | [diff] [blame] | 661 | return true; |
| 662 | StringRef Name = getLinkageName(); |
| 663 | if (Name.empty()) |
| 664 | Name = getName(); |
| 665 | return F->getName() == Name; |
| 666 | } |
| 667 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 668 | DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 669 | Metadata *File, unsigned Line, |
| 670 | unsigned Column, StorageType Storage, |
| 671 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | b09eb9f | 2015-08-28 22:58:50 +0000 | [diff] [blame] | 672 | // Fixup column. |
| 673 | adjustColumn(Column); |
| 674 | |
Duncan P. N. Exon Smith | 0e202b9 | 2015-03-30 16:37:48 +0000 | [diff] [blame] | 675 | assert(Scope && "Expected scope"); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 676 | DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 677 | Metadata *Ops[] = {File, Scope}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 678 | DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 681 | DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 682 | Metadata *Scope, Metadata *File, |
| 683 | unsigned Discriminator, |
| 684 | StorageType Storage, |
| 685 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 0e202b9 | 2015-03-30 16:37:48 +0000 | [diff] [blame] | 686 | assert(Scope && "Expected scope"); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 687 | DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 688 | Metadata *Ops[] = {File, Scope}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 689 | DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 692 | DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope, |
Adrian Prantl | fed4f39 | 2017-04-28 22:25:46 +0000 | [diff] [blame] | 693 | MDString *Name, bool ExportSymbols, |
| 694 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 695 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Adrian Prantl | fed4f39 | 2017-04-28 22:25:46 +0000 | [diff] [blame] | 696 | DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols)); |
| 697 | // The nullptr is for DIScope's File operand. This should be refactored. |
| 698 | Metadata *Ops[] = {nullptr, Scope, Name}; |
| 699 | DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 702 | DICommonBlock *DICommonBlock::getImpl(LLVMContext &Context, Metadata *Scope, |
| 703 | Metadata *Decl, MDString *Name, |
| 704 | Metadata *File, unsigned LineNo, |
| 705 | StorageType Storage, bool ShouldCreate) { |
| 706 | assert(isCanonical(Name) && "Expected canonical MDString"); |
| 707 | DEFINE_GETIMPL_LOOKUP(DICommonBlock, (Scope, Decl, Name, File, LineNo)); |
| 708 | // The nullptr is for DIScope's File operand. This should be refactored. |
| 709 | Metadata *Ops[] = {Scope, Decl, Name, File}; |
| 710 | DEFINE_GETIMPL_STORE(DICommonBlock, (LineNo), Ops); |
| 711 | } |
| 712 | |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 713 | DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope, |
| 714 | MDString *Name, MDString *ConfigurationMacros, |
| 715 | MDString *IncludePath, MDString *ISysRoot, |
| 716 | StorageType Storage, bool ShouldCreate) { |
| 717 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 718 | DEFINE_GETIMPL_LOOKUP( |
| 719 | DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot)); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 720 | Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot}; |
| 721 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops); |
| 722 | } |
| 723 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 724 | DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context, |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 725 | MDString *Name, |
| 726 | Metadata *Type, |
| 727 | StorageType Storage, |
| 728 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 729 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 730 | DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type)); |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 731 | Metadata *Ops[] = {Name, Type}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 732 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 735 | DITemplateValueParameter *DITemplateValueParameter::getImpl( |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 736 | LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, |
| 737 | Metadata *Value, StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 738 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 739 | DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value)); |
Duncan P. N. Exon Smith | 3d62bba | 2015-02-19 00:37:21 +0000 | [diff] [blame] | 740 | Metadata *Ops[] = {Name, Type, Value}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 741 | DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 744 | DIGlobalVariable * |
| 745 | DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 746 | MDString *LinkageName, Metadata *File, unsigned Line, |
| 747 | Metadata *Type, bool IsLocalToUnit, bool IsDefinition, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 748 | Metadata *StaticDataMemberDeclaration, |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 749 | Metadata *TemplateParams, uint32_t AlignInBits, |
| 750 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 751 | assert(isCanonical(Name) && "Expected canonical MDString"); |
| 752 | assert(isCanonical(LinkageName) && "Expected canonical MDString"); |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 753 | DEFINE_GETIMPL_LOOKUP(DIGlobalVariable, (Scope, Name, LinkageName, File, Line, |
| 754 | Type, IsLocalToUnit, IsDefinition, |
| 755 | StaticDataMemberDeclaration, |
| 756 | TemplateParams, AlignInBits)); |
| 757 | Metadata *Ops[] = {Scope, |
| 758 | Name, |
| 759 | File, |
| 760 | Type, |
| 761 | Name, |
| 762 | LinkageName, |
| 763 | StaticDataMemberDeclaration, |
| 764 | TemplateParams}; |
Victor Leschuk | 2ede126 | 2016-10-20 00:13:12 +0000 | [diff] [blame] | 765 | DEFINE_GETIMPL_STORE(DIGlobalVariable, |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 766 | (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 769 | DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope, |
| 770 | MDString *Name, Metadata *File, |
| 771 | unsigned Line, Metadata *Type, |
Leny Kholodov | 5fcc418 | 2016-09-06 10:46:28 +0000 | [diff] [blame] | 772 | unsigned Arg, DIFlags Flags, |
Victor Leschuk | a37660c | 2016-10-26 21:32:29 +0000 | [diff] [blame] | 773 | uint32_t AlignInBits, |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 774 | StorageType Storage, |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 775 | bool ShouldCreate) { |
Duncan P. N. Exon Smith | 1ec75ae | 2015-04-28 01:07:33 +0000 | [diff] [blame] | 776 | // 64K ought to be enough for any frontend. |
| 777 | assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits"); |
Duncan P. N. Exon Smith | 72fe2d0 | 2015-02-13 01:39:44 +0000 | [diff] [blame] | 778 | |
Duncan P. N. Exon Smith | e2c61d9 | 2015-03-27 17:56:39 +0000 | [diff] [blame] | 779 | assert(Scope && "Expected scope"); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 780 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Duncan P. N. Exon Smith | ed013cd | 2015-07-31 18:58:39 +0000 | [diff] [blame] | 781 | DEFINE_GETIMPL_LOOKUP(DILocalVariable, |
Victor Leschuk | 2ede126 | 2016-10-20 00:13:12 +0000 | [diff] [blame] | 782 | (Scope, Name, File, Line, Type, Arg, Flags, |
| 783 | AlignInBits)); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 784 | Metadata *Ops[] = {Scope, Name, File, Type}; |
Victor Leschuk | 2ede126 | 2016-10-20 00:13:12 +0000 | [diff] [blame] | 785 | DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Adrian Prantl | 3e0e1d0 | 2017-11-28 00:57:51 +0000 | [diff] [blame] | 788 | Optional<uint64_t> DIVariable::getSizeInBits() const { |
| 789 | // This is used by the Verifier so be mindful of broken types. |
| 790 | const Metadata *RawType = getRawType(); |
| 791 | while (RawType) { |
| 792 | // Try to get the size directly. |
| 793 | if (auto *T = dyn_cast<DIType>(RawType)) |
| 794 | if (uint64_t Size = T->getSizeInBits()) |
| 795 | return Size; |
| 796 | |
| 797 | if (auto *DT = dyn_cast<DIDerivedType>(RawType)) { |
| 798 | // Look at the base type. |
| 799 | RawType = DT->getRawBaseType(); |
| 800 | continue; |
| 801 | } |
| 802 | |
| 803 | // Missing type or size. |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | // Fail gracefully. |
| 808 | return None; |
| 809 | } |
| 810 | |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 811 | DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope, |
| 812 | MDString *Name, Metadata *File, unsigned Line, |
| 813 | StorageType Storage, |
| 814 | bool ShouldCreate) { |
| 815 | assert(Scope && "Expected scope"); |
| 816 | assert(isCanonical(Name) && "Expected canonical MDString"); |
| 817 | DEFINE_GETIMPL_LOOKUP(DILabel, |
| 818 | (Scope, Name, File, Line)); |
| 819 | Metadata *Ops[] = {Scope, Name, File}; |
| 820 | DEFINE_GETIMPL_STORE(DILabel, (Line), Ops); |
| 821 | } |
| 822 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 823 | DIExpression *DIExpression::getImpl(LLVMContext &Context, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 824 | ArrayRef<uint64_t> Elements, |
| 825 | StorageType Storage, bool ShouldCreate) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 826 | DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements)); |
| 827 | DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 830 | unsigned DIExpression::ExprOperand::getSize() const { |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 831 | switch (getOp()) { |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 832 | case dwarf::DW_OP_LLVM_convert: |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 833 | case dwarf::DW_OP_LLVM_fragment: |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 834 | return 3; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 835 | case dwarf::DW_OP_constu: |
Florian Hahn | c9c403c | 2017-06-13 16:54:44 +0000 | [diff] [blame] | 836 | case dwarf::DW_OP_plus_uconst: |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 837 | return 2; |
| 838 | default: |
| 839 | return 1; |
| 840 | } |
| 841 | } |
| 842 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 843 | bool DIExpression::isValid() const { |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 844 | for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { |
| 845 | // Check that there's space for the operand. |
| 846 | if (I->get() + I->getSize() > E->get()) |
| 847 | return false; |
| 848 | |
| 849 | // Check that the operand is valid. |
| 850 | switch (I->getOp()) { |
| 851 | default: |
| 852 | return false; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 853 | case dwarf::DW_OP_LLVM_fragment: |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 854 | // A fragment operator must appear at the end. |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 855 | return I->get() + I->getSize() == E->get(); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 856 | case dwarf::DW_OP_stack_value: { |
| 857 | // Must be the last one or followed by a DW_OP_LLVM_fragment. |
| 858 | if (I->get() + I->getSize() == E->get()) |
| 859 | break; |
| 860 | auto J = I; |
| 861 | if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment) |
| 862 | return false; |
| 863 | break; |
| 864 | } |
Konstantin Zhuravlyov | f9b41cd | 2017-03-08 00:28:57 +0000 | [diff] [blame] | 865 | case dwarf::DW_OP_swap: { |
| 866 | // Must be more than one implicit element on the stack. |
| 867 | |
| 868 | // FIXME: A better way to implement this would be to add a local variable |
| 869 | // that keeps track of the stack depth and introduce something like a |
| 870 | // DW_LLVM_OP_implicit_location as a placeholder for the location this |
| 871 | // DIExpression is attached to, or else pass the number of implicit stack |
| 872 | // elements into isValid. |
| 873 | if (getNumElements() == 1) |
| 874 | return false; |
| 875 | break; |
| 876 | } |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 877 | case dwarf::DW_OP_LLVM_convert: |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 878 | case dwarf::DW_OP_constu: |
Florian Hahn | c9c403c | 2017-06-13 16:54:44 +0000 | [diff] [blame] | 879 | case dwarf::DW_OP_plus_uconst: |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 880 | case dwarf::DW_OP_plus: |
Evgeniy Stepanov | f608111 | 2015-09-30 19:55:43 +0000 | [diff] [blame] | 881 | case dwarf::DW_OP_minus: |
Strahinja Petrovic | 29202f6 | 2017-09-21 10:04:02 +0000 | [diff] [blame] | 882 | case dwarf::DW_OP_mul: |
Vedant Kumar | 4011c26 | 2018-02-13 01:09:52 +0000 | [diff] [blame] | 883 | case dwarf::DW_OP_div: |
| 884 | case dwarf::DW_OP_mod: |
Vedant Kumar | 04386d8 | 2018-02-09 19:19:55 +0000 | [diff] [blame] | 885 | case dwarf::DW_OP_or: |
Petar Jovanovic | 1768957 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 886 | case dwarf::DW_OP_and: |
Vedant Kumar | 96b7dc0 | 2018-02-13 01:09:46 +0000 | [diff] [blame] | 887 | case dwarf::DW_OP_xor: |
Vedant Kumar | 31ec356 | 2018-02-13 01:09:49 +0000 | [diff] [blame] | 888 | case dwarf::DW_OP_shl: |
| 889 | case dwarf::DW_OP_shr: |
| 890 | case dwarf::DW_OP_shra: |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 891 | case dwarf::DW_OP_deref: |
Konstantin Zhuravlyov | f9b41cd | 2017-03-08 00:28:57 +0000 | [diff] [blame] | 892 | case dwarf::DW_OP_xderef: |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 893 | case dwarf::DW_OP_lit0: |
| 894 | case dwarf::DW_OP_not: |
| 895 | case dwarf::DW_OP_dup: |
Duncan P. N. Exon Smith | 193a4fd | 2015-02-13 01:07:46 +0000 | [diff] [blame] | 896 | break; |
| 897 | } |
| 898 | } |
| 899 | return true; |
| 900 | } |
| 901 | |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 902 | Optional<DIExpression::FragmentInfo> |
| 903 | DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) { |
| 904 | for (auto I = Start; I != End; ++I) |
| 905 | if (I->getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 906 | DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)}; |
| 907 | return Info; |
| 908 | } |
| 909 | return None; |
Duncan P. N. Exon Smith | 86cc332 | 2015-04-07 03:49:59 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 912 | void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops, |
| 913 | int64_t Offset) { |
| 914 | if (Offset > 0) { |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 915 | Ops.push_back(dwarf::DW_OP_plus_uconst); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 916 | Ops.push_back(Offset); |
| 917 | } else if (Offset < 0) { |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 918 | Ops.push_back(dwarf::DW_OP_constu); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 919 | Ops.push_back(-Offset); |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 920 | Ops.push_back(dwarf::DW_OP_minus); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 924 | bool DIExpression::extractIfOffset(int64_t &Offset) const { |
| 925 | if (getNumElements() == 0) { |
| 926 | Offset = 0; |
| 927 | return true; |
| 928 | } |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 929 | |
| 930 | if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) { |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 931 | Offset = Elements[1]; |
| 932 | return true; |
| 933 | } |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 934 | |
| 935 | if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) { |
| 936 | if (Elements[2] == dwarf::DW_OP_plus) { |
| 937 | Offset = Elements[1]; |
| 938 | return true; |
| 939 | } |
| 940 | if (Elements[2] == dwarf::DW_OP_minus) { |
| 941 | Offset = -Elements[1]; |
| 942 | return true; |
| 943 | } |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 944 | } |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 945 | |
Reid Kleckner | b5fced7 | 2017-05-09 19:59:29 +0000 | [diff] [blame] | 946 | return false; |
| 947 | } |
| 948 | |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 949 | const DIExpression *DIExpression::extractAddressClass(const DIExpression *Expr, |
| 950 | unsigned &AddrClass) { |
| 951 | const unsigned PatternSize = 4; |
| 952 | if (Expr->Elements.size() >= PatternSize && |
| 953 | Expr->Elements[PatternSize - 4] == dwarf::DW_OP_constu && |
| 954 | Expr->Elements[PatternSize - 2] == dwarf::DW_OP_swap && |
| 955 | Expr->Elements[PatternSize - 1] == dwarf::DW_OP_xderef) { |
| 956 | AddrClass = Expr->Elements[PatternSize - 3]; |
| 957 | |
| 958 | if (Expr->Elements.size() == PatternSize) |
| 959 | return nullptr; |
| 960 | return DIExpression::get(Expr->getContext(), |
| 961 | makeArrayRef(&*Expr->Elements.begin(), |
| 962 | Expr->Elements.size() - PatternSize)); |
| 963 | } |
| 964 | return Expr; |
| 965 | } |
| 966 | |
Adrian Prantl | d131701 | 2017-12-08 21:58:18 +0000 | [diff] [blame] | 967 | DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore, |
| 968 | int64_t Offset, bool DerefAfter, |
| 969 | bool StackValue) { |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 970 | SmallVector<uint64_t, 8> Ops; |
Adrian Prantl | d131701 | 2017-12-08 21:58:18 +0000 | [diff] [blame] | 971 | if (DerefBefore) |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 972 | Ops.push_back(dwarf::DW_OP_deref); |
Bjorn Pettersson | aa02580 | 2018-07-03 12:39:52 +0000 | [diff] [blame] | 973 | |
Adrian Prantl | d131701 | 2017-12-08 21:58:18 +0000 | [diff] [blame] | 974 | appendOffset(Ops, Offset); |
| 975 | if (DerefAfter) |
| 976 | Ops.push_back(dwarf::DW_OP_deref); |
| 977 | |
Adrian Prantl | 210a29d | 2018-04-27 21:41:36 +0000 | [diff] [blame] | 978 | return prependOpcodes(Expr, Ops, StackValue); |
Vedant Kumar | 04386d8 | 2018-02-09 19:19:55 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Adrian Prantl | 210a29d | 2018-04-27 21:41:36 +0000 | [diff] [blame] | 981 | DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, |
| 982 | SmallVectorImpl<uint64_t> &Ops, |
| 983 | bool StackValue) { |
Vedant Kumar | 8a36808 | 2018-07-06 21:06:20 +0000 | [diff] [blame] | 984 | assert(Expr && "Can't prepend ops to this expression"); |
| 985 | |
Bjorn Pettersson | 8dd6cf7 | 2018-07-03 11:29:00 +0000 | [diff] [blame] | 986 | // If there are no ops to prepend, do not even add the DW_OP_stack_value. |
| 987 | if (Ops.empty()) |
| 988 | StackValue = false; |
Vedant Kumar | 8a36808 | 2018-07-06 21:06:20 +0000 | [diff] [blame] | 989 | for (auto Op : Expr->expr_ops()) { |
| 990 | // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. |
| 991 | if (StackValue) { |
| 992 | if (Op.getOp() == dwarf::DW_OP_stack_value) |
| 993 | StackValue = false; |
| 994 | else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 995 | Ops.push_back(dwarf::DW_OP_stack_value); |
| 996 | StackValue = false; |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 997 | } |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 998 | } |
Vedant Kumar | 71c7c43 | 2018-07-06 21:06:21 +0000 | [diff] [blame] | 999 | Op.appendToVector(Ops); |
Vedant Kumar | 8a36808 | 2018-07-06 21:06:20 +0000 | [diff] [blame] | 1000 | } |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 1001 | if (StackValue) |
| 1002 | Ops.push_back(dwarf::DW_OP_stack_value); |
Adrian Prantl | 109b236 | 2017-04-28 17:51:05 +0000 | [diff] [blame] | 1003 | return DIExpression::get(Expr->getContext(), Ops); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Vedant Kumar | b572f64 | 2018-07-26 20:56:53 +0000 | [diff] [blame] | 1006 | DIExpression *DIExpression::append(const DIExpression *Expr, |
| 1007 | ArrayRef<uint64_t> Ops) { |
| 1008 | assert(Expr && !Ops.empty() && "Can't append ops to this expression"); |
| 1009 | |
| 1010 | // Copy Expr's current op list. |
| 1011 | SmallVector<uint64_t, 16> NewOps; |
| 1012 | for (auto Op : Expr->expr_ops()) { |
| 1013 | // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}. |
| 1014 | if (Op.getOp() == dwarf::DW_OP_stack_value || |
| 1015 | Op.getOp() == dwarf::DW_OP_LLVM_fragment) { |
| 1016 | NewOps.append(Ops.begin(), Ops.end()); |
| 1017 | |
| 1018 | // Ensure that the new opcodes are only appended once. |
| 1019 | Ops = None; |
| 1020 | } |
| 1021 | Op.appendToVector(NewOps); |
| 1022 | } |
| 1023 | |
| 1024 | NewOps.append(Ops.begin(), Ops.end()); |
| 1025 | return DIExpression::get(Expr->getContext(), NewOps); |
| 1026 | } |
| 1027 | |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1028 | DIExpression *DIExpression::appendToStack(const DIExpression *Expr, |
| 1029 | ArrayRef<uint64_t> Ops) { |
| 1030 | assert(Expr && !Ops.empty() && "Can't append ops to this expression"); |
Vedant Kumar | b572f64 | 2018-07-26 20:56:53 +0000 | [diff] [blame] | 1031 | assert(none_of(Ops, |
| 1032 | [](uint64_t Op) { |
| 1033 | return Op == dwarf::DW_OP_stack_value || |
| 1034 | Op == dwarf::DW_OP_LLVM_fragment; |
| 1035 | }) && |
| 1036 | "Can't append this op"); |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Append a DW_OP_deref after Expr's current op list if it's non-empty and |
| 1039 | // has no DW_OP_stack_value. |
| 1040 | // |
| 1041 | // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?. |
| 1042 | Optional<FragmentInfo> FI = Expr->getFragmentInfo(); |
| 1043 | unsigned DropUntilStackValue = FI.hasValue() ? 3 : 0; |
Vedant Kumar | b572f64 | 2018-07-26 20:56:53 +0000 | [diff] [blame] | 1044 | ArrayRef<uint64_t> ExprOpsBeforeFragment = |
| 1045 | Expr->getElements().drop_back(DropUntilStackValue); |
| 1046 | bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) && |
| 1047 | (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value); |
| 1048 | bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty(); |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1049 | |
Vedant Kumar | b572f64 | 2018-07-26 20:56:53 +0000 | [diff] [blame] | 1050 | // Append a DW_OP_deref after Expr's current op list if needed, then append |
| 1051 | // the new ops, and finally ensure that a single DW_OP_stack_value is present. |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1052 | SmallVector<uint64_t, 16> NewOps; |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1053 | if (NeedsDeref) |
| 1054 | NewOps.push_back(dwarf::DW_OP_deref); |
| 1055 | NewOps.append(Ops.begin(), Ops.end()); |
Vedant Kumar | b572f64 | 2018-07-26 20:56:53 +0000 | [diff] [blame] | 1056 | if (NeedsStackValue) |
| 1057 | NewOps.push_back(dwarf::DW_OP_stack_value); |
| 1058 | return DIExpression::append(Expr, NewOps); |
Vedant Kumar | 6379a62 | 2018-07-06 17:32:39 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Adrian Prantl | 25a09dd | 2017-11-07 00:45:34 +0000 | [diff] [blame] | 1061 | Optional<DIExpression *> DIExpression::createFragmentExpression( |
| 1062 | const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) { |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 1063 | SmallVector<uint64_t, 8> Ops; |
| 1064 | // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment. |
| 1065 | if (Expr) { |
| 1066 | for (auto Op : Expr->expr_ops()) { |
Adrian Prantl | 25a09dd | 2017-11-07 00:45:34 +0000 | [diff] [blame] | 1067 | switch (Op.getOp()) { |
| 1068 | default: break; |
| 1069 | case dwarf::DW_OP_plus: |
| 1070 | case dwarf::DW_OP_minus: |
| 1071 | // We can't safely split arithmetic into multiple fragments because we |
| 1072 | // can't express carry-over between fragments. |
| 1073 | // |
| 1074 | // FIXME: We *could* preserve the lowest fragment of a constant offset |
| 1075 | // operation if the offset fits into SizeInBits. |
| 1076 | return None; |
| 1077 | case dwarf::DW_OP_LLVM_fragment: { |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 1078 | // Make the new offset point into the existing fragment. |
| 1079 | uint64_t FragmentOffsetInBits = Op.getArg(0); |
Bjorn Pettersson | 5479ad2 | 2018-05-03 17:04:21 +0000 | [diff] [blame] | 1080 | uint64_t FragmentSizeInBits = Op.getArg(1); |
| 1081 | (void)FragmentSizeInBits; |
| 1082 | assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) && |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 1083 | "new fragment outside of original fragment"); |
| 1084 | OffsetInBits += FragmentOffsetInBits; |
Adrian Prantl | 25a09dd | 2017-11-07 00:45:34 +0000 | [diff] [blame] | 1085 | continue; |
| 1086 | } |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 1087 | } |
Vedant Kumar | 71c7c43 | 2018-07-06 21:06:21 +0000 | [diff] [blame] | 1088 | Op.appendToVector(Ops); |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | Ops.push_back(dwarf::DW_OP_LLVM_fragment); |
| 1092 | Ops.push_back(OffsetInBits); |
| 1093 | Ops.push_back(SizeInBits); |
| 1094 | return DIExpression::get(Expr->getContext(), Ops); |
| 1095 | } |
| 1096 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 1097 | bool DIExpression::isConstant() const { |
| 1098 | // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?. |
| 1099 | if (getNumElements() != 3 && getNumElements() != 6) |
| 1100 | return false; |
| 1101 | if (getElement(0) != dwarf::DW_OP_constu || |
| 1102 | getElement(2) != dwarf::DW_OP_stack_value) |
| 1103 | return false; |
| 1104 | if (getNumElements() == 6 && getElement(3) != dwarf::DW_OP_LLVM_fragment) |
| 1105 | return false; |
| 1106 | return true; |
| 1107 | } |
| 1108 | |
| 1109 | DIGlobalVariableExpression * |
| 1110 | DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable, |
| 1111 | Metadata *Expression, StorageType Storage, |
| 1112 | bool ShouldCreate) { |
| 1113 | DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression)); |
| 1114 | Metadata *Ops[] = {Variable, Expression}; |
| 1115 | DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops); |
| 1116 | } |
| 1117 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1118 | DIObjCProperty *DIObjCProperty::getImpl( |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1119 | LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, |
| 1120 | MDString *GetterName, MDString *SetterName, unsigned Attributes, |
| 1121 | Metadata *Type, StorageType Storage, bool ShouldCreate) { |
| 1122 | assert(isCanonical(Name) && "Expected canonical MDString"); |
| 1123 | assert(isCanonical(GetterName) && "Expected canonical MDString"); |
| 1124 | assert(isCanonical(SetterName) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 1125 | DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName, |
| 1126 | SetterName, Attributes, Type)); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1127 | Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1128 | DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1131 | DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1132 | Metadata *Scope, Metadata *Entity, |
Adrian Prantl | d63bfd2 | 2017-07-19 00:09:54 +0000 | [diff] [blame] | 1133 | Metadata *File, unsigned Line, |
| 1134 | MDString *Name, StorageType Storage, |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1135 | bool ShouldCreate) { |
| 1136 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Adrian Prantl | d63bfd2 | 2017-07-19 00:09:54 +0000 | [diff] [blame] | 1137 | DEFINE_GETIMPL_LOOKUP(DIImportedEntity, |
| 1138 | (Tag, Scope, Entity, File, Line, Name)); |
| 1139 | Metadata *Ops[] = {Scope, Entity, Name, File}; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1140 | DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops); |
Duncan P. N. Exon Smith | 01fc176 | 2015-02-10 00:52:32 +0000 | [diff] [blame] | 1141 | } |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType, |
| 1144 | unsigned Line, MDString *Name, MDString *Value, |
| 1145 | StorageType Storage, bool ShouldCreate) { |
| 1146 | assert(isCanonical(Name) && "Expected canonical MDString"); |
Mehdi Amini | 5d99c4e | 2016-03-19 01:02:34 +0000 | [diff] [blame] | 1147 | DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value)); |
Amjad Aboud | a9bcf16 | 2015-12-10 12:56:35 +0000 | [diff] [blame] | 1148 | Metadata *Ops[] = { Name, Value }; |
| 1149 | DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops); |
| 1150 | } |
| 1151 | |
| 1152 | DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType, |
| 1153 | unsigned Line, Metadata *File, |
| 1154 | Metadata *Elements, StorageType Storage, |
| 1155 | bool ShouldCreate) { |
| 1156 | DEFINE_GETIMPL_LOOKUP(DIMacroFile, |
| 1157 | (MIType, Line, File, Elements)); |
| 1158 | Metadata *Ops[] = { File, Elements }; |
| 1159 | DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops); |
| 1160 | } |