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