blob: 16f8dc8e20aeedfa91b42e25868ddab040c4a6ad [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"
24
Jim Laskey86cbdba2006-02-06 15:33:21 +000025#include <iostream>
26
Jim Laskey6af56812006-01-04 13:36:38 +000027using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000028using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000029
30// Handle the Pass registration stuff necessary to use TargetData's.
31namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000032 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
33}
Jim Laskey063e7652006-01-17 17:31:53 +000034
Jim Laskeyb3e789a2006-01-26 20:21:46 +000035//===----------------------------------------------------------------------===//
36
Jim Laskey86cbdba2006-02-06 15:33:21 +000037/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
Jim Laskeyb3e789a2006-01-26 20:21:46 +000038/// specified value in their initializer somewhere.
39static void
40getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
41 // Scan though value users.
42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000044 // If the user is a GlobalVariable then add to result.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000045 Result.push_back(GV);
46 } else if (Constant *C = dyn_cast<Constant>(*I)) {
47 // If the user is a constant variable then scan its users
48 getGlobalVariablesUsing(C, Result);
49 }
50 }
51}
52
Jim Laskey86cbdba2006-02-06 15:33:21 +000053/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
54/// named GlobalVariable.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000055static std::vector<GlobalVariable*>
56getGlobalVariablesUsing(Module &M, const std::string &RootName) {
Jim Laskey86cbdba2006-02-06 15:33:21 +000057 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria.
Jim Laskeyce72b172006-02-11 01:01:30 +000058
59 std::vector<const Type*> FieldTypes;
60 FieldTypes.push_back(Type::UIntTy);
Jim Laskeye8c3e3b2006-03-07 20:53:47 +000061 FieldTypes.push_back(Type::UIntTy);
Jim Laskeyb3e789a2006-01-26 20:21:46 +000062
Jim Laskey86cbdba2006-02-06 15:33:21 +000063 // Get the GlobalVariable root.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000064 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
Jim Laskeyce72b172006-02-11 01:01:30 +000065 StructType::get(FieldTypes));
Jim Laskeyb3e789a2006-01-26 20:21:46 +000066
67 // If present and linkonce then scan for users.
68 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
69 getGlobalVariablesUsing(UseRoot, Result);
70 }
71
72 return Result;
73}
74
Jim Laskey86cbdba2006-02-06 15:33:21 +000075/// isStringValue - Return true if the given value can be coerced to a string.
76///
77static bool isStringValue(Value *V) {
78 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
79 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
80 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
81 return Init->isString();
82 }
83 } else if (Constant *C = dyn_cast<Constant>(V)) {
84 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
85 return isStringValue(GV);
86 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 if (CE->getOpcode() == Instruction::GetElementPtr) {
88 if (CE->getNumOperands() == 3 &&
89 cast<Constant>(CE->getOperand(1))->isNullValue() &&
90 isa<ConstantInt>(CE->getOperand(2))) {
91 return isStringValue(CE->getOperand(0));
92 }
93 }
94 }
95 }
96 return false;
97}
98
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +000099/// getGlobalVariable - Return either a direct or cast Global value.
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000100///
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000101static GlobalVariable *getGlobalVariable(Value *V) {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000102 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
103 return GV;
104 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000105 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000106 return dyn_cast<GlobalVariable>(CE->getOperand(0));
107 }
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000108 }
109 return NULL;
110}
111
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000112/// isGlobalVariable - Return true if the given value can be coerced to a
Jim Laskey86cbdba2006-02-06 15:33:21 +0000113/// GlobalVariable.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000114static bool isGlobalVariable(Value *V) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000115 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
116 return true;
117 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000118 if (CE->getOpcode() == Instruction::BitCast) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000119 return isa<GlobalVariable>(CE->getOperand(0));
120 }
121 }
122 return false;
123}
124
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000125/// getUIntOperand - Return ith operand if it is an unsigned integer.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000126///
Reid Spencerb83eb642006-10-20 07:07:24 +0000127static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000128 // Make sure the GlobalVariable has an initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000129 if (!GV->hasInitializer()) return NULL;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000130
Jim Laskey86cbdba2006-02-06 15:33:21 +0000131 // Get the initializer constant.
132 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000133 if (!CI) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000134
Jim Laskey86cbdba2006-02-06 15:33:21 +0000135 // Check if there is at least i + 1 operands.
136 unsigned N = CI->getNumOperands();
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000137 if (i >= N) return NULL;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000138
Jim Laskey86cbdba2006-02-06 15:33:21 +0000139 // Check constant.
Reid Spencerb83eb642006-10-20 07:07:24 +0000140 return dyn_cast<ConstantInt>(CI->getOperand(i));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000141}
Reid Spencerb83eb642006-10-20 07:07:24 +0000142
Jim Laskey86cbdba2006-02-06 15:33:21 +0000143//===----------------------------------------------------------------------===//
144
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000145/// ApplyToFields - Target the visitor to each field of the debug information
Jim Laskey86cbdba2006-02-06 15:33:21 +0000146/// descriptor.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000147void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000148 DD->ApplyToFields(this);
149}
150
151//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000152/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
153/// the supplied DebugInfoDesc.
154class DICountVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000155private:
156 unsigned Count; // Running count of fields.
157
158public:
Jim Laskeyce72b172006-02-11 01:01:30 +0000159 DICountVisitor() : DIVisitor(), Count(0) {}
Jim Laskey86cbdba2006-02-06 15:33:21 +0000160
161 // Accessors.
162 unsigned getCount() const { return Count; }
163
164 /// Apply - Count each of the fields.
165 ///
166 virtual void Apply(int &Field) { ++Count; }
167 virtual void Apply(unsigned &Field) { ++Count; }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000168 virtual void Apply(int64_t &Field) { ++Count; }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000169 virtual void Apply(uint64_t &Field) { ++Count; }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000170 virtual void Apply(bool &Field) { ++Count; }
171 virtual void Apply(std::string &Field) { ++Count; }
172 virtual void Apply(DebugInfoDesc *&Field) { ++Count; }
173 virtual void Apply(GlobalVariable *&Field) { ++Count; }
Jim Laskey45ccae52006-02-28 20:15:07 +0000174 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
175 ++Count;
176 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000177};
178
179//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000180/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
181/// supplied DebugInfoDesc.
182class DIDeserializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000183private:
184 DIDeserializer &DR; // Active deserializer.
185 unsigned I; // Current operand index.
186 ConstantStruct *CI; // GlobalVariable constant initializer.
187
188public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000189 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
190 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000191 , DR(D)
Jim Laskeyce72b172006-02-11 01:01:30 +0000192 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000193 , CI(cast<ConstantStruct>(GV->getInitializer()))
194 {}
195
196 /// Apply - Set the value of each of the fields.
197 ///
198 virtual void Apply(int &Field) {
199 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000200 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000201 }
202 virtual void Apply(unsigned &Field) {
203 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000204 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000205 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000206 virtual void Apply(int64_t &Field) {
207 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000208 Field = cast<ConstantInt>(C)->getSExtValue();
Jim Laskeyf8913f12006-03-01 17:53:02 +0000209 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000210 virtual void Apply(uint64_t &Field) {
211 Constant *C = CI->getOperand(I++);
Reid Spencerb83eb642006-10-20 07:07:24 +0000212 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000213 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000214 virtual void Apply(bool &Field) {
215 Constant *C = CI->getOperand(I++);
216 Field = cast<ConstantBool>(C)->getValue();
217 }
218 virtual void Apply(std::string &Field) {
219 Constant *C = CI->getOperand(I++);
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000220 Field = C->getStringValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000221 }
222 virtual void Apply(DebugInfoDesc *&Field) {
223 Constant *C = CI->getOperand(I++);
224 Field = DR.Deserialize(C);
225 }
226 virtual void Apply(GlobalVariable *&Field) {
227 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000228 Field = getGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000229 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000230 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000231 Field.resize(0);
Jim Laskey45ccae52006-02-28 20:15:07 +0000232 Constant *C = CI->getOperand(I++);
233 GlobalVariable *GV = getGlobalVariable(C);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000234 if (GV->hasInitializer()) {
235 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) {
236 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
237 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
238 DebugInfoDesc *DE = DR.Deserialize(GVE);
239 Field.push_back(DE);
240 }
241 } else if (GV->getInitializer()->isNullValue()) {
242 if (const ArrayType *T =
243 dyn_cast<ArrayType>(GV->getType()->getElementType())) {
244 Field.resize(T->getNumElements());
245 }
Jim Laskey2b0e3092006-03-08 02:07:02 +0000246 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000247 }
248 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000249};
250
251//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000252/// DISerializeVisitor - This DIVisitor serializes all the fields in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000253/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000254class DISerializeVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000255private:
256 DISerializer &SR; // Active serializer.
257 std::vector<Constant*> &Elements; // Element accumulator.
258
259public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000260 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
261 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000262 , SR(S)
263 , Elements(E)
264 {}
265
266 /// Apply - Set the value of each of the fields.
267 ///
268 virtual void Apply(int &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000269 Elements.push_back(ConstantInt::get(Type::IntTy, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000270 }
271 virtual void Apply(unsigned &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000272 Elements.push_back(ConstantInt::get(Type::UIntTy, uint32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000273 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000274 virtual void Apply(int64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000275 Elements.push_back(ConstantInt::get(Type::LongTy, int64_t(Field)));
Jim Laskeyf8913f12006-03-01 17:53:02 +0000276 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000277 virtual void Apply(uint64_t &Field) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000278 Elements.push_back(ConstantInt::get(Type::ULongTy, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000279 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280 virtual void Apply(bool &Field) {
281 Elements.push_back(ConstantBool::get(Field));
282 }
283 virtual void Apply(std::string &Field) {
Jim Laskey21407982006-03-14 18:37:57 +0000284 Elements.push_back(SR.getString(Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000285 }
286 virtual void Apply(DebugInfoDesc *&Field) {
287 GlobalVariable *GV = NULL;
288
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000289 // If non-NULL then convert to global.
Jim Laskey86cbdba2006-02-06 15:33:21 +0000290 if (Field) GV = SR.Serialize(Field);
291
292 // FIXME - At some point should use specific type.
293 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
294
295 if (GV) {
296 // Set to pointer to global.
297 Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
298 } else {
299 // Use NULL.
300 Elements.push_back(ConstantPointerNull::get(EmptyTy));
301 }
302 }
303 virtual void Apply(GlobalVariable *&Field) {
304 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
Jim Laskeyce72b172006-02-11 01:01:30 +0000305 if (Field) {
306 Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
307 } else {
308 Elements.push_back(ConstantPointerNull::get(EmptyTy));
309 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000310 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000311 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
312 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
313 unsigned N = Field.size();
314 ArrayType *AT = ArrayType::get(EmptyTy, N);
315 std::vector<Constant *> ArrayElements;
316
317 for (unsigned i = 0, N = Field.size(); i < N; ++i) {
Jim Laskeyd04c1592006-07-13 15:27:42 +0000318 if (DebugInfoDesc *Element = Field[i]) {
319 GlobalVariable *GVE = SR.Serialize(Element);
320 Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
321 ArrayElements.push_back(cast<Constant>(CE));
322 } else {
323 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy));
324 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000325 }
326
327 Constant *CA = ConstantArray::get(AT, ArrayElements);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000328 GlobalVariable *CAGV = new GlobalVariable(AT, true,
329 GlobalValue::InternalLinkage,
330 CA, "llvm.dbg.array",
331 SR.getModule());
Jim Laskey78098112006-03-07 22:00:35 +0000332 CAGV->setSection("llvm.metadata");
Jim Laskeyf8913f12006-03-01 17:53:02 +0000333 Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
Jim Laskey45ccae52006-02-28 20:15:07 +0000334 Elements.push_back(CAE);
335 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000336};
337
338//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000339/// DIGetTypesVisitor - This DIVisitor gathers all the field types in
Jim Laskey86cbdba2006-02-06 15:33:21 +0000340/// the supplied DebugInfoDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000341class DIGetTypesVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000342private:
343 DISerializer &SR; // Active serializer.
344 std::vector<const Type*> &Fields; // Type accumulator.
345
346public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000347 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
348 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000349 , SR(S)
350 , Fields(F)
351 {}
352
353 /// Apply - Set the value of each of the fields.
354 ///
355 virtual void Apply(int &Field) {
356 Fields.push_back(Type::IntTy);
357 }
358 virtual void Apply(unsigned &Field) {
359 Fields.push_back(Type::UIntTy);
360 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000361 virtual void Apply(int64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000362 Fields.push_back(Type::LongTy);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000363 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000364 virtual void Apply(uint64_t &Field) {
Jim Laskey014f98c2006-06-14 11:35:03 +0000365 Fields.push_back(Type::ULongTy);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000366 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000367 virtual void Apply(bool &Field) {
368 Fields.push_back(Type::BoolTy);
369 }
370 virtual void Apply(std::string &Field) {
371 Fields.push_back(SR.getStrPtrType());
372 }
373 virtual void Apply(DebugInfoDesc *&Field) {
374 // FIXME - At some point should use specific type.
375 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
376 Fields.push_back(EmptyTy);
377 }
378 virtual void Apply(GlobalVariable *&Field) {
379 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
380 Fields.push_back(EmptyTy);
381 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000382 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
383 const PointerType *EmptyTy = SR.getEmptyStructPtrType();
384 Fields.push_back(EmptyTy);
385 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000386};
387
388//===----------------------------------------------------------------------===//
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000389/// DIVerifyVisitor - This DIVisitor verifies all the field types against
Jim Laskey86cbdba2006-02-06 15:33:21 +0000390/// a constant initializer.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000391class DIVerifyVisitor : public DIVisitor {
Jim Laskey86cbdba2006-02-06 15:33:21 +0000392private:
393 DIVerifier &VR; // Active verifier.
394 bool IsValid; // Validity status.
395 unsigned I; // Current operand index.
396 ConstantStruct *CI; // GlobalVariable constant initializer.
397
398public:
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000399 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
400 : DIVisitor()
Jim Laskey86cbdba2006-02-06 15:33:21 +0000401 , VR(V)
402 , IsValid(true)
Jim Laskeyce72b172006-02-11 01:01:30 +0000403 , I(0)
Jim Laskey86cbdba2006-02-06 15:33:21 +0000404 , CI(cast<ConstantStruct>(GV->getInitializer()))
405 {
406 }
407
408 // Accessors.
409 bool isValid() const { return IsValid; }
410
411 /// Apply - Set the value of each of the fields.
412 ///
413 virtual void Apply(int &Field) {
414 Constant *C = CI->getOperand(I++);
415 IsValid = IsValid && isa<ConstantInt>(C);
416 }
417 virtual void Apply(unsigned &Field) {
418 Constant *C = CI->getOperand(I++);
419 IsValid = IsValid && isa<ConstantInt>(C);
420 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000421 virtual void Apply(int64_t &Field) {
422 Constant *C = CI->getOperand(I++);
423 IsValid = IsValid && isa<ConstantInt>(C);
424 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000425 virtual void Apply(uint64_t &Field) {
426 Constant *C = CI->getOperand(I++);
427 IsValid = IsValid && isa<ConstantInt>(C);
428 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000429 virtual void Apply(bool &Field) {
430 Constant *C = CI->getOperand(I++);
431 IsValid = IsValid && isa<ConstantBool>(C);
432 }
433 virtual void Apply(std::string &Field) {
434 Constant *C = CI->getOperand(I++);
Jim Laskey21407982006-03-14 18:37:57 +0000435 IsValid = IsValid && (!C || isStringValue(C));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000436 }
437 virtual void Apply(DebugInfoDesc *&Field) {
438 // FIXME - Prepare the correct descriptor.
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 }
442 virtual void Apply(GlobalVariable *&Field) {
443 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000444 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000445 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000446 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
447 Constant *C = CI->getOperand(I++);
448 IsValid = IsValid && isGlobalVariable(C);
449 if (!IsValid) return;
450
451 GlobalVariable *GV = getGlobalVariable(C);
452 IsValid = IsValid && GV && GV->hasInitializer();
453 if (!IsValid) return;
454
455 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
456 IsValid = IsValid && CA;
457 if (!IsValid) return;
458
459 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
460 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
461 if (!IsValid) return;
462
463 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
464 VR.Verify(GVE);
465 }
466 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000467};
468
Jim Laskeyce72b172006-02-11 01:01:30 +0000469
Jim Laskey86cbdba2006-02-06 15:33:21 +0000470//===----------------------------------------------------------------------===//
471
Jim Laskeyed4e5662006-06-14 14:45:39 +0000472/// TagFromGlobal - Returns the tag number from a debug info descriptor
473/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000474unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000475 ConstantInt *C = getUIntOperand(GV, 0);
476 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000477 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000478}
479
480/// VersionFromGlobal - Returns the version number from a debug info
481/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
482/// int.
483unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000484 ConstantInt *C = getUIntOperand(GV, 0);
485 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000486 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000487}
488
489/// DescFactory - Create an instance of debug info descriptor based on Tag.
490/// Return NULL if not a recognized Tag.
491DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
492 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000493 case DW_TAG_anchor: return new AnchorDesc();
494 case DW_TAG_compile_unit: return new CompileUnitDesc();
495 case DW_TAG_variable: return new GlobalVariableDesc();
496 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000497 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000498 case DW_TAG_base_type: return new BasicTypeDesc();
499 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000500 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000501 case DW_TAG_reference_type:
502 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000503 case DW_TAG_volatile_type:
504 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000505 case DW_TAG_member:
506 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000507 case DW_TAG_array_type:
508 case DW_TAG_structure_type:
509 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000510 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000511 case DW_TAG_vector_type:
512 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000513 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000514 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000515 case DW_TAG_return_variable:
516 case DW_TAG_arg_variable:
517 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000518 default: break;
519 }
520 return NULL;
521}
522
523/// getLinkage - get linkage appropriate for this type of descriptor.
524///
525GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
526 return GlobalValue::InternalLinkage;
527}
528
529/// ApplyToFields - Target the vistor to the fields of the descriptor.
530///
531void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
532 Visitor->Apply(Tag);
533}
534
535//===----------------------------------------------------------------------===//
536
Jim Laskey9c4447a2006-03-01 20:39:36 +0000537AnchorDesc::AnchorDesc()
538: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000539, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000540{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000541AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000542: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000543, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000544{}
545
546// Implement isa/cast/dyncast.
547bool AnchorDesc::classof(const DebugInfoDesc *D) {
548 return D->getTag() == DW_TAG_anchor;
549}
550
Jim Laskeyce72b172006-02-11 01:01:30 +0000551/// getLinkage - get linkage appropriate for this type of descriptor.
552///
553GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
554 return GlobalValue::LinkOnceLinkage;
555}
556
557/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
558///
559void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
560 DebugInfoDesc::ApplyToFields(Visitor);
561
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000562 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000563}
564
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000565/// getDescString - Return a string used to compose global names and labels. A
566/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000567/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000568/// to the list of names left external in the internalizer.
569/// ExternalNames.insert("llvm.dbg.compile_units");
570/// ExternalNames.insert("llvm.dbg.global_variables");
571/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000572const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000573 switch (AnchorTag) {
574 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
575 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
576 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
577 default: break;
578 }
579
580 assert(0 && "Tag does not have a case for anchor string");
581 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000582}
583
584/// getTypeString - Return a string used to label this descriptors type.
585///
586const char *AnchorDesc::getTypeString() const {
587 return "llvm.dbg.anchor.type";
588}
589
590#ifndef NDEBUG
591void AnchorDesc::dump() {
592 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000593 << "Version(" << getVersion() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000594 << "Tag(" << getTag() << "), "
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000595 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000596}
597#endif
598
599//===----------------------------------------------------------------------===//
600
601AnchoredDesc::AnchoredDesc(unsigned T)
602: DebugInfoDesc(T)
603, Anchor(NULL)
604{}
605
606/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
607///
608void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
609 DebugInfoDesc::ApplyToFields(Visitor);
610
Jim Laskey7089f452006-06-16 13:14:03 +0000611 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000612}
613
614//===----------------------------------------------------------------------===//
615
616CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000617: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000618, Language(0)
619, FileName("")
620, Directory("")
621, Producer("")
622{}
623
Jim Laskey9c4447a2006-03-01 20:39:36 +0000624// Implement isa/cast/dyncast.
625bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
626 return D->getTag() == DW_TAG_compile_unit;
627}
628
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000629/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
630///
631void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000632 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000633
634 // Handle cases out of sync with compiler.
635 if (getVersion() == 0) {
636 unsigned DebugVersion;
637 Visitor->Apply(DebugVersion);
638 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000639
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000640 Visitor->Apply(Language);
641 Visitor->Apply(FileName);
642 Visitor->Apply(Directory);
643 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000644}
645
Jim Laskeyce72b172006-02-11 01:01:30 +0000646/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000647///
Jim Laskeyce72b172006-02-11 01:01:30 +0000648const char *CompileUnitDesc::getDescString() const {
649 return "llvm.dbg.compile_unit";
650}
651
652/// getTypeString - Return a string used to label this descriptors type.
653///
654const char *CompileUnitDesc::getTypeString() const {
655 return "llvm.dbg.compile_unit.type";
656}
657
658/// getAnchorString - Return a string used to label this descriptor's anchor.
659///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000660const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000661const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000662 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000663}
664
Jim Laskey86cbdba2006-02-06 15:33:21 +0000665#ifndef NDEBUG
666void CompileUnitDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +0000667 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000668 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000669 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +0000670 << "Anchor(" << getAnchor() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +0000671 << "Language(" << Language << "), "
672 << "FileName(\"" << FileName << "\"), "
673 << "Directory(\"" << Directory << "\"), "
674 << "Producer(\"" << Producer << "\")\n";
675}
676#endif
677
678//===----------------------------------------------------------------------===//
679
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000680TypeDesc::TypeDesc(unsigned T)
681: DebugInfoDesc(T)
682, Context(NULL)
683, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000684, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000685, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000686, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000687, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000688, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000689, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000690{}
691
Jim Laskey69906002006-02-24 16:46:40 +0000692/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000693///
694void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
695 DebugInfoDesc::ApplyToFields(Visitor);
696
697 Visitor->Apply(Context);
698 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000699 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000700 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000701 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000702 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000703 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000704 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000705}
706
707/// getDescString - Return a string used to compose global names and labels.
708///
709const char *TypeDesc::getDescString() const {
710 return "llvm.dbg.type";
711}
712
713/// getTypeString - Return a string used to label this descriptor's type.
714///
715const char *TypeDesc::getTypeString() const {
716 return "llvm.dbg.type.type";
717}
718
719#ifndef NDEBUG
720void TypeDesc::dump() {
721 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000722 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000723 << "Tag(" << getTag() << "), "
724 << "Context(" << Context << "), "
725 << "Name(\"" << Name << "\"), "
Jim Laskey69906002006-02-24 16:46:40 +0000726 << "File(" << File << "), "
727 << "Line(" << Line << "), "
Jim Laskeyf01e5472006-03-03 15:06:57 +0000728 << "Size(" << Size << "), "
Chris Lattner2695de42006-03-09 17:48:46 +0000729 << "Align(" << Align << "), "
Jim Laskeye2a78f22006-07-11 15:58:09 +0000730 << "Offset(" << Offset << "), "
731 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000732}
733#endif
734
735//===----------------------------------------------------------------------===//
736
737BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000738: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000739, Encoding(0)
740{}
741
Jim Laskey9c4447a2006-03-01 20:39:36 +0000742// Implement isa/cast/dyncast.
743bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
744 return D->getTag() == DW_TAG_base_type;
745}
746
Jim Laskey69906002006-02-24 16:46:40 +0000747/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000748///
749void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
750 TypeDesc::ApplyToFields(Visitor);
751
752 Visitor->Apply(Encoding);
753}
754
Jim Laskeyf8913f12006-03-01 17:53:02 +0000755/// getDescString - Return a string used to compose global names and labels.
756///
757const char *BasicTypeDesc::getDescString() const {
758 return "llvm.dbg.basictype";
759}
760
761/// getTypeString - Return a string used to label this descriptor's type.
762///
763const char *BasicTypeDesc::getTypeString() const {
764 return "llvm.dbg.basictype.type";
765}
766
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000767#ifndef NDEBUG
768void BasicTypeDesc::dump() {
769 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000770 << "Version(" << getVersion() << "), "
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000771 << "Tag(" << getTag() << "), "
772 << "Context(" << getContext() << "), "
773 << "Name(\"" << getName() << "\"), "
774 << "Size(" << getSize() << "), "
775 << "Encoding(" << Encoding << ")\n";
776}
777#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000778
Jim Laskey434b40b2006-02-23 22:37:30 +0000779//===----------------------------------------------------------------------===//
780
Jim Laskey69906002006-02-24 16:46:40 +0000781DerivedTypeDesc::DerivedTypeDesc(unsigned T)
782: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000783, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000784{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000785
Jim Laskey9c4447a2006-03-01 20:39:36 +0000786// Implement isa/cast/dyncast.
787bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
788 unsigned T = D->getTag();
789 switch (T) {
790 case DW_TAG_typedef:
791 case DW_TAG_pointer_type:
792 case DW_TAG_reference_type:
793 case DW_TAG_const_type:
794 case DW_TAG_volatile_type:
795 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000796 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000797 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000798 return true;
799 default: break;
800 }
801 return false;
802}
803
Jim Laskey69906002006-02-24 16:46:40 +0000804/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000805///
Jim Laskey69906002006-02-24 16:46:40 +0000806void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000807 TypeDesc::ApplyToFields(Visitor);
808
Jim Laskey7089f452006-06-16 13:14:03 +0000809 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000810}
811
Jim Laskeyf8913f12006-03-01 17:53:02 +0000812/// getDescString - Return a string used to compose global names and labels.
813///
814const char *DerivedTypeDesc::getDescString() const {
815 return "llvm.dbg.derivedtype";
816}
817
818/// getTypeString - Return a string used to label this descriptor's type.
819///
820const char *DerivedTypeDesc::getTypeString() const {
821 return "llvm.dbg.derivedtype.type";
822}
823
Jim Laskey434b40b2006-02-23 22:37:30 +0000824#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000825void DerivedTypeDesc::dump() {
Jim Laskey434b40b2006-02-23 22:37:30 +0000826 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000827 << "Version(" << getVersion() << "), "
Jim Laskey434b40b2006-02-23 22:37:30 +0000828 << "Tag(" << getTag() << "), "
829 << "Context(" << getContext() << "), "
830 << "Name(\"" << getName() << "\"), "
831 << "Size(" << getSize() << "), "
Jim Laskey69906002006-02-24 16:46:40 +0000832 << "File(" << getFile() << "), "
833 << "Line(" << getLine() << "), "
834 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000835}
836#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000837
838//===----------------------------------------------------------------------===//
839
Jim Laskeyf8913f12006-03-01 17:53:02 +0000840CompositeTypeDesc::CompositeTypeDesc(unsigned T)
841: DerivedTypeDesc(T)
842, Elements()
843{}
844
Jim Laskey9c4447a2006-03-01 20:39:36 +0000845// Implement isa/cast/dyncast.
846bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
847 unsigned T = D->getTag();
848 switch (T) {
849 case DW_TAG_array_type:
850 case DW_TAG_structure_type:
851 case DW_TAG_union_type:
852 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000853 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000854 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000855 return true;
856 default: break;
857 }
858 return false;
859}
860
Jim Laskeyf8913f12006-03-01 17:53:02 +0000861/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
862///
863void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000864 DerivedTypeDesc::ApplyToFields(Visitor);
865
Jim Laskeyf8913f12006-03-01 17:53:02 +0000866 Visitor->Apply(Elements);
867}
868
869/// getDescString - Return a string used to compose global names and labels.
870///
871const char *CompositeTypeDesc::getDescString() const {
872 return "llvm.dbg.compositetype";
873}
874
875/// getTypeString - Return a string used to label this descriptor's type.
876///
877const char *CompositeTypeDesc::getTypeString() const {
878 return "llvm.dbg.compositetype.type";
879}
880
881#ifndef NDEBUG
882void CompositeTypeDesc::dump() {
883 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000884 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000885 << "Tag(" << getTag() << "), "
886 << "Context(" << getContext() << "), "
887 << "Name(\"" << getName() << "\"), "
888 << "Size(" << getSize() << "), "
889 << "File(" << getFile() << "), "
890 << "Line(" << getLine() << "), "
891 << "FromType(" << getFromType() << "), "
892 << "Elements.size(" << Elements.size() << ")\n";
893}
894#endif
895
896//===----------------------------------------------------------------------===//
897
898SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000899: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000900, Lo(0)
901, Hi(0)
902{}
903
Jim Laskey9c4447a2006-03-01 20:39:36 +0000904// Implement isa/cast/dyncast.
905bool SubrangeDesc::classof(const DebugInfoDesc *D) {
906 return D->getTag() == DW_TAG_subrange_type;
907}
908
Jim Laskeyf8913f12006-03-01 17:53:02 +0000909/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
910///
911void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
912 DebugInfoDesc::ApplyToFields(Visitor);
913
914 Visitor->Apply(Lo);
915 Visitor->Apply(Hi);
916}
917
918/// getDescString - Return a string used to compose global names and labels.
919///
920const char *SubrangeDesc::getDescString() const {
921 return "llvm.dbg.subrange";
922}
923
924/// getTypeString - Return a string used to label this descriptor's type.
925///
926const char *SubrangeDesc::getTypeString() const {
927 return "llvm.dbg.subrange.type";
928}
929
930#ifndef NDEBUG
931void SubrangeDesc::dump() {
932 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000933 << "Version(" << getVersion() << "), "
Jim Laskeyf8913f12006-03-01 17:53:02 +0000934 << "Tag(" << getTag() << "), "
935 << "Lo(" << Lo << "), "
936 << "Hi(" << Hi << ")\n";
937}
938#endif
939
940//===----------------------------------------------------------------------===//
941
Jim Laskey6a3eb012006-03-01 23:52:37 +0000942EnumeratorDesc::EnumeratorDesc()
943: DebugInfoDesc(DW_TAG_enumerator)
944, Name("")
945, Value(0)
946{}
947
948// Implement isa/cast/dyncast.
949bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
950 return D->getTag() == DW_TAG_enumerator;
951}
952
953/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
954///
955void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
956 DebugInfoDesc::ApplyToFields(Visitor);
957
958 Visitor->Apply(Name);
959 Visitor->Apply(Value);
960}
961
962/// getDescString - Return a string used to compose global names and labels.
963///
964const char *EnumeratorDesc::getDescString() const {
965 return "llvm.dbg.enumerator";
966}
967
968/// getTypeString - Return a string used to label this descriptor's type.
969///
970const char *EnumeratorDesc::getTypeString() const {
971 return "llvm.dbg.enumerator.type";
972}
973
974#ifndef NDEBUG
975void EnumeratorDesc::dump() {
976 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +0000977 << "Version(" << getVersion() << "), "
Jim Laskey6a3eb012006-03-01 23:52:37 +0000978 << "Tag(" << getTag() << "), "
979 << "Name(" << Name << "), "
980 << "Value(" << Value << ")\n";
981}
982#endif
983
984//===----------------------------------------------------------------------===//
985
Jim Laskeyb8509c52006-03-23 18:07:55 +0000986VariableDesc::VariableDesc(unsigned T)
987: DebugInfoDesc(T)
988, Context(NULL)
989, Name("")
990, File(NULL)
991, Line(0)
992, TyDesc(0)
993{}
994
995// Implement isa/cast/dyncast.
996bool VariableDesc::classof(const DebugInfoDesc *D) {
997 unsigned T = D->getTag();
998 switch (T) {
999 case DW_TAG_auto_variable:
1000 case DW_TAG_arg_variable:
1001 case DW_TAG_return_variable:
1002 return true;
1003 default: break;
1004 }
1005 return false;
1006}
1007
1008/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1009///
1010void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1011 DebugInfoDesc::ApplyToFields(Visitor);
1012
1013 Visitor->Apply(Context);
1014 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001015 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001016 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001017 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001018}
1019
1020/// getDescString - Return a string used to compose global names and labels.
1021///
1022const char *VariableDesc::getDescString() const {
1023 return "llvm.dbg.variable";
1024}
1025
1026/// getTypeString - Return a string used to label this descriptor's type.
1027///
1028const char *VariableDesc::getTypeString() const {
1029 return "llvm.dbg.variable.type";
1030}
1031
1032#ifndef NDEBUG
1033void VariableDesc::dump() {
1034 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001035 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001036 << "Tag(" << getTag() << "), "
1037 << "Context(" << Context << "), "
1038 << "Name(\"" << Name << "\"), "
1039 << "File(" << File << "), "
1040 << "Line(" << Line << "), "
1041 << "TyDesc(" << TyDesc << ")\n";
1042}
1043#endif
1044
1045//===----------------------------------------------------------------------===//
1046
Jim Laskeyce72b172006-02-11 01:01:30 +00001047GlobalDesc::GlobalDesc(unsigned T)
1048: AnchoredDesc(T)
1049, Context(0)
1050, Name("")
Jim Laskeye2a78f22006-07-11 15:58:09 +00001051, DisplayName("")
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 Laskeye2a78f22006-07-11 15:58:09 +00001066 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(DisplayName);
Jim Laskey7089f452006-06-16 13:14:03 +00001067 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001068 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001069 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001070 Visitor->Apply(IsStatic);
1071 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001072}
1073
1074//===----------------------------------------------------------------------===//
1075
1076GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001077: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001078, Global(NULL)
1079{}
1080
Jim Laskey9c4447a2006-03-01 20:39:36 +00001081// Implement isa/cast/dyncast.
1082bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1083 return D->getTag() == DW_TAG_variable;
1084}
1085
Jim Laskeyce72b172006-02-11 01:01:30 +00001086/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1087///
1088void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1089 GlobalDesc::ApplyToFields(Visitor);
1090
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001091 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001092}
1093
Jim Laskeyce72b172006-02-11 01:01:30 +00001094/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001095///
Jim Laskeyce72b172006-02-11 01:01:30 +00001096const char *GlobalVariableDesc::getDescString() const {
1097 return "llvm.dbg.global_variable";
1098}
1099
1100/// getTypeString - Return a string used to label this descriptors type.
1101///
1102const char *GlobalVariableDesc::getTypeString() const {
1103 return "llvm.dbg.global_variable.type";
1104}
1105
1106/// getAnchorString - Return a string used to label this descriptor's anchor.
1107///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001108const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001109const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001110 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001111}
1112
Jim Laskey86cbdba2006-02-06 15:33:21 +00001113#ifndef NDEBUG
1114void GlobalVariableDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001115 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001116 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001117 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001118 << "Anchor(" << getAnchor() << "), "
1119 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001120 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001121 << "File(" << getFile() << "),"
1122 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001123 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001124 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1125 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001126 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001127}
1128#endif
1129
1130//===----------------------------------------------------------------------===//
1131
Jim Laskeyce72b172006-02-11 01:01:30 +00001132SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001133: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001134{}
1135
Jim Laskey9c4447a2006-03-01 20:39:36 +00001136// Implement isa/cast/dyncast.
1137bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1138 return D->getTag() == DW_TAG_subprogram;
1139}
1140
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001141/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001142/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001143void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001144 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001145}
1146
Jim Laskeyce72b172006-02-11 01:01:30 +00001147/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001148///
Jim Laskeyce72b172006-02-11 01:01:30 +00001149const char *SubprogramDesc::getDescString() const {
1150 return "llvm.dbg.subprogram";
1151}
1152
1153/// getTypeString - Return a string used to label this descriptors type.
1154///
1155const char *SubprogramDesc::getTypeString() const {
1156 return "llvm.dbg.subprogram.type";
1157}
1158
1159/// getAnchorString - Return a string used to label this descriptor's anchor.
1160///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001161const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001162const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001163 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001164}
1165
Jim Laskey86cbdba2006-02-06 15:33:21 +00001166#ifndef NDEBUG
1167void SubprogramDesc::dump() {
Jim Laskeyce72b172006-02-11 01:01:30 +00001168 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001169 << "Version(" << getVersion() << "), "
Jim Laskey86cbdba2006-02-06 15:33:21 +00001170 << "Tag(" << getTag() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001171 << "Anchor(" << getAnchor() << "), "
1172 << "Name(\"" << getName() << "\"), "
Jim Laskeye2a78f22006-07-11 15:58:09 +00001173 << "DisplayName(\"" << getDisplayName() << "\"), "
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001174 << "File(" << getFile() << "),"
1175 << "Line(" << getLine() << "),"
Jim Laskey774f8542006-10-13 13:01:34 +00001176 << "Type(" << getType() << "), "
Jim Laskeyce72b172006-02-11 01:01:30 +00001177 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1178 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001179}
1180#endif
1181
Jim Laskey45ccae52006-02-28 20:15:07 +00001182//===----------------------------------------------------------------------===//
1183
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001184BlockDesc::BlockDesc()
1185: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001186, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001187{}
1188
1189// Implement isa/cast/dyncast.
1190bool BlockDesc::classof(const DebugInfoDesc *D) {
1191 return D->getTag() == DW_TAG_lexical_block;
1192}
1193
1194/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1195///
1196void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1197 DebugInfoDesc::ApplyToFields(Visitor);
1198
Jim Laskeyb8509c52006-03-23 18:07:55 +00001199 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001200}
1201
1202/// getDescString - Return a string used to compose global names and labels.
1203///
1204const char *BlockDesc::getDescString() const {
1205 return "llvm.dbg.block";
1206}
1207
1208/// getTypeString - Return a string used to label this descriptors type.
1209///
1210const char *BlockDesc::getTypeString() const {
1211 return "llvm.dbg.block.type";
1212}
1213
1214#ifndef NDEBUG
1215void BlockDesc::dump() {
1216 std::cerr << getDescString() << " "
Jim Laskeyed4e5662006-06-14 14:45:39 +00001217 << "Version(" << getVersion() << "), "
Jim Laskeyb8509c52006-03-23 18:07:55 +00001218 << "Tag(" << getTag() << "),"
1219 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001220}
1221#endif
1222
1223//===----------------------------------------------------------------------===//
1224
Jim Laskey86cbdba2006-02-06 15:33:21 +00001225DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001226 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001227}
1228DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001229 // Handle NULL.
1230 if (!GV) return NULL;
1231
Jim Laskey86cbdba2006-02-06 15:33:21 +00001232 // Check to see if it has been already deserialized.
1233 DebugInfoDesc *&Slot = GlobalDescs[GV];
1234 if (Slot) return Slot;
1235
1236 // Get the Tag from the global.
1237 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1238
Jim Laskey86cbdba2006-02-06 15:33:21 +00001239 // Create an empty instance of the correct sort.
1240 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001241
Jim Laskey21407982006-03-14 18:37:57 +00001242 // If not a user defined descriptor.
1243 if (Slot) {
1244 // Deserialize the fields.
1245 DIDeserializeVisitor DRAM(*this, GV);
1246 DRAM.ApplyToFields(Slot);
1247 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001248
1249 return Slot;
1250}
1251
1252//===----------------------------------------------------------------------===//
1253
1254/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001255///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001256const PointerType *DISerializer::getStrPtrType() {
1257 // If not already defined.
1258 if (!StrPtrTy) {
1259 // Construct the pointer to signed bytes.
1260 StrPtrTy = PointerType::get(Type::SByteTy);
1261 }
1262
1263 return StrPtrTy;
1264}
1265
1266/// getEmptyStructPtrType - Return a "{ }*" type.
1267///
1268const PointerType *DISerializer::getEmptyStructPtrType() {
1269 // If not already defined.
1270 if (!EmptyStructPtrTy) {
1271 // Construct the empty structure type.
1272 const StructType *EmptyStructTy =
1273 StructType::get(std::vector<const Type*>());
1274 // Construct the pointer to empty structure type.
1275 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1276 }
1277
1278 return EmptyStructPtrTy;
1279}
1280
1281/// getTagType - Return the type describing the specified descriptor (via tag.)
1282///
1283const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1284 // Attempt to get the previously defined type.
1285 StructType *&Ty = TagTypes[DD->getTag()];
1286
1287 // If not already defined.
1288 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001289 // Set up fields vector.
1290 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001291 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001292 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001293 GTAM.ApplyToFields(DD);
1294
1295 // Construct structured type.
1296 Ty = StructType::get(Fields);
1297
Jim Laskey86cbdba2006-02-06 15:33:21 +00001298 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001299 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001300 }
1301
1302 return Ty;
1303}
1304
1305/// getString - Construct the string as constant string global.
1306///
Jim Laskeyce72b172006-02-11 01:01:30 +00001307Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001308 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001309 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001310 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001311 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001312 // If empty string then use a sbyte* null instead.
1313 if (String.empty()) {
1314 Slot = ConstantPointerNull::get(getStrPtrType());
1315 } else {
1316 // Construct string as an llvm constant.
1317 Constant *ConstStr = ConstantArray::get(String);
1318 // Otherwise create and return a new string global.
1319 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1320 GlobalVariable::InternalLinkage,
1321 ConstStr, "str", M);
1322 StrGV->setSection("llvm.metadata");
1323 // Convert to generic string pointer.
1324 Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1325 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001326 return Slot;
1327
Jim Laskey86cbdba2006-02-06 15:33:21 +00001328}
1329
1330/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1331/// so that it can be serialized to a .bc or .ll file.
1332GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1333 // Check if the DebugInfoDesc is already in the map.
1334 GlobalVariable *&Slot = DescGlobals[DD];
1335
1336 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1337 if (Slot) return Slot;
1338
Jim Laskey86cbdba2006-02-06 15:33:21 +00001339 // Get the type associated with the Tag.
1340 const StructType *Ty = getTagType(DD);
1341
1342 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001343 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1344 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001345 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001346
1347 // Insert new GlobalVariable in DescGlobals map.
1348 Slot = GV;
1349
1350 // Set up elements vector
1351 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001352 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001353 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001354 SRAM.ApplyToFields(DD);
1355
1356 // Set the globals initializer.
1357 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1358
1359 return GV;
1360}
1361
1362//===----------------------------------------------------------------------===//
1363
Jim Laskey86cbdba2006-02-06 15:33:21 +00001364/// Verify - Return true if the GlobalVariable appears to be a valid
1365/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001366bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001367 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001368}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001369bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001370 // NULLs are valid.
1371 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001372
Jim Laskey98e04102006-03-26 22:45:20 +00001373 // Check prior validity.
1374 unsigned &ValiditySlot = Validity[GV];
1375
1376 // If visited before then use old state.
1377 if (ValiditySlot) return ValiditySlot == Valid;
1378
1379 // Assume validity for the time being (recursion.)
1380 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001381
1382 // Make sure the global is internal or link once (anchor.)
1383 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1384 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1385 ValiditySlot = Invalid;
1386 return false;
1387 }
Jim Laskey98e04102006-03-26 22:45:20 +00001388
Jim Laskey86cbdba2006-02-06 15:33:21 +00001389 // Get the Tag
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001390 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001391
1392 // Check for user defined descriptors.
1393 if (Tag == DW_TAG_invalid) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001394
Jim Laskey86cbdba2006-02-06 15:33:21 +00001395 // Construct an empty DebugInfoDesc.
1396 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001397
1398 // Allow for user defined descriptors.
1399 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001400
1401 // Get the initializer constant.
1402 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1403
1404 // Get the operand count.
1405 unsigned N = CI->getNumOperands();
1406
1407 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001408 unsigned &CountSlot = Counts[Tag];
1409 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001410 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001411 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001412 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001413 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001414 }
1415
Jim Laskey21407982006-03-14 18:37:57 +00001416 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001417 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001418 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001419 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001420 return false;
1421 }
1422
1423 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001424 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001425 VRAM.ApplyToFields(DD);
1426
1427 // Release empty DebugInfoDesc.
1428 delete DD;
1429
Jim Laskey98e04102006-03-26 22:45:20 +00001430 // If fields are not valid.
1431 if (!VRAM.isValid()) {
1432 ValiditySlot = Invalid;
1433 return false;
1434 }
1435
1436 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001437}
1438
1439//===----------------------------------------------------------------------===//
1440
Jim Laskeyb8509c52006-03-23 18:07:55 +00001441DebugScope::~DebugScope() {
1442 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1443 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1444}
1445
1446//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001447
1448MachineDebugInfo::MachineDebugInfo()
Jim Laskeyce72b172006-02-11 01:01:30 +00001449: DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001450, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001451, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001452, Directories()
1453, SourceFiles()
1454, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001455, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001456, ScopeMap()
1457, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001458, FrameMoves()
1459{}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001460MachineDebugInfo::~MachineDebugInfo() {
1461
1462}
1463
Jim Laskeyb2efb852006-01-04 22:28:25 +00001464/// doInitialization - Initialize the debug state for a new module.
1465///
1466bool MachineDebugInfo::doInitialization() {
1467 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001468}
1469
Jim Laskeyb2efb852006-01-04 22:28:25 +00001470/// doFinalization - Tear down the debug state after completion of a module.
1471///
1472bool MachineDebugInfo::doFinalization() {
1473 return false;
1474}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001475
Jim Laskey41886992006-04-07 16:34:46 +00001476/// BeginFunction - Begin gathering function debug information.
1477///
1478void MachineDebugInfo::BeginFunction(MachineFunction *MF) {
1479 // Coming soon.
1480}
1481
1482/// MachineDebugInfo::EndFunction - Discard function debug information.
1483///
1484void MachineDebugInfo::EndFunction() {
1485 // Clean up scope information.
1486 if (RootScope) {
1487 delete RootScope;
1488 ScopeMap.clear();
1489 RootScope = NULL;
1490 }
1491
1492 // Clean up frame info.
1493 for (unsigned i = 0, N = FrameMoves.size(); i < N; ++i) delete FrameMoves[i];
1494 FrameMoves.clear();
1495}
1496
Jim Laskeyd96185a2006-02-13 12:50:39 +00001497/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001498///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001499// FIXME - use new Value type when available.
1500DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001501 return DR.Deserialize(V);
1502}
1503
1504/// Verify - Verify that a Value is debug information descriptor.
1505///
1506bool MachineDebugInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001507 return VR.Verify(V);
1508}
1509
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001510/// AnalyzeModule - Scan the module for global debug information.
1511///
1512void MachineDebugInfo::AnalyzeModule(Module &M) {
1513 SetupCompileUnits(M);
1514}
1515
1516/// SetupCompileUnits - Set up the unique vector of compile units.
1517///
1518void MachineDebugInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001519 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001520
Jim Laskey0420f2a2006-02-22 19:02:11 +00001521 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1522 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001523 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001524}
1525
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001526/// getCompileUnits - Return a vector of debug compile units.
1527///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001528const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001529 return CompileUnits;
1530}
1531
Jim Laskey0420f2a2006-02-22 19:02:11 +00001532/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1533/// named GlobalVariable.
1534std::vector<GlobalVariable*>
1535MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1536 const std::string &RootName) {
1537 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001538}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001539
1540/// RecordLabel - Records location information and associates it with a
1541/// debug label. Returns a unique label ID used to generate a label and
1542/// provide correspondence to the source line list.
1543unsigned MachineDebugInfo::RecordLabel(unsigned Line, unsigned Column,
1544 unsigned Source) {
1545 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001546 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001547 return ID;
1548}
1549
1550/// RecordSource - Register a source file with debug info. Returns an source
1551/// ID.
1552unsigned MachineDebugInfo::RecordSource(const std::string &Directory,
1553 const std::string &Source) {
1554 unsigned DirectoryID = Directories.insert(Directory);
1555 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1556}
1557unsigned MachineDebugInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
1558 return RecordSource(CompileUnit->getDirectory(),
1559 CompileUnit->getFileName());
1560}
1561
1562/// RecordRegionStart - Indicate the start of a region.
1563///
1564unsigned MachineDebugInfo::RecordRegionStart(Value *V) {
1565 // FIXME - need to be able to handle split scopes because of bb cloning.
1566 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1567 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1568 unsigned ID = NextLabelID();
1569 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1570 return ID;
1571}
1572
1573/// RecordRegionEnd - Indicate the end of a region.
1574///
1575unsigned MachineDebugInfo::RecordRegionEnd(Value *V) {
1576 // FIXME - need to be able to handle split scopes because of bb cloning.
1577 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1578 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1579 unsigned ID = NextLabelID();
1580 Scope->setEndLabelID(ID);
1581 return ID;
1582}
1583
1584/// RecordVariable - Indicate the declaration of a local variable.
1585///
1586void MachineDebugInfo::RecordVariable(Value *V, unsigned FrameIndex) {
1587 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1588 DebugScope *Scope = getOrCreateScope(VD->getContext());
1589 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1590 Scope->AddVariable(DV);
1591}
1592
1593/// getOrCreateScope - Returns the scope associated with the given descriptor.
1594///
1595DebugScope *MachineDebugInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
1596 DebugScope *&Slot = ScopeMap[ScopeDesc];
1597 if (!Slot) {
1598 // FIXME - breaks down when the context is an inlined function.
1599 DebugInfoDesc *ParentDesc = NULL;
1600 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1601 ParentDesc = Block->getContext();
1602 }
1603 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1604 Slot = new DebugScope(Parent, ScopeDesc);
1605 if (Parent) {
1606 Parent->AddScope(Slot);
1607 } else if (RootScope) {
1608 // FIXME - Add inlined function scopes to the root so we can delete
1609 // them later. Long term, handle inlined functions properly.
1610 RootScope->AddScope(Slot);
1611 } else {
1612 // First function is top level function.
1613 RootScope = Slot;
1614 }
1615 }
1616 return Slot;
1617}
1618
Jim Laskey9d4209f2006-11-07 19:33:46 +00001619//===----------------------------------------------------------------------===//
1620/// DebugLabelFolding pass - This pass prunes out redundant debug labels. This
1621/// allows a debug emitter to determine if the range of two labels is empty,
1622/// by seeing if the labels map to the same reduced label.
1623
1624namespace llvm {
1625
1626struct DebugLabelFolder : public MachineFunctionPass {
1627 virtual bool runOnMachineFunction(MachineFunction &MF);
1628 virtual const char *getPassName() const { return "Debug Label Folder"; }
1629};
1630
1631bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
1632 // Get machine debug info.
1633 MachineDebugInfo *MDI = getAnalysisToUpdate<MachineDebugInfo>();
1634 if (!MDI) return false;
1635 // Get target instruction info.
1636 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1637 if (!TII) return false;
1638 // Get target version of the debug label opcode.
1639 unsigned DWARF_LABELOpc = TII->getDWARF_LABELOpcode();
1640 if (!DWARF_LABELOpc) return false;
1641
1642 // Track if change is made.
1643 bool MadeChange = false;
1644 // No prior label to begin.
1645 unsigned PriorLabel = 0;
1646
1647 // Iterate through basic blocks.
1648 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1649 BB != E; ++BB) {
1650 // Iterate through instructions.
1651 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1652 // Is it a debug label.
1653 if ((unsigned)I->getOpcode() == DWARF_LABELOpc) {
1654 // The label ID # is always operand #0, an immediate.
1655 unsigned NextLabel = I->getOperand(0).getImm();
1656
1657 // If there was an immediate prior label.
1658 if (PriorLabel) {
1659 // Remap the current label to prior label.
1660 MDI->RemapLabel(NextLabel, PriorLabel);
1661 // Delete the current label.
1662 I = BB->erase(I);
1663 // Indicate a change has been made.
1664 MadeChange = true;
1665 continue;
1666 } else {
1667 // Start a new round.
1668 PriorLabel = NextLabel;
1669 }
1670 } else {
1671 // No consecutive labels.
1672 PriorLabel = 0;
1673 }
1674
1675 ++I;
1676 }
1677 }
1678
1679 return MadeChange;
1680}
1681
1682FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1683
1684}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001685