lower ARM::MOVi32imm properly.
[oota-llvm.git] / lib / Target / ARM / AsmPrinter / ARMAsmPrinter.cpp
1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
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 "ARMAddressingModes.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMInstPrinter.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "ARMMCInstLower.h"
23 #include "ARMTargetMachine.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Module.h"
26 #include "llvm/Assembly/Writer.h"
27 #include "llvm/CodeGen/AsmPrinter.h"
28 #include "llvm/CodeGen/DwarfWriter.h"
29 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCContext.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/MC/MCSectionMachO.h"
36 #include "llvm/MC/MCStreamer.h"
37 #include "llvm/MC/MCSymbol.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetLoweringObjectFile.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetOptions.h"
42 #include "llvm/Target/TargetRegistry.h"
43 #include "llvm/ADT/SmallPtrSet.h"
44 #include "llvm/ADT/SmallString.h"
45 #include "llvm/ADT/Statistic.h"
46 #include "llvm/ADT/StringSet.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/FormattedStream.h"
50 #include "llvm/Support/Mangler.h"
51 #include "llvm/Support/MathExtras.h"
52 #include <cctype>
53 using namespace llvm;
54
55 STATISTIC(EmittedInsts, "Number of machine instrs printed");
56
57 static cl::opt<bool>
58 EnableMCInst("enable-arm-mcinst-printer", cl::Hidden,
59             cl::desc("enable experimental asmprinter gunk in the arm backend"));
60
61 namespace {
62   class ARMAsmPrinter : public AsmPrinter {
63
64     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
65     /// make the right decision when printing asm code for different targets.
66     const ARMSubtarget *Subtarget;
67
68     /// AFI - Keep a pointer to ARMFunctionInfo for the current
69     /// MachineFunction.
70     ARMFunctionInfo *AFI;
71
72     /// MCP - Keep a pointer to constantpool entries of the current
73     /// MachineFunction.
74     const MachineConstantPool *MCP;
75
76   public:
77     explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
78                            const MCAsmInfo *T, bool V)
79       : AsmPrinter(O, TM, T, V), AFI(NULL), MCP(NULL) {
80       Subtarget = &TM.getSubtarget<ARMSubtarget>();
81     }
82
83     virtual const char *getPassName() const {
84       return "ARM Assembly Printer";
85     }
86     
87     void printMCInst(const MCInst *MI) {
88       ARMInstPrinter(O, *MAI, VerboseAsm).printInstruction(MI);
89     }
90     
91     void printInstructionThroughMCStreamer(const MachineInstr *MI);
92     
93
94     void printOperand(const MachineInstr *MI, int OpNum,
95                       const char *Modifier = 0);
96     void printSOImmOperand(const MachineInstr *MI, int OpNum);
97     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum);
98     void printSORegOperand(const MachineInstr *MI, int OpNum);
99     void printAddrMode2Operand(const MachineInstr *MI, int OpNum);
100     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum);
101     void printAddrMode3Operand(const MachineInstr *MI, int OpNum);
102     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum);
103     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,
104                                const char *Modifier = 0);
105     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,
106                                const char *Modifier = 0);
107     void printAddrMode6Operand(const MachineInstr *MI, int OpNum);
108     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
109                                 const char *Modifier = 0);
110     void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum);
111
112     void printThumbITMask(const MachineInstr *MI, int OpNum);
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 printT2SOOperand(const MachineInstr *MI, int OpNum);
122     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum);
123     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum);
124     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum);
125     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum);
126     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum);
127
128     void printPredicateOperand(const MachineInstr *MI, int OpNum);
129     void printSBitModifierOperand(const MachineInstr *MI, int OpNum);
130     void printPCLabel(const MachineInstr *MI, int OpNum);
131     void printRegisterList(const MachineInstr *MI, int OpNum);
132     void printCPInstOperand(const MachineInstr *MI, int OpNum,
133                             const char *Modifier);
134     void printJTBlockOperand(const MachineInstr *MI, int OpNum);
135     void printJT2BlockOperand(const MachineInstr *MI, int OpNum);
136     void printTBAddrMode(const MachineInstr *MI, int OpNum);
137     void printNoHashImmediate(const MachineInstr *MI, int OpNum);
138
139     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
140                                  unsigned AsmVariant, const char *ExtraCode);
141     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
142                                        unsigned AsmVariant,
143                                        const char *ExtraCode);
144
145     void PrintGlobalVariable(const GlobalVariable* GVar);
146     void printInstruction(const MachineInstr *MI);  // autogenerated.
147     static const char *getRegisterName(unsigned RegNo);
148
149     void printMachineInstruction(const MachineInstr *MI);
150     bool runOnMachineFunction(MachineFunction &F);
151     void EmitStartOfAsmFile(Module &M);
152     void EmitEndOfAsmFile(Module &M);
153
154     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
155     /// the .s file.
156     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
157       printDataDirective(MCPV->getType());
158
159       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
160       GlobalValue *GV = ACPV->getGV();
161       std::string Name;
162
163       if (ACPV->isLSDA()) {
164         SmallString<16> LSDAName;
165         raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
166           "_LSDA_" << getFunctionNumber();
167         Name = LSDAName.str();
168       } else if (GV) {
169         bool isIndirect = Subtarget->isTargetDarwin() &&
170           Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
171         if (!isIndirect)
172           Name = Mang->getMangledName(GV);
173         else {
174           // FIXME: Remove this when Darwin transition to @GOT like syntax.
175           Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
176           MCSymbol *Sym = OutContext.GetOrCreateSymbol(StringRef(Name));
177           
178           MachineModuleInfoMachO &MMIMachO =
179             MMI->getObjFileInfo<MachineModuleInfoMachO>();
180           const MCSymbol *&StubSym =
181             GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
182                                         MMIMachO.getGVStubEntry(Sym);
183           if (StubSym == 0) {
184             SmallString<128> NameStr;
185             Mang->getNameWithPrefix(NameStr, GV, false);
186             StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
187           }
188         }
189       } else
190         Name = Mang->makeNameProper(ACPV->getSymbol());
191       O << Name;
192
193       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
194       if (ACPV->getPCAdjustment() != 0) {
195         O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
196           << ACPV->getLabelId()
197           << "+" << (unsigned)ACPV->getPCAdjustment();
198          if (ACPV->mustAddCurrentAddress())
199            O << "-.";
200          O << ")";
201       }
202       O << "\n";
203     }
204
205     void getAnalysisUsage(AnalysisUsage &AU) const {
206       AsmPrinter::getAnalysisUsage(AU);
207       AU.setPreservesAll();
208       AU.addRequired<MachineModuleInfo>();
209       AU.addRequired<DwarfWriter>();
210     }
211   };
212 } // end of anonymous namespace
213
214 #include "ARMGenAsmWriter.inc"
215
216 /// runOnMachineFunction - This uses the printInstruction()
217 /// method to print assembly for each instruction.
218 ///
219 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
220   this->MF = &MF;
221
222   AFI = MF.getInfo<ARMFunctionInfo>();
223   MCP = MF.getConstantPool();
224
225   SetupMachineFunction(MF);
226   O << "\n";
227
228   // NOTE: we don't print out constant pools here, they are handled as
229   // instructions.
230
231   O << '\n';
232
233   // Print out labels for the function.
234   const Function *F = MF.getFunction();
235   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
236
237   switch (F->getLinkage()) {
238   default: llvm_unreachable("Unknown linkage type!");
239   case Function::PrivateLinkage:
240   case Function::InternalLinkage:
241     break;
242   case Function::ExternalLinkage:
243     O << "\t.globl\t" << CurrentFnName << "\n";
244     break;
245   case Function::LinkerPrivateLinkage:
246   case Function::WeakAnyLinkage:
247   case Function::WeakODRLinkage:
248   case Function::LinkOnceAnyLinkage:
249   case Function::LinkOnceODRLinkage:
250     if (Subtarget->isTargetDarwin()) {
251       O << "\t.globl\t" << CurrentFnName << "\n";
252       O << "\t.weak_definition\t" << CurrentFnName << "\n";
253     } else {
254       O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
255     }
256     break;
257   }
258
259   printVisibility(CurrentFnName, F->getVisibility());
260
261   unsigned FnAlign = 1 << MF.getAlignment();  // MF alignment is log2.
262   if (AFI->isThumbFunction()) {
263     EmitAlignment(FnAlign, F, AFI->getAlign());
264     O << "\t.code\t16\n";
265     O << "\t.thumb_func";
266     if (Subtarget->isTargetDarwin())
267       O << "\t" << CurrentFnName;
268     O << "\n";
269   } else {
270     EmitAlignment(FnAlign, F);
271   }
272
273   O << CurrentFnName << ":\n";
274   // Emit pre-function debug information.
275   DW->BeginFunction(&MF);
276
277   if (Subtarget->isTargetDarwin()) {
278     // If the function is empty, then we need to emit *something*. Otherwise,
279     // the function's label might be associated with something that it wasn't
280     // meant to be associated with. We emit a noop in this situation.
281     MachineFunction::iterator I = MF.begin();
282
283     if (++I == MF.end() && MF.front().empty())
284       O << "\tnop\n";
285   }
286
287   // Print out code for the function.
288   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
289        I != E; ++I) {
290     // Print a label for the basic block.
291     if (I != MF.begin())
292       EmitBasicBlockStart(I);
293
294     // Print the assembly for the instruction.
295     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
296          II != E; ++II)
297       printMachineInstruction(II);
298   }
299
300   if (MAI->hasDotTypeDotSizeDirective())
301     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
302
303   // Emit post-function debug information.
304   DW->EndFunction(&MF);
305
306   return false;
307 }
308
309 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
310                                  const char *Modifier) {
311   const MachineOperand &MO = MI->getOperand(OpNum);
312   switch (MO.getType()) {
313   default:
314     assert(0 && "<unknown operand type>");
315   case MachineOperand::MO_Register: {
316     unsigned Reg = MO.getReg();
317     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
318     if (Modifier && strcmp(Modifier, "dregpair") == 0) {
319       unsigned DRegLo = TRI->getSubReg(Reg, 5); // arm_dsubreg_0
320       unsigned DRegHi = TRI->getSubReg(Reg, 6); // arm_dsubreg_1
321       O << '{'
322         << getRegisterName(DRegLo) << ',' << getRegisterName(DRegHi)
323         << '}';
324     } else if (Modifier && strcmp(Modifier, "lane") == 0) {
325       unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
326       unsigned DReg = TRI->getMatchingSuperReg(Reg, RegNum & 1 ? 2 : 1,
327                                                &ARM::DPR_VFP2RegClass);
328       O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
329     } else {
330       O << getRegisterName(Reg);
331     }
332     break;
333   }
334   case MachineOperand::MO_Immediate: {
335     int64_t Imm = MO.getImm();
336     O << '#';
337     if (Modifier) {
338       if (strcmp(Modifier, "lo16") == 0)
339         O << ":lower16:";
340       else if (strcmp(Modifier, "hi16") == 0)
341         O << ":upper16:";
342     }
343     O << Imm;
344     break;
345   }
346   case MachineOperand::MO_MachineBasicBlock:
347     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
348     return;
349   case MachineOperand::MO_GlobalAddress: {
350     bool isCallOp = Modifier && !strcmp(Modifier, "call");
351     GlobalValue *GV = MO.getGlobal();
352     O << Mang->getMangledName(GV);
353
354     printOffset(MO.getOffset());
355
356     if (isCallOp && Subtarget->isTargetELF() &&
357         TM.getRelocationModel() == Reloc::PIC_)
358       O << "(PLT)";
359     break;
360   }
361   case MachineOperand::MO_ExternalSymbol: {
362     bool isCallOp = Modifier && !strcmp(Modifier, "call");
363     std::string Name = Mang->makeNameProper(MO.getSymbolName());
364
365     O << Name;
366     if (isCallOp && Subtarget->isTargetELF() &&
367         TM.getRelocationModel() == Reloc::PIC_)
368       O << "(PLT)";
369     break;
370   }
371   case MachineOperand::MO_ConstantPoolIndex:
372     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
373       << '_' << MO.getIndex();
374     break;
375   case MachineOperand::MO_JumpTableIndex:
376     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
377       << '_' << MO.getIndex();
378     break;
379   }
380 }
381
382 static void printSOImm(formatted_raw_ostream &O, int64_t V, bool VerboseAsm,
383                        const MCAsmInfo *MAI) {
384   // Break it up into two parts that make up a shifter immediate.
385   V = ARM_AM::getSOImmVal(V);
386   assert(V != -1 && "Not a valid so_imm value!");
387
388   unsigned Imm = ARM_AM::getSOImmValImm(V);
389   unsigned Rot = ARM_AM::getSOImmValRot(V);
390
391   // Print low-level immediate formation info, per
392   // A5.1.3: "Data-processing operands - Immediate".
393   if (Rot) {
394     O << "#" << Imm << ", " << Rot;
395     // Pretty printed version.
396     if (VerboseAsm)
397       O << ' ' << MAI->getCommentString()
398         << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
399   } else {
400     O << "#" << Imm;
401   }
402 }
403
404 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
405 /// immediate in bits 0-7.
406 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
407   const MachineOperand &MO = MI->getOperand(OpNum);
408   assert(MO.isImm() && "Not a valid so_imm value!");
409   printSOImm(O, MO.getImm(), VerboseAsm, MAI);
410 }
411
412 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
413 /// followed by an 'orr' to materialize.
414 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
415   const MachineOperand &MO = MI->getOperand(OpNum);
416   assert(MO.isImm() && "Not a valid so_imm value!");
417   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
418   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
419   printSOImm(O, V1, VerboseAsm, MAI);
420   O << "\n\torr";
421   printPredicateOperand(MI, 2);
422   O << " ";
423   printOperand(MI, 0);
424   O << ", ";
425   printOperand(MI, 0);
426   O << ", ";
427   printSOImm(O, V2, VerboseAsm, MAI);
428 }
429
430 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
431 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
432 //    REG 0   0           - e.g. R5
433 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
434 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
435 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
436   const MachineOperand &MO1 = MI->getOperand(Op);
437   const MachineOperand &MO2 = MI->getOperand(Op+1);
438   const MachineOperand &MO3 = MI->getOperand(Op+2);
439
440   O << getRegisterName(MO1.getReg());
441
442   // Print the shift opc.
443   O << ", "
444     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
445     << " ";
446
447   if (MO2.getReg()) {
448     O << getRegisterName(MO2.getReg());
449     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
450   } else {
451     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
452   }
453 }
454
455 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
456   const MachineOperand &MO1 = MI->getOperand(Op);
457   const MachineOperand &MO2 = MI->getOperand(Op+1);
458   const MachineOperand &MO3 = MI->getOperand(Op+2);
459
460   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
461     printOperand(MI, Op);
462     return;
463   }
464
465   O << "[" << getRegisterName(MO1.getReg());
466
467   if (!MO2.getReg()) {
468     if (ARM_AM::getAM2Offset(MO3.getImm()))  // Don't print +0.
469       O << ", #"
470         << (char)ARM_AM::getAM2Op(MO3.getImm())
471         << ARM_AM::getAM2Offset(MO3.getImm());
472     O << "]";
473     return;
474   }
475
476   O << ", "
477     << (char)ARM_AM::getAM2Op(MO3.getImm())
478     << getRegisterName(MO2.getReg());
479
480   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
481     O << ", "
482       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
483       << " #" << ShImm;
484   O << "]";
485 }
486
487 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
488   const MachineOperand &MO1 = MI->getOperand(Op);
489   const MachineOperand &MO2 = MI->getOperand(Op+1);
490
491   if (!MO1.getReg()) {
492     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
493     assert(ImmOffs && "Malformed indexed load / store!");
494     O << "#"
495       << (char)ARM_AM::getAM2Op(MO2.getImm())
496       << ImmOffs;
497     return;
498   }
499
500   O << (char)ARM_AM::getAM2Op(MO2.getImm())
501     << getRegisterName(MO1.getReg());
502
503   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
504     O << ", "
505       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
506       << " #" << ShImm;
507 }
508
509 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
510   const MachineOperand &MO1 = MI->getOperand(Op);
511   const MachineOperand &MO2 = MI->getOperand(Op+1);
512   const MachineOperand &MO3 = MI->getOperand(Op+2);
513
514   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
515   O << "[" << getRegisterName(MO1.getReg());
516
517   if (MO2.getReg()) {
518     O << ", "
519       << (char)ARM_AM::getAM3Op(MO3.getImm())
520       << getRegisterName(MO2.getReg())
521       << "]";
522     return;
523   }
524
525   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
526     O << ", #"
527       << (char)ARM_AM::getAM3Op(MO3.getImm())
528       << ImmOffs;
529   O << "]";
530 }
531
532 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
533   const MachineOperand &MO1 = MI->getOperand(Op);
534   const MachineOperand &MO2 = MI->getOperand(Op+1);
535
536   if (MO1.getReg()) {
537     O << (char)ARM_AM::getAM3Op(MO2.getImm())
538       << getRegisterName(MO1.getReg());
539     return;
540   }
541
542   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
543   assert(ImmOffs && "Malformed indexed load / store!");
544   O << "#"
545     << (char)ARM_AM::getAM3Op(MO2.getImm())
546     << ImmOffs;
547 }
548
549 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
550                                           const char *Modifier) {
551   const MachineOperand &MO1 = MI->getOperand(Op);
552   const MachineOperand &MO2 = MI->getOperand(Op+1);
553   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
554   if (Modifier && strcmp(Modifier, "submode") == 0) {
555     if (MO1.getReg() == ARM::SP) {
556       // FIXME
557       bool isLDM = (MI->getOpcode() == ARM::LDM ||
558                     MI->getOpcode() == ARM::LDM_RET ||
559                     MI->getOpcode() == ARM::t2LDM ||
560                     MI->getOpcode() == ARM::t2LDM_RET);
561       O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
562     } else
563       O << ARM_AM::getAMSubModeStr(Mode);
564   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
565     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
566     if (Mode == ARM_AM::ia)
567       O << ".w";
568   } else {
569     printOperand(MI, Op);
570     if (ARM_AM::getAM4WBFlag(MO2.getImm()))
571       O << "!";
572   }
573 }
574
575 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
576                                           const char *Modifier) {
577   const MachineOperand &MO1 = MI->getOperand(Op);
578   const MachineOperand &MO2 = MI->getOperand(Op+1);
579
580   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
581     printOperand(MI, Op);
582     return;
583   }
584
585   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
586
587   if (Modifier && strcmp(Modifier, "submode") == 0) {
588     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
589     if (MO1.getReg() == ARM::SP) {
590       bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
591                      MI->getOpcode() == ARM::FLDMS);
592       O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
593     } else
594       O << ARM_AM::getAMSubModeStr(Mode);
595     return;
596   } else if (Modifier && strcmp(Modifier, "base") == 0) {
597     // Used for FSTM{D|S} and LSTM{D|S} operations.
598     O << getRegisterName(MO1.getReg());
599     if (ARM_AM::getAM5WBFlag(MO2.getImm()))
600       O << "!";
601     return;
602   }
603
604   O << "[" << getRegisterName(MO1.getReg());
605
606   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
607     O << ", #"
608       << (char)ARM_AM::getAM5Op(MO2.getImm())
609       << ImmOffs*4;
610   }
611   O << "]";
612 }
613
614 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op) {
615   const MachineOperand &MO1 = MI->getOperand(Op);
616   const MachineOperand &MO2 = MI->getOperand(Op+1);
617   const MachineOperand &MO3 = MI->getOperand(Op+2);
618
619   // FIXME: No support yet for specifying alignment.
620   O << "[" << getRegisterName(MO1.getReg()) << "]";
621
622   if (ARM_AM::getAM6WBFlag(MO3.getImm())) {
623     if (MO2.getReg() == 0)
624       O << "!";
625     else
626       O << ", " << getRegisterName(MO2.getReg());
627   }
628 }
629
630 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
631                                            const char *Modifier) {
632   if (Modifier && strcmp(Modifier, "label") == 0) {
633     printPCLabel(MI, Op+1);
634     return;
635   }
636
637   const MachineOperand &MO1 = MI->getOperand(Op);
638   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
639   O << "[pc, +" << getRegisterName(MO1.getReg()) << "]";
640 }
641
642 void
643 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op) {
644   const MachineOperand &MO = MI->getOperand(Op);
645   uint32_t v = ~MO.getImm();
646   int32_t lsb = CountTrailingZeros_32(v);
647   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
648   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
649   O << "#" << lsb << ", #" << width;
650 }
651
652 //===--------------------------------------------------------------------===//
653
654 void
655 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
656   // (3 - the number of trailing zeros) is the number of then / else.
657   unsigned Mask = MI->getOperand(Op).getImm();
658   unsigned NumTZ = CountTrailingZeros_32(Mask);
659   assert(NumTZ <= 3 && "Invalid IT mask!");
660   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
661     bool T = (Mask & (1 << Pos)) == 0;
662     if (T)
663       O << 't';
664     else
665       O << 'e';
666   }
667 }
668
669 void
670 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
671   const MachineOperand &MO1 = MI->getOperand(Op);
672   const MachineOperand &MO2 = MI->getOperand(Op+1);
673   O << "[" << getRegisterName(MO1.getReg());
674   O << ", " << getRegisterName(MO2.getReg()) << "]";
675 }
676
677 void
678 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
679                                             unsigned Scale) {
680   const MachineOperand &MO1 = MI->getOperand(Op);
681   const MachineOperand &MO2 = MI->getOperand(Op+1);
682   const MachineOperand &MO3 = MI->getOperand(Op+2);
683
684   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
685     printOperand(MI, Op);
686     return;
687   }
688
689   O << "[" << getRegisterName(MO1.getReg());
690   if (MO3.getReg())
691     O << ", " << getRegisterName(MO3.getReg());
692   else if (unsigned ImmOffs = MO2.getImm()) {
693     O << ", #" << ImmOffs;
694     if (Scale > 1)
695       O << " * " << Scale;
696   }
697   O << "]";
698 }
699
700 void
701 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
702   printThumbAddrModeRI5Operand(MI, Op, 1);
703 }
704 void
705 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
706   printThumbAddrModeRI5Operand(MI, Op, 2);
707 }
708 void
709 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
710   printThumbAddrModeRI5Operand(MI, Op, 4);
711 }
712
713 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
714   const MachineOperand &MO1 = MI->getOperand(Op);
715   const MachineOperand &MO2 = MI->getOperand(Op+1);
716   O << "[" << getRegisterName(MO1.getReg());
717   if (unsigned ImmOffs = MO2.getImm())
718     O << ", #" << ImmOffs << " * 4";
719   O << "]";
720 }
721
722 //===--------------------------------------------------------------------===//
723
724 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
725 // register with shift forms.
726 // REG 0   0           - e.g. R5
727 // REG IMM, SH_OPC     - e.g. R5, LSL #3
728 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum) {
729   const MachineOperand &MO1 = MI->getOperand(OpNum);
730   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
731
732   unsigned Reg = MO1.getReg();
733   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
734   O << getRegisterName(Reg);
735
736   // Print the shift opc.
737   O << ", "
738     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
739     << " ";
740
741   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
742   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
743 }
744
745 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
746                                                 int OpNum) {
747   const MachineOperand &MO1 = MI->getOperand(OpNum);
748   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
749
750   O << "[" << getRegisterName(MO1.getReg());
751
752   unsigned OffImm = MO2.getImm();
753   if (OffImm)  // Don't print +0.
754     O << ", #+" << OffImm;
755   O << "]";
756 }
757
758 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
759                                                int OpNum) {
760   const MachineOperand &MO1 = MI->getOperand(OpNum);
761   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
762
763   O << "[" << getRegisterName(MO1.getReg());
764
765   int32_t OffImm = (int32_t)MO2.getImm();
766   // Don't print +0.
767   if (OffImm < 0)
768     O << ", #-" << -OffImm;
769   else if (OffImm > 0)
770     O << ", #+" << OffImm;
771   O << "]";
772 }
773
774 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
775                                                  int OpNum) {
776   const MachineOperand &MO1 = MI->getOperand(OpNum);
777   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
778
779   O << "[" << getRegisterName(MO1.getReg());
780
781   int32_t OffImm = (int32_t)MO2.getImm() / 4;
782   // Don't print +0.
783   if (OffImm < 0)
784     O << ", #-" << -OffImm << " * 4";
785   else if (OffImm > 0)
786     O << ", #+" << OffImm << " * 4";
787   O << "]";
788 }
789
790 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
791                                                      int OpNum) {
792   const MachineOperand &MO1 = MI->getOperand(OpNum);
793   int32_t OffImm = (int32_t)MO1.getImm();
794   // Don't print +0.
795   if (OffImm < 0)
796     O << "#-" << -OffImm;
797   else if (OffImm > 0)
798     O << "#+" << OffImm;
799 }
800
801 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
802                                                 int OpNum) {
803   const MachineOperand &MO1 = MI->getOperand(OpNum);
804   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
805   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
806
807   O << "[" << getRegisterName(MO1.getReg());
808
809   assert(MO2.getReg() && "Invalid so_reg load / store address!");
810   O << ", " << getRegisterName(MO2.getReg());
811
812   unsigned ShAmt = MO3.getImm();
813   if (ShAmt) {
814     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
815     O << ", lsl #" << ShAmt;
816   }
817   O << "]";
818 }
819
820
821 //===--------------------------------------------------------------------===//
822
823 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum) {
824   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
825   if (CC != ARMCC::AL)
826     O << ARMCondCodeToString(CC);
827 }
828
829 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum){
830   unsigned Reg = MI->getOperand(OpNum).getReg();
831   if (Reg) {
832     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
833     O << 's';
834   }
835 }
836
837 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum) {
838   int Id = (int)MI->getOperand(OpNum).getImm();
839   O << MAI->getPrivateGlobalPrefix() << "PC" << Id;
840 }
841
842 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum) {
843   O << "{";
844   // Always skip the first operand, it's the optional (and implicit writeback).
845   for (unsigned i = OpNum+1, e = MI->getNumOperands(); i != e; ++i) {
846     if (MI->getOperand(i).isImplicit())
847       continue;
848     if ((int)i != OpNum+1) O << ", ";
849     printOperand(MI, i);
850   }
851   O << "}";
852 }
853
854 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
855                                        const char *Modifier) {
856   assert(Modifier && "This operand only works with a modifier!");
857   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
858   // data itself.
859   if (!strcmp(Modifier, "label")) {
860     unsigned ID = MI->getOperand(OpNum).getImm();
861     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
862       << '_' << ID << ":\n";
863   } else {
864     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
865     unsigned CPI = MI->getOperand(OpNum).getIndex();
866
867     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
868
869     if (MCPE.isMachineConstantPoolEntry()) {
870       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
871     } else {
872       EmitGlobalConstant(MCPE.Val.ConstVal);
873     }
874   }
875 }
876
877 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum) {
878   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
879
880   const MachineOperand &MO1 = MI->getOperand(OpNum);
881   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
882   unsigned JTI = MO1.getIndex();
883   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
884     << '_' << JTI << '_' << MO2.getImm() << ":\n";
885
886   const char *JTEntryDirective = MAI->getData32bitsDirective();
887
888   const MachineFunction *MF = MI->getParent()->getParent();
889   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
890   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
891   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
892   bool UseSet= MAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
893   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
894   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
895     MachineBasicBlock *MBB = JTBBs[i];
896     bool isNew = JTSets.insert(MBB);
897
898     if (UseSet && isNew)
899       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
900
901     O << JTEntryDirective << ' ';
902     if (UseSet)
903       O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
904         << '_' << JTI << '_' << MO2.getImm()
905         << "_set_" << MBB->getNumber();
906     else if (TM.getRelocationModel() == Reloc::PIC_) {
907       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
908       O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
909         << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
910     } else {
911       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
912     }
913     if (i != e-1)
914       O << '\n';
915   }
916 }
917
918 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum) {
919   const MachineOperand &MO1 = MI->getOperand(OpNum);
920   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
921   unsigned JTI = MO1.getIndex();
922   O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
923     << '_' << JTI << '_' << MO2.getImm() << ":\n";
924
925   const MachineFunction *MF = MI->getParent()->getParent();
926   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
927   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
928   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
929   bool ByteOffset = false, HalfWordOffset = false;
930   if (MI->getOpcode() == ARM::t2TBB)
931     ByteOffset = true;
932   else if (MI->getOpcode() == ARM::t2TBH)
933     HalfWordOffset = true;
934
935   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
936     MachineBasicBlock *MBB = JTBBs[i];
937     if (ByteOffset)
938       O << MAI->getData8bitsDirective();
939     else if (HalfWordOffset)
940       O << MAI->getData16bitsDirective();
941     if (ByteOffset || HalfWordOffset) {
942       O << '(';
943       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
944       O << "-" << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
945         << '_' << JTI << '_' << MO2.getImm() << ")/2";
946     } else {
947       O << "\tb.w ";
948       GetMBBSymbol(MBB->getNumber())->print(O, MAI);
949     }
950     if (i != e-1)
951       O << '\n';
952   }
953
954   // Make sure the instruction that follows TBB is 2-byte aligned.
955   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
956   if (ByteOffset && (JTBBs.size() & 1)) {
957     O << '\n';
958     EmitAlignment(1);
959   }
960 }
961
962 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum) {
963   O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
964   if (MI->getOpcode() == ARM::t2TBH)
965     O << ", lsl #1";
966   O << ']';
967 }
968
969 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum) {
970   O << MI->getOperand(OpNum).getImm();
971 }
972
973 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
974                                     unsigned AsmVariant, const char *ExtraCode){
975   // Does this asm operand have a single letter operand modifier?
976   if (ExtraCode && ExtraCode[0]) {
977     if (ExtraCode[1] != 0) return true; // Unknown modifier.
978
979     switch (ExtraCode[0]) {
980     default: return true;  // Unknown modifier.
981     case 'a': // Print as a memory address.
982       if (MI->getOperand(OpNum).isReg()) {
983         O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
984         return false;
985       }
986       // Fallthrough
987     case 'c': // Don't print "#" before an immediate operand.
988       if (!MI->getOperand(OpNum).isImm())
989         return true;
990       printNoHashImmediate(MI, OpNum);
991       return false;
992     case 'P': // Print a VFP double precision register.
993       printOperand(MI, OpNum);
994       return false;
995     case 'Q':
996       if (TM.getTargetData()->isLittleEndian())
997         break;
998       // Fallthrough
999     case 'R':
1000       if (TM.getTargetData()->isBigEndian())
1001         break;
1002       // Fallthrough
1003     case 'H': // Write second word of DI / DF reference.
1004       // Verify that this operand has two consecutive registers.
1005       if (!MI->getOperand(OpNum).isReg() ||
1006           OpNum+1 == MI->getNumOperands() ||
1007           !MI->getOperand(OpNum+1).isReg())
1008         return true;
1009       ++OpNum;   // Return the high-part.
1010     }
1011   }
1012
1013   printOperand(MI, OpNum);
1014   return false;
1015 }
1016
1017 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1018                                           unsigned OpNum, unsigned AsmVariant,
1019                                           const char *ExtraCode) {
1020   if (ExtraCode && ExtraCode[0])
1021     return true; // Unknown modifier.
1022
1023   const MachineOperand &MO = MI->getOperand(OpNum);
1024   assert(MO.isReg() && "unexpected inline asm memory operand");
1025   O << "[" << getRegisterName(MO.getReg()) << "]";
1026   return false;
1027 }
1028
1029 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
1030   ++EmittedInsts;
1031
1032   // Call the autogenerated instruction printer routines.
1033   processDebugLoc(MI, true);
1034   
1035   if (EnableMCInst) {
1036     printInstructionThroughMCStreamer(MI);
1037   } else {
1038     int Opc = MI->getOpcode();
1039     if (Opc == ARM::CONSTPOOL_ENTRY)
1040       EmitAlignment(2);
1041     
1042     printInstruction(MI);
1043   }
1044   
1045   if (VerboseAsm && !MI->getDebugLoc().isUnknown())
1046     EmitComments(*MI);
1047   O << '\n';
1048   processDebugLoc(MI, false);
1049 }
1050
1051 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1052   if (Subtarget->isTargetDarwin()) {
1053     Reloc::Model RelocM = TM.getRelocationModel();
1054     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1055       // Declare all the text sections up front (before the DWARF sections
1056       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1057       // them together at the beginning of the object file.  This helps
1058       // avoid out-of-range branches that are due a fundamental limitation of
1059       // the way symbol offsets are encoded with the current Darwin ARM
1060       // relocations.
1061       TargetLoweringObjectFileMachO &TLOFMacho = 
1062         static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1063       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1064       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1065       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1066       if (RelocM == Reloc::DynamicNoPIC) {
1067         const MCSection *sect =
1068           TLOFMacho.getMachOSection("__TEXT", "__symbol_stub4",
1069                                     MCSectionMachO::S_SYMBOL_STUBS,
1070                                     12, SectionKind::getText());
1071         OutStreamer.SwitchSection(sect);
1072       } else {
1073         const MCSection *sect =
1074           TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub4",
1075                                     MCSectionMachO::S_SYMBOL_STUBS,
1076                                     16, SectionKind::getText());
1077         OutStreamer.SwitchSection(sect);
1078       }
1079     }
1080   }
1081
1082   // Use unified assembler syntax mode for Thumb.
1083   if (Subtarget->isThumb())
1084     O << "\t.syntax unified\n";
1085
1086   // Emit ARM Build Attributes
1087   if (Subtarget->isTargetELF()) {
1088     // CPU Type
1089     std::string CPUString = Subtarget->getCPUString();
1090     if (CPUString != "generic")
1091       O << "\t.cpu " << CPUString << '\n';
1092
1093     // FIXME: Emit FPU type
1094     if (Subtarget->hasVFP2())
1095       O << "\t.eabi_attribute " << ARMBuildAttrs::VFP_arch << ", 2\n";
1096
1097     // Signal various FP modes.
1098     if (!UnsafeFPMath)
1099       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_denormal << ", 1\n"
1100         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_exceptions << ", 1\n";
1101
1102     if (FiniteOnlyFPMath())
1103       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 1\n";
1104     else
1105       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_FP_number_model << ", 3\n";
1106
1107     // 8-bytes alignment stuff.
1108     O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_needed << ", 1\n"
1109       << "\t.eabi_attribute " << ARMBuildAttrs::ABI_align8_preserved << ", 1\n";
1110
1111     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1112     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard)
1113       O << "\t.eabi_attribute " << ARMBuildAttrs::ABI_HardFP_use << ", 3\n"
1114         << "\t.eabi_attribute " << ARMBuildAttrs::ABI_VFP_args << ", 1\n";
1115
1116     // FIXME: Should we signal R9 usage?
1117   }
1118 }
1119
1120 void ARMAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
1121   const TargetData *TD = TM.getTargetData();
1122
1123   if (!GVar->hasInitializer())   // External global require no code
1124     return;
1125
1126   // Check to see if this is a special global used by LLVM, if so, emit it.
1127
1128   if (EmitSpecialLLVMGlobal(GVar)) {
1129     if (Subtarget->isTargetDarwin() &&
1130         TM.getRelocationModel() == Reloc::Static) {
1131       if (GVar->getName() == "llvm.global_ctors")
1132         O << ".reference .constructors_used\n";
1133       else if (GVar->getName() == "llvm.global_dtors")
1134         O << ".reference .destructors_used\n";
1135     }
1136     return;
1137   }
1138
1139   std::string name = Mang->getMangledName(GVar);
1140   Constant *C = GVar->getInitializer();
1141   const Type *Type = C->getType();
1142   unsigned Size = TD->getTypeAllocSize(Type);
1143   unsigned Align = TD->getPreferredAlignmentLog(GVar);
1144   bool isDarwin = Subtarget->isTargetDarwin();
1145
1146   printVisibility(name, GVar->getVisibility());
1147
1148   if (Subtarget->isTargetELF())
1149     O << "\t.type " << name << ",%object\n";
1150
1151   const MCSection *TheSection =
1152     getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
1153   OutStreamer.SwitchSection(TheSection);
1154
1155   // FIXME: get this stuff from section kind flags.
1156   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal() &&
1157       // Don't put things that should go in the cstring section into "comm".
1158       !TheSection->getKind().isMergeableCString()) {
1159     if (GVar->hasExternalLinkage()) {
1160       if (const char *Directive = MAI->getZeroFillDirective()) {
1161         O << "\t.globl\t" << name << "\n";
1162         O << Directive << "__DATA, __common, " << name << ", "
1163           << Size << ", " << Align << "\n";
1164         return;
1165       }
1166     }
1167
1168     if (GVar->hasLocalLinkage() || GVar->isWeakForLinker()) {
1169       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
1170
1171       if (isDarwin) {
1172         if (GVar->hasLocalLinkage()) {
1173           O << MAI->getLCOMMDirective()  << name << "," << Size
1174             << ',' << Align;
1175         } else if (GVar->hasCommonLinkage()) {
1176           O << MAI->getCOMMDirective()  << name << "," << Size
1177             << ',' << Align;
1178         } else {
1179           OutStreamer.SwitchSection(TheSection);
1180           O << "\t.globl " << name << '\n'
1181             << MAI->getWeakDefDirective() << name << '\n';
1182           EmitAlignment(Align, GVar);
1183           O << name << ":";
1184           if (VerboseAsm) {
1185             O << "\t\t\t\t" << MAI->getCommentString() << ' ';
1186             WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1187           }
1188           O << '\n';
1189           EmitGlobalConstant(C);
1190           return;
1191         }
1192       } else if (MAI->getLCOMMDirective() != NULL) {
1193         if (GVar->hasLocalLinkage()) {
1194           O << MAI->getLCOMMDirective() << name << "," << Size;
1195         } else {
1196           O << MAI->getCOMMDirective()  << name << "," << Size;
1197           if (MAI->getCOMMDirectiveTakesAlignment())
1198             O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1199         }
1200       } else {
1201         if (GVar->hasLocalLinkage())
1202           O << "\t.local\t" << name << "\n";
1203         O << MAI->getCOMMDirective()  << name << "," << Size;
1204         if (MAI->getCOMMDirectiveTakesAlignment())
1205           O << "," << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
1206       }
1207       if (VerboseAsm) {
1208         O << "\t\t" << MAI->getCommentString() << " ";
1209         WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1210       }
1211       O << "\n";
1212       return;
1213     }
1214   }
1215
1216   switch (GVar->getLinkage()) {
1217   case GlobalValue::CommonLinkage:
1218   case GlobalValue::LinkOnceAnyLinkage:
1219   case GlobalValue::LinkOnceODRLinkage:
1220   case GlobalValue::WeakAnyLinkage:
1221   case GlobalValue::WeakODRLinkage:
1222   case GlobalValue::LinkerPrivateLinkage:
1223     if (isDarwin) {
1224       O << "\t.globl " << name << "\n"
1225         << "\t.weak_definition " << name << "\n";
1226     } else {
1227       O << "\t.weak " << name << "\n";
1228     }
1229     break;
1230   case GlobalValue::AppendingLinkage:
1231   // FIXME: appending linkage variables should go into a section of
1232   // their name or something.  For now, just emit them as external.
1233   case GlobalValue::ExternalLinkage:
1234     O << "\t.globl " << name << "\n";
1235     break;
1236   case GlobalValue::PrivateLinkage:
1237   case GlobalValue::InternalLinkage:
1238     break;
1239   default:
1240     llvm_unreachable("Unknown linkage type!");
1241   }
1242
1243   EmitAlignment(Align, GVar);
1244   O << name << ":";
1245   if (VerboseAsm) {
1246     O << "\t\t\t\t" << MAI->getCommentString() << " ";
1247     WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
1248   }
1249   O << "\n";
1250   if (MAI->hasDotTypeDotSizeDirective())
1251     O << "\t.size " << name << ", " << Size << "\n";
1252
1253   EmitGlobalConstant(C);
1254   O << '\n';
1255 }
1256
1257
1258 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
1259   if (Subtarget->isTargetDarwin()) {
1260     // All darwin targets use mach-o.
1261     TargetLoweringObjectFileMachO &TLOFMacho =
1262       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1263     MachineModuleInfoMachO &MMIMacho =
1264       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1265
1266     O << '\n';
1267
1268     // Output non-lazy-pointers for external and common global variables.
1269     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1270     
1271     if (!Stubs.empty()) {
1272       // Switch with ".non_lazy_symbol_pointer" directive.
1273       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1274       EmitAlignment(2);
1275       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1276         Stubs[i].first->print(O, MAI);
1277         O << ":\n\t.indirect_symbol ";
1278         Stubs[i].second->print(O, MAI);
1279         O << "\n\t.long\t0\n";
1280       }
1281     }
1282
1283     Stubs = MMIMacho.GetHiddenGVStubList();
1284     if (!Stubs.empty()) {
1285       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1286       EmitAlignment(2);
1287       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1288         Stubs[i].first->print(O, MAI);
1289         O << ":\n\t.long ";
1290         Stubs[i].second->print(O, MAI);
1291         O << "\n";
1292       }
1293     }
1294
1295     // Funny Darwin hack: This flag tells the linker that no global symbols
1296     // contain code that falls through to other global symbols (e.g. the obvious
1297     // implementation of multiple entry points).  If this doesn't occur, the
1298     // linker can safely perform dead code stripping.  Since LLVM never
1299     // generates code that does this, it is always safe to set.
1300     OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1301   }
1302 }
1303
1304 // Force static initialization.
1305 extern "C" void LLVMInitializeARMAsmPrinter() {
1306   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1307   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1308 }
1309
1310
1311 //===----------------------------------------------------------------------===//
1312
1313 void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
1314   ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
1315   switch (MI->getOpcode()) {
1316   default: break;
1317   case TargetInstrInfo::DBG_LABEL:
1318   case TargetInstrInfo::EH_LABEL:
1319   case TargetInstrInfo::GC_LABEL:
1320     printLabel(MI);
1321     return;
1322   case TargetInstrInfo::KILL:
1323     return;
1324   case TargetInstrInfo::INLINEASM:
1325     O << '\t';
1326     printInlineAsm(MI);
1327     return;
1328   case TargetInstrInfo::IMPLICIT_DEF:
1329     printImplicitDef(MI);
1330     return;
1331   case ARM::PICADD: { // FIXME: Remove asm string from td file.
1332     // This is a pseudo op for a label + instruction sequence, which looks like:
1333     // LPC0:
1334     //     add r0, pc, r0
1335     // This adds the address of LPC0 to r0.
1336     
1337     // Emit the label.
1338     // FIXME: MOVE TO SHARED PLACE.
1339     unsigned Id = (unsigned)MI->getOperand(2).getImm();
1340     const char *Prefix = MAI->getPrivateGlobalPrefix();
1341     MCSymbol *Label =OutContext.GetOrCreateSymbol(Twine(Prefix)+"PC"+Twine(Id));
1342     OutStreamer.EmitLabel(Label);
1343     
1344     
1345     // Form and emit tha dd.
1346     MCInst AddInst;
1347     AddInst.setOpcode(ARM::ADDrr);
1348     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1349     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1350     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1351     printMCInst(&AddInst);
1352     return;
1353   }
1354   case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1355     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1356     /// in the function.  The first operand is the ID# for this instruction, the
1357     /// second is the index into the MachineConstantPool that this is, the third
1358     /// is the size in bytes of this constant pool entry.
1359     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1360     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1361
1362     EmitAlignment(2);
1363
1364     const char *Prefix = MAI->getPrivateGlobalPrefix();
1365     MCSymbol *Label = OutContext.GetOrCreateSymbol(Twine(Prefix)+"CPI"+
1366                                                    Twine(getFunctionNumber())+
1367                                                    "_"+ Twine(LabelId));
1368     OutStreamer.EmitLabel(Label);
1369
1370     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1371     if (MCPE.isMachineConstantPoolEntry())
1372       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1373     else
1374       EmitGlobalConstant(MCPE.Val.ConstVal);
1375     
1376     return;
1377   }
1378   case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1379     // This is a hack that lowers as a two instruction sequence.
1380     unsigned DstReg = MI->getOperand(0).getReg();
1381     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1382
1383     unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1384     unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1385     
1386     {
1387       MCInst TmpInst;
1388       TmpInst.setOpcode(ARM::MOVi);
1389       TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1390       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1391       
1392       // Predicate.
1393       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1394       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1395
1396       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1397       printMCInst(&TmpInst);
1398       O << '\n';
1399     }
1400
1401     {
1402       MCInst TmpInst;
1403       TmpInst.setOpcode(ARM::ORRri);
1404       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // dstreg
1405       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // inreg
1406       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1407       // Predicate.
1408       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1409       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1410       
1411       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1412       printMCInst(&TmpInst);
1413     }
1414     return; 
1415   }
1416   case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1417     // This is a hack that lowers as a two instruction sequence.
1418     unsigned DstReg = MI->getOperand(0).getReg();
1419     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1420     
1421     {
1422       MCInst TmpInst;
1423       TmpInst.setOpcode(ARM::MOVi16);
1424       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1425       TmpInst.addOperand(MCOperand::CreateImm(ImmVal & 65535)); // lower16(imm)
1426       
1427       // Predicate.
1428       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1429       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1430       
1431       printMCInst(&TmpInst);
1432       O << '\n';
1433     }
1434     
1435     {
1436       MCInst TmpInst;
1437       TmpInst.setOpcode(ARM::MOVTi16);
1438       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1439       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // srcreg
1440       TmpInst.addOperand(MCOperand::CreateImm(ImmVal >> 16));   // upper16(imm)
1441       
1442       // Predicate.
1443       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1444       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1445       
1446       printMCInst(&TmpInst);
1447     }
1448     
1449     return;
1450   }
1451       
1452   // FIXME: Handle t2MOVi32imm also.
1453   }
1454       
1455   MCInst TmpInst;
1456   MCInstLowering.Lower(MI, TmpInst);
1457   
1458   printMCInst(&TmpInst);
1459 }