LLVM API Documentation
00001 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a printer that converts from our internal representation 00011 // of machine-dependent LLVM code to PowerPC assembly language. This printer is 00012 // the output mechanism used by `llc'. 00013 // 00014 // Documentation at http://developer.apple.com/documentation/DeveloperTools/ 00015 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #define DEBUG_TYPE "asmprinter" 00020 #include "PPC.h" 00021 #include "PPCPredicates.h" 00022 #include "PPCTargetMachine.h" 00023 #include "PPCSubtarget.h" 00024 #include "llvm/Constants.h" 00025 #include "llvm/DerivedTypes.h" 00026 #include "llvm/Module.h" 00027 #include "llvm/Assembly/Writer.h" 00028 #include "llvm/CodeGen/AsmPrinter.h" 00029 #include "llvm/CodeGen/DwarfWriter.h" 00030 #include "llvm/CodeGen/MachineModuleInfo.h" 00031 #include "llvm/CodeGen/MachineFunctionPass.h" 00032 #include "llvm/CodeGen/MachineInstr.h" 00033 #include "llvm/CodeGen/MachineInstrBuilder.h" 00034 #include "llvm/Support/Mangler.h" 00035 #include "llvm/Support/MathExtras.h" 00036 #include "llvm/Support/CommandLine.h" 00037 #include "llvm/Support/Debug.h" 00038 #include "llvm/Support/Compiler.h" 00039 #include "llvm/Support/raw_ostream.h" 00040 #include "llvm/Target/TargetAsmInfo.h" 00041 #include "llvm/Target/TargetRegisterInfo.h" 00042 #include "llvm/Target/TargetInstrInfo.h" 00043 #include "llvm/Target/TargetOptions.h" 00044 #include "llvm/ADT/Statistic.h" 00045 #include "llvm/ADT/StringExtras.h" 00046 #include "llvm/ADT/StringSet.h" 00047 using namespace llvm; 00048 00049 STATISTIC(EmittedInsts, "Number of machine instrs printed"); 00050 00051 namespace { 00052 struct VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter { 00053 StringSet<> FnStubs, GVStubs, HiddenGVStubs; 00054 const PPCSubtarget &Subtarget; 00055 00056 PPCAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T) 00057 : AsmPrinter(O, TM, T), Subtarget(TM.getSubtarget<PPCSubtarget>()) { 00058 } 00059 00060 virtual const char *getPassName() const { 00061 return "PowerPC Assembly Printer"; 00062 } 00063 00064 PPCTargetMachine &getTM() { 00065 return static_cast<PPCTargetMachine&>(TM); 00066 } 00067 00068 unsigned enumRegToMachineReg(unsigned enumReg) { 00069 switch (enumReg) { 00070 default: assert(0 && "Unhandled register!"); break; 00071 case PPC::CR0: return 0; 00072 case PPC::CR1: return 1; 00073 case PPC::CR2: return 2; 00074 case PPC::CR3: return 3; 00075 case PPC::CR4: return 4; 00076 case PPC::CR5: return 5; 00077 case PPC::CR6: return 6; 00078 case PPC::CR7: return 7; 00079 } 00080 abort(); 00081 } 00082 00083 /// printInstruction - This method is automatically generated by tablegen 00084 /// from the instruction set description. This method returns true if the 00085 /// machine instruction was sufficiently described to print it, otherwise it 00086 /// returns false. 00087 bool printInstruction(const MachineInstr *MI); 00088 00089 void printMachineInstruction(const MachineInstr *MI); 00090 void printOp(const MachineOperand &MO); 00091 00092 /// stripRegisterPrefix - This method strips the character prefix from a 00093 /// register name so that only the number is left. Used by for linux asm. 00094 const char *stripRegisterPrefix(const char *RegName) { 00095 switch (RegName[0]) { 00096 case 'r': 00097 case 'f': 00098 case 'v': return RegName + 1; 00099 case 'c': if (RegName[1] == 'r') return RegName + 2; 00100 } 00101 00102 return RegName; 00103 } 00104 00105 /// printRegister - Print register according to target requirements. 00106 /// 00107 void printRegister(const MachineOperand &MO, bool R0AsZero) { 00108 unsigned RegNo = MO.getReg(); 00109 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??"); 00110 00111 // If we should use 0 for R0. 00112 if (R0AsZero && RegNo == PPC::R0) { 00113 O << "0"; 00114 return; 00115 } 00116 00117 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName; 00118 // Linux assembler (Others?) does not take register mnemonics. 00119 // FIXME - What about special registers used in mfspr/mtspr? 00120 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName); 00121 O << RegName; 00122 } 00123 00124 void printOperand(const MachineInstr *MI, unsigned OpNo) { 00125 const MachineOperand &MO = MI->getOperand(OpNo); 00126 if (MO.isReg()) { 00127 printRegister(MO, false); 00128 } else if (MO.isImm()) { 00129 O << MO.getImm(); 00130 } else { 00131 printOp(MO); 00132 } 00133 } 00134 00135 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00136 unsigned AsmVariant, const char *ExtraCode); 00137 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 00138 unsigned AsmVariant, const char *ExtraCode); 00139 00140 00141 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00142 char value = MI->getOperand(OpNo).getImm(); 00143 value = (value << (32-5)) >> (32-5); 00144 O << (int)value; 00145 } 00146 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00147 unsigned char value = MI->getOperand(OpNo).getImm(); 00148 assert(value <= 31 && "Invalid u5imm argument!"); 00149 O << (unsigned int)value; 00150 } 00151 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00152 unsigned char value = MI->getOperand(OpNo).getImm(); 00153 assert(value <= 63 && "Invalid u6imm argument!"); 00154 O << (unsigned int)value; 00155 } 00156 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00157 O << (short)MI->getOperand(OpNo).getImm(); 00158 } 00159 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00160 O << (unsigned short)MI->getOperand(OpNo).getImm(); 00161 } 00162 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) { 00163 if (MI->getOperand(OpNo).isImm()) { 00164 O << (short)(MI->getOperand(OpNo).getImm()*4); 00165 } else { 00166 O << "lo16("; 00167 printOp(MI->getOperand(OpNo)); 00168 if (TM.getRelocationModel() == Reloc::PIC_) 00169 O << "-\"L" << getFunctionNumber() << "$pb\")"; 00170 else 00171 O << ')'; 00172 } 00173 } 00174 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) { 00175 // Branches can take an immediate operand. This is used by the branch 00176 // selection pass to print $+8, an eight byte displacement from the PC. 00177 if (MI->getOperand(OpNo).isImm()) { 00178 O << "$+" << MI->getOperand(OpNo).getImm()*4; 00179 } else { 00180 printOp(MI->getOperand(OpNo)); 00181 } 00182 } 00183 void printCallOperand(const MachineInstr *MI, unsigned OpNo) { 00184 const MachineOperand &MO = MI->getOperand(OpNo); 00185 if (TM.getRelocationModel() != Reloc::Static) { 00186 if (MO.getType() == MachineOperand::MO_GlobalAddress) { 00187 GlobalValue *GV = MO.getGlobal(); 00188 if (((GV->isDeclaration() || GV->hasWeakLinkage() || 00189 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage()))) { 00190 // Dynamically-resolved functions need a stub for the function. 00191 std::string Name = Mang->getValueName(GV); 00192 FnStubs.insert(Name); 00193 printSuffixedName(Name, "$stub"); 00194 if (GV->hasExternalWeakLinkage()) 00195 ExtWeakSymbols.insert(GV); 00196 return; 00197 } 00198 } 00199 if (MO.getType() == MachineOperand::MO_ExternalSymbol) { 00200 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName(); 00201 FnStubs.insert(Name); 00202 printSuffixedName(Name, "$stub"); 00203 return; 00204 } 00205 } 00206 00207 printOp(MI->getOperand(OpNo)); 00208 } 00209 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) { 00210 O << (int)MI->getOperand(OpNo).getImm()*4; 00211 } 00212 void printPICLabel(const MachineInstr *MI, unsigned OpNo) { 00213 O << "\"L" << getFunctionNumber() << "$pb\"\n"; 00214 O << "\"L" << getFunctionNumber() << "$pb\":"; 00215 } 00216 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) { 00217 if (MI->getOperand(OpNo).isImm()) { 00218 printS16ImmOperand(MI, OpNo); 00219 } else { 00220 if (Subtarget.isDarwin()) O << "ha16("; 00221 printOp(MI->getOperand(OpNo)); 00222 if (TM.getRelocationModel() == Reloc::PIC_) 00223 O << "-\"L" << getFunctionNumber() << "$pb\""; 00224 if (Subtarget.isDarwin()) 00225 O << ')'; 00226 else 00227 O << "@ha"; 00228 } 00229 } 00230 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) { 00231 if (MI->getOperand(OpNo).isImm()) { 00232 printS16ImmOperand(MI, OpNo); 00233 } else { 00234 if (Subtarget.isDarwin()) O << "lo16("; 00235 printOp(MI->getOperand(OpNo)); 00236 if (TM.getRelocationModel() == Reloc::PIC_) 00237 O << "-\"L" << getFunctionNumber() << "$pb\""; 00238 if (Subtarget.isDarwin()) 00239 O << ')'; 00240 else 00241 O << "@l"; 00242 } 00243 } 00244 void printcrbitm(const MachineInstr *MI, unsigned OpNo) { 00245 unsigned CCReg = MI->getOperand(OpNo).getReg(); 00246 unsigned RegNo = enumRegToMachineReg(CCReg); 00247 O << (0x80 >> RegNo); 00248 } 00249 // The new addressing mode printers. 00250 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) { 00251 printSymbolLo(MI, OpNo); 00252 O << '('; 00253 if (MI->getOperand(OpNo+1).isReg() && 00254 MI->getOperand(OpNo+1).getReg() == PPC::R0) 00255 O << "0"; 00256 else 00257 printOperand(MI, OpNo+1); 00258 O << ')'; 00259 } 00260 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) { 00261 if (MI->getOperand(OpNo).isImm()) 00262 printS16X4ImmOperand(MI, OpNo); 00263 else 00264 printSymbolLo(MI, OpNo); 00265 O << '('; 00266 if (MI->getOperand(OpNo+1).isReg() && 00267 MI->getOperand(OpNo+1).getReg() == PPC::R0) 00268 O << "0"; 00269 else 00270 printOperand(MI, OpNo+1); 00271 O << ')'; 00272 } 00273 00274 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) { 00275 // When used as the base register, r0 reads constant zero rather than 00276 // the value contained in the register. For this reason, the darwin 00277 // assembler requires that we print r0 as 0 (no r) when used as the base. 00278 const MachineOperand &MO = MI->getOperand(OpNo); 00279 printRegister(MO, true); 00280 O << ", "; 00281 printOperand(MI, OpNo+1); 00282 } 00283 00284 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo, 00285 const char *Modifier); 00286 00287 virtual bool runOnMachineFunction(MachineFunction &F) = 0; 00288 virtual bool doFinalization(Module &M) = 0; 00289 00290 virtual void EmitExternalGlobal(const GlobalVariable *GV); 00291 }; 00292 00293 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux 00294 struct VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter { 00295 DwarfWriter *DW; 00296 MachineModuleInfo *MMI; 00297 00298 PPCLinuxAsmPrinter(raw_ostream &O, PPCTargetMachine &TM, 00299 const TargetAsmInfo *T) 00300 : PPCAsmPrinter(O, TM, T), DW(0), MMI(0) { 00301 } 00302 00303 virtual const char *getPassName() const { 00304 return "Linux PPC Assembly Printer"; 00305 } 00306 00307 bool runOnMachineFunction(MachineFunction &F); 00308 bool doInitialization(Module &M); 00309 bool doFinalization(Module &M); 00310 00311 void getAnalysisUsage(AnalysisUsage &AU) const { 00312 AU.setPreservesAll(); 00313 AU.addRequired<MachineModuleInfo>(); 00314 AU.addRequired<DwarfWriter>(); 00315 PPCAsmPrinter::getAnalysisUsage(AU); 00316 } 00317 00318 void printModuleLevelGV(const GlobalVariable* GVar); 00319 }; 00320 00321 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac 00322 /// OS X 00323 struct VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter { 00324 00325 DwarfWriter *DW; 00326 MachineModuleInfo *MMI; 00327 raw_ostream &OS; 00328 PPCDarwinAsmPrinter(raw_ostream &O, PPCTargetMachine &TM, 00329 const TargetAsmInfo *T) 00330 : PPCAsmPrinter(O, TM, T), DW(0), MMI(0), OS(O) { 00331 } 00332 00333 virtual const char *getPassName() const { 00334 return "Darwin PPC Assembly Printer"; 00335 } 00336 00337 bool runOnMachineFunction(MachineFunction &F); 00338 bool doInitialization(Module &M); 00339 bool doFinalization(Module &M); 00340 00341 void getAnalysisUsage(AnalysisUsage &AU) const { 00342 AU.setPreservesAll(); 00343 AU.addRequired<MachineModuleInfo>(); 00344 AU.addRequired<DwarfWriter>(); 00345 PPCAsmPrinter::getAnalysisUsage(AU); 00346 } 00347 00348 void printModuleLevelGV(const GlobalVariable* GVar); 00349 }; 00350 } // end of anonymous namespace 00351 00352 // Include the auto-generated portion of the assembly writer 00353 #include "PPCGenAsmWriter.inc" 00354 00355 void PPCAsmPrinter::printOp(const MachineOperand &MO) { 00356 switch (MO.getType()) { 00357 case MachineOperand::MO_Immediate: 00358 cerr << "printOp() does not handle immediate values\n"; 00359 abort(); 00360 return; 00361 00362 case MachineOperand::MO_MachineBasicBlock: 00363 printBasicBlockLabel(MO.getMBB()); 00364 return; 00365 case MachineOperand::MO_JumpTableIndex: 00366 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 00367 << '_' << MO.getIndex(); 00368 // FIXME: PIC relocation model 00369 return; 00370 case MachineOperand::MO_ConstantPoolIndex: 00371 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() 00372 << '_' << MO.getIndex(); 00373 return; 00374 case MachineOperand::MO_ExternalSymbol: 00375 // Computing the address of an external symbol, not calling it. 00376 if (TM.getRelocationModel() != Reloc::Static) { 00377 std::string Name(TAI->getGlobalPrefix()); Name += MO.getSymbolName(); 00378 GVStubs.insert(Name); 00379 printSuffixedName(Name, "$non_lazy_ptr"); 00380 return; 00381 } 00382 O << TAI->getGlobalPrefix() << MO.getSymbolName(); 00383 return; 00384 case MachineOperand::MO_GlobalAddress: { 00385 // Computing the address of a global symbol, not calling it. 00386 GlobalValue *GV = MO.getGlobal(); 00387 std::string Name = Mang->getValueName(GV); 00388 00389 // External or weakly linked global variables need non-lazily-resolved stubs 00390 if (TM.getRelocationModel() != Reloc::Static) { 00391 if (GV->isDeclaration() || GV->mayBeOverridden()) { 00392 if (GV->hasHiddenVisibility()) { 00393 if (!GV->isDeclaration() && !GV->hasCommonLinkage()) 00394 O << Name; 00395 else { 00396 HiddenGVStubs.insert(Name); 00397 printSuffixedName(Name, "$non_lazy_ptr"); 00398 } 00399 } else { 00400 GVStubs.insert(Name); 00401 printSuffixedName(Name, "$non_lazy_ptr"); 00402 } 00403 if (GV->hasExternalWeakLinkage()) 00404 ExtWeakSymbols.insert(GV); 00405 return; 00406 } 00407 } 00408 O << Name; 00409 00410 printOffset(MO.getOffset()); 00411 00412 if (GV->hasExternalWeakLinkage()) 00413 ExtWeakSymbols.insert(GV); 00414 return; 00415 } 00416 00417 default: 00418 O << "<unknown operand type: " << MO.getType() << ">"; 00419 return; 00420 } 00421 } 00422 00423 /// EmitExternalGlobal - In this case we need to use the indirect symbol. 00424 /// 00425 void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { 00426 std::string Name = getGlobalLinkName(GV); 00427 if (TM.getRelocationModel() != Reloc::Static) { 00428 if (GV->hasHiddenVisibility()) 00429 HiddenGVStubs.insert(Name); 00430 else 00431 GVStubs.insert(Name); 00432 printSuffixedName(Name, "$non_lazy_ptr"); 00433 return; 00434 } 00435 O << Name; 00436 } 00437 00438 /// PrintAsmOperand - Print out an operand for an inline asm expression. 00439 /// 00440 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00441 unsigned AsmVariant, 00442 const char *ExtraCode) { 00443 // Does this asm operand have a single letter operand modifier? 00444 if (ExtraCode && ExtraCode[0]) { 00445 if (ExtraCode[1] != 0) return true; // Unknown modifier. 00446 00447 switch (ExtraCode[0]) { 00448 default: return true; // Unknown modifier. 00449 case 'c': // Don't print "$" before a global var name or constant. 00450 // PPC never has a prefix. 00451 printOperand(MI, OpNo); 00452 return false; 00453 case 'L': // Write second word of DImode reference. 00454 // Verify that this operand has two consecutive registers. 00455 if (!MI->getOperand(OpNo).isReg() || 00456 OpNo+1 == MI->getNumOperands() || 00457 !MI->getOperand(OpNo+1).isReg()) 00458 return true; 00459 ++OpNo; // Return the high-part. 00460 break; 00461 case 'I': 00462 // Write 'i' if an integer constant, otherwise nothing. Used to print 00463 // addi vs add, etc. 00464 if (MI->getOperand(OpNo).isImm()) 00465 O << "i"; 00466 return false; 00467 } 00468 } 00469 00470 printOperand(MI, OpNo); 00471 return false; 00472 } 00473 00474 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 00475 unsigned AsmVariant, 00476 const char *ExtraCode) { 00477 if (ExtraCode && ExtraCode[0]) 00478 return true; // Unknown modifier. 00479 if (MI->getOperand(OpNo).isReg()) 00480 printMemRegReg(MI, OpNo); 00481 else 00482 printMemRegImm(MI, OpNo); 00483 return false; 00484 } 00485 00486 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo, 00487 const char *Modifier) { 00488 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!"); 00489 unsigned Code = MI->getOperand(OpNo).getImm(); 00490 if (!strcmp(Modifier, "cc")) { 00491 switch ((PPC::Predicate)Code) { 00492 case PPC::PRED_ALWAYS: return; // Don't print anything for always. 00493 case PPC::PRED_LT: O << "lt"; return; 00494 case PPC::PRED_LE: O << "le"; return; 00495 case PPC::PRED_EQ: O << "eq"; return; 00496 case PPC::PRED_GE: O << "ge"; return; 00497 case PPC::PRED_GT: O << "gt"; return; 00498 case PPC::PRED_NE: O << "ne"; return; 00499 case PPC::PRED_UN: O << "un"; return; 00500 case PPC::PRED_NU: O << "nu"; return; 00501 } 00502 00503 } else { 00504 assert(!strcmp(Modifier, "reg") && 00505 "Need to specify 'cc' or 'reg' as predicate op modifier!"); 00506 // Don't print the register for 'always'. 00507 if (Code == PPC::PRED_ALWAYS) return; 00508 printOperand(MI, OpNo+1); 00509 } 00510 } 00511 00512 00513 /// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to 00514 /// the current output stream. 00515 /// 00516 void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00517 ++EmittedInsts; 00518 00519 // Check for slwi/srwi mnemonics. 00520 if (MI->getOpcode() == PPC::RLWINM) { 00521 bool FoundMnemonic = false; 00522 unsigned char SH = MI->getOperand(2).getImm(); 00523 unsigned char MB = MI->getOperand(3).getImm(); 00524 unsigned char ME = MI->getOperand(4).getImm(); 00525 if (SH <= 31 && MB == 0 && ME == (31-SH)) { 00526 O << "\tslwi "; FoundMnemonic = true; 00527 } 00528 if (SH <= 31 && MB == (32-SH) && ME == 31) { 00529 O << "\tsrwi "; FoundMnemonic = true; 00530 SH = 32-SH; 00531 } 00532 if (FoundMnemonic) { 00533 printOperand(MI, 0); 00534 O << ", "; 00535 printOperand(MI, 1); 00536 O << ", " << (unsigned int)SH << '\n'; 00537 return; 00538 } 00539 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) { 00540 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) { 00541 O << "\tmr "; 00542 printOperand(MI, 0); 00543 O << ", "; 00544 printOperand(MI, 1); 00545 O << '\n'; 00546 return; 00547 } 00548 } else if (MI->getOpcode() == PPC::RLDICR) { 00549 unsigned char SH = MI->getOperand(2).getImm(); 00550 unsigned char ME = MI->getOperand(3).getImm(); 00551 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH 00552 if (63-SH == ME) { 00553 O << "\tsldi "; 00554 printOperand(MI, 0); 00555 O << ", "; 00556 printOperand(MI, 1); 00557 O << ", " << (unsigned int)SH << '\n'; 00558 return; 00559 } 00560 } 00561 00562 if (printInstruction(MI)) 00563 return; // Printer was automatically generated 00564 00565 assert(0 && "Unhandled instruction in asm writer!"); 00566 abort(); 00567 return; 00568 } 00569 00570 /// runOnMachineFunction - This uses the printMachineInstruction() 00571 /// method to print assembly for each instruction. 00572 /// 00573 bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00574 00575 SetupMachineFunction(MF); 00576 O << "\n\n"; 00577 00578 // Print out constants referenced by the function 00579 EmitConstantPool(MF.getConstantPool()); 00580 00581 // Print out labels for the function. 00582 const Function *F = MF.getFunction(); 00583 SwitchToSection(TAI->SectionForGlobal(F)); 00584 00585 switch (F->getLinkage()) { 00586 default: assert(0 && "Unknown linkage type!"); 00587 case Function::PrivateLinkage: 00588 case Function::InternalLinkage: // Symbols default to internal. 00589 break; 00590 case Function::ExternalLinkage: 00591 O << "\t.global\t" << CurrentFnName << '\n' 00592 << "\t.type\t" << CurrentFnName << ", @function\n"; 00593 break; 00594 case Function::WeakLinkage: 00595 case Function::LinkOnceLinkage: 00596 O << "\t.global\t" << CurrentFnName << '\n'; 00597 O << "\t.weak\t" << CurrentFnName << '\n'; 00598 break; 00599 } 00600 00601 printVisibility(CurrentFnName, F->getVisibility()); 00602 00603 EmitAlignment(2, F); 00604 O << CurrentFnName << ":\n"; 00605 00606 // Emit pre-function debug information. 00607 DW->BeginFunction(&MF); 00608 00609 // Print out code for the function. 00610 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00611 I != E; ++I) { 00612 // Print a label for the basic block. 00613 if (I != MF.begin()) { 00614 printBasicBlockLabel(I, true, true); 00615 O << '\n'; 00616 } 00617 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00618 II != E; ++II) { 00619 // Print the assembly for the instruction. 00620 printMachineInstruction(II); 00621 } 00622 } 00623 00624 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n'; 00625 00626 // Print out jump tables referenced by the function. 00627 EmitJumpTableInfo(MF.getJumpTableInfo(), MF); 00628 00629 SwitchToSection(TAI->SectionForGlobal(F)); 00630 00631 // Emit post-function debug information. 00632 DW->EndFunction(&MF); 00633 00634 O.flush(); 00635 00636 // We didn't modify anything. 00637 return false; 00638 } 00639 00640 bool PPCLinuxAsmPrinter::doInitialization(Module &M) { 00641 bool Result = AsmPrinter::doInitialization(M); 00642 00643 // Emit initial debug information. 00644 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00645 assert(MMI); 00646 DW = getAnalysisIfAvailable<DwarfWriter>(); 00647 assert(DW && "DwarfWriter is not available"); 00648 DW->BeginModule(&M, MMI, O, this, TAI); 00649 00650 // GNU as handles section names wrapped in quotes 00651 Mang->setUseQuotes(true); 00652 00653 SwitchToSection(TAI->getTextSection()); 00654 00655 return Result; 00656 } 00657 00658 /// PrintUnmangledNameSafely - Print out the printable characters in the name. 00659 /// Don't print things like \n or \0. 00660 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) { 00661 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen(); 00662 Name != E; ++Name) 00663 if (isprint(*Name)) 00664 OS << *Name; 00665 } 00666 00667 void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { 00668 const TargetData *TD = TM.getTargetData(); 00669 00670 if (!GVar->hasInitializer()) 00671 return; // External global require no code 00672 00673 // Check to see if this is a special global used by LLVM, if so, emit it. 00674 if (EmitSpecialLLVMGlobal(GVar)) 00675 return; 00676 00677 std::string name = Mang->getValueName(GVar); 00678 00679 printVisibility(name, GVar->getVisibility()); 00680 00681 Constant *C = GVar->getInitializer(); 00682 const Type *Type = C->getType(); 00683 unsigned Size = TD->getTypePaddedSize(Type); 00684 unsigned Align = TD->getPreferredAlignmentLog(GVar); 00685 00686 SwitchToSection(TAI->SectionForGlobal(GVar)); 00687 00688 if (C->isNullValue() && /* FIXME: Verify correct */ 00689 !GVar->hasSection() && 00690 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() || 00691 GVar->mayBeOverridden())) { 00692 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 00693 00694 if (GVar->hasExternalLinkage()) { 00695 O << "\t.global " << name << '\n'; 00696 O << "\t.type " << name << ", @object\n"; 00697 O << name << ":\n"; 00698 O << "\t.zero " << Size << '\n'; 00699 } else if (GVar->hasLocalLinkage()) { 00700 O << TAI->getLCOMMDirective() << name << ',' << Size; 00701 } else { 00702 O << ".comm " << name << ',' << Size; 00703 } 00704 O << "\t\t" << TAI->getCommentString() << " '"; 00705 PrintUnmangledNameSafely(GVar, O); 00706 O << "'\n"; 00707 return; 00708 } 00709 00710 switch (GVar->getLinkage()) { 00711 case GlobalValue::LinkOnceLinkage: 00712 case GlobalValue::WeakLinkage: 00713 case GlobalValue::CommonLinkage: 00714 O << "\t.global " << name << '\n' 00715 << "\t.type " << name << ", @object\n" 00716 << "\t.weak " << name << '\n'; 00717 break; 00718 case GlobalValue::AppendingLinkage: 00719 // FIXME: appending linkage variables should go into a section of 00720 // their name or something. For now, just emit them as external. 00721 case GlobalValue::ExternalLinkage: 00722 // If external or appending, declare as a global symbol 00723 O << "\t.global " << name << '\n' 00724 << "\t.type " << name << ", @object\n"; 00725 // FALL THROUGH 00726 case GlobalValue::InternalLinkage: 00727 case GlobalValue::PrivateLinkage: 00728 break; 00729 default: 00730 cerr << "Unknown linkage type!"; 00731 abort(); 00732 } 00733 00734 EmitAlignment(Align, GVar); 00735 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"; 00736 PrintUnmangledNameSafely(GVar, O); 00737 O << "'\n"; 00738 00739 // If the initializer is a extern weak symbol, remember to emit the weak 00740 // reference! 00741 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 00742 if (GV->hasExternalWeakLinkage()) 00743 ExtWeakSymbols.insert(GV); 00744 00745 EmitGlobalConstant(C); 00746 O << '\n'; 00747 } 00748 00749 bool PPCLinuxAsmPrinter::doFinalization(Module &M) { 00750 // Print out module-level global variables here. 00751 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00752 I != E; ++I) 00753 printModuleLevelGV(I); 00754 00755 // TODO 00756 00757 // Emit initial debug information. 00758 DW->EndModule(); 00759 00760 return AsmPrinter::doFinalization(M); 00761 } 00762 00763 /// runOnMachineFunction - This uses the printMachineInstruction() 00764 /// method to print assembly for each instruction. 00765 /// 00766 bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00767 SetupMachineFunction(MF); 00768 O << "\n\n"; 00769 00770 // Print out constants referenced by the function 00771 EmitConstantPool(MF.getConstantPool()); 00772 00773 // Print out labels for the function. 00774 const Function *F = MF.getFunction(); 00775 SwitchToSection(TAI->SectionForGlobal(F)); 00776 00777 switch (F->getLinkage()) { 00778 default: assert(0 && "Unknown linkage type!"); 00779 case Function::PrivateLinkage: 00780 case Function::InternalLinkage: // Symbols default to internal. 00781 break; 00782 case Function::ExternalLinkage: 00783 O << "\t.globl\t" << CurrentFnName << '\n'; 00784 break; 00785 case Function::WeakLinkage: 00786 case Function::LinkOnceLinkage: 00787 O << "\t.globl\t" << CurrentFnName << '\n'; 00788 O << "\t.weak_definition\t" << CurrentFnName << '\n'; 00789 break; 00790 } 00791 00792 printVisibility(CurrentFnName, F->getVisibility()); 00793 00794 EmitAlignment(F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4, F); 00795 O << CurrentFnName << ":\n"; 00796 00797 // Emit pre-function debug information. 00798 DW->BeginFunction(&MF); 00799 00800 // If the function is empty, then we need to emit *something*. Otherwise, the 00801 // function's label might be associated with something that it wasn't meant to 00802 // be associated with. We emit a noop in this situation. 00803 MachineFunction::iterator I = MF.begin(); 00804 00805 if (++I == MF.end() && MF.front().empty()) 00806 O << "\tnop\n"; 00807 00808 // Print out code for the function. 00809 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00810 I != E; ++I) { 00811 // Print a label for the basic block. 00812 if (I != MF.begin()) { 00813 printBasicBlockLabel(I, true, true); 00814 O << '\n'; 00815 } 00816 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); 00817 II != IE; ++II) { 00818 // Print the assembly for the instruction. 00819 printMachineInstruction(II); 00820 } 00821 } 00822 00823 // Print out jump tables referenced by the function. 00824 EmitJumpTableInfo(MF.getJumpTableInfo(), MF); 00825 00826 // Emit post-function debug information. 00827 DW->EndFunction(&MF); 00828 00829 // We didn't modify anything. 00830 return false; 00831 } 00832 00833 00834 bool PPCDarwinAsmPrinter::doInitialization(Module &M) { 00835 static const char *const CPUDirectives[] = { 00836 "", 00837 "ppc", 00838 "ppc601", 00839 "ppc602", 00840 "ppc603", 00841 "ppc7400", 00842 "ppc750", 00843 "ppc970", 00844 "ppc64" 00845 }; 00846 00847 unsigned Directive = Subtarget.getDarwinDirective(); 00848 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970) 00849 Directive = PPC::DIR_970; 00850 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400) 00851 Directive = PPC::DIR_7400; 00852 if (Subtarget.isPPC64() && Directive < PPC::DIR_970) 00853 Directive = PPC::DIR_64; 00854 assert(Directive <= PPC::DIR_64 && "Directive out of range."); 00855 O << "\t.machine " << CPUDirectives[Directive] << '\n'; 00856 00857 bool Result = AsmPrinter::doInitialization(M); 00858 00859 // Emit initial debug information. 00860 // We need this for Personality functions. 00861 // AsmPrinter::doInitialization should have done this analysis. 00862 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00863 assert(MMI); 00864 DW = getAnalysisIfAvailable<DwarfWriter>(); 00865 assert(DW && "DwarfWriter is not available"); 00866 DW->BeginModule(&M, MMI, O, this, TAI); 00867 00868 // Darwin wants symbols to be quoted if they have complex names. 00869 Mang->setUseQuotes(true); 00870 00871 // Prime text sections so they are adjacent. This reduces the likelihood a 00872 // large data or debug section causes a branch to exceed 16M limit. 00873 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced," 00874 "pure_instructions"); 00875 if (TM.getRelocationModel() == Reloc::PIC_) { 00876 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs," 00877 "pure_instructions,32"); 00878 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) { 00879 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs," 00880 "pure_instructions,16"); 00881 } 00882 SwitchToSection(TAI->getTextSection()); 00883 00884 return Result; 00885 } 00886 00887 void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { 00888 const TargetData *TD = TM.getTargetData(); 00889 00890 if (!GVar->hasInitializer()) 00891 return; // External global require no code 00892 00893 // Check to see if this is a special global used by LLVM, if so, emit it. 00894 if (EmitSpecialLLVMGlobal(GVar)) { 00895 if (TM.getRelocationModel() == Reloc::Static) { 00896 if (GVar->getName() == "llvm.global_ctors") 00897 O << ".reference .constructors_used\n"; 00898 else if (GVar->getName() == "llvm.global_dtors") 00899 O << ".reference .destructors_used\n"; 00900 } 00901 return; 00902 } 00903 00904 std::string name = Mang->getValueName(GVar); 00905 00906 printVisibility(name, GVar->getVisibility()); 00907 00908 Constant *C = GVar->getInitializer(); 00909 const Type *Type = C->getType(); 00910 unsigned Size = TD->getTypePaddedSize(Type); 00911 unsigned Align = TD->getPreferredAlignmentLog(GVar); 00912 00913 SwitchToSection(TAI->SectionForGlobal(GVar)); 00914 00915 if (C->isNullValue() && /* FIXME: Verify correct */ 00916 !GVar->hasSection() && 00917 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() || 00918 GVar->mayBeOverridden())) { 00919 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 00920 00921 if (GVar->hasExternalLinkage()) { 00922 O << "\t.globl " << name << '\n'; 00923 O << "\t.zerofill __DATA, __common, " << name << ", " 00924 << Size << ", " << Align; 00925 } else if (GVar->hasLocalLinkage()) { 00926 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align; 00927 } else if (!GVar->hasCommonLinkage()) { 00928 O << "\t.globl " << name << '\n' 00929 << TAI->getWeakDefDirective() << name << '\n'; 00930 EmitAlignment(Align, GVar); 00931 O << name << ":\t\t\t\t" << TAI->getCommentString() << " "; 00932 PrintUnmangledNameSafely(GVar, O); 00933 O << '\n'; 00934 EmitGlobalConstant(C); 00935 return; 00936 } else { 00937 O << ".comm " << name << ',' << Size; 00938 // Darwin 9 and above support aligned common data. 00939 if (Subtarget.isDarwin9()) 00940 O << ',' << Align; 00941 } 00942 O << "\t\t" << TAI->getCommentString() << " '"; 00943 PrintUnmangledNameSafely(GVar, O); 00944 O << "'\n"; 00945 return; 00946 } 00947 00948 switch (GVar->getLinkage()) { 00949 case GlobalValue::LinkOnceLinkage: 00950 case GlobalValue::WeakLinkage: 00951 case GlobalValue::CommonLinkage: 00952 O << "\t.globl " << name << '\n' 00953 << "\t.weak_definition " << name << '\n'; 00954 break; 00955 case GlobalValue::AppendingLinkage: 00956 // FIXME: appending linkage variables should go into a section of 00957 // their name or something. For now, just emit them as external. 00958 case GlobalValue::ExternalLinkage: 00959 // If external or appending, declare as a global symbol 00960 O << "\t.globl " << name << '\n'; 00961 // FALL THROUGH 00962 case GlobalValue::InternalLinkage: 00963 case GlobalValue::PrivateLinkage: 00964 break; 00965 default: 00966 cerr << "Unknown linkage type!"; 00967 abort(); 00968 } 00969 00970 EmitAlignment(Align, GVar); 00971 O << name << ":\t\t\t\t" << TAI->getCommentString() << " '"; 00972 PrintUnmangledNameSafely(GVar, O); 00973 O << "'\n"; 00974 00975 // If the initializer is a extern weak symbol, remember to emit the weak 00976 // reference! 00977 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 00978 if (GV->hasExternalWeakLinkage()) 00979 ExtWeakSymbols.insert(GV); 00980 00981 EmitGlobalConstant(C); 00982 O << '\n'; 00983 } 00984 00985 bool PPCDarwinAsmPrinter::doFinalization(Module &M) { 00986 const TargetData *TD = TM.getTargetData(); 00987 00988 // Print out module-level global variables here. 00989 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00990 I != E; ++I) 00991 printModuleLevelGV(I); 00992 00993 bool isPPC64 = TD->getPointerSizeInBits() == 64; 00994 00995 // Output stubs for dynamically-linked functions 00996 if (TM.getRelocationModel() == Reloc::PIC_) { 00997 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end(); 00998 i != e; ++i) { 00999 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs," 01000 "pure_instructions,32"); 01001 EmitAlignment(4); 01002 const char *p = i->getKeyData(); 01003 bool hasQuote = p[0]=='\"'; 01004 printSuffixedName(p, "$stub"); 01005 O << ":\n"; 01006 O << "\t.indirect_symbol " << p << '\n'; 01007 O << "\tmflr r0\n"; 01008 O << "\tbcl 20,31,"; 01009 if (hasQuote) 01010 O << "\"L0$" << &p[1]; 01011 else 01012 O << "L0$" << p; 01013 O << '\n'; 01014 if (hasQuote) 01015 O << "\"L0$" << &p[1]; 01016 else 01017 O << "L0$" << p; 01018 O << ":\n"; 01019 O << "\tmflr r11\n"; 01020 O << "\taddis r11,r11,ha16("; 01021 printSuffixedName(p, "$lazy_ptr"); 01022 O << "-"; 01023 if (hasQuote) 01024 O << "\"L0$" << &p[1]; 01025 else 01026 O << "L0$" << p; 01027 O << ")\n"; 01028 O << "\tmtlr r0\n"; 01029 if (isPPC64) 01030 O << "\tldu r12,lo16("; 01031 else 01032 O << "\tlwzu r12,lo16("; 01033 printSuffixedName(p, "$lazy_ptr"); 01034 O << "-"; 01035 if (hasQuote) 01036 O << "\"L0$" << &p[1]; 01037 else 01038 O << "L0$" << p; 01039 O << ")(r11)\n"; 01040 O << "\tmtctr r12\n"; 01041 O << "\tbctr\n"; 01042 SwitchToDataSection(".lazy_symbol_pointer"); 01043 printSuffixedName(p, "$lazy_ptr"); 01044 O << ":\n"; 01045 O << "\t.indirect_symbol " << p << '\n'; 01046 if (isPPC64) 01047 O << "\t.quad dyld_stub_binding_helper\n"; 01048 else 01049 O << "\t.long dyld_stub_binding_helper\n"; 01050 } 01051 } else { 01052 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end(); 01053 i != e; ++i) { 01054 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs," 01055 "pure_instructions,16"); 01056 EmitAlignment(4); 01057 const char *p = i->getKeyData(); 01058 printSuffixedName(p, "$stub"); 01059 O << ":\n"; 01060 O << "\t.indirect_symbol " << p << '\n'; 01061 O << "\tlis r11,ha16("; 01062 printSuffixedName(p, "$lazy_ptr"); 01063 O << ")\n"; 01064 if (isPPC64) 01065 O << "\tldu r12,lo16("; 01066 else 01067 O << "\tlwzu r12,lo16("; 01068 printSuffixedName(p, "$lazy_ptr"); 01069 O << ")(r11)\n"; 01070 O << "\tmtctr r12\n"; 01071 O << "\tbctr\n"; 01072 SwitchToDataSection(".lazy_symbol_pointer"); 01073 printSuffixedName(p, "$lazy_ptr"); 01074 O << ":\n"; 01075 O << "\t.indirect_symbol " << p << '\n'; 01076 if (isPPC64) 01077 O << "\t.quad dyld_stub_binding_helper\n"; 01078 else 01079 O << "\t.long dyld_stub_binding_helper\n"; 01080 } 01081 } 01082 01083 O << '\n'; 01084 01085 if (TAI->doesSupportExceptionHandling() && MMI) { 01086 // Add the (possibly multiple) personalities to the set of global values. 01087 // Only referenced functions get into the Personalities list. 01088 const std::vector<Function *>& Personalities = MMI->getPersonalities(); 01089 01090 for (std::vector<Function *>::const_iterator I = Personalities.begin(), 01091 E = Personalities.end(); I != E; ++I) 01092 if (*I) GVStubs.insert("_" + (*I)->getName()); 01093 } 01094 01095 // Output stubs for external and common global variables. 01096 if (!GVStubs.empty()) { 01097 SwitchToDataSection(".non_lazy_symbol_pointer"); 01098 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end(); 01099 i != e; ++i) { 01100 std::string p = i->getKeyData(); 01101 printSuffixedName(p, "$non_lazy_ptr"); 01102 O << ":\n"; 01103 O << "\t.indirect_symbol " << p << '\n'; 01104 if (isPPC64) 01105 O << "\t.quad\t0\n"; 01106 else 01107 O << "\t.long\t0\n"; 01108 } 01109 } 01110 01111 if (!HiddenGVStubs.empty()) { 01112 SwitchToSection(TAI->getDataSection()); 01113 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end(); 01114 i != e; ++i) { 01115 std::string p = i->getKeyData(); 01116 EmitAlignment(isPPC64 ? 3 : 2); 01117 printSuffixedName(p, "$non_lazy_ptr"); 01118 O << ":\n"; 01119 if (isPPC64) 01120 O << "\t.quad\t"; 01121 else 01122 O << "\t.long\t"; 01123 O << p << '\n'; 01124 } 01125 } 01126 01127 01128 // Emit initial debug information. 01129 DW->EndModule(); 01130 01131 // Funny Darwin hack: This flag tells the linker that no global symbols 01132 // contain code that falls through to other global symbols (e.g. the obvious 01133 // implementation of multiple entry points). If this doesn't occur, the 01134 // linker can safely perform dead code stripping. Since LLVM never generates 01135 // code that does this, it is always safe to set. 01136 O << "\t.subsections_via_symbols\n"; 01137 01138 return AsmPrinter::doFinalization(M); 01139 } 01140 01141 01142 01143 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code 01144 /// for a MachineFunction to the given output stream, in a format that the 01145 /// Darwin assembler can deal with. 01146 /// 01147 FunctionPass *llvm::createPPCAsmPrinterPass(raw_ostream &o, 01148 PPCTargetMachine &tm) { 01149 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>(); 01150 01151 if (Subtarget->isDarwin()) { 01152 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo()); 01153 } else { 01154 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo()); 01155 } 01156 } 01157 01158 namespace { 01159 static struct Register { 01160 Register() { 01161 PPCTargetMachine::registerAsmPrinter(createPPCAsmPrinterPass); 01162 } 01163 } Registrator; 01164 } 01165 01166 extern "C" int PowerPCAsmPrinterForceLink; 01167 int PowerPCAsmPrinterForceLink = 0;