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