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/Support/raw_ostream.h"
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 = "";
27 unsigned short BaseTy = GetTypeDebugNumber(Name);
28 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
29 TypeNo = TypeNo | (0xffff & BaseTy);
31 else if (Ty.isDerivedType(Ty.getTag())) {
34 case dwarf::DW_TAG_pointer_type:
35 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
36 TypeNo = TypeNo | PIC16Dbg::DT_PTR;
39 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
41 DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
42 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);
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];
65 // In auxillary entry for array, 7th and 8th byte represent array size.
68 DIType BaseType = CTy.getTypeDerivedFrom();
69 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);
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;
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.
93 case dwarf::DW_TAG_enumeration_type: {
94 TypeNo = TypeNo << PIC16Dbg::S_BASIC;
95 TypeNo = TypeNo | PIC16Dbg::T_ENUM;
99 TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
103 TypeNo = PIC16Dbg::T_NULL;
110 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) {
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;
131 short PIC16DbgInfo::getClass(DIGlobalVariable DIGV) {
133 if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
134 // Generating C_AUTO here fails due to error in linker. Change it once
136 ClassNo = PIC16Dbg::C_STAT;
138 else if (DIGV.isLocalToUnit())
139 ClassNo = PIC16Dbg::C_STAT;
141 ClassNo = PIC16Dbg::C_EXT;
145 void PIC16DbgInfo::Init(Module &M) {
146 // Do all debug related initializations here.
147 EmitFileDirective(M);
148 EmitCompositeTypeDecls(M);
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
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 ) {
163 std::string DIVar = I->getName();
164 // Get the number after llvm.dbg.composite and make UniqueSuffix from
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;
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);
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;
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);
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;
206 EmitAuxEntry(MangMemName, ElementAux, PIC16Dbg::AuxSize, TypeName);
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);
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;
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);
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;
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.
245 EFAux[5] = Line >> 8;
246 EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
247 EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
248 EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
252 /// EmitAuxEntry - Emit Auxiliary debug information.
254 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int num,
256 O << "\n\t.dim " << VarName << ", 1" ;
259 for (int i = 0; i<num; i++)
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 = "
268 O << ", value = " << Value;
271 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
272 GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.global_variables");
276 Constant *RootC = cast<Constant>(*Root->use_begin());
277 for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
279 for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
281 DIGlobalVariable DIGV(cast<GlobalVariable>(*UUI));
282 DIType Ty = DIGV.getType();
283 unsigned short TypeNo = 0;
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;
295 EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TypeName);
302 void PIC16DbgInfo::EmitFileDirective(Module &M) {
303 GlobalVariable *CU = M.getNamedGlobal("llvm.dbg.compile_unit");
305 EmitDebugDirectives = true;
306 EmitFileDirective(CU, false);
310 void PIC16DbgInfo::EmitFileDirective(GlobalVariable *CU, bool EmitEof) {
312 DICompileUnit DIUnit(CU);
313 std::string File = DIUnit.getDirectory(Dir) + "/" + DIUnit.getFilename(FN);
314 if ( File != CurFile ) {
317 O << "\n\t.file\t\"" << File << "\"\n" ;
322 void PIC16DbgInfo::EmitEOF() {
327 void PIC16DbgInfo::SetFunctBeginLine(unsigned line) {
328 FunctBeginLine = line;