Unbreak build. Jim, please review.
[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   // TRAP and tTRAP need special handling for non-Darwin. The GNU binutils
1214   // don't (yet) support the 'trap' mnemonic. (Use decimal, not hex, to
1215   // be consistent with the MC instruction printer.)
1216   // FIXME: This really should be in AsmPrinter/ARMInstPrinter.cpp, not here.
1217   //        Need a way to ask "isTargetDarwin()" there, first, though.
1218   if (MI->getOpcode() == ARM::TRAP && !Subtarget->isTargetDarwin()) {
1219     OS << "\t.long\t3892305662\t\t" << MAI->getCommentString() << "trap";
1220   } else if (MI->getOpcode() == ARM::tTRAP && !Subtarget->isTargetDarwin()) {
1221     OS << "\t.short\t57086\t\t\t" << MAI->getCommentString() << " trap";
1222   } else
1223     printInstruction(MI, OS);
1224
1225   // Output the instruction to the stream
1226   OutStreamer.EmitRawText(OS.str());
1227
1228   // Make sure the instruction that follows TBB is 2-byte aligned.
1229   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1230   if (MI->getOpcode() == ARM::t2TBB)
1231     EmitAlignment(1);
1232 }
1233
1234 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1235   if (Subtarget->isTargetDarwin()) {
1236     Reloc::Model RelocM = TM.getRelocationModel();
1237     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1238       // Declare all the text sections up front (before the DWARF sections
1239       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1240       // them together at the beginning of the object file.  This helps
1241       // avoid out-of-range branches that are due a fundamental limitation of
1242       // the way symbol offsets are encoded with the current Darwin ARM
1243       // relocations.
1244       const TargetLoweringObjectFileMachO &TLOFMacho =
1245         static_cast<const TargetLoweringObjectFileMachO &>(
1246           getObjFileLowering());
1247       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1248       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1249       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1250       if (RelocM == Reloc::DynamicNoPIC) {
1251         const MCSection *sect =
1252           OutContext.getMachOSection("__TEXT", "__symbol_stub4",
1253                                      MCSectionMachO::S_SYMBOL_STUBS,
1254                                      12, SectionKind::getText());
1255         OutStreamer.SwitchSection(sect);
1256       } else {
1257         const MCSection *sect =
1258           OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
1259                                      MCSectionMachO::S_SYMBOL_STUBS,
1260                                      16, SectionKind::getText());
1261         OutStreamer.SwitchSection(sect);
1262       }
1263       const MCSection *StaticInitSect =
1264         OutContext.getMachOSection("__TEXT", "__StaticInit",
1265                                    MCSectionMachO::S_REGULAR |
1266                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1267                                    SectionKind::getText());
1268       OutStreamer.SwitchSection(StaticInitSect);
1269     }
1270   }
1271
1272   // Use unified assembler syntax.
1273   OutStreamer.EmitRawText(StringRef("\t.syntax unified"));
1274
1275   // Emit ARM Build Attributes
1276   if (Subtarget->isTargetELF()) {
1277     // CPU Type
1278     std::string CPUString = Subtarget->getCPUString();
1279     if (CPUString != "generic")
1280       OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
1281
1282     // FIXME: Emit FPU type
1283     if (Subtarget->hasVFP2())
1284       OutStreamer.EmitRawText("\t.eabi_attribute " +
1285                               Twine(ARMBuildAttrs::VFP_arch) + ", 2");
1286
1287     // Signal various FP modes.
1288     if (!UnsafeFPMath) {
1289       OutStreamer.EmitRawText("\t.eabi_attribute " +
1290                               Twine(ARMBuildAttrs::ABI_FP_denormal) + ", 1");
1291       OutStreamer.EmitRawText("\t.eabi_attribute " +
1292                               Twine(ARMBuildAttrs::ABI_FP_exceptions) + ", 1");
1293     }
1294
1295     if (NoInfsFPMath && NoNaNsFPMath)
1296       OutStreamer.EmitRawText("\t.eabi_attribute " +
1297                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 1");
1298     else
1299       OutStreamer.EmitRawText("\t.eabi_attribute " +
1300                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 3");
1301
1302     // 8-bytes alignment stuff.
1303     OutStreamer.EmitRawText("\t.eabi_attribute " +
1304                             Twine(ARMBuildAttrs::ABI_align8_needed) + ", 1");
1305     OutStreamer.EmitRawText("\t.eabi_attribute " +
1306                             Twine(ARMBuildAttrs::ABI_align8_preserved) + ", 1");
1307
1308     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1309     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
1310       OutStreamer.EmitRawText("\t.eabi_attribute " +
1311                               Twine(ARMBuildAttrs::ABI_HardFP_use) + ", 3");
1312       OutStreamer.EmitRawText("\t.eabi_attribute " +
1313                               Twine(ARMBuildAttrs::ABI_VFP_args) + ", 1");
1314     }
1315     // FIXME: Should we signal R9 usage?
1316   }
1317 }
1318
1319
1320 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
1321   if (Subtarget->isTargetDarwin()) {
1322     // All darwin targets use mach-o.
1323     const TargetLoweringObjectFileMachO &TLOFMacho =
1324       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1325     MachineModuleInfoMachO &MMIMacho =
1326       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1327
1328     // Output non-lazy-pointers for external and common global variables.
1329     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1330
1331     if (!Stubs.empty()) {
1332       // Switch with ".non_lazy_symbol_pointer" directive.
1333       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1334       EmitAlignment(2);
1335       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1336         // L_foo$stub:
1337         OutStreamer.EmitLabel(Stubs[i].first);
1338         //   .indirect_symbol _foo
1339         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1340         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
1341
1342         if (MCSym.getInt())
1343           // External to current translation unit.
1344           OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
1345         else
1346           // Internal to current translation unit.
1347           //
1348           // When we place the LSDA into the TEXT section, the type info
1349           // pointers need to be indirect and pc-rel. We accomplish this by
1350           // using NLPs; however, sometimes the types are local to the file.
1351           // We need to fill in the value for the NLP in those cases.
1352           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1353                                                         OutContext),
1354                                 4/*size*/, 0/*addrspace*/);
1355       }
1356
1357       Stubs.clear();
1358       OutStreamer.AddBlankLine();
1359     }
1360
1361     Stubs = MMIMacho.GetHiddenGVStubList();
1362     if (!Stubs.empty()) {
1363       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1364       EmitAlignment(2);
1365       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1366         // L_foo$stub:
1367         OutStreamer.EmitLabel(Stubs[i].first);
1368         //   .long _foo
1369         OutStreamer.EmitValue(MCSymbolRefExpr::
1370                               Create(Stubs[i].second.getPointer(),
1371                                      OutContext),
1372                               4/*size*/, 0/*addrspace*/);
1373       }
1374
1375       Stubs.clear();
1376       OutStreamer.AddBlankLine();
1377     }
1378
1379     // Funny Darwin hack: This flag tells the linker that no global symbols
1380     // contain code that falls through to other global symbols (e.g. the obvious
1381     // implementation of multiple entry points).  If this doesn't occur, the
1382     // linker can safely perform dead code stripping.  Since LLVM never
1383     // generates code that does this, it is always safe to set.
1384     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1385   }
1386 }
1387
1388 //===----------------------------------------------------------------------===//
1389
1390 static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
1391                              unsigned LabelId, MCContext &Ctx) {
1392
1393   MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
1394                        + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
1395   return Label;
1396 }
1397
1398 void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
1399   unsigned Opcode = MI->getOpcode();
1400   int OpNum = 1;
1401   if (Opcode == ARM::BR_JTadd)
1402     OpNum = 2;
1403   else if (Opcode == ARM::BR_JTm)
1404     OpNum = 3;
1405
1406   const MachineOperand &MO1 = MI->getOperand(OpNum);
1407   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1408   unsigned JTI = MO1.getIndex();
1409
1410   // Emit a label for the jump table.
1411   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1412   OutStreamer.EmitLabel(JTISymbol);
1413
1414   // Emit each entry of the table.
1415   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1416   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1417   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1418
1419   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1420     MachineBasicBlock *MBB = JTBBs[i];
1421     // Construct an MCExpr for the entry. We want a value of the form:
1422     // (BasicBlockAddr - TableBeginAddr)
1423     //
1424     // For example, a table with entries jumping to basic blocks BB0 and BB1
1425     // would look like:
1426     // LJTI_0_0:
1427     //    .word (LBB0 - LJTI_0_0)
1428     //    .word (LBB1 - LJTI_0_0)
1429     const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1430
1431     if (TM.getRelocationModel() == Reloc::PIC_)
1432       Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
1433                                                                    OutContext),
1434                                      OutContext);
1435     OutStreamer.EmitValue(Expr, 4);
1436   }
1437 }
1438
1439 void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1440   unsigned Opcode = MI->getOpcode();
1441   int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1442   const MachineOperand &MO1 = MI->getOperand(OpNum);
1443   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1444   unsigned JTI = MO1.getIndex();
1445
1446   // Emit a label for the jump table.
1447   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1448   OutStreamer.EmitLabel(JTISymbol);
1449
1450   // Emit each entry of the table.
1451   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1452   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1453   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1454   unsigned OffsetWidth = 4;
1455   if (MI->getOpcode() == ARM::t2TBB)
1456     OffsetWidth = 1;
1457   else if (MI->getOpcode() == ARM::t2TBH)
1458     OffsetWidth = 2;
1459
1460   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1461     MachineBasicBlock *MBB = JTBBs[i];
1462     const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1463                                                       OutContext);
1464     // If this isn't a TBB or TBH, the entries are direct branch instructions.
1465     if (OffsetWidth == 4) {
1466       MCInst BrInst;
1467       BrInst.setOpcode(ARM::t2B);
1468       BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
1469       OutStreamer.EmitInstruction(BrInst);
1470       continue;
1471     }
1472     // Otherwise it's an offset from the dispatch instruction. Construct an
1473     // MCExpr for the entry. We want a value of the form:
1474     // (BasicBlockAddr - TableBeginAddr) / 2
1475     //
1476     // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1477     // would look like:
1478     // LJTI_0_0:
1479     //    .byte (LBB0 - LJTI_0_0) / 2
1480     //    .byte (LBB1 - LJTI_0_0) / 2
1481     const MCExpr *Expr =
1482       MCBinaryExpr::CreateSub(MBBSymbolExpr,
1483                               MCSymbolRefExpr::Create(JTISymbol, OutContext),
1484                               OutContext);
1485     Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1486                                    OutContext);
1487     OutStreamer.EmitValue(Expr, OffsetWidth);
1488   }
1489
1490   // Make sure the instruction that follows TBB is 2-byte aligned.
1491   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1492   if (MI->getOpcode() == ARM::t2TBB)
1493     EmitAlignment(1);
1494 }
1495
1496 void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
1497   ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
1498   switch (MI->getOpcode()) {
1499   case ARM::t2MOVi32imm:
1500     assert(0 && "Should be lowered by thumb2it pass");
1501   default: break;
1502   case ARM::tPICADD: {
1503     // This is a pseudo op for a label + instruction sequence, which looks like:
1504     // LPC0:
1505     //     add r0, pc
1506     // This adds the address of LPC0 to r0.
1507
1508     // Emit the label.
1509     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1510                           getFunctionNumber(), MI->getOperand(2).getImm(),
1511                           OutContext));
1512
1513     // Form and emit the add.
1514     MCInst AddInst;
1515     AddInst.setOpcode(ARM::tADDhirr);
1516     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1517     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1518     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1519     // Add predicate operands.
1520     AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1521     AddInst.addOperand(MCOperand::CreateReg(0));
1522     OutStreamer.EmitInstruction(AddInst);
1523     return;
1524   }
1525   case ARM::PICADD: { // FIXME: Remove asm string from td file.
1526     // This is a pseudo op for a label + instruction sequence, which looks like:
1527     // LPC0:
1528     //     add r0, pc, r0
1529     // This adds the address of LPC0 to r0.
1530
1531     // Emit the label.
1532     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1533                           getFunctionNumber(), MI->getOperand(2).getImm(),
1534                           OutContext));
1535
1536     // Form and emit the add.
1537     MCInst AddInst;
1538     AddInst.setOpcode(ARM::ADDrr);
1539     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1540     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1541     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1542     // Add predicate operands.
1543     AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1544     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1545     // Add 's' bit operand (always reg0 for this)
1546     AddInst.addOperand(MCOperand::CreateReg(0));
1547     OutStreamer.EmitInstruction(AddInst);
1548     return;
1549   }
1550   case ARM::PICSTR:
1551   case ARM::PICSTRB:
1552   case ARM::PICSTRH:
1553   case ARM::PICLDR:
1554   case ARM::PICLDRB:
1555   case ARM::PICLDRH:
1556   case ARM::PICLDRSB:
1557   case ARM::PICLDRSH: {
1558     // This is a pseudo op for a label + instruction sequence, which looks like:
1559     // LPC0:
1560     //     OP r0, [pc, r0]
1561     // The LCP0 label is referenced by a constant pool entry in order to get
1562     // a PC-relative address at the ldr instruction.
1563
1564     // Emit the label.
1565     OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1566                           getFunctionNumber(), MI->getOperand(2).getImm(),
1567                           OutContext));
1568
1569     // Form and emit the load
1570     unsigned Opcode;
1571     switch (MI->getOpcode()) {
1572     default:
1573       llvm_unreachable("Unexpected opcode!");
1574     case ARM::PICSTR:   Opcode = ARM::STR; break;
1575     case ARM::PICSTRB:  Opcode = ARM::STRB; break;
1576     case ARM::PICSTRH:  Opcode = ARM::STRH; break;
1577     case ARM::PICLDR:   Opcode = ARM::LDR; break;
1578     case ARM::PICLDRB:  Opcode = ARM::LDRB; break;
1579     case ARM::PICLDRH:  Opcode = ARM::LDRH; break;
1580     case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1581     case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1582     }
1583     MCInst LdStInst;
1584     LdStInst.setOpcode(Opcode);
1585     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1586     LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1587     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1588     LdStInst.addOperand(MCOperand::CreateImm(0));
1589     // Add predicate operands.
1590     LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1591     LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1592     OutStreamer.EmitInstruction(LdStInst);
1593
1594     return;
1595   }
1596   case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1597     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1598     /// in the function.  The first operand is the ID# for this instruction, the
1599     /// second is the index into the MachineConstantPool that this is, the third
1600     /// is the size in bytes of this constant pool entry.
1601     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1602     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1603
1604     EmitAlignment(2);
1605     OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1606
1607     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1608     if (MCPE.isMachineConstantPoolEntry())
1609       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1610     else
1611       EmitGlobalConstant(MCPE.Val.ConstVal);
1612
1613     return;
1614   }
1615   case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1616     // This is a hack that lowers as a two instruction sequence.
1617     unsigned DstReg = MI->getOperand(0).getReg();
1618     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1619
1620     unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1621     unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1622
1623     {
1624       MCInst TmpInst;
1625       TmpInst.setOpcode(ARM::MOVi);
1626       TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1627       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1628
1629       // Predicate.
1630       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1631       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1632
1633       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1634       OutStreamer.EmitInstruction(TmpInst);
1635     }
1636
1637     {
1638       MCInst TmpInst;
1639       TmpInst.setOpcode(ARM::ORRri);
1640       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // dstreg
1641       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // inreg
1642       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1643       // Predicate.
1644       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1645       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1646
1647       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1648       OutStreamer.EmitInstruction(TmpInst);
1649     }
1650     return;
1651   }
1652   case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1653     // This is a hack that lowers as a two instruction sequence.
1654     unsigned DstReg = MI->getOperand(0).getReg();
1655     const MachineOperand &MO = MI->getOperand(1);
1656     MCOperand V1, V2;
1657     if (MO.isImm()) {
1658       unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1659       V1 = MCOperand::CreateImm(ImmVal & 65535);
1660       V2 = MCOperand::CreateImm(ImmVal >> 16);
1661     } else if (MO.isGlobal()) {
1662       MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO.getGlobal());
1663       const MCSymbolRefExpr *SymRef1 =
1664         MCSymbolRefExpr::Create(Symbol,
1665                                 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
1666       const MCSymbolRefExpr *SymRef2 =
1667         MCSymbolRefExpr::Create(Symbol,
1668                                 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
1669       V1 = MCOperand::CreateExpr(SymRef1);
1670       V2 = MCOperand::CreateExpr(SymRef2);
1671     } else {
1672       // FIXME: External symbol?
1673       MI->dump();
1674       llvm_unreachable("cannot handle this operand");
1675     }
1676
1677     {
1678       MCInst TmpInst;
1679       TmpInst.setOpcode(ARM::MOVi16);
1680       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1681       TmpInst.addOperand(V1); // lower16(imm)
1682
1683       // Predicate.
1684       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1685       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1686
1687       OutStreamer.EmitInstruction(TmpInst);
1688     }
1689
1690     {
1691       MCInst TmpInst;
1692       TmpInst.setOpcode(ARM::MOVTi16);
1693       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1694       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // srcreg
1695       TmpInst.addOperand(V2);   // upper16(imm)
1696
1697       // Predicate.
1698       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1699       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1700
1701       OutStreamer.EmitInstruction(TmpInst);
1702     }
1703
1704     return;
1705   }
1706   case ARM::t2TBB:
1707   case ARM::t2TBH:
1708   case ARM::t2BR_JT: {
1709     // Lower and emit the instruction itself, then the jump table following it.
1710     MCInst TmpInst;
1711     MCInstLowering.Lower(MI, TmpInst);
1712     OutStreamer.EmitInstruction(TmpInst);
1713     EmitJump2Table(MI);
1714     return;
1715   }
1716   case ARM::tBR_JTr:
1717   case ARM::BR_JTr:
1718   case ARM::BR_JTm:
1719   case ARM::BR_JTadd: {
1720     // Lower and emit the instruction itself, then the jump table following it.
1721     MCInst TmpInst;
1722     MCInstLowering.Lower(MI, TmpInst);
1723     OutStreamer.EmitInstruction(TmpInst);
1724     EmitJumpTable(MI);
1725     return;
1726   }
1727   case ARM::TRAP: {
1728     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1729     // FIXME: Remove this special case when they do.
1730     if (!Subtarget->isTargetDarwin()) {
1731       //.long 0xe7ffdefe ${:comment} trap
1732       uint32_t Val = 0xe7ffdefe;
1733       OutStreamer.AddComment("trap");
1734       OutStreamer.EmitIntValue(Val, 4);
1735       return;
1736     }
1737     break;
1738   }
1739   case ARM::tTRAP: {
1740     // Non-Darwin binutils don't yet support the "trap" mnemonic.
1741     // FIXME: Remove this special case when they do.
1742     if (!Subtarget->isTargetDarwin()) {
1743       //.short 57086 ${:comment} trap
1744       uint16_t Val = 0xdefe;
1745       OutStreamer.AddComment("trap");
1746       OutStreamer.EmitIntValue(Val, 2);
1747       return;
1748     }
1749     break;
1750   }
1751   }
1752
1753   MCInst TmpInst;
1754   MCInstLowering.Lower(MI, TmpInst);
1755   OutStreamer.EmitInstruction(TmpInst);
1756 }
1757
1758 //===----------------------------------------------------------------------===//
1759 // Target Registry Stuff
1760 //===----------------------------------------------------------------------===//
1761
1762 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1763                                              unsigned SyntaxVariant,
1764                                              const MCAsmInfo &MAI) {
1765   if (SyntaxVariant == 0)
1766     return new ARMInstPrinter(MAI);
1767   return 0;
1768 }
1769
1770 // Force static initialization.
1771 extern "C" void LLVMInitializeARMAsmPrinter() {
1772   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1773   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1774
1775   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1776   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1777 }
1778