Rename getAnalysisToUpdate to getAnalysisIfAvailable.
[oota-llvm.git] / lib / Target / ARM / AsmPrinter / ARMAsmPrinter.cpp
1 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
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 "ARMTargetMachine.h"
18 #include "ARMAddressingModes.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/ADT/StringSet.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include <cctype>
40 using namespace llvm;
41
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
44 namespace {
45   struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
46     ARMAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
47       : AsmPrinter(O, TM, T), DW(0), MMI(NULL), AFI(NULL), MCP(NULL),
48         InCPMode(false) {
49       Subtarget = &TM.getSubtarget<ARMSubtarget>();
50     }
51
52     DwarfWriter *DW;
53     MachineModuleInfo *MMI;
54
55     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
56     /// make the right decision when printing asm code for different targets.
57     const ARMSubtarget *Subtarget;
58
59     /// AFI - Keep a pointer to ARMFunctionInfo for the current
60     /// MachineFunction.
61     ARMFunctionInfo *AFI;
62
63     /// MCP - Keep a pointer to constantpool entries of the current
64     /// MachineFunction.
65     const MachineConstantPool *MCP;
66
67     /// We name each basic block in a Function with a unique number, so
68     /// that we can consistently refer to them later. This is cleared
69     /// at the beginning of each call to runOnMachineFunction().
70     ///
71     typedef std::map<const Value *, unsigned> ValueMapTy;
72     ValueMapTy NumberForBB;
73
74     /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
75     /// non-lazy-pointers for indirect access.
76     StringSet<> GVNonLazyPtrs;
77
78     /// HiddenGVNonLazyPtrs - Keeps the set of GlobalValues with hidden
79     /// visibility that require non-lazy-pointers for indirect access.
80     StringSet<> HiddenGVNonLazyPtrs;
81
82     /// FnStubs - Keeps the set of external function GlobalAddresses that the
83     /// asm printer should generate stubs for.
84     StringSet<> FnStubs;
85
86     /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
87     bool InCPMode;
88     
89     virtual const char *getPassName() const {
90       return "ARM Assembly Printer";
91     }
92
93     void printOperand(const MachineInstr *MI, int opNum,
94                       const char *Modifier = 0);
95     void printSOImmOperand(const MachineInstr *MI, int opNum);
96     void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
97     void printSORegOperand(const MachineInstr *MI, int opNum);
98     void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
99     void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
100     void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
101     void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
102     void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
103                                const char *Modifier = 0);
104     void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
105                                const char *Modifier = 0);
106     void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
107                                 const char *Modifier = 0);
108     void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
109     void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
110                                       unsigned Scale);
111     void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
112     void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
113     void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
114     void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
115     void printPredicateOperand(const MachineInstr *MI, int opNum);
116     void printSBitModifierOperand(const MachineInstr *MI, int opNum);
117     void printPCLabel(const MachineInstr *MI, int opNum);
118     void printRegisterList(const MachineInstr *MI, int opNum);
119     void printCPInstOperand(const MachineInstr *MI, int opNum,
120                             const char *Modifier);
121     void printJTBlockOperand(const MachineInstr *MI, int opNum);
122
123     virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
124                                  unsigned AsmVariant, const char *ExtraCode);
125
126     void printModuleLevelGV(const GlobalVariable* GVar);
127     bool printInstruction(const MachineInstr *MI);  // autogenerated.
128     void printMachineInstruction(const MachineInstr *MI);
129     bool runOnMachineFunction(MachineFunction &F);
130     bool doInitialization(Module &M);
131     bool doFinalization(Module &M);
132
133     /// EmitMachineConstantPoolValue - Print a machine constantpool value to
134     /// the .s file.
135     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
136       printDataDirective(MCPV->getType());
137
138       ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
139       GlobalValue *GV = ACPV->getGV();
140       std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
141       if (!GV)
142         Name += ACPV->getSymbol();
143       if (ACPV->isNonLazyPointer()) {
144         if (GV->hasHiddenVisibility())
145           HiddenGVNonLazyPtrs.insert(Name);
146         else
147           GVNonLazyPtrs.insert(Name);
148         printSuffixedName(Name, "$non_lazy_ptr");
149       } else if (ACPV->isStub()) {
150         FnStubs.insert(Name);
151         printSuffixedName(Name, "$stub");
152       } else
153         O << Name;
154       if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
155       if (ACPV->getPCAdjustment() != 0) {
156         O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
157           << utostr(ACPV->getLabelId())
158           << "+" << (unsigned)ACPV->getPCAdjustment();
159          if (ACPV->mustAddCurrentAddress())
160            O << "-.";
161          O << ")";
162       }
163       O << "\n";
164
165       // If the constant pool value is a extern weak symbol, remember to emit
166       // the weak reference.
167       if (GV && GV->hasExternalWeakLinkage())
168         ExtWeakSymbols.insert(GV);
169     }
170     
171     void getAnalysisUsage(AnalysisUsage &AU) const {
172       AsmPrinter::getAnalysisUsage(AU);
173       AU.setPreservesAll();
174       AU.addRequired<MachineModuleInfo>();
175       AU.addRequired<DwarfWriter>();
176     }
177   };
178 } // end of anonymous namespace
179
180 #include "ARMGenAsmWriter.inc"
181
182 /// runOnMachineFunction - This uses the printInstruction()
183 /// method to print assembly for each instruction.
184 ///
185 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
186   AFI = MF.getInfo<ARMFunctionInfo>();
187   MCP = MF.getConstantPool();
188
189   SetupMachineFunction(MF);
190   O << "\n";
191
192   // NOTE: we don't print out constant pools here, they are handled as
193   // instructions.
194
195   O << "\n";
196   // Print out labels for the function.
197   const Function *F = MF.getFunction();
198   switch (F->getLinkage()) {
199   default: assert(0 && "Unknown linkage type!");
200   case Function::PrivateLinkage:
201   case Function::InternalLinkage:
202     SwitchToTextSection("\t.text", F);
203     break;
204   case Function::ExternalLinkage:
205     SwitchToTextSection("\t.text", F);
206     O << "\t.globl\t" << CurrentFnName << "\n";
207     break;
208   case Function::WeakLinkage:
209   case Function::LinkOnceLinkage:
210     if (Subtarget->isTargetDarwin()) {
211       SwitchToTextSection(
212                 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
213       O << "\t.globl\t" << CurrentFnName << "\n";
214       O << "\t.weak_definition\t" << CurrentFnName << "\n";
215     } else {
216       O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
217     }
218     break;
219   }
220
221   printVisibility(CurrentFnName, F->getVisibility());
222
223   if (AFI->isThumbFunction()) {
224     EmitAlignment(1, F, AFI->getAlign());
225     O << "\t.code\t16\n";
226     O << "\t.thumb_func";
227     if (Subtarget->isTargetDarwin())
228       O << "\t" << CurrentFnName;
229     O << "\n";
230     InCPMode = false;
231   } else
232     EmitAlignment(2, F);
233
234   O << CurrentFnName << ":\n";
235   // Emit pre-function debug information.
236   DW->BeginFunction(&MF);
237
238   if (Subtarget->isTargetDarwin()) {
239     // If the function is empty, then we need to emit *something*. Otherwise,
240     // the function's label might be associated with something that it wasn't
241     // meant to be associated with. We emit a noop in this situation.
242     MachineFunction::iterator I = MF.begin();
243
244     if (++I == MF.end() && MF.front().empty())
245       O << "\tnop\n";
246   }
247
248   // Print out code for the function.
249   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250        I != E; ++I) {
251     // Print a label for the basic block.
252     if (I != MF.begin()) {
253       printBasicBlockLabel(I, true, true);
254       O << '\n';
255     }
256     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
257          II != E; ++II) {
258       // Print the assembly for the instruction.
259       printMachineInstruction(II);
260     }
261   }
262
263   if (TAI->hasDotTypeDotSizeDirective())
264     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
265
266   // Emit post-function debug information.
267   DW->EndFunction(&MF);
268
269   O.flush();
270
271   return false;
272 }
273
274 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
275                                  const char *Modifier) {
276   const MachineOperand &MO = MI->getOperand(opNum);
277   switch (MO.getType()) {
278   case MachineOperand::MO_Register:
279     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
280       O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
281     else
282       assert(0 && "not implemented");
283     break;
284   case MachineOperand::MO_Immediate: {
285     if (!Modifier || strcmp(Modifier, "no_hash") != 0)
286       O << "#";
287
288     O << (int)MO.getImm();
289     break;
290   }
291   case MachineOperand::MO_MachineBasicBlock:
292     printBasicBlockLabel(MO.getMBB());
293     return;
294   case MachineOperand::MO_GlobalAddress: {
295     bool isCallOp = Modifier && !strcmp(Modifier, "call");
296     GlobalValue *GV = MO.getGlobal();
297     std::string Name = Mang->getValueName(GV);
298     bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
299                   GV->hasLinkOnceLinkage());
300     if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
301         TM.getRelocationModel() != Reloc::Static) {
302       printSuffixedName(Name, "$stub");
303       FnStubs.insert(Name);
304     } else
305       O << Name;
306
307     printOffset(MO.getOffset());
308
309     if (isCallOp && Subtarget->isTargetELF() &&
310         TM.getRelocationModel() == Reloc::PIC_)
311       O << "(PLT)";
312     if (GV->hasExternalWeakLinkage())
313       ExtWeakSymbols.insert(GV);
314     break;
315   }
316   case MachineOperand::MO_ExternalSymbol: {
317     bool isCallOp = Modifier && !strcmp(Modifier, "call");
318     std::string Name(TAI->getGlobalPrefix());
319     Name += MO.getSymbolName();
320     if (isCallOp && Subtarget->isTargetDarwin() &&
321         TM.getRelocationModel() != Reloc::Static) {
322       printSuffixedName(Name, "$stub");
323       FnStubs.insert(Name);
324     } else
325       O << Name;
326     if (isCallOp && Subtarget->isTargetELF() &&
327         TM.getRelocationModel() == Reloc::PIC_)
328       O << "(PLT)";
329     break;
330   }
331   case MachineOperand::MO_ConstantPoolIndex:
332     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
333       << '_' << MO.getIndex();
334     break;
335   case MachineOperand::MO_JumpTableIndex:
336     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
337       << '_' << MO.getIndex();
338     break;
339   default:
340     O << "<unknown operand type>"; abort (); break;
341   }
342 }
343
344 static void printSOImm(raw_ostream &O, int64_t V, const TargetAsmInfo *TAI) {
345   assert(V < (1 << 12) && "Not a valid so_imm value!");
346   unsigned Imm = ARM_AM::getSOImmValImm(V);
347   unsigned Rot = ARM_AM::getSOImmValRot(V);
348
349   // Print low-level immediate formation info, per
350   // A5.1.3: "Data-processing operands - Immediate".
351   if (Rot) {
352     O << "#" << Imm << ", " << Rot;
353     // Pretty printed version.
354     O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
355   } else {
356     O << "#" << Imm;
357   }
358 }
359
360 /// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
361 /// immediate in bits 0-7.
362 void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
363   const MachineOperand &MO = MI->getOperand(OpNum);
364   assert(MO.isImm() && "Not a valid so_imm value!");
365   printSOImm(O, MO.getImm(), TAI);
366 }
367
368 /// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
369 /// followed by an 'orr' to materialize.
370 void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
371   const MachineOperand &MO = MI->getOperand(OpNum);
372   assert(MO.isImm() && "Not a valid so_imm value!");
373   unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
374   unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
375   printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
376   O << "\n\torr";
377   printPredicateOperand(MI, 2);
378   O << " ";
379   printOperand(MI, 0); 
380   O << ", ";
381   printOperand(MI, 0); 
382   O << ", ";
383   printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
384 }
385
386 // so_reg is a 4-operand unit corresponding to register forms of the A5.1
387 // "Addressing Mode 1 - Data-processing operands" forms.  This includes:
388 //    REG 0   0    - e.g. R5
389 //    REG REG 0,SH_OPC     - e.g. R5, ROR R3
390 //    REG 0   IMM,SH_OPC  - e.g. R5, LSL #3
391 void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
392   const MachineOperand &MO1 = MI->getOperand(Op);
393   const MachineOperand &MO2 = MI->getOperand(Op+1);
394   const MachineOperand &MO3 = MI->getOperand(Op+2);
395
396   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
397   O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
398
399   // Print the shift opc.
400   O << ", "
401     << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
402     << " ";
403
404   if (MO2.getReg()) {
405     assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
406     O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
407     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
408   } else {
409     O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
410   }
411 }
412
413 void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
414   const MachineOperand &MO1 = MI->getOperand(Op);
415   const MachineOperand &MO2 = MI->getOperand(Op+1);
416   const MachineOperand &MO3 = MI->getOperand(Op+2);
417
418   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
419     printOperand(MI, Op);
420     return;
421   }
422
423   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
424
425   if (!MO2.getReg()) {
426     if (ARM_AM::getAM2Offset(MO3.getImm()))  // Don't print +0.
427       O << ", #"
428         << (char)ARM_AM::getAM2Op(MO3.getImm())
429         << ARM_AM::getAM2Offset(MO3.getImm());
430     O << "]";
431     return;
432   }
433
434   O << ", "
435     << (char)ARM_AM::getAM2Op(MO3.getImm())
436     << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
437   
438   if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
439     O << ", "
440       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
441       << " #" << ShImm;
442   O << "]";
443 }
444
445 void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
446   const MachineOperand &MO1 = MI->getOperand(Op);
447   const MachineOperand &MO2 = MI->getOperand(Op+1);
448
449   if (!MO1.getReg()) {
450     unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
451     assert(ImmOffs && "Malformed indexed load / store!");
452     O << "#"
453       << (char)ARM_AM::getAM2Op(MO2.getImm())
454       << ImmOffs;
455     return;
456   }
457
458   O << (char)ARM_AM::getAM2Op(MO2.getImm())
459     << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
460   
461   if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
462     O << ", "
463       << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
464       << " #" << ShImm;
465 }
466
467 void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
468   const MachineOperand &MO1 = MI->getOperand(Op);
469   const MachineOperand &MO2 = MI->getOperand(Op+1);
470   const MachineOperand &MO3 = MI->getOperand(Op+2);
471   
472   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
473   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
474
475   if (MO2.getReg()) {
476     O << ", "
477       << (char)ARM_AM::getAM3Op(MO3.getImm())
478       << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
479       << "]";
480     return;
481   }
482   
483   if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
484     O << ", #"
485       << (char)ARM_AM::getAM3Op(MO3.getImm())
486       << ImmOffs;
487   O << "]";
488 }
489
490 void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
491   const MachineOperand &MO1 = MI->getOperand(Op);
492   const MachineOperand &MO2 = MI->getOperand(Op+1);
493
494   if (MO1.getReg()) {
495     O << (char)ARM_AM::getAM3Op(MO2.getImm())
496       << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
497     return;
498   }
499
500   unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
501   assert(ImmOffs && "Malformed indexed load / store!");
502   O << "#"
503     << (char)ARM_AM::getAM3Op(MO2.getImm())
504     << ImmOffs;
505 }
506   
507 void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
508                                           const char *Modifier) {
509   const MachineOperand &MO1 = MI->getOperand(Op);
510   const MachineOperand &MO2 = MI->getOperand(Op+1);
511   ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
512   if (Modifier && strcmp(Modifier, "submode") == 0) {
513     if (MO1.getReg() == ARM::SP) {
514       bool isLDM = (MI->getOpcode() == ARM::LDM ||
515                     MI->getOpcode() == ARM::LDM_RET);
516       O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
517     } else
518       O << ARM_AM::getAMSubModeStr(Mode);
519   } else {
520     printOperand(MI, Op);
521     if (ARM_AM::getAM4WBFlag(MO2.getImm()))
522       O << "!";
523   }
524 }
525
526 void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
527                                           const char *Modifier) {
528   const MachineOperand &MO1 = MI->getOperand(Op);
529   const MachineOperand &MO2 = MI->getOperand(Op+1);
530
531   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
532     printOperand(MI, Op);
533     return;
534   }
535   
536   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
537
538   if (Modifier && strcmp(Modifier, "submode") == 0) {
539     ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
540     if (MO1.getReg() == ARM::SP) {
541       bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
542                      MI->getOpcode() == ARM::FLDMS);
543       O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
544     } else
545       O << ARM_AM::getAMSubModeStr(Mode);
546     return;
547   } else if (Modifier && strcmp(Modifier, "base") == 0) {
548     // Used for FSTM{D|S} and LSTM{D|S} operations.
549     O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
550     if (ARM_AM::getAM5WBFlag(MO2.getImm()))
551       O << "!";
552     return;
553   }
554   
555   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
556   
557   if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
558     O << ", #"
559       << (char)ARM_AM::getAM5Op(MO2.getImm())
560       << ImmOffs*4;
561   }
562   O << "]";
563 }
564
565 void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
566                                            const char *Modifier) {
567   if (Modifier && strcmp(Modifier, "label") == 0) {
568     printPCLabel(MI, Op+1);
569     return;
570   }
571
572   const MachineOperand &MO1 = MI->getOperand(Op);
573   assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
574   O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
575 }
576
577 void
578 ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
579   const MachineOperand &MO1 = MI->getOperand(Op);
580   const MachineOperand &MO2 = MI->getOperand(Op+1);
581   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
582   O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
583 }
584
585 void
586 ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
587                                             unsigned Scale) {
588   const MachineOperand &MO1 = MI->getOperand(Op);
589   const MachineOperand &MO2 = MI->getOperand(Op+1);
590   const MachineOperand &MO3 = MI->getOperand(Op+2);
591
592   if (!MO1.isReg()) {   // FIXME: This is for CP entries, but isn't right.
593     printOperand(MI, Op);
594     return;
595   }
596
597   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
598   if (MO3.getReg())
599     O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
600   else if (unsigned ImmOffs = MO2.getImm()) {
601     O << ", #" << ImmOffs;
602     if (Scale > 1)
603       O << " * " << Scale;
604   }
605   O << "]";
606 }
607
608 void
609 ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
610   printThumbAddrModeRI5Operand(MI, Op, 1);
611 }
612 void
613 ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
614   printThumbAddrModeRI5Operand(MI, Op, 2);
615 }
616 void
617 ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
618   printThumbAddrModeRI5Operand(MI, Op, 4);
619 }
620
621 void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
622   const MachineOperand &MO1 = MI->getOperand(Op);
623   const MachineOperand &MO2 = MI->getOperand(Op+1);
624   O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
625   if (unsigned ImmOffs = MO2.getImm())
626     O << ", #" << ImmOffs << " * 4";
627   O << "]";
628 }
629
630 void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
631   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
632   if (CC != ARMCC::AL)
633     O << ARMCondCodeToString(CC);
634 }
635
636 void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
637   unsigned Reg = MI->getOperand(opNum).getReg();
638   if (Reg) {
639     assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
640     O << 's';
641   }
642 }
643
644 void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
645   int Id = (int)MI->getOperand(opNum).getImm();
646   O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
647 }
648
649 void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
650   O << "{";
651   for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
652     printOperand(MI, i);
653     if (i != e-1) O << ", ";
654   }
655   O << "}";
656 }
657
658 void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
659                                        const char *Modifier) {
660   assert(Modifier && "This operand only works with a modifier!");
661   // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
662   // data itself.
663   if (!strcmp(Modifier, "label")) {
664     unsigned ID = MI->getOperand(OpNo).getImm();
665     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
666       << '_' << ID << ":\n";
667   } else {
668     assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
669     unsigned CPI = MI->getOperand(OpNo).getIndex();
670
671     const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
672     
673     if (MCPE.isMachineConstantPoolEntry()) {
674       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
675     } else {
676       EmitGlobalConstant(MCPE.Val.ConstVal);
677       // remember to emit the weak reference
678       if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
679         if (GV->hasExternalWeakLinkage())
680           ExtWeakSymbols.insert(GV);
681     }
682   }
683 }
684
685 void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
686   const MachineOperand &MO1 = MI->getOperand(OpNo);
687   const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
688   unsigned JTI = MO1.getIndex();
689   O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
690     << '_' << JTI << '_' << MO2.getImm() << ":\n";
691
692   const char *JTEntryDirective = TAI->getJumpTableDirective();
693   if (!JTEntryDirective)
694     JTEntryDirective = TAI->getData32bitsDirective();
695
696   const MachineFunction *MF = MI->getParent()->getParent();
697   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
698   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
699   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
700   bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
701   std::set<MachineBasicBlock*> JTSets;
702   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
703     MachineBasicBlock *MBB = JTBBs[i];
704     if (UseSet && JTSets.insert(MBB).second)
705       printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
706
707     O << JTEntryDirective << ' ';
708     if (UseSet)
709       O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
710         << '_' << JTI << '_' << MO2.getImm()
711         << "_set_" << MBB->getNumber();
712     else if (TM.getRelocationModel() == Reloc::PIC_) {
713       printBasicBlockLabel(MBB, false, false, false);
714       // If the arch uses custom Jump Table directives, don't calc relative to JT
715       if (!TAI->getJumpTableDirective()) 
716         O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
717           << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
718     } else
719       printBasicBlockLabel(MBB, false, false, false);
720     if (i != e-1)
721       O << '\n';
722   }
723 }
724
725
726 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
727                                     unsigned AsmVariant, const char *ExtraCode){
728   // Does this asm operand have a single letter operand modifier?
729   if (ExtraCode && ExtraCode[0]) {
730     if (ExtraCode[1] != 0) return true; // Unknown modifier.
731     
732     switch (ExtraCode[0]) {
733     default: return true;  // Unknown modifier.
734     case 'c': // Don't print "$" before a global var name or constant.
735     case 'P': // Print a VFP double precision register.
736       printOperand(MI, OpNo);
737       return false;
738     case 'Q':
739       if (TM.getTargetData()->isLittleEndian())
740         break;
741       // Fallthrough
742     case 'R':
743       if (TM.getTargetData()->isBigEndian())
744         break;
745       // Fallthrough
746     case 'H': // Write second word of DI / DF reference.  
747       // Verify that this operand has two consecutive registers.
748       if (!MI->getOperand(OpNo).isReg() ||
749           OpNo+1 == MI->getNumOperands() ||
750           !MI->getOperand(OpNo+1).isReg())
751         return true;
752       ++OpNo;   // Return the high-part.
753     }
754   }
755   
756   printOperand(MI, OpNo);
757   return false;
758 }
759
760 void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
761   ++EmittedInsts;
762
763   int Opc = MI->getOpcode();
764   switch (Opc) {
765   case ARM::CONSTPOOL_ENTRY:
766     if (!InCPMode && AFI->isThumbFunction()) {
767       EmitAlignment(2);
768       InCPMode = true;
769     }
770     break;
771   default: {
772     if (InCPMode && AFI->isThumbFunction())
773       InCPMode = false;
774   }}
775
776   // Call the autogenerated instruction printer routines.
777   printInstruction(MI);
778 }
779
780 bool ARMAsmPrinter::doInitialization(Module &M) {
781
782   bool Result = AsmPrinter::doInitialization(M);
783
784   // Emit initial debug information.
785   MMI = getAnalysisIfAvailable<MachineModuleInfo>();
786   assert(MMI);
787   DW = getAnalysisIfAvailable<DwarfWriter>();
788   assert(DW && "Dwarf Writer is not available");
789   DW->BeginModule(&M, MMI, O, this, TAI);
790
791   // Darwin wants symbols to be quoted if they have complex names.
792   if (Subtarget->isTargetDarwin())
793     Mang->setUseQuotes(true);
794
795   return Result;
796 }
797
798 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
799 /// Don't print things like \n or \0.
800 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
801   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
802        Name != E; ++Name)
803     if (isprint(*Name))
804       OS << *Name;
805 }
806
807 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
808   const TargetData *TD = TM.getTargetData();
809
810   if (!GVar->hasInitializer())   // External global require no code
811     return;
812
813   // Check to see if this is a special global used by LLVM, if so, emit it.
814
815   if (EmitSpecialLLVMGlobal(GVar)) {
816     if (Subtarget->isTargetDarwin() &&
817         TM.getRelocationModel() == Reloc::Static) {
818       if (GVar->getName() == "llvm.global_ctors")
819         O << ".reference .constructors_used\n";
820       else if (GVar->getName() == "llvm.global_dtors")
821         O << ".reference .destructors_used\n";
822     }
823     return;
824   }
825
826   std::string name = Mang->getValueName(GVar);
827   Constant *C = GVar->getInitializer();
828   const Type *Type = C->getType();
829   unsigned Size = TD->getTypePaddedSize(Type);
830   unsigned Align = TD->getPreferredAlignmentLog(GVar);
831   bool isDarwin = Subtarget->isTargetDarwin();
832
833   printVisibility(name, GVar->getVisibility());
834
835   if (Subtarget->isTargetELF())
836     O << "\t.type " << name << ",%object\n";
837
838   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
839     // FIXME: This seems to be pretty darwin-specific
840
841     if (GVar->hasExternalLinkage()) {
842       SwitchToSection(TAI->SectionForGlobal(GVar));
843       if (const char *Directive = TAI->getZeroFillDirective()) {
844         O << "\t.globl\t" << name << "\n";
845         O << Directive << "__DATA, __common, " << name << ", "
846           << Size << ", " << Align << "\n";
847         return;
848       }
849     }
850
851     if (GVar->hasLocalLinkage() || GVar->mayBeOverridden()) {
852       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
853
854       if (isDarwin) {
855         if (GVar->hasLocalLinkage()) {
856           O << TAI->getLCOMMDirective()  << name << "," << Size
857             << ',' << Align;
858         } else if (GVar->hasCommonLinkage()) {
859           O << TAI->getCOMMDirective()  << name << "," << Size
860             << ',' << Align;
861         } else {
862           SwitchToSection(TAI->SectionForGlobal(GVar));
863           O << "\t.globl " << name << '\n'
864             << TAI->getWeakDefDirective() << name << '\n';
865           EmitAlignment(Align, GVar);
866           O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
867           PrintUnmangledNameSafely(GVar, O);
868           O << '\n';
869           EmitGlobalConstant(C);
870           return;
871         }
872       } else if (TAI->getLCOMMDirective() != NULL) {
873         if (GVar->hasLocalLinkage()) {
874           O << TAI->getLCOMMDirective() << name << "," << Size;
875         } else {
876           O << TAI->getCOMMDirective()  << name << "," << Size;
877           if (TAI->getCOMMDirectiveTakesAlignment())
878             O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
879         }
880       } else {
881         SwitchToSection(TAI->SectionForGlobal(GVar));
882         if (GVar->hasLocalLinkage())
883           O << "\t.local\t" << name << "\n";
884         O << TAI->getCOMMDirective()  << name << "," << Size;
885         if (TAI->getCOMMDirectiveTakesAlignment())
886           O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
887       }
888       O << "\t\t" << TAI->getCommentString() << " ";
889       PrintUnmangledNameSafely(GVar, O);
890       O << "\n";
891       return;
892     }
893   }
894
895   SwitchToSection(TAI->SectionForGlobal(GVar));
896   switch (GVar->getLinkage()) {
897    case GlobalValue::CommonLinkage:
898    case GlobalValue::LinkOnceLinkage:
899    case GlobalValue::WeakLinkage:
900     if (isDarwin) {
901       O << "\t.globl " << name << "\n"
902         << "\t.weak_definition " << name << "\n";
903     } else {
904       O << "\t.weak " << name << "\n";
905     }
906     break;
907    case GlobalValue::AppendingLinkage:
908     // FIXME: appending linkage variables should go into a section of
909     // their name or something.  For now, just emit them as external.
910    case GlobalValue::ExternalLinkage:
911     O << "\t.globl " << name << "\n";
912     // FALL THROUGH
913    case GlobalValue::PrivateLinkage:
914    case GlobalValue::InternalLinkage:
915     break;
916    default:
917     assert(0 && "Unknown linkage type!");
918     break;
919   }
920
921   EmitAlignment(Align, GVar);
922   O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
923   PrintUnmangledNameSafely(GVar, O);
924   O << "\n";
925   if (TAI->hasDotTypeDotSizeDirective())
926     O << "\t.size " << name << ", " << Size << "\n";
927
928   // If the initializer is a extern weak symbol, remember to emit the weak
929   // reference!
930   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
931     if (GV->hasExternalWeakLinkage())
932       ExtWeakSymbols.insert(GV);
933
934   EmitGlobalConstant(C);
935   O << '\n';
936 }
937
938
939 bool ARMAsmPrinter::doFinalization(Module &M) {
940   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
941        I != E; ++I)
942     printModuleLevelGV(I);
943
944   if (Subtarget->isTargetDarwin()) {
945     SwitchToDataSection("");
946
947     // Output stubs for dynamically-linked functions
948     for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
949          i != e; ++i) {
950       if (TM.getRelocationModel() == Reloc::PIC_)
951         SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
952                             "none,16", 0);
953       else
954         SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
955                             "none,12", 0);
956
957       EmitAlignment(2);
958       O << "\t.code\t32\n";
959
960       const char *p = i->getKeyData();
961       printSuffixedName(p, "$stub");
962       O << ":\n";
963       O << "\t.indirect_symbol " << p << "\n";
964       O << "\tldr ip, ";
965       printSuffixedName(p, "$slp");
966       O << "\n";
967       if (TM.getRelocationModel() == Reloc::PIC_) {
968         printSuffixedName(p, "$scv");
969         O << ":\n";
970         O << "\tadd ip, pc, ip\n";
971       }
972       O << "\tldr pc, [ip, #0]\n";
973       printSuffixedName(p, "$slp");
974       O << ":\n";
975       O << "\t.long\t";
976       printSuffixedName(p, "$lazy_ptr");
977       if (TM.getRelocationModel() == Reloc::PIC_) {
978         O << "-(";
979         printSuffixedName(p, "$scv");
980         O << "+8)\n";
981       } else
982         O << "\n";
983       SwitchToDataSection(".lazy_symbol_pointer", 0);
984       printSuffixedName(p, "$lazy_ptr");
985       O << ":\n";
986       O << "\t.indirect_symbol " << p << "\n";
987       O << "\t.long\tdyld_stub_binding_helper\n";
988     }
989     O << "\n";
990
991     // Output non-lazy-pointers for external and common global variables.
992     if (!GVNonLazyPtrs.empty()) {
993       SwitchToDataSection("\t.non_lazy_symbol_pointer", 0);
994       for (StringSet<>::iterator i =  GVNonLazyPtrs.begin(),
995              e = GVNonLazyPtrs.end(); i != e; ++i) {
996         const char *p = i->getKeyData();
997         printSuffixedName(p, "$non_lazy_ptr");
998         O << ":\n";
999         O << "\t.indirect_symbol " << p << "\n";
1000         O << "\t.long\t0\n";
1001       }
1002     }
1003
1004     if (!HiddenGVNonLazyPtrs.empty()) {
1005       SwitchToSection(TAI->getDataSection());
1006       for (StringSet<>::iterator i = HiddenGVNonLazyPtrs.begin(),
1007              e = HiddenGVNonLazyPtrs.end(); i != e; ++i) {
1008         const char *p = i->getKeyData();
1009         EmitAlignment(2);
1010         printSuffixedName(p, "$non_lazy_ptr");
1011         O << ":\n";
1012         O << "\t.long " << p << "\n";
1013       }
1014     }
1015
1016
1017     // Emit initial debug information.
1018     DW->EndModule();
1019
1020     // Funny Darwin hack: This flag tells the linker that no global symbols
1021     // contain code that falls through to other global symbols (e.g. the obvious
1022     // implementation of multiple entry points).  If this doesn't occur, the
1023     // linker can safely perform dead code stripping.  Since LLVM never
1024     // generates code that does this, it is always safe to set.
1025     O << "\t.subsections_via_symbols\n";
1026   } else {
1027     // Emit final debug information for ELF.
1028     DW->EndModule();
1029   }
1030
1031   return AsmPrinter::doFinalization(M);
1032 }
1033
1034 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1035 /// assembly code for a MachineFunction to the given output stream,
1036 /// using the given target machine description.  This should work
1037 /// regardless of whether the function is in SSA form.
1038 ///
1039 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1040                                              ARMTargetMachine &tm) {
1041   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1042 }
1043
1044 namespace {
1045   static struct Register {
1046     Register() {
1047       ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1048     }
1049   } Registrator;
1050 }