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