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