Clean up some ARM GV asm printing out; minor fixes to match what gcc does.
[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(O, this, T), 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     }
176   };
177 } // end of anonymous namespace
178
179 #include "ARMGenAsmWriter.inc"
180
181 /// runOnMachineFunction - This uses the printInstruction()
182 /// method to print assembly for each instruction.
183 ///
184 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
185   AFI = MF.getInfo<ARMFunctionInfo>();
186   MCP = MF.getConstantPool();
187
188   SetupMachineFunction(MF);
189   O << "\n";
190
191   // NOTE: we don't print out constant pools here, they are handled as
192   // instructions.
193
194   O << "\n";
195   // Print out labels for the function.
196   const Function *F = MF.getFunction();
197   switch (F->getLinkage()) {
198   default: assert(0 && "Unknown linkage type!");
199   case Function::InternalLinkage:
200     SwitchToTextSection("\t.text", F);
201     break;
202   case Function::ExternalLinkage:
203     SwitchToTextSection("\t.text", F);
204     O << "\t.globl\t" << CurrentFnName << "\n";
205     break;
206   case Function::WeakLinkage:
207   case Function::LinkOnceLinkage:
208     if (Subtarget->isTargetDarwin()) {
209       SwitchToTextSection(
210                 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
211       O << "\t.globl\t" << CurrentFnName << "\n";
212       O << "\t.weak_definition\t" << CurrentFnName << "\n";
213     } else {
214       O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
215     }
216     break;
217   }
218
219   printVisibility(CurrentFnName, F->getVisibility());
220
221   if (AFI->isThumbFunction()) {
222     EmitAlignment(1, F, AFI->getAlign());
223     O << "\t.code\t16\n";
224     O << "\t.thumb_func";
225     if (Subtarget->isTargetDarwin())
226       O << "\t" << CurrentFnName;
227     O << "\n";
228     InCPMode = false;
229   } else
230     EmitAlignment(2, F);
231
232   O << CurrentFnName << ":\n";
233   // Emit pre-function debug information.
234   // FIXME: Dwarf support.
235   //DW.BeginFunction(&MF);
236
237   if (Subtarget->isTargetDarwin()) {
238     // If the function is empty, then we need to emit *something*. Otherwise,
239     // the function's label might be associated with something that it wasn't
240     // meant to be associated with. We emit a noop in this situation.
241     MachineFunction::iterator I = MF.begin();
242
243     if (++I == MF.end() && MF.front().empty())
244       O << "\tnop\n";
245   }
246
247   // Print out code for the function.
248   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
249        I != E; ++I) {
250     // Print a label for the basic block.
251     if (I != MF.begin()) {
252       printBasicBlockLabel(I, true, true);
253       O << '\n';
254     }
255     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
256          II != E; ++II) {
257       // Print the assembly for the instruction.
258       printMachineInstruction(II);
259     }
260   }
261
262   if (TAI->hasDotTypeDotSizeDirective())
263     O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
264
265   // Emit post-function debug information.
266   // FIXME: Dwarf support.
267   //DW.EndFunction();
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   // Emit initial debug information.
782   // FIXME: Dwarf support.
783   //DW.BeginModule(&M);
784   
785   bool Result = AsmPrinter::doInitialization(M);
786
787   // AsmPrinter::doInitialization should have done this analysis.
788   MMI = getAnalysisToUpdate<MachineModuleInfo>();
789   assert(MMI);
790   // FIXME: Dwarf support.
791   //DW.SetModuleInfo(MMI);
792
793   // Darwin wants symbols to be quoted if they have complex names.
794   if (Subtarget->isTargetDarwin())
795     Mang->setUseQuotes(true);
796
797   return Result;
798 }
799
800 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
801 /// Don't print things like \n or \0.
802 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
803   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
804        Name != E; ++Name)
805     if (isprint(*Name))
806       OS << *Name;
807 }
808
809 void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
810   const TargetData *TD = TM.getTargetData();
811
812   if (!GVar->hasInitializer())   // External global require no code
813     return;
814
815   // Check to see if this is a special global used by LLVM, if so, emit it.
816
817   if (EmitSpecialLLVMGlobal(GVar)) {
818     if (Subtarget->isTargetDarwin() &&
819         TM.getRelocationModel() == Reloc::Static) {
820       if (GVar->getName() == "llvm.global_ctors")
821         O << ".reference .constructors_used\n";
822       else if (GVar->getName() == "llvm.global_dtors")
823         O << ".reference .destructors_used\n";
824     }
825     return;
826   }
827
828   std::string name = Mang->getValueName(GVar);
829   Constant *C = GVar->getInitializer();
830   const Type *Type = C->getType();
831   unsigned Size = TD->getABITypeSize(Type);
832   unsigned Align = TD->getPreferredAlignmentLog(GVar);
833   bool isDarwin = Subtarget->isTargetDarwin();
834
835   printVisibility(name, GVar->getVisibility());
836
837   if (Subtarget->isTargetELF())
838     O << "\t.type " << name << ",%object\n";
839
840   if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
841     // FIXME: This seems to be pretty darwin-specific
842
843     if (GVar->hasExternalLinkage()) {
844       SwitchToSection(TAI->SectionForGlobal(GVar));
845       if (const char *Directive = TAI->getZeroFillDirective()) {
846         O << "\t.globl\t" << name << "\n";
847         O << Directive << "__DATA, __common, " << name << ", "
848           << Size << ", " << Align << "\n";
849         return;
850       }
851     }
852
853     if (GVar->hasInternalLinkage() || GVar->mayBeOverridden()) {
854       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
855
856       if (isDarwin) {
857         if (GVar->hasInternalLinkage()) {
858           O << TAI->getLCOMMDirective()  << name << "," << Size
859             << ',' << Align;
860         } else if (GVar->hasCommonLinkage()) {
861           O << TAI->getCOMMDirective()  << name << "," << Size
862             << ',' << Align;
863         } else {
864           SwitchToSection(TAI->SectionForGlobal(GVar));
865           O << "\t.globl " << name << '\n'
866             << TAI->getWeakDefDirective() << name << '\n';
867           EmitAlignment(Align, GVar);
868           O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
869           PrintUnmangledNameSafely(GVar, O);
870           O << '\n';
871           EmitGlobalConstant(C);
872           return;
873         }
874       } else if (TAI->getLCOMMDirective() != NULL) {
875         if (GVar->hasInternalLinkage()) {
876           O << TAI->getLCOMMDirective() << name << "," << Size;
877         } else {
878           O << TAI->getCOMMDirective()  << name << "," << Size;
879           if (TAI->getCOMMDirectiveTakesAlignment())
880             O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
881         }
882       } else {
883         SwitchToSection(TAI->SectionForGlobal(GVar));
884         if (GVar->hasInternalLinkage())
885           O << "\t.local\t" << name << "\n";
886         O << TAI->getCOMMDirective()  << name << "," << Size;
887         if (TAI->getCOMMDirectiveTakesAlignment())
888           O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
889       }
890       O << "\t\t" << TAI->getCommentString() << " ";
891       PrintUnmangledNameSafely(GVar, O);
892       O << "\n";
893       return;
894     }
895   }
896
897   SwitchToSection(TAI->SectionForGlobal(GVar));
898   switch (GVar->getLinkage()) {
899    case GlobalValue::CommonLinkage:
900    case GlobalValue::LinkOnceLinkage:
901    case GlobalValue::WeakLinkage:
902     if (isDarwin) {
903       O << "\t.globl " << name << "\n"
904         << "\t.weak_definition " << name << "\n";
905     } else {
906       O << "\t.weak " << name << "\n";
907     }
908     break;
909    case GlobalValue::AppendingLinkage:
910     // FIXME: appending linkage variables should go into a section of
911     // their name or something.  For now, just emit them as external.
912    case GlobalValue::ExternalLinkage:
913     O << "\t.globl " << name << "\n";
914     // FALL THROUGH
915    case GlobalValue::InternalLinkage:
916     break;
917    default:
918     assert(0 && "Unknown linkage type!");
919     break;
920   }
921
922   EmitAlignment(Align, GVar);
923   O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
924   PrintUnmangledNameSafely(GVar, O);
925   O << "\n";
926   if (TAI->hasDotTypeDotSizeDirective())
927     O << "\t.size " << name << ", " << Size << "\n";
928
929   // If the initializer is a extern weak symbol, remember to emit the weak
930   // reference!
931   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
932     if (GV->hasExternalWeakLinkage())
933       ExtWeakSymbols.insert(GV);
934
935   EmitGlobalConstant(C);
936   O << '\n';
937 }
938
939
940 bool ARMAsmPrinter::doFinalization(Module &M) {
941   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
942        I != E; ++I)
943     printModuleLevelGV(I);
944
945   if (Subtarget->isTargetDarwin()) {
946     SwitchToDataSection("");
947
948     // Output stubs for dynamically-linked functions
949     for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
950          i != e; ++i) {
951       if (TM.getRelocationModel() == Reloc::PIC_)
952         SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
953                             "none,16", 0);
954       else
955         SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
956                             "none,12", 0);
957
958       EmitAlignment(2);
959       O << "\t.code\t32\n";
960
961       const char *p = i->getKeyData();
962       printSuffixedName(p, "$stub");
963       O << ":\n";
964       O << "\t.indirect_symbol " << p << "\n";
965       O << "\tldr ip, ";
966       printSuffixedName(p, "$slp");
967       O << "\n";
968       if (TM.getRelocationModel() == Reloc::PIC_) {
969         printSuffixedName(p, "$scv");
970         O << ":\n";
971         O << "\tadd ip, pc, ip\n";
972       }
973       O << "\tldr pc, [ip, #0]\n";
974       printSuffixedName(p, "$slp");
975       O << ":\n";
976       O << "\t.long\t";
977       printSuffixedName(p, "$lazy_ptr");
978       if (TM.getRelocationModel() == Reloc::PIC_) {
979         O << "-(";
980         printSuffixedName(p, "$scv");
981         O << "+8)\n";
982       } else
983         O << "\n";
984       SwitchToDataSection(".lazy_symbol_pointer", 0);
985       printSuffixedName(p, "$lazy_ptr");
986       O << ":\n";
987       O << "\t.indirect_symbol " << p << "\n";
988       O << "\t.long\tdyld_stub_binding_helper\n";
989     }
990     O << "\n";
991
992     // Output non-lazy-pointers for external and common global variables.
993     if (!GVNonLazyPtrs.empty()) {
994       SwitchToDataSection("\t.non_lazy_symbol_pointer", 0);
995       for (StringSet<>::iterator i =  GVNonLazyPtrs.begin(),
996              e = GVNonLazyPtrs.end(); i != e; ++i) {
997         const char *p = i->getKeyData();
998         printSuffixedName(p, "$non_lazy_ptr");
999         O << ":\n";
1000         O << "\t.indirect_symbol " << p << "\n";
1001         O << "\t.long\t0\n";
1002       }
1003     }
1004
1005     if (!HiddenGVNonLazyPtrs.empty()) {
1006       SwitchToSection(TAI->getDataSection());
1007       for (StringSet<>::iterator i = HiddenGVNonLazyPtrs.begin(),
1008              e = HiddenGVNonLazyPtrs.end(); i != e; ++i) {
1009         const char *p = i->getKeyData();
1010         EmitAlignment(2);
1011         printSuffixedName(p, "$non_lazy_ptr");
1012         O << ":\n";
1013         O << "\t.long " << p << "\n";
1014       }
1015     }
1016
1017
1018     // Emit initial debug information.
1019     // FIXME: Dwarf support.
1020     //DW.EndModule();
1021
1022     // Funny Darwin hack: This flag tells the linker that no global symbols
1023     // contain code that falls through to other global symbols (e.g. the obvious
1024     // implementation of multiple entry points).  If this doesn't occur, the
1025     // linker can safely perform dead code stripping.  Since LLVM never
1026     // generates code that does this, it is always safe to set.
1027     O << "\t.subsections_via_symbols\n";
1028   } else {
1029     // Emit final debug information for ELF.
1030     // FIXME: Dwarf support.
1031     //DW.EndModule();
1032   }
1033
1034   return AsmPrinter::doFinalization(M);
1035 }
1036
1037 /// createARMCodePrinterPass - Returns a pass that prints the ARM
1038 /// assembly code for a MachineFunction to the given output stream,
1039 /// using the given target machine description.  This should work
1040 /// regardless of whether the function is in SSA form.
1041 ///
1042 FunctionPass *llvm::createARMCodePrinterPass(raw_ostream &o,
1043                                              ARMTargetMachine &tm) {
1044   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1045 }
1046
1047 namespace {
1048   static struct Register {
1049     Register() {
1050       ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1051     }
1052   } Registrator;
1053 }