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