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