1 //===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the helper functions for representing debug information.
12 //===----------------------------------------------------------------------===//
15 #include "PIC16DebugInfo.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/Support/DebugLoc.h"
19 #include "llvm/Support/FormattedStream.h"
20 #include "llvm/ADT/SmallString.h"
24 /// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
26 void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo,
27 bool &HasAux, int Aux[],
28 std::string &TagName) {
29 if (Ty.isBasicType(Ty.getTag()))
30 PopulateBasicTypeInfo (Ty, TypeNo);
31 else if (Ty.isDerivedType(Ty.getTag()))
32 PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
33 else if (Ty.isCompositeType(Ty.getTag()))
34 PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
36 TypeNo = PIC16Dbg::T_NULL;
42 /// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
44 void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
45 std::string Name = "";
47 unsigned short BaseTy = GetTypeDebugNumber(Name);
48 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
49 TypeNo = TypeNo | (0xffff & BaseTy);
52 /// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type
53 /// from Ty. Derived types are mostly pointers.
55 void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
56 bool &HasAux, int Aux[],
57 std::string &TagName) {
61 case dwarf::DW_TAG_pointer_type:
62 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
63 TypeNo = TypeNo | PIC16Dbg::DT_PTR;
66 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
69 // We also need to encode the the information about the base type of
71 DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
72 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
75 /// PopulateArrayTypeInfo - Populate TypeNo, Aux[] for array from Ty.
76 void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo,
77 bool &HasAux, int Aux[],
78 std::string &TagName) {
80 DICompositeType CTy = DICompositeType(Ty.getGV());
81 DIArray Elements = CTy.getTypeArray();
82 unsigned short size = 1;
83 unsigned short Dimension[4]={0,0,0,0};
84 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
85 DIDescriptor Element = Elements.getElement(i);
86 if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
87 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
88 TypeNo = TypeNo | PIC16Dbg::DT_ARY;
89 DISubrange SubRange = DISubrange(Element.getGV());
90 Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
91 // Each dimension is represented by 2 bytes starting at byte 9.
92 Aux[8+i*2+0] = Dimension[i];
93 Aux[8+i*2+1] = Dimension[i] >> 8;
94 size = size * Dimension[i];
98 // In auxillary entry for array, 7th and 8th byte represent array size.
101 DIType BaseType = CTy.getTypeDerivedFrom();
102 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
105 /// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for
106 /// structure or union.
108 void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty,
109 unsigned short &TypeNo,
110 bool &HasAux, int Aux[],
111 std::string &TagName) {
112 DICompositeType CTy = DICompositeType(Ty.getGV());
113 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
114 if (Ty.getTag() == dwarf::DW_TAG_structure_type)
115 TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
117 TypeNo = TypeNo | PIC16Dbg::T_UNION;
118 CTy.getName(TagName);
119 // UniqueSuffix is .number where number is obtained from
120 // llvm.dbg.composite<number>.
121 // FIXME: This will break when composite type is not represented by
122 // llvm.dbg.composite* global variable. This is not supported.
123 std::string UniqueSuffix = "." + Ty.getGV()->getNameStr().substr(18);
124 TagName += UniqueSuffix;
125 unsigned short size = CTy.getSizeInBits()/8;
126 // 7th and 8th byte represent size.
128 Aux[6] = size & 0xff;
132 /// PopulateEnumTypeInfo - Populate TypeNo for enum from Ty.
133 void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) {
134 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
135 TypeNo = TypeNo | PIC16Dbg::T_ENUM;
138 /// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for
139 /// composite types from Ty.
141 void PIC16DbgInfo::PopulateCompositeTypeInfo (DIType Ty, unsigned short &TypeNo,
142 bool &HasAux, int Aux[],
143 std::string &TagName) {
144 switch (Ty.getTag()) {
145 case dwarf::DW_TAG_array_type: {
146 PopulateArrayTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
149 case dwarf:: DW_TAG_union_type:
150 case dwarf::DW_TAG_structure_type: {
151 PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
154 case dwarf::DW_TAG_enumeration_type: {
155 PopulateEnumTypeInfo (Ty, TypeNo);
159 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
163 /// GetTypeDebugNumber - Get debug type number for given type.
165 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) {
167 return PIC16Dbg::T_CHAR;
168 else if (type == "short")
169 return PIC16Dbg::T_SHORT;
170 else if (type == "int")
171 return PIC16Dbg::T_INT;
172 else if (type == "long")
173 return PIC16Dbg::T_LONG;
174 else if (type == "unsigned char")
175 return PIC16Dbg::T_UCHAR;
176 else if (type == "unsigned short")
177 return PIC16Dbg::T_USHORT;
178 else if (type == "unsigned int")
179 return PIC16Dbg::T_UINT;
180 else if (type == "unsigned long")
181 return PIC16Dbg::T_ULONG;
186 /// GetStorageClass - Get storage class for give debug variable.
188 short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
190 if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
191 // Generating C_AUTO here fails due to error in linker. Change it once
193 ClassNo = PIC16Dbg::C_STAT;
195 else if (DIGV.isLocalToUnit())
196 ClassNo = PIC16Dbg::C_STAT;
198 ClassNo = PIC16Dbg::C_EXT;
202 /// BeginModule - Emit necessary debug info to start a Module and do other
203 /// required initializations.
204 void PIC16DbgInfo::BeginModule(Module &M) {
205 // Emit file directive for module.
206 DebugInfoFinder DbgFinder;
207 DbgFinder.processModule(M);
208 if (DbgFinder.compile_unit_count() != 0) {
209 // FIXME : What if more then one CUs are present in a module ?
210 GlobalVariable *CU = *DbgFinder.compile_unit_begin();
211 EmitDebugDirectives = true;
215 // Emit debug info for decls of composite types.
216 EmitCompositeTypeDecls(M);
219 /// Helper to find first valid debug loc for a function.
221 static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
223 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
225 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
227 DL = II->getDebugLoc();
235 /// BeginFunction - Emit necessary debug info to start a function.
237 void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
238 if (! EmitDebugDirectives) return;
240 // Retreive the first valid debug Loc and process it.
241 const DebugLoc &DL = GetDebugLocForFunction(MF);
242 // Emit debug info only if valid debug info is available.
243 if (!DL.isUnknown()) {
244 ChangeDebugLoc(MF, DL, true);
245 EmitFunctBeginDI(MF.getFunction());
247 // Set current line to 0 so that.line directive is genearted after .bf.
251 /// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
252 /// CurFile and CurLine may change as a result of this.
254 void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,
255 const DebugLoc &DL, bool IsInBeginFunction) {
256 if (! EmitDebugDirectives) return;
257 assert (! DL.isUnknown() && "can't change to invalid debug loc");
259 GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit;
260 unsigned line = MF.getDebugLocTuple(DL).Line;
263 SwitchToLine(line, IsInBeginFunction);
266 /// SwitchToLine - Emit line directive for a new line.
268 void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
269 if (CurLine == Line) return;
270 if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n";
274 /// EndFunction - Emit .ef for end of function.
276 void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
277 if (! EmitDebugDirectives) return;
278 const DebugLoc &DL = GetDebugLocForFunction(MF);
279 // Emit debug info only if valid debug info is available.
281 EmitFunctEndDI(MF.getFunction(), CurLine);
284 /// EndModule - Emit .eof for end of module.
286 void PIC16DbgInfo::EndModule(Module &M) {
287 if (! EmitDebugDirectives) return;
289 if (CurFile != "") O << "\n\t.eof";
292 /// EmitCompositeTypeElements - Emit debug information for members of a
295 void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
297 unsigned long Value = 0;
298 DIArray Elements = CTy.getTypeArray();
299 for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
300 DIDescriptor Element = Elements.getElement(i);
301 unsigned short TypeNo = 0;
303 int ElementAux[PIC16Dbg::AuxSize] = { 0 };
304 std::string TagName = "";
305 std::string ElementName;
306 GlobalVariable *GV = Element.getGV();
307 DIDerivedType DITy(GV);
308 DITy.getName(ElementName);
309 unsigned short ElementSize = DITy.getSizeInBits()/8;
310 // Get mangleddd name for this structure/union element.
311 SmallString<128> MangMemName(ElementName.begin(), ElementName.end());
312 MangMemName.append_uint_32(SuffixNo);
313 PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName);
315 if( CTy.getTag() == dwarf::DW_TAG_union_type)
316 Class = PIC16Dbg::C_MOU;
317 else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
318 Class = PIC16Dbg::C_MOS;
319 EmitSymbol(MangMemName.c_str(), Class, TypeNo, Value);
320 if (CTy.getTag() == dwarf::DW_TAG_structure_type)
321 Value += ElementSize;
323 EmitAuxEntry(MangMemName.c_str(), ElementAux, PIC16Dbg::AuxSize, TagName);
327 /// EmitCompositeTypeDecls - Emit composite type declarations like structure
328 /// and union declarations.
330 void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
331 DebugInfoFinder DbgFinder;
332 DbgFinder.processModule(M);
333 unsigned SuffixNo = 0;
334 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
335 E = DbgFinder.global_variable_end(); I != E; ++I) {
336 DICompositeType CTy(*I);
339 if (CTy.getTag() == dwarf::DW_TAG_union_type ||
340 CTy.getTag() == dwarf::DW_TAG_structure_type ) {
343 SmallString<128> MangledCTyName(Name.begin(), Name.end());
344 MangledCTyName.append_uint_32(++SuffixNo);
345 unsigned short size = CTy.getSizeInBits()/8;
346 int Aux[PIC16Dbg::AuxSize] = {0};
347 // 7th and 8th byte represent size of structure/union.
348 Aux[6] = size & 0xff;
350 // Emit .def for structure/union tag.
351 if( CTy.getTag() == dwarf::DW_TAG_union_type)
352 EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_UNTAG);
353 else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
354 EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_STRTAG);
356 // Emit auxiliary debug information for structure/union tag.
357 EmitAuxEntry(MangledCTyName.c_str(), Aux, PIC16Dbg::AuxSize);
360 EmitCompositeTypeElements (CTy, SuffixNo);
362 // Emit mangled Symbol for end of structure/union.
363 SmallString<128> EOSSymbol(Name.begin(), Name.end());
365 EOSSymbol.append_uint_32(SuffixNo);
366 EmitSymbol(EOSSymbol.c_str(), PIC16Dbg::C_EOS);
367 EmitAuxEntry(EOSSymbol.c_str(), Aux, PIC16Dbg::AuxSize,
368 MangledCTyName.c_str());
374 /// EmitFunctBeginDI - Emit .bf for function.
376 void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
377 std::string FunctName = F->getName();
378 if (EmitDebugDirectives) {
379 std::string FunctBeginSym = ".bf." + FunctName;
380 std::string BlockBeginSym = ".bb." + FunctName;
382 int BFAux[PIC16Dbg::AuxSize] = {0};
384 BFAux[5] = CurLine >> 8;
386 // Emit debug directives for beginning of function.
387 EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
388 EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
390 EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
391 EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
395 /// EmitFunctEndDI - Emit .ef for function end.
397 void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
398 std::string FunctName = F->getName();
399 if (EmitDebugDirectives) {
400 std::string FunctEndSym = ".ef." + FunctName;
401 std::string BlockEndSym = ".eb." + FunctName;
403 // Emit debug directives for end of function.
404 EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
405 int EFAux[PIC16Dbg::AuxSize] = {0};
406 // 5th and 6th byte stand for line number.
408 EFAux[5] = CurLine >> 8;
409 EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
410 EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
411 EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
415 /// EmitAuxEntry - Emit Auxiliary debug information.
417 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
418 std::string TagName) {
419 O << "\n\t.dim " << VarName << ", 1" ;
420 // TagName is emitted in case of structure/union objects.
422 O << ", " << TagName;
423 for (int i = 0; i<Num; i++)
427 /// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
429 void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
430 Type, unsigned long Value) {
431 O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = "
434 O << ", value = " << Value;
437 /// EmitVarDebugInfo - Emit debug information for all variables.
439 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
440 DebugInfoFinder DbgFinder;
441 DbgFinder.processModule(M);
443 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
444 E = DbgFinder.global_variable_end(); I != E; ++I) {
445 DIGlobalVariable DIGV(*I);
446 DIType Ty = DIGV.getType();
447 unsigned short TypeNo = 0;
449 int Aux[PIC16Dbg::AuxSize] = { 0 };
450 std::string TagName = "";
451 std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getNameStr();
452 PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
453 // Emit debug info only if type information is availaible.
454 if (TypeNo != PIC16Dbg::T_NULL) {
455 O << "\n\t.type " << VarName << ", " << TypeNo;
456 short ClassNo = getStorageClass(DIGV);
457 O << "\n\t.class " << VarName << ", " << ClassNo;
459 EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
465 /// SwitchToCU - Switch to a new compilation unit.
467 void PIC16DbgInfo::SwitchToCU(GlobalVariable *CU) {
468 // Get the file path from CU.
469 DICompileUnit cu(CU);
470 std::string DirName, FileName;
471 std::string FilePath = cu.getDirectory(DirName) + "/" +
472 cu.getFilename(FileName);
474 // Nothing to do if source file is still same.
475 if ( FilePath == CurFile ) return;
477 // Else, close the current one and start a new.
478 if (CurFile != "") O << "\n\t.eof";
479 O << "\n\t.file\t\"" << FilePath << "\"\n" ;
484 /// EmitEOF - Emit .eof for end of file.
486 void PIC16DbgInfo::EmitEOF() {