PIC16DebugInfo.cpp [plain text]
#include "PIC16.h"
#include "PIC16ABINames.h"
#include "PIC16DebugInfo.h"
#include "llvm/GlobalVariable.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Support/DebugLoc.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/ADT/SmallString.h"
using namespace llvm;
void PIC16DbgInfo::PopulateDebugInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
if (Ty.isBasicType())
PopulateBasicTypeInfo (Ty, TypeNo);
else if (Ty.isCompositeType())
PopulateCompositeTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
else if (Ty.isDerivedType())
PopulateDerivedTypeInfo (Ty, TypeNo, HasAux, Aux, TagName);
else {
TypeNo = PIC16Dbg::T_NULL;
HasAux = false;
}
return;
}
void PIC16DbgInfo::PopulateBasicTypeInfo (DIType Ty, unsigned short &TypeNo) {
std::string Name = Ty.getName();
unsigned short BaseTy = GetTypeDebugNumber(Name);
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
TypeNo = TypeNo | (0xffff & BaseTy);
}
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;
}
DIType BaseType = DIDerivedType(Ty.getNode()).getTypeDerivedFrom();
PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
}
void PIC16DbgInfo::PopulateArrayTypeInfo (DIType Ty, unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
DICompositeType CTy = DICompositeType(Ty.getNode());
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.getNode());
Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
Aux[8+i*2+0] = Dimension[i];
Aux[8+i*2+1] = Dimension[i] >> 8;
size = size * Dimension[i];
}
}
HasAux = true;
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
DIType BaseType = CTy.getTypeDerivedFrom();
PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TagName);
}
void PIC16DbgInfo::PopulateStructOrUnionTypeInfo (DIType Ty,
unsigned short &TypeNo,
bool &HasAux, int Aux[],
std::string &TagName) {
DICompositeType CTy = DICompositeType(Ty.getNode());
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
if (Ty.getTag() == dwarf::DW_TAG_structure_type)
TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
else
TypeNo = TypeNo | PIC16Dbg::T_UNION;
TagName = CTy.getName();
std::string UniqueSuffix = "." + Ty.getNode()->getNameStr().substr(18);
TagName += UniqueSuffix;
unsigned short size = CTy.getSizeInBits()/8;
HasAux = true;
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
}
void PIC16DbgInfo::PopulateEnumTypeInfo (DIType Ty, unsigned short &TypeNo) {
TypeNo = TypeNo << PIC16Dbg::S_BASIC;
TypeNo = TypeNo | PIC16Dbg::T_ENUM;
}
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;
}
}
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;
}
short PIC16DbgInfo::getStorageClass(DIGlobalVariable DIGV) {
short ClassNo;
if (PAN::isLocalName(DIGV.getName())) {
ClassNo = PIC16Dbg::C_STAT;
}
else if (DIGV.isLocalToUnit())
ClassNo = PIC16Dbg::C_STAT;
else
ClassNo = PIC16Dbg::C_EXT;
return ClassNo;
}
void PIC16DbgInfo::BeginModule(Module &M) {
DebugInfoFinder DbgFinder;
DbgFinder.processModule(M);
if (DbgFinder.compile_unit_count() != 0) {
MDNode *CU = *DbgFinder.compile_unit_begin();
EmitDebugDirectives = true;
SwitchToCU(CU);
}
EmitCompositeTypeDecls(M);
}
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;
}
void PIC16DbgInfo::BeginFunction(const MachineFunction &MF) {
if (! EmitDebugDirectives) return;
const DebugLoc &DL = GetDebugLocForFunction(MF);
if (!DL.isUnknown()) {
ChangeDebugLoc(MF, DL, true);
EmitFunctBeginDI(MF.getFunction());
}
CurLine = 0;
}
void PIC16DbgInfo::ChangeDebugLoc(const MachineFunction &MF,
const DebugLoc &DL, bool IsInBeginFunction) {
if (!EmitDebugDirectives) return;
assert(!DL.isUnknown() && "can't change to invalid debug loc");
SwitchToCU(DL.getScope(MF.getFunction()->getContext()));
SwitchToLine(DL.getLine(), IsInBeginFunction);
}
void PIC16DbgInfo::SwitchToLine(unsigned Line, bool IsInBeginFunction) {
if (CurLine == Line) return;
if (!IsInBeginFunction) O << "\n\t.line " << Line << "\n";
CurLine = Line;
}
void PIC16DbgInfo::EndFunction(const MachineFunction &MF) {
if (! EmitDebugDirectives) return;
const DebugLoc &DL = GetDebugLocForFunction(MF);
if (!DL.isUnknown())
EmitFunctEndDI(MF.getFunction(), CurLine);
}
void PIC16DbgInfo::EndModule(Module &M) {
if (! EmitDebugDirectives) return;
EmitVarDebugInfo(M);
if (CurFile != "") O << "\n\t.eof";
}
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 = "";
DIDerivedType DITy(Element.getNode());
unsigned short ElementSize = DITy.getSizeInBits()/8;
std::string MangMemName = DITy.getName().str() + 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);
}
}
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.Verify())
continue;
if (CTy.getTag() == dwarf::DW_TAG_union_type ||
CTy.getTag() == dwarf::DW_TAG_structure_type ) {
std::string DIVar = CTy.getNode()->getNameStr();
std::string UniqueSuffix = "." + DIVar.substr(18);
std::string MangledCTyName = CTy.getName().str() + UniqueSuffix;
unsigned short size = CTy.getSizeInBits()/8;
int Aux[PIC16Dbg::AuxSize] = {0};
Aux[6] = size & 0xff;
Aux[7] = size >> 8;
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);
EmitAuxEntry(MangledCTyName.c_str(), Aux, PIC16Dbg::AuxSize);
EmitCompositeTypeElements (CTy, UniqueSuffix);
std::string EOSSymbol = ".eos" + UniqueSuffix;
EmitSymbol(EOSSymbol.c_str(), PIC16Dbg::C_EOS);
EmitAuxEntry(EOSSymbol.c_str(), Aux, PIC16Dbg::AuxSize,
MangledCTyName.c_str());
}
}
}
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;
EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
}
}
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;
EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
int EFAux[PIC16Dbg::AuxSize] = {0};
EFAux[4] = CurLine;
EFAux[5] = CurLine >> 8;
EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
}
}
void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int Num,
std::string TagName) {
O << "\n\t.dim " << VarName << ", 1" ;
if (TagName != "")
O << ", " << TagName;
for (int i = 0; i<Num; i++)
O << "," << (Aux[i] && 0xff);
}
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;
}
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 = DIGV.getName();
VarName = MAI->getGlobalPrefix() + VarName;
PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TagName);
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";
}
void PIC16DbgInfo::SwitchToCU(MDNode *CU) {
DICompileUnit cu(CU);
std::string DirName = cu.getDirectory();
std::string FileName = cu.getFilename();
std::string FilePath = DirName + "/" + FileName;
if ( FilePath == CurFile ) return;
if (CurFile != "") O << "\n\t.eof";
O << "\n\t.file\t\"" << FilePath << "\"\n" ;
CurFile = FilePath;
CurLine = 0;
}
void PIC16DbgInfo::EmitEOF() {
if (CurFile != "")
O << "\n\t.EOF";
}