[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.
This commit is contained in:
Adrian Prantl 2025-01-03 15:26:40 -08:00 committed by GitHub
parent dfa4312c9b
commit ee1adc5aab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 0 deletions

View File

@ -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

View File

@ -35,6 +35,7 @@ define_opcode(6, "rot", "rot")
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")
@ -342,6 +343,9 @@ def interpret(bytecode: bytearray, control: list, data: list, tracing: bool = Fa
else:
frame.append(control.pop())
control.pop()
elif b == op_return:
control.clear()
return data[-1]
# Literals.
elif b == op_lit_uint:

View File

@ -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:

View File

@ -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)

View File

@ -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));