LLVM API Documentation
00001 //===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T 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 AT&T format assembly 00012 // language. This printer is the output mechanism used by `llc'. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #define DEBUG_TYPE "asm-printer" 00017 #include "X86ATTAsmPrinter.h" 00018 #include "X86.h" 00019 #include "X86COFF.h" 00020 #include "X86MachineFunctionInfo.h" 00021 #include "X86TargetMachine.h" 00022 #include "X86TargetAsmInfo.h" 00023 #include "llvm/CallingConv.h" 00024 #include "llvm/DerivedTypes.h" 00025 #include "llvm/Module.h" 00026 #include "llvm/Type.h" 00027 #include "llvm/ADT/Statistic.h" 00028 #include "llvm/ADT/StringExtras.h" 00029 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00030 #include "llvm/Support/Mangler.h" 00031 #include "llvm/Support/raw_ostream.h" 00032 #include "llvm/Target/TargetAsmInfo.h" 00033 #include "llvm/Target/TargetOptions.h" 00034 using namespace llvm; 00035 00036 STATISTIC(EmittedInsts, "Number of machine instrs printed"); 00037 00038 static std::string getPICLabelString(unsigned FnNum, 00039 const TargetAsmInfo *TAI, 00040 const X86Subtarget* Subtarget) { 00041 std::string label; 00042 if (Subtarget->isTargetDarwin()) 00043 label = "\"L" + utostr_32(FnNum) + "$pb\""; 00044 else if (Subtarget->isTargetELF()) 00045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel"; 00046 else 00047 assert(0 && "Don't know how to print PIC label!\n"); 00048 00049 return label; 00050 } 00051 00052 static X86MachineFunctionInfo calculateFunctionInfo(const Function *F, 00053 const TargetData *TD) { 00054 X86MachineFunctionInfo Info; 00055 uint64_t Size = 0; 00056 00057 switch (F->getCallingConv()) { 00058 case CallingConv::X86_StdCall: 00059 Info.setDecorationStyle(StdCall); 00060 break; 00061 case CallingConv::X86_FastCall: 00062 Info.setDecorationStyle(FastCall); 00063 break; 00064 default: 00065 return Info; 00066 } 00067 00068 unsigned argNum = 1; 00069 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 00070 AI != AE; ++AI, ++argNum) { 00071 const Type* Ty = AI->getType(); 00072 00073 // 'Dereference' type in case of byval parameter attribute 00074 if (F->paramHasAttr(argNum, Attribute::ByVal)) 00075 Ty = cast<PointerType>(Ty)->getElementType(); 00076 00077 // Size should be aligned to DWORD boundary 00078 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4; 00079 } 00080 00081 // We're not supporting tooooo huge arguments :) 00082 Info.setBytesToPopOnReturn((unsigned int)Size); 00083 return Info; 00084 } 00085 00086 /// PrintUnmangledNameSafely - Print out the printable characters in the name. 00087 /// Don't print things like \n or \0. 00088 static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) { 00089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen(); 00090 Name != E; ++Name) 00091 if (isprint(*Name)) 00092 OS << *Name; 00093 } 00094 00095 /// decorateName - Query FunctionInfoMap and use this information for various 00096 /// name decoration. 00097 void X86ATTAsmPrinter::decorateName(std::string &Name, 00098 const GlobalValue *GV) { 00099 const Function *F = dyn_cast<Function>(GV); 00100 if (!F) return; 00101 00102 // We don't want to decorate non-stdcall or non-fastcall functions right now 00103 unsigned CC = F->getCallingConv(); 00104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) 00105 return; 00106 00107 // Decorate names only when we're targeting Cygwin/Mingw32 targets 00108 if (!Subtarget->isTargetCygMing()) 00109 return; 00110 00111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F); 00112 00113 const X86MachineFunctionInfo *Info; 00114 if (info_item == FunctionInfoMap.end()) { 00115 // Calculate apropriate function info and populate map 00116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData()); 00117 Info = &FunctionInfoMap[F]; 00118 } else { 00119 Info = &info_item->second; 00120 } 00121 00122 const FunctionType *FT = F->getFunctionType(); 00123 switch (Info->getDecorationStyle()) { 00124 case None: 00125 break; 00126 case StdCall: 00127 // "Pure" variadic functions do not receive @0 suffix. 00128 if (!FT->isVarArg() || (FT->getNumParams() == 0) || 00129 (FT->getNumParams() == 1 && F->hasStructRetAttr())) 00130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); 00131 break; 00132 case FastCall: 00133 // "Pure" variadic functions do not receive @0 suffix. 00134 if (!FT->isVarArg() || (FT->getNumParams() == 0) || 00135 (FT->getNumParams() == 1 && F->hasStructRetAttr())) 00136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); 00137 00138 if (Name[0] == '_') { 00139 Name[0] = '@'; 00140 } else { 00141 Name = '@' + Name; 00142 } 00143 break; 00144 default: 00145 assert(0 && "Unsupported DecorationStyle"); 00146 } 00147 } 00148 00149 void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) { 00150 const Function *F = MF.getFunction(); 00151 00152 decorateName(CurrentFnName, F); 00153 00154 SwitchToSection(TAI->SectionForGlobal(F)); 00155 00156 unsigned FnAlign = 4; 00157 if (F->hasFnAttr(Attribute::OptimizeForSize)) 00158 FnAlign = 1; 00159 switch (F->getLinkage()) { 00160 default: assert(0 && "Unknown linkage type!"); 00161 case Function::InternalLinkage: // Symbols default to internal. 00162 EmitAlignment(FnAlign, F); 00163 break; 00164 case Function::DLLExportLinkage: 00165 case Function::ExternalLinkage: 00166 EmitAlignment(FnAlign, F); 00167 O << "\t.globl\t" << CurrentFnName << '\n'; 00168 break; 00169 case Function::LinkOnceLinkage: 00170 case Function::WeakLinkage: 00171 EmitAlignment(FnAlign, F); 00172 if (Subtarget->isTargetDarwin()) { 00173 O << "\t.globl\t" << CurrentFnName << '\n'; 00174 O << TAI->getWeakDefDirective() << CurrentFnName << '\n'; 00175 } else if (Subtarget->isTargetCygMing()) { 00176 O << "\t.globl\t" << CurrentFnName << "\n" 00177 "\t.linkonce discard\n"; 00178 } else { 00179 O << "\t.weak\t" << CurrentFnName << '\n'; 00180 } 00181 break; 00182 } 00183 00184 printVisibility(CurrentFnName, F->getVisibility()); 00185 00186 if (Subtarget->isTargetELF()) 00187 O << "\t.type\t" << CurrentFnName << ",@function\n"; 00188 else if (Subtarget->isTargetCygMing()) { 00189 O << "\t.def\t " << CurrentFnName 00190 << ";\t.scl\t" << 00191 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT) 00192 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) 00193 << ";\t.endef\n"; 00194 } 00195 00196 O << CurrentFnName << ":\n"; 00197 // Add some workaround for linkonce linkage on Cygwin\MinGW 00198 if (Subtarget->isTargetCygMing() && 00199 (F->getLinkage() == Function::LinkOnceLinkage || 00200 F->getLinkage() == Function::WeakLinkage)) 00201 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n"; 00202 } 00203 00204 /// runOnMachineFunction - This uses the printMachineInstruction() 00205 /// method to print assembly for each instruction. 00206 /// 00207 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00208 const Function *F = MF.getFunction(); 00209 unsigned CC = F->getCallingConv(); 00210 00211 SetupMachineFunction(MF); 00212 O << "\n\n"; 00213 00214 // Populate function information map. Actually, We don't want to populate 00215 // non-stdcall or non-fastcall functions' information right now. 00216 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) 00217 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>(); 00218 00219 // Print out constants referenced by the function 00220 EmitConstantPool(MF.getConstantPool()); 00221 00222 if (F->hasDLLExportLinkage()) 00223 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), "")); 00224 00225 // Print the 'header' of function 00226 emitFunctionHeader(MF); 00227 00228 // Emit pre-function debug and/or EH information. 00229 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling()) 00230 DW.BeginFunction(&MF); 00231 00232 // Print out code for the function. 00233 bool hasAnyRealCode = false; 00234 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00235 I != E; ++I) { 00236 // Print a label for the basic block. 00237 if (!I->pred_empty()) { 00238 printBasicBlockLabel(I, true, true); 00239 O << '\n'; 00240 } 00241 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); 00242 II != IE; ++II) { 00243 // Print the assembly for the instruction. 00244 if (!II->isLabel()) 00245 hasAnyRealCode = true; 00246 printMachineInstruction(II); 00247 } 00248 } 00249 00250 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) { 00251 // If the function is empty, then we need to emit *something*. Otherwise, 00252 // the function's label might be associated with something that it wasn't 00253 // meant to be associated with. We emit a noop in this situation. 00254 // We are assuming inline asms are code. 00255 O << "\tnop\n"; 00256 } 00257 00258 if (TAI->hasDotTypeDotSizeDirective()) 00259 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n'; 00260 00261 // Emit post-function debug information. 00262 if (TAI->doesSupportDebugInformation()) 00263 DW.EndFunction(&MF); 00264 00265 // Print out jump tables referenced by the function. 00266 EmitJumpTableInfo(MF.getJumpTableInfo(), MF); 00267 00268 O.flush(); 00269 00270 // We didn't modify anything. 00271 return false; 00272 } 00273 00274 static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) { 00275 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_; 00276 } 00277 00278 static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) { 00279 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ && 00280 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT()); 00281 } 00282 00283 static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) { 00284 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static; 00285 } 00286 00287 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 00288 const char *Modifier, bool NotRIPRel) { 00289 const MachineOperand &MO = MI->getOperand(OpNo); 00290 switch (MO.getType()) { 00291 case MachineOperand::MO_Register: { 00292 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && 00293 "Virtual registers should not make it this far!"); 00294 O << '%'; 00295 unsigned Reg = MO.getReg(); 00296 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { 00297 MVT VT = (strcmp(Modifier+6,"64") == 0) ? 00298 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 : 00299 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8)); 00300 Reg = getX86SubSuperRegister(Reg, VT); 00301 } 00302 O << TRI->getAsmName(Reg); 00303 return; 00304 } 00305 00306 case MachineOperand::MO_Immediate: 00307 if (!Modifier || 00308 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem"))) 00309 O << '$'; 00310 O << MO.getImm(); 00311 return; 00312 case MachineOperand::MO_MachineBasicBlock: 00313 printBasicBlockLabel(MO.getMBB()); 00314 return; 00315 case MachineOperand::MO_JumpTableIndex: { 00316 bool isMemOp = Modifier && !strcmp(Modifier, "mem"); 00317 if (!isMemOp) O << '$'; 00318 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_' 00319 << MO.getIndex(); 00320 00321 if (TM.getRelocationModel() == Reloc::PIC_) { 00322 if (Subtarget->isPICStyleStub()) 00323 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 00324 << "$pb\""; 00325 else if (Subtarget->isPICStyleGOT()) 00326 O << "@GOTOFF"; 00327 } 00328 00329 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel) 00330 O << "(%rip)"; 00331 return; 00332 } 00333 case MachineOperand::MO_ConstantPoolIndex: { 00334 bool isMemOp = Modifier && !strcmp(Modifier, "mem"); 00335 if (!isMemOp) O << '$'; 00336 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 00337 << MO.getIndex(); 00338 00339 if (TM.getRelocationModel() == Reloc::PIC_) { 00340 if (Subtarget->isPICStyleStub()) 00341 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 00342 << "$pb\""; 00343 else if (Subtarget->isPICStyleGOT()) 00344 O << "@GOTOFF"; 00345 } 00346 00347 printOffset(MO.getOffset()); 00348 00349 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel) 00350 O << "(%rip)"; 00351 return; 00352 } 00353 case MachineOperand::MO_GlobalAddress: { 00354 bool isCallOp = Modifier && !strcmp(Modifier, "call"); 00355 bool isMemOp = Modifier && !strcmp(Modifier, "mem"); 00356 bool needCloseParen = false; 00357 00358 const GlobalValue *GV = MO.getGlobal(); 00359 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 00360 if (!GVar) { 00361 // If GV is an alias then use the aliasee for determining 00362 // thread-localness. 00363 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) 00364 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)); 00365 } 00366 00367 bool isThreadLocal = GVar && GVar->isThreadLocal(); 00368 00369 std::string Name = Mang->getValueName(GV); 00370 decorateName(Name, GV); 00371 00372 if (!isMemOp && !isCallOp) 00373 O << '$'; 00374 else if (Name[0] == '$') { 00375 // The name begins with a dollar-sign. In order to avoid having it look 00376 // like an integer immediate to the assembler, enclose it in parens. 00377 O << '('; 00378 needCloseParen = true; 00379 } 00380 00381 if (shouldPrintStub(TM, Subtarget)) { 00382 // Link-once, declaration, or Weakly-linked global variables need 00383 // non-lazily-resolved stubs 00384 if (GV->isDeclaration() || GV->mayBeOverridden()) { 00385 // Dynamically-resolved functions need a stub for the function. 00386 if (isCallOp && isa<Function>(GV)) { 00387 // Function stubs are no longer needed for Mac OS X 10.5 and up. 00388 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) { 00389 O << Name; 00390 } else { 00391 FnStubs.insert(Name); 00392 printSuffixedName(Name, "$stub"); 00393 } 00394 } else if (GV->hasHiddenVisibility()) { 00395 if (!GV->isDeclaration() && !GV->hasCommonLinkage()) 00396 // Definition is not definitely in the current translation unit. 00397 O << Name; 00398 else { 00399 HiddenGVStubs.insert(Name); 00400 printSuffixedName(Name, "$non_lazy_ptr"); 00401 } 00402 } else { 00403 GVStubs.insert(Name); 00404 printSuffixedName(Name, "$non_lazy_ptr"); 00405 } 00406 } else { 00407 if (GV->hasDLLImportLinkage()) 00408 O << "__imp_"; 00409 O << Name; 00410 } 00411 00412 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_) 00413 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget); 00414 } else { 00415 if (GV->hasDLLImportLinkage()) { 00416 O << "__imp_"; 00417 } 00418 O << Name; 00419 00420 if (isCallOp) { 00421 if (shouldPrintPLT(TM, Subtarget)) { 00422 // Assemble call via PLT for externally visible symbols 00423 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() && 00424 !GV->hasInternalLinkage()) 00425 O << "@PLT"; 00426 } 00427 if (Subtarget->isTargetCygMing() && GV->isDeclaration()) 00428 // Save function name for later type emission 00429 FnStubs.insert(Name); 00430 } 00431 } 00432 00433 if (GV->hasExternalWeakLinkage()) 00434 ExtWeakSymbols.insert(GV); 00435 00436 printOffset(MO.getOffset()); 00437 00438 if (isThreadLocal) { 00439 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit()) 00440 O << "@TLSGD"; // general dynamic TLS model 00441 else 00442 if (GV->isDeclaration()) 00443 O << "@INDNTPOFF"; // initial exec TLS model 00444 else 00445 O << "@NTPOFF"; // local exec TLS model 00446 } else if (isMemOp) { 00447 if (shouldPrintGOT(TM, Subtarget)) { 00448 if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) 00449 O << "@GOT"; 00450 else 00451 O << "@GOTOFF"; 00452 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel && 00453 TM.getRelocationModel() != Reloc::Static) { 00454 if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) 00455 O << "@GOTPCREL"; 00456 00457 if (needCloseParen) { 00458 needCloseParen = false; 00459 O << ')'; 00460 } 00461 00462 // Use rip when possible to reduce code size, except when 00463 // index or base register are also part of the address. e.g. 00464 // foo(%rip)(%rcx,%rax,4) is not legal 00465 O << "(%rip)"; 00466 } 00467 } 00468 00469 if (needCloseParen) 00470 O << ')'; 00471 00472 return; 00473 } 00474 case MachineOperand::MO_ExternalSymbol: { 00475 bool isCallOp = Modifier && !strcmp(Modifier, "call"); 00476 bool needCloseParen = false; 00477 std::string Name(TAI->getGlobalPrefix()); 00478 Name += MO.getSymbolName(); 00479 // Print function stub suffix unless it's Mac OS X 10.5 and up. 00480 if (isCallOp && shouldPrintStub(TM, Subtarget) && 00481 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) { 00482 FnStubs.insert(Name); 00483 printSuffixedName(Name, "$stub"); 00484 return; 00485 } 00486 if (!isCallOp) 00487 O << '$'; 00488 else if (Name[0] == '$') { 00489 // The name begins with a dollar-sign. In order to avoid having it look 00490 // like an integer immediate to the assembler, enclose it in parens. 00491 O << '('; 00492 needCloseParen = true; 00493 } 00494 00495 O << Name; 00496 00497 if (shouldPrintPLT(TM, Subtarget)) { 00498 std::string GOTName(TAI->getGlobalPrefix()); 00499 GOTName+="_GLOBAL_OFFSET_TABLE_"; 00500 if (Name == GOTName) 00501 // HACK! Emit extra offset to PC during printing GOT offset to 00502 // compensate for the size of popl instruction. The resulting code 00503 // should look like: 00504 // call .piclabel 00505 // piclabel: 00506 // popl %some_register 00507 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register 00508 O << " + [.-" 00509 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']'; 00510 00511 if (isCallOp) 00512 O << "@PLT"; 00513 } 00514 00515 if (needCloseParen) 00516 O << ')'; 00517 00518 if (!isCallOp && Subtarget->isPICStyleRIPRel()) 00519 O << "(%rip)"; 00520 00521 return; 00522 } 00523 default: 00524 O << "<unknown operand type>"; return; 00525 } 00526 } 00527 00528 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { 00529 unsigned char value = MI->getOperand(Op).getImm(); 00530 assert(value <= 7 && "Invalid ssecc argument!"); 00531 switch (value) { 00532 case 0: O << "eq"; break; 00533 case 1: O << "lt"; break; 00534 case 2: O << "le"; break; 00535 case 3: O << "unord"; break; 00536 case 4: O << "neq"; break; 00537 case 5: O << "nlt"; break; 00538 case 6: O << "nle"; break; 00539 case 7: O << "ord"; break; 00540 } 00541 } 00542 00543 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op, 00544 const char *Modifier){ 00545 assert(isMem(MI, Op) && "Invalid memory reference!"); 00546 MachineOperand BaseReg = MI->getOperand(Op); 00547 MachineOperand IndexReg = MI->getOperand(Op+2); 00548 const MachineOperand &DispSpec = MI->getOperand(Op+3); 00549 00550 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg(); 00551 if (DispSpec.isGlobal() || 00552 DispSpec.isCPI() || 00553 DispSpec.isJTI()) { 00554 printOperand(MI, Op+3, "mem", NotRIPRel); 00555 } else { 00556 int DispVal = DispSpec.getImm(); 00557 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) 00558 O << DispVal; 00559 } 00560 00561 if (IndexReg.getReg() || BaseReg.getReg()) { 00562 unsigned ScaleVal = MI->getOperand(Op+1).getImm(); 00563 unsigned BaseRegOperand = 0, IndexRegOperand = 2; 00564 00565 // There are cases where we can end up with ESP/RSP in the indexreg slot. 00566 // If this happens, swap the base/index register to support assemblers that 00567 // don't work when the index is *SP. 00568 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) { 00569 assert(ScaleVal == 1 && "Scale not supported for stack pointer!"); 00570 std::swap(BaseReg, IndexReg); 00571 std::swap(BaseRegOperand, IndexRegOperand); 00572 } 00573 00574 O << '('; 00575 if (BaseReg.getReg()) 00576 printOperand(MI, Op+BaseRegOperand, Modifier); 00577 00578 if (IndexReg.getReg()) { 00579 O << ','; 00580 printOperand(MI, Op+IndexRegOperand, Modifier); 00581 if (ScaleVal != 1) 00582 O << ',' << ScaleVal; 00583 } 00584 O << ')'; 00585 } 00586 } 00587 00588 void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid, 00589 const MachineBasicBlock *MBB) const { 00590 if (!TAI->getSetDirective()) 00591 return; 00592 00593 // We don't need .set machinery if we have GOT-style relocations 00594 if (Subtarget->isPICStyleGOT()) 00595 return; 00596 00597 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 00598 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; 00599 printBasicBlockLabel(MBB, false, false, false); 00600 if (Subtarget->isPICStyleRIPRel()) 00601 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 00602 << '_' << uid << '\n'; 00603 else 00604 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n'; 00605 } 00606 00607 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) { 00608 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget); 00609 O << label << '\n' << label << ':'; 00610 } 00611 00612 00613 void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 00614 const MachineBasicBlock *MBB, 00615 unsigned uid) const 00616 { 00617 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ? 00618 TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); 00619 00620 O << JTEntryDirective << ' '; 00621 00622 if (TM.getRelocationModel() == Reloc::PIC_) { 00623 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) { 00624 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 00625 << '_' << uid << "_set_" << MBB->getNumber(); 00626 } else if (Subtarget->isPICStyleGOT()) { 00627 printBasicBlockLabel(MBB, false, false, false); 00628 O << "@GOTOFF"; 00629 } else 00630 assert(0 && "Don't know how to print MBB label for this PIC mode"); 00631 } else 00632 printBasicBlockLabel(MBB, false, false, false); 00633 } 00634 00635 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, 00636 const char Mode) { 00637 unsigned Reg = MO.getReg(); 00638 switch (Mode) { 00639 default: return true; // Unknown mode. 00640 case 'b': // Print QImode register 00641 Reg = getX86SubSuperRegister(Reg, MVT::i8); 00642 break; 00643 case 'h': // Print QImode high register 00644 Reg = getX86SubSuperRegister(Reg, MVT::i8, true); 00645 break; 00646 case 'w': // Print HImode register 00647 Reg = getX86SubSuperRegister(Reg, MVT::i16); 00648 break; 00649 case 'k': // Print SImode register 00650 Reg = getX86SubSuperRegister(Reg, MVT::i32); 00651 break; 00652 case 'q': // Print DImode register 00653 Reg = getX86SubSuperRegister(Reg, MVT::i64); 00654 break; 00655 } 00656 00657 O << '%'<< TRI->getAsmName(Reg); 00658 return false; 00659 } 00660 00661 /// PrintAsmOperand - Print out an operand for an inline asm expression. 00662 /// 00663 bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00664 unsigned AsmVariant, 00665 const char *ExtraCode) { 00666 // Does this asm operand have a single letter operand modifier? 00667 if (ExtraCode && ExtraCode[0]) { 00668 if (ExtraCode[1] != 0) return true; // Unknown modifier. 00669 00670 switch (ExtraCode[0]) { 00671 default: return true; // Unknown modifier. 00672 case 'c': // Don't print "$" before a global var name or constant. 00673 printOperand(MI, OpNo, "mem"); 00674 return false; 00675 case 'b': // Print QImode register 00676 case 'h': // Print QImode high register 00677 case 'w': // Print HImode register 00678 case 'k': // Print SImode register 00679 case 'q': // Print DImode register 00680 if (MI->getOperand(OpNo).isReg()) 00681 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]); 00682 printOperand(MI, OpNo); 00683 return false; 00684 00685 case 'P': // Don't print @PLT, but do print as memory. 00686 printOperand(MI, OpNo, "mem"); 00687 return false; 00688 } 00689 } 00690 00691 printOperand(MI, OpNo); 00692 return false; 00693 } 00694 00695 bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 00696 unsigned OpNo, 00697 unsigned AsmVariant, 00698 const char *ExtraCode) { 00699 if (ExtraCode && ExtraCode[0]) { 00700 if (ExtraCode[1] != 0) return true; // Unknown modifier. 00701 00702 switch (ExtraCode[0]) { 00703 default: return true; // Unknown modifier. 00704 case 'b': // Print QImode register 00705 case 'h': // Print QImode high register 00706 case 'w': // Print HImode register 00707 case 'k': // Print SImode register 00708 case 'q': // Print SImode register 00709 // These only apply to registers, ignore on mem. 00710 break; 00711 } 00712 } 00713 printMemReference(MI, OpNo); 00714 return false; 00715 } 00716 00717 /// printMachineInstruction -- Print out a single X86 LLVM instruction 00718 /// MI in AT&T syntax to the current output stream. 00719 /// 00720 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00721 ++EmittedInsts; 00722 00723 // Call the autogenerated instruction printer routines. 00724 printInstruction(MI); 00725 } 00726 00727 /// doInitialization 00728 bool X86ATTAsmPrinter::doInitialization(Module &M) { 00729 if (TAI->doesSupportDebugInformation()) { 00730 // Emit initial debug information. 00731 DW.BeginModule(&M); 00732 } 00733 00734 bool Result = AsmPrinter::doInitialization(M); 00735 00736 if (TAI->doesSupportDebugInformation()) { 00737 // Let PassManager know we need debug information and relay 00738 // the MachineModuleInfo address on to DwarfWriter. 00739 // AsmPrinter::doInitialization did this analysis. 00740 MMI = getAnalysisToUpdate<MachineModuleInfo>(); 00741 DW.SetModuleInfo(MMI); 00742 } 00743 00744 // Darwin wants symbols to be quoted if they have complex names. 00745 if (