Add an "alignment" field to the MachineFunction object. It makes more sense to
[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, CodeGenOpt::Level OL,
86                            bool V)
87       : AsmPrinter(O, TM, T, OL, 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 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::printAddrModePCOperand(const MachineInstr *MI, int Op,
592                                            const char *Modifier) {
593   if (Modifier && strcmp(Modifier, "label") == 0) {
594     printPCLabel(MI, Op+1);
595     return;
596   }
597
598   const MachineOperand &MO1 = MI->getOperand(Op);
599   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
600   O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
601 }
602
603 void
604 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
605   const MachineOperand &MO = MI->getOperand(Op);
606   uint32_t v = ~MO.getImm();
607   int32_t lsb = CountTrailingZeros_32(v);
608   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
609   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
610   O << "#" << lsb << ", #" << width;
611 }
612
613 //===--------------------------------------------------------------------===//
614
615 void
616 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
617   const MachineOperand &MO1 = MI->getOperand(Op);
618   const MachineOperand &MO2 = MI->getOperand(Op+1);
619   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
620   O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
621 }
622
623 void
624 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
625                                             unsigned Scale) {
626   const MachineOperand &MO1 = MI->getOperand(Op);
627   const MachineOperand &MO2 = MI->getOperand(Op+1);
628   const MachineOperand &MO3 = MI->getOperand(Op+2);
629
630   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
631     printOperand(MI, Op);
632     return;
633   }
634
635   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
636   if (MO3.getReg())
637     O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
638   else if (unsigned ImmOffs = MO2.getImm()) {
639     O << ", #" << ImmOffs;
640     if (Scale > 1)
641       O << " * " << Scale;
642   }
643   O << "]";
644 }
645
646 void
647 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
648   printThumbAddrModeRI5Operand(MI, Op, 1);
649 }
650 void
651 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
652   printThumbAddrModeRI5Operand(MI, Op, 2);
653 }
654 void
655 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
656   printThumbAddrModeRI5Operand(MI, Op, 4);
657 }
658
659 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
660   const MachineOperand &MO1 = MI->getOperand(Op);
661   const MachineOperand &MO2 = MI->getOperand(Op+1);
662   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
663   if (unsigned ImmOffs = MO2.getImm())
664     O << ", #" << ImmOffs << " * 4";
665   O << "]";
666 }
667
668 //===--------------------------------------------------------------------===//
669
670 /// printT2SOImmOperand - T2SOImm is:
671 ///  1. a 4-bit splat control value and 8 bit immediate value
672 ///  2. a 5-bit rotate amount and a non-zero 8-bit immediate value
673 ///     represented by a normalizedin 7-bit value (msb is always 1)
674 void ARMAsmPrinter::printT2SOImmOperand(const MachineInstr *MI, int OpNum) {
675   const MachineOperand &MO = MI->getOperand(OpNum);
676   assert(MO.isImm() && "Not a valid so_imm value!");
677
678   unsigned Imm = ARM_AM::getT2SOImmValDecode(MO.getImm());  
679   // Always print the immediate directly, as the "rotate" form
680   // is deprecated in some contexts.
681   O << "#" << Imm;
682 }
683
684 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
685 // register with shift forms.
686 // REG 0   0           - e.g. R5
687 // REG IMM, SH_OPC     - e.g. R5, LSL #3
688 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
689   const MachineOperand &MO1 = MI->getOperand(OpNum);
690   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
691
692   unsigned Reg = MO1.getReg();
693   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
694   O << TM.getRegisterInfo()->getAsmName(Reg);
695
696   // Print the shift opc.
697   O << ", "
698     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
699     << " ";
700
701   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
702   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
703 }
704
705 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
706                                                 int OpNum) {
707   const MachineOperand &MO1 = MI->getOperand(OpNum);
708   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
709
710   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
711
712   unsigned OffImm = MO2.getImm();
713   if (OffImm)  // Don't print +0.
714     O << ", #+" << OffImm;
715   O << "]";
716 }
717
718 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
719                                                int OpNum) {
720   const MachineOperand &MO1 = MI->getOperand(OpNum);
721   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
722
723   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
724
725   int32_t OffImm = (int32_t)MO2.getImm();
726   // Don't print +0.
727   if (OffImm < 0)
728     O << ", #-" << -OffImm;
729   else if (OffImm > 0)
730     O << ", #+" << OffImm;
731   O << "]";
732 }
733
734 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
735                                                 int OpNum) {
736   const MachineOperand &MO1 = MI->getOperand(OpNum);
737   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
738   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
739
740   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
741
742   if (MO2.getReg()) {
743     O << ", +"
744       << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
745
746     unsigned ShAmt = MO3.getImm();
747     if (ShAmt) {
748       assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
749       O << ", lsl #" << ShAmt;
750     }
751   }
752   O << "]";
753 }
754
755
756 //===--------------------------------------------------------------------===//
757
758 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
759   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
760   if (CC != ARMCC::AL)
761     O << ARMCondCodeToString(CC);
762 }
763
764 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
765   unsigned Reg = MI->getOperand(OpNum).getReg();
766   if (Reg) {
767     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
768     O << 's';
769   }
770 }
771
772 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
773   int Id = (int)MI->getOperand(OpNum).getImm();
774   O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
775 }
776
777 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
778   O << "{";
779   for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
780     printOperand(MI, i);
781     if (i != e-1) O << ", ";
782   }
783   O << "}";
784 }
785
786 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
787                                        const char *Modifier) {
788   assert(Modifier && "This operand only works with a modifier!");
789   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
790   // data itself.
791   if (!strcmp(Modifier, "label")) {
792     unsigned ID = MI->getOperand(OpNum).getImm();
793     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
794       << '_' << ID << ":\n";
795   } else {
796     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
797     unsigned CPI = MI->getOperand(OpNum).getIndex();
798
799     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
800     
801     if (MCPE.isMachineConstantPoolEntry()) {
802       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
803     } else {
804       EmitGlobalConstant(MCPE.Val.ConstVal);
805     }
806   }
807 }
808
809 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
810   const MachineOperand &MO1 = MI->getOperand(OpNum);
811   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
812   unsigned JTI = MO1.getIndex();
813   O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
814     << '_' << JTI << '_' << MO2.getImm() << ":\n";
815
816   const char *JTEntryDirective = TAI->getJumpTableDirective();
817   if (!JTEntryDirective)
818     JTEntryDirective = TAI->getData32bitsDirective();
819
820   const MachineFunction *MF = MI->getParent()->getParent();
821   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
822   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
823   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
824   bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
825   std::set<MachineBasicBlock*> JTSets;
826   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
827     MachineBasicBlock *MBB = JTBBs[i];
828     if (UseSet && JTSets.insert(MBB).second)
829       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
830
831     O << JTEntryDirective << ' ';
832     if (UseSet)
833       O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
834         << '_' << JTI << '_' << MO2.getImm()
835         << "_set_" << MBB->getNumber();
836     else if (TM.getRelocationModel() == Reloc::PIC_) {
837       printBasicBlockLabel(MBB, false, false, false);
838       // If the arch uses custom Jump Table directives, don't calc relative to JT
839       if (!TAI->getJumpTableDirective()) 
840         O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
841           << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
842     } else
843       printBasicBlockLabel(MBB, false, false, false);
844     if (i != e-1)
845       O << '\n';
846   }
847 }
848
849
850 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
851                                     unsigned AsmVariant, const char *ExtraCode){
852   // Does this asm operand have a single letter operand modifier?
853   if (ExtraCode && ExtraCode[0]) {
854     if (ExtraCode[1] != 0) return true; // Unknown modifier.
855     
856     switch (ExtraCode[0]) {
857     default: return true;  // Unknown modifier.
858     case 'a': // Don't print "#" before a global var name or constant.
859     case 'c': // Don't print "$" before a global var name or constant.
860       printOperand(MI, OpNum, "no_hash");
861       return false;
862     case 'P': // Print a VFP double precision register.
863       printOperand(MI, OpNum);
864       return false;
865     case 'Q':
866       if (TM.getTargetData()->isLittleEndian())
867         break;
868       // Fallthrough
869     case 'R':
870       if (TM.getTargetData()->isBigEndian())
871         break;
872       // Fallthrough
873     case 'H': // Write second word of DI / DF reference.  
874       // Verify that this operand has two consecutive registers.
875       if (!MI->getOperand(OpNum).isReg() ||
876           OpNum+1 == MI->getNumOperands() ||
877           !MI->getOperand(OpNum+1).isReg())
878         return true;
879       ++OpNum;   // Return the high-part.
880     }
881   }
882   
883   printOperand(MI, OpNum);
884   return false;
885 }
886
887 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
888                                           unsigned OpNum, unsigned AsmVariant,
889                                           const char *ExtraCode) {
890   if (ExtraCode && ExtraCode[0])
891     return true; // Unknown modifier.
892   printAddrMode2Operand(MI, OpNum);
893   return false;
894 }
895
896 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
897   ++EmittedInsts;
898
899   int Opc = MI->getOpcode();
900   switch (Opc) {
901   case ARM::CONSTPOOL_ENTRY:
902     if (!InCPMode && AFI->isThumbFunction()) {
903       EmitAlignment(2);
904       InCPMode = true;
905     }
906     break;
907   default: {
908     if (InCPMode && AFI->isThumbFunction())
909       InCPMode = false;
910   }}
911
912   // Call the autogenerated instruction printer routines.
913   printInstruction(MI);
914 }
915
916 bool ARMAsmPrinter::doInitialization(Module &M) {
917
918   bool Result = AsmPrinter::doInitialization(M);
919   DW = getAnalysisIfAvailable<DwarfWriter>();
920
921   // Thumb-2 instructions are supported only in unified assembler syntax mode.
922   if (Subtarget->hasThumb2())
923     O << "\t.syntax unified\n";
924
925   // Emit ARM Build Attributes
926   if (Subtarget->isTargetELF()) {
927     // CPU Type
928     std::string CPUString = Subtarget->getCPUString();
929     if (CPUString != "generic")
930       O << "\t.cpu " << CPUString << '\n';
931
932     // FIXME: Emit FPU type
933     if (Subtarget->hasVFP2())
934       O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
935
936     // Signal various FP modes.
937     if (!UnsafeFPMath)
938       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
939         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
940
941     if (FiniteOnlyFPMath())
942       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
943     else
944       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
945
946     // 8-bytes alignment stuff.
947     O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
948       << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
949
950     // FIXME: Should we signal R9 usage?
951   }
952
953   return Result;
954 }
955
956 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
957 /// Don't print things like \\n or \\0.
958 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
959   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
960        Name != E; ++Name)
961     if (isprint(*Name))
962       OS << *Name;
963 }
964
965 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
966   const TargetData *TD = TM.getTargetData();
967
968   if (!GVar->hasInitializer())   // External global require no code
969     return;
970
971   // Check to see if this is a special global used by LLVM, if so, emit it.
972
973   if (EmitSpecialLLVMGlobal(GVar)) {
974     if (Subtarget->isTargetDarwin() &&
975         TM.getRelocationModel() == Reloc::Static) {
976       if (GVar->getName() == "llvm.global_ctors")
977         O << ".reference .constructors_used\n";
978       else if (GVar->getName() == "llvm.global_dtors")
979         O << ".reference .destructors_used\n";
980     }
981     return;
982   }
983
984   std::string name = Mang->getValueName(GVar);
985   Constant *C = GVar->getInitializer();
986   if (isa<MDNode>(C) || isa<MDString>(C))
987     return;
988   const Type *Type = C->getType();
989   unsigned Size = TD->getTypeAllocSize(Type);
990   unsigned Align = TD->getPreferredAlignmentLog(GVar);
991   bool isDarwin = Subtarget->isTargetDarwin();
992
993   printVisibility(name, GVar->getVisibility());
994
995   if (Subtarget->isTargetELF())
996     O << "\t.type " << name << ",%object\n";
997
998   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
999       !(isDarwin &&
1000         TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
1001     // FIXME: This seems to be pretty darwin-specific
1002
1003     if (GVar->hasExternalLinkage()) {
1004       SwitchToSection(TAI->SectionForGlobal(GVar));
1005       if (const char *Directive = TAI->getZeroFillDirective()) {
1006         O << "\t.globl\t" << name << "\n";
1007         O << Directive << "__DATA, __common, " << name << ", "
1008           << Size << ", " << Align << "\n";
1009         return;
1010       }
1011     }
1012
1013     if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
1014       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1015
1016       if (isDarwin) {
1017         if (GVar->hasLocalLinkage()) {
1018           O << TAI->getLCOMMDirective()  << name << "," << Size
1019             << ',' << Align;
1020         } else if (GVar->hasCommonLinkage()) {
1021           O << TAI->getCOMMDirective()  << name << "," << Size
1022             << ',' << Align;
1023         } else {
1024           SwitchToSection(TAI->SectionForGlobal(GVar));
1025           O << "\t.globl " << name << '\n'
1026             << TAI->getWeakDefDirective() << name << '\n';
1027           EmitAlignment(Align, GVar);
1028           O << name << ":";
1029           if (VerboseAsm) {
1030             O << "\t\t\t\t" << TAI->getCommentString() << ' ';
1031             PrintUnmangledNameSafely(GVar, O);
1032           }
1033           O << '\n';
1034           EmitGlobalConstant(C);
1035           return;
1036         }
1037       } else if (TAI->getLCOMMDirective() != NULL) {
1038         if (GVar->hasLocalLinkage()) {
1039           O << TAI->getLCOMMDirective() << name << "," << Size;
1040         } else {
1041           O << TAI->getCOMMDirective()  << name << "," << Size;
1042           if (TAI->getCOMMDirectiveTakesAlignment())
1043             O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1044         }
1045       } else {
1046         SwitchToSection(TAI->SectionForGlobal(GVar));
1047         if (GVar->hasLocalLinkage())
1048           O << "\t.local\t" << name << "\n";
1049         O << TAI->getCOMMDirective()  << name << "," << Size;
1050         if (TAI->getCOMMDirectiveTakesAlignment())
1051           O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1052       }
1053       if (VerboseAsm) {
1054         O << "\t\t" << TAI->getCommentString() << " ";
1055         PrintUnmangledNameSafely(GVar, O);
1056       }
1057       O << "\n";
1058       return;
1059     }
1060   }
1061
1062   SwitchToSection(TAI->SectionForGlobal(GVar));
1063   switch (GVar->getLinkage()) {
1064    case GlobalValue::CommonLinkage:
1065    case GlobalValue::LinkOnceAnyLinkage:
1066    case GlobalValue::LinkOnceODRLinkage:
1067    case GlobalValue::WeakAnyLinkage:
1068    case GlobalValue::WeakODRLinkage:
1069     if (isDarwin) {
1070       O << "\t.globl " << name << "\n"
1071         << "\t.weak_definition " << name << "\n";
1072     } else {
1073       O << "\t.weak " << name << "\n";
1074     }
1075     break;
1076    case GlobalValue::AppendingLinkage:
1077     // FIXME: appending linkage variables should go into a section of
1078     // their name or something.  For now, just emit them as external.
1079    case GlobalValue::ExternalLinkage:
1080     O << "\t.globl " << name << "\n";
1081     // FALL THROUGH
1082    case GlobalValue::PrivateLinkage:
1083    case GlobalValue::InternalLinkage:
1084     break;
1085    default:
1086     assert(0 && "Unknown linkage type!");
1087     break;
1088   }
1089
1090   EmitAlignment(Align, GVar);
1091   O << name << ":";
1092   if (VerboseAsm) {
1093     O << "\t\t\t\t" << TAI->getCommentString() << " ";
1094     PrintUnmangledNameSafely(GVar, O);
1095   }
1096   O << "\n";
1097   if (TAI->hasDotTypeDotSizeDirective())
1098     O << "\t.size " << name << ", " << Size << "\n";
1099
1100   EmitGlobalConstant(C);
1101   O << '\n';
1102 }
1103
1104
1105 bool ARMAsmPrinter::doFinalization(Module &M) {
1106   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1107        I != E; ++I)
1108     printModuleLevelGV(I);
1109
1110   if (Subtarget->isTargetDarwin()) {
1111     SwitchToDataSection("");
1112
1113     // Output stubs for dynamically-linked functions
1114     for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1115          i != e; ++i) {
1116       if (TM.getRelocationModel() == Reloc::PIC_)
1117         SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
1118                             "none,16", 0);
1119       else
1120         SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
1121                             "none,12", 0);
1122
1123       EmitAlignment(2);
1124       O << "\t.code\t32\n";
1125
1126       const char *p = i->getKeyData();
1127       printSuffixedName(p, "$stub");
1128       O << ":\n";
1129       O << "\t.indirect_symbol " << p << "\n";
1130       O << "\tldr ip, ";
1131       printSuffixedName(p, "$slp");
1132       O << "\n";
1133       if (TM.getRelocationModel() == Reloc::PIC_) {
1134         printSuffixedName(p, "$scv");
1135         O << ":\n";
1136         O << "\tadd ip, pc, ip\n";
1137       }
1138       O << "\tldr pc, [ip, #0]\n";
1139       printSuffixedName(p, "$slp");
1140       O << ":\n";
1141       O << "\t.long\t";
1142       printSuffixedName(p, "$lazy_ptr");
1143       if (TM.getRelocationModel() == Reloc::PIC_) {
1144         O << "-(";
1145         printSuffixedName(p, "$scv");
1146         O << "+8)\n";
1147       } else
1148         O << "\n";
1149       SwitchToDataSection(".lazy_symbol_pointer", 0);
1150       printSuffixedName(p, "$lazy_ptr");
1151       O << ":\n";
1152       O << "\t.indirect_symbol " << p << "\n";
1153       O << "\t.long\tdyld_stub_binding_helper\n";
1154     }
1155     O << "\n";
1156
1157     // Output non-lazy-pointers for external and common global variables.
1158     if (!GVNonLazyPtrs.empty()) {
1159       SwitchToDataSection("\t.non_lazy_symbol_pointer", 0);
1160       for (StringSet<>::iterator i =  GVNonLazyPtrs.begin(),
1161              e = GVNonLazyPtrs.end(); i != e; ++i) {
1162         const char *p = i->getKeyData();
1163         printSuffixedName(p, "$non_lazy_ptr");
1164         O << ":\n";
1165         O << "\t.indirect_symbol " << p << "\n";
1166         O << "\t.long\t0\n";
1167       }
1168     }
1169
1170     if (!HiddenGVNonLazyPtrs.empty()) {
1171       SwitchToSection(TAI->getDataSection());
1172       for (StringSet<>::iterator i = HiddenGVNonLazyPtrs.begin(),
1173              e = HiddenGVNonLazyPtrs.end(); i != e; ++i) {
1174         const char *p = i->getKeyData();
1175         EmitAlignment(2);
1176         printSuffixedName(p, "$non_lazy_ptr");
1177         O << ":\n";
1178         O << "\t.long " << p << "\n";
1179       }
1180     }
1181
1182
1183     // Funny Darwin hack: This flag tells the linker that no global symbols
1184     // contain code that falls through to other global symbols (e.g. the obvious
1185     // implementation of multiple entry points).  If this doesn't occur, the
1186     // linker can safely perform dead code stripping.  Since LLVM never
1187     // generates code that does this, it is always safe to set.
1188     O << "\t.subsections_via_symbols\n";
1189   }
1190
1191   return AsmPrinter::doFinalization(M);
1192 }
1193
1194 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1195 /// assembly code for a MachineFunction to the given output stream,
1196 /// using the given target machine description.  This should work
1197 /// regardless of whether the function is in SSA form.
1198 ///
1199 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1200                                              ARMBaseTargetMachine &tm,
1201                                              CodeGenOpt::Level OptLevel,
1202                                              bool verbose) {
1203   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
1204 }
1205
1206 namespace {
1207   static struct Register {
1208     Register() {
1209       ARMBaseTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1210     }
1211   } Registrator;
1212 }
1213
1214 // Force static initialization.
1215 extern "C" void LLVMInitializeARMAsmPrinter() { }