1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AsmPrinter class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Module.h"
19 #include "llvm/CodeGen/GCMetadataPrinter.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineJumpTableInfo.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/Target/TargetAsmInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/ADT/StringExtras.h"
38 char AsmPrinter::ID = 0;
39 AsmPrinter::AsmPrinter(raw_ostream &o, TargetMachine &tm,
40 const TargetAsmInfo *T)
41 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
42 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
43 IsInTextSection(false)
46 AsmPrinter::~AsmPrinter() {
47 for (gcp_iterator I = GCMetadataPrinters.begin(),
48 E = GCMetadataPrinters.end(); I != E; ++I)
52 std::string AsmPrinter::getSectionForFunction(const Function &F) const {
53 return TAI->getTextSection();
57 /// SwitchToTextSection - Switch to the specified text section of the executable
58 /// if we are not already in it!
60 void AsmPrinter::SwitchToTextSection(const char *NewSection,
61 const GlobalValue *GV) {
63 if (GV && GV->hasSection())
64 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
68 // If we're already in this section, we're done.
69 if (CurrentSection == NS) return;
71 // Close the current section, if applicable.
72 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
73 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
77 if (!CurrentSection.empty())
78 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
80 IsInTextSection = true;
83 /// SwitchToDataSection - Switch to the specified data section of the executable
84 /// if we are not already in it!
86 void AsmPrinter::SwitchToDataSection(const char *NewSection,
87 const GlobalValue *GV) {
89 if (GV && GV->hasSection())
90 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
94 // If we're already in this section, we're done.
95 if (CurrentSection == NS) return;
97 // Close the current section, if applicable.
98 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
99 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
103 if (!CurrentSection.empty())
104 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
106 IsInTextSection = false;
110 void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
111 MachineFunctionPass::getAnalysisUsage(AU);
112 AU.addRequired<GCModuleInfo>();
115 bool AsmPrinter::doInitialization(Module &M) {
116 Mang = new Mangler(M, TAI->getGlobalPrefix());
118 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
119 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
120 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
121 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
122 MP->beginAssembly(O, *this, *TAI);
124 if (!M.getModuleInlineAsm().empty())
125 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
126 << M.getModuleInlineAsm()
127 << '\n' << TAI->getCommentString()
128 << " End of file scope inline assembly\n";
130 SwitchToDataSection(""); // Reset back to no section.
132 MMI = getAnalysisToUpdate<MachineModuleInfo>();
133 if (MMI) MMI->AnalyzeModule(M);
138 bool AsmPrinter::doFinalization(Module &M) {
139 if (TAI->getWeakRefDirective()) {
140 if (!ExtWeakSymbols.empty())
141 SwitchToDataSection("");
143 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
144 e = ExtWeakSymbols.end(); i != e; ++i) {
145 const GlobalValue *GV = *i;
146 std::string Name = Mang->getValueName(GV);
147 O << TAI->getWeakRefDirective() << Name << '\n';
151 if (TAI->getSetDirective()) {
152 if (!M.alias_empty())
153 SwitchToTextSection(TAI->getTextSection());
156 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
158 std::string Name = Mang->getValueName(I);
161 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
162 Target = Mang->getValueName(GV);
164 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
165 O << "\t.globl\t" << Name << '\n';
166 else if (I->hasWeakLinkage())
167 O << TAI->getWeakRefDirective() << Name << '\n';
168 else if (!I->hasInternalLinkage())
169 assert(0 && "Invalid alias linkage");
171 if (I->hasHiddenVisibility()) {
172 if (const char *Directive = TAI->getHiddenDirective())
173 O << Directive << Name << '\n';
174 } else if (I->hasProtectedVisibility()) {
175 if (const char *Directive = TAI->getProtectedDirective())
176 O << Directive << Name << '\n';
179 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
181 // If the aliasee has external weak linkage it can be referenced only by
182 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
183 // weak reference in such case.
184 if (GV->hasExternalWeakLinkage()) {
185 if (TAI->getWeakRefDirective())
186 O << TAI->getWeakRefDirective() << Target << '\n';
188 O << "\t.globl\t" << Target << '\n';
193 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
194 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
195 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
196 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
197 MP->finishAssembly(O, *this, *TAI);
199 // If we don't have any trampolines, then we don't require stack memory
200 // to be executable. Some targets have a directive to declare this.
201 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
202 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
203 if (TAI->getNonexecutableStackDirective())
204 O << TAI->getNonexecutableStackDirective() << '\n';
206 delete Mang; Mang = 0;
210 std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
211 assert(MF && "No machine function?");
212 std::string Name = MF->getFunction()->getName();
214 Name = Mang->getValueName(MF->getFunction());
215 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
218 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
219 // What's my mangled name?
220 CurrentFnName = Mang->getValueName(MF.getFunction());
221 IncrementFunctionNumber();
224 /// EmitConstantPool - Print to the current output stream assembly
225 /// representations of the constants in the constant pool MCP. This is
226 /// used to print out constants which have been "spilled to memory" by
227 /// the code generator.
229 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
230 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
231 if (CP.empty()) return;
233 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
234 // in special sections.
235 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
236 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
239 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
240 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
241 MachineConstantPoolEntry CPE = CP[i];
242 const Type *Ty = CPE.getType();
243 if (TAI->getFourByteConstantSection() &&
244 TM.getTargetData()->getABITypeSize(Ty) == 4)
245 FourByteCPs.push_back(std::make_pair(CPE, i));
246 else if (TAI->getEightByteConstantSection() &&
247 TM.getTargetData()->getABITypeSize(Ty) == 8)
248 EightByteCPs.push_back(std::make_pair(CPE, i));
249 else if (TAI->getSixteenByteConstantSection() &&
250 TM.getTargetData()->getABITypeSize(Ty) == 16)
251 SixteenByteCPs.push_back(std::make_pair(CPE, i));
253 OtherCPs.push_back(std::make_pair(CPE, i));
256 unsigned Alignment = MCP->getConstantPoolAlignment();
257 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
258 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
259 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
261 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
264 void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
265 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
266 if (CP.empty()) return;
268 SwitchToDataSection(Section);
269 EmitAlignment(Alignment);
270 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
271 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
272 << CP[i].second << ":\t\t\t\t\t";
273 // O << TAI->getCommentString() << ' ' <<
274 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
276 if (CP[i].first.isMachineConstantPoolEntry())
277 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
279 EmitGlobalConstant(CP[i].first.Val.ConstVal);
281 const Type *Ty = CP[i].first.getType();
283 TM.getTargetData()->getABITypeSize(Ty);
284 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
285 // Emit inter-object padding for alignment.
286 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
291 /// EmitJumpTableInfo - Print assembly representations of the jump tables used
292 /// by the current function to the current output stream.
294 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
295 MachineFunction &MF) {
296 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
297 if (JT.empty()) return;
299 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
301 // Pick the directive to use to print the jump table entries, and switch to
302 // the appropriate section.
303 TargetLowering *LoweringInfo = TM.getTargetLowering();
305 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
306 const Function *F = MF.getFunction();
307 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
308 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
309 !JumpTableDataSection ||
310 SectionFlags & SectionFlags::Linkonce) {
311 // In PIC mode, we need to emit the jump table to the same section as the
312 // function body itself, otherwise the label differences won't make sense.
313 // We should also do if the section name is NULL or function is declared in
314 // discardable section.
315 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
317 SwitchToDataSection(JumpTableDataSection);
320 EmitAlignment(Log2_32(MJTI->getAlignment()));
322 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
323 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
325 // If this jump table was deleted, ignore it.
326 if (JTBBs.empty()) continue;
328 // For PIC codegen, if possible we want to use the SetDirective to reduce
329 // the number of relocations the assembler will generate for the jump table.
330 // Set directives are all printed before the jump table itself.
331 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
332 if (TAI->getSetDirective() && IsPic)
333 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
334 if (EmittedSets.insert(JTBBs[ii]))
335 printPICJumpTableSetLabel(i, JTBBs[ii]);
337 // On some targets (e.g. darwin) we want to emit two consequtive labels
338 // before each jump table. The first label is never referenced, but tells
339 // the assembler and linker the extents of the jump table object. The
340 // second label is actually referenced by the code.
341 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
342 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
344 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
345 << '_' << i << ":\n";
347 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
348 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
354 void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
355 const MachineBasicBlock *MBB,
356 unsigned uid) const {
357 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
359 // Use JumpTableDirective otherwise honor the entry size from the jump table
361 const char *JTEntryDirective = TAI->getJumpTableDirective();
362 bool HadJTEntryDirective = JTEntryDirective != NULL;
363 if (!HadJTEntryDirective) {
364 JTEntryDirective = MJTI->getEntrySize() == 4 ?
365 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
368 O << JTEntryDirective << ' ';
370 // If we have emitted set directives for the jump table entries, print
371 // them rather than the entries themselves. If we're emitting PIC, then
372 // emit the table entries as differences between two text section labels.
373 // If we're emitting non-PIC code, then emit the entries as direct
374 // references to the target basic blocks.
376 if (TAI->getSetDirective()) {
377 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
378 << '_' << uid << "_set_" << MBB->getNumber();
380 printBasicBlockLabel(MBB, false, false, false);
381 // If the arch uses custom Jump Table directives, don't calc relative to
383 if (!HadJTEntryDirective)
384 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
385 << getFunctionNumber() << '_' << uid;
388 printBasicBlockLabel(MBB, false, false, false);
393 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
394 /// special global used by LLVM. If so, emit it and return true, otherwise
395 /// do nothing and return false.
396 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
397 if (GV->getName() == "llvm.used") {
398 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
399 EmitLLVMUsedList(GV->getInitializer());
403 // Ignore debug and non-emitted data.
404 if (GV->getSection() == "llvm.metadata") return true;
406 if (!GV->hasAppendingLinkage()) return false;
408 assert(GV->hasInitializer() && "Not a special LLVM global!");
410 const TargetData *TD = TM.getTargetData();
411 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
412 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
413 SwitchToDataSection(TAI->getStaticCtorsSection());
414 EmitAlignment(Align, 0);
415 EmitXXStructorList(GV->getInitializer());
419 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
420 SwitchToDataSection(TAI->getStaticDtorsSection());
421 EmitAlignment(Align, 0);
422 EmitXXStructorList(GV->getInitializer());
429 /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
430 /// global in the specified llvm.used list as being used with this directive.
431 void AsmPrinter::EmitLLVMUsedList(Constant *List) {
432 const char *Directive = TAI->getUsedDirective();
434 // Should be an array of 'sbyte*'.
435 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
436 if (InitList == 0) return;
438 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
440 EmitConstantValueOnly(InitList->getOperand(i));
445 /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
446 /// function pointers, ignoring the init priority.
447 void AsmPrinter::EmitXXStructorList(Constant *List) {
448 // Should be an array of '{ int, void ()* }' structs. The first value is the
449 // init priority, which we ignore.
450 if (!isa<ConstantArray>(List)) return;
451 ConstantArray *InitList = cast<ConstantArray>(List);
452 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
453 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
454 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
456 if (CS->getOperand(1)->isNullValue())
457 return; // Found a null terminator, exit printing.
458 // Emit the function pointer.
459 EmitGlobalConstant(CS->getOperand(1));
463 /// getGlobalLinkName - Returns the asm/link name of of the specified
464 /// global variable. Should be overridden by each target asm printer to
465 /// generate the appropriate value.
466 const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
467 std::string LinkName;
469 if (isa<Function>(GV)) {
470 LinkName += TAI->getFunctionAddrPrefix();
471 LinkName += Mang->getValueName(GV);
472 LinkName += TAI->getFunctionAddrSuffix();
474 LinkName += TAI->getGlobalVarAddrPrefix();
475 LinkName += Mang->getValueName(GV);
476 LinkName += TAI->getGlobalVarAddrSuffix();
482 /// EmitExternalGlobal - Emit the external reference to a global variable.
483 /// Should be overridden if an indirect reference should be used.
484 void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
485 O << getGlobalLinkName(GV);
490 //===----------------------------------------------------------------------===//
491 /// LEB 128 number encoding.
493 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
494 /// representing an unsigned leb128 value.
495 void AsmPrinter::PrintULEB128(unsigned Value) const {
497 unsigned Byte = Value & 0x7f;
499 if (Value) Byte |= 0x80;
500 O << "0x" << utohexstr(Byte);
501 if (Value) O << ", ";
505 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
506 /// representing a signed leb128 value.
507 void AsmPrinter::PrintSLEB128(int Value) const {
508 int Sign = Value >> (8 * sizeof(Value) - 1);
512 unsigned Byte = Value & 0x7f;
514 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
515 if (IsMore) Byte |= 0x80;
516 O << "0x" << utohexstr(Byte);
517 if (IsMore) O << ", ";
521 //===--------------------------------------------------------------------===//
522 // Emission and print routines
525 /// PrintHex - Print a value as a hexidecimal value.
527 void AsmPrinter::PrintHex(int Value) const {
528 O << "0x" << utohexstr(static_cast<unsigned>(Value));
531 /// EOL - Print a newline character to asm stream. If a comment is present
532 /// then it will be printed first. Comments should not contain '\n'.
533 void AsmPrinter::EOL() const {
537 void AsmPrinter::EOL(const std::string &Comment) const {
538 if (VerboseAsm && !Comment.empty()) {
540 << TAI->getCommentString()
547 void AsmPrinter::EOL(const char* Comment) const {
548 if (VerboseAsm && *Comment) {
550 << TAI->getCommentString()
557 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
558 /// unsigned leb128 value.
559 void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
560 if (TAI->hasLEB128()) {
564 O << TAI->getData8bitsDirective();
569 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
570 /// signed leb128 value.
571 void AsmPrinter::EmitSLEB128Bytes(int Value) const {
572 if (TAI->hasLEB128()) {
576 O << TAI->getData8bitsDirective();
581 /// EmitInt8 - Emit a byte directive and value.
583 void AsmPrinter::EmitInt8(int Value) const {
584 O << TAI->getData8bitsDirective();
585 PrintHex(Value & 0xFF);
588 /// EmitInt16 - Emit a short directive and value.
590 void AsmPrinter::EmitInt16(int Value) const {
591 O << TAI->getData16bitsDirective();
592 PrintHex(Value & 0xFFFF);
595 /// EmitInt32 - Emit a long directive and value.
597 void AsmPrinter::EmitInt32(int Value) const {
598 O << TAI->getData32bitsDirective();
602 /// EmitInt64 - Emit a long long directive and value.
604 void AsmPrinter::EmitInt64(uint64_t Value) const {
605 if (TAI->getData64bitsDirective()) {
606 O << TAI->getData64bitsDirective();
609 if (TM.getTargetData()->isBigEndian()) {
610 EmitInt32(unsigned(Value >> 32)); O << '\n';
611 EmitInt32(unsigned(Value));
613 EmitInt32(unsigned(Value)); O << '\n';
614 EmitInt32(unsigned(Value >> 32));
619 /// toOctal - Convert the low order bits of X into an octal digit.
621 static inline char toOctal(int X) {
625 /// printStringChar - Print a char, escaped if necessary.
627 static void printStringChar(raw_ostream &O, char C) {
630 } else if (C == '\\') {
632 } else if (isprint(C)) {
636 case '\b': O << "\\b"; break;
637 case '\f': O << "\\f"; break;
638 case '\n': O << "\\n"; break;
639 case '\r': O << "\\r"; break;
640 case '\t': O << "\\t"; break;
643 O << toOctal(C >> 6);
644 O << toOctal(C >> 3);
645 O << toOctal(C >> 0);
651 /// EmitString - Emit a string with quotes and a null terminator.
652 /// Special characters are emitted properly.
653 /// \literal (Eg. '\t') \endliteral
654 void AsmPrinter::EmitString(const std::string &String) const {
655 const char* AscizDirective = TAI->getAscizDirective();
659 O << TAI->getAsciiDirective();
661 for (unsigned i = 0, N = String.size(); i < N; ++i) {
662 unsigned char C = String[i];
663 printStringChar(O, C);
672 /// EmitFile - Emit a .file directive.
673 void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
674 O << "\t.file\t" << Number << " \"";
675 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
676 unsigned char C = Name[i];
677 printStringChar(O, C);
683 //===----------------------------------------------------------------------===//
685 // EmitAlignment - Emit an alignment directive to the specified power of
686 // two boundary. For example, if you pass in 3 here, you will get an 8
687 // byte alignment. If a global value is specified, and if that global has
688 // an explicit alignment requested, it will unconditionally override the
689 // alignment request. However, if ForcedAlignBits is specified, this value
690 // has final say: the ultimate alignment will be the max of ForcedAlignBits
691 // and the alignment computed with NumBits and the global.
695 // if (GV && GV->hasalignment) Align = GV->getalignment();
696 // Align = std::max(Align, ForcedAlignBits);
698 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
699 unsigned ForcedAlignBits,
700 bool UseFillExpr) const {
701 if (GV && GV->getAlignment())
702 NumBits = Log2_32(GV->getAlignment());
703 NumBits = std::max(NumBits, ForcedAlignBits);
705 if (NumBits == 0) return; // No need to emit alignment.
706 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
707 O << TAI->getAlignDirective() << NumBits;
709 unsigned FillValue = TAI->getTextAlignFillValue();
710 UseFillExpr &= IsInTextSection && FillValue;
711 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
716 /// EmitZeros - Emit a block of zeros.
718 void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
720 if (TAI->getZeroDirective()) {
721 O << TAI->getZeroDirective() << NumZeros;
722 if (TAI->getZeroDirectiveSuffix())
723 O << TAI->getZeroDirectiveSuffix();
726 for (; NumZeros; --NumZeros)
727 O << TAI->getData8bitsDirective() << "0\n";
732 // Print out the specified constant, without a storage class. Only the
733 // constants valid in constant expressions can occur here.
734 void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
735 if (CV->isNullValue() || isa<UndefValue>(CV))
737 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
738 O << CI->getZExtValue();
739 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
740 // This is a constant address for a global variable or function. Use the
741 // name of the variable or function as the address value, possibly
742 // decorating it with GlobalVarAddrPrefix/Suffix or
743 // FunctionAddrPrefix/Suffix (these all default to "" )
744 if (isa<Function>(GV)) {
745 O << TAI->getFunctionAddrPrefix()
746 << Mang->getValueName(GV)
747 << TAI->getFunctionAddrSuffix();
749 O << TAI->getGlobalVarAddrPrefix()
750 << Mang->getValueName(GV)
751 << TAI->getGlobalVarAddrSuffix();
753 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
754 const TargetData *TD = TM.getTargetData();
755 unsigned Opcode = CE->getOpcode();
757 case Instruction::GetElementPtr: {
758 // generate a symbolic expression for the byte address
759 const Constant *ptrVal = CE->getOperand(0);
760 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
761 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
765 EmitConstantValueOnly(ptrVal);
767 O << ") + " << Offset;
769 O << ") - " << -Offset;
771 EmitConstantValueOnly(ptrVal);
775 case Instruction::Trunc:
776 case Instruction::ZExt:
777 case Instruction::SExt:
778 case Instruction::FPTrunc:
779 case Instruction::FPExt:
780 case Instruction::UIToFP:
781 case Instruction::SIToFP:
782 case Instruction::FPToUI:
783 case Instruction::FPToSI:
784 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
786 case Instruction::BitCast:
787 return EmitConstantValueOnly(CE->getOperand(0));
789 case Instruction::IntToPtr: {
790 // Handle casts to pointers by changing them into casts to the appropriate
791 // integer type. This promotes constant folding and simplifies this code.
792 Constant *Op = CE->getOperand(0);
793 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
794 return EmitConstantValueOnly(Op);
798 case Instruction::PtrToInt: {
799 // Support only foldable casts to/from pointers that can be eliminated by
800 // changing the pointer to the appropriately sized integer type.
801 Constant *Op = CE->getOperand(0);
802 const Type *Ty = CE->getType();
804 // We can emit the pointer value into this slot if the slot is an
805 // integer slot greater or equal to the size of the pointer.
806 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
807 return EmitConstantValueOnly(Op);
810 EmitConstantValueOnly(Op);
811 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
814 ptrMask.toStringUnsigned(S);
815 O << ") & " << S.c_str() << ')';
818 case Instruction::Add:
819 case Instruction::Sub:
820 case Instruction::And:
821 case Instruction::Or:
822 case Instruction::Xor:
824 EmitConstantValueOnly(CE->getOperand(0));
827 case Instruction::Add:
830 case Instruction::Sub:
833 case Instruction::And:
836 case Instruction::Or:
839 case Instruction::Xor:
846 EmitConstantValueOnly(CE->getOperand(1));
850 assert(0 && "Unsupported operator!");
853 assert(0 && "Unknown constant value!");
857 /// printAsCString - Print the specified array as a C compatible string, only if
858 /// the predicate isString is true.
860 static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
862 assert(CVA->isString() && "Array is not string compatible!");
865 for (unsigned i = 0; i != LastElt; ++i) {
867 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
868 printStringChar(O, C);
873 /// EmitString - Emit a zero-byte-terminated string constant.
875 void AsmPrinter::EmitString(const ConstantArray *CVA) const {
876 unsigned NumElts = CVA->getNumOperands();
877 if (TAI->getAscizDirective() && NumElts &&
878 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
879 O << TAI->getAscizDirective();
880 printAsCString(O, CVA, NumElts-1);
882 O << TAI->getAsciiDirective();
883 printAsCString(O, CVA, NumElts);
888 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
889 void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
890 const TargetData *TD = TM.getTargetData();
891 unsigned Size = TD->getABITypeSize(CV->getType());
893 if (CV->isNullValue() || isa<UndefValue>(CV)) {
896 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
897 if (CVA->isString()) {
899 } else { // Not a string. Print the values in successive locations
900 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
901 EmitGlobalConstant(CVA->getOperand(i));
904 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
905 // Print the fields in successive locations. Pad to align if needed!
906 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
907 uint64_t sizeSoFar = 0;
908 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
909 const Constant* field = CVS->getOperand(i);
911 // Check if padding is needed and insert one or more 0s.
912 uint64_t fieldSize = TD->getABITypeSize(field->getType());
913 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
914 - cvsLayout->getElementOffset(i)) - fieldSize;
915 sizeSoFar += fieldSize + padSize;
917 // Now print the actual field value.
918 EmitGlobalConstant(field);
920 // Insert padding - this may include padding to increase the size of the
921 // current field up to the ABI size (if the struct is not packed) as well
922 // as padding to ensure that the next field starts at the right offset.
925 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
926 "Layout of constant struct may be incorrect!");
928 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
929 // FP Constants are printed as integer constants to avoid losing
931 if (CFP->getType() == Type::DoubleTy) {
932 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
933 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
934 if (TAI->getData64bitsDirective())
935 O << TAI->getData64bitsDirective() << i << '\t'
936 << TAI->getCommentString() << " double value: " << Val << '\n';
937 else if (TD->isBigEndian()) {
938 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
939 << '\t' << TAI->getCommentString()
940 << " double most significant word " << Val << '\n';
941 O << TAI->getData32bitsDirective() << unsigned(i)
942 << '\t' << TAI->getCommentString()
943 << " double least significant word " << Val << '\n';
945 O << TAI->getData32bitsDirective() << unsigned(i)
946 << '\t' << TAI->getCommentString()
947 << " double least significant word " << Val << '\n';
948 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
949 << '\t' << TAI->getCommentString()
950 << " double most significant word " << Val << '\n';
953 } else if (CFP->getType() == Type::FloatTy) {
954 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
955 O << TAI->getData32bitsDirective()
956 << CFP->getValueAPF().convertToAPInt().getZExtValue()
957 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
959 } else if (CFP->getType() == Type::X86_FP80Ty) {
960 // all long double variants are printed as hex
961 // api needed to prevent premature destruction
962 APInt api = CFP->getValueAPF().convertToAPInt();
963 const uint64_t *p = api.getRawData();
964 APFloat DoubleVal = CFP->getValueAPF();
965 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
966 if (TD->isBigEndian()) {
967 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
968 << '\t' << TAI->getCommentString()
969 << " long double most significant halfword of ~"
970 << DoubleVal.convertToDouble() << '\n';
971 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
972 << '\t' << TAI->getCommentString()
973 << " long double next halfword\n";
974 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
975 << '\t' << TAI->getCommentString()
976 << " long double next halfword\n";
977 O << TAI->getData16bitsDirective() << uint16_t(p[0])
978 << '\t' << TAI->getCommentString()
979 << " long double next halfword\n";
980 O << TAI->getData16bitsDirective() << uint16_t(p[1])
981 << '\t' << TAI->getCommentString()
982 << " long double least significant halfword\n";
984 O << TAI->getData16bitsDirective() << uint16_t(p[1])
985 << '\t' << TAI->getCommentString()
986 << " long double least significant halfword of ~"
987 << DoubleVal.convertToDouble() << '\n';
988 O << TAI->getData16bitsDirective() << uint16_t(p[0])
989 << '\t' << TAI->getCommentString()
990 << " long double next halfword\n";
991 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
992 << '\t' << TAI->getCommentString()
993 << " long double next halfword\n";
994 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
995 << '\t' << TAI->getCommentString()
996 << " long double next halfword\n";
997 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
998 << '\t' << TAI->getCommentString()
999 << " long double most significant halfword\n";
1001 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
1003 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1004 // all long double variants are printed as hex
1005 // api needed to prevent premature destruction
1006 APInt api = CFP->getValueAPF().convertToAPInt();
1007 const uint64_t *p = api.getRawData();
1008 if (TD->isBigEndian()) {
1009 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
1010 << '\t' << TAI->getCommentString()
1011 << " long double most significant word\n";
1012 O << TAI->getData32bitsDirective() << uint32_t(p[0])
1013 << '\t' << TAI->getCommentString()
1014 << " long double next word\n";
1015 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
1016 << '\t' << TAI->getCommentString()
1017 << " long double next word\n";
1018 O << TAI->getData32bitsDirective() << uint32_t(p[1])
1019 << '\t' << TAI->getCommentString()
1020 << " long double least significant word\n";
1022 O << TAI->getData32bitsDirective() << uint32_t(p[1])
1023 << '\t' << TAI->getCommentString()
1024 << " long double least significant word\n";
1025 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
1026 << '\t' << TAI->getCommentString()
1027 << " long double next word\n";
1028 O << TAI->getData32bitsDirective() << uint32_t(p[0])
1029 << '\t' << TAI->getCommentString()
1030 << " long double next word\n";
1031 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
1032 << '\t' << TAI->getCommentString()
1033 << " long double most significant word\n";
1036 } else assert(0 && "Floating point constant type not handled");
1037 } else if (CV->getType() == Type::Int64Ty) {
1038 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1039 uint64_t Val = CI->getZExtValue();
1041 if (TAI->getData64bitsDirective())
1042 O << TAI->getData64bitsDirective() << Val << '\n';
1043 else if (TD->isBigEndian()) {
1044 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1045 << '\t' << TAI->getCommentString()
1046 << " Double-word most significant word " << Val << '\n';
1047 O << TAI->getData32bitsDirective() << unsigned(Val)
1048 << '\t' << TAI->getCommentString()
1049 << " Double-word least significant word " << Val << '\n';
1051 O << TAI->getData32bitsDirective() << unsigned(Val)
1052 << '\t' << TAI->getCommentString()
1053 << " Double-word least significant word " << Val << '\n';
1054 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1055 << '\t' << TAI->getCommentString()
1056 << " Double-word most significant word " << Val << '\n';
1060 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1061 const VectorType *PTy = CP->getType();
1063 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1064 EmitGlobalConstant(CP->getOperand(I));
1069 const Type *type = CV->getType();
1070 printDataDirective(type);
1071 EmitConstantValueOnly(CV);
1072 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1074 CI->getValue().toStringUnsigned(S, 16);
1075 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1080 void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1081 // Target doesn't support this yet!
1085 /// PrintSpecial - Print information related to the specified machine instr
1086 /// that is independent of the operand, and may be independent of the instr
1087 /// itself. This can be useful for portably encoding the comment character
1088 /// or other bits of target-specific knowledge into the asmstrings. The
1089 /// syntax used is ${:comment}. Targets can override this to add support
1090 /// for their own strange codes.
1091 void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
1092 if (!strcmp(Code, "private")) {
1093 O << TAI->getPrivateGlobalPrefix();
1094 } else if (!strcmp(Code, "comment")) {
1095 O << TAI->getCommentString();
1096 } else if (!strcmp(Code, "uid")) {
1097 // Assign a unique ID to this machine instruction.
1098 static const MachineInstr *LastMI = 0;
1099 static const Function *F = 0;
1100 static unsigned Counter = 0U-1;
1102 // Comparing the address of MI isn't sufficient, because machineinstrs may
1103 // be allocated to the same address across functions.
1104 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1106 // If this is a new machine instruction, bump the counter.
1107 if (LastMI != MI || F != ThisF) {
1114 cerr << "Unknown special formatter '" << Code
1115 << "' for machine instr: " << *MI;
1121 /// printInlineAsm - This method formats and prints the specified machine
1122 /// instruction that is an inline asm.
1123 void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1124 unsigned NumOperands = MI->getNumOperands();
1126 // Count the number of register definitions.
1127 unsigned NumDefs = 0;
1128 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
1130 assert(NumDefs != NumOperands-1 && "No asm string?");
1132 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
1134 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1135 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1137 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1138 // These are useful to see where empty asm's wound up.
1139 if (AsmStr[0] == 0) {
1140 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
1144 O << TAI->getInlineAsmStart() << "\n\t";
1146 // The variant of the current asmprinter.
1147 int AsmPrinterVariant = TAI->getAssemblerDialect();
1149 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1150 const char *LastEmitted = AsmStr; // One past the last character emitted.
1152 while (*LastEmitted) {
1153 switch (*LastEmitted) {
1155 // Not a special case, emit the string section literally.
1156 const char *LiteralEnd = LastEmitted+1;
1157 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1158 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1160 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1161 O.write(LastEmitted, LiteralEnd-LastEmitted);
1162 LastEmitted = LiteralEnd;
1166 ++LastEmitted; // Consume newline character.
1167 O << '\n'; // Indent code with newline.
1170 ++LastEmitted; // Consume '$' character.
1174 switch (*LastEmitted) {
1175 default: Done = false; break;
1176 case '$': // $$ -> $
1177 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1179 ++LastEmitted; // Consume second '$' character.
1181 case '(': // $( -> same as GCC's { character.
1182 ++LastEmitted; // Consume '(' character.
1183 if (CurVariant != -1) {
1184 cerr << "Nested variants found in inline asm string: '"
1188 CurVariant = 0; // We're in the first variant now.
1191 ++LastEmitted; // consume '|' character.
1192 if (CurVariant == -1) {
1193 cerr << "Found '|' character outside of variant in inline asm "
1194 << "string: '" << AsmStr << "'\n";
1197 ++CurVariant; // We're in the next variant.
1199 case ')': // $) -> same as GCC's } char.
1200 ++LastEmitted; // consume ')' character.
1201 if (CurVariant == -1) {
1202 cerr << "Found '}' character outside of variant in inline asm "
1203 << "string: '" << AsmStr << "'\n";
1211 bool HasCurlyBraces = false;
1212 if (*LastEmitted == '{') { // ${variable}
1213 ++LastEmitted; // Consume '{' character.
1214 HasCurlyBraces = true;
1217 const char *IDStart = LastEmitted;
1220 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1221 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1222 cerr << "Bad $ operand number in inline asm string: '"
1226 LastEmitted = IDEnd;
1228 char Modifier[2] = { 0, 0 };
1230 if (HasCurlyBraces) {
1231 // If we have curly braces, check for a modifier character. This
1232 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1233 if (*LastEmitted == ':') {
1234 ++LastEmitted; // Consume ':' character.
1235 if (*LastEmitted == 0) {
1236 cerr << "Bad ${:} expression in inline asm string: '"
1241 Modifier[0] = *LastEmitted;
1242 ++LastEmitted; // Consume modifier character.
1245 if (*LastEmitted != '}') {
1246 cerr << "Bad ${} expression in inline asm string: '"
1250 ++LastEmitted; // Consume '}' character.
1253 if ((unsigned)Val >= NumOperands-1) {
1254 cerr << "Invalid $ operand number in inline asm string: '"
1259 // Okay, we finally have a value number. Ask the target to print this
1261 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1266 // Scan to find the machine operand number for the operand.
1267 for (; Val; --Val) {
1268 if (OpNo >= MI->getNumOperands()) break;
1269 unsigned OpFlags = MI->getOperand(OpNo).getImm();
1270 OpNo += (OpFlags >> 3) + 1;
1273 if (OpNo >= MI->getNumOperands()) {
1276 unsigned OpFlags = MI->getOperand(OpNo).getImm();
1277 ++OpNo; // Skip over the ID number.
1279 if (Modifier[0]=='l') // labels are target independent
1280 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
1281 false, false, false);
1283 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1284 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1285 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1286 Modifier[0] ? Modifier : 0);
1288 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1289 Modifier[0] ? Modifier : 0);
1294 cerr << "Invalid operand found in inline asm: '"
1304 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
1307 /// printImplicitDef - This method prints the specified machine instruction
1308 /// that is an implicit def.
1309 void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
1310 O << '\t' << TAI->getCommentString() << " implicit-def: "
1311 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
1314 /// printLabel - This method prints a local label used by debug and
1315 /// exception handling tables.
1316 void AsmPrinter::printLabel(const MachineInstr *MI) const {
1317 printLabel(MI->getOperand(0).getImm());
1320 void AsmPrinter::printLabel(unsigned Id) const {
1321 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
1324 /// printDeclare - This method prints a local variable declaration used by
1326 /// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1327 /// entry into dwarf table.
1328 void AsmPrinter::printDeclare(const MachineInstr *MI) const {
1329 int FI = MI->getOperand(0).getIndex();
1330 GlobalValue *GV = MI->getOperand(1).getGlobal();
1331 MMI->RecordVariable(GV, FI);
1334 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1335 /// instruction, using the specified assembler variant. Targets should
1336 /// overried this to format as appropriate.
1337 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1338 unsigned AsmVariant, const char *ExtraCode) {
1339 // Target doesn't support this yet!
1343 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1344 unsigned AsmVariant,
1345 const char *ExtraCode) {
1346 // Target doesn't support this yet!
1350 /// printBasicBlockLabel - This method prints the label for the specified
1351 /// MachineBasicBlock
1352 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1355 bool printComment) const {
1357 unsigned Align = MBB->getAlignment();
1359 EmitAlignment(Log2_32(Align));
1362 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
1363 << MBB->getNumber();
1366 if (printComment && MBB->getBasicBlock())
1367 O << '\t' << TAI->getCommentString() << ' '
1368 << MBB->getBasicBlock()->getNameStart();
1371 /// printPICJumpTableSetLabel - This method prints a set label for the
1372 /// specified MachineBasicBlock for a jumptable entry.
1373 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1374 const MachineBasicBlock *MBB) const {
1375 if (!TAI->getSetDirective())
1378 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1379 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
1380 printBasicBlockLabel(MBB, false, false, false);
1381 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1382 << '_' << uid << '\n';
1385 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1386 const MachineBasicBlock *MBB) const {
1387 if (!TAI->getSetDirective())
1390 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1391 << getFunctionNumber() << '_' << uid << '_' << uid2
1392 << "_set_" << MBB->getNumber() << ',';
1393 printBasicBlockLabel(MBB, false, false, false);
1394 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1395 << '_' << uid << '_' << uid2 << '\n';
1398 /// printDataDirective - This method prints the asm directive for the
1400 void AsmPrinter::printDataDirective(const Type *type) {
1401 const TargetData *TD = TM.getTargetData();
1402 switch (type->getTypeID()) {
1403 case Type::IntegerTyID: {
1404 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1406 O << TAI->getData8bitsDirective();
1407 else if (BitWidth <= 16)
1408 O << TAI->getData16bitsDirective();
1409 else if (BitWidth <= 32)
1410 O << TAI->getData32bitsDirective();
1411 else if (BitWidth <= 64) {
1412 assert(TAI->getData64bitsDirective() &&
1413 "Target cannot handle 64-bit constant exprs!");
1414 O << TAI->getData64bitsDirective();
1418 case Type::PointerTyID:
1419 if (TD->getPointerSize() == 8) {
1420 assert(TAI->getData64bitsDirective() &&
1421 "Target cannot handle 64-bit pointer exprs!");
1422 O << TAI->getData64bitsDirective();
1424 O << TAI->getData32bitsDirective();
1427 case Type::FloatTyID: case Type::DoubleTyID:
1428 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1429 assert (0 && "Should have already output floating point constant.");
1431 assert (0 && "Can't handle printing this type of thing");
1436 void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1437 const char *Prefix) {
1440 O << TAI->getPrivateGlobalPrefix();
1441 if (Prefix) O << Prefix;
1453 void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
1454 printSuffixedName(Name.c_str(), Suffix);
1457 void AsmPrinter::printVisibility(const std::string& Name,
1458 unsigned Visibility) const {
1459 if (Visibility == GlobalValue::HiddenVisibility) {
1460 if (const char *Directive = TAI->getHiddenDirective())
1461 O << Directive << Name << '\n';
1462 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1463 if (const char *Directive = TAI->getProtectedDirective())
1464 O << Directive << Name << '\n';
1468 GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1469 if (!S->usesMetadata())
1472 gcp_iterator GCPI = GCMetadataPrinters.find(S);
1473 if (GCPI != GCMetadataPrinters.end())
1474 return GCPI->second;
1476 const char *Name = S->getName().c_str();
1478 for (GCMetadataPrinterRegistry::iterator
1479 I = GCMetadataPrinterRegistry::begin(),
1480 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1481 if (strcmp(Name, I->getName()) == 0) {
1482 GCMetadataPrinter *GMP = I->instantiate();
1484 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1488 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";