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/CodeGen/DebugLoc.h"
19 #include "llvm/Support/raw_ostream.h"
23 /// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
25 void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo,
26 bool &HasAux, int Aux[],
27 std::string &TagName) {
28 if (Ty.isBasicType(Ty.getTag()))
29 PopulateBasicTypeInfo (Ty, TypeNo);
30 else if (Ty.isDerivedType(Ty.getTag()))
31 PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
32 else if (Ty.isCompositeType(Ty.getTag()))
33 PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
35 TypeNo = PIC16Dbg::T_NULL;
41 /// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
43 void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
44 std::string Name = "";
46 unsigned short BaseTy = GetTypeDebugNumber(Name);
47 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
48 TypeNo = TypeNo | (0xffff & BaseTy);
51 /// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type
52 /// from Ty. Derived types are mostly pointers.
54 void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
55 bool &HasAux, int Aux[],
56 std::string &TagName) {
60 case dwarf::DW_TAG_pointer_type:
61 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
62 TypeNo = TypeNo | PIC16Dbg::DT_PTR;
65 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
68 // We also need to encode the the information about the base type of
70 DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
71 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
74 /// PopulateArrayTypeInfo - Populate TypeNo, Aux[] for array from Ty.
75 void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo,
76 bool &HasAux, int Aux[],
77 std::string &TagName) {
79 DICompositeType CTy = DICompositeType(Ty.getGV());
80 DIArray Elements = CTy.getTypeArray();
81 unsigned short size = 1;
82 unsigned short Dimension[4]={0,0,0,0};
83 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
84 DIDescriptor Element = Elements.getElement(i);
85 if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
86 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
87 TypeNo = TypeNo | PIC16Dbg::DT_ARY;
88 DISubrange SubRange = DISubrange(Element.getGV());
89 Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
90 // Each dimension is represented by 2 bytes starting at byte 9.
91 Aux[8+i*2+0] = Dimension[i];
92 Aux[8+i*2+1] = Dimension[i] >> 8;
93 size = size * Dimension[i];
97 // In auxillary entry for array, 7th and 8th byte represent array size.
100 DIType BaseType = CTy.getTypeDerivedFrom();
101 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
104 /// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for
105 /// structure or union.
107 void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty,
108 unsigned short &TypeNo,
109 bool &HasAux, int Aux[],
110 std::string &TagName) {
111 DICompositeType CTy = DICompositeType(Ty.getGV());
112 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
113 if (Ty.getTag() == dwarf::DW_TAG_structure_type)
114 TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
116 TypeNo = TypeNo | PIC16Dbg::T_UNION;
117 CTy.getName(TagName);
118 // UniqueSuffix is .number where number is obtained from
119 // llvm.dbg.composite<number>.
120 std::string UniqueSuffix = "." + Ty.getGV()->getName().substr(18);
121 TagName += UniqueSuffix;
122 unsigned short size = CTy.getSizeInBits()/8;
123 // 7th and 8th byte represent size.
125 Aux[6] = size & 0xff;
129 /// PopulateEnumTypeInfo - Populate TypeNo for enum from Ty.
130 void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) {
131 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
132 TypeNo = TypeNo | PIC16Dbg::T_ENUM;
135 /// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for
136 /// composite types from Ty.
138 void PIC16DbgInfo::PopulateCompositeTypeInfo (DIType Ty, unsigned short &TypeNo,
139 bool &HasAux, int Aux[],
140 std::string &TagName) {
141 switch (Ty.getTag()) {
142 case dwarf::DW_TAG_array_type: {
143 PopulateArrayTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
146 case dwarf:: DW_TAG_union_type:
147 case dwarf::DW_TAG_structure_type: {
148 PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
151 case dwarf::DW_TAG_enumeration_type: {
152 PopulateEnumTypeInfo (Ty, TypeNo);
156 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
160 /// GetTypeDebugNumber - Get debug type number for given type.
162 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) {
164 return PIC16Dbg::T_CHAR;
165 else if (type == "short")
166 return PIC16Dbg::T_SHORT;
167 else if (type == "int")
168 return PIC16Dbg::T_INT;
169 else if (type == "long")
170 return PIC16Dbg::T_LONG;
171 else if (type == "unsigned char")
172 return PIC16Dbg::T_UCHAR;
173 else if (type == "unsigned short")
174 return PIC16Dbg::T_USHORT;
175 else if (type == "unsigned int")
176 return PIC16Dbg::T_UINT;
177 else if (type == "unsigned long")
178 return PIC16Dbg::T_ULONG;
183 /// GetStorageClass - Get storage class for give debug variable.
185 short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
187 if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
188 // Generating C_AUTO here fails due to error in linker. Change it once
190 ClassNo = PIC16Dbg::C_STAT;
192 else if (DIGV.isLocalToUnit())
193 ClassNo = PIC16Dbg::C_STAT;
195 ClassNo = PIC16Dbg::C_EXT;
199 /// BeginModule - Emit necessary debug info to start a Module and do other
200 /// required initializations.
201 void PIC16DbgInfo::BeginModule(Module &M) {
202 // Emit file directive for module.
203 GlobalVariable *CU = M.getNamedGlobal("llvm.dbg.compile_unit");
205 EmitDebugDirectives = true;
209 // Emit debug info for decls of composite types.
210 EmitCompositeTypeDecls(M);
213 /// Helper to find first valid debug loc for a function.
215 static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
217 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
219 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
221 DL = II->getDebugLoc();
229 /// BeginFunction - Emit necessary debug info to start a function.
231 void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
232 if (! EmitDebugDirectives) return;
234 // Retreive the first valid debug Loc and process it.
235 const DebugLoc &DL = GetDebugLocForFunction(MF);
236 ChangeDebugLoc(MF, DL, true);
238 EmitFunctBeginDI(MF.getFunction());
240 // Set current line to 0 so that.line directive is genearted after .bf.
244 /// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
245 /// CurFile and CurLine may change as a result of this.
247 void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,
248 const DebugLoc &DL, bool IsInBeginFunction) {
249 if (! EmitDebugDirectives) return;
250 assert (! DL.isUnknown() && "can't change to invalid debug loc");
252 GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit;
253 unsigned line = MF.getDebugLocTuple(DL).Line;
256 SwitchToLine(line, IsInBeginFunction);
259 /// SwitchToLine - Emit line directive for a new line.
261 void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
262 if (CurLine == Line) return;
263 if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n";
267 /// EndFunction - Emit .ef for end of function.
269 void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
270 if (! EmitDebugDirectives) return;
271 EmitFunctEndDI(MF.getFunction(), CurLine);
274 /// EndModule - Emit .eof for end of module.
276 void PIC16DbgInfo::EndModule(Module &M) {
277 if (! EmitDebugDirectives) return;
279 if (CurFile != "") O << "\n\t.eof";
282 /// EmitCompositeTypeElements - Emit debug information for members of a
285 void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
286 std::string UniqueSuffix) {
287 unsigned long Value = 0;
288 DIArray Elements = CTy.getTypeArray();
289 for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
290 DIDescriptor Element = Elements.getElement(i);
291 unsigned short TypeNo = 0;
293 int ElementAux[PIC16Dbg::AuxSize] = { 0 };
294 std::string TagName = "";
295 std::string ElementName;
296 GlobalVariable *GV = Element.getGV();
297 DIDerivedType DITy(GV);
298 DITy.getName(ElementName);
299 unsigned short ElementSize = DITy.getSizeInBits()/8;
300 // Get mangleddd name for this structure/union element.
301 std::string MangMemName = ElementName + UniqueSuffix;
302 PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName);
304 if( CTy.getTag() == dwarf::DW_TAG_union_type)
305 Class = PIC16Dbg::C_MOU;
306 else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
307 Class = PIC16Dbg::C_MOS;
308 EmitSymbol(MangMemName, Class, TypeNo, Value);
309 if (CTy.getTag() == dwarf::DW_TAG_structure_type)
310 Value += ElementSize;
312 EmitAuxEntry(MangMemName, ElementAux, PIC16Dbg::AuxSize, TagName);
316 /// EmitCompositeTypeDecls - Emit composite type declarations like structure
317 /// and union declarations.
319 void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
320 for(iplist<GlobalVariable>::iterator I = M.getGlobalList().begin(),
321 E = M.getGlobalList().end(); I != E; I++) {
322 // Structures and union declaration's debug info has llvm.dbg.composite
324 if(I->getName().find("llvm.dbg.composite") != std::string::npos) {
325 GlobalVariable *GV = cast<GlobalVariable >(I);
326 DICompositeType CTy(GV);
327 if (CTy.getTag() == dwarf::DW_TAG_union_type ||
328 CTy.getTag() == dwarf::DW_TAG_structure_type ) {
331 std::string DIVar = I->getName();
332 // Get the number after llvm.dbg.composite and make UniqueSuffix from
334 std::string UniqueSuffix = "." + DIVar.substr(18);
335 std::string MangledCTyName = name + UniqueSuffix;
336 unsigned short size = CTy.getSizeInBits()/8;
337 int Aux[PIC16Dbg::AuxSize] = {0};
338 // 7th and 8th byte represent size of structure/union.
339 Aux[6] = size & 0xff;
341 // Emit .def for structure/union tag.
342 if( CTy.getTag() == dwarf::DW_TAG_union_type)
343 EmitSymbol(MangledCTyName, PIC16Dbg::C_UNTAG);
344 else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
345 EmitSymbol(MangledCTyName, PIC16Dbg::C_STRTAG);
347 // Emit auxiliary debug information for structure/union tag.
348 EmitAuxEntry(MangledCTyName, Aux, PIC16Dbg::AuxSize);
351 EmitCompositeTypeElements (CTy, UniqueSuffix);
353 // Emit mangled Symbol for end of structure/union.
354 std::string EOSSymbol = ".eos" + UniqueSuffix;
355 EmitSymbol(EOSSymbol, PIC16Dbg::C_EOS);
356 EmitAuxEntry(EOSSymbol, Aux, PIC16Dbg::AuxSize, MangledCTyName);
362 /// EmitFunctBeginDI - Emit .bf for function.
364 void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
365 std::string FunctName = F->getName();
366 if (EmitDebugDirectives) {
367 std::string FunctBeginSym = ".bf." + FunctName;
368 std::string BlockBeginSym = ".bb." + FunctName;
370 int BFAux[PIC16Dbg::AuxSize] = {0};
372 BFAux[5] = CurLine >> 8;
374 // Emit debug directives for beginning of function.
375 EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
376 EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
378 EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
379 EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
383 /// EmitFunctEndDI - Emit .ef for function end.
385 void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
386 std::string FunctName = F->getName();
387 if (EmitDebugDirectives) {
388 std::string FunctEndSym = ".ef." + FunctName;
389 std::string BlockEndSym = ".eb." + FunctName;
391 // Emit debug directives for end of function.
392 EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
393 int EFAux[PIC16Dbg::AuxSize] = {0};
394 // 5th and 6th byte stand for line number.
396 EFAux[5] = CurLine >> 8;
397 EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
398 EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
399 EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
403 /// EmitAuxEntry - Emit Auxiliary debug information.
405 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
406 std::string TagName) {
407 O << "\n\t.dim " << VarName << ", 1" ;
408 // TagName is emitted in case of structure/union objects.
410 O << ", " << TagName;
411 for (int i = 0; i<Num; i++)
415 /// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
417 void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
418 Type, unsigned long Value) {
419 O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = "
422 O << ", value = " << Value;
425 /// EmitVarDebugInfo - Emit debug information for all variables.
427 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
428 GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.global_variables");
432 Constant *RootC = cast<Constant>(*Root->use_begin());
433 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
435 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
437 DIGlobalVariable DIGV(cast<GlobalVariable>(*UUI));
438 DIType Ty = DIGV.getType();
439 unsigned short TypeNo = 0;
441 int Aux[PIC16Dbg::AuxSize] = { 0 };
442 std::string TagName = "";
443 std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getName();
444 PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
445 // Emit debug info only if type information is availaible.
446 if (TypeNo != PIC16Dbg::T_NULL) {
447 O << "\n\t.type " << VarName << ", " << TypeNo;
448 short ClassNo = getStorageClass(DIGV);
449 O << "\n\t.class " << VarName << ", " << ClassNo;
451 EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
458 /// SwitchToCU - Switch to a new compilation unit.
460 void PIC16DbgInfo::SwitchToCU(GlobalVariable *CU) {
461 // Get the file path from CU.
462 DICompileUnit cu(CU);
463 std::string DirName, FileName;
464 std::string FilePath = cu.getDirectory(DirName) + "/" +
465 cu.getFilename(FileName);
467 // Nothing to do if source file is still same.
468 if ( FilePath == CurFile ) return;
470 // Else, close the current one and start a new.
471 if (CurFile != "") O << "\n\t.eof";
472 O << "\n\t.file\t\"" << FilePath << "\"\n" ;
477 /// EmitEOF - Emit .eof for end of file.
479 void PIC16DbgInfo::EmitEOF() {