1 //===-- ARMMCTargetDesc.cpp - ARM Target Descriptions ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides ARM specific target descriptions.
12 //===----------------------------------------------------------------------===//
14 #include "ARMBaseInfo.h"
15 #include "ARMMCAsmInfo.h"
16 #include "ARMMCTargetDesc.h"
17 #include "InstPrinter/ARMInstPrinter.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCCodeGenInfo.h"
20 #include "llvm/MC/MCELFStreamer.h"
21 #include "llvm/MC/MCInstrAnalysis.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TargetRegistry.h"
30 #define GET_REGINFO_MC_DESC
31 #include "ARMGenRegisterInfo.inc"
33 static bool getMCRDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
35 if (STI.getFeatureBits() & llvm::ARM::HasV7Ops &&
36 (MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 15) &&
37 (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) &&
38 // Checks for the deprecated CP15ISB encoding:
39 // mcr p15, #0, rX, c7, c5, #4
40 (MI.getOperand(3).isImm() && MI.getOperand(3).getImm() == 7)) {
41 if ((MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 4)) {
42 if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 5) {
43 Info = "deprecated since v7, use 'isb'";
47 // Checks for the deprecated CP15DSB encoding:
48 // mcr p15, #0, rX, c7, c10, #4
49 if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10) {
50 Info = "deprecated since v7, use 'dsb'";
54 // Checks for the deprecated CP15DMB encoding:
55 // mcr p15, #0, rX, c7, c10, #5
56 if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10 &&
57 (MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 5)) {
58 Info = "deprecated since v7, use 'dmb'";
65 static bool getITDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
67 if (STI.getFeatureBits() & llvm::ARM::HasV8Ops &&
68 MI.getOperand(1).isImm() && MI.getOperand(1).getImm() != 8) {
69 Info = "applying IT instruction to more than one subsequent instruction is deprecated";
76 #define GET_INSTRINFO_MC_DESC
77 #include "ARMGenInstrInfo.inc"
79 #define GET_SUBTARGETINFO_MC_DESC
80 #include "ARMGenSubtargetInfo.inc"
83 std::string ARM_MC::ParseARMTriple(StringRef TT, StringRef CPU) {
86 // Set the boolean corresponding to the current target triple, or the default
87 // if one cannot be determined, to true.
88 unsigned Len = TT.size();
91 // FIXME: Enhance Triple helper class to extract ARM version.
92 bool isThumb = triple.getArch() == Triple::thumb;
93 if (Len >= 5 && TT.substr(0, 4) == "armv")
95 else if (Len >= 7 && TT.substr(0, 6) == "thumbv")
98 bool NoCPU = CPU == "generic" || CPU.empty();
99 std::string ARMArchFeature;
101 unsigned SubVer = TT[Idx];
104 // v8a: FeatureDB, FeatureFPARMv8, FeatureNEON, FeatureDSPThumb2, FeatureMP,
105 // FeatureHWDiv, FeatureHWDivARM, FeatureTrustZone, FeatureT2XtPk, FeatureCrypto, FeatureCRC
106 ARMArchFeature = "+v8,+db,+fp-armv8,+neon,+t2dsp,+mp,+hwdiv,+hwdiv-arm,+trustzone,+t2xtpk,+crypto,+crc";
108 // Use CPU to figure out the exact features
109 ARMArchFeature = "+v8";
110 } else if (SubVer == '7') {
111 if (Len >= Idx+2 && TT[Idx+1] == 'm') {
114 // v7m: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureMClass
115 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+mclass";
117 // Use CPU to figure out the exact features.
118 ARMArchFeature = "+v7";
119 } else if (Len >= Idx+3 && TT[Idx+1] == 'e'&& TT[Idx+2] == 'm') {
121 // v7em: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureDSPThumb2,
122 // FeatureT2XtPk, FeatureMClass
123 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+t2dsp,t2xtpk,+mclass";
125 // Use CPU to figure out the exact features.
126 ARMArchFeature = "+v7";
127 } else if (Len >= Idx+2 && TT[Idx+1] == 's') {
129 // v7s: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
131 ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+t2xtpk";
133 // Use CPU to figure out the exact features.
134 ARMArchFeature = "+v7";
136 // v7 CPUs have lots of different feature sets. If no CPU is specified,
137 // then assume v7a (e.g. cortex-a8) feature set. Otherwise, return
138 // the "minimum" feature set and use CPU string to figure out the exact
141 // v7a: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
142 ARMArchFeature = "+v7,+neon,+db,+t2dsp,+t2xtpk";
144 // Use CPU to figure out the exact features.
145 ARMArchFeature = "+v7";
147 } else if (SubVer == '6') {
148 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == '2')
149 ARMArchFeature = "+v6t2";
150 else if (Len >= Idx+2 && TT[Idx+1] == 'm') {
153 // v6m: FeatureNoARM, FeatureMClass
154 ARMArchFeature = "+v6m,+noarm,+mclass";
156 ARMArchFeature = "+v6";
158 ARMArchFeature = "+v6";
159 } else if (SubVer == '5') {
160 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == 'e')
161 ARMArchFeature = "+v5te";
163 ARMArchFeature = "+v5t";
164 } else if (SubVer == '4' && Len >= Idx+2 && TT[Idx+1] == 't')
165 ARMArchFeature = "+v4t";
169 if (ARMArchFeature.empty())
170 ARMArchFeature = "+thumb-mode";
172 ARMArchFeature += ",+thumb-mode";
175 if (triple.isOSNaCl()) {
176 if (ARMArchFeature.empty())
177 ARMArchFeature = "+nacl-trap";
179 ARMArchFeature += ",+nacl-trap";
182 return ARMArchFeature;
185 MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(StringRef TT, StringRef CPU,
187 std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
190 ArchFS = ArchFS + "," + FS.str();
195 MCSubtargetInfo *X = new MCSubtargetInfo();
196 InitARMMCSubtargetInfo(X, TT, CPU, ArchFS);
200 static MCInstrInfo *createARMMCInstrInfo() {
201 MCInstrInfo *X = new MCInstrInfo();
202 InitARMMCInstrInfo(X);
206 static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
207 MCRegisterInfo *X = new MCRegisterInfo();
208 InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
212 static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
213 Triple TheTriple(TT);
215 if (TheTriple.isOSDarwin())
216 return new ARMMCAsmInfoDarwin();
218 return new ARMELFMCAsmInfo();
221 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
223 CodeGenOpt::Level OL) {
224 MCCodeGenInfo *X = new MCCodeGenInfo();
225 if (RM == Reloc::Default) {
226 Triple TheTriple(TT);
227 // Default relocation model on Darwin is PIC, not DynamicNoPIC.
228 RM = TheTriple.isOSDarwin() ? Reloc::PIC_ : Reloc::DynamicNoPIC;
230 X->InitMCCodeGenInfo(RM, CM, OL);
234 // This is duplicated code. Refactor this.
235 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
236 MCContext &Ctx, MCAsmBackend &MAB,
238 MCCodeEmitter *Emitter,
241 Triple TheTriple(TT);
243 if (TheTriple.isOSDarwin())
244 return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
246 if (TheTriple.isOSWindows()) {
247 llvm_unreachable("ARM does not support Windows COFF format");
250 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
251 TheTriple.getArch() == Triple::thumb);
254 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
255 unsigned SyntaxVariant,
256 const MCAsmInfo &MAI,
257 const MCInstrInfo &MII,
258 const MCRegisterInfo &MRI,
259 const MCSubtargetInfo &STI) {
260 if (SyntaxVariant == 0)
261 return new ARMInstPrinter(MAI, MII, MRI, STI);
265 static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
267 Triple TheTriple(TT);
268 if (TheTriple.isOSBinFormatMachO())
269 return createARMMachORelocationInfo(Ctx);
270 // Default to the stock relocation info.
271 return llvm::createMCRelocationInfo(TT, Ctx);
276 class ARMMCInstrAnalysis : public MCInstrAnalysis {
278 ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
280 virtual bool isUnconditionalBranch(const MCInst &Inst) const {
281 // BCCs with the "always" predicate are unconditional branches.
282 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
284 return MCInstrAnalysis::isUnconditionalBranch(Inst);
287 virtual bool isConditionalBranch(const MCInst &Inst) const {
288 // BCCs with the "always" predicate are unconditional branches.
289 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
291 return MCInstrAnalysis::isConditionalBranch(Inst);
294 bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
295 uint64_t Size, uint64_t &Target) const {
296 // We only handle PCRel branches for now.
297 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
300 int64_t Imm = Inst.getOperand(0).getImm();
301 // FIXME: This is not right for thumb.
302 Target = Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
309 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
310 return new ARMMCInstrAnalysis(Info);
313 // Force static initialization.
314 extern "C" void LLVMInitializeARMTargetMC() {
315 // Register the MC asm info.
316 RegisterMCAsmInfoFn A(TheARMTarget, createARMMCAsmInfo);
317 RegisterMCAsmInfoFn B(TheThumbTarget, createARMMCAsmInfo);
319 // Register the MC codegen info.
320 TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
321 TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
323 // Register the MC instruction info.
324 TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
325 TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
327 // Register the MC register info.
328 TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
329 TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
331 // Register the MC subtarget info.
332 TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
333 ARM_MC::createARMMCSubtargetInfo);
334 TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
335 ARM_MC::createARMMCSubtargetInfo);
337 // Register the MC instruction analyzer.
338 TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
339 createARMMCInstrAnalysis);
340 TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
341 createARMMCInstrAnalysis);
343 // Register the MC Code Emitter
344 TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
345 TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
347 // Register the asm backend.
348 TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
349 TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
351 // Register the object streamer.
352 TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
353 TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
355 // Register the asm streamer.
356 TargetRegistry::RegisterAsmStreamer(TheARMTarget, createMCAsmStreamer);
357 TargetRegistry::RegisterAsmStreamer(TheThumbTarget, createMCAsmStreamer);
359 // Register the MCInstPrinter.
360 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
361 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
363 // Register the MC relocation info.
364 TargetRegistry::RegisterMCRelocationInfo(TheARMTarget,
365 createARMMCRelocationInfo);
366 TargetRegistry::RegisterMCRelocationInfo(TheThumbTarget,
367 createARMMCRelocationInfo);