1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "PPCPredicates.h"
22 #include "PPCTargetMachine.h"
23 #include "PPCSubtarget.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/MDNode.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/DwarfWriter.h"
31 #include "llvm/CodeGen/MachineModuleInfo.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/Support/Mangler.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Target/TargetAsmInfo.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetOptions.h"
46 #include "llvm/ADT/Statistic.h"
47 #include "llvm/ADT/StringExtras.h"
48 #include "llvm/ADT/StringSet.h"
51 STATISTIC(EmittedInsts, "Number of machine instrs printed");
54 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
56 StringSet<> FnStubs, GVStubs, HiddenGVStubs;
57 const PPCSubtarget &Subtarget;
59 explicit PPCAsmPrinter(raw_ostream &O, TargetMachine &TM,
60 const TargetAsmInfo *T, bool V)
61 : AsmPrinter(O, TM, T, V),
62 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
64 virtual const char *getPassName() const {
65 return "PowerPC Assembly Printer";
68 PPCTargetMachine &getTM() {
69 return static_cast<PPCTargetMachine&>(TM);
72 unsigned enumRegToMachineReg(unsigned enumReg) {
74 default: LLVM_UNREACHABLE("Unhandled register!");
75 case PPC::CR0: return 0;
76 case PPC::CR1: return 1;
77 case PPC::CR2: return 2;
78 case PPC::CR3: return 3;
79 case PPC::CR4: return 4;
80 case PPC::CR5: return 5;
81 case PPC::CR6: return 6;
82 case PPC::CR7: return 7;
87 /// printInstruction - This method is automatically generated by tablegen
88 /// from the instruction set description. This method returns true if the
89 /// machine instruction was sufficiently described to print it, otherwise it
91 bool printInstruction(const MachineInstr *MI);
93 void printMachineInstruction(const MachineInstr *MI);
94 void printOp(const MachineOperand &MO);
96 /// stripRegisterPrefix - This method strips the character prefix from a
97 /// register name so that only the number is left. Used by for linux asm.
98 const char *stripRegisterPrefix(const char *RegName) {
102 case 'v': return RegName + 1;
103 case 'c': if (RegName[1] == 'r') return RegName + 2;
109 /// printRegister - Print register according to target requirements.
111 void printRegister(const MachineOperand &MO, bool R0AsZero) {
112 unsigned RegNo = MO.getReg();
113 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
115 // If we should use 0 for R0.
116 if (R0AsZero && RegNo == PPC::R0) {
121 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
122 // Linux assembler (Others?) does not take register mnemonics.
123 // FIXME - What about special registers used in mfspr/mtspr?
124 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
128 void printOperand(const MachineInstr *MI, unsigned OpNo) {
129 const MachineOperand &MO = MI->getOperand(OpNo);
131 printRegister(MO, false);
132 } else if (MO.isImm()) {
139 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
140 unsigned AsmVariant, const char *ExtraCode);
141 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
142 unsigned AsmVariant, const char *ExtraCode);
145 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
146 char value = MI->getOperand(OpNo).getImm();
147 value = (value << (32-5)) >> (32-5);
150 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
151 unsigned char value = MI->getOperand(OpNo).getImm();
152 assert(value <= 31 && "Invalid u5imm argument!");
153 O << (unsigned int)value;
155 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
156 unsigned char value = MI->getOperand(OpNo).getImm();
157 assert(value <= 63 && "Invalid u6imm argument!");
158 O << (unsigned int)value;
160 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
161 O << (short)MI->getOperand(OpNo).getImm();
163 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
164 O << (unsigned short)MI->getOperand(OpNo).getImm();
166 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
167 if (MI->getOperand(OpNo).isImm()) {
168 O << (short)(MI->getOperand(OpNo).getImm()*4);
171 printOp(MI->getOperand(OpNo));
172 if (TM.getRelocationModel() == Reloc::PIC_)
173 O << "-\"L" << getFunctionNumber() << "$pb\")";
178 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
179 // Branches can take an immediate operand. This is used by the branch
180 // selection pass to print $+8, an eight byte displacement from the PC.
181 if (MI->getOperand(OpNo).isImm()) {
182 O << "$+" << MI->getOperand(OpNo).getImm()*4;
184 printOp(MI->getOperand(OpNo));
187 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
188 const MachineOperand &MO = MI->getOperand(OpNo);
189 if (TM.getRelocationModel() != Reloc::Static) {
190 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
191 GlobalValue *GV = MO.getGlobal();
192 if (GV->isDeclaration() || GV->isWeakForLinker()) {
193 // Dynamically-resolved functions need a stub for the function.
194 std::string Name = Mang->getValueName(GV);
195 FnStubs.insert(Name);
196 printSuffixedName(Name, "$stub");
200 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
201 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
202 FnStubs.insert(Name);
203 printSuffixedName(Name, "$stub");
208 printOp(MI->getOperand(OpNo));
210 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
211 O << (int)MI->getOperand(OpNo).getImm()*4;
213 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
214 O << "\"L" << getFunctionNumber() << "$pb\"\n";
215 O << "\"L" << getFunctionNumber() << "$pb\":";
217 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
218 if (MI->getOperand(OpNo).isImm()) {
219 printS16ImmOperand(MI, OpNo);
221 if (Subtarget.isDarwin()) O << "ha16(";
222 printOp(MI->getOperand(OpNo));
223 if (TM.getRelocationModel() == Reloc::PIC_)
224 O << "-\"L" << getFunctionNumber() << "$pb\"";
225 if (Subtarget.isDarwin())
231 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
232 if (MI->getOperand(OpNo).isImm()) {
233 printS16ImmOperand(MI, OpNo);
235 if (Subtarget.isDarwin()) O << "lo16(";
236 printOp(MI->getOperand(OpNo));
237 if (TM.getRelocationModel() == Reloc::PIC_)
238 O << "-\"L" << getFunctionNumber() << "$pb\"";
239 if (Subtarget.isDarwin())
245 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
246 unsigned CCReg = MI->getOperand(OpNo).getReg();
247 unsigned RegNo = enumRegToMachineReg(CCReg);
248 O << (0x80 >> RegNo);
250 // The new addressing mode printers.
251 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
252 printSymbolLo(MI, OpNo);
254 if (MI->getOperand(OpNo+1).isReg() &&
255 MI->getOperand(OpNo+1).getReg() == PPC::R0)
258 printOperand(MI, OpNo+1);
261 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
262 if (MI->getOperand(OpNo).isImm())
263 printS16X4ImmOperand(MI, OpNo);
265 printSymbolLo(MI, OpNo);
267 if (MI->getOperand(OpNo+1).isReg() &&
268 MI->getOperand(OpNo+1).getReg() == PPC::R0)
271 printOperand(MI, OpNo+1);
275 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
276 // When used as the base register, r0 reads constant zero rather than
277 // the value contained in the register. For this reason, the darwin
278 // assembler requires that we print r0 as 0 (no r) when used as the base.
279 const MachineOperand &MO = MI->getOperand(OpNo);
280 printRegister(MO, true);
282 printOperand(MI, OpNo+1);
285 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
286 const char *Modifier);
288 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
289 virtual bool doFinalization(Module &M) = 0;
291 virtual void EmitExternalGlobal(const GlobalVariable *GV);
294 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
295 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
297 explicit PPCLinuxAsmPrinter(raw_ostream &O, PPCTargetMachine &TM,
298 const TargetAsmInfo *T, bool V)
299 : PPCAsmPrinter(O, TM, T, V){}
301 virtual const char *getPassName() const {
302 return "Linux PPC Assembly Printer";
305 bool runOnMachineFunction(MachineFunction &F);
306 bool doFinalization(Module &M);
308 void getAnalysisUsage(AnalysisUsage &AU) const {
309 AU.setPreservesAll();
310 AU.addRequired<MachineModuleInfo>();
311 AU.addRequired<DwarfWriter>();
312 PPCAsmPrinter::getAnalysisUsage(AU);
315 void printModuleLevelGV(const GlobalVariable* GVar);
318 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
320 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
323 explicit PPCDarwinAsmPrinter(raw_ostream &O, PPCTargetMachine &TM,
324 const TargetAsmInfo *T, bool V)
325 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
327 virtual const char *getPassName() const {
328 return "Darwin PPC Assembly Printer";
331 bool runOnMachineFunction(MachineFunction &F);
332 bool doInitialization(Module &M);
333 bool doFinalization(Module &M);
335 void getAnalysisUsage(AnalysisUsage &AU) const {
336 AU.setPreservesAll();
337 AU.addRequired<MachineModuleInfo>();
338 AU.addRequired<DwarfWriter>();
339 PPCAsmPrinter::getAnalysisUsage(AU);
342 void printModuleLevelGV(const GlobalVariable* GVar);
344 } // end of anonymous namespace
346 // Include the auto-generated portion of the assembly writer
347 #include "PPCGenAsmWriter.inc"
349 void PPCAsmPrinter::printOp(const MachineOperand &MO) {
350 switch (MO.getType()) {
351 case MachineOperand::MO_Immediate:
352 LLVM_UNREACHABLE("printOp() does not handle immediate values");
354 case MachineOperand::MO_MachineBasicBlock:
355 printBasicBlockLabel(MO.getMBB());
357 case MachineOperand::MO_JumpTableIndex:
358 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
359 << '_' << MO.getIndex();
360 // FIXME: PIC relocation model
362 case MachineOperand::MO_ConstantPoolIndex:
363 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
364 << '_' << MO.getIndex();
366 case MachineOperand::MO_ExternalSymbol:
367 // Computing the address of an external symbol, not calling it.
368 if (TM.getRelocationModel() != Reloc::Static) {
369 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName();
370 GVStubs.insert(Name);
371 printSuffixedName(Name, "$non_lazy_ptr");
374 O << TAI->getGlobalPrefix() << MO.getSymbolName();
376 case MachineOperand::MO_GlobalAddress: {
377 // Computing the address of a global symbol, not calling it.
378 GlobalValue *GV = MO.getGlobal();
379 std::string Name = Mang->getValueName(GV);
381 // External or weakly linked global variables need non-lazily-resolved stubs
382 if (TM.getRelocationModel() != Reloc::Static) {
383 if (GV->isDeclaration() || GV->isWeakForLinker()) {
384 if (GV->hasHiddenVisibility()) {
385 if (GV->isDeclaration() || GV->hasCommonLinkage() ||
386 GV->hasAvailableExternallyLinkage()) {
387 HiddenGVStubs.insert(Name);
388 printSuffixedName(Name, "$non_lazy_ptr");
393 GVStubs.insert(Name);
394 printSuffixedName(Name, "$non_lazy_ptr");
401 printOffset(MO.getOffset());
406 O << "<unknown operand type: " << MO.getType() << ">";
411 /// EmitExternalGlobal - In this case we need to use the indirect symbol.
413 void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
415 getGlobalLinkName(GV, Name);
416 if (TM.getRelocationModel() != Reloc::Static) {
417 if (GV->hasHiddenVisibility())
418 HiddenGVStubs.insert(Name);
420 GVStubs.insert(Name);
421 printSuffixedName(Name, "$non_lazy_ptr");
427 /// PrintAsmOperand - Print out an operand for an inline asm expression.
429 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
431 const char *ExtraCode) {
432 // Does this asm operand have a single letter operand modifier?
433 if (ExtraCode && ExtraCode[0]) {
434 if (ExtraCode[1] != 0) return true; // Unknown modifier.
436 switch (ExtraCode[0]) {
437 default: return true; // Unknown modifier.
438 case 'c': // Don't print "$" before a global var name or constant.
439 // PPC never has a prefix.
440 printOperand(MI, OpNo);
442 case 'L': // Write second word of DImode reference.
443 // Verify that this operand has two consecutive registers.
444 if (!MI->getOperand(OpNo).isReg() ||
445 OpNo+1 == MI->getNumOperands() ||
446 !MI->getOperand(OpNo+1).isReg())
448 ++OpNo; // Return the high-part.
451 // Write 'i' if an integer constant, otherwise nothing. Used to print
453 if (MI->getOperand(OpNo).isImm())
459 printOperand(MI, OpNo);
463 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
465 const char *ExtraCode) {
466 if (ExtraCode && ExtraCode[0])
467 return true; // Unknown modifier.
468 if (MI->getOperand(OpNo).isReg())
469 printMemRegReg(MI, OpNo);
471 printMemRegImm(MI, OpNo);
475 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
476 const char *Modifier) {
477 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
478 unsigned Code = MI->getOperand(OpNo).getImm();
479 if (!strcmp(Modifier, "cc")) {
480 switch ((PPC::Predicate)Code) {
481 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
482 case PPC::PRED_LT: O << "lt"; return;
483 case PPC::PRED_LE: O << "le"; return;
484 case PPC::PRED_EQ: O << "eq"; return;
485 case PPC::PRED_GE: O << "ge"; return;
486 case PPC::PRED_GT: O << "gt"; return;
487 case PPC::PRED_NE: O << "ne"; return;
488 case PPC::PRED_UN: O << "un"; return;
489 case PPC::PRED_NU: O << "nu"; return;
493 assert(!strcmp(Modifier, "reg") &&
494 "Need to specify 'cc' or 'reg' as predicate op modifier!");
495 // Don't print the register for 'always'.
496 if (Code == PPC::PRED_ALWAYS) return;
497 printOperand(MI, OpNo+1);
502 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
503 /// the current output stream.
505 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
508 // Check for slwi/srwi mnemonics.
509 if (MI->getOpcode() == PPC::RLWINM) {
510 bool FoundMnemonic = false;
511 unsigned char SH = MI->getOperand(2).getImm();
512 unsigned char MB = MI->getOperand(3).getImm();
513 unsigned char ME = MI->getOperand(4).getImm();
514 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
515 O << "\tslwi "; FoundMnemonic = true;
517 if (SH <= 31 && MB == (32-SH) && ME == 31) {
518 O << "\tsrwi "; FoundMnemonic = true;
525 O << ", " << (unsigned int)SH << '\n';
528 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
529 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
537 } else if (MI->getOpcode() == PPC::RLDICR) {
538 unsigned char SH = MI->getOperand(2).getImm();
539 unsigned char ME = MI->getOperand(3).getImm();
540 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
546 O << ", " << (unsigned int)SH << '\n';
551 if (printInstruction(MI))
552 return; // Printer was automatically generated
554 LLVM_UNREACHABLE("Unhandled instruction in asm writer!");
557 /// runOnMachineFunction - This uses the printMachineInstruction()
558 /// method to print assembly for each instruction.
560 bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
563 SetupMachineFunction(MF);
566 // Print out constants referenced by the function
567 EmitConstantPool(MF.getConstantPool());
569 // Print out labels for the function.
570 const Function *F = MF.getFunction();
571 SwitchToSection(TAI->SectionForGlobal(F));
573 switch (F->getLinkage()) {
574 default: assert(0 && "Unknown linkage type!");
575 case Function::PrivateLinkage:
576 case Function::InternalLinkage: // Symbols default to internal.
578 case Function::ExternalLinkage:
579 O << "\t.global\t" << CurrentFnName << '\n'
580 << "\t.type\t" << CurrentFnName << ", @function\n";
582 case Function::WeakAnyLinkage:
583 case Function::WeakODRLinkage:
584 case Function::LinkOnceAnyLinkage:
585 case Function::LinkOnceODRLinkage:
586 O << "\t.global\t" << CurrentFnName << '\n';
587 O << "\t.weak\t" << CurrentFnName << '\n';
591 printVisibility(CurrentFnName, F->getVisibility());
593 EmitAlignment(MF.getAlignment(), F);
594 O << CurrentFnName << ":\n";
596 // Emit pre-function debug information.
597 DW->BeginFunction(&MF);
599 // Print out code for the function.
600 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
602 // Print a label for the basic block.
603 if (I != MF.begin()) {
604 printBasicBlockLabel(I, true, true);
607 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
609 // Print the assembly for the instruction.
610 printMachineInstruction(II);
614 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
616 // Print out jump tables referenced by the function.
617 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
619 SwitchToSection(TAI->SectionForGlobal(F));
621 // Emit post-function debug information.
622 DW->EndFunction(&MF);
626 // We didn't modify anything.
630 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
631 /// Don't print things like \\n or \\0.
632 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
633 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
639 void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
640 const TargetData *TD = TM.getTargetData();
642 if (!GVar->hasInitializer())
643 return; // External global require no code
645 // Check to see if this is a special global used by LLVM, if so, emit it.
646 if (EmitSpecialLLVMGlobal(GVar))
649 std::string name = Mang->getValueName(GVar);
651 printVisibility(name, GVar->getVisibility());
653 Constant *C = GVar->getInitializer();
654 if (isa<MDNode>(C) || isa<MDString>(C))
656 const Type *Type = C->getType();
657 unsigned Size = TD->getTypeAllocSize(Type);
658 unsigned Align = TD->getPreferredAlignmentLog(GVar);
660 SwitchToSection(TAI->SectionForGlobal(GVar));
662 if (C->isNullValue() && /* FIXME: Verify correct */
663 !GVar->hasSection() &&
664 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
665 GVar->isWeakForLinker())) {
666 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
668 if (GVar->hasExternalLinkage()) {
669 O << "\t.global " << name << '\n';
670 O << "\t.type " << name << ", @object\n";
672 O << "\t.zero " << Size << '\n';
673 } else if (GVar->hasLocalLinkage()) {
674 O << TAI->getLCOMMDirective() << name << ',' << Size;
676 O << ".comm " << name << ',' << Size;
679 O << "\t\t" << TAI->getCommentString() << " '";
680 PrintUnmangledNameSafely(GVar, O);
687 switch (GVar->getLinkage()) {
688 case GlobalValue::LinkOnceAnyLinkage:
689 case GlobalValue::LinkOnceODRLinkage:
690 case GlobalValue::WeakAnyLinkage:
691 case GlobalValue::WeakODRLinkage:
692 case GlobalValue::CommonLinkage:
693 O << "\t.global " << name << '\n'
694 << "\t.type " << name << ", @object\n"
695 << "\t.weak " << name << '\n';
697 case GlobalValue::AppendingLinkage:
698 // FIXME: appending linkage variables should go into a section of
699 // their name or something. For now, just emit them as external.
700 case GlobalValue::ExternalLinkage:
701 // If external or appending, declare as a global symbol
702 O << "\t.global " << name << '\n'
703 << "\t.type " << name << ", @object\n";
705 case GlobalValue::InternalLinkage:
706 case GlobalValue::PrivateLinkage:
709 LLVM_UNREACHABLE("Unknown linkage type!");
712 EmitAlignment(Align, GVar);
715 O << "\t\t\t\t" << TAI->getCommentString() << " '";
716 PrintUnmangledNameSafely(GVar, O);
721 EmitGlobalConstant(C);
725 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
726 // Print out module-level global variables here.
727 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
729 printModuleLevelGV(I);
731 return AsmPrinter::doFinalization(M);
734 /// runOnMachineFunction - This uses the printMachineInstruction()
735 /// method to print assembly for each instruction.
737 bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
740 SetupMachineFunction(MF);
743 // Print out constants referenced by the function
744 EmitConstantPool(MF.getConstantPool());
746 // Print out labels for the function.
747 const Function *F = MF.getFunction();
748 SwitchToSection(TAI->SectionForGlobal(F));
750 switch (F->getLinkage()) {
751 default: assert(0 && "Unknown linkage type!");
752 case Function::PrivateLinkage:
753 case Function::InternalLinkage: // Symbols default to internal.
755 case Function::ExternalLinkage:
756 O << "\t.globl\t" << CurrentFnName << '\n';
758 case Function::WeakAnyLinkage:
759 case Function::WeakODRLinkage:
760 case Function::LinkOnceAnyLinkage:
761 case Function::LinkOnceODRLinkage:
762 O << "\t.globl\t" << CurrentFnName << '\n';
763 O << "\t.weak_definition\t" << CurrentFnName << '\n';
767 printVisibility(CurrentFnName, F->getVisibility());
769 EmitAlignment(MF.getAlignment(), F);
770 O << CurrentFnName << ":\n";
772 // Emit pre-function debug information.
773 DW->BeginFunction(&MF);
775 // If the function is empty, then we need to emit *something*. Otherwise, the
776 // function's label might be associated with something that it wasn't meant to
777 // be associated with. We emit a noop in this situation.
778 MachineFunction::iterator I = MF.begin();
780 if (++I == MF.end() && MF.front().empty())
783 // Print out code for the function.
784 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
786 // Print a label for the basic block.
787 if (I != MF.begin()) {
788 printBasicBlockLabel(I, true, true, VerboseAsm);
791 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
793 // Print the assembly for the instruction.
794 printMachineInstruction(II);
798 // Print out jump tables referenced by the function.
799 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
801 // Emit post-function debug information.
802 DW->EndFunction(&MF);
804 // We didn't modify anything.
809 bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
810 static const char *const CPUDirectives[] = {
822 unsigned Directive = Subtarget.getDarwinDirective();
823 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
824 Directive = PPC::DIR_970;
825 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
826 Directive = PPC::DIR_7400;
827 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
828 Directive = PPC::DIR_64;
829 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
830 O << "\t.machine " << CPUDirectives[Directive] << '\n';
832 bool Result = AsmPrinter::doInitialization(M);
835 // Prime text sections so they are adjacent. This reduces the likelihood a
836 // large data or debug section causes a branch to exceed 16M limit.
837 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
838 "pure_instructions");
839 if (TM.getRelocationModel() == Reloc::PIC_) {
840 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
841 "pure_instructions,32");
842 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
843 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
844 "pure_instructions,16");
846 SwitchToSection(TAI->getTextSection());
851 void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
852 const TargetData *TD = TM.getTargetData();
854 if (!GVar->hasInitializer())
855 return; // External global require no code
857 // Check to see if this is a special global used by LLVM, if so, emit it.
858 if (EmitSpecialLLVMGlobal(GVar)) {
859 if (TM.getRelocationModel() == Reloc::Static) {
860 if (GVar->getName() == "llvm.global_ctors")
861 O << ".reference .constructors_used\n";
862 else if (GVar->getName() == "llvm.global_dtors")
863 O << ".reference .destructors_used\n";
868 std::string name = Mang->getValueName(GVar);
870 printVisibility(name, GVar->getVisibility());
872 Constant *C = GVar->getInitializer();
873 const Type *Type = C->getType();
874 unsigned Size = TD->getTypeAllocSize(Type);
875 unsigned Align = TD->getPreferredAlignmentLog(GVar);
877 SwitchToSection(TAI->SectionForGlobal(GVar));
879 if (C->isNullValue() && /* FIXME: Verify correct */
880 !GVar->hasSection() &&
881 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
882 GVar->isWeakForLinker()) &&
883 TAI->SectionKindForGlobal(GVar) != SectionKind::RODataMergeStr) {
884 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
886 if (GVar->hasExternalLinkage()) {
887 O << "\t.globl " << name << '\n';
888 O << "\t.zerofill __DATA, __common, " << name << ", "
889 << Size << ", " << Align;
890 } else if (GVar->hasLocalLinkage()) {
891 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
892 } else if (!GVar->hasCommonLinkage()) {
893 O << "\t.globl " << name << '\n'
894 << TAI->getWeakDefDirective() << name << '\n';
895 EmitAlignment(Align, GVar);
898 O << "\t\t\t\t" << TAI->getCommentString() << " ";
899 PrintUnmangledNameSafely(GVar, O);
902 EmitGlobalConstant(C);
905 O << ".comm " << name << ',' << Size;
906 // Darwin 9 and above support aligned common data.
907 if (Subtarget.isDarwin9())
911 O << "\t\t" << TAI->getCommentString() << " '";
912 PrintUnmangledNameSafely(GVar, O);
919 switch (GVar->getLinkage()) {
920 case GlobalValue::LinkOnceAnyLinkage:
921 case GlobalValue::LinkOnceODRLinkage:
922 case GlobalValue::WeakAnyLinkage:
923 case GlobalValue::WeakODRLinkage:
924 case GlobalValue::CommonLinkage:
925 O << "\t.globl " << name << '\n'
926 << "\t.weak_definition " << name << '\n';
928 case GlobalValue::AppendingLinkage:
929 // FIXME: appending linkage variables should go into a section of
930 // their name or something. For now, just emit them as external.
931 case GlobalValue::ExternalLinkage:
932 // If external or appending, declare as a global symbol
933 O << "\t.globl " << name << '\n';
935 case GlobalValue::InternalLinkage:
936 case GlobalValue::PrivateLinkage:
939 LLVM_UNREACHABLE("Unknown linkage type!");
942 EmitAlignment(Align, GVar);
945 O << "\t\t\t\t" << TAI->getCommentString() << " '";
946 PrintUnmangledNameSafely(GVar, O);
951 EmitGlobalConstant(C);
955 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
956 const TargetData *TD = TM.getTargetData();
958 // Print out module-level global variables here.
959 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
961 printModuleLevelGV(I);
963 bool isPPC64 = TD->getPointerSizeInBits() == 64;
965 // Output stubs for dynamically-linked functions
966 if (TM.getRelocationModel() == Reloc::PIC_) {
967 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
969 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
970 "pure_instructions,32");
972 const char *p = i->getKeyData();
973 bool hasQuote = p[0]=='\"';
974 printSuffixedName(p, "$stub");
976 O << "\t.indirect_symbol " << p << '\n';
980 O << "\"L0$" << &p[1];
985 O << "\"L0$" << &p[1];
990 O << "\taddis r11,r11,ha16(";
991 printSuffixedName(p, "$lazy_ptr");
994 O << "\"L0$" << &p[1];
1000 O << "\tldu r12,lo16(";
1002 O << "\tlwzu r12,lo16(";
1003 printSuffixedName(p, "$lazy_ptr");
1006 O << "\"L0$" << &p[1];
1010 O << "\tmtctr r12\n";
1012 SwitchToDataSection(".lazy_symbol_pointer");
1013 printSuffixedName(p, "$lazy_ptr");
1015 O << "\t.indirect_symbol " << p << '\n';
1017 O << "\t.quad dyld_stub_binding_helper\n";
1019 O << "\t.long dyld_stub_binding_helper\n";
1022 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1024 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1025 "pure_instructions,16");
1027 const char *p = i->getKeyData();
1028 printSuffixedName(p, "$stub");
1030 O << "\t.indirect_symbol " << p << '\n';
1031 O << "\tlis r11,ha16(";
1032 printSuffixedName(p, "$lazy_ptr");
1035 O << "\tldu r12,lo16(";
1037 O << "\tlwzu r12,lo16(";
1038 printSuffixedName(p, "$lazy_ptr");
1040 O << "\tmtctr r12\n";
1042 SwitchToDataSection(".lazy_symbol_pointer");
1043 printSuffixedName(p, "$lazy_ptr");
1045 O << "\t.indirect_symbol " << p << '\n';
1047 O << "\t.quad dyld_stub_binding_helper\n";
1049 O << "\t.long dyld_stub_binding_helper\n";
1055 if (TAI->doesSupportExceptionHandling() && MMI) {
1056 // Add the (possibly multiple) personalities to the set of global values.
1057 // Only referenced functions get into the Personalities list.
1058 const std::vector<Function *> &Personalities = MMI->getPersonalities();
1059 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1060 E = Personalities.end(); I != E; ++I)
1061 if (*I) GVStubs.insert("_" + (*I)->getName());
1064 // Output stubs for external and common global variables.
1065 if (!GVStubs.empty()) {
1066 SwitchToDataSection(".non_lazy_symbol_pointer");
1067 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1069 std::string p = i->getKeyData();
1070 printSuffixedName(p, "$non_lazy_ptr");
1072 O << "\t.indirect_symbol " << p << '\n';
1074 O << "\t.quad\t0\n";
1076 O << "\t.long\t0\n";
1080 if (!HiddenGVStubs.empty()) {
1081 SwitchToSection(TAI->getDataSection());
1082 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1084 std::string p = i->getKeyData();
1085 EmitAlignment(isPPC64 ? 3 : 2);
1086 printSuffixedName(p, "$non_lazy_ptr");
1096 // Funny Darwin hack: This flag tells the linker that no global symbols
1097 // contain code that falls through to other global symbols (e.g. the obvious
1098 // implementation of multiple entry points). If this doesn't occur, the
1099 // linker can safely perform dead code stripping. Since LLVM never generates
1100 // code that does this, it is always safe to set.
1101 O << "\t.subsections_via_symbols\n";
1103 return AsmPrinter::doFinalization(M);
1108 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1109 /// for a MachineFunction to the given output stream, in a format that the
1110 /// Darwin assembler can deal with.
1112 FunctionPass *llvm::createPPCAsmPrinterPass(raw_ostream &o,
1113 PPCTargetMachine &tm,
1115 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1117 if (Subtarget->isDarwin()) {
1118 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
1120 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
1125 static struct Register {
1127 PPCTargetMachine::registerAsmPrinter(createPPCAsmPrinterPass);
1132 extern "C" int PowerPCAsmPrinterForceLink;
1133 int PowerPCAsmPrinterForceLink = 0;
1135 // Force static initialization.
1136 extern "C" void LLVMInitializePowerPCAsmPrinter() { }