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