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 "PowerPCTargetMachine.h"
22 #include "PowerPCSubtarget.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/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/ValueTypes.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/ADT/Statistic.h"
39 #include "llvm/ADT/StringExtras.h"
44 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
46 struct PPCAsmPrinter : public AsmPrinter {
47 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
49 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
50 : AsmPrinter(O, TM), LabelNumber(0) {}
52 /// Unique incrementer for label values for referencing Global values.
56 virtual const char *getPassName() const {
57 return "PowerPC Assembly Printer";
60 PowerPCTargetMachine &getTM() {
61 return static_cast<PowerPCTargetMachine&>(TM);
64 unsigned enumRegToMachineReg(unsigned enumReg) {
66 default: assert(0 && "Unhandled register!"); break;
67 case PPC::CR0: return 0;
68 case PPC::CR1: return 1;
69 case PPC::CR2: return 2;
70 case PPC::CR3: return 3;
71 case PPC::CR4: return 4;
72 case PPC::CR5: return 5;
73 case PPC::CR6: return 6;
74 case PPC::CR7: return 7;
79 /// printInstruction - This method is automatically generated by tablegen
80 /// from the instruction set description. This method returns true if the
81 /// machine instruction was sufficiently described to print it, otherwise it
83 bool printInstruction(const MachineInstr *MI);
85 void printMachineInstruction(const MachineInstr *MI);
86 void printOp(const MachineOperand &MO, bool IsCallOp = false);
88 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
89 const MachineOperand &MO = MI->getOperand(OpNo);
90 if (MO.getType() == MachineOperand::MO_MachineRegister) {
91 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
92 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
93 } else if (MO.isImmediate()) {
94 O << MO.getImmedValue();
100 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
102 unsigned char value = MI->getOperand(OpNo).getImmedValue();
103 assert(value <= 31 && "Invalid u5imm argument!");
104 O << (unsigned int)value;
106 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
108 unsigned char value = MI->getOperand(OpNo).getImmedValue();
109 assert(value <= 63 && "Invalid u6imm argument!");
110 O << (unsigned int)value;
112 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
114 O << (short)MI->getOperand(OpNo).getImmedValue();
116 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
118 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
120 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
122 // Branches can take an immediate operand. This is used by the branch
123 // selection pass to print $+8, an eight byte displacement from the PC.
124 if (MI->getOperand(OpNo).isImmediate()) {
125 O << "$+" << MI->getOperand(OpNo).getImmedValue();
127 printOp(MI->getOperand(OpNo),
128 TM.getInstrInfo()->isCall(MI->getOpcode()));
131 void printPICLabel(const MachineInstr *MI, unsigned OpNo,
133 // FIXME: should probably be converted to cout.width and cout.fill
134 O << "\"L0000" << LabelNumber << "$pb\"\n";
135 O << "\"L0000" << LabelNumber << "$pb\":";
137 void printSymbolHi(const MachineInstr *MI, unsigned OpNo,
139 if (MI->getOperand(OpNo).isImmediate()) {
140 printS16ImmOperand(MI, OpNo, VT);
143 printOp(MI->getOperand(OpNo));
145 O << "-\"L0000" << LabelNumber << "$pb\")";
150 void printSymbolLo(const MachineInstr *MI, unsigned OpNo,
152 if (MI->getOperand(OpNo).isImmediate()) {
153 printS16ImmOperand(MI, OpNo, VT);
156 printOp(MI->getOperand(OpNo));
158 O << "-\"L0000" << LabelNumber << "$pb\")";
163 void printcrbitm(const MachineInstr *MI, unsigned OpNo,
165 unsigned CCReg = MI->getOperand(OpNo).getReg();
166 unsigned RegNo = enumRegToMachineReg(CCReg);
167 O << (0x80 >> RegNo);
170 virtual void printConstantPool(MachineConstantPool *MCP) = 0;
171 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
172 virtual bool doFinalization(Module &M) = 0;
175 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
178 struct DarwinAsmPrinter : public PPCAsmPrinter {
180 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
181 : PPCAsmPrinter(O, TM) {
184 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
185 Data64bitsDirective = 0; // we can't emit a 64-bit unit
186 AlignmentIsInBytes = false; // Alignment is by power of 2.
189 virtual const char *getPassName() const {
190 return "Darwin PPC Assembly Printer";
193 void printConstantPool(MachineConstantPool *MCP);
194 bool runOnMachineFunction(MachineFunction &F);
195 bool doInitialization(Module &M);
196 bool doFinalization(Module &M);
199 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
201 struct AIXAsmPrinter : public PPCAsmPrinter {
202 /// Map for labels corresponding to global variables
204 std::map<const GlobalVariable*,std::string> GVToLabelMap;
206 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
207 : PPCAsmPrinter(O, TM) {
210 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
211 Data64bitsDirective = 0; // we can't emit a 64-bit unit
212 AlignmentIsInBytes = false; // Alignment is by power of 2.
215 virtual const char *getPassName() const {
216 return "AIX PPC Assembly Printer";
219 void printConstantPool(MachineConstantPool *MCP);
220 bool runOnMachineFunction(MachineFunction &F);
221 bool doInitialization(Module &M);
222 bool doFinalization(Module &M);
224 } // end of anonymous namespace
226 // SwitchSection - Switch to the specified section of the executable if we are
227 // not already in it!
229 static void SwitchSection(std::ostream &OS, std::string &CurSection,
230 const char *NewSection) {
231 if (CurSection != NewSection) {
232 CurSection = NewSection;
233 if (!CurSection.empty())
234 OS << "\t" << NewSection << "\n";
238 /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
239 /// code for a MachineFunction to the given output stream, in a format that the
240 /// Darwin assembler can deal with.
242 FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
243 return new DarwinAsmPrinter(o, tm);
246 /// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
247 /// for a MachineFunction to the given output stream, in a format that the
248 /// AIX 5L assembler can deal with.
250 FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
251 return new AIXAsmPrinter(o, tm);
254 // Include the auto-generated portion of the assembly writer
255 #include "PPCGenAsmWriter.inc"
257 void PPCAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
258 const MRegisterInfo &RI = *TM.getRegisterInfo();
261 switch (MO.getType()) {
262 case MachineOperand::MO_VirtualRegister:
263 if (Value *V = MO.getVRegValueOrNull()) {
264 O << "<" << V->getName() << ">";
268 case MachineOperand::MO_MachineRegister:
269 case MachineOperand::MO_CCRegister:
270 O << RI.get(MO.getReg()).Name;
273 case MachineOperand::MO_SignExtendedImmed:
274 case MachineOperand::MO_UnextendedImmed:
275 std::cerr << "printOp() does not handle immediate values\n";
279 case MachineOperand::MO_PCRelativeDisp:
280 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
284 case MachineOperand::MO_MachineBasicBlock: {
285 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
286 O << "LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
287 << "_" << MBBOp->getNumber() << "\t; "
288 << MBBOp->getBasicBlock()->getName();
292 case MachineOperand::MO_ConstantPoolIndex:
293 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
296 case MachineOperand::MO_ExternalSymbol:
298 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
299 FnStubs.insert(Name);
300 O << "L" << Name << "$stub";
303 O << GlobalPrefix << MO.getSymbolName();
306 case MachineOperand::MO_GlobalAddress: {
307 GlobalValue *GV = MO.getGlobal();
308 std::string Name = Mang->getValueName(GV);
310 // Dynamically-resolved functions need a stub for the function. Be
311 // wary however not to output $stub for external functions whose addresses
312 // are taken. Those should be emitted as $non_lazy_ptr below.
313 Function *F = dyn_cast<Function>(GV);
314 if (F && IsCallOp && F->isExternal()) {
315 FnStubs.insert(Name);
316 O << "L" << Name << "$stub";
320 // External or weakly linked global variables need non-lazily-resolved stubs
321 if ((GV->isExternal() || GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())){
322 if (GV->hasLinkOnceLinkage())
323 LinkOnceStubs.insert(Name);
325 GVStubs.insert(Name);
326 O << "L" << Name << "$non_lazy_ptr";
330 O << Mang->getValueName(GV);
335 O << "<unknown operand type: " << MO.getType() << ">";
340 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
341 /// the current output stream.
343 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
346 // Check for slwi/srwi mnemonics.
347 if (MI->getOpcode() == PPC::RLWINM) {
348 bool FoundMnemonic = false;
349 unsigned char SH = MI->getOperand(2).getImmedValue();
350 unsigned char MB = MI->getOperand(3).getImmedValue();
351 unsigned char ME = MI->getOperand(4).getImmedValue();
352 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
353 O << "slwi "; FoundMnemonic = true;
355 if (SH <= 31 && MB == (32-SH) && ME == 31) {
356 O << "srwi "; FoundMnemonic = true;
360 printOperand(MI, 0, MVT::i64);
362 printOperand(MI, 1, MVT::i64);
363 O << ", " << (unsigned int)SH << "\n";
368 if (printInstruction(MI))
369 return; // Printer was automatically generated
371 assert(0 && "Unhandled instruction in asm writer!");
376 /// runOnMachineFunction - This uses the printMachineInstruction()
377 /// method to print assembly for each instruction.
379 bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
380 setupMachineFunction(MF);
383 // Print out constants referenced by the function
384 printConstantPool(MF.getConstantPool());
386 // Print out labels for the function.
389 O << "\t.globl\t" << CurrentFnName << "\n";
390 O << CurrentFnName << ":\n";
392 // Print out code for the function.
393 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
395 // Print a label for the basic block.
396 if (I != MF.begin()) {
397 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t";
398 if (!I->getBasicBlock()->getName().empty())
399 O << CommentString << " " << I->getBasicBlock()->getName();
402 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
404 // Print the assembly for the instruction.
406 printMachineInstruction(II);
411 // We didn't modify anything.
415 /// printConstantPool - Print to the current output stream assembly
416 /// representations of the constants in the constant pool MCP. This is
417 /// used to print out constants which have been "spilled to memory" by
418 /// the code generator.
420 void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
421 const std::vector<Constant*> &CP = MCP->getConstants();
422 const TargetData &TD = TM.getTargetData();
424 if (CP.empty()) return;
426 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
428 // FIXME: force doubles to be naturally aligned. We should handle this
429 // more correctly in the future.
430 if (Type::DoubleTy == CP[i]->getType())
433 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
434 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
436 emitGlobalConstant(CP[i]);
440 bool DarwinAsmPrinter::doInitialization(Module &M) {
441 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
442 O << "\t.machine ppc970\n";
443 AsmPrinter::doInitialization(M);
447 bool DarwinAsmPrinter::doFinalization(Module &M) {
448 const TargetData &TD = TM.getTargetData();
449 std::string CurSection;
451 // Print out module-level global variables here.
452 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
453 if (I->hasInitializer()) { // External global require no code
455 std::string name = Mang->getValueName(I);
456 Constant *C = I->getInitializer();
457 unsigned Size = TD.getTypeSize(C->getType());
458 unsigned Align = TD.getTypeAlignmentShift(C->getType());
460 if (C->isNullValue() && /* FIXME: Verify correct */
461 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
462 I->hasLinkOnceLinkage())) {
463 SwitchSection(O, CurSection, ".data");
464 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
465 if (I->hasInternalLinkage())
466 O << ".lcomm " << name << "," << Size << "," << Align;
468 O << ".comm " << name << "," << Size;
469 O << "\t\t; '" << I->getName() << "'\n";
471 switch (I->getLinkage()) {
472 case GlobalValue::LinkOnceLinkage:
473 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
474 << ".weak_definition " << name << '\n'
475 << ".private_extern " << name << '\n'
476 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
477 LinkOnceStubs.insert(name);
479 case GlobalValue::WeakLinkage:
480 O << ".weak_definition " << name << '\n'
481 << ".private_extern " << name << '\n';
483 case GlobalValue::AppendingLinkage:
484 // FIXME: appending linkage variables should go into a section of
485 // their name or something. For now, just emit them as external.
486 case GlobalValue::ExternalLinkage:
487 // If external or appending, declare as a global symbol
488 O << "\t.globl " << name << "\n";
490 case GlobalValue::InternalLinkage:
491 SwitchSection(O, CurSection, ".data");
493 case GlobalValue::GhostLinkage:
494 std::cerr << "Error: unmaterialized (GhostLinkage) function in asm!";
498 emitAlignment(Align);
499 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
500 emitGlobalConstant(C);
504 // Output stubs for dynamically-linked functions
505 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
510 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
512 O << "L" << *i << "$stub:\n";
513 O << "\t.indirect_symbol " << *i << "\n";
515 O << "\tbcl 20,31,L0$" << *i << "\n";
516 O << "L0$" << *i << ":\n";
518 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
520 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
521 O << "\tmtctr r12\n";
524 O << ".lazy_symbol_pointer\n";
525 O << "L" << *i << "$lazy_ptr:\n";
526 O << "\t.indirect_symbol " << *i << "\n";
527 O << "\t.long dyld_stub_binding_helper\n";
529 O << "\t.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16\n";
531 O << "L" << *i << "$stub:\n";
532 O << "\t.indirect_symbol " << *i << "\n";
533 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
534 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
535 O << "\tmtctr r12\n";
537 O << "\t.lazy_symbol_pointer\n";
538 O << "L" << *i << "$lazy_ptr:\n";
539 O << "\t.indirect_symbol " << *i << "\n";
540 O << "\t.long dyld_stub_binding_helper\n";
546 // Output stubs for external global variables
547 if (GVStubs.begin() != GVStubs.end())
548 O << ".data\n.non_lazy_symbol_pointer\n";
549 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
551 O << "L" << *i << "$non_lazy_ptr:\n";
552 O << "\t.indirect_symbol " << *i << "\n";
556 // Output stubs for link-once variables
557 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
558 O << ".data\n.align 2\n";
559 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
560 e = LinkOnceStubs.end(); i != e; ++i) {
561 O << "L" << *i << "$non_lazy_ptr:\n"
562 << "\t.long\t" << *i << '\n';
565 AsmPrinter::doFinalization(M);
566 return false; // success
569 /// runOnMachineFunction - This uses the printMachineInstruction()
570 /// method to print assembly for each instruction.
572 bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
573 CurrentFnName = MF.getFunction()->getName();
575 // Print out constants referenced by the function
576 printConstantPool(MF.getConstantPool());
578 // Print out header for the function.
579 O << "\t.csect .text[PR]\n"
581 << "\t.globl " << CurrentFnName << '\n'
582 << "\t.globl ." << CurrentFnName << '\n'
583 << "\t.csect " << CurrentFnName << "[DS],3\n"
584 << CurrentFnName << ":\n"
585 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
586 << "\t.csect .text[PR]\n"
587 << '.' << CurrentFnName << ":\n";
589 // Print out code for the function.
590 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
592 // Print a label for the basic block.
593 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
594 << I->getBasicBlock()->getName() << "\n";
595 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
597 // Print the assembly for the instruction.
599 printMachineInstruction(II);
604 O << "LT.." << CurrentFnName << ":\n"
606 << "\t.byte 0,0,32,65,128,0,0,0\n"
607 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
609 << "\t.byte \"" << CurrentFnName << "\"\n"
612 // We didn't modify anything.
616 /// printConstantPool - Print to the current output stream assembly
617 /// representations of the constants in the constant pool MCP. This is
618 /// used to print out constants which have been "spilled to memory" by
619 /// the code generator.
621 void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
622 const std::vector<Constant*> &CP = MCP->getConstants();
623 const TargetData &TD = TM.getTargetData();
625 if (CP.empty()) return;
627 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
629 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
631 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
633 emitGlobalConstant(CP[i]);
637 bool AIXAsmPrinter::doInitialization(Module &M) {
638 const TargetData &TD = TM.getTargetData();
639 std::string CurSection;
641 O << "\t.machine \"ppc64\"\n"
643 << "\t.csect .text[PR]\n";
645 // Print out module-level global variables
646 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
648 if (!I->hasInitializer())
651 std::string Name = I->getName();
652 Constant *C = I->getInitializer();
653 // N.B.: We are defaulting to writable strings
654 if (I->hasExternalLinkage()) {
655 O << "\t.globl " << Name << '\n'
656 << "\t.csect .data[RW],3\n";
658 O << "\t.csect _global.rw_c[RW],3\n";
661 emitGlobalConstant(C);
664 // Output labels for globals
665 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
666 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
668 const GlobalVariable *GV = I;
669 // Do not output labels for unused variables
670 if (GV->isExternal() && GV->use_begin() == GV->use_end())
673 std::string Name = GV->getName();
674 std::string Label = "LC.." + utostr(LabelNumber++);
675 GVToLabelMap[GV] = Label;
677 << "\t.tc " << Name << "[TC]," << Name;
678 if (GV->isExternal()) O << "[RW]";
682 Mang = new Mangler(M, ".");
683 return false; // success
686 bool AIXAsmPrinter::doFinalization(Module &M) {
687 const TargetData &TD = TM.getTargetData();
688 // Print out module-level global variables
689 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
691 if (I->hasInitializer() || I->hasExternalLinkage())
694 std::string Name = I->getName();
695 if (I->hasInternalLinkage()) {
696 O << "\t.lcomm " << Name << ",16,_global.bss_c";
698 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
699 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
702 WriteAsOperand(O, I, false, true, &M);
706 O << "_section_.text:\n"
707 << "\t.csect .data[RW],3\n"
708 << "\t.llong _section_.text\n";
711 return false; // success