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
14#include "lldb/Utility/Stream.h"
15#include "llvm/ADT/DenseMap.h"
16
17#include "llvm/ADT/Enum.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/DebugInfo/CodeView/CodeView.h"
20#include "llvm/DebugInfo/CodeView/EnumTables.h"
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::postfix;
25
26static uint32_t ResolveLLDBRegisterNum(llvm::StringRef reg_name, llvm::Triple::ArchType arch_type) {
27 // lookup register name to get lldb register number
28 llvm::codeview::CPUType cpu_type;
29 switch (arch_type) {
30 case llvm::Triple::ArchType::aarch64:
31 cpu_type = llvm::codeview::CPUType::ARM64;
32 break;
33
34 default:
35 cpu_type = llvm::codeview::CPUType::X64;
36 break;
37 }
38
39 llvm::EnumStrings<uint16_t> register_names =
40 llvm::codeview::getRegisterNames(cpu_type);
41 auto it =
42 llvm::find_if(register_names, [&reg_name](const auto &register_entry) {
43 return reg_name.compare_insensitive(register_entry.name()) == 0;
44 });
45
46 if (it == register_names.end())
48
49 auto reg_id = static_cast<llvm::codeview::RegisterId>(it->value());
50 return npdb::GetLLDBRegisterNumber(arch_type, reg_id);
51}
52
53static Node *ResolveFPOProgram(llvm::StringRef program,
54 llvm::StringRef register_name,
55 llvm::Triple::ArchType arch_type,
56 llvm::BumpPtrAllocator &alloc) {
57 std::vector<std::pair<llvm::StringRef, Node *>> parsed =
58 postfix::ParseFPOProgram(program, alloc);
59
60 for (auto it = parsed.begin(), end = parsed.end(); it != end; ++it) {
61 // Emplace valid dependent subtrees to make target assignment independent
62 // from predecessors. Resolve all other SymbolNodes as registers.
63 bool success =
64 ResolveSymbols(it->second, [&](SymbolNode &symbol) -> Node * {
65 for (const auto &pair : llvm::make_range(parsed.begin(), it)) {
66 if (pair.first == symbol.GetName())
67 return pair.second;
68 }
69
70 uint32_t reg_num =
71 ResolveLLDBRegisterNum(symbol.GetName().drop_front(1), arch_type);
72
73 if (reg_num == LLDB_INVALID_REGNUM)
74 return nullptr;
75
76 return MakeNode<RegisterNode>(alloc, reg_num);
77 });
78 if (!success)
79 return nullptr;
80
81 if (it->first == register_name) {
82 // found target assignment program - no need to parse further
83 return it->second;
84 }
85 }
86
87 return nullptr;
88}
89
91 llvm::StringRef program, llvm::StringRef register_name,
92 llvm::Triple::ArchType arch_type, Stream &stream) {
93 llvm::BumpPtrAllocator node_alloc;
94 Node *target_program =
95 ResolveFPOProgram(program, register_name, arch_type, node_alloc);
96 if (target_program == nullptr) {
97 return false;
98 }
99
100 ToDWARF(*target_program, stream);
101 return true;
102}
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.