LLVM  8.0.1
AArch64MCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- AArch64MCTargetDesc.cpp - AArch64 Target Descriptions ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides AArch64 specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64MCTargetDesc.h"
15 #include "AArch64ELFStreamer.h"
16 #include "AArch64MCAsmInfo.h"
17 #include "AArch64WinCOFFStreamer.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/Support/Endian.h"
31 
32 using namespace llvm;
33 
34 #define GET_INSTRINFO_MC_DESC
35 #define GET_INSTRINFO_MC_HELPERS
36 #include "AArch64GenInstrInfo.inc"
37 
38 #define GET_SUBTARGETINFO_MC_DESC
39 #include "AArch64GenSubtargetInfo.inc"
40 
41 #define GET_REGINFO_MC_DESC
42 #include "AArch64GenRegisterInfo.inc"
43 
45  MCInstrInfo *X = new MCInstrInfo();
46  InitAArch64MCInstrInfo(X);
47  return X;
48 }
49 
50 static MCSubtargetInfo *
52  if (CPU.empty())
53  CPU = "generic";
54 
55  return createAArch64MCSubtargetInfoImpl(TT, CPU, FS);
56 }
57 
59  for (unsigned Reg = AArch64::NoRegister + 1;
60  Reg < AArch64::NUM_TARGET_REGS; ++Reg) {
61  unsigned CV = MRI->getEncodingValue(Reg);
62  MRI->mapLLVMRegToCVReg(Reg, CV);
63  }
64 }
65 
68  InitAArch64MCRegisterInfo(X, AArch64::LR);
70  return X;
71 }
72 
74  const Triple &TheTriple) {
75  MCAsmInfo *MAI;
76  if (TheTriple.isOSBinFormatMachO())
77  MAI = new AArch64MCAsmInfoDarwin();
78  else if (TheTriple.isWindowsMSVCEnvironment())
80  else if (TheTriple.isOSBinFormatCOFF())
81  MAI = new AArch64MCAsmInfoGNUCOFF();
82  else {
83  assert(TheTriple.isOSBinFormatELF() && "Invalid target");
84  MAI = new AArch64MCAsmInfoELF(TheTriple);
85  }
86 
87  // Initial state of the frame pointer is SP.
88  unsigned Reg = MRI.getDwarfRegNum(AArch64::SP, true);
89  MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(nullptr, Reg, 0);
90  MAI->addInitialFrameState(Inst);
91 
92  return MAI;
93 }
94 
96  unsigned SyntaxVariant,
97  const MCAsmInfo &MAI,
98  const MCInstrInfo &MII,
99  const MCRegisterInfo &MRI) {
100  if (SyntaxVariant == 0)
101  return new AArch64InstPrinter(MAI, MII, MRI);
102  if (SyntaxVariant == 1)
103  return new AArch64AppleInstPrinter(MAI, MII, MRI);
104 
105  return nullptr;
106 }
107 
109  std::unique_ptr<MCAsmBackend> &&TAB,
110  std::unique_ptr<MCObjectWriter> &&OW,
111  std::unique_ptr<MCCodeEmitter> &&Emitter,
112  bool RelaxAll) {
113  return createAArch64ELFStreamer(Ctx, std::move(TAB), std::move(OW),
114  std::move(Emitter), RelaxAll);
115 }
116 
118  std::unique_ptr<MCAsmBackend> &&TAB,
119  std::unique_ptr<MCObjectWriter> &&OW,
120  std::unique_ptr<MCCodeEmitter> &&Emitter,
121  bool RelaxAll,
122  bool DWARFMustBeAtTheEnd) {
123  return createMachOStreamer(Ctx, std::move(TAB), std::move(OW),
124  std::move(Emitter), RelaxAll, DWARFMustBeAtTheEnd,
125  /*LabelSections*/ true);
126 }
127 
128 static MCStreamer *
129 createWinCOFFStreamer(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
130  std::unique_ptr<MCObjectWriter> &&OW,
131  std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
132  bool IncrementalLinkerCompatible) {
133  return createAArch64WinCOFFStreamer(Ctx, std::move(TAB), std::move(OW),
134  std::move(Emitter), RelaxAll,
135  IncrementalLinkerCompatible);
136 }
137 
138 namespace {
139 
140 class AArch64MCInstrAnalysis : public MCInstrAnalysis {
141 public:
142  AArch64MCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
143 
144  bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
145  uint64_t &Target) const override {
146  // Search for a PC-relative argument.
147  // This will handle instructions like bcc (where the first argument is the
148  // condition code) and cbz (where it is a register).
149  const auto &Desc = Info->get(Inst.getOpcode());
150  for (unsigned i = 0, e = Inst.getNumOperands(); i != e; i++) {
151  if (Desc.OpInfo[i].OperandType == MCOI::OPERAND_PCREL) {
152  int64_t Imm = Inst.getOperand(i).getImm() * 4;
153  Target = Addr + Imm;
154  return true;
155  }
156  }
157  return false;
158  }
159 
160  std::vector<std::pair<uint64_t, uint64_t>>
161  findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
162  uint64_t GotPltSectionVA,
163  const Triple &TargetTriple) const override {
164  // Do a lightweight parsing of PLT entries.
165  std::vector<std::pair<uint64_t, uint64_t>> Result;
166  for (uint64_t Byte = 0, End = PltContents.size(); Byte + 7 < End;
167  Byte += 4) {
168  uint32_t Insn = support::endian::read32le(PltContents.data() + Byte);
169  // Check for adrp.
170  if ((Insn & 0x9f000000) != 0x90000000)
171  continue;
172  uint64_t Imm = (((PltSectionVA + Byte) >> 12) << 12) +
173  (((Insn >> 29) & 3) << 12) + (((Insn >> 5) & 0x3ffff) << 14);
174  uint32_t Insn2 = support::endian::read32le(PltContents.data() + Byte + 4);
175  // Check for: ldr Xt, [Xn, #pimm].
176  if (Insn2 >> 22 == 0x3e5) {
177  Imm += ((Insn2 >> 10) & 0xfff) << 3;
178  Result.push_back(std::make_pair(PltSectionVA + Byte, Imm));
179  Byte += 4;
180  }
181  }
182  return Result;
183  }
184 };
185 
186 } // end anonymous namespace
187 
189  return new AArch64MCInstrAnalysis(Info);
190 }
191 
192 // Force static initialization.
193 extern "C" void LLVMInitializeAArch64TargetMC() {
195  &getTheARM64Target()}) {
196  // Register the MC asm info.
198 
199  // Register the MC instruction info.
201 
202  // Register the MC register info.
204 
205  // Register the MC subtarget info.
207 
208  // Register the MC instruction analyzer.
210 
211  // Register the MC Code Emitter
213 
214  // Register the obj streamers.
218 
219  // Register the obj target streamer.
222 
223  // Register the asm streamer.
226  // Register the MCInstPrinter.
228  }
229 
230  // Register the asm backend.
235 }
Target & getTheAArch64beTarget()
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static MCInstrAnalysis * createAArch64InstrAnalysis(const MCInstrInfo *Info)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:604
Target & getTheAArch64leTarget()
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target...
unsigned Reg
MCWinCOFFStreamer * createAArch64WinCOFFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool RelaxAll, bool IncrementalLinkerCompatible)
static MCInstrInfo * createAArch64MCInstrInfo()
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
MCAsmBackend * createAArch64leAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
Target & getTheARM64Target()
static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn)
Context object for machine code objects.
Definition: MCContext.h:63
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:601
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static MCSubtargetInfo * createAArch64MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
MCELFStreamer * createAArch64ELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool RelaxAll)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
int64_t getImm() const
Definition: MCInst.h:76
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
Streaming machine code generation interface.
Definition: MCStreamer.h:189
unsigned const MachineRegisterInfo * MRI
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
Definition: MCDwarf.h:461
static MCRegisterInfo * createAArch64MCRegisterInfo(const Triple &Triple)
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:609
static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
MCStreamer * createELFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
static MCAsmInfo * createAArch64MCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple)
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
unsigned getNumOperands() const
Definition: MCInst.h:184
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:614
static MCInstPrinter * createAArch64MCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target...
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
const T * data() const
Definition: ArrayRef.h:146
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCTargetStreamer * createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
MCCodeEmitter * createAArch64MCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
Target - Wrapper for Target specific information.
static MCStreamer * createWinCOFFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&Emitter, bool RelaxAll, bool IncrementalLinkerCompatible)
MCAsmBackend * createAArch64beAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
void initLLVMToCVRegMapping(MCRegisterInfo *MRI)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:40
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
void mapLLVMRegToCVReg(unsigned LLVMReg, int CVReg)
MCTargetStreamer * createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool isVerboseAsm)
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target. ...
uint32_t read32le(const void *P)
Definition: Endian.h:369
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:47
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MCStreamer * createMachOStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll, bool DWARFMustBeAtTheEnd, bool LabelSections=false)
unsigned getOpcode() const
Definition: MCInst.h:174
bool isWindowsMSVCEnvironment() const
Checks if the environment could be MSVC.
Definition: Triple.h:528
void LLVMInitializeAArch64TargetMC()