* Add spaces between words and numbers in comments printed out for longs/floats
[oota-llvm.git] / lib / Target / PowerPC / PPCAsmPrinter.cpp
1 //===-- Printer.cpp - Convert LLVM code to PowerPC assembly ---------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 PowerPC assembly language. This printer is
12 // the output mechanism used by `llc' and `lli -print-machineinstrs'.
13 //
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16 //
17 //===----------------------------------------------------------------------===//
18
19 #define DEBUG_TYPE "asmprinter"
20 #include "PowerPC.h"
21 #include "PowerPCInstrInfo.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Support/Mangler.h"
31 #include "Support/CommandLine.h"
32 #include "Support/Debug.h"
33 #include "Support/Statistic.h"
34 #include "Support/StringExtras.h"
35 #include <set>
36
37 namespace llvm {
38
39 namespace {
40   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
41
42   struct Printer : public MachineFunctionPass {
43     /// Output stream on which we're printing assembly code.
44     ///
45     std::ostream &O;
46
47     /// Target machine description which we query for reg. names, data
48     /// layout, etc.
49     ///
50     TargetMachine &TM;
51
52     /// Name-mangler for global names.
53     ///
54     Mangler *Mang;
55     std::set<std::string> Stubs;
56     std::set<std::string> Strings;
57
58     Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm), labelNumber(0)
59       { }
60
61     /// Cache of mangled name for current function. This is
62     /// recalculated at the beginning of each call to
63     /// runOnMachineFunction().
64     ///
65     std::string CurrentFnName;
66
67     /// Unique incrementer for label values for referencing
68     /// Global values.
69     ///
70     unsigned int labelNumber;
71
72     virtual const char *getPassName() const {
73       return "PowerPC Assembly Printer";
74     }
75
76     void printMachineInstruction(const MachineInstr *MI);
77     void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
78     void printConstantPool(MachineConstantPool *MCP);
79     bool runOnMachineFunction(MachineFunction &F);    
80     bool doInitialization(Module &M);
81     bool doFinalization(Module &M);
82     void emitGlobalConstant(const Constant* CV);
83     void emitConstantValueOnly(const Constant *CV);
84   };
85 } // end of anonymous namespace
86
87 /// createPPCCodePrinterPass - Returns a pass that prints the PPC
88 /// assembly code for a MachineFunction to the given output stream,
89 /// using the given target machine description.  This should work
90 /// regardless of whether the function is in SSA form.
91 ///
92 FunctionPass *createPPCCodePrinterPass(std::ostream &o,TargetMachine &tm) {
93   return new Printer(o, tm);
94 }
95
96 /// isStringCompatible - Can we treat the specified array as a string?
97 /// Only if it is an array of ubytes or non-negative sbytes.
98 ///
99 static bool isStringCompatible(const ConstantArray *CVA) {
100   const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
101   if (ETy == Type::UByteTy) return true;
102   if (ETy != Type::SByteTy) return false;
103
104   for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
105     if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
106       return false;
107
108   return true;
109 }
110
111 /// toOctal - Convert the low order bits of X into an octal digit.
112 ///
113 static inline char toOctal(int X) {
114   return (X&7)+'0';
115 }
116
117 /// getAsCString - Return the specified array as a C compatible
118 /// string, only if the predicate isStringCompatible is true.
119 ///
120 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
121   assert(isStringCompatible(CVA) && "Array is not string compatible!");
122
123   O << "\"";
124   for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
125     unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
126
127     if (C == '"') {
128       O << "\\\"";
129     } else if (C == '\\') {
130       O << "\\\\";
131     } else if (isprint(C)) {
132       O << C;
133     } else {
134       switch(C) {
135       case '\b': O << "\\b"; break;
136       case '\f': O << "\\f"; break;
137       case '\n': O << "\\n"; break;
138       case '\r': O << "\\r"; break;
139       case '\t': O << "\\t"; break;
140       default:
141         O << '\\';
142         O << toOctal(C >> 6);
143         O << toOctal(C >> 3);
144         O << toOctal(C >> 0);
145         break;
146       }
147     }
148   }
149   O << "\"";
150 }
151
152 // Print out the specified constant, without a storage class.  Only the
153 // constants valid in constant expressions can occur here.
154 void Printer::emitConstantValueOnly(const Constant *CV) {
155   if (CV->isNullValue())
156     O << "0";
157   else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
158     assert(CB == ConstantBool::True);
159     O << "1";
160   } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
161     O << CI->getValue();
162   else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
163     O << CI->getValue();
164   else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
165     // This is a constant address for a global variable or function.  Use the
166     // name of the variable or function as the address value.
167     O << Mang->getValueName(CPR->getValue());
168   else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
169     const TargetData &TD = TM.getTargetData();
170     switch(CE->getOpcode()) {
171     case Instruction::GetElementPtr: {
172       // generate a symbolic expression for the byte address
173       const Constant *ptrVal = CE->getOperand(0);
174       std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
175       if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
176         O << "(";
177         emitConstantValueOnly(ptrVal);
178         O << ") + " << Offset;
179       } else {
180         emitConstantValueOnly(ptrVal);
181       }
182       break;
183     }
184     case Instruction::Cast: {
185       // Support only non-converting or widening casts for now, that is, ones
186       // that do not involve a change in value.  This assertion is really gross,
187       // and may not even be a complete check.
188       Constant *Op = CE->getOperand(0);
189       const Type *OpTy = Op->getType(), *Ty = CE->getType();
190
191       // Remember, kids, pointers on x86 can be losslessly converted back and
192       // forth into 32-bit or wider integers, regardless of signedness. :-P
193       assert(((isa<PointerType>(OpTy)
194                && (Ty == Type::LongTy || Ty == Type::ULongTy
195                    || Ty == Type::IntTy || Ty == Type::UIntTy))
196               || (isa<PointerType>(Ty)
197                   && (OpTy == Type::LongTy || OpTy == Type::ULongTy
198                       || OpTy == Type::IntTy || OpTy == Type::UIntTy))
199               || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
200                    && OpTy->isLosslesslyConvertibleTo(Ty))))
201              && "FIXME: Don't yet support this kind of constant cast expr");
202       O << "(";
203       emitConstantValueOnly(Op);
204       O << ")";
205       break;
206     }
207     case Instruction::Add:
208       O << "(";
209       emitConstantValueOnly(CE->getOperand(0));
210       O << ") + (";
211       emitConstantValueOnly(CE->getOperand(1));
212       O << ")";
213       break;
214     default:
215       assert(0 && "Unsupported operator!");
216     }
217   } else {
218     assert(0 && "Unknown constant value!");
219   }
220 }
221
222 // Print a constant value or values, with the appropriate storage class as a
223 // prefix.
224 void Printer::emitGlobalConstant(const Constant *CV) {  
225   const TargetData &TD = TM.getTargetData();
226
227   if (CV->isNullValue()) {
228     O << "\t.space\t " << TD.getTypeSize(CV->getType()) << "\n";      
229     return;
230   } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
231     if (isStringCompatible(CVA)) {
232       O << "\t.ascii ";
233       printAsCString(O, CVA);
234       O << "\n";
235     } else { // Not a string.  Print the values in successive locations
236       const std::vector<Use> &constValues = CVA->getValues();
237       for (unsigned i=0; i < constValues.size(); i++)
238         emitGlobalConstant(cast<Constant>(constValues[i].get()));
239     }
240     return;
241   } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
242     // Print the fields in successive locations. Pad to align if needed!
243     const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
244     const std::vector<Use>& constValues = CVS->getValues();
245     unsigned sizeSoFar = 0;
246     for (unsigned i=0, N = constValues.size(); i < N; i++) {
247       const Constant* field = cast<Constant>(constValues[i].get());
248
249       // Check if padding is needed and insert one or more 0s.
250       unsigned fieldSize = TD.getTypeSize(field->getType());
251       unsigned padSize = ((i == N-1? cvsLayout->StructSize
252                            : cvsLayout->MemberOffsets[i+1])
253                           - cvsLayout->MemberOffsets[i]) - fieldSize;
254       sizeSoFar += fieldSize + padSize;
255
256       // Now print the actual field value
257       emitGlobalConstant(field);
258
259       // Insert the field padding unless it's zero bytes...
260       if (padSize)
261         O << "\t.space\t " << padSize << "\n";      
262     }
263     assert(sizeSoFar == cvsLayout->StructSize &&
264            "Layout of constant struct may be incorrect!");
265     return;
266   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
267     // FP Constants are printed as integer constants to avoid losing
268     // precision...
269     double Val = CFP->getValue();
270     switch (CFP->getType()->getTypeID()) {
271     default: assert(0 && "Unknown floating point type!");
272     case Type::FloatTyID: {
273       union FU {                            // Abide by C TBAA rules
274         float FVal;
275         unsigned UVal;
276       } U;
277       U.FVal = Val;
278       O << ".long\t" << U.UVal << "\t; float " << Val << "\n";
279       return;
280     }
281     case Type::DoubleTyID: {
282       union DU {                            // Abide by C TBAA rules
283         double FVal;
284         uint64_t UVal;
285         struct {
286           uint32_t MSWord;
287           uint32_t LSWord;
288         } T;
289       } U;
290       U.FVal = Val;
291       
292       O << ".long\t" << U.T.MSWord << "\t; double most significant word " 
293         << Val << "\n";
294       O << ".long\t" << U.T.LSWord << "\t; double least significant word " 
295         << Val << "\n";
296       return;
297     }
298     }
299   } else if (CV->getType()->getPrimitiveSize() == 64) {
300     if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
301       union DU {                            // Abide by C TBAA rules
302         int64_t UVal;
303         struct {
304           uint32_t MSWord;
305           uint32_t LSWord;
306         } T;
307       } U;
308       U.UVal = CI->getRawValue();
309         
310       O << ".long\t" << U.T.MSWord << "\t; Double-word most significant word " 
311         << U.UVal << "\n";
312       O << ".long\t" << U.T.LSWord << "\t; Double-word least significant word " 
313         << U.UVal << "\n";
314       return;
315     }
316   }
317
318   const Type *type = CV->getType();
319   O << "\t";
320   switch (type->getTypeID()) {
321   case Type::UByteTyID: case Type::SByteTyID:
322     O << ".byte";
323     break;
324   case Type::UShortTyID: case Type::ShortTyID:
325     O << ".short";
326     break;
327   case Type::BoolTyID: 
328   case Type::PointerTyID:
329   case Type::UIntTyID: case Type::IntTyID:
330     O << ".long";
331     break;
332   case Type::ULongTyID: case Type::LongTyID:    
333     assert (0 && "Should have already output double-word constant.");
334   case Type::FloatTyID: case Type::DoubleTyID:
335     assert (0 && "Should have already output floating point constant.");
336   default:
337     assert (0 && "Can't handle printing this type of thing");
338     break;
339   }
340   O << "\t";
341   emitConstantValueOnly(CV);
342   O << "\n";
343 }
344
345 /// printConstantPool - Print to the current output stream assembly
346 /// representations of the constants in the constant pool MCP. This is
347 /// used to print out constants which have been "spilled to memory" by
348 /// the code generator.
349 ///
350 void Printer::printConstantPool(MachineConstantPool *MCP) {
351   const std::vector<Constant*> &CP = MCP->getConstants();
352   const TargetData &TD = TM.getTargetData();
353  
354   if (CP.empty()) return;
355
356   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
357     O << "\t.const\n";
358     O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
359       << "\n";
360     O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
361       << *CP[i] << "\n";
362     emitGlobalConstant(CP[i]);
363   }
364 }
365
366 /// runOnMachineFunction - This uses the printMachineInstruction()
367 /// method to print assembly for each instruction.
368 ///
369 bool Printer::runOnMachineFunction(MachineFunction &MF) {
370   O << "\n\n";
371   // What's my mangled name?
372   CurrentFnName = Mang->getValueName(MF.getFunction());
373
374   // Print out constants referenced by the function
375   printConstantPool(MF.getConstantPool());
376
377   // Print out labels for the function.
378   O << "\t.text\n"; 
379   O << "\t.globl\t" << CurrentFnName << "\n";
380   O << "\t.align 2\n";
381   O << CurrentFnName << ":\n";
382
383   // Print out code for the function.
384   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
385        I != E; ++I) {
386     // Print a label for the basic block.
387     O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
388       << I->getBasicBlock()->getName() << "\n";
389     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
390       II != E; ++II) {
391       // Print the assembly for the instruction.
392       O << "\t";
393       printMachineInstruction(II);
394     }
395   }
396
397   // We didn't modify anything.
398   return false;
399 }
400
401 void Printer::printOp(const MachineOperand &MO,
402                       bool elideOffsetKeyword /* = false */) {
403   const MRegisterInfo &RI = *TM.getRegisterInfo();
404   int new_symbol;
405   
406   switch (MO.getType()) {
407   case MachineOperand::MO_VirtualRegister:
408     if (Value *V = MO.getVRegValueOrNull()) {
409       O << "<" << V->getName() << ">";
410       return;
411     }
412     // FALLTHROUGH
413   case MachineOperand::MO_MachineRegister:
414   case MachineOperand::MO_CCRegister:
415     O << LowercaseString(RI.get(MO.getReg()).Name);
416     return;
417
418   case MachineOperand::MO_SignExtendedImmed:
419   case MachineOperand::MO_UnextendedImmed:
420     O << (int)MO.getImmedValue();
421     return;
422     
423   case MachineOperand::MO_PCRelativeDisp:
424     std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
425     abort();
426     return;
427     
428   case MachineOperand::MO_MachineBasicBlock: {
429     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
430     O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
431       << "_" << MBBOp->getNumber() << "\t; "
432       << MBBOp->getBasicBlock()->getName();
433     return;
434   }
435
436   case MachineOperand::MO_ConstantPoolIndex:
437     O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
438     return;
439
440   case MachineOperand::MO_ExternalSymbol:
441     O << MO.getSymbolName();
442     return;
443
444   case MachineOperand::MO_GlobalAddress:
445     if (!elideOffsetKeyword) {
446       // Dynamically-resolved functions need a stub for the function
447       Function *F = dyn_cast<Function>(MO.getGlobal());
448       if (F && F->isExternal()) {
449         Stubs.insert(Mang->getValueName(MO.getGlobal()));
450         O << "L" << Mang->getValueName(MO.getGlobal()) << "$stub";
451       } else {
452         O << Mang->getValueName(MO.getGlobal());
453       }
454     }
455     return;
456     
457   default:
458     O << "<unknown operand type: " << MO.getType() << ">";
459     return;
460   }
461 }
462
463 #if 0
464 static inline
465 unsigned int ValidOpcodes(const MachineInstr *MI, unsigned int ArgType[5]) {
466   int i;
467   unsigned int retval = 1;
468   
469   for(i = 0; i<5; i++) {
470     switch(ArgType[i]) {
471       case none:
472         break;
473       case Gpr:
474       case Gpr0:
475         Type::UIntTy
476       case Simm16:
477       case Zimm16:
478       case PCRelimm24:
479       case Imm24:
480       case Imm5:
481       case PCRelimm14:
482       case Imm14:
483       case Imm2:
484       case Crf:
485       case Imm3:
486       case Imm1:
487       case Fpr:
488       case Imm4:
489       case Imm8:
490       case Disimm16:
491       case Spr:
492       case Sgr:
493   };
494     
495     }
496   }
497 }
498 #endif
499
500 /// printMachineInstruction -- Print out a single PPC32 LLVM instruction
501 /// MI in Darwin syntax to the current output stream.
502 ///
503 void Printer::printMachineInstruction(const MachineInstr *MI) {
504   unsigned Opcode = MI->getOpcode();
505   const TargetInstrInfo &TII = *TM.getInstrInfo();
506   const TargetInstrDescriptor &Desc = TII.get(Opcode);
507   unsigned int i;
508
509   unsigned int ArgCount = MI->getNumOperands();
510     //Desc.TSFlags & PPC32II::ArgCountMask;
511   unsigned int ArgType[] = {
512     (Desc.TSFlags >> PPC32II::Arg0TypeShift) & PPC32II::ArgTypeMask,
513     (Desc.TSFlags >> PPC32II::Arg1TypeShift) & PPC32II::ArgTypeMask,
514     (Desc.TSFlags >> PPC32II::Arg2TypeShift) & PPC32II::ArgTypeMask,
515     (Desc.TSFlags >> PPC32II::Arg3TypeShift) & PPC32II::ArgTypeMask,
516     (Desc.TSFlags >> PPC32II::Arg4TypeShift) & PPC32II::ArgTypeMask
517   };
518   assert(((Desc.TSFlags & PPC32II::VMX) == 0) &&
519          "Instruction requires VMX support");
520   assert(((Desc.TSFlags & PPC32II::PPC64) == 0) &&
521          "Instruction requires 64 bit support");
522   //assert ( ValidOpcodes(MI, ArgType) && "Instruction has invalid inputs");
523   ++EmittedInsts;
524
525   if (Opcode == PPC32::IMPLICIT_DEF) {
526     O << "; IMPLICIT DEF ";
527     printOp(MI->getOperand(0));
528     O << "\n";
529     return;
530   }
531   // FIXME: should probably be converted to cout.width and cout.fill
532   if (Opcode == PPC32::MovePCtoLR) {
533     O << "bl \"L0000" << labelNumber << "$pb\"\n";
534     O << "\"L0000" << labelNumber << "$pb\":\n";
535     O << "\tmflr ";
536     printOp(MI->getOperand(0));
537     O << "\n";
538     return;
539   }
540
541   O << TII.getName(MI->getOpcode()) << " ";
542   DEBUG(std::cerr << TII.getName(MI->getOpcode()) << " expects " 
543                   << ArgCount << " args\n");
544
545   if (Opcode == PPC32::LOADLoAddr) {
546     printOp(MI->getOperand(0));
547     O << ", lo16(";
548     printOp(MI->getOperand(2));
549     O << "-\"L0000" << labelNumber << "$pb\")";
550     labelNumber++;
551     O << "(";
552     if (MI->getOperand(1).getReg() == PPC32::R0)
553       O << "0";
554     else
555       printOp(MI->getOperand(1));
556     O << ")\n";
557   } else if (Opcode == PPC32::LOADHiAddr) {
558     printOp(MI->getOperand(0));
559     O << ", ";
560     if (MI->getOperand(1).getReg() == PPC32::R0)
561       O << "0";
562     else
563       printOp(MI->getOperand(1));
564     O << ", ha16(" ;
565     printOp(MI->getOperand(2));
566      O << "-\"L0000" << labelNumber << "$pb\")\n";
567   } else if (ArgCount == 3 && ArgType[1] == PPC32II::Disimm16) {
568     printOp(MI->getOperand(0));
569     O << ", ";
570     printOp(MI->getOperand(1));
571     O << "(";
572     if (MI->getOperand(2).hasAllocatedReg() &&
573         MI->getOperand(2).getReg() == PPC32::R0)
574       O << "0";
575     else
576       printOp(MI->getOperand(2));
577     O << ")\n";
578   } else {
579     for (i = 0; i < ArgCount; ++i) {
580       if (i == 1 && ArgCount == 3 && ArgType[2] == PPC32II::Simm16 &&
581           MI->getOperand(1).hasAllocatedReg() && 
582           MI->getOperand(1).getReg() == PPC32::R0) {
583         O << "0";
584       } else {
585         //std::cout << "DEBUG " << (*(TM.getRegisterInfo())).get(MI->getOperand(i).getReg()).Name << "\n";
586         printOp(MI->getOperand(i));
587       }
588       if (ArgCount - 1 == i)
589         O << "\n";
590       else
591         O << ", ";
592     }
593   }
594 }
595
596 bool Printer::doInitialization(Module &M) {
597   Mang = new Mangler(M, true);
598   return false; // success
599 }
600
601 // SwitchSection - Switch to the specified section of the executable if we are
602 // not already in it!
603 //
604 static void SwitchSection(std::ostream &OS, std::string &CurSection,
605                           const char *NewSection) {
606   if (CurSection != NewSection) {
607     CurSection = NewSection;
608     if (!CurSection.empty())
609       OS << "\t" << NewSection << "\n";
610   }
611 }
612
613 bool Printer::doFinalization(Module &M) {
614   const TargetData &TD = TM.getTargetData();
615   std::string CurSection;
616
617   // Print out module-level global variables here.
618   for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
619     if (I->hasInitializer()) {   // External global require no code
620       O << "\n\n";
621       std::string name = Mang->getValueName(I);
622       Constant *C = I->getInitializer();
623       unsigned Size = TD.getTypeSize(C->getType());
624       unsigned Align = TD.getTypeAlignment(C->getType());
625
626       if (C->isNullValue() && 
627           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
628            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
629         SwitchSection(O, CurSection, ".data");
630         if (I->hasInternalLinkage())
631           O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
632             << "," << (unsigned)TD.getTypeAlignment(C->getType());
633         else 
634           O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
635         O << "\t\t; ";
636         WriteAsOperand(O, I, true, true, &M);
637         O << "\n";
638       } else {
639         switch (I->getLinkage()) {
640         case GlobalValue::LinkOnceLinkage:
641         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
642           // Nonnull linkonce -> weak
643           O << "\t.weak " << name << "\n";
644           SwitchSection(O, CurSection, "");
645           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
646           break;
647         
648         case GlobalValue::AppendingLinkage:
649           // FIXME: appending linkage variables should go into a section of
650           // their name or something.  For now, just emit them as external.
651         case GlobalValue::ExternalLinkage:
652           // If external or appending, declare as a global symbol
653           O << "\t.globl " << name << "\n";
654           // FALL THROUGH
655         case GlobalValue::InternalLinkage:
656           SwitchSection(O, CurSection, ".data");
657           break;
658         }
659
660         O << "\t.align " << Align << "\n";
661         O << name << ":\t\t\t\t; ";
662         WriteAsOperand(O, I, true, true, &M);
663         O << " = ";
664         WriteAsOperand(O, C, false, false, &M);
665         O << "\n";
666         emitGlobalConstant(C);
667       }
668     }
669         
670   for(std::set<std::string>::iterator i = Stubs.begin(); i != Stubs.end(); ++i)
671   {
672     O << "\t.picsymbol_stub\n";
673     O << "L" << *i << "$stub:\n";
674     O << "\t.indirect_symbol " << *i << "\n";
675     O << "\tmflr r0\n";
676     O << "\tbl L0$" << *i << "\n";
677     O << "L0$" << *i << ":\n";
678     O << "\tmflr r11\n";
679     O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
680     O << "\tmtlr r0\n";
681     O << "\tlwz r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
682     O << "\tmtctr r12\n";
683     O << "\taddi r11,r11,lo16(L" << *i << "$lazy_ptr - L0$" << *i << ")\n";
684     O << "\tbctr\n";
685     O << ".data\n";
686     O << ".lazy_symbol_pointer\n";
687     O << "L" << *i << "$lazy_ptr:\n";
688     O << ".indirect_symbol " << *i << "\n";
689     O << ".long dyld_stub_binding_helper\n";
690   }
691
692   delete Mang;
693   return false; // success
694 }
695
696 } // End llvm namespace