LLVM API Documentation
00001 //===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the helper functions for representing debug information. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "PIC16.h" 00015 #include "PIC16DebugInfo.h" 00016 #include "llvm/GlobalVariable.h" 00017 #include "llvm/CodeGen/MachineFunction.h" 00018 #include "llvm/Support/DebugLoc.h" 00019 #include "llvm/Support/FormattedStream.h" 00020 #include "llvm/ADT/SmallString.h" 00021 00022 using namespace llvm; 00023 00024 /// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty. 00025 /// 00026 void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo, 00027 bool &HasAux, int Aux[], 00028 std::string &TagName) { 00029 if (Ty.isBasicType(Ty.getTag())) 00030 PopulateBasicTypeInfo (Ty, TypeNo); 00031 else if (Ty.isDerivedType(Ty.getTag())) 00032 PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName); 00033 else if (Ty.isCompositeType(Ty.getTag())) 00034 PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName); 00035 else { 00036 TypeNo = PIC16Dbg::T_NULL; 00037 HasAux = false; 00038 } 00039 return; 00040 } 00041 00042 /// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty. 00043 /// 00044 void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) { 00045 std::string Name = ""; 00046 Ty.getName(Name); 00047 unsigned short BaseTy = GetTypeDebugNumber(Name); 00048 TypeNo = TypeNo << PIC16Dbg::S_BASIC; 00049 TypeNo = TypeNo | (0xffff & BaseTy); 00050 } 00051 00052 /// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type 00053 /// from Ty. Derived types are mostly pointers. 00054 /// 00055 void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo, 00056 bool &HasAux, int Aux[], 00057 std::string &TagName) { 00058 00059 switch(Ty.getTag()) 00060 { 00061 case dwarf::DW_TAG_pointer_type: 00062 TypeNo = TypeNo << PIC16Dbg::S_DERIVED; 00063 TypeNo = TypeNo | PIC16Dbg::DT_PTR; 00064 break; 00065 default: 00066 TypeNo = TypeNo << PIC16Dbg::S_DERIVED; 00067 } 00068 00069 // We also need to encode the the information about the base type of 00070 // pointer in TypeNo. 00071 DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom(); 00072 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName); 00073 } 00074 00075 /// PopulateArrayTypeInfo - Populate TypeNo, Aux[] for array from Ty. 00076 void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo, 00077 bool &HasAux, int Aux[], 00078 std::string &TagName) { 00079 00080 DICompositeType CTy = DICompositeType(Ty.getGV()); 00081 DIArray Elements = CTy.getTypeArray(); 00082 unsigned short size = 1; 00083 unsigned short Dimension[4]={0,0,0,0}; 00084 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 00085 DIDescriptor Element = Elements.getElement(i); 00086 if (Element.getTag() == dwarf::DW_TAG_subrange_type) { 00087 TypeNo = TypeNo << PIC16Dbg::S_DERIVED; 00088 TypeNo = TypeNo | PIC16Dbg::DT_ARY; 00089 DISubrange SubRange = DISubrange(Element.getGV()); 00090 Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1; 00091 // Each dimension is represented by 2 bytes starting at byte 9. 00092 Aux[8+i*2+0] = Dimension[i]; 00093 Aux[8+i*2+1] = Dimension[i] >> 8; 00094 size = size * Dimension[i]; 00095 } 00096 } 00097 HasAux = true; 00098 // In auxillary entry for array, 7th and 8th byte represent array size. 00099 Aux[6] = size & 0xff; 00100 Aux[7] = size >> 8; 00101 DIType BaseType = CTy.getTypeDerivedFrom(); 00102 PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName); 00103 } 00104 00105 /// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for 00106 /// structure or union. 00107 /// 00108 void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty, 00109 unsigned short &TypeNo, 00110 bool &HasAux, int Aux[], 00111 std::string &TagName) { 00112 DICompositeType CTy = DICompositeType(Ty.getGV()); 00113 TypeNo = TypeNo << PIC16Dbg::S_BASIC; 00114 if (Ty.getTag() == dwarf::DW_TAG_structure_type) 00115 TypeNo = TypeNo | PIC16Dbg::T_STRUCT; 00116 else 00117 TypeNo = TypeNo | PIC16Dbg::T_UNION; 00118 CTy.getName(TagName); 00119 // UniqueSuffix is .number where number is obtained from 00120 // llvm.dbg.composite<number>. 00121 // FIXME: This will break when composite type is not represented by 00122 // llvm.dbg.composite* global variable. Since we need to revisit 00123 // PIC16DebugInfo implementation anyways after the MDNodes based 00124 // framework is done, let us continue with the way it is. 00125 std::string UniqueSuffix = "." + Ty.getGV()->getNameStr().substr(18); 00126 TagName += UniqueSuffix; 00127 unsigned short size = CTy.getSizeInBits()/8; 00128 // 7th and 8th byte represent size. 00129 HasAux = true; 00130 Aux[6] = size & 0xff; 00131 Aux[7] = size >> 8; 00132 } 00133 00134 /// PopulateEnumTypeInfo - Populate TypeNo for enum from Ty. 00135 void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) { 00136 TypeNo = TypeNo << PIC16Dbg::S_BASIC; 00137 TypeNo = TypeNo | PIC16Dbg::T_ENUM; 00138 } 00139 00140 /// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for 00141 /// composite types from Ty. 00142 /// 00143 void PIC16DbgInfo::PopulateCompositeTypeInfo (DIType Ty, unsigned short &TypeNo, 00144 bool &HasAux, int Aux[], 00145 std::string &TagName) { 00146 switch (Ty.getTag()) { 00147 case dwarf::DW_TAG_array_type: { 00148 PopulateArrayTypeInfo (Ty, TypeNo, HasAux, Aux, TagName); 00149 break; 00150 } 00151 case dwarf:: DW_TAG_union_type: 00152 case dwarf::DW_TAG_structure_type: { 00153 PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName); 00154 break; 00155 } 00156 case dwarf::DW_TAG_enumeration_type: { 00157 PopulateEnumTypeInfo (Ty, TypeNo); 00158 break; 00159 } 00160 default: 00161 TypeNo = TypeNo << PIC16Dbg::S_DERIVED; 00162 } 00163 } 00164 00165 /// GetTypeDebugNumber - Get debug type number for given type. 00166 /// 00167 unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) { 00168 if (type == "char") 00169 return PIC16Dbg::T_CHAR; 00170 else if (type == "short") 00171 return PIC16Dbg::T_SHORT; 00172 else if (type == "int") 00173 return PIC16Dbg::T_INT; 00174 else if (type == "long") 00175 return PIC16Dbg::T_LONG; 00176 else if (type == "unsigned char") 00177 return PIC16Dbg::T_UCHAR; 00178 else if (type == "unsigned short") 00179 return PIC16Dbg::T_USHORT; 00180 else if (type == "unsigned int") 00181 return PIC16Dbg::T_UINT; 00182 else if (type == "unsigned long") 00183 return PIC16Dbg::T_ULONG; 00184 else 00185 return 0; 00186 } 00187 00188 /// GetStorageClass - Get storage class for give debug variable. 00189 /// 00190 short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) { 00191 short ClassNo; 00192 if (PAN::isLocalName(DIGV.getGlobal()->getName())) { 00193 // Generating C_AUTO here fails due to error in linker. Change it once 00194 // linker is fixed. 00195 ClassNo = PIC16Dbg::C_STAT; 00196 } 00197 else if (DIGV.isLocalToUnit()) 00198 ClassNo = PIC16Dbg::C_STAT; 00199 else 00200 ClassNo = PIC16Dbg::C_EXT; 00201 return ClassNo; 00202 } 00203 00204 /// BeginModule - Emit necessary debug info to start a Module and do other 00205 /// required initializations. 00206 void PIC16DbgInfo::BeginModule(Module &M) { 00207 // Emit file directive for module. 00208 DebugInfoFinder DbgFinder; 00209 DbgFinder.processModule(M); 00210 if (DbgFinder.compile_unit_count() != 0) { 00211 // FIXME : What if more then one CUs are present in a module ? 00212 GlobalVariable *CU = *DbgFinder.compile_unit_begin(); 00213 EmitDebugDirectives = true; 00214 SwitchToCU(CU); 00215 } 00216 00217 // Emit debug info for decls of composite types. 00218 EmitCompositeTypeDecls(M); 00219 } 00220 00221 /// Helper to find first valid debug loc for a function. 00222 /// 00223 static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) { 00224 DebugLoc DL; 00225 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00226 I != E; ++I) { 00227 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00228 II != E; ++II) { 00229 DL = II->getDebugLoc(); 00230 if (!DL.isUnknown()) 00231 return DL; 00232 } 00233 } 00234 return DL; 00235 } 00236 00237 /// BeginFunction - Emit necessary debug info to start a function. 00238 /// 00239 void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) { 00240 if (! EmitDebugDirectives) return; 00241 00242 // Retreive the first valid debug Loc and process it. 00243 const DebugLoc &DL = GetDebugLocForFunction(MF); 00244 // Emit debug info only if valid debug info is available. 00245 if (!DL.isUnknown()) { 00246 ChangeDebugLoc(MF, DL, true); 00247 EmitFunctBeginDI(MF.getFunction()); 00248 } 00249 // Set current line to 0 so that.line directive is genearted after .bf. 00250 CurLine = 0; 00251 } 00252 00253 /// ChangeDebugLoc - Take necessary steps when DebugLoc changes. 00254 /// CurFile and CurLine may change as a result of this. 00255 /// 00256 void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF, 00257 const DebugLoc &DL, bool IsInBeginFunction) { 00258 if (! EmitDebugDirectives) return; 00259 assert (! DL.isUnknown() && "can't change to invalid debug loc"); 00260 00261 GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit; 00262 unsigned line = MF.getDebugLocTuple(DL).Line; 00263 00264 SwitchToCU(CU); 00265 SwitchToLine(line, IsInBeginFunction); 00266 } 00267 00268 /// SwitchToLine - Emit line directive for a new line. 00269 /// 00270 void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) { 00271 if (CurLine == Line) return; 00272 if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n"; 00273 CurLine = Line; 00274 } 00275 00276 /// EndFunction - Emit .ef for end of function. 00277 /// 00278 void PIC16DbgInfo::EndFunction(const MachineFunction &MF) { 00279 if (! EmitDebugDirectives) return; 00280 const DebugLoc &DL = GetDebugLocForFunction(MF); 00281 // Emit debug info only if valid debug info is available. 00282 if (!DL.isUnknown()) 00283 EmitFunctEndDI(MF.getFunction(), CurLine); 00284 } 00285 00286 /// EndModule - Emit .eof for end of module. 00287 /// 00288 void PIC16DbgInfo::EndModule(Module &M) { 00289 if (! EmitDebugDirectives) return; 00290 EmitVarDebugInfo(M); 00291 if (CurFile != "") O << "\n\t.eof"; 00292 } 00293 00294 /// EmitCompositeTypeElements - Emit debug information for members of a 00295 /// composite type. 00296 /// 00297 void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy, 00298 std::string SuffixNo) { 00299 unsigned long Value = 0; 00300 DIArray Elements = CTy.getTypeArray(); 00301 for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) { 00302 DIDescriptor Element = Elements.getElement(i); 00303 unsigned short TypeNo = 0; 00304 bool HasAux = false; 00305 int ElementAux[PIC16Dbg::AuxSize] = { 0 }; 00306 std::string TagName = ""; 00307 std::string ElementName; 00308 GlobalVariable *GV = Element.getGV(); 00309 DIDerivedType DITy(GV); 00310 DITy.getName(ElementName); 00311 unsigned short ElementSize = DITy.getSizeInBits()/8; 00312 // Get mangleddd name for this structure/union element. 00313 std::string MangMemName = ElementName + SuffixNo; 00314 PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName); 00315 short Class = 0; 00316 if( CTy.getTag() == dwarf::DW_TAG_union_type) 00317 Class = PIC16Dbg::C_MOU; 00318 else if (CTy.getTag() == dwarf::DW_TAG_structure_type) 00319 Class = PIC16Dbg::C_MOS; 00320 EmitSymbol(MangMemName.c_str(), Class, TypeNo, Value); 00321 if (CTy.getTag() == dwarf::DW_TAG_structure_type) 00322 Value += ElementSize; 00323 if (HasAux) 00324 EmitAuxEntry(MangMemName.c_str(), ElementAux, PIC16Dbg::AuxSize, TagName); 00325 } 00326 } 00327 00328 /// EmitCompositeTypeDecls - Emit composite type declarations like structure 00329 /// and union declarations. 00330 /// 00331 void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) { 00332 DebugInfoFinder DbgFinder; 00333 DbgFinder.processModule(M); 00334 for (DebugInfoFinder::iterator I = DbgFinder.type_begin(), 00335 E = DbgFinder.type_end(); I != E; ++I) { 00336 DICompositeType CTy(*I); 00337 if (CTy.isNull()) 00338 continue; 00339 if (CTy.getTag() == dwarf::DW_TAG_union_type || 00340 CTy.getTag() == dwarf::DW_TAG_structure_type ) { 00341 std::string Name; 00342 CTy.getName(Name); 00343 // Get the number after llvm.dbg.composite and make UniqueSuffix from 00344 // it. 00345 std::string DIVar = CTy.getGV()->getNameStr(); 00346 std::string UniqueSuffix = "." + DIVar.substr(18); 00347 std::string MangledCTyName = Name + UniqueSuffix; 00348 unsigned short size = CTy.getSizeInBits()/8; 00349 int Aux[PIC16Dbg::AuxSize] = {0}; 00350 // 7th and 8th byte represent size of structure/union. 00351 Aux[6] = size & 0xff; 00352 Aux[7] = size >> 8; 00353 // Emit .def for structure/union tag. 00354 if( CTy.getTag() == dwarf::DW_TAG_union_type) 00355 EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_UNTAG); 00356 else if (CTy.getTag() == dwarf::DW_TAG_structure_type) 00357 EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_STRTAG); 00358 00359 // Emit auxiliary debug information for structure/union tag. 00360 EmitAuxEntry(MangledCTyName.c_str(), Aux, PIC16Dbg::AuxSize); 00361 00362 // Emit members. 00363 EmitCompositeTypeElements (CTy, UniqueSuffix); 00364 00365 // Emit mangled Symbol for end of structure/union. 00366 std::string EOSSymbol = ".eos" + UniqueSuffix; 00367 EmitSymbol(EOSSymbol.c_str(), PIC16Dbg::C_EOS); 00368 EmitAuxEntry(EOSSymbol.c_str(), Aux, PIC16Dbg::AuxSize, 00369 MangledCTyName.c_str()); 00370 } 00371 } 00372 } 00373 00374 00375 /// EmitFunctBeginDI - Emit .bf for function. 00376 /// 00377 void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) { 00378 std::string FunctName = F->getName(); 00379 if (EmitDebugDirectives) { 00380 std::string FunctBeginSym = ".bf." + FunctName; 00381 std::string BlockBeginSym = ".bb." + FunctName; 00382 00383 int BFAux[PIC16Dbg::AuxSize] = {0}; 00384 BFAux[4] = CurLine; 00385 BFAux[5] = CurLine >> 8; 00386 00387 // Emit debug directives for beginning of function. 00388 EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN); 00389 EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize); 00390 00391 EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK); 00392 EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize); 00393 } 00394 } 00395 00396 /// EmitFunctEndDI - Emit .ef for function end. 00397 /// 00398 void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) { 00399 std::string FunctName = F->getName(); 00400 if (EmitDebugDirectives) { 00401 std::string FunctEndSym = ".ef." + FunctName; 00402 std::string BlockEndSym = ".eb." + FunctName; 00403 00404 // Emit debug directives for end of function. 00405 EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK); 00406 int EFAux[PIC16Dbg::AuxSize] = {0}; 00407 // 5th and 6th byte stand for line number. 00408 EFAux[4] = CurLine; 00409 EFAux[5] = CurLine >> 8; 00410 EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize); 00411 EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN); 00412 EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize); 00413 } 00414 } 00415 00416 /// EmitAuxEntry - Emit Auxiliary debug information. 00417 /// 00418 void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num, 00419 std::string TagName) { 00420 O << "\n\t.dim " << VarName << ", 1" ; 00421 // TagName is emitted in case of structure/union objects. 00422 if (TagName != "") 00423 O << ", " << TagName; 00424 for (int i = 0; i<Num; i++) 00425 O << "," << Aux[i]; 00426 } 00427 00428 /// EmitSymbol - Emit .def for a symbol. Value is offset for the member. 00429 /// 00430 void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short 00431 Type, unsigned long Value) { 00432 O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = " 00433 << Class; 00434 if (Value > 0) 00435 O << ", value = " << Value; 00436 } 00437 00438 /// EmitVarDebugInfo - Emit debug information for all variables. 00439 /// 00440 void PIC16DbgInfo::EmitVarDebugInfo(Module &M) { 00441 DebugInfoFinder DbgFinder; 00442 DbgFinder.processModule(M); 00443 00444 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(), 00445 E = DbgFinder.global_variable_end(); I != E; ++I) { 00446 DIGlobalVariable DIGV(*I); 00447 DIType Ty = DIGV.getType(); 00448 unsigned short TypeNo = 0; 00449 bool HasAux = false; 00450 int Aux[PIC16Dbg::AuxSize] = { 0 }; 00451 std::string TagName = ""; 00452 std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getNameStr(); 00453 PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName); 00454 // Emit debug info only if type information is availaible. 00455 if (TypeNo != PIC16Dbg::T_NULL) { 00456 O << "\n\t.type " << VarName << ", " << TypeNo; 00457 short ClassNo = getStorageClass(DIGV); 00458 O << "\n\t.class " << VarName << ", " << ClassNo; 00459 if (HasAux) 00460 EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName); 00461 } 00462 } 00463 O << "\n"; 00464 } 00465 00466 /// SwitchToCU - Switch to a new compilation unit. 00467 /// 00468 void PIC16DbgInfo::SwitchToCU(GlobalVariable *CU) { 00469 // Get the file path from CU. 00470 DICompileUnit cu(CU); 00471 std::string DirName, FileName; 00472 std::string FilePath = cu.getDirectory(DirName) + "/" + 00473 cu.getFilename(FileName); 00474 00475 // Nothing to do if source file is still same. 00476 if ( FilePath == CurFile ) return; 00477 00478 // Else, close the current one and start a new. 00479 if (CurFile != "") O << "\n\t.eof"; 00480 O << "\n\t.file\t\"" << FilePath << "\"\n" ; 00481 CurFile = FilePath; 00482 CurLine = 0; 00483 } 00484 00485 /// EmitEOF - Emit .eof for end of file. 00486 /// 00487 void PIC16DbgInfo::EmitEOF() { 00488 if (CurFile != "") 00489 O << "\n\t.EOF"; 00490 } 00491