Revise ARM inline assembly memory operands to require the memory address to
[oota-llvm.git] / lib / Target / ARM / AsmPrinter / ARMAsmPrinter.cpp
1 //                     The LLVM Compiler Infrastructure
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file contains a printer that converts from our internal representation
9 // of machine-dependent LLVM code to GAS-format ARM assembly language.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "asm-printer"
14 #include "ARM.h"
15 #include "ARMBuildAttrs.h"
16 #include "ARMTargetMachine.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Module.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/MC/MCSectionMachO.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/Target/TargetRegistry.h"
37 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/ADT/SmallString.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/ADT/StringSet.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/Mangler.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include <cctype>
47 using namespace llvm;
48
49 STATISTIC(EmittedInsts, "Number of machine instrs printed");
50
51 namespace {
52   class VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
53
54     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55     /// make the right decision when printing asm code for different targets.
56     const ARMSubtarget *Subtarget;
57
58     /// AFI - Keep a pointer to ARMFunctionInfo for the current
59     /// MachineFunction.
60     ARMFunctionInfo *AFI;
61
62     /// MCP - Keep a pointer to constantpool entries of the current
63     /// MachineFunction.
64     const MachineConstantPool *MCP;
65
66     /// We name each basic block in a Function with a unique number, so
67     /// that we can consistently refer to them later. This is cleared
68     /// at the beginning of each call to runOnMachineFunction().
69     ///
70     typedef std::map<const Value *, unsigned> ValueMapTy;
71     ValueMapTy NumberForBB;
72
73     /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
74     /// non-lazy-pointers for indirect access.
75     StringMap<std::string> GVNonLazyPtrs;
76
77     /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
78     /// visibility that require non-lazy-pointers for indirect access.
79     StringMap<std::string> HiddenGVNonLazyPtrs;
80
81     /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
82     bool InCPMode;
83   public:
84     explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
85                            const MCAsmInfo *T, bool V)
86       : AsmPrinter(O, TM, T, V), AFI(NULL), MCP(NULL),
87         InCPMode(false) {
88       Subtarget = &TM.getSubtarget<ARMSubtarget>();
89     }
90
91     virtual const char *getPassName() const {
92       return "ARM Assembly Printer";
93     }
94
95     void printOperand(const MachineInstr *MI, int OpNum,
96                       const char *Modifier = 0);
97     void printSOImmOperand(const MachineInstr *MI, int OpNum);
98     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum);
99     void printSORegOperand(const MachineInstr *MI, int OpNum);
100     void printAddrMode2Operand(const MachineInstr *MI, int OpNum);
101     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum);
102     void printAddrMode3Operand(const MachineInstr *MI, int OpNum);
103     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum);
104     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,
105                                const char *Modifier = 0);
106     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,
107                                const char *Modifier = 0);
108     void printAddrMode6Operand(const MachineInstr *MI, int OpNum);
109     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
110                                 const char *Modifier = 0);
111     void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum);
112
113     void printThumbITMask(const MachineInstr *MI, int OpNum);
114     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum);
115     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
116                                       unsigned Scale);
117     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum);
118     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum);
119     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum);
120     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum);
121
122     void printT2SOOperand(const MachineInstr *MI, int OpNum);
123     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum);
124     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum);
125     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum);
126     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum);
127     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum);
128
129     void printPredicateOperand(const MachineInstr *MI, int OpNum);
130     void printSBitModifierOperand(const MachineInstr *MI, int OpNum);
131     void printPCLabel(const MachineInstr *MI, int OpNum);
132     void printRegisterList(const MachineInstr *MI, int OpNum);
133     void printCPInstOperand(const MachineInstr *MI, int OpNum,
134                             const char *Modifier);
135     void printJTBlockOperand(const MachineInstr *MI, int OpNum);
136     void printJT2BlockOperand(const MachineInstr *MI, int OpNum);
137     void printTBAddrMode(const MachineInstr *MI, int OpNum);
138     void printNoHashImmediate(const MachineInstr *MI, int OpNum);
139
140     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
141                                  unsigned AsmVariant, const char *ExtraCode);
142     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
143                                        unsigned AsmVariant,
144                                        const char *ExtraCode);
145
146     void PrintGlobalVariable(const GlobalVariable* GVar);
147     void printInstruction(const MachineInstr *MI);  // autogenerated.
148     static const char *getRegisterName(unsigned RegNo);
149
150     void printMachineInstruction(const MachineInstr *MI);
151     bool runOnMachineFunction(MachineFunction &F);
152     bool doFinalization(Module &M);
153     void EmitStartOfAsmFile(Module &M);
154
155     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
156     /// the .s file.
157     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
158       printDataDirective(MCPV->getType());
159
160       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
161       GlobalValue *GV = ACPV->getGV();
162       std::string Name;
163
164       if (ACPV->isLSDA()) {
165         SmallString<16> LSDAName;
166         raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
167           "_LSDA_" << getFunctionNumber();
168         Name = LSDAName.str();
169       } else if (GV) {
170         bool isIndirect = Subtarget->isTargetDarwin() &&
171           Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
172         if (!isIndirect)
173           Name = Mang->getMangledName(GV);
174         else {
175           // FIXME: Remove this when Darwin transition to @GOT like syntax.
176           std::string SymName = Mang->getMangledName(GV);
177           Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
178           if (GV->hasHiddenVisibility())
179             HiddenGVNonLazyPtrs[SymName] = Name;
180           else
181             GVNonLazyPtrs[SymName] = Name;
182         }
183       } else
184         Name = Mang->makeNameProper(ACPV->getSymbol());
185       O << Name;
186
187       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
188       if (ACPV->getPCAdjustment() != 0) {
189         O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
190           << ACPV->getLabelId()
191           << "+" << (unsigned)ACPV->getPCAdjustment();
192          if (ACPV->mustAddCurrentAddress())
193            O << "-.";
194          O << ")";
195       }
196       O << "\n";
197     }
198
199     void getAnalysisUsage(AnalysisUsage &AU) const {
200       AsmPrinter::getAnalysisUsage(AU);
201       AU.setPreservesAll();
202       AU.addRequired<MachineModuleInfo>();
203       AU.addRequired<DwarfWriter>();
204     }
205   };
206 } // end of anonymous namespace
207
208 #include "ARMGenAsmWriter.inc"
209
210 /// runOnMachineFunction - This uses the printInstruction()
211 /// method to print assembly for each instruction.
212 ///
213 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
214   this->MF = &MF;
215
216   AFI = MF.getInfo<ARMFunctionInfo>();
217   MCP = MF.getConstantPool();
218
219   SetupMachineFunction(MF);
220   O << "\n";
221
222   // NOTE: we don't print out constant pools here, they are handled as
223   // instructions.
224
225   O << '\n';
226
227   // Print out labels for the function.
228   const Function *F = MF.getFunction();
229   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
230
231   switch (F->getLinkage()) {
232   default: llvm_unreachable("Unknown linkage type!");
233   case Function::PrivateLinkage:
234   case Function::InternalLinkage:
235     break;
236   case Function::ExternalLinkage:
237     O << "\t.globl\t" << CurrentFnName << "\n";
238     break;
239   case Function::LinkerPrivateLinkage:
240   case Function::WeakAnyLinkage:
241   case Function::WeakODRLinkage:
242   case Function::LinkOnceAnyLinkage:
243   case Function::LinkOnceODRLinkage:
244     if (Subtarget->isTargetDarwin()) {
245       O << "\t.globl\t" << CurrentFnName << "\n";
246       O << "\t.weak_definition\t" << CurrentFnName << "\n";
247     } else {
248       O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
249     }
250     break;
251   }
252
253   printVisibility(CurrentFnName, F->getVisibility());
254
255   unsigned FnAlign = 1 << MF.getAlignment();  // MF alignment is log2.
256   if (AFI->isThumbFunction()) {
257     EmitAlignment(FnAlign, F, AFI->getAlign());
258     O << "\t.code\t16\n";
259     O << "\t.thumb_func";
260     if (Subtarget->isTargetDarwin())
261       O << "\t" << CurrentFnName;
262     O << "\n";
263     InCPMode = false;
264   } else {
265     EmitAlignment(FnAlign, F);
266   }
267
268   O << CurrentFnName << ":\n";
269   // Emit pre-function debug information.
270   DW->BeginFunction(&MF);
271
272   if (Subtarget->isTargetDarwin()) {
273     // If the function is empty, then we need to emit *something*. Otherwise,
274     // the function's label might be associated with something that it wasn't
275     // meant to be associated with. We emit a noop in this situation.
276     MachineFunction::iterator I = MF.begin();
277
278     if (++I == MF.end() && MF.front().empty())
279       O << "\tnop\n";
280   }
281
282   // Print out code for the function.
283   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
284        I != E; ++I) {
285     // Print a label for the basic block.
286     if (I != MF.begin()) {
287       EmitBasicBlockStart(I);
288     }
289     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
290          II != E; ++II) {
291       // Print the assembly for the instruction.
292       printMachineInstruction(II);
293     }
294   }
295
296   if (MAI->hasDotTypeDotSizeDirective())
297     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
298
299   // Emit post-function debug information.
300   DW->EndFunction(&MF);
301
302   return false;
303 }
304
305 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
306                                  const char *Modifier) {
307   const MachineOperand &MO = MI->getOperand(OpNum);
308   switch (MO.getType()) {
309   case MachineOperand::MO_Register: {
310     unsigned Reg = MO.getReg();
311     if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
312       if (Modifier && strcmp(Modifier, "dregpair") == 0) {
313         unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
314         unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
315         O << '{'
316           << getRegisterName(DRegLo) << ',' << getRegisterName(DRegHi)
317           << '}';
318       } else if (Modifier && strcmp(Modifier, "lane") == 0) {
319         unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
320         unsigned DReg = TRI->getMatchingSuperReg(Reg, RegNum & 1 ? 2 : 1,
321                                                  &ARM::DPR_VFP2RegClass);
322         O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
323       } else {
324         O << getRegisterName(Reg);
325       }
326     } else
327       llvm_unreachable("not implemented");
328     break;
329   }
330   case MachineOperand::MO_Immediate: {
331     int64_t Imm = MO.getImm();
332     O << '#';
333     if (Modifier) {
334       if (strcmp(Modifier, "lo16") == 0)
335         O << ":lower16:";
336       else if (strcmp(Modifier, "hi16") == 0)
337         O << ":upper16:";
338     }
339     O << Imm;
340     break;
341   }
342   case MachineOperand::MO_MachineBasicBlock:
343     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
344     return;
345   case MachineOperand::MO_GlobalAddress: {
346     bool isCallOp = Modifier && !strcmp(Modifier, "call");
347     GlobalValue *GV = MO.getGlobal();
348     O << Mang->getMangledName(GV);
349
350     printOffset(MO.getOffset());
351
352     if (isCallOp && Subtarget->isTargetELF() &&
353         TM.getRelocationModel() == Reloc::PIC_)
354       O << "(PLT)";
355     break;
356   }
357   case MachineOperand::MO_ExternalSymbol: {
358     bool isCallOp = Modifier && !strcmp(Modifier, "call");
359     std::string Name = Mang->makeNameProper(MO.getSymbolName());
360
361     O << Name;
362     if (isCallOp && Subtarget->isTargetELF() &&
363         TM.getRelocationModel() == Reloc::PIC_)
364       O << "(PLT)";
365     break;
366   }
367   case MachineOperand::MO_ConstantPoolIndex:
368     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
369       << '_' << MO.getIndex();
370     break;
371   case MachineOperand::MO_JumpTableIndex:
372     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
373       << '_' << MO.getIndex();
374     break;
375   default:
376     O << "<unknown operand type>"; abort (); break;
377   }
378 }
379
380 static void printSOImm(formatted_raw_ostream &O, int64_t V, bool VerboseAsm,
381                        const MCAsmInfo *MAI) {
382   // Break it up into two parts that make up a shifter immediate.
383   V = ARM_AM::getSOImmVal(V);
384   assert(V != -1 && "Not a valid so_imm value!");
385
386   unsigned Imm = ARM_AM::getSOImmValImm(V);
387   unsigned Rot = ARM_AM::getSOImmValRot(V);
388
389   // Print low-level immediate formation info, per
390   // A5.1.3: "Data-processing operands - Immediate".
391   if (Rot) {
392     O << "#" << Imm << ", " << Rot;
393     // Pretty printed version.
394     if (VerboseAsm)
395       O << ' ' << MAI->getCommentString()
396         << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
397   } else {
398     O << "#" << Imm;
399   }
400 }
401
402 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
403 /// immediate in bits 0-7.
404 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
405   const MachineOperand &MO = MI->getOperand(OpNum);
406   assert(MO.isImm() && "Not a valid so_imm value!");
407   printSOImm(O, MO.getImm(), VerboseAsm, MAI);
408 }
409
410 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
411 /// followed by an 'orr' to materialize.
412 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
413   const MachineOperand &MO = MI->getOperand(OpNum);
414   assert(MO.isImm() && "Not a valid so_imm value!");
415   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
416   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
417   printSOImm(O, V1, VerboseAsm, MAI);
418   O << "\n\torr";
419   printPredicateOperand(MI, 2);
420   O << " ";
421   printOperand(MI, 0);
422   O << ", ";
423   printOperand(MI, 0);
424   O << ", ";
425   printSOImm(O, V2, VerboseAsm, MAI);
426 }
427
428 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
429 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
430 //    REG 0   0           - e.g. R5
431 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
432 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
433 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
434   const MachineOperand &MO1 = MI->getOperand(Op);
435   const MachineOperand &MO2 = MI->getOperand(Op+1);
436   const MachineOperand &MO3 = MI->getOperand(Op+2);
437
438   O << getRegisterName(MO1.getReg());
439
440   // Print the shift opc.
441   O << ", "
442     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
443     << " ";
444
445   if (MO2.getReg()) {
446     O << getRegisterName(MO2.getReg());
447     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
448   } else {
449     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
450   }
451 }
452
453 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
454   const MachineOperand &MO1 = MI->getOperand(Op);
455   const MachineOperand &MO2 = MI->getOperand(Op+1);
456   const MachineOperand &MO3 = MI->getOperand(Op+2);
457
458   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
459     printOperand(MI, Op);
460     return;
461   }
462
463   O << "[" << getRegisterName(MO1.getReg());
464
465   if (!MO2.getReg()) {
466     if (ARM_AM::getAM2Offset(MO3.getImm()))  // Don't print +0.
467       O << ", #"
468         << (char)ARM_AM::getAM2Op(MO3.getImm())
469         << ARM_AM::getAM2Offset(MO3.getImm());
470     O << "]";
471     return;
472   }
473
474   O << ", "
475     << (char)ARM_AM::getAM2Op(MO3.getImm())
476     << getRegisterName(MO2.getReg());
477
478   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
479     O << ", "
480       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
481       << " #" << ShImm;
482   O << "]";
483 }
484
485 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
486   const MachineOperand &MO1 = MI->getOperand(Op);
487   const MachineOperand &MO2 = MI->getOperand(Op+1);
488
489   if (!MO1.getReg()) {
490     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
491     assert(ImmOffs && "Malformed indexed load / store!");
492     O << "#"
493       << (char)ARM_AM::getAM2Op(MO2.getImm())
494       << ImmOffs;
495     return;
496   }
497
498   O << (char)ARM_AM::getAM2Op(MO2.getImm())
499     << getRegisterName(MO1.getReg());
500
501   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
502     O << ", "
503       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
504       << " #" << ShImm;
505 }
506
507 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
508   const MachineOperand &MO1 = MI->getOperand(Op);
509   const MachineOperand &MO2 = MI->getOperand(Op+1);
510   const MachineOperand &MO3 = MI->getOperand(Op+2);
511
512   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
513   O << "[" << getRegisterName(MO1.getReg());
514
515   if (MO2.getReg()) {
516     O << ", "
517       << (char)ARM_AM::getAM3Op(MO3.getImm())
518       << getRegisterName(MO2.getReg())
519       << "]";
520     return;
521   }
522
523   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
524     O << ", #"
525       << (char)ARM_AM::getAM3Op(MO3.getImm())
526       << ImmOffs;
527   O << "]";
528 }
529
530 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
531   const MachineOperand &MO1 = MI->getOperand(Op);
532   const MachineOperand &MO2 = MI->getOperand(Op+1);
533
534   if (MO1.getReg()) {
535     O << (char)ARM_AM::getAM3Op(MO2.getImm())
536       << getRegisterName(MO1.getReg());
537     return;
538   }
539
540   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
541   assert(ImmOffs && "Malformed indexed load / store!");
542   O << "#"
543     << (char)ARM_AM::getAM3Op(MO2.getImm())
544     << ImmOffs;
545 }
546
547 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
548                                           const char *Modifier) {
549   const MachineOperand &MO1 = MI->getOperand(Op);
550   const MachineOperand &MO2 = MI->getOperand(Op+1);
551   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
552   if (Modifier && strcmp(Modifier, "submode") == 0) {
553     if (MO1.getReg() == ARM::SP) {
554       // FIXME
555       bool isLDM = (MI->getOpcode() == ARM::LDM ||
556                     MI->getOpcode() == ARM::LDM_RET ||
557                     MI->getOpcode() == ARM::t2LDM ||
558                     MI->getOpcode() == ARM::t2LDM_RET);
559       O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
560     } else
561       O << ARM_AM::getAMSubModeStr(Mode);
562   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
563     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
564     if (Mode == ARM_AM::ia)
565       O << ".w";
566   } else {
567     printOperand(MI, Op);
568     if (ARM_AM::getAM4WBFlag(MO2.getImm()))
569       O << "!";
570   }
571 }
572
573 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
574                                           const char *Modifier) {
575   const MachineOperand &MO1 = MI->getOperand(Op);
576   const MachineOperand &MO2 = MI->getOperand(Op+1);
577
578   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
579     printOperand(MI, Op);
580     return;
581   }
582
583   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
584
585   if (Modifier && strcmp(Modifier, "submode") == 0) {
586     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
587     if (MO1.getReg() == ARM::SP) {
588       bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
589                      MI->getOpcode() == ARM::FLDMS);
590       O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
591     } else
592       O << ARM_AM::getAMSubModeStr(Mode);
593     return;
594   } else if (Modifier && strcmp(Modifier, "base") == 0) {
595     // Used for FSTM{D|S} and LSTM{D|S} operations.
596     O << getRegisterName(MO1.getReg());
597     if (ARM_AM::getAM5WBFlag(MO2.getImm()))
598       O << "!";
599     return;
600   }
601
602   O << "[" << getRegisterName(MO1.getReg());
603
604   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
605     O << ", #"
606       << (char)ARM_AM::getAM5Op(MO2.getImm())
607       << ImmOffs*4;
608   }
609   O << "]";
610 }
611
612 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
613   const MachineOperand &MO1 = MI->getOperand(Op);
614   const MachineOperand &MO2 = MI->getOperand(Op+1);
615   const MachineOperand &MO3 = MI->getOperand(Op+2);
616
617   // FIXME: No support yet for specifying alignment.
618   O << "[" << getRegisterName(MO1.getReg()) << "]";
619
620   if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
621     if (MO2.getReg() == 0)
622       O << "!";
623     else
624       O << ", " << getRegisterName(MO2.getReg());
625   }
626 }
627
628 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
629                                            const char *Modifier) {
630   if (Modifier && strcmp(Modifier, "label") == 0) {
631     printPCLabel(MI, Op+1);
632     return;
633   }
634
635   const MachineOperand &MO1 = MI->getOperand(Op);
636   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
637   O << "[pc, +" << getRegisterName(MO1.getReg()) << "]";
638 }
639
640 void
641 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
642   const MachineOperand &MO = MI->getOperand(Op);
643   uint32_t v = ~MO.getImm();
644   int32_t lsb = CountTrailingZeros_32(v);
645   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
646   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
647   O << "#" << lsb << ", #" << width;
648 }
649
650 //===--------------------------------------------------------------------===//
651
652 void
653 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
654   // (3 - the number of trailing zeros) is the number of then / else.
655   unsigned Mask = MI->getOperand(Op).getImm();
656   unsigned NumTZ = CountTrailingZeros_32(Mask);
657   assert(NumTZ <= 3 && "Invalid IT mask!");
658   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
659     bool T = (Mask & (1 << Pos)) == 0;
660     if (T)
661       O << 't';
662     else
663       O << 'e';
664   }
665 }
666
667 void
668 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
669   const MachineOperand &MO1 = MI->getOperand(Op);
670   const MachineOperand &MO2 = MI->getOperand(Op+1);
671   O << "[" << getRegisterName(MO1.getReg());
672   O << ", " << getRegisterName(MO2.getReg()) << "]";
673 }
674
675 void
676 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
677                                             unsigned Scale) {
678   const MachineOperand &MO1 = MI->getOperand(Op);
679   const MachineOperand &MO2 = MI->getOperand(Op+1);
680   const MachineOperand &MO3 = MI->getOperand(Op+2);
681
682   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
683     printOperand(MI, Op);
684     return;
685   }
686
687   O << "[" << getRegisterName(MO1.getReg());
688   if (MO3.getReg())
689     O << ", " << getRegisterName(MO3.getReg());
690   else if (unsigned ImmOffs = MO2.getImm()) {
691     O << ", #" << ImmOffs;
692     if (Scale > 1)
693       O << " * " << Scale;
694   }
695   O << "]";
696 }
697
698 void
699 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
700   printThumbAddrModeRI5Operand(MI, Op, 1);
701 }
702 void
703 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
704   printThumbAddrModeRI5Operand(MI, Op, 2);
705 }
706 void
707 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
708   printThumbAddrModeRI5Operand(MI, Op, 4);
709 }
710
711 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
712   const MachineOperand &MO1 = MI->getOperand(Op);
713   const MachineOperand &MO2 = MI->getOperand(Op+1);
714   O << "[" << getRegisterName(MO1.getReg());
715   if (unsigned ImmOffs = MO2.getImm())
716     O << ", #" << ImmOffs << " * 4";
717   O << "]";
718 }
719
720 //===--------------------------------------------------------------------===//
721
722 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
723 // register with shift forms.
724 // REG 0   0           - e.g. R5
725 // REG IMM, SH_OPC     - e.g. R5, LSL #3
726 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
727   const MachineOperand &MO1 = MI->getOperand(OpNum);
728   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
729
730   unsigned Reg = MO1.getReg();
731   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
732   O << getRegisterName(Reg);
733
734   // Print the shift opc.
735   O << ", "
736     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
737     << " ";
738
739   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
740   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
741 }
742
743 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
744                                                 int OpNum) {
745   const MachineOperand &MO1 = MI->getOperand(OpNum);
746   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
747
748   O << "[" << getRegisterName(MO1.getReg());
749
750   unsigned OffImm = MO2.getImm();
751   if (OffImm)  // Don't print +0.
752     O << ", #+" << OffImm;
753   O << "]";
754 }
755
756 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
757                                                int OpNum) {
758   const MachineOperand &MO1 = MI->getOperand(OpNum);
759   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
760
761   O << "[" << getRegisterName(MO1.getReg());
762
763   int32_t OffImm = (int32_t)MO2.getImm();
764   // Don't print +0.
765   if (OffImm < 0)
766     O << ", #-" << -OffImm;
767   else if (OffImm > 0)
768     O << ", #+" << OffImm;
769   O << "]";
770 }
771
772 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
773                                                  int OpNum) {
774   const MachineOperand &MO1 = MI->getOperand(OpNum);
775   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
776
777   O << "[" << getRegisterName(MO1.getReg());
778
779   int32_t OffImm = (int32_t)MO2.getImm() / 4;
780   // Don't print +0.
781   if (OffImm < 0)
782     O << ", #-" << -OffImm << " * 4";
783   else if (OffImm > 0)
784     O << ", #+" << OffImm << " * 4";
785   O << "]";
786 }
787
788 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
789                                                      int OpNum) {
790   const MachineOperand &MO1 = MI->getOperand(OpNum);
791   int32_t OffImm = (int32_t)MO1.getImm();
792   // Don't print +0.
793   if (OffImm < 0)
794     O << "#-" << -OffImm;
795   else if (OffImm > 0)
796     O << "#+" << OffImm;
797 }
798
799 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
800                                                 int OpNum) {
801   const MachineOperand &MO1 = MI->getOperand(OpNum);
802   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
803   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
804
805   O << "[" << getRegisterName(MO1.getReg());
806
807   assert(MO2.getReg() && "Invalid so_reg load / store address!");
808   O << ", " << getRegisterName(MO2.getReg());
809
810   unsigned ShAmt = MO3.getImm();
811   if (ShAmt) {
812     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
813     O << ", lsl #" << ShAmt;
814   }
815   O << "]";
816 }
817
818
819 //===--------------------------------------------------------------------===//
820
821 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
822   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
823   if (CC != ARMCC::AL)
824     O << ARMCondCodeToString(CC);
825 }
826
827 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
828   unsigned Reg = MI->getOperand(OpNum).getReg();
829   if (Reg) {
830     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
831     O << 's';
832   }
833 }
834
835 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
836   int Id = (int)MI->getOperand(OpNum).getImm();
837   O << MAI->getPrivateGlobalPrefix() << "PC" << Id;
838 }
839
840 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
841   O << "{";
842   // Always skip the first operand, it's the optional (and implicit writeback).
843   for (unsigned i = OpNum+1, e = MI->getNumOperands(); i != e; ++i) {
844     if (MI->getOperand(i).isImplicit())
845       continue;
846     if ((int)i != OpNum+1) O << ", ";
847     printOperand(MI, i);
848   }
849   O << "}";
850 }
851
852 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
853                                        const char *Modifier) {
854   assert(Modifier && "This operand only works with a modifier!");
855   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
856   // data itself.
857   if (!strcmp(Modifier, "label")) {
858     unsigned ID = MI->getOperand(OpNum).getImm();
859     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
860       << '_' << ID << ":\n";
861   } else {
862     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
863     unsigned CPI = MI->getOperand(OpNum).getIndex();
864
865     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
866
867     if (MCPE.isMachineConstantPoolEntry()) {
868       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
869     } else {
870       EmitGlobalConstant(MCPE.Val.ConstVal);
871     }
872   }
873 }
874
875 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
876   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
877
878   const MachineOperand &MO1 = MI->getOperand(OpNum);
879   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
880   unsigned JTI = MO1.getIndex();
881   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
882     << '_' << JTI << '_' << MO2.getImm() << ":\n";
883
884   const char *JTEntryDirective = MAI->getData32bitsDirective();
885
886   const MachineFunction *MF = MI->getParent()->getParent();
887   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
888   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
889   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
890   bool UseSet= MAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
891   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
892   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
893     MachineBasicBlock *MBB = JTBBs[i];
894     bool isNew = JTSets.insert(MBB);
895
896     if (UseSet && isNew)
897       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
898
899     O << JTEntryDirective << ' ';
900     if (UseSet)
901       O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
902         << '_' << JTI << '_' << MO2.getImm()
903         << "_set_" << MBB->getNumber();
904     else if (TM.getRelocationModel() == Reloc::PIC_) {
905       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
906       O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
907         << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
908     } else {
909       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
910     }
911     if (i != e-1)
912       O << '\n';
913   }
914 }
915
916 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum) {
917   const MachineOperand &MO1 = MI->getOperand(OpNum);
918   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
919   unsigned JTI = MO1.getIndex();
920   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
921     << '_' << JTI << '_' << MO2.getImm() << ":\n";
922
923   const MachineFunction *MF = MI->getParent()->getParent();
924   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
925   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
926   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
927   bool ByteOffset = false, HalfWordOffset = false;
928   if (MI->getOpcode() == ARM::t2TBB)
929     ByteOffset = true;
930   else if (MI->getOpcode() == ARM::t2TBH)
931     HalfWordOffset = true;
932
933   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
934     MachineBasicBlock *MBB = JTBBs[i];
935     if (ByteOffset)
936       O << MAI->getData8bitsDirective();
937     else if (HalfWordOffset)
938       O << MAI->getData16bitsDirective();
939     if (ByteOffset || HalfWordOffset) {
940       O << '(';
941       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
942       O << "-" << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
943         << '_' << JTI << '_' << MO2.getImm() << ")/2";
944     } else {
945       O << "\tb.w ";
946       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
947     }
948     if (i != e-1)
949       O << '\n';
950   }
951
952   // Make sure the instruction that follows TBB is 2-byte aligned.
953   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
954   if (ByteOffset && (JTBBs.size() & 1)) {
955     O << '\n';
956     EmitAlignment(1);
957   }
958 }
959
960 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum) {
961   O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
962   if (MI->getOpcode() == ARM::t2TBH)
963     O << ", lsl #1";
964   O << ']';
965 }
966
967 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum) {
968   O << MI->getOperand(OpNum).getImm();
969 }
970
971 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
972                                     unsigned AsmVariant, const char *ExtraCode){
973   // Does this asm operand have a single letter operand modifier?
974   if (ExtraCode && ExtraCode[0]) {
975     if (ExtraCode[1] != 0) return true; // Unknown modifier.
976
977     switch (ExtraCode[0]) {
978     default: return true;  // Unknown modifier.
979     case 'a': // Print as a memory address.
980       if (MI->getOperand(OpNum).isReg()) {
981         O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
982         return false;
983       }
984       // Fallthrough
985     case 'c': // Don't print "#" before an immediate operand.
986       if (!MI->getOperand(OpNum).isImm())
987         return true;
988       printNoHashImmediate(MI, OpNum);
989       return false;
990     case 'P': // Print a VFP double precision register.
991       printOperand(MI, OpNum);
992       return false;
993     case 'Q':
994       if (TM.getTargetData()->isLittleEndian())
995         break;
996       // Fallthrough
997     case 'R':
998       if (TM.getTargetData()->isBigEndian())
999         break;
1000       // Fallthrough
1001     case 'H': // Write second word of DI / DF reference.
1002       // Verify that this operand has two consecutive registers.
1003       if (!MI->getOperand(OpNum).isReg() ||
1004           OpNum+1 == MI->getNumOperands() ||
1005           !MI->getOperand(OpNum+1).isReg())
1006         return true;
1007       ++OpNum;   // Return the high-part.
1008     }
1009   }
1010
1011   printOperand(MI, OpNum);
1012   return false;
1013 }
1014
1015 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1016                                           unsigned OpNum, unsigned AsmVariant,
1017                                           const char *ExtraCode) {
1018   if (ExtraCode && ExtraCode[0])
1019     return true; // Unknown modifier.
1020
1021   const MachineOperand &MO = MI->getOperand(OpNum);
1022   assert(MO.isReg() && "unexpected inline asm memory operand");
1023   O << "[" << getRegisterName(MO.getReg()) << "]";
1024   return false;
1025 }
1026
1027 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
1028   ++EmittedInsts;
1029
1030   int Opc = MI->getOpcode();
1031   switch (Opc) {
1032   case ARM::CONSTPOOL_ENTRY:
1033     if (!InCPMode && AFI->isThumbFunction()) {
1034       EmitAlignment(2);
1035       InCPMode = true;
1036     }
1037     break;
1038   default: {
1039     if (InCPMode && AFI->isThumbFunction())
1040       InCPMode = false;
1041   }}
1042
1043   // Call the autogenerated instruction printer routines.
1044   processDebugLoc(MI, true);
1045   printInstruction(MI);
1046   if (VerboseAsm && !MI->getDebugLoc().isUnknown())
1047     EmitComments(*MI);
1048   O << '\n';
1049   processDebugLoc(MI, false);
1050 }
1051
1052 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1053   if (Subtarget->isTargetDarwin()) {
1054     Reloc::Model RelocM = TM.getRelocationModel();
1055     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1056       // Declare all the text sections up front (before the DWARF sections
1057       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1058       // them together at the beginning of the object file.  This helps
1059       // avoid out-of-range branches that are due a fundamental limitation of
1060       // the way symbol offsets are encoded with the current Darwin ARM
1061       // relocations.
1062       TargetLoweringObjectFileMachO &TLOFMacho = 
1063         static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1064       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1065       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1066       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1067       if (RelocM == Reloc::DynamicNoPIC) {
1068         const MCSection *sect =
1069           TLOFMacho.getMachOSection("__TEXT", "__symbol_stub4",
1070                                     MCSectionMachO::S_SYMBOL_STUBS,
1071                                     12, SectionKind::getText());
1072         OutStreamer.SwitchSection(sect);
1073       } else {
1074         const MCSection *sect =
1075           TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub4",
1076                                     MCSectionMachO::S_SYMBOL_STUBS,
1077                                     16, SectionKind::getText());
1078         OutStreamer.SwitchSection(sect);
1079       }
1080     }
1081   }
1082
1083   // Use unified assembler syntax mode for Thumb.
1084   if (Subtarget->isThumb())
1085     O << "\t.syntax unified\n";
1086
1087   // Emit ARM Build Attributes
1088   if (Subtarget->isTargetELF()) {
1089     // CPU Type
1090     std::string CPUString = Subtarget->getCPUString();
1091     if (CPUString != "generic")
1092       O << "\t.cpu " << CPUString << '\n';
1093
1094     // FIXME: Emit FPU type
1095     if (Subtarget->hasVFP2())
1096       O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
1097
1098     // Signal various FP modes.
1099     if (!UnsafeFPMath)
1100       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
1101         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
1102
1103     if (FiniteOnlyFPMath())
1104       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
1105     else
1106       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
1107
1108     // 8-bytes alignment stuff.
1109     O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
1110       << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
1111
1112     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1113     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard)
1114       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_HardFP_use << ", 3\n"
1115         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_VFP_args << ", 1\n";
1116
1117     // FIXME: Should we signal R9 usage?
1118   }
1119 }
1120
1121 void ARMAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
1122   const TargetData *TD = TM.getTargetData();
1123
1124   if (!GVar->hasInitializer())   // External global require no code
1125     return;
1126
1127   // Check to see if this is a special global used by LLVM, if so, emit it.
1128
1129   if (EmitSpecialLLVMGlobal(GVar)) {
1130     if (Subtarget->isTargetDarwin() &&
1131         TM.getRelocationModel() == Reloc::Static) {
1132       if (GVar->getName() == "llvm.global_ctors")
1133         O << ".reference .constructors_used\n";
1134       else if (GVar->getName() == "llvm.global_dtors")
1135         O << ".reference .destructors_used\n";
1136     }
1137     return;
1138   }
1139
1140   std::string name = Mang->getMangledName(GVar);
1141   Constant *C = GVar->getInitializer();
1142   const Type *Type = C->getType();
1143   unsigned Size = TD->getTypeAllocSize(Type);
1144   unsigned Align = TD->getPreferredAlignmentLog(GVar);
1145   bool isDarwin = Subtarget->isTargetDarwin();
1146
1147   printVisibility(name, GVar->getVisibility());
1148
1149   if (Subtarget->isTargetELF())
1150     O << "\t.type " << name << ",%object\n";
1151
1152   const MCSection *TheSection =
1153     getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
1154   OutStreamer.SwitchSection(TheSection);
1155
1156   // FIXME: get this stuff from section kind flags.
1157   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
1158       // Don't put things that should go in the cstring section into "comm".
1159       !TheSection->getKind().isMergeableCString()) {
1160     if (GVar->hasExternalLinkage()) {
1161       if (const char *Directive = MAI->getZeroFillDirective()) {
1162         O << "\t.globl\t" << name << "\n";
1163         O << Directive << "__DATA, __common, " << name << ", "
1164           << Size << ", " << Align << "\n";
1165         return;
1166       }
1167     }
1168
1169     if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
1170       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1171
1172       if (isDarwin) {
1173         if (GVar->hasLocalLinkage()) {
1174           O << MAI->getLCOMMDirective()  << name << "," << Size
1175             << ',' << Align;
1176         } else if (GVar->hasCommonLinkage()) {
1177           O << MAI->getCOMMDirective()  << name << "," << Size
1178             << ',' << Align;
1179         } else {
1180           OutStreamer.SwitchSection(TheSection);
1181           O << "\t.globl " << name << '\n'
1182             << MAI->getWeakDefDirective() << name << '\n';
1183           EmitAlignment(Align, GVar);
1184           O << name << ":";
1185           if (VerboseAsm) {
1186             O << "\t\t\t\t" << MAI->getCommentString() << ' ';
1187             WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1188           }
1189           O << '\n';
1190           EmitGlobalConstant(C);
1191           return;
1192         }
1193       } else if (MAI->getLCOMMDirective() != NULL) {
1194         if (GVar->hasLocalLinkage()) {
1195           O << MAI->getLCOMMDirective() << name << "," << Size;
1196         } else {
1197           O << MAI->getCOMMDirective()  << name << "," << Size;
1198           if (MAI->getCOMMDirectiveTakesAlignment())
1199             O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1200         }
1201       } else {
1202         if (GVar->hasLocalLinkage())
1203           O << "\t.local\t" << name << "\n";
1204         O << MAI->getCOMMDirective()  << name << "," << Size;
1205         if (MAI->getCOMMDirectiveTakesAlignment())
1206           O << "," << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1207       }
1208       if (VerboseAsm) {
1209         O << "\t\t" << MAI->getCommentString() << " ";
1210         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1211       }
1212       O << "\n";
1213       return;
1214     }
1215   }
1216
1217   switch (GVar->getLinkage()) {
1218   case GlobalValue::CommonLinkage:
1219   case GlobalValue::LinkOnceAnyLinkage:
1220   case GlobalValue::LinkOnceODRLinkage:
1221   case GlobalValue::WeakAnyLinkage:
1222   case GlobalValue::WeakODRLinkage:
1223   case GlobalValue::LinkerPrivateLinkage:
1224     if (isDarwin) {
1225       O << "\t.globl " << name << "\n"
1226         << "\t.weak_definition " << name << "\n";
1227     } else {
1228       O << "\t.weak " << name << "\n";
1229     }
1230     break;
1231   case GlobalValue::AppendingLinkage:
1232   // FIXME: appending linkage variables should go into a section of
1233   // their name or something.  For now, just emit them as external.
1234   case GlobalValue::ExternalLinkage:
1235     O << "\t.globl " << name << "\n";
1236     break;
1237   case GlobalValue::PrivateLinkage:
1238   case GlobalValue::InternalLinkage:
1239     break;
1240   default:
1241     llvm_unreachable("Unknown linkage type!");
1242   }
1243
1244   EmitAlignment(Align, GVar);
1245   O << name << ":";
1246   if (VerboseAsm) {
1247     O << "\t\t\t\t" << MAI->getCommentString() << " ";
1248     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1249   }
1250   O << "\n";
1251   if (MAI->hasDotTypeDotSizeDirective())
1252     O << "\t.size " << name << ", " << Size << "\n";
1253
1254   EmitGlobalConstant(C);
1255   O << '\n';
1256 }
1257
1258
1259 bool ARMAsmPrinter::doFinalization(Module &M) {
1260   if (Subtarget->isTargetDarwin()) {
1261     // All darwin targets use mach-o.
1262     TargetLoweringObjectFileMachO &TLOFMacho =
1263       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1264
1265     O << '\n';
1266
1267     // Output non-lazy-pointers for external and common global variables.
1268     if (!GVNonLazyPtrs.empty()) {
1269       // Switch with ".non_lazy_symbol_pointer" directive.
1270       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1271       EmitAlignment(2);
1272       for (StringMap<std::string>::iterator I = GVNonLazyPtrs.begin(),
1273            E = GVNonLazyPtrs.end(); I != E; ++I) {
1274         O << I->second << ":\n";
1275         O << "\t.indirect_symbol " << I->getKeyData() << "\n";
1276         O << "\t.long\t0\n";
1277       }
1278     }
1279
1280     if (!HiddenGVNonLazyPtrs.empty()) {
1281       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1282       EmitAlignment(2);
1283       for (StringMap<std::string>::iterator I = HiddenGVNonLazyPtrs.begin(),
1284              E = HiddenGVNonLazyPtrs.end(); I != E; ++I) {
1285         O << I->second << ":\n";
1286         O << "\t.long " << I->getKeyData() << "\n";
1287       }
1288     }
1289
1290     // Funny Darwin hack: This flag tells the linker that no global symbols
1291     // contain code that falls through to other global symbols (e.g. the obvious
1292     // implementation of multiple entry points).  If this doesn't occur, the
1293     // linker can safely perform dead code stripping.  Since LLVM never
1294     // generates code that does this, it is always safe to set.
1295     O << "\t.subsections_via_symbols\n";
1296   }
1297
1298   return AsmPrinter::doFinalization(M);
1299 }
1300
1301 // Force static initialization.
1302 extern "C" void LLVMInitializeARMAsmPrinter() {
1303   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1304   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1305 }