[llvm-dev] Aarch64: Build MachineInstruction for "ADR Xd, ."

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 16 07:38:05 PDT 2020


Hi Max,

On Wed, 16 Sep 2020 at 13:02, Max Muster via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Register PcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
> BuildMI(bb, bb.instr_begin(),
>   DebugLoc(), TII.get(AArch64::ADR))
>    .addReg(PcReg)
>    .addLabel(".");
>
> However, I am struggling in adding a label  for the current location as the second operand.

If you literally just want the current address then ".addImm(0)" will
do it. You need a label if some other part of the program needs to
refer back to that location. In that case I think you'll need a pseudo
that gets converted to a label and an ADR by AArch64AsmPrinter.cpp.
Something like

  case AArch64::ADRAnchor: {
    MCSymbol *Label = MF->getContext().createTempSymbol();
    MCExpr *LabelE = MCSymbolRefExpr::create(Label, MF->getContext());

    OutStreamer->emitLabel(Label);
    EmitToStreamer(OutStreamer, MCInstBuilder(AArch64::ADR)
        .addReg(DestReg)
        .addExpr(LabelE)));
  }

And then of course you have to work out how to tell the other code
about this temporary label you created.

Cheers.

Tim.


More information about the llvm-dev mailing list