Factor out dbg_value comment printing and teach MC asm printing to use it.
[oota-llvm.git] / lib / Target / ARM / 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 "AsmPrinter/ARMInstPrinter.h"
21 #include "ARMMachineFunctionInfo.h"
22 #include "ARMMCInstLower.h"
23 #include "ARMTargetMachine.h"
24 #include "llvm/Analysis/DebugInfo.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Module.h"
27 #include "llvm/Type.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineJumpTableInfo.h"
33 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCSectionMachO.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbol.h"
41 #include "llvm/Target/Mangler.h"
42 #include "llvm/Target/TargetData.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 #include "llvm/Target/TargetRegistry.h"
46 #include "llvm/ADT/SmallPtrSet.h"
47 #include "llvm/ADT/SmallString.h"
48 #include "llvm/ADT/StringExtras.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include <cctype>
54 using namespace llvm;
55
56 static cl::opt<bool>
57 EnableMCInst("enable-arm-mcinst-printer", cl::Hidden, cl::init(true),
58             cl::desc("enable experimental asmprinter gunk in the arm backend"));
59
60 namespace llvm {
61   namespace ARM {
62     enum DW_ISA {
63       DW_ISA_ARM_thumb = 1,
64       DW_ISA_ARM_arm = 2
65     };
66   }
67 }
68
69 namespace {
70   class ARMAsmPrinter : public AsmPrinter {
71
72     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
73     /// make the right decision when printing asm code for different targets.
74     const ARMSubtarget *Subtarget;
75
76     /// AFI - Keep a pointer to ARMFunctionInfo for the current
77     /// MachineFunction.
78     ARMFunctionInfo *AFI;
79
80     /// MCP - Keep a pointer to constantpool entries of the current
81     /// MachineFunction.
82     const MachineConstantPool *MCP;
83
84   public:
85     explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
86       : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
87       Subtarget = &TM.getSubtarget<ARMSubtarget>();
88     }
89
90     virtual const char *getPassName() const {
91       return "ARM Assembly Printer";
92     }
93
94     void EmitJumpTable(const MachineInstr *MI);
95     void EmitJump2Table(const MachineInstr *MI);
96     void printInstructionThroughMCStreamer(const MachineInstr *MI);
97
98
99     void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
100                       const char *Modifier = 0);
101     void printSOImmOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
102     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum,
103                                 raw_ostream &O);
104     void printSORegOperand(const MachineInstr *MI, int OpNum,
105                            raw_ostream &O);
106     void printAddrMode2Operand(const MachineInstr *MI, int OpNum,
107                                raw_ostream &O);
108     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum,
109                                      raw_ostream &O);
110     void printAddrMode3Operand(const MachineInstr *MI, int OpNum,
111                                raw_ostream &O);
112     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum,
113                                      raw_ostream &O);
114     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
115                                const char *Modifier = 0);
116     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
117                                const char *Modifier = 0);
118     void printAddrMode6Operand(const MachineInstr *MI, int OpNum,
119                                raw_ostream &O);
120     void printAddrMode6OffsetOperand(const MachineInstr *MI, int OpNum,
121                                      raw_ostream &O);
122     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
123                                 raw_ostream &O,
124                                 const char *Modifier = 0);
125     void printBitfieldInvMaskImmOperand(const MachineInstr *MI, int OpNum,
126                                         raw_ostream &O);
127     void printMemBOption(const MachineInstr *MI, int OpNum,
128                          raw_ostream &O);
129     void printShiftImmOperand(const MachineInstr *MI, int OpNum,
130                               raw_ostream &O);
131
132     void printThumbS4ImmOperand(const MachineInstr *MI, int OpNum,
133                                 raw_ostream &O);
134     void printThumbITMask(const MachineInstr *MI, int OpNum, raw_ostream &O);
135     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum,
136                                      raw_ostream &O);
137     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
138                                       raw_ostream &O,
139                                       unsigned Scale);
140     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum,
141                                      raw_ostream &O);
142     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum,
143                                      raw_ostream &O);
144     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum,
145                                      raw_ostream &O);
146     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum,
147                                      raw_ostream &O);
148
149     void printT2SOOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
150     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum,
151                                      raw_ostream &O);
152     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum,
153                                     raw_ostream &O);
154     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum,
155                                       raw_ostream &O);
156     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum,
157                                           raw_ostream &O);
158     void printT2AddrModeImm8s4OffsetOperand(const MachineInstr *MI, int OpNum,
159                                             raw_ostream &O) {}
160     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum,
161                                      raw_ostream &O);
162
163     void printCPSOptionOperand(const MachineInstr *MI, int OpNum,
164                                raw_ostream &O) {}
165     void printMSRMaskOperand(const MachineInstr *MI, int OpNum,
166                              raw_ostream &O) {}
167     void printNegZeroOperand(const MachineInstr *MI, int OpNum,
168                              raw_ostream &O) {}
169     void printPredicateOperand(const MachineInstr *MI, int OpNum,
170                                raw_ostream &O);
171     void printMandatoryPredicateOperand(const MachineInstr *MI, int OpNum,
172                                         raw_ostream &O);
173     void printSBitModifierOperand(const MachineInstr *MI, int OpNum,
174                                   raw_ostream &O);
175     void printPCLabel(const MachineInstr *MI, int OpNum,
176                       raw_ostream &O);
177     void printRegisterList(const MachineInstr *MI, int OpNum,
178                            raw_ostream &O);
179     void printCPInstOperand(const MachineInstr *MI, int OpNum,
180                             raw_ostream &O,
181                             const char *Modifier);
182     void printJTBlockOperand(const MachineInstr *MI, int OpNum,
183                              raw_ostream &O);
184     void printJT2BlockOperand(const MachineInstr *MI, int OpNum,
185                               raw_ostream &O);
186     void printTBAddrMode(const MachineInstr *MI, int OpNum,
187                          raw_ostream &O);
188     void printNoHashImmediate(const MachineInstr *MI, int OpNum,
189                               raw_ostream &O);
190     void printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
191                                raw_ostream &O);
192     void printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
193                                raw_ostream &O);
194     void printNEONModImmOperand(const MachineInstr *MI, int OpNum,
195                                 raw_ostream &O);
196
197     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
198                                  unsigned AsmVariant, const char *ExtraCode,
199                                  raw_ostream &O);
200     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
201                                        unsigned AsmVariant,
202                                        const char *ExtraCode, raw_ostream &O);
203
204     void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen
205     static const char *getRegisterName(unsigned RegNo);
206
207     virtual void EmitInstruction(const MachineInstr *MI);
208     bool runOnMachineFunction(MachineFunction &F);
209
210     virtual void EmitConstantPool() {} // we emit constant pools customly!
211     virtual void EmitFunctionEntryLabel();
212     void EmitStartOfAsmFile(Module &M);
213     void EmitEndOfAsmFile(Module &M);
214
215     void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
216
217     MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
218       MachineLocation Location;
219       assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
220       // Frame address.  Currently handles register +- offset only.
221       if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
222         Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
223       else {
224         DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
225       }
226       return Location;
227     }
228
229     virtual unsigned getISAEncoding() {
230       // ARM/Darwin adds ISA to the DWARF info for each function.
231       if (!Subtarget->isTargetDarwin())
232         return 0;
233       return Subtarget->isThumb() ?
234         llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
235     }
236
237     MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
238                                           const MachineBasicBlock *MBB) const;
239     MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
240
241     MCSymbol *GetARMSJLJEHLabel(void) const;
242
243     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
244     /// the .s file.
245     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
246       SmallString<128> Str;
247       raw_svector_ostream OS(Str);
248       EmitMachineConstantPoolValue(MCPV, OS);
249       OutStreamer.EmitRawText(OS.str());
250     }
251
252     void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
253                                       raw_ostream &O) {
254       switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
255       case 1: O << MAI->getData8bitsDirective(0); break;
256       case 2: O << MAI->getData16bitsDirective(0); break;
257       case 4: O << MAI->getData32bitsDirective(0); break;
258       default: assert(0 && "Unknown CPV size");
259       }
260
261       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
262
263       if (ACPV->isLSDA()) {
264         O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
265       } else if (ACPV->isBlockAddress()) {
266         O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
267       } else if (ACPV->isGlobalValue()) {
268         const GlobalValue *GV = ACPV->getGV();
269         bool isIndirect = Subtarget->isTargetDarwin() &&
270           Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
271         if (!isIndirect)
272           O << *Mang->getSymbol(GV);
273         else {
274           // FIXME: Remove this when Darwin transition to @GOT like syntax.
275           MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
276           O << *Sym;
277
278           MachineModuleInfoMachO &MMIMachO =
279             MMI->getObjFileInfo<MachineModuleInfoMachO>();
280           MachineModuleInfoImpl::StubValueTy &StubSym =
281             GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
282                                         MMIMachO.getGVStubEntry(Sym);
283           if (StubSym.getPointer() == 0)
284             StubSym = MachineModuleInfoImpl::
285               StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
286         }
287       } else {
288         assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
289         O << *GetExternalSymbolSymbol(ACPV->getSymbol());
290       }
291
292       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
293       if (ACPV->getPCAdjustment() != 0) {
294         O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
295           << getFunctionNumber() << "_"  << ACPV->getLabelId()
296           << "+" << (unsigned)ACPV->getPCAdjustment();
297          if (ACPV->mustAddCurrentAddress())
298            O << "-.";
299          O << ')';
300       }
301     }
302   };
303 } // end of anonymous namespace
304
305 #include "ARMGenAsmWriter.inc"
306
307 void ARMAsmPrinter::EmitFunctionEntryLabel() {
308   if (AFI->isThumbFunction()) {
309     OutStreamer.EmitRawText(StringRef("\t.code\t16"));
310     if (!Subtarget->isTargetDarwin())
311       OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
312     else {
313       // This needs to emit to a temporary string to get properly quoted
314       // MCSymbols when they have spaces in them.
315       SmallString<128> Tmp;
316       raw_svector_ostream OS(Tmp);
317       OS << "\t.thumb_func\t" << *CurrentFnSym;
318       OutStreamer.EmitRawText(OS.str());
319     }
320   }
321
322   OutStreamer.EmitLabel(CurrentFnSym);
323 }
324
325 /// runOnMachineFunction - This uses the printInstruction()
326 /// method to print assembly for each instruction.
327 ///
328 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
329   AFI = MF.getInfo<ARMFunctionInfo>();
330   MCP = MF.getConstantPool();
331
332   return AsmPrinter::runOnMachineFunction(MF);
333 }
334
335 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
336                                  raw_ostream &O, const char *Modifier) {
337   const MachineOperand &MO = MI->getOperand(OpNum);
338   unsigned TF = MO.getTargetFlags();
339
340   switch (MO.getType()) {
341   default:
342     assert(0 && "<unknown operand type>");
343   case MachineOperand::MO_Register: {
344     unsigned Reg = MO.getReg();
345     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
346     if (Modifier && strcmp(Modifier, "lane") == 0) {
347       unsigned RegNum = getARMRegisterNumbering(Reg);
348       unsigned DReg =
349         TM.getRegisterInfo()->getMatchingSuperReg(Reg,
350           RegNum & 1 ? ARM::ssub_1 : ARM::ssub_0, &ARM::DPR_VFP2RegClass);
351       O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
352     } else {
353       assert(!MO.getSubReg() && "Subregs should be eliminated!");
354       O << getRegisterName(Reg);
355     }
356     break;
357   }
358   case MachineOperand::MO_Immediate: {
359     int64_t Imm = MO.getImm();
360     O << '#';
361     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
362         (TF & ARMII::MO_LO16))
363       O << ":lower16:";
364     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
365              (TF & ARMII::MO_HI16))
366       O << ":upper16:";
367     O << Imm;
368     break;
369   }
370   case MachineOperand::MO_MachineBasicBlock:
371     O << *MO.getMBB()->getSymbol();
372     return;
373   case MachineOperand::MO_GlobalAddress: {
374     bool isCallOp = Modifier && !strcmp(Modifier, "call");
375     const GlobalValue *GV = MO.getGlobal();
376
377     if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
378         (TF & ARMII::MO_LO16))
379       O << ":lower16:";
380     else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
381              (TF & ARMII::MO_HI16))
382       O << ":upper16:";
383     O << *Mang->getSymbol(GV);
384
385     printOffset(MO.getOffset(), O);
386
387     if (isCallOp && Subtarget->isTargetELF() &&
388         TM.getRelocationModel() == Reloc::PIC_)
389       O << "(PLT)";
390     break;
391   }
392   case MachineOperand::MO_ExternalSymbol: {
393     bool isCallOp = Modifier && !strcmp(Modifier, "call");
394     O << *GetExternalSymbolSymbol(MO.getSymbolName());
395
396     if (isCallOp && Subtarget->isTargetELF() &&
397         TM.getRelocationModel() == Reloc::PIC_)
398       O << "(PLT)";
399     break;
400   }
401   case MachineOperand::MO_ConstantPoolIndex:
402     O << *GetCPISymbol(MO.getIndex());
403     break;
404   case MachineOperand::MO_JumpTableIndex:
405     O << *GetJTISymbol(MO.getIndex());
406     break;
407   }
408 }
409
410 static void printSOImm(raw_ostream &O, int64_t V, bool VerboseAsm,
411                        const MCAsmInfo *MAI) {
412   // Break it up into two parts that make up a shifter immediate.
413   V = ARM_AM::getSOImmVal(V);
414   assert(V != -1 && "Not a valid so_imm value!");
415
416   unsigned Imm = ARM_AM::getSOImmValImm(V);
417   unsigned Rot = ARM_AM::getSOImmValRot(V);
418
419   // Print low-level immediate formation info, per
420   // A5.1.3: "Data-processing operands - Immediate".
421   if (Rot) {
422     O << "#" << Imm << ", " << Rot;
423     // Pretty printed version.
424     if (VerboseAsm) {
425       O << "\t" << MAI->getCommentString() << ' ';
426       O << (int)ARM_AM::rotr32(Imm, Rot);
427     }
428   } else {
429     O << "#" << Imm;
430   }
431 }
432
433 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
434 /// immediate in bits 0-7.
435 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum,
436                                       raw_ostream &O) {
437   const MachineOperand &MO = MI->getOperand(OpNum);
438   assert(MO.isImm() && "Not a valid so_imm value!");
439   printSOImm(O, MO.getImm(), isVerbose(), MAI);
440 }
441
442 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
443 /// followed by an 'orr' to materialize.
444 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum,
445                                            raw_ostream &O) {
446   const MachineOperand &MO = MI->getOperand(OpNum);
447   assert(MO.isImm() && "Not a valid so_imm value!");
448   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
449   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
450   printSOImm(O, V1, isVerbose(), MAI);
451   O << "\n\torr";
452   printPredicateOperand(MI, 2, O);
453   O << "\t";
454   printOperand(MI, 0, O);
455   O << ", ";
456   printOperand(MI, 0, O);
457   O << ", ";
458   printSOImm(O, V2, isVerbose(), MAI);
459 }
460
461 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
462 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
463 //    REG 0   0           - e.g. R5
464 //    REG REG 0,SH_OPC    - e.g. R5, ROR R3
465 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
466 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op,
467                                       raw_ostream &O) {
468   const MachineOperand &MO1 = MI->getOperand(Op);
469   const MachineOperand &MO2 = MI->getOperand(Op+1);
470   const MachineOperand &MO3 = MI->getOperand(Op+2);
471
472   O << getRegisterName(MO1.getReg());
473
474   // Print the shift opc.
475   ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO3.getImm());
476   O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
477   if (MO2.getReg()) {
478     O << ' ' << getRegisterName(MO2.getReg());
479     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
480   } else if (ShOpc != ARM_AM::rrx) {
481     O << " #" << ARM_AM::getSORegOffset(MO3.getImm());
482   }
483 }
484
485 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op,
486                                           raw_ostream &O) {
487   const MachineOperand &MO1 = MI->getOperand(Op);
488   const MachineOperand &MO2 = MI->getOperand(Op+1);
489   const MachineOperand &MO3 = MI->getOperand(Op+2);
490
491   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
492     printOperand(MI, Op, O);
493     return;
494   }
495
496   O << "[" << getRegisterName(MO1.getReg());
497
498   if (!MO2.getReg()) {
499     if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
500       O << ", #"
501         << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
502         << ARM_AM::getAM2Offset(MO3.getImm());
503     O << "]";
504     return;
505   }
506
507   O << ", "
508     << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
509     << getRegisterName(MO2.getReg());
510
511   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
512     O << ", "
513       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
514       << " #" << ShImm;
515   O << "]";
516 }
517
518 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op,
519                                                 raw_ostream &O) {
520   const MachineOperand &MO1 = MI->getOperand(Op);
521   const MachineOperand &MO2 = MI->getOperand(Op+1);
522
523   if (!MO1.getReg()) {
524     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
525     O << "#"
526       << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
527       << ImmOffs;
528     return;
529   }
530
531   O << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
532     << getRegisterName(MO1.getReg());
533
534   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
535     O << ", "
536       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
537       << " #" << ShImm;
538 }
539
540 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op,
541                                           raw_ostream &O) {
542   const MachineOperand &MO1 = MI->getOperand(Op);
543   const MachineOperand &MO2 = MI->getOperand(Op+1);
544   const MachineOperand &MO3 = MI->getOperand(Op+2);
545
546   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
547   O << "[" << getRegisterName(MO1.getReg());
548
549   if (MO2.getReg()) {
550     O << ", "
551       << (char)ARM_AM::getAM3Op(MO3.getImm())
552       << getRegisterName(MO2.getReg())
553       << "]";
554     return;
555   }
556
557   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
558     O << ", #"
559       << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO3.getImm()))
560       << ImmOffs;
561   O << "]";
562 }
563
564 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op,
565                                                 raw_ostream &O){
566   const MachineOperand &MO1 = MI->getOperand(Op);
567   const MachineOperand &MO2 = MI->getOperand(Op+1);
568
569   if (MO1.getReg()) {
570     O << (char)ARM_AM::getAM3Op(MO2.getImm())
571       << getRegisterName(MO1.getReg());
572     return;
573   }
574
575   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
576   O << "#"
577     << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO2.getImm()))
578     << ImmOffs;
579 }
580
581 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
582                                           raw_ostream &O,
583                                           const char *Modifier) {
584   const MachineOperand &MO2 = MI->getOperand(Op+1);
585   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
586   if (Modifier && strcmp(Modifier, "submode") == 0) {
587     O << ARM_AM::getAMSubModeStr(Mode);
588   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
589     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
590     if (Mode == ARM_AM::ia)
591       O << ".w";
592   } else {
593     printOperand(MI, Op, O);
594   }
595 }
596
597 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
598                                           raw_ostream &O,
599                                           const char *Modifier) {
600   const MachineOperand &MO1 = MI->getOperand(Op);
601   const MachineOperand &MO2 = MI->getOperand(Op+1);
602
603   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
604     printOperand(MI, Op, O);
605     return;
606   }
607
608   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
609
610   O << "[" << getRegisterName(MO1.getReg());
611
612   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
613     O << ", #"
614       << ARM_AM::getAddrOpcStr(ARM_AM::getAM5Op(MO2.getImm()))
615       << ImmOffs*4;
616   }
617   O << "]";
618 }
619
620 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op,
621                                           raw_ostream &O) {
622   const MachineOperand &MO1 = MI->getOperand(Op);
623   const MachineOperand &MO2 = MI->getOperand(Op+1);
624
625   O << "[" << getRegisterName(MO1.getReg());
626   if (MO2.getImm()) {
627     // FIXME: Both darwin as and GNU as violate ARM docs here.
628     O << ", :" << (MO2.getImm() << 3);
629   }
630   O << "]";
631 }
632
633 void ARMAsmPrinter::printAddrMode6OffsetOperand(const MachineInstr *MI, int Op,
634                                                 raw_ostream &O){
635   const MachineOperand &MO = MI->getOperand(Op);
636   if (MO.getReg() == 0)
637     O << "!";
638   else
639     O << ", " << getRegisterName(MO.getReg());
640 }
641
642 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
643                                            raw_ostream &O,
644                                            const char *Modifier) {
645   if (Modifier && strcmp(Modifier, "label") == 0) {
646     printPCLabel(MI, Op+1, O);
647     return;
648   }
649
650   const MachineOperand &MO1 = MI->getOperand(Op);
651   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
652   O << "[pc, " << getRegisterName(MO1.getReg()) << "]";
653 }
654
655 void
656 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op,
657                                               raw_ostream &O) {
658   const MachineOperand &MO = MI->getOperand(Op);
659   uint32_t v = ~MO.getImm();
660   int32_t lsb = CountTrailingZeros_32(v);
661   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
662   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
663   O << "#" << lsb << ", #" << width;
664 }
665
666 void
667 ARMAsmPrinter::printMemBOption(const MachineInstr *MI, int OpNum,
668                                raw_ostream &O) {
669   unsigned val = MI->getOperand(OpNum).getImm();
670   O << ARM_MB::MemBOptToString(val);
671 }
672
673 void ARMAsmPrinter::printShiftImmOperand(const MachineInstr *MI, int OpNum,
674                                          raw_ostream &O) {
675   unsigned ShiftOp = MI->getOperand(OpNum).getImm();
676   ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
677   switch (Opc) {
678   case ARM_AM::no_shift:
679     return;
680   case ARM_AM::lsl:
681     O << ", lsl #";
682     break;
683   case ARM_AM::asr:
684     O << ", asr #";
685     break;
686   default:
687     assert(0 && "unexpected shift opcode for shift immediate operand");
688   }
689   O << ARM_AM::getSORegOffset(ShiftOp);
690 }
691
692 //===--------------------------------------------------------------------===//
693
694 void ARMAsmPrinter::printThumbS4ImmOperand(const MachineInstr *MI, int Op,
695                                            raw_ostream &O) {
696   O << "#" <<  MI->getOperand(Op).getImm() * 4;
697 }
698
699 void
700 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op,
701                                 raw_ostream &O) {
702   // (3 - the number of trailing zeros) is the number of then / else.
703   unsigned Mask = MI->getOperand(Op).getImm();
704   unsigned CondBit0 = Mask >> 4 & 1;
705   unsigned NumTZ = CountTrailingZeros_32(Mask);
706   assert(NumTZ <= 3 && "Invalid IT mask!");
707   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
708     bool T = ((Mask >> Pos) & 1) == CondBit0;
709     if (T)
710       O << 't';
711     else
712       O << 'e';
713   }
714 }
715
716 void
717 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op,
718                                            raw_ostream &O) {
719   const MachineOperand &MO1 = MI->getOperand(Op);
720   const MachineOperand &MO2 = MI->getOperand(Op+1);
721   O << "[" << getRegisterName(MO1.getReg());
722   O << ", " << getRegisterName(MO2.getReg()) << "]";
723 }
724
725 void
726 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
727                                             raw_ostream &O,
728                                             unsigned Scale) {
729   const MachineOperand &MO1 = MI->getOperand(Op);
730   const MachineOperand &MO2 = MI->getOperand(Op+1);
731   const MachineOperand &MO3 = MI->getOperand(Op+2);
732
733   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
734     printOperand(MI, Op, O);
735     return;
736   }
737
738   O << "[" << getRegisterName(MO1.getReg());
739   if (MO3.getReg())
740     O << ", " << getRegisterName(MO3.getReg());
741   else if (unsigned ImmOffs = MO2.getImm())
742     O << ", #" << ImmOffs * Scale;
743   O << "]";
744 }
745
746 void
747 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op,
748                                            raw_ostream &O) {
749   printThumbAddrModeRI5Operand(MI, Op, O, 1);
750 }
751 void
752 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op,
753                                            raw_ostream &O) {
754   printThumbAddrModeRI5Operand(MI, Op, O, 2);
755 }
756 void
757 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op,
758                                            raw_ostream &O) {
759   printThumbAddrModeRI5Operand(MI, Op, O, 4);
760 }
761
762 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op,
763                                                 raw_ostream &O) {
764   const MachineOperand &MO1 = MI->getOperand(Op);
765   const MachineOperand &MO2 = MI->getOperand(Op+1);
766   O << "[" << getRegisterName(MO1.getReg());
767   if (unsigned ImmOffs = MO2.getImm())
768     O << ", #" << ImmOffs*4;
769   O << "]";
770 }
771
772 //===--------------------------------------------------------------------===//
773
774 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
775 // register with shift forms.
776 // REG 0   0           - e.g. R5
777 // REG IMM, SH_OPC     - e.g. R5, LSL #3
778 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum,
779                                      raw_ostream &O) {
780   const MachineOperand &MO1 = MI->getOperand(OpNum);
781   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
782
783   unsigned Reg = MO1.getReg();
784   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
785   O << getRegisterName(Reg);
786
787   // Print the shift opc.
788   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
789   ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO2.getImm());
790   O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
791   if (ShOpc != ARM_AM::rrx)
792     O << " #" << ARM_AM::getSORegOffset(MO2.getImm());
793 }
794
795 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
796                                                 int OpNum,
797                                                 raw_ostream &O) {
798   const MachineOperand &MO1 = MI->getOperand(OpNum);
799   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
800
801   O << "[" << getRegisterName(MO1.getReg());
802
803   unsigned OffImm = MO2.getImm();
804   if (OffImm)  // Don't print +0.
805     O << ", #" << OffImm;
806   O << "]";
807 }
808
809 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
810                                                int OpNum,
811                                                raw_ostream &O) {
812   const MachineOperand &MO1 = MI->getOperand(OpNum);
813   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
814
815   O << "[" << getRegisterName(MO1.getReg());
816
817   int32_t OffImm = (int32_t)MO2.getImm();
818   // Don't print +0.
819   if (OffImm < 0)
820     O << ", #-" << -OffImm;
821   else if (OffImm > 0)
822     O << ", #" << OffImm;
823   O << "]";
824 }
825
826 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
827                                                  int OpNum,
828                                                  raw_ostream &O) {
829   const MachineOperand &MO1 = MI->getOperand(OpNum);
830   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
831
832   O << "[" << getRegisterName(MO1.getReg());
833
834   int32_t OffImm = (int32_t)MO2.getImm() / 4;
835   // Don't print +0.
836   if (OffImm < 0)
837     O << ", #-" << -OffImm * 4;
838   else if (OffImm > 0)
839     O << ", #" << OffImm * 4;
840   O << "]";
841 }
842
843 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
844                                                      int OpNum,
845                                                      raw_ostream &O) {
846   const MachineOperand &MO1 = MI->getOperand(OpNum);
847   int32_t OffImm = (int32_t)MO1.getImm();
848   // Don't print +0.
849   if (OffImm < 0)
850     O << "#-" << -OffImm;
851   else if (OffImm > 0)
852     O << "#" << OffImm;
853 }
854
855 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
856                                                 int OpNum,
857                                                 raw_ostream &O) {
858   const MachineOperand &MO1 = MI->getOperand(OpNum);
859   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
860   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
861
862   O << "[" << getRegisterName(MO1.getReg());
863
864   assert(MO2.getReg() && "Invalid so_reg load / store address!");
865   O << ", " << getRegisterName(MO2.getReg());
866
867   unsigned ShAmt = MO3.getImm();
868   if (ShAmt) {
869     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
870     O << ", lsl #" << ShAmt;
871   }
872   O << "]";
873 }
874
875
876 //===--------------------------------------------------------------------===//
877
878 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum,
879                                           raw_ostream &O) {
880   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
881   if (CC != ARMCC::AL)
882     O << ARMCondCodeToString(CC);
883 }
884
885 void ARMAsmPrinter::printMandatoryPredicateOperand(const MachineInstr *MI,
886                                                    int OpNum,
887                                                    raw_ostream &O) {
888   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
889   O << ARMCondCodeToString(CC);
890 }
891
892 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum,
893                                              raw_ostream &O){
894   unsigned Reg = MI->getOperand(OpNum).getReg();
895   if (Reg) {
896     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
897     O << 's';
898   }
899 }
900
901 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum,
902                                  raw_ostream &O) {
903   int Id = (int)MI->getOperand(OpNum).getImm();
904   O << MAI->getPrivateGlobalPrefix()
905     << "PC" << getFunctionNumber() << "_" << Id;
906 }
907
908 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum,
909                                       raw_ostream &O) {
910   O << "{";
911   for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
912     if (MI->getOperand(i).isImplicit())
913       continue;
914     if ((int)i != OpNum) O << ", ";
915     printOperand(MI, i, O);
916   }
917   O << "}";
918 }
919
920 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
921                                        raw_ostream &O, const char *Modifier) {
922   assert(Modifier && "This operand only works with a modifier!");
923   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
924   // data itself.
925   if (!strcmp(Modifier, "label")) {
926     unsigned ID = MI->getOperand(OpNum).getImm();
927     OutStreamer.EmitLabel(GetCPISymbol(ID));
928   } else {
929     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
930     unsigned CPI = MI->getOperand(OpNum).getIndex();
931
932     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
933
934     if (MCPE.isMachineConstantPoolEntry()) {
935       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
936     } else {
937       EmitGlobalConstant(MCPE.Val.ConstVal);
938     }
939   }
940 }
941
942 MCSymbol *ARMAsmPrinter::
943 GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
944                             const MachineBasicBlock *MBB) const {
945   SmallString<60> Name;
946   raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
947     << getFunctionNumber() << '_' << uid << '_' << uid2
948     << "_set_" << MBB->getNumber();
949   return OutContext.GetOrCreateSymbol(Name.str());
950 }
951
952 MCSymbol *ARMAsmPrinter::
953 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
954   SmallString<60> Name;
955   raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
956     << getFunctionNumber() << '_' << uid << '_' << uid2;
957   return OutContext.GetOrCreateSymbol(Name.str());
958 }
959
960
961 MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
962   SmallString<60> Name;
963   raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
964     << getFunctionNumber();
965   return OutContext.GetOrCreateSymbol(Name.str());
966 }
967
968 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum,
969                                         raw_ostream &O) {
970   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
971
972   const MachineOperand &MO1 = MI->getOperand(OpNum);
973   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
974
975   unsigned JTI = MO1.getIndex();
976   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
977   // Can't use EmitLabel until instprinter happens, label comes out in the wrong
978   // order.
979   O << "\n" << *JTISymbol << ":\n";
980
981   const char *JTEntryDirective = MAI->getData32bitsDirective();
982
983   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
984   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
985   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
986   bool UseSet= MAI->hasSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
987   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
988   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
989     MachineBasicBlock *MBB = JTBBs[i];
990     bool isNew = JTSets.insert(MBB);
991
992     if (UseSet && isNew) {
993       O << "\t.set\t"
994         << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB) << ','
995         << *MBB->getSymbol() << '-' << *JTISymbol << '\n';
996     }
997
998     O << JTEntryDirective << ' ';
999     if (UseSet)
1000       O << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB);
1001     else if (TM.getRelocationModel() == Reloc::PIC_)
1002       O << *MBB->getSymbol() << '-' << *JTISymbol;
1003     else
1004       O << *MBB->getSymbol();
1005
1006     if (i != e-1)
1007       O << '\n';
1008   }
1009 }
1010
1011 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum,
1012                                          raw_ostream &O) {
1013   const MachineOperand &MO1 = MI->getOperand(OpNum);
1014   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1015   unsigned JTI = MO1.getIndex();
1016
1017   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1018
1019   // Can't use EmitLabel until instprinter happens, label comes out in the wrong
1020   // order.
1021   O << "\n" << *JTISymbol << ":\n";
1022
1023   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1024   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1025   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1026   bool ByteOffset = false, HalfWordOffset = false;
1027   if (MI->getOpcode() == ARM::t2TBB)
1028     ByteOffset = true;
1029   else if (MI->getOpcode() == ARM::t2TBH)
1030     HalfWordOffset = true;
1031
1032   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1033     MachineBasicBlock *MBB = JTBBs[i];
1034     if (ByteOffset)
1035       O << MAI->getData8bitsDirective();
1036     else if (HalfWordOffset)
1037       O << MAI->getData16bitsDirective();
1038
1039     if (ByteOffset || HalfWordOffset)
1040       O << '(' << *MBB->getSymbol() << "-" << *JTISymbol << ")/2";
1041     else
1042       O << "\tb.w " << *MBB->getSymbol();
1043
1044     if (i != e-1)
1045       O << '\n';
1046   }
1047 }
1048
1049 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum,
1050                                     raw_ostream &O) {
1051   O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
1052   if (MI->getOpcode() == ARM::t2TBH)
1053     O << ", lsl #1";
1054   O << ']';
1055 }
1056
1057 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum,
1058                                          raw_ostream &O) {
1059   O << MI->getOperand(OpNum).getImm();
1060 }
1061
1062 void ARMAsmPrinter::printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
1063                                           raw_ostream &O) {
1064   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1065   O << '#' << FP->getValueAPF().convertToFloat();
1066   if (isVerbose()) {
1067     O << "\t\t" << MAI->getCommentString() << ' ';
1068     WriteAsOperand(O, FP, /*PrintType=*/false);
1069   }
1070 }
1071
1072 void ARMAsmPrinter::printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
1073                                           raw_ostream &O) {
1074   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1075   O << '#' << FP->getValueAPF().convertToDouble();
1076   if (isVerbose()) {
1077     O << "\t\t" << MAI->getCommentString() << ' ';
1078     WriteAsOperand(O, FP, /*PrintType=*/false);
1079   }
1080 }
1081
1082 void ARMAsmPrinter::printNEONModImmOperand(const MachineInstr *MI, int OpNum,
1083                                            raw_ostream &O) {
1084   unsigned EncodedImm = MI->getOperand(OpNum).getImm();
1085   unsigned EltBits;
1086   uint64_t Val = ARM_AM::decodeNEONModImm(EncodedImm, EltBits);
1087   O << "#0x" << utohexstr(Val);
1088 }
1089
1090 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
1091                                     unsigned AsmVariant, const char *ExtraCode,
1092                                     raw_ostream &O) {
1093   // Does this asm operand have a single letter operand modifier?
1094   if (ExtraCode && ExtraCode[0]) {
1095     if (ExtraCode[1] != 0) return true; // Unknown modifier.
1096
1097     switch (ExtraCode[0]) {
1098     default: return true;  // Unknown modifier.
1099     case 'a': // Print as a memory address.
1100       if (MI->getOperand(OpNum).isReg()) {
1101         O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
1102         return false;
1103       }
1104       // Fallthrough
1105     case 'c': // Don't print "#" before an immediate operand.
1106       if (!MI->getOperand(OpNum).isImm())
1107         return true;
1108       printNoHashImmediate(MI, OpNum, O);
1109       return false;
1110     case 'P': // Print a VFP double precision register.
1111     case 'q': // Print a NEON quad precision register.
1112       printOperand(MI, OpNum, O);
1113       return false;
1114     case 'Q':
1115     case 'R':
1116     case 'H':
1117       report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
1118       return true;
1119     }
1120   }
1121
1122   printOperand(MI, OpNum, O);
1123   return false;
1124 }
1125
1126 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1127                                           unsigned OpNum, unsigned AsmVariant,
1128                                           const char *ExtraCode,
1129                                           raw_ostream &O) {
1130   if (ExtraCode && ExtraCode[0])
1131     return true; // Unknown modifier.
1132
1133   const MachineOperand &MO = MI->getOperand(OpNum);
1134   assert(MO.isReg() && "unexpected inline asm memory operand");
1135   O << "[" << getRegisterName(MO.getReg()) << "]";
1136   return false;
1137 }
1138
1139 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1140   if (EnableMCInst) {
1141     printInstructionThroughMCStreamer(MI);
1142     return;
1143   }
1144
1145   if (MI->getOpcode() == ARM::CONSTPOOL_ENTRY)
1146     EmitAlignment(2);
1147
1148   SmallString<128> Str;
1149   raw_svector_ostream OS(Str);
1150   if (MI->getOpcode() == ARM::DBG_VALUE) {
1151     PrintDebugValueComment(MI, OS);
1152   } else if (MI->getOpcode() == ARM::MOVs) {
1153     // FIXME: Thumb variants?
1154     const MachineOperand &Dst = MI->getOperand(0);
1155     const MachineOperand &MO1 = MI->getOperand(1);
1156     const MachineOperand &MO2 = MI->getOperand(2);
1157     const MachineOperand &MO3 = MI->getOperand(3);
1158
1159     OS << '\t' << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()));
1160     printSBitModifierOperand(MI, 6, OS);
1161     printPredicateOperand(MI, 4, OS);
1162
1163     OS << '\t' << getRegisterName(Dst.getReg())
1164        << ", " << getRegisterName(MO1.getReg());
1165
1166     if (ARM_AM::getSORegShOp(MO3.getImm()) != ARM_AM::rrx) {
1167       OS << ", ";
1168
1169       if (MO2.getReg()) {
1170         OS << getRegisterName(MO2.getReg());
1171         assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
1172       } else {
1173         OS << "#" << ARM_AM::getSORegOffset(MO3.getImm());
1174       }
1175     }
1176   } else
1177   // A8.6.123 PUSH
1178   if ((MI->getOpcode() == ARM::STM_UPD || MI->getOpcode() == ARM::t2STM_UPD) &&
1179       MI->getOperand(0).getReg() == ARM::SP &&
1180       ARM_AM::getAM4SubMode(MI->getOperand(2).getImm()) == ARM_AM::db) {
1181     OS << '\t' << "push";
1182     printPredicateOperand(MI, 3, OS);
1183     OS << '\t';
1184     printRegisterList(MI, 5, OS);
1185   } else
1186   // A8.6.122 POP
1187   if ((MI->getOpcode() == ARM::LDM_UPD || MI->getOpcode() == ARM::t2LDM_UPD) &&
1188       MI->getOperand(0).getReg() == ARM::SP &&
1189       ARM_AM::getAM4SubMode(MI->getOperand(2).getImm()) == ARM_AM::ia) {
1190     OS << '\t' << "pop";
1191     printPredicateOperand(MI, 3, OS);
1192     OS << '\t';
1193     printRegisterList(MI, 5, OS);
1194   } else
1195   // A8.6.355 VPUSH
1196   if ((MI->getOpcode() == ARM::VSTMS_UPD || MI->getOpcode() ==ARM::VSTMD_UPD) &&
1197       MI->getOperand(0).getReg() == ARM::SP &&
1198       ARM_AM::getAM4SubMode(MI->getOperand(2).getImm()) == ARM_AM::db) {
1199     OS << '\t' << "vpush";
1200     printPredicateOperand(MI, 3, OS);
1201     OS << '\t';
1202     printRegisterList(MI, 5, OS);
1203   } else
1204   // A8.6.354 VPOP
1205   if ((MI->getOpcode() == ARM::VLDMS_UPD || MI->getOpcode() ==ARM::VLDMD_UPD) &&
1206       MI->getOperand(0).getReg() == ARM::SP &&
1207       ARM_AM::getAM4SubMode(MI->getOperand(2).getImm()) == ARM_AM::ia) {
1208     OS << '\t' << "vpop";
1209     printPredicateOperand(MI, 3, OS);
1210     OS << '\t';
1211     printRegisterList(MI, 5, OS);
1212   } else
1213   // TRAP and tTRAP need special handling for non-Darwin. The GNU binutils
1214   // don't (yet) support the 'trap' mnemonic. (Use decimal, not hex, to
1215   // be consistent with the MC instruction printer.)
1216   // FIXME: This really should be in AsmPrinter/ARMInstPrinter.cpp, not here.
1217   //        Need a way to ask "isTargetDarwin()" there, first, though.
1218   if (MI->getOpcode() == ARM::TRAP && !Subtarget->isTargetDarwin()) {
1219     OS << "\t.long\t3892305662\t\t" << MAI->getCommentString() << "trap";
1220   } else if (MI->getOpcode() == ARM::tTRAP && !Subtarget->isTargetDarwin()) {
1221     OS << "\t.short\t57086\t\t\t" << MAI->getCommentString() << " trap";
1222   } else
1223     printInstruction(MI, OS);
1224
1225   // Output the instruction to the stream
1226   OutStreamer.EmitRawText(OS.str());
1227
1228   // Make sure the instruction that follows TBB is 2-byte aligned.
1229   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1230   if (MI->getOpcode() == ARM::t2TBB)
1231     EmitAlignment(1);
1232 }
1233
1234 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1235   if (Subtarget->isTargetDarwin()) {
1236     Reloc::Model RelocM = TM.getRelocationModel();
1237     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1238       // Declare all the text sections up front (before the DWARF sections
1239       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1240       // them together at the beginning of the object file.  This helps
1241       // avoid out-of-range branches that are due a fundamental limitation of
1242       // the way symbol offsets are encoded with the current Darwin ARM
1243       // relocations.
1244       const TargetLoweringObjectFileMachO &TLOFMacho =
1245         static_cast<const TargetLoweringObjectFileMachO &>(
1246           getObjFileLowering());
1247       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1248       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1249       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1250       if (RelocM == Reloc::DynamicNoPIC) {
1251         const MCSection *sect =
1252           OutContext.getMachOSection("__TEXT", "__symbol_stub4",
1253                                      MCSectionMachO::S_SYMBOL_STUBS,
1254                                      12, SectionKind::getText());
1255         OutStreamer.SwitchSection(sect);
1256       } else {
1257         const MCSection *sect =
1258           OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
1259                                      MCSectionMachO::S_SYMBOL_STUBS,
1260                                      16, SectionKind::getText());
1261         OutStreamer.SwitchSection(sect);
1262       }
1263       const MCSection *StaticInitSect =
1264         OutContext.getMachOSection("__TEXT", "__StaticInit",
1265                                    MCSectionMachO::S_REGULAR |
1266                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1267                                    SectionKind::getText());
1268       OutStreamer.SwitchSection(StaticInitSect);
1269     }
1270   }
1271
1272   // Use unified assembler syntax.
1273   OutStreamer.EmitRawText(StringRef("\t.syntax unified"));
1274
1275   // Emit ARM Build Attributes
1276   if (Subtarget->isTargetELF()) {
1277     // CPU Type
1278     std::string CPUString = Subtarget->getCPUString();
1279     if (CPUString != "generic")
1280       OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
1281
1282     // FIXME: Emit FPU type
1283     if (Subtarget->hasVFP2())
1284       OutStreamer.EmitRawText("\t.eabi_attribute " +
1285                               Twine(ARMBuildAttrs::VFP_arch) + ", 2");
1286
1287     // Signal various FP modes.
1288     if (!UnsafeFPMath) {
1289       OutStreamer.EmitRawText("\t.eabi_attribute " +
1290                               Twine(ARMBuildAttrs::ABI_FP_denormal) + ", 1");
1291       OutStreamer.EmitRawText("\t.eabi_attribute " +
1292                               Twine(ARMBuildAttrs::ABI_FP_exceptions) + ", 1");
1293     }
1294
1295     if (NoInfsFPMath && NoNaNsFPMath)
1296       OutStreamer.EmitRawText("\t.eabi_attribute " +
1297                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 1");
1298     else
1299       OutStreamer.EmitRawText("\t.eabi_attribute " +
1300                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 3");
1301
1302     // 8-bytes alignment stuff.
1303     OutStreamer.EmitRawText("\t.eabi_attribute " +
1304                             Twine(ARMBuildAttrs::ABI_align8_needed) + ", 1");
1305     OutStreamer.EmitRawText("\t.eabi_attribute " +
1306                             Twine(ARMBuildAttrs::ABI_align8_preserved) + ", 1");
1307
1308     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1309     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
1310       OutStreamer.EmitRawText("\t.eabi_attribute " +
1311                               Twine(ARMBuildAttrs::ABI_HardFP_use) + ", 3");
1312       OutStreamer.EmitRawText("\t.eabi_attribute " +
1313                               Twine(ARMBuildAttrs::ABI_VFP_args) + ", 1");
1314     }
1315     // FIXME: Should we signal R9 usage?
1316   }
1317 }
1318
1319
1320 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
1321   if (Subtarget->isTargetDarwin()) {
1322     // All darwin targets use mach-o.
1323     const TargetLoweringObjectFileMachO &TLOFMacho =
1324       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1325     MachineModuleInfoMachO &MMIMacho =
1326       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1327
1328     // Output non-lazy-pointers for external and common global variables.
1329     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1330
1331     if (!Stubs.empty()) {
1332       // Switch with ".non_lazy_symbol_pointer" directive.
1333       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1334       EmitAlignment(2);
1335       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1336         // L_foo$stub:
1337         OutStreamer.EmitLabel(Stubs[i].first);
1338         //   .indirect_symbol _foo
1339         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1340         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
1341
1342         if (MCSym.getInt())
1343           // External to current translation unit.
1344           OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
1345         else
1346           // Internal to current translation unit.
1347           //
1348           // When we place the LSDA into the TEXT section, the type info
1349           // pointers need to be indirect and pc-rel. We accomplish this by
1350           // using NLPs; however, sometimes the types are local to the file.
1351           // We need to fill in the value for the NLP in those cases.
1352           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1353                                                         OutContext),
1354                                 4/*size*/, 0/*addrspace*/);
1355       }
1356
1357       Stubs.clear();
1358       OutStreamer.AddBlankLine();
1359     }
1360
1361     Stubs = MMIMacho.GetHiddenGVStubList();
1362     if (!Stubs.empty()) {
1363       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1364       EmitAlignment(2);
1365       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1366         // L_foo$stub:
1367         OutStreamer.EmitLabel(Stubs[i].first);
1368         //   .long _foo
1369         OutStreamer.EmitValue(MCSymbolRefExpr::
1370                               Create(Stubs[i].second.getPointer(),
1371                                      OutContext),
1372                               4/*size*/, 0/*addrspace*/);
1373       }
1374
1375       Stubs.clear();
1376       OutStreamer.AddBlankLine();
1377     }
1378
1379     // Funny Darwin hack: This flag tells the linker that no global symbols
1380     // contain code that falls through to other global symbols (e.g. the obvious
1381     // implementation of multiple entry points).  If this doesn't occur, the
1382     // linker can safely perform dead code stripping.  Since LLVM never
1383     // generates code that does this, it is always safe to set.
1384     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1385   }
1386 }
1387
1388 //===----------------------------------------------------------------------===//
1389
1390 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
1391                              unsigned LabelId, MCContext &Ctx) {
1392
1393   MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
1394                        + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
1395   return Label;
1396 }
1397
1398 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
1399   unsigned Opcode = MI->getOpcode();
1400   int OpNum = 1;
1401   if (Opcode == ARM::BR_JTadd)
1402     OpNum = 2;
1403   else if (Opcode == ARM::BR_JTm)
1404     OpNum = 3;
1405
1406   const MachineOperand &MO1 = MI->getOperand(OpNum);
1407   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1408   unsigned JTI = MO1.getIndex();
1409
1410   // Emit a label for the jump table.
1411   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1412   OutStreamer.EmitLabel(JTISymbol);
1413
1414   // Emit each entry of the table.
1415   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1416   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1417   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1418
1419   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1420     MachineBasicBlock *MBB = JTBBs[i];
1421     // Construct an MCExpr for the entry. We want a value of the form:
1422     // (BasicBlockAddr - TableBeginAddr)
1423     //
1424     // For example, a table with entries jumping to basic blocks BB0 and BB1
1425     // would look like:
1426     // LJTI_0_0:
1427     //    .word (LBB0 - LJTI_0_0)
1428     //    .word (LBB1 - LJTI_0_0)
1429     const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1430
1431     if (TM.getRelocationModel() == Reloc::PIC_)
1432       Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
1433                                                                    OutContext),
1434                                      OutContext);
1435     OutStreamer.EmitValue(Expr, 4);
1436   }
1437 }
1438
1439 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1440   unsigned Opcode = MI->getOpcode();
1441   int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1442   const MachineOperand &MO1 = MI->getOperand(OpNum);
1443   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1444   unsigned JTI = MO1.getIndex();
1445
1446   // Emit a label for the jump table.
1447   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1448   OutStreamer.EmitLabel(JTISymbol);
1449
1450   // Emit each entry of the table.
1451   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1452   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1453   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1454   unsigned OffsetWidth = 4;
1455   if (MI->getOpcode() == ARM::t2TBB)
1456     OffsetWidth = 1;
1457   else if (MI->getOpcode() == ARM::t2TBH)
1458     OffsetWidth = 2;
1459
1460   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1461     MachineBasicBlock *MBB = JTBBs[i];
1462     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1463                                                       OutContext);
1464     // If this isn't a TBB or TBH, the entries are direct branch instructions.
1465     if (OffsetWidth == 4) {
1466       MCInst BrInst;
1467       BrInst.setOpcode(ARM::t2B);
1468       BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
1469       OutStreamer.EmitInstruction(BrInst);
1470       continue;
1471     }
1472     // Otherwise it's an offset from the dispatch instruction. Construct an
1473     // MCExpr for the entry. We want a value of the form:
1474     // (BasicBlockAddr - TableBeginAddr) / 2
1475     //
1476     // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1477     // would look like:
1478     // LJTI_0_0:
1479     //    .byte (LBB0 - LJTI_0_0) / 2
1480     //    .byte (LBB1 - LJTI_0_0) / 2
1481     const MCExpr *Expr =
1482       MCBinaryExpr::CreateSub(MBBSymbolExpr,
1483                               MCSymbolRefExpr::Create(JTISymbol, OutContext),
1484                               OutContext);
1485     Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1486                                    OutContext);
1487     OutStreamer.EmitValue(Expr, OffsetWidth);
1488   }
1489
1490   // Make sure the instruction that follows TBB is 2-byte aligned.
1491   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1492   if (MI->getOpcode() == ARM::t2TBB)
1493     EmitAlignment(1);
1494 }
1495
1496 void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1497                                            raw_ostream &OS) {
1498   unsigned NOps = MI->getNumOperands();
1499   assert(NOps==4);
1500   OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1501   // cast away const; DIetc do not take const operands for some reason.
1502   DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1503   OS << V.getName();
1504   OS << " <- ";
1505   // Frame address.  Currently handles register +- offset only.
1506   assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1507   OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1508   OS << ']';
1509   OS << "+";
1510   printOperand(MI, NOps-2, OS);
1511 }
1512
1513 void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
1514   ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
1515   switch (MI->getOpcode()) {
1516   case ARM::t2MOVi32imm:
1517     assert(0 && "Should be lowered by thumb2it pass");
1518   default: break;
1519   case ARM::DBG_VALUE: {
1520     if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1521       SmallString<128> TmpStr;
1522       raw_svector_ostream OS(TmpStr);
1523       PrintDebugValueComment(MI, OS);
1524       OutStreamer.EmitRawText(StringRef(OS.str()));
1525     }
1526     return;
1527   }
1528   case ARM::tPICADD: {
1529     // This is a pseudo op for a label + instruction sequence, which looks like:
1530     // LPC0:
1531     //     add r0, pc
1532     // This adds the address of LPC0 to r0.
1533
1534     // Emit the label.
1535     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1536                           getFunctionNumber(), MI->getOperand(2).getImm(),
1537                           OutContext));
1538
1539     // Form and emit the add.
1540     MCInst AddInst;
1541     AddInst.setOpcode(ARM::tADDhirr);
1542     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1543     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1544     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1545     // Add predicate operands.
1546     AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1547     AddInst.addOperand(MCOperand::CreateReg(0));
1548     OutStreamer.EmitInstruction(AddInst);
1549     return;
1550   }
1551   case ARM::PICADD: { // FIXME: Remove asm string from td file.
1552     // This is a pseudo op for a label + instruction sequence, which looks like:
1553     // LPC0:
1554     //     add r0, pc, r0
1555     // This adds the address of LPC0 to r0.
1556
1557     // Emit the label.
1558     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1559                           getFunctionNumber(), MI->getOperand(2).getImm(),
1560                           OutContext));
1561
1562     // Form and emit the add.
1563     MCInst AddInst;
1564     AddInst.setOpcode(ARM::ADDrr);
1565     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1566     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1567     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1568     // Add predicate operands.
1569     AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1570     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1571     // Add 's' bit operand (always reg0 for this)
1572     AddInst.addOperand(MCOperand::CreateReg(0));
1573     OutStreamer.EmitInstruction(AddInst);
1574     return;
1575   }
1576   case ARM::PICSTR:
1577   case ARM::PICSTRB:
1578   case ARM::PICSTRH:
1579   case ARM::PICLDR:
1580   case ARM::PICLDRB:
1581   case ARM::PICLDRH:
1582   case ARM::PICLDRSB:
1583   case ARM::PICLDRSH: {
1584     // This is a pseudo op for a label + instruction sequence, which looks like:
1585     // LPC0:
1586     //     OP r0, [pc, r0]
1587     // The LCP0 label is referenced by a constant pool entry in order to get
1588     // a PC-relative address at the ldr instruction.
1589
1590     // Emit the label.
1591     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1592                           getFunctionNumber(), MI->getOperand(2).getImm(),
1593                           OutContext));
1594
1595     // Form and emit the load
1596     unsigned Opcode;
1597     switch (MI->getOpcode()) {
1598     default:
1599       llvm_unreachable("Unexpected opcode!");
1600     case ARM::PICSTR:   Opcode = ARM::STR; break;
1601     case ARM::PICSTRB:  Opcode = ARM::STRB; break;
1602     case ARM::PICSTRH:  Opcode = ARM::STRH; break;
1603     case ARM::PICLDR:   Opcode = ARM::LDR; break;
1604     case ARM::PICLDRB:  Opcode = ARM::LDRB; break;
1605     case ARM::PICLDRH:  Opcode = ARM::LDRH; break;
1606     case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1607     case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1608     }
1609     MCInst LdStInst;
1610     LdStInst.setOpcode(Opcode);
1611     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1612     LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1613     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1614     LdStInst.addOperand(MCOperand::CreateImm(0));
1615     // Add predicate operands.
1616     LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1617     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1618     OutStreamer.EmitInstruction(LdStInst);
1619
1620     return;
1621   }
1622   case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1623     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1624     /// in the function.  The first operand is the ID# for this instruction, the
1625     /// second is the index into the MachineConstantPool that this is, the third
1626     /// is the size in bytes of this constant pool entry.
1627     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1628     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1629
1630     EmitAlignment(2);
1631     OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1632
1633     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1634     if (MCPE.isMachineConstantPoolEntry())
1635       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1636     else
1637       EmitGlobalConstant(MCPE.Val.ConstVal);
1638
1639     return;
1640   }
1641   case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1642     // This is a hack that lowers as a two instruction sequence.
1643     unsigned DstReg = MI->getOperand(0).getReg();
1644     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1645
1646     unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1647     unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1648
1649     {
1650       MCInst TmpInst;
1651       TmpInst.setOpcode(ARM::MOVi);
1652       TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1653       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1654
1655       // Predicate.
1656       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1657       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1658
1659       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1660       OutStreamer.EmitInstruction(TmpInst);
1661     }
1662
1663     {
1664       MCInst TmpInst;
1665       TmpInst.setOpcode(ARM::ORRri);
1666       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // dstreg
1667       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // inreg
1668       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1669       // Predicate.
1670       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1671       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1672
1673       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1674       OutStreamer.EmitInstruction(TmpInst);
1675     }
1676     return;
1677   }
1678   case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1679     // This is a hack that lowers as a two instruction sequence.
1680     unsigned DstReg = MI->getOperand(0).getReg();
1681     const MachineOperand &MO = MI->getOperand(1);
1682     MCOperand V1, V2;
1683     if (MO.isImm()) {
1684       unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1685       V1 = MCOperand::CreateImm(ImmVal & 65535);
1686       V2 = MCOperand::CreateImm(ImmVal >> 16);
1687     } else if (MO.isGlobal()) {
1688       MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO.getGlobal());
1689       const MCSymbolRefExpr *SymRef1 =
1690         MCSymbolRefExpr::Create(Symbol,
1691                                 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
1692       const MCSymbolRefExpr *SymRef2 =
1693         MCSymbolRefExpr::Create(Symbol,
1694                                 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
1695       V1 = MCOperand::CreateExpr(SymRef1);
1696       V2 = MCOperand::CreateExpr(SymRef2);
1697     } else {
1698       // FIXME: External symbol?
1699       MI->dump();
1700       llvm_unreachable("cannot handle this operand");
1701     }
1702
1703     {
1704       MCInst TmpInst;
1705       TmpInst.setOpcode(ARM::MOVi16);
1706       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1707       TmpInst.addOperand(V1); // lower16(imm)
1708
1709       // Predicate.
1710       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1711       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1712
1713       OutStreamer.EmitInstruction(TmpInst);
1714     }
1715
1716     {
1717       MCInst TmpInst;
1718       TmpInst.setOpcode(ARM::MOVTi16);
1719       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1720       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // srcreg
1721       TmpInst.addOperand(V2);   // upper16(imm)
1722
1723       // Predicate.
1724       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1725       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1726
1727       OutStreamer.EmitInstruction(TmpInst);
1728     }
1729
1730     return;
1731   }
1732   case ARM::t2TBB:
1733   case ARM::t2TBH:
1734   case ARM::t2BR_JT: {
1735     // Lower and emit the instruction itself, then the jump table following it.
1736     MCInst TmpInst;
1737     MCInstLowering.Lower(MI, TmpInst);
1738     OutStreamer.EmitInstruction(TmpInst);
1739     EmitJump2Table(MI);
1740     return;
1741   }
1742   case ARM::tBR_JTr:
1743   case ARM::BR_JTr:
1744   case ARM::BR_JTm:
1745   case ARM::BR_JTadd: {
1746     // Lower and emit the instruction itself, then the jump table following it.
1747     MCInst TmpInst;
1748     MCInstLowering.Lower(MI, TmpInst);
1749     OutStreamer.EmitInstruction(TmpInst);
1750     EmitJumpTable(MI);
1751     return;
1752   }
1753   case ARM::TRAP: {
1754     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1755     // FIXME: Remove this special case when they do.
1756     if (!Subtarget->isTargetDarwin()) {
1757       //.long 0xe7ffdefe ${:comment} trap
1758       uint32_t Val = 0xe7ffdefeUL;
1759       OutStreamer.AddComment("trap");
1760       OutStreamer.EmitIntValue(Val, 4);
1761       return;
1762     }
1763     break;
1764   }
1765   case ARM::tTRAP: {
1766     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1767     // FIXME: Remove this special case when they do.
1768     if (!Subtarget->isTargetDarwin()) {
1769       //.short 57086 ${:comment} trap
1770       uint16_t Val = 0xdefe;
1771       OutStreamer.AddComment("trap");
1772       OutStreamer.EmitIntValue(Val, 2);
1773       return;
1774     }
1775     break;
1776   }
1777   case ARM::t2Int_eh_sjlj_setjmp:
1778   case ARM::t2Int_eh_sjlj_setjmp_nofp:
1779   case ARM::tInt_eh_sjlj_setjmp: { // FIXME: Remove asmstring from td file.
1780     // Two incoming args: GPR:$src, GPR:$val
1781     // mov $val, pc
1782     // adds $val, #7
1783     // str $val, [$src, #4]
1784     // movs r0, #0
1785     // b 1f
1786     // movs r0, #1
1787     // 1:
1788     unsigned SrcReg = MI->getOperand(0).getReg();
1789     unsigned ValReg = MI->getOperand(1).getReg();
1790     MCSymbol *Label = GetARMSJLJEHLabel();
1791     {
1792       MCInst TmpInst;
1793       TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
1794       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1795       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1796       // 's' bit operand
1797       TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1798       OutStreamer.AddComment("eh_setjmp begin");
1799       OutStreamer.EmitInstruction(TmpInst);
1800     }
1801     {
1802       MCInst TmpInst;
1803       TmpInst.setOpcode(ARM::tADDi3);
1804       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1805       // 's' bit operand
1806       TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1807       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1808       TmpInst.addOperand(MCOperand::CreateImm(7));
1809       // Predicate.
1810       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1811       TmpInst.addOperand(MCOperand::CreateReg(0));
1812       OutStreamer.EmitInstruction(TmpInst);
1813     }
1814     {
1815       MCInst TmpInst;
1816       TmpInst.setOpcode(ARM::tSTR);
1817       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1818       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1819       // The offset immediate is #4. The operand value is scaled by 4 for the
1820       // tSTR instruction.
1821       TmpInst.addOperand(MCOperand::CreateImm(1));
1822       TmpInst.addOperand(MCOperand::CreateReg(0));
1823       // Predicate.
1824       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1825       TmpInst.addOperand(MCOperand::CreateReg(0));
1826       OutStreamer.EmitInstruction(TmpInst);
1827     }
1828     {
1829       MCInst TmpInst;
1830       TmpInst.setOpcode(ARM::tMOVi8);
1831       TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1832       TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1833       TmpInst.addOperand(MCOperand::CreateImm(0));
1834       // Predicate.
1835       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1836       TmpInst.addOperand(MCOperand::CreateReg(0));
1837       OutStreamer.EmitInstruction(TmpInst);
1838     }
1839     {
1840       const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1841       MCInst TmpInst;
1842       TmpInst.setOpcode(ARM::tB);
1843       TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1844       OutStreamer.EmitInstruction(TmpInst);
1845     }
1846     {
1847       MCInst TmpInst;
1848       TmpInst.setOpcode(ARM::tMOVi8);
1849       TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1850       TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1851       TmpInst.addOperand(MCOperand::CreateImm(1));
1852       // Predicate.
1853       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1854       TmpInst.addOperand(MCOperand::CreateReg(0));
1855       OutStreamer.AddComment("eh_setjmp end");
1856       OutStreamer.EmitInstruction(TmpInst);
1857     }
1858     OutStreamer.EmitLabel(Label);
1859     return;
1860   }
1861
1862   case ARM::Int_eh_sjlj_setjmp_nofp:
1863   case ARM::Int_eh_sjlj_setjmp: { // FIXME: Remove asmstring from td file.
1864     // Two incoming args: GPR:$src, GPR:$val
1865     // add $val, pc, #8
1866     // str $val, [$src, #+4]
1867     // mov r0, #0
1868     // add pc, pc, #0
1869     // mov r0, #1
1870     unsigned SrcReg = MI->getOperand(0).getReg();
1871     unsigned ValReg = MI->getOperand(1).getReg();
1872
1873     {
1874       MCInst TmpInst;
1875       TmpInst.setOpcode(ARM::ADDri);
1876       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1877       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1878       TmpInst.addOperand(MCOperand::CreateImm(8));
1879       // Predicate.
1880       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1881       TmpInst.addOperand(MCOperand::CreateReg(0));
1882       // 's' bit operand (always reg0 for this).
1883       TmpInst.addOperand(MCOperand::CreateReg(0));
1884       OutStreamer.AddComment("eh_setjmp begin");
1885       OutStreamer.EmitInstruction(TmpInst);
1886     }
1887     {
1888       MCInst TmpInst;
1889       TmpInst.setOpcode(ARM::STR);
1890       TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1891       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1892       TmpInst.addOperand(MCOperand::CreateReg(0));
1893       TmpInst.addOperand(MCOperand::CreateImm(4));
1894       // Predicate.
1895       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1896       TmpInst.addOperand(MCOperand::CreateReg(0));
1897       OutStreamer.EmitInstruction(TmpInst);
1898     }
1899     {
1900       MCInst TmpInst;
1901       TmpInst.setOpcode(ARM::MOVi);
1902       TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1903       TmpInst.addOperand(MCOperand::CreateImm(0));
1904       // Predicate.
1905       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1906       TmpInst.addOperand(MCOperand::CreateReg(0));
1907       // 's' bit operand (always reg0 for this).
1908       TmpInst.addOperand(MCOperand::CreateReg(0));
1909       OutStreamer.EmitInstruction(TmpInst);
1910     }
1911     {
1912       MCInst TmpInst;
1913       TmpInst.setOpcode(ARM::ADDri);
1914       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1915       TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1916       TmpInst.addOperand(MCOperand::CreateImm(0));
1917       // Predicate.
1918       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1919       TmpInst.addOperand(MCOperand::CreateReg(0));
1920       // 's' bit operand (always reg0 for this).
1921       TmpInst.addOperand(MCOperand::CreateReg(0));
1922       OutStreamer.EmitInstruction(TmpInst);
1923     }
1924     {
1925       MCInst TmpInst;
1926       TmpInst.setOpcode(ARM::MOVi);
1927       TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1928       TmpInst.addOperand(MCOperand::CreateImm(1));
1929       // Predicate.
1930       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1931       TmpInst.addOperand(MCOperand::CreateReg(0));
1932       // 's' bit operand (always reg0 for this).
1933       TmpInst.addOperand(MCOperand::CreateReg(0));
1934       OutStreamer.AddComment("eh_setjmp end");
1935       OutStreamer.EmitInstruction(TmpInst);
1936     }
1937     return;
1938   }
1939   case ARM::Int_eh_sjlj_longjmp: {
1940     // ldr sp, [$src, #8]
1941     // ldr $scratch, [$src, #4]
1942     // ldr r7, [$src]
1943     // bx $scratch
1944     unsigned SrcReg = MI->getOperand(0).getReg();
1945     unsigned ScratchReg = MI->getOperand(1).getReg();
1946     {
1947       MCInst TmpInst;
1948       TmpInst.setOpcode(ARM::LDR);
1949       TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1950       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1951       TmpInst.addOperand(MCOperand::CreateReg(0));
1952       TmpInst.addOperand(MCOperand::CreateImm(8));
1953       // Predicate.
1954       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1955       TmpInst.addOperand(MCOperand::CreateReg(0));
1956       OutStreamer.EmitInstruction(TmpInst);
1957     }
1958     {
1959       MCInst TmpInst;
1960       TmpInst.setOpcode(ARM::LDR);
1961       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1962       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1963       TmpInst.addOperand(MCOperand::CreateReg(0));
1964       TmpInst.addOperand(MCOperand::CreateImm(4));
1965       // Predicate.
1966       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1967       TmpInst.addOperand(MCOperand::CreateReg(0));
1968       OutStreamer.EmitInstruction(TmpInst);
1969     }
1970     {
1971       MCInst TmpInst;
1972       TmpInst.setOpcode(ARM::LDR);
1973       TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1974       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1975       TmpInst.addOperand(MCOperand::CreateReg(0));
1976       TmpInst.addOperand(MCOperand::CreateImm(0));
1977       // Predicate.
1978       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1979       TmpInst.addOperand(MCOperand::CreateReg(0));
1980       OutStreamer.EmitInstruction(TmpInst);
1981     }
1982     {
1983       MCInst TmpInst;
1984       TmpInst.setOpcode(ARM::BRIND);
1985       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1986       // Predicate.
1987       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1988       TmpInst.addOperand(MCOperand::CreateReg(0));
1989       OutStreamer.EmitInstruction(TmpInst);
1990     }
1991     return;
1992   }
1993   case ARM::tInt_eh_sjlj_longjmp: {
1994     // ldr $scratch, [$src, #8]
1995     // mov sp, $scratch
1996     // ldr $scratch, [$src, #4]
1997     // ldr r7, [$src]
1998     // bx $scratch
1999     unsigned SrcReg = MI->getOperand(0).getReg();
2000     unsigned ScratchReg = MI->getOperand(1).getReg();
2001     {
2002       MCInst TmpInst;
2003       TmpInst.setOpcode(ARM::tLDR);
2004       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2005       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
2006       // The offset immediate is #8. The operand value is scaled by 4 for the
2007       // tSTR instruction.
2008       TmpInst.addOperand(MCOperand::CreateImm(2));
2009       TmpInst.addOperand(MCOperand::CreateReg(0));
2010       // Predicate.
2011       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2012       TmpInst.addOperand(MCOperand::CreateReg(0));
2013       OutStreamer.EmitInstruction(TmpInst);
2014     }
2015     {
2016       MCInst TmpInst;
2017       TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
2018       TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
2019       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2020       // Predicate.
2021       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2022       TmpInst.addOperand(MCOperand::CreateReg(0));
2023       OutStreamer.EmitInstruction(TmpInst);
2024     }
2025     {
2026       MCInst TmpInst;
2027       TmpInst.setOpcode(ARM::tLDR);
2028       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2029       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
2030       TmpInst.addOperand(MCOperand::CreateImm(1));
2031       TmpInst.addOperand(MCOperand::CreateReg(0));
2032       // Predicate.
2033       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2034       TmpInst.addOperand(MCOperand::CreateReg(0));
2035       OutStreamer.EmitInstruction(TmpInst);
2036     }
2037     {
2038       MCInst TmpInst;
2039       TmpInst.setOpcode(ARM::tLDR);
2040       TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
2041       TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
2042       TmpInst.addOperand(MCOperand::CreateImm(0));
2043       TmpInst.addOperand(MCOperand::CreateReg(0));
2044       // Predicate.
2045       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2046       TmpInst.addOperand(MCOperand::CreateReg(0));
2047       OutStreamer.EmitInstruction(TmpInst);
2048     }
2049     {
2050       MCInst TmpInst;
2051       TmpInst.setOpcode(ARM::tBX_RET_vararg);
2052       TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2053       // Predicate.
2054       TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2055       TmpInst.addOperand(MCOperand::CreateReg(0));
2056       OutStreamer.EmitInstruction(TmpInst);
2057     }
2058     return;
2059   }
2060   }
2061
2062   MCInst TmpInst;
2063   MCInstLowering.Lower(MI, TmpInst);
2064   OutStreamer.EmitInstruction(TmpInst);
2065 }
2066
2067 //===----------------------------------------------------------------------===//
2068 // Target Registry Stuff
2069 //===----------------------------------------------------------------------===//
2070
2071 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
2072                                              unsigned SyntaxVariant,
2073                                              const MCAsmInfo &MAI) {
2074   if (SyntaxVariant == 0)
2075     return new ARMInstPrinter(MAI);
2076   return 0;
2077 }
2078
2079 // Force static initialization.
2080 extern "C" void LLVMInitializeARMAsmPrinter() {
2081   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
2082   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
2083
2084   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
2085   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
2086 }
2087