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 <string>
16#include <vector>
17
18namespace llvm {
19
20class raw_ostream;
21
22class DWARFDebugLine {
23public:
24  struct FileNameEntry {
25    FileNameEntry() : DirIdx(0), ModTime(0), Length(0) {}
26
27    std::string Name;
28    uint64_t DirIdx;
29    uint64_t ModTime;
30    uint64_t Length;
31  };
32
33  struct Prologue {
34    Prologue()
35      : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
36        DefaultIsStmt(0), LineBase(0), LineRange(0), OpcodeBase(0) {}
37
38    // The size in bytes of the statement information for this compilation unit
39    // (not including the total_length field itself).
40    uint32_t TotalLength;
41    // Version identifier for the statement information format.
42    uint16_t Version;
43    // The number of bytes following the prologue_length field to the beginning
44    // of the first byte of the statement program itself.
45    uint32_t PrologueLength;
46    // The size in bytes of the smallest target machine instruction. Statement
47    // program opcodes that alter the address register first multiply their
48    // operands by this value.
49    uint8_t MinInstLength;
50    // The initial value of theis_stmtregister.
51    uint8_t DefaultIsStmt;
52    // This parameter affects the meaning of the special opcodes. See below.
53    int8_t LineBase;
54    // This parameter affects the meaning of the special opcodes. See below.
55    uint8_t LineRange;
56    // The number assigned to the first special opcode.
57    uint8_t OpcodeBase;
58    std::vector<uint8_t> StandardOpcodeLengths;
59    std::vector<std::string> IncludeDirectories;
60    std::vector<FileNameEntry> FileNames;
61
62    // Length of the prologue in bytes.
63    uint32_t getLength() const {
64      return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
65             sizeof(PrologueLength);
66    }
67    // Length of the line table data in bytes (not including the prologue).
68    uint32_t getStatementTableLength() const {
69      return TotalLength + sizeof(TotalLength) - getLength();
70    }
71    int32_t getMaxLineIncrementForSpecialOpcode() const {
72      return LineBase + (int8_t)LineRange - 1;
73    }
74    void dump(raw_ostream &OS) const;
75    void clear() {
76      TotalLength = Version = PrologueLength = 0;
77      MinInstLength = LineBase = LineRange = OpcodeBase = 0;
78      StandardOpcodeLengths.clear();
79      IncludeDirectories.clear();
80      FileNames.clear();
81    }
82  };
83
84  // Standard .debug_line state machine structure.
85  struct Row {
86    Row(bool default_is_stmt = false) { reset(default_is_stmt); }
87    /// Called after a row is appended to the matrix.
88    void postAppend();
89    void reset(bool default_is_stmt);
90    void dump(raw_ostream &OS) const;
91
92    // The program-counter value corresponding to a machine instruction
93    // generated by the compiler.
94    uint64_t Address;
95    // An unsigned integer indicating a source line number. Lines are numbered
96    // beginning at 1. The compiler may emit the value 0 in cases where an
97    // instruction cannot be attributed to any source line.
98    uint32_t Line;
99    // An unsigned integer indicating a column number within a source line.
100    // Columns are numbered beginning at 1. The value 0 is reserved to indicate
101    // that a statement begins at the 'left edge' of the line.
102    uint16_t Column;
103    // An unsigned integer indicating the identity of the source file
104    // corresponding to a machine instruction.
105    uint16_t File;
106    // An unsigned integer whose value encodes the applicable instruction set
107    // architecture for the current instruction.
108    uint8_t Isa;
109    // A boolean indicating that the current instruction is the beginning of a
110    // statement.
111    uint8_t IsStmt:1,
112            // A boolean indicating that the current instruction is the
113            // beginning of a basic block.
114            BasicBlock:1,
115            // A boolean indicating that the current address is that of the
116            // first byte after the end of a sequence of target machine
117            // instructions.
118            EndSequence:1,
119            // A boolean indicating that the current address is one (of possibly
120            // many) where execution should be suspended for an entry breakpoint
121            // of a function.
122            PrologueEnd:1,
123            // A boolean indicating that the current address is one (of possibly
124            // many) where execution should be suspended for an exit breakpoint
125            // of a function.
126            EpilogueBegin:1;
127  };
128
129  struct LineTable {
130    void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
131    void clear() {
132      Prologue.clear();
133      Rows.clear();
134    }
135
136    uint32_t lookupAddress(uint64_t address, uint64_t cu_high_pc) const;
137    void dump(raw_ostream &OS) const;
138
139    struct Prologue Prologue;
140    std::vector<Row> Rows;
141  };
142
143  struct State : public Row, public LineTable {
144    // Special row codes.
145    enum {
146      StartParsingLineTable = 0,
147      DoneParsingLineTable = -1
148    };
149
150    State() : row(StartParsingLineTable) {}
151    virtual ~State();
152
153    virtual void appendRowToMatrix(uint32_t offset);
154    virtual void finalize(uint32_t offset) { row = DoneParsingLineTable; }
155    virtual void reset() { Row::reset(Prologue.DefaultIsStmt); }
156
157    // The row number that starts at zero for the prologue, and increases for
158    // each row added to the matrix.
159    unsigned row;
160  };
161
162  struct DumpingState : public State {
163    DumpingState(raw_ostream &OS) : OS(OS) {}
164    virtual ~DumpingState();
165    virtual void finalize(uint32_t offset);
166  private:
167    raw_ostream &OS;
168  };
169
170  static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
171                            Prologue *prologue);
172  /// Parse a single line table (prologue and all rows).
173  static bool parseStatementTable(DataExtractor debug_line_data,
174                                  uint32_t *offset_ptr, State &state);
175
176  const LineTable *getLineTable(uint32_t offset) const;
177  const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
178                                       uint32_t offset);
179
180private:
181  typedef std::map<uint32_t, LineTable> LineTableMapTy;
182  typedef LineTableMapTy::iterator LineTableIter;
183  typedef LineTableMapTy::const_iterator LineTableConstIter;
184
185  LineTableMapTy LineTableMap;
186};
187
188}
189
190#endif
191