Skip to content

Commit cf1f908

Browse files
committed
[LLDB] *WIP* Add scalar literal node and binary addition
1 parent 6440b10 commit cf1f908

File tree

8 files changed

+581
-6
lines changed

8 files changed

+581
-6
lines changed

lldb/include/lldb/ValueObject/DILAST.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_VALUEOBJECT_DILAST_H
1010
#define LLDB_VALUEOBJECT_DILAST_H
1111

12+
#include "lldb/ValueObject/DILLexer.h"
1213
#include "lldb/ValueObject/ValueObject.h"
1314
#include "llvm/Support/Error.h"
1415
#include <cstdint>
@@ -19,10 +20,12 @@ namespace lldb_private::dil {
1920
/// The various types DIL AST nodes (used by the DIL parser).
2021
enum class NodeKind {
2122
eArraySubscriptNode,
23+
eBinaryOpNode,
2224
eBitExtractionNode,
2325
eErrorNode,
2426
eIdentifierNode,
2527
eMemberOfNode,
28+
eScalarLiteralNode,
2629
eUnaryOpNode,
2730
};
2831

@@ -32,6 +35,14 @@ enum class UnaryOpKind {
3235
Deref, // "*"
3336
};
3437

38+
enum class BinaryOpKind {
39+
Add, // "+"
40+
Sub, // "-"
41+
};
42+
43+
/// Translates DIL tokens to BinaryOpKind.
44+
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind);
45+
3546
/// Forward declaration, for use in DIL AST nodes. Definition is at the very
3647
/// end of this file.
3748
class Visitor;
@@ -178,6 +189,49 @@ class BitFieldExtractionNode : public ASTNode {
178189
int64_t m_last_index;
179190
};
180191

192+
class ScalarLiteralNode : public ASTNode {
193+
public:
194+
ScalarLiteralNode(uint32_t location, lldb::BasicType type, Scalar value)
195+
: ASTNode(location, NodeKind::eScalarLiteralNode), m_type(type),
196+
m_value(value) {}
197+
198+
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
199+
200+
lldb::BasicType GetType() const { return m_type; }
201+
Scalar GetValue() const & { return m_value; }
202+
203+
static bool classof(const ASTNode *node) {
204+
return node->GetKind() == NodeKind::eScalarLiteralNode;
205+
}
206+
207+
private:
208+
lldb::BasicType m_type;
209+
Scalar m_value;
210+
};
211+
212+
class BinaryOpNode : public ASTNode {
213+
public:
214+
BinaryOpNode(uint32_t location, BinaryOpKind kind, ASTNodeUP lhs,
215+
ASTNodeUP rhs)
216+
: ASTNode(location, NodeKind::eBinaryOpNode), m_kind(kind),
217+
m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
218+
219+
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
220+
221+
BinaryOpKind GetKind() const { return m_kind; }
222+
ASTNode *GetLHS() const { return m_lhs.get(); }
223+
ASTNode *GetRHS() const { return m_rhs.get(); }
224+
225+
static bool classof(const ASTNode *node) {
226+
return node->GetKind() == NodeKind::eBinaryOpNode;
227+
}
228+
229+
private:
230+
BinaryOpKind m_kind;
231+
ASTNodeUP m_lhs;
232+
ASTNodeUP m_rhs;
233+
};
234+
181235
/// This class contains one Visit method for each specialized type of
182236
/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
183237
/// the correct function in the DIL expression evaluator for evaluating that
@@ -195,6 +249,10 @@ class Visitor {
195249
Visit(const ArraySubscriptNode *node) = 0;
196250
virtual llvm::Expected<lldb::ValueObjectSP>
197251
Visit(const BitFieldExtractionNode *node) = 0;
252+
virtual llvm::Expected<lldb::ValueObjectSP>
253+
Visit(const ScalarLiteralNode *node) = 0;
254+
virtual llvm::Expected<lldb::ValueObjectSP>
255+
Visit(const BinaryOpNode *node) = 0;
198256
};
199257

200258
} // namespace lldb_private::dil

lldb/include/lldb/ValueObject/DILEval.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ class Interpreter : Visitor {
5454
Visit(const ArraySubscriptNode *node) override;
5555
llvm::Expected<lldb::ValueObjectSP>
5656
Visit(const BitFieldExtractionNode *node) override;
57+
llvm::Expected<lldb::ValueObjectSP>
58+
Visit(const ScalarLiteralNode *node) override;
59+
llvm::Expected<lldb::ValueObjectSP> Visit(const BinaryOpNode *node) override;
60+
61+
lldb::ValueObjectSP
62+
ConvertValueObjectToTypeSystem(lldb::ValueObjectSP valobj,
63+
lldb::TypeSystemSP type_system);
64+
65+
llvm::Error PrepareBinaryAddition(lldb::ValueObjectSP &lhs,
66+
lldb::ValueObjectSP &rhs,
67+
uint32_t location);
68+
69+
llvm::Expected<lldb::ValueObjectSP>
70+
EvaluateBinaryAddition(lldb::ValueObjectSP lhs, lldb::ValueObjectSP rhs,
71+
uint32_t location);
5772

5873
// Used by the interpreter to create objects, perform casts, etc.
5974
lldb::TargetSP m_target;

lldb/include/lldb/ValueObject/DILLexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Token {
3434
minus,
3535
numeric_constant,
3636
period,
37+
plus,
3738
r_paren,
3839
r_square,
3940
star,

lldb/include/lldb/ValueObject/DILParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class DILParser {
8787
ASTNodeUP Run();
8888

8989
ASTNodeUP ParseExpression();
90+
ASTNodeUP ParseAdditiveExpression();
9091
ASTNodeUP ParseUnaryExpression();
9192
ASTNodeUP ParsePostfixExpression();
9293
ASTNodeUP ParsePrimaryExpression();
@@ -96,6 +97,8 @@ class DILParser {
9697
std::string ParseIdExpression();
9798
std::string ParseUnqualifiedId();
9899
std::optional<int64_t> ParseIntegerConstant();
100+
ASTNodeUP ParseNumericLiteral();
101+
ASTNodeUP ParseNumericConstant();
99102

100103
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
101104

lldb/source/ValueObject/DILAST.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace lldb_private::dil {
1313

14+
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind) {
15+
switch (token_kind) {
16+
case Token::minus:
17+
return BinaryOpKind::Sub;
18+
case Token::plus:
19+
return BinaryOpKind::Add;
20+
default:
21+
break;
22+
}
23+
llvm_unreachable("Unknown binary operator kind.");
24+
}
25+
1426
llvm::Expected<lldb::ValueObjectSP> ErrorNode::Accept(Visitor *v) const {
1527
llvm_unreachable("Attempting to Visit a DIL ErrorNode.");
1628
}
@@ -37,4 +49,13 @@ BitFieldExtractionNode::Accept(Visitor *v) const {
3749
return v->Visit(this);
3850
}
3951

52+
llvm::Expected<lldb::ValueObjectSP>
53+
ScalarLiteralNode::Accept(Visitor *v) const {
54+
return v->Visit(this);
55+
}
56+
57+
llvm::Expected<lldb::ValueObjectSP> BinaryOpNode::Accept(Visitor *v) const {
58+
return v->Visit(this);
59+
}
60+
4061
} // namespace lldb_private::dil

0 commit comments

Comments
 (0)