1 //===-- PdbFPOProgramToDWARFExpression.cpp --------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "PdbFPOProgramToDWARFExpression.h"
10 #include "CodeViewRegisterMapping.h"
12 #include "lldb/Symbol/PostfixExpression.h"
13 #include "lldb/Utility/LLDBAssert.h"
14 #include "lldb/Utility/Stream.h"
15 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/DebugInfo/CodeView/CodeView.h"
19 #include "llvm/DebugInfo/CodeView/EnumTables.h"
20 #include "llvm/Support/ScopedPrinter.h"
23 using namespace lldb_private
;
24 using namespace lldb_private::postfix
;
26 static 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
;
30 case llvm::Triple::ArchType::aarch64
:
31 cpu_type
= llvm::codeview::CPUType::ARM64
;
35 cpu_type
= llvm::codeview::CPUType::X64
;
39 llvm::ArrayRef
<llvm::EnumEntry
<uint16_t>> register_names
=
40 llvm::codeview::getRegisterNames(cpu_type
);
41 auto it
= llvm::find_if(
43 [®_name
](const llvm::EnumEntry
<uint16_t> ®ister_entry
) {
44 return reg_name
.compare_insensitive(register_entry
.Name
) == 0;
47 if (it
== register_names
.end())
48 return LLDB_INVALID_REGNUM
;
50 auto reg_id
= static_cast<llvm::codeview::RegisterId
>(it
->Value
);
51 return npdb::GetLLDBRegisterNumber(arch_type
, reg_id
);
54 static Node
*ResolveFPOProgram(llvm::StringRef program
,
55 llvm::StringRef register_name
,
56 llvm::Triple::ArchType arch_type
,
57 llvm::BumpPtrAllocator
&alloc
) {
58 std::vector
<std::pair
<llvm::StringRef
, Node
*>> parsed
=
59 postfix::ParseFPOProgram(program
, alloc
);
61 for (auto it
= parsed
.begin(), end
= parsed
.end(); it
!= end
; ++it
) {
62 // Emplace valid dependent subtrees to make target assignment independent
63 // from predecessors. Resolve all other SymbolNodes as registers.
65 ResolveSymbols(it
->second
, [&](SymbolNode
&symbol
) -> Node
* {
66 for (const auto &pair
: llvm::make_range(parsed
.begin(), it
)) {
67 if (pair
.first
== symbol
.GetName())
72 ResolveLLDBRegisterNum(symbol
.GetName().drop_front(1), arch_type
);
74 if (reg_num
== LLDB_INVALID_REGNUM
)
77 return MakeNode
<RegisterNode
>(alloc
, reg_num
);
82 if (it
->first
== register_name
) {
83 // found target assignment program - no need to parse further
91 bool lldb_private::npdb::TranslateFPOProgramToDWARFExpression(
92 llvm::StringRef program
, llvm::StringRef register_name
,
93 llvm::Triple::ArchType arch_type
, Stream
&stream
) {
94 llvm::BumpPtrAllocator node_alloc
;
95 Node
*target_program
=
96 ResolveFPOProgram(program
, register_name
, arch_type
, node_alloc
);
97 if (target_program
== nullptr) {
101 ToDWARF(*target_program
, stream
);