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.
93 if (Len >= 5 && TT.substr(0, 4) == "armv")
95 else if (Len >= 6 && TT.substr(0, 5) == "thumb") {
97 if (Len >= 7 && TT[5] == 'v')
101 bool NoCPU = CPU == "generic" || CPU.empty();
102 std::string ARMArchFeature;
104 unsigned SubVer = TT[Idx];
106 // FIXME: Parse v8 features
107 ARMArchFeature = "+v8,+db";
108 } else if (SubVer == '7') {
109 if (Len >= Idx+2 && TT[Idx+1] == 'm') {
112 // v7m: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureMClass
113 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+mclass";
115 // Use CPU to figure out the exact features.
116 ARMArchFeature = "+v7";
117 } else if (Len >= Idx+3 && TT[Idx+1] == 'e'&& TT[Idx+2] == 'm') {
119 // v7em: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureDSPThumb2,
120 // FeatureT2XtPk, FeatureMClass
121 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+t2dsp,t2xtpk,+mclass";
123 // Use CPU to figure out the exact features.
124 ARMArchFeature = "+v7";
125 } else if (Len >= Idx+2 && TT[Idx+1] == 's') {
127 // v7s: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
129 ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+t2xtpk";
131 // Use CPU to figure out the exact features.
132 ARMArchFeature = "+v7";
134 // v7 CPUs have lots of different feature sets. If no CPU is specified,
135 // then assume v7a (e.g. cortex-a8) feature set. Otherwise, return
136 // the "minimum" feature set and use CPU string to figure out the exact
139 // v7a: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
140 ARMArchFeature = "+v7,+neon,+db,+t2dsp,+t2xtpk";
142 // Use CPU to figure out the exact features.
143 ARMArchFeature = "+v7";
145 } else if (SubVer == '6') {
146 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == '2')
147 ARMArchFeature = "+v6t2";
148 else if (Len >= Idx+2 && TT[Idx+1] == 'm') {
151 // v6m: FeatureNoARM, FeatureMClass
152 ARMArchFeature = "+v6m,+noarm,+mclass";
154 ARMArchFeature = "+v6";
156 ARMArchFeature = "+v6";
157 } else if (SubVer == '5') {
158 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == 'e')
159 ARMArchFeature = "+v5te";
161 ARMArchFeature = "+v5t";
162 } else if (SubVer == '4' && Len >= Idx+2 && TT[Idx+1] == 't')
163 ARMArchFeature = "+v4t";
167 if (ARMArchFeature.empty())
168 ARMArchFeature = "+thumb-mode";
170 ARMArchFeature += ",+thumb-mode";
173 if (triple.isOSNaCl()) {
174 if (ARMArchFeature.empty())
175 ARMArchFeature = "+nacl-trap";
177 ARMArchFeature += ",+nacl-trap";
180 return ARMArchFeature;
183 MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(StringRef TT, StringRef CPU,
185 std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
188 ArchFS = ArchFS + "," + FS.str();
193 MCSubtargetInfo *X = new MCSubtargetInfo();
194 InitARMMCSubtargetInfo(X, TT, CPU, ArchFS);
198 static MCInstrInfo *createARMMCInstrInfo() {
199 MCInstrInfo *X = new MCInstrInfo();
200 InitARMMCInstrInfo(X);
204 static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
205 MCRegisterInfo *X = new MCRegisterInfo();
206 InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
210 static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
211 Triple TheTriple(TT);
213 if (TheTriple.isOSDarwin())
214 return new ARMMCAsmInfoDarwin();
216 return new ARMELFMCAsmInfo();
219 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
221 CodeGenOpt::Level OL) {
222 MCCodeGenInfo *X = new MCCodeGenInfo();
223 if (RM == Reloc::Default) {
224 Triple TheTriple(TT);
225 // Default relocation model on Darwin is PIC, not DynamicNoPIC.
226 RM = TheTriple.isOSDarwin() ? Reloc::PIC_ : Reloc::DynamicNoPIC;
228 X->InitMCCodeGenInfo(RM, CM, OL);
232 // This is duplicated code. Refactor this.
233 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
234 MCContext &Ctx, MCAsmBackend &MAB,
236 MCCodeEmitter *Emitter,
239 Triple TheTriple(TT);
241 if (TheTriple.isOSDarwin())
242 return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
244 if (TheTriple.isOSWindows()) {
245 llvm_unreachable("ARM does not support Windows COFF format");
248 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
249 TheTriple.getArch() == Triple::thumb);
252 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
253 unsigned SyntaxVariant,
254 const MCAsmInfo &MAI,
255 const MCInstrInfo &MII,
256 const MCRegisterInfo &MRI,
257 const MCSubtargetInfo &STI) {
258 if (SyntaxVariant == 0)
259 return new ARMInstPrinter(MAI, MII, MRI, STI);
263 static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
265 Triple TheTriple(TT);
266 if (TheTriple.isEnvironmentMachO())
267 return createARMMachORelocationInfo(Ctx);
268 // Default to the stock relocation info.
269 return llvm::createMCRelocationInfo(TT, Ctx);
274 class ARMMCInstrAnalysis : public MCInstrAnalysis {
276 ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
278 virtual bool isUnconditionalBranch(const MCInst &Inst) const {
279 // BCCs with the "always" predicate are unconditional branches.
280 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
282 return MCInstrAnalysis::isUnconditionalBranch(Inst);
285 virtual bool isConditionalBranch(const MCInst &Inst) const {
286 // BCCs with the "always" predicate are unconditional branches.
287 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
289 return MCInstrAnalysis::isConditionalBranch(Inst);
292 bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
293 uint64_t Size, uint64_t &Target) const {
294 // We only handle PCRel branches for now.
295 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
298 int64_t Imm = Inst.getOperand(0).getImm();
299 // FIXME: This is not right for thumb.
300 Target = Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
307 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
308 return new ARMMCInstrAnalysis(Info);
311 // Force static initialization.
312 extern "C" void LLVMInitializeARMTargetMC() {
313 // Register the MC asm info.
314 RegisterMCAsmInfoFn A(TheARMTarget, createARMMCAsmInfo);
315 RegisterMCAsmInfoFn B(TheThumbTarget, createARMMCAsmInfo);
317 // Register the MC codegen info.
318 TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
319 TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
321 // Register the MC instruction info.
322 TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
323 TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
325 // Register the MC register info.
326 TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
327 TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
329 // Register the MC subtarget info.
330 TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
331 ARM_MC::createARMMCSubtargetInfo);
332 TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
333 ARM_MC::createARMMCSubtargetInfo);
335 // Register the MC instruction analyzer.
336 TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
337 createARMMCInstrAnalysis);
338 TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
339 createARMMCInstrAnalysis);
341 // Register the MC Code Emitter
342 TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
343 TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
345 // Register the asm backend.
346 TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
347 TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
349 // Register the object streamer.
350 TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
351 TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
353 // Register the asm streamer.
354 TargetRegistry::RegisterAsmStreamer(TheARMTarget, createMCAsmStreamer);
355 TargetRegistry::RegisterAsmStreamer(TheThumbTarget, createMCAsmStreamer);
357 // Register the MCInstPrinter.
358 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
359 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
361 // Register the MC relocation info.
362 TargetRegistry::RegisterMCRelocationInfo(TheARMTarget,
363 createARMMCRelocationInfo);
364 TargetRegistry::RegisterMCRelocationInfo(TheThumbTarget,
365 createARMMCRelocationInfo);