Skip to content

[lldb] Add a return opcode to the formatter bytecode #121602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/docs/resources/formatterbytecode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ These manipulate the control stack and program counter. Both `if` and `ifelse` e
0x12 `ifelse` `(UInt -> )` pop two blocks from the control stack, if
the top of the data stack is nonzero, execute the first,
otherwise the second.
0x13 `return` pop the entire control stack and return
======== ========== ============================================================

Literals for basic types
Expand Down
4 changes: 4 additions & 0 deletions lldb/examples/python/formatter_bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def define_opcode(n, mnemonic, name):
define_opcode(0x10, "{", "begin")
define_opcode(0x11, "if", "if")
define_opcode(0x12, "ifelse", "ifelse")
define_opcode(0x13, "return", "return")

define_opcode(0x20, None, "lit_uint")
define_opcode(0x21, None, "lit_int")
Expand Down Expand Up @@ -342,6 +343,9 @@ def next_byte():
else:
frame.append(control.pop())
control.pop()
elif b == op_return:
control.clear()
return data[-1]

# Literals.
elif b == op_lit_uint:
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/DataFormatters/FormatterBytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
control.pop_back();
activate_block();
continue;
case op_return:
control.clear();
return pc.takeError();

// Literals.
case op_lit_uint:
Expand Down
1 change: 1 addition & 0 deletions lldb/source/DataFormatters/FormatterBytecode.def
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ DEFINE_OPCODE(0x06, "rot", rot)
DEFINE_OPCODE(0x10, "{", begin)
DEFINE_OPCODE(0x11, "if", if)
DEFINE_OPCODE(0x12, "ifelse", ifelse)
DEFINE_OPCODE(0x13, "return", return)

DEFINE_OPCODE(0x20, nullptr, lit_uint)
DEFINE_OPCODE(0x21, nullptr, lit_int)
Expand Down
10 changes: 10 additions & 0 deletions lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ TEST_F(FormatterBytecodeTest, ControlOps) {
data));
ASSERT_EQ(data.Pop<uint64_t>(), 42u);
}
{
DataStack data;
ASSERT_TRUE(Interpret({op_lit_uint, 1, op_begin, 3, op_lit_uint, 42,
op_return, op_if, op_lit_uint, 23},
data));
ASSERT_EQ(data.Pop<uint64_t>(), 42u);
}
}

TEST_F(FormatterBytecodeTest, ConversionOps) {
{
DataStack data(lldb::ValueObjectSP{});
ASSERT_TRUE(Interpret({op_is_null}, data));
Expand Down
Loading