1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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'.
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
17 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "asmprinter"
21 #include "PPCTargetMachine.h"
22 #include "PPCSubtarget.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/AsmPrinter.h"
28 #include "llvm/CodeGen/DwarfWriter.h"
29 #include "llvm/CodeGen/MachineDebugInfo.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Target/MRegisterInfo.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/ADT/StringExtras.h"
46 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
48 class PPCAsmPrinter : public AsmPrinter {
50 std::set<std::string> FnStubs, GVStubs;
52 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
53 : AsmPrinter(O, TM) {}
55 virtual const char *getPassName() const {
56 return "PowerPC Assembly Printer";
59 PPCTargetMachine &getTM() {
60 return static_cast<PPCTargetMachine&>(TM);
63 unsigned enumRegToMachineReg(unsigned enumReg) {
65 default: assert(0 && "Unhandled register!"); break;
66 case PPC::CR0: return 0;
67 case PPC::CR1: return 1;
68 case PPC::CR2: return 2;
69 case PPC::CR3: return 3;
70 case PPC::CR4: return 4;
71 case PPC::CR5: return 5;
72 case PPC::CR6: return 6;
73 case PPC::CR7: return 7;
78 /// printInstruction - This method is automatically generated by tablegen
79 /// from the instruction set description. This method returns true if the
80 /// machine instruction was sufficiently described to print it, otherwise it
82 bool printInstruction(const MachineInstr *MI);
84 void printMachineInstruction(const MachineInstr *MI);
85 void printOp(const MachineOperand &MO);
87 void printOperand(const MachineInstr *MI, unsigned OpNo) {
88 const MachineOperand &MO = MI->getOperand(OpNo);
89 if (MO.getType() == MachineOperand::MO_MachineRegister) {
90 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
91 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
92 } else if (MO.isImmediate()) {
93 O << MO.getImmedValue();
99 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
100 unsigned AsmVariant, const char *ExtraCode);
101 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
102 unsigned AsmVariant, const char *ExtraCode);
105 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
106 unsigned char value = MI->getOperand(OpNo).getImmedValue();
107 assert(value <= 31 && "Invalid u5imm argument!");
108 O << (unsigned int)value;
110 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
111 unsigned char value = MI->getOperand(OpNo).getImmedValue();
112 assert(value <= 63 && "Invalid u6imm argument!");
113 O << (unsigned int)value;
115 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
116 O << (short)MI->getOperand(OpNo).getImmedValue();
118 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
119 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
121 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
122 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
124 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
125 // Branches can take an immediate operand. This is used by the branch
126 // selection pass to print $+8, an eight byte displacement from the PC.
127 if (MI->getOperand(OpNo).isImmediate()) {
128 O << "$+" << MI->getOperand(OpNo).getImmedValue();
130 printOp(MI->getOperand(OpNo));
133 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
134 const MachineOperand &MO = MI->getOperand(OpNo);
135 if (TM.getRelocationModel() != Reloc::Static) {
136 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
137 GlobalValue *GV = MO.getGlobal();
138 if (((GV->isExternal() || GV->hasWeakLinkage() ||
139 GV->hasLinkOnceLinkage()))) {
140 // Dynamically-resolved functions need a stub for the function.
141 std::string Name = Mang->getValueName(GV);
142 FnStubs.insert(Name);
143 O << "L" << Name << "$stub";
147 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
148 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
149 FnStubs.insert(Name);
150 O << "L" << Name << "$stub";
155 printOp(MI->getOperand(OpNo));
157 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
158 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
160 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
161 O << "\"L" << getFunctionNumber() << "$pb\"\n";
162 O << "\"L" << getFunctionNumber() << "$pb\":";
164 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
165 if (MI->getOperand(OpNo).isImmediate()) {
166 printS16ImmOperand(MI, OpNo);
169 printOp(MI->getOperand(OpNo));
170 if (TM.getRelocationModel() == Reloc::PIC)
171 O << "-\"L" << getFunctionNumber() << "$pb\")";
176 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
177 if (MI->getOperand(OpNo).isImmediate()) {
178 printS16ImmOperand(MI, OpNo);
181 printOp(MI->getOperand(OpNo));
182 if (TM.getRelocationModel() == Reloc::PIC)
183 O << "-\"L" << getFunctionNumber() << "$pb\")";
188 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
189 unsigned CCReg = MI->getOperand(OpNo).getReg();
190 unsigned RegNo = enumRegToMachineReg(CCReg);
191 O << (0x80 >> RegNo);
193 // The new addressing mode printers.
194 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
195 printSymbolLo(MI, OpNo);
197 if (MI->getOperand(OpNo+1).isRegister() &&
198 MI->getOperand(OpNo+1).getReg() == PPC::R0)
201 printOperand(MI, OpNo+1);
204 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
205 if (MI->getOperand(OpNo).isImmediate())
206 printS16X4ImmOperand(MI, OpNo);
208 printSymbolLo(MI, OpNo);
210 if (MI->getOperand(OpNo+1).isRegister() &&
211 MI->getOperand(OpNo+1).getReg() == PPC::R0)
214 printOperand(MI, OpNo+1);
218 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
219 // When used as the base register, r0 reads constant zero rather than
220 // the value contained in the register. For this reason, the darwin
221 // assembler requires that we print r0 as 0 (no r) when used as the base.
222 const MachineOperand &MO = MI->getOperand(OpNo);
223 if (MO.getReg() == PPC::R0)
226 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
228 printOperand(MI, OpNo+1);
231 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
232 virtual bool doFinalization(Module &M) = 0;
236 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X
238 struct DarwinDwarfWriter : public DwarfWriter {
240 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap)
244 DwarfAbbrevSection = ".section __DWARFA,__debug_abbrev";
245 DwarfInfoSection = ".section __DWARFA,__debug_info";
246 DwarfLineSection = ".section __DWARFA,__debug_line";
247 DwarfFrameSection = ".section __DWARFA,__debug_frame";
248 DwarfPubNamesSection = ".section __DWARFA,__debug_pubnames";
249 DwarfPubTypesSection = ".section __DWARFA,__debug_pubtypes";
250 DwarfStrSection = ".section __DWARFA,__debug_str";
251 DwarfLocSection = ".section __DWARFA,__debug_loc";
252 DwarfARangesSection = ".section __DWARFA,__debug_aranges";
253 DwarfRangesSection = ".section __DWARFA,__debug_ranges";
254 DwarfMacInfoSection = ".section __DWARFA,__debug_macinfo";
255 TextSection = ".text";
256 DataSection = ".data";
260 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
262 struct DarwinAsmPrinter : public PPCAsmPrinter {
264 DarwinDwarfWriter DW;
266 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
267 : PPCAsmPrinter(O, TM), DW(O, this) {
270 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
271 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
272 Data64bitsDirective = 0; // we can't emit a 64-bit unit
273 AlignmentIsInBytes = false; // Alignment is by power of 2.
274 ConstantPoolSection = "\t.const\t";
275 LCOMMDirective = "\t.lcomm\t";
276 StaticCtorsSection = ".mod_init_func";
277 StaticDtorsSection = ".mod_term_func";
278 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
281 virtual const char *getPassName() const {
282 return "Darwin PPC Assembly Printer";
285 bool runOnMachineFunction(MachineFunction &F);
286 bool doInitialization(Module &M);
287 bool doFinalization(Module &M);
289 void getAnalysisUsage(AnalysisUsage &AU) const {
290 AU.setPreservesAll();
291 AU.addRequired<MachineDebugInfo>();
292 PPCAsmPrinter::getAnalysisUsage(AU);
297 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
299 struct AIXAsmPrinter : public PPCAsmPrinter {
300 /// Map for labels corresponding to global variables
302 std::map<const GlobalVariable*,std::string> GVToLabelMap;
304 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
305 : PPCAsmPrinter(O, TM) {
308 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
309 Data64bitsDirective = 0; // we can't emit a 64-bit unit
310 AlignmentIsInBytes = false; // Alignment is by power of 2.
311 ConstantPoolSection = "\t.const\t";
314 virtual const char *getPassName() const {
315 return "AIX PPC Assembly Printer";
318 bool runOnMachineFunction(MachineFunction &F);
319 bool doInitialization(Module &M);
320 bool doFinalization(Module &M);
322 } // end of anonymous namespace
324 /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
325 /// code for a MachineFunction to the given output stream, in a format that the
326 /// Darwin assembler can deal with.
328 FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o,
329 PPCTargetMachine &tm) {
330 return new DarwinAsmPrinter(o, tm);
333 /// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
334 /// for a MachineFunction to the given output stream, in a format that the
335 /// AIX 5L assembler can deal with.
337 FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, PPCTargetMachine &tm) {
338 return new AIXAsmPrinter(o, tm);
341 // Include the auto-generated portion of the assembly writer
342 #include "PPCGenAsmWriter.inc"
344 void PPCAsmPrinter::printOp(const MachineOperand &MO) {
345 const MRegisterInfo &RI = *TM.getRegisterInfo();
348 switch (MO.getType()) {
349 case MachineOperand::MO_VirtualRegister:
350 if (Value *V = MO.getVRegValueOrNull()) {
351 O << "<" << V->getName() << ">";
355 case MachineOperand::MO_MachineRegister:
356 case MachineOperand::MO_CCRegister:
357 O << RI.get(MO.getReg()).Name;
360 case MachineOperand::MO_SignExtendedImmed:
361 case MachineOperand::MO_UnextendedImmed:
362 std::cerr << "printOp() does not handle immediate values\n";
366 case MachineOperand::MO_PCRelativeDisp:
367 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
371 case MachineOperand::MO_MachineBasicBlock: {
372 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
373 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
374 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
378 case MachineOperand::MO_ConstantPoolIndex:
379 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
380 << '_' << MO.getConstantPoolIndex();
382 case MachineOperand::MO_ExternalSymbol:
383 // Computing the address of an external symbol, not calling it.
384 if (TM.getRelocationModel() != Reloc::Static) {
385 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
386 GVStubs.insert(Name);
387 O << "L" << Name << "$non_lazy_ptr";
390 O << GlobalPrefix << MO.getSymbolName();
392 case MachineOperand::MO_GlobalAddress: {
393 // Computing the address of a global symbol, not calling it.
394 GlobalValue *GV = MO.getGlobal();
395 std::string Name = Mang->getValueName(GV);
396 int offset = MO.getOffset();
398 // External or weakly linked global variables need non-lazily-resolved stubs
399 if (TM.getRelocationModel() != Reloc::Static) {
400 if (((GV->isExternal() || GV->hasWeakLinkage() ||
401 GV->hasLinkOnceLinkage()))) {
402 GVStubs.insert(Name);
403 O << "L" << Name << "$non_lazy_ptr";
413 O << "<unknown operand type: " << MO.getType() << ">";
418 /// PrintAsmOperand - Print out an operand for an inline asm expression.
420 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
422 const char *ExtraCode) {
423 // Does this asm operand have a single letter operand modifier?
424 if (ExtraCode && ExtraCode[0]) {
425 if (ExtraCode[1] != 0) return true; // Unknown modifier.
427 switch (ExtraCode[0]) {
428 default: return true; // Unknown modifier.
429 case 'L': // Write second word of DImode reference.
430 // Verify that this operand has two consecutive registers.
431 if (!MI->getOperand(OpNo).isRegister() ||
432 OpNo+1 == MI->getNumOperands() ||
433 !MI->getOperand(OpNo+1).isRegister())
435 ++OpNo; // Return the high-part.
440 printOperand(MI, OpNo);
444 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
446 const char *ExtraCode) {
447 if (ExtraCode && ExtraCode[0])
448 return true; // Unknown modifier.
449 printMemRegReg(MI, OpNo);
453 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
454 /// the current output stream.
456 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
459 // Check for slwi/srwi mnemonics.
460 if (MI->getOpcode() == PPC::RLWINM) {
461 bool FoundMnemonic = false;
462 unsigned char SH = MI->getOperand(2).getImmedValue();
463 unsigned char MB = MI->getOperand(3).getImmedValue();
464 unsigned char ME = MI->getOperand(4).getImmedValue();
465 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
466 O << "slwi "; FoundMnemonic = true;
468 if (SH <= 31 && MB == (32-SH) && ME == 31) {
469 O << "srwi "; FoundMnemonic = true;
476 O << ", " << (unsigned int)SH << "\n";
479 } else if (MI->getOpcode() == PPC::OR4 || MI->getOpcode() == PPC::OR8) {
480 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
490 if (printInstruction(MI))
491 return; // Printer was automatically generated
493 assert(0 && "Unhandled instruction in asm writer!");
499 /// runOnMachineFunction - This uses the printMachineInstruction()
500 /// method to print assembly for each instruction.
502 bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
503 // FIXME - is this the earliest this can be set?
504 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
506 SetupMachineFunction(MF);
509 // Emit pre-function debug information.
510 DW.BeginFunction(&MF);
512 // Print out constants referenced by the function
513 EmitConstantPool(MF.getConstantPool());
515 // Print out labels for the function.
516 const Function *F = MF.getFunction();
517 switch (F->getLinkage()) {
518 default: assert(0 && "Unknown linkage type!");
519 case Function::InternalLinkage: // Symbols default to internal.
520 SwitchSection(".text", F);
522 case Function::ExternalLinkage:
523 SwitchSection(".text", F);
524 O << "\t.globl\t" << CurrentFnName << "\n";
526 case Function::WeakLinkage:
527 case Function::LinkOnceLinkage:
528 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
530 O << "\t.globl\t" << CurrentFnName << "\n";
531 O << "\t.weak_definition\t" << CurrentFnName << "\n";
535 O << CurrentFnName << ":\n";
537 // Print out code for the function.
538 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
540 // Print a label for the basic block.
541 if (I != MF.begin()) {
542 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
543 << I->getNumber() << ":\t";
544 if (!I->getBasicBlock()->getName().empty())
545 O << CommentString << " " << I->getBasicBlock()->getName();
548 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
550 // Print the assembly for the instruction.
552 printMachineInstruction(II);
556 // Emit post-function debug information.
559 // We didn't modify anything.
564 bool DarwinAsmPrinter::doInitialization(Module &M) {
565 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
566 O << "\t.machine ppc970\n";
567 AsmPrinter::doInitialization(M);
569 // Darwin wants symbols to be quoted if they have complex names.
570 Mang->setUseQuotes(true);
572 // Emit initial debug information.
577 bool DarwinAsmPrinter::doFinalization(Module &M) {
578 const TargetData &TD = TM.getTargetData();
580 // Print out module-level global variables here.
581 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
583 if (!I->hasInitializer()) continue; // External global require no code
585 // Check to see if this is a special global used by LLVM, if so, emit it.
586 if (EmitSpecialLLVMGlobal(I))
589 std::string name = Mang->getValueName(I);
590 Constant *C = I->getInitializer();
591 unsigned Size = TD.getTypeSize(C->getType());
592 unsigned Align = getPreferredAlignmentLog(I);
594 if (C->isNullValue() && /* FIXME: Verify correct */
595 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
596 I->hasLinkOnceLinkage() ||
597 (I->hasExternalLinkage() && !I->hasSection()))) {
598 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
599 if (I->hasExternalLinkage()) {
600 O << "\t.globl " << name << '\n';
601 O << "\t.zerofill __DATA, __common, " << name << ", "
602 << Size << ", " << Align;
603 } else if (I->hasInternalLinkage()) {
604 SwitchSection(".data", I);
605 O << LCOMMDirective << name << "," << Size << "," << Align;
607 SwitchSection(".data", I);
608 O << ".comm " << name << "," << Size;
610 O << "\t\t; '" << I->getName() << "'\n";
612 switch (I->getLinkage()) {
613 case GlobalValue::LinkOnceLinkage:
614 case GlobalValue::WeakLinkage:
615 O << "\t.globl " << name << '\n'
616 << "\t.weak_definition " << name << '\n';
617 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
619 case GlobalValue::AppendingLinkage:
620 // FIXME: appending linkage variables should go into a section of
621 // their name or something. For now, just emit them as external.
622 case GlobalValue::ExternalLinkage:
623 // If external or appending, declare as a global symbol
624 O << "\t.globl " << name << "\n";
626 case GlobalValue::InternalLinkage:
627 SwitchSection(".data", I);
630 std::cerr << "Unknown linkage type!";
634 EmitAlignment(Align, I);
635 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
636 EmitGlobalConstant(C);
641 // Output stubs for dynamically-linked functions
642 if (TM.getRelocationModel() == Reloc::PIC) {
643 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
645 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
646 "pure_instructions,32", 0);
648 O << "L" << *i << "$stub:\n";
649 O << "\t.indirect_symbol " << *i << "\n";
651 O << "\tbcl 20,31,L0$" << *i << "\n";
652 O << "L0$" << *i << ":\n";
654 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
656 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
657 O << "\tmtctr r12\n";
659 SwitchSection(".lazy_symbol_pointer", 0);
660 O << "L" << *i << "$lazy_ptr:\n";
661 O << "\t.indirect_symbol " << *i << "\n";
662 O << "\t.long dyld_stub_binding_helper\n";
665 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
667 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
668 "pure_instructions,16", 0);
670 O << "L" << *i << "$stub:\n";
671 O << "\t.indirect_symbol " << *i << "\n";
672 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
673 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
674 O << "\tmtctr r12\n";
676 SwitchSection(".lazy_symbol_pointer", 0);
677 O << "L" << *i << "$lazy_ptr:\n";
678 O << "\t.indirect_symbol " << *i << "\n";
679 O << "\t.long dyld_stub_binding_helper\n";
685 // Output stubs for external and common global variables.
686 if (GVStubs.begin() != GVStubs.end()) {
687 SwitchSection(".non_lazy_symbol_pointer", 0);
688 for (std::set<std::string>::iterator I = GVStubs.begin(),
689 E = GVStubs.end(); I != E; ++I) {
690 O << "L" << *I << "$non_lazy_ptr:\n";
691 O << "\t.indirect_symbol " << *I << "\n";
696 // Emit initial debug information.
699 // Funny Darwin hack: This flag tells the linker that no global symbols
700 // contain code that falls through to other global symbols (e.g. the obvious
701 // implementation of multiple entry points). If this doesn't occur, the
702 // linker can safely perform dead code stripping. Since LLVM never generates
703 // code that does this, it is always safe to set.
704 O << "\t.subsections_via_symbols\n";
706 AsmPrinter::doFinalization(M);
707 return false; // success
710 /// runOnMachineFunction - This uses the e()
711 /// method to print assembly for each instruction.
713 bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
714 SetupMachineFunction(MF);
716 // Print out constants referenced by the function
717 EmitConstantPool(MF.getConstantPool());
719 // Print out header for the function.
720 O << "\t.csect .text[PR]\n"
722 << "\t.globl " << CurrentFnName << '\n'
723 << "\t.globl ." << CurrentFnName << '\n'
724 << "\t.csect " << CurrentFnName << "[DS],3\n"
725 << CurrentFnName << ":\n"
726 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
727 << "\t.csect .text[PR]\n"
728 << '.' << CurrentFnName << ":\n";
730 // Print out code for the function.
731 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
733 // Print a label for the basic block.
734 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
736 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
737 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
739 // Print the assembly for the instruction.
741 printMachineInstruction(II);
745 O << "LT.." << CurrentFnName << ":\n"
747 << "\t.byte 0,0,32,65,128,0,0,0\n"
748 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
750 << "\t.byte \"" << CurrentFnName << "\"\n"
753 // We didn't modify anything.
757 bool AIXAsmPrinter::doInitialization(Module &M) {
758 SwitchSection("", 0);
759 const TargetData &TD = TM.getTargetData();
761 O << "\t.machine \"ppc64\"\n"
763 << "\t.csect .text[PR]\n";
765 // Print out module-level global variables
766 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
768 if (!I->hasInitializer())
771 std::string Name = I->getName();
772 Constant *C = I->getInitializer();
773 // N.B.: We are defaulting to writable strings
774 if (I->hasExternalLinkage()) {
775 O << "\t.globl " << Name << '\n'
776 << "\t.csect .data[RW],3\n";
778 O << "\t.csect _global.rw_c[RW],3\n";
781 EmitGlobalConstant(C);
784 // Output labels for globals
785 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
786 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
788 const GlobalVariable *GV = I;
789 // Do not output labels for unused variables
790 if (GV->isExternal() && GV->use_begin() == GV->use_end())
793 IncrementFunctionNumber();
794 std::string Name = GV->getName();
795 std::string Label = "LC.." + utostr(getFunctionNumber());
796 GVToLabelMap[GV] = Label;
798 << "\t.tc " << Name << "[TC]," << Name;
799 if (GV->isExternal()) O << "[RW]";
803 AsmPrinter::doInitialization(M);
804 return false; // success
807 bool AIXAsmPrinter::doFinalization(Module &M) {
808 const TargetData &TD = TM.getTargetData();
809 // Print out module-level global variables
810 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
812 if (I->hasInitializer() || I->hasExternalLinkage())
815 std::string Name = I->getName();
816 if (I->hasInternalLinkage()) {
817 O << "\t.lcomm " << Name << ",16,_global.bss_c";
819 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
820 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
822 O << "\t\t" << CommentString << " ";
823 WriteAsOperand(O, I, false, true, &M);
827 O << "_section_.text:\n"
828 << "\t.csect .data[RW],3\n"
829 << "\t.llong _section_.text\n";
830 AsmPrinter::doFinalization(M);
831 return false; // success