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 "ARMMCTargetDesc.h"
15 #include "ARMELFStreamer.h"
16 #include "ARMMCAsmInfo.h"
17 #include "ARMBaseInfo.h"
18 #include "ARMMCAsmInfo.h"
19 #include "InstPrinter/ARMInstPrinter.h"
20 #include "llvm/MC/MCCodeGenInfo.h"
21 #include "llvm/MC/MCInstrAnalysis.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/TargetRegistry.h"
29 #define GET_REGINFO_MC_DESC
30 #include "ARMGenRegisterInfo.inc"
32 #define GET_INSTRINFO_MC_DESC
33 #include "ARMGenInstrInfo.inc"
35 #define GET_SUBTARGETINFO_MC_DESC
36 #include "ARMGenSubtargetInfo.inc"
40 std::string ARM_MC::ParseARMTriple(StringRef TT, StringRef CPU) {
41 // Set the boolean corresponding to the current target triple, or the default
42 // if one cannot be determined, to true.
43 unsigned Len = TT.size();
46 // FIXME: Enhance Triple helper class to extract ARM version.
48 if (Len >= 5 && TT.substr(0, 4) == "armv")
50 else if (Len >= 6 && TT.substr(0, 5) == "thumb") {
52 if (Len >= 7 && TT[5] == 'v')
56 bool NoCPU = CPU == "generic" || CPU.empty();
57 std::string ARMArchFeature;
59 unsigned SubVer = TT[Idx];
60 if (SubVer >= '7' && SubVer <= '9') {
61 if (Len >= Idx+2 && TT[Idx+1] == 'm') {
63 // v7m: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureMClass
64 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+mclass";
66 // Use CPU to figure out the exact features.
67 ARMArchFeature = "+v7";
68 } else if (Len >= Idx+3 && TT[Idx+1] == 'e'&& TT[Idx+2] == 'm') {
70 // v7em: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureDSPThumb2,
71 // FeatureT2XtPk, FeatureMClass
72 ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+t2dsp,t2xtpk,+mclass";
74 // Use CPU to figure out the exact features.
75 ARMArchFeature = "+v7";
76 } else if (Len >= Idx+2 && TT[Idx+1] == 's') {
78 // v7s: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
80 ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+t2xtpk";
82 // Use CPU to figure out the exact features.
83 ARMArchFeature = "+v7";
85 // v7 CPUs have lots of different feature sets. If no CPU is specified,
86 // then assume v7a (e.g. cortex-a8) feature set. Otherwise, return
87 // the "minimum" feature set and use CPU string to figure out the exact
90 // v7a: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
91 ARMArchFeature = "+v7,+neon,+db,+t2dsp,+t2xtpk";
93 // Use CPU to figure out the exact features.
94 ARMArchFeature = "+v7";
96 } else if (SubVer == '6') {
97 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == '2')
98 ARMArchFeature = "+v6t2";
99 else if (Len >= Idx+2 && TT[Idx+1] == 'm') {
101 // v6m: FeatureNoARM, FeatureMClass
102 ARMArchFeature = "+v6,+noarm,+mclass";
104 ARMArchFeature = "+v6";
106 ARMArchFeature = "+v6";
107 } else if (SubVer == '5') {
108 if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == 'e')
109 ARMArchFeature = "+v5te";
111 ARMArchFeature = "+v5t";
112 } else if (SubVer == '4' && Len >= Idx+2 && TT[Idx+1] == 't')
113 ARMArchFeature = "+v4t";
117 if (ARMArchFeature.empty())
118 ARMArchFeature = "+thumb-mode";
120 ARMArchFeature += ",+thumb-mode";
123 return ARMArchFeature;
126 MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(StringRef TT, StringRef CPU,
128 std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
131 ArchFS = ArchFS + "," + FS.str();
136 MCSubtargetInfo *X = new MCSubtargetInfo();
137 InitARMMCSubtargetInfo(X, TT, CPU, ArchFS);
141 static MCInstrInfo *createARMMCInstrInfo() {
142 MCInstrInfo *X = new MCInstrInfo();
143 InitARMMCInstrInfo(X);
147 static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
148 MCRegisterInfo *X = new MCRegisterInfo();
149 InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
153 static MCAsmInfo *createARMMCAsmInfo(const Target &T, StringRef TT) {
154 Triple TheTriple(TT);
156 if (TheTriple.isOSDarwin())
157 return new ARMMCAsmInfoDarwin();
159 return new ARMELFMCAsmInfo();
162 static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
164 CodeGenOpt::Level OL) {
165 MCCodeGenInfo *X = new MCCodeGenInfo();
166 if (RM == Reloc::Default) {
167 Triple TheTriple(TT);
168 // Default relocation model on Darwin is PIC, not DynamicNoPIC.
169 RM = TheTriple.isOSDarwin() ? Reloc::PIC_ : Reloc::DynamicNoPIC;
171 X->InitMCCodeGenInfo(RM, CM, OL);
175 // This is duplicated code. Refactor this.
176 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
177 MCContext &Ctx, MCAsmBackend &MAB,
179 MCCodeEmitter *Emitter,
182 Triple TheTriple(TT);
184 if (TheTriple.isOSDarwin())
185 return createMachOStreamer(Ctx, MAB, OS, Emitter, false);
187 if (TheTriple.isOSWindows()) {
188 llvm_unreachable("ARM does not support Windows COFF format");
191 return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
192 TheTriple.getArch() == Triple::thumb);
195 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
196 unsigned SyntaxVariant,
197 const MCAsmInfo &MAI,
198 const MCInstrInfo &MII,
199 const MCRegisterInfo &MRI,
200 const MCSubtargetInfo &STI) {
201 if (SyntaxVariant == 0)
202 return new ARMInstPrinter(MAI, MII, MRI, STI);
208 class ARMMCInstrAnalysis : public MCInstrAnalysis {
210 ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
212 virtual bool isUnconditionalBranch(const MCInst &Inst) const {
213 // BCCs with the "always" predicate are unconditional branches.
214 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
216 return MCInstrAnalysis::isUnconditionalBranch(Inst);
219 virtual bool isConditionalBranch(const MCInst &Inst) const {
220 // BCCs with the "always" predicate are unconditional branches.
221 if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
223 return MCInstrAnalysis::isConditionalBranch(Inst);
226 uint64_t evaluateBranch(const MCInst &Inst, uint64_t Addr,
227 uint64_t Size) const {
228 // We only handle PCRel branches for now.
229 if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
232 int64_t Imm = Inst.getOperand(0).getImm();
233 // FIXME: This is not right for thumb.
234 return Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
240 static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
241 return new ARMMCInstrAnalysis(Info);
244 // Force static initialization.
245 extern "C" void LLVMInitializeARMTargetMC() {
246 // Register the MC asm info.
247 RegisterMCAsmInfoFn A(TheARMTarget, createARMMCAsmInfo);
248 RegisterMCAsmInfoFn B(TheThumbTarget, createARMMCAsmInfo);
250 // Register the MC codegen info.
251 TargetRegistry::RegisterMCCodeGenInfo(TheARMTarget, createARMMCCodeGenInfo);
252 TargetRegistry::RegisterMCCodeGenInfo(TheThumbTarget, createARMMCCodeGenInfo);
254 // Register the MC instruction info.
255 TargetRegistry::RegisterMCInstrInfo(TheARMTarget, createARMMCInstrInfo);
256 TargetRegistry::RegisterMCInstrInfo(TheThumbTarget, createARMMCInstrInfo);
258 // Register the MC register info.
259 TargetRegistry::RegisterMCRegInfo(TheARMTarget, createARMMCRegisterInfo);
260 TargetRegistry::RegisterMCRegInfo(TheThumbTarget, createARMMCRegisterInfo);
262 // Register the MC subtarget info.
263 TargetRegistry::RegisterMCSubtargetInfo(TheARMTarget,
264 ARM_MC::createARMMCSubtargetInfo);
265 TargetRegistry::RegisterMCSubtargetInfo(TheThumbTarget,
266 ARM_MC::createARMMCSubtargetInfo);
268 // Register the MC instruction analyzer.
269 TargetRegistry::RegisterMCInstrAnalysis(TheARMTarget,
270 createARMMCInstrAnalysis);
271 TargetRegistry::RegisterMCInstrAnalysis(TheThumbTarget,
272 createARMMCInstrAnalysis);
274 // Register the MC Code Emitter
275 TargetRegistry::RegisterMCCodeEmitter(TheARMTarget, createARMMCCodeEmitter);
276 TargetRegistry::RegisterMCCodeEmitter(TheThumbTarget, createARMMCCodeEmitter);
278 // Register the asm backend.
279 TargetRegistry::RegisterMCAsmBackend(TheARMTarget, createARMAsmBackend);
280 TargetRegistry::RegisterMCAsmBackend(TheThumbTarget, createARMAsmBackend);
282 // Register the object streamer.
283 TargetRegistry::RegisterMCObjectStreamer(TheARMTarget, createMCStreamer);
284 TargetRegistry::RegisterMCObjectStreamer(TheThumbTarget, createMCStreamer);
286 // Register the MCInstPrinter.
287 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
288 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);