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