1 //===-- PostfixExpression.h -------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements support for postfix expressions found in several symbol 10 // file formats, and their conversion to DWARF. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLDB_SYMBOL_POSTFIXEXPRESSION_H 15 #define LLDB_SYMBOL_POSTFIXEXPRESSION_H 16 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Allocator.h" 19 #include "llvm/Support/Casting.h" 20 21 namespace lldb_private { 22 23 class Stream; 24 25 namespace postfix { 26 27 /// The base class for all nodes in the parsed postfix tree. 28 class Node { 29 public: 30 enum Kind { 31 BinaryOp, 32 InitialValue, 33 Integer, 34 Register, 35 Symbol, 36 UnaryOp, 37 }; 38 39 protected: 40 Node(Kind kind) : m_kind(kind) {} 41 42 public: 43 Kind GetKind() const { return m_kind; } 44 45 private: 46 Kind m_kind; 47 }; 48 49 /// A node representing a binary expression. 50 class BinaryOpNode : public Node { 51 public: 52 enum OpType { 53 Align, // alignDown(a, b) 54 Minus, // a - b 55 Plus, // a + b 56 }; 57 58 BinaryOpNode(OpType op_type, Node &left, Node &right) 59 : Node(BinaryOp), m_op_type(op_type), m_left(&left), m_right(&right) {} 60 61 OpType GetOpType() const { return m_op_type; } 62 63 const Node *Left() const { return m_left; } 64 Node *&Left() { return m_left; } 65 66 const Node *Right() const { return m_right; } 67 Node *&Right() { return m_right; } 68 69 static bool classof(const Node *node) { return node->GetKind() == BinaryOp; } 70 71 private: 72 OpType m_op_type; 73 Node *m_left; 74 Node *m_right; 75 }; 76 77 /// A node representing the canonical frame address. 78 class InitialValueNode: public Node { 79 public: 80 InitialValueNode() : Node(InitialValue) {} 81 82 static bool classof(const Node *node) { 83 return node->GetKind() == InitialValue; 84 } 85 }; 86 87 /// A node representing an integer literal. 88 class IntegerNode : public Node { 89 public: 90 IntegerNode(int64_t value) : Node(Integer), m_value(value) {} 91 92 int64_t GetValue() const { return m_value; } 93 94 static bool classof(const Node *node) { return node->GetKind() == Integer; } 95 96 private: 97 int64_t m_value; 98 }; 99 100 /// A node representing the value of a register with the given register number. 101 /// The register kind (RegisterKind enum) used for the specifying the register 102 /// number is implicit and assumed to be the same for all Register nodes in a 103 /// given tree. 104 class RegisterNode : public Node { 105 public: 106 RegisterNode(uint32_t reg_num) : Node(Register), m_reg_num(reg_num) {} 107 108 uint32_t GetRegNum() const { return m_reg_num; } 109 110 static bool classof(const Node *node) { return node->GetKind() == Register; } 111 112 private: 113 uint32_t m_reg_num; 114 }; 115 116 /// A node representing a symbolic reference to a named entity. This may be a 117 /// register, which hasn't yet been resolved to a RegisterNode. 118 class SymbolNode : public Node { 119 public: 120 SymbolNode(llvm::StringRef name) : Node(Symbol), m_name(name) {} 121 122 llvm::StringRef GetName() const { return m_name; } 123 124 static bool classof(const Node *node) { return node->GetKind() == Symbol; } 125 126 private: 127 llvm::StringRef m_name; 128 }; 129 130 /// A node representing a unary operation. 131 class UnaryOpNode : public Node { 132 public: 133 enum OpType { 134 Deref, // *a 135 }; 136 137 UnaryOpNode(OpType op_type, Node &operand) 138 : Node(UnaryOp), m_op_type(op_type), m_operand(&operand) {} 139 140 OpType GetOpType() const { return m_op_type; } 141 142 const Node *Operand() const { return m_operand; } 143 Node *&Operand() { return m_operand; } 144 145 static bool classof(const Node *node) { return node->GetKind() == UnaryOp; } 146 147 private: 148 OpType m_op_type; 149 Node *m_operand; 150 }; 151 152 /// A template class implementing a visitor pattern, but with a couple of 153 /// twists: 154 /// - It uses type switch instead of virtual double dispatch. This allows the 155 // node classes to be vtable-free and trivially destructible. 156 /// - The Visit functions get an extra Node *& parameter, which refers to the 157 /// child pointer of the parent of the node we are currently visiting. This 158 /// allows mutating algorithms, which replace the currently visited node with 159 /// a different one. 160 /// - The class is templatized on the return type of the Visit functions, which 161 /// means it's possible to return values from them. 162 template <typename ResultT = void> class Visitor { 163 protected: 164 virtual ~Visitor() = default; 165 166 virtual ResultT Visit(BinaryOpNode &binary, Node *&ref) = 0; 167 virtual ResultT Visit(InitialValueNode &val, Node *&ref) = 0; 168 virtual ResultT Visit(IntegerNode &integer, Node *&) = 0; 169 virtual ResultT Visit(RegisterNode ®, Node *&) = 0; 170 virtual ResultT Visit(SymbolNode &symbol, Node *&ref) = 0; 171 virtual ResultT Visit(UnaryOpNode &unary, Node *&ref) = 0; 172 173 /// Invoke the correct Visit function based on the dynamic type of the given 174 /// node. 175 ResultT Dispatch(Node *&node) { 176 switch (node->GetKind()) { 177 case Node::BinaryOp: 178 return Visit(llvm::cast<BinaryOpNode>(*node), node); 179 case Node::InitialValue: 180 return Visit(llvm::cast<InitialValueNode>(*node), node); 181 case Node::Integer: 182 return Visit(llvm::cast<IntegerNode>(*node), node); 183 case Node::Register: 184 return Visit(llvm::cast<RegisterNode>(*node), node); 185 case Node::Symbol: 186 return Visit(llvm::cast<SymbolNode>(*node), node); 187 case Node::UnaryOp: 188 return Visit(llvm::cast<UnaryOpNode>(*node), node); 189 } 190 llvm_unreachable("Fully covered switch!"); 191 } 192 }; 193 194 /// A utility function for "resolving" SymbolNodes. It traverses a tree and 195 /// calls the callback function for all SymbolNodes it encountered. The 196 /// replacement function should return the node it wished to replace the current 197 /// SymbolNode with (this can also be the original node), or nullptr in case of 198 /// an error. The nodes returned by the callback are inspected and replaced 199 /// recursively, *except* for the case when the function returns the exact same 200 /// node as the input one. It returns true if all SymbolNodes were replaced 201 /// successfully. 202 bool ResolveSymbols(Node *&node, 203 llvm::function_ref<Node *(SymbolNode &symbol)> replacer); 204 205 template <typename T, typename... Args> 206 inline T *MakeNode(llvm::BumpPtrAllocator &alloc, Args &&... args) { 207 static_assert(std::is_trivially_destructible<T>::value, 208 "This object will not be destroyed!"); 209 return new (alloc.Allocate<T>()) T(std::forward<Args>(args)...); 210 } 211 212 /// Parse the given postfix expression. The parsed nodes are placed into the 213 /// provided allocator. 214 Node *Parse(llvm::StringRef expr, llvm::BumpPtrAllocator &alloc); 215 216 /// Serialize the given expression tree as DWARF. The result is written into the 217 /// given stream. The AST should not contain any SymbolNodes. If the expression 218 /// contains InitialValueNodes, the generated expression will assume that their 219 /// value will be provided as the top value of the initial evaluation stack (as 220 /// is the case with the CFA value in register eh_unwind rules). 221 void ToDWARF(Node &node, Stream &stream); 222 223 } // namespace postfix 224 } // namespace lldb_private 225 226 #endif // LLDB_SYMBOL_POSTFIXEXPRESSION_H 227