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