Change TargetAsmInfo to be constructed via TargetRegistry from a Target+Triple
[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 #include "llvm/ADT/SmallString.h"
21
22 using namespace llvm;
23
24 /// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
25 ///
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);
35   else {
36     TypeNo = PIC16Dbg::T_NULL;
37     HasAux = false;
38   }
39   return;
40 }
41
42 /// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
43 ///
44 void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
45   std::string Name = "";
46   Ty.getName(Name);
47   unsigned short BaseTy = GetTypeDebugNumber(Name);
48   TypeNo = TypeNo << PIC16Dbg::S_BASIC;
49   TypeNo = TypeNo | (0xffff & BaseTy);
50 }
51
52 /// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type 
53 /// from Ty. Derived types are mostly pointers.
54 ///
55 void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
56                                             bool &HasAux, int Aux[],
57                                             std::string &TagName) {
58
59   switch(Ty.getTag())
60   {
61     case dwarf::DW_TAG_pointer_type:
62       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
63       TypeNo = TypeNo | PIC16Dbg::DT_PTR;
64       break;
65     default:
66       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
67   }
68   
69   // We also need to encode the the information about the base type of
70   // pointer in TypeNo.
71   DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
72   PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
73 }
74
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) {
79
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];
95     }
96   }
97   HasAux = true;
98   // In auxillary entry for array, 7th and 8th byte represent array size.
99   Aux[6] = size & 0xff;
100   Aux[7] = size >> 8;
101   DIType BaseType = CTy.getTypeDerivedFrom();
102   PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
103 }
104
105 /// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for 
106 /// structure or union.
107 ///
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;
116   else
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.
127   HasAux = true;
128   Aux[6] = size & 0xff;
129   Aux[7] = size >> 8;
130 }
131
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;
136 }
137
138 /// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for 
139 /// composite types from Ty.
140 ///
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);
147       break;
148     }
149     case dwarf:: DW_TAG_union_type:
150     case dwarf::DW_TAG_structure_type: {
151       PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
152       break;
153     }
154     case dwarf::DW_TAG_enumeration_type: {
155       PopulateEnumTypeInfo (Ty, TypeNo);
156       break;
157     }
158     default:
159       TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
160   }
161 }
162
163 /// GetTypeDebugNumber - Get debug type number for given type.
164 ///
165 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type)  {
166   if (type == "char")
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;
182   else
183     return 0;
184 }
185  
186 /// GetStorageClass - Get storage class for give debug variable.
187 ///
188 short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
189   short ClassNo;
190   if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
191     // Generating C_AUTO here fails due to error in linker. Change it once
192     // linker is fixed.
193     ClassNo = PIC16Dbg::C_STAT;
194   }
195   else if (DIGV.isLocalToUnit())
196     ClassNo = PIC16Dbg::C_STAT;
197   else
198     ClassNo = PIC16Dbg::C_EXT;
199   return ClassNo;
200 }
201
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;
212     SwitchToCU(CU);
213   }
214
215   // Emit debug info for decls of composite types.
216   EmitCompositeTypeDecls(M);
217 }
218
219 /// Helper to find first valid debug loc for a function.
220 ///
221 static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
222   DebugLoc DL;
223   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
224        I != E; ++I) {
225     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
226          II != E; ++II) {
227       DL = II->getDebugLoc();
228       if (!DL.isUnknown())
229         return DL;
230     }
231   }
232   return DL;
233 }
234
235 /// BeginFunction - Emit necessary debug info to start a function.
236 ///
237 void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
238   if (! EmitDebugDirectives) return;
239   
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());
246   } 
247   // Set current line to 0 so that.line directive is genearted after .bf.
248   CurLine = 0;
249 }
250
251 /// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
252 /// CurFile and CurLine may change as a result of this.
253 ///
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");
258
259   GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit;
260   unsigned line = MF.getDebugLocTuple(DL).Line;
261
262   SwitchToCU(CU);
263   SwitchToLine(line, IsInBeginFunction);
264 }
265
266 /// SwitchToLine - Emit line directive for a new line.
267 ///
268 void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
269   if (CurLine == Line) return;
270   if (!IsInBeginFunction)  O << "\n\t.line " << Line << "\n";
271   CurLine = Line;
272 }
273
274 /// EndFunction - Emit .ef for end of function.
275 ///
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.
280   if (!DL.isUnknown())
281     EmitFunctEndDI(MF.getFunction(), CurLine);
282 }
283
284 /// EndModule - Emit .eof for end of module.
285 ///
286 void PIC16DbgInfo::EndModule(Module &M) {
287   if (! EmitDebugDirectives) return;
288   EmitVarDebugInfo(M);
289   if (CurFile != "") O << "\n\t.eof";
290 }
291  
292 /// EmitCompositeTypeElements - Emit debug information for members of a 
293 /// composite type.
294 /// 
295 void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
296                                               unsigned SuffixNo) {
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;
302     bool HasAux = false;
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);
314     short Class = 0;
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;
322     if (HasAux)
323       EmitAuxEntry(MangMemName.c_str(), ElementAux, PIC16Dbg::AuxSize, TagName);
324   }
325 }
326
327 /// EmitCompositeTypeDecls - Emit composite type declarations like structure 
328 /// and union declarations.
329 ///
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);
337     if (CTy.isNull())
338       continue;
339     if (CTy.getTag() == dwarf::DW_TAG_union_type ||
340         CTy.getTag() == dwarf::DW_TAG_structure_type ) {
341       std::string Name;
342       CTy.getName(Name);
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;
349       Aux[7] = size >> 8;
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);
355       
356       // Emit auxiliary debug information for structure/union tag. 
357       EmitAuxEntry(MangledCTyName.c_str(), Aux, PIC16Dbg::AuxSize);
358       
359       // Emit members.
360       EmitCompositeTypeElements (CTy, SuffixNo);
361       
362       // Emit mangled Symbol for end of structure/union.
363       SmallString<128> EOSSymbol(Name.begin(), Name.end());
364       EOSSymbol += ".eos";
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());
369     }
370   }
371 }
372
373
374 /// EmitFunctBeginDI - Emit .bf for function.
375 ///
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;
381
382     int BFAux[PIC16Dbg::AuxSize] = {0};
383     BFAux[4] = CurLine;
384     BFAux[5] = CurLine >> 8;
385
386     // Emit debug directives for beginning of function.
387     EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
388     EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
389
390     EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
391     EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
392   }
393 }
394
395 /// EmitFunctEndDI - Emit .ef for function end.
396 ///
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;
402
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.
407     EFAux[4] = CurLine;
408     EFAux[5] = CurLine >> 8;
409     EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
410     EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
411     EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
412   }
413 }
414
415 /// EmitAuxEntry - Emit Auxiliary debug information.
416 ///
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.
421   if (TagName != "")
422     O << ", " << TagName;
423   for (int i = 0; i<Num; i++)
424     O << "," << Aux[i];
425 }
426
427 /// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
428 ///
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 = " 
432     << Class;
433   if (Value > 0)
434     O  << ", value = " << Value;
435 }
436
437 /// EmitVarDebugInfo - Emit debug information for all variables.
438 ///
439 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
440   DebugInfoFinder DbgFinder;
441   DbgFinder.processModule(M);
442
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;
448     bool HasAux = false;
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;
458       if (HasAux) 
459         EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
460     }
461   }
462   O << "\n";
463 }
464
465 /// SwitchToCU - Switch to a new compilation unit.
466 ///
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);
473
474   // Nothing to do if source file is still same.
475   if ( FilePath == CurFile ) return;
476
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" ;
480   CurFile = FilePath;
481   CurLine = 0;
482 }
483
484 /// EmitEOF - Emit .eof for end of file.
485 ///
486 void PIC16DbgInfo::EmitEOF() {
487   if (CurFile != "")
488     O << "\n\t.EOF";
489 }
490