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