]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/DWARFDebugLine.h
Import libc++ trunk r165949. Among other improvements and bug fixes,
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / DWARFDebugLine.h
1 //===-- DWARFDebugLine.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_DEBUGINFO_DWARFDEBUGLINE_H
11 #define LLVM_DEBUGINFO_DWARFDEBUGLINE_H
12
13 #include "llvm/Support/DataExtractor.h"
14 #include <map>
15 #include <vector>
16
17 namespace llvm {
18
19 class raw_ostream;
20
21 class DWARFDebugLine {
22 public:
23   struct FileNameEntry {
24     FileNameEntry() : Name(0), DirIdx(0), ModTime(0), Length(0) {}
25
26     const char *Name;
27     uint64_t DirIdx;
28     uint64_t ModTime;
29     uint64_t Length;
30   };
31
32   struct Prologue {
33     Prologue()
34       : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
35         DefaultIsStmt(0), LineBase(0), LineRange(0), OpcodeBase(0) {}
36
37     // The size in bytes of the statement information for this compilation unit
38     // (not including the total_length field itself).
39     uint32_t TotalLength;
40     // Version identifier for the statement information format.
41     uint16_t Version;
42     // The number of bytes following the prologue_length field to the beginning
43     // of the first byte of the statement program itself.
44     uint32_t PrologueLength;
45     // The size in bytes of the smallest target machine instruction. Statement
46     // program opcodes that alter the address register first multiply their
47     // operands by this value.
48     uint8_t MinInstLength;
49     // The initial value of theis_stmtregister.
50     uint8_t DefaultIsStmt;
51     // This parameter affects the meaning of the special opcodes. See below.
52     int8_t LineBase;
53     // This parameter affects the meaning of the special opcodes. See below.
54     uint8_t LineRange;
55     // The number assigned to the first special opcode.
56     uint8_t OpcodeBase;
57     std::vector<uint8_t> StandardOpcodeLengths;
58     std::vector<const char*> IncludeDirectories;
59     std::vector<FileNameEntry> FileNames;
60
61     // Length of the prologue in bytes.
62     uint32_t getLength() const {
63       return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
64              sizeof(PrologueLength);
65     }
66     // Length of the line table data in bytes (not including the prologue).
67     uint32_t getStatementTableLength() const {
68       return TotalLength + sizeof(TotalLength) - getLength();
69     }
70     int32_t getMaxLineIncrementForSpecialOpcode() const {
71       return LineBase + (int8_t)LineRange - 1;
72     }
73     void dump(raw_ostream &OS) const;
74     void clear() {
75       TotalLength = Version = PrologueLength = 0;
76       MinInstLength = LineBase = LineRange = OpcodeBase = 0;
77       StandardOpcodeLengths.clear();
78       IncludeDirectories.clear();
79       FileNames.clear();
80     }
81   };
82
83   // Standard .debug_line state machine structure.
84   struct Row {
85     Row(bool default_is_stmt = false) { reset(default_is_stmt); }
86     /// Called after a row is appended to the matrix.
87     void postAppend();
88     void reset(bool default_is_stmt);
89     void dump(raw_ostream &OS) const;
90
91     static bool orderByAddress(const Row& LHS, const Row& RHS) {
92       return LHS.Address < RHS.Address;
93     }
94
95     // The program-counter value corresponding to a machine instruction
96     // generated by the compiler.
97     uint64_t Address;
98     // An unsigned integer indicating a source line number. Lines are numbered
99     // beginning at 1. The compiler may emit the value 0 in cases where an
100     // instruction cannot be attributed to any source line.
101     uint32_t Line;
102     // An unsigned integer indicating a column number within a source line.
103     // Columns are numbered beginning at 1. The value 0 is reserved to indicate
104     // that a statement begins at the 'left edge' of the line.
105     uint16_t Column;
106     // An unsigned integer indicating the identity of the source file
107     // corresponding to a machine instruction.
108     uint16_t File;
109     // An unsigned integer whose value encodes the applicable instruction set
110     // architecture for the current instruction.
111     uint8_t Isa;
112     // A boolean indicating that the current instruction is the beginning of a
113     // statement.
114     uint8_t IsStmt:1,
115             // A boolean indicating that the current instruction is the
116             // beginning of a basic block.
117             BasicBlock:1,
118             // A boolean indicating that the current address is that of the
119             // first byte after the end of a sequence of target machine
120             // instructions.
121             EndSequence:1,
122             // A boolean indicating that the current address is one (of possibly
123             // many) where execution should be suspended for an entry breakpoint
124             // of a function.
125             PrologueEnd:1,
126             // A boolean indicating that the current address is one (of possibly
127             // many) where execution should be suspended for an exit breakpoint
128             // of a function.
129             EpilogueBegin:1;
130   };
131
132   // Represents a series of contiguous machine instructions. Line table for each
133   // compilation unit may consist of multiple sequences, which are not
134   // guaranteed to be in the order of ascending instruction address.
135   struct Sequence {
136     // Sequence describes instructions at address range [LowPC, HighPC)
137     // and is described by line table rows [FirstRowIndex, LastRowIndex).
138     uint64_t LowPC;
139     uint64_t HighPC;
140     unsigned FirstRowIndex;
141     unsigned LastRowIndex;
142     bool Empty;
143
144     Sequence() { reset(); }
145     void reset() {
146       LowPC = 0;
147       HighPC = 0;
148       FirstRowIndex = 0;
149       LastRowIndex = 0;
150       Empty = true;
151     }
152     static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
153       return LHS.LowPC < RHS.LowPC;
154     }
155     bool isValid() const {
156       return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
157     }
158     bool containsPC(uint64_t pc) const {
159       return (LowPC <= pc && pc < HighPC);
160     }
161   };
162
163   struct LineTable {
164     void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
165     void appendSequence(const DWARFDebugLine::Sequence &sequence) {
166       Sequences.push_back(sequence);
167     }
168     void clear() {
169       Prologue.clear();
170       Rows.clear();
171       Sequences.clear();
172     }
173
174     // Returns the index of the row with file/line info for a given address,
175     // or -1 if there is no such row.
176     uint32_t lookupAddress(uint64_t address) const;
177     void dump(raw_ostream &OS) const;
178
179     struct Prologue Prologue;
180     typedef std::vector<Row> RowVector;
181     typedef RowVector::const_iterator RowIter;
182     typedef std::vector<Sequence> SequenceVector;
183     typedef SequenceVector::const_iterator SequenceIter;
184     RowVector Rows;
185     SequenceVector Sequences;
186   };
187
188   struct State : public Row, public Sequence, public LineTable {
189     // Special row codes.
190     enum {
191       StartParsingLineTable = 0,
192       DoneParsingLineTable = -1
193     };
194
195     State() : row(StartParsingLineTable) {}
196     virtual ~State();
197
198     virtual void appendRowToMatrix(uint32_t offset);
199     virtual void finalize();
200     virtual void reset() {
201       Row::reset(Prologue.DefaultIsStmt);
202       Sequence::reset();
203     }
204
205     // The row number that starts at zero for the prologue, and increases for
206     // each row added to the matrix.
207     unsigned row;
208   };
209
210   struct DumpingState : public State {
211     DumpingState(raw_ostream &OS) : OS(OS) {}
212     virtual ~DumpingState();
213     virtual void finalize();
214   private:
215     raw_ostream &OS;
216   };
217
218   static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
219                             Prologue *prologue);
220   /// Parse a single line table (prologue and all rows).
221   static bool parseStatementTable(DataExtractor debug_line_data,
222                                   uint32_t *offset_ptr, State &state);
223
224   const LineTable *getLineTable(uint32_t offset) const;
225   const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
226                                        uint32_t offset);
227
228 private:
229   typedef std::map<uint32_t, LineTable> LineTableMapTy;
230   typedef LineTableMapTy::iterator LineTableIter;
231   typedef LineTableMapTy::const_iterator LineTableConstIter;
232
233   LineTableMapTy LineTableMap;
234 };
235
236 }
237
238 #endif