blob: ab42886cb4897b61eb0d1348635f44d24063f19f [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"
13#include "llvm/DerivedTypes.h"
14#include "llvm/Intrinsics.h"
15#include "llvm/Instructions.h"
16#include "llvm/Module.h"
17#include "llvm/Support/Dwarf.h"
18
Jim Laskey6af56812006-01-04 13:36:38 +000019using namespace llvm;
20
21// Handle the Pass registration stuff necessary to use TargetData's.
22namespace {
Jim Laskeyb2efb852006-01-04 22:28:25 +000023 RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
24}
Jim Laskey063e7652006-01-17 17:31:53 +000025
Jim Laskeyb3e789a2006-01-26 20:21:46 +000026//===----------------------------------------------------------------------===//
27
28/// getGlobalVariablesUsing - Return all of the global variables which have the
29/// specified value in their initializer somewhere.
30static void
31getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
32 // Scan though value users.
33 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
34 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
35 // If the user is a global variable then add to result.
36 Result.push_back(GV);
37 } else if (Constant *C = dyn_cast<Constant>(*I)) {
38 // If the user is a constant variable then scan its users
39 getGlobalVariablesUsing(C, Result);
40 }
41 }
42}
43
44/// getGlobalVariablesUsing - Return all of the global variables that use the
45/// named global variable.
46static std::vector<GlobalVariable*>
47getGlobalVariablesUsing(Module &M, const std::string &RootName) {
48 std::vector<GlobalVariable*> Result; // Global variables matching criteria.
49
50 // Get the global variable root.
51 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
52 StructType::get(std::vector<const Type*>()));
53
54 // If present and linkonce then scan for users.
55 if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
56 getGlobalVariablesUsing(UseRoot, Result);
57 }
58
59 return Result;
60}
61
62/// getStringValue - Turn an LLVM constant pointer that eventually points to a
63/// global into a string value. Return an empty string if we can't do it.
64///
Chris Lattner22760af2006-01-27 17:31:30 +000065static const std::string getStringValue(Value *V, unsigned Offset = 0) {
Jim Laskeyb3e789a2006-01-26 20:21:46 +000066 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
67 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
68 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
69 if (Init->isString()) {
70 std::string Result = Init->getAsString();
71 if (Offset < Result.size()) {
72 // If we are pointing INTO The string, erase the beginning...
73 Result.erase(Result.begin(), Result.begin()+Offset);
74
75 // Take off the null terminator, and any string fragments after it.
76 std::string::size_type NullPos = Result.find_first_of((char)0);
77 if (NullPos != std::string::npos)
78 Result.erase(Result.begin()+NullPos, Result.end());
79 return Result;
80 }
81 }
82 }
83 } else if (Constant *C = dyn_cast<Constant>(V)) {
84 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
85 return getStringValue(GV, Offset);
86 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 if (CE->getOpcode() == Instruction::GetElementPtr) {
88 // Turn a gep into the specified offset.
89 if (CE->getNumOperands() == 3 &&
90 cast<Constant>(CE->getOperand(1))->isNullValue() &&
91 isa<ConstantInt>(CE->getOperand(2))) {
92 return getStringValue(CE->getOperand(0),
93 Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
94 }
95 }
96 }
97 }
98 return "";
99}
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000100
101/// getGlobalValue - Return either a direct or cast Global value.
102///
103static GlobalVariable *getGlobalValue(Value *V) {
104 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
105 return GV;
106 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
107 return CE->getOpcode() == Instruction::Cast ? dyn_cast<GlobalVariable>(V)
108 : NULL;
109 }
110 return NULL;
111}
112
Jim Laskeyb2efb852006-01-04 22:28:25 +0000113
Jim Laskey063e7652006-01-17 17:31:53 +0000114//===----------------------------------------------------------------------===//
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000115
116DebugInfoWrapper::DebugInfoWrapper(GlobalVariable *G)
117: GV(G)
118, IC(dyn_cast<ConstantStruct>(GV->getInitializer())) {
119 assert(IC && "llvm.db.global is missing structured constant");
120}
121
122//===----------------------------------------------------------------------===//
123
124CompileUnitWrapper::CompileUnitWrapper(GlobalVariable *G)
125: DebugInfoWrapper(G)
126{
127 // FIXME - should probably ease up on the number of operands (version.)
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000128 assert(IC->getNumOperands() == N_op &&
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000129 "Compile unit does not have correct number of operands");
130}
131
132/// getTag - Return the compile unit's tag number. Currently should be
133/// DW_TAG_variable.
134unsigned CompileUnitWrapper::getTag() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000135 return cast<ConstantUInt>(IC->getOperand(Tag_op))->getValue();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000136}
137
138/// isCorrectDebugVersion - Return true if is the correct llvm debug version.
139/// Currently the value is 0 (zero.) If the value is is not correct then
140/// ignore all debug information.
141bool CompileUnitWrapper::isCorrectDebugVersion() const {
Jim Laskey1a058512006-01-27 15:46:54 +0000142 return cast<ConstantUInt>(IC->getOperand(Version_op))->getValue() == 0;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000143}
144
145/// getLanguage - Return the compile unit's language number (ex. DW_LANG_C89.)
146///
147unsigned CompileUnitWrapper::getLanguage() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000148 return cast<ConstantUInt>(IC->getOperand(Language_op))->getValue();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000149}
150
151/// getFileName - Return the compile unit's file name.
152///
153const std::string CompileUnitWrapper::getFileName() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000154 return getStringValue(IC->getOperand(FileName_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000155}
156
157/// getDirectory - Return the compile unit's file directory.
158///
159const std::string CompileUnitWrapper::getDirectory() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000160 return getStringValue(IC->getOperand(Directory_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000161}
162
163/// getProducer - Return the compile unit's generator name.
164///
165const std::string CompileUnitWrapper::getProducer() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000166 return getStringValue(IC->getOperand(Producer_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000167}
168
169//===----------------------------------------------------------------------===//
170
171GlobalWrapper::GlobalWrapper(GlobalVariable *G)
172: DebugInfoWrapper(G)
173{
174 // FIXME - should probably ease up on the number of operands (version.)
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000175 assert(IC->getNumOperands() == N_op &&
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000176 "Global does not have correct number of operands");
177}
178
179/// getTag - Return the global's tag number. Currently should be
180/// DW_TAG_variable or DW_TAG_subprogram.
181unsigned GlobalWrapper::getTag() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000182 return cast<ConstantUInt>(IC->getOperand(Tag_op))->getValue();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000183}
184
185/// getContext - Return the "lldb.compile_unit" context global.
186///
187GlobalVariable *GlobalWrapper::getContext() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000188 return getGlobalValue(IC->getOperand(Context_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000189}
190
191/// getName - Return the name of the global.
192///
193const std::string GlobalWrapper::getName() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000194 return getStringValue(IC->getOperand(Name_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000195}
196
197/// getType - Return the type of the global.
198///
199const GlobalVariable *GlobalWrapper::getType() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000200 return getGlobalValue(IC->getOperand(Type_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000201}
202
203/// isStatic - Return true if the global is static.
204///
205bool GlobalWrapper::isStatic() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000206 return cast<ConstantBool>(IC->getOperand(Static_op))->getValue();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000207}
208
209/// isDefinition - Return true if the global is a definition.
210///
211bool GlobalWrapper::isDefinition() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000212 return dyn_cast<ConstantBool>(IC->getOperand(Definition_op))->getValue();
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000213}
214
215/// getGlobalVariable - Return the global variable (tag == DW_TAG_variable.)
216///
217GlobalVariable *GlobalWrapper::getGlobalVariable() const {
Jim Laskeyd8f77ba2006-01-27 15:20:54 +0000218 return getGlobalValue(IC->getOperand(GlobalVariable_op));
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000219}
220
221//===----------------------------------------------------------------------===//
222
223
224MachineDebugInfo::MachineDebugInfo()
225: CompileUnits()
226, Directories()
227, SourceFiles()
228, Lines()
229{
230
231}
232MachineDebugInfo::~MachineDebugInfo() {
233
234}
235
Jim Laskeyb2efb852006-01-04 22:28:25 +0000236/// doInitialization - Initialize the debug state for a new module.
237///
238bool MachineDebugInfo::doInitialization() {
239 return false;
Jim Laskey6af56812006-01-04 13:36:38 +0000240}
241
Jim Laskeyb2efb852006-01-04 22:28:25 +0000242/// doFinalization - Tear down the debug state after completion of a module.
243///
244bool MachineDebugInfo::doFinalization() {
245 return false;
246}
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000247
248/// AnalyzeModule - Scan the module for global debug information.
249///
250void MachineDebugInfo::AnalyzeModule(Module &M) {
251 SetupCompileUnits(M);
252}
253
254/// SetupCompileUnits - Set up the unique vector of compile units.
255///
256void MachineDebugInfo::SetupCompileUnits(Module &M) {
257 // Get vector of all debug compile units.
258 std::vector<GlobalVariable*> Globals =
259 getGlobalVariablesUsing(M, "llvm.dbg.translation_units");
260
261 // Scan all compile unit globals.
262 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
263 // Create wrapper for compile unit.
264 CompileUnitWrapper CUI(Globals[i]);
265 // Add to result.
266 if (CUI.isCorrectDebugVersion()) CompileUnits.insert(CUI);
267 }
268
269 // If there any bad compile units then suppress debug information
270 if (CompileUnits.size() != Globals.size()) CompileUnits.reset();
271}
272
Jim Laskey6e87c0e2006-01-26 21:22:49 +0000273/// getCompileUnits - Return a vector of debug compile units.
274///
275const UniqueVector<CompileUnitWrapper> MachineDebugInfo::getCompileUnits()const{
276 return CompileUnits;
277}
278
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000279/// getGlobalVariables - Return a vector of debug global variables.
280///
281std::vector<GlobalWrapper> MachineDebugInfo::getGlobalVariables(Module &M) {
282 // Get vector of all debug global objects.
283 std::vector<GlobalVariable*> Globals =
284 getGlobalVariablesUsing(M, "llvm.dbg.globals");
285
286 // Accumulation of global variables.
287 std::vector<GlobalWrapper> GlobalVariables;
288
289// FIXME - skip until globals have new format
290#if 0
291 // Scan all globals.
292 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
293 // Create wrapper for global.
294 GlobalWrapper GW(Globals[i]);
295 // If the global is a variable then add to result.
296 if (GW.getTag() == DW_TAG_variable) GlobalVariables.push_back(GW);
297 }
298#endif
299
300 return GlobalVariables;
301}
302