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