1 //===-- ARMMCTargetDesc.cpp - ARM Target Descriptions ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides ARM specific target descriptions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ARMMCTargetDesc.h"
14 #include "ARMBaseInfo.h"
15 #include "ARMInstPrinter.h"
16 #include "ARMMCAsmInfo.h"
17 #include "TargetInfo/ARMTargetInfo.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCELFStreamer.h"
22 #include "llvm/MC/MCInstrAnalysis.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"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/TargetParser.h"
30 #include "llvm/Support/TargetRegistry.h"
31 
32 using namespace llvm;
33 
34 #define GET_REGINFO_MC_DESC
35 #include "ARMGenRegisterInfo.inc"
36 
37 static bool getMCRDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
38                                   std::string &Info) {
39   if (STI.getFeatureBits()[llvm::ARM::HasV7Ops] &&
40       (MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 15) &&
41       (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) &&
42       // Checks for the deprecated CP15ISB encoding:
43       // mcr p15, #0, rX, c7, c5, #4
44       (MI.getOperand(3).isImm() && MI.getOperand(3).getImm() == 7)) {
45     if ((MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 4)) {
46       if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 5) {
47         Info = "deprecated since v7, use 'isb'";
48         return true;
49       }
50 
51       // Checks for the deprecated CP15DSB encoding:
52       // mcr p15, #0, rX, c7, c10, #4
53       if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10) {
54         Info = "deprecated since v7, use 'dsb'";
55         return true;
56       }
57     }
58     // Checks for the deprecated CP15DMB encoding:
59     // mcr p15, #0, rX, c7, c10, #5
60     if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10 &&
61         (MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 5)) {
62       Info = "deprecated since v7, use 'dmb'";
63       return true;
64     }
65   }
66   return false;
67 }
68 
69 static bool getITDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
70                                  std::string &Info) {
71   if (STI.getFeatureBits()[llvm::ARM::HasV8Ops] && MI.getOperand(1).isImm() &&
72       MI.getOperand(1).getImm() != 8) {
73     Info = "applying IT instruction to more than one subsequent instruction is "
74            "deprecated";
75     return true;
76   }
77 
78   return false;
79 }
80 
81 static bool getARMStoreDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
82                                        std::string &Info) {
83   assert(!STI.getFeatureBits()[llvm::ARM::ModeThumb] &&
84          "cannot predicate thumb instructions");
85 
86   assert(MI.getNumOperands() >= 4 && "expected >= 4 arguments");
87   for (unsigned OI = 4, OE = MI.getNumOperands(); OI < OE; ++OI) {
88     assert(MI.getOperand(OI).isReg() && "expected register");
89     if (MI.getOperand(OI).getReg() == ARM::SP ||
90         MI.getOperand(OI).getReg() == ARM::PC) {
91       Info = "use of SP or PC in the list is deprecated";
92       return true;
93     }
94   }
95   return false;
96 }
97 
98 static bool getARMLoadDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
99                                       std::string &Info) {
100   assert(!STI.getFeatureBits()[llvm::ARM::ModeThumb] &&
101          "cannot predicate thumb instructions");
102 
103   assert(MI.getNumOperands() >= 4 && "expected >= 4 arguments");
104   bool ListContainsPC = false, ListContainsLR = false;
105   for (unsigned OI = 4, OE = MI.getNumOperands(); OI < OE; ++OI) {
106     assert(MI.getOperand(OI).isReg() && "expected register");
107     switch (MI.getOperand(OI).getReg()) {
108     default:
109       break;
110     case ARM::LR:
111       ListContainsLR = true;
112       break;
113     case ARM::PC:
114       ListContainsPC = true;
115       break;
116     case ARM::SP:
117       Info = "use of SP in the list is deprecated";
118       return true;
119     }
120   }
121 
122   if (ListContainsPC && ListContainsLR) {
123     Info = "use of LR and PC simultaneously in the list is deprecated";
124     return true;
125   }
126 
127   return false;
128 }
129 
130 #define GET_INSTRINFO_MC_DESC
131 #include "ARMGenInstrInfo.inc"
132 
133 #define GET_SUBTARGETINFO_MC_DESC
134 #include "ARMGenSubtargetInfo.inc"
135 
136 std::string ARM_MC::ParseARMTriple(const Triple &TT, StringRef CPU) {
137   std::string ARMArchFeature;
138 
139   ARM::ArchKind ArchID = ARM::parseArch(TT.getArchName());
140   if (ArchID != ARM::ArchKind::INVALID &&  (CPU.empty() || CPU == "generic"))
141     ARMArchFeature = (ARMArchFeature + "+" + ARM::getArchName(ArchID)).str();
142 
143   if (TT.isThumb()) {
144     if (!ARMArchFeature.empty())
145       ARMArchFeature += ",";
146     ARMArchFeature += "+thumb-mode,+v4t";
147   }
148 
149   if (TT.isOSNaCl()) {
150     if (!ARMArchFeature.empty())
151       ARMArchFeature += ",";
152     ARMArchFeature += "+nacl-trap";
153   }
154 
155   if (TT.isOSWindows()) {
156     if (!ARMArchFeature.empty())
157       ARMArchFeature += ",";
158     ARMArchFeature += "+noarm";
159   }
160 
161   return ARMArchFeature;
162 }
163 
164 MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(const Triple &TT,
165                                                   StringRef CPU, StringRef FS) {
166   std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
167   if (!FS.empty()) {
168     if (!ArchFS.empty())
169       ArchFS = (Twine(ArchFS) + "," + FS).str();
170     else
171       ArchFS = std::string(FS);
172   }
173 
174   return createARMMCSubtargetInfoImpl(TT, CPU, ArchFS);
175 }
176 
177 static MCInstrInfo *createARMMCInstrInfo() {
178   MCInstrInfo *X = new MCInstrInfo();
179   InitARMMCInstrInfo(X);
180   return X;
181 }
182 
183 static MCRegisterInfo *createARMMCRegisterInfo(const Triple &Triple) {
184   MCRegisterInfo *X = new MCRegisterInfo();
185   InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
186   return X;
187 }
188 
189 static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI,
190                                      const Triple &TheTriple,
191                                      const MCTargetOptions &Options) {
192   MCAsmInfo *MAI;
193   if (TheTriple.isOSDarwin() || TheTriple.isOSBinFormatMachO())
194     MAI = new ARMMCAsmInfoDarwin(TheTriple);
195   else if (TheTriple.isWindowsMSVCEnvironment())
196     MAI = new ARMCOFFMCAsmInfoMicrosoft();
197   else if (TheTriple.isOSWindows())
198     MAI = new ARMCOFFMCAsmInfoGNU();
199   else
200     MAI = new ARMELFMCAsmInfo(TheTriple);
201 
202   unsigned Reg = MRI.getDwarfRegNum(ARM::SP, true);
203   MAI->addInitialFrameState(MCCFIInstruction::cfiDefCfa(nullptr, Reg, 0));
204 
205   return MAI;
206 }
207 
208 static MCStreamer *createELFStreamer(const Triple &T, MCContext &Ctx,
209                                      std::unique_ptr<MCAsmBackend> &&MAB,
210                                      std::unique_ptr<MCObjectWriter> &&OW,
211                                      std::unique_ptr<MCCodeEmitter> &&Emitter,
212                                      bool RelaxAll) {
213   return createARMELFStreamer(
214       Ctx, std::move(MAB), std::move(OW), std::move(Emitter), false,
215       (T.getArch() == Triple::thumb || T.getArch() == Triple::thumbeb),
216       T.isAndroid());
217 }
218 
219 static MCStreamer *
220 createARMMachOStreamer(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&MAB,
221                        std::unique_ptr<MCObjectWriter> &&OW,
222                        std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
223                        bool DWARFMustBeAtTheEnd) {
224   return createMachOStreamer(Ctx, std::move(MAB), std::move(OW),
225                              std::move(Emitter), false, DWARFMustBeAtTheEnd);
226 }
227 
228 static MCInstPrinter *createARMMCInstPrinter(const Triple &T,
229                                              unsigned SyntaxVariant,
230                                              const MCAsmInfo &MAI,
231                                              const MCInstrInfo &MII,
232                                              const MCRegisterInfo &MRI) {
233   if (SyntaxVariant == 0)
234     return new ARMInstPrinter(MAI, MII, MRI);
235   return nullptr;
236 }
237 
238 static MCRelocationInfo *createARMMCRelocationInfo(const Triple &TT,
239                                                    MCContext &Ctx) {
240   if (TT.isOSBinFormatMachO())
241     return createARMMachORelocationInfo(Ctx);
242   // Default to the stock relocation info.
243   return llvm::createMCRelocationInfo(TT, Ctx);
244 }
245 
246 namespace {
247 
248 class ARMMCInstrAnalysis : public MCInstrAnalysis {
249 public:
250   ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
251 
252   bool isUnconditionalBranch(const MCInst &Inst) const override {
253     // BCCs with the "always" predicate are unconditional branches.
254     if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
255       return true;
256     return MCInstrAnalysis::isUnconditionalBranch(Inst);
257   }
258 
259   bool isConditionalBranch(const MCInst &Inst) const override {
260     // BCCs with the "always" predicate are unconditional branches.
261     if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
262       return false;
263     return MCInstrAnalysis::isConditionalBranch(Inst);
264   }
265 
266   bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
267                       uint64_t Size, uint64_t &Target) const override {
268     // We only handle PCRel branches for now.
269     if (Inst.getNumOperands() == 0 ||
270         Info->get(Inst.getOpcode()).OpInfo[0].OperandType !=
271             MCOI::OPERAND_PCREL)
272       return false;
273 
274     int64_t Imm = Inst.getOperand(0).getImm();
275     Target = Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
276     return true;
277   }
278 };
279 
280 class ThumbMCInstrAnalysis : public ARMMCInstrAnalysis {
281 public:
282   ThumbMCInstrAnalysis(const MCInstrInfo *Info) : ARMMCInstrAnalysis(Info) {}
283 
284   bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
285                       uint64_t &Target) const override {
286     unsigned OpId;
287     switch (Inst.getOpcode()) {
288     default:
289       OpId = 0;
290       if (Inst.getNumOperands() == 0)
291         return false;
292       break;
293     case ARM::MVE_WLSTP_8:
294     case ARM::MVE_WLSTP_16:
295     case ARM::MVE_WLSTP_32:
296     case ARM::MVE_WLSTP_64:
297     case ARM::t2WLS:
298     case ARM::MVE_LETP:
299     case ARM::t2LEUpdate:
300       OpId = 2;
301       break;
302     case ARM::t2LE:
303       OpId = 1;
304       break;
305     }
306 
307     // We only handle PCRel branches for now.
308     if (Info->get(Inst.getOpcode()).OpInfo[OpId].OperandType !=
309         MCOI::OPERAND_PCREL)
310       return false;
311 
312     // In Thumb mode the PC is always off by 4 bytes.
313     Target = Addr + Inst.getOperand(OpId).getImm() + 4;
314     return true;
315   }
316 };
317 
318 }
319 
320 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
321   return new ARMMCInstrAnalysis(Info);
322 }
323 
324 static MCInstrAnalysis *createThumbMCInstrAnalysis(const MCInstrInfo *Info) {
325   return new ThumbMCInstrAnalysis(Info);
326 }
327 
328 bool ARM::isCDECoproc(size_t Coproc, const MCSubtargetInfo &STI) {
329   // Unfortunately we don't have ARMTargetInfo in the disassembler, so we have
330   // to rely on feature bits.
331   if (Coproc >= 8)
332     return false;
333   return STI.getFeatureBits()[ARM::FeatureCoprocCDE0 + Coproc];
334 }
335 
336 // Force static initialization.
337 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMTargetMC() {
338   for (Target *T : {&getTheARMLETarget(), &getTheARMBETarget(),
339                     &getTheThumbLETarget(), &getTheThumbBETarget()}) {
340     // Register the MC asm info.
341     RegisterMCAsmInfoFn X(*T, createARMMCAsmInfo);
342 
343     // Register the MC instruction info.
344     TargetRegistry::RegisterMCInstrInfo(*T, createARMMCInstrInfo);
345 
346     // Register the MC register info.
347     TargetRegistry::RegisterMCRegInfo(*T, createARMMCRegisterInfo);
348 
349     // Register the MC subtarget info.
350     TargetRegistry::RegisterMCSubtargetInfo(*T,
351                                             ARM_MC::createARMMCSubtargetInfo);
352 
353     TargetRegistry::RegisterELFStreamer(*T, createELFStreamer);
354     TargetRegistry::RegisterCOFFStreamer(*T, createARMWinCOFFStreamer);
355     TargetRegistry::RegisterMachOStreamer(*T, createARMMachOStreamer);
356 
357     // Register the obj target streamer.
358     TargetRegistry::RegisterObjectTargetStreamer(*T,
359                                                  createARMObjectTargetStreamer);
360 
361     // Register the asm streamer.
362     TargetRegistry::RegisterAsmTargetStreamer(*T, createARMTargetAsmStreamer);
363 
364     // Register the null TargetStreamer.
365     TargetRegistry::RegisterNullTargetStreamer(*T, createARMNullTargetStreamer);
366 
367     // Register the MCInstPrinter.
368     TargetRegistry::RegisterMCInstPrinter(*T, createARMMCInstPrinter);
369 
370     // Register the MC relocation info.
371     TargetRegistry::RegisterMCRelocationInfo(*T, createARMMCRelocationInfo);
372   }
373 
374   // Register the MC instruction analyzer.
375   for (Target *T : {&getTheARMLETarget(), &getTheARMBETarget()})
376     TargetRegistry::RegisterMCInstrAnalysis(*T, createARMMCInstrAnalysis);
377   for (Target *T : {&getTheThumbLETarget(), &getTheThumbBETarget()})
378     TargetRegistry::RegisterMCInstrAnalysis(*T, createThumbMCInstrAnalysis);
379 
380   for (Target *T : {&getTheARMLETarget(), &getTheThumbLETarget()}) {
381     TargetRegistry::RegisterMCCodeEmitter(*T, createARMLEMCCodeEmitter);
382     TargetRegistry::RegisterMCAsmBackend(*T, createARMLEAsmBackend);
383   }
384   for (Target *T : {&getTheARMBETarget(), &getTheThumbBETarget()}) {
385     TargetRegistry::RegisterMCCodeEmitter(*T, createARMBEMCCodeEmitter);
386     TargetRegistry::RegisterMCAsmBackend(*T, createARMBEAsmBackend);
387   }
388 }
389