Path: blob/main/contrib/llvm-project/lldb/source/Symbol/PostfixExpression.cpp
39587 views
//===-- PostfixExpression.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements support for postfix expressions found in several symbol9// file formats, and their conversion to DWARF.10//11//===----------------------------------------------------------------------===//1213#include "lldb/Symbol/PostfixExpression.h"14#include "lldb/Core/dwarf.h"15#include "lldb/Utility/Stream.h"16#include "llvm/ADT/StringExtras.h"17#include <optional>1819using namespace lldb_private;20using namespace lldb_private::postfix;21using namespace lldb_private::dwarf;2223static std::optional<BinaryOpNode::OpType>24GetBinaryOpType(llvm::StringRef token) {25if (token.size() != 1)26return std::nullopt;27switch (token[0]) {28case '@':29return BinaryOpNode::Align;30case '-':31return BinaryOpNode::Minus;32case '+':33return BinaryOpNode::Plus;34}35return std::nullopt;36}3738static std::optional<UnaryOpNode::OpType>39GetUnaryOpType(llvm::StringRef token) {40if (token == "^")41return UnaryOpNode::Deref;42return std::nullopt;43}4445Node *postfix::ParseOneExpression(llvm::StringRef expr,46llvm::BumpPtrAllocator &alloc) {47llvm::SmallVector<Node *, 4> stack;4849llvm::StringRef token;50while (std::tie(token, expr) = getToken(expr), !token.empty()) {51if (auto op_type = GetBinaryOpType(token)) {52// token is binary operator53if (stack.size() < 2)54return nullptr;5556Node *right = stack.pop_back_val();57Node *left = stack.pop_back_val();58stack.push_back(MakeNode<BinaryOpNode>(alloc, *op_type, *left, *right));59continue;60}6162if (auto op_type = GetUnaryOpType(token)) {63// token is unary operator64if (stack.empty())65return nullptr;6667Node *operand = stack.pop_back_val();68stack.push_back(MakeNode<UnaryOpNode>(alloc, *op_type, *operand));69continue;70}7172int64_t value;73if (to_integer(token, value, 10)) {74// token is integer literal75stack.push_back(MakeNode<IntegerNode>(alloc, value));76continue;77}7879stack.push_back(MakeNode<SymbolNode>(alloc, token));80}8182if (stack.size() != 1)83return nullptr;8485return stack.back();86}8788std::vector<std::pair<llvm::StringRef, Node *>>89postfix::ParseFPOProgram(llvm::StringRef prog, llvm::BumpPtrAllocator &alloc) {90llvm::SmallVector<llvm::StringRef, 4> exprs;91prog.split(exprs, '=');92if (exprs.empty() || !exprs.back().trim().empty())93return {};94exprs.pop_back();9596std::vector<std::pair<llvm::StringRef, Node *>> result;97for (llvm::StringRef expr : exprs) {98llvm::StringRef lhs;99std::tie(lhs, expr) = getToken(expr);100Node *rhs = ParseOneExpression(expr, alloc);101if (!rhs)102return {};103result.emplace_back(lhs, rhs);104}105return result;106}107108namespace {109class SymbolResolver : public Visitor<bool> {110public:111SymbolResolver(llvm::function_ref<Node *(SymbolNode &symbol)> replacer)112: m_replacer(replacer) {}113114using Visitor<bool>::Dispatch;115116private:117bool Visit(BinaryOpNode &binary, Node *&) override {118return Dispatch(binary.Left()) && Dispatch(binary.Right());119}120121bool Visit(InitialValueNode &, Node *&) override { return true; }122bool Visit(IntegerNode &, Node *&) override { return true; }123bool Visit(RegisterNode &, Node *&) override { return true; }124125bool Visit(SymbolNode &symbol, Node *&ref) override {126if (Node *replacement = m_replacer(symbol)) {127ref = replacement;128if (replacement != &symbol)129return Dispatch(ref);130return true;131}132return false;133}134135bool Visit(UnaryOpNode &unary, Node *&) override {136return Dispatch(unary.Operand());137}138139llvm::function_ref<Node *(SymbolNode &symbol)> m_replacer;140};141142class DWARFCodegen : public Visitor<> {143public:144DWARFCodegen(Stream &stream) : m_out_stream(stream) {}145146using Visitor<>::Dispatch;147148private:149void Visit(BinaryOpNode &binary, Node *&) override;150151void Visit(InitialValueNode &val, Node *&) override;152153void Visit(IntegerNode &integer, Node *&) override {154m_out_stream.PutHex8(DW_OP_consts);155m_out_stream.PutSLEB128(integer.GetValue());156++m_stack_depth;157}158159void Visit(RegisterNode ®, Node *&) override;160161void Visit(SymbolNode &symbol, Node *&) override {162llvm_unreachable("Symbols should have been resolved by now!");163}164165void Visit(UnaryOpNode &unary, Node *&) override;166167Stream &m_out_stream;168169/// The number keeping track of the evaluation stack depth at any given170/// moment. Used for implementing InitialValueNodes. We start with171/// m_stack_depth = 1, assuming that the initial value is already on the172/// stack. This initial value will be the value of all InitialValueNodes. If173/// the expression does not contain InitialValueNodes, then m_stack_depth is174/// not used, and the generated expression will run correctly even without an175/// initial value.176size_t m_stack_depth = 1;177};178} // namespace179180void DWARFCodegen::Visit(BinaryOpNode &binary, Node *&) {181Dispatch(binary.Left());182Dispatch(binary.Right());183184switch (binary.GetOpType()) {185case BinaryOpNode::Plus:186m_out_stream.PutHex8(DW_OP_plus);187// NOTE: can be optimized by using DW_OP_plus_uconst opcpode188// if right child node is constant value189break;190case BinaryOpNode::Minus:191m_out_stream.PutHex8(DW_OP_minus);192break;193case BinaryOpNode::Align:194// emit align operator a @ b as195// a & ~(b - 1)196// NOTE: implicitly assuming that b is power of 2197m_out_stream.PutHex8(DW_OP_lit1);198m_out_stream.PutHex8(DW_OP_minus);199m_out_stream.PutHex8(DW_OP_not);200201m_out_stream.PutHex8(DW_OP_and);202break;203}204--m_stack_depth; // Two pops, one push.205}206207void DWARFCodegen::Visit(InitialValueNode &, Node *&) {208// We never go below the initial stack, so we can pick the initial value from209// the bottom of the stack at any moment.210assert(m_stack_depth >= 1);211m_out_stream.PutHex8(DW_OP_pick);212m_out_stream.PutHex8(m_stack_depth - 1);213++m_stack_depth;214}215216void DWARFCodegen::Visit(RegisterNode ®, Node *&) {217uint32_t reg_num = reg.GetRegNum();218assert(reg_num != LLDB_INVALID_REGNUM);219220if (reg_num > 31) {221m_out_stream.PutHex8(DW_OP_bregx);222m_out_stream.PutULEB128(reg_num);223} else224m_out_stream.PutHex8(DW_OP_breg0 + reg_num);225226m_out_stream.PutSLEB128(0);227++m_stack_depth;228}229230void DWARFCodegen::Visit(UnaryOpNode &unary, Node *&) {231Dispatch(unary.Operand());232233switch (unary.GetOpType()) {234case UnaryOpNode::Deref:235m_out_stream.PutHex8(DW_OP_deref);236break;237}238// Stack depth unchanged.239}240241bool postfix::ResolveSymbols(242Node *&node, llvm::function_ref<Node *(SymbolNode &)> replacer) {243return SymbolResolver(replacer).Dispatch(node);244}245246void postfix::ToDWARF(Node &node, Stream &stream) {247Node *ptr = &node;248DWARFCodegen(stream).Dispatch(ptr);249}250251252