2019-04-24 15:27:05 +08:00
|
|
|
//===-- PostfixExpression.cpp -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements support for postfix expressions found in several symbol
|
|
|
|
// file formats, and their conversion to DWARF.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Symbol/PostfixExpression.h"
|
2019-04-26 16:52:04 +08:00
|
|
|
#include "lldb/Core/dwarf.h"
|
|
|
|
#include "lldb/Utility/Stream.h"
|
2019-04-24 15:27:05 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb_private::postfix;
|
|
|
|
|
|
|
|
static llvm::Optional<BinaryOpNode::OpType>
|
|
|
|
GetBinaryOpType(llvm::StringRef token) {
|
|
|
|
if (token.size() != 1)
|
|
|
|
return llvm::None;
|
|
|
|
switch (token[0]) {
|
|
|
|
case '@':
|
|
|
|
return BinaryOpNode::Align;
|
|
|
|
case '-':
|
|
|
|
return BinaryOpNode::Minus;
|
|
|
|
case '+':
|
|
|
|
return BinaryOpNode::Plus;
|
|
|
|
}
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Optional<UnaryOpNode::OpType>
|
|
|
|
GetUnaryOpType(llvm::StringRef token) {
|
|
|
|
if (token == "^")
|
|
|
|
return UnaryOpNode::Deref;
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
2019-08-26 19:44:14 +08:00
|
|
|
Node *postfix::ParseOneExpression(llvm::StringRef expr,
|
|
|
|
llvm::BumpPtrAllocator &alloc) {
|
2019-04-24 15:27:05 +08:00
|
|
|
llvm::SmallVector<Node *, 4> stack;
|
|
|
|
|
|
|
|
llvm::StringRef token;
|
|
|
|
while (std::tie(token, expr) = getToken(expr), !token.empty()) {
|
|
|
|
if (auto op_type = GetBinaryOpType(token)) {
|
|
|
|
// token is binary operator
|
|
|
|
if (stack.size() < 2)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Node *right = stack.pop_back_val();
|
|
|
|
Node *left = stack.pop_back_val();
|
|
|
|
stack.push_back(MakeNode<BinaryOpNode>(alloc, *op_type, *left, *right));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto op_type = GetUnaryOpType(token)) {
|
|
|
|
// token is unary operator
|
|
|
|
if (stack.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Node *operand = stack.pop_back_val();
|
|
|
|
stack.push_back(MakeNode<UnaryOpNode>(alloc, *op_type, *operand));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
PostfixExpression: Use signed integers in IntegerNode
Summary:
This is necessary to support parsing expressions like ".cfa -16 + ^", as
that format is used in breakpad STACK CFI expressions.
Since the PDB expressions use the same parser, this change will affect
them too, but I don't believe that should be a problem in practice. If
PDBs do contain the negative values, it's very likely that they are
intended to be parsed the same way, and if they don't, then it doesn't
matter.
In case that we do ever need to handle this differently, we can always
make the parser behavior customizable, or just use a different parser.
To make sure that the integer size is big enough for everyone, I switch
from using a (unsigned) 32-bit integer to a 64-bit (signed) one.
Reviewers: amccarth, clayborg, aleksandr.urakov
Subscribers: markmentovai, lldb-commits
Differential Revision: https://reviews.llvm.org/D61311
llvm-svn: 360166
2019-05-07 23:58:20 +08:00
|
|
|
int64_t value;
|
2019-04-24 15:27:05 +08:00
|
|
|
if (to_integer(token, value, 10)) {
|
|
|
|
// token is integer literal
|
|
|
|
stack.push_back(MakeNode<IntegerNode>(alloc, value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.push_back(MakeNode<SymbolNode>(alloc, token));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.size() != 1)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return stack.back();
|
|
|
|
}
|
2019-04-26 16:52:04 +08:00
|
|
|
|
2019-08-26 19:44:14 +08:00
|
|
|
std::vector<std::pair<llvm::StringRef, Node *>>
|
|
|
|
postfix::ParseFPOProgram(llvm::StringRef prog, llvm::BumpPtrAllocator &alloc) {
|
|
|
|
llvm::SmallVector<llvm::StringRef, 4> exprs;
|
|
|
|
prog.split(exprs, '=');
|
|
|
|
if (exprs.empty() || !exprs.back().trim().empty())
|
|
|
|
return {};
|
|
|
|
exprs.pop_back();
|
|
|
|
|
|
|
|
std::vector<std::pair<llvm::StringRef, Node *>> result;
|
|
|
|
for (llvm::StringRef expr : exprs) {
|
|
|
|
llvm::StringRef lhs;
|
|
|
|
std::tie(lhs, expr) = getToken(expr);
|
|
|
|
Node *rhs = ParseOneExpression(expr, alloc);
|
|
|
|
if (!rhs)
|
|
|
|
return {};
|
|
|
|
result.emplace_back(lhs, rhs);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-26 16:52:04 +08:00
|
|
|
namespace {
|
|
|
|
class SymbolResolver : public Visitor<bool> {
|
|
|
|
public:
|
|
|
|
SymbolResolver(llvm::function_ref<Node *(SymbolNode &symbol)> replacer)
|
|
|
|
: m_replacer(replacer) {}
|
|
|
|
|
|
|
|
using Visitor<bool>::Dispatch;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool Visit(BinaryOpNode &binary, Node *&) override {
|
|
|
|
return Dispatch(binary.Left()) && Dispatch(binary.Right());
|
|
|
|
}
|
|
|
|
|
2019-04-30 21:33:18 +08:00
|
|
|
bool Visit(InitialValueNode &, Node *&) override { return true; }
|
|
|
|
bool Visit(IntegerNode &, Node *&) override { return true; }
|
|
|
|
bool Visit(RegisterNode &, Node *&) override { return true; }
|
2019-04-26 16:52:04 +08:00
|
|
|
|
|
|
|
bool Visit(SymbolNode &symbol, Node *&ref) override {
|
|
|
|
if (Node *replacement = m_replacer(symbol)) {
|
|
|
|
ref = replacement;
|
|
|
|
if (replacement != &symbol)
|
|
|
|
return Dispatch(ref);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Visit(UnaryOpNode &unary, Node *&) override {
|
|
|
|
return Dispatch(unary.Operand());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::function_ref<Node *(SymbolNode &symbol)> m_replacer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DWARFCodegen : public Visitor<> {
|
|
|
|
public:
|
|
|
|
DWARFCodegen(Stream &stream) : m_out_stream(stream) {}
|
|
|
|
|
|
|
|
using Visitor<>::Dispatch;
|
|
|
|
|
|
|
|
private:
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(BinaryOpNode &binary, Node *&) override;
|
2019-04-26 16:52:04 +08:00
|
|
|
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(InitialValueNode &val, Node *&) override;
|
2019-04-30 21:33:18 +08:00
|
|
|
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(IntegerNode &integer, Node *&) override {
|
PostfixExpression: Use signed integers in IntegerNode
Summary:
This is necessary to support parsing expressions like ".cfa -16 + ^", as
that format is used in breakpad STACK CFI expressions.
Since the PDB expressions use the same parser, this change will affect
them too, but I don't believe that should be a problem in practice. If
PDBs do contain the negative values, it's very likely that they are
intended to be parsed the same way, and if they don't, then it doesn't
matter.
In case that we do ever need to handle this differently, we can always
make the parser behavior customizable, or just use a different parser.
To make sure that the integer size is big enough for everyone, I switch
from using a (unsigned) 32-bit integer to a 64-bit (signed) one.
Reviewers: amccarth, clayborg, aleksandr.urakov
Subscribers: markmentovai, lldb-commits
Differential Revision: https://reviews.llvm.org/D61311
llvm-svn: 360166
2019-05-07 23:58:20 +08:00
|
|
|
m_out_stream.PutHex8(DW_OP_consts);
|
|
|
|
m_out_stream.PutSLEB128(integer.GetValue());
|
2019-04-30 21:33:18 +08:00
|
|
|
++m_stack_depth;
|
2019-04-26 16:52:04 +08:00
|
|
|
}
|
|
|
|
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(RegisterNode ®, Node *&) override;
|
2019-04-26 16:52:04 +08:00
|
|
|
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(SymbolNode &symbol, Node *&) override {
|
2019-04-26 16:52:04 +08:00
|
|
|
llvm_unreachable("Symbols should have been resolved by now!");
|
|
|
|
}
|
|
|
|
|
C.128 override, virtual keyword handling
Summary:
According to [C128] "Virtual functions should specify exactly one
of `virtual`, `override`, or `final`", I've added override where a
virtual function is overriden but the explicit `override` keyword
was missing. Whenever both `virtual` and `override` were specified,
I removed `virtual`. As C.128 puts it:
> [...] writing more than one of these three is both redundant and
> a potential source of errors.
I anticipate a discussion about whether or not to add `override` to
destructors but I went for it because of an example in [ISOCPP1000].
Let me repeat the comment for you here:
Consider this code:
```
struct Base {
virtual ~Base(){}
};
struct SubClass : Base {
~SubClass() {
std::cout << "It works!\n";
}
};
int main() {
std::unique_ptr<Base> ptr = std::make_unique<SubClass>();
}
```
If for some odd reason somebody removes the `virtual` keyword from the
`Base` struct, the code will no longer print `It works!`. So adding
`override` to destructors actively protects us from accidentally
breaking our code at runtime.
[C128]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final
[ISOCPP1000]: https://github.com/isocpp/CppCoreGuidelines/issues/1000#issuecomment-476951555
Reviewers: teemperor, JDevlieghere, davide, shafik
Reviewed By: teemperor
Subscribers: kwk, arphaman, kadircet, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D61440
llvm-svn: 359868
2019-05-03 18:03:28 +08:00
|
|
|
void Visit(UnaryOpNode &unary, Node *&) override;
|
2019-04-26 16:52:04 +08:00
|
|
|
|
|
|
|
Stream &m_out_stream;
|
2019-04-30 21:33:18 +08:00
|
|
|
|
|
|
|
/// The number keeping track of the evaluation stack depth at any given
|
|
|
|
/// moment. Used for implementing InitialValueNodes. We start with
|
|
|
|
/// m_stack_depth = 1, assuming that the initial value is already on the
|
|
|
|
/// stack. This initial value will be the value of all InitialValueNodes. If
|
|
|
|
/// the expression does not contain InitialValueNodes, then m_stack_depth is
|
|
|
|
/// not used, and the generated expression will run correctly even without an
|
|
|
|
/// initial value.
|
|
|
|
size_t m_stack_depth = 1;
|
2019-04-26 16:52:04 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void DWARFCodegen::Visit(BinaryOpNode &binary, Node *&) {
|
|
|
|
Dispatch(binary.Left());
|
|
|
|
Dispatch(binary.Right());
|
|
|
|
|
|
|
|
switch (binary.GetOpType()) {
|
|
|
|
case BinaryOpNode::Plus:
|
|
|
|
m_out_stream.PutHex8(DW_OP_plus);
|
|
|
|
// NOTE: can be optimized by using DW_OP_plus_uconst opcpode
|
|
|
|
// if right child node is constant value
|
|
|
|
break;
|
|
|
|
case BinaryOpNode::Minus:
|
|
|
|
m_out_stream.PutHex8(DW_OP_minus);
|
|
|
|
break;
|
|
|
|
case BinaryOpNode::Align:
|
|
|
|
// emit align operator a @ b as
|
|
|
|
// a & ~(b - 1)
|
|
|
|
// NOTE: implicitly assuming that b is power of 2
|
|
|
|
m_out_stream.PutHex8(DW_OP_lit1);
|
|
|
|
m_out_stream.PutHex8(DW_OP_minus);
|
|
|
|
m_out_stream.PutHex8(DW_OP_not);
|
|
|
|
|
|
|
|
m_out_stream.PutHex8(DW_OP_and);
|
|
|
|
break;
|
|
|
|
}
|
2019-04-30 21:33:18 +08:00
|
|
|
--m_stack_depth; // Two pops, one push.
|
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFCodegen::Visit(InitialValueNode &, Node *&) {
|
|
|
|
// We never go below the initial stack, so we can pick the initial value from
|
|
|
|
// the bottom of the stack at any moment.
|
|
|
|
assert(m_stack_depth >= 1);
|
|
|
|
m_out_stream.PutHex8(DW_OP_pick);
|
|
|
|
m_out_stream.PutHex8(m_stack_depth - 1);
|
|
|
|
++m_stack_depth;
|
2019-04-26 16:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFCodegen::Visit(RegisterNode ®, Node *&) {
|
|
|
|
uint32_t reg_num = reg.GetRegNum();
|
|
|
|
assert(reg_num != LLDB_INVALID_REGNUM);
|
|
|
|
|
|
|
|
if (reg_num > 31) {
|
|
|
|
m_out_stream.PutHex8(DW_OP_bregx);
|
|
|
|
m_out_stream.PutULEB128(reg_num);
|
|
|
|
} else
|
|
|
|
m_out_stream.PutHex8(DW_OP_breg0 + reg_num);
|
|
|
|
|
|
|
|
m_out_stream.PutSLEB128(0);
|
2019-04-30 21:33:18 +08:00
|
|
|
++m_stack_depth;
|
2019-04-26 16:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFCodegen::Visit(UnaryOpNode &unary, Node *&) {
|
|
|
|
Dispatch(unary.Operand());
|
|
|
|
|
|
|
|
switch (unary.GetOpType()) {
|
|
|
|
case UnaryOpNode::Deref:
|
|
|
|
m_out_stream.PutHex8(DW_OP_deref);
|
|
|
|
break;
|
|
|
|
}
|
2019-04-30 21:33:18 +08:00
|
|
|
// Stack depth unchanged.
|
2019-04-26 16:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool postfix::ResolveSymbols(
|
|
|
|
Node *&node, llvm::function_ref<Node *(SymbolNode &)> replacer) {
|
|
|
|
return SymbolResolver(replacer).Dispatch(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void postfix::ToDWARF(Node &node, Stream &stream) {
|
|
|
|
Node *ptr = &node;
|
|
|
|
DWARFCodegen(stream).Dispatch(ptr);
|
|
|
|
}
|