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