blob: a1f4f1338b20bf188296335c3452580d5dd4d1b8 [file] [log] [blame]
Jim Laskey6af56812006-01-04 13:36:38 +00001//===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by James M. Laskey and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Jim Laskey6af56812006-01-04 13:36:38 +00009
10#include "llvm/CodeGen/MachineDebugInfo.h"
11
Jim Laskeyb3e789a2006-01-26 20:21:46 +000012#include "llvm/Constants.h"
Jim Laskey41886992006-04-07 16:34:46 +000013#include "llvm/CodeGen/MachineLocation.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000014#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000015#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000016#include "llvm/Intrinsics.h"
17#include "llvm/Instructions.h"
18#include "llvm/Module.h"
19#include "llvm/Support/Dwarf.h"
20
Jim Laskey86cbdba2006-02-06 15:33:21 +000021#include <iostream>
22
Jim Laskey6af56812006-01-04 13:36:38 +000023using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000024using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000025
26// Handle the Pass registration stuff necessary to use TargetData's.
27namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000028 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
29}
Jim Laskey063e7652006-01-17 17:31:53 +000030
Jim Laskeyb3e789a2006-01-26 20:21:46 +000031//===----------------------------------------------------------------------===//
32
Jim Laskey86cbdba2006-02-06 15:33:21 +000033/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000034/// specified value in their initializer somewhere.
35static void
36getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
37 // Scan though value users.
38 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
39 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000040 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000041 Result.push_back(GV);
42 } else if (Constant *C = dyn_cast<Constant>(*I)) {
43 // If the user is a constant variable then scan its users
44 getGlobalVariablesUsing(C, Result);
45 }
46 }
47}
48
Jim Laskey86cbdba2006-02-06 15:33:21 +000049/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
50/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000051static std::vector<GlobalVariable*>
52getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000053 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000054
55 std::vector<const Type*> FieldTypes;
56 FieldTypes.push_back(Type::UIntTy);
Jim Laskeye8c3e3b2006-03-07 20:53:47 +000057 FieldTypes.push_back(Type::UIntTy);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000058
Jim Laskey86cbdba2006-02-06 15:33:21 +000059 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000060 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000061 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
63 // If present and linkonce then scan for users.
64 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
65 getGlobalVariablesUsing(UseRoot, Result);
66 }
67
68 return Result;
69}
70
Jim Laskey86cbdba2006-02-06 15:33:21 +000071/// isStringValue - Return true if the given value can be coerced to a string.
72///
73static bool isStringValue(Value *V) {
74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77 return Init->isString();
78 }
79 } else if (Constant *C = dyn_cast<Constant>(V)) {
80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
81 return isStringValue(GV);
82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 if (CE->getOpcode() == Instruction::GetElementPtr) {
84 if (CE->getNumOperands() == 3 &&
85 cast<Constant>(CE->getOperand(1))->isNullValue() &&
86 isa<ConstantInt>(CE->getOperand(2))) {
87 return isStringValue(CE->getOperand(0));
88 }
89 }
90 }
91 }
92 return false;
93}
94
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000095/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000096///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
99 return GV;
100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000101 if (CE->getOpcode() == Instruction::Cast) {
102 return dyn_cast<GlobalVariable>(CE->getOperand(0));
103 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000104 }
105 return NULL;
106}
107
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000108/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000109/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000110static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000111 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
112 return true;
113 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
114 if (CE->getOpcode() == Instruction::Cast) {
115 return isa<GlobalVariable>(CE->getOperand(0));
116 }
117 }
118 return false;
119}
120
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000121/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000122///
Reid Spencerb83eb642006-10-20 07:07:24 +0000123static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000124 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000125 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000126
Jim Laskey86cbdba2006-02-06 15:33:21 +0000127 // Get the initializer constant.
128 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000129 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000130
Jim Laskey86cbdba2006-02-06 15:33:21 +0000131 // Check if there is at least i + 1 operands.
132 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000133 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000134
Jim Laskey86cbdba2006-02-06 15:33:21 +0000135 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000136 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000137}
Reid Spencerb83eb642006-10-20 07:07:24 +0000138
Jim Laskey86cbdba2006-02-06 15:33:21 +0000139//===----------------------------------------------------------------------===//
140
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000141/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000142/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000143void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000144 DD->ApplyToFields(this);
145}
146
147//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000148/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
149/// the supplied DebugInfoDesc.
150class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000151private:
152 unsigned Count; // Running count of fields.
153
154public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000155 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000156
157 // Accessors.
158 unsigned getCount() const { return Count; }
159
160 /// Apply - Count each of the fields.
161 ///
162 virtual void Apply(int &Field) { ++Count; }
163 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000164 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000165 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000166 virtual void Apply(bool &Field) { ++Count; }
167 virtual void Apply(std::string &Field) { ++Count; }
168 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
169 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000170 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
171 ++Count;
172 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000173};
174
175//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000176/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
177/// supplied DebugInfoDesc.
178class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000179private:
180 DIDeserializer &DR; // Active deserializer.
181 unsigned I; // Current operand index.
182 ConstantStruct *CI; // GlobalVariable constant initializer.
183
184public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000185 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
186 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000187 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000188 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000189 , CI(cast<ConstantStruct>(GV->getInitializer()))
190 {}
191
192 /// Apply - Set the value of each of the fields.
193 ///
194 virtual void Apply(int &Field) {
195 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000196 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000197 }
198 virtual void Apply(unsigned &Field) {
199 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000200 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000201 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000202 virtual void Apply(int64_t &Field) {
203 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000204 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000205 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000206 virtual void Apply(uint64_t &Field) {
207 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000208 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000209 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000210 virtual void Apply(bool &Field) {
211 Constant *C = CI->getOperand(I++);
212 Field = cast<ConstantBool>(C)->getValue();
213 }
214 virtual void Apply(std::string &Field) {
215 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000216 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000217 }
218 virtual void Apply(DebugInfoDesc *&Field) {
219 Constant *C = CI->getOperand(I++);
220 Field = DR.Deserialize(C);
221 }
222 virtual void Apply(GlobalVariable *&Field) {
223 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000224 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000225 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000226 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000227 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000228 Constant *C = CI->getOperand(I++);
229 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000230 if (GV->hasInitializer()) {
231 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
232 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
233 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
234 DebugInfoDesc *DE = DR.Deserialize(GVE);
235 Field.push_back(DE);
236 }
237 } else if (GV->getInitializer()->isNullValue()) {
238 if (const ArrayType *T =
239 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
240 Field.resize(T->getNumElements());
241 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000242 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000243 }
244 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000245};
246
247//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000248/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000249/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000250class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000251private:
252 DISerializer &SR; // Active serializer.
253 std::vector<Constant*> &Elements; // Element accumulator.
254
255public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000256 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
257 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000258 , SR(S)
259 , Elements(E)
260 {}
261
262 /// Apply - Set the value of each of the fields.
263 ///
264 virtual void Apply(int &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000265 Elements.push_back(ConstantInt::get(Type::IntTy, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000266 }
267 virtual void Apply(unsigned &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000268 Elements.push_back(ConstantInt::get(Type::UIntTy, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000269 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000270 virtual void Apply(int64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000271 Elements.push_back(ConstantInt::get(Type::LongTy, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000272 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000273 virtual void Apply(uint64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000274 Elements.push_back(ConstantInt::get(Type::ULongTy, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000275 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000276 virtual void Apply(bool &Field) {
277 Elements.push_back(ConstantBool::get(Field));
278 }
279 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000280 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000281 }
282 virtual void Apply(DebugInfoDesc *&Field) {
283 GlobalVariable *GV = NULL;
284
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000285 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000286 if (Field) GV = SR.Serialize(Field);
287
288 // FIXME - At some point should use specific type.
289 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
290
291 if (GV) {
292 // Set to pointer to global.
293 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
294 } else {
295 // Use NULL.
296 Elements.push_back(ConstantPointerNull::get(EmptyTy));
297 }
298 }
299 virtual void Apply(GlobalVariable *&Field) {
300 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000301 if (Field) {
302 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
303 } else {
304 Elements.push_back(ConstantPointerNull::get(EmptyTy));
305 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000306 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000307 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
308 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
309 unsigned N = Field.size();
310 ArrayType *AT = ArrayType::get(EmptyTy, N);
311 std::vector<Constant *> ArrayElements;
312
313 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000314 if (DebugInfoDesc *Element = Field[i]) {
315 GlobalVariable *GVE = SR.Serialize(Element);
316 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
317 ArrayElements.push_back(cast<Constant>(CE));
318 } else {
319 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
320 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000321 }
322
323 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000324 GlobalVariable *CAGV = new GlobalVariable(AT, true,
325 GlobalValue::InternalLinkage,
326 CA, "llvm.dbg.array",
327 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000328 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000329 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000330 Elements.push_back(CAE);
331 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000332};
333
334//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000335/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000336/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000337class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000338private:
339 DISerializer &SR; // Active serializer.
340 std::vector<const Type*> &Fields; // Type accumulator.
341
342public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000343 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
344 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000345 , SR(S)
346 , Fields(F)
347 {}
348
349 /// Apply - Set the value of each of the fields.
350 ///
351 virtual void Apply(int &Field) {
352 Fields.push_back(Type::IntTy);
353 }
354 virtual void Apply(unsigned &Field) {
355 Fields.push_back(Type::UIntTy);
356 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000357 virtual void Apply(int64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000358 Fields.push_back(Type::LongTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000359 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000360 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000361 Fields.push_back(Type::ULongTy);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000362 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000363 virtual void Apply(bool &Field) {
364 Fields.push_back(Type::BoolTy);
365 }
366 virtual void Apply(std::string &Field) {
367 Fields.push_back(SR.getStrPtrType());
368 }
369 virtual void Apply(DebugInfoDesc *&Field) {
370 // FIXME - At some point should use specific type.
371 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
372 Fields.push_back(EmptyTy);
373 }
374 virtual void Apply(GlobalVariable *&Field) {
375 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
376 Fields.push_back(EmptyTy);
377 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000378 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
379 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
380 Fields.push_back(EmptyTy);
381 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000382};
383
384//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000385/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000386/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000387class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000388private:
389 DIVerifier &VR; // Active verifier.
390 bool IsValid; // Validity status.
391 unsigned I; // Current operand index.
392 ConstantStruct *CI; // GlobalVariable constant initializer.
393
394public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000395 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
396 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000397 , VR(V)
398 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000399 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000400 , CI(cast<ConstantStruct>(GV->getInitializer()))
401 {
402 }
403
404 // Accessors.
405 bool isValid() const { return IsValid; }
406
407 /// Apply - Set the value of each of the fields.
408 ///
409 virtual void Apply(int &Field) {
410 Constant *C = CI->getOperand(I++);
411 IsValid = IsValid && isa<ConstantInt>(C);
412 }
413 virtual void Apply(unsigned &Field) {
414 Constant *C = CI->getOperand(I++);
415 IsValid = IsValid && isa<ConstantInt>(C);
416 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000417 virtual void Apply(int64_t &Field) {
418 Constant *C = CI->getOperand(I++);
419 IsValid = IsValid && isa<ConstantInt>(C);
420 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000421 virtual void Apply(uint64_t &Field) {
422 Constant *C = CI->getOperand(I++);
423 IsValid = IsValid && isa<ConstantInt>(C);
424 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000425 virtual void Apply(bool &Field) {
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid && isa<ConstantBool>(C);
428 }
429 virtual void Apply(std::string &Field) {
430 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000431 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000432 }
433 virtual void Apply(DebugInfoDesc *&Field) {
434 // FIXME - Prepare the correct descriptor.
435 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000436 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000437 }
438 virtual void Apply(GlobalVariable *&Field) {
439 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000440 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000441 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000442 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
443 Constant *C = CI->getOperand(I++);
444 IsValid = IsValid && isGlobalVariable(C);
445 if (!IsValid) return;
446
447 GlobalVariable *GV = getGlobalVariable(C);
448 IsValid = IsValid && GV && GV->hasInitializer();
449 if (!IsValid) return;
450
451 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
452 IsValid = IsValid && CA;
453 if (!IsValid) return;
454
455 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
456 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
457 if (!IsValid) return;
458
459 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
460 VR.Verify(GVE);
461 }
462 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000463};
464
Jim Laskeyce72b172006-02-11 01:01:30 +0000465
Jim Laskey86cbdba2006-02-06 15:33:21 +0000466//===----------------------------------------------------------------------===//
467
Jim Laskeyed4e5662006-06-14 14:45:39 +0000468/// TagFromGlobal - Returns the tag number from a debug info descriptor
469/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000470unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000471 ConstantInt *C = getUIntOperand(GV, 0);
472 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000473 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000474}
475
476/// VersionFromGlobal - Returns the version number from a debug info
477/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
478/// int.
479unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000480 ConstantInt *C = getUIntOperand(GV, 0);
481 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000482 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000483}
484
485/// DescFactory - Create an instance of debug info descriptor based on Tag.
486/// Return NULL if not a recognized Tag.
487DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
488 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000489 case DW_TAG_anchor: return new AnchorDesc();
490 case DW_TAG_compile_unit: return new CompileUnitDesc();
491 case DW_TAG_variable: return new GlobalVariableDesc();
492 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000493 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000494 case DW_TAG_base_type: return new BasicTypeDesc();
495 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000496 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000497 case DW_TAG_reference_type:
498 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000499 case DW_TAG_volatile_type:
500 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000501 case DW_TAG_member:
502 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000503 case DW_TAG_array_type:
504 case DW_TAG_structure_type:
505 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000506 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000507 case DW_TAG_vector_type:
508 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000509 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000510 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000511 case DW_TAG_return_variable:
512 case DW_TAG_arg_variable:
513 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000514 default: break;
515 }
516 return NULL;
517}
518
519/// getLinkage - get linkage appropriate for this type of descriptor.
520///
521GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
522 return GlobalValue::InternalLinkage;
523}
524
525/// ApplyToFields - Target the vistor to the fields of the descriptor.
526///
527void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
528 Visitor->Apply(Tag);
529}
530
531//===----------------------------------------------------------------------===//
532
Jim Laskey9c4447a2006-03-01 20:39:36 +0000533AnchorDesc::AnchorDesc()
534: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000535, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000536{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000537AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000538: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000539, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000540{}
541
542// Implement isa/cast/dyncast.
543bool AnchorDesc::classof(const DebugInfoDesc *D) {
544 return D->getTag() == DW_TAG_anchor;
545}
546
Jim Laskeyce72b172006-02-11 01:01:30 +0000547/// getLinkage - get linkage appropriate for this type of descriptor.
548///
549GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
550 return GlobalValue::LinkOnceLinkage;
551}
552
553/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
554///
555void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
556 DebugInfoDesc::ApplyToFields(Visitor);
557
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000558 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000559}
560
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000561/// getDescString - Return a string used to compose global names and labels. A
562/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000563/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000564/// to the list of names left external in the internalizer.
565/// ExternalNames.insert("llvm.dbg.compile_units");
566/// ExternalNames.insert("llvm.dbg.global_variables");
567/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000568const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000569 switch (AnchorTag) {
570 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
571 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
572 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
573 default: break;
574 }
575
576 assert(0 && "Tag does not have a case for anchor string");
577 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000578}
579
580/// getTypeString - Return a string used to label this descriptors type.
581///
582const char *AnchorDesc::getTypeString() const {
583 return "llvm.dbg.anchor.type";
584}
585
586#ifndef NDEBUG
587void AnchorDesc::dump() {
588 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000589 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000590 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000591 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000592}
593#endif
594
595//===----------------------------------------------------------------------===//
596
597AnchoredDesc::AnchoredDesc(unsigned T)
598: DebugInfoDesc(T)
599, Anchor(NULL)
600{}
601
602/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
603///
604void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
605 DebugInfoDesc::ApplyToFields(Visitor);
606
Jim Laskey7089f452006-06-16 13:14:03 +0000607 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000608}
609
610//===----------------------------------------------------------------------===//
611
612CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000613: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000614, Language(0)
615, FileName("")
616, Directory("")
617, Producer("")
618{}
619
Jim Laskey9c4447a2006-03-01 20:39:36 +0000620// Implement isa/cast/dyncast.
621bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
622 return D->getTag() == DW_TAG_compile_unit;
623}
624
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000625/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
626///
627void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000628 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000629
630 // Handle cases out of sync with compiler.
631 if (getVersion() == 0) {
632 unsigned DebugVersion;
633 Visitor->Apply(DebugVersion);
634 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000635
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000636 Visitor->Apply(Language);
637 Visitor->Apply(FileName);
638 Visitor->Apply(Directory);
639 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000640}
641
Jim Laskeyce72b172006-02-11 01:01:30 +0000642/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000643///
Jim Laskeyce72b172006-02-11 01:01:30 +0000644const char *CompileUnitDesc::getDescString() const {
645 return "llvm.dbg.compile_unit";
646}
647
648/// getTypeString - Return a string used to label this descriptors type.
649///
650const char *CompileUnitDesc::getTypeString() const {
651 return "llvm.dbg.compile_unit.type";
652}
653
654/// getAnchorString - Return a string used to label this descriptor's anchor.
655///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000656const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000657const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000658 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000659}
660
Jim Laskey86cbdba2006-02-06 15:33:21 +0000661#ifndef NDEBUG
662void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000663 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000664 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000665 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000666 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000667 << "Language(" << Language << "), "
668 << "FileName(\"" << FileName << "\"), "
669 << "Directory(\"" << Directory << "\"), "
670 << "Producer(\"" << Producer << "\")\n";
671}
672#endif
673
674//===----------------------------------------------------------------------===//
675
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000676TypeDesc::TypeDesc(unsigned T)
677: DebugInfoDesc(T)
678, Context(NULL)
679, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000680, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000681, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000682, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000683, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000684, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000685, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000686{}
687
Jim Laskey69906002006-02-24 16:46:40 +0000688/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000689///
690void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
691 DebugInfoDesc::ApplyToFields(Visitor);
692
693 Visitor->Apply(Context);
694 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000695 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000696 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000697 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000698 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000699 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000700 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701}
702
703/// getDescString - Return a string used to compose global names and labels.
704///
705const char *TypeDesc::getDescString() const {
706 return "llvm.dbg.type";
707}
708
709/// getTypeString - Return a string used to label this descriptor's type.
710///
711const char *TypeDesc::getTypeString() const {
712 return "llvm.dbg.type.type";
713}
714
715#ifndef NDEBUG
716void TypeDesc::dump() {
717 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000718 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000719 << "Tag(" << getTag() << "), "
720 << "Context(" << Context << "), "
721 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000722 << "File(" << File << "), "
723 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000724 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000725 << "Align(" << Align << "), "
Jim Laskeye2a78f22006-07-11 15:58:09 +0000726 << "Offset(" << Offset << "), "
727 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000728}
729#endif
730
731//===----------------------------------------------------------------------===//
732
733BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000734: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000735, Encoding(0)
736{}
737
Jim Laskey9c4447a2006-03-01 20:39:36 +0000738// Implement isa/cast/dyncast.
739bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
740 return D->getTag() == DW_TAG_base_type;
741}
742
Jim Laskey69906002006-02-24 16:46:40 +0000743/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000744///
745void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
746 TypeDesc::ApplyToFields(Visitor);
747
748 Visitor->Apply(Encoding);
749}
750
Jim Laskeyf8913f12006-03-01 17:53:02 +0000751/// getDescString - Return a string used to compose global names and labels.
752///
753const char *BasicTypeDesc::getDescString() const {
754 return "llvm.dbg.basictype";
755}
756
757/// getTypeString - Return a string used to label this descriptor's type.
758///
759const char *BasicTypeDesc::getTypeString() const {
760 return "llvm.dbg.basictype.type";
761}
762
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000763#ifndef NDEBUG
764void BasicTypeDesc::dump() {
765 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000766 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000767 << "Tag(" << getTag() << "), "
768 << "Context(" << getContext() << "), "
769 << "Name(\"" << getName() << "\"), "
770 << "Size(" << getSize() << "), "
771 << "Encoding(" << Encoding << ")\n";
772}
773#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000774
Jim Laskey434b40b2006-02-23 22:37:30 +0000775//===----------------------------------------------------------------------===//
776
Jim Laskey69906002006-02-24 16:46:40 +0000777DerivedTypeDesc::DerivedTypeDesc(unsigned T)
778: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000779, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000780{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000781
Jim Laskey9c4447a2006-03-01 20:39:36 +0000782// Implement isa/cast/dyncast.
783bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
784 unsigned T = D->getTag();
785 switch (T) {
786 case DW_TAG_typedef:
787 case DW_TAG_pointer_type:
788 case DW_TAG_reference_type:
789 case DW_TAG_const_type:
790 case DW_TAG_volatile_type:
791 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000792 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000793 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000794 return true;
795 default: break;
796 }
797 return false;
798}
799
Jim Laskey69906002006-02-24 16:46:40 +0000800/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000801///
Jim Laskey69906002006-02-24 16:46:40 +0000802void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000803 TypeDesc::ApplyToFields(Visitor);
804
Jim Laskey7089f452006-06-16 13:14:03 +0000805 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000806}
807
Jim Laskeyf8913f12006-03-01 17:53:02 +0000808/// getDescString - Return a string used to compose global names and labels.
809///
810const char *DerivedTypeDesc::getDescString() const {
811 return "llvm.dbg.derivedtype";
812}
813
814/// getTypeString - Return a string used to label this descriptor's type.
815///
816const char *DerivedTypeDesc::getTypeString() const {
817 return "llvm.dbg.derivedtype.type";
818}
819
Jim Laskey434b40b2006-02-23 22:37:30 +0000820#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000821void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000822 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000823 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000824 << "Tag(" << getTag() << "), "
825 << "Context(" << getContext() << "), "
826 << "Name(\"" << getName() << "\"), "
827 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000828 << "File(" << getFile() << "), "
829 << "Line(" << getLine() << "), "
830 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000831}
832#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000833
834//===----------------------------------------------------------------------===//
835
Jim Laskeyf8913f12006-03-01 17:53:02 +0000836CompositeTypeDesc::CompositeTypeDesc(unsigned T)
837: DerivedTypeDesc(T)
838, Elements()
839{}
840
Jim Laskey9c4447a2006-03-01 20:39:36 +0000841// Implement isa/cast/dyncast.
842bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
843 unsigned T = D->getTag();
844 switch (T) {
845 case DW_TAG_array_type:
846 case DW_TAG_structure_type:
847 case DW_TAG_union_type:
848 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000849 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000850 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000851 return true;
852 default: break;
853 }
854 return false;
855}
856
Jim Laskeyf8913f12006-03-01 17:53:02 +0000857/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
858///
859void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000860 DerivedTypeDesc::ApplyToFields(Visitor);
861
Jim Laskeyf8913f12006-03-01 17:53:02 +0000862 Visitor->Apply(Elements);
863}
864
865/// getDescString - Return a string used to compose global names and labels.
866///
867const char *CompositeTypeDesc::getDescString() const {
868 return "llvm.dbg.compositetype";
869}
870
871/// getTypeString - Return a string used to label this descriptor's type.
872///
873const char *CompositeTypeDesc::getTypeString() const {
874 return "llvm.dbg.compositetype.type";
875}
876
877#ifndef NDEBUG
878void CompositeTypeDesc::dump() {
879 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000880 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000881 << "Tag(" << getTag() << "), "
882 << "Context(" << getContext() << "), "
883 << "Name(\"" << getName() << "\"), "
884 << "Size(" << getSize() << "), "
885 << "File(" << getFile() << "), "
886 << "Line(" << getLine() << "), "
887 << "FromType(" << getFromType() << "), "
888 << "Elements.size(" << Elements.size() << ")\n";
889}
890#endif
891
892//===----------------------------------------------------------------------===//
893
894SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000895: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000896, Lo(0)
897, Hi(0)
898{}
899
Jim Laskey9c4447a2006-03-01 20:39:36 +0000900// Implement isa/cast/dyncast.
901bool SubrangeDesc::classof(const DebugInfoDesc *D) {
902 return D->getTag() == DW_TAG_subrange_type;
903}
904
Jim Laskeyf8913f12006-03-01 17:53:02 +0000905/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
906///
907void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
908 DebugInfoDesc::ApplyToFields(Visitor);
909
910 Visitor->Apply(Lo);
911 Visitor->Apply(Hi);
912}
913
914/// getDescString - Return a string used to compose global names and labels.
915///
916const char *SubrangeDesc::getDescString() const {
917 return "llvm.dbg.subrange";
918}
919
920/// getTypeString - Return a string used to label this descriptor's type.
921///
922const char *SubrangeDesc::getTypeString() const {
923 return "llvm.dbg.subrange.type";
924}
925
926#ifndef NDEBUG
927void SubrangeDesc::dump() {
928 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000929 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000930 << "Tag(" << getTag() << "), "
931 << "Lo(" << Lo << "), "
932 << "Hi(" << Hi << ")\n";
933}
934#endif
935
936//===----------------------------------------------------------------------===//
937
Jim Laskey6a3eb012006-03-01 23:52:37 +0000938EnumeratorDesc::EnumeratorDesc()
939: DebugInfoDesc(DW_TAG_enumerator)
940, Name("")
941, Value(0)
942{}
943
944// Implement isa/cast/dyncast.
945bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
946 return D->getTag() == DW_TAG_enumerator;
947}
948
949/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
950///
951void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
952 DebugInfoDesc::ApplyToFields(Visitor);
953
954 Visitor->Apply(Name);
955 Visitor->Apply(Value);
956}
957
958/// getDescString - Return a string used to compose global names and labels.
959///
960const char *EnumeratorDesc::getDescString() const {
961 return "llvm.dbg.enumerator";
962}
963
964/// getTypeString - Return a string used to label this descriptor's type.
965///
966const char *EnumeratorDesc::getTypeString() const {
967 return "llvm.dbg.enumerator.type";
968}
969
970#ifndef NDEBUG
971void EnumeratorDesc::dump() {
972 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000973 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000974 << "Tag(" << getTag() << "), "
975 << "Name(" << Name << "), "
976 << "Value(" << Value << ")\n";
977}
978#endif
979
980//===----------------------------------------------------------------------===//
981
Jim Laskeyb8509c52006-03-23 18:07:55 +0000982VariableDesc::VariableDesc(unsigned T)
983: DebugInfoDesc(T)
984, Context(NULL)
985, Name("")
986, File(NULL)
987, Line(0)
988, TyDesc(0)
989{}
990
991// Implement isa/cast/dyncast.
992bool VariableDesc::classof(const DebugInfoDesc *D) {
993 unsigned T = D->getTag();
994 switch (T) {
995 case DW_TAG_auto_variable:
996 case DW_TAG_arg_variable:
997 case DW_TAG_return_variable:
998 return true;
999 default: break;
1000 }
1001 return false;
1002}
1003
1004/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1005///
1006void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1007 DebugInfoDesc::ApplyToFields(Visitor);
1008
1009 Visitor->Apply(Context);
1010 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001011 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001012 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001013 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001014}
1015
1016/// getDescString - Return a string used to compose global names and labels.
1017///
1018const char *VariableDesc::getDescString() const {
1019 return "llvm.dbg.variable";
1020}
1021
1022/// getTypeString - Return a string used to label this descriptor's type.
1023///
1024const char *VariableDesc::getTypeString() const {
1025 return "llvm.dbg.variable.type";
1026}
1027
1028#ifndef NDEBUG
1029void VariableDesc::dump() {
1030 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001031 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001032 << "Tag(" << getTag() << "), "
1033 << "Context(" << Context << "), "
1034 << "Name(\"" << Name << "\"), "
1035 << "File(" << File << "), "
1036 << "Line(" << Line << "), "
1037 << "TyDesc(" << TyDesc << ")\n";
1038}
1039#endif
1040
1041//===----------------------------------------------------------------------===//
1042
Jim Laskeyce72b172006-02-11 01:01:30 +00001043GlobalDesc::GlobalDesc(unsigned T)
1044: AnchoredDesc(T)
1045, Context(0)
1046, Name("")
Jim Laskeye2a78f22006-07-11 15:58:09 +00001047, DisplayName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001048, File(NULL)
1049, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001050, TyDesc(NULL)
1051, IsStatic(false)
1052, IsDefinition(false)
1053{}
1054
1055/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001056///
Jim Laskeyce72b172006-02-11 01:01:30 +00001057void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1058 AnchoredDesc::ApplyToFields(Visitor);
1059
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001060 Visitor->Apply(Context);
1061 Visitor->Apply(Name);
Jim Laskeye2a78f22006-07-11 15:58:09 +00001062 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
Jim Laskey7089f452006-06-16 13:14:03 +00001063 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001064 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001065 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001066 Visitor->Apply(IsStatic);
1067 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001068}
1069
1070//===----------------------------------------------------------------------===//
1071
1072GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001073: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001074, Global(NULL)
1075{}
1076
Jim Laskey9c4447a2006-03-01 20:39:36 +00001077// Implement isa/cast/dyncast.
1078bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1079 return D->getTag() == DW_TAG_variable;
1080}
1081
Jim Laskeyce72b172006-02-11 01:01:30 +00001082/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1083///
1084void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1085 GlobalDesc::ApplyToFields(Visitor);
1086
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001087 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001088}
1089
Jim Laskeyce72b172006-02-11 01:01:30 +00001090/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001091///
Jim Laskeyce72b172006-02-11 01:01:30 +00001092const char *GlobalVariableDesc::getDescString() const {
1093 return "llvm.dbg.global_variable";
1094}
1095
1096/// getTypeString - Return a string used to label this descriptors type.
1097///
1098const char *GlobalVariableDesc::getTypeString() const {
1099 return "llvm.dbg.global_variable.type";
1100}
1101
1102/// getAnchorString - Return a string used to label this descriptor's anchor.
1103///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001104const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001105const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001106 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001107}
1108
Jim Laskey86cbdba2006-02-06 15:33:21 +00001109#ifndef NDEBUG
1110void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001111 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001112 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001113 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001114 << "Anchor(" << getAnchor() << "), "
1115 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001116 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001117 << "File(" << getFile() << "),"
1118 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001119 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001120 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1121 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001122 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001123}
1124#endif
1125
1126//===----------------------------------------------------------------------===//
1127
Jim Laskeyce72b172006-02-11 01:01:30 +00001128SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001129: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001130{}
1131
Jim Laskey9c4447a2006-03-01 20:39:36 +00001132// Implement isa/cast/dyncast.
1133bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1134 return D->getTag() == DW_TAG_subprogram;
1135}
1136
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001137/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001138/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001139void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001140 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001141}
1142
Jim Laskeyce72b172006-02-11 01:01:30 +00001143/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001144///
Jim Laskeyce72b172006-02-11 01:01:30 +00001145const char *SubprogramDesc::getDescString() const {
1146 return "llvm.dbg.subprogram";
1147}
1148
1149/// getTypeString - Return a string used to label this descriptors type.
1150///
1151const char *SubprogramDesc::getTypeString() const {
1152 return "llvm.dbg.subprogram.type";
1153}
1154
1155/// getAnchorString - Return a string used to label this descriptor's anchor.
1156///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001157const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001158const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001159 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001160}
1161
Jim Laskey86cbdba2006-02-06 15:33:21 +00001162#ifndef NDEBUG
1163void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001164 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001165 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001166 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001167 << "Anchor(" << getAnchor() << "), "
1168 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001169 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001170 << "File(" << getFile() << "),"
1171 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001172 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001173 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1174 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001175}
1176#endif
1177
Jim Laskey45ccae52006-02-28 20:15:07 +00001178//===----------------------------------------------------------------------===//
1179
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001180BlockDesc::BlockDesc()
1181: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001182, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001183{}
1184
1185// Implement isa/cast/dyncast.
1186bool BlockDesc::classof(const DebugInfoDesc *D) {
1187 return D->getTag() == DW_TAG_lexical_block;
1188}
1189
1190/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1191///
1192void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1193 DebugInfoDesc::ApplyToFields(Visitor);
1194
Jim Laskeyb8509c52006-03-23 18:07:55 +00001195 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001196}
1197
1198/// getDescString - Return a string used to compose global names and labels.
1199///
1200const char *BlockDesc::getDescString() const {
1201 return "llvm.dbg.block";
1202}
1203
1204/// getTypeString - Return a string used to label this descriptors type.
1205///
1206const char *BlockDesc::getTypeString() const {
1207 return "llvm.dbg.block.type";
1208}
1209
1210#ifndef NDEBUG
1211void BlockDesc::dump() {
1212 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001213 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001214 << "Tag(" << getTag() << "),"
1215 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001216}
1217#endif
1218
1219//===----------------------------------------------------------------------===//
1220
Jim Laskey86cbdba2006-02-06 15:33:21 +00001221DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001222 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001223}
1224DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001225 // Handle NULL.
1226 if (!GV) return NULL;
1227
Jim Laskey86cbdba2006-02-06 15:33:21 +00001228 // Check to see if it has been already deserialized.
1229 DebugInfoDesc *&Slot = GlobalDescs[GV];
1230 if (Slot) return Slot;
1231
1232 // Get the Tag from the global.
1233 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1234
Jim Laskey86cbdba2006-02-06 15:33:21 +00001235 // Create an empty instance of the correct sort.
1236 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001237
Jim Laskey21407982006-03-14 18:37:57 +00001238 // If not a user defined descriptor.
1239 if (Slot) {
1240 // Deserialize the fields.
1241 DIDeserializeVisitor DRAM(*this, GV);
1242 DRAM.ApplyToFields(Slot);
1243 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001244
1245 return Slot;
1246}
1247
1248//===----------------------------------------------------------------------===//
1249
1250/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001251///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001252const PointerType *DISerializer::getStrPtrType() {
1253 // If not already defined.
1254 if (!StrPtrTy) {
1255 // Construct the pointer to signed bytes.
1256 StrPtrTy = PointerType::get(Type::SByteTy);
1257 }
1258
1259 return StrPtrTy;
1260}
1261
1262/// getEmptyStructPtrType - Return a "{ }*" type.
1263///
1264const PointerType *DISerializer::getEmptyStructPtrType() {
1265 // If not already defined.
1266 if (!EmptyStructPtrTy) {
1267 // Construct the empty structure type.
1268 const StructType *EmptyStructTy =
1269 StructType::get(std::vector<const Type*>());
1270 // Construct the pointer to empty structure type.
1271 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1272 }
1273
1274 return EmptyStructPtrTy;
1275}
1276
1277/// getTagType - Return the type describing the specified descriptor (via tag.)
1278///
1279const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1280 // Attempt to get the previously defined type.
1281 StructType *&Ty = TagTypes[DD->getTag()];
1282
1283 // If not already defined.
1284 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001285 // Set up fields vector.
1286 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001287 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001288 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001289 GTAM.ApplyToFields(DD);
1290
1291 // Construct structured type.
1292 Ty = StructType::get(Fields);
1293
Jim Laskey86cbdba2006-02-06 15:33:21 +00001294 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001295 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001296 }
1297
1298 return Ty;
1299}
1300
1301/// getString - Construct the string as constant string global.
1302///
Jim Laskeyce72b172006-02-11 01:01:30 +00001303Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001304 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001305 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001306 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001307 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001308 // If empty string then use a sbyte* null instead.
1309 if (String.empty()) {
1310 Slot = ConstantPointerNull::get(getStrPtrType());
1311 } else {
1312 // Construct string as an llvm constant.
1313 Constant *ConstStr = ConstantArray::get(String);
1314 // Otherwise create and return a new string global.
1315 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1316 GlobalVariable::InternalLinkage,
1317 ConstStr, "str", M);
1318 StrGV->setSection("llvm.metadata");
1319 // Convert to generic string pointer.
1320 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1321 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001322 return Slot;
1323
Jim Laskey86cbdba2006-02-06 15:33:21 +00001324}
1325
1326/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1327/// so that it can be serialized to a .bc or .ll file.
1328GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1329 // Check if the DebugInfoDesc is already in the map.
1330 GlobalVariable *&Slot = DescGlobals[DD];
1331
1332 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1333 if (Slot) return Slot;
1334
Jim Laskey86cbdba2006-02-06 15:33:21 +00001335 // Get the type associated with the Tag.
1336 const StructType *Ty = getTagType(DD);
1337
1338 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001339 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1340 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001341 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001342
1343 // Insert new GlobalVariable in DescGlobals map.
1344 Slot = GV;
1345
1346 // Set up elements vector
1347 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001348 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001349 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001350 SRAM.ApplyToFields(DD);
1351
1352 // Set the globals initializer.
1353 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1354
1355 return GV;
1356}
1357
1358//===----------------------------------------------------------------------===//
1359
Jim Laskey86cbdba2006-02-06 15:33:21 +00001360/// Verify - Return true if the GlobalVariable appears to be a valid
1361/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001362bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001363 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001364}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001365bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001366 // NULLs are valid.
1367 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001368
Jim Laskey98e04102006-03-26 22:45:20 +00001369 // Check prior validity.
1370 unsigned &ValiditySlot = Validity[GV];
1371
1372 // If visited before then use old state.
1373 if (ValiditySlot) return ValiditySlot == Valid;
1374
1375 // Assume validity for the time being (recursion.)
1376 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001377
1378 // Make sure the global is internal or link once (anchor.)
1379 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1380 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1381 ValiditySlot = Invalid;
1382 return false;
1383 }
Jim Laskey98e04102006-03-26 22:45:20 +00001384
Jim Laskey86cbdba2006-02-06 15:33:21 +00001385 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001386 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001387
1388 // Check for user defined descriptors.
1389 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001390
Jim Laskey86cbdba2006-02-06 15:33:21 +00001391 // Construct an empty DebugInfoDesc.
1392 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001393
1394 // Allow for user defined descriptors.
1395 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001396
1397 // Get the initializer constant.
1398 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1399
1400 // Get the operand count.
1401 unsigned N = CI->getNumOperands();
1402
1403 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001404 unsigned &CountSlot = Counts[Tag];
1405 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001406 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001407 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001408 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001409 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001410 }
1411
Jim Laskey21407982006-03-14 18:37:57 +00001412 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001413 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001414 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001415 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001416 return false;
1417 }
1418
1419 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001420 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001421 VRAM.ApplyToFields(DD);
1422
1423 // Release empty DebugInfoDesc.
1424 delete DD;
1425
Jim Laskey98e04102006-03-26 22:45:20 +00001426 // If fields are not valid.
1427 if (!VRAM.isValid()) {
1428 ValiditySlot = Invalid;
1429 return false;
1430 }
1431
1432 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001433}
1434
1435//===----------------------------------------------------------------------===//
1436
Jim Laskeyb8509c52006-03-23 18:07:55 +00001437DebugScope::~DebugScope() {
1438 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1439 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1440}
1441
1442//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001443
1444MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001445: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001446, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001447, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001448, Directories()
1449, SourceFiles()
1450, Lines()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001451, LabelID(0)
1452, ScopeMap()
1453, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001454, FrameMoves()
1455{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001456MachineDebugInfo::~MachineDebugInfo() {
1457
1458}
1459
Jim Laskeyb2efb852006-01-04 22:28:25 +00001460/// doInitialization - Initialize the debug state for a new module.
1461///
1462bool MachineDebugInfo::doInitialization() {
1463 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001464}
1465
Jim Laskeyb2efb852006-01-04 22:28:25 +00001466/// doFinalization - Tear down the debug state after completion of a module.
1467///
1468bool MachineDebugInfo::doFinalization() {
1469 return false;
1470}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001471
Jim Laskey41886992006-04-07 16:34:46 +00001472/// BeginFunction - Begin gathering function debug information.
1473///
1474void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1475 // Coming soon.
1476}
1477
1478/// MachineDebugInfo::EndFunction - Discard function debug information.
1479///
1480void MachineDebugInfo::EndFunction() {
1481 // Clean up scope information.
1482 if (RootScope) {
1483 delete RootScope;
1484 ScopeMap.clear();
1485 RootScope = NULL;
1486 }
1487
1488 // Clean up frame info.
1489 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1490 FrameMoves.clear();
1491}
1492
Jim Laskeyd96185a2006-02-13 12:50:39 +00001493/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001494///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001495// FIXME - use new Value type when available.
1496DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001497 return DR.Deserialize(V);
1498}
1499
1500/// Verify - Verify that a Value is debug information descriptor.
1501///
1502bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001503 return VR.Verify(V);
1504}
1505
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001506/// AnalyzeModule - Scan the module for global debug information.
1507///
1508void MachineDebugInfo::AnalyzeModule(Module &M) {
1509 SetupCompileUnits(M);
1510}
1511
1512/// SetupCompileUnits - Set up the unique vector of compile units.
1513///
1514void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001515 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001516
Jim Laskey0420f2a2006-02-22 19:02:11 +00001517 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1518 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001519 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001520}
1521
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001522/// getCompileUnits - Return a vector of debug compile units.
1523///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001524const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001525 return CompileUnits;
1526}
1527
Jim Laskey0420f2a2006-02-22 19:02:11 +00001528/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1529/// named GlobalVariable.
1530std::vector<GlobalVariable*>
1531MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1532 const std::string &RootName) {
1533 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001534}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001535
1536/// RecordLabel - Records location information and associates it with a
1537/// debug label. Returns a unique label ID used to generate a label and
1538/// provide correspondence to the source line list.
1539unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1540 unsigned Source) {
1541 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001542 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001543 return ID;
1544}
1545
Chris Lattnera97906b2006-10-17 23:16:42 +00001546static bool LabelUIDComparison(const SourceLineInfo &LI, unsigned UID) {
1547 return LI.getLabelID() < UID;
1548}
1549
1550/// RemoveLabelInfo - Remove the specified label # from MachineDebugInfo, for
1551/// example because the code was deleted.
1552void MachineDebugInfo::RemoveLabelInfo(unsigned LabelUID) {
1553 std::vector<SourceLineInfo>::iterator I =
1554 std::lower_bound(Lines.begin(), Lines.end(), LabelUID, LabelUIDComparison);
1555 assert(I != Lines.end() && "Didn't find label UID in MachineDebugInfo!");
1556 Lines.erase(I);
1557}
1558
1559
Jim Laskeyb8509c52006-03-23 18:07:55 +00001560/// RecordSource - Register a source file with debug info. Returns an source
1561/// ID.
1562unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1563 const std::string &Source) {
1564 unsigned DirectoryID = Directories.insert(Directory);
1565 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1566}
1567unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1568 return RecordSource(CompileUnit->getDirectory(),
1569 CompileUnit->getFileName());
1570}
1571
1572/// RecordRegionStart - Indicate the start of a region.
1573///
1574unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1575 // FIXME - need to be able to handle split scopes because of bb cloning.
1576 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1577 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1578 unsigned ID = NextLabelID();
1579 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1580 return ID;
1581}
1582
1583/// RecordRegionEnd - Indicate the end of a region.
1584///
1585unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1586 // FIXME - need to be able to handle split scopes because of bb cloning.
1587 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1588 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1589 unsigned ID = NextLabelID();
1590 Scope->setEndLabelID(ID);
1591 return ID;
1592}
1593
1594/// RecordVariable - Indicate the declaration of a local variable.
1595///
1596void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1597 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1598 DebugScope *Scope = getOrCreateScope(VD->getContext());
1599 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1600 Scope->AddVariable(DV);
1601}
1602
1603/// getOrCreateScope - Returns the scope associated with the given descriptor.
1604///
1605DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1606 DebugScope *&Slot = ScopeMap[ScopeDesc];
1607 if (!Slot) {
1608 // FIXME - breaks down when the context is an inlined function.
1609 DebugInfoDesc *ParentDesc = NULL;
1610 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1611 ParentDesc = Block->getContext();
1612 }
1613 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1614 Slot = new DebugScope(Parent, ScopeDesc);
1615 if (Parent) {
1616 Parent->AddScope(Slot);
1617 } else if (RootScope) {
1618 // FIXME - Add inlined function scopes to the root so we can delete
1619 // them later. Long term, handle inlined functions properly.
1620 RootScope->AddScope(Slot);
1621 } else {
1622 // First function is top level function.
1623 RootScope = Slot;
1624 }
1625 }
1626 return Slot;
1627}
1628
Jim Laskeyb8509c52006-03-23 18:07:55 +00001629