ARM MC: Fix the initial DWARF CFI unwind info at the start of a function
[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, FeatureHasRAS
130           //      Swift
131           ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+ras";
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   MCAsmInfo *MAI;
216   if (TheTriple.isOSBinFormatMachO())
217     MAI = new ARMMCAsmInfoDarwin();
218   else
219     MAI = new ARMELFMCAsmInfo();
220
221   unsigned Reg = MRI.getDwarfRegNum(ARM::SP, true);
222   MAI->addInitialFrameState(MCCFIInstruction::createDefCfa(0, Reg, 0));
223
224   return MAI;
225 }
226
227 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
228                                              CodeModel::Model CM,
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;
235   }
236   X->InitMCCodeGenInfo(RM, CM, OL);
237   return X;
238 }
239
240 // This is duplicated code. Refactor this.
241 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
242                                     MCContext &Ctx, MCAsmBackend &MAB,
243                                     raw_ostream &OS,
244                                     MCCodeEmitter *Emitter,
245                                     const MCSubtargetInfo &STI,
246                                     bool RelaxAll,
247                                     bool NoExecStack) {
248   Triple TheTriple(TT);
249
250   if (TheTriple.isOSBinFormatMachO())
251     return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
252
253   if (TheTriple.isOSWindows()) {
254     llvm_unreachable("ARM does not support Windows COFF format");
255   }
256
257   return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
258                               TheTriple.getArch() == Triple::thumb);
259 }
260
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);
269   return 0;
270 }
271
272 static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
273                                                    MCContext &Ctx) {
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);
279 }
280
281 namespace {
282
283 class ARMMCInstrAnalysis : public MCInstrAnalysis {
284 public:
285   ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
286
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)
290       return true;
291     return MCInstrAnalysis::isUnconditionalBranch(Inst);
292   }
293
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)
297       return false;
298     return MCInstrAnalysis::isConditionalBranch(Inst);
299   }
300
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)
305       return false;
306
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.
310     return true;
311   }
312 };
313
314 }
315
316 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
317   return new ARMMCInstrAnalysis(Info);
318 }
319
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);
325
326   // Register the MC codegen info.
327   TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
328   TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
329
330   // Register the MC instruction info.
331   TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
332   TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
333
334   // Register the MC register info.
335   TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
336   TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
337
338   // Register the MC subtarget info.
339   TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
340                                           ARM_MC::createARMMCSubtargetInfo);
341   TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
342                                           ARM_MC::createARMMCSubtargetInfo);
343
344   // Register the MC instruction analyzer.
345   TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
346                                           createARMMCInstrAnalysis);
347   TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
348                                           createARMMCInstrAnalysis);
349
350   // Register the MC Code Emitter
351   TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
352   TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
353
354   // Register the asm backend.
355   TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
356   TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
357
358   // Register the object streamer.
359   TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
360   TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
361
362   // Register the asm streamer.
363   TargetRegistry::RegisterAsmStreamer(TheARMTarget, createMCAsmStreamer);
364   TargetRegistry::RegisterAsmStreamer(TheThumbTarget, createMCAsmStreamer);
365
366   // Register the MCInstPrinter.
367   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
368   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
369
370   // Register the MC relocation info.
371   TargetRegistry::RegisterMCRelocationInfo(TheARMTarget,
372                                            createARMMCRelocationInfo);
373   TargetRegistry::RegisterMCRelocationInfo(TheThumbTarget,
374                                            createARMMCRelocationInfo);
375 }