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