Implement target specific getDebugValueLocation().
[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 printInstructionThroughMCStreamer(const MachineInstr *MI);
95     
96
97     void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
98                       const char *Modifier = 0);
99     void printSOImmOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
100     void printSOImm2PartOperand(const MachineInstr *MI, int OpNum,
101                                 raw_ostream &O);
102     void printSORegOperand(const MachineInstr *MI, int OpNum,
103                            raw_ostream &O);
104     void printAddrMode2Operand(const MachineInstr *MI, int OpNum,
105                                raw_ostream &O);
106     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum,
107                                      raw_ostream &O);
108     void printAddrMode3Operand(const MachineInstr *MI, int OpNum,
109                                raw_ostream &O);
110     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum,
111                                      raw_ostream &O);
112     void printAddrMode4Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
113                                const char *Modifier = 0);
114     void printAddrMode5Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
115                                const char *Modifier = 0);
116     void printAddrMode6Operand(const MachineInstr *MI, int OpNum,
117                                raw_ostream &O);
118     void printAddrMode6OffsetOperand(const MachineInstr *MI, int OpNum,
119                                      raw_ostream &O);
120     void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
121                                 raw_ostream &O,
122                                 const char *Modifier = 0);
123     void printBitfieldInvMaskImmOperand (const MachineInstr *MI, int OpNum,
124                                          raw_ostream &O);
125
126     void printThumbS4ImmOperand(const MachineInstr *MI, int OpNum,
127                                 raw_ostream &O);
128     void printThumbITMask(const MachineInstr *MI, int OpNum, raw_ostream &O);
129     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum,
130                                      raw_ostream &O);
131     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
132                                       raw_ostream &O,
133                                       unsigned Scale);
134     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum,
135                                      raw_ostream &O);
136     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum,
137                                      raw_ostream &O);
138     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum,
139                                      raw_ostream &O);
140     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum,
141                                      raw_ostream &O);
142
143     void printT2SOOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
144     void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum,
145                                      raw_ostream &O);
146     void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum,
147                                     raw_ostream &O);
148     void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum,
149                                       raw_ostream &O);
150     void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum,
151                                           raw_ostream &O);
152     void printT2AddrModeImm8s4OffsetOperand(const MachineInstr *MI, int OpNum,
153                                             raw_ostream &O) {}
154     void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum,
155                                      raw_ostream &O);
156
157     void printCPSOptionOperand(const MachineInstr *MI, int OpNum,
158                                raw_ostream &O) {}
159     void printMSRMaskOperand(const MachineInstr *MI, int OpNum,
160                              raw_ostream &O) {}
161     void printNegZeroOperand(const MachineInstr *MI, int OpNum,
162                              raw_ostream &O) {}
163     void printPredicateOperand(const MachineInstr *MI, int OpNum,
164                                raw_ostream &O);
165     void printMandatoryPredicateOperand(const MachineInstr *MI, int OpNum,
166                                         raw_ostream &O);
167     void printSBitModifierOperand(const MachineInstr *MI, int OpNum,
168                                   raw_ostream &O);
169     void printPCLabel(const MachineInstr *MI, int OpNum,
170                       raw_ostream &O);
171     void printRegisterList(const MachineInstr *MI, int OpNum,
172                            raw_ostream &O);
173     void printCPInstOperand(const MachineInstr *MI, int OpNum,
174                             raw_ostream &O,
175                             const char *Modifier);
176     void printJTBlockOperand(const MachineInstr *MI, int OpNum,
177                              raw_ostream &O);
178     void printJT2BlockOperand(const MachineInstr *MI, int OpNum,
179                               raw_ostream &O);
180     void printTBAddrMode(const MachineInstr *MI, int OpNum,
181                          raw_ostream &O);
182     void printNoHashImmediate(const MachineInstr *MI, int OpNum,
183                               raw_ostream &O);
184     void printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
185                                raw_ostream &O);
186     void printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
187                                raw_ostream &O);
188     void printNEONModImmOperand(const MachineInstr *MI, int OpNum,
189                                 raw_ostream &O);
190
191     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
192                                  unsigned AsmVariant, const char *ExtraCode,
193                                  raw_ostream &O);
194     virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
195                                        unsigned AsmVariant,
196                                        const char *ExtraCode, raw_ostream &O);
197
198     void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen
199     static const char *getRegisterName(unsigned RegNo);
200
201     virtual void EmitInstruction(const MachineInstr *MI);
202     bool runOnMachineFunction(MachineFunction &F);
203     
204     virtual void EmitConstantPool() {} // we emit constant pools customly!
205     virtual void EmitFunctionEntryLabel();
206     void EmitStartOfAsmFile(Module &M);
207     void EmitEndOfAsmFile(Module &M);
208
209     MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
210       MachineLocation Location;
211       assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
212       // Frame address.  Currently handles register +- offset only.
213       if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
214         Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
215       else {
216         DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
217       }
218       return Location;
219     }
220
221     virtual unsigned getISAEncoding() {
222       // ARM/Darwin adds ISA to the DWARF info for each function.
223       if (!Subtarget->isTargetDarwin())
224         return 0;
225       return Subtarget->isThumb() ?
226         llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
227     }
228
229     MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
230                                           const MachineBasicBlock *MBB) const;
231     MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
232
233     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
234     /// the .s file.
235     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
236       SmallString<128> Str;
237       raw_svector_ostream OS(Str);
238       EmitMachineConstantPoolValue(MCPV, OS);
239       OutStreamer.EmitRawText(OS.str());
240     }
241     
242     void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
243                                       raw_ostream &O) {
244       switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
245       case 1: O << MAI->getData8bitsDirective(0); break;
246       case 2: O << MAI->getData16bitsDirective(0); break;
247       case 4: O << MAI->getData32bitsDirective(0); break;
248       default: assert(0 && "Unknown CPV size");
249       }
250
251       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
252
253       if (ACPV->isLSDA()) {
254         O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
255       } else if (ACPV->isBlockAddress()) {
256         O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
257       } else if (ACPV->isGlobalValue()) {
258         const GlobalValue *GV = ACPV->getGV();
259         bool isIndirect = Subtarget->isTargetDarwin() &&
260           Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
261         if (!isIndirect)
262           O << *Mang->getSymbol(GV);
263         else {
264           // FIXME: Remove this when Darwin transition to @GOT like syntax.
265           MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
266           O << *Sym;
267           
268           MachineModuleInfoMachO &MMIMachO =
269             MMI->getObjFileInfo<MachineModuleInfoMachO>();
270           MachineModuleInfoImpl::StubValueTy &StubSym =
271             GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
272                                         MMIMachO.getGVStubEntry(Sym);
273           if (StubSym.getPointer() == 0)
274             StubSym = MachineModuleInfoImpl::
275               StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
276         }
277       } else {
278         assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
279         O << *GetExternalSymbolSymbol(ACPV->getSymbol());
280       }
281
282       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
283       if (ACPV->getPCAdjustment() != 0) {
284         O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
285           << getFunctionNumber() << "_"  << ACPV->getLabelId()
286           << "+" << (unsigned)ACPV->getPCAdjustment();
287          if (ACPV->mustAddCurrentAddress())
288            O << "-.";
289          O << ')';
290       }
291     }
292   };
293 } // end of anonymous namespace
294
295 #include "ARMGenAsmWriter.inc"
296
297 void ARMAsmPrinter::EmitFunctionEntryLabel() {
298   if (AFI->isThumbFunction()) {
299     OutStreamer.EmitRawText(StringRef("\t.code\t16"));
300     if (!Subtarget->isTargetDarwin())
301       OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
302     else {
303       // This needs to emit to a temporary string to get properly quoted
304       // MCSymbols when they have spaces in them.
305       SmallString<128> Tmp;
306       raw_svector_ostream OS(Tmp);
307       OS << "\t.thumb_func\t" << *CurrentFnSym;
308       OutStreamer.EmitRawText(OS.str());
309     }
310   }
311   
312   OutStreamer.EmitLabel(CurrentFnSym);
313 }
314
315 /// runOnMachineFunction - This uses the printInstruction()
316 /// method to print assembly for each instruction.
317 ///
318 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
319   AFI = MF.getInfo<ARMFunctionInfo>();
320   MCP = MF.getConstantPool();
321
322   return AsmPrinter::runOnMachineFunction(MF);
323 }
324
325 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
326                                  raw_ostream &O, const char *Modifier) {
327   const MachineOperand &MO = MI->getOperand(OpNum);
328   unsigned TF = MO.getTargetFlags();
329
330   switch (MO.getType()) {
331   default:
332     assert(0 && "<unknown operand type>");
333   case MachineOperand::MO_Register: {
334     unsigned Reg = MO.getReg();
335     assert(TargetRegisterInfo::isPhysicalRegister(Reg));
336     if (Modifier && strcmp(Modifier, "dregpair") == 0) {
337       unsigned DRegLo = TM.getRegisterInfo()->getSubReg(Reg, ARM::dsub_0);
338       unsigned DRegHi = TM.getRegisterInfo()->getSubReg(Reg, ARM::dsub_1);
339       O << '{'
340         << getRegisterName(DRegLo) << ", " << getRegisterName(DRegHi)
341         << '}';
342     } else if (Modifier && strcmp(Modifier, "lane") == 0) {
343       unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(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   O << ", "
472     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
473     << " ";
474
475   if (MO2.getReg()) {
476     O << getRegisterName(MO2.getReg());
477     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
478   } else {
479     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
480   }
481 }
482
483 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op,
484                                           raw_ostream &O) {
485   const MachineOperand &MO1 = MI->getOperand(Op);
486   const MachineOperand &MO2 = MI->getOperand(Op+1);
487   const MachineOperand &MO3 = MI->getOperand(Op+2);
488
489   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
490     printOperand(MI, Op, O);
491     return;
492   }
493
494   O << "[" << getRegisterName(MO1.getReg());
495
496   if (!MO2.getReg()) {
497     if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
498       O << ", #"
499         << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
500         << ARM_AM::getAM2Offset(MO3.getImm());
501     O << "]";
502     return;
503   }
504
505   O << ", "
506     << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
507     << getRegisterName(MO2.getReg());
508
509   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
510     O << ", "
511       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
512       << " #" << ShImm;
513   O << "]";
514 }
515
516 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op,
517                                                 raw_ostream &O) {
518   const MachineOperand &MO1 = MI->getOperand(Op);
519   const MachineOperand &MO2 = MI->getOperand(Op+1);
520
521   if (!MO1.getReg()) {
522     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
523     O << "#"
524       << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
525       << ImmOffs;
526     return;
527   }
528
529   O << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
530     << getRegisterName(MO1.getReg());
531
532   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
533     O << ", "
534       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
535       << " #" << ShImm;
536 }
537
538 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op,
539                                           raw_ostream &O) {
540   const MachineOperand &MO1 = MI->getOperand(Op);
541   const MachineOperand &MO2 = MI->getOperand(Op+1);
542   const MachineOperand &MO3 = MI->getOperand(Op+2);
543
544   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
545   O << "[" << getRegisterName(MO1.getReg());
546
547   if (MO2.getReg()) {
548     O << ", "
549       << (char)ARM_AM::getAM3Op(MO3.getImm())
550       << getRegisterName(MO2.getReg())
551       << "]";
552     return;
553   }
554
555   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
556     O << ", #"
557       << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO3.getImm()))
558       << ImmOffs;
559   O << "]";
560 }
561
562 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op,
563                                                 raw_ostream &O){
564   const MachineOperand &MO1 = MI->getOperand(Op);
565   const MachineOperand &MO2 = MI->getOperand(Op+1);
566
567   if (MO1.getReg()) {
568     O << (char)ARM_AM::getAM3Op(MO2.getImm())
569       << getRegisterName(MO1.getReg());
570     return;
571   }
572
573   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
574   O << "#"
575     << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO2.getImm()))
576     << ImmOffs;
577 }
578
579 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
580                                           raw_ostream &O,
581                                           const char *Modifier) {
582   const MachineOperand &MO2 = MI->getOperand(Op+1);
583   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
584   if (Modifier && strcmp(Modifier, "submode") == 0) {
585     O << ARM_AM::getAMSubModeStr(Mode);
586   } else if (Modifier && strcmp(Modifier, "wide") == 0) {
587     ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
588     if (Mode == ARM_AM::ia)
589       O << ".w";
590   } else {
591     printOperand(MI, Op, O);
592   }
593 }
594
595 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
596                                           raw_ostream &O,
597                                           const char *Modifier) {
598   const MachineOperand &MO1 = MI->getOperand(Op);
599   const MachineOperand &MO2 = MI->getOperand(Op+1);
600
601   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
602     printOperand(MI, Op, O);
603     return;
604   }
605
606   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
607
608   if (Modifier && strcmp(Modifier, "submode") == 0) {
609     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
610     O << ARM_AM::getAMSubModeStr(Mode);
611     return;
612   } else if (Modifier && strcmp(Modifier, "base") == 0) {
613     // Used for FSTM{D|S} and LSTM{D|S} operations.
614     O << getRegisterName(MO1.getReg());
615     return;
616   }
617
618   O << "[" << getRegisterName(MO1.getReg());
619
620   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
621     O << ", #"
622       << ARM_AM::getAddrOpcStr(ARM_AM::getAM5Op(MO2.getImm()))
623       << ImmOffs*4;
624   }
625   O << "]";
626 }
627
628 void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op,
629                                           raw_ostream &O) {
630   const MachineOperand &MO1 = MI->getOperand(Op);
631   const MachineOperand &MO2 = MI->getOperand(Op+1);
632
633   O << "[" << getRegisterName(MO1.getReg());
634   if (MO2.getImm()) {
635     // FIXME: Both darwin as and GNU as violate ARM docs here.
636     O << ", :" << (MO2.getImm() << 3);
637   }
638   O << "]";
639 }
640
641 void ARMAsmPrinter::printAddrMode6OffsetOperand(const MachineInstr *MI, int Op,
642                                                 raw_ostream &O){
643   const MachineOperand &MO = MI->getOperand(Op);
644   if (MO.getReg() == 0)
645     O << "!";
646   else
647     O << ", " << getRegisterName(MO.getReg());
648 }
649
650 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
651                                            raw_ostream &O,
652                                            const char *Modifier) {
653   if (Modifier && strcmp(Modifier, "label") == 0) {
654     printPCLabel(MI, Op+1, O);
655     return;
656   }
657
658   const MachineOperand &MO1 = MI->getOperand(Op);
659   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
660   O << "[pc, " << getRegisterName(MO1.getReg()) << "]";
661 }
662
663 void
664 ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op,
665                                               raw_ostream &O) {
666   const MachineOperand &MO = MI->getOperand(Op);
667   uint32_t v = ~MO.getImm();
668   int32_t lsb = CountTrailingZeros_32(v);
669   int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
670   assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
671   O << "#" << lsb << ", #" << width;
672 }
673
674 //===--------------------------------------------------------------------===//
675
676 void ARMAsmPrinter::printThumbS4ImmOperand(const MachineInstr *MI, int Op,
677                                            raw_ostream &O) {
678   O << "#" <<  MI->getOperand(Op).getImm() * 4;
679 }
680
681 void
682 ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op,
683                                 raw_ostream &O) {
684   // (3 - the number of trailing zeros) is the number of then / else.
685   unsigned Mask = MI->getOperand(Op).getImm();
686   unsigned CondBit0 = Mask >> 4 & 1;
687   unsigned NumTZ = CountTrailingZeros_32(Mask);
688   assert(NumTZ <= 3 && "Invalid IT mask!");
689   for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
690     bool T = ((Mask >> Pos) & 1) == CondBit0;
691     if (T)
692       O << 't';
693     else
694       O << 'e';
695   }
696 }
697
698 void
699 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op,
700                                            raw_ostream &O) {
701   const MachineOperand &MO1 = MI->getOperand(Op);
702   const MachineOperand &MO2 = MI->getOperand(Op+1);
703   O << "[" << getRegisterName(MO1.getReg());
704   O << ", " << getRegisterName(MO2.getReg()) << "]";
705 }
706
707 void
708 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
709                                             raw_ostream &O,
710                                             unsigned Scale) {
711   const MachineOperand &MO1 = MI->getOperand(Op);
712   const MachineOperand &MO2 = MI->getOperand(Op+1);
713   const MachineOperand &MO3 = MI->getOperand(Op+2);
714
715   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
716     printOperand(MI, Op, O);
717     return;
718   }
719
720   O << "[" << getRegisterName(MO1.getReg());
721   if (MO3.getReg())
722     O << ", " << getRegisterName(MO3.getReg());
723   else if (unsigned ImmOffs = MO2.getImm())
724     O << ", #" << ImmOffs * Scale;
725   O << "]";
726 }
727
728 void
729 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op,
730                                            raw_ostream &O) {
731   printThumbAddrModeRI5Operand(MI, Op, O, 1);
732 }
733 void
734 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op,
735                                            raw_ostream &O) {
736   printThumbAddrModeRI5Operand(MI, Op, O, 2);
737 }
738 void
739 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op,
740                                            raw_ostream &O) {
741   printThumbAddrModeRI5Operand(MI, Op, O, 4);
742 }
743
744 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op,
745                                                 raw_ostream &O) {
746   const MachineOperand &MO1 = MI->getOperand(Op);
747   const MachineOperand &MO2 = MI->getOperand(Op+1);
748   O << "[" << getRegisterName(MO1.getReg());
749   if (unsigned ImmOffs = MO2.getImm())
750     O << ", #" << ImmOffs*4;
751   O << "]";
752 }
753
754 //===--------------------------------------------------------------------===//
755
756 // Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
757 // register with shift forms.
758 // REG 0   0           - e.g. R5
759 // REG IMM, SH_OPC     - e.g. R5, LSL #3
760 void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum,
761                                      raw_ostream &O) {
762   const MachineOperand &MO1 = MI->getOperand(OpNum);
763   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
764
765   unsigned Reg = MO1.getReg();
766   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
767   O << getRegisterName(Reg);
768
769   // Print the shift opc.
770   O << ", "
771     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO2.getImm()))
772     << " ";
773
774   assert(MO2.isImm() && "Not a valid t2_so_reg value!");
775   O << "#" << ARM_AM::getSORegOffset(MO2.getImm());
776 }
777
778 void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
779                                                 int OpNum,
780                                                 raw_ostream &O) {
781   const MachineOperand &MO1 = MI->getOperand(OpNum);
782   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
783
784   O << "[" << getRegisterName(MO1.getReg());
785
786   unsigned OffImm = MO2.getImm();
787   if (OffImm)  // Don't print +0.
788     O << ", #" << OffImm;
789   O << "]";
790 }
791
792 void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
793                                                int OpNum,
794                                                raw_ostream &O) {
795   const MachineOperand &MO1 = MI->getOperand(OpNum);
796   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
797
798   O << "[" << getRegisterName(MO1.getReg());
799
800   int32_t OffImm = (int32_t)MO2.getImm();
801   // Don't print +0.
802   if (OffImm < 0)
803     O << ", #-" << -OffImm;
804   else if (OffImm > 0)
805     O << ", #" << OffImm;
806   O << "]";
807 }
808
809 void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
810                                                  int OpNum,
811                                                  raw_ostream &O) {
812   const MachineOperand &MO1 = MI->getOperand(OpNum);
813   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
814
815   O << "[" << getRegisterName(MO1.getReg());
816
817   int32_t OffImm = (int32_t)MO2.getImm() / 4;
818   // Don't print +0.
819   if (OffImm < 0)
820     O << ", #-" << -OffImm * 4;
821   else if (OffImm > 0)
822     O << ", #" << OffImm * 4;
823   O << "]";
824 }
825
826 void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
827                                                      int OpNum,
828                                                      raw_ostream &O) {
829   const MachineOperand &MO1 = MI->getOperand(OpNum);
830   int32_t OffImm = (int32_t)MO1.getImm();
831   // Don't print +0.
832   if (OffImm < 0)
833     O << "#-" << -OffImm;
834   else if (OffImm > 0)
835     O << "#" << OffImm;
836 }
837
838 void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
839                                                 int OpNum,
840                                                 raw_ostream &O) {
841   const MachineOperand &MO1 = MI->getOperand(OpNum);
842   const MachineOperand &MO2 = MI->getOperand(OpNum+1);
843   const MachineOperand &MO3 = MI->getOperand(OpNum+2);
844
845   O << "[" << getRegisterName(MO1.getReg());
846
847   assert(MO2.getReg() && "Invalid so_reg load / store address!");
848   O << ", " << getRegisterName(MO2.getReg());
849
850   unsigned ShAmt = MO3.getImm();
851   if (ShAmt) {
852     assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
853     O << ", lsl #" << ShAmt;
854   }
855   O << "]";
856 }
857
858
859 //===--------------------------------------------------------------------===//
860
861 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum,
862                                           raw_ostream &O) {
863   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
864   if (CC != ARMCC::AL)
865     O << ARMCondCodeToString(CC);
866 }
867
868 void ARMAsmPrinter::printMandatoryPredicateOperand(const MachineInstr *MI,
869                                                    int OpNum,
870                                                    raw_ostream &O) {
871   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
872   O << ARMCondCodeToString(CC);
873 }
874
875 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum,
876                                              raw_ostream &O){
877   unsigned Reg = MI->getOperand(OpNum).getReg();
878   if (Reg) {
879     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
880     O << 's';
881   }
882 }
883
884 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum,
885                                  raw_ostream &O) {
886   int Id = (int)MI->getOperand(OpNum).getImm();
887   O << MAI->getPrivateGlobalPrefix()
888     << "PC" << getFunctionNumber() << "_" << Id;
889 }
890
891 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum,
892                                       raw_ostream &O) {
893   O << "{";
894   for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
895     if (MI->getOperand(i).isImplicit())
896       continue;
897     if ((int)i != OpNum) O << ", ";
898     printOperand(MI, i, O);
899   }
900   O << "}";
901 }
902
903 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
904                                        raw_ostream &O, const char *Modifier) {
905   assert(Modifier && "This operand only works with a modifier!");
906   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
907   // data itself.
908   if (!strcmp(Modifier, "label")) {
909     unsigned ID = MI->getOperand(OpNum).getImm();
910     OutStreamer.EmitLabel(GetCPISymbol(ID));
911   } else {
912     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
913     unsigned CPI = MI->getOperand(OpNum).getIndex();
914
915     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
916
917     if (MCPE.isMachineConstantPoolEntry()) {
918       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
919     } else {
920       EmitGlobalConstant(MCPE.Val.ConstVal);
921     }
922   }
923 }
924
925 MCSymbol *ARMAsmPrinter::
926 GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
927                             const MachineBasicBlock *MBB) const {
928   SmallString<60> Name;
929   raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
930     << getFunctionNumber() << '_' << uid << '_' << uid2
931     << "_set_" << MBB->getNumber();
932   return OutContext.GetOrCreateSymbol(Name.str());
933 }
934
935 MCSymbol *ARMAsmPrinter::
936 GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
937   SmallString<60> Name;
938   raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
939     << getFunctionNumber() << '_' << uid << '_' << uid2;
940   return OutContext.GetOrCreateSymbol(Name.str());
941 }
942
943 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum,
944                                         raw_ostream &O) {
945   assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
946
947   const MachineOperand &MO1 = MI->getOperand(OpNum);
948   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
949   
950   unsigned JTI = MO1.getIndex();
951   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
952   // Can't use EmitLabel until instprinter happens, label comes out in the wrong
953   // order.
954   O << "\n" << *JTISymbol << ":\n";
955
956   const char *JTEntryDirective = MAI->getData32bitsDirective();
957
958   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
959   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
960   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
961   bool UseSet= MAI->hasSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
962   SmallPtrSet<MachineBasicBlock*, 8> JTSets;
963   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
964     MachineBasicBlock *MBB = JTBBs[i];
965     bool isNew = JTSets.insert(MBB);
966
967     if (UseSet && isNew) {
968       O << "\t.set\t"
969         << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB) << ','
970         << *MBB->getSymbol() << '-' << *JTISymbol << '\n';
971     }
972
973     O << JTEntryDirective << ' ';
974     if (UseSet)
975       O << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB);
976     else if (TM.getRelocationModel() == Reloc::PIC_)
977       O << *MBB->getSymbol() << '-' << *JTISymbol;
978     else
979       O << *MBB->getSymbol();
980
981     if (i != e-1)
982       O << '\n';
983   }
984 }
985
986 void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum,
987                                          raw_ostream &O) {
988   const MachineOperand &MO1 = MI->getOperand(OpNum);
989   const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
990   unsigned JTI = MO1.getIndex();
991   
992   MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
993   
994   // Can't use EmitLabel until instprinter happens, label comes out in the wrong
995   // order.
996   O << "\n" << *JTISymbol << ":\n";
997
998   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
999   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1000   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1001   bool ByteOffset = false, HalfWordOffset = false;
1002   if (MI->getOpcode() == ARM::t2TBB)
1003     ByteOffset = true;
1004   else if (MI->getOpcode() == ARM::t2TBH)
1005     HalfWordOffset = true;
1006
1007   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1008     MachineBasicBlock *MBB = JTBBs[i];
1009     if (ByteOffset)
1010       O << MAI->getData8bitsDirective();
1011     else if (HalfWordOffset)
1012       O << MAI->getData16bitsDirective();
1013     
1014     if (ByteOffset || HalfWordOffset)
1015       O << '(' << *MBB->getSymbol() << "-" << *JTISymbol << ")/2";
1016     else
1017       O << "\tb.w " << *MBB->getSymbol();
1018
1019     if (i != e-1)
1020       O << '\n';
1021   }
1022 }
1023
1024 void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum,
1025                                     raw_ostream &O) {
1026   O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
1027   if (MI->getOpcode() == ARM::t2TBH)
1028     O << ", lsl #1";
1029   O << ']';
1030 }
1031
1032 void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum,
1033                                          raw_ostream &O) {
1034   O << MI->getOperand(OpNum).getImm();
1035 }
1036
1037 void ARMAsmPrinter::printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
1038                                           raw_ostream &O) {
1039   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1040   O << '#' << FP->getValueAPF().convertToFloat();
1041   if (isVerbose()) {
1042     O << "\t\t" << MAI->getCommentString() << ' ';
1043     WriteAsOperand(O, FP, /*PrintType=*/false);
1044   }
1045 }
1046
1047 void ARMAsmPrinter::printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
1048                                           raw_ostream &O) {
1049   const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
1050   O << '#' << FP->getValueAPF().convertToDouble();
1051   if (isVerbose()) {
1052     O << "\t\t" << MAI->getCommentString() << ' ';
1053     WriteAsOperand(O, FP, /*PrintType=*/false);
1054   }
1055 }
1056
1057 void ARMAsmPrinter::printNEONModImmOperand(const MachineInstr *MI, int OpNum,
1058                                            raw_ostream &O) {
1059   unsigned EncodedImm = MI->getOperand(OpNum).getImm();
1060   unsigned EltBits;
1061   uint64_t Val = ARM_AM::decodeNEONModImm(EncodedImm, EltBits);
1062   O << "#0x" << utohexstr(Val);
1063 }
1064
1065 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
1066                                     unsigned AsmVariant, const char *ExtraCode,
1067                                     raw_ostream &O) {
1068   // Does this asm operand have a single letter operand modifier?
1069   if (ExtraCode && ExtraCode[0]) {
1070     if (ExtraCode[1] != 0) return true; // Unknown modifier.
1071
1072     switch (ExtraCode[0]) {
1073     default: return true;  // Unknown modifier.
1074     case 'a': // Print as a memory address.
1075       if (MI->getOperand(OpNum).isReg()) {
1076         O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
1077         return false;
1078       }
1079       // Fallthrough
1080     case 'c': // Don't print "#" before an immediate operand.
1081       if (!MI->getOperand(OpNum).isImm())
1082         return true;
1083       printNoHashImmediate(MI, OpNum, O);
1084       return false;
1085     case 'P': // Print a VFP double precision register.
1086     case 'q': // Print a NEON quad precision register.
1087       printOperand(MI, OpNum, O);
1088       return false;
1089     case 'Q':
1090     case 'R':
1091     case 'H':
1092       report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
1093       return true;
1094     }
1095   }
1096
1097   printOperand(MI, OpNum, O);
1098   return false;
1099 }
1100
1101 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
1102                                           unsigned OpNum, unsigned AsmVariant,
1103                                           const char *ExtraCode,
1104                                           raw_ostream &O) {
1105   if (ExtraCode && ExtraCode[0])
1106     return true; // Unknown modifier.
1107
1108   const MachineOperand &MO = MI->getOperand(OpNum);
1109   assert(MO.isReg() && "unexpected inline asm memory operand");
1110   O << "[" << getRegisterName(MO.getReg()) << "]";
1111   return false;
1112 }
1113
1114 void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1115   if (EnableMCInst) {
1116     printInstructionThroughMCStreamer(MI);
1117     return;
1118   }
1119   
1120   if (MI->getOpcode() == ARM::CONSTPOOL_ENTRY)
1121     EmitAlignment(2);
1122   
1123   SmallString<128> Str;
1124   raw_svector_ostream OS(Str);
1125   if (MI->getOpcode() == ARM::DBG_VALUE) {
1126     unsigned NOps = MI->getNumOperands();
1127     assert(NOps==4);
1128     OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1129     // cast away const; DIetc do not take const operands for some reason.
1130     DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1131     OS << V.getName();
1132     OS << " <- ";
1133     // Frame address.  Currently handles register +- offset only.
1134     assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1135     OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1136     OS << ']';
1137     OS << "+";
1138     printOperand(MI, NOps-2, OS);
1139     OutStreamer.EmitRawText(OS.str());
1140     return;
1141   }
1142
1143   printInstruction(MI, OS);
1144   OutStreamer.EmitRawText(OS.str());
1145   
1146   // Make sure the instruction that follows TBB is 2-byte aligned.
1147   // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1148   if (MI->getOpcode() == ARM::t2TBB)
1149     EmitAlignment(1);
1150 }
1151
1152 void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
1153   if (Subtarget->isTargetDarwin()) {
1154     Reloc::Model RelocM = TM.getRelocationModel();
1155     if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1156       // Declare all the text sections up front (before the DWARF sections
1157       // emitted by AsmPrinter::doInitialization) so the assembler will keep
1158       // them together at the beginning of the object file.  This helps
1159       // avoid out-of-range branches that are due a fundamental limitation of
1160       // the way symbol offsets are encoded with the current Darwin ARM
1161       // relocations.
1162       const TargetLoweringObjectFileMachO &TLOFMacho = 
1163         static_cast<const TargetLoweringObjectFileMachO &>(
1164           getObjFileLowering());
1165       OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1166       OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1167       OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1168       if (RelocM == Reloc::DynamicNoPIC) {
1169         const MCSection *sect =
1170           OutContext.getMachOSection("__TEXT", "__symbol_stub4",
1171                                      MCSectionMachO::S_SYMBOL_STUBS,
1172                                      12, SectionKind::getText());
1173         OutStreamer.SwitchSection(sect);
1174       } else {
1175         const MCSection *sect =
1176           OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
1177                                      MCSectionMachO::S_SYMBOL_STUBS,
1178                                      16, SectionKind::getText());
1179         OutStreamer.SwitchSection(sect);
1180       }
1181       const MCSection *StaticInitSect =
1182         OutContext.getMachOSection("__TEXT", "__StaticInit",
1183                                    MCSectionMachO::S_REGULAR |
1184                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1185                                    SectionKind::getText());
1186       OutStreamer.SwitchSection(StaticInitSect);
1187     }
1188   }
1189
1190   // Use unified assembler syntax.
1191   OutStreamer.EmitRawText(StringRef("\t.syntax unified"));
1192
1193   // Emit ARM Build Attributes
1194   if (Subtarget->isTargetELF()) {
1195     // CPU Type
1196     std::string CPUString = Subtarget->getCPUString();
1197     if (CPUString != "generic")
1198       OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
1199
1200     // FIXME: Emit FPU type
1201     if (Subtarget->hasVFP2())
1202       OutStreamer.EmitRawText("\t.eabi_attribute " +
1203                               Twine(ARMBuildAttrs::VFP_arch) + ", 2");
1204
1205     // Signal various FP modes.
1206     if (!UnsafeFPMath) {
1207       OutStreamer.EmitRawText("\t.eabi_attribute " +
1208                               Twine(ARMBuildAttrs::ABI_FP_denormal) + ", 1");
1209       OutStreamer.EmitRawText("\t.eabi_attribute " +
1210                               Twine(ARMBuildAttrs::ABI_FP_exceptions) + ", 1");
1211     }
1212     
1213     if (NoInfsFPMath && NoNaNsFPMath)
1214       OutStreamer.EmitRawText("\t.eabi_attribute " +
1215                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 1");
1216     else
1217       OutStreamer.EmitRawText("\t.eabi_attribute " +
1218                               Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 3");
1219
1220     // 8-bytes alignment stuff.
1221     OutStreamer.EmitRawText("\t.eabi_attribute " +
1222                             Twine(ARMBuildAttrs::ABI_align8_needed) + ", 1");
1223     OutStreamer.EmitRawText("\t.eabi_attribute " +
1224                             Twine(ARMBuildAttrs::ABI_align8_preserved) + ", 1");
1225
1226     // Hard float.  Use both S and D registers and conform to AAPCS-VFP.
1227     if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
1228       OutStreamer.EmitRawText("\t.eabi_attribute " +
1229                               Twine(ARMBuildAttrs::ABI_HardFP_use) + ", 3");
1230       OutStreamer.EmitRawText("\t.eabi_attribute " +
1231                               Twine(ARMBuildAttrs::ABI_VFP_args) + ", 1");
1232     }
1233     // FIXME: Should we signal R9 usage?
1234   }
1235 }
1236
1237
1238 void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
1239   if (Subtarget->isTargetDarwin()) {
1240     // All darwin targets use mach-o.
1241     const TargetLoweringObjectFileMachO &TLOFMacho =
1242       static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1243     MachineModuleInfoMachO &MMIMacho =
1244       MMI->getObjFileInfo<MachineModuleInfoMachO>();
1245
1246     // Output non-lazy-pointers for external and common global variables.
1247     MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1248
1249     if (!Stubs.empty()) {
1250       // Switch with ".non_lazy_symbol_pointer" directive.
1251       OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1252       EmitAlignment(2);
1253       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1254         // L_foo$stub:
1255         OutStreamer.EmitLabel(Stubs[i].first);
1256         //   .indirect_symbol _foo
1257         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1258         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
1259
1260         if (MCSym.getInt())
1261           // External to current translation unit.
1262           OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
1263         else
1264           // Internal to current translation unit.
1265           //
1266           // When we place the LSDA into the TEXT section, the type info pointers
1267           // need to be indirect and pc-rel. We accomplish this by using NLPs.
1268           // However, sometimes the types are local to the file. So we need to
1269           // fill in the value for the NLP in those cases.
1270           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1271                                                         OutContext),
1272                                 4/*size*/, 0/*addrspace*/);
1273       }
1274
1275       Stubs.clear();
1276       OutStreamer.AddBlankLine();
1277     }
1278
1279     Stubs = MMIMacho.GetHiddenGVStubList();
1280     if (!Stubs.empty()) {
1281       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1282       EmitAlignment(2);
1283       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1284         // L_foo$stub:
1285         OutStreamer.EmitLabel(Stubs[i].first);
1286         //   .long _foo
1287         OutStreamer.EmitValue(MCSymbolRefExpr::
1288                               Create(Stubs[i].second.getPointer(),
1289                                      OutContext),
1290                               4/*size*/, 0/*addrspace*/);
1291       }
1292
1293       Stubs.clear();
1294       OutStreamer.AddBlankLine();
1295     }
1296
1297     // Funny Darwin hack: This flag tells the linker that no global symbols
1298     // contain code that falls through to other global symbols (e.g. the obvious
1299     // implementation of multiple entry points).  If this doesn't occur, the
1300     // linker can safely perform dead code stripping.  Since LLVM never
1301     // generates code that does this, it is always safe to set.
1302     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1303   }
1304 }
1305
1306 //===----------------------------------------------------------------------===//
1307
1308 void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
1309   ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
1310   switch (MI->getOpcode()) {
1311   case ARM::t2MOVi32imm:
1312     assert(0 && "Should be lowered by thumb2it pass");
1313   default: break;
1314   case ARM::PICADD: { // FIXME: Remove asm string from td file.
1315     // This is a pseudo op for a label + instruction sequence, which looks like:
1316     // LPC0:
1317     //     add r0, pc, r0
1318     // This adds the address of LPC0 to r0.
1319     
1320     // Emit the label.
1321     // FIXME: MOVE TO SHARED PLACE.
1322     unsigned Id = (unsigned)MI->getOperand(2).getImm();
1323     const char *Prefix = MAI->getPrivateGlobalPrefix();
1324     MCSymbol *Label =OutContext.GetOrCreateSymbol(Twine(Prefix)
1325                          + "PC" + Twine(getFunctionNumber()) + "_" + Twine(Id));
1326     OutStreamer.EmitLabel(Label);
1327     
1328     
1329     // Form and emit tha dd.
1330     MCInst AddInst;
1331     AddInst.setOpcode(ARM::ADDrr);
1332     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1333     AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1334     AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1335     OutStreamer.EmitInstruction(AddInst);
1336     return;
1337   }
1338   case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1339     /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1340     /// in the function.  The first operand is the ID# for this instruction, the
1341     /// second is the index into the MachineConstantPool that this is, the third
1342     /// is the size in bytes of this constant pool entry.
1343     unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1344     unsigned CPIdx   = (unsigned)MI->getOperand(1).getIndex();
1345
1346     EmitAlignment(2);
1347     OutStreamer.EmitLabel(GetCPISymbol(LabelId));
1348
1349     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1350     if (MCPE.isMachineConstantPoolEntry())
1351       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1352     else
1353       EmitGlobalConstant(MCPE.Val.ConstVal);
1354     
1355     return;
1356   }
1357   case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1358     // This is a hack that lowers as a two instruction sequence.
1359     unsigned DstReg = MI->getOperand(0).getReg();
1360     unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1361
1362     unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1363     unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1364     
1365     {
1366       MCInst TmpInst;
1367       TmpInst.setOpcode(ARM::MOVi);
1368       TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1369       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1370       
1371       // Predicate.
1372       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1373       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1374
1375       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1376       OutStreamer.EmitInstruction(TmpInst);
1377     }
1378
1379     {
1380       MCInst TmpInst;
1381       TmpInst.setOpcode(ARM::ORRri);
1382       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // dstreg
1383       TmpInst.addOperand(MCOperand::CreateReg(DstReg));     // inreg
1384       TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1385       // Predicate.
1386       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1387       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1388       
1389       TmpInst.addOperand(MCOperand::CreateReg(0));          // cc_out
1390       OutStreamer.EmitInstruction(TmpInst);
1391     }
1392     return; 
1393   }
1394   case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1395     // This is a hack that lowers as a two instruction sequence.
1396     unsigned DstReg = MI->getOperand(0).getReg();
1397     const MachineOperand &MO = MI->getOperand(1);
1398     MCOperand V1, V2;
1399     if (MO.isImm()) {
1400       unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1401       V1 = MCOperand::CreateImm(ImmVal & 65535);
1402       V2 = MCOperand::CreateImm(ImmVal >> 16);
1403     } else if (MO.isGlobal()) {
1404       MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO);
1405       const MCSymbolRefExpr *SymRef1 =
1406         MCSymbolRefExpr::Create(Symbol,
1407                                 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
1408       const MCSymbolRefExpr *SymRef2 =
1409         MCSymbolRefExpr::Create(Symbol,
1410                                 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
1411       V1 = MCOperand::CreateExpr(SymRef1);
1412       V2 = MCOperand::CreateExpr(SymRef2);
1413     } else {
1414       MI->dump();
1415       llvm_unreachable("cannot handle this operand");
1416     }
1417
1418     {
1419       MCInst TmpInst;
1420       TmpInst.setOpcode(ARM::MOVi16);
1421       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1422       TmpInst.addOperand(V1); // lower16(imm)
1423       
1424       // Predicate.
1425       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1426       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1427       
1428       OutStreamer.EmitInstruction(TmpInst);
1429     }
1430     
1431     {
1432       MCInst TmpInst;
1433       TmpInst.setOpcode(ARM::MOVTi16);
1434       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // dstreg
1435       TmpInst.addOperand(MCOperand::CreateReg(DstReg));         // srcreg
1436       TmpInst.addOperand(V2);   // upper16(imm)
1437       
1438       // Predicate.
1439       TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1440       TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1441       
1442       OutStreamer.EmitInstruction(TmpInst);
1443     }
1444     
1445     return;
1446   }
1447   }
1448       
1449   MCInst TmpInst;
1450   MCInstLowering.Lower(MI, TmpInst);
1451   OutStreamer.EmitInstruction(TmpInst);
1452 }
1453
1454 //===----------------------------------------------------------------------===//
1455 // Target Registry Stuff
1456 //===----------------------------------------------------------------------===//
1457
1458 static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1459                                              unsigned SyntaxVariant,
1460                                              const MCAsmInfo &MAI) {
1461   if (SyntaxVariant == 0)
1462     return new ARMInstPrinter(MAI, false);
1463   return 0;
1464 }
1465
1466 // Force static initialization.
1467 extern "C" void LLVMInitializeARMAsmPrinter() {
1468   RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1469   RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1470
1471   TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1472   TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1473 }
1474