https://github.com/adrian-prantl created https://github.com/llvm/llvm-project/pull/121602
In LLVM we love our early exists and this opcode allows for simpler code generation. >From bffb3f827b2e2918cd6858728efbfefd319ae2dc Mon Sep 17 00:00:00 2001 From: Adrian Prantl <apra...@apple.com> Date: Fri, 3 Jan 2025 12:51:15 -0800 Subject: [PATCH] [lldb] Add a return opcode to the formatter bytecode In LLVM we love our early exists and this opcode allows for simpler code generation. --- lldb/docs/resources/formatterbytecode.rst | 1 + lldb/examples/python/formatter_bytecode.py | 4 ++++ lldb/source/DataFormatters/FormatterBytecode.cpp | 3 +++ lldb/source/DataFormatters/FormatterBytecode.def | 1 + lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp | 10 ++++++++++ 5 files changed, 19 insertions(+) diff --git a/lldb/docs/resources/formatterbytecode.rst b/lldb/docs/resources/formatterbytecode.rst index 20e148363ef957..34fb0f7ee924c4 100644 --- a/lldb/docs/resources/formatterbytecode.rst +++ b/lldb/docs/resources/formatterbytecode.rst @@ -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 diff --git a/lldb/examples/python/formatter_bytecode.py b/lldb/examples/python/formatter_bytecode.py index ccd0c68a75483c..36a14be283f319 100644 --- a/lldb/examples/python/formatter_bytecode.py +++ b/lldb/examples/python/formatter_bytecode.py @@ -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") @@ -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: diff --git a/lldb/source/DataFormatters/FormatterBytecode.cpp b/lldb/source/DataFormatters/FormatterBytecode.cpp index e49c7506781875..7f3dbe0dba37d6 100644 --- a/lldb/source/DataFormatters/FormatterBytecode.cpp +++ b/lldb/source/DataFormatters/FormatterBytecode.cpp @@ -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: diff --git a/lldb/source/DataFormatters/FormatterBytecode.def b/lldb/source/DataFormatters/FormatterBytecode.def index c6645631fa0065..29e0bee541c73c 100644 --- a/lldb/source/DataFormatters/FormatterBytecode.def +++ b/lldb/source/DataFormatters/FormatterBytecode.def @@ -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) diff --git a/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp b/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp index 7307db650c1629..5e980c3e1913cf 100644 --- a/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp +++ b/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp @@ -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)); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits