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 | //===----------------------------------------------------------------------===// |
| 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 | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/AsmPrinter.h" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
| 19 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 20 | #include <iostream> |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 21 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 23 | |
| 24 | static cl::opt<bool> |
| 25 | DwarfVerbose("dwarf-verbose", cl::Hidden, |
| 26 | cl::desc("Add comments to dwarf directives.")); |
| 27 | |
| 28 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 29 | /// unsigned leb128 value. Comment is added to the end of the directive if |
| 30 | /// DwarfVerbose is true (should not contain any newlines.) |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 31 | /// |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 32 | void DwarfWriter::EmitULEB128Bytes(unsigned Value, const char *Comment) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 33 | if (hasLEB128) { |
| 34 | O << "\t.uleb128\t" |
| 35 | << Value; |
| 36 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 37 | O << Asm->Data8bitsDirective; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 38 | EmitULEB128(Value); |
| 39 | } |
| 40 | if (DwarfVerbose) { |
| 41 | O << "\t" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 42 | << Asm->CommentString |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 43 | << " " |
| 44 | << Comment |
| 45 | << " " |
| 46 | << Value; |
| 47 | } |
| 48 | O << "\n"; |
| 49 | } |
| 50 | |
| 51 | /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 52 | /// signed leb128 value. Comment is added to the end of the directive if |
| 53 | /// DwarfVerbose is true (should not contain any newlines.) |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 54 | /// |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 55 | void DwarfWriter::EmitSLEB128Bytes(int Value, const char *Comment) const { |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 56 | if (hasLEB128) { |
| 57 | O << "\t.sleb128\t" |
| 58 | << Value; |
| 59 | } else { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 60 | O << Asm->Data8bitsDirective; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 61 | EmitSLEB128(Value); |
| 62 | } |
| 63 | if (DwarfVerbose) { |
| 64 | O << "\t" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 65 | << Asm->CommentString |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 66 | << " " |
| 67 | << Comment |
| 68 | << " " |
| 69 | << Value; |
| 70 | } |
| 71 | O << "\n"; |
| 72 | } |
| 73 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 74 | /// EmitHex - Emit a hexidecimal string to the output stream. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 75 | /// |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 76 | void DwarfWriter::EmitHex(unsigned Value) const { |
| 77 | O << "0x" |
| 78 | << std::hex |
| 79 | << Value |
| 80 | << std::dec; |
| 81 | } |
| 82 | |
| 83 | /// EmitComment - Emit a simple string comment. |
| 84 | /// |
| 85 | void DwarfWriter::EmitComment(const char *Comment) const { |
| 86 | O << "\t" |
| 87 | << Asm->CommentString |
| 88 | << " " |
| 89 | << Comment |
| 90 | << "\n"; |
| 91 | } |
| 92 | |
| 93 | /// EmitULEB128 - Emit a series of hexidecimal values (separated by commas) |
| 94 | /// representing an unsigned leb128 value. |
| 95 | /// |
| 96 | void DwarfWriter::EmitULEB128(unsigned Value) const { |
| 97 | do { |
| 98 | unsigned Byte = Value & 0x7f; |
| 99 | Value >>= 7; |
| 100 | if (Value) Byte |= 0x80; |
| 101 | EmitHex(Byte); |
| 102 | if (Value) O << ", "; |
| 103 | } while (Value); |
| 104 | } |
| 105 | |
| 106 | /// EmitSLEB128 - Emit a series of hexidecimal values (separated by commas) |
| 107 | /// representing a signed leb128 value. |
| 108 | /// |
| 109 | void DwarfWriter::EmitSLEB128(int Value) const { |
| 110 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 111 | bool IsMore; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 112 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 113 | do { |
| 114 | unsigned Byte = Value & 0x7f; |
| 115 | Value >>= 7; |
| 116 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 117 | if (IsMore) Byte |= 0x80; |
| 118 | EmitHex(Byte); |
| 119 | if (IsMore) O << ", "; |
| 120 | } while (IsMore); |
| 121 | } |
| 122 | |
| 123 | /// EmitLabelName - Emit label name for internal use by dwarf. |
| 124 | /// |
| 125 | void DwarfWriter::EmitLabelName(const char *Tag, int Num) const { |
| 126 | O << Asm->PrivateGlobalPrefix |
| 127 | << "debug_" |
| 128 | << Tag |
| 129 | << Num; |
| 130 | } |
| 131 | |
| 132 | /// EmitLabel - Emit location label for internal use by dwarf. |
| 133 | /// |
| 134 | void DwarfWriter::EmitLabel(const char *Tag, int Num) const { |
| 135 | EmitLabelName(Tag, Num); |
| 136 | O << ":\n"; |
| 137 | } |
| 138 | |
| 139 | /// EmitInitial -Emit initial dwarf declarations. |
| 140 | /// |
| 141 | void DwarfWriter::EmitInitial() const { |
| 142 | // Dwarf section's base addresses. |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 143 | Asm->SwitchSection(DwarfAbbrevSection, 0); |
| 144 | EmitLabel("abbrev", 0); |
| 145 | Asm->SwitchSection(DwarfInfoSection, 0); |
| 146 | EmitLabel("info", 0); |
| 147 | Asm->SwitchSection(DwarfLineSection, 0); |
| 148 | EmitLabel("line", 0); |
| 149 | } |
| 150 | |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 151 | /// ShouldEmitDwarf - Determine if dwarf declarations should be made. |
| 152 | /// |
| 153 | bool DwarfWriter::ShouldEmitDwarf() { |
| 154 | // Check if debug info is present. |
| 155 | if (!DebugInfo || !DebugInfo->hasInfo()) return false; |
| 156 | |
| 157 | // Make sure initial declarations are made. |
| 158 | if (!didInitial) { |
| 159 | EmitInitial(); |
| 160 | didInitial = true; |
| 161 | } |
| 162 | |
| 163 | // Okay to emit. |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /// BeginModule - Emit all dwarf sections that should come prior to the content. |
| 168 | /// |
| 169 | void DwarfWriter::BeginModule() { |
| 170 | if (!ShouldEmitDwarf()) return; |
| 171 | EmitComment("Dwarf Begin Module"); |
| 172 | } |
| 173 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 174 | /// EndModule - Emit all dwarf sections that should come after the content. |
| 175 | /// |
| 176 | void DwarfWriter::EndModule() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 177 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 178 | EmitComment("Dwarf End Module"); |
| 179 | // Print out dwarf file info |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 180 | std::vector<std::string> Sources = DebugInfo->getSourceFiles(); |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 181 | for (unsigned i = 0, N = Sources.size(); i < N; i++) { |
| 182 | O << "\t; .file\t" << (i + 1) << "," << "\"" << Sources[i] << "\"" << "\n"; |
| 183 | } |
| 184 | } |
| 185 | |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 186 | /// BeginFunction - Emit pre-function debug information. |
| 187 | /// |
| 188 | void DwarfWriter::BeginFunction() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 189 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 190 | EmitComment("Dwarf Begin Function"); |
| 191 | } |
| 192 | |
| 193 | /// EndFunction - Emit post-function debug information. |
| 194 | /// |
| 195 | void DwarfWriter::EndFunction() { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 196 | if (!ShouldEmitDwarf()) return; |
Jim Laskey | a7cea6f | 2006-01-04 13:52:30 +0000 | [diff] [blame] | 197 | EmitComment("Dwarf End Function"); |
| 198 | } |