1254721Semaste//===-- LineTable.h ---------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_LineTable_h_
11254721Semaste#define liblldb_LineTable_h_
12254721Semaste
13254721Semaste#include <vector>
14254721Semaste
15254721Semaste#include "lldb/lldb-private.h"
16254721Semaste#include "lldb/Symbol/LineEntry.h"
17254721Semaste#include "lldb/Core/ModuleChild.h"
18254721Semaste#include "lldb/Core/Section.h"
19254721Semaste#include "lldb/Core/RangeMap.h"
20254721Semaste
21254721Semastenamespace lldb_private {
22254721Semaste
23254721Semaste//----------------------------------------------------------------------
24254721Semaste/// @class LineSequence LineTable.h "lldb/Symbol/LineTable.h"
25254721Semaste/// @brief An abstract base class used during symbol table creation.
26254721Semaste//----------------------------------------------------------------------
27254721Semasteclass LineSequence
28254721Semaste{
29254721Semastepublic:
30254721Semaste    LineSequence ();
31254721Semaste
32254721Semaste    virtual
33254721Semaste    ~LineSequence() {}
34254721Semaste
35254721Semaste    virtual void
36254721Semaste    Clear() = 0;
37254721Semaste
38254721Semasteprivate:
39254721Semaste    DISALLOW_COPY_AND_ASSIGN (LineSequence);
40254721Semaste};
41254721Semaste
42254721Semaste//----------------------------------------------------------------------
43254721Semaste/// @class LineTable LineTable.h "lldb/Symbol/LineTable.h"
44254721Semaste/// @brief A line table class.
45254721Semaste//----------------------------------------------------------------------
46254721Semasteclass LineTable
47254721Semaste{
48254721Semastepublic:
49254721Semaste    //------------------------------------------------------------------
50254721Semaste    /// Construct with compile unit.
51254721Semaste    ///
52254721Semaste    /// @param[in] comp_unit
53254721Semaste    ///     The compile unit to which this line table belongs.
54254721Semaste    //------------------------------------------------------------------
55254721Semaste    LineTable (CompileUnit* comp_unit);
56254721Semaste
57254721Semaste    //------------------------------------------------------------------
58254721Semaste    /// Destructor.
59254721Semaste    //------------------------------------------------------------------
60254721Semaste    ~LineTable ();
61254721Semaste
62254721Semaste    //------------------------------------------------------------------
63254721Semaste    /// Adds a new line entry to this line table.
64254721Semaste    ///
65254721Semaste    /// All line entries are maintained in file address order.
66254721Semaste    ///
67254721Semaste    /// @param[in] line_entry
68254721Semaste    ///     A const reference to a new line_entry to add to this line
69254721Semaste    ///     table.
70254721Semaste    ///
71254721Semaste    /// @see Address::DumpStyle
72254721Semaste    //------------------------------------------------------------------
73254721Semaste//  void
74254721Semaste//  AddLineEntry (const LineEntry& line_entry);
75254721Semaste
76254721Semaste    // Called when you can't guarantee the addresses are in increasing order
77254721Semaste    void
78254721Semaste    InsertLineEntry (lldb::addr_t file_addr,
79254721Semaste                     uint32_t line,
80254721Semaste                     uint16_t column,
81254721Semaste                     uint16_t file_idx,
82254721Semaste                     bool is_start_of_statement,
83254721Semaste                     bool is_start_of_basic_block,
84254721Semaste                     bool is_prologue_end,
85254721Semaste                     bool is_epilogue_begin,
86254721Semaste                     bool is_terminal_entry);
87254721Semaste
88254721Semaste    // Used to instantiate the LineSequence helper classw
89254721Semaste    LineSequence*
90254721Semaste    CreateLineSequenceContainer ();
91254721Semaste
92254721Semaste    // Append an entry to a caller-provided collection that will later be
93254721Semaste    // inserted in this line table.
94254721Semaste    void
95254721Semaste    AppendLineEntryToSequence (LineSequence* sequence,
96254721Semaste                               lldb::addr_t file_addr,
97254721Semaste                               uint32_t line,
98254721Semaste                               uint16_t column,
99254721Semaste                               uint16_t file_idx,
100254721Semaste                               bool is_start_of_statement,
101254721Semaste                               bool is_start_of_basic_block,
102254721Semaste                               bool is_prologue_end,
103254721Semaste                               bool is_epilogue_begin,
104254721Semaste                               bool is_terminal_entry);
105254721Semaste
106254721Semaste    // Insert a sequence of entries into this line table.
107254721Semaste    void
108254721Semaste    InsertSequence (LineSequence* sequence);
109254721Semaste
110254721Semaste    //------------------------------------------------------------------
111254721Semaste    /// Dump all line entries in this line table to the stream \a s.
112254721Semaste    ///
113254721Semaste    /// @param[in] s
114254721Semaste    ///     The stream to which to dump the object descripton.
115254721Semaste    ///
116254721Semaste    /// @param[in] style
117254721Semaste    ///     The display style for the address.
118254721Semaste    ///
119254721Semaste    /// @see Address::DumpStyle
120254721Semaste    //------------------------------------------------------------------
121254721Semaste    void
122254721Semaste    Dump (Stream *s, Target *target,
123254721Semaste          Address::DumpStyle style,
124254721Semaste          Address::DumpStyle fallback_style,
125254721Semaste          bool show_line_ranges);
126254721Semaste
127254721Semaste    void
128254721Semaste    GetDescription (Stream *s,
129254721Semaste                    Target *target,
130254721Semaste                    lldb::DescriptionLevel level);
131254721Semaste
132254721Semaste    //------------------------------------------------------------------
133254721Semaste    /// Find a line entry that contains the section offset address \a
134254721Semaste    /// so_addr.
135254721Semaste    ///
136254721Semaste    /// @param[in] so_addr
137254721Semaste    ///     A section offset address object containing the address we
138254721Semaste    ///     are searching for.
139254721Semaste    ///
140254721Semaste    /// @param[out] line_entry
141254721Semaste    ///     A copy of the line entry that was found if \b true is
142254721Semaste    ///     returned, otherwise \a entry is left unmodified.
143254721Semaste    ///
144254721Semaste    /// @param[out] index_ptr
145254721Semaste    ///     A pointer to a 32 bit integer that will get the actual line
146254721Semaste    ///     entry index if it is not NULL.
147254721Semaste    ///
148254721Semaste    /// @return
149254721Semaste    ///     Returns \b true if \a so_addr is contained in a line entry
150254721Semaste    ///     in this line table, \b false otherwise.
151254721Semaste    //------------------------------------------------------------------
152254721Semaste    bool
153254721Semaste    FindLineEntryByAddress (const Address &so_addr, LineEntry& line_entry, uint32_t *index_ptr = NULL);
154254721Semaste
155254721Semaste    //------------------------------------------------------------------
156254721Semaste    /// Find a line entry index that has a matching file index and
157254721Semaste    /// source line number.
158254721Semaste    ///
159254721Semaste    /// Finds the next line entry that has a matching \a file_idx and
160254721Semaste    /// source line number \a line starting at the \a start_idx entries
161254721Semaste    /// into the line entry collection.
162254721Semaste    ///
163254721Semaste    /// @param[in] start_idx
164254721Semaste    ///     The number of entries to skip when starting the search.
165254721Semaste    ///
166254721Semaste    /// @param[out] file_idx
167254721Semaste    ///     The file index to search for that should be found prior
168254721Semaste    ///     to calling this function using the following functions:
169254721Semaste    ///     CompileUnit::GetSupportFiles()
170254721Semaste    ///     FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
171254721Semaste    ///
172254721Semaste    /// @param[in] line
173254721Semaste    ///     The source line to match.
174254721Semaste    ///
175254721Semaste    /// @param[in] exact
176254721Semaste    ///     If true, match only if you find a line entry exactly matching \a line.
177254721Semaste    ///     If false, return the closest line entry greater than \a line.
178254721Semaste    ///
179254721Semaste    /// @param[out] line_entry
180254721Semaste    ///     A reference to a line entry object that will get a copy of
181254721Semaste    ///     the line entry if \b true is returned, otherwise \a
182254721Semaste    ///     line_entry is left untouched.
183254721Semaste    ///
184254721Semaste    /// @return
185254721Semaste    ///     Returns \b true if a matching line entry is found in this
186254721Semaste    ///     line table, \b false otherwise.
187254721Semaste    ///
188254721Semaste    /// @see CompileUnit::GetSupportFiles()
189254721Semaste    /// @see FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
190254721Semaste    //------------------------------------------------------------------
191254721Semaste    uint32_t
192254721Semaste    FindLineEntryIndexByFileIndex (uint32_t start_idx,
193254721Semaste                                   uint32_t file_idx,
194254721Semaste                                   uint32_t line,
195254721Semaste                                   bool exact,
196254721Semaste                                   LineEntry* line_entry_ptr);
197254721Semaste
198254721Semaste    uint32_t
199254721Semaste    FindLineEntryIndexByFileIndex (uint32_t start_idx,
200254721Semaste                                   const std::vector<uint32_t> &file_indexes,
201254721Semaste                                   uint32_t line,
202254721Semaste                                   bool exact,
203254721Semaste                                   LineEntry* line_entry_ptr);
204254721Semaste
205254721Semaste    size_t
206254721Semaste    FineLineEntriesForFileIndex (uint32_t file_idx,
207254721Semaste                                 bool append,
208254721Semaste                                 SymbolContextList &sc_list);
209254721Semaste
210254721Semaste    //------------------------------------------------------------------
211254721Semaste    /// Get the line entry from the line table at index \a idx.
212254721Semaste    ///
213254721Semaste    /// @param[in] idx
214254721Semaste    ///     An index into the line table entry collection.
215254721Semaste    ///
216254721Semaste    /// @return
217254721Semaste    ///     A valid line entry if \a idx is a valid index, or an invalid
218254721Semaste    ///     line entry if \a idx is not valid.
219254721Semaste    ///
220254721Semaste    /// @see LineTable::GetSize()
221254721Semaste    /// @see LineEntry::IsValid() const
222254721Semaste    //------------------------------------------------------------------
223254721Semaste    bool
224254721Semaste    GetLineEntryAtIndex(uint32_t idx, LineEntry& line_entry);
225254721Semaste
226254721Semaste    //------------------------------------------------------------------
227254721Semaste    /// Gets the size of the line table in number of line table entries.
228254721Semaste    ///
229254721Semaste    /// @return
230254721Semaste    ///     The number of line table entries in this line table.
231254721Semaste    //------------------------------------------------------------------
232254721Semaste    uint32_t
233254721Semaste    GetSize () const;
234254721Semaste
235254721Semaste    typedef lldb_private::RangeArray<lldb::addr_t, lldb::addr_t, 32> FileAddressRanges;
236254721Semaste
237254721Semaste    //------------------------------------------------------------------
238254721Semaste    /// Gets all contiguous file address ranges for the entire line table.
239254721Semaste    ///
240254721Semaste    /// @param[out] file_ranges
241254721Semaste    ///     A collection of file address ranges that will be filled in
242254721Semaste    ///     by this function.
243254721Semaste    ///
244254721Semaste    /// @param[out] append
245254721Semaste    ///     If \b true, then append to \a file_ranges, otherwise clear
246254721Semaste    ///     \a file_ranges prior to adding any ranges.
247254721Semaste    ///
248254721Semaste    /// @return
249254721Semaste    ///     The number of address ranges added to \a file_ranges
250254721Semaste    //------------------------------------------------------------------
251254721Semaste    size_t
252254721Semaste    GetContiguousFileAddressRanges (FileAddressRanges &file_ranges, bool append);
253254721Semaste
254254721Semaste    //------------------------------------------------------------------
255254721Semaste    /// Given a file range link map, relink the current line table
256254721Semaste    /// and return a fixed up line table.
257254721Semaste    ///
258254721Semaste    /// @param[out] file_range_map
259254721Semaste    ///     A collection of file ranges that maps to new file ranges
260254721Semaste    ///     that will be used when linking the line table.
261254721Semaste    ///
262254721Semaste    /// @return
263254721Semaste    ///     A new line table if at least one line table entry was able
264254721Semaste    ///     to be mapped.
265254721Semaste    //------------------------------------------------------------------
266254721Semaste    typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t> FileRangeMap;
267254721Semaste
268254721Semaste    LineTable *
269254721Semaste    LinkLineTable (const FileRangeMap &file_range_map);
270254721Semaste
271254721Semasteprotected:
272254721Semaste
273254721Semaste    struct Entry
274254721Semaste    {
275254721Semaste        Entry () :
276254721Semaste            file_addr (LLDB_INVALID_ADDRESS),
277254721Semaste            line (0),
278254721Semaste            column (0),
279254721Semaste            file_idx (0),
280254721Semaste            is_start_of_statement (false),
281254721Semaste            is_start_of_basic_block (false),
282254721Semaste            is_prologue_end (false),
283254721Semaste            is_epilogue_begin (false),
284254721Semaste            is_terminal_entry (false)
285254721Semaste        {
286254721Semaste        }
287254721Semaste
288254721Semaste        Entry ( lldb::addr_t _file_addr,
289254721Semaste                uint32_t _line,
290254721Semaste                uint16_t _column,
291254721Semaste                uint16_t _file_idx,
292254721Semaste                bool _is_start_of_statement,
293254721Semaste                bool _is_start_of_basic_block,
294254721Semaste                bool _is_prologue_end,
295254721Semaste                bool _is_epilogue_begin,
296254721Semaste                bool _is_terminal_entry) :
297254721Semaste            file_addr (_file_addr),
298254721Semaste            line (_line),
299254721Semaste            column (_column),
300254721Semaste            file_idx (_file_idx),
301254721Semaste            is_start_of_statement (_is_start_of_statement),
302254721Semaste            is_start_of_basic_block (_is_start_of_basic_block),
303254721Semaste            is_prologue_end (_is_prologue_end),
304254721Semaste            is_epilogue_begin (_is_epilogue_begin),
305254721Semaste            is_terminal_entry (_is_terminal_entry)
306254721Semaste        {
307254721Semaste        }
308254721Semaste
309254721Semaste        int
310254721Semaste        bsearch_compare (const void *key, const void *arrmem);
311254721Semaste
312254721Semaste        void
313254721Semaste        Clear ()
314254721Semaste        {
315254721Semaste            file_addr = LLDB_INVALID_ADDRESS;
316254721Semaste            line = 0;
317254721Semaste            column = 0;
318254721Semaste            file_idx = 0;
319254721Semaste            is_start_of_statement = false;
320254721Semaste            is_start_of_basic_block = false;
321254721Semaste            is_prologue_end = false;
322254721Semaste            is_epilogue_begin = false;
323254721Semaste            is_terminal_entry = false;
324254721Semaste        }
325254721Semaste
326254721Semaste        static int
327254721Semaste        Compare (const Entry& lhs, const Entry& rhs)
328254721Semaste        {
329254721Semaste            // Compare the sections before calling
330254721Semaste            #define SCALAR_COMPARE(a,b) if (a < b) return -1; if (a > b) return +1
331254721Semaste            SCALAR_COMPARE (lhs.file_addr, rhs.file_addr);
332254721Semaste            SCALAR_COMPARE (lhs.line, rhs.line);
333254721Semaste            SCALAR_COMPARE (lhs.column, rhs.column);
334254721Semaste            SCALAR_COMPARE (lhs.is_start_of_statement, rhs.is_start_of_statement);
335254721Semaste            SCALAR_COMPARE (lhs.is_start_of_basic_block, rhs.is_start_of_basic_block);
336254721Semaste            // rhs and lhs reversed on purpose below.
337254721Semaste            SCALAR_COMPARE (rhs.is_prologue_end, lhs.is_prologue_end);
338254721Semaste            SCALAR_COMPARE (lhs.is_epilogue_begin, rhs.is_epilogue_begin);
339254721Semaste            // rhs and lhs reversed on purpose below.
340254721Semaste            SCALAR_COMPARE (rhs.is_terminal_entry, lhs.is_terminal_entry);
341254721Semaste            SCALAR_COMPARE (lhs.file_idx, rhs.file_idx);
342254721Semaste            #undef SCALAR_COMPARE
343254721Semaste            return 0;
344254721Semaste        }
345254721Semaste
346254721Semaste
347254721Semaste        class LessThanBinaryPredicate
348254721Semaste        {
349254721Semaste        public:
350254721Semaste            LessThanBinaryPredicate(LineTable *line_table);
351254721Semaste            bool operator() (const LineTable::Entry&, const LineTable::Entry&) const;
352254721Semaste        protected:
353254721Semaste            LineTable *m_line_table;
354254721Semaste        };
355254721Semaste
356254721Semaste        static bool EntryAddressLessThan (const Entry& lhs, const Entry& rhs)
357254721Semaste        {
358254721Semaste            return lhs.file_addr < rhs.file_addr;
359254721Semaste        }
360254721Semaste
361254721Semaste        //------------------------------------------------------------------
362254721Semaste        // Member variables.
363254721Semaste        //------------------------------------------------------------------
364254721Semaste        lldb::addr_t file_addr;                 ///< The file address for this line entry
365254721Semaste        uint32_t    line;                       ///< The source line number, or zero if there is no line number information.
366254721Semaste        uint16_t    column;                     ///< The column number of the source line, or zero if there is no column information.
367254721Semaste        uint16_t    file_idx:11,                ///< The file index into CompileUnit's file table, or zero if there is no file information.
368254721Semaste                    is_start_of_statement:1,    ///< Indicates this entry is the beginning of a statement.
369254721Semaste                    is_start_of_basic_block:1,  ///< Indicates this entry is the beginning of a basic block.
370254721Semaste                    is_prologue_end:1,          ///< Indicates this entry is one (of possibly many) where execution should be suspended for an entry breakpoint of a function.
371254721Semaste                    is_epilogue_begin:1,        ///< Indicates this entry is one (of possibly many) where execution should be suspended for an exit breakpoint of a function.
372254721Semaste                    is_terminal_entry:1;        ///< Indicates this entry is that of the first byte after the end of a sequence of target machine instructions.
373254721Semaste    };
374254721Semaste
375254721Semaste    struct EntrySearchInfo
376254721Semaste    {
377254721Semaste        LineTable* line_table;
378254721Semaste        lldb_private::Section *a_section;
379254721Semaste        Entry *a_entry;
380254721Semaste    };
381254721Semaste
382254721Semaste    //------------------------------------------------------------------
383254721Semaste    // Types
384254721Semaste    //------------------------------------------------------------------
385254721Semaste    typedef std::vector<lldb_private::Section*> section_collection; ///< The collection type for the sections.
386254721Semaste    typedef std::vector<Entry>                  entry_collection;   ///< The collection type for the line entries.
387254721Semaste    //------------------------------------------------------------------
388254721Semaste    // Member variables.
389254721Semaste    //------------------------------------------------------------------
390254721Semaste    CompileUnit* m_comp_unit;   ///< The compile unit that this line table belongs to.
391254721Semaste    entry_collection m_entries; ///< The collection of line entries in this line table.
392254721Semaste
393254721Semaste    //------------------------------------------------------------------
394254721Semaste    // Helper class
395254721Semaste    //------------------------------------------------------------------
396254721Semaste    class LineSequenceImpl : public LineSequence
397254721Semaste    {
398254721Semaste    public:
399254721Semaste        LineSequenceImpl() :
400254721Semaste            LineSequence()
401254721Semaste        {}
402254721Semaste
403254721Semaste        virtual
404254721Semaste        ~LineSequenceImpl()
405254721Semaste        {}
406254721Semaste
407254721Semaste        virtual void
408254721Semaste        Clear();
409254721Semaste
410254721Semaste        entry_collection m_entries; ///< The collection of line entries in this sequence.
411254721Semaste    };
412254721Semaste
413254721Semaste    bool
414254721Semaste    ConvertEntryAtIndexToLineEntry (uint32_t idx, LineEntry &line_entry);
415254721Semaste
416254721Semasteprivate:
417254721Semaste    DISALLOW_COPY_AND_ASSIGN (LineTable);
418254721Semaste};
419254721Semaste
420254721Semaste} // namespace lldb_private
421254721Semaste
422254721Semaste#endif  // liblldb_LineTable_h_
423