reduce indentation
[oota-llvm.git] / lib / Target / PIC16 / PIC16DebugInfo.cpp
1 //===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source 
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the helper functions for representing debug information.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PIC16.h"
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
21 using namespace llvm;
22
23 /// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
24 ///
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);
34   else {
35     TypeNo = PIC16Dbg::T_NULL;
36     HasAux = false;
37   }
38   return;
39 }
40
41 /// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
42 ///
43 void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
44   std::string Name = "";
45   Ty.getName(Name);
46   unsigned short BaseTy = GetTypeDebugNumber(Name);
47   TypeNo = TypeNo << PIC16Dbg::S_BASIC;
48   TypeNo = TypeNo | (0xffff & BaseTy);
49 }
50
51 /// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type 
52 /// from Ty. Derived types are mostly pointers.
53 ///
54 void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
55                                             bool &HasAux, int Aux[],
56                                             std::string &TagName) {
57
58   switch(Ty.getTag())
59   {
60     case dwarf::DW_TAG_pointer_type:
61       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
62       TypeNo = TypeNo | PIC16Dbg::DT_PTR;
63       break;
64     default:
65       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
66   }
67   
68   // We also need to encode the the information about the base type of
69   // pointer in TypeNo.
70   DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
71   PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
72 }
73
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) {
78
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];
94     }
95   }
96   HasAux = true;
97   // In auxillary entry for array, 7th and 8th byte represent array size.
98   Aux[6] = size & 0xff;
99   Aux[7] = size >> 8;
100   DIType BaseType = CTy.getTypeDerivedFrom();
101   PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
102 }
103
104 /// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for 
105 /// structure or union.
106 ///
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;
115   else
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()->getNameStr().substr(18);
121   TagName += UniqueSuffix;
122   unsigned short size = CTy.getSizeInBits()/8;
123   // 7th and 8th byte represent size.
124   HasAux = true;
125   Aux[6] = size & 0xff;
126   Aux[7] = size >> 8;
127 }
128
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;
133 }
134
135 /// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for 
136 /// composite types from Ty.
137 ///
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);
144       break;
145     }
146     case dwarf:: DW_TAG_union_type:
147     case dwarf::DW_TAG_structure_type: {
148       PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
149       break;
150     }
151     case dwarf::DW_TAG_enumeration_type: {
152       PopulateEnumTypeInfo (Ty, TypeNo);
153       break;
154     }
155     default:
156       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
157   }
158 }
159
160 /// GetTypeDebugNumber - Get debug type number for given type.
161 ///
162 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type)  {
163   if (type == "char")
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;
179   else
180     return 0;
181 }
182  
183 /// GetStorageClass - Get storage class for give debug variable.
184 ///
185 short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
186   short ClassNo;
187   if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
188     // Generating C_AUTO here fails due to error in linker. Change it once
189     // linker is fixed.
190     ClassNo = PIC16Dbg::C_STAT;
191   }
192   else if (DIGV.isLocalToUnit())
193     ClassNo = PIC16Dbg::C_STAT;
194   else
195     ClassNo = PIC16Dbg::C_EXT;
196   return ClassNo;
197 }
198
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   SmallVector<GlobalVariable *, 2> CUs;
204   SmallVector<GlobalVariable *, 4> GVs;
205   SmallVector<GlobalVariable *, 4> SPs;
206   CollectDebugInfoAnchors(M, CUs, GVs, SPs);
207   if (!CUs.empty()) {
208     // FIXME : What if more then one CUs are present in a module ?
209     GlobalVariable *CU = CUs[0];
210     EmitDebugDirectives = true;
211     SwitchToCU(CU);
212   }
213
214   // Emit debug info for decls of composite types.
215   EmitCompositeTypeDecls(M);
216 }
217
218 /// Helper to find first valid debug loc for a function.
219 ///
220 static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
221   DebugLoc DL;
222   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
223        I != E; ++I) {
224     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
225          II != E; ++II) {
226       DL = II->getDebugLoc();
227       if (!DL.isUnknown())
228         return DL;
229     }
230   }
231   return DL;
232 }
233
234 /// BeginFunction - Emit necessary debug info to start a function.
235 ///
236 void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
237   if (! EmitDebugDirectives) return;
238   
239   // Retreive the first valid debug Loc and process it.
240   const DebugLoc &DL = GetDebugLocForFunction(MF);
241   ChangeDebugLoc(MF, DL, true);
242
243   EmitFunctBeginDI(MF.getFunction());
244   
245   // Set current line to 0 so that.line directive is genearted after .bf.
246   CurLine = 0;
247 }
248
249 /// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
250 /// CurFile and CurLine may change as a result of this.
251 ///
252 void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,  
253                                   const DebugLoc &DL, bool IsInBeginFunction) {
254   if (! EmitDebugDirectives) return;
255   assert (! DL.isUnknown()  && "can't change to invalid debug loc");
256
257   GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit;
258   unsigned line = MF.getDebugLocTuple(DL).Line;
259
260   SwitchToCU(CU);
261   SwitchToLine(line, IsInBeginFunction);
262 }
263
264 /// SwitchToLine - Emit line directive for a new line.
265 ///
266 void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
267   if (CurLine == Line) return;
268   if (!IsInBeginFunction)  O << "\n\t.line " << Line << "\n";
269   CurLine = Line;
270 }
271
272 /// EndFunction - Emit .ef for end of function.
273 ///
274 void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
275   if (! EmitDebugDirectives) return;
276   EmitFunctEndDI(MF.getFunction(), CurLine);
277 }
278
279 /// EndModule - Emit .eof for end of module.
280 ///
281 void PIC16DbgInfo::EndModule(Module &M) {
282   if (! EmitDebugDirectives) return;
283   EmitVarDebugInfo(M);
284   if (CurFile != "") O << "\n\t.eof";
285 }
286  
287 /// EmitCompositeTypeElements - Emit debug information for members of a 
288 /// composite type.
289 /// 
290 void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
291                                               std::string UniqueSuffix) { 
292   unsigned long Value = 0;
293   DIArray Elements = CTy.getTypeArray();
294   for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
295     DIDescriptor Element = Elements.getElement(i);
296     unsigned short TypeNo = 0;
297     bool HasAux = false;
298     int ElementAux[PIC16Dbg::AuxSize] = { 0 };
299     std::string TagName = "";
300     std::string ElementName;
301     GlobalVariable *GV = Element.getGV();
302     DIDerivedType DITy(GV);
303     DITy.getName(ElementName);
304     unsigned short ElementSize = DITy.getSizeInBits()/8;
305     // Get mangleddd name for this structure/union  element.
306     std::string MangMemName = ElementName + UniqueSuffix;
307     PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName);
308     short Class = 0;
309     if( CTy.getTag() == dwarf::DW_TAG_union_type)
310       Class = PIC16Dbg::C_MOU;
311     else if  (CTy.getTag() == dwarf::DW_TAG_structure_type)
312       Class = PIC16Dbg::C_MOS;
313     EmitSymbol(MangMemName, Class, TypeNo, Value);
314     if (CTy.getTag() == dwarf::DW_TAG_structure_type)
315       Value += ElementSize;
316     if (HasAux)
317       EmitAuxEntry(MangMemName, ElementAux, PIC16Dbg::AuxSize, TagName);
318   }
319 }
320
321 /// EmitCompositeTypeDecls - Emit composite type declarations like structure 
322 /// and union declarations.
323 ///
324 void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
325   for(iplist<GlobalVariable>::iterator I = M.getGlobalList().begin(),
326       E = M.getGlobalList().end(); I != E; I++) {
327     // Structures and union declaration's debug info has llvm.dbg.composite
328     // in its name.
329     // FIXME: Checking and relying on llvm.dbg.composite name is not a good idea.
330     if(I->getNameStr().find("llvm.dbg.composite") != std::string::npos) {
331       GlobalVariable *GV = cast<GlobalVariable >(I);
332       DICompositeType CTy(GV);
333       if (CTy.getTag() == dwarf::DW_TAG_union_type ||
334           CTy.getTag() == dwarf::DW_TAG_structure_type ) {
335         std::string name;
336         CTy.getName(name);
337         std::string DIVar = I->getName();
338         // Get the number after llvm.dbg.composite and make UniqueSuffix from 
339         // it.
340         std::string UniqueSuffix = "." + DIVar.substr(18);
341         std::string MangledCTyName = name + UniqueSuffix;
342         unsigned short size = CTy.getSizeInBits()/8;
343         int Aux[PIC16Dbg::AuxSize] = {0};
344         // 7th and 8th byte represent size of structure/union.
345         Aux[6] = size & 0xff;
346         Aux[7] = size >> 8;
347         // Emit .def for structure/union tag.
348         if( CTy.getTag() == dwarf::DW_TAG_union_type)
349           EmitSymbol(MangledCTyName, PIC16Dbg::C_UNTAG);
350         else if  (CTy.getTag() == dwarf::DW_TAG_structure_type) 
351           EmitSymbol(MangledCTyName, PIC16Dbg::C_STRTAG);
352
353         // Emit auxiliary debug information for structure/union tag. 
354         EmitAuxEntry(MangledCTyName, Aux, PIC16Dbg::AuxSize);
355
356         // Emit members.
357         EmitCompositeTypeElements (CTy, UniqueSuffix);
358
359         // Emit mangled Symbol for end of structure/union.
360         std::string EOSSymbol = ".eos" + UniqueSuffix;
361         EmitSymbol(EOSSymbol, PIC16Dbg::C_EOS);
362         EmitAuxEntry(EOSSymbol, Aux, PIC16Dbg::AuxSize, MangledCTyName);
363       }
364     }
365   }
366 }
367
368 /// EmitFunctBeginDI - Emit .bf for function.
369 ///
370 void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
371   std::string FunctName = F->getName();
372   if (EmitDebugDirectives) {
373     std::string FunctBeginSym = ".bf." + FunctName;
374     std::string BlockBeginSym = ".bb." + FunctName;
375
376     int BFAux[PIC16Dbg::AuxSize] = {0};
377     BFAux[4] = CurLine;
378     BFAux[5] = CurLine >> 8;
379
380     // Emit debug directives for beginning of function.
381     EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
382     EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
383
384     EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
385     EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
386   }
387 }
388
389 /// EmitFunctEndDI - Emit .ef for function end.
390 ///
391 void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
392   std::string FunctName = F->getName();
393   if (EmitDebugDirectives) {
394     std::string FunctEndSym = ".ef." + FunctName;
395     std::string BlockEndSym = ".eb." + FunctName;
396
397     // Emit debug directives for end of function.
398     EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
399     int EFAux[PIC16Dbg::AuxSize] = {0};
400     // 5th and 6th byte stand for line number.
401     EFAux[4] = CurLine;
402     EFAux[5] = CurLine >> 8;
403     EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
404     EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
405     EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
406   }
407 }
408
409 /// EmitAuxEntry - Emit Auxiliary debug information.
410 ///
411 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
412                                 std::string TagName) {
413   O << "\n\t.dim " << VarName << ", 1" ;
414   // TagName is emitted in case of structure/union objects.
415   if (TagName != "")
416     O << ", " << TagName;
417   for (int i = 0; i<Num; i++)
418     O << "," << Aux[i];
419 }
420
421 /// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
422 ///
423 void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
424                               Type, unsigned long Value) {
425   O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = " 
426     << Class;
427   if (Value > 0)
428     O  << ", value = " << Value;
429 }
430
431 /// EmitVarDebugInfo - Emit debug information for all variables.
432 ///
433 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
434   SmallVector<GlobalVariable *, 2> CUs;
435   SmallVector<GlobalVariable *, 4> GVs;
436   SmallVector<GlobalVariable *, 4> SPs;
437   CollectDebugInfoAnchors(M, CUs, GVs, SPs);
438   if (GVs.empty())
439     return;
440
441   for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(),
442          E = GVs.end(); I != E; ++I)  {
443     DIGlobalVariable DIGV(*I);
444     DIType Ty = DIGV.getType();
445     unsigned short TypeNo = 0;
446     bool HasAux = false;
447     int Aux[PIC16Dbg::AuxSize] = { 0 };
448     std::string TagName = "";
449     std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getNameStr();
450     PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
451     // Emit debug info only if type information is availaible.
452     if (TypeNo != PIC16Dbg::T_NULL) {
453       O << "\n\t.type " << VarName << ", " << TypeNo;
454       short ClassNo = getStorageClass(DIGV);
455       O << "\n\t.class " << VarName << ", " << ClassNo;
456       if (HasAux) 
457         EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
458     }
459   }
460   O << "\n";
461 }
462
463 /// SwitchToCU - Switch to a new compilation unit.
464 ///
465 void PIC16DbgInfo::SwitchToCU(GlobalVariable *CU) {
466   // Get the file path from CU.
467   DICompileUnit cu(CU);
468   std::string DirName, FileName;
469   std::string FilePath = cu.getDirectory(DirName) + "/" + 
470                          cu.getFilename(FileName);
471
472   // Nothing to do if source file is still same.
473   if ( FilePath == CurFile ) return;
474
475   // Else, close the current one and start a new.
476   if (CurFile != "") O << "\n\t.eof";
477   O << "\n\t.file\t\"" << FilePath << "\"\n" ;
478   CurFile = FilePath;
479   CurLine = 0;
480 }
481
482 /// EmitEOF - Emit .eof for end of file.
483 ///
484 void PIC16DbgInfo::EmitEOF() {
485   if (CurFile != "")
486     O << "\n\t.EOF";
487 }
488