Jim Laskey | e503289 | 2005-12-21 19:48:16 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by James M. Laskey and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing dwarf debug info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Jim Laskey | 3ea0e0e | 2006-01-27 18:32:41 +0000 | [diff] [blame] | 13 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/DwarfWriter.h" |
Jim Laskey | e503289 | 2005-12-21 19:48:16 +0000 | [diff] [blame] | 15 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Type.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 21 | #include "llvm/CodeGen/MachineLocation.h" |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Dwarf.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mangler.h" |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 25 | #include "llvm/Target/MRegisterInfo.h" |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 27 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 28 | #include <iostream> |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 29 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Jim Laskey | 9a777a3 | 2006-02-27 22:37:23 +0000 | [diff] [blame] | 31 | using namespace llvm::dwarf; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 32 | |
| 33 | static cl::opt<bool> |
| 34 | DwarfVerbose("dwarf-verbose", cl::Hidden, |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 35 | cl::desc("Add comments to Dwarf directives.")); |
| 36 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 37 | namespace llvm { |
| 38 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 40 | // Forward declarations. |
| 41 | // |
| 42 | class CompileUnit; |
| 43 | class DIE; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 44 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 46 | // CompileUnit - This dwarf writer support class manages information associate |
| 47 | // with a source file. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 48 | class CompileUnit { |
| 49 | private: |
| 50 | CompileUnitDesc *Desc; // Compile unit debug descriptor. |
| 51 | unsigned ID; // File ID for source. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 52 | DIE *Die; // Compile unit debug information entry. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 53 | std::map<std::string, DIE *> Globals; // A map of globally visible named |
| 54 | // entities for this unit. |
| 55 | |
| 56 | public: |
| 57 | CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D) |
| 58 | : Desc(CUD) |
| 59 | , ID(I) |
| 60 | , Die(D) |
| 61 | , Globals() |
| 62 | {} |
| 63 | |
| 64 | ~CompileUnit(); |
| 65 | |
| 66 | // Accessors. |
| 67 | CompileUnitDesc *getDesc() const { return Desc; } |
| 68 | unsigned getID() const { return ID; } |
| 69 | DIE* getDie() const { return Die; } |
| 70 | std::map<std::string, DIE *> &getGlobals() { return Globals; } |
| 71 | |
| 72 | /// hasContent - Return true if this compile unit has something to write out. |
| 73 | /// |
| 74 | bool hasContent() const; |
| 75 | |
| 76 | /// AddGlobal - Add a new global entity to the compile unit. |
| 77 | /// |
| 78 | void AddGlobal(const std::string &Name, DIE *Die); |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 83 | // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a |
| 84 | // Dwarf abbreviation. |
| 85 | class DIEAbbrevData { |
| 86 | private: |
| 87 | unsigned Attribute; // Dwarf attribute code. |
| 88 | unsigned Form; // Dwarf form code. |
| 89 | |
| 90 | public: |
| 91 | DIEAbbrevData(unsigned A, unsigned F) |
| 92 | : Attribute(A) |
| 93 | , Form(F) |
| 94 | {} |
| 95 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 96 | // Accessors. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 97 | unsigned getAttribute() const { return Attribute; } |
| 98 | unsigned getForm() const { return Form; } |
| 99 | |
| 100 | /// operator== - Used by DIEAbbrev to locate entry. |
| 101 | /// |
| 102 | bool operator==(const DIEAbbrevData &DAD) const { |
| 103 | return Attribute == DAD.Attribute && Form == DAD.Form; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 104 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 105 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 106 | /// operator!= - Used by DIEAbbrev to locate entry. |
| 107 | /// |
| 108 | bool operator!=(const DIEAbbrevData &DAD) const { |
| 109 | return Attribute != DAD.Attribute || Form != DAD.Form; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 110 | } |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 111 | |
| 112 | /// operator< - Used by DIEAbbrev to locate entry. |
| 113 | /// |
| 114 | bool operator<(const DIEAbbrevData &DAD) const { |
| 115 | return Attribute < DAD.Attribute || |
| 116 | (Attribute == DAD.Attribute && Form < DAD.Form); |
| 117 | } |
| 118 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 119 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug |
| 122 | // information object. |
| 123 | class DIEAbbrev { |
| 124 | private: |
| 125 | unsigned Tag; // Dwarf tag code. |
| 126 | unsigned ChildrenFlag; // Dwarf children flag. |
| 127 | std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 128 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 129 | public: |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 130 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 131 | DIEAbbrev(unsigned T, unsigned C) |
| 132 | : Tag(T) |
| 133 | , ChildrenFlag(C) |
| 134 | , Data() |
| 135 | {} |
| 136 | ~DIEAbbrev() {} |
| 137 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 138 | // Accessors. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 139 | unsigned getTag() const { return Tag; } |
| 140 | unsigned getChildrenFlag() const { return ChildrenFlag; } |
| 141 | const std::vector<DIEAbbrevData> &getData() const { return Data; } |
| 142 | void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 143 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 144 | /// operator== - Used by UniqueVector to locate entry. |
| 145 | /// |
| 146 | bool operator==(const DIEAbbrev &DA) const; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 147 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 148 | /// operator< - Used by UniqueVector to locate entry. |
| 149 | /// |
| 150 | bool operator<(const DIEAbbrev &DA) const; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 151 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 152 | /// AddAttribute - Adds another set of attribute information to the |
| 153 | /// abbreviation. |
| 154 | void AddAttribute(unsigned Attribute, unsigned Form) { |
| 155 | Data.push_back(DIEAbbrevData(Attribute, Form)); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 156 | } |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 157 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 158 | /// AddFirstAttribute - Adds a set of attribute information to the front |
| 159 | /// of the abbreviation. |
| 160 | void AddFirstAttribute(unsigned Attribute, unsigned Form) { |
| 161 | Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form)); |
| 162 | } |
| 163 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 164 | /// Emit - Print the abbreviation using the specified Dwarf writer. |
| 165 | /// |
| 166 | void Emit(const DwarfWriter &DW) const; |
| 167 | |
| 168 | #ifndef NDEBUG |
| 169 | void print(std::ostream &O); |
| 170 | void dump(); |
| 171 | #endif |
| 172 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 173 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 174 | //===----------------------------------------------------------------------===// |
| 175 | // DIEValue - A debug information entry value. |
| 176 | // |
| 177 | class DIEValue { |
| 178 | public: |
| 179 | enum { |
| 180 | isInteger, |
| 181 | isString, |
| 182 | isLabel, |
| 183 | isAsIsLabel, |
| 184 | isDelta, |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 185 | isEntry, |
| 186 | isBlock |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | unsigned Type; // Type of the value |
| 190 | |
| 191 | DIEValue(unsigned T) : Type(T) {} |
| 192 | virtual ~DIEValue() {} |
| 193 | |
| 194 | // Implement isa/cast/dyncast. |
| 195 | static bool classof(const DIEValue *) { return true; } |
| 196 | |
| 197 | /// EmitValue - Emit value via the Dwarf writer. |
| 198 | /// |
| 199 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0; |
| 200 | |
| 201 | /// SizeOf - Return the size of a value in bytes. |
| 202 | /// |
| 203 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0; |
| 204 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 205 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // DWInteger - An integer value DIE. |
| 208 | // |
| 209 | class DIEInteger : public DIEValue { |
| 210 | private: |
| 211 | uint64_t Integer; |
| 212 | |
| 213 | public: |
| 214 | DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {} |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 215 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 216 | // Implement isa/cast/dyncast. |
| 217 | static bool classof(const DIEInteger *) { return true; } |
| 218 | static bool classof(const DIEValue *I) { return I->Type == isInteger; } |
| 219 | |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 220 | /// BestForm - Choose the best form for integer. |
| 221 | /// |
| 222 | unsigned BestForm(bool IsSigned); |
| 223 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 224 | /// EmitValue - Emit integer of appropriate size. |
| 225 | /// |
| 226 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 227 | |
| 228 | /// SizeOf - Determine size of integer value in bytes. |
| 229 | /// |
| 230 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 231 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 232 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | // DIEString - A string value DIE. |
| 235 | // |
| 236 | struct DIEString : public DIEValue { |
| 237 | const std::string String; |
| 238 | |
| 239 | DIEString(const std::string &S) : DIEValue(isString), String(S) {} |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 240 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 241 | // Implement isa/cast/dyncast. |
| 242 | static bool classof(const DIEString *) { return true; } |
| 243 | static bool classof(const DIEValue *S) { return S->Type == isString; } |
| 244 | |
| 245 | /// EmitValue - Emit string value. |
| 246 | /// |
| 247 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 248 | |
| 249 | /// SizeOf - Determine size of string value in bytes. |
| 250 | /// |
| 251 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 252 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 253 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
| 255 | // DIEDwarfLabel - A Dwarf internal label expression DIE. |
| 256 | // |
| 257 | struct DIEDwarfLabel : public DIEValue { |
| 258 | const DWLabel Label; |
| 259 | |
| 260 | DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {} |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 261 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 262 | // Implement isa/cast/dyncast. |
| 263 | static bool classof(const DIEDwarfLabel *) { return true; } |
| 264 | static bool classof(const DIEValue *L) { return L->Type == isLabel; } |
| 265 | |
| 266 | /// EmitValue - Emit label value. |
| 267 | /// |
| 268 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 269 | |
| 270 | /// SizeOf - Determine size of label value in bytes. |
| 271 | /// |
| 272 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 273 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 274 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 275 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 276 | //===----------------------------------------------------------------------===// |
| 277 | // DIEObjectLabel - A label to an object in code or data. |
| 278 | // |
| 279 | struct DIEObjectLabel : public DIEValue { |
| 280 | const std::string Label; |
| 281 | |
| 282 | DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {} |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 283 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 284 | // Implement isa/cast/dyncast. |
| 285 | static bool classof(const DIEObjectLabel *) { return true; } |
| 286 | static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; } |
| 287 | |
| 288 | /// EmitValue - Emit label value. |
| 289 | /// |
| 290 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 291 | |
| 292 | /// SizeOf - Determine size of label value in bytes. |
| 293 | /// |
| 294 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 295 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 296 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 297 | //===----------------------------------------------------------------------===// |
| 298 | // DIEDelta - A simple label difference DIE. |
| 299 | // |
| 300 | struct DIEDelta : public DIEValue { |
| 301 | const DWLabel LabelHi; |
| 302 | const DWLabel LabelLo; |
| 303 | |
| 304 | DIEDelta(const DWLabel &Hi, const DWLabel &Lo) |
| 305 | : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {} |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 306 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 307 | // Implement isa/cast/dyncast. |
| 308 | static bool classof(const DIEDelta *) { return true; } |
| 309 | static bool classof(const DIEValue *D) { return D->Type == isDelta; } |
| 310 | |
| 311 | /// EmitValue - Emit delta value. |
| 312 | /// |
| 313 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 314 | |
| 315 | /// SizeOf - Determine size of delta value in bytes. |
| 316 | /// |
| 317 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 318 | }; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 319 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 320 | //===----------------------------------------------------------------------===// |
| 321 | // DIEntry - A pointer to a debug information entry. |
| 322 | // |
| 323 | struct DIEntry : public DIEValue { |
| 324 | DIE *Entry; |
| 325 | |
| 326 | DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {} |
| 327 | |
| 328 | // Implement isa/cast/dyncast. |
| 329 | static bool classof(const DIEntry *) { return true; } |
| 330 | static bool classof(const DIEValue *E) { return E->Type == isEntry; } |
| 331 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 332 | /// EmitValue - Emit debug information entry offset. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 333 | /// |
| 334 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 335 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 336 | /// SizeOf - Determine size of debug information entry in bytes. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 337 | /// |
| 338 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 339 | }; |
| 340 | |
| 341 | //===----------------------------------------------------------------------===// |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 342 | // DIEBlock - A block of values. Primarily used for location expressions. |
| 343 | // |
| 344 | struct DIEBlock : public DIEValue { |
| 345 | unsigned Size; // Size in bytes excluding size header. |
| 346 | std::vector<unsigned> Forms; // Data forms. |
| 347 | std::vector<DIEValue *> Values; // Block values. |
| 348 | |
| 349 | DIEBlock() |
| 350 | : DIEValue(isBlock) |
| 351 | , Size(0) |
| 352 | , Forms() |
| 353 | , Values() |
| 354 | {} |
| 355 | ~DIEBlock(); |
| 356 | |
| 357 | // Implement isa/cast/dyncast. |
| 358 | static bool classof(const DIEBlock *) { return true; } |
| 359 | static bool classof(const DIEValue *E) { return E->Type == isBlock; } |
| 360 | |
| 361 | /// ComputeSize - calculate the size of the block. |
| 362 | /// |
| 363 | unsigned ComputeSize(DwarfWriter &DW); |
| 364 | |
| 365 | /// BestForm - Choose the best form for data. |
| 366 | /// |
| 367 | unsigned BestForm(); |
| 368 | |
| 369 | /// EmitValue - Emit block data. |
| 370 | /// |
| 371 | virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const; |
| 372 | |
| 373 | /// SizeOf - Determine size of block data in bytes. |
| 374 | /// |
| 375 | virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const; |
| 376 | |
| 377 | /// AddUInt - Add an unsigned integer value. |
| 378 | /// |
| 379 | void AddUInt(unsigned Form, uint64_t Integer); |
| 380 | |
| 381 | /// AddSInt - Add an signed integer value. |
| 382 | /// |
| 383 | void AddSInt(unsigned Form, int64_t Integer); |
| 384 | |
| 385 | /// AddString - Add a std::string value. |
| 386 | /// |
| 387 | void AddString(unsigned Form, const std::string &String); |
| 388 | |
| 389 | /// AddLabel - Add a Dwarf label value. |
| 390 | /// |
| 391 | void AddLabel(unsigned Form, const DWLabel &Label); |
| 392 | |
| 393 | /// AddObjectLabel - Add a non-Dwarf label value. |
| 394 | /// |
| 395 | void AddObjectLabel(unsigned Form, const std::string &Label); |
| 396 | |
| 397 | /// AddDelta - Add a label delta value. |
| 398 | /// |
| 399 | void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo); |
| 400 | |
| 401 | /// AddDIEntry - Add a DIE value. |
| 402 | /// |
| 403 | void AddDIEntry(unsigned Form, DIE *Entry); |
| 404 | |
| 405 | }; |
| 406 | |
| 407 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 408 | // DIE - A structured debug information entry. Has an abbreviation which |
| 409 | // describes it's organization. |
| 410 | class DIE { |
| 411 | private: |
| 412 | DIEAbbrev *Abbrev; // Temporary buffer for abbreviation. |
| 413 | unsigned AbbrevID; // Decribing abbreviation ID. |
| 414 | unsigned Offset; // Offset in debug info section. |
| 415 | unsigned Size; // Size of instance + children. |
| 416 | std::vector<DIE *> Children; // Children DIEs. |
| 417 | std::vector<DIEValue *> Values; // Attributes values. |
| 418 | |
| 419 | public: |
| 420 | DIE(unsigned Tag); |
| 421 | ~DIE(); |
| 422 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 423 | // Accessors. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 424 | unsigned getAbbrevID() const { return AbbrevID; } |
| 425 | unsigned getOffset() const { return Offset; } |
| 426 | unsigned getSize() const { return Size; } |
| 427 | const std::vector<DIE *> &getChildren() const { return Children; } |
| 428 | const std::vector<DIEValue *> &getValues() const { return Values; } |
| 429 | void setOffset(unsigned O) { Offset = O; } |
| 430 | void setSize(unsigned S) { Size = S; } |
| 431 | |
| 432 | /// SiblingOffset - Return the offset of the debug information entry's |
| 433 | /// sibling. |
| 434 | unsigned SiblingOffset() const { return Offset + Size; } |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 435 | |
| 436 | /// AddSiblingOffset - Add a sibling offset field to the front of the DIE. |
| 437 | /// |
| 438 | void AddSiblingOffset(); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 439 | |
| 440 | /// AddUInt - Add an unsigned integer attribute data and value. |
| 441 | /// |
| 442 | void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer); |
| 443 | |
| 444 | /// AddSInt - Add an signed integer attribute data and value. |
| 445 | /// |
| 446 | void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer); |
| 447 | |
| 448 | /// AddString - Add a std::string attribute data and value. |
| 449 | /// |
| 450 | void AddString(unsigned Attribute, unsigned Form, |
| 451 | const std::string &String); |
| 452 | |
| 453 | /// AddLabel - Add a Dwarf label attribute data and value. |
| 454 | /// |
| 455 | void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label); |
| 456 | |
| 457 | /// AddObjectLabel - Add a non-Dwarf label attribute data and value. |
| 458 | /// |
| 459 | void AddObjectLabel(unsigned Attribute, unsigned Form, |
| 460 | const std::string &Label); |
| 461 | |
| 462 | /// AddDelta - Add a label delta attribute data and value. |
| 463 | /// |
| 464 | void AddDelta(unsigned Attribute, unsigned Form, |
| 465 | const DWLabel &Hi, const DWLabel &Lo); |
| 466 | |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 467 | /// AddDIEntry - Add a DIE attribute data and value. |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 468 | /// |
| 469 | void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry); |
| 470 | |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 471 | /// AddBlock - Add block data. |
| 472 | /// |
| 473 | void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block); |
| 474 | |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 475 | /// Complete - Indicate that all attributes have been added and |
| 476 | /// ready to get an abbreviation ID. |
| 477 | /// |
| 478 | void Complete(DwarfWriter &DW); |
| 479 | |
| 480 | /// AddChild - Add a child to the DIE. |
| 481 | void AddChild(DIE *Child); |
| 482 | }; |
| 483 | |
| 484 | } // End of namespace llvm |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 485 | |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 488 | CompileUnit::~CompileUnit() { |
| 489 | delete Die; |
| 490 | } |
| 491 | |
| 492 | /// hasContent - Return true if this compile unit has something to write out. |
| 493 | /// |
| 494 | bool CompileUnit::hasContent() const { |
| 495 | return !Die->getChildren().empty(); |
| 496 | } |
| 497 | |
| 498 | /// AddGlobal - Add a new global entity to the compile unit. |
| 499 | /// |
| 500 | void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) { |
| 501 | Globals[Name] = Die; |
| 502 | } |
| 503 | |
| 504 | //===----------------------------------------------------------------------===// |
| 505 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 506 | /// operator== - Used by UniqueVector to locate entry. |
| 507 | /// |
| 508 | bool DIEAbbrev::operator==(const DIEAbbrev &DA) const { |
| 509 | if (Tag != DA.Tag) return false; |
| 510 | if (ChildrenFlag != DA.ChildrenFlag) return false; |
| 511 | if (Data.size() != DA.Data.size()) return false; |
| 512 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 513 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
Jim Laskey | 63ae85f | 2006-01-21 01:13:18 +0000 | [diff] [blame] | 514 | if (Data[i] != DA.Data[i]) return false; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | /// operator< - Used by UniqueVector to locate entry. |
| 521 | /// |
| 522 | bool DIEAbbrev::operator<(const DIEAbbrev &DA) const { |
| 523 | if (Tag != DA.Tag) return Tag < DA.Tag; |
| 524 | if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag; |
| 525 | if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size(); |
| 526 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 527 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
Jim Laskey | 63ae85f | 2006-01-21 01:13:18 +0000 | [diff] [blame] | 528 | if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i]; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | /// Emit - Print the abbreviation using the specified Dwarf writer. |
| 535 | /// |
| 536 | void DIEAbbrev::Emit(const DwarfWriter &DW) const { |
| 537 | // Emit its Dwarf tag type. |
| 538 | DW.EmitULEB128Bytes(Tag); |
| 539 | DW.EOL(TagString(Tag)); |
| 540 | |
| 541 | // Emit whether it has children DIEs. |
| 542 | DW.EmitULEB128Bytes(ChildrenFlag); |
| 543 | DW.EOL(ChildrenString(ChildrenFlag)); |
| 544 | |
| 545 | // For each attribute description. |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 546 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 547 | const DIEAbbrevData &AttrData = Data[i]; |
| 548 | |
| 549 | // Emit attribute type. |
| 550 | DW.EmitULEB128Bytes(AttrData.getAttribute()); |
| 551 | DW.EOL(AttributeString(AttrData.getAttribute())); |
| 552 | |
| 553 | // Emit form type. |
| 554 | DW.EmitULEB128Bytes(AttrData.getForm()); |
| 555 | DW.EOL(FormEncodingString(AttrData.getForm())); |
| 556 | } |
| 557 | |
| 558 | // Mark end of abbreviation. |
| 559 | DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)"); |
| 560 | DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)"); |
| 561 | } |
| 562 | |
| 563 | #ifndef NDEBUG |
| 564 | void DIEAbbrev::print(std::ostream &O) { |
| 565 | O << "Abbreviation @" |
Jeff Cohen | 05ebc8d | 2006-01-25 17:18:50 +0000 | [diff] [blame] | 566 | << std::hex << (intptr_t)this << std::dec |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 567 | << " " |
| 568 | << TagString(Tag) |
| 569 | << " " |
| 570 | << ChildrenString(ChildrenFlag) |
| 571 | << "\n"; |
| 572 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 573 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 574 | O << " " |
| 575 | << AttributeString(Data[i].getAttribute()) |
| 576 | << " " |
| 577 | << FormEncodingString(Data[i].getForm()) |
| 578 | << "\n"; |
| 579 | } |
| 580 | } |
| 581 | void DIEAbbrev::dump() { print(std::cerr); } |
| 582 | #endif |
| 583 | |
| 584 | //===----------------------------------------------------------------------===// |
| 585 | |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 586 | /// BestForm - Choose the best form for integer. |
| 587 | /// |
| 588 | unsigned DIEInteger::BestForm(bool IsSigned) { |
| 589 | if (IsSigned) { |
| 590 | if ((char)Integer == (signed)Integer) return DW_FORM_data1; |
| 591 | if ((short)Integer == (signed)Integer) return DW_FORM_data2; |
| 592 | if ((int)Integer == (signed)Integer) return DW_FORM_data4; |
| 593 | } else { |
| 594 | if ((unsigned char)Integer == Integer) return DW_FORM_data1; |
| 595 | if ((unsigned short)Integer == Integer) return DW_FORM_data2; |
| 596 | if ((unsigned int)Integer == Integer) return DW_FORM_data4; |
| 597 | } |
| 598 | return DW_FORM_data8; |
| 599 | } |
| 600 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 601 | /// EmitValue - Emit integer of appropriate size. |
| 602 | /// |
| 603 | void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
| 604 | switch (Form) { |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 605 | case DW_FORM_flag: // Fall thru |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 606 | case DW_FORM_ref1: // Fall thru |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 607 | case DW_FORM_data1: DW.EmitInt8(Integer); break; |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 608 | case DW_FORM_ref2: // Fall thru |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 609 | case DW_FORM_data2: DW.EmitInt16(Integer); break; |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 610 | case DW_FORM_ref4: // Fall thru |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 611 | case DW_FORM_data4: DW.EmitInt32(Integer); break; |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 612 | case DW_FORM_ref8: // Fall thru |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 613 | case DW_FORM_data8: DW.EmitInt64(Integer); break; |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 614 | case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break; |
| 615 | case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 616 | default: assert(0 && "DIE Value form not supported yet"); break; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /// SizeOf - Determine size of integer value in bytes. |
| 621 | /// |
| 622 | unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 623 | switch (Form) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 624 | case DW_FORM_flag: // Fall thru |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 625 | case DW_FORM_ref1: // Fall thru |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 626 | case DW_FORM_data1: return sizeof(int8_t); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 627 | case DW_FORM_ref2: // Fall thru |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 628 | case DW_FORM_data2: return sizeof(int16_t); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 629 | case DW_FORM_ref4: // Fall thru |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 630 | case DW_FORM_data4: return sizeof(int32_t); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 631 | case DW_FORM_ref8: // Fall thru |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 632 | case DW_FORM_data8: return sizeof(int64_t); |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 633 | case DW_FORM_udata: return DW.SizeULEB128(Integer); |
| 634 | case DW_FORM_sdata: return DW.SizeSLEB128(Integer); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 635 | default: assert(0 && "DIE Value form not supported yet"); break; |
| 636 | } |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | |
| 642 | /// EmitValue - Emit string value. |
| 643 | /// |
| 644 | void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 645 | DW.EmitString(String); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /// SizeOf - Determine size of string value in bytes. |
| 649 | /// |
| 650 | unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | 4002017 | 2006-01-20 21:02:36 +0000 | [diff] [blame] | 651 | return String.size() + sizeof(char); // sizeof('\0'); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
| 656 | /// EmitValue - Emit label value. |
| 657 | /// |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 658 | void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 659 | DW.EmitReference(Label); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /// SizeOf - Determine size of label value in bytes. |
| 663 | /// |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 664 | unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 665 | return DW.getAddressSize(); |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 670 | /// EmitValue - Emit label value. |
| 671 | /// |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 672 | void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 673 | DW.EmitReference(Label); |
| 674 | } |
| 675 | |
| 676 | /// SizeOf - Determine size of label value in bytes. |
| 677 | /// |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 678 | unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 679 | return DW.getAddressSize(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | //===----------------------------------------------------------------------===// |
| 683 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 684 | /// EmitValue - Emit delta value. |
| 685 | /// |
| 686 | void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 687 | DW.EmitDifference(LabelHi, LabelLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /// SizeOf - Determine size of delta value in bytes. |
| 691 | /// |
| 692 | unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 693 | return DW.getAddressSize(); |
| 694 | } |
| 695 | |
| 696 | //===----------------------------------------------------------------------===// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 697 | /// EmitValue - Emit debug information entry offset. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 698 | /// |
| 699 | void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 700 | DW.EmitInt32(Entry->getOffset()); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 703 | /// SizeOf - Determine size of debug information entry value in bytes. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 704 | /// |
| 705 | unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 706 | return sizeof(int32_t); |
| 707 | } |
| 708 | |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 711 | DIEBlock::~DIEBlock() { |
| 712 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 713 | delete Values[i]; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /// ComputeSize - calculate the size of the block. |
| 718 | /// |
| 719 | unsigned DIEBlock::ComputeSize(DwarfWriter &DW) { |
| 720 | Size = 0; |
| 721 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 722 | Size += Values[i]->SizeOf(DW, Forms[i]); |
| 723 | } |
| 724 | return Size; |
| 725 | } |
| 726 | |
| 727 | /// BestForm - Choose the best form for data. |
| 728 | /// |
| 729 | unsigned DIEBlock::BestForm() { |
| 730 | if ((unsigned char)Size == Size) return DW_FORM_block1; |
| 731 | if ((unsigned short)Size == Size) return DW_FORM_block2; |
| 732 | if ((unsigned int)Size == Size) return DW_FORM_block4; |
| 733 | return DW_FORM_block; |
| 734 | } |
| 735 | |
| 736 | /// EmitValue - Emit block data. |
| 737 | /// |
| 738 | void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const { |
| 739 | switch (Form) { |
| 740 | case DW_FORM_block1: DW.EmitInt8(Size); break; |
| 741 | case DW_FORM_block2: DW.EmitInt16(Size); break; |
| 742 | case DW_FORM_block4: DW.EmitInt32(Size); break; |
| 743 | case DW_FORM_block: DW.EmitULEB128Bytes(Size); break; |
| 744 | default: assert(0 && "Improper form for block"); break; |
| 745 | } |
| 746 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 747 | DW.EOL(""); |
| 748 | Values[i]->EmitValue(DW, Forms[i]); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /// SizeOf - Determine size of block data in bytes. |
| 753 | /// |
| 754 | unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const { |
| 755 | switch (Form) { |
| 756 | case DW_FORM_block1: return Size + sizeof(int8_t); |
| 757 | case DW_FORM_block2: return Size + sizeof(int16_t); |
| 758 | case DW_FORM_block4: return Size + sizeof(int32_t); |
| 759 | case DW_FORM_block: return Size + DW.SizeULEB128(Size); |
| 760 | default: assert(0 && "Improper form for block"); break; |
| 761 | } |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | /// AddUInt - Add an unsigned integer value. |
| 766 | /// |
| 767 | void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) { |
| 768 | DIEInteger *DI = new DIEInteger(Integer); |
| 769 | Values.push_back(DI); |
| 770 | if (Form == 0) Form = DI->BestForm(false); |
| 771 | Forms.push_back(Form); |
| 772 | } |
| 773 | |
| 774 | /// AddSInt - Add an signed integer value. |
| 775 | /// |
| 776 | void DIEBlock::AddSInt(unsigned Form, int64_t Integer) { |
| 777 | DIEInteger *DI = new DIEInteger(Integer); |
| 778 | Values.push_back(DI); |
| 779 | if (Form == 0) Form = DI->BestForm(true); |
| 780 | Forms.push_back(Form); |
| 781 | } |
| 782 | |
| 783 | /// AddString - Add a std::string value. |
| 784 | /// |
| 785 | void DIEBlock::AddString(unsigned Form, const std::string &String) { |
| 786 | Values.push_back(new DIEString(String)); |
| 787 | Forms.push_back(Form); |
| 788 | } |
| 789 | |
| 790 | /// AddLabel - Add a Dwarf label value. |
| 791 | /// |
| 792 | void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) { |
| 793 | Values.push_back(new DIEDwarfLabel(Label)); |
| 794 | Forms.push_back(Form); |
| 795 | } |
| 796 | |
| 797 | /// AddObjectLabel - Add a non-Dwarf label value. |
| 798 | /// |
| 799 | void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) { |
| 800 | Values.push_back(new DIEObjectLabel(Label)); |
| 801 | Forms.push_back(Form); |
| 802 | } |
| 803 | |
| 804 | /// AddDelta - Add a label delta value. |
| 805 | /// |
| 806 | void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) { |
| 807 | Values.push_back(new DIEDelta(Hi, Lo)); |
| 808 | Forms.push_back(Form); |
| 809 | } |
| 810 | |
| 811 | /// AddDIEntry - Add a DIE value. |
| 812 | /// |
| 813 | void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) { |
| 814 | Values.push_back(new DIEntry(Entry)); |
| 815 | Forms.push_back(Form); |
| 816 | } |
| 817 | |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 820 | DIE::DIE(unsigned Tag) |
| 821 | : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no)) |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 822 | , AbbrevID(0) |
| 823 | , Offset(0) |
| 824 | , Size(0) |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 825 | , Children() |
| 826 | , Values() |
| 827 | {} |
| 828 | |
| 829 | DIE::~DIE() { |
| 830 | if (Abbrev) delete Abbrev; |
| 831 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 832 | for (unsigned i = 0, N = Children.size(); i < N; ++i) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 833 | delete Children[i]; |
| 834 | } |
| 835 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 836 | for (unsigned j = 0, M = Values.size(); j < M; ++j) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 837 | delete Values[j]; |
| 838 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 841 | /// AddSiblingOffset - Add a sibling offset field to the front of the DIE. |
| 842 | /// |
| 843 | void DIE::AddSiblingOffset() { |
| 844 | DIEInteger *DI = new DIEInteger(0); |
| 845 | Values.insert(Values.begin(), DI); |
| 846 | Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4); |
| 847 | } |
| 848 | |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 849 | /// AddUInt - Add an unsigned integer attribute data and value. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 850 | /// |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 851 | void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) { |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 852 | DIEInteger *DI = new DIEInteger(Integer); |
| 853 | Values.push_back(DI); |
| 854 | if (!Form) Form = DI->BestForm(false); |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 855 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /// AddSInt - Add an signed integer attribute data and value. |
| 859 | /// |
| 860 | void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) { |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 861 | DIEInteger *DI = new DIEInteger(Integer); |
| 862 | Values.push_back(DI); |
| 863 | if (!Form) Form = DI->BestForm(true); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 864 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /// AddString - Add a std::string attribute data and value. |
| 868 | /// |
| 869 | void DIE::AddString(unsigned Attribute, unsigned Form, |
| 870 | const std::string &String) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 871 | Values.push_back(new DIEString(String)); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 872 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /// AddLabel - Add a Dwarf label attribute data and value. |
| 876 | /// |
| 877 | void DIE::AddLabel(unsigned Attribute, unsigned Form, |
| 878 | const DWLabel &Label) { |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 879 | Values.push_back(new DIEDwarfLabel(Label)); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 880 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 883 | /// AddObjectLabel - Add an non-Dwarf label attribute data and value. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 884 | /// |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 885 | void DIE::AddObjectLabel(unsigned Attribute, unsigned Form, |
| 886 | const std::string &Label) { |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 887 | Values.push_back(new DIEObjectLabel(Label)); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 888 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /// AddDelta - Add a label delta attribute data and value. |
| 892 | /// |
| 893 | void DIE::AddDelta(unsigned Attribute, unsigned Form, |
| 894 | const DWLabel &Hi, const DWLabel &Lo) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 895 | Values.push_back(new DIEDelta(Hi, Lo)); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 896 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /// AddDIEntry - Add a DIE attribute data and value. |
| 900 | /// |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 901 | void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 902 | Values.push_back(new DIEntry(Entry)); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 903 | Abbrev->AddAttribute(Attribute, Form); |
| 904 | } |
| 905 | |
| 906 | /// AddBlock - Add block data. |
| 907 | /// |
| 908 | void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) { |
| 909 | assert(Block->Size && "Block size has not been computed"); |
| 910 | Values.push_back(Block); |
| 911 | if (!Form) Form = Block->BestForm(); |
| 912 | Abbrev->AddAttribute(Attribute, Form); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /// Complete - Indicate that all attributes have been added and ready to get an |
| 916 | /// abbreviation ID. |
| 917 | void DIE::Complete(DwarfWriter &DW) { |
| 918 | AbbrevID = DW.NewAbbreviation(Abbrev); |
| 919 | delete Abbrev; |
| 920 | Abbrev = NULL; |
| 921 | } |
| 922 | |
| 923 | /// AddChild - Add a child to the DIE. |
| 924 | /// |
| 925 | void DIE::AddChild(DIE *Child) { |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 926 | assert(Abbrev && "Adding children without an abbreviation"); |
| 927 | Abbrev->setChildrenFlag(DW_CHILDREN_yes); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 928 | Children.push_back(Child); |
| 929 | } |
| 930 | |
| 931 | //===----------------------------------------------------------------------===// |
| 932 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 933 | /// DWContext |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 934 | |
| 935 | //===----------------------------------------------------------------------===// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 936 | |
| 937 | /// PrintHex - Print a value as a hexidecimal value. |
| 938 | /// |
| 939 | void DwarfWriter::PrintHex(int Value) const { |
| 940 | O << "0x" << std::hex << Value << std::dec; |
| 941 | } |
| 942 | |
| 943 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 944 | /// then it will be printed first. Comments should not contain '\n'. |
| 945 | void DwarfWriter::EOL(const std::string &Comment) const { |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 946 | if (DwarfVerbose && !Comment.empty()) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 947 | O << "\t" |
| 948 | << Asm->CommentString |
| 949 | << " " |
| 950 | << Comment; |
| 951 | } |
| 952 | O << "\n"; |
| 953 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 954 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 955 | /// EmitAlign - Print a align directive. |
| 956 | /// |
| 957 | void DwarfWriter::EmitAlign(unsigned Alignment) const { |
| 958 | O << Asm->AlignDirective << Alignment << "\n"; |
| 959 | } |
| 960 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 961 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 962 | /// unsigned leb128 value. |
| 963 | void DwarfWriter::EmitULEB128Bytes(unsigned Value) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 964 | if (hasLEB128) { |
| 965 | O << "\t.uleb128\t" |
| 966 | << Value; |
| 967 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 968 | O << Asm->Data8bitsDirective; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 969 | PrintULEB128(Value); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 970 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 974 | /// signed leb128 value. |
| 975 | void DwarfWriter::EmitSLEB128Bytes(int Value) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 976 | if (hasLEB128) { |
| 977 | O << "\t.sleb128\t" |
| 978 | << Value; |
| 979 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 980 | O << Asm->Data8bitsDirective; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 981 | PrintSLEB128(Value); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 982 | } |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 985 | /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 986 | /// representing an unsigned leb128 value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 987 | void DwarfWriter::PrintULEB128(unsigned Value) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 988 | do { |
| 989 | unsigned Byte = Value & 0x7f; |
| 990 | Value >>= 7; |
| 991 | if (Value) Byte |= 0x80; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 992 | PrintHex(Byte); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 993 | if (Value) O << ", "; |
| 994 | } while (Value); |
| 995 | } |
| 996 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 997 | /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128 |
| 998 | /// value. |
| 999 | unsigned DwarfWriter::SizeULEB128(unsigned Value) { |
| 1000 | unsigned Size = 0; |
| 1001 | do { |
| 1002 | Value >>= 7; |
| 1003 | Size += sizeof(int8_t); |
| 1004 | } while (Value); |
| 1005 | return Size; |
| 1006 | } |
| 1007 | |
| 1008 | /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1009 | /// representing a signed leb128 value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1010 | void DwarfWriter::PrintSLEB128(int Value) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1011 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 1012 | bool IsMore; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1013 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1014 | do { |
| 1015 | unsigned Byte = Value & 0x7f; |
| 1016 | Value >>= 7; |
| 1017 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 1018 | if (IsMore) Byte |= 0x80; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1019 | PrintHex(Byte); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1020 | if (IsMore) O << ", "; |
| 1021 | } while (IsMore); |
| 1022 | } |
| 1023 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1024 | /// SizeSLEB128 - Compute the number of bytes required for a signed leb128 |
| 1025 | /// value. |
| 1026 | unsigned DwarfWriter::SizeSLEB128(int Value) { |
| 1027 | unsigned Size = 0; |
| 1028 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 1029 | bool IsMore; |
| 1030 | |
| 1031 | do { |
| 1032 | unsigned Byte = Value & 0x7f; |
| 1033 | Value >>= 7; |
| 1034 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 1035 | Size += sizeof(int8_t); |
| 1036 | } while (IsMore); |
| 1037 | return Size; |
| 1038 | } |
| 1039 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1040 | /// EmitInt8 - Emit a byte directive and value. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1041 | /// |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1042 | void DwarfWriter::EmitInt8(int Value) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1043 | O << Asm->Data8bitsDirective; |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1044 | PrintHex(Value & 0xFF); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1047 | /// EmitInt16 - Emit a short directive and value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1048 | /// |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1049 | void DwarfWriter::EmitInt16(int Value) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1050 | O << Asm->Data16bitsDirective; |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1051 | PrintHex(Value & 0xFFFF); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1054 | /// EmitInt32 - Emit a long directive and value. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1055 | /// |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1056 | void DwarfWriter::EmitInt32(int Value) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1057 | O << Asm->Data32bitsDirective; |
| 1058 | PrintHex(Value); |
| 1059 | } |
| 1060 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1061 | /// EmitInt64 - Emit a long long directive and value. |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 1062 | /// |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1063 | void DwarfWriter::EmitInt64(uint64_t Value) const { |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 1064 | if (Asm->Data64bitsDirective) { |
| 1065 | O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec; |
| 1066 | } else { |
| 1067 | const TargetData &TD = Asm->TM.getTargetData(); |
| 1068 | |
| 1069 | if (TD.isBigEndian()) { |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1070 | EmitInt32(unsigned(Value >> 32)); O << "\n"; |
| 1071 | EmitInt32(unsigned(Value)); |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 1072 | } else { |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1073 | EmitInt32(unsigned(Value)); O << "\n"; |
| 1074 | EmitInt32(unsigned(Value >> 32)); |
Jim Laskey | d8f77ba | 2006-01-27 15:20:54 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1079 | /// EmitString - Emit a string with quotes and a null terminator. |
| 1080 | /// Special characters are emitted properly. (Eg. '\t') |
| 1081 | void DwarfWriter::EmitString(const std::string &String) const { |
| 1082 | O << Asm->AsciiDirective |
| 1083 | << "\""; |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1084 | for (unsigned i = 0, N = String.size(); i < N; ++i) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1085 | unsigned char C = String[i]; |
| 1086 | |
| 1087 | if (!isascii(C) || iscntrl(C)) { |
| 1088 | switch(C) { |
| 1089 | case '\b': O << "\\b"; break; |
| 1090 | case '\f': O << "\\f"; break; |
| 1091 | case '\n': O << "\\n"; break; |
| 1092 | case '\r': O << "\\r"; break; |
| 1093 | case '\t': O << "\\t"; break; |
| 1094 | default: |
| 1095 | O << '\\'; |
| 1096 | O << char('0' + (C >> 6)); |
| 1097 | O << char('0' + (C >> 3)); |
| 1098 | O << char('0' + (C >> 0)); |
| 1099 | break; |
| 1100 | } |
| 1101 | } else if (C == '\"') { |
| 1102 | O << "\\\""; |
| 1103 | } else if (C == '\'') { |
| 1104 | O << "\\\'"; |
| 1105 | } else { |
| 1106 | O << C; |
| 1107 | } |
| 1108 | } |
| 1109 | O << "\\0\""; |
| 1110 | } |
| 1111 | |
| 1112 | /// PrintLabelName - Print label name in form used by Dwarf writer. |
| 1113 | /// |
| 1114 | void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1115 | O << Asm->PrivateGlobalPrefix |
| 1116 | << "debug_" |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1117 | << Tag; |
| 1118 | if (Number) O << Number; |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1121 | /// EmitLabel - Emit location label for internal use by Dwarf. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1122 | /// |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1123 | void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const { |
| 1124 | PrintLabelName(Tag, Number); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1125 | O << ":\n"; |
| 1126 | } |
| 1127 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1128 | /// EmitReference - Emit a reference to a label. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1129 | /// |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 1130 | void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1131 | if (AddressSize == 4) |
| 1132 | O << Asm->Data32bitsDirective; |
| 1133 | else |
| 1134 | O << Asm->Data64bitsDirective; |
| 1135 | |
| 1136 | PrintLabelName(Tag, Number); |
| 1137 | } |
Jim Laskey | 7368321 | 2006-01-21 00:59:54 +0000 | [diff] [blame] | 1138 | void DwarfWriter::EmitReference(const std::string &Name) const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1139 | if (AddressSize == 4) |
| 1140 | O << Asm->Data32bitsDirective; |
| 1141 | else |
| 1142 | O << Asm->Data64bitsDirective; |
| 1143 | |
| 1144 | O << Name; |
| 1145 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1146 | |
| 1147 | /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some |
| 1148 | /// assemblers do not accept absolute expressions with data directives, so there |
| 1149 | /// is an option (needsSet) to use an intermediary 'set' expression. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1150 | void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi, |
| 1151 | const char *TagLo, unsigned NumberLo) const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1152 | if (needsSet) { |
| 1153 | static unsigned SetCounter = 0; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1154 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1155 | O << "\t.set\t"; |
| 1156 | PrintLabelName("set", SetCounter); |
| 1157 | O << ","; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1158 | PrintLabelName(TagHi, NumberHi); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1159 | O << "-"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1160 | PrintLabelName(TagLo, NumberLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1161 | O << "\n"; |
| 1162 | |
| 1163 | if (AddressSize == sizeof(int32_t)) |
| 1164 | O << Asm->Data32bitsDirective; |
| 1165 | else |
| 1166 | O << Asm->Data64bitsDirective; |
| 1167 | |
| 1168 | PrintLabelName("set", SetCounter); |
| 1169 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1170 | ++SetCounter; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1171 | } else { |
| 1172 | if (AddressSize == sizeof(int32_t)) |
| 1173 | O << Asm->Data32bitsDirective; |
| 1174 | else |
| 1175 | O << Asm->Data64bitsDirective; |
| 1176 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1177 | PrintLabelName(TagHi, NumberHi); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1178 | O << "-"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1179 | PrintLabelName(TagLo, NumberLo); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1183 | /// NewAbbreviation - Add the abbreviation to the Abbreviation vector. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1184 | /// |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1185 | unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) { |
| 1186 | return Abbreviations.insert(*Abbrev); |
| 1187 | } |
| 1188 | |
| 1189 | /// NewString - Add a string to the constant pool and returns a label. |
| 1190 | /// |
| 1191 | DWLabel DwarfWriter::NewString(const std::string &String) { |
| 1192 | unsigned StringID = StringPool.insert(String); |
| 1193 | return DWLabel("string", StringID); |
| 1194 | } |
| 1195 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1196 | /// AddSourceLine - Add location information to specified debug information |
| 1197 | /// entry. |
| 1198 | void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) { |
| 1199 | if (File && Line) { |
| 1200 | CompileUnit *FileUnit = FindCompileUnit(File); |
| 1201 | unsigned FileID = FileUnit->getID(); |
| 1202 | Die->AddUInt(DW_AT_decl_file, 0, FileID); |
| 1203 | Die->AddUInt(DW_AT_decl_line, 0, Line); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1208 | /// NewBasicType - Creates a new basic type if necessary, then adds to the |
| 1209 | /// owner. |
| 1210 | /// FIXME - Should never be needed. |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1211 | DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) { |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1212 | DIE *&Slot = TypeToDieMap[Ty]; |
| 1213 | if (Slot) return Slot; |
| 1214 | |
| 1215 | const char *Name; |
| 1216 | unsigned Size; |
| 1217 | unsigned Encoding = 0; |
| 1218 | |
| 1219 | switch (Ty->getTypeID()) { |
| 1220 | case Type::UByteTyID: |
| 1221 | Name = "unsigned char"; |
| 1222 | Size = 1; |
| 1223 | Encoding = DW_ATE_unsigned_char; |
| 1224 | break; |
| 1225 | case Type::SByteTyID: |
| 1226 | Name = "char"; |
| 1227 | Size = 1; |
| 1228 | Encoding = DW_ATE_signed_char; |
| 1229 | break; |
| 1230 | case Type::UShortTyID: |
| 1231 | Name = "unsigned short"; |
| 1232 | Size = 2; |
| 1233 | Encoding = DW_ATE_unsigned; |
| 1234 | break; |
| 1235 | case Type::ShortTyID: |
| 1236 | Name = "short"; |
| 1237 | Size = 2; |
| 1238 | Encoding = DW_ATE_signed; |
| 1239 | break; |
| 1240 | case Type::UIntTyID: |
| 1241 | Name = "unsigned int"; |
| 1242 | Size = 4; |
| 1243 | Encoding = DW_ATE_unsigned; |
| 1244 | break; |
| 1245 | case Type::IntTyID: |
| 1246 | Name = "int"; |
| 1247 | Size = 4; |
| 1248 | Encoding = DW_ATE_signed; |
| 1249 | break; |
| 1250 | case Type::ULongTyID: |
| 1251 | Name = "unsigned long long"; |
| 1252 | Size = 7; |
| 1253 | Encoding = DW_ATE_unsigned; |
| 1254 | break; |
| 1255 | case Type::LongTyID: |
| 1256 | Name = "long long"; |
| 1257 | Size = 7; |
| 1258 | Encoding = DW_ATE_signed; |
| 1259 | break; |
| 1260 | case Type::FloatTyID: |
| 1261 | Name = "float"; |
| 1262 | Size = 4; |
| 1263 | Encoding = DW_ATE_float; |
| 1264 | break; |
| 1265 | case Type::DoubleTyID: |
| 1266 | Name = "double"; |
| 1267 | Size = 8; |
| 1268 | Encoding = DW_ATE_float; |
| 1269 | break; |
| 1270 | default: |
| 1271 | // FIXME - handle more complex types. |
| 1272 | Name = "unknown"; |
| 1273 | Size = 1; |
| 1274 | Encoding = DW_ATE_address; |
| 1275 | break; |
| 1276 | } |
| 1277 | |
| 1278 | // construct the type DIE. |
| 1279 | Slot = new DIE(DW_TAG_base_type); |
| 1280 | Slot->AddString(DW_AT_name, DW_FORM_string, Name); |
| 1281 | Slot->AddUInt (DW_AT_byte_size, 0, Size); |
| 1282 | Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding); |
| 1283 | |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1284 | // Add to context. |
| 1285 | Context->AddChild(Slot); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1286 | |
| 1287 | return Slot; |
| 1288 | } |
| 1289 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1290 | /// NewType - Create a new type DIE. |
| 1291 | /// |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1292 | DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) { |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1293 | if (!TyDesc) return NewBasicType(Context, Type::IntTy); |
| 1294 | |
| 1295 | // FIXME - Should handle other contexts that compile units. |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1296 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1297 | // Check for pre-existence. |
| 1298 | DIE *&Slot = DescToDieMap[TyDesc]; |
| 1299 | if (Slot) return Slot; |
| 1300 | |
| 1301 | // Get core information. |
| 1302 | const std::string &Name = TyDesc->getName(); |
Jim Laskey | 288fe0f | 2006-03-01 18:13:05 +0000 | [diff] [blame] | 1303 | uint64_t Size = TyDesc->getSize() >> 3; |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1304 | |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1305 | DIE *Ty = NULL; |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1306 | |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1307 | if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) { |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1308 | // Fundamental types like int, float, bool |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1309 | Slot = Ty = new DIE(DW_TAG_base_type); |
| 1310 | unsigned Encoding = BasicTy->getEncoding(); |
| 1311 | Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding); |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1312 | } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) { |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1313 | // Create specific DIE. |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 1314 | Slot = Ty = new DIE(DerivedTy->getTag()); |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1315 | |
| 1316 | // Map to main type, void will not have a type. |
| 1317 | if (TypeDesc *FromTy = DerivedTy->getFromType()) { |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1318 | Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy)); |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1319 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1320 | } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) { |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1321 | // Create specific DIE. |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 1322 | Slot = Ty = new DIE(CompTy->getTag()); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1323 | std::vector<DebugInfoDesc *> &Elements = CompTy->getElements(); |
| 1324 | |
| 1325 | switch (CompTy->getTag()) { |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 1326 | case DW_TAG_array_type: { |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1327 | // Add element type. |
| 1328 | if (TypeDesc *FromTy = CompTy->getFromType()) { |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1329 | Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy)); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1330 | } |
| 1331 | // Don't emit size attribute. |
| 1332 | Size = 0; |
| 1333 | |
| 1334 | // Construct an anonymous type for index type. |
| 1335 | DIE *IndexTy = new DIE(DW_TAG_base_type); |
| 1336 | IndexTy->AddUInt(DW_AT_byte_size, 0, 4); |
| 1337 | IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); |
| 1338 | // Add to context. |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1339 | Context->AddChild(IndexTy); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Add subranges to array type. |
| 1342 | for(unsigned i = 0, N = Elements.size(); i < N; ++i) { |
| 1343 | SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]); |
| 1344 | int64_t Lo = SRD->getLo(); |
| 1345 | int64_t Hi = SRD->getHi(); |
| 1346 | DIE *Subrange = new DIE(DW_TAG_subrange_type); |
| 1347 | |
| 1348 | // If a range is available. |
| 1349 | if (Lo != Hi) { |
| 1350 | Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy); |
| 1351 | // Only add low if non-zero. |
Jim Laskey | 6a3eb01 | 2006-03-01 23:52:37 +0000 | [diff] [blame] | 1352 | if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo); |
| 1353 | Subrange->AddSInt(DW_AT_upper_bound, 0, Hi); |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1354 | } |
| 1355 | Ty->AddChild(Subrange); |
| 1356 | } |
| 1357 | |
| 1358 | break; |
| 1359 | } |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1360 | case DW_TAG_structure_type: |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 1361 | case DW_TAG_union_type: { |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1362 | // FIXME - this is just the basics. |
| 1363 | // Add elements to structure type. |
| 1364 | for(unsigned i = 0, N = Elements.size(); i < N; ++i) { |
| 1365 | DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Extract the basic information. |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1368 | const std::string &Name = MemberDesc->getName(); |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1369 | TypeDesc *MemTy = MemberDesc->getFromType(); |
| 1370 | uint64_t Size = MemberDesc->getSize(); |
Chris Lattner | 2695de4 | 2006-03-09 17:48:46 +0000 | [diff] [blame] | 1371 | uint64_t Align = MemberDesc->getAlign(); |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1372 | uint64_t Offset = MemberDesc->getOffset(); |
| 1373 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1374 | // Construct member debug information entry. |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1375 | DIE *Member = new DIE(DW_TAG_member); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1376 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1377 | // Add name if not "". |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1378 | if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1379 | // Add location if available. |
| 1380 | AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine()); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1381 | |
Jim Laskey | 54689c2 | 2006-03-09 13:28:47 +0000 | [diff] [blame] | 1382 | // Most of the time the field info is the same as the members. |
| 1383 | uint64_t FieldSize = Size; |
| 1384 | uint64_t FieldAlign = Align; |
| 1385 | uint64_t FieldOffset = Offset; |
Jim Laskey | 20c3ed8 | 2006-03-07 15:51:33 +0000 | [diff] [blame] | 1386 | |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1387 | if (TypeDesc *FromTy = MemberDesc->getFromType()) { |
| 1388 | Member->AddDIEntry(DW_AT_type, DW_FORM_ref4, |
| 1389 | NewType(Context, FromTy)); |
Jim Laskey | 54689c2 | 2006-03-09 13:28:47 +0000 | [diff] [blame] | 1390 | FieldSize = FromTy->getSize(); |
| 1391 | FieldAlign = FromTy->getSize(); |
Jim Laskey | 20c3ed8 | 2006-03-07 15:51:33 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Jim Laskey | 54689c2 | 2006-03-09 13:28:47 +0000 | [diff] [blame] | 1394 | // Unless we have a bit field. |
| 1395 | if (FieldSize != Size) { |
| 1396 | // Construct the alignment mask. |
| 1397 | uint64_t AlignMask = ~(FieldAlign - 1); |
| 1398 | // Determine the high bit + 1 of the declared size. |
| 1399 | uint64_t HiMark = (Offset + FieldSize) & AlignMask; |
| 1400 | // Work backwards to determine the base offset of the field. |
| 1401 | FieldOffset = HiMark - FieldSize; |
| 1402 | // Now normalize offset to the field. |
| 1403 | Offset -= FieldOffset; |
| 1404 | |
| 1405 | // Maybe we need to work from the other. |
| 1406 | const TargetData &TD = Asm->TM.getTargetData(); |
| 1407 | if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size); |
| 1408 | |
| 1409 | Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3); |
| 1410 | Member->AddUInt(DW_AT_bit_size, 0, Size); |
| 1411 | Member->AddUInt(DW_AT_bit_offset, 0, Offset); |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1412 | } |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Add computation for offset. |
| 1415 | DIEBlock *Block = new DIEBlock(); |
| 1416 | Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst); |
Jim Laskey | 54689c2 | 2006-03-09 13:28:47 +0000 | [diff] [blame] | 1417 | Block->AddUInt(DW_FORM_udata, FieldOffset >> 3); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1418 | Block->ComputeSize(*this); |
| 1419 | Member->AddBlock(DW_AT_data_member_location, 0, Block); |
| 1420 | |
Jim Laskey | f01e547 | 2006-03-03 15:06:57 +0000 | [diff] [blame] | 1421 | Ty->AddChild(Member); |
| 1422 | } |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | } |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 1425 | case DW_TAG_enumeration_type: { |
Jim Laskey | 6a3eb01 | 2006-03-01 23:52:37 +0000 | [diff] [blame] | 1426 | // Add enumerators to enumeration type. |
| 1427 | for(unsigned i = 0, N = Elements.size(); i < N; ++i) { |
| 1428 | EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]); |
| 1429 | const std::string &Name = ED->getName(); |
| 1430 | int64_t Value = ED->getValue(); |
| 1431 | DIE *Enumerator = new DIE(DW_TAG_enumerator); |
| 1432 | Enumerator->AddString(DW_AT_name, DW_FORM_string, Name); |
| 1433 | Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value); |
| 1434 | Ty->AddChild(Enumerator); |
| 1435 | } |
| 1436 | |
Jim Laskey | f8913f1 | 2006-03-01 17:53:02 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | } |
| 1439 | default: break; |
| 1440 | } |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1441 | } |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1442 | |
| 1443 | assert(Ty && "Type not supported yet"); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1444 | |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1445 | // Add size if non-zero (derived types don't have a size.) |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1446 | if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size); |
Jim Laskey | 6990600 | 2006-02-24 16:46:40 +0000 | [diff] [blame] | 1447 | // Add name if not anonymous or intermediate type. |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1448 | if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1449 | // Add source line info if available. |
| 1450 | AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine()); |
Jim Laskey | 434b40b | 2006-02-23 22:37:30 +0000 | [diff] [blame] | 1451 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1452 | // Add to context owner. |
Jim Laskey | 92ae740 | 2006-03-01 18:20:30 +0000 | [diff] [blame] | 1453 | Context->AddChild(Ty); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1454 | |
| 1455 | return Slot; |
| 1456 | } |
| 1457 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1458 | /// NewCompileUnit - Create new compile unit and it's debug information entry. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1459 | /// |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1460 | CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc, |
| 1461 | unsigned ID) { |
| 1462 | // Construct debug information entry. |
| 1463 | DIE *Die = new DIE(DW_TAG_compile_unit); |
| 1464 | Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0)); |
| 1465 | Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0)); |
| 1466 | Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0)); |
| 1467 | Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer()); |
| 1468 | Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage()); |
| 1469 | Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName()); |
| 1470 | Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory()); |
| 1471 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1472 | // Add debug information entry to descriptor map. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1473 | DescToDieMap[UnitDesc] = Die; |
| 1474 | |
| 1475 | // Construct compile unit. |
| 1476 | CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die); |
| 1477 | |
| 1478 | // Add Unit to compile unit map. |
| 1479 | DescToUnitMap[UnitDesc] = Unit; |
| 1480 | |
| 1481 | return Unit; |
| 1482 | } |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1483 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1484 | /// FindCompileUnit - Get the compile unit for the given descriptor. |
| 1485 | /// |
| 1486 | CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) { |
| 1487 | CompileUnit *Unit = DescToUnitMap[UnitDesc]; |
| 1488 | assert(Unit && "Missing compile unit."); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1489 | return Unit; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1492 | /// NewGlobalVariable - Add a new global variable DIE. |
| 1493 | /// |
| 1494 | DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) { |
| 1495 | // Check for pre-existence. |
| 1496 | DIE *&Slot = DescToDieMap[GVD]; |
| 1497 | if (Slot) return Slot; |
| 1498 | |
| 1499 | // Get the compile unit context. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1500 | CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext()); |
| 1501 | CompileUnit *Unit = FindCompileUnit(UnitDesc); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1502 | // Get the global variable itself. |
| 1503 | GlobalVariable *GV = GVD->getGlobalVariable(); |
| 1504 | // Generate the mangled name. |
| 1505 | std::string MangledName = Asm->Mang->getValueName(GV); |
| 1506 | |
| 1507 | // Gather the details (simplify add attribute code.) |
| 1508 | const std::string &Name = GVD->getName(); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1509 | |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1510 | // Get the global's type. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1511 | DIE *Type = NewType(Unit->getDie(), GVD->getType()); |
Jim Laskey | f4afdd9 | 2006-02-23 16:58:18 +0000 | [diff] [blame] | 1512 | |
| 1513 | // Create the globale variable DIE. |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1514 | DIE *VariableDie = new DIE(DW_TAG_variable); |
| 1515 | VariableDie->AddString (DW_AT_name, DW_FORM_string, Name); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1516 | VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type); |
| 1517 | VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1518 | |
| 1519 | // Add source line info if available. |
| 1520 | AddSourceLine(VariableDie, UnitDesc, GVD->getLine()); |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1521 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1522 | // Add address. |
Jim Laskey | b80af6f | 2006-03-03 21:00:14 +0000 | [diff] [blame] | 1523 | DIEBlock *Block = new DIEBlock(); |
| 1524 | Block->AddUInt(DW_FORM_data1, DW_OP_addr); |
| 1525 | Block->AddObjectLabel(DW_FORM_udata, MangledName); |
| 1526 | Block->ComputeSize(*this); |
| 1527 | VariableDie->AddBlock(DW_AT_location, 0, Block); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1528 | |
| 1529 | // Add to map. |
| 1530 | Slot = VariableDie; |
| 1531 | |
| 1532 | // Add to context owner. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1533 | Unit->getDie()->AddChild(VariableDie); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Expose as global. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1536 | // FIXME - need to check external flag. |
| 1537 | Unit->AddGlobal(Name, VariableDie); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1538 | |
| 1539 | return VariableDie; |
| 1540 | } |
| 1541 | |
| 1542 | /// NewSubprogram - Add a new subprogram DIE. |
| 1543 | /// |
| 1544 | DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) { |
| 1545 | // Check for pre-existence. |
| 1546 | DIE *&Slot = DescToDieMap[SPD]; |
| 1547 | if (Slot) return Slot; |
| 1548 | |
| 1549 | // Get the compile unit context. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1550 | CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext()); |
| 1551 | CompileUnit *Unit = FindCompileUnit(UnitDesc); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1552 | |
| 1553 | // Gather the details (simplify add attribute code.) |
| 1554 | const std::string &Name = SPD->getName(); |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 1555 | DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy); |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 1556 | unsigned IsExternal = SPD->isStatic() ? 0 : 1; |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1557 | |
Jim Laskey | 8a8e975 | 2006-02-27 20:37:42 +0000 | [diff] [blame] | 1558 | DIE *SubprogramDie = new DIE(DW_TAG_subprogram); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1559 | SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1560 | SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type); |
Jim Laskey | 9d0ff8e | 2006-03-15 19:09:58 +0000 | [diff] [blame] | 1561 | SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1562 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1563 | // Add source line info if available. |
| 1564 | AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine()); |
| 1565 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1566 | // Add to map. |
| 1567 | Slot = SubprogramDie; |
| 1568 | |
| 1569 | // Add to context owner. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1570 | Unit->getDie()->AddChild(SubprogramDie); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Expose as global. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1573 | Unit->AddGlobal(Name, SubprogramDie); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1574 | |
| 1575 | return SubprogramDie; |
| 1576 | } |
| 1577 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1578 | |
| 1579 | /// NewScopeVariable - Create a new scope variable. |
| 1580 | /// |
| 1581 | DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) { |
| 1582 | // Get the descriptor. |
| 1583 | VariableDesc *VD = DV->getDesc(); |
| 1584 | |
| 1585 | // Translate tag to proper Dwarf tag. The result variable is dropped for now. |
| 1586 | unsigned Tag; |
| 1587 | switch (VD->getTag()) { |
| 1588 | case DW_TAG_return_variable: return NULL; |
| 1589 | case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break; |
| 1590 | case DW_TAG_auto_variable: // fall thru |
| 1591 | default: Tag = DW_TAG_variable; break; |
| 1592 | } |
| 1593 | |
| 1594 | // Define variable debug information entry. |
| 1595 | DIE *VariableDie = new DIE(Tag); |
| 1596 | VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName()); |
| 1597 | |
| 1598 | // Add source line info if available. |
| 1599 | AddSourceLine(VariableDie, VD->getFile(), VD->getLine()); |
| 1600 | |
| 1601 | // Add variable type. |
| 1602 | DIE *Type = NewType(Unit->getDie(), VD->getType()); |
| 1603 | VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type); |
| 1604 | |
| 1605 | // Get variable address. |
| 1606 | MachineLocation Location; |
| 1607 | Asm->TM.getRegisterInfo()->getLocation(*MF, DV->getFrameIndex(), Location); |
| 1608 | |
| 1609 | // Add computation for variable. |
| 1610 | DIEBlock *Block = new DIEBlock(); |
| 1611 | if (Location.isRegister()) { |
| 1612 | // FIXME - This is a real hack. |
| 1613 | Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Location.getRegister()); |
| 1614 | } else { |
| 1615 | // FIXME - This is a real hack. |
| 1616 | Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Location.getRegister()); |
| 1617 | Block->AddUInt(DW_FORM_sdata, Location.getOffset()); |
| 1618 | } |
| 1619 | Block->ComputeSize(*this); |
| 1620 | VariableDie->AddBlock(DW_AT_location, 0, Block); |
| 1621 | |
| 1622 | return VariableDie; |
| 1623 | } |
| 1624 | |
| 1625 | /// ConstructScope - Construct the components of a scope. |
| 1626 | /// |
| 1627 | void DwarfWriter::ConstructScope(DebugScope *ParentScope, |
| 1628 | DIE *ParentDie, CompileUnit *Unit) { |
| 1629 | // Add variables to scope. |
| 1630 | std::vector<DebugVariable *> &Variables = ParentScope->getVariables(); |
| 1631 | for (unsigned i = 0, N = Variables.size(); i < N; ++i) { |
| 1632 | DIE *VariableDie = NewScopeVariable(Variables[i], Unit); |
| 1633 | if (VariableDie) ParentDie->AddChild(VariableDie); |
| 1634 | } |
| 1635 | |
| 1636 | // Add nested scopes. |
| 1637 | std::vector<DebugScope *> &Scopes = ParentScope->getScopes(); |
| 1638 | for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { |
| 1639 | // Define the Scope debug information entry. |
| 1640 | DebugScope *Scope = Scopes[j]; |
| 1641 | // FIXME - Ignore inlined functions for the time being. |
| 1642 | if (Scope->getParent()) continue; |
| 1643 | |
| 1644 | DIE *ScopeDie = new DIE(DW_TAG_lexical_block); |
| 1645 | |
| 1646 | // Add the scope bounds. |
| 1647 | if (unsigned StartID = Scope->getStartLabelID()) { |
| 1648 | ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr, |
| 1649 | DWLabel("loc", StartID)); |
| 1650 | } else { |
| 1651 | ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr, |
| 1652 | DWLabel("func_begin", SubprogramCount)); |
| 1653 | } |
| 1654 | if (unsigned EndID = Scope->getEndLabelID()) { |
| 1655 | ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr, |
| 1656 | DWLabel("loc", EndID)); |
| 1657 | } else { |
| 1658 | ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr, |
| 1659 | DWLabel("func_end", SubprogramCount)); |
| 1660 | } |
| 1661 | |
| 1662 | // Add the scope contents. |
| 1663 | ConstructScope(Scope, ScopeDie, Unit); |
| 1664 | ParentDie->AddChild(ScopeDie); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | /// ConstructRootScope - Construct the scope for the subprogram. |
| 1669 | /// |
| 1670 | void DwarfWriter::ConstructRootScope(DebugScope *RootScope) { |
| 1671 | // Exit if there is no root scope. |
| 1672 | if (!RootScope) return; |
| 1673 | |
| 1674 | // Get the subprogram debug information entry. |
| 1675 | SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc()); |
| 1676 | DIE *SPDie = DescToDieMap[SPD]; |
| 1677 | assert(SPDie && "Missing subprogram descriptor"); |
| 1678 | |
| 1679 | // Add the function bounds. |
| 1680 | SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr, |
| 1681 | DWLabel("func_begin", SubprogramCount)); |
| 1682 | SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr, |
| 1683 | DWLabel("func_end", SubprogramCount)); |
| 1684 | |
| 1685 | CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext()); |
| 1686 | CompileUnit *Unit = FindCompileUnit(UnitDesc); |
| 1687 | |
| 1688 | ConstructScope(RootScope, SPDie, Unit); |
| 1689 | } |
| 1690 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1691 | /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc |
| 1692 | /// tools to recognize the object file contains Dwarf information. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1693 | /// |
| 1694 | void DwarfWriter::EmitInitial() const { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1695 | // Dwarf sections base addresses. |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1696 | Asm->SwitchSection(DwarfFrameSection, 0); |
| 1697 | EmitLabel("section_frame", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1698 | Asm->SwitchSection(DwarfInfoSection, 0); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1699 | EmitLabel("section_info", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1700 | EmitLabel("info", 0); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1701 | Asm->SwitchSection(DwarfAbbrevSection, 0); |
| 1702 | EmitLabel("section_abbrev", 0); |
| 1703 | EmitLabel("abbrev", 0); |
| 1704 | Asm->SwitchSection(DwarfARangesSection, 0); |
| 1705 | EmitLabel("section_aranges", 0); |
| 1706 | Asm->SwitchSection(DwarfMacInfoSection, 0); |
| 1707 | EmitLabel("section_macinfo", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1708 | Asm->SwitchSection(DwarfLineSection, 0); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1709 | EmitLabel("section_line", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1710 | EmitLabel("line", 0); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1711 | Asm->SwitchSection(DwarfLocSection, 0); |
| 1712 | EmitLabel("section_loc", 0); |
| 1713 | Asm->SwitchSection(DwarfPubNamesSection, 0); |
| 1714 | EmitLabel("section_pubnames", 0); |
| 1715 | Asm->SwitchSection(DwarfStrSection, 0); |
| 1716 | EmitLabel("section_str", 0); |
| 1717 | Asm->SwitchSection(DwarfRangesSection, 0); |
| 1718 | EmitLabel("section_ranges", 0); |
| 1719 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1720 | Asm->SwitchSection(TextSection, 0); |
| 1721 | EmitLabel("text_begin", 0); |
| 1722 | Asm->SwitchSection(DataSection, 0); |
| 1723 | EmitLabel("data_begin", 0); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1726 | /// EmitDIE - Recusively Emits a debug information entry. |
| 1727 | /// |
| 1728 | void DwarfWriter::EmitDIE(DIE *Die) const { |
| 1729 | // Get the abbreviation for this DIE. |
| 1730 | unsigned AbbrevID = Die->getAbbrevID(); |
| 1731 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1732 | |
| 1733 | O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1734 | |
| 1735 | // Emit the code (index) for the abbreviation. |
| 1736 | EmitULEB128Bytes(AbbrevID); |
| 1737 | EOL(std::string("Abbrev [" + |
| 1738 | utostr(AbbrevID) + |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1739 | "] 0x" + utohexstr(Die->getOffset()) + |
| 1740 | ":0x" + utohexstr(Die->getSize()) + " " + |
| 1741 | TagString(Abbrev.getTag()))); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1742 | |
| 1743 | const std::vector<DIEValue *> &Values = Die->getValues(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1744 | const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1745 | |
| 1746 | // Emit the DIE attribute values. |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1747 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1748 | unsigned Attr = AbbrevData[i].getAttribute(); |
| 1749 | unsigned Form = AbbrevData[i].getForm(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1750 | assert(Form && "Too many attributes for DIE (check abbreviation)"); |
| 1751 | |
| 1752 | switch (Attr) { |
| 1753 | case DW_AT_sibling: { |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1754 | EmitInt32(Die->SiblingOffset()); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1755 | break; |
| 1756 | } |
| 1757 | default: { |
| 1758 | // Emit an attribute using the defined form. |
| 1759 | Values[i]->EmitValue(*this, Form); |
| 1760 | break; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | EOL(AttributeString(Attr)); |
| 1765 | } |
| 1766 | |
| 1767 | // Emit the DIE children if any. |
| 1768 | if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) { |
| 1769 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1770 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1771 | for (unsigned j = 0, M = Children.size(); j < M; ++j) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1772 | EmitDIE(Children[j]); |
| 1773 | } |
| 1774 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1775 | EmitInt8(0); EOL("End Of Children Mark"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | /// SizeAndOffsetDie - Compute the size and offset of a DIE. |
| 1780 | /// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1781 | unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) { |
| 1782 | // Get the children. |
| 1783 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1784 | |
| 1785 | // If not last sibling and has children then add sibling offset attribute. |
| 1786 | if (!Last && !Children.empty()) Die->AddSiblingOffset(); |
| 1787 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1788 | // Record the abbreviation. |
| 1789 | Die->Complete(*this); |
| 1790 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1791 | // Get the abbreviation for this DIE. |
| 1792 | unsigned AbbrevID = Die->getAbbrevID(); |
| 1793 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
| 1794 | |
| 1795 | // Set DIE offset |
| 1796 | Die->setOffset(Offset); |
| 1797 | |
| 1798 | // Start the size with the size of abbreviation code. |
| 1799 | Offset += SizeULEB128(AbbrevID); |
| 1800 | |
| 1801 | const std::vector<DIEValue *> &Values = Die->getValues(); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1802 | const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1803 | |
| 1804 | // Emit the DIE attribute values. |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1805 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1806 | // Size attribute value. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1807 | Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm()); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | // Emit the DIE children if any. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1811 | if (!Children.empty()) { |
| 1812 | assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes && |
| 1813 | "Children flag not set"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1814 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1815 | for (unsigned j = 0, M = Children.size(); j < M; ++j) { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1816 | Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | // End of children marker. |
| 1820 | Offset += sizeof(int8_t); |
| 1821 | } |
| 1822 | |
| 1823 | Die->setSize(Offset - Die->getOffset()); |
| 1824 | return Offset; |
| 1825 | } |
| 1826 | |
| 1827 | /// SizeAndOffsets - Compute the size and offset of all the DIEs. |
| 1828 | /// |
| 1829 | void DwarfWriter::SizeAndOffsets() { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1830 | |
| 1831 | // Process each compile unit. |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1832 | for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) { |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1833 | CompileUnit *Unit = CompileUnits[i]; |
| 1834 | if (Unit->hasContent()) { |
| 1835 | // Compute size of compile unit header |
| 1836 | unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info |
| 1837 | sizeof(int16_t) + // DWARF version number |
| 1838 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 1839 | sizeof(int8_t); // Pointer Size (in bytes) |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 1840 | SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N); |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1841 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | /// EmitDebugInfo - Emit the debug info section. |
| 1846 | /// |
| 1847 | void DwarfWriter::EmitDebugInfo() const { |
| 1848 | // Start debug info section. |
| 1849 | Asm->SwitchSection(DwarfInfoSection, 0); |
| 1850 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1851 | // Process each compile unit. |
| 1852 | for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) { |
| 1853 | CompileUnit *Unit = CompileUnits[i]; |
| 1854 | |
| 1855 | if (Unit->hasContent()) { |
| 1856 | DIE *Die = Unit->getDie(); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1857 | // Emit the compile units header. |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1858 | EmitLabel("info_begin", Unit->getID()); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1859 | // Emit size of content not including length itself |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1860 | unsigned ContentSize = Die->getSize() + |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1861 | sizeof(int16_t) + // DWARF version number |
| 1862 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 1863 | sizeof(int8_t); // Pointer Size (in bytes) |
| 1864 | |
| 1865 | EmitInt32(ContentSize); EOL("Length of Compilation Unit Info"); |
| 1866 | EmitInt16(DWARF_VERSION); EOL("DWARF version number"); |
| 1867 | EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section"); |
| 1868 | EmitInt8(AddressSize); EOL("Address Size (in bytes)"); |
| 1869 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 1870 | EmitDIE(Die); |
| 1871 | EmitLabel("info_end", Unit->getID()); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1872 | } |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1873 | |
| 1874 | O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | /// EmitAbbreviations - Emit the abbreviation section. |
| 1879 | /// |
| 1880 | void DwarfWriter::EmitAbbreviations() const { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1881 | // Check to see if it is worth the effort. |
| 1882 | if (!Abbreviations.empty()) { |
| 1883 | // Start the debug abbrev section. |
| 1884 | Asm->SwitchSection(DwarfAbbrevSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1885 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1886 | EmitLabel("abbrev_begin", 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1887 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1888 | // For each abbrevation. |
| 1889 | for (unsigned AbbrevID = 1, NAID = Abbreviations.size(); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1890 | AbbrevID <= NAID; ++AbbrevID) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1891 | // Get abbreviation data |
| 1892 | const DIEAbbrev &Abbrev = Abbreviations[AbbrevID]; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1893 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1894 | // Emit the abbrevations code (base 1 index.) |
| 1895 | EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1896 | |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1897 | // Emit the abbreviations data. |
| 1898 | Abbrev.Emit(*this); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1899 | |
| 1900 | O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1901 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 1902 | |
| 1903 | EmitLabel("abbrev_end", 0); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 1904 | |
| 1905 | O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | /// EmitDebugLines - Emit source line information. |
| 1910 | /// |
| 1911 | void DwarfWriter::EmitDebugLines() const { |
| 1912 | // Minimum line delta, thus ranging from -10..(255-10). |
| 1913 | const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1); |
| 1914 | // Maximum line delta, thus ranging from -10..(255-10). |
| 1915 | const int MaxLineDelta = 255 + MinLineDelta; |
| 1916 | |
| 1917 | // Start the dwarf line section. |
| 1918 | Asm->SwitchSection(DwarfLineSection, 0); |
| 1919 | |
| 1920 | // Construct the section header. |
| 1921 | |
| 1922 | EmitDifference("line_end", 0, "line_begin", 0); |
| 1923 | EOL("Length of Source Line Info"); |
| 1924 | EmitLabel("line_begin", 0); |
| 1925 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1926 | EmitInt16(DWARF_VERSION); EOL("DWARF version number"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1927 | |
| 1928 | EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0); |
| 1929 | EOL("Prolog Length"); |
| 1930 | EmitLabel("line_prolog_begin", 0); |
| 1931 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1932 | EmitInt8(1); EOL("Minimum Instruction Length"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1933 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1934 | EmitInt8(1); EOL("Default is_stmt_start flag"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1935 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1936 | EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1937 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1938 | EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1939 | |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1940 | EmitInt8(-MinLineDelta); EOL("Special Opcode Base"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Line number standard opcode encodings argument count |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1943 | EmitInt8(0); EOL("DW_LNS_copy arg count"); |
| 1944 | EmitInt8(1); EOL("DW_LNS_advance_pc arg count"); |
| 1945 | EmitInt8(1); EOL("DW_LNS_advance_line arg count"); |
| 1946 | EmitInt8(1); EOL("DW_LNS_set_file arg count"); |
| 1947 | EmitInt8(1); EOL("DW_LNS_set_column arg count"); |
| 1948 | EmitInt8(0); EOL("DW_LNS_negate_stmt arg count"); |
| 1949 | EmitInt8(0); EOL("DW_LNS_set_basic_block arg count"); |
| 1950 | EmitInt8(0); EOL("DW_LNS_const_add_pc arg count"); |
| 1951 | EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1952 | |
| 1953 | const UniqueVector<std::string> &Directories = DebugInfo->getDirectories(); |
| 1954 | const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles(); |
| 1955 | |
| 1956 | // Emit directories. |
| 1957 | for (unsigned DirectoryID = 1, NDID = Directories.size(); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1958 | DirectoryID <= NDID; ++DirectoryID) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1959 | EmitString(Directories[DirectoryID]); EOL("Directory"); |
| 1960 | } |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1961 | EmitInt8(0); EOL("End of directories"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Emit files. |
| 1964 | for (unsigned SourceID = 1, NSID = SourceFiles.size(); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1965 | SourceID <= NSID; ++SourceID) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1966 | const SourceFileInfo &SourceFile = SourceFiles[SourceID]; |
| 1967 | EmitString(SourceFile.getName()); EOL("Source"); |
| 1968 | EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #"); |
| 1969 | EmitULEB128Bytes(0); EOL("Mod date"); |
| 1970 | EmitULEB128Bytes(0); EOL("File size"); |
| 1971 | } |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1972 | EmitInt8(0); EOL("End of files"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1973 | |
| 1974 | EmitLabel("line_prolog_end", 0); |
| 1975 | |
| 1976 | // Emit line information |
| 1977 | const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines(); |
| 1978 | |
| 1979 | // Dwarf assumes we start with first line of first source file. |
| 1980 | unsigned Source = 1; |
| 1981 | unsigned Line = 1; |
| 1982 | |
| 1983 | // Construct rows of the address, source, line, column matrix. |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 1984 | for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) { |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1985 | SourceLineInfo *LineInfo = LineInfos[i]; |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 1986 | |
| 1987 | if (DwarfVerbose) { |
| 1988 | unsigned SourceID = LineInfo->getSourceID(); |
| 1989 | const SourceFileInfo &SourceFile = SourceFiles[SourceID]; |
| 1990 | unsigned DirectoryID = SourceFile.getDirectoryID(); |
| 1991 | O << "\t" |
| 1992 | << Asm->CommentString << " " |
| 1993 | << Directories[DirectoryID] |
| 1994 | << SourceFile.getName() << ":" |
| 1995 | << LineInfo->getLine() << "\n"; |
| 1996 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 1997 | |
| 1998 | // Define the line address. |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 1999 | EmitInt8(0); EOL("Extended Op"); |
| 2000 | EmitInt8(4 + 1); EOL("Op size"); |
| 2001 | EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address"); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2002 | EmitReference("loc", LineInfo->getLabelID()); EOL("Location label"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2003 | |
| 2004 | // If change of source, then switch to the new source. |
| 2005 | if (Source != LineInfo->getSourceID()) { |
| 2006 | Source = LineInfo->getSourceID(); |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2007 | EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file"); |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2008 | EmitULEB128Bytes(Source); EOL("New Source"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | // If change of line. |
| 2012 | if (Line != LineInfo->getLine()) { |
| 2013 | // Determine offset. |
| 2014 | int Offset = LineInfo->getLine() - Line; |
| 2015 | int Delta = Offset - MinLineDelta; |
| 2016 | |
| 2017 | // Update line. |
| 2018 | Line = LineInfo->getLine(); |
| 2019 | |
| 2020 | // If delta is small enough and in range... |
| 2021 | if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { |
| 2022 | // ... then use fast opcode. |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2023 | EmitInt8(Delta - MinLineDelta); EOL("Line Delta"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2024 | } else { |
| 2025 | // ... otherwise use long hand. |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2026 | EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2027 | EmitSLEB128Bytes(Offset); EOL("Line Offset"); |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2028 | EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2029 | } |
| 2030 | } else { |
| 2031 | // Copy the previous row (different address or source) |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2032 | EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2036 | // Define last address. |
| 2037 | EmitInt8(0); EOL("Extended Op"); |
| 2038 | EmitInt8(4 + 1); EOL("Op size"); |
| 2039 | EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address"); |
| 2040 | EmitReference("text_end", 0); EOL("Location label"); |
| 2041 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2042 | // Mark end of matrix. |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2043 | EmitInt8(0); EOL("DW_LNE_end_sequence"); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2044 | EmitULEB128Bytes(1); O << "\n"; |
Jim Laskey | da427fa | 2006-01-27 20:31:25 +0000 | [diff] [blame] | 2045 | EmitInt8(1); O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2046 | |
| 2047 | EmitLabel("line_end", 0); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 2048 | |
| 2049 | O << "\n"; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2050 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2051 | |
| 2052 | /// EmitDebugFrame - Emit visible names into a debug frame section. |
| 2053 | /// |
| 2054 | void DwarfWriter::EmitDebugFrame() { |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2055 | // Start the dwarf pubnames section. |
| 2056 | Asm->SwitchSection(DwarfFrameSection, 0); |
| 2057 | |
| 2058 | EmitDifference("frame_common_end", 0, |
| 2059 | "frame_common_begin", 0); |
| 2060 | EOL("Length of Common Information Entry"); |
| 2061 | |
| 2062 | EmitLabel("frame_common_begin", 0); |
| 2063 | EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag"); |
| 2064 | EmitInt8(DW_CIE_VERSION); EOL("CIE Version"); |
| 2065 | EmitString(""); EOL("CIE Augmentation"); |
| 2066 | EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor"); |
| 2067 | // FIXME - needs to change based on stack direction. |
| 2068 | EmitSLEB128Bytes(-sizeof(int32_t)); EOL("CIE Data Alignment Factor"); |
| 2069 | // FIXME - hard coded for PPC (LR). |
| 2070 | EmitInt8(0x41); EOL("CIE RA Column Hardcoded (PPC LR)"); |
| 2071 | // FIXME - hard coded for PPC 0(SP). |
| 2072 | EmitULEB128Bytes(DW_CFA_def_cfa); EOL("DW_CFA_def_cfa"); |
| 2073 | EmitULEB128Bytes(1); EOL("PPC Register SP"); |
| 2074 | EmitULEB128Bytes(0); EOL("PPC offset 0 as in 0(SP)"); |
| 2075 | EmitAlign(2); |
| 2076 | EmitLabel("frame_common_end", 0); |
| 2077 | |
| 2078 | O << "\n"; |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | /// EmitDebugPubNames - Emit visible names into a debug pubnames section. |
| 2082 | /// |
| 2083 | void DwarfWriter::EmitDebugPubNames() { |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2084 | // Start the dwarf pubnames section. |
| 2085 | Asm->SwitchSection(DwarfPubNamesSection, 0); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2086 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2087 | // Process each compile unit. |
| 2088 | for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) { |
| 2089 | CompileUnit *Unit = CompileUnits[i]; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2090 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2091 | if (Unit->hasContent()) { |
| 2092 | EmitDifference("pubnames_end", Unit->getID(), |
| 2093 | "pubnames_begin", Unit->getID()); |
| 2094 | EOL("Length of Public Names Info"); |
| 2095 | |
| 2096 | EmitLabel("pubnames_begin", Unit->getID()); |
| 2097 | |
| 2098 | EmitInt16(DWARF_VERSION); EOL("DWARF Version"); |
| 2099 | |
| 2100 | EmitReference("info_begin", Unit->getID()); |
| 2101 | EOL("Offset of Compilation Unit Info"); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2102 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2103 | EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID()); |
| 2104 | EOL("Compilation Unit Length"); |
| 2105 | |
| 2106 | std::map<std::string, DIE *> &Globals = Unit->getGlobals(); |
| 2107 | |
| 2108 | for (std::map<std::string, DIE *>::iterator GI = Globals.begin(), |
| 2109 | GE = Globals.end(); |
| 2110 | GI != GE; ++GI) { |
| 2111 | const std::string &Name = GI->first; |
| 2112 | DIE * Entity = GI->second; |
| 2113 | |
| 2114 | EmitInt32(Entity->getOffset()); EOL("DIE offset"); |
| 2115 | EmitString(Name); EOL("External Name"); |
| 2116 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2117 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2118 | EmitInt32(0); EOL("End Mark"); |
| 2119 | EmitLabel("pubnames_end", Unit->getID()); |
| 2120 | |
| 2121 | O << "\n"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2122 | } |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2123 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | /// EmitDebugStr - Emit visible names into a debug str section. |
| 2127 | /// |
| 2128 | void DwarfWriter::EmitDebugStr() { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2129 | // Check to see if it is worth the effort. |
| 2130 | if (!StringPool.empty()) { |
| 2131 | // Start the dwarf str section. |
| 2132 | Asm->SwitchSection(DwarfStrSection, 0); |
| 2133 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2134 | // For each of strings in the string pool. |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2135 | for (unsigned StringID = 1, N = StringPool.size(); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2136 | StringID <= N; ++StringID) { |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2137 | // Emit a label for reference from debug information entries. |
| 2138 | EmitLabel("string", StringID); |
| 2139 | // Emit the string itself. |
| 2140 | const std::string &String = StringPool[StringID]; |
| 2141 | EmitString(String); O << "\n"; |
| 2142 | } |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 2143 | |
| 2144 | O << "\n"; |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2145 | } |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /// EmitDebugLoc - Emit visible names into a debug loc section. |
| 2149 | /// |
| 2150 | void DwarfWriter::EmitDebugLoc() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2151 | // Start the dwarf loc section. |
| 2152 | Asm->SwitchSection(DwarfLocSection, 0); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 2153 | |
| 2154 | O << "\n"; |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /// EmitDebugARanges - Emit visible names into a debug aranges section. |
| 2158 | /// |
| 2159 | void DwarfWriter::EmitDebugARanges() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2160 | // Start the dwarf aranges section. |
| 2161 | Asm->SwitchSection(DwarfARangesSection, 0); |
| 2162 | |
| 2163 | // FIXME - Mock up |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2164 | #if 0 |
| 2165 | // Process each compile unit. |
| 2166 | for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) { |
| 2167 | CompileUnit *Unit = CompileUnits[i]; |
| 2168 | |
| 2169 | if (Unit->hasContent()) { |
| 2170 | // Don't include size of length |
| 2171 | EmitInt32(0x1c); EOL("Length of Address Ranges Info"); |
| 2172 | |
| 2173 | EmitInt16(DWARF_VERSION); EOL("Dwarf Version"); |
| 2174 | |
| 2175 | EmitReference("info_begin", Unit->getID()); |
| 2176 | EOL("Offset of Compilation Unit Info"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2177 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2178 | EmitInt8(AddressSize); EOL("Size of Address"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2179 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2180 | EmitInt8(0); EOL("Size of Segment Descriptor"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2181 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2182 | EmitInt16(0); EOL("Pad (1)"); |
| 2183 | EmitInt16(0); EOL("Pad (2)"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2184 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2185 | // Range 1 |
| 2186 | EmitReference("text_begin", 0); EOL("Address"); |
| 2187 | EmitDifference("text_end", 0, "text_begin", 0); EOL("Length"); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2188 | |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2189 | EmitInt32(0); EOL("EOM (1)"); |
| 2190 | EmitInt32(0); EOL("EOM (2)"); |
| 2191 | |
| 2192 | O << "\n"; |
| 2193 | } |
| 2194 | } |
| 2195 | #endif |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /// EmitDebugRanges - Emit visible names into a debug ranges section. |
| 2199 | /// |
| 2200 | void DwarfWriter::EmitDebugRanges() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2201 | // Start the dwarf ranges section. |
| 2202 | Asm->SwitchSection(DwarfRangesSection, 0); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 2203 | |
| 2204 | O << "\n"; |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /// EmitDebugMacInfo - Emit visible names into a debug macinfo section. |
| 2208 | /// |
| 2209 | void DwarfWriter::EmitDebugMacInfo() { |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2210 | // Start the dwarf macinfo section. |
| 2211 | Asm->SwitchSection(DwarfMacInfoSection, 0); |
Jim Laskey | 0d086af | 2006-02-27 12:43:29 +0000 | [diff] [blame] | 2212 | |
| 2213 | O << "\n"; |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2214 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2215 | |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2216 | /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and |
| 2217 | /// header file. |
| 2218 | void DwarfWriter::ConstructCompileUnitDIEs() { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 2219 | const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits(); |
Jim Laskey | 6e87c0e | 2006-01-26 21:22:49 +0000 | [diff] [blame] | 2220 | |
| 2221 | for (unsigned i = 1, N = CUW.size(); i <= N; ++i) { |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2222 | CompileUnit *Unit = NewCompileUnit(CUW[i], i); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2223 | CompileUnits.push_back(Unit); |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global |
| 2228 | /// variables. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2229 | void DwarfWriter::ConstructGlobalDIEs() { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 2230 | std::vector<GlobalVariableDesc *> GlobalVariables = |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2231 | DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 2232 | |
| 2233 | for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) { |
Jim Laskey | 86cbdba | 2006-02-06 15:33:21 +0000 | [diff] [blame] | 2234 | GlobalVariableDesc *GVD = GlobalVariables[i]; |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2235 | NewGlobalVariable(GVD); |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2239 | /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible |
| 2240 | /// subprograms. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2241 | void DwarfWriter::ConstructSubprogramDIEs() { |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2242 | std::vector<SubprogramDesc *> Subprograms = |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2243 | DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M); |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2244 | |
| 2245 | for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) { |
| 2246 | SubprogramDesc *SPD = Subprograms[i]; |
| 2247 | NewSubprogram(SPD); |
| 2248 | } |
| 2249 | } |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2250 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2251 | /// ShouldEmitDwarf - Determine if Dwarf declarations should be made. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2252 | /// |
| 2253 | bool DwarfWriter::ShouldEmitDwarf() { |
| 2254 | // Check if debug info is present. |
| 2255 | if (!DebugInfo || !DebugInfo->hasInfo()) return false; |
| 2256 | |
| 2257 | // Make sure initial declarations are made. |
| 2258 | if (!didInitial) { |
| 2259 | EmitInitial(); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2260 | |
| 2261 | // Create all the compile unit DIEs. |
| 2262 | ConstructCompileUnitDIEs(); |
| 2263 | |
| 2264 | // Create DIEs for each of the externally visible global variables. |
| 2265 | ConstructGlobalDIEs(); |
| 2266 | |
| 2267 | // Create DIEs for each of the externally visible subprograms. |
| 2268 | ConstructSubprogramDIEs(); |
| 2269 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2270 | didInitial = true; |
| 2271 | } |
| 2272 | |
| 2273 | // Okay to emit. |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2277 | //===----------------------------------------------------------------------===// |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2278 | // Main entry points. |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2279 | // |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2280 | |
| 2281 | DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A) |
| 2282 | : O(OS) |
| 2283 | , Asm(A) |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2284 | , M(NULL) |
| 2285 | , MF(NULL) |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2286 | , DebugInfo(NULL) |
| 2287 | , didInitial(false) |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2288 | , SubprogramCount(0) |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2289 | , CompileUnits() |
| 2290 | , Abbreviations() |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2291 | , StringPool() |
Jim Laskey | bd76184 | 2006-02-27 17:27:12 +0000 | [diff] [blame] | 2292 | , DescToUnitMap() |
Jim Laskey | 0420f2a | 2006-02-22 19:02:11 +0000 | [diff] [blame] | 2293 | , DescToDieMap() |
| 2294 | , TypeToDieMap() |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2295 | , AddressSize(sizeof(int32_t)) |
| 2296 | , hasLEB128(false) |
| 2297 | , hasDotLoc(false) |
| 2298 | , hasDotFile(false) |
| 2299 | , needsSet(false) |
| 2300 | , DwarfAbbrevSection(".debug_abbrev") |
| 2301 | , DwarfInfoSection(".debug_info") |
| 2302 | , DwarfLineSection(".debug_line") |
| 2303 | , DwarfFrameSection(".debug_frame") |
| 2304 | , DwarfPubNamesSection(".debug_pubnames") |
| 2305 | , DwarfPubTypesSection(".debug_pubtypes") |
| 2306 | , DwarfStrSection(".debug_str") |
| 2307 | , DwarfLocSection(".debug_loc") |
| 2308 | , DwarfARangesSection(".debug_aranges") |
| 2309 | , DwarfRangesSection(".debug_ranges") |
| 2310 | , DwarfMacInfoSection(".debug_macinfo") |
| 2311 | , TextSection(".text") |
| 2312 | , DataSection(".data") |
| 2313 | {} |
| 2314 | DwarfWriter::~DwarfWriter() { |
| 2315 | for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) { |
| 2316 | delete CompileUnits[i]; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2317 | } |
Jim Laskey | 52060a0 | 2006-01-24 00:49:18 +0000 | [diff] [blame] | 2318 | } |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2319 | |
| 2320 | /// BeginModule - Emit all Dwarf sections that should come prior to the content. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2321 | /// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2322 | void DwarfWriter::BeginModule(Module *M) { |
| 2323 | this->M = M; |
| 2324 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2325 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2326 | EOL("Dwarf Begin Module"); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2329 | /// EndModule - Emit all Dwarf sections that should come after the content. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2330 | /// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2331 | void DwarfWriter::EndModule() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2332 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2333 | EOL("Dwarf End Module"); |
| 2334 | |
| 2335 | // Standard sections final addresses. |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2336 | Asm->SwitchSection(TextSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2337 | EmitLabel("text_end", 0); |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2338 | Asm->SwitchSection(DataSection, 0); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2339 | EmitLabel("data_end", 0); |
Jim Laskey | d18e289 | 2006-01-20 20:34:06 +0000 | [diff] [blame] | 2340 | |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2341 | // Compute DIE offsets and sizes. |
| 2342 | SizeAndOffsets(); |
| 2343 | |
| 2344 | // Emit all the DIEs into a debug info section |
| 2345 | EmitDebugInfo(); |
| 2346 | |
| 2347 | // Corresponding abbreviations into a abbrev section. |
| 2348 | EmitAbbreviations(); |
| 2349 | |
| 2350 | // Emit source line correspondence into a debug line section. |
| 2351 | EmitDebugLines(); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2352 | |
| 2353 | // Emit info into a debug frame section. |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2354 | EmitDebugFrame(); |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2355 | |
| 2356 | // Emit info into a debug pubnames section. |
| 2357 | EmitDebugPubNames(); |
| 2358 | |
Jim Laskey | 19ef4ef | 2006-01-17 20:41:40 +0000 | [diff] [blame] | 2359 | // Emit info into a debug str section. |
| 2360 | EmitDebugStr(); |
| 2361 | |
| 2362 | // Emit info into a debug loc section. |
| 2363 | EmitDebugLoc(); |
| 2364 | |
| 2365 | // Emit info into a debug aranges section. |
| 2366 | EmitDebugARanges(); |
| 2367 | |
| 2368 | // Emit info into a debug ranges section. |
| 2369 | EmitDebugRanges(); |
| 2370 | |
| 2371 | // Emit info into a debug macinfo section. |
| 2372 | EmitDebugMacInfo(); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2375 | /// BeginFunction - Gather pre-function debug information. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2376 | /// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2377 | void DwarfWriter::BeginFunction(MachineFunction *MF) { |
| 2378 | this->MF = MF; |
| 2379 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2380 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2381 | EOL("Dwarf Begin Function"); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2382 | |
| 2383 | // Define begin label for subprogram. |
| 2384 | Asm->SwitchSection(TextSection, 0); |
| 2385 | EmitLabel("func_begin", ++SubprogramCount); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2388 | |
Jim Laskey | e719a7c | 2006-01-18 16:54:26 +0000 | [diff] [blame] | 2389 | /// EndFunction - Gather and emit post-function debug information. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2390 | /// |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2391 | void DwarfWriter::EndFunction() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2392 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 2393 | EOL("Dwarf End Function"); |
Jim Laskey | b8509c5 | 2006-03-23 18:07:55 +0000 | [diff] [blame^] | 2394 | |
| 2395 | // Define end label for subprogram. |
| 2396 | Asm->SwitchSection(TextSection, 0); |
| 2397 | EmitLabel("func_end", SubprogramCount); |
| 2398 | |
| 2399 | // Construct scopes for subprogram. |
| 2400 | ConstructRootScope(DebugInfo->getRootScope()); |
| 2401 | DebugInfo->ClearScopes(); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 2402 | } |