blob: 33872fa110429273b4bac03bd9cc4c842438daeb [file] [log] [blame]
Jim Laskey6da18642007-01-26 21:38:26 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
Jim Laskey6af56812006-01-04 13:36:38 +00002//
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
Jim Laskey6da18642007-01-26 21:38:26 +000010#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey6af56812006-01-04 13:36:38 +000011
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 Laskeyc1c47c32007-01-29 23:40:33 +000018#include "llvm/Target/TargetOptions.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000019#include "llvm/DerivedTypes.h"
Jim Laskey86cbdba2006-02-06 15:33:21 +000020#include "llvm/GlobalVariable.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000021#include "llvm/Intrinsics.h"
22#include "llvm/Instructions.h"
23#include "llvm/Module.h"
24#include "llvm/Support/Dwarf.h"
Bill Wendling832171c2006-12-07 20:04:42 +000025#include "llvm/Support/Streams.h"
Jim Laskey6af56812006-01-04 13:36:38 +000026using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000027using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000028
29// Handle the Pass registration stuff necessary to use TargetData's.
30namespace {
Jim Laskey6da18642007-01-26 21:38:26 +000031 RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information");
Jim Laskeyb2efb852006-01-04 22:28:25 +000032}
Devang Patel19974732007-05-03 01:11:54 +000033char MachineModuleInfo::ID = 0;
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;
Reid Spencer47857812006-12-31 05:55:36 +000060 FieldTypes.push_back(Type::Int32Ty);
61 FieldTypes.push_back(Type::Int32Ty);
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++);
Reid Spencer579dca12007-01-12 04:24:46 +0000216 Field = cast<ConstantInt>(C)->getZExtValue();
Jim Laskey86cbdba2006-02-06 15:33:21 +0000217 }
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 Spencer47857812006-12-31 05:55:36 +0000269 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field)));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000270 }
271 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000272 Elements.push_back(ConstantInt::get(Type::Int32Ty, 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 Spencer47857812006-12-31 05:55:36 +0000275 Elements.push_back(ConstantInt::get(Type::Int64Ty, 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 Spencer47857812006-12-31 05:55:36 +0000278 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field)));
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000279 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000280 virtual void Apply(bool &Field) {
Reid Spencer579dca12007-01-12 04:24:46 +0000281 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000282 }
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.
Reid Spencer15f46d62006-12-12 01:17:41 +0000297 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy));
Jim Laskey86cbdba2006-02-06 15:33:21 +0000298 } 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) {
Reid Spencer15f46d62006-12-12 01:17:41 +0000306 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy));
Jim Laskeyce72b172006-02-11 01:01:30 +0000307 } 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);
Reid Spencer15f46d62006-12-12 01:17:41 +0000320 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy);
Jim Laskeyd04c1592006-07-13 15:27:42 +0000321 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");
Reid Spencer15f46d62006-12-12 01:17:41 +0000333 Constant *CAE = ConstantExpr::getBitCast(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) {
Reid Spencer47857812006-12-31 05:55:36 +0000356 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000357 }
358 virtual void Apply(unsigned &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000359 Fields.push_back(Type::Int32Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000360 }
Jim Laskeyf8913f12006-03-01 17:53:02 +0000361 virtual void Apply(int64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000362 Fields.push_back(Type::Int64Ty);
Jim Laskeyf8913f12006-03-01 17:53:02 +0000363 }
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000364 virtual void Apply(uint64_t &Field) {
Reid Spencer47857812006-12-31 05:55:36 +0000365 Fields.push_back(Type::Int64Ty);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000366 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000367 virtual void Apply(bool &Field) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000368 Fields.push_back(Type::Int1Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000369 }
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++);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000431 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty;
Jim Laskey86cbdba2006-02-06 15:33:21 +0000432 }
433 virtual void Apply(std::string &Field) {
434 Constant *C = CI->getOperand(I++);
Jim Laskey26a36872007-01-03 13:46:20 +0000435 IsValid = IsValid &&
436 (!C || isStringValue(C) || C->isNullValue());
Jim Laskey86cbdba2006-02-06 15:33:21 +0000437 }
438 virtual void Apply(DebugInfoDesc *&Field) {
439 // FIXME - Prepare the correct descriptor.
440 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000441 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000442 }
443 virtual void Apply(GlobalVariable *&Field) {
444 Constant *C = CI->getOperand(I++);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000445 IsValid = IsValid && isGlobalVariable(C);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000446 }
Jim Laskey45ccae52006-02-28 20:15:07 +0000447 virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
448 Constant *C = CI->getOperand(I++);
449 IsValid = IsValid && isGlobalVariable(C);
450 if (!IsValid) return;
451
452 GlobalVariable *GV = getGlobalVariable(C);
453 IsValid = IsValid && GV && GV->hasInitializer();
454 if (!IsValid) return;
455
456 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
457 IsValid = IsValid && CA;
458 if (!IsValid) return;
459
460 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
461 IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
462 if (!IsValid) return;
463
464 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
465 VR.Verify(GVE);
466 }
467 }
Jim Laskey86cbdba2006-02-06 15:33:21 +0000468};
469
Jim Laskeyce72b172006-02-11 01:01:30 +0000470
Jim Laskey86cbdba2006-02-06 15:33:21 +0000471//===----------------------------------------------------------------------===//
472
Jim Laskeyed4e5662006-06-14 14:45:39 +0000473/// TagFromGlobal - Returns the tag number from a debug info descriptor
474/// GlobalVariable. Return DIIValid if operand is not an unsigned int.
Jim Laskeyce72b172006-02-11 01:01:30 +0000475unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000476 ConstantInt *C = getUIntOperand(GV, 0);
477 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) :
Jim Laskey7089f452006-06-16 13:14:03 +0000478 (unsigned)DW_TAG_invalid;
Jim Laskeyed4e5662006-06-14 14:45:39 +0000479}
480
481/// VersionFromGlobal - Returns the version number from a debug info
482/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned
483/// int.
484unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000485 ConstantInt *C = getUIntOperand(GV, 0);
486 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) :
Jim Laskeyed4e5662006-06-14 14:45:39 +0000487 (unsigned)DW_TAG_invalid;
Jim Laskeyce72b172006-02-11 01:01:30 +0000488}
489
490/// DescFactory - Create an instance of debug info descriptor based on Tag.
491/// Return NULL if not a recognized Tag.
492DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
493 switch (Tag) {
Jim Laskey9c4447a2006-03-01 20:39:36 +0000494 case DW_TAG_anchor: return new AnchorDesc();
495 case DW_TAG_compile_unit: return new CompileUnitDesc();
496 case DW_TAG_variable: return new GlobalVariableDesc();
497 case DW_TAG_subprogram: return new SubprogramDesc();
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000498 case DW_TAG_lexical_block: return new BlockDesc();
Jim Laskey9c4447a2006-03-01 20:39:36 +0000499 case DW_TAG_base_type: return new BasicTypeDesc();
500 case DW_TAG_typedef:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000501 case DW_TAG_pointer_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000502 case DW_TAG_reference_type:
503 case DW_TAG_const_type:
Jim Laskey9d0ff8e2006-03-15 19:09:58 +0000504 case DW_TAG_volatile_type:
505 case DW_TAG_restrict_type:
Jim Laskey760383e2006-08-21 21:20:18 +0000506 case DW_TAG_member:
507 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000508 case DW_TAG_array_type:
509 case DW_TAG_structure_type:
510 case DW_TAG_union_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000511 case DW_TAG_enumeration_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000512 case DW_TAG_vector_type:
513 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag);
Jim Laskey9c4447a2006-03-01 20:39:36 +0000514 case DW_TAG_subrange_type: return new SubrangeDesc();
Jim Laskey6a3eb012006-03-01 23:52:37 +0000515 case DW_TAG_enumerator: return new EnumeratorDesc();
Jim Laskeyb8509c52006-03-23 18:07:55 +0000516 case DW_TAG_return_variable:
517 case DW_TAG_arg_variable:
518 case DW_TAG_auto_variable: return new VariableDesc(Tag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000519 default: break;
520 }
521 return NULL;
522}
523
524/// getLinkage - get linkage appropriate for this type of descriptor.
525///
526GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
527 return GlobalValue::InternalLinkage;
528}
529
530/// ApplyToFields - Target the vistor to the fields of the descriptor.
531///
532void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
533 Visitor->Apply(Tag);
534}
535
536//===----------------------------------------------------------------------===//
537
Jim Laskey9c4447a2006-03-01 20:39:36 +0000538AnchorDesc::AnchorDesc()
539: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000540, AnchorTag(0)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000541{}
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000542AnchorDesc::AnchorDesc(AnchoredDesc *D)
Jim Laskey9c4447a2006-03-01 20:39:36 +0000543: DebugInfoDesc(DW_TAG_anchor)
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000544, AnchorTag(D->getTag())
Jim Laskey9c4447a2006-03-01 20:39:36 +0000545{}
546
547// Implement isa/cast/dyncast.
548bool AnchorDesc::classof(const DebugInfoDesc *D) {
549 return D->getTag() == DW_TAG_anchor;
550}
551
Jim Laskeyce72b172006-02-11 01:01:30 +0000552/// getLinkage - get linkage appropriate for this type of descriptor.
553///
554GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
555 return GlobalValue::LinkOnceLinkage;
556}
557
558/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
559///
560void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
561 DebugInfoDesc::ApplyToFields(Visitor);
562
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000563 Visitor->Apply(AnchorTag);
Jim Laskeyce72b172006-02-11 01:01:30 +0000564}
565
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000566/// getDescString - Return a string used to compose global names and labels. A
567/// A global variable name needs to be defined for each debug descriptor that is
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000568/// anchored. NOTE: that each global variable named here also needs to be added
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000569/// to the list of names left external in the internalizer.
570/// ExternalNames.insert("llvm.dbg.compile_units");
571/// ExternalNames.insert("llvm.dbg.global_variables");
572/// ExternalNames.insert("llvm.dbg.subprograms");
Jim Laskeyce72b172006-02-11 01:01:30 +0000573const char *AnchorDesc::getDescString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000574 switch (AnchorTag) {
575 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString;
576 case DW_TAG_variable: return GlobalVariableDesc::AnchorString;
577 case DW_TAG_subprogram: return SubprogramDesc::AnchorString;
578 default: break;
579 }
580
581 assert(0 && "Tag does not have a case for anchor string");
582 return "";
Jim Laskeyce72b172006-02-11 01:01:30 +0000583}
584
585/// getTypeString - Return a string used to label this descriptors type.
586///
587const char *AnchorDesc::getTypeString() const {
588 return "llvm.dbg.anchor.type";
589}
590
591#ifndef NDEBUG
592void AnchorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000593 cerr << getDescString() << " "
594 << "Version(" << getVersion() << "), "
595 << "Tag(" << getTag() << "), "
596 << "AnchorTag(" << AnchorTag << ")\n";
Jim Laskeyce72b172006-02-11 01:01:30 +0000597}
598#endif
599
600//===----------------------------------------------------------------------===//
601
602AnchoredDesc::AnchoredDesc(unsigned T)
603: DebugInfoDesc(T)
604, Anchor(NULL)
605{}
606
607/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
608///
609void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
610 DebugInfoDesc::ApplyToFields(Visitor);
611
Jim Laskey7089f452006-06-16 13:14:03 +0000612 Visitor->Apply(Anchor);
Jim Laskeyce72b172006-02-11 01:01:30 +0000613}
614
615//===----------------------------------------------------------------------===//
616
617CompileUnitDesc::CompileUnitDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000618: AnchoredDesc(DW_TAG_compile_unit)
Jim Laskeyce72b172006-02-11 01:01:30 +0000619, Language(0)
620, FileName("")
621, Directory("")
622, Producer("")
623{}
624
Jim Laskey9c4447a2006-03-01 20:39:36 +0000625// Implement isa/cast/dyncast.
626bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
627 return D->getTag() == DW_TAG_compile_unit;
628}
629
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000630/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
631///
632void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +0000633 AnchoredDesc::ApplyToFields(Visitor);
Jim Laskeyca0dc562006-06-19 12:54:15 +0000634
635 // Handle cases out of sync with compiler.
636 if (getVersion() == 0) {
637 unsigned DebugVersion;
638 Visitor->Apply(DebugVersion);
639 }
Jim Laskeyce72b172006-02-11 01:01:30 +0000640
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +0000641 Visitor->Apply(Language);
642 Visitor->Apply(FileName);
643 Visitor->Apply(Directory);
644 Visitor->Apply(Producer);
Jim Laskey86cbdba2006-02-06 15:33:21 +0000645}
646
Jim Laskeyce72b172006-02-11 01:01:30 +0000647/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000648///
Jim Laskeyce72b172006-02-11 01:01:30 +0000649const char *CompileUnitDesc::getDescString() const {
650 return "llvm.dbg.compile_unit";
651}
652
653/// getTypeString - Return a string used to label this descriptors type.
654///
655const char *CompileUnitDesc::getTypeString() const {
656 return "llvm.dbg.compile_unit.type";
657}
658
659/// getAnchorString - Return a string used to label this descriptor's anchor.
660///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000661const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
Jim Laskeyce72b172006-02-11 01:01:30 +0000662const char *CompileUnitDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +0000663 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000664}
665
Jim Laskey86cbdba2006-02-06 15:33:21 +0000666#ifndef NDEBUG
667void CompileUnitDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000668 cerr << getDescString() << " "
669 << "Version(" << getVersion() << "), "
670 << "Tag(" << getTag() << "), "
671 << "Anchor(" << getAnchor() << "), "
672 << "Language(" << Language << "), "
673 << "FileName(\"" << FileName << "\"), "
674 << "Directory(\"" << Directory << "\"), "
675 << "Producer(\"" << Producer << "\")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +0000676}
677#endif
678
679//===----------------------------------------------------------------------===//
680
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000681TypeDesc::TypeDesc(unsigned T)
682: DebugInfoDesc(T)
683, Context(NULL)
684, Name("")
Jim Laskey69906002006-02-24 16:46:40 +0000685, File(NULL)
Jim Laskeyb8509c52006-03-23 18:07:55 +0000686, Line(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000687, Size(0)
Chris Lattner2695de42006-03-09 17:48:46 +0000688, Align(0)
Jim Laskeyf01e5472006-03-03 15:06:57 +0000689, Offset(0)
Jim Laskeye2a78f22006-07-11 15:58:09 +0000690, Flags(0)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000691{}
692
Jim Laskey69906002006-02-24 16:46:40 +0000693/// ApplyToFields - Target the visitor to the fields of the TypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000694///
695void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
696 DebugInfoDesc::ApplyToFields(Visitor);
697
698 Visitor->Apply(Context);
699 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +0000700 Visitor->Apply(File);
Jim Laskey69906002006-02-24 16:46:40 +0000701 Visitor->Apply(Line);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000702 Visitor->Apply(Size);
Chris Lattner2695de42006-03-09 17:48:46 +0000703 Visitor->Apply(Align);
Jim Laskeyf01e5472006-03-03 15:06:57 +0000704 Visitor->Apply(Offset);
Jim Laskeye2a78f22006-07-11 15:58:09 +0000705 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags);
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000706}
707
708/// getDescString - Return a string used to compose global names and labels.
709///
710const char *TypeDesc::getDescString() const {
711 return "llvm.dbg.type";
712}
713
714/// getTypeString - Return a string used to label this descriptor's type.
715///
716const char *TypeDesc::getTypeString() const {
717 return "llvm.dbg.type.type";
718}
719
720#ifndef NDEBUG
721void TypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000722 cerr << getDescString() << " "
723 << "Version(" << getVersion() << "), "
724 << "Tag(" << getTag() << "), "
725 << "Context(" << Context << "), "
726 << "Name(\"" << Name << "\"), "
727 << "File(" << File << "), "
728 << "Line(" << Line << "), "
729 << "Size(" << Size << "), "
730 << "Align(" << Align << "), "
731 << "Offset(" << Offset << "), "
732 << "Flags(" << Flags << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000733}
734#endif
735
736//===----------------------------------------------------------------------===//
737
738BasicTypeDesc::BasicTypeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000739: TypeDesc(DW_TAG_base_type)
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000740, Encoding(0)
741{}
742
Jim Laskey9c4447a2006-03-01 20:39:36 +0000743// Implement isa/cast/dyncast.
744bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
745 return D->getTag() == DW_TAG_base_type;
746}
747
Jim Laskey69906002006-02-24 16:46:40 +0000748/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000749///
750void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
751 TypeDesc::ApplyToFields(Visitor);
752
753 Visitor->Apply(Encoding);
754}
755
Jim Laskeyf8913f12006-03-01 17:53:02 +0000756/// getDescString - Return a string used to compose global names and labels.
757///
758const char *BasicTypeDesc::getDescString() const {
759 return "llvm.dbg.basictype";
760}
761
762/// getTypeString - Return a string used to label this descriptor's type.
763///
764const char *BasicTypeDesc::getTypeString() const {
765 return "llvm.dbg.basictype.type";
766}
767
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000768#ifndef NDEBUG
769void BasicTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000770 cerr << getDescString() << " "
771 << "Version(" << getVersion() << "), "
772 << "Tag(" << getTag() << "), "
773 << "Context(" << getContext() << "), "
774 << "Name(\"" << getName() << "\"), "
775 << "Size(" << getSize() << "), "
776 << "Encoding(" << Encoding << ")\n";
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000777}
778#endif
Jim Laskeyf8913f12006-03-01 17:53:02 +0000779
Jim Laskey434b40b2006-02-23 22:37:30 +0000780//===----------------------------------------------------------------------===//
781
Jim Laskey69906002006-02-24 16:46:40 +0000782DerivedTypeDesc::DerivedTypeDesc(unsigned T)
783: TypeDesc(T)
Jim Laskey434b40b2006-02-23 22:37:30 +0000784, FromType(NULL)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000785{}
Jim Laskey434b40b2006-02-23 22:37:30 +0000786
Jim Laskey9c4447a2006-03-01 20:39:36 +0000787// Implement isa/cast/dyncast.
788bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
789 unsigned T = D->getTag();
790 switch (T) {
791 case DW_TAG_typedef:
792 case DW_TAG_pointer_type:
793 case DW_TAG_reference_type:
794 case DW_TAG_const_type:
795 case DW_TAG_volatile_type:
796 case DW_TAG_restrict_type:
Jim Laskeyf01e5472006-03-03 15:06:57 +0000797 case DW_TAG_member:
Jim Laskey760383e2006-08-21 21:20:18 +0000798 case DW_TAG_inheritance:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000799 return true;
800 default: break;
801 }
802 return false;
803}
804
Jim Laskey69906002006-02-24 16:46:40 +0000805/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
Jim Laskey434b40b2006-02-23 22:37:30 +0000806///
Jim Laskey69906002006-02-24 16:46:40 +0000807void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey434b40b2006-02-23 22:37:30 +0000808 TypeDesc::ApplyToFields(Visitor);
809
Jim Laskey7089f452006-06-16 13:14:03 +0000810 Visitor->Apply(FromType);
Jim Laskey434b40b2006-02-23 22:37:30 +0000811}
812
Jim Laskeyf8913f12006-03-01 17:53:02 +0000813/// getDescString - Return a string used to compose global names and labels.
814///
815const char *DerivedTypeDesc::getDescString() const {
816 return "llvm.dbg.derivedtype";
817}
818
819/// getTypeString - Return a string used to label this descriptor's type.
820///
821const char *DerivedTypeDesc::getTypeString() const {
822 return "llvm.dbg.derivedtype.type";
823}
824
Jim Laskey434b40b2006-02-23 22:37:30 +0000825#ifndef NDEBUG
Jim Laskey69906002006-02-24 16:46:40 +0000826void DerivedTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000827 cerr << getDescString() << " "
828 << "Version(" << getVersion() << "), "
829 << "Tag(" << getTag() << "), "
830 << "Context(" << getContext() << "), "
831 << "Name(\"" << getName() << "\"), "
832 << "Size(" << getSize() << "), "
833 << "File(" << getFile() << "), "
834 << "Line(" << getLine() << "), "
835 << "FromType(" << FromType << ")\n";
Jim Laskey434b40b2006-02-23 22:37:30 +0000836}
837#endif
Jim Laskeyf4afdd92006-02-23 16:58:18 +0000838
839//===----------------------------------------------------------------------===//
840
Jim Laskeyf8913f12006-03-01 17:53:02 +0000841CompositeTypeDesc::CompositeTypeDesc(unsigned T)
842: DerivedTypeDesc(T)
843, Elements()
844{}
845
Jim Laskey9c4447a2006-03-01 20:39:36 +0000846// Implement isa/cast/dyncast.
847bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
848 unsigned T = D->getTag();
849 switch (T) {
850 case DW_TAG_array_type:
851 case DW_TAG_structure_type:
852 case DW_TAG_union_type:
853 case DW_TAG_enumeration_type:
Jim Laskey7089f452006-06-16 13:14:03 +0000854 case DW_TAG_vector_type:
Jim Laskey650f6092006-06-20 19:41:06 +0000855 case DW_TAG_subroutine_type:
Jim Laskey9c4447a2006-03-01 20:39:36 +0000856 return true;
857 default: break;
858 }
859 return false;
860}
861
Jim Laskeyf8913f12006-03-01 17:53:02 +0000862/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
863///
864void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskey7089f452006-06-16 13:14:03 +0000865 DerivedTypeDesc::ApplyToFields(Visitor);
866
Jim Laskeyf8913f12006-03-01 17:53:02 +0000867 Visitor->Apply(Elements);
868}
869
870/// getDescString - Return a string used to compose global names and labels.
871///
872const char *CompositeTypeDesc::getDescString() const {
873 return "llvm.dbg.compositetype";
874}
875
876/// getTypeString - Return a string used to label this descriptor's type.
877///
878const char *CompositeTypeDesc::getTypeString() const {
879 return "llvm.dbg.compositetype.type";
880}
881
882#ifndef NDEBUG
883void CompositeTypeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000884 cerr << getDescString() << " "
885 << "Version(" << getVersion() << "), "
886 << "Tag(" << getTag() << "), "
887 << "Context(" << getContext() << "), "
888 << "Name(\"" << getName() << "\"), "
889 << "Size(" << getSize() << "), "
890 << "File(" << getFile() << "), "
891 << "Line(" << getLine() << "), "
892 << "FromType(" << getFromType() << "), "
893 << "Elements.size(" << Elements.size() << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000894}
895#endif
896
897//===----------------------------------------------------------------------===//
898
899SubrangeDesc::SubrangeDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +0000900: DebugInfoDesc(DW_TAG_subrange_type)
Jim Laskeyf8913f12006-03-01 17:53:02 +0000901, Lo(0)
902, Hi(0)
903{}
904
Jim Laskey9c4447a2006-03-01 20:39:36 +0000905// Implement isa/cast/dyncast.
906bool SubrangeDesc::classof(const DebugInfoDesc *D) {
907 return D->getTag() == DW_TAG_subrange_type;
908}
909
Jim Laskeyf8913f12006-03-01 17:53:02 +0000910/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
911///
912void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
913 DebugInfoDesc::ApplyToFields(Visitor);
914
915 Visitor->Apply(Lo);
916 Visitor->Apply(Hi);
917}
918
919/// getDescString - Return a string used to compose global names and labels.
920///
921const char *SubrangeDesc::getDescString() const {
922 return "llvm.dbg.subrange";
923}
924
925/// getTypeString - Return a string used to label this descriptor's type.
926///
927const char *SubrangeDesc::getTypeString() const {
928 return "llvm.dbg.subrange.type";
929}
930
931#ifndef NDEBUG
932void SubrangeDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000933 cerr << getDescString() << " "
934 << "Version(" << getVersion() << "), "
935 << "Tag(" << getTag() << "), "
936 << "Lo(" << Lo << "), "
937 << "Hi(" << Hi << ")\n";
Jim Laskeyf8913f12006-03-01 17:53:02 +0000938}
939#endif
940
941//===----------------------------------------------------------------------===//
942
Jim Laskey6a3eb012006-03-01 23:52:37 +0000943EnumeratorDesc::EnumeratorDesc()
944: DebugInfoDesc(DW_TAG_enumerator)
945, Name("")
946, Value(0)
947{}
948
949// Implement isa/cast/dyncast.
950bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
951 return D->getTag() == DW_TAG_enumerator;
952}
953
954/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
955///
956void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
957 DebugInfoDesc::ApplyToFields(Visitor);
958
959 Visitor->Apply(Name);
960 Visitor->Apply(Value);
961}
962
963/// getDescString - Return a string used to compose global names and labels.
964///
965const char *EnumeratorDesc::getDescString() const {
966 return "llvm.dbg.enumerator";
967}
968
969/// getTypeString - Return a string used to label this descriptor's type.
970///
971const char *EnumeratorDesc::getTypeString() const {
972 return "llvm.dbg.enumerator.type";
973}
974
975#ifndef NDEBUG
976void EnumeratorDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +0000977 cerr << getDescString() << " "
978 << "Version(" << getVersion() << "), "
979 << "Tag(" << getTag() << "), "
980 << "Name(" << Name << "), "
981 << "Value(" << Value << ")\n";
Jim Laskey6a3eb012006-03-01 23:52:37 +0000982}
983#endif
984
985//===----------------------------------------------------------------------===//
986
Jim Laskeyb8509c52006-03-23 18:07:55 +0000987VariableDesc::VariableDesc(unsigned T)
988: DebugInfoDesc(T)
989, Context(NULL)
990, Name("")
991, File(NULL)
992, Line(0)
993, TyDesc(0)
994{}
995
996// Implement isa/cast/dyncast.
997bool VariableDesc::classof(const DebugInfoDesc *D) {
998 unsigned T = D->getTag();
999 switch (T) {
1000 case DW_TAG_auto_variable:
1001 case DW_TAG_arg_variable:
1002 case DW_TAG_return_variable:
1003 return true;
1004 default: break;
1005 }
1006 return false;
1007}
1008
1009/// ApplyToFields - Target the visitor to the fields of the VariableDesc.
1010///
1011void VariableDesc::ApplyToFields(DIVisitor *Visitor) {
1012 DebugInfoDesc::ApplyToFields(Visitor);
1013
1014 Visitor->Apply(Context);
1015 Visitor->Apply(Name);
Jim Laskey7089f452006-06-16 13:14:03 +00001016 Visitor->Apply(File);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001017 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001018 Visitor->Apply(TyDesc);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001019}
1020
1021/// getDescString - Return a string used to compose global names and labels.
1022///
1023const char *VariableDesc::getDescString() const {
1024 return "llvm.dbg.variable";
1025}
1026
1027/// getTypeString - Return a string used to label this descriptor's type.
1028///
1029const char *VariableDesc::getTypeString() const {
1030 return "llvm.dbg.variable.type";
1031}
1032
1033#ifndef NDEBUG
1034void VariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001035 cerr << getDescString() << " "
1036 << "Version(" << getVersion() << "), "
1037 << "Tag(" << getTag() << "), "
1038 << "Context(" << Context << "), "
1039 << "Name(\"" << Name << "\"), "
1040 << "File(" << File << "), "
1041 << "Line(" << Line << "), "
1042 << "TyDesc(" << TyDesc << ")\n";
Jim Laskeyb8509c52006-03-23 18:07:55 +00001043}
1044#endif
1045
1046//===----------------------------------------------------------------------===//
1047
Jim Laskeyce72b172006-02-11 01:01:30 +00001048GlobalDesc::GlobalDesc(unsigned T)
1049: AnchoredDesc(T)
1050, Context(0)
1051, Name("")
Jim Laskey2172f962006-11-30 14:35:45 +00001052, FullName("")
1053, LinkageName("")
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001054, File(NULL)
1055, Line(0)
Jim Laskeyce72b172006-02-11 01:01:30 +00001056, TyDesc(NULL)
1057, IsStatic(false)
1058, IsDefinition(false)
1059{}
1060
1061/// ApplyToFields - Target the visitor to the fields of the global.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001062///
Jim Laskeyce72b172006-02-11 01:01:30 +00001063void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
1064 AnchoredDesc::ApplyToFields(Visitor);
1065
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001066 Visitor->Apply(Context);
1067 Visitor->Apply(Name);
Jim Laskey2172f962006-11-30 14:35:45 +00001068 Visitor->Apply(FullName);
1069 Visitor->Apply(LinkageName);
Jim Laskey7089f452006-06-16 13:14:03 +00001070 Visitor->Apply(File);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001071 Visitor->Apply(Line);
Jim Laskey7089f452006-06-16 13:14:03 +00001072 Visitor->Apply(TyDesc);
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001073 Visitor->Apply(IsStatic);
1074 Visitor->Apply(IsDefinition);
Jim Laskeyce72b172006-02-11 01:01:30 +00001075}
1076
1077//===----------------------------------------------------------------------===//
1078
1079GlobalVariableDesc::GlobalVariableDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001080: GlobalDesc(DW_TAG_variable)
Jim Laskeyce72b172006-02-11 01:01:30 +00001081, Global(NULL)
1082{}
1083
Jim Laskey9c4447a2006-03-01 20:39:36 +00001084// Implement isa/cast/dyncast.
1085bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
1086 return D->getTag() == DW_TAG_variable;
1087}
1088
Jim Laskeyce72b172006-02-11 01:01:30 +00001089/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
1090///
1091void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
1092 GlobalDesc::ApplyToFields(Visitor);
1093
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001094 Visitor->Apply(Global);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001095}
1096
Jim Laskeyce72b172006-02-11 01:01:30 +00001097/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001098///
Jim Laskeyce72b172006-02-11 01:01:30 +00001099const char *GlobalVariableDesc::getDescString() const {
1100 return "llvm.dbg.global_variable";
1101}
1102
1103/// getTypeString - Return a string used to label this descriptors type.
1104///
1105const char *GlobalVariableDesc::getTypeString() const {
1106 return "llvm.dbg.global_variable.type";
1107}
1108
1109/// getAnchorString - Return a string used to label this descriptor's anchor.
1110///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001111const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
Jim Laskeyce72b172006-02-11 01:01:30 +00001112const char *GlobalVariableDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001113 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001114}
1115
Jim Laskey86cbdba2006-02-06 15:33:21 +00001116#ifndef NDEBUG
1117void GlobalVariableDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001118 cerr << getDescString() << " "
1119 << "Version(" << getVersion() << "), "
1120 << "Tag(" << getTag() << "), "
1121 << "Anchor(" << getAnchor() << "), "
1122 << "Name(\"" << getName() << "\"), "
1123 << "FullName(\"" << getFullName() << "\"), "
1124 << "LinkageName(\"" << getLinkageName() << "\"), "
1125 << "File(" << getFile() << "),"
1126 << "Line(" << getLine() << "),"
1127 << "Type(" << getType() << "), "
1128 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1129 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1130 << "Global(" << Global << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001131}
1132#endif
1133
1134//===----------------------------------------------------------------------===//
1135
Jim Laskeyce72b172006-02-11 01:01:30 +00001136SubprogramDesc::SubprogramDesc()
Jim Laskey9c4447a2006-03-01 20:39:36 +00001137: GlobalDesc(DW_TAG_subprogram)
Jim Laskeyce72b172006-02-11 01:01:30 +00001138{}
1139
Jim Laskey9c4447a2006-03-01 20:39:36 +00001140// Implement isa/cast/dyncast.
1141bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1142 return D->getTag() == DW_TAG_subprogram;
1143}
1144
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001145/// ApplyToFields - Target the visitor to the fields of the
Jim Laskey86cbdba2006-02-06 15:33:21 +00001146/// SubprogramDesc.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001147void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001148 GlobalDesc::ApplyToFields(Visitor);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001149}
1150
Jim Laskeyce72b172006-02-11 01:01:30 +00001151/// getDescString - Return a string used to compose global names and labels.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001152///
Jim Laskeyce72b172006-02-11 01:01:30 +00001153const char *SubprogramDesc::getDescString() const {
1154 return "llvm.dbg.subprogram";
1155}
1156
1157/// getTypeString - Return a string used to label this descriptors type.
1158///
1159const char *SubprogramDesc::getTypeString() const {
1160 return "llvm.dbg.subprogram.type";
1161}
1162
1163/// getAnchorString - Return a string used to label this descriptor's anchor.
1164///
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001165const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
Jim Laskeyce72b172006-02-11 01:01:30 +00001166const char *SubprogramDesc::getAnchorString() const {
Jim Laskeye8c3e3b2006-03-07 20:53:47 +00001167 return AnchorString;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001168}
1169
Jim Laskey86cbdba2006-02-06 15:33:21 +00001170#ifndef NDEBUG
1171void SubprogramDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001172 cerr << getDescString() << " "
1173 << "Version(" << getVersion() << "), "
1174 << "Tag(" << getTag() << "), "
1175 << "Anchor(" << getAnchor() << "), "
1176 << "Name(\"" << getName() << "\"), "
1177 << "FullName(\"" << getFullName() << "\"), "
1178 << "LinkageName(\"" << getLinkageName() << "\"), "
1179 << "File(" << getFile() << "),"
1180 << "Line(" << getLine() << "),"
1181 << "Type(" << getType() << "), "
1182 << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1183 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
Jim Laskey86cbdba2006-02-06 15:33:21 +00001184}
1185#endif
1186
Jim Laskey45ccae52006-02-28 20:15:07 +00001187//===----------------------------------------------------------------------===//
1188
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001189BlockDesc::BlockDesc()
1190: DebugInfoDesc(DW_TAG_lexical_block)
Jim Laskeyb8509c52006-03-23 18:07:55 +00001191, Context(NULL)
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001192{}
1193
1194// Implement isa/cast/dyncast.
1195bool BlockDesc::classof(const DebugInfoDesc *D) {
1196 return D->getTag() == DW_TAG_lexical_block;
1197}
1198
1199/// ApplyToFields - Target the visitor to the fields of the BlockDesc.
1200///
1201void BlockDesc::ApplyToFields(DIVisitor *Visitor) {
1202 DebugInfoDesc::ApplyToFields(Visitor);
1203
Jim Laskeyb8509c52006-03-23 18:07:55 +00001204 Visitor->Apply(Context);
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001205}
1206
1207/// getDescString - Return a string used to compose global names and labels.
1208///
1209const char *BlockDesc::getDescString() const {
1210 return "llvm.dbg.block";
1211}
1212
1213/// getTypeString - Return a string used to label this descriptors type.
1214///
1215const char *BlockDesc::getTypeString() const {
1216 return "llvm.dbg.block.type";
1217}
1218
1219#ifndef NDEBUG
1220void BlockDesc::dump() {
Bill Wendling832171c2006-12-07 20:04:42 +00001221 cerr << getDescString() << " "
1222 << "Version(" << getVersion() << "), "
1223 << "Tag(" << getTag() << "),"
1224 << "Context(" << Context << ")\n";
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001225}
1226#endif
1227
1228//===----------------------------------------------------------------------===//
1229
Jim Laskey86cbdba2006-02-06 15:33:21 +00001230DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001231 return Deserialize(getGlobalVariable(V));
Jim Laskey86cbdba2006-02-06 15:33:21 +00001232}
1233DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001234 // Handle NULL.
1235 if (!GV) return NULL;
1236
Jim Laskey86cbdba2006-02-06 15:33:21 +00001237 // Check to see if it has been already deserialized.
1238 DebugInfoDesc *&Slot = GlobalDescs[GV];
1239 if (Slot) return Slot;
1240
1241 // Get the Tag from the global.
1242 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1243
Jim Laskey86cbdba2006-02-06 15:33:21 +00001244 // Create an empty instance of the correct sort.
1245 Slot = DebugInfoDesc::DescFactory(Tag);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001246
Jim Laskey21407982006-03-14 18:37:57 +00001247 // If not a user defined descriptor.
1248 if (Slot) {
1249 // Deserialize the fields.
1250 DIDeserializeVisitor DRAM(*this, GV);
1251 DRAM.ApplyToFields(Slot);
1252 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001253
1254 return Slot;
1255}
1256
1257//===----------------------------------------------------------------------===//
1258
1259/// getStrPtrType - Return a "sbyte *" type.
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001260///
Jim Laskey86cbdba2006-02-06 15:33:21 +00001261const PointerType *DISerializer::getStrPtrType() {
1262 // If not already defined.
1263 if (!StrPtrTy) {
1264 // Construct the pointer to signed bytes.
Reid Spencer47857812006-12-31 05:55:36 +00001265 StrPtrTy = PointerType::get(Type::Int8Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001266 }
1267
1268 return StrPtrTy;
1269}
1270
1271/// getEmptyStructPtrType - Return a "{ }*" type.
1272///
1273const PointerType *DISerializer::getEmptyStructPtrType() {
1274 // If not already defined.
1275 if (!EmptyStructPtrTy) {
1276 // Construct the empty structure type.
1277 const StructType *EmptyStructTy =
1278 StructType::get(std::vector<const Type*>());
1279 // Construct the pointer to empty structure type.
1280 EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1281 }
1282
1283 return EmptyStructPtrTy;
1284}
1285
1286/// getTagType - Return the type describing the specified descriptor (via tag.)
1287///
1288const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1289 // Attempt to get the previously defined type.
1290 StructType *&Ty = TagTypes[DD->getTag()];
1291
1292 // If not already defined.
1293 if (!Ty) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001294 // Set up fields vector.
1295 std::vector<const Type*> Fields;
Jim Laskeyce72b172006-02-11 01:01:30 +00001296 // Get types of fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001297 DIGetTypesVisitor GTAM(*this, Fields);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001298 GTAM.ApplyToFields(DD);
1299
1300 // Construct structured type.
1301 Ty = StructType::get(Fields);
1302
Jim Laskey86cbdba2006-02-06 15:33:21 +00001303 // Register type name with module.
Jim Laskeyce72b172006-02-11 01:01:30 +00001304 M->addTypeName(DD->getTypeString(), Ty);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001305 }
1306
1307 return Ty;
1308}
1309
1310/// getString - Construct the string as constant string global.
1311///
Jim Laskeyce72b172006-02-11 01:01:30 +00001312Constant *DISerializer::getString(const std::string &String) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001313 // Check string cache for previous edition.
Jim Laskeyce72b172006-02-11 01:01:30 +00001314 Constant *&Slot = StringCache[String];
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001315 // Return Constant if previously defined.
Jim Laskey86cbdba2006-02-06 15:33:21 +00001316 if (Slot) return Slot;
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001317 // If empty string then use a sbyte* null instead.
1318 if (String.empty()) {
1319 Slot = ConstantPointerNull::get(getStrPtrType());
1320 } else {
1321 // Construct string as an llvm constant.
1322 Constant *ConstStr = ConstantArray::get(String);
1323 // Otherwise create and return a new string global.
1324 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1325 GlobalVariable::InternalLinkage,
1326 ConstStr, "str", M);
1327 StrGV->setSection("llvm.metadata");
1328 // Convert to generic string pointer.
Reid Spencer15f46d62006-12-12 01:17:41 +00001329 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
Jim Laskey9d0ff8e2006-03-15 19:09:58 +00001330 }
Jim Laskeyce72b172006-02-11 01:01:30 +00001331 return Slot;
1332
Jim Laskey86cbdba2006-02-06 15:33:21 +00001333}
1334
1335/// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1336/// so that it can be serialized to a .bc or .ll file.
1337GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1338 // Check if the DebugInfoDesc is already in the map.
1339 GlobalVariable *&Slot = DescGlobals[DD];
1340
1341 // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1342 if (Slot) return Slot;
1343
Jim Laskey86cbdba2006-02-06 15:33:21 +00001344 // Get the type associated with the Tag.
1345 const StructType *Ty = getTagType(DD);
1346
1347 // Create the GlobalVariable early to prevent infinite recursion.
Jim Laskeyce72b172006-02-11 01:01:30 +00001348 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1349 NULL, DD->getDescString(), M);
Jim Laskey78098112006-03-07 22:00:35 +00001350 GV->setSection("llvm.metadata");
Jim Laskey86cbdba2006-02-06 15:33:21 +00001351
1352 // Insert new GlobalVariable in DescGlobals map.
1353 Slot = GV;
1354
1355 // Set up elements vector
1356 std::vector<Constant*> Elements;
Jim Laskeyce72b172006-02-11 01:01:30 +00001357 // Add fields.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001358 DISerializeVisitor SRAM(*this, Elements);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001359 SRAM.ApplyToFields(DD);
1360
1361 // Set the globals initializer.
1362 GV->setInitializer(ConstantStruct::get(Ty, Elements));
1363
1364 return GV;
1365}
1366
1367//===----------------------------------------------------------------------===//
1368
Jim Laskey86cbdba2006-02-06 15:33:21 +00001369/// Verify - Return true if the GlobalVariable appears to be a valid
1370/// serialization of a DebugInfoDesc.
Jim Laskeyce72b172006-02-11 01:01:30 +00001371bool DIVerifier::Verify(Value *V) {
Jim Laskeyaaa80eb2006-03-28 01:30:18 +00001372 return !V || Verify(getGlobalVariable(V));
Jim Laskeyce72b172006-02-11 01:01:30 +00001373}
Jim Laskey86cbdba2006-02-06 15:33:21 +00001374bool DIVerifier::Verify(GlobalVariable *GV) {
Jim Laskey98e04102006-03-26 22:45:20 +00001375 // NULLs are valid.
1376 if (!GV) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001377
Jim Laskey98e04102006-03-26 22:45:20 +00001378 // Check prior validity.
1379 unsigned &ValiditySlot = Validity[GV];
1380
1381 // If visited before then use old state.
1382 if (ValiditySlot) return ValiditySlot == Valid;
1383
1384 // Assume validity for the time being (recursion.)
1385 ValiditySlot = Valid;
Jim Laskeya8299de2006-03-27 01:51:47 +00001386
1387 // Make sure the global is internal or link once (anchor.)
1388 if (GV->getLinkage() != GlobalValue::InternalLinkage &&
1389 GV->getLinkage() != GlobalValue::LinkOnceLinkage) {
1390 ValiditySlot = Invalid;
1391 return false;
1392 }
Jim Laskey98e04102006-03-26 22:45:20 +00001393
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001394 // Get the Tag.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001395 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
Jim Laskeyb8509c52006-03-23 18:07:55 +00001396
1397 // Check for user defined descriptors.
Jim Laskey2bbff6d2006-11-30 18:29:23 +00001398 if (Tag == DW_TAG_invalid) {
1399 ValiditySlot = Valid;
1400 return true;
1401 }
1402
1403 // Get the Version.
1404 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV);
1405
1406 // Check for version mismatch.
1407 if (Version != LLVMDebugVersion) {
1408 ValiditySlot = Invalid;
1409 return false;
1410 }
Jim Laskey86cbdba2006-02-06 15:33:21 +00001411
Jim Laskey86cbdba2006-02-06 15:33:21 +00001412 // Construct an empty DebugInfoDesc.
1413 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
Jim Laskey21407982006-03-14 18:37:57 +00001414
1415 // Allow for user defined descriptors.
1416 if (!DD) return true;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001417
1418 // Get the initializer constant.
1419 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1420
1421 // Get the operand count.
1422 unsigned N = CI->getNumOperands();
1423
1424 // Get the field count.
Jim Laskey98e04102006-03-26 22:45:20 +00001425 unsigned &CountSlot = Counts[Tag];
1426 if (!CountSlot) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001427 // Check the operand count to the field count
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001428 DICountVisitor CTAM;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001429 CTAM.ApplyToFields(DD);
Jim Laskey98e04102006-03-26 22:45:20 +00001430 CountSlot = CTAM.getCount();
Jim Laskey86cbdba2006-02-06 15:33:21 +00001431 }
1432
Jim Laskey21407982006-03-14 18:37:57 +00001433 // Field count must be at most equal operand count.
Jim Laskey98e04102006-03-26 22:45:20 +00001434 if (CountSlot > N) {
Jim Laskey86cbdba2006-02-06 15:33:21 +00001435 delete DD;
Jim Laskey98e04102006-03-26 22:45:20 +00001436 ValiditySlot = Invalid;
Jim Laskey86cbdba2006-02-06 15:33:21 +00001437 return false;
1438 }
1439
1440 // Check each field for valid type.
Jim Laskeyc2f0c8d2006-02-06 19:12:02 +00001441 DIVerifyVisitor VRAM(*this, GV);
Jim Laskey86cbdba2006-02-06 15:33:21 +00001442 VRAM.ApplyToFields(DD);
1443
1444 // Release empty DebugInfoDesc.
1445 delete DD;
1446
Jim Laskey98e04102006-03-26 22:45:20 +00001447 // If fields are not valid.
1448 if (!VRAM.isValid()) {
1449 ValiditySlot = Invalid;
1450 return false;
1451 }
1452
1453 return true;
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001454}
1455
1456//===----------------------------------------------------------------------===//
1457
Jim Laskeyb8509c52006-03-23 18:07:55 +00001458DebugScope::~DebugScope() {
1459 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1460 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1461}
1462
1463//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001464
Jim Laskey6da18642007-01-26 21:38:26 +00001465MachineModuleInfo::MachineModuleInfo()
Devang Patel794fd752007-05-01 21:15:47 +00001466: ImmutablePass((intptr_t)&ID)
1467, DR()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001468, VR()
Jim Laskey86cbdba2006-02-06 15:33:21 +00001469, CompileUnits()
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001470, Directories()
1471, SourceFiles()
1472, Lines()
Jim Laskey9d4209f2006-11-07 19:33:46 +00001473, LabelIDList()
Jim Laskeyb8509c52006-03-23 18:07:55 +00001474, ScopeMap()
1475, RootScope(NULL)
Jim Laskey41886992006-04-07 16:34:46 +00001476, FrameMoves()
Jim Laskey59667fe2007-02-21 22:38:31 +00001477, LandingPads()
Jim Laskey41886992006-04-07 16:34:46 +00001478{}
Jim Laskey6da18642007-01-26 21:38:26 +00001479MachineModuleInfo::~MachineModuleInfo() {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001480
1481}
1482
Jim Laskey6da18642007-01-26 21:38:26 +00001483/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001484///
Jim Laskey6da18642007-01-26 21:38:26 +00001485bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001486 return false;
Jim Laskey6af56812006-01-04 13:36:38 +00001487}
1488
Jim Laskey6da18642007-01-26 21:38:26 +00001489/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +00001490///
Jim Laskey6da18642007-01-26 21:38:26 +00001491bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +00001492 return false;
1493}
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001494
Jim Laskey6da18642007-01-26 21:38:26 +00001495/// BeginFunction - Begin gathering function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001496///
Jim Laskey6da18642007-01-26 21:38:26 +00001497void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
Jim Laskey41886992006-04-07 16:34:46 +00001498 // Coming soon.
1499}
1500
Jim Laskey6da18642007-01-26 21:38:26 +00001501/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +00001502///
Jim Laskey6da18642007-01-26 21:38:26 +00001503void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +00001504 // Clean up scope information.
1505 if (RootScope) {
1506 delete RootScope;
1507 ScopeMap.clear();
1508 RootScope = NULL;
1509 }
1510
Jim Laskeyb82313f2007-02-01 16:31:34 +00001511 // Clean up line info.
1512 Lines.clear();
1513
Jim Laskey41886992006-04-07 16:34:46 +00001514 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +00001515 FrameMoves.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +00001516
1517 // Clean up exception info.
1518 LandingPads.clear();
1519 TypeInfos.clear();
Jim Laskey41886992006-04-07 16:34:46 +00001520}
1521
Jim Laskeyd96185a2006-02-13 12:50:39 +00001522/// getDescFor - Convert a Value to a debug information descriptor.
Jim Laskeyce72b172006-02-11 01:01:30 +00001523///
Jim Laskeyd96185a2006-02-13 12:50:39 +00001524// FIXME - use new Value type when available.
Jim Laskey6da18642007-01-26 21:38:26 +00001525DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001526 return DR.Deserialize(V);
1527}
1528
1529/// Verify - Verify that a Value is debug information descriptor.
1530///
Jim Laskey6da18642007-01-26 21:38:26 +00001531bool MachineModuleInfo::Verify(Value *V) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001532 return VR.Verify(V);
1533}
1534
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001535/// AnalyzeModule - Scan the module for global debug information.
1536///
Jim Laskey6da18642007-01-26 21:38:26 +00001537void MachineModuleInfo::AnalyzeModule(Module &M) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001538 SetupCompileUnits(M);
1539}
1540
Jim Laskeyc1c47c32007-01-29 23:40:33 +00001541/// needsFrameInfo - Returns true if we need to gather callee-saved register
1542/// move info for the frame.
1543bool MachineModuleInfo::needsFrameInfo() const {
1544 return hasDebugInfo() || ExceptionHandling;
1545}
1546
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001547/// SetupCompileUnits - Set up the unique vector of compile units.
1548///
Jim Laskey6da18642007-01-26 21:38:26 +00001549void MachineModuleInfo::SetupCompileUnits(Module &M) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001550 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001551
Jim Laskey0420f2a2006-02-22 19:02:11 +00001552 for (unsigned i = 0, N = CU.size(); i < N; i++) {
1553 CompileUnits.insert(CU[i]);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001554 }
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001555}
1556
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001557/// getCompileUnits - Return a vector of debug compile units.
1558///
Jim Laskey6da18642007-01-26 21:38:26 +00001559const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
Jim Laskey6e87c0e2006-01-26 21:22:49 +00001560 return CompileUnits;
1561}
1562
Jim Laskey0420f2a2006-02-22 19:02:11 +00001563/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1564/// named GlobalVariable.
1565std::vector<GlobalVariable*>
Jim Laskey6da18642007-01-26 21:38:26 +00001566MachineModuleInfo::getGlobalVariablesUsing(Module &M,
1567 const std::string &RootName) {
Jim Laskey0420f2a2006-02-22 19:02:11 +00001568 return ::getGlobalVariablesUsing(M, RootName);
Jim Laskeyb3e789a2006-01-26 20:21:46 +00001569}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001570
1571/// RecordLabel - Records location information and associates it with a
1572/// debug label. Returns a unique label ID used to generate a label and
1573/// provide correspondence to the source line list.
Jim Laskey6da18642007-01-26 21:38:26 +00001574unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
Jim Laskeyb8509c52006-03-23 18:07:55 +00001575 unsigned Source) {
1576 unsigned ID = NextLabelID();
Chris Lattner8466b212006-10-17 22:06:46 +00001577 Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
Jim Laskeyb8509c52006-03-23 18:07:55 +00001578 return ID;
1579}
1580
1581/// RecordSource - Register a source file with debug info. Returns an source
1582/// ID.
Jim Laskey6da18642007-01-26 21:38:26 +00001583unsigned MachineModuleInfo::RecordSource(const std::string &Directory,
1584 const std::string &Source) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001585 unsigned DirectoryID = Directories.insert(Directory);
1586 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
1587}
Jim Laskey6da18642007-01-26 21:38:26 +00001588unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001589 return RecordSource(CompileUnit->getDirectory(),
1590 CompileUnit->getFileName());
1591}
1592
1593/// RecordRegionStart - Indicate the start of a region.
1594///
Jim Laskey6da18642007-01-26 21:38:26 +00001595unsigned MachineModuleInfo::RecordRegionStart(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001596 // FIXME - need to be able to handle split scopes because of bb cloning.
1597 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1598 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1599 unsigned ID = NextLabelID();
1600 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
1601 return ID;
1602}
1603
1604/// RecordRegionEnd - Indicate the end of a region.
1605///
Jim Laskey6da18642007-01-26 21:38:26 +00001606unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001607 // FIXME - need to be able to handle split scopes because of bb cloning.
1608 DebugInfoDesc *ScopeDesc = DR.Deserialize(V);
1609 DebugScope *Scope = getOrCreateScope(ScopeDesc);
1610 unsigned ID = NextLabelID();
1611 Scope->setEndLabelID(ID);
1612 return ID;
1613}
1614
1615/// RecordVariable - Indicate the declaration of a local variable.
1616///
Jim Laskey6da18642007-01-26 21:38:26 +00001617void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001618 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
1619 DebugScope *Scope = getOrCreateScope(VD->getContext());
1620 DebugVariable *DV = new DebugVariable(VD, FrameIndex);
1621 Scope->AddVariable(DV);
1622}
1623
1624/// getOrCreateScope - Returns the scope associated with the given descriptor.
1625///
Jim Laskey6da18642007-01-26 21:38:26 +00001626DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) {
Jim Laskeyb8509c52006-03-23 18:07:55 +00001627 DebugScope *&Slot = ScopeMap[ScopeDesc];
1628 if (!Slot) {
1629 // FIXME - breaks down when the context is an inlined function.
1630 DebugInfoDesc *ParentDesc = NULL;
1631 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) {
1632 ParentDesc = Block->getContext();
1633 }
1634 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL;
1635 Slot = new DebugScope(Parent, ScopeDesc);
1636 if (Parent) {
1637 Parent->AddScope(Slot);
1638 } else if (RootScope) {
1639 // FIXME - Add inlined function scopes to the root so we can delete
1640 // them later. Long term, handle inlined functions properly.
1641 RootScope->AddScope(Slot);
1642 } else {
1643 // First function is top level function.
1644 RootScope = Slot;
1645 }
1646 }
1647 return Slot;
1648}
1649
Jim Laskey59667fe2007-02-21 22:38:31 +00001650//===-EH-------------------------------------------------------------------===//
1651
1652/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1653/// specified MachineBasicBlock.
1654LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
1655 (MachineBasicBlock *LandingPad) {
1656 unsigned N = LandingPads.size();
1657 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +00001658 LandingPadInfo &LP = LandingPads[i];
1659 if (LP.LandingPadBlock == LandingPad)
1660 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +00001661 }
1662
1663 LandingPads.push_back(LandingPadInfo(LandingPad));
1664 return LandingPads[N];
1665}
1666
1667/// addInvoke - Provide the begin and end labels of an invoke style call and
1668/// associate it with a try landing pad block.
1669void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
1670 unsigned BeginLabel, unsigned EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +00001671 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001672 LP.BeginLabels.push_back(BeginLabel);
1673 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +00001674}
1675
1676/// addLandingPad - Provide the label of a try LandingPad block.
1677///
1678unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
1679 unsigned LandingPadLabel = NextLabelID();
Jim Laskey59e84342007-03-01 20:25:32 +00001680 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1681 LP.LandingPadLabel = LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +00001682 return LandingPadLabel;
1683}
1684
1685/// addPersonality - Provide the personality function for the exception
1686/// information.
1687void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
1688 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +00001689 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1690 LP.Personality = Personality;
Jim Laskey59667fe2007-02-21 22:38:31 +00001691}
1692
1693/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1694///
1695void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
1696 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +00001697 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +00001698 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +00001699 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +00001700}
1701
Jim Laskey59e84342007-03-01 20:25:32 +00001702/// setIsFilterLandingPad - Indicates that the landing pad is a throw filter.
1703///
1704void MachineModuleInfo::setIsFilterLandingPad(MachineBasicBlock *LandingPad) {
1705 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
1706 LP.IsFilter = true;
1707}
1708
Jim Laskey59667fe2007-02-21 22:38:31 +00001709/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1710/// pads.
1711void MachineModuleInfo::TidyLandingPads() {
1712 for (unsigned i = 0; i != LandingPads.size(); ) {
1713 LandingPadInfo &LandingPad = LandingPads[i];
Jim Laskey59667fe2007-02-21 22:38:31 +00001714 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001715
1716 if (!LandingPad.LandingPadLabel) {
Jim Laskey59667fe2007-02-21 22:38:31 +00001717 LandingPads.erase(LandingPads.begin() + i);
1718 continue;
1719 }
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +00001720
1721 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
1722 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
1723 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
1724
1725
1726 if (!BeginLabel || !EndLabel) {
1727 printf("Tidy: %d, %d, %d\n", BeginLabel, EndLabel, LandingPad.LandingPadLabel);
1728 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
1729 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
1730 continue;
1731 }
1732
1733 LandingPad.BeginLabels[j] = BeginLabel;
1734 LandingPad.EndLabels[j] = EndLabel;
1735 ++j;
1736 }
Jim Laskey59667fe2007-02-21 22:38:31 +00001737
1738 ++i;
1739 }
1740}
1741
1742/// getTypeIDFor - Return the type id for the specified typeinfo. This is
1743/// function wide.
1744unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
1745 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
1746 if (TypeInfos[i] == TI) return i + 1;
1747
1748 TypeInfos.push_back(TI);
1749 return TypeInfos.size();
1750}
1751
1752/// getLandingPadInfos - Return a reference to the landing pad info for the
1753/// current function.
1754Function *MachineModuleInfo::getPersonality() const {
1755 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
1756}
1757
1758
Jim Laskey9d4209f2006-11-07 19:33:46 +00001759//===----------------------------------------------------------------------===//
Jim Laskey6da18642007-01-26 21:38:26 +00001760/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows
1761/// a info consumer to determine if the range of two labels is empty, by seeing
1762/// if the labels map to the same reduced label.
Jim Laskey9d4209f2006-11-07 19:33:46 +00001763
1764namespace llvm {
1765
1766struct DebugLabelFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +00001767 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +00001768 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {}
1769
Jim Laskey9d4209f2006-11-07 19:33:46 +00001770 virtual bool runOnMachineFunction(MachineFunction &MF);
Jim Laskey6da18642007-01-26 21:38:26 +00001771 virtual const char *getPassName() const { return "Label Folder"; }
Jim Laskey9d4209f2006-11-07 19:33:46 +00001772};
1773
Devang Patel19974732007-05-03 01:11:54 +00001774char DebugLabelFolder::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +00001775
Jim Laskey9d4209f2006-11-07 19:33:46 +00001776bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
Jim Laskey6da18642007-01-26 21:38:26 +00001777 // Get machine module info.
1778 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
1779 if (!MMI) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001780 // Get target instruction info.
1781 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
1782 if (!TII) return false;
Jim Laskey9d4209f2006-11-07 19:33:46 +00001783
1784 // Track if change is made.
1785 bool MadeChange = false;
1786 // No prior label to begin.
1787 unsigned PriorLabel = 0;
1788
1789 // Iterate through basic blocks.
1790 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
1791 BB != E; ++BB) {
1792 // Iterate through instructions.
1793 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Jim Laskey6da18642007-01-26 21:38:26 +00001794 // Is it a label.
Jim Laskey1ee29252007-01-26 14:34:52 +00001795 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
Jim Laskey9d4209f2006-11-07 19:33:46 +00001796 // The label ID # is always operand #0, an immediate.
1797 unsigned NextLabel = I->getOperand(0).getImm();
1798
1799 // If there was an immediate prior label.
1800 if (PriorLabel) {
1801 // Remap the current label to prior label.
Jim Laskey6da18642007-01-26 21:38:26 +00001802 MMI->RemapLabel(NextLabel, PriorLabel);
Jim Laskey9d4209f2006-11-07 19:33:46 +00001803 // Delete the current label.
1804 I = BB->erase(I);
1805 // Indicate a change has been made.
1806 MadeChange = true;
1807 continue;
1808 } else {
1809 // Start a new round.
1810 PriorLabel = NextLabel;
1811 }
1812 } else {
1813 // No consecutive labels.
1814 PriorLabel = 0;
1815 }
1816
1817 ++I;
1818 }
1819 }
1820
1821 return MadeChange;
1822}
1823
1824FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
1825
1826}
Jim Laskeyb8509c52006-03-23 18:07:55 +00001827