400dab71486f6995ab2ac108ddbed9b6a1b7ced6
[oota-llvm.git] / lib / Target / ARM / MCTargetDesc / ARMMCTargetDesc.cpp
1 //===-- ARMMCTargetDesc.cpp - ARM Target Descriptions ---------------------===//
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 ARM specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
27
28 using namespace llvm;
29
30 #define GET_REGINFO_MC_DESC
31 #include "ARMGenRegisterInfo.inc"
32
33 static bool getMCRDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
34                                   std::string &Info) {
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'";
44         return true;
45       }
46
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'";
51         return true;
52       }
53     }
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'";
59       return true;
60     }
61   }
62   return false;
63 }
64
65 static bool getITDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
66                                   std::string &Info) {
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";
70     return true;
71   }
72
73   return false;
74 }
75
76 #define GET_INSTRINFO_MC_DESC
77 #include "ARMGenInstrInfo.inc"
78
79 #define GET_SUBTARGETINFO_MC_DESC
80 #include "ARMGenSubtargetInfo.inc"
81
82
83 std::string ARM_MC::ParseARMTriple(StringRef TT, StringRef CPU) {
84   Triple triple(TT);
85
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();
89   unsigned Idx = 0;
90
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")
94     Idx = 4;
95   else if (Len >= 7 && TT.substr(0, 6) == "thumbv")
96     Idx = 6;
97
98   bool NoCPU = CPU == "generic" || CPU.empty();
99   std::string ARMArchFeature;
100   if (Idx) {
101     unsigned SubVer = TT[Idx];
102     if (SubVer == '8') {
103       if (NoCPU)
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";
107       else
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') {
112         isThumb = true;
113         if (NoCPU)
114           // v7m: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureMClass
115           ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+mclass";
116         else
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') {
120         if (NoCPU)
121           // v7em: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureDSPThumb2,
122           //       FeatureT2XtPk, FeatureMClass
123           ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+t2dsp,t2xtpk,+mclass";
124         else
125           // Use CPU to figure out the exact features.
126           ARMArchFeature = "+v7";
127       } else if (Len >= Idx+2 && TT[Idx+1] == 's') {
128         if (NoCPU)
129           // v7s: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
130           //      Swift
131           ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+t2xtpk";
132         else
133           // Use CPU to figure out the exact features.
134           ARMArchFeature = "+v7";
135       } else {
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
139         // features.
140         if (NoCPU)
141           // v7a: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
142           ARMArchFeature = "+v7,+neon,+db,+t2dsp,+t2xtpk";
143         else
144           // Use CPU to figure out the exact features.
145           ARMArchFeature = "+v7";
146       }
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') {
151         isThumb = true;
152         if (NoCPU)
153           // v6m: FeatureNoARM, FeatureMClass
154           ARMArchFeature = "+v6m,+noarm,+mclass";
155         else
156           ARMArchFeature = "+v6";
157       } else
158         ARMArchFeature = "+v6";
159     } else if (SubVer == '5') {
160       if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == 'e')
161         ARMArchFeature = "+v5te";
162       else
163         ARMArchFeature = "+v5t";
164     } else if (SubVer == '4' && Len >= Idx+2 && TT[Idx+1] == 't')
165       ARMArchFeature = "+v4t";
166   }
167
168   if (isThumb) {
169     if (ARMArchFeature.empty())
170       ARMArchFeature = "+thumb-mode";
171     else
172       ARMArchFeature += ",+thumb-mode";
173   }
174
175   if (triple.isOSNaCl()) {
176     if (ARMArchFeature.empty())
177       ARMArchFeature = "+nacl-trap";
178     else
179       ARMArchFeature += ",+nacl-trap";
180   }
181
182   return ARMArchFeature;
183 }
184
185 MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(StringRef TT, StringRef CPU,
186                                                   StringRef FS) {
187   std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
188   if (!FS.empty()) {
189     if (!ArchFS.empty())
190       ArchFS = ArchFS + "," + FS.str();
191     else
192       ArchFS = FS;
193   }
194
195   MCSubtargetInfo *X = new MCSubtargetInfo();
196   InitARMMCSubtargetInfo(X, TT, CPU, ArchFS);
197   return X;
198 }
199
200 static MCInstrInfo *createARMMCInstrInfo() {
201   MCInstrInfo *X = new MCInstrInfo();
202   InitARMMCInstrInfo(X);
203   return X;
204 }
205
206 static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
207   MCRegisterInfo *X = new MCRegisterInfo();
208   InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
209   return X;
210 }
211
212 static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
213   Triple TheTriple(TT);
214
215   if (TheTriple.isOSDarwin())
216     return new ARMMCAsmInfoDarwin();
217
218   return new ARMELFMCAsmInfo();
219 }
220
221 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
222                                              CodeModel::Model CM,
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;
229   }
230   X->InitMCCodeGenInfo(RM, CM, OL);
231   return X;
232 }
233
234 // This is duplicated code. Refactor this.
235 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
236                                     MCContext &Ctx, MCAsmBackend &MAB,
237                                     raw_ostream &OS,
238                                     MCCodeEmitter *Emitter,
239                                     bool RelaxAll,
240                                     bool NoExecStack) {
241   Triple TheTriple(TT);
242
243   if (TheTriple.isOSDarwin())
244     return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
245
246   if (TheTriple.isOSWindows()) {
247     llvm_unreachable("ARM does not support Windows COFF format");
248   }
249
250   return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
251                               TheTriple.getArch() == Triple::thumb);
252 }
253
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);
262   return 0;
263 }
264
265 static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
266                                                    MCContext &Ctx) {
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);
272 }
273
274 namespace {
275
276 class ARMMCInstrAnalysis : public MCInstrAnalysis {
277 public:
278   ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
279
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)
283       return true;
284     return MCInstrAnalysis::isUnconditionalBranch(Inst);
285   }
286
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)
290       return false;
291     return MCInstrAnalysis::isConditionalBranch(Inst);
292   }
293
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)
298       return false;
299
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.
303     return true;
304   }
305 };
306
307 }
308
309 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
310   return new ARMMCInstrAnalysis(Info);
311 }
312
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);
318
319   // Register the MC codegen info.
320   TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
321   TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
322
323   // Register the MC instruction info.
324   TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
325   TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
326
327   // Register the MC register info.
328   TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
329   TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
330
331   // Register the MC subtarget info.
332   TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
333                                           ARM_MC::createARMMCSubtargetInfo);
334   TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
335                                           ARM_MC::createARMMCSubtargetInfo);
336
337   // Register the MC instruction analyzer.
338   TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
339                                           createARMMCInstrAnalysis);
340   TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
341                                           createARMMCInstrAnalysis);
342
343   // Register the MC Code Emitter
344   TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
345   TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
346
347   // Register the asm backend.
348   TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
349   TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
350
351   // Register the object streamer.
352   TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
353   TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
354
355   // Register the asm streamer.
356   TargetRegistry::RegisterAsmStreamer(TheARMTarget, createMCAsmStreamer);
357   TargetRegistry::RegisterAsmStreamer(TheThumbTarget, createMCAsmStreamer);
358
359   // Register the MCInstPrinter.
360   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
361   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
362
363   // Register the MC relocation info.
364   TargetRegistry::RegisterMCRelocationInfo(TheARMTarget,
365                                            createARMMCRelocationInfo);
366   TargetRegistry::RegisterMCRelocationInfo(TheThumbTarget,
367                                            createARMMCRelocationInfo);
368 }