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/FormattedStream.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/Target/TargetRegistry.h"
47 #include "llvm/ADT/Statistic.h"
48 #include "llvm/ADT/StringExtras.h"
49 #include "llvm/ADT/StringSet.h"
52 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
55 class VISIBILITY_HIDDEN PPCAsmPrinter
: public AsmPrinter
{
58 std::string Stub
, LazyPtr
, AnonSymbol
;
62 void Init(const GlobalValue
*GV
, Mangler
*Mang
) {
63 // Already initialized.
64 if (!Stub
.empty()) return;
65 Stub
= Mang
->getMangledName(GV
, "$stub", true);
66 LazyPtr
= Mang
->getMangledName(GV
, "$lazy_ptr", true);
67 AnonSymbol
= Mang
->getMangledName(GV
, "$stub$tmp", true);
70 void Init(const std::string
&GV
, Mangler
*Mang
) {
71 // Already initialized.
72 if (!Stub
.empty()) return;
73 Stub
= Mang
->makeNameProper(GV
+"$stub", true);
74 LazyPtr
= Mang
->makeNameProper(GV
+"$lazy_ptr", true);
75 AnonSymbol
= Mang
->makeNameProper(GV
+"$stub$tmp", true);
79 StringMap
<FnStubInfo
> FnStubs
;
80 StringMap
<std::string
> GVStubs
, HiddenGVStubs
;
81 const PPCSubtarget
&Subtarget
;
83 explicit PPCAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
84 const TargetAsmInfo
*T
, bool V
)
85 : AsmPrinter(O
, TM
, T
, V
),
86 Subtarget(TM
.getSubtarget
<PPCSubtarget
>()) {}
88 virtual const char *getPassName() const {
89 return "PowerPC Assembly Printer";
92 PPCTargetMachine
&getTM() {
93 return static_cast<PPCTargetMachine
&>(TM
);
96 unsigned enumRegToMachineReg(unsigned enumReg
) {
98 default: llvm_unreachable("Unhandled register!");
99 case PPC::CR0
: return 0;
100 case PPC::CR1
: return 1;
101 case PPC::CR2
: return 2;
102 case PPC::CR3
: return 3;
103 case PPC::CR4
: return 4;
104 case PPC::CR5
: return 5;
105 case PPC::CR6
: return 6;
106 case PPC::CR7
: return 7;
111 /// printInstruction - This method is automatically generated by tablegen
112 /// from the instruction set description. This method returns true if the
113 /// machine instruction was sufficiently described to print it, otherwise it
115 bool printInstruction(const MachineInstr
*MI
);
117 void printMachineInstruction(const MachineInstr
*MI
);
118 void printOp(const MachineOperand
&MO
);
120 /// stripRegisterPrefix - This method strips the character prefix from a
121 /// register name so that only the number is left. Used by for linux asm.
122 const char *stripRegisterPrefix(const char *RegName
) {
123 switch (RegName
[0]) {
126 case 'v': return RegName
+ 1;
127 case 'c': if (RegName
[1] == 'r') return RegName
+ 2;
133 /// printRegister - Print register according to target requirements.
135 void printRegister(const MachineOperand
&MO
, bool R0AsZero
) {
136 unsigned RegNo
= MO
.getReg();
137 assert(TargetRegisterInfo::isPhysicalRegister(RegNo
) && "Not physreg??");
139 // If we should use 0 for R0.
140 if (R0AsZero
&& RegNo
== PPC::R0
) {
145 const char *RegName
= TM
.getRegisterInfo()->get(RegNo
).AsmName
;
146 // Linux assembler (Others?) does not take register mnemonics.
147 // FIXME - What about special registers used in mfspr/mtspr?
148 if (!Subtarget
.isDarwin()) RegName
= stripRegisterPrefix(RegName
);
152 void printOperand(const MachineInstr
*MI
, unsigned OpNo
) {
153 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
155 printRegister(MO
, false);
156 } else if (MO
.isImm()) {
163 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
164 unsigned AsmVariant
, const char *ExtraCode
);
165 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
166 unsigned AsmVariant
, const char *ExtraCode
);
169 void printS5ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
170 char value
= MI
->getOperand(OpNo
).getImm();
171 value
= (value
<< (32-5)) >> (32-5);
174 void printU5ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
175 unsigned char value
= MI
->getOperand(OpNo
).getImm();
176 assert(value
<= 31 && "Invalid u5imm argument!");
177 O
<< (unsigned int)value
;
179 void printU6ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
180 unsigned char value
= MI
->getOperand(OpNo
).getImm();
181 assert(value
<= 63 && "Invalid u6imm argument!");
182 O
<< (unsigned int)value
;
184 void printS16ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
185 O
<< (short)MI
->getOperand(OpNo
).getImm();
187 void printU16ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
188 O
<< (unsigned short)MI
->getOperand(OpNo
).getImm();
190 void printS16X4ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
191 if (MI
->getOperand(OpNo
).isImm()) {
192 O
<< (short)(MI
->getOperand(OpNo
).getImm()*4);
195 printOp(MI
->getOperand(OpNo
));
196 if (TM
.getRelocationModel() == Reloc::PIC_
)
197 O
<< "-\"L" << getFunctionNumber() << "$pb\")";
202 void printBranchOperand(const MachineInstr
*MI
, unsigned OpNo
) {
203 // Branches can take an immediate operand. This is used by the branch
204 // selection pass to print $+8, an eight byte displacement from the PC.
205 if (MI
->getOperand(OpNo
).isImm()) {
206 O
<< "$+" << MI
->getOperand(OpNo
).getImm()*4;
208 printOp(MI
->getOperand(OpNo
));
211 void printCallOperand(const MachineInstr
*MI
, unsigned OpNo
) {
212 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
213 if (TM
.getRelocationModel() != Reloc::Static
) {
214 if (MO
.getType() == MachineOperand::MO_GlobalAddress
) {
215 GlobalValue
*GV
= MO
.getGlobal();
216 if (GV
->isDeclaration() || GV
->isWeakForLinker()) {
217 // Dynamically-resolved functions need a stub for the function.
218 FnStubInfo
&FnInfo
= FnStubs
[Mang
->getMangledName(GV
)];
219 FnInfo
.Init(GV
, Mang
);
224 if (MO
.getType() == MachineOperand::MO_ExternalSymbol
) {
225 FnStubInfo
&FnInfo
=FnStubs
[Mang
->makeNameProper(MO
.getSymbolName())];
226 FnInfo
.Init(MO
.getSymbolName(), Mang
);
232 printOp(MI
->getOperand(OpNo
));
234 void printAbsAddrOperand(const MachineInstr
*MI
, unsigned OpNo
) {
235 O
<< (int)MI
->getOperand(OpNo
).getImm()*4;
237 void printPICLabel(const MachineInstr
*MI
, unsigned OpNo
) {
238 O
<< "\"L" << getFunctionNumber() << "$pb\"\n";
239 O
<< "\"L" << getFunctionNumber() << "$pb\":";
241 void printSymbolHi(const MachineInstr
*MI
, unsigned OpNo
) {
242 if (MI
->getOperand(OpNo
).isImm()) {
243 printS16ImmOperand(MI
, OpNo
);
245 if (Subtarget
.isDarwin()) O
<< "ha16(";
246 printOp(MI
->getOperand(OpNo
));
247 if (TM
.getRelocationModel() == Reloc::PIC_
)
248 O
<< "-\"L" << getFunctionNumber() << "$pb\"";
249 if (Subtarget
.isDarwin())
255 void printSymbolLo(const MachineInstr
*MI
, unsigned OpNo
) {
256 if (MI
->getOperand(OpNo
).isImm()) {
257 printS16ImmOperand(MI
, OpNo
);
259 if (Subtarget
.isDarwin()) O
<< "lo16(";
260 printOp(MI
->getOperand(OpNo
));
261 if (TM
.getRelocationModel() == Reloc::PIC_
)
262 O
<< "-\"L" << getFunctionNumber() << "$pb\"";
263 if (Subtarget
.isDarwin())
269 void printcrbitm(const MachineInstr
*MI
, unsigned OpNo
) {
270 unsigned CCReg
= MI
->getOperand(OpNo
).getReg();
271 unsigned RegNo
= enumRegToMachineReg(CCReg
);
272 O
<< (0x80 >> RegNo
);
274 // The new addressing mode printers.
275 void printMemRegImm(const MachineInstr
*MI
, unsigned OpNo
) {
276 printSymbolLo(MI
, OpNo
);
278 if (MI
->getOperand(OpNo
+1).isReg() &&
279 MI
->getOperand(OpNo
+1).getReg() == PPC::R0
)
282 printOperand(MI
, OpNo
+1);
285 void printMemRegImmShifted(const MachineInstr
*MI
, unsigned OpNo
) {
286 if (MI
->getOperand(OpNo
).isImm())
287 printS16X4ImmOperand(MI
, OpNo
);
289 printSymbolLo(MI
, OpNo
);
291 if (MI
->getOperand(OpNo
+1).isReg() &&
292 MI
->getOperand(OpNo
+1).getReg() == PPC::R0
)
295 printOperand(MI
, OpNo
+1);
299 void printMemRegReg(const MachineInstr
*MI
, unsigned OpNo
) {
300 // When used as the base register, r0 reads constant zero rather than
301 // the value contained in the register. For this reason, the darwin
302 // assembler requires that we print r0 as 0 (no r) when used as the base.
303 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
304 printRegister(MO
, true);
306 printOperand(MI
, OpNo
+1);
309 void printPredicateOperand(const MachineInstr
*MI
, unsigned OpNo
,
310 const char *Modifier
);
312 virtual bool runOnMachineFunction(MachineFunction
&F
) = 0;
313 virtual bool doFinalization(Module
&M
) = 0;
315 virtual void EmitExternalGlobal(const GlobalVariable
*GV
);
318 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
319 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter
: public PPCAsmPrinter
{
321 explicit PPCLinuxAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
322 const TargetAsmInfo
*T
, bool V
)
323 : PPCAsmPrinter(O
, TM
, T
, V
){}
325 virtual const char *getPassName() const {
326 return "Linux PPC Assembly Printer";
329 bool runOnMachineFunction(MachineFunction
&F
);
330 bool doFinalization(Module
&M
);
332 void getAnalysisUsage(AnalysisUsage
&AU
) const {
333 AU
.setPreservesAll();
334 AU
.addRequired
<MachineModuleInfo
>();
335 AU
.addRequired
<DwarfWriter
>();
336 PPCAsmPrinter::getAnalysisUsage(AU
);
339 void printModuleLevelGV(const GlobalVariable
* GVar
);
342 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
344 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter
: public PPCAsmPrinter
{
345 formatted_raw_ostream
&OS
;
347 explicit PPCDarwinAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
348 const TargetAsmInfo
*T
, bool V
)
349 : PPCAsmPrinter(O
, TM
, T
, V
), OS(O
) {}
351 virtual const char *getPassName() const {
352 return "Darwin PPC Assembly Printer";
355 bool runOnMachineFunction(MachineFunction
&F
);
356 bool doInitialization(Module
&M
);
357 bool doFinalization(Module
&M
);
359 void getAnalysisUsage(AnalysisUsage
&AU
) const {
360 AU
.setPreservesAll();
361 AU
.addRequired
<MachineModuleInfo
>();
362 AU
.addRequired
<DwarfWriter
>();
363 PPCAsmPrinter::getAnalysisUsage(AU
);
366 void printModuleLevelGV(const GlobalVariable
* GVar
);
368 } // end of anonymous namespace
370 // Include the auto-generated portion of the assembly writer
371 #include "PPCGenAsmWriter.inc"
373 void PPCAsmPrinter::printOp(const MachineOperand
&MO
) {
374 switch (MO
.getType()) {
375 case MachineOperand::MO_Immediate
:
376 llvm_unreachable("printOp() does not handle immediate values");
378 case MachineOperand::MO_MachineBasicBlock
:
379 printBasicBlockLabel(MO
.getMBB());
381 case MachineOperand::MO_JumpTableIndex
:
382 O
<< TAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
383 << '_' << MO
.getIndex();
384 // FIXME: PIC relocation model
386 case MachineOperand::MO_ConstantPoolIndex
:
387 O
<< TAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
388 << '_' << MO
.getIndex();
390 case MachineOperand::MO_ExternalSymbol
: {
391 // Computing the address of an external symbol, not calling it.
392 std::string
Name(TAI
->getGlobalPrefix());
393 Name
+= MO
.getSymbolName();
395 if (TM
.getRelocationModel() != Reloc::Static
) {
396 GVStubs
[Name
] = Name
+"$non_lazy_ptr";
397 Name
+= "$non_lazy_ptr";
402 case MachineOperand::MO_GlobalAddress
: {
403 // Computing the address of a global symbol, not calling it.
404 GlobalValue
*GV
= MO
.getGlobal();
407 // External or weakly linked global variables need non-lazily-resolved stubs
408 if (TM
.getRelocationModel() != Reloc::Static
&&
409 (GV
->isDeclaration() || GV
->isWeakForLinker())) {
410 if (!GV
->hasHiddenVisibility()) {
411 Name
= Mang
->getMangledName(GV
, "$non_lazy_ptr", true);
412 GVStubs
[Mang
->getMangledName(GV
)] = Name
;
413 } else if (GV
->isDeclaration() || GV
->hasCommonLinkage() ||
414 GV
->hasAvailableExternallyLinkage()) {
415 Name
= Mang
->getMangledName(GV
, "$non_lazy_ptr", true);
416 HiddenGVStubs
[Mang
->getMangledName(GV
)] = Name
;
418 Name
= Mang
->getMangledName(GV
);
421 Name
= Mang
->getMangledName(GV
);
425 printOffset(MO
.getOffset());
430 O
<< "<unknown operand type: " << MO
.getType() << ">";
435 /// EmitExternalGlobal - In this case we need to use the indirect symbol.
437 void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable
*GV
) {
440 if (TM
.getRelocationModel() != Reloc::Static
) {
441 Name
= Mang
->getMangledName(GV
, "$non_lazy_ptr", true);
443 Name
= Mang
->getMangledName(GV
);
448 /// PrintAsmOperand - Print out an operand for an inline asm expression.
450 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
452 const char *ExtraCode
) {
453 // Does this asm operand have a single letter operand modifier?
454 if (ExtraCode
&& ExtraCode
[0]) {
455 if (ExtraCode
[1] != 0) return true; // Unknown modifier.
457 switch (ExtraCode
[0]) {
458 default: return true; // Unknown modifier.
459 case 'c': // Don't print "$" before a global var name or constant.
460 // PPC never has a prefix.
461 printOperand(MI
, OpNo
);
463 case 'L': // Write second word of DImode reference.
464 // Verify that this operand has two consecutive registers.
465 if (!MI
->getOperand(OpNo
).isReg() ||
466 OpNo
+1 == MI
->getNumOperands() ||
467 !MI
->getOperand(OpNo
+1).isReg())
469 ++OpNo
; // Return the high-part.
472 // Write 'i' if an integer constant, otherwise nothing. Used to print
474 if (MI
->getOperand(OpNo
).isImm())
480 printOperand(MI
, OpNo
);
484 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
486 const char *ExtraCode
) {
487 if (ExtraCode
&& ExtraCode
[0])
488 return true; // Unknown modifier.
489 if (MI
->getOperand(OpNo
).isReg())
490 printMemRegReg(MI
, OpNo
);
492 printMemRegImm(MI
, OpNo
);
496 void PPCAsmPrinter::printPredicateOperand(const MachineInstr
*MI
, unsigned OpNo
,
497 const char *Modifier
) {
498 assert(Modifier
&& "Must specify 'cc' or 'reg' as predicate op modifier!");
499 unsigned Code
= MI
->getOperand(OpNo
).getImm();
500 if (!strcmp(Modifier
, "cc")) {
501 switch ((PPC::Predicate
)Code
) {
502 case PPC::PRED_ALWAYS
: return; // Don't print anything for always.
503 case PPC::PRED_LT
: O
<< "lt"; return;
504 case PPC::PRED_LE
: O
<< "le"; return;
505 case PPC::PRED_EQ
: O
<< "eq"; return;
506 case PPC::PRED_GE
: O
<< "ge"; return;
507 case PPC::PRED_GT
: O
<< "gt"; return;
508 case PPC::PRED_NE
: O
<< "ne"; return;
509 case PPC::PRED_UN
: O
<< "un"; return;
510 case PPC::PRED_NU
: O
<< "nu"; return;
514 assert(!strcmp(Modifier
, "reg") &&
515 "Need to specify 'cc' or 'reg' as predicate op modifier!");
516 // Don't print the register for 'always'.
517 if (Code
== PPC::PRED_ALWAYS
) return;
518 printOperand(MI
, OpNo
+1);
523 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
524 /// the current output stream.
526 void PPCAsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
529 // Check for slwi/srwi mnemonics.
530 if (MI
->getOpcode() == PPC::RLWINM
) {
531 bool FoundMnemonic
= false;
532 unsigned char SH
= MI
->getOperand(2).getImm();
533 unsigned char MB
= MI
->getOperand(3).getImm();
534 unsigned char ME
= MI
->getOperand(4).getImm();
535 if (SH
<= 31 && MB
== 0 && ME
== (31-SH
)) {
536 O
<< "\tslwi "; FoundMnemonic
= true;
538 if (SH
<= 31 && MB
== (32-SH
) && ME
== 31) {
539 O
<< "\tsrwi "; FoundMnemonic
= true;
546 O
<< ", " << (unsigned int)SH
<< '\n';
549 } else if (MI
->getOpcode() == PPC::OR
|| MI
->getOpcode() == PPC::OR8
) {
550 if (MI
->getOperand(1).getReg() == MI
->getOperand(2).getReg()) {
558 } else if (MI
->getOpcode() == PPC::RLDICR
) {
559 unsigned char SH
= MI
->getOperand(2).getImm();
560 unsigned char ME
= MI
->getOperand(3).getImm();
561 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
567 O
<< ", " << (unsigned int)SH
<< '\n';
572 if (printInstruction(MI
))
573 return; // Printer was automatically generated
575 llvm_unreachable("Unhandled instruction in asm writer!");
578 /// runOnMachineFunction - This uses the printMachineInstruction()
579 /// method to print assembly for each instruction.
581 bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
584 SetupMachineFunction(MF
);
587 // Print out constants referenced by the function
588 EmitConstantPool(MF
.getConstantPool());
590 // Print out labels for the function.
591 const Function
*F
= MF
.getFunction();
592 SwitchToSection(TAI
->SectionForGlobal(F
));
594 switch (F
->getLinkage()) {
595 default: llvm_unreachable("Unknown linkage type!");
596 case Function::PrivateLinkage
:
597 case Function::InternalLinkage
: // Symbols default to internal.
599 case Function::ExternalLinkage
:
600 O
<< "\t.global\t" << CurrentFnName
<< '\n'
601 << "\t.type\t" << CurrentFnName
<< ", @function\n";
603 case Function::WeakAnyLinkage
:
604 case Function::WeakODRLinkage
:
605 case Function::LinkOnceAnyLinkage
:
606 case Function::LinkOnceODRLinkage
:
607 O
<< "\t.global\t" << CurrentFnName
<< '\n';
608 O
<< "\t.weak\t" << CurrentFnName
<< '\n';
612 printVisibility(CurrentFnName
, F
->getVisibility());
614 EmitAlignment(MF
.getAlignment(), F
);
615 O
<< CurrentFnName
<< ":\n";
617 // Emit pre-function debug information.
618 DW
->BeginFunction(&MF
);
620 // Print out code for the function.
621 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
623 // Print a label for the basic block.
624 if (I
!= MF
.begin()) {
625 printBasicBlockLabel(I
, true, true);
628 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
630 // Print the assembly for the instruction.
631 printMachineInstruction(II
);
635 O
<< "\t.size\t" << CurrentFnName
<< ",.-" << CurrentFnName
<< '\n';
637 // Print out jump tables referenced by the function.
638 EmitJumpTableInfo(MF
.getJumpTableInfo(), MF
);
640 SwitchToSection(TAI
->SectionForGlobal(F
));
642 // Emit post-function debug information.
643 DW
->EndFunction(&MF
);
647 // We didn't modify anything.
651 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
652 /// Don't print things like \\n or \\0.
653 static void PrintUnmangledNameSafely(const Value
*V
, formatted_raw_ostream
&OS
) {
654 for (const char *Name
= V
->getNameStart(), *E
= Name
+V
->getNameLen();
660 void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable
* GVar
) {
661 const TargetData
*TD
= TM
.getTargetData();
663 if (!GVar
->hasInitializer())
664 return; // External global require no code
666 // Check to see if this is a special global used by LLVM, if so, emit it.
667 if (EmitSpecialLLVMGlobal(GVar
))
670 std::string name
= Mang
->getMangledName(GVar
);
672 printVisibility(name
, GVar
->getVisibility());
674 Constant
*C
= GVar
->getInitializer();
675 if (isa
<MDNode
>(C
) || isa
<MDString
>(C
))
677 const Type
*Type
= C
->getType();
678 unsigned Size
= TD
->getTypeAllocSize(Type
);
679 unsigned Align
= TD
->getPreferredAlignmentLog(GVar
);
681 SwitchToSection(TAI
->SectionForGlobal(GVar
));
683 if (C
->isNullValue() && /* FIXME: Verify correct */
684 !GVar
->hasSection() &&
685 (GVar
->hasLocalLinkage() || GVar
->hasExternalLinkage() ||
686 GVar
->isWeakForLinker())) {
687 if (Size
== 0) Size
= 1; // .comm Foo, 0 is undefined, avoid it.
689 if (GVar
->hasExternalLinkage()) {
690 O
<< "\t.global " << name
<< '\n';
691 O
<< "\t.type " << name
<< ", @object\n";
693 O
<< "\t.zero " << Size
<< '\n';
694 } else if (GVar
->hasLocalLinkage()) {
695 O
<< TAI
->getLCOMMDirective() << name
<< ',' << Size
;
697 O
<< ".comm " << name
<< ',' << Size
;
700 O
<< "\t\t" << TAI
->getCommentString() << " '";
701 PrintUnmangledNameSafely(GVar
, O
);
708 switch (GVar
->getLinkage()) {
709 case GlobalValue::LinkOnceAnyLinkage
:
710 case GlobalValue::LinkOnceODRLinkage
:
711 case GlobalValue::WeakAnyLinkage
:
712 case GlobalValue::WeakODRLinkage
:
713 case GlobalValue::CommonLinkage
:
714 O
<< "\t.global " << name
<< '\n'
715 << "\t.type " << name
<< ", @object\n"
716 << "\t.weak " << name
<< '\n';
718 case GlobalValue::AppendingLinkage
:
719 // FIXME: appending linkage variables should go into a section of
720 // their name or something. For now, just emit them as external.
721 case GlobalValue::ExternalLinkage
:
722 // If external or appending, declare as a global symbol
723 O
<< "\t.global " << name
<< '\n'
724 << "\t.type " << name
<< ", @object\n";
726 case GlobalValue::InternalLinkage
:
727 case GlobalValue::PrivateLinkage
:
730 llvm_unreachable("Unknown linkage type!");
733 EmitAlignment(Align
, GVar
);
736 O
<< "\t\t\t\t" << TAI
->getCommentString() << " '";
737 PrintUnmangledNameSafely(GVar
, O
);
742 EmitGlobalConstant(C
);
746 bool PPCLinuxAsmPrinter::doFinalization(Module
&M
) {
747 // Print out module-level global variables here.
748 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
750 printModuleLevelGV(I
);
752 return AsmPrinter::doFinalization(M
);
755 /// runOnMachineFunction - This uses the printMachineInstruction()
756 /// method to print assembly for each instruction.
758 bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
761 SetupMachineFunction(MF
);
764 // Print out constants referenced by the function
765 EmitConstantPool(MF
.getConstantPool());
767 // Print out labels for the function.
768 const Function
*F
= MF
.getFunction();
769 SwitchToSection(TAI
->SectionForGlobal(F
));
771 switch (F
->getLinkage()) {
772 default: llvm_unreachable("Unknown linkage type!");
773 case Function::PrivateLinkage
:
774 case Function::InternalLinkage
: // Symbols default to internal.
776 case Function::ExternalLinkage
:
777 O
<< "\t.globl\t" << CurrentFnName
<< '\n';
779 case Function::WeakAnyLinkage
:
780 case Function::WeakODRLinkage
:
781 case Function::LinkOnceAnyLinkage
:
782 case Function::LinkOnceODRLinkage
:
783 O
<< "\t.globl\t" << CurrentFnName
<< '\n';
784 O
<< "\t.weak_definition\t" << CurrentFnName
<< '\n';
788 printVisibility(CurrentFnName
, F
->getVisibility());
790 EmitAlignment(MF
.getAlignment(), F
);
791 O
<< CurrentFnName
<< ":\n";
793 // Emit pre-function debug information.
794 DW
->BeginFunction(&MF
);
796 // If the function is empty, then we need to emit *something*. Otherwise, the
797 // function's label might be associated with something that it wasn't meant to
798 // be associated with. We emit a noop in this situation.
799 MachineFunction::iterator I
= MF
.begin();
801 if (++I
== MF
.end() && MF
.front().empty())
804 // Print out code for the function.
805 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
807 // Print a label for the basic block.
808 if (I
!= MF
.begin()) {
809 printBasicBlockLabel(I
, true, true, VerboseAsm
);
812 for (MachineBasicBlock::const_iterator II
= I
->begin(), IE
= I
->end();
814 // Print the assembly for the instruction.
815 printMachineInstruction(II
);
819 // Print out jump tables referenced by the function.
820 EmitJumpTableInfo(MF
.getJumpTableInfo(), MF
);
822 // Emit post-function debug information.
823 DW
->EndFunction(&MF
);
825 // We didn't modify anything.
830 bool PPCDarwinAsmPrinter::doInitialization(Module
&M
) {
831 static const char *const CPUDirectives
[] = {
843 unsigned Directive
= Subtarget
.getDarwinDirective();
844 if (Subtarget
.isGigaProcessor() && Directive
< PPC::DIR_970
)
845 Directive
= PPC::DIR_970
;
846 if (Subtarget
.hasAltivec() && Directive
< PPC::DIR_7400
)
847 Directive
= PPC::DIR_7400
;
848 if (Subtarget
.isPPC64() && Directive
< PPC::DIR_970
)
849 Directive
= PPC::DIR_64
;
850 assert(Directive
<= PPC::DIR_64
&& "Directive out of range.");
851 O
<< "\t.machine " << CPUDirectives
[Directive
] << '\n';
853 bool Result
= AsmPrinter::doInitialization(M
);
856 // Prime text sections so they are adjacent. This reduces the likelihood a
857 // large data or debug section causes a branch to exceed 16M limit.
858 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
859 "pure_instructions");
860 if (TM
.getRelocationModel() == Reloc::PIC_
) {
861 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
862 "pure_instructions,32");
863 } else if (TM
.getRelocationModel() == Reloc::DynamicNoPIC
) {
864 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
865 "pure_instructions,16");
867 SwitchToSection(TAI
->getTextSection());
872 void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable
* GVar
) {
873 const TargetData
*TD
= TM
.getTargetData();
875 if (!GVar
->hasInitializer())
876 return; // External global require no code
878 // Check to see if this is a special global used by LLVM, if so, emit it.
879 if (EmitSpecialLLVMGlobal(GVar
)) {
880 if (TM
.getRelocationModel() == Reloc::Static
) {
881 if (GVar
->getName() == "llvm.global_ctors")
882 O
<< ".reference .constructors_used\n";
883 else if (GVar
->getName() == "llvm.global_dtors")
884 O
<< ".reference .destructors_used\n";
889 std::string name
= Mang
->getMangledName(GVar
);
890 printVisibility(name
, GVar
->getVisibility());
892 Constant
*C
= GVar
->getInitializer();
893 const Type
*Type
= C
->getType();
894 unsigned Size
= TD
->getTypeAllocSize(Type
);
895 unsigned Align
= TD
->getPreferredAlignmentLog(GVar
);
897 SwitchToSection(TAI
->SectionForGlobal(GVar
));
899 if (C
->isNullValue() && /* FIXME: Verify correct */
900 !GVar
->hasSection() &&
901 (GVar
->hasLocalLinkage() || GVar
->hasExternalLinkage() ||
902 GVar
->isWeakForLinker()) &&
903 TAI
->SectionKindForGlobal(GVar
) != SectionKind::RODataMergeStr
) {
904 if (Size
== 0) Size
= 1; // .comm Foo, 0 is undefined, avoid it.
906 if (GVar
->hasExternalLinkage()) {
907 O
<< "\t.globl " << name
<< '\n';
908 O
<< "\t.zerofill __DATA, __common, " << name
<< ", "
909 << Size
<< ", " << Align
;
910 } else if (GVar
->hasLocalLinkage()) {
911 O
<< TAI
->getLCOMMDirective() << name
<< ',' << Size
<< ',' << Align
;
912 } else if (!GVar
->hasCommonLinkage()) {
913 O
<< "\t.globl " << name
<< '\n'
914 << TAI
->getWeakDefDirective() << name
<< '\n';
915 EmitAlignment(Align
, GVar
);
918 O
<< "\t\t\t\t" << TAI
->getCommentString() << " ";
919 PrintUnmangledNameSafely(GVar
, O
);
922 EmitGlobalConstant(C
);
925 O
<< ".comm " << name
<< ',' << Size
;
926 // Darwin 9 and above support aligned common data.
927 if (Subtarget
.isDarwin9())
931 O
<< "\t\t" << TAI
->getCommentString() << " '";
932 PrintUnmangledNameSafely(GVar
, O
);
939 switch (GVar
->getLinkage()) {
940 case GlobalValue::LinkOnceAnyLinkage
:
941 case GlobalValue::LinkOnceODRLinkage
:
942 case GlobalValue::WeakAnyLinkage
:
943 case GlobalValue::WeakODRLinkage
:
944 case GlobalValue::CommonLinkage
:
945 O
<< "\t.globl " << name
<< '\n'
946 << "\t.weak_definition " << name
<< '\n';
948 case GlobalValue::AppendingLinkage
:
949 // FIXME: appending linkage variables should go into a section of
950 // their name or something. For now, just emit them as external.
951 case GlobalValue::ExternalLinkage
:
952 // If external or appending, declare as a global symbol
953 O
<< "\t.globl " << name
<< '\n';
955 case GlobalValue::InternalLinkage
:
956 case GlobalValue::PrivateLinkage
:
959 llvm_unreachable("Unknown linkage type!");
962 EmitAlignment(Align
, GVar
);
965 O
<< "\t\t\t\t" << TAI
->getCommentString() << " '";
966 PrintUnmangledNameSafely(GVar
, O
);
971 EmitGlobalConstant(C
);
975 bool PPCDarwinAsmPrinter::doFinalization(Module
&M
) {
976 const TargetData
*TD
= TM
.getTargetData();
978 // Print out module-level global variables here.
979 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
981 printModuleLevelGV(I
);
983 bool isPPC64
= TD
->getPointerSizeInBits() == 64;
985 // Output stubs for dynamically-linked functions
986 if (TM
.getRelocationModel() == Reloc::PIC_
&& !FnStubs
.empty()) {
987 for (StringMap
<FnStubInfo
>::iterator I
= FnStubs
.begin(), E
= FnStubs
.end();
989 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
990 "pure_instructions,32");
992 const FnStubInfo
&Info
= I
->second
;
993 O
<< Info
.Stub
<< ":\n";
994 O
<< "\t.indirect_symbol " << I
->getKeyData() << '\n';
996 O
<< "\tbcl 20,31," << Info
.AnonSymbol
<< '\n';
997 O
<< Info
.AnonSymbol
<< ":\n";
999 O
<< "\taddis r11,r11,ha16(" << Info
.LazyPtr
<< "-" << Info
.AnonSymbol
;
1002 O
<< (isPPC64
? "\tldu" : "\tlwzu") << " r12,lo16(";
1003 O
<< Info
.LazyPtr
<< "-" << Info
.AnonSymbol
<< ")(r11)\n";
1004 O
<< "\tmtctr r12\n";
1007 SwitchToDataSection(".lazy_symbol_pointer");
1008 O
<< Info
.LazyPtr
<< ":\n";
1009 O
<< "\t.indirect_symbol " << I
->getKeyData() << '\n';
1010 O
<< (isPPC64
? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1012 } else if (!FnStubs
.empty()) {
1013 for (StringMap
<FnStubInfo
>::iterator I
= FnStubs
.begin(), E
= FnStubs
.end();
1015 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1016 "pure_instructions,16");
1018 const FnStubInfo
&Info
= I
->second
;
1019 O
<< Info
.Stub
<< ":\n";
1020 O
<< "\t.indirect_symbol " << I
->getKeyData() << '\n';
1021 O
<< "\tlis r11,ha16(" << Info
.LazyPtr
<< ")\n";
1022 O
<< (isPPC64
? "\tldu" : "\tlwzu") << " r12,lo16(";
1023 O
<< Info
.LazyPtr
<< ")(r11)\n";
1024 O
<< "\tmtctr r12\n";
1026 SwitchToDataSection(".lazy_symbol_pointer");
1027 O
<< Info
.LazyPtr
<< ":\n";
1028 O
<< "\t.indirect_symbol " << I
->getKeyData() << '\n';
1029 O
<< (isPPC64
? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
1035 if (TAI
->doesSupportExceptionHandling() && MMI
) {
1036 // Add the (possibly multiple) personalities to the set of global values.
1037 // Only referenced functions get into the Personalities list.
1038 const std::vector
<Function
*> &Personalities
= MMI
->getPersonalities();
1039 for (std::vector
<Function
*>::const_iterator I
= Personalities
.begin(),
1040 E
= Personalities
.end(); I
!= E
; ++I
) {
1042 GVStubs
[Mang
->getMangledName(*I
)] =
1043 Mang
->getMangledName(*I
, "$non_lazy_ptr", true);
1047 // Output stubs for external and common global variables.
1048 if (!GVStubs
.empty()) {
1049 SwitchToDataSection(".non_lazy_symbol_pointer");
1050 for (StringMap
<std::string
>::iterator I
= GVStubs
.begin(),
1051 E
= GVStubs
.end(); I
!= E
; ++I
) {
1052 O
<< I
->second
<< ":\n";
1053 O
<< "\t.indirect_symbol " << I
->getKeyData() << '\n';
1054 O
<< (isPPC64
? "\t.quad\t0\n" : "\t.long\t0\n");
1058 if (!HiddenGVStubs
.empty()) {
1059 SwitchToSection(TAI
->getDataSection());
1060 EmitAlignment(isPPC64
? 3 : 2);
1061 for (StringMap
<std::string
>::iterator I
= HiddenGVStubs
.begin(),
1062 E
= HiddenGVStubs
.end(); I
!= E
; ++I
) {
1063 O
<< I
->second
<< ":\n";
1064 O
<< (isPPC64
? "\t.quad\t" : "\t.long\t") << I
->getKeyData() << '\n';
1068 // Funny Darwin hack: This flag tells the linker that no global symbols
1069 // contain code that falls through to other global symbols (e.g. the obvious
1070 // implementation of multiple entry points). If this doesn't occur, the
1071 // linker can safely perform dead code stripping. Since LLVM never generates
1072 // code that does this, it is always safe to set.
1073 O
<< "\t.subsections_via_symbols\n";
1075 return AsmPrinter::doFinalization(M
);
1080 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1081 /// for a MachineFunction to the given output stream, in a format that the
1082 /// Darwin assembler can deal with.
1084 FunctionPass
*llvm::createPPCAsmPrinterPass(formatted_raw_ostream
&o
,
1087 const PPCSubtarget
*Subtarget
= &tm
.getSubtarget
<PPCSubtarget
>();
1089 if (Subtarget
->isDarwin()) {
1090 return new PPCDarwinAsmPrinter(o
, tm
, tm
.getTargetAsmInfo(), verbose
);
1092 return new PPCLinuxAsmPrinter(o
, tm
, tm
.getTargetAsmInfo(), verbose
);
1096 // Force static initialization.
1097 extern "C" void LLVMInitializePowerPCAsmPrinter() {
1098 extern Target ThePPC32Target
;
1099 TargetRegistry::RegisterAsmPrinter(ThePPC32Target
, createPPCAsmPrinterPass
);
1101 extern Target ThePPC64Target
;
1102 TargetRegistry::RegisterAsmPrinter(ThePPC64Target
, createPPCAsmPrinterPass
);