Fixed some bugs.
[llvm/zpu.git] / lib / VMCore / DebugLoc.cpp
blobf8b45eed0d5e90853650dd41f13cabadeafb72a4
1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 using namespace llvm;
14 //===----------------------------------------------------------------------===//
15 // DebugLoc Implementation
16 //===----------------------------------------------------------------------===//
18 MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
19 if (ScopeIdx == 0) return 0;
21 if (ScopeIdx > 0) {
22 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
23 // position specified.
24 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
25 "Invalid ScopeIdx!");
26 return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
29 // Otherwise, the index is in the ScopeInlinedAtRecords array.
30 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
31 "Invalid ScopeIdx");
32 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
35 MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
36 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
37 // position specified. Zero is invalid.
38 if (ScopeIdx >= 0) return 0;
40 // Otherwise, the index is in the ScopeInlinedAtRecords array.
41 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
42 "Invalid ScopeIdx");
43 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
46 /// Return both the Scope and the InlinedAt values.
47 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
48 const LLVMContext &Ctx) const {
49 if (ScopeIdx == 0) {
50 Scope = IA = 0;
51 return;
54 if (ScopeIdx > 0) {
55 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
56 // position specified.
57 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
58 "Invalid ScopeIdx!");
59 Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
60 IA = 0;
61 return;
64 // Otherwise, the index is in the ScopeInlinedAtRecords array.
65 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
66 "Invalid ScopeIdx");
67 Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
68 IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
72 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
73 MDNode *Scope, MDNode *InlinedAt) {
74 DebugLoc Result;
76 // If no scope is available, this is an unknown location.
77 if (Scope == 0) return Result;
79 // Saturate line and col to "unknown".
80 if (Col > 255) Col = 0;
81 if (Line >= (1 << 24)) Line = 0;
82 Result.LineCol = Line | (Col << 24);
84 LLVMContext &Ctx = Scope->getContext();
86 // If there is no inlined-at location, use the ScopeRecords array.
87 if (InlinedAt == 0)
88 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
89 else
90 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
91 InlinedAt, 0);
93 return Result;
96 /// getAsMDNode - This method converts the compressed DebugLoc node into a
97 /// DILocation compatible MDNode.
98 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
99 if (isUnknown()) return 0;
101 MDNode *Scope, *IA;
102 getScopeAndInlinedAt(Scope, IA, Ctx);
103 assert(Scope && "If scope is null, this should be isUnknown()");
105 LLVMContext &Ctx2 = Scope->getContext();
106 const Type *Int32 = Type::getInt32Ty(Ctx2);
107 Value *Elts[] = {
108 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
109 Scope, IA
111 return MDNode::get(Ctx2, &Elts[0], 4);
114 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
115 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
116 if (N == 0 || N->getNumOperands() != 4) return DebugLoc();
118 MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(2));
119 if (Scope == 0) return DebugLoc();
121 unsigned LineNo = 0, ColNo = 0;
122 if (ConstantInt *Line = dyn_cast_or_null<ConstantInt>(N->getOperand(0)))
123 LineNo = Line->getZExtValue();
124 if (ConstantInt *Col = dyn_cast_or_null<ConstantInt>(N->getOperand(1)))
125 ColNo = Col->getZExtValue();
127 return get(LineNo, ColNo, Scope, dyn_cast_or_null<MDNode>(N->getOperand(3)));
130 //===----------------------------------------------------------------------===//
131 // LLVMContextImpl Implementation
132 //===----------------------------------------------------------------------===//
134 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
135 int ExistingIdx) {
136 // If we already have an entry for this scope, return it.
137 int &Idx = ScopeRecordIdx[Scope];
138 if (Idx) return Idx;
140 // If we don't have an entry, but ExistingIdx is specified, use it.
141 if (ExistingIdx)
142 return Idx = ExistingIdx;
144 // Otherwise add a new entry.
146 // Start out ScopeRecords with a minimal reasonable size to avoid
147 // excessive reallocation starting out.
148 if (ScopeRecords.empty())
149 ScopeRecords.reserve(128);
151 // Index is biased by 1 for index.
152 Idx = ScopeRecords.size()+1;
153 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
154 return Idx;
157 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
158 int ExistingIdx) {
159 // If we already have an entry, return it.
160 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
161 if (Idx) return Idx;
163 // If we don't have an entry, but ExistingIdx is specified, use it.
164 if (ExistingIdx)
165 return Idx = ExistingIdx;
167 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
168 // excessive reallocation starting out.
169 if (ScopeInlinedAtRecords.empty())
170 ScopeInlinedAtRecords.reserve(128);
172 // Index is biased by 1 and negated.
173 Idx = -ScopeInlinedAtRecords.size()-1;
174 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
175 DebugRecVH(IA, this, Idx)));
176 return Idx;
180 //===----------------------------------------------------------------------===//
181 // DebugRecVH Implementation
182 //===----------------------------------------------------------------------===//
184 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
185 /// to drop to null and we need remove our entry from the DenseMap.
186 void DebugRecVH::deleted() {
187 // If this is a non-canonical reference, just drop the value to null, we know
188 // it doesn't have a map entry.
189 if (Idx == 0) {
190 setValPtr(0);
191 return;
194 MDNode *Cur = get();
196 // If the index is positive, it is an entry in ScopeRecords.
197 if (Idx > 0) {
198 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
199 Ctx->ScopeRecordIdx.erase(Cur);
200 // Reset this VH to null and we're done.
201 setValPtr(0);
202 Idx = 0;
203 return;
206 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
207 // is the scope or the inlined-at record entry.
208 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
209 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
210 assert((this == &Entry.first || this == &Entry.second) &&
211 "Mapping out of date!");
213 MDNode *OldScope = Entry.first.get();
214 MDNode *OldInlinedAt = Entry.second.get();
215 assert(OldScope != 0 && OldInlinedAt != 0 &&
216 "Entry should be non-canonical if either val dropped to null");
218 // Otherwise, we do have an entry in it, nuke it and we're done.
219 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
220 "Mapping out of date");
221 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
223 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
224 // we're in non-canonical form now.
225 setValPtr(0);
226 Entry.first.Idx = Entry.second.Idx = 0;
229 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
230 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
231 // the mdnode got deleted.
232 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
233 if (NewVal == 0) return deleted();
235 // If this is a non-canonical reference, just change it, we know it already
236 // doesn't have a map entry.
237 if (Idx == 0) {
238 setValPtr(NewVa);
239 return;
242 MDNode *OldVal = get();
243 assert(OldVal != NewVa && "Node replaced with self?");
245 // If the index is positive, it is an entry in ScopeRecords.
246 if (Idx > 0) {
247 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
248 Ctx->ScopeRecordIdx.erase(OldVal);
249 setValPtr(NewVal);
251 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
253 // If NewVal already has an entry, this becomes a non-canonical reference,
254 // just drop Idx to 0 to signify this.
255 if (NewEntry != Idx)
256 Idx = 0;
257 return;
260 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
261 // is the scope or the inlined-at record entry.
262 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
263 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
264 assert((this == &Entry.first || this == &Entry.second) &&
265 "Mapping out of date!");
267 MDNode *OldScope = Entry.first.get();
268 MDNode *OldInlinedAt = Entry.second.get();
269 assert(OldScope != 0 && OldInlinedAt != 0 &&
270 "Entry should be non-canonical if either val dropped to null");
272 // Otherwise, we do have an entry in it, nuke it and we're done.
273 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
274 "Mapping out of date");
275 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
277 // Reset this VH to the new value.
278 setValPtr(NewVal);
280 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
281 Entry.second.get(), Idx);
282 // If NewVal already has an entry, this becomes a non-canonical reference,
283 // just drop Idx to 0 to signify this.
284 if (NewIdx != Idx) {
285 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
286 Entry.first.Idx = Entry.second.Idx = 0;