1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
//===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the helper functions for representing debug information.
//
//===----------------------------------------------------------------------===//
#include "PIC16.h"
#include "PIC16DebugInfo.h"
#include "llvm/GlobalVariable.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Support/DebugLoc.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/ADT/SmallString.h"
using namespace llvm;
/// PopulateDebugInfo - Populate the TypeNo, Aux[] and TagName from Ty.
///
void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
if (Ty.isBasicType(Ty.getTag()))
PopulateBasicTypeInfo (Ty, TypeNo);
else if (Ty.isDerivedType(Ty.getTag()))
PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
else if (Ty.isCompositeType(Ty.getTag()))
PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
else {
TypeNo = PIC16Dbg::T_NULL;
HasAux = false;
}
return;
}
/// PopulateBasicTypeInfo- Populate TypeNo for basic type from Ty.
///
void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
std::string Name = "";
Ty.getName(Name);
unsigned short BaseTy = GetTypeDebugNumber(Name);
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
TypeNo = TypeNo | (0xffff & BaseTy);
}
/// PopulateDerivedTypeInfo - Populate TypeNo, Aux[], TagName for derived type
/// from Ty. Derived types are mostly pointers.
///
void PIC16DbgInfo::PopulateDerivedTypeInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
switch(Ty.getTag())
{
case dwarf::DW_TAG_pointer_type:
TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
TypeNo = TypeNo | PIC16Dbg::DT_PTR;
break;
default:
TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
}
// We also need to encode the the information about the base type of
// pointer in TypeNo.
DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
}
/// PopulateArrayTypeInfo - Populate TypeNo, Aux[] for array from Ty.
void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
DICompositeType CTy = DICompositeType(Ty.getGV());
DIArray Elements = CTy.getTypeArray();
unsigned short size = 1;
unsigned short Dimension[4]={0,0,0,0};
for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
DIDescriptor Element = Elements.getElement(i);
if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
TypeNo = TypeNo | PIC16Dbg::DT_ARY;
DISubrange SubRange = DISubrange(Element.getGV());
Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
// Each dimension is represented by 2 bytes starting at byte 9.
Aux[8+i*2+0] = Dimension[i];
Aux[8+i*2+1] = Dimension[i] >> 8;
size = size * Dimension[i];
}
}
HasAux = true;
// In auxillary entry for array, 7th and 8th byte represent array size.
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
DIType BaseType = CTy.getTypeDerivedFrom();
PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
}
/// PopulateStructOrUnionTypeInfo - Populate TypeNo, Aux[] , TagName for
/// structure or union.
///
void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty,
unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
DICompositeType CTy = DICompositeType(Ty.getGV());
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
if (Ty.getTag() == dwarf::DW_TAG_structure_type)
TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
else
TypeNo = TypeNo | PIC16Dbg::T_UNION;
CTy.getName(TagName);
// UniqueSuffix is .number where number is obtained from
// llvm.dbg.composite<number>.
// FIXME: This will break when composite type is not represented by
// llvm.dbg.composite* global variable. Since we need to revisit
// PIC16DebugInfo implementation anyways after the MDNodes based
// framework is done, let us continue with the way it is.
std::string UniqueSuffix = "." + Ty.getGV()->getNameStr().substr(18);
TagName += UniqueSuffix;
unsigned short size = CTy.getSizeInBits()/8;
// 7th and 8th byte represent size.
HasAux = true;
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
}
/// PopulateEnumTypeInfo - Populate TypeNo for enum from Ty.
void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) {
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
TypeNo = TypeNo | PIC16Dbg::T_ENUM;
}
/// PopulateCompositeTypeInfo - Populate TypeNo, Aux[] and TagName for
/// composite types from Ty.
///
void PIC16DbgInfo::PopulateCompositeTypeInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
switch (Ty.getTag()) {
case dwarf::DW_TAG_array_type: {
PopulateArrayTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
break;
}
case dwarf:: DW_TAG_union_type:
case dwarf::DW_TAG_structure_type: {
PopulateStructOrUnionTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
break;
}
case dwarf::DW_TAG_enumeration_type: {
PopulateEnumTypeInfo (Ty, TypeNo);
break;
}
default:
TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
}
}
/// GetTypeDebugNumber - Get debug type number for given type.
///
unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type) {
if (type == "char")
return PIC16Dbg::T_CHAR;
else if (type == "short")
return PIC16Dbg::T_SHORT;
else if (type == "int")
return PIC16Dbg::T_INT;
else if (type == "long")
return PIC16Dbg::T_LONG;
else if (type == "unsigned char")
return PIC16Dbg::T_UCHAR;
else if (type == "unsigned short")
return PIC16Dbg::T_USHORT;
else if (type == "unsigned int")
return PIC16Dbg::T_UINT;
else if (type == "unsigned long")
return PIC16Dbg::T_ULONG;
else
return 0;
}
/// GetStorageClass - Get storage class for give debug variable.
///
short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
short ClassNo;
if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
// Generating C_AUTO here fails due to error in linker. Change it once
// linker is fixed.
ClassNo = PIC16Dbg::C_STAT;
}
else if (DIGV.isLocalToUnit())
ClassNo = PIC16Dbg::C_STAT;
else
ClassNo = PIC16Dbg::C_EXT;
return ClassNo;
}
/// BeginModule - Emit necessary debug info to start a Module and do other
/// required initializations.
void PIC16DbgInfo::BeginModule(Module &M) {
// Emit file directive for module.
DebugInfoFinder DbgFinder;
DbgFinder.processModule(M);
if (DbgFinder.compile_unit_count() != 0) {
// FIXME : What if more then one CUs are present in a module ?
GlobalVariable *CU = *DbgFinder.compile_unit_begin();
EmitDebugDirectives = true;
SwitchToCU(CU);
}
// Emit debug info for decls of composite types.
EmitCompositeTypeDecls(M);
}
/// Helper to find first valid debug loc for a function.
///
static const DebugLoc GetDebugLocForFunction(const MachineFunction &MF) {
DebugLoc DL;
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
I != E; ++I) {
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
II != E; ++II) {
DL = II->getDebugLoc();
if (!DL.isUnknown())
return DL;
}
}
return DL;
}
/// BeginFunction - Emit necessary debug info to start a function.
///
void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
if (! EmitDebugDirectives) return;
// Retreive the first valid debug Loc and process it.
const DebugLoc &DL = GetDebugLocForFunction(MF);
// Emit debug info only if valid debug info is available.
if (!DL.isUnknown()) {
ChangeDebugLoc(MF, DL, true);
EmitFunctBeginDI(MF.getFunction());
}
// Set current line to 0 so that.line directive is genearted after .bf.
CurLine = 0;
}
/// ChangeDebugLoc - Take necessary steps when DebugLoc changes.
/// CurFile and CurLine may change as a result of this.
///
void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,
const DebugLoc &DL, bool IsInBeginFunction) {
if (! EmitDebugDirectives) return;
assert (! DL.isUnknown() && "can't change to invalid debug loc");
GlobalVariable *CU = MF.getDebugLocTuple(DL).CompileUnit;
unsigned line = MF.getDebugLocTuple(DL).Line;
SwitchToCU(CU);
SwitchToLine(line, IsInBeginFunction);
}
/// SwitchToLine - Emit line directive for a new line.
///
void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
if (CurLine == Line) return;
if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n";
CurLine = Line;
}
/// EndFunction - Emit .ef for end of function.
///
void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
if (! EmitDebugDirectives) return;
const DebugLoc &DL = GetDebugLocForFunction(MF);
// Emit debug info only if valid debug info is available.
if (!DL.isUnknown())
EmitFunctEndDI(MF.getFunction(), CurLine);
}
/// EndModule - Emit .eof for end of module.
///
void PIC16DbgInfo::EndModule(Module &M) {
if (! EmitDebugDirectives) return;
EmitVarDebugInfo(M);
if (CurFile != "") O << "\n\t.eof";
}
/// EmitCompositeTypeElements - Emit debug information for members of a
/// composite type.
///
void PIC16DbgInfo::EmitCompositeTypeElements (DICompositeType CTy,
std::string SuffixNo) {
unsigned long Value = 0;
DIArray Elements = CTy.getTypeArray();
for (unsigned i = 0, N = Elements.getNumElements(); i < N; i++) {
DIDescriptor Element = Elements.getElement(i);
unsigned short TypeNo = 0;
bool HasAux = false;
int ElementAux[PIC16Dbg::AuxSize] = { 0 };
std::string TagName = "";
std::string ElementName;
GlobalVariable *GV = Element.getGV();
DIDerivedType DITy(GV);
DITy.getName(ElementName);
unsigned short ElementSize = DITy.getSizeInBits()/8;
// Get mangleddd name for this structure/union element.
std::string MangMemName = ElementName + SuffixNo;
PopulateDebugInfo(DITy, TypeNo, HasAux, ElementAux, TagName);
short Class = 0;
if( CTy.getTag() == dwarf::DW_TAG_union_type)
Class = PIC16Dbg::C_MOU;
else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
Class = PIC16Dbg::C_MOS;
EmitSymbol(MangMemName.c_str(), Class, TypeNo, Value);
if (CTy.getTag() == dwarf::DW_TAG_structure_type)
Value += ElementSize;
if (HasAux)
EmitAuxEntry(MangMemName.c_str(), ElementAux, PIC16Dbg::AuxSize, TagName);
}
}
/// EmitCompositeTypeDecls - Emit composite type declarations like structure
/// and union declarations.
///
void PIC16DbgInfo::EmitCompositeTypeDecls(Module &M) {
DebugInfoFinder DbgFinder;
DbgFinder.processModule(M);
for (DebugInfoFinder::iterator I = DbgFinder.type_begin(),
E = DbgFinder.type_end(); I != E; ++I) {
DICompositeType CTy(*I);
if (CTy.isNull())
continue;
if (CTy.getTag() == dwarf::DW_TAG_union_type ||
CTy.getTag() == dwarf::DW_TAG_structure_type ) {
std::string Name;
CTy.getName(Name);
// Get the number after llvm.dbg.composite and make UniqueSuffix from
// it.
std::string DIVar = CTy.getGV()->getNameStr();
std::string UniqueSuffix = "." + DIVar.substr(18);
std::string MangledCTyName = Name + UniqueSuffix;
unsigned short size = CTy.getSizeInBits()/8;
int Aux[PIC16Dbg::AuxSize] = {0};
// 7th and 8th byte represent size of structure/union.
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
// Emit .def for structure/union tag.
if( CTy.getTag() == dwarf::DW_TAG_union_type)
EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_UNTAG);
else if (CTy.getTag() == dwarf::DW_TAG_structure_type)
EmitSymbol(MangledCTyName.c_str(), PIC16Dbg::C_STRTAG);
// Emit auxiliary debug information for structure/union tag.
EmitAuxEntry(MangledCTyName.c_str(), Aux, PIC16Dbg::AuxSize);
// Emit members.
EmitCompositeTypeElements (CTy, UniqueSuffix);
// Emit mangled Symbol for end of structure/union.
std::string EOSSymbol = ".eos" + UniqueSuffix;
EmitSymbol(EOSSymbol.c_str(), PIC16Dbg::C_EOS);
EmitAuxEntry(EOSSymbol.c_str(), Aux, PIC16Dbg::AuxSize,
MangledCTyName.c_str());
}
}
}
/// EmitFunctBeginDI - Emit .bf for function.
///
void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
std::string FunctName = F->getName();
if (EmitDebugDirectives) {
std::string FunctBeginSym = ".bf." + FunctName;
std::string BlockBeginSym = ".bb." + FunctName;
int BFAux[PIC16Dbg::AuxSize] = {0};
BFAux[4] = CurLine;
BFAux[5] = CurLine >> 8;
// Emit debug directives for beginning of function.
EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
}
}
/// EmitFunctEndDI - Emit .ef for function end.
///
void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
std::string FunctName = F->getName();
if (EmitDebugDirectives) {
std::string FunctEndSym = ".ef." + FunctName;
std::string BlockEndSym = ".eb." + FunctName;
// Emit debug directives for end of function.
EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
int EFAux[PIC16Dbg::AuxSize] = {0};
// 5th and 6th byte stand for line number.
EFAux[4] = CurLine;
EFAux[5] = CurLine >> 8;
EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
}
}
/// EmitAuxEntry - Emit Auxiliary debug information.
///
void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
std::string TagName) {
O << "\n\t.dim " << VarName << ", 1" ;
// TagName is emitted in case of structure/union objects.
if (TagName != "")
O << ", " << TagName;
for (int i = 0; i<Num; i++)
O << "," << Aux[i];
}
/// EmitSymbol - Emit .def for a symbol. Value is offset for the member.
///
void PIC16DbgInfo::EmitSymbol(std::string Name, short Class, unsigned short
Type, unsigned long Value) {
O << "\n\t" << ".def "<< Name << ", type = " << Type << ", class = "
<< Class;
if (Value > 0)
O << ", value = " << Value;
}
/// EmitVarDebugInfo - Emit debug information for all variables.
///
void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
DebugInfoFinder DbgFinder;
DbgFinder.processModule(M);
for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
E = DbgFinder.global_variable_end(); I != E; ++I) {
DIGlobalVariable DIGV(*I);
DIType Ty = DIGV.getType();
unsigned short TypeNo = 0;
bool HasAux = false;
int Aux[PIC16Dbg::AuxSize] = { 0 };
std::string TagName = "";
std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getNameStr();
PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
// Emit debug info only if type information is availaible.
if (TypeNo != PIC16Dbg::T_NULL) {
O << "\n\t.type " << VarName << ", " << TypeNo;
short ClassNo = getStorageClass(DIGV);
O << "\n\t.class " << VarName << ", " << ClassNo;
if (HasAux)
EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize, TagName);
}
}
O << "\n";
}
/// SwitchToCU - Switch to a new compilation unit.
///
void PIC16DbgInfo::SwitchToCU(GlobalVariable *CU) {
// Get the file path from CU.
DICompileUnit cu(CU);
std::string DirName, FileName;
std::string FilePath = cu.getDirectory(DirName) + "/" +
cu.getFilename(FileName);
// Nothing to do if source file is still same.
if ( FilePath == CurFile ) return;
// Else, close the current one and start a new.
if (CurFile != "") O << "\n\t.eof";
O << "\n\t.file\t\"" << FilePath << "\"\n" ;
CurFile = FilePath;
CurLine = 0;
}
/// EmitEOF - Emit .eof for end of file.
///
void PIC16DbgInfo::EmitEOF() {
if (CurFile != "")
O << "\n\t.EOF";
}
|