LLDB mainline
PdbFPOProgramToDWARFExpression.cpp
Go to the documentation of this file.
1//===-- PdbFPOProgramToDWARFExpression.cpp --------------------------------===//
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
11
13#include "lldb/Utility/Stream.h"
14#include "llvm/ADT/DenseMap.h"
15
16#include "llvm/ADT/Enum.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/DebugInfo/CodeView/CodeView.h"
19#include "llvm/DebugInfo/CodeView/EnumTables.h"
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::postfix;
24
25static uint32_t ResolveLLDBRegisterNum(llvm::StringRef reg_name, llvm::Triple::ArchType arch_type) {
26 // lookup register name to get lldb register number
27 llvm::codeview::CPUType cpu_type;
28 switch (arch_type) {
29 case llvm::Triple::ArchType::aarch64:
30 cpu_type = llvm::codeview::CPUType::ARM64;
31 break;
32
33 default:
34 cpu_type = llvm::codeview::CPUType::X64;
35 break;
36 }
37
38 llvm::EnumStrings<uint16_t> register_names =
39 llvm::codeview::getRegisterNames(cpu_type);
40 auto it =
41 llvm::find_if(register_names, [&reg_name](const auto &register_entry) {
42 return reg_name.compare_insensitive(register_entry.name()) == 0;
43 });
44
45 if (it == register_names.end())
47
48 auto reg_id = static_cast<llvm::codeview::RegisterId>(it->value());
49 return npdb::GetLLDBRegisterNumber(arch_type, reg_id);
50}
51
52static Node *ResolveFPOProgram(llvm::StringRef program,
53 llvm::StringRef register_name,
54 llvm::Triple::ArchType arch_type,
55 llvm::BumpPtrAllocator &alloc) {
56 std::vector<std::pair<llvm::StringRef, Node *>> parsed =
57 postfix::ParseFPOProgram(program, alloc);
58
59 for (auto it = parsed.begin(), end = parsed.end(); it != end; ++it) {
60 // Emplace valid dependent subtrees to make target assignment independent
61 // from predecessors. Resolve all other SymbolNodes as registers.
62 bool success =
63 ResolveSymbols(it->second, [&](SymbolNode &symbol) -> Node * {
64 for (const auto &pair : llvm::make_range(parsed.begin(), it)) {
65 if (pair.first == symbol.GetName())
66 return pair.second;
67 }
68
69 uint32_t reg_num =
70 ResolveLLDBRegisterNum(symbol.GetName().drop_front(1), arch_type);
71
72 if (reg_num == LLDB_INVALID_REGNUM)
73 return nullptr;
74
75 return MakeNode<RegisterNode>(alloc, reg_num);
76 });
77 if (!success)
78 return nullptr;
79
80 if (it->first == register_name) {
81 // found target assignment program - no need to parse further
82 return it->second;
83 }
84 }
85
86 return nullptr;
87}
88
90 llvm::StringRef program, llvm::StringRef register_name,
91 llvm::Triple::ArchType arch_type, Stream &stream) {
92 llvm::BumpPtrAllocator node_alloc;
93 Node *target_program =
94 ResolveFPOProgram(program, register_name, arch_type, node_alloc);
95 if (target_program == nullptr) {
96 return false;
97 }
98
99 ToDWARF(*target_program, stream);
100 return true;
101}
static uint32_t ResolveLLDBRegisterNum(llvm::StringRef reg_name, llvm::Triple::ArchType arch_type)
static Node * ResolveFPOProgram(llvm::StringRef program, llvm::StringRef register_name, llvm::Triple::ArchType arch_type, llvm::BumpPtrAllocator &alloc)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
The base class for all nodes in the parsed postfix tree.
A node representing a symbolic reference to a named entity.
#define LLDB_INVALID_REGNUM
bool TranslateFPOProgramToDWARFExpression(llvm::StringRef program, llvm::StringRef register_name, llvm::Triple::ArchType arch_type, lldb_private::Stream &stream)
uint32_t GetLLDBRegisterNumber(llvm::Triple::ArchType arch_type, llvm::codeview::RegisterId register_id)
void ToDWARF(Node &node, Stream &stream)
Serialize the given expression tree as DWARF.
bool ResolveSymbols(Node *&node, llvm::function_ref< Node *(SymbolNode &symbol)> replacer)
A utility function for "resolving" SymbolNodes.
T * MakeNode(llvm::BumpPtrAllocator &alloc, Args &&... args)
std::vector< std::pair< llvm::StringRef, Node * > > ParseFPOProgram(llvm::StringRef prog, llvm::BumpPtrAllocator &alloc)
A class that represents a running process on the host machine.