The subprogram descriptor for a function may be missing (llvm-ld linking two static...
[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/Support/raw_ostream.h"
18
19 using namespace llvm;
20
21 void PIC16DbgInfo::PopulateDebugInfo(DIType Ty, unsigned short &TypeNo,
22                                      bool &HasAux, int Aux[], 
23                                      std::string &TypeName) {
24   if (Ty.isBasicType(Ty.getTag())) {
25     std::string Name = "";
26     Ty.getName(Name);
27     unsigned short BaseTy = GetTypeDebugNumber(Name);
28     TypeNo = TypeNo << PIC16Dbg::S_BASIC;
29     TypeNo = TypeNo | (0xffff & BaseTy);
30   }
31   else if (Ty.isDerivedType(Ty.getTag())) {
32     switch(Ty.getTag())
33     {
34       case dwarf::DW_TAG_pointer_type:
35         TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
36         TypeNo = TypeNo | PIC16Dbg::DT_PTR;
37         break;
38       default:
39         TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
40     }
41     DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
42     PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);
43   }
44   else if (Ty.isCompositeType(Ty.getTag())) {
45     switch (Ty.getTag()) {
46       case dwarf::DW_TAG_array_type: {
47         DICompositeType CTy = DICompositeType(Ty.getGV());
48         DIArray Elements = CTy.getTypeArray();
49         unsigned short size = 1;
50         unsigned short Dimension[4]={0,0,0,0};
51         for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
52           DIDescriptor Element = Elements.getElement(i);
53           if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
54             TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
55             TypeNo = TypeNo | PIC16Dbg::DT_ARY;
56             DISubrange SubRange = DISubrange(Element.getGV());
57             Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
58             // Each dimension is represented by 2 bytes starting at byte 9.
59             Aux[8+i*2+0] = Dimension[i];
60             Aux[8+i*2+1] = Dimension[i] >> 8;
61             size = size * Dimension[i];
62           }
63         }
64         HasAux = true;
65         // In auxillary entry for array, 7th and 8th byte represent array size.
66         Aux[6] = size & 0xff;
67         Aux[7] = size >> 8;
68         DIType BaseType = CTy.getTypeDerivedFrom();
69         PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);
70
71         break;
72       }
73       case dwarf:: DW_TAG_union_type:
74       case dwarf::DW_TAG_structure_type: {
75         DICompositeType CTy = DICompositeType(Ty.getGV());
76         TypeNo = TypeNo << PIC16Dbg::S_BASIC;
77         if (Ty.getTag() == dwarf::DW_TAG_structure_type)
78           TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
79         else
80           TypeNo = TypeNo | PIC16Dbg::T_UNION;
81         CTy.getName(TypeName);
82         // UniqueSuffix is .number where number is obtained from 
83         // llvm.dbg.composite<number>.
84         std::string UniqueSuffix = "." + Ty.getGV()->getName().substr(18);
85         TypeName += UniqueSuffix;
86         unsigned short size = CTy.getSizeInBits()/8;
87         // 7th and 8th byte represent size.   
88         HasAux = true;
89         Aux[6] = size & 0xff;
90         Aux[7] = size >> 8;
91         break;
92       }
93       case dwarf::DW_TAG_enumeration_type: {
94         TypeNo = TypeNo << PIC16Dbg::S_BASIC;
95         TypeNo = TypeNo | PIC16Dbg::T_ENUM;
96         break;
97       }
98       default:
99         TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
100     }
101   }
102   else {
103     TypeNo = PIC16Dbg::T_NULL;
104     HasAux = false;
105   }
106   return;
107 }
108
109
110 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type)  {
111   if (type == "char")
112     return PIC16Dbg::T_CHAR;
113   else if (type == "short")
114     return PIC16Dbg::T_SHORT;
115   else if (type == "int")
116     return PIC16Dbg::T_INT;
117   else if (type == "long")
118     return PIC16Dbg::T_LONG;
119   else if (type == "unsigned char")
120     return PIC16Dbg::T_UCHAR;
121   else if (type == "unsigned short")
122     return PIC16Dbg::T_USHORT;
123   else if (type == "unsigned int")
124     return PIC16Dbg::T_UINT;
125   else if (type == "unsigned long")
126     return PIC16Dbg::T_ULONG;
127   else
128     return 0;
129 }
130
131 short PIC16DbgInfo::getClass(DIGlobalVariable DIGV) {
132   short ClassNo;
133   if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
134     // Generating C_AUTO here fails due to error in linker. Change it once
135     // linker is fixed.
136     ClassNo = PIC16Dbg::C_STAT;
137   }
138   else if (DIGV.isLocalToUnit())
139     ClassNo = PIC16Dbg::C_STAT;
140   else
141     ClassNo = PIC16Dbg::C_EXT;
142   return ClassNo;
143 }
144
145 void PIC16DbgInfo::Init(Module &M) {
146   // Do all debug related initializations here.
147   EmitFileDirective(M);
148   EmitCompositeTypeDecls(M);
149 }
150
151 void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
152   for(iplist<GlobalVariable>::iterator I = M.getGlobalList().begin(),
153       E = M.getGlobalList().end(); I != E; I++) {
154     // Structures and union declaration's debug info has llvm.dbg.composite
155     // in its name.
156     if(I->getName().find("llvm.dbg.composite") != std::string::npos) {
157       GlobalVariable *GV = cast<GlobalVariable >(I);
158       DICompositeType CTy(GV);
159       if (CTy.getTag() == dwarf::DW_TAG_union_type ||
160           CTy.getTag() == dwarf::DW_TAG_structure_type ) {
161         std::string name;
162         CTy.getName(name);
163         std::string DIVar = I->getName();
164         // Get the number after llvm.dbg.composite and make UniqueSuffix from 
165         // it.
166         std::string UniqueSuffix = "." + DIVar.substr(18);
167         std::string MangledCTyName = name + UniqueSuffix;
168         unsigned short size = CTy.getSizeInBits()/8;
169         int Aux[PIC16Dbg::AuxSize] = {0};
170         // 7th and 8th byte represent size of structure/union.
171         Aux[6] = size & 0xff;
172         Aux[7] = size >> 8;
173         // Emit .def for structure/union tag.
174         if( CTy.getTag() == dwarf::DW_TAG_union_type)
175           EmitSymbol(MangledCTyName, PIC16Dbg::C_UNTAG);
176         else if  (CTy.getTag() == dwarf::DW_TAG_structure_type) 
177           EmitSymbol(MangledCTyName, PIC16Dbg::C_STRTAG);
178
179         // Emit auxiliary debug information for structure/union tag. 
180         EmitAuxEntry(MangledCTyName, Aux, PIC16Dbg::AuxSize);
181         unsigned long Value = 0;
182         DIArray Elements = CTy.getTypeArray();
183         for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
184           DIDescriptor Element = Elements.getElement(i);
185           unsigned short TypeNo = 0;
186           bool HasAux = false;
187           int ElementAux[PIC16Dbg::AuxSize] = { 0 };
188           std::string TypeName = "";
189           std::string ElementName;
190           GlobalVariable *GV = Element.getGV();
191           DIDerivedType DITy(GV);
192           DITy.getName(ElementName);
193           unsigned short ElementSize = DITy.getSizeInBits()/8;
194           // Get mangleddd name for this structure/union  element.
195           std::string MangMemName = ElementName + UniqueSuffix;
196           PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TypeName);
197           short Class;
198           if( CTy.getTag() == dwarf::DW_TAG_union_type)
199             Class = PIC16Dbg::C_MOU;
200           else if  (CTy.getTag() == dwarf::DW_TAG_structure_type)
201             Class = PIC16Dbg::C_MOS;
202           EmitSymbol(MangMemName, Class, TypeNo, Value);
203           if (CTy.getTag() == dwarf::DW_TAG_structure_type)
204             Value += ElementSize;
205           if (HasAux)
206             EmitAuxEntry(MangMemName, ElementAux, PIC16Dbg::AuxSize, TypeName);
207         }
208         // Emit mangled Symbol for end of structure/union.
209         std::string EOSSymbol = ".eos" + UniqueSuffix;
210         EmitSymbol(EOSSymbol, PIC16Dbg::C_EOS);
211         EmitAuxEntry(EOSSymbol, Aux, PIC16Dbg::AuxSize, MangledCTyName);
212       }
213     }
214   }
215 }
216
217 void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
218   std::string FunctName = F->getName();
219   if (EmitDebugDirectives) {
220     std::string FunctBeginSym = ".bf." + FunctName;
221     std::string BlockBeginSym = ".bb." + FunctName;
222
223     int BFAux[PIC16Dbg::AuxSize] = {0};
224     BFAux[4] = FunctBeginLine;
225     BFAux[5] = FunctBeginLine >> 8;
226     // Emit debug directives for beginning of function.
227     EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
228     EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
229     EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
230     EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
231   }
232 }
233
234 void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
235   std::string FunctName = F->getName();
236   if (EmitDebugDirectives) {
237     std::string FunctEndSym = ".ef." + FunctName;
238     std::string BlockEndSym = ".eb." + FunctName;
239
240     // Emit debug directives for end of function.
241     EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
242     int EFAux[PIC16Dbg::AuxSize] = {0};
243     // 5th and 6th byte stand for line number.
244     EFAux[4] = Line;
245     EFAux[5] = Line >> 8;
246     EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
247     EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
248     EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
249   }
250 }
251
252 /// EmitAuxEntry - Emit Auxiliary debug information.
253 ///
254 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int num,
255                                 std::string tag) {
256   O << "\n\t.dim " << VarName << ", 1" ;
257   if (tag != "")
258     O << ", " << tag;
259   for (int i = 0; i<num; i++)
260     O << "," << Aux[i];
261 }
262
263 void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
264                               Type, unsigned long Value) {
265   O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = " 
266     << Class;
267   if (Value > 0)
268     O  << ", value = " << Value;
269 }
270
271 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
272   GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.global_variables");
273   if (!Root)
274     return;
275
276   Constant *RootC = cast<Constant>(*Root->use_begin());
277   for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
278        UI != UE; ++UI) {
279     for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
280          UUI != UUE; ++UUI) {
281       DIGlobalVariable DIGV(cast<GlobalVariable>(*UUI));
282       DIType Ty = DIGV.getType();
283       unsigned short TypeNo = 0;
284       bool HasAux = false;
285       int Aux[PIC16Dbg::AuxSize] = { 0 };
286       std::string TypeName = "";
287       std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getName();
288       PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TypeName);
289       // Emit debug info only if type information is availaible.
290       if (TypeNo != PIC16Dbg::T_NULL) {
291         O << "\n\t.type " << VarName << ", " << TypeNo;
292         short ClassNo = getClass(DIGV);
293         O << "\n\t.class " << VarName << ", " << ClassNo;
294         if (HasAux) 
295           EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TypeName);
296       }
297     }
298   }
299   O << "\n";
300 }
301
302 void PIC16DbgInfo::EmitFileDirective(Module &M) {
303   GlobalVariable *CU = M.getNamedGlobal("llvm.dbg.compile_unit");
304   if (CU) {
305     EmitDebugDirectives = true;
306     EmitFileDirective(CU, false);
307   }
308 }
309
310 void PIC16DbgInfo::EmitFileDirective(GlobalVariable *CU, bool EmitEof) {
311   std::string Dir, FN;
312   DICompileUnit DIUnit(CU);
313   std::string File = DIUnit.getDirectory(Dir) + "/" + DIUnit.getFilename(FN);
314   if ( File != CurFile ) {
315     if (EmitEof)
316       EmitEOF();
317     O << "\n\t.file\t\"" << File << "\"\n" ;
318     CurFile = File;
319   }
320 }
321
322 void PIC16DbgInfo::EmitEOF() {
323   if (CurFile != "")
324     O << "\n\t.EOF";
325 }
326
327 void PIC16DbgInfo::SetFunctBeginLine(unsigned line) {
328   FunctBeginLine = line;
329 }