File indexing completed on 2024-05-12 05:43:27

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef DWARFEXPRESSION_H
0019 #define DWARFEXPRESSION_H
0020 
0021 #include <QByteArray>
0022 #include <QMetaType>
0023 #include <QStack>
0024 
0025 #include <libdwarf.h>
0026 
0027 class QString;
0028 
0029 /** Handles DW_FORM_exprloc attribute values. */
0030 class DwarfExpression
0031 {
0032 public:
0033     DwarfExpression();
0034     DwarfExpression(const DwarfExpression&) = default;
0035     explicit DwarfExpression(Dwarf_Ptr block, Dwarf_Unsigned len, uint8_t addrSize);
0036     ~DwarfExpression() = default;
0037 
0038     DwarfExpression& operator=(const DwarfExpression&) = default;
0039 
0040     QString displayString() const;
0041 
0042     /** Push a value to the evaluation stack.
0043      *  Depending on the type of the expression, this might be needed before evaluation.
0044      */
0045     void push(uint64_t value);
0046     /** Returns the top-most value of the evaluation stack.
0047      *  This is typically used to retrieve the result of an evaluation.
0048      */
0049     uint64_t top() const;
0050     uint64_t pop();
0051 
0052     /** Evaluate a simple expression, ie. one that results in one result value.
0053      *  @return @c true on success, @c false otherwise, the stack contains invalid data in that case.
0054      */
0055     bool evaluateSimple();
0056 
0057 private:
0058     template <typename T> T readNumber(int index) const;
0059     int evaluateOne(int index);
0060 
0061     QByteArray m_block;
0062     uint8_t m_addrSize;
0063     QStack<uint64_t> m_stack;
0064 };
0065 
0066 Q_DECLARE_METATYPE(DwarfExpression)
0067 
0068 #endif // DWARFEXPRESSION_H