[llvm] [LLVM][DecoderEmitter] Add option to use lambdas in decodeToMCInst (PR #144814)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 18 16:53:18 PDT 2025


jurahul wrote:

Example of the generated code for the test case:

```C++
template <typename InsnType>
static DecodeStatus decodeToMCInst(DecodeStatus S, unsigned Idx, InsnType insn, MCInst &MI,
                                   uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete) {
  DecodeComplete = true;
  using TmpType = std::conditional_t<std::is_integral<InsnType>::value, InsnType, uint64_t>;
  TmpType tmp;
  auto decodeLambda0 = [&]() {
    return S;
  };
  auto decodeLambda1 = [&]() {
    tmp = fieldFromInstruction(insn, 2, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    return S;
  };
  auto decodeLambda2 = [&]() {
    tmp = fieldFromInstruction(insn, 2, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    tmp = fieldFromInstruction(insn, 4, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    return S;
  };
  auto decodeLambda3 = [&]() {
    tmp = fieldFromInstruction(insn, 2, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    tmp = fieldFromInstruction(insn, 4, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    tmp = fieldFromInstruction(insn, 6, 2);
    if (!Check(S, DecodeRegsRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
    return S;
  };
  llvm::function_ref<DecodeStatus()> decodeLambdaTable[] = {
    decodeLambda0,
    decodeLambda1,
    decodeLambda2,
    decodeLambda3,
  };
  if (Idx >= 4)
    llvm_unreachable("Invalid index!");
  return decodeLambdaTable[Idx]();
}```

https://github.com/llvm/llvm-project/pull/144814


More information about the llvm-commits mailing list