Skip to content

Commit ee1adc5

Browse files
[lldb] Add a return opcode to the formatter bytecode (#121602)
In LLVM we love our early exists and this opcode allows for simpler code generation.
1 parent dfa4312 commit ee1adc5

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

lldb/docs/resources/formatterbytecode.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ These manipulate the control stack and program counter. Both `if` and `ifelse` e
7575
0x12 `ifelse` `(UInt -> )` pop two blocks from the control stack, if
7676
the top of the data stack is nonzero, execute the first,
7777
otherwise the second.
78+
0x13 `return` pop the entire control stack and return
7879
======== ========== ============================================================
7980

8081
Literals for basic types

lldb/examples/python/formatter_bytecode.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def define_opcode(n, mnemonic, name):
3535
define_opcode(0x10, "{", "begin")
3636
define_opcode(0x11, "if", "if")
3737
define_opcode(0x12, "ifelse", "ifelse")
38+
define_opcode(0x13, "return", "return")
3839

3940
define_opcode(0x20, None, "lit_uint")
4041
define_opcode(0x21, None, "lit_int")
@@ -342,6 +343,9 @@ def next_byte():
342343
else:
343344
frame.append(control.pop())
344345
control.pop()
346+
elif b == op_return:
347+
control.clear()
348+
return data[-1]
345349

346350
# Literals.
347351
elif b == op_lit_uint:

lldb/source/DataFormatters/FormatterBytecode.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
304304
control.pop_back();
305305
activate_block();
306306
continue;
307+
case op_return:
308+
control.clear();
309+
return pc.takeError();
307310

308311
// Literals.
309312
case op_lit_uint:

lldb/source/DataFormatters/FormatterBytecode.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ DEFINE_OPCODE(0x06, "rot", rot)
2727
DEFINE_OPCODE(0x10, "{", begin)
2828
DEFINE_OPCODE(0x11, "if", if)
2929
DEFINE_OPCODE(0x12, "ifelse", ifelse)
30+
DEFINE_OPCODE(0x13, "return", return)
3031

3132
DEFINE_OPCODE(0x20, nullptr, lit_uint)
3233
DEFINE_OPCODE(0x21, nullptr, lit_int)

lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ TEST_F(FormatterBytecodeTest, ControlOps) {
9797
data));
9898
ASSERT_EQ(data.Pop<uint64_t>(), 42u);
9999
}
100+
{
101+
DataStack data;
102+
ASSERT_TRUE(Interpret({op_lit_uint, 1, op_begin, 3, op_lit_uint, 42,
103+
op_return, op_if, op_lit_uint, 23},
104+
data));
105+
ASSERT_EQ(data.Pop<uint64_t>(), 42u);
106+
}
107+
}
108+
109+
TEST_F(FormatterBytecodeTest, ConversionOps) {
100110
{
101111
DataStack data(lldb::ValueObjectSP{});
102112
ASSERT_TRUE(Interpret({op_is_null}, data));

0 commit comments

Comments
 (0)