This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objdump] [NFC] Factor out DisassemblerTarget class.
ClosedPublic

Authored by jacek on Apr 24 2023, 1:03 PM.

Details

Summary

This is a preparation for ARM64EC/ARM64X binaries, which may contain both ARM64 and x86_64 code in the same file. llvm-objdump already has partial support for mixing disassemblers for ARM thumb mode support. However, for ARM64EC we can't share MCContext, MCInstrAnalysis and PrettyPrinter instances. This patch provides additional abstraction which makes adding mixed code support later in the series easier.

Diff Detail

Event Timeline

jacek created this revision.Apr 24 2023, 1:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 1:03 PM
jacek requested review of this revision.Apr 24 2023, 1:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 1:03 PM

Largely looks okay to me. Just a few small points.

llvm/tools/llvm-objdump/llvm-objdump.cpp
1417

Perhaps best to call this TheTarget here, to avoid unusual syntax like const class *Target.

1426

These should be StringRef or string_view right, not passed-by-value std::string?

1445

This and some of the other code can move to the initialiser list, right?

1466–1469

This feels a little weird to me (having two pointer members essentially to the same thing), although I see why you've done it. Did you consider using a shared_ptr instead? That also avoids coupling the secondary target quite so much to the primary target, so destruction order is less important.

1498

This can all be done in the initialiser list, rather than the constructor body, if I'm not mistaken.

jacek updated this revision to Diff 516781.Apr 25 2023, 6:38 AM

Thanks. The new version renames Target to TheTarget, uses StringRef for constructor arguments, uses initialiser lists for more members. It also uses std::shared_ptr for members that may be shared among instances.

MaskRay accepted this revision.Jun 29 2023, 1:18 PM

LGTM

llvm/tools/llvm-objdump/llvm-objdump.cpp
1486

.get()

This revision is now accepted and ready to land.Jun 29 2023, 1:18 PM
barannikov88 added inline comments.
llvm/tools/llvm-objdump/llvm-objdump.cpp
1415–1422

The class should be in anonymous namespace.

jacek updated this revision to Diff 536420.Jun 30 2023, 2:06 PM

The new version uses get() and moves the class to anonymous namespace. Thanks for reviews.

jhenderson added inline comments.Jul 3 2023, 12:16 AM
llvm/tools/llvm-objdump/llvm-objdump.cpp
953–954

As an alternative to my suggested inline edit, you could add a function that creates the relevant smart pointer and checks for the error before returning the pointer. You could then move most/all of the body of this class into the initialiser list. Something like the following rough code:

template <typename PtrTy, typename... ArgsTy>
PtrTy makePtrOrError(std::function<PtrTy(ArgsTy...)> Func, StringRef Message, ArgsTy... Args) {
  PtrTy Ptr = Func(Args...);
  if (!Ptr)
    reportError(Message);
  return Ptr;
}

DisassemblerTarget::DisassemblerTarget(...)
  : ...
    RegisterInfo(makePtrOrError(...)), AsmInfo(makePtrOrError(...)), /*etc*/{}
1011

Nit: don't need the new line at the end of the namespace IMHO.

jacek updated this revision to Diff 537189.Jul 4 2023, 4:08 PM

Updated with suggested changes.

jhenderson accepted this revision.Jul 5 2023, 1:47 AM

LGTM. Please make sure this is clang-formatted (it may well be) before landing.

This revision was automatically updated to reflect the committed changes.