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