blob: 041a88cdca5207067acdcfaa1de1324bdce89b8f [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 Laskey9d4209f2006-11-07 19:33:46 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
14#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey41886992006-04-07 16:34:46 +000015#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000016#include "llvm/Target/TargetInstrInfo.h"
17#include "llvm/Target/TargetMachine.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000018#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000019#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000020#include "llvm/Intrinsics.h"
21#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000024#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000025using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000026using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000027
28// Handle the Pass registration stuff necessary to use TargetData's.
29namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000030 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
31}
Jim Laskey063e7652006-01-17 17:31:53 +000032
Jim Laskeyb3e789a2006-01-26 20:21:46 +000033//===----------------------------------------------------------------------===//
34
Jim Laskey86cbdba2006-02-06 15:33:21 +000035/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000036/// specified value in their initializer somewhere.
37static void
38getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
39 // Scan though value users.
40 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
41 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000042 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000043 Result.push_back(GV);
44 } else if (Constant *C = dyn_cast<Constant>(*I)) {
45 // If the user is a constant variable then scan its users
46 getGlobalVariablesUsing(C, Result);
47 }
48 }
49}
50
Jim Laskey86cbdba2006-02-06 15:33:21 +000051/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
52/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000053static std::vector<GlobalVariable*>
54getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000055 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000056
57 std::vector<const Type*> FieldTypes;
Reid Spencer47857812006-12-31 05:55:36 +000058 FieldTypes.push_back(Type::Int32Ty);
59 FieldTypes.push_back(Type::Int32Ty);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000060
Jim Laskey86cbdba2006-02-06 15:33:21 +000061 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000063 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064
65 // If present and linkonce then scan for users.
66 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
67 getGlobalVariablesUsing(UseRoot, Result);
68 }
69
70 return Result;
71}
72
Jim Laskey86cbdba2006-02-06 15:33:21 +000073/// isStringValue - Return true if the given value can be coerced to a string.
74///
75static bool isStringValue(Value *V) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
77 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
78 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
79 return Init->isString();
80 }
81 } else if (Constant *C = dyn_cast<Constant>(V)) {
82 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
83 return isStringValue(GV);
84 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
85 if (CE->getOpcode() == Instruction::GetElementPtr) {
86 if (CE->getNumOperands() == 3 &&
87 cast<Constant>(CE->getOperand(1))->isNullValue() &&
88 isa<ConstantInt>(CE->getOperand(2))) {
89 return isStringValue(CE->getOperand(0));
90 }
91 }
92 }
93 }
94 return false;
95}
96
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000097/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +000098///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000099static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000100 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
101 return GV;
102 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000103 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000104 return dyn_cast<GlobalVariable>(CE->getOperand(0));
105 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000106 }
107 return NULL;
108}
109
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000110/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000111/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000112static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000113 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
114 return true;
115 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000116 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000117 return isa<GlobalVariable>(CE->getOperand(0));
118 }
119 }
120 return false;
121}
122
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000123/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000124///
Reid Spencerb83eb642006-10-20 07:07:24 +0000125static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000126 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000127 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000128
Jim Laskey86cbdba2006-02-06 15:33:21 +0000129 // Get the initializer constant.
130 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000131 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000132
Jim Laskey86cbdba2006-02-06 15:33:21 +0000133 // Check if there is at least i + 1 operands.
134 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000135 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000136
Jim Laskey86cbdba2006-02-06 15:33:21 +0000137 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000138 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000139}
Reid Spencerb83eb642006-10-20 07:07:24 +0000140
Jim Laskey86cbdba2006-02-06 15:33:21 +0000141//===----------------------------------------------------------------------===//
142
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000143/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000144/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000146 DD->ApplyToFields(this);
147}
148
149//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000150/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
151/// the supplied DebugInfoDesc.
152class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000153private:
154 unsigned Count; // Running count of fields.
155
156public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000157 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000158
159 // Accessors.
160 unsigned getCount() const { return Count; }
161
162 /// Apply - Count each of the fields.
163 ///
164 virtual void Apply(int &Field) { ++Count; }
165 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000166 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000167 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000168 virtual void Apply(bool &Field) { ++Count; }
169 virtual void Apply(std::string &Field) { ++Count; }
170 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
171 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000172 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
173 ++Count;
174 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000175};
176
177//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000178/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
179/// supplied DebugInfoDesc.
180class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000181private:
182 DIDeserializer &DR; // Active deserializer.
183 unsigned I; // Current operand index.
184 ConstantStruct *CI; // GlobalVariable constant initializer.
185
186public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000187 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
188 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000189 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000190 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000191 , CI(cast<ConstantStruct>(GV->getInitializer()))
192 {}
193
194 /// Apply - Set the value of each of the fields.
195 ///
196 virtual void Apply(int &Field) {
197 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000198 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000199 }
200 virtual void Apply(unsigned &Field) {
201 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000202 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000203 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000204 virtual void Apply(int64_t &Field) {
205 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000206 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000207 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000208 virtual void Apply(uint64_t &Field) {
209 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000211 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000212 virtual void Apply(bool &Field) {
213 Constant *C = CI->getOperand(I++);
214 Field = cast<ConstantBool>(C)->getValue();
215 }
216 virtual void Apply(std::string &Field) {
217 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000218 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000219 }
220 virtual void Apply(DebugInfoDesc *&Field) {
221 Constant *C = CI->getOperand(I++);
222 Field = DR.Deserialize(C);
223 }
224 virtual void Apply(GlobalVariable *&Field) {
225 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000226 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000227 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000228 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000229 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000230 Constant *C = CI->getOperand(I++);
231 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000232 if (GV->hasInitializer()) {
233 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
234 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
235 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
236 DebugInfoDesc *DE = DR.Deserialize(GVE);
237 Field.push_back(DE);
238 }
239 } else if (GV->getInitializer()->isNullValue()) {
240 if (const ArrayType *T =
241 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
242 Field.resize(T->getNumElements());
243 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000244 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000245 }
246 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000247};
248
249//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000250/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000251/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000252class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000253private:
254 DISerializer &SR; // Active serializer.
255 std::vector<Constant*> &Elements; // Element accumulator.
256
257public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000258 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
259 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000260 , SR(S)
261 , Elements(E)
262 {}
263
264 /// Apply - Set the value of each of the fields.
265 ///
266 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000267 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000268 }
269 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000270 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000271 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000272 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000273 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000274 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000275 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000276 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000277 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000278 virtual void Apply(bool &Field) {
279 Elements.push_back(ConstantBool::get(Field));
280 }
281 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000282 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000283 }
284 virtual void Apply(DebugInfoDesc *&Field) {
285 GlobalVariable *GV = NULL;
286
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000287 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000288 if (Field) GV = SR.Serialize(Field);
289
290 // FIXME - At some point should use specific type.
291 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
292
293 if (GV) {
294 // Set to pointer to global.
Reid Spencer15f46d62006-12-12 01:17:41 +0000295 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000296 } else {
297 // Use NULL.
298 Elements.push_back(ConstantPointerNull::get(EmptyTy));
299 }
300 }
301 virtual void Apply(GlobalVariable *&Field) {
302 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000303 if (Field) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000304 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000305 } else {
306 Elements.push_back(ConstantPointerNull::get(EmptyTy));
307 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000308 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000309 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
310 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
311 unsigned N = Field.size();
312 ArrayType *AT = ArrayType::get(EmptyTy, N);
313 std::vector<Constant *> ArrayElements;
314
315 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000316 if (DebugInfoDesc *Element = Field[i]) {
317 GlobalVariable *GVE = SR.Serialize(Element);
Reid Spencer15f46d62006-12-12 01:17:41 +0000318 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000319 ArrayElements.push_back(cast<Constant>(CE));
320 } else {
321 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
322 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000323 }
324
325 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000326 GlobalVariable *CAGV = new GlobalVariable(AT, true,
327 GlobalValue::InternalLinkage,
328 CA, "llvm.dbg.array",
329 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000330 CAGV->setSection("llvm.metadata");
Reid Spencer15f46d62006-12-12 01:17:41 +0000331 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000332 Elements.push_back(CAE);
333 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000334};
335
336//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000337/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000338/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000339class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000340private:
341 DISerializer &SR; // Active serializer.
342 std::vector<const Type*> &Fields; // Type accumulator.
343
344public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000345 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
346 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000347 , SR(S)
348 , Fields(F)
349 {}
350
351 /// Apply - Set the value of each of the fields.
352 ///
353 virtual void Apply(int &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000354 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000355 }
356 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000357 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000358 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000359 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000360 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000361 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000362 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000363 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000364 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000365 virtual void Apply(bool &Field) {
366 Fields.push_back(Type::BoolTy);
367 }
368 virtual void Apply(std::string &Field) {
369 Fields.push_back(SR.getStrPtrType());
370 }
371 virtual void Apply(DebugInfoDesc *&Field) {
372 // FIXME - At some point should use specific type.
373 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
374 Fields.push_back(EmptyTy);
375 }
376 virtual void Apply(GlobalVariable *&Field) {
377 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
378 Fields.push_back(EmptyTy);
379 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000380 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
381 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
382 Fields.push_back(EmptyTy);
383 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000384};
385
386//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000387/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000388/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000389class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000390private:
391 DIVerifier &VR; // Active verifier.
392 bool IsValid; // Validity status.
393 unsigned I; // Current operand index.
394 ConstantStruct *CI; // GlobalVariable constant initializer.
395
396public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000397 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
398 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000399 , VR(V)
400 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000401 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000402 , CI(cast<ConstantStruct>(GV->getInitializer()))
403 {
404 }
405
406 // Accessors.
407 bool isValid() const { return IsValid; }
408
409 /// Apply - Set the value of each of the fields.
410 ///
411 virtual void Apply(int &Field) {
412 Constant *C = CI->getOperand(I++);
413 IsValid = IsValid && isa<ConstantInt>(C);
414 }
415 virtual void Apply(unsigned &Field) {
416 Constant *C = CI->getOperand(I++);
417 IsValid = IsValid && isa<ConstantInt>(C);
418 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000419 virtual void Apply(int64_t &Field) {
420 Constant *C = CI->getOperand(I++);
421 IsValid = IsValid && isa<ConstantInt>(C);
422 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000423 virtual void Apply(uint64_t &Field) {
424 Constant *C = CI->getOperand(I++);
425 IsValid = IsValid && isa<ConstantInt>(C);
426 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000427 virtual void Apply(bool &Field) {
428 Constant *C = CI->getOperand(I++);
429 IsValid = IsValid && isa<ConstantBool>(C);
430 }
431 virtual void Apply(std::string &Field) {
432 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000433 IsValid = IsValid &&
434 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000435 }
436 virtual void Apply(DebugInfoDesc *&Field) {
437 // FIXME - Prepare the correct descriptor.
438 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000439 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000440 }
441 virtual void Apply(GlobalVariable *&Field) {
442 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000443 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000444 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000445 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
446 Constant *C = CI->getOperand(I++);
447 IsValid = IsValid && isGlobalVariable(C);
448 if (!IsValid) return;
449
450 GlobalVariable *GV = getGlobalVariable(C);
451 IsValid = IsValid && GV && GV->hasInitializer();
452 if (!IsValid) return;
453
454 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
455 IsValid = IsValid && CA;
456 if (!IsValid) return;
457
458 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
459 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
460 if (!IsValid) return;
461
462 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
463 VR.Verify(GVE);
464 }
465 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000466};
467
Jim Laskeyce72b172006-02-11 01:01:30 +0000468
Jim Laskey86cbdba2006-02-06 15:33:21 +0000469//===----------------------------------------------------------------------===//
470
Jim Laskeyed4e5662006-06-14 14:45:39 +0000471/// TagFromGlobal - Returns the tag number from a debug info descriptor
472/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000473unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000474 ConstantInt *C = getUIntOperand(GV, 0);
475 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000476 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000477}
478
479/// VersionFromGlobal - Returns the version number from a debug info
480/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
481/// int.
482unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000483 ConstantInt *C = getUIntOperand(GV, 0);
484 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000485 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000486}
487
488/// DescFactory - Create an instance of debug info descriptor based on Tag.
489/// Return NULL if not a recognized Tag.
490DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
491 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000492 case DW_TAG_anchor: return new AnchorDesc();
493 case DW_TAG_compile_unit: return new CompileUnitDesc();
494 case DW_TAG_variable: return new GlobalVariableDesc();
495 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000496 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000497 case DW_TAG_base_type: return new BasicTypeDesc();
498 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000499 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000500 case DW_TAG_reference_type:
501 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000502 case DW_TAG_volatile_type:
503 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000504 case DW_TAG_member:
505 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000506 case DW_TAG_array_type:
507 case DW_TAG_structure_type:
508 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000509 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000510 case DW_TAG_vector_type:
511 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000512 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000513 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000514 case DW_TAG_return_variable:
515 case DW_TAG_arg_variable:
516 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000517 default: break;
518 }
519 return NULL;
520}
521
522/// getLinkage - get linkage appropriate for this type of descriptor.
523///
524GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
525 return GlobalValue::InternalLinkage;
526}
527
528/// ApplyToFields - Target the vistor to the fields of the descriptor.
529///
530void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
531 Visitor->Apply(Tag);
532}
533
534//===----------------------------------------------------------------------===//
535
Jim Laskey9c4447a2006-03-01 20:39:36 +0000536AnchorDesc::AnchorDesc()
537: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000538, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000539{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000540AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000541: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000542, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000543{}
544
545// Implement isa/cast/dyncast.
546bool AnchorDesc::classof(const DebugInfoDesc *D) {
547 return D->getTag() == DW_TAG_anchor;
548}
549
Jim Laskeyce72b172006-02-11 01:01:30 +0000550/// getLinkage - get linkage appropriate for this type of descriptor.
551///
552GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
553 return GlobalValue::LinkOnceLinkage;
554}
555
556/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
557///
558void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
559 DebugInfoDesc::ApplyToFields(Visitor);
560
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000561 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000562}
563
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000564/// getDescString - Return a string used to compose global names and labels. A
565/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000566/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000567/// to the list of names left external in the internalizer.
568/// ExternalNames.insert("llvm.dbg.compile_units");
569/// ExternalNames.insert("llvm.dbg.global_variables");
570/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000571const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000572 switch (AnchorTag) {
573 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
574 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
575 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
576 default: break;
577 }
578
579 assert(0 && "Tag does not have a case for anchor string");
580 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000581}
582
583/// getTypeString - Return a string used to label this descriptors type.
584///
585const char *AnchorDesc::getTypeString() const {
586 return "llvm.dbg.anchor.type";
587}
588
589#ifndef NDEBUG
590void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000591 cerr << getDescString() << " "
592 << "Version(" << getVersion() << "), "
593 << "Tag(" << getTag() << "), "
594 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000595}
596#endif
597
598//===----------------------------------------------------------------------===//
599
600AnchoredDesc::AnchoredDesc(unsigned T)
601: DebugInfoDesc(T)
602, Anchor(NULL)
603{}
604
605/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
606///
607void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
608 DebugInfoDesc::ApplyToFields(Visitor);
609
Jim Laskey7089f452006-06-16 13:14:03 +0000610 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000611}
612
613//===----------------------------------------------------------------------===//
614
615CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000616: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000617, Language(0)
618, FileName("")
619, Directory("")
620, Producer("")
621{}
622
Jim Laskey9c4447a2006-03-01 20:39:36 +0000623// Implement isa/cast/dyncast.
624bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
625 return D->getTag() == DW_TAG_compile_unit;
626}
627
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000628/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
629///
630void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000631 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000632
633 // Handle cases out of sync with compiler.
634 if (getVersion() == 0) {
635 unsigned DebugVersion;
636 Visitor->Apply(DebugVersion);
637 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000638
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000639 Visitor->Apply(Language);
640 Visitor->Apply(FileName);
641 Visitor->Apply(Directory);
642 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000643}
644
Jim Laskeyce72b172006-02-11 01:01:30 +0000645/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000646///
Jim Laskeyce72b172006-02-11 01:01:30 +0000647const char *CompileUnitDesc::getDescString() const {
648 return "llvm.dbg.compile_unit";
649}
650
651/// getTypeString - Return a string used to label this descriptors type.
652///
653const char *CompileUnitDesc::getTypeString() const {
654 return "llvm.dbg.compile_unit.type";
655}
656
657/// getAnchorString - Return a string used to label this descriptor's anchor.
658///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000659const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000660const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000661 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000662}
663
Jim Laskey86cbdba2006-02-06 15:33:21 +0000664#ifndef NDEBUG
665void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000666 cerr << getDescString() << " "
667 << "Version(" << getVersion() << "), "
668 << "Tag(" << getTag() << "), "
669 << "Anchor(" << getAnchor() << "), "
670 << "Language(" << Language << "), "
671 << "FileName(\"" << FileName << "\"), "
672 << "Directory(\"" << Directory << "\"), "
673 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000674}
675#endif
676
677//===----------------------------------------------------------------------===//
678
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000679TypeDesc::TypeDesc(unsigned T)
680: DebugInfoDesc(T)
681, Context(NULL)
682, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000683, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000684, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000685, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000686, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000687, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000688, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000689{}
690
Jim Laskey69906002006-02-24 16:46:40 +0000691/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000692///
693void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
694 DebugInfoDesc::ApplyToFields(Visitor);
695
696 Visitor->Apply(Context);
697 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000698 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000699 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000700 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000701 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000702 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000703 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000704}
705
706/// getDescString - Return a string used to compose global names and labels.
707///
708const char *TypeDesc::getDescString() const {
709 return "llvm.dbg.type";
710}
711
712/// getTypeString - Return a string used to label this descriptor's type.
713///
714const char *TypeDesc::getTypeString() const {
715 return "llvm.dbg.type.type";
716}
717
718#ifndef NDEBUG
719void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000720 cerr << getDescString() << " "
721 << "Version(" << getVersion() << "), "
722 << "Tag(" << getTag() << "), "
723 << "Context(" << Context << "), "
724 << "Name(\"" << Name << "\"), "
725 << "File(" << File << "), "
726 << "Line(" << Line << "), "
727 << "Size(" << Size << "), "
728 << "Align(" << Align << "), "
729 << "Offset(" << Offset << "), "
730 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000731}
732#endif
733
734//===----------------------------------------------------------------------===//
735
736BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000737: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000738, Encoding(0)
739{}
740
Jim Laskey9c4447a2006-03-01 20:39:36 +0000741// Implement isa/cast/dyncast.
742bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
743 return D->getTag() == DW_TAG_base_type;
744}
745
Jim Laskey69906002006-02-24 16:46:40 +0000746/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000747///
748void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
749 TypeDesc::ApplyToFields(Visitor);
750
751 Visitor->Apply(Encoding);
752}
753
Jim Laskeyf8913f12006-03-01 17:53:02 +0000754/// getDescString - Return a string used to compose global names and labels.
755///
756const char *BasicTypeDesc::getDescString() const {
757 return "llvm.dbg.basictype";
758}
759
760/// getTypeString - Return a string used to label this descriptor's type.
761///
762const char *BasicTypeDesc::getTypeString() const {
763 return "llvm.dbg.basictype.type";
764}
765
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000766#ifndef NDEBUG
767void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000768 cerr << getDescString() << " "
769 << "Version(" << getVersion() << "), "
770 << "Tag(" << getTag() << "), "
771 << "Context(" << getContext() << "), "
772 << "Name(\"" << getName() << "\"), "
773 << "Size(" << getSize() << "), "
774 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000775}
776#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000777
Jim Laskey434b40b2006-02-23 22:37:30 +0000778//===----------------------------------------------------------------------===//
779
Jim Laskey69906002006-02-24 16:46:40 +0000780DerivedTypeDesc::DerivedTypeDesc(unsigned T)
781: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000782, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000783{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000784
Jim Laskey9c4447a2006-03-01 20:39:36 +0000785// Implement isa/cast/dyncast.
786bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
787 unsigned T = D->getTag();
788 switch (T) {
789 case DW_TAG_typedef:
790 case DW_TAG_pointer_type:
791 case DW_TAG_reference_type:
792 case DW_TAG_const_type:
793 case DW_TAG_volatile_type:
794 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000795 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000796 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000797 return true;
798 default: break;
799 }
800 return false;
801}
802
Jim Laskey69906002006-02-24 16:46:40 +0000803/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000804///
Jim Laskey69906002006-02-24 16:46:40 +0000805void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000806 TypeDesc::ApplyToFields(Visitor);
807
Jim Laskey7089f452006-06-16 13:14:03 +0000808 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000809}
810
Jim Laskeyf8913f12006-03-01 17:53:02 +0000811/// getDescString - Return a string used to compose global names and labels.
812///
813const char *DerivedTypeDesc::getDescString() const {
814 return "llvm.dbg.derivedtype";
815}
816
817/// getTypeString - Return a string used to label this descriptor's type.
818///
819const char *DerivedTypeDesc::getTypeString() const {
820 return "llvm.dbg.derivedtype.type";
821}
822
Jim Laskey434b40b2006-02-23 22:37:30 +0000823#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000824void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000825 cerr << getDescString() << " "
826 << "Version(" << getVersion() << "), "
827 << "Tag(" << getTag() << "), "
828 << "Context(" << getContext() << "), "
829 << "Name(\"" << getName() << "\"), "
830 << "Size(" << getSize() << "), "
831 << "File(" << getFile() << "), "
832 << "Line(" << getLine() << "), "
833 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000834}
835#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000836
837//===----------------------------------------------------------------------===//
838
Jim Laskeyf8913f12006-03-01 17:53:02 +0000839CompositeTypeDesc::CompositeTypeDesc(unsigned T)
840: DerivedTypeDesc(T)
841, Elements()
842{}
843
Jim Laskey9c4447a2006-03-01 20:39:36 +0000844// Implement isa/cast/dyncast.
845bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
846 unsigned T = D->getTag();
847 switch (T) {
848 case DW_TAG_array_type:
849 case DW_TAG_structure_type:
850 case DW_TAG_union_type:
851 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000852 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000853 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000854 return true;
855 default: break;
856 }
857 return false;
858}
859
Jim Laskeyf8913f12006-03-01 17:53:02 +0000860/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
861///
862void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000863 DerivedTypeDesc::ApplyToFields(Visitor);
864
Jim Laskeyf8913f12006-03-01 17:53:02 +0000865 Visitor->Apply(Elements);
866}
867
868/// getDescString - Return a string used to compose global names and labels.
869///
870const char *CompositeTypeDesc::getDescString() const {
871 return "llvm.dbg.compositetype";
872}
873
874/// getTypeString - Return a string used to label this descriptor's type.
875///
876const char *CompositeTypeDesc::getTypeString() const {
877 return "llvm.dbg.compositetype.type";
878}
879
880#ifndef NDEBUG
881void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000882 cerr << getDescString() << " "
883 << "Version(" << getVersion() << "), "
884 << "Tag(" << getTag() << "), "
885 << "Context(" << getContext() << "), "
886 << "Name(\"" << getName() << "\"), "
887 << "Size(" << getSize() << "), "
888 << "File(" << getFile() << "), "
889 << "Line(" << getLine() << "), "
890 << "FromType(" << getFromType() << "), "
891 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000892}
893#endif
894
895//===----------------------------------------------------------------------===//
896
897SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000898: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000899, Lo(0)
900, Hi(0)
901{}
902
Jim Laskey9c4447a2006-03-01 20:39:36 +0000903// Implement isa/cast/dyncast.
904bool SubrangeDesc::classof(const DebugInfoDesc *D) {
905 return D->getTag() == DW_TAG_subrange_type;
906}
907
Jim Laskeyf8913f12006-03-01 17:53:02 +0000908/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
909///
910void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
911 DebugInfoDesc::ApplyToFields(Visitor);
912
913 Visitor->Apply(Lo);
914 Visitor->Apply(Hi);
915}
916
917/// getDescString - Return a string used to compose global names and labels.
918///
919const char *SubrangeDesc::getDescString() const {
920 return "llvm.dbg.subrange";
921}
922
923/// getTypeString - Return a string used to label this descriptor's type.
924///
925const char *SubrangeDesc::getTypeString() const {
926 return "llvm.dbg.subrange.type";
927}
928
929#ifndef NDEBUG
930void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000931 cerr << getDescString() << " "
932 << "Version(" << getVersion() << "), "
933 << "Tag(" << getTag() << "), "
934 << "Lo(" << Lo << "), "
935 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000936}
937#endif
938
939//===----------------------------------------------------------------------===//
940
Jim Laskey6a3eb012006-03-01 23:52:37 +0000941EnumeratorDesc::EnumeratorDesc()
942: DebugInfoDesc(DW_TAG_enumerator)
943, Name("")
944, Value(0)
945{}
946
947// Implement isa/cast/dyncast.
948bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
949 return D->getTag() == DW_TAG_enumerator;
950}
951
952/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
953///
954void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
955 DebugInfoDesc::ApplyToFields(Visitor);
956
957 Visitor->Apply(Name);
958 Visitor->Apply(Value);
959}
960
961/// getDescString - Return a string used to compose global names and labels.
962///
963const char *EnumeratorDesc::getDescString() const {
964 return "llvm.dbg.enumerator";
965}
966
967/// getTypeString - Return a string used to label this descriptor's type.
968///
969const char *EnumeratorDesc::getTypeString() const {
970 return "llvm.dbg.enumerator.type";
971}
972
973#ifndef NDEBUG
974void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000975 cerr << getDescString() << " "
976 << "Version(" << getVersion() << "), "
977 << "Tag(" << getTag() << "), "
978 << "Name(" << Name << "), "
979 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000980}
981#endif
982
983//===----------------------------------------------------------------------===//
984
Jim Laskeyb8509c52006-03-23 18:07:55 +0000985VariableDesc::VariableDesc(unsigned T)
986: DebugInfoDesc(T)
987, Context(NULL)
988, Name("")
989, File(NULL)
990, Line(0)
991, TyDesc(0)
992{}
993
994// Implement isa/cast/dyncast.
995bool VariableDesc::classof(const DebugInfoDesc *D) {
996 unsigned T = D->getTag();
997 switch (T) {
998 case DW_TAG_auto_variable:
999 case DW_TAG_arg_variable:
1000 case DW_TAG_return_variable:
1001 return true;
1002 default: break;
1003 }
1004 return false;
1005}
1006
1007/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1008///
1009void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1010 DebugInfoDesc::ApplyToFields(Visitor);
1011
1012 Visitor->Apply(Context);
1013 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001014 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001015 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001016 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001017}
1018
1019/// getDescString - Return a string used to compose global names and labels.
1020///
1021const char *VariableDesc::getDescString() const {
1022 return "llvm.dbg.variable";
1023}
1024
1025/// getTypeString - Return a string used to label this descriptor's type.
1026///
1027const char *VariableDesc::getTypeString() const {
1028 return "llvm.dbg.variable.type";
1029}
1030
1031#ifndef NDEBUG
1032void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001033 cerr << getDescString() << " "
1034 << "Version(" << getVersion() << "), "
1035 << "Tag(" << getTag() << "), "
1036 << "Context(" << Context << "), "
1037 << "Name(\"" << Name << "\"), "
1038 << "File(" << File << "), "
1039 << "Line(" << Line << "), "
1040 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001041}
1042#endif
1043
1044//===----------------------------------------------------------------------===//
1045
Jim Laskeyce72b172006-02-11 01:01:30 +00001046GlobalDesc::GlobalDesc(unsigned T)
1047: AnchoredDesc(T)
1048, Context(0)
1049, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001050, FullName("")
1051, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001052, File(NULL)
1053, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001054, TyDesc(NULL)
1055, IsStatic(false)
1056, IsDefinition(false)
1057{}
1058
1059/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001060///
Jim Laskeyce72b172006-02-11 01:01:30 +00001061void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1062 AnchoredDesc::ApplyToFields(Visitor);
1063
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001064 Visitor->Apply(Context);
1065 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001066 Visitor->Apply(FullName);
1067 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001068 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001069 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001070 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001071 Visitor->Apply(IsStatic);
1072 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001073}
1074
1075//===----------------------------------------------------------------------===//
1076
1077GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001078: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001079, Global(NULL)
1080{}
1081
Jim Laskey9c4447a2006-03-01 20:39:36 +00001082// Implement isa/cast/dyncast.
1083bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1084 return D->getTag() == DW_TAG_variable;
1085}
1086
Jim Laskeyce72b172006-02-11 01:01:30 +00001087/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1088///
1089void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1090 GlobalDesc::ApplyToFields(Visitor);
1091
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001092 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001093}
1094
Jim Laskeyce72b172006-02-11 01:01:30 +00001095/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001096///
Jim Laskeyce72b172006-02-11 01:01:30 +00001097const char *GlobalVariableDesc::getDescString() const {
1098 return "llvm.dbg.global_variable";
1099}
1100
1101/// getTypeString - Return a string used to label this descriptors type.
1102///
1103const char *GlobalVariableDesc::getTypeString() const {
1104 return "llvm.dbg.global_variable.type";
1105}
1106
1107/// getAnchorString - Return a string used to label this descriptor's anchor.
1108///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001109const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001110const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001111 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001112}
1113
Jim Laskey86cbdba2006-02-06 15:33:21 +00001114#ifndef NDEBUG
1115void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001116 cerr << getDescString() << " "
1117 << "Version(" << getVersion() << "), "
1118 << "Tag(" << getTag() << "), "
1119 << "Anchor(" << getAnchor() << "), "
1120 << "Name(\"" << getName() << "\"), "
1121 << "FullName(\"" << getFullName() << "\"), "
1122 << "LinkageName(\"" << getLinkageName() << "\"), "
1123 << "File(" << getFile() << "),"
1124 << "Line(" << getLine() << "),"
1125 << "Type(" << getType() << "), "
1126 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1127 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1128 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001129}
1130#endif
1131
1132//===----------------------------------------------------------------------===//
1133
Jim Laskeyce72b172006-02-11 01:01:30 +00001134SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001135: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001136{}
1137
Jim Laskey9c4447a2006-03-01 20:39:36 +00001138// Implement isa/cast/dyncast.
1139bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1140 return D->getTag() == DW_TAG_subprogram;
1141}
1142
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001143/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001144/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001145void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001146 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001147}
1148
Jim Laskeyce72b172006-02-11 01:01:30 +00001149/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001150///
Jim Laskeyce72b172006-02-11 01:01:30 +00001151const char *SubprogramDesc::getDescString() const {
1152 return "llvm.dbg.subprogram";
1153}
1154
1155/// getTypeString - Return a string used to label this descriptors type.
1156///
1157const char *SubprogramDesc::getTypeString() const {
1158 return "llvm.dbg.subprogram.type";
1159}
1160
1161/// getAnchorString - Return a string used to label this descriptor's anchor.
1162///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001163const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001164const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001165 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001166}
1167
Jim Laskey86cbdba2006-02-06 15:33:21 +00001168#ifndef NDEBUG
1169void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001170 cerr << getDescString() << " "
1171 << "Version(" << getVersion() << "), "
1172 << "Tag(" << getTag() << "), "
1173 << "Anchor(" << getAnchor() << "), "
1174 << "Name(\"" << getName() << "\"), "
1175 << "FullName(\"" << getFullName() << "\"), "
1176 << "LinkageName(\"" << getLinkageName() << "\"), "
1177 << "File(" << getFile() << "),"
1178 << "Line(" << getLine() << "),"
1179 << "Type(" << getType() << "), "
1180 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1181 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001182}
1183#endif
1184
Jim Laskey45ccae52006-02-28 20:15:07 +00001185//===----------------------------------------------------------------------===//
1186
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001187BlockDesc::BlockDesc()
1188: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001189, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001190{}
1191
1192// Implement isa/cast/dyncast.
1193bool BlockDesc::classof(const DebugInfoDesc *D) {
1194 return D->getTag() == DW_TAG_lexical_block;
1195}
1196
1197/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1198///
1199void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1200 DebugInfoDesc::ApplyToFields(Visitor);
1201
Jim Laskeyb8509c52006-03-23 18:07:55 +00001202 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001203}
1204
1205/// getDescString - Return a string used to compose global names and labels.
1206///
1207const char *BlockDesc::getDescString() const {
1208 return "llvm.dbg.block";
1209}
1210
1211/// getTypeString - Return a string used to label this descriptors type.
1212///
1213const char *BlockDesc::getTypeString() const {
1214 return "llvm.dbg.block.type";
1215}
1216
1217#ifndef NDEBUG
1218void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001219 cerr << getDescString() << " "
1220 << "Version(" << getVersion() << "), "
1221 << "Tag(" << getTag() << "),"
1222 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001223}
1224#endif
1225
1226//===----------------------------------------------------------------------===//
1227
Jim Laskey86cbdba2006-02-06 15:33:21 +00001228DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001229 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001230}
1231DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001232 // Handle NULL.
1233 if (!GV) return NULL;
1234
Jim Laskey86cbdba2006-02-06 15:33:21 +00001235 // Check to see if it has been already deserialized.
1236 DebugInfoDesc *&Slot = GlobalDescs[GV];
1237 if (Slot) return Slot;
1238
1239 // Get the Tag from the global.
1240 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1241
Jim Laskey86cbdba2006-02-06 15:33:21 +00001242 // Create an empty instance of the correct sort.
1243 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001244
Jim Laskey21407982006-03-14 18:37:57 +00001245 // If not a user defined descriptor.
1246 if (Slot) {
1247 // Deserialize the fields.
1248 DIDeserializeVisitor DRAM(*this, GV);
1249 DRAM.ApplyToFields(Slot);
1250 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001251
1252 return Slot;
1253}
1254
1255//===----------------------------------------------------------------------===//
1256
1257/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001258///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001259const PointerType *DISerializer::getStrPtrType() {
1260 // If not already defined.
1261 if (!StrPtrTy) {
1262 // Construct the pointer to signed bytes.
Reid Spencer47857812006-12-31 05:55:36 +00001263 StrPtrTy = PointerType::get(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001264 }
1265
1266 return StrPtrTy;
1267}
1268
1269/// getEmptyStructPtrType - Return a "{ }*" type.
1270///
1271const PointerType *DISerializer::getEmptyStructPtrType() {
1272 // If not already defined.
1273 if (!EmptyStructPtrTy) {
1274 // Construct the empty structure type.
1275 const StructType *EmptyStructTy =
1276 StructType::get(std::vector<const Type*>());
1277 // Construct the pointer to empty structure type.
1278 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1279 }
1280
1281 return EmptyStructPtrTy;
1282}
1283
1284/// getTagType - Return the type describing the specified descriptor (via tag.)
1285///
1286const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1287 // Attempt to get the previously defined type.
1288 StructType *&Ty = TagTypes[DD->getTag()];
1289
1290 // If not already defined.
1291 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001292 // Set up fields vector.
1293 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001294 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001295 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001296 GTAM.ApplyToFields(DD);
1297
1298 // Construct structured type.
1299 Ty = StructType::get(Fields);
1300
Jim Laskey86cbdba2006-02-06 15:33:21 +00001301 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001302 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001303 }
1304
1305 return Ty;
1306}
1307
1308/// getString - Construct the string as constant string global.
1309///
Jim Laskeyce72b172006-02-11 01:01:30 +00001310Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001311 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001312 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001313 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001314 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001315 // If empty string then use a sbyte* null instead.
1316 if (String.empty()) {
1317 Slot = ConstantPointerNull::get(getStrPtrType());
1318 } else {
1319 // Construct string as an llvm constant.
1320 Constant *ConstStr = ConstantArray::get(String);
1321 // Otherwise create and return a new string global.
1322 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1323 GlobalVariable::InternalLinkage,
1324 ConstStr, "str", M);
1325 StrGV->setSection("llvm.metadata");
1326 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001327 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001328 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001329 return Slot;
1330
Jim Laskey86cbdba2006-02-06 15:33:21 +00001331}
1332
1333/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1334/// so that it can be serialized to a .bc or .ll file.
1335GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1336 // Check if the DebugInfoDesc is already in the map.
1337 GlobalVariable *&Slot = DescGlobals[DD];
1338
1339 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1340 if (Slot) return Slot;
1341
Jim Laskey86cbdba2006-02-06 15:33:21 +00001342 // Get the type associated with the Tag.
1343 const StructType *Ty = getTagType(DD);
1344
1345 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001346 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1347 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001348 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001349
1350 // Insert new GlobalVariable in DescGlobals map.
1351 Slot = GV;
1352
1353 // Set up elements vector
1354 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001355 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001356 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001357 SRAM.ApplyToFields(DD);
1358
1359 // Set the globals initializer.
1360 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1361
1362 return GV;
1363}
1364
1365//===----------------------------------------------------------------------===//
1366
Jim Laskey86cbdba2006-02-06 15:33:21 +00001367/// Verify - Return true if the GlobalVariable appears to be a valid
1368/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001369bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001370 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001371}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001372bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001373 // NULLs are valid.
1374 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001375
Jim Laskey98e04102006-03-26 22:45:20 +00001376 // Check prior validity.
1377 unsigned &ValiditySlot = Validity[GV];
1378
1379 // If visited before then use old state.
1380 if (ValiditySlot) return ValiditySlot == Valid;
1381
1382 // Assume validity for the time being (recursion.)
1383 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001384
1385 // Make sure the global is internal or link once (anchor.)
1386 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1387 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1388 ValiditySlot = Invalid;
1389 return false;
1390 }
Jim Laskey98e04102006-03-26 22:45:20 +00001391
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001392 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001393 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001394
1395 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001396 if (Tag == DW_TAG_invalid) {
1397 ValiditySlot = Valid;
1398 return true;
1399 }
1400
1401 // Get the Version.
1402 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1403
1404 // Check for version mismatch.
1405 if (Version != LLVMDebugVersion) {
1406 ValiditySlot = Invalid;
1407 return false;
1408 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001409
Jim Laskey86cbdba2006-02-06 15:33:21 +00001410 // Construct an empty DebugInfoDesc.
1411 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001412
1413 // Allow for user defined descriptors.
1414 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001415
1416 // Get the initializer constant.
1417 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1418
1419 // Get the operand count.
1420 unsigned N = CI->getNumOperands();
1421
1422 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001423 unsigned &CountSlot = Counts[Tag];
1424 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001425 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001426 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001427 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001428 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001429 }
1430
Jim Laskey21407982006-03-14 18:37:57 +00001431 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001432 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001433 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001434 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001435 return false;
1436 }
1437
1438 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001439 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001440 VRAM.ApplyToFields(DD);
1441
1442 // Release empty DebugInfoDesc.
1443 delete DD;
1444
Jim Laskey98e04102006-03-26 22:45:20 +00001445 // If fields are not valid.
1446 if (!VRAM.isValid()) {
1447 ValiditySlot = Invalid;
1448 return false;
1449 }
1450
1451 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001452}
1453
1454//===----------------------------------------------------------------------===//
1455
Jim Laskeyb8509c52006-03-23 18:07:55 +00001456DebugScope::~DebugScope() {
1457 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1458 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1459}
1460
1461//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001462
1463MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001464: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001465, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001466, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001467, Directories()
1468, SourceFiles()
1469, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001470, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001471, ScopeMap()
1472, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001473, FrameMoves()
1474{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001475MachineDebugInfo::~MachineDebugInfo() {
1476
1477}
1478
Jim Laskeyb2efb852006-01-04 22:28:25 +00001479/// doInitialization - Initialize the debug state for a new module.
1480///
1481bool MachineDebugInfo::doInitialization() {
1482 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001483}
1484
Jim Laskeyb2efb852006-01-04 22:28:25 +00001485/// doFinalization - Tear down the debug state after completion of a module.
1486///
1487bool MachineDebugInfo::doFinalization() {
1488 return false;
1489}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001490
Jim Laskey41886992006-04-07 16:34:46 +00001491/// BeginFunction - Begin gathering function debug information.
1492///
1493void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1494 // Coming soon.
1495}
1496
1497/// MachineDebugInfo::EndFunction - Discard function debug information.
1498///
1499void MachineDebugInfo::EndFunction() {
1500 // Clean up scope information.
1501 if (RootScope) {
1502 delete RootScope;
1503 ScopeMap.clear();
1504 RootScope = NULL;
1505 }
1506
1507 // Clean up frame info.
1508 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1509 FrameMoves.clear();
1510}
1511
Jim Laskeyd96185a2006-02-13 12:50:39 +00001512/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001513///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001514// FIXME - use new Value type when available.
1515DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001516 return DR.Deserialize(V);
1517}
1518
1519/// Verify - Verify that a Value is debug information descriptor.
1520///
1521bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001522 return VR.Verify(V);
1523}
1524
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001525/// AnalyzeModule - Scan the module for global debug information.
1526///
1527void MachineDebugInfo::AnalyzeModule(Module &M) {
1528 SetupCompileUnits(M);
1529}
1530
1531/// SetupCompileUnits - Set up the unique vector of compile units.
1532///
1533void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001534 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001535
Jim Laskey0420f2a2006-02-22 19:02:11 +00001536 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1537 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001538 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001539}
1540
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001541/// getCompileUnits - Return a vector of debug compile units.
1542///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001543const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001544 return CompileUnits;
1545}
1546
Jim Laskey0420f2a2006-02-22 19:02:11 +00001547/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1548/// named GlobalVariable.
1549std::vector<GlobalVariable*>
1550MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1551 const std::string &RootName) {
1552 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001553}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001554
1555/// RecordLabel - Records location information and associates it with a
1556/// debug label. Returns a unique label ID used to generate a label and
1557/// provide correspondence to the source line list.
1558unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1559 unsigned Source) {
1560 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001561 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001562 return ID;
1563}
1564
1565/// RecordSource - Register a source file with debug info. Returns an source
1566/// ID.
1567unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1568 const std::string &Source) {
1569 unsigned DirectoryID = Directories.insert(Directory);
1570 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1571}
1572unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1573 return RecordSource(CompileUnit->getDirectory(),
1574 CompileUnit->getFileName());
1575}
1576
1577/// RecordRegionStart - Indicate the start of a region.
1578///
1579unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1580 // FIXME - need to be able to handle split scopes because of bb cloning.
1581 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1582 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1583 unsigned ID = NextLabelID();
1584 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1585 return ID;
1586}
1587
1588/// RecordRegionEnd - Indicate the end of a region.
1589///
1590unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1591 // FIXME - need to be able to handle split scopes because of bb cloning.
1592 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1593 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1594 unsigned ID = NextLabelID();
1595 Scope->setEndLabelID(ID);
1596 return ID;
1597}
1598
1599/// RecordVariable - Indicate the declaration of a local variable.
1600///
1601void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1602 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1603 DebugScope *Scope = getOrCreateScope(VD->getContext());
1604 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1605 Scope->AddVariable(DV);
1606}
1607
1608/// getOrCreateScope - Returns the scope associated with the given descriptor.
1609///
1610DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1611 DebugScope *&Slot = ScopeMap[ScopeDesc];
1612 if (!Slot) {
1613 // FIXME - breaks down when the context is an inlined function.
1614 DebugInfoDesc *ParentDesc = NULL;
1615 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1616 ParentDesc = Block->getContext();
1617 }
1618 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1619 Slot = new DebugScope(Parent, ScopeDesc);
1620 if (Parent) {
1621 Parent->AddScope(Slot);
1622 } else if (RootScope) {
1623 // FIXME - Add inlined function scopes to the root so we can delete
1624 // them later. Long term, handle inlined functions properly.
1625 RootScope->AddScope(Slot);
1626 } else {
1627 // First function is top level function.
1628 RootScope = Slot;
1629 }
1630 }
1631 return Slot;
1632}
1633
Jim Laskey9d4209f2006-11-07 19:33:46 +00001634//===----------------------------------------------------------------------===//
1635/// DebugLabelFolding pass - This pass prunes out redundant debug labels. This
1636/// allows a debug emitter to determine if the range of two labels is empty,
1637/// by seeing if the labels map to the same reduced label.
1638
1639namespace llvm {
1640
1641struct DebugLabelFolder : public MachineFunctionPass {
1642 virtual bool runOnMachineFunction(MachineFunction &MF);
1643 virtual const char *getPassName() const { return "Debug Label Folder"; }
1644};
1645
1646bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1647 // Get machine debug info.
1648 MachineDebugInfo *MDI = getAnalysisToUpdate<MachineDebugInfo>();
1649 if (!MDI) return false;
1650 // Get target instruction info.
1651 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1652 if (!TII) return false;
1653 // Get target version of the debug label opcode.
1654 unsigned DWARF_LABELOpc = TII->getDWARF_LABELOpcode();
1655 if (!DWARF_LABELOpc) return false;
1656
1657 // Track if change is made.
1658 bool MadeChange = false;
1659 // No prior label to begin.
1660 unsigned PriorLabel = 0;
1661
1662 // Iterate through basic blocks.
1663 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1664 BB != E; ++BB) {
1665 // Iterate through instructions.
1666 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1667 // Is it a debug label.
1668 if ((unsigned)I->getOpcode() == DWARF_LABELOpc) {
1669 // The label ID # is always operand #0, an immediate.
1670 unsigned NextLabel = I->getOperand(0).getImm();
1671
1672 // If there was an immediate prior label.
1673 if (PriorLabel) {
1674 // Remap the current label to prior label.
1675 MDI->RemapLabel(NextLabel, PriorLabel);
1676 // Delete the current label.
1677 I = BB->erase(I);
1678 // Indicate a change has been made.
1679 MadeChange = true;
1680 continue;
1681 } else {
1682 // Start a new round.
1683 PriorLabel = NextLabel;
1684 }
1685 } else {
1686 // No consecutive labels.
1687 PriorLabel = 0;
1688 }
1689
1690 ++I;
1691 }
1692 }
1693
1694 return MadeChange;
1695}
1696
1697FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1698
1699}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001700