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