PostfixExpression: move parser out of NativePDB internals

Summary:
The postfix expressions in PDB and breakpad symbol files are similar
enough that they can be parsed by the same parser. This patch
generalizes the parser in the NativePDB plugin and moves it into the
PostfixExpression file created in the previous commit (r358976).

The generalization consists of treating any unrecognised token as a
"symbol" node (previously these would only be created for tokens
starting with "$", and other token would abort the parse). This is
needed because breakpad symbols can also contain ".cfa" tokens, which
refer to the frame's CFA.

The cosmetic changes include:
- using a factory function instead of a class for creating nodes (this
  is more generic as it allows the same BumpPtrAllocator to be used for
  other things too)
- using dedicated function for parsing operator tokens instead of a
  DenseMap (more efficient as we don't need to create the DenseMap every
  time).

Reviewers: amccarth, clayborg, JDevlieghere, aleksandr.urakov

Subscribers: jasonmolenda, lldb-commits, markmentovai, mgorny

Differential Revision: https://reviews.llvm.org/D61003

llvm-svn: 359073
This commit is contained in:
Pavel Labath
2019-04-24 07:27:05 +00:00
parent 71e8c6f20f
commit 88813103cd
6 changed files with 205 additions and 96 deletions

View File

@@ -26,19 +26,6 @@ using namespace lldb_private::postfix;
namespace {
class NodeAllocator {
public:
template <typename T, typename... Args> T *makeNode(Args &&... args) {
static_assert(std::is_trivially_destructible<T>::value,
"This object will not be destroyed!");
void *new_node_mem = m_alloc.Allocate(sizeof(T), alignof(T));
return new (new_node_mem) T(std::forward<Args>(args)...);
}
private:
llvm::BumpPtrAllocator m_alloc;
};
class FPOProgramASTVisitorMergeDependent : public Visitor<> {
public:
void Visit(BinaryOpNode &binary, Node *&) override {
@@ -81,7 +68,8 @@ class FPOProgramASTVisitorResolveRegisterRefs : public Visitor<bool> {
public:
static bool
Resolve(const llvm::DenseMap<llvm::StringRef, Node *> &dependent_programs,
llvm::Triple::ArchType arch_type, NodeAllocator &alloc, Node *&ast) {
llvm::Triple::ArchType arch_type, llvm::BumpPtrAllocator &alloc,
Node *&ast) {
return FPOProgramASTVisitorResolveRegisterRefs(dependent_programs,
arch_type, alloc)
.Dispatch(ast);
@@ -104,13 +92,13 @@ public:
private:
FPOProgramASTVisitorResolveRegisterRefs(
const llvm::DenseMap<llvm::StringRef, Node *> &dependent_programs,
llvm::Triple::ArchType arch_type, NodeAllocator &alloc)
llvm::Triple::ArchType arch_type, llvm::BumpPtrAllocator &alloc)
: m_dependent_programs(dependent_programs), m_arch_type(arch_type),
m_alloc(alloc) {}
const llvm::DenseMap<llvm::StringRef, Node *> &m_dependent_programs;
llvm::Triple::ArchType m_arch_type;
NodeAllocator &m_alloc;
llvm::BumpPtrAllocator &m_alloc;
};
static uint32_t ResolveLLDBRegisterNum(llvm::StringRef reg_name, llvm::Triple::ArchType arch_type) {
@@ -145,7 +133,7 @@ bool FPOProgramASTVisitorResolveRegisterRefs::Visit(SymbolNode &symbol,
if (reg_num == LLDB_INVALID_REGNUM)
return false;
ref = m_alloc.makeNode<RegisterNode>(reg_num);
ref = MakeNode<RegisterNode>(m_alloc, reg_num);
return true;
}
@@ -227,95 +215,23 @@ void FPOProgramASTVisitorDWARFCodegen::Visit(UnaryOpNode &unary, Node *&) {
} // namespace
static bool ParseFPOSingleAssignmentProgram(llvm::StringRef program,
NodeAllocator &alloc,
llvm::BumpPtrAllocator &alloc,
llvm::StringRef &register_name,
Node *&ast) {
llvm::SmallVector<llvm::StringRef, 16> tokens;
llvm::SplitString(program, tokens, " ");
if (tokens.empty())
return false;
llvm::SmallVector<Node *, 4> eval_stack;
llvm::DenseMap<llvm::StringRef, BinaryOpNode::OpType> ops_binary = {
{"+", BinaryOpNode::Plus},
{"-", BinaryOpNode::Minus},
{"@", BinaryOpNode::Align},
};
llvm::DenseMap<llvm::StringRef, UnaryOpNode::OpType> ops_unary = {
{"^", UnaryOpNode::Deref},
};
constexpr llvm::StringLiteral ra_search_keyword = ".raSearch";
// lvalue of assignment is always first token
// rvalue program goes next
for (size_t i = 1; i < tokens.size(); ++i) {
llvm::StringRef cur = tokens[i];
auto ops_binary_it = ops_binary.find(cur);
if (ops_binary_it != ops_binary.end()) {
// token is binary operator
if (eval_stack.size() < 2) {
return false;
}
Node *right = eval_stack.pop_back_val();
Node *left = eval_stack.pop_back_val();
Node *node =
alloc.makeNode<BinaryOpNode>(ops_binary_it->second, *left, *right);
eval_stack.push_back(node);
continue;
}
auto ops_unary_it = ops_unary.find(cur);
if (ops_unary_it != ops_unary.end()) {
// token is unary operator
if (eval_stack.empty()) {
return false;
}
Node *operand = eval_stack.pop_back_val();
Node *node = alloc.makeNode<UnaryOpNode>(ops_unary_it->second, *operand);
eval_stack.push_back(node);
continue;
}
if (cur.startswith("$")) {
eval_stack.push_back(alloc.makeNode<SymbolNode>(cur));
continue;
}
if (cur == ra_search_keyword) {
// TODO: .raSearch is unsupported
return false;
}
uint32_t value;
if (!cur.getAsInteger(10, value)) {
// token is integer literal
eval_stack.push_back(alloc.makeNode<IntegerNode>(value));
continue;
}
// unexpected token
std::tie(register_name, program) = getToken(program);
if (register_name.empty())
return false;
}
if (eval_stack.size() != 1) {
return false;
}
register_name = tokens[0];
ast = eval_stack.pop_back_val();
return true;
ast = Parse(program, alloc);
return ast != nullptr;
}
static Node *ParseFPOProgram(llvm::StringRef program,
llvm::StringRef register_name,
llvm::Triple::ArchType arch_type,
NodeAllocator &alloc) {
llvm::BumpPtrAllocator &alloc) {
llvm::DenseMap<llvm::StringRef, Node *> dependent_programs;
size_t cur = 0;
@@ -365,7 +281,7 @@ static Node *ParseFPOProgram(llvm::StringRef program,
bool lldb_private::npdb::TranslateFPOProgramToDWARFExpression(
llvm::StringRef program, llvm::StringRef register_name,
llvm::Triple::ArchType arch_type, Stream &stream) {
NodeAllocator node_alloc;
llvm::BumpPtrAllocator node_alloc;
Node *target_program =
ParseFPOProgram(program, register_name, arch_type, node_alloc);
if (target_program == nullptr) {