PPCAsmPrinter.cpp revision d2ee218b499fcd364aae7da031819b738f009cd1
1//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains a printer that converts from our internal representation 11// of machine-dependent LLVM code to PowerPC assembly language. This printer is 12// the output mechanism used by `llc'. 13// 14// Documentation at http://developer.apple.com/documentation/DeveloperTools/ 15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 16// 17//===----------------------------------------------------------------------===// 18 19#define DEBUG_TYPE "asmprinter" 20#include "PPC.h" 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" 41#include <iostream> 42#include <set> 43using namespace llvm; 44 45namespace { 46 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); 47 48 class PPCAsmPrinter : public AsmPrinter { 49 public: 50 std::set<std::string> FnStubs, GVStubs; 51 52 PPCAsmPrinter(std::ostream &O, TargetMachine &TM) 53 : AsmPrinter(O, TM) {} 54 55 virtual const char *getPassName() const { 56 return "PowerPC Assembly Printer"; 57 } 58 59 PPCTargetMachine &getTM() { 60 return static_cast<PPCTargetMachine&>(TM); 61 } 62 63 unsigned enumRegToMachineReg(unsigned enumReg) { 64 switch (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; 74 } 75 abort(); 76 } 77 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 81 /// returns false. 82 bool printInstruction(const MachineInstr *MI); 83 84 void printMachineInstruction(const MachineInstr *MI); 85 void printOp(const MachineOperand &MO); 86 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(); 94 } else { 95 printOp(MO); 96 } 97 } 98 99 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 100 unsigned AsmVariant, const char *ExtraCode) { 101 printOperand(MI, OpNo); 102 return false; 103 } 104 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; 109 } 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; 114 } 115 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) { 116 O << (short)MI->getOperand(OpNo).getImmedValue(); 117 } 118 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) { 119 O << (unsigned short)MI->getOperand(OpNo).getImmedValue(); 120 } 121 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) { 122 O << (short)MI->getOperand(OpNo).getImmedValue()*4; 123 } 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(); 129 } else { 130 printOp(MI->getOperand(OpNo)); 131 } 132 } 133 void printCallOperand(const MachineInstr *MI, unsigned OpNo) { 134 const MachineOperand &MO = MI->getOperand(OpNo); 135 if (!PPCGenerateStaticCode) { 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"; 144 return; 145 } 146 } 147 if (MO.getType() == MachineOperand::MO_ExternalSymbol) { 148 std::string Name(GlobalPrefix); Name += MO.getSymbolName(); 149 FnStubs.insert(Name); 150 O << "L" << Name << "$stub"; 151 return; 152 } 153 } 154 155 printOp(MI->getOperand(OpNo)); 156 } 157 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) { 158 O << (int)MI->getOperand(OpNo).getImmedValue()*4; 159 } 160 void printPICLabel(const MachineInstr *MI, unsigned OpNo) { 161 O << "\"L" << getFunctionNumber() << "$pb\"\n"; 162 O << "\"L" << getFunctionNumber() << "$pb\":"; 163 } 164 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { 165 if (MI->getOperand(OpNo).isImmediate()) { 166 printS16ImmOperand(MI, OpNo); 167 } else { 168 O << "ha16("; 169 printOp(MI->getOperand(OpNo)); 170 if (PICEnabled) 171 O << "-\"L" << getFunctionNumber() << "$pb\")"; 172 else 173 O << ')'; 174 } 175 } 176 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { 177 if (MI->getOperand(OpNo).isImmediate()) { 178 printS16ImmOperand(MI, OpNo); 179 } else { 180 O << "lo16("; 181 printOp(MI->getOperand(OpNo)); 182 if (PICEnabled) 183 O << "-\"L" << getFunctionNumber() << "$pb\")"; 184 else 185 O << ')'; 186 } 187 } 188 void printcrbitm(const MachineInstr *MI, unsigned OpNo) { 189 unsigned CCReg = MI->getOperand(OpNo).getReg(); 190 unsigned RegNo = enumRegToMachineReg(CCReg); 191 O << (0x80 >> RegNo); 192 } 193 // The new addressing mode printers, currently empty 194 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) { 195 printSymbolLo(MI, OpNo); 196 O << '('; 197 printOperand(MI, OpNo+1); 198 O << ')'; 199 } 200 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) { 201 // When used as the base register, r0 reads constant zero rather than 202 // the value contained in the register. For this reason, the darwin 203 // assembler requires that we print r0 as 0 (no r) when used as the base. 204 const MachineOperand &MO = MI->getOperand(OpNo); 205 if (MO.getReg() == PPC::R0) 206 O << '0'; 207 else 208 O << TM.getRegisterInfo()->get(MO.getReg()).Name; 209 O << ", "; 210 printOperand(MI, OpNo+1); 211 } 212 213 virtual bool runOnMachineFunction(MachineFunction &F) = 0; 214 virtual bool doFinalization(Module &M) = 0; 215 216 }; 217 218 /// DarwinDwarfWriter - Dwarf debug info writer customized for Darwin/Mac OS X 219 /// 220 struct DarwinDwarfWriter : public DwarfWriter { 221 // Ctor. 222 DarwinDwarfWriter(std::ostream &o, AsmPrinter *ap) 223 : DwarfWriter(o, ap) 224 { 225 needsSet = true; 226 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev"; 227 DwarfInfoSection = ".section __DWARF,__debug_info"; 228 DwarfLineSection = ".section __DWARF,__debug_line"; 229 DwarfFrameSection = 230 ".section __DWARF,__debug_frame,,coalesced,no_toc+strip_static_syms"; 231 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames"; 232 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes"; 233 DwarfStrSection = ".section __DWARF,__debug_str"; 234 DwarfLocSection = ".section __DWARF,__debug_loc"; 235 DwarfARangesSection = ".section __DWARF,__debug_aranges"; 236 DwarfRangesSection = ".section __DWARF,__debug_ranges"; 237 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo"; 238 TextSection = ".text"; 239 DataSection = ".data"; 240 } 241 }; 242 243 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS 244 /// X 245 struct DarwinAsmPrinter : public PPCAsmPrinter { 246 247 DarwinDwarfWriter DW; 248 249 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM) 250 : PPCAsmPrinter(O, TM), DW(O, this) { 251 CommentString = ";"; 252 GlobalPrefix = "_"; 253 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs 254 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. 255 Data64bitsDirective = 0; // we can't emit a 64-bit unit 256 AlignmentIsInBytes = false; // Alignment is by power of 2. 257 ConstantPoolSection = "\t.const\t"; 258 LCOMMDirective = "\t.lcomm\t"; 259 StaticCtorsSection = ".mod_init_func"; 260 StaticDtorsSection = ".mod_term_func"; 261 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP 262 } 263 264 virtual const char *getPassName() const { 265 return "Darwin PPC Assembly Printer"; 266 } 267 268 bool runOnMachineFunction(MachineFunction &F); 269 bool doInitialization(Module &M); 270 bool doFinalization(Module &M); 271 272 void getAnalysisUsage(AnalysisUsage &AU) const { 273 AU.setPreservesAll(); 274 AU.addRequired<MachineDebugInfo>(); 275 PPCAsmPrinter::getAnalysisUsage(AU); 276 } 277 278 }; 279 280 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX 281 /// 282 struct AIXAsmPrinter : public PPCAsmPrinter { 283 /// Map for labels corresponding to global variables 284 /// 285 std::map<const GlobalVariable*,std::string> GVToLabelMap; 286 287 AIXAsmPrinter(std::ostream &O, TargetMachine &TM) 288 : PPCAsmPrinter(O, TM) { 289 CommentString = "#"; 290 GlobalPrefix = "."; 291 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. 292 Data64bitsDirective = 0; // we can't emit a 64-bit unit 293 AlignmentIsInBytes = false; // Alignment is by power of 2. 294 ConstantPoolSection = "\t.const\t"; 295 } 296 297 virtual const char *getPassName() const { 298 return "AIX PPC Assembly Printer"; 299 } 300 301 bool runOnMachineFunction(MachineFunction &F); 302 bool doInitialization(Module &M); 303 bool doFinalization(Module &M); 304 }; 305} // end of anonymous namespace 306 307/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly 308/// code for a MachineFunction to the given output stream, in a format that the 309/// Darwin assembler can deal with. 310/// 311FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) { 312 return new DarwinAsmPrinter(o, tm); 313} 314 315/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code 316/// for a MachineFunction to the given output stream, in a format that the 317/// AIX 5L assembler can deal with. 318/// 319FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) { 320 return new AIXAsmPrinter(o, tm); 321} 322 323// Include the auto-generated portion of the assembly writer 324#include "PPCGenAsmWriter.inc" 325 326void PPCAsmPrinter::printOp(const MachineOperand &MO) { 327 const MRegisterInfo &RI = *TM.getRegisterInfo(); 328 int new_symbol; 329 330 switch (MO.getType()) { 331 case MachineOperand::MO_VirtualRegister: 332 if (Value *V = MO.getVRegValueOrNull()) { 333 O << "<" << V->getName() << ">"; 334 return; 335 } 336 // FALLTHROUGH 337 case MachineOperand::MO_MachineRegister: 338 case MachineOperand::MO_CCRegister: 339 O << RI.get(MO.getReg()).Name; 340 return; 341 342 case MachineOperand::MO_SignExtendedImmed: 343 case MachineOperand::MO_UnextendedImmed: 344 std::cerr << "printOp() does not handle immediate values\n"; 345 abort(); 346 return; 347 348 case MachineOperand::MO_PCRelativeDisp: 349 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; 350 abort(); 351 return; 352 353 case MachineOperand::MO_MachineBasicBlock: { 354 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); 355 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_" 356 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName(); 357 return; 358 } 359 360 case MachineOperand::MO_ConstantPoolIndex: 361 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() 362 << '_' << MO.getConstantPoolIndex(); 363 return; 364 case MachineOperand::MO_ExternalSymbol: 365 // Computing the address of an external symbol, not calling it. 366 if (!PPCGenerateStaticCode) { 367 std::string Name(GlobalPrefix); Name += MO.getSymbolName(); 368 GVStubs.insert(Name); 369 O << "L" << Name << "$non_lazy_ptr"; 370 return; 371 } 372 O << GlobalPrefix << MO.getSymbolName(); 373 return; 374 case MachineOperand::MO_GlobalAddress: { 375 // Computing the address of a global symbol, not calling it. 376 GlobalValue *GV = MO.getGlobal(); 377 std::string Name = Mang->getValueName(GV); 378 int offset = MO.getOffset(); 379 380 // External or weakly linked global variables need non-lazily-resolved stubs 381 if (!PPCGenerateStaticCode) { 382 if (((GV->isExternal() || GV->hasWeakLinkage() || 383 GV->hasLinkOnceLinkage()))) { 384 GVStubs.insert(Name); 385 O << "L" << Name << "$non_lazy_ptr"; 386 return; 387 } 388 } 389 390 O << Name; 391 return; 392 } 393 394 default: 395 O << "<unknown operand type: " << MO.getType() << ">"; 396 return; 397 } 398} 399 400/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to 401/// the current output stream. 402/// 403void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { 404 ++EmittedInsts; 405 406 // Check for slwi/srwi mnemonics. 407 if (MI->getOpcode() == PPC::RLWINM) { 408 bool FoundMnemonic = false; 409 unsigned char SH = MI->getOperand(2).getImmedValue(); 410 unsigned char MB = MI->getOperand(3).getImmedValue(); 411 unsigned char ME = MI->getOperand(4).getImmedValue(); 412 if (SH <= 31 && MB == 0 && ME == (31-SH)) { 413 O << "slwi "; FoundMnemonic = true; 414 } 415 if (SH <= 31 && MB == (32-SH) && ME == 31) { 416 O << "srwi "; FoundMnemonic = true; 417 SH = 32-SH; 418 } 419 if (FoundMnemonic) { 420 printOperand(MI, 0); 421 O << ", "; 422 printOperand(MI, 1); 423 O << ", " << (unsigned int)SH << "\n"; 424 return; 425 } 426 } else if (MI->getOpcode() == PPC::OR4 || MI->getOpcode() == PPC::OR8) { 427 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) { 428 O << "mr "; 429 printOperand(MI, 0); 430 O << ", "; 431 printOperand(MI, 1); 432 O << "\n"; 433 return; 434 } 435 } 436 437 if (printInstruction(MI)) 438 return; // Printer was automatically generated 439 440 assert(0 && "Unhandled instruction in asm writer!"); 441 abort(); 442 return; 443} 444 445 446/// runOnMachineFunction - This uses the printMachineInstruction() 447/// method to print assembly for each instruction. 448/// 449bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 450 // FIXME - is this the earliest this can be set? 451 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>()); 452 453 SetupMachineFunction(MF); 454 O << "\n\n"; 455 456 // Emit pre-function debug information. 457 DW.BeginFunction(MF); 458 459 // Print out constants referenced by the function 460 EmitConstantPool(MF.getConstantPool()); 461 462 // Print out labels for the function. 463 const Function *F = MF.getFunction(); 464 switch (F->getLinkage()) { 465 default: assert(0 && "Unknown linkage type!"); 466 case Function::InternalLinkage: // Symbols default to internal. 467 SwitchSection(".text", F); 468 break; 469 case Function::ExternalLinkage: 470 SwitchSection(".text", F); 471 O << "\t.globl\t" << CurrentFnName << "\n"; 472 break; 473 case Function::WeakLinkage: 474 case Function::LinkOnceLinkage: 475 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions", 476 F); 477 O << "\t.globl\t" << CurrentFnName << "\n"; 478 O << "\t.weak_definition\t" << CurrentFnName << "\n"; 479 break; 480 } 481 EmitAlignment(4, F); 482 O << CurrentFnName << ":\n"; 483 484 // Print out code for the function. 485 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 486 I != E; ++I) { 487 // Print a label for the basic block. 488 if (I != MF.begin()) { 489 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' 490 << I->getNumber() << ":\t"; 491 if (!I->getBasicBlock()->getName().empty()) 492 O << CommentString << " " << I->getBasicBlock()->getName(); 493 O << "\n"; 494 } 495 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 496 II != E; ++II) { 497 // Print the assembly for the instruction. 498 O << "\t"; 499 printMachineInstruction(II); 500 } 501 } 502 503 // Emit post-function debug information. 504 DW.EndFunction(MF); 505 506 // We didn't modify anything. 507 return false; 508} 509 510 511bool DarwinAsmPrinter::doInitialization(Module &M) { 512 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) 513 O << "\t.machine ppc970\n"; 514 AsmPrinter::doInitialization(M); 515 516 // Darwin wants symbols to be quoted if they have complex names. 517 Mang->setUseQuotes(true); 518 519 // Emit initial debug information. 520 DW.BeginModule(M); 521 return false; 522} 523 524bool DarwinAsmPrinter::doFinalization(Module &M) { 525 const TargetData &TD = TM.getTargetData(); 526 527 // Print out module-level global variables here. 528 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 529 I != E; ++I) { 530 if (!I->hasInitializer()) continue; // External global require no code 531 532 // Check to see if this is a special global used by LLVM, if so, emit it. 533 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I)) 534 continue; 535 536 std::string name = Mang->getValueName(I); 537 Constant *C = I->getInitializer(); 538 unsigned Size = TD.getTypeSize(C->getType()); 539 unsigned Align = getPreferredAlignmentLog(I); 540 541 if (C->isNullValue() && /* FIXME: Verify correct */ 542 (I->hasInternalLinkage() || I->hasWeakLinkage() || 543 I->hasLinkOnceLinkage() || 544 (I->hasExternalLinkage() && !I->hasSection()))) { 545 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 546 if (I->hasExternalLinkage()) { 547 O << "\t.globl " << name << '\n'; 548 O << "\t.zerofill __DATA, __common, " << name << ", " 549 << Size << ", " << Align; 550 } else if (I->hasInternalLinkage()) { 551 SwitchSection(".data", I); 552 O << LCOMMDirective << name << "," << Size << "," << Align; 553 } else { 554 SwitchSection(".data", I); 555 O << ".comm " << name << "," << Size; 556 } 557 O << "\t\t; '" << I->getName() << "'\n"; 558 } else { 559 switch (I->getLinkage()) { 560 case GlobalValue::LinkOnceLinkage: 561 case GlobalValue::WeakLinkage: 562 O << "\t.globl " << name << '\n' 563 << "\t.weak_definition " << name << '\n'; 564 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I); 565 break; 566 case GlobalValue::AppendingLinkage: 567 // FIXME: appending linkage variables should go into a section of 568 // their name or something. For now, just emit them as external. 569 case GlobalValue::ExternalLinkage: 570 // If external or appending, declare as a global symbol 571 O << "\t.globl " << name << "\n"; 572 // FALL THROUGH 573 case GlobalValue::InternalLinkage: 574 SwitchSection(".data", I); 575 break; 576 default: 577 std::cerr << "Unknown linkage type!"; 578 abort(); 579 } 580 581 EmitAlignment(Align, I); 582 O << name << ":\t\t\t\t; '" << I->getName() << "'\n"; 583 EmitGlobalConstant(C); 584 O << '\n'; 585 } 586 } 587 588 // Output stubs for dynamically-linked functions 589 if (PICEnabled) { 590 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); 591 i != e; ++i) { 592 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs," 593 "pure_instructions,32", 0); 594 EmitAlignment(2); 595 O << "L" << *i << "$stub:\n"; 596 O << "\t.indirect_symbol " << *i << "\n"; 597 O << "\tmflr r0\n"; 598 O << "\tbcl 20,31,L0$" << *i << "\n"; 599 O << "L0$" << *i << ":\n"; 600 O << "\tmflr r11\n"; 601 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; 602 O << "\tmtlr r0\n"; 603 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; 604 O << "\tmtctr r12\n"; 605 O << "\tbctr\n"; 606 SwitchSection(".lazy_symbol_pointer", 0); 607 O << "L" << *i << "$lazy_ptr:\n"; 608 O << "\t.indirect_symbol " << *i << "\n"; 609 O << "\t.long dyld_stub_binding_helper\n"; 610 } 611 } else { 612 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); 613 i != e; ++i) { 614 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs," 615 "pure_instructions,16", 0); 616 EmitAlignment(4); 617 O << "L" << *i << "$stub:\n"; 618 O << "\t.indirect_symbol " << *i << "\n"; 619 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n"; 620 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n"; 621 O << "\tmtctr r12\n"; 622 O << "\tbctr\n"; 623 SwitchSection(".lazy_symbol_pointer", 0); 624 O << "L" << *i << "$lazy_ptr:\n"; 625 O << "\t.indirect_symbol " << *i << "\n"; 626 O << "\t.long dyld_stub_binding_helper\n"; 627 } 628 } 629 630 O << "\n"; 631 632 // Output stubs for external and common global variables. 633 if (GVStubs.begin() != GVStubs.end()) { 634 SwitchSection(".non_lazy_symbol_pointer", 0); 635 for (std::set<std::string>::iterator I = GVStubs.begin(), 636 E = GVStubs.end(); I != E; ++I) { 637 O << "L" << *I << "$non_lazy_ptr:\n"; 638 O << "\t.indirect_symbol " << *I << "\n"; 639 O << "\t.long\t0\n"; 640 } 641 } 642 643 // Emit initial debug information. 644 DW.EndModule(M); 645 646 // Funny Darwin hack: This flag tells the linker that no global symbols 647 // contain code that falls through to other global symbols (e.g. the obvious 648 // implementation of multiple entry points). If this doesn't occur, the 649 // linker can safely perform dead code stripping. Since LLVM never generates 650 // code that does this, it is always safe to set. 651 O << "\t.subsections_via_symbols\n"; 652 653 AsmPrinter::doFinalization(M); 654 return false; // success 655} 656 657/// runOnMachineFunction - This uses the e() 658/// method to print assembly for each instruction. 659/// 660bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 661 SetupMachineFunction(MF); 662 663 // Print out constants referenced by the function 664 EmitConstantPool(MF.getConstantPool()); 665 666 // Print out header for the function. 667 O << "\t.csect .text[PR]\n" 668 << "\t.align 2\n" 669 << "\t.globl " << CurrentFnName << '\n' 670 << "\t.globl ." << CurrentFnName << '\n' 671 << "\t.csect " << CurrentFnName << "[DS],3\n" 672 << CurrentFnName << ":\n" 673 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n" 674 << "\t.csect .text[PR]\n" 675 << '.' << CurrentFnName << ":\n"; 676 677 // Print out code for the function. 678 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 679 I != E; ++I) { 680 // Print a label for the basic block. 681 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_' 682 << I->getNumber() 683 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n'; 684 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 685 II != E; ++II) { 686 // Print the assembly for the instruction. 687 O << "\t"; 688 printMachineInstruction(II); 689 } 690 } 691 692 O << "LT.." << CurrentFnName << ":\n" 693 << "\t.long 0\n" 694 << "\t.byte 0,0,32,65,128,0,0,0\n" 695 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n' 696 << "\t.short 3\n" 697 << "\t.byte \"" << CurrentFnName << "\"\n" 698 << "\t.align 2\n"; 699 700 // We didn't modify anything. 701 return false; 702} 703 704bool AIXAsmPrinter::doInitialization(Module &M) { 705 SwitchSection("", 0); 706 const TargetData &TD = TM.getTargetData(); 707 708 O << "\t.machine \"ppc64\"\n" 709 << "\t.toc\n" 710 << "\t.csect .text[PR]\n"; 711 712 // Print out module-level global variables 713 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 714 I != E; ++I) { 715 if (!I->hasInitializer()) 716 continue; 717 718 std::string Name = I->getName(); 719 Constant *C = I->getInitializer(); 720 // N.B.: We are defaulting to writable strings 721 if (I->hasExternalLinkage()) { 722 O << "\t.globl " << Name << '\n' 723 << "\t.csect .data[RW],3\n"; 724 } else { 725 O << "\t.csect _global.rw_c[RW],3\n"; 726 } 727 O << Name << ":\n"; 728 EmitGlobalConstant(C); 729 } 730 731 // Output labels for globals 732 if (M.global_begin() != M.global_end()) O << "\t.toc\n"; 733 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 734 I != E; ++I) { 735 const GlobalVariable *GV = I; 736 // Do not output labels for unused variables 737 if (GV->isExternal() && GV->use_begin() == GV->use_end()) 738 continue; 739 740 IncrementFunctionNumber(); 741 std::string Name = GV->getName(); 742 std::string Label = "LC.." + utostr(getFunctionNumber()); 743 GVToLabelMap[GV] = Label; 744 O << Label << ":\n" 745 << "\t.tc " << Name << "[TC]," << Name; 746 if (GV->isExternal()) O << "[RW]"; 747 O << '\n'; 748 } 749 750 AsmPrinter::doInitialization(M); 751 return false; // success 752} 753 754bool AIXAsmPrinter::doFinalization(Module &M) { 755 const TargetData &TD = TM.getTargetData(); 756 // Print out module-level global variables 757 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 758 I != E; ++I) { 759 if (I->hasInitializer() || I->hasExternalLinkage()) 760 continue; 761 762 std::string Name = I->getName(); 763 if (I->hasInternalLinkage()) { 764 O << "\t.lcomm " << Name << ",16,_global.bss_c"; 765 } else { 766 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType()) 767 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType())); 768 } 769 O << "\t\t" << CommentString << " "; 770 WriteAsOperand(O, I, false, true, &M); 771 O << "\n"; 772 } 773 774 O << "_section_.text:\n" 775 << "\t.csect .data[RW],3\n" 776 << "\t.llong _section_.text\n"; 777 AsmPrinter::doFinalization(M); 778 return false; // success 779} 780