10b57cec5SDimitry Andric //===-- PostfixExpression.h -------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements support for postfix expressions found in several symbol
100b57cec5SDimitry Andric // file formats, and their conversion to DWARF.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #ifndef LLDB_SYMBOL_POSTFIXEXPRESSION_H
150b57cec5SDimitry Andric #define LLDB_SYMBOL_POSTFIXEXPRESSION_H
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
190b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
20*9dba64beSDimitry Andric #include <vector>
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric namespace lldb_private {
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric class Stream;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric namespace postfix {
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric /// The base class for all nodes in the parsed postfix tree.
290b57cec5SDimitry Andric class Node {
300b57cec5SDimitry Andric public:
310b57cec5SDimitry Andric enum Kind {
320b57cec5SDimitry Andric BinaryOp,
330b57cec5SDimitry Andric InitialValue,
340b57cec5SDimitry Andric Integer,
350b57cec5SDimitry Andric Register,
360b57cec5SDimitry Andric Symbol,
370b57cec5SDimitry Andric UnaryOp,
380b57cec5SDimitry Andric };
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric protected:
Node(Kind kind)410b57cec5SDimitry Andric Node(Kind kind) : m_kind(kind) {}
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric public:
GetKind()440b57cec5SDimitry Andric Kind GetKind() const { return m_kind; }
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric private:
470b57cec5SDimitry Andric Kind m_kind;
480b57cec5SDimitry Andric };
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric /// A node representing a binary expression.
510b57cec5SDimitry Andric class BinaryOpNode : public Node {
520b57cec5SDimitry Andric public:
530b57cec5SDimitry Andric enum OpType {
540b57cec5SDimitry Andric Align, // alignDown(a, b)
550b57cec5SDimitry Andric Minus, // a - b
560b57cec5SDimitry Andric Plus, // a + b
570b57cec5SDimitry Andric };
580b57cec5SDimitry Andric
BinaryOpNode(OpType op_type,Node & left,Node & right)590b57cec5SDimitry Andric BinaryOpNode(OpType op_type, Node &left, Node &right)
600b57cec5SDimitry Andric : Node(BinaryOp), m_op_type(op_type), m_left(&left), m_right(&right) {}
610b57cec5SDimitry Andric
GetOpType()620b57cec5SDimitry Andric OpType GetOpType() const { return m_op_type; }
630b57cec5SDimitry Andric
Left()640b57cec5SDimitry Andric const Node *Left() const { return m_left; }
Left()650b57cec5SDimitry Andric Node *&Left() { return m_left; }
660b57cec5SDimitry Andric
Right()670b57cec5SDimitry Andric const Node *Right() const { return m_right; }
Right()680b57cec5SDimitry Andric Node *&Right() { return m_right; }
690b57cec5SDimitry Andric
classof(const Node * node)700b57cec5SDimitry Andric static bool classof(const Node *node) { return node->GetKind() == BinaryOp; }
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric private:
730b57cec5SDimitry Andric OpType m_op_type;
740b57cec5SDimitry Andric Node *m_left;
750b57cec5SDimitry Andric Node *m_right;
760b57cec5SDimitry Andric };
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric /// A node representing the canonical frame address.
790b57cec5SDimitry Andric class InitialValueNode: public Node {
800b57cec5SDimitry Andric public:
InitialValueNode()810b57cec5SDimitry Andric InitialValueNode() : Node(InitialValue) {}
820b57cec5SDimitry Andric
classof(const Node * node)830b57cec5SDimitry Andric static bool classof(const Node *node) {
840b57cec5SDimitry Andric return node->GetKind() == InitialValue;
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric };
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric /// A node representing an integer literal.
890b57cec5SDimitry Andric class IntegerNode : public Node {
900b57cec5SDimitry Andric public:
IntegerNode(int64_t value)910b57cec5SDimitry Andric IntegerNode(int64_t value) : Node(Integer), m_value(value) {}
920b57cec5SDimitry Andric
GetValue()930b57cec5SDimitry Andric int64_t GetValue() const { return m_value; }
940b57cec5SDimitry Andric
classof(const Node * node)950b57cec5SDimitry Andric static bool classof(const Node *node) { return node->GetKind() == Integer; }
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric private:
980b57cec5SDimitry Andric int64_t m_value;
990b57cec5SDimitry Andric };
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric /// A node representing the value of a register with the given register number.
1020b57cec5SDimitry Andric /// The register kind (RegisterKind enum) used for the specifying the register
1030b57cec5SDimitry Andric /// number is implicit and assumed to be the same for all Register nodes in a
1040b57cec5SDimitry Andric /// given tree.
1050b57cec5SDimitry Andric class RegisterNode : public Node {
1060b57cec5SDimitry Andric public:
RegisterNode(uint32_t reg_num)1070b57cec5SDimitry Andric RegisterNode(uint32_t reg_num) : Node(Register), m_reg_num(reg_num) {}
1080b57cec5SDimitry Andric
GetRegNum()1090b57cec5SDimitry Andric uint32_t GetRegNum() const { return m_reg_num; }
1100b57cec5SDimitry Andric
classof(const Node * node)1110b57cec5SDimitry Andric static bool classof(const Node *node) { return node->GetKind() == Register; }
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric private:
1140b57cec5SDimitry Andric uint32_t m_reg_num;
1150b57cec5SDimitry Andric };
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric /// A node representing a symbolic reference to a named entity. This may be a
1180b57cec5SDimitry Andric /// register, which hasn't yet been resolved to a RegisterNode.
1190b57cec5SDimitry Andric class SymbolNode : public Node {
1200b57cec5SDimitry Andric public:
SymbolNode(llvm::StringRef name)1210b57cec5SDimitry Andric SymbolNode(llvm::StringRef name) : Node(Symbol), m_name(name) {}
1220b57cec5SDimitry Andric
GetName()1230b57cec5SDimitry Andric llvm::StringRef GetName() const { return m_name; }
1240b57cec5SDimitry Andric
classof(const Node * node)1250b57cec5SDimitry Andric static bool classof(const Node *node) { return node->GetKind() == Symbol; }
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric private:
1280b57cec5SDimitry Andric llvm::StringRef m_name;
1290b57cec5SDimitry Andric };
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric /// A node representing a unary operation.
1320b57cec5SDimitry Andric class UnaryOpNode : public Node {
1330b57cec5SDimitry Andric public:
1340b57cec5SDimitry Andric enum OpType {
1350b57cec5SDimitry Andric Deref, // *a
1360b57cec5SDimitry Andric };
1370b57cec5SDimitry Andric
UnaryOpNode(OpType op_type,Node & operand)1380b57cec5SDimitry Andric UnaryOpNode(OpType op_type, Node &operand)
1390b57cec5SDimitry Andric : Node(UnaryOp), m_op_type(op_type), m_operand(&operand) {}
1400b57cec5SDimitry Andric
GetOpType()1410b57cec5SDimitry Andric OpType GetOpType() const { return m_op_type; }
1420b57cec5SDimitry Andric
Operand()1430b57cec5SDimitry Andric const Node *Operand() const { return m_operand; }
Operand()1440b57cec5SDimitry Andric Node *&Operand() { return m_operand; }
1450b57cec5SDimitry Andric
classof(const Node * node)1460b57cec5SDimitry Andric static bool classof(const Node *node) { return node->GetKind() == UnaryOp; }
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric private:
1490b57cec5SDimitry Andric OpType m_op_type;
1500b57cec5SDimitry Andric Node *m_operand;
1510b57cec5SDimitry Andric };
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric /// A template class implementing a visitor pattern, but with a couple of
1540b57cec5SDimitry Andric /// twists:
1550b57cec5SDimitry Andric /// - It uses type switch instead of virtual double dispatch. This allows the
1560b57cec5SDimitry Andric // node classes to be vtable-free and trivially destructible.
1570b57cec5SDimitry Andric /// - The Visit functions get an extra Node *& parameter, which refers to the
1580b57cec5SDimitry Andric /// child pointer of the parent of the node we are currently visiting. This
1590b57cec5SDimitry Andric /// allows mutating algorithms, which replace the currently visited node with
1600b57cec5SDimitry Andric /// a different one.
1610b57cec5SDimitry Andric /// - The class is templatized on the return type of the Visit functions, which
1620b57cec5SDimitry Andric /// means it's possible to return values from them.
1630b57cec5SDimitry Andric template <typename ResultT = void> class Visitor {
1640b57cec5SDimitry Andric protected:
1650b57cec5SDimitry Andric virtual ~Visitor() = default;
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric virtual ResultT Visit(BinaryOpNode &binary, Node *&ref) = 0;
1680b57cec5SDimitry Andric virtual ResultT Visit(InitialValueNode &val, Node *&ref) = 0;
1690b57cec5SDimitry Andric virtual ResultT Visit(IntegerNode &integer, Node *&) = 0;
1700b57cec5SDimitry Andric virtual ResultT Visit(RegisterNode ®, Node *&) = 0;
1710b57cec5SDimitry Andric virtual ResultT Visit(SymbolNode &symbol, Node *&ref) = 0;
1720b57cec5SDimitry Andric virtual ResultT Visit(UnaryOpNode &unary, Node *&ref) = 0;
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric /// Invoke the correct Visit function based on the dynamic type of the given
1750b57cec5SDimitry Andric /// node.
Dispatch(Node * & node)1760b57cec5SDimitry Andric ResultT Dispatch(Node *&node) {
1770b57cec5SDimitry Andric switch (node->GetKind()) {
1780b57cec5SDimitry Andric case Node::BinaryOp:
1790b57cec5SDimitry Andric return Visit(llvm::cast<BinaryOpNode>(*node), node);
1800b57cec5SDimitry Andric case Node::InitialValue:
1810b57cec5SDimitry Andric return Visit(llvm::cast<InitialValueNode>(*node), node);
1820b57cec5SDimitry Andric case Node::Integer:
1830b57cec5SDimitry Andric return Visit(llvm::cast<IntegerNode>(*node), node);
1840b57cec5SDimitry Andric case Node::Register:
1850b57cec5SDimitry Andric return Visit(llvm::cast<RegisterNode>(*node), node);
1860b57cec5SDimitry Andric case Node::Symbol:
1870b57cec5SDimitry Andric return Visit(llvm::cast<SymbolNode>(*node), node);
1880b57cec5SDimitry Andric case Node::UnaryOp:
1890b57cec5SDimitry Andric return Visit(llvm::cast<UnaryOpNode>(*node), node);
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric llvm_unreachable("Fully covered switch!");
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric };
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric /// A utility function for "resolving" SymbolNodes. It traverses a tree and
1960b57cec5SDimitry Andric /// calls the callback function for all SymbolNodes it encountered. The
1970b57cec5SDimitry Andric /// replacement function should return the node it wished to replace the current
1980b57cec5SDimitry Andric /// SymbolNode with (this can also be the original node), or nullptr in case of
1990b57cec5SDimitry Andric /// an error. The nodes returned by the callback are inspected and replaced
2000b57cec5SDimitry Andric /// recursively, *except* for the case when the function returns the exact same
2010b57cec5SDimitry Andric /// node as the input one. It returns true if all SymbolNodes were replaced
2020b57cec5SDimitry Andric /// successfully.
2030b57cec5SDimitry Andric bool ResolveSymbols(Node *&node,
2040b57cec5SDimitry Andric llvm::function_ref<Node *(SymbolNode &symbol)> replacer);
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric template <typename T, typename... Args>
MakeNode(llvm::BumpPtrAllocator & alloc,Args &&...args)2070b57cec5SDimitry Andric inline T *MakeNode(llvm::BumpPtrAllocator &alloc, Args &&... args) {
2080b57cec5SDimitry Andric static_assert(std::is_trivially_destructible<T>::value,
2090b57cec5SDimitry Andric "This object will not be destroyed!");
2100b57cec5SDimitry Andric return new (alloc.Allocate<T>()) T(std::forward<Args>(args)...);
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric /// Parse the given postfix expression. The parsed nodes are placed into the
2140b57cec5SDimitry Andric /// provided allocator.
215*9dba64beSDimitry Andric Node *ParseOneExpression(llvm::StringRef expr, llvm::BumpPtrAllocator &alloc);
216*9dba64beSDimitry Andric
217*9dba64beSDimitry Andric std::vector<std::pair<llvm::StringRef, Node *>>
218*9dba64beSDimitry Andric ParseFPOProgram(llvm::StringRef prog, llvm::BumpPtrAllocator &alloc);
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric /// Serialize the given expression tree as DWARF. The result is written into the
2210b57cec5SDimitry Andric /// given stream. The AST should not contain any SymbolNodes. If the expression
2220b57cec5SDimitry Andric /// contains InitialValueNodes, the generated expression will assume that their
2230b57cec5SDimitry Andric /// value will be provided as the top value of the initial evaluation stack (as
2240b57cec5SDimitry Andric /// is the case with the CFA value in register eh_unwind rules).
2250b57cec5SDimitry Andric void ToDWARF(Node &node, Stream &stream);
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric } // namespace postfix
2280b57cec5SDimitry Andric } // namespace lldb_private
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric #endif // LLDB_SYMBOL_POSTFIXEXPRESSION_H
231