blob: 8368a3c142cea43debf9bf972690712a4728c82c [file] [log] [blame]
Devang Patele4b27562009-08-28 23:24:31 +00001
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +00002//===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains the helper functions for representing debug information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PIC16.h"
Sanjiv Gupta753ec152009-10-15 19:26:25 +000016#include "PIC16ABINames.h"
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +000017#include "PIC16DebugInfo.h"
18#include "llvm/GlobalVariable.h"
Sanjiv Guptabde79422009-06-16 09:45:18 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere4d110b2009-08-22 20:56:12 +000020#include "llvm/MC/MCAsmInfo.h"
Devang Patel1e86a662009-06-19 22:08:58 +000021#include "llvm/Support/DebugLoc.h"
David Greene71847812009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Devang Patel715c6622009-08-10 22:11:20 +000023#include "llvm/ADT/SmallString.h"
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +000024
25using namespace llvm;
26
Sanjiv Guptabde79422009-06-16 09:45:18 +000027/// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
28///
29void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo,
30 bool &HasAux, int Aux[],
31 std::string &TagName) {
Devang Patel6ceea332009-08-31 18:49:10 +000032 if (Ty.isBasicType())
Sanjiv Guptabde79422009-06-16 09:45:18 +000033 PopulateBasicTypeInfo (Ty, TypeNo);
Devang Patel6ceea332009-08-31 18:49:10 +000034 else if (Ty.isCompositeType())
Sanjiv Guptabde79422009-06-16 09:45:18 +000035 PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
Sanjiv Guptacf1e5be2009-10-15 09:48:25 +000036 else if (Ty.isDerivedType())
37 PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +000038 else {
39 TypeNo = PIC16Dbg::T_NULL;
40 HasAux = false;
41 }
42 return;
43}
44
Sanjiv Guptabde79422009-06-16 09:45:18 +000045/// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
46///
47void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
Devang Patel5ccdd102009-09-29 18:40:58 +000048 std::string Name = Ty.getName();
Sanjiv Guptabde79422009-06-16 09:45:18 +000049 unsigned short BaseTy = GetTypeDebugNumber(Name);
50 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
51 TypeNo = TypeNo | (0xffff & BaseTy);
52}
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +000053
Sanjiv Guptabde79422009-06-16 09:45:18 +000054/// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type
55/// from Ty. Derived types are mostly pointers.
56///
57void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
58 bool &HasAux, int Aux[],
59 std::string &TagName) {
60
61 switch(Ty.getTag())
62 {
63 case dwarf::DW_TAG_pointer_type:
64 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
65 TypeNo = TypeNo | PIC16Dbg::DT_PTR;
66 break;
67 default:
68 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
69 }
70
71 // We also need to encode the the information about the base type of
72 // pointer in TypeNo.
Devang Patele4b27562009-08-28 23:24:31 +000073 DIType BaseType = DIDerivedType(Ty.getNode()).getTypeDerivedFrom();
Sanjiv Guptabde79422009-06-16 09:45:18 +000074 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
75}
76
77/// PopulateArrayTypeInfo - Populate TypeNo, Aux[] for array from Ty.
78void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo,
79 bool &HasAux, int Aux[],
80 std::string &TagName) {
81
Devang Patele4b27562009-08-28 23:24:31 +000082 DICompositeType CTy = DICompositeType(Ty.getNode());
Sanjiv Guptabde79422009-06-16 09:45:18 +000083 DIArray Elements = CTy.getTypeArray();
84 unsigned short size = 1;
85 unsigned short Dimension[4]={0,0,0,0};
86 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
87 DIDescriptor Element = Elements.getElement(i);
88 if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
89 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
90 TypeNo = TypeNo | PIC16Dbg::DT_ARY;
Devang Patele4b27562009-08-28 23:24:31 +000091 DISubrange SubRange = DISubrange(Element.getNode());
Sanjiv Guptabde79422009-06-16 09:45:18 +000092 Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
93 // Each dimension is represented by 2 bytes starting at byte 9.
94 Aux[8+i*2+0] = Dimension[i];
95 Aux[8+i*2+1] = Dimension[i] >> 8;
96 size = size * Dimension[i];
97 }
98 }
99 HasAux = true;
100 // In auxillary entry for array, 7th and 8th byte represent array size.
101 Aux[6] = size & 0xff;
102 Aux[7] = size >> 8;
103 DIType BaseType = CTy.getTypeDerivedFrom();
104 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
105}
106
107/// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for
108/// structure or union.
109///
110void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty,
111 unsigned short &TypeNo,
112 bool &HasAux, int Aux[],
113 std::string &TagName) {
Devang Patele4b27562009-08-28 23:24:31 +0000114 DICompositeType CTy = DICompositeType(Ty.getNode());
Sanjiv Guptabde79422009-06-16 09:45:18 +0000115 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
116 if (Ty.getTag() == dwarf::DW_TAG_structure_type)
117 TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
118 else
119 TypeNo = TypeNo | PIC16Dbg::T_UNION;
Devang Patel5ccdd102009-09-29 18:40:58 +0000120 TagName = CTy.getName();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000121 // UniqueSuffix is .number where number is obtained from
122 // llvm.dbg.composite<number>.
Devang Patel715c6622009-08-10 22:11:20 +0000123 // FIXME: This will break when composite type is not represented by
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000124 // llvm.dbg.composite* global variable. Since we need to revisit
125 // PIC16DebugInfo implementation anyways after the MDNodes based
126 // framework is done, let us continue with the way it is.
Devang Patele4b27562009-08-28 23:24:31 +0000127 std::string UniqueSuffix = "." + Ty.getNode()->getNameStr().substr(18);
Sanjiv Guptabde79422009-06-16 09:45:18 +0000128 TagName += UniqueSuffix;
129 unsigned short size = CTy.getSizeInBits()/8;
130 // 7th and 8th byte represent size.
131 HasAux = true;
132 Aux[6] = size & 0xff;
133 Aux[7] = size >> 8;
134}
135
136/// PopulateEnumTypeInfo - Populate TypeNo for enum from Ty.
137void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) {
138 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
139 TypeNo = TypeNo | PIC16Dbg::T_ENUM;
140}
141
142/// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for
143/// composite types from Ty.
144///
145void PIC16DbgInfo::PopulateCompositeTypeInfo (DIType Ty, unsigned short &TypeNo,
146 bool &HasAux, int Aux[],
147 std::string &TagName) {
148 switch (Ty.getTag()) {
149 case dwarf::DW_TAG_array_type: {
150 PopulateArrayTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
151 break;
152 }
153 case dwarf:: DW_TAG_union_type:
154 case dwarf::DW_TAG_structure_type: {
155 PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
156 break;
157 }
158 case dwarf::DW_TAG_enumeration_type: {
159 PopulateEnumTypeInfo (Ty, TypeNo);
160 break;
161 }
162 default:
163 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
164 }
165}
166
167/// GetTypeDebugNumber - Get debug type number for given type.
168///
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000169unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) {
170 if (type == "char")
171 return PIC16Dbg::T_CHAR;
172 else if (type == "short")
173 return PIC16Dbg::T_SHORT;
174 else if (type == "int")
175 return PIC16Dbg::T_INT;
176 else if (type == "long")
177 return PIC16Dbg::T_LONG;
178 else if (type == "unsigned char")
179 return PIC16Dbg::T_UCHAR;
180 else if (type == "unsigned short")
181 return PIC16Dbg::T_USHORT;
182 else if (type == "unsigned int")
183 return PIC16Dbg::T_UINT;
184 else if (type == "unsigned long")
185 return PIC16Dbg::T_ULONG;
186 else
187 return 0;
188}
Sanjiv Guptabde79422009-06-16 09:45:18 +0000189
190/// GetStorageClass - Get storage class for give debug variable.
191///
192short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000193 short ClassNo;
Sanjiv Guptacf1e5be2009-10-15 09:48:25 +0000194 if (PAN::isLocalName(DIGV.getName())) {
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000195 // Generating C_AUTO here fails due to error in linker. Change it once
196 // linker is fixed.
197 ClassNo = PIC16Dbg::C_STAT;
198 }
199 else if (DIGV.isLocalToUnit())
200 ClassNo = PIC16Dbg::C_STAT;
201 else
202 ClassNo = PIC16Dbg::C_EXT;
203 return ClassNo;
204}
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000205
Sanjiv Guptabde79422009-06-16 09:45:18 +0000206/// BeginModule - Emit necessary debug info to start a Module and do other
207/// required initializations.
208void PIC16DbgInfo::BeginModule(Module &M) {
209 // Emit file directive for module.
Devang Patel22516662009-08-06 20:53:24 +0000210 DebugInfoFinder DbgFinder;
211 DbgFinder.processModule(M);
212 if (DbgFinder.compile_unit_count() != 0) {
Devang Patel92c55112009-07-06 23:28:36 +0000213 // FIXME : What if more then one CUs are present in a module ?
Devang Patele4b27562009-08-28 23:24:31 +0000214 MDNode *CU = *DbgFinder.compile_unit_begin();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000215 EmitDebugDirectives = true;
216 SwitchToCU(CU);
217 }
Sanjiv Guptabde79422009-06-16 09:45:18 +0000218 // Emit debug info for decls of composite types.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000219 EmitCompositeTypeDecls(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000220}
221
Sanjiv Guptabde79422009-06-16 09:45:18 +0000222/// Helper to find first valid debug loc for a function.
223///
224static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
225 DebugLoc DL;
226 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
227 I != E; ++I) {
228 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
229 II != E; ++II) {
230 DL = II->getDebugLoc();
231 if (!DL.isUnknown())
232 return DL;
233 }
234 }
235 return DL;
236}
237
238/// BeginFunction - Emit necessary debug info to start a function.
239///
240void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
241 if (! EmitDebugDirectives) return;
242
243 // Retreive the first valid debug Loc and process it.
244 const DebugLoc &DL = GetDebugLocForFunction(MF);
Sanjiv Gupta394a1a22009-08-07 11:00:02 +0000245 // Emit debug info only if valid debug info is available.
246 if (!DL.isUnknown()) {
247 ChangeDebugLoc(MF, DL, true);
248 EmitFunctBeginDI(MF.getFunction());
249 }
Sanjiv Guptabde79422009-06-16 09:45:18 +0000250 // Set current line to 0 so that.line directive is genearted after .bf.
251 CurLine = 0;
252}
253
254/// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
255/// CurFile and CurLine may change as a result of this.
256///
257void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,
258 const DebugLoc &DL, bool IsInBeginFunction) {
259 if (! EmitDebugDirectives) return;
260 assert (! DL.isUnknown() && "can't change to invalid debug loc");
261
Devang Patel6b61f582010-01-16 06:09:35 +0000262 DILocation Loc = MF.getDILocation(DL);
263 MDNode *CU = Loc.getScope().getNode();
264 unsigned line = Loc.getLineNumber();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000265
266 SwitchToCU(CU);
267 SwitchToLine(line, IsInBeginFunction);
268}
269
270/// SwitchToLine - Emit line directive for a new line.
271///
272void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
273 if (CurLine == Line) return;
274 if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n";
275 CurLine = Line;
276}
277
278/// EndFunction - Emit .ef for end of function.
279///
280void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
281 if (! EmitDebugDirectives) return;
Sanjiv Gupta394a1a22009-08-07 11:00:02 +0000282 const DebugLoc &DL = GetDebugLocForFunction(MF);
283 // Emit debug info only if valid debug info is available.
284 if (!DL.isUnknown())
285 EmitFunctEndDI(MF.getFunction(), CurLine);
Sanjiv Guptabde79422009-06-16 09:45:18 +0000286}
287
288/// EndModule - Emit .eof for end of module.
289///
290void PIC16DbgInfo::EndModule(Module &M) {
291 if (! EmitDebugDirectives) return;
292 EmitVarDebugInfo(M);
293 if (CurFile != "") O << "\n\t.eof";
294}
295
296/// EmitCompositeTypeElements - Emit debug information for members of a
297/// composite type.
298///
299void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000300 std::string SuffixNo) {
Sanjiv Guptabde79422009-06-16 09:45:18 +0000301 unsigned long Value = 0;
302 DIArray Elements = CTy.getTypeArray();
303 for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
304 DIDescriptor Element = Elements.getElement(i);
305 unsigned short TypeNo = 0;
306 bool HasAux = false;
307 int ElementAux[PIC16Dbg::AuxSize] = { 0 };
308 std::string TagName = "";
Devang Patele4b27562009-08-28 23:24:31 +0000309 DIDerivedType DITy(Element.getNode());
Sanjiv Guptabde79422009-06-16 09:45:18 +0000310 unsigned short ElementSize = DITy.getSizeInBits()/8;
311 // Get mangleddd name for this structure/union element.
Benjamin Kramer1c3451f2009-11-25 18:26:09 +0000312 std::string MangMemName = DITy.getName().str() + SuffixNo;
Sanjiv Guptabde79422009-06-16 09:45:18 +0000313 PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName);
Daniel Dunbar1c723b72009-06-26 02:03:52 +0000314 short Class = 0;
Sanjiv Guptabde79422009-06-16 09:45:18 +0000315 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;
Devang Patel715c6622009-08-10 22:11:20 +0000319 EmitSymbol(MangMemName.c_str(), Class, TypeNo, Value);
Sanjiv Guptabde79422009-06-16 09:45:18 +0000320 if (CTy.getTag() == dwarf::DW_TAG_structure_type)
321 Value += ElementSize;
322 if (HasAux)
Devang Patel715c6622009-08-10 22:11:20 +0000323 EmitAuxEntry(MangMemName.c_str(), ElementAux, PIC16Dbg::AuxSize, TagName);
Sanjiv Guptabde79422009-06-16 09:45:18 +0000324 }
325}
326
327/// EmitCompositeTypeDecls - Emit composite type declarations like structure
328/// and union declarations.
329///
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000330void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
Devang Patel715c6622009-08-10 22:11:20 +0000331 DebugInfoFinder DbgFinder;
332 DbgFinder.processModule(M);
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000333 for (DebugInfoFinder::iterator I = DbgFinder.type_begin(),
334 E = DbgFinder.type_end(); I != E; ++I) {
Devang Patel715c6622009-08-10 22:11:20 +0000335 DICompositeType CTy(*I);
336 if (CTy.isNull())
337 continue;
338 if (CTy.getTag() == dwarf::DW_TAG_union_type ||
339 CTy.getTag() == dwarf::DW_TAG_structure_type ) {
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000340 // Get the number after llvm.dbg.composite and make UniqueSuffix from
341 // it.
Devang Patele4b27562009-08-28 23:24:31 +0000342 std::string DIVar = CTy.getNode()->getNameStr();
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000343 std::string UniqueSuffix = "." + DIVar.substr(18);
Benjamin Kramer1c3451f2009-11-25 18:26:09 +0000344 std::string MangledCTyName = CTy.getName().str() + UniqueSuffix;
Devang Patel715c6622009-08-10 22:11:20 +0000345 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.
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000360 EmitCompositeTypeElements (CTy, UniqueSuffix);
Devang Patel715c6622009-08-10 22:11:20 +0000361
362 // Emit mangled Symbol for end of structure/union.
Sanjiv Guptabfa79b82009-08-15 14:36:48 +0000363 std::string EOSSymbol = ".eos" + UniqueSuffix;
Devang Patel715c6622009-08-10 22:11:20 +0000364 EmitSymbol(EOSSymbol.c_str(), PIC16Dbg::C_EOS);
365 EmitAuxEntry(EOSSymbol.c_str(), Aux, PIC16Dbg::AuxSize,
366 MangledCTyName.c_str());
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000367 }
368 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000369}
370
Devang Patel715c6622009-08-10 22:11:20 +0000371
Sanjiv Guptabde79422009-06-16 09:45:18 +0000372/// EmitFunctBeginDI - Emit .bf for function.
373///
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000374void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
375 std::string FunctName = F->getName();
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000376 if (EmitDebugDirectives) {
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000377 std::string FunctBeginSym = ".bf." + FunctName;
378 std::string BlockBeginSym = ".bb." + FunctName;
379
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000380 int BFAux[PIC16Dbg::AuxSize] = {0};
Sanjiv Guptabde79422009-06-16 09:45:18 +0000381 BFAux[4] = CurLine;
382 BFAux[5] = CurLine >> 8;
383
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000384 // Emit debug directives for beginning of function.
385 EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
386 EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
Sanjiv Guptabde79422009-06-16 09:45:18 +0000387
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000388 EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
389 EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
390 }
391}
392
Sanjiv Guptabde79422009-06-16 09:45:18 +0000393/// EmitFunctEndDI - Emit .ef for function end.
394///
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000395void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
396 std::string FunctName = F->getName();
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000397 if (EmitDebugDirectives) {
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000398 std::string FunctEndSym = ".ef." + FunctName;
399 std::string BlockEndSym = ".eb." + FunctName;
400
401 // Emit debug directives for end of function.
402 EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
403 int EFAux[PIC16Dbg::AuxSize] = {0};
404 // 5th and 6th byte stand for line number.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000405 EFAux[4] = CurLine;
406 EFAux[5] = CurLine >> 8;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000407 EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
408 EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
409 EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
410 }
411}
412
413/// EmitAuxEntry - Emit Auxiliary debug information.
414///
Sanjiv Guptabde79422009-06-16 09:45:18 +0000415void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
416 std::string TagName) {
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000417 O << "\n\t.dim " << VarName << ", 1" ;
Sanjiv Guptabde79422009-06-16 09:45:18 +0000418 // TagName is emitted in case of structure/union objects.
419 if (TagName != "")
420 O << ", " << TagName;
421 for (int i = 0; i<Num; i++)
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000422 O << "," << Aux[i];
423}
424
Sanjiv Guptabde79422009-06-16 09:45:18 +0000425/// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
426///
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000427void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
428 Type, unsigned long Value) {
429 O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = "
430 << Class;
431 if (Value > 0)
432 O << ", value = " << Value;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000433}
434
Sanjiv Guptabde79422009-06-16 09:45:18 +0000435/// EmitVarDebugInfo - Emit debug information for all variables.
436///
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000437void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
Devang Patel22516662009-08-06 20:53:24 +0000438 DebugInfoFinder DbgFinder;
439 DbgFinder.processModule(M);
Devang Patele4b27562009-08-28 23:24:31 +0000440
Devang Patel22516662009-08-06 20:53:24 +0000441 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
442 E = DbgFinder.global_variable_end(); I != E; ++I) {
Devang Patel92c55112009-07-06 23:28:36 +0000443 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 = "";
Sanjiv Guptacf1e5be2009-10-15 09:48:25 +0000449 std::string VarName = DIGV.getName();
450 VarName = MAI->getGlobalPrefix() + VarName;
Devang Patel92c55112009-07-06 23:28:36 +0000451 PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
452 // Emit debug info only if type information is availaible.
453 if (TypeNo != PIC16Dbg::T_NULL) {
454 O << "\n\t.type " << VarName << ", " << TypeNo;
455 short ClassNo = getStorageClass(DIGV);
456 O << "\n\t.class " << VarName << ", " << ClassNo;
457 if (HasAux)
458 EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000459 }
460 }
461 O << "\n";
462}
463
Sanjiv Guptabde79422009-06-16 09:45:18 +0000464/// SwitchToCU - Switch to a new compilation unit.
465///
Devang Patele4b27562009-08-28 23:24:31 +0000466void PIC16DbgInfo::SwitchToCU(MDNode *CU) {
Sanjiv Guptabde79422009-06-16 09:45:18 +0000467 // Get the file path from CU.
468 DICompileUnit cu(CU);
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000469 std::string DirName = cu.getDirectory();
470 std::string FileName = cu.getFilename();
471 std::string FilePath = DirName + "/" + FileName;
Sanjiv Guptabde79422009-06-16 09:45:18 +0000472
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000473 // Nothing to do if source file is still same.
474 if ( FilePath == CurFile ) return;
Sanjiv Guptabde79422009-06-16 09:45:18 +0000475
476 // Else, close the current one and start a new.
477 if (CurFile != "") O << "\n\t.eof";
478 O << "\n\t.file\t\"" << FilePath << "\"\n" ;
479 CurFile = FilePath;
480 CurLine = 0;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000481}
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000482
Sanjiv Guptabde79422009-06-16 09:45:18 +0000483/// EmitEOF - Emit .eof for end of file.
484///
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000485void PIC16DbgInfo::EmitEOF() {
486 if (CurFile != "")
487 O << "\n\t.EOF";
488}
489