LLDB  mainline
PdbFPOProgramToDWARFExpression.cpp
Go to the documentation of this file.
1 //===-- PDBFPOProgramToDWARFExpression.cpp ----------------------*- 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 
11 
12 #include "lldb/Core/StreamBuffer.h"
15 #include "lldb/Utility/Stream.h"
16 #include "llvm/ADT/DenseMap.h"
17 
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/DebugInfo/CodeView/CodeView.h"
20 #include "llvm/DebugInfo/CodeView/EnumTables.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 using namespace lldb_private::postfix;
25 
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::ArrayRef<llvm::EnumEntry<uint16_t>> register_names =
29  llvm::codeview::getRegisterNames();
30  auto it = llvm::find_if(
31  register_names,
32  [&reg_name](const llvm::EnumEntry<uint16_t> &register_entry) {
33  return reg_name.compare_lower(register_entry.Name) == 0;
34  });
35 
36  if (it == register_names.end())
37  return LLDB_INVALID_REGNUM;
38 
39  auto reg_id = static_cast<llvm::codeview::RegisterId>(it->Value);
40  return npdb::GetLLDBRegisterNumber(arch_type, reg_id);
41 }
42 
43 static bool ParseFPOSingleAssignmentProgram(llvm::StringRef program,
44  llvm::BumpPtrAllocator &alloc,
45  llvm::StringRef &register_name,
46  Node *&ast) {
47  // lvalue of assignment is always first token
48  // rvalue program goes next
49  std::tie(register_name, program) = getToken(program);
50  if (register_name.empty())
51  return false;
52 
53  ast = Parse(program, alloc);
54  return ast != nullptr;
55 }
56 
57 static Node *ParseFPOProgram(llvm::StringRef program,
58  llvm::StringRef register_name,
59  llvm::Triple::ArchType arch_type,
60  llvm::BumpPtrAllocator &alloc) {
61  llvm::DenseMap<llvm::StringRef, Node *> dependent_programs;
62 
63  size_t cur = 0;
64  while (true) {
65  size_t assign_index = program.find('=', cur);
66  if (assign_index == llvm::StringRef::npos) {
67  llvm::StringRef tail = program.slice(cur, llvm::StringRef::npos);
68  if (!tail.trim().empty()) {
69  // missing assign operator
70  return nullptr;
71  }
72  break;
73  }
74  llvm::StringRef assignment_program = program.slice(cur, assign_index);
75 
76  llvm::StringRef lvalue_name;
77  Node *rvalue_ast = nullptr;
78  if (!ParseFPOSingleAssignmentProgram(assignment_program, alloc, lvalue_name,
79  rvalue_ast)) {
80  return nullptr;
81  }
82 
83  lldbassert(rvalue_ast);
84 
85  // Emplace valid dependent subtrees to make target assignment independent
86  // from predecessors. Resolve all other SymbolNodes as registers.
87  bool success =
88  ResolveSymbols(rvalue_ast, [&](SymbolNode &symbol) -> Node * {
89  if (Node *node = dependent_programs.lookup(symbol.GetName()))
90  return node;
91  uint32_t reg_num =
92  ResolveLLDBRegisterNum(symbol.GetName().drop_front(1), arch_type);
93 
94  if (reg_num == LLDB_INVALID_REGNUM)
95  return nullptr;
96 
97  return MakeNode<RegisterNode>(alloc, reg_num);
98  });
99  if (!success)
100  return nullptr;
101 
102  if (lvalue_name == register_name) {
103  // found target assignment program - no need to parse further
104  return rvalue_ast;
105  }
106 
107  dependent_programs[lvalue_name] = rvalue_ast;
108  cur = assign_index + 1;
109  }
110 
111  return nullptr;
112 }
113 
115  llvm::StringRef program, llvm::StringRef register_name,
116  llvm::Triple::ArchType arch_type, Stream &stream) {
117  llvm::BumpPtrAllocator node_alloc;
118  Node *target_program =
119  ParseFPOProgram(program, register_name, arch_type, node_alloc);
120  if (target_program == nullptr) {
121  return false;
122  }
123 
124  ToDWARF(*target_program, stream);
125  return true;
126 }
void ToDWARF(Node &node, Stream &stream)
Serialize the given expression tree as DWARF.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define lldbassert(x)
Definition: LLDBAssert.h:15
uint32_t GetLLDBRegisterNumber(llvm::Triple::ArchType arch_type, llvm::codeview::RegisterId register_id)
static uint32_t ResolveLLDBRegisterNum(llvm::StringRef reg_name, llvm::Triple::ArchType arch_type)
A node representing a symbolic reference to a named entity.
bool ResolveSymbols(Node *&node, llvm::function_ref< Node *(SymbolNode &symbol)> replacer)
A utility function for "resolving" SymbolNodes.
static bool ParseFPOSingleAssignmentProgram(llvm::StringRef program, llvm::BumpPtrAllocator &alloc, llvm::StringRef &register_name, Node *&ast)
Node * Parse(llvm::StringRef expr, llvm::BumpPtrAllocator &alloc)
Parse the given postfix expression.
The base class for all nodes in the parsed postfix tree.
static Node * ParseFPOProgram(llvm::StringRef program, llvm::StringRef register_name, llvm::Triple::ArchType arch_type, llvm::BumpPtrAllocator &alloc)
Definition: SBAddress.h:15
bool TranslateFPOProgramToDWARFExpression(llvm::StringRef program, llvm::StringRef register_name, llvm::Triple::ArchType arch_type, lldb_private::Stream &stream)
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:90