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