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, FeatureHasRAS
131 ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+ras";
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);
216 if (TheTriple.isOSBinFormatMachO())
217 MAI = new ARMMCAsmInfoDarwin();
219 MAI = new ARMELFMCAsmInfo();
221 unsigned Reg = MRI.getDwarfRegNum(ARM::SP, true);
222 MAI->addInitialFrameState(MCCFIInstruction::createDefCfa(0, Reg, 0));
227 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
229 CodeGenOpt::Level OL) {
230 MCCodeGenInfo *X = new MCCodeGenInfo();
231 if (RM == Reloc::Default) {
232 Triple TheTriple(TT);
233 // Default relocation model on Darwin is PIC, not DynamicNoPIC.
234 RM = TheTriple.isOSDarwin() ? Reloc::PIC_ : Reloc::DynamicNoPIC;
236 X->InitMCCodeGenInfo(RM, CM, OL);
240 // This is duplicated code. Refactor this.
241 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
242 MCContext &Ctx, MCAsmBackend &MAB,
244 MCCodeEmitter *Emitter,
245 const MCSubtargetInfo &STI,
248 Triple TheTriple(TT);
250 if (TheTriple.isOSBinFormatMachO())
251 return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
253 if (TheTriple.isOSWindows()) {
254 llvm_unreachable("ARM does not support Windows COFF format");
257 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
258 TheTriple.getArch() == Triple::thumb);
261 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
262 unsigned SyntaxVariant,
263 const MCAsmInfo &MAI,
264 const MCInstrInfo &MII,
265 const MCRegisterInfo &MRI,
266 const MCSubtargetInfo &STI) {
267 if (SyntaxVariant == 0)
268 return new ARMInstPrinter(MAI, MII, MRI, STI);
272 static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
274 Triple TheTriple(TT);
275 if (TheTriple.isOSBinFormatMachO())
276 return createARMMachORelocationInfo(Ctx);
277 // Default to the stock relocation info.
278 return llvm::createMCRelocationInfo(TT, Ctx);
283 class ARMMCInstrAnalysis : public MCInstrAnalysis {
285 ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
287 virtual bool isUnconditionalBranch(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::isUnconditionalBranch(Inst);
294 virtual bool isConditionalBranch(const MCInst &Inst) const {
295 // BCCs with the "always" predicate are unconditional branches.
296 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
298 return MCInstrAnalysis::isConditionalBranch(Inst);
301 bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
302 uint64_t Size, uint64_t &Target) const {
303 // We only handle PCRel branches for now.
304 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
307 int64_t Imm = Inst.getOperand(0).getImm();
308 // FIXME: This is not right for thumb.
309 Target = Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
316 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
317 return new ARMMCInstrAnalysis(Info);
320 // Force static initialization.
321 extern "C" void LLVMInitializeARMTargetMC() {
322 // Register the MC asm info.
323 RegisterMCAsmInfoFn A(TheARMTarget, createARMMCAsmInfo);
324 RegisterMCAsmInfoFn B(TheThumbTarget, createARMMCAsmInfo);
326 // Register the MC codegen info.
327 TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
328 TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
330 // Register the MC instruction info.
331 TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
332 TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
334 // Register the MC register info.
335 TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
336 TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
338 // Register the MC subtarget info.
339 TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
340 ARM_MC::createARMMCSubtargetInfo);
341 TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
342 ARM_MC::createARMMCSubtargetInfo);
344 // Register the MC instruction analyzer.
345 TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
346 createARMMCInstrAnalysis);
347 TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
348 createARMMCInstrAnalysis);
350 // Register the MC Code Emitter
351 TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
352 TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
354 // Register the asm backend.
355 TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
356 TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
358 // Register the object streamer.
359 TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
360 TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
362 // Register the asm streamer.
363 TargetRegistry::RegisterAsmStreamer(TheARMTarget, createMCAsmStreamer);
364 TargetRegistry::RegisterAsmStreamer(TheThumbTarget, createMCAsmStreamer);
366 // Register the MCInstPrinter.
367 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
368 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
370 // Register the MC relocation info.
371 TargetRegistry::RegisterMCRelocationInfo(TheARMTarget,
372 createARMMCRelocationInfo);
373 TargetRegistry::RegisterMCRelocationInfo(TheThumbTarget,
374 createARMMCRelocationInfo);