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