LLVM API Documentation
00001 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// 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 implements the AsmPrinter class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/AsmPrinter.h" 00015 #include "llvm/Assembly/Writer.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/CodeGen/GCMetadataPrinter.h" 00020 #include "llvm/CodeGen/MachineConstantPool.h" 00021 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00022 #include "llvm/CodeGen/MachineModuleInfo.h" 00023 #include "llvm/CodeGen/DwarfWriter.h" 00024 #include "llvm/Support/Mangler.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 #include "llvm/Target/TargetAsmInfo.h" 00027 #include "llvm/Target/TargetData.h" 00028 #include "llvm/Target/TargetLowering.h" 00029 #include "llvm/Target/TargetMachine.h" 00030 #include "llvm/Target/TargetOptions.h" 00031 #include "llvm/Target/TargetRegisterInfo.h" 00032 #include "llvm/ADT/SmallPtrSet.h" 00033 #include "llvm/ADT/SmallString.h" 00034 #include "llvm/ADT/StringExtras.h" 00035 #include <cerrno> 00036 using namespace llvm; 00037 00038 char AsmPrinter::ID = 0; 00039 AsmPrinter::AsmPrinter(raw_ostream &o, TargetMachine &tm, 00040 const TargetAsmInfo *T) 00041 : MachineFunctionPass(&ID), FunctionNumber(0), O(o), 00042 TM(tm), TAI(T), TRI(tm.getRegisterInfo()), 00043 IsInTextSection(false) 00044 {} 00045 00046 AsmPrinter::~AsmPrinter() { 00047 for (gcp_iterator I = GCMetadataPrinters.begin(), 00048 E = GCMetadataPrinters.end(); I != E; ++I) 00049 delete I->second; 00050 } 00051 00052 /// SwitchToTextSection - Switch to the specified text section of the executable 00053 /// if we are not already in it! 00054 /// 00055 void AsmPrinter::SwitchToTextSection(const char *NewSection, 00056 const GlobalValue *GV) { 00057 std::string NS; 00058 if (GV && GV->hasSection()) 00059 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 00060 else 00061 NS = NewSection; 00062 00063 // If we're already in this section, we're done. 00064 if (CurrentSection == NS) return; 00065 00066 // Close the current section, if applicable. 00067 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 00068 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 00069 00070 CurrentSection = NS; 00071 00072 if (!CurrentSection.empty()) 00073 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n'; 00074 00075 IsInTextSection = true; 00076 } 00077 00078 /// SwitchToDataSection - Switch to the specified data section of the executable 00079 /// if we are not already in it! 00080 /// 00081 void AsmPrinter::SwitchToDataSection(const char *NewSection, 00082 const GlobalValue *GV) { 00083 std::string NS; 00084 if (GV && GV->hasSection()) 00085 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 00086 else 00087 NS = NewSection; 00088 00089 // If we're already in this section, we're done. 00090 if (CurrentSection == NS) return; 00091 00092 // Close the current section, if applicable. 00093 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 00094 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 00095 00096 CurrentSection = NS; 00097 00098 if (!CurrentSection.empty()) 00099 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n'; 00100 00101 IsInTextSection = false; 00102 } 00103 00104 /// SwitchToSection - Switch to the specified section of the executable if we 00105 /// are not already in it! 00106 void AsmPrinter::SwitchToSection(const Section* NS) { 00107 const std::string& NewSection = NS->getName(); 00108 00109 // If we're already in this section, we're done. 00110 if (CurrentSection == NewSection) return; 00111 00112 // Close the current section, if applicable. 00113 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 00114 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 00115 00116 // FIXME: Make CurrentSection a Section* in the future 00117 CurrentSection = NewSection; 00118 CurrentSection_ = NS; 00119 00120 if (!CurrentSection.empty()) { 00121 // If section is named we need to switch into it via special '.section' 00122 // directive and also append funky flags. Otherwise - section name is just 00123 // some magic assembler directive. 00124 if (NS->isNamed()) 00125 O << TAI->getSwitchToSectionDirective() 00126 << CurrentSection 00127 << TAI->getSectionFlags(NS->getFlags()); 00128 else 00129 O << CurrentSection; 00130 O << TAI->getDataSectionStartSuffix() << '\n'; 00131 } 00132 00133 IsInTextSection = (NS->getFlags() & SectionFlags::Code); 00134 } 00135 00136 void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { 00137 MachineFunctionPass::getAnalysisUsage(AU); 00138 AU.addRequired<GCModuleInfo>(); 00139 } 00140 00141 bool AsmPrinter::doInitialization(Module &M) { 00142 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix()); 00143 00144 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 00145 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 00146 00147 if (TAI->hasSingleParameterDotFile()) { 00148 /* Very minimal debug info. It is ignored if we emit actual 00149 debug info. If we don't, this at helps the user find where 00150 a function came from. */ 00151 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n"; 00152 } 00153 00154 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) 00155 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) 00156 MP->beginAssembly(O, *this, *TAI); 00157 00158 if (!M.getModuleInlineAsm().empty()) 00159 O << TAI->getCommentString() << " Start of file scope inline assembly\n" 00160 << M.getModuleInlineAsm() 00161 << '\n' << TAI->getCommentString() 00162 << " End of file scope inline assembly\n"; 00163 00164 SwitchToDataSection(""); // Reset back to no section. 00165 00166 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00167 if (MMI) MMI->AnalyzeModule(M); 00168 DW = getAnalysisIfAvailable<DwarfWriter>(); 00169 return false; 00170 } 00171 00172 bool AsmPrinter::doFinalization(Module &M) { 00173 if (TAI->getWeakRefDirective()) { 00174 if (!ExtWeakSymbols.empty()) 00175 SwitchToDataSection(""); 00176 00177 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(), 00178 e = ExtWeakSymbols.end(); i != e; ++i) { 00179 const GlobalValue *GV = *i; 00180 std::string Name = Mang->getValueName(GV); 00181 O << TAI->getWeakRefDirective() << Name << '\n'; 00182 } 00183 } 00184 00185 if (TAI->getSetDirective()) { 00186 if (!M.alias_empty()) 00187 SwitchToSection(TAI->getTextSection()); 00188 00189 O << '\n'; 00190 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); 00191 I!=E; ++I) { 00192 std::string Name = Mang->getValueName(I); 00193 std::string Target; 00194 00195 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); 00196 Target = Mang->getValueName(GV); 00197 00198 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) 00199 O << "\t.globl\t" << Name << '\n'; 00200 else if (I->hasWeakLinkage()) 00201 O << TAI->getWeakRefDirective() << Name << '\n'; 00202 else if (!I->hasLocalLinkage()) 00203 assert(0 && "Invalid alias linkage"); 00204 00205 printVisibility(Name, I->getVisibility()); 00206 00207 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; 00208 00209 // If the aliasee has external weak linkage it can be referenced only by 00210 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit 00211 // weak reference in such case. 00212 if (GV->hasExternalWeakLinkage()) { 00213 if (TAI->getWeakRefDirective()) 00214 O << TAI->getWeakRefDirective() << Target << '\n'; 00215 else 00216 O << "\t.globl\t" << Target << '\n'; 00217 } 00218 } 00219 } 00220 00221 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 00222 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 00223 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) 00224 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) 00225 MP->finishAssembly(O, *this, *TAI); 00226 00227 // If we don't have any trampolines, then we don't require stack memory 00228 // to be executable. Some targets have a directive to declare this. 00229 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); 00230 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) 00231 if (TAI->getNonexecutableStackDirective()) 00232 O << TAI->getNonexecutableStackDirective() << '\n'; 00233 00234 delete Mang; Mang = 0; 00235 return false; 00236 } 00237 00238 std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) { 00239 assert(MF && "No machine function?"); 00240 std::string Name = MF->getFunction()->getName(); 00241 if (Name.empty()) 00242 Name = Mang->getValueName(MF->getFunction()); 00243 return Mang->makeNameProper(TAI->getEHGlobalPrefix() + 00244 Name + ".eh", TAI->getGlobalPrefix()); 00245 } 00246 00247 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { 00248 // What's my mangled name? 00249 CurrentFnName = Mang->getValueName(MF.getFunction()); 00250 IncrementFunctionNumber(); 00251 } 00252 00253 /// EmitConstantPool - Print to the current output stream assembly 00254 /// representations of the constants in the constant pool MCP. This is 00255 /// used to print out constants which have been "spilled to memory" by 00256 /// the code generator. 00257 /// 00258 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { 00259 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); 00260 if (CP.empty()) return; 00261 00262 // Calculate sections for constant pool entries. We collect entries to go into 00263 // the same section together to reduce amount of section switch statements. 00264 typedef 00265 std::multimap<const Section*, 00266 std::pair<MachineConstantPoolEntry, unsigned> > CPMap; 00267 CPMap CPs; 00268 SmallPtrSet<const Section*, 5> Sections; 00269 00270 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 00271 MachineConstantPoolEntry CPE = CP[i]; 00272 const Section* S = TAI->SelectSectionForMachineConst(CPE.getType()); 00273 CPs.insert(std::make_pair(S, std::make_pair(CPE, i))); 00274 Sections.insert(S); 00275 } 00276 00277 // Now print stuff into the calculated sections. 00278 for (SmallPtrSet<const Section*, 5>::iterator IS = Sections.begin(), 00279 ES = Sections.end(); IS != ES; ++IS) { 00280 SwitchToSection(*IS); 00281 EmitAlignment(MCP->getConstantPoolAlignment()); 00282 00283 std::pair<CPMap::iterator, CPMap::iterator> II = CPs.equal_range(*IS); 00284 for (CPMap::iterator I = II.first, E = II.second; I != E; ++I) { 00285 CPMap::iterator J = next(I); 00286 MachineConstantPoolEntry Entry = I->second.first; 00287 unsigned index = I->second.second; 00288 00289 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 00290 << index << ":\t\t\t\t\t"; 00291 // O << TAI->getCommentString() << ' ' << 00292 // WriteTypeSymbolic(O, CP[i].first.getType(), 0); 00293 O << '\n'; 00294 if (Entry.isMachineConstantPoolEntry()) 00295 EmitMachineConstantPoolValue(Entry.Val.MachineCPVal); 00296 else 00297 EmitGlobalConstant(Entry.Val.ConstVal); 00298 00299 // Emit inter-object padding for alignment. 00300 if (J != E) { 00301 const Type *Ty = Entry.getType(); 00302 unsigned EntSize = TM.getTargetData()->getTypePaddedSize(Ty); 00303 unsigned ValEnd = Entry.getOffset() + EntSize; 00304 EmitZeros(J->second.first.getOffset()-ValEnd); 00305 } 00306 } 00307 } 00308 } 00309 00310 /// EmitJumpTableInfo - Print assembly representations of the jump tables used 00311 /// by the current function to the current output stream. 00312 /// 00313 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, 00314 MachineFunction &MF) { 00315 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 00316 if (JT.empty()) return; 00317 00318 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 00319 00320 // Pick the directive to use to print the jump table entries, and switch to 00321 // the appropriate section. 00322 TargetLowering *LoweringInfo = TM.getTargetLowering(); 00323 00324 const char* JumpTableDataSection = TAI->getJumpTableDataSection(); 00325 const Function *F = MF.getFunction(); 00326 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F); 00327 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) || 00328 !JumpTableDataSection || 00329 SectionFlags & SectionFlags::Linkonce) { 00330 // In PIC mode, we need to emit the jump table to the same section as the 00331 // function body itself, otherwise the label differences won't make sense. 00332 // We should also do if the section name is NULL or function is declared in 00333 // discardable section. 00334 SwitchToSection(TAI->SectionForGlobal(F)); 00335 } else { 00336 SwitchToDataSection(JumpTableDataSection); 00337 } 00338 00339 EmitAlignment(Log2_32(MJTI->getAlignment())); 00340 00341 for (unsigned i = 0, e = JT.size(); i != e; ++i) { 00342 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; 00343 00344 // If this jump table was deleted, ignore it. 00345 if (JTBBs.empty()) continue; 00346 00347 // For PIC codegen, if possible we want to use the SetDirective to reduce 00348 // the number of relocations the assembler will generate for the jump table. 00349 // Set directives are all printed before the jump table itself. 00350 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; 00351 if (TAI->getSetDirective() && IsPic) 00352 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) 00353 if (EmittedSets.insert(JTBBs[ii])) 00354 printPICJumpTableSetLabel(i, JTBBs[ii]); 00355 00356 // On some targets (e.g. darwin) we want to emit two consequtive labels 00357 // before each jump table. The first label is never referenced, but tells 00358 // the assembler and linker the extents of the jump table object. The 00359 // second label is actually referenced by the code. 00360 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) 00361 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; 00362 00363 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 00364 << '_' << i << ":\n"; 00365 00366 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { 00367 printPICJumpTableEntry(MJTI, JTBBs[ii], i); 00368 O << '\n'; 00369 } 00370 } 00371 } 00372 00373 void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 00374 const MachineBasicBlock *MBB, 00375 unsigned uid) const { 00376 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 00377 00378 // Use JumpTableDirective otherwise honor the entry size from the jump table 00379 // info. 00380 const char *JTEntryDirective = TAI->getJumpTableDirective(); 00381 bool HadJTEntryDirective = JTEntryDirective != NULL; 00382 if (!HadJTEntryDirective) { 00383 JTEntryDirective = MJTI->getEntrySize() == 4 ? 00384 TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); 00385 } 00386 00387 O << JTEntryDirective << ' '; 00388 00389 // If we have emitted set directives for the jump table entries, print 00390 // them rather than the entries themselves. If we're emitting PIC, then 00391 // emit the table entries as differences between two text section labels. 00392 // If we're emitting non-PIC code, then emit the entries as direct 00393 // references to the target basic blocks. 00394 if (IsPic) { 00395 if (TAI->getSetDirective()) { 00396 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 00397 << '_' << uid << "_set_" << MBB->getNumber(); 00398 } else { 00399 printBasicBlockLabel(MBB, false, false, false); 00400 // If the arch uses custom Jump Table directives, don't calc relative to 00401 // JT 00402 if (!HadJTEntryDirective) 00403 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" 00404 << getFunctionNumber() << '_' << uid; 00405 } 00406 } else { 00407 printBasicBlockLabel(MBB, false, false, false); 00408 } 00409 } 00410 00411 00412 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 00413 /// special global used by LLVM. If so, emit it and return true, otherwise 00414 /// do nothing and return false. 00415 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { 00416 if (GV->getName() == "llvm.used") { 00417 if (TAI->getUsedDirective() != 0) // No need to emit this at all. 00418 EmitLLVMUsedList(GV->getInitializer()); 00419 return true; 00420 } 00421 00422 // Ignore debug and non-emitted data. 00423 if (GV->getSection() == "llvm.metadata") return true; 00424 00425 if (!GV->hasAppendingLinkage()) return false; 00426 00427 assert(GV->hasInitializer() && "Not a special LLVM global!"); 00428 00429 const TargetData *TD = TM.getTargetData(); 00430 unsigned Align = Log2_32(TD->getPointerPrefAlignment()); 00431 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { 00432 SwitchToDataSection(TAI->getStaticCtorsSection()); 00433 EmitAlignment(Align, 0); 00434 EmitXXStructorList(GV->getInitializer()); 00435 return true; 00436 } 00437 00438 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { 00439 SwitchToDataSection(TAI->getStaticDtorsSection()); 00440 EmitAlignment(Align, 0); 00441 EmitXXStructorList(GV->getInitializer()); 00442 return true; 00443 } 00444 00445 return false; 00446 } 00447 00448 /// findGlobalValue - if CV is an expression equivalent to a single 00449 /// global value, return that value. 00450 const GlobalValue * AsmPrinter::findGlobalValue(const Constant *CV) { 00451 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) 00452 return GV; 00453 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 00454 const TargetData *TD = TM.getTargetData(); 00455 unsigned Opcode = CE->getOpcode(); 00456 switch (Opcode) { 00457 case Instruction::GetElementPtr: { 00458 const Constant *ptrVal = CE->getOperand(0); 00459 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); 00460 if (TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], idxVec.size())) 00461 return 0; 00462 return findGlobalValue(ptrVal); 00463 } 00464 case Instruction::BitCast: 00465 return findGlobalValue(CE->getOperand(0)); 00466 default: 00467 return 0; 00468 } 00469 } 00470 return 0; 00471 } 00472 00473 /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each 00474 /// global in the specified llvm.used list for which emitUsedDirectiveFor 00475 /// is true, as being used with this directive. 00476 00477 void AsmPrinter::EmitLLVMUsedList(Constant *List) { 00478 const char *Directive = TAI->getUsedDirective(); 00479 00480 // Should be an array of 'sbyte*'. 00481 ConstantArray *InitList = dyn_cast<ConstantArray>(List); 00482 if (InitList == 0) return; 00483 00484 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 00485 const GlobalValue *GV = findGlobalValue(InitList->getOperand(i)); 00486 if (TAI->emitUsedDirectiveFor(GV, Mang)) { 00487 O << Directive; 00488 EmitConstantValueOnly(InitList->getOperand(i)); 00489 O << '\n'; 00490 } 00491 } 00492 } 00493 00494 /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the 00495 /// function pointers, ignoring the init priority. 00496 void AsmPrinter::EmitXXStructorList(Constant *List) { 00497 // Should be an array of '{ int, void ()* }' structs. The first value is the 00498 // init priority, which we ignore. 00499 if (!isa<ConstantArray>(List)) return; 00500 ConstantArray *InitList = cast<ConstantArray>(List); 00501 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 00502 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 00503 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 00504 00505 if (CS->getOperand(1)->isNullValue()) 00506 return; // Found a null terminator, exit printing. 00507 // Emit the function pointer. 00508 EmitGlobalConstant(CS->getOperand(1)); 00509 } 00510 } 00511 00512 /// getGlobalLinkName - Returns the asm/link name of of the specified 00513 /// global variable. Should be overridden by each target asm printer to 00514 /// generate the appropriate value. 00515 const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{ 00516 std::string LinkName; 00517 00518 if (isa<Function>(GV)) { 00519 LinkName += TAI->getFunctionAddrPrefix(); 00520 LinkName += Mang->getValueName(GV); 00521 LinkName += TAI->getFunctionAddrSuffix(); 00522 } else { 00523 LinkName += TAI->getGlobalVarAddrPrefix(); 00524 LinkName += Mang->getValueName(GV); 00525 LinkName += TAI->getGlobalVarAddrSuffix(); 00526 } 00527 00528 return LinkName; 00529 } 00530 00531 /// EmitExternalGlobal - Emit the external reference to a global variable. 00532 /// Should be overridden if an indirect reference should be used. 00533 void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { 00534 O << getGlobalLinkName(GV); 00535 } 00536 00537 00538 00539 //===----------------------------------------------------------------------===// 00540 /// LEB 128 number encoding. 00541 00542 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) 00543 /// representing an unsigned leb128 value. 00544 void AsmPrinter::PrintULEB128(unsigned Value) const { 00545 char Buffer[20]; 00546 do { 00547 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 00548 Value >>= 7; 00549 if (Value) Byte |= 0x80; 00550 O << "0x" << utohex_buffer(Byte, Buffer+20); 00551 if (Value) O << ", "; 00552 } while (Value); 00553 } 00554 00555 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) 00556 /// representing a signed leb128 value. 00557 void AsmPrinter::PrintSLEB128(int Value) const { 00558 int Sign = Value >> (8 * sizeof(Value) - 1); 00559 bool IsMore; 00560 char Buffer[20]; 00561 00562 do { 00563 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 00564 Value >>= 7; 00565 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 00566 if (IsMore) Byte |= 0x80; 00567 O << "0x" << utohex_buffer(Byte, Buffer+20); 00568 if (IsMore) O << ", "; 00569 } while (IsMore); 00570 } 00571 00572 //===--------------------------------------------------------------------===// 00573 // Emission and print routines 00574 // 00575 00576 /// PrintHex - Print a value as a hexidecimal value. 00577 /// 00578 void AsmPrinter::PrintHex(int Value) const { 00579 char Buffer[20]; 00580 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20); 00581 } 00582 00583 /// EOL - Print a newline character to asm stream. If a comment is present 00584 /// then it will be printed first. Comments should not contain '\n'. 00585 void AsmPrinter::EOL() const { 00586 O << '\n'; 00587 } 00588 00589 void AsmPrinter::EOL(const std::string &Comment) const { 00590 if (VerboseAsm && !Comment.empty()) { 00591 O << '\t' 00592 << TAI->getCommentString() 00593 << ' ' 00594 << Comment; 00595 } 00596 O << '\n'; 00597 } 00598 00599 void AsmPrinter::EOL(const char* Comment) const { 00600 if (VerboseAsm && *Comment) { 00601 O << '\t' 00602 << TAI->getCommentString() 00603 << ' ' 00604 << Comment; 00605 } 00606 O << '\n'; 00607 } 00608 00609 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 00610 /// unsigned leb128 value. 00611 void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { 00612 if (TAI->hasLEB128()) { 00613 O << "\t.uleb128\t" 00614 << Value; 00615 } else { 00616 O << TAI->getData8bitsDirective(); 00617 PrintULEB128(Value); 00618 } 00619 } 00620 00621 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 00622 /// signed leb128 value. 00623 void AsmPrinter::EmitSLEB128Bytes(int Value) const { 00624 if (TAI->hasLEB128()) { 00625 O << "\t.sleb128\t" 00626 << Value; 00627 } else { 00628 O << TAI->getData8bitsDirective(); 00629 PrintSLEB128(Value); 00630 } 00631 } 00632 00633 /// EmitInt8 - Emit a byte directive and value. 00634 /// 00635 void AsmPrinter::EmitInt8(int Value) const { 00636 O << TAI->getData8bitsDirective(); 00637 PrintHex(Value & 0xFF); 00638 } 00639 00640 /// EmitInt16 - Emit a short directive and value. 00641 /// 00642 void AsmPrinter::EmitInt16(int Value) const { 00643 O << TAI->getData16bitsDirective(); 00644 PrintHex(Value & 0xFFFF); 00645 } 00646 00647 /// EmitInt32 - Emit a long directive and value. 00648 /// 00649 void AsmPrinter::EmitInt32(int Value) const { 00650 O << TAI->getData32bitsDirective(); 00651 PrintHex(Value); 00652 } 00653 00654 /// EmitInt64 - Emit a long long directive and value. 00655 /// 00656 void AsmPrinter::EmitInt64(uint64_t Value) const { 00657 if (TAI->getData64bitsDirective()) { 00658 O << TAI->getData64bitsDirective(); 00659 PrintHex(Value); 00660 } else { 00661 if (TM.getTargetData()->isBigEndian()) { 00662 EmitInt32(unsigned(Value >> 32)); O << '\n'; 00663 EmitInt32(unsigned(Value)); 00664 } else { 00665 EmitInt32(unsigned(Value)); O << '\n'; 00666 EmitInt32(unsigned(Value >> 32)); 00667 } 00668 } 00669 } 00670 00671 /// toOctal - Convert the low order bits of X into an octal digit. 00672 /// 00673 static inline char toOctal(int X) { 00674 return (X&7)+'0'; 00675 } 00676 00677 /// printStringChar - Print a char, escaped if necessary. 00678 /// 00679 static void printStringChar(raw_ostream &O, char C) { 00680 if (C == '"') { 00681 O << "\\\""; 00682 } else if (C == '\\') { 00683 O << "\\\\"; 00684 } else if (isprint((unsigned char)C)) { 00685 O << C; 00686 } else { 00687 switch(C) { 00688 case '\b': O << "\\b"; break; 00689 case '\f': O << "\\f"; break; 00690 case '\n': O << "\\n"; break; 00691 case '\r': O << "\\r"; break; 00692 case '\t': O << "\\t"; break; 00693 default: 00694 O << '\\'; 00695 O << toOctal(C >> 6); 00696 O << toOctal(C >> 3); 00697 O << toOctal(C >> 0); 00698 break; 00699 } 00700 } 00701 } 00702 00703 /// EmitString - Emit a string with quotes and a null terminator. 00704 /// Special characters are emitted properly. 00705 /// \literal (Eg. '\t') \endliteral 00706 void AsmPrinter::EmitString(const std::string &String) const { 00707 const char* AscizDirective = TAI->getAscizDirective(); 00708 if (AscizDirective) 00709 O << AscizDirective; 00710 else 00711 O << TAI->getAsciiDirective(); 00712 O << '\"'; 00713 for (unsigned i = 0, N = String.size(); i < N; ++i) { 00714 unsigned char C = String[i]; 00715 printStringChar(O, C); 00716 } 00717 if (AscizDirective) 00718 O << '\"'; 00719 else 00720 O << "\\0\""; 00721 } 00722 00723 00724 /// EmitFile - Emit a .file directive. 00725 void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { 00726 O << "\t.file\t" << Number << " \""; 00727 for (unsigned i = 0, N = Name.size(); i < N; ++i) { 00728 unsigned char C = Name[i]; 00729 printStringChar(O, C); 00730 } 00731 O << '\"'; 00732 } 00733 00734 00735 //===----------------------------------------------------------------------===// 00736 00737 // EmitAlignment - Emit an alignment directive to the specified power of 00738 // two boundary. For example, if you pass in 3 here, you will get an 8 00739 // byte alignment. If a global value is specified, and if that global has 00740 // an explicit alignment requested, it will unconditionally override the 00741 // alignment request. However, if ForcedAlignBits is specified, this value 00742 // has final say: the ultimate alignment will be the max of ForcedAlignBits 00743 // and the alignment computed with NumBits and the global. 00744 // 00745 // The algorithm is: 00746 // Align = NumBits; 00747 // if (GV && GV->hasalignment) Align = GV->getalignment(); 00748 // Align = std::max(Align, ForcedAlignBits); 00749 // 00750 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, 00751 unsigned ForcedAlignBits, 00752 bool UseFillExpr) const { 00753 if (GV && GV->getAlignment()) 00754 NumBits = Log2_32(GV->getAlignment()); 00755 NumBits = std::max(NumBits, ForcedAlignBits); 00756 00757 if (NumBits == 0) return; // No need to emit alignment. 00758 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; 00759 O << TAI->getAlignDirective() << NumBits; 00760 00761 unsigned FillValue = TAI->getTextAlignFillValue(); 00762 UseFillExpr &= IsInTextSection && FillValue; 00763 if (UseFillExpr) { 00764 O << ','; 00765 PrintHex(FillValue); 00766 } 00767 O << '\n'; 00768 } 00769 00770 00771 /// EmitZeros - Emit a block of zeros. 00772 /// 00773 void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const { 00774 if (NumZeros) { 00775 if (TAI->getZeroDirective()) { 00776 O << TAI->getZeroDirective() << NumZeros; 00777 if (TAI->getZeroDirectiveSuffix()) 00778 O << TAI->getZeroDirectiveSuffix(); 00779 O << '\n'; 00780 } else { 00781 for (; NumZeros; --NumZeros) 00782 O << TAI->getData8bitsDirective(AddrSpace) << "0\n"; 00783 } 00784 } 00785 } 00786 00787 // Print out the specified constant, without a storage class. Only the 00788 // constants valid in constant expressions can occur here. 00789 void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { 00790 if (CV->isNullValue() || isa<UndefValue>(CV)) 00791 O << '0'; 00792 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 00793 O << CI->getZExtValue(); 00794 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 00795 // This is a constant address for a global variable or function. Use the 00796 // name of the variable or function as the address value, possibly 00797 // decorating it with GlobalVarAddrPrefix/Suffix or 00798 // FunctionAddrPrefix/Suffix (these all default to "" ) 00799 if (isa<Function>(GV)) { 00800 O << TAI->getFunctionAddrPrefix() 00801 << Mang->getValueName(GV) 00802 << TAI->getFunctionAddrSuffix(); 00803 } else { 00804 O << TAI->getGlobalVarAddrPrefix() 00805 << Mang->getValueName(GV) 00806 << TAI->getGlobalVarAddrSuffix(); 00807 } 00808 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 00809 const TargetData *TD = TM.getTargetData(); 00810 unsigned Opcode = CE->getOpcode(); 00811 switch (Opcode) { 00812 case Instruction::GetElementPtr: { 00813 // generate a symbolic expression for the byte address 00814 const Constant *ptrVal = CE->getOperand(0); 00815 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); 00816 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], 00817 idxVec.size())) { 00818 if (Offset) 00819 O << '('; 00820 EmitConstantValueOnly(ptrVal); 00821 if (Offset > 0) 00822 O << ") + " << Offset; 00823 else if (Offset < 0) 00824 O << ") - " << -Offset; 00825 } else { 00826 EmitConstantValueOnly(ptrVal); 00827 } 00828 break; 00829 } 00830 case Instruction::Trunc: 00831 case Instruction::ZExt: 00832 case Instruction::SExt: 00833 case Instruction::FPTrunc: 00834 case Instruction::FPExt: 00835 case Instruction::UIToFP: 00836 case Instruction::SIToFP: 00837 case Instruction::FPToUI: 00838 case Instruction::FPToSI: 00839 assert(0 && "FIXME: Don't yet support this kind of constant cast expr"); 00840 break; 00841 case Instruction::BitCast: 00842 return EmitConstantValueOnly(CE->getOperand(0)); 00843 00844 case Instruction::IntToPtr: { 00845 // Handle casts to pointers by changing them into casts to the appropriate 00846 // integer type. This promotes constant folding and simplifies this code. 00847 Constant *Op = CE->getOperand(0); 00848 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); 00849 return EmitConstantValueOnly(Op); 00850 } 00851 00852 00853 case Instruction::PtrToInt: { 00854 // Support only foldable casts to/from pointers that can be eliminated by 00855 // changing the pointer to the appropriately sized integer type. 00856 Constant *Op = CE->getOperand(0); 00857 const Type *Ty = CE->getType(); 00858 00859 // We can emit the pointer value into this slot if the slot is an 00860 // integer slot greater or equal to the size of the pointer. 00861 if (TD->getTypePaddedSize(Ty) >= TD->getTypePaddedSize(Op->getType())) 00862 return EmitConstantValueOnly(Op); 00863 00864 O << "(("; 00865 EmitConstantValueOnly(Op); 00866 APInt ptrMask = APInt::getAllOnesValue(TD->getTypePaddedSizeInBits(Ty)); 00867 00868 SmallString<40> S; 00869 ptrMask.toStringUnsigned(S); 00870 O << ") & " << S.c_str() << ')'; 00871 break; 00872 } 00873 case Instruction::Add: 00874 case Instruction::Sub: 00875 case Instruction::And: 00876 case Instruction::Or: 00877 case Instruction::Xor: 00878 O << '('; 00879 EmitConstantValueOnly(CE->getOperand(0)); 00880 O << ')'; 00881 switch (Opcode) { 00882 case Instruction::Add: 00883 O << " + "; 00884 break; 00885 case Instruction::Sub: 00886 O << " - "; 00887 break; 00888 case Instruction::And: 00889 O << " & "; 00890 break; 00891 case Instruction::Or: 00892 O << " | "; 00893 break; 00894 case Instruction::Xor: 00895 O << " ^ "; 00896 break; 00897 default: 00898 break; 00899 } 00900 O << '('; 00901 EmitConstantValueOnly(CE->getOperand(1)); 00902 O << ')'; 00903 break; 00904 default: 00905 assert(0 && "Unsupported operator!"); 00906 } 00907 } else { 00908 assert(0 && "Unknown constant value!"); 00909 } 00910 } 00911 00912 /// printAsCString - Print the specified array as a C compatible string, only if 00913 /// the predicate isString is true. 00914 /// 00915 static void printAsCString(raw_ostream &O, const ConstantArray *CVA, 00916 unsigned LastElt) { 00917 assert(CVA->isString() && "Array is not string compatible!"); 00918 00919 O << '\"'; 00920 for (unsigned i = 0; i != LastElt; ++i) { 00921 unsigned char C = 00922 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); 00923 printStringChar(O, C); 00924 } 00925 O << '\"'; 00926 } 00927 00928 /// EmitString - Emit a zero-byte-terminated string constant. 00929 /// 00930 void AsmPrinter::EmitString(const ConstantArray *CVA) const { 00931 unsigned NumElts = CVA->getNumOperands(); 00932 if (TAI->getAscizDirective() && NumElts && 00933 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { 00934 O << TAI->getAscizDirective(); 00935 printAsCString(O, CVA, NumElts-1); 00936 } else { 00937 O << TAI->getAsciiDirective(); 00938 printAsCString(O, CVA, NumElts); 00939 } 00940 O << '\n'; 00941 } 00942 00943 void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA) { 00944 if (CVA->isString()) { 00945 EmitString(CVA); 00946 } else { // Not a string. Print the values in successive locations 00947 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) 00948 EmitGlobalConstant(CVA->getOperand(i)); 00949 } 00950 } 00951 00952 void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) { 00953 const VectorType *PTy = CP->getType(); 00954 00955 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) 00956 EmitGlobalConstant(CP->getOperand(I)); 00957 } 00958 00959 void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS, 00960 unsigned AddrSpace) { 00961 // Print the fields in successive locations. Pad to align if needed! 00962 const TargetData *TD = TM.getTargetData(); 00963 unsigned Size = TD->getTypePaddedSize(CVS->getType()); 00964 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); 00965 uint64_t sizeSoFar = 0; 00966 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { 00967 const Constant* field = CVS->getOperand(i); 00968 00969 // Check if padding is needed and insert one or more 0s. 00970 uint64_t fieldSize = TD->getTypePaddedSize(field->getType()); 00971 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) 00972 - cvsLayout->getElementOffset(i)) - fieldSize; 00973 sizeSoFar += fieldSize + padSize; 00974 00975 // Now print the actual field value. 00976 EmitGlobalConstant(field, AddrSpace); 00977 00978 // Insert padding - this may include padding to increase the size of the 00979 // current field up to the ABI size (if the struct is not packed) as well 00980 // as padding to ensure that the next field starts at the right offset. 00981 EmitZeros(padSize, AddrSpace); 00982 } 00983 assert(sizeSoFar == cvsLayout->getSizeInBytes() && 00984 "Layout of constant struct may be incorrect!"); 00985 } 00986 00987 void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP, 00988 unsigned AddrSpace) { 00989 // FP Constants are printed as integer constants to avoid losing 00990 // precision... 00991 const TargetData *TD = TM.getTargetData(); 00992 if (CFP->getType() == Type::DoubleTy) { 00993 double Val = CFP->getValueAPF().convertToDouble(); // for comment only 00994 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 00995 if (TAI->getData64bitsDirective(AddrSpace)) 00996 O << TAI->getData64bitsDirective(AddrSpace) << i << '\t' 00997 << TAI->getCommentString() << " double value: " << Val << '\n'; 00998 else if (TD->isBigEndian()) { 00999 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32) 01000 << '\t' << TAI->getCommentString() 01001 << " double most significant word " << Val << '\n'; 01002 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i) 01003 << '\t' << TAI->getCommentString() 01004 << " double least significant word " << Val << '\n'; 01005 } else { 01006 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i) 01007 << '\t' << TAI->getCommentString() 01008 << " double least significant word " << Val << '\n'; 01009 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32) 01010 << '\t' << TAI->getCommentString() 01011 << " double most significant word " << Val << '\n'; 01012 } 01013 return; 01014 } else if (CFP->getType() == Type::FloatTy) { 01015 float Val = CFP->getValueAPF().convertToFloat(); // for comment only 01016 O << TAI->getData32bitsDirective(AddrSpace) 01017 << CFP->getValueAPF().bitcastToAPInt().getZExtValue() 01018 << '\t' << TAI->getCommentString() << " float " << Val << '\n'; 01019 return; 01020 } else if (CFP->getType() == Type::X86_FP80Ty) { 01021 // all long double variants are printed as hex 01022 // api needed to prevent premature destruction 01023 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01024 const uint64_t *p = api.getRawData(); 01025 // Convert to double so we can print the approximate val as a comment. 01026 APFloat DoubleVal = CFP->getValueAPF(); 01027 bool ignored; 01028 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, 01029 &ignored); 01030 if (TD->isBigEndian()) { 01031 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48) 01032 << '\t' << TAI->getCommentString() 01033 << " long double most significant halfword of ~" 01034 << DoubleVal.convertToDouble() << '\n'; 01035 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32) 01036 << '\t' << TAI->getCommentString() 01037 << " long double next halfword\n"; 01038 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16) 01039 << '\t' << TAI->getCommentString() 01040 << " long double next halfword\n"; 01041 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]) 01042 << '\t' << TAI->getCommentString() 01043 << " long double next halfword\n"; 01044 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]) 01045 << '\t' << TAI->getCommentString() 01046 << " long double least significant halfword\n"; 01047 } else { 01048 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]) 01049 << '\t' << TAI->getCommentString() 01050 << " long double least significant halfword of ~" 01051 << DoubleVal.convertToDouble() << '\n'; 01052 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]) 01053 << '\t' << TAI->getCommentString() 01054 << " long double next halfword\n"; 01055 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16) 01056 << '\t' << TAI->getCommentString() 01057 << " long double next halfword\n"; 01058 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32) 01059 << '\t' << TAI->getCommentString() 01060 << " long double next halfword\n"; 01061 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48) 01062 << '\t' << TAI->getCommentString() 01063 << " long double most significant halfword\n"; 01064 } 01065 EmitZeros(TD->getTypePaddedSize(Type::X86_FP80Ty) - 01066 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace); 01067 return; 01068 } else if (CFP->getType() == Type::PPC_FP128Ty) { 01069 // all long double variants are printed as hex 01070 // api needed to prevent premature destruction 01071 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01072 const uint64_t *p = api.getRawData(); 01073 if (TD->isBigEndian()) { 01074 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32) 01075 << '\t' << TAI->getCommentString() 01076 << " long double most significant word\n"; 01077 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]) 01078 << '\t' << TAI->getCommentString() 01079 << " long double next word\n"; 01080 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32) 01081 << '\t' << TAI->getCommentString() 01082 << " long double next word\n"; 01083 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]) 01084 << '\t' << TAI->getCommentString() 01085 << " long double least significant word\n"; 01086 } else { 01087 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]) 01088 << '\t' << TAI->getCommentString() 01089 << " long double least significant word\n"; 01090 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32) 01091 << '\t' << TAI->getCommentString() 01092 << " long double next word\n"; 01093 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]) 01094 << '\t' << TAI->getCommentString() 01095 << " long double next word\n"; 01096 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32) 01097 << '\t' << TAI->getCommentString() 01098 << " long double most significant word\n"; 01099 } 01100 return; 01101 } else assert(0 && "Floating point constant type not handled"); 01102 } 01103 01104 void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI, 01105 unsigned AddrSpace) { 01106 const TargetData *TD = TM.getTargetData(); 01107 unsigned BitWidth = CI->getBitWidth(); 01108 assert(isPowerOf2_32(BitWidth) && 01109 "Non-power-of-2-sized integers not handled!"); 01110 01111 // We don't expect assemblers to support integer data directives 01112 // for more than 64 bits, so we emit the data in at most 64-bit 01113 // quantities at a time. 01114 const uint64_t *RawData = CI->getValue().getRawData(); 01115 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { 01116 uint64_t Val; 01117 if (TD->isBigEndian()) 01118 Val = RawData[e - i - 1]; 01119 else 01120 Val = RawData[i]; 01121 01122 if (TAI->getData64bitsDirective(AddrSpace)) 01123 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n'; 01124 else if (TD->isBigEndian()) { 01125 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32) 01126 << '\t' << TAI->getCommentString() 01127 << " Double-word most significant word " << Val << '\n'; 01128 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val) 01129 << '\t' << TAI->getCommentString() 01130 << " Double-word least significant word " << Val << '\n'; 01131 } else { 01132 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val) 01133 << '\t' << TAI->getCommentString() 01134 << " Double-word least significant word " << Val << '\n'; 01135 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32) 01136 << '\t' << TAI->getCommentString() 01137 << " Double-word most significant word " << Val << '\n'; 01138 } 01139 } 01140 } 01141 01142 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 01143 void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { 01144 const TargetData *TD = TM.getTargetData(); 01145 const Type *type = CV->getType(); 01146 unsigned Size = TD->getTypePaddedSize(type); 01147 01148 if (CV->isNullValue() || isa<UndefValue>(CV)) { 01149 EmitZeros(Size, AddrSpace); 01150 return; 01151 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { 01152 EmitGlobalConstantArray(CVA); 01153 return; 01154 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { 01155 EmitGlobalConstantStruct(CVS, AddrSpace); 01156 return; 01157 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 01158 EmitGlobalConstantFP(CFP, AddrSpace); 01159 return; 01160 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 01161 // Small integers are handled below; large integers are handled here. 01162 if (Size > 4) { 01163 EmitGlobalConstantLargeInt(CI, AddrSpace); 01164 return; 01165 } 01166 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { 01167 EmitGlobalConstantVector(CP); 01168 return; 01169 } 01170 01171 printDataDirective(type, AddrSpace); 01172 EmitConstantValueOnly(CV); 01173 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 01174 SmallString<40> S; 01175 CI->getValue().toStringUnsigned(S, 16); 01176 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str(); 01177 } 01178 O << '\n'; 01179 } 01180 01181 void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { 01182 // Target doesn't support this yet! 01183 abort(); 01184 } 01185 01186 /// PrintSpecial - Print information related to the specified machine instr 01187 /// that is independent of the operand, and may be independent of the instr 01188 /// itself. This can be useful for portably encoding the comment character 01189 /// or other bits of target-specific knowledge into the asmstrings. The 01190 /// syntax used is ${:comment}. Targets can override this to add support 01191 /// for their own strange codes. 01192 void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) { 01193 if (!strcmp(Code, "private")) { 01194 O << TAI->getPrivateGlobalPrefix(); 01195 } else if (!strcmp(Code, "comment")) { 01196 O << TAI->getCommentString(); 01197 } else if (!strcmp(Code, "uid")) { 01198 // Assign a unique ID to this machine instruction. 01199 static const MachineInstr *LastMI = 0; 01200 static const Function *F = 0; 01201 static unsigned Counter = 0U-1; 01202 01203 // Comparing the address of MI isn't sufficient, because machineinstrs may 01204 // be allocated to the same address across functions. 01205 const Function *ThisF = MI->getParent()->getParent()->getFunction(); 01206 01207 // If this is a new machine instruction, bump the counter. 01208 if (LastMI != MI || F != ThisF) { 01209 ++Counter; 01210 LastMI = MI; 01211 F = ThisF; 01212 } 01213 O << Counter; 01214 } else { 01215 cerr << "Unknown special formatter '" << Code 01216 << "' for machine instr: " << *MI; 01217 exit(1); 01218 } 01219 } 01220 01221 01222 /// printInlineAsm - This method formats and prints the specified machine 01223 /// instruction that is an inline asm. 01224 void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { 01225 unsigned NumOperands = MI->getNumOperands(); 01226 01227 // Count the number of register definitions. 01228 unsigned NumDefs = 0; 01229 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); 01230 ++NumDefs) 01231 assert(NumDefs != NumOperands-1 && "No asm string?"); 01232 01233 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); 01234 01235 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. 01236 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); 01237 01238 // If this asmstr is empty, just print the #APP/#NOAPP markers. 01239 // These are useful to see where empty asm's wound up. 01240 if (AsmStr[0] == 0) { 01241 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 01242 return; 01243 } 01244 01245 O << TAI->getInlineAsmStart() << "\n\t"; 01246 01247 // The variant of the current asmprinter. 01248 int AsmPrinterVariant = TAI->getAssemblerDialect(); 01249 01250 int CurVariant = -1; // The number of the {.|.|.} region we are in. 01251 const char *LastEmitted = AsmStr; // One past the last character emitted. 01252 01253 while (*LastEmitted) { 01254 switch (*LastEmitted) { 01255 default: { 01256 // Not a special case, emit the string section literally. 01257 const char *LiteralEnd = LastEmitted+1; 01258 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 01259 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 01260 ++LiteralEnd; 01261 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 01262 O.write(LastEmitted, LiteralEnd-LastEmitted); 01263 LastEmitted = LiteralEnd; 01264 break; 01265 } 01266 case '\n': 01267 ++LastEmitted; // Consume newline character. 01268 O << '\n'; // Indent code with newline. 01269 break; 01270 case '$': { 01271 ++LastEmitted; // Consume '$' character. 01272 bool Done = true; 01273 01274 // Handle escapes. 01275 switch (*LastEmitted) { 01276 default: Done = false; break; 01277 case '$': // $$ -> $ 01278 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 01279 O << '$'; 01280 ++LastEmitted; // Consume second '$' character. 01281 break; 01282 case '(': // $( -> same as GCC's { character. 01283 ++LastEmitted; // Consume '(' character. 01284 if (CurVariant != -1) { 01285 cerr << "Nested variants found in inline asm string: '" 01286 << AsmStr << "'\n"; 01287 exit(1); 01288 } 01289 CurVariant = 0; // We're in the first variant now. 01290 break; 01291 case '|': 01292 ++LastEmitted; // consume '|' character. 01293 if (CurVariant == -1) 01294 O << '|'; // this is gcc's behavior for | outside a variant 01295 else 01296 ++CurVariant; // We're in the next variant. 01297 break; 01298 case ')': // $) -> same as GCC's } char. 01299 ++LastEmitted; // consume ')' character. 01300 if (CurVariant == -1) 01301 O << '}'; // this is gcc's behavior for } outside a variant 01302 else 01303 CurVariant = -1; 01304 break; 01305 } 01306 if (Done) break; 01307 01308 bool HasCurlyBraces = false; 01309 if (*LastEmitted == '{') { // ${variable} 01310 ++LastEmitted; // Consume '{' character. 01311 HasCurlyBraces = true; 01312 } 01313 01314 const char *IDStart = LastEmitted; 01315 char *IDEnd; 01316 errno = 0; 01317 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. 01318 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { 01319 cerr << "Bad $ operand number in inline asm string: '" 01320 << AsmStr << "'\n"; 01321 exit(1); 01322 } 01323 LastEmitted = IDEnd; 01324 01325 char Modifier[2] = { 0, 0 }; 01326 01327 if (HasCurlyBraces) { 01328 // If we have curly braces, check for a modifier character. This 01329 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 01330 if (*LastEmitted == ':') { 01331 ++LastEmitted; // Consume ':' character. 01332 if (*LastEmitted == 0) { 01333 cerr << "Bad ${:} expression in inline asm string: '" 01334 << AsmStr << "'\n"; 01335 exit(1); 01336 } 01337 01338 Modifier[0] = *LastEmitted; 01339 ++LastEmitted; // Consume modifier character. 01340 } 01341 01342 if (*LastEmitted != '}') { 01343 cerr << "Bad ${} expression in inline asm string: '" 01344 << AsmStr << "'\n"; 01345 exit(1); 01346 } 01347 ++LastEmitted; // Consume '}' character. 01348 } 01349 01350 if ((unsigned)Val >= NumOperands-1) { 01351 cerr << "Invalid $ operand number in inline asm string: '" 01352 << AsmStr << "'\n"; 01353 exit(1); 01354 } 01355 01356 // Okay, we finally have a value number. Ask the target to print this 01357 // operand! 01358 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { 01359 unsigned OpNo = 1; 01360 01361 bool Error = false; 01362 01363 // Scan to find the machine operand number for the operand. 01364 for (; Val; --Val) { 01365 if (OpNo >= MI->getNumOperands()) break; 01366 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 01367 OpNo += (OpFlags >> 3) + 1; 01368 } 01369 01370 if (OpNo >= MI->getNumOperands()) { 01371 Error = true; 01372 } else { 01373 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 01374 ++OpNo; // Skip over the ID number. 01375 01376 if (Modifier[0]=='l') // labels are target independent 01377 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), 01378 false, false, false); 01379 else { 01380 AsmPrinter *AP = const_cast<AsmPrinter*>(this); 01381 if ((OpFlags & 7) == 4) { 01382 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, 01383 Modifier[0] ? Modifier : 0); 01384 } else { 01385 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, 01386 Modifier[0] ? Modifier : 0); 01387 } 01388 } 01389 } 01390 if (Error) { 01391 cerr << "Invalid operand found in inline asm: '" 01392 << AsmStr << "'\n"; 01393 MI->dump(); 01394 exit(1); 01395 } 01396 } 01397 break; 01398 } 01399 } 01400 } 01401 O << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 01402 } 01403 01404 /// printImplicitDef - This method prints the specified machine instruction 01405 /// that is an implicit def. 01406 void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { 01407 O << '\t' << TAI->getCommentString() << " implicit-def: " 01408 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; 01409 } 01410 01411 /// printLabel - This method prints a local label used by debug and 01412 /// exception handling tables. 01413 void AsmPrinter::printLabel(const MachineInstr *MI) const { 01414 printLabel(MI->getOperand(0).getImm()); 01415 } 01416 01417 void AsmPrinter::printLabel(unsigned Id) const { 01418 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; 01419 } 01420 01421 /// printDeclare - This method prints a local variable declaration used by 01422 /// debug tables. 01423 /// FIXME: It doesn't really print anything rather it inserts a DebugVariable 01424 /// entry into dwarf table. 01425 void AsmPrinter::printDeclare(const MachineInstr *MI) const { 01426 unsigned FI = MI->getOperand(0).getIndex(); 01427 GlobalValue *GV = MI->getOperand(1).getGlobal(); 01428 DW->RecordVariable(cast<GlobalVariable>(GV), FI); 01429 } 01430 01431 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 01432 /// instruction, using the specified assembler variant. Targets should 01433 /// overried this to format as appropriate. 01434 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 01435 unsigned AsmVariant, const char *ExtraCode) { 01436 // Target doesn't support this yet! 01437 return true; 01438 } 01439 01440 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 01441 unsigned AsmVariant, 01442 const char *ExtraCode) { 01443 // Target doesn't support this yet! 01444 return true; 01445 } 01446 01447 /// printBasicBlockLabel - This method prints the label for the specified 01448 /// MachineBasicBlock 01449 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, 01450 bool printAlign, 01451 bool printColon, 01452 bool printComment) const { 01453 if (printAlign) { 01454 unsigned Align = MBB->getAlignment(); 01455 if (Align) 01456 EmitAlignment(Log2_32(Align)); 01457 } 01458 01459 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' 01460 << MBB->getNumber(); 01461 if (printColon) 01462 O << ':'; 01463 if (printComment && MBB->getBasicBlock()) 01464 O << '\t' << TAI->getCommentString() << ' ' 01465 << MBB->getBasicBlock()->getNameStart(); 01466 } 01467 01468 /// printPICJumpTableSetLabel - This method prints a set label for the 01469 /// specified MachineBasicBlock for a jumptable entry. 01470 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, 01471 const MachineBasicBlock *MBB) const { 01472 if (!TAI->getSetDirective()) 01473 return; 01474 01475 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 01476 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; 01477 printBasicBlockLabel(MBB, false, false, false); 01478 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 01479 << '_' << uid << '\n'; 01480 } 01481 01482 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 01483 const MachineBasicBlock *MBB) const { 01484 if (!TAI->getSetDirective()) 01485 return; 01486 01487 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 01488 << getFunctionNumber() << '_' << uid << '_' << uid2 01489 << "_set_" << MBB->getNumber() << ','; 01490 printBasicBlockLabel(MBB, false, false, false); 01491 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 01492 << '_' << uid << '_' << uid2 << '\n'; 01493 } 01494 01495 /// printDataDirective - This method prints the asm directive for the 01496 /// specified type. 01497 void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) { 01498 const TargetData *TD = TM.getTargetData(); 01499 switch (type->getTypeID()) { 01500 case Type::IntegerTyID: { 01501 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); 01502 if (BitWidth <= 8) 01503 O << TAI->getData8bitsDirective(AddrSpace); 01504 else if (BitWidth <= 16) 01505 O << TAI->getData16bitsDirective(AddrSpace); 01506 else if (BitWidth <= 32) 01507 O << TAI->getData32bitsDirective(AddrSpace); 01508 else if (BitWidth <= 64) { 01509 assert(TAI->getData64bitsDirective(AddrSpace) && 01510 "Target cannot handle 64-bit constant exprs!"); 01511 O << TAI->getData64bitsDirective(AddrSpace); 01512 } else { 01513 assert(0 && "Target cannot handle given data directive width!"); 01514 } 01515 break; 01516 } 01517 case Type::PointerTyID: 01518 if (TD->getPointerSize() == 8) { 01519 assert(TAI->getData64bitsDirective(AddrSpace) && 01520 "Target cannot handle 64-bit pointer exprs!"); 01521 O << TAI->getData64bitsDirective(AddrSpace); 01522 } else if (TD->getPointerSize() == 2) { 01523 O << TAI->getData16bitsDirective(AddrSpace); 01524 } else if (TD->getPointerSize() == 1) { 01525 O << TAI->getData8bitsDirective(AddrSpace); 01526 } else { 01527 O << TAI->getData32bitsDirective(AddrSpace); 01528 } 01529 break; 01530 case Type::FloatTyID: case Type::DoubleTyID: 01531 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: 01532 assert (0 && "Should have already output floating point constant."); 01533 default: 01534 assert (0 && "Can't handle printing this type of thing"); 01535 break; 01536 } 01537 } 01538 01539 void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix, 01540 const char *Prefix) { 01541 if (Name[0]=='\"') 01542 O << '\"'; 01543 O << TAI->getPrivateGlobalPrefix(); 01544 if (Prefix) O << Prefix; 01545 if (Name[0]=='\"') 01546 O << '\"'; 01547 if (Name[0]=='\"') 01548 O << Name[1]; 01549 else 01550 O << Name; 01551 O << Suffix; 01552 if (Name[0]=='\"') 01553 O << '\"'; 01554 } 01555 01556 void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) { 01557 printSuffixedName(Name.c_str(), Suffix); 01558 } 01559 01560 void AsmPrinter::printVisibility(const std::string& Name, 01561 unsigned Visibility) const { 01562 if (Visibility == GlobalValue::HiddenVisibility) { 01563 if (const char *Directive = TAI->getHiddenDirective()) 01564 O << Directive << Name << '\n'; 01565 } else if (Visibility == GlobalValue::ProtectedVisibility) { 01566 if (const char *Directive = TAI->getProtectedDirective()) 01567 O << Directive << Name << '\n'; 01568 } 01569 } 01570 01571 void AsmPrinter::printOffset(int64_t Offset) const { 01572 if (Offset > 0) 01573 O << '+' << Offset; 01574 else if (Offset < 0) 01575 O << Offset; 01576 } 01577 01578 GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { 01579 if (!S->usesMetadata()) 01580 return 0; 01581 01582 gcp_iterator GCPI = GCMetadataPrinters.find(S); 01583 if (GCPI != GCMetadataPrinters.end()) 01584 return GCPI->second; 01585 01586 const char *Name = S->getName().c_str(); 01587 01588 for (GCMetadataPrinterRegistry::iterator 01589 I = GCMetadataPrinterRegistry::begin(), 01590 E = GCMetadataPrinterRegistry::end(); I != E; ++I) 01591 if (strcmp(Name, I->getName()) == 0) { 01592 GCMetadataPrinter *GMP = I->instantiate(); 01593 GMP->S = S; 01594 GCMetadataPrinters.insert(std::make_pair(S, GMP)); 01595 return GMP; 01596 } 01597 01598 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; 01599 abort(); 01600 }