Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 1 | //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AsmPrinter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | 932f022 | 2006-03-03 02:04:29 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/AsmPrinter.h" |
Evan Cheng | 246ae0d | 2006-03-01 22:18:09 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Devang Patel | c48c550 | 2009-01-13 21:44:10 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/DwarfWriter.h" |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/DebugInfo.h" |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCContext.h" |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInst.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSection.h" |
| 28 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Mangler.h" |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetAsmInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 0336fdb | 2006-10-06 22:50:56 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallString.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 42 | #include <cerrno> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 45 | static cl::opt<cl::boolOrDefault> |
| 46 | AsmVerbose("asm-verbose", cl::desc("Add comments to directives."), |
| 47 | cl::init(cl::BOU_UNSET)); |
| 48 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 49 | char AsmPrinter::ID = 0; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 50 | AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, |
Daniel Dunbar | 5bcc8bd | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 51 | const TargetAsmInfo *T, bool VDef) |
| 52 | : MachineFunctionPass(&ID), FunctionNumber(0), O(o), |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 53 | TM(tm), TAI(T), TRI(tm.getRegisterInfo()), |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 54 | |
| 55 | OutContext(*new MCContext()), |
| 56 | OutStreamer(*createAsmStreamer(OutContext, O)), |
| 57 | |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 58 | LastMI(0), LastFn(0), Counter(~0U), |
Owen Anderson | a8dbf36 | 2009-06-25 16:55:32 +0000 | [diff] [blame] | 59 | PrevDLT(0, ~0U, ~0U) { |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 60 | CurrentSection = 0; |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 61 | DW = 0; MMI = 0; |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 62 | switch (AsmVerbose) { |
| 63 | case cl::BOU_UNSET: VerboseAsm = VDef; break; |
| 64 | case cl::BOU_TRUE: VerboseAsm = true; break; |
| 65 | case cl::BOU_FALSE: VerboseAsm = false; break; |
| 66 | } |
| 67 | } |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 68 | |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 69 | AsmPrinter::~AsmPrinter() { |
| 70 | for (gcp_iterator I = GCMetadataPrinters.begin(), |
| 71 | E = GCMetadataPrinters.end(); I != E; ++I) |
| 72 | delete I->second; |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 73 | |
| 74 | delete &OutStreamer; |
| 75 | delete &OutContext; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 38c3988 | 2009-08-03 19:12:26 +0000 | [diff] [blame] | 78 | TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 79 | return TM.getTargetLowering()->getObjFileLowering(); |
| 80 | } |
| 81 | |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 82 | /// SwitchToSection - Switch to the specified section of the executable if we |
Chris Lattner | d54a6b8 | 2009-08-03 18:04:42 +0000 | [diff] [blame] | 83 | /// are not already in it! If "NS" is null, then this causes us to exit the |
| 84 | /// current section and not reenter another one. This is generally used for |
| 85 | /// asmprinter hacks. |
| 86 | /// |
| 87 | /// FIXME: Remove support for null sections. |
| 88 | /// |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 89 | void AsmPrinter::SwitchToSection(const MCSection *NS) { |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 90 | // If we're already in this section, we're done. |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 91 | if (CurrentSection == NS) return; |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 92 | |
| 93 | // Close the current section, if applicable. |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 94 | if (NS != 0 && TAI->getSectionEndDirectiveSuffix()) |
| 95 | O << NS->getName() << TAI->getSectionEndDirectiveSuffix() << '\n'; |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 97 | CurrentSection = NS; |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 99 | if (NS != 0) { |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 100 | // If section is named we need to switch into it via special '.section' |
| 101 | // directive and also append funky flags. Otherwise - section name is just |
| 102 | // some magic assembler directive. |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 103 | if (!NS->isDirective()) { |
Chris Lattner | f40761d | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 104 | SmallString<32> FlagsStr; |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 105 | |
| 106 | getObjFileLowering().getSectionFlagsAsString(NS->getKind(), FlagsStr); |
Chris Lattner | 5fe575f | 2009-07-27 05:32:16 +0000 | [diff] [blame] | 107 | |
| 108 | O << TAI->getSwitchToSectionDirective() |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 109 | << CurrentSection->getName() << FlagsStr.c_str(); |
Chris Lattner | f40761d | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 110 | } else { |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 111 | O << CurrentSection->getName(); |
Chris Lattner | f40761d | 2009-07-26 07:33:58 +0000 | [diff] [blame] | 112 | } |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 113 | O << TAI->getDataSectionStartSuffix() << '\n'; |
| 114 | } |
Anton Korobeynikov | b5a32e2 | 2008-09-24 22:12:10 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 116 | |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 117 | void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 118 | AU.setPreservesAll(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 119 | MachineFunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 120 | AU.addRequired<GCModuleInfo>(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 123 | bool AsmPrinter::doInitialization(Module &M) { |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 124 | // Initialize TargetLoweringObjectFile. |
| 125 | const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) |
| 126 | .Initialize(OutContext, TM); |
| 127 | |
Bill Wendling | 4cef584 | 2009-07-20 21:30:28 +0000 | [diff] [blame] | 128 | Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(), |
Chris Lattner | 90f8b70 | 2009-07-21 17:30:51 +0000 | [diff] [blame] | 129 | TAI->getLinkerPrivateGlobalPrefix()); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 130 | |
Chris Lattner | a93ca92 | 2009-06-18 23:41:35 +0000 | [diff] [blame] | 131 | if (TAI->doesAllowQuotesInName()) |
| 132 | Mang->setUseQuotes(true); |
| 133 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 134 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 135 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 136 | |
| 137 | if (TAI->hasSingleParameterDotFile()) { |
| 138 | /* Very minimal debug info. It is ignored if we emit actual |
| 139 | debug info. If we don't, this at helps the user find where |
| 140 | a function came from. */ |
| 141 | O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n"; |
| 142 | } |
| 143 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 144 | for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) |
| 145 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) |
| 146 | MP->beginAssembly(O, *this, *TAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 148 | if (!M.getModuleInlineAsm().empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 149 | O << TAI->getCommentString() << " Start of file scope inline assembly\n" |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 150 | << M.getModuleInlineAsm() |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 151 | << '\n' << TAI->getCommentString() |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 152 | << " End of file scope inline assembly\n"; |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 153 | |
Chris Lattner | c6fdced | 2009-08-03 23:10:34 +0000 | [diff] [blame] | 154 | SwitchToSection(0); // Reset back to no section to close off sections. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 156 | if (TAI->doesSupportDebugInformation() || |
| 157 | TAI->doesSupportExceptionHandling()) { |
| 158 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 159 | if (MMI) |
Devang Patel | 14a55d9 | 2009-06-19 21:54:26 +0000 | [diff] [blame] | 160 | MMI->AnalyzeModule(M); |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 161 | DW = getAnalysisIfAvailable<DwarfWriter>(); |
| 162 | if (DW) |
| 163 | DW->BeginModule(&M, MMI, O, this, TAI); |
Devang Patel | 14a55d9 | 2009-06-19 21:54:26 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
| 169 | bool AsmPrinter::doFinalization(Module &M) { |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 170 | // Emit global variables. |
| 171 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 172 | I != E; ++I) |
| 173 | PrintGlobalVariable(I); |
| 174 | |
Chris Lattner | 1f522fe | 2009-06-24 18:54:37 +0000 | [diff] [blame] | 175 | // Emit final debug information. |
| 176 | if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling()) |
| 177 | DW->EndModule(); |
| 178 | |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 179 | // If the target wants to know about weak references, print them all. |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 180 | if (TAI->getWeakRefDirective()) { |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 181 | // FIXME: This is not lazy, it would be nice to only print weak references |
| 182 | // to stuff that is actually used. Note that doing so would require targets |
| 183 | // to notice uses in operands (due to constant exprs etc). This should |
| 184 | // happen with the MC stuff eventually. |
Chris Lattner | c6fdced | 2009-08-03 23:10:34 +0000 | [diff] [blame] | 185 | SwitchToSection(0); |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 187 | // Print out module-level global variables here. |
| 188 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 189 | I != E; ++I) { |
| 190 | if (I->hasExternalWeakLinkage()) |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 191 | O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | c6fdced | 2009-08-03 23:10:34 +0000 | [diff] [blame] | 194 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 195 | if (I->hasExternalWeakLinkage()) |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 196 | O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 197 | } |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 200 | if (TAI->getSetDirective()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 201 | O << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 202 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 203 | I != E; ++I) { |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 204 | std::string Name = Mang->getMangledName(I); |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 205 | |
| 206 | const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 207 | std::string Target = Mang->getMangledName(GV); |
Anton Korobeynikov | 541af7f | 2008-09-24 22:21:04 +0000 | [diff] [blame] | 208 | |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 209 | if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 210 | O << "\t.globl\t" << Name << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 211 | else if (I->hasWeakLinkage()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 212 | O << TAI->getWeakRefDirective() << Name << '\n'; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 213 | else if (!I->hasLocalLinkage()) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 214 | llvm_unreachable("Invalid alias linkage"); |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 215 | |
Anton Korobeynikov | 541af7f | 2008-09-24 22:21:04 +0000 | [diff] [blame] | 216 | printVisibility(Name, I->getVisibility()); |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 217 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 218 | O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 222 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 223 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 224 | for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) |
| 225 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) |
| 226 | MP->finishAssembly(O, *this, *TAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 227 | |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 228 | // If we don't have any trampolines, then we don't require stack memory |
| 229 | // to be executable. Some targets have a directive to declare this. |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 230 | Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 231 | if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) |
| 232 | if (TAI->getNonexecutableStackDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 233 | O << TAI->getNonexecutableStackDirective() << '\n'; |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 234 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 235 | delete Mang; Mang = 0; |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 236 | DW = 0; MMI = 0; |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 237 | |
| 238 | OutStreamer.Finish(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 242 | std::string |
| 243 | AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const { |
Bill Wendling | 6e19896 | 2007-09-18 01:47:22 +0000 | [diff] [blame] | 244 | assert(MF && "No machine function?"); |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 245 | return Mang->getMangledName(MF->getFunction(), ".eh", |
| 246 | TAI->is_EHSymbolPrivate()); |
Bill Wendling | 6e19896 | 2007-09-18 01:47:22 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 249 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 250 | // What's my mangled name? |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 251 | CurrentFnName = Mang->getMangledName(MF.getFunction()); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 252 | IncrementFunctionNumber(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 255 | namespace { |
| 256 | // SectionCPs - Keep track the alignment, constpool entries per Section. |
| 257 | struct SectionCPs { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 258 | const MCSection *S; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 259 | unsigned Alignment; |
| 260 | SmallVector<unsigned, 4> CPEs; |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 261 | SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 262 | }; |
| 263 | } |
| 264 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 265 | /// EmitConstantPool - Print to the current output stream assembly |
| 266 | /// representations of the constants in the constant pool MCP. This is |
| 267 | /// used to print out constants which have been "spilled to memory" by |
| 268 | /// the code generator. |
| 269 | /// |
| 270 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 271 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 272 | if (CP.empty()) return; |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 273 | |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 274 | // Calculate sections for constant pool entries. We collect entries to go into |
| 275 | // the same section together to reduce amount of section switch statements. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 276 | SmallVector<SectionCPs, 4> CPSections; |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 277 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 278 | const MachineConstantPoolEntry &CPE = CP[i]; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 279 | unsigned Align = CPE.getAlignment(); |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 280 | |
| 281 | SectionKind Kind; |
| 282 | switch (CPE.getRelocationInfo()) { |
| 283 | default: llvm_unreachable("Unknown section kind"); |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 284 | case 2: Kind = SectionKind::getReadOnlyWithRel(); break; |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 285 | case 1: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 286 | Kind = SectionKind::getReadOnlyWithRelLocal(); |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 287 | break; |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 288 | case 0: |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 289 | switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 290 | case 4: Kind = SectionKind::getMergeableConst4(); break; |
| 291 | case 8: Kind = SectionKind::getMergeableConst8(); break; |
| 292 | case 16: Kind = SectionKind::getMergeableConst16();break; |
| 293 | default: Kind = SectionKind::getMergeableConst(); break; |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 294 | } |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 297 | const MCSection *S = getObjFileLowering().getSectionForConstant(Kind); |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 298 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 299 | // The number of sections are small, just do a linear search from the |
| 300 | // last section to the first. |
| 301 | bool Found = false; |
| 302 | unsigned SecIdx = CPSections.size(); |
| 303 | while (SecIdx != 0) { |
| 304 | if (CPSections[--SecIdx].S == S) { |
| 305 | Found = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | if (!Found) { |
| 310 | SecIdx = CPSections.size(); |
| 311 | CPSections.push_back(SectionCPs(S, Align)); |
| 312 | } |
| 313 | |
| 314 | if (Align > CPSections[SecIdx].Alignment) |
| 315 | CPSections[SecIdx].Alignment = Align; |
| 316 | CPSections[SecIdx].CPEs.push_back(i); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 319 | // Now print stuff into the calculated sections. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 320 | for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { |
| 321 | SwitchToSection(CPSections[i].S); |
| 322 | EmitAlignment(Log2_32(CPSections[i].Alignment)); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 323 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 324 | unsigned Offset = 0; |
| 325 | for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { |
| 326 | unsigned CPI = CPSections[i].CPEs[j]; |
| 327 | MachineConstantPoolEntry CPE = CP[CPI]; |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 329 | // Emit inter-object padding for alignment. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 330 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 331 | unsigned NewOffset = (Offset + AlignMask) & ~AlignMask; |
| 332 | EmitZeros(NewOffset - Offset); |
| 333 | |
| 334 | const Type *Ty = CPE.getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 335 | Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 336 | |
| 337 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' |
| 338 | << CPI << ":\t\t\t\t\t"; |
| 339 | if (VerboseAsm) { |
| 340 | O << TAI->getCommentString() << ' '; |
| 341 | WriteTypeSymbolic(O, CPE.getType(), 0); |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 342 | } |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 343 | O << '\n'; |
| 344 | if (CPE.isMachineConstantPoolEntry()) |
| 345 | EmitMachineConstantPoolValue(CPE.Val.MachineCPVal); |
| 346 | else |
| 347 | EmitGlobalConstant(CPE.Val.ConstVal); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 352 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 353 | /// by the current function to the current output stream. |
| 354 | /// |
Chris Lattner | 1da31ee | 2006-10-05 03:01:21 +0000 | [diff] [blame] | 355 | void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, |
| 356 | MachineFunction &MF) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 357 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 358 | if (JT.empty()) return; |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 359 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 360 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | 4d9bbdc | 2006-07-27 16:46:58 +0000 | [diff] [blame] | 361 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 362 | // Pick the directive to use to print the jump table entries, and switch to |
| 363 | // the appropriate section. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 364 | TargetLowering *LoweringInfo = TM.getTargetLowering(); |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 365 | |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 366 | const Function *F = MF.getFunction(); |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 367 | bool JTInDiffSection = false; |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 368 | if (F->isWeakForLinker() || |
| 369 | (IsPic && !LoweringInfo->usesGlobalOffsetTable())) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 370 | // In PIC mode, we need to emit the jump table to the same section as the |
| 371 | // function body itself, otherwise the label differences won't make sense. |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 372 | // We should also do if the section name is NULL or function is declared in |
| 373 | // discardable section. |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 374 | SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 375 | } else { |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 376 | // Otherwise, drop it in the readonly section. |
| 377 | const MCSection *ReadOnlySection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 378 | getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly()); |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 379 | SwitchToSection(ReadOnlySection); |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 380 | JTInDiffSection = true; |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 381 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 382 | |
| 383 | EmitAlignment(Log2_32(MJTI->getAlignment())); |
Chris Lattner | 0c4e678 | 2006-07-15 01:34:12 +0000 | [diff] [blame] | 384 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 385 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 386 | const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; |
Chris Lattner | 0737188 | 2006-10-28 18:10:06 +0000 | [diff] [blame] | 387 | |
| 388 | // If this jump table was deleted, ignore it. |
| 389 | if (JTBBs.empty()) continue; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 390 | |
| 391 | // For PIC codegen, if possible we want to use the SetDirective to reduce |
| 392 | // the number of relocations the assembler will generate for the jump table. |
| 393 | // Set directives are all printed before the jump table itself. |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 394 | SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 395 | if (TAI->getSetDirective() && IsPic) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 396 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 397 | if (EmittedSets.insert(JTBBs[ii])) |
| 398 | printPICJumpTableSetLabel(i, JTBBs[ii]); |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 400 | // On some targets (e.g. darwin) we want to emit two consequtive labels |
| 401 | // before each jump table. The first label is never referenced, but tells |
| 402 | // the assembler and linker the extents of the jump table object. The |
| 403 | // second label is actually referenced by the code. |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 404 | if (JTInDiffSection) { |
| 405 | if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) |
| 406 | O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; |
| 407 | } |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 408 | |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 409 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 410 | << '_' << i << ":\n"; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 411 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 412 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 413 | printPICJumpTableEntry(MJTI, JTBBs[ii], i); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 414 | O << '\n'; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 419 | void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 420 | const MachineBasicBlock *MBB, |
| 421 | unsigned uid) const { |
| 422 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
| 423 | |
| 424 | // Use JumpTableDirective otherwise honor the entry size from the jump table |
| 425 | // info. |
| 426 | const char *JTEntryDirective = TAI->getJumpTableDirective(); |
| 427 | bool HadJTEntryDirective = JTEntryDirective != NULL; |
| 428 | if (!HadJTEntryDirective) { |
| 429 | JTEntryDirective = MJTI->getEntrySize() == 4 ? |
| 430 | TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); |
| 431 | } |
| 432 | |
| 433 | O << JTEntryDirective << ' '; |
| 434 | |
| 435 | // If we have emitted set directives for the jump table entries, print |
| 436 | // them rather than the entries themselves. If we're emitting PIC, then |
| 437 | // emit the table entries as differences between two text section labels. |
| 438 | // If we're emitting non-PIC code, then emit the entries as direct |
| 439 | // references to the target basic blocks. |
| 440 | if (IsPic) { |
| 441 | if (TAI->getSetDirective()) { |
| 442 | O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 443 | << '_' << uid << "_set_" << MBB->getNumber(); |
| 444 | } else { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 445 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 446 | // If the arch uses custom Jump Table directives, don't calc relative to |
| 447 | // JT |
| 448 | if (!HadJTEntryDirective) |
| 449 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" |
| 450 | << getFunctionNumber() << '_' << uid; |
| 451 | } |
| 452 | } else { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 453 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 458 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 459 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 460 | /// do nothing and return false. |
| 461 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 462 | if (GV->getName() == "llvm.used") { |
Andrew Lenharth | b753a9b | 2007-08-22 19:33:11 +0000 | [diff] [blame] | 463 | if (TAI->getUsedDirective() != 0) // No need to emit this at all. |
| 464 | EmitLLVMUsedList(GV->getInitializer()); |
| 465 | return true; |
| 466 | } |
| 467 | |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 468 | // Ignore debug and non-emitted data. This handles llvm.compiler.used. |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 469 | if (GV->getSection() == "llvm.metadata" || |
| 470 | GV->hasAvailableExternallyLinkage()) |
| 471 | return true; |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 472 | |
| 473 | if (!GV->hasAppendingLinkage()) return false; |
| 474 | |
| 475 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 476 | |
Evan Cheng | 916d07c | 2007-06-04 20:39:18 +0000 | [diff] [blame] | 477 | const TargetData *TD = TM.getTargetData(); |
| 478 | unsigned Align = Log2_32(TD->getPointerPrefAlignment()); |
Chris Lattner | f231c07 | 2009-03-09 05:52:15 +0000 | [diff] [blame] | 479 | if (GV->getName() == "llvm.global_ctors") { |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 480 | SwitchToSection(getObjFileLowering().getStaticCtorSection()); |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 481 | EmitAlignment(Align, 0); |
| 482 | EmitXXStructorList(GV->getInitializer()); |
| 483 | return true; |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | f231c07 | 2009-03-09 05:52:15 +0000 | [diff] [blame] | 486 | if (GV->getName() == "llvm.global_dtors") { |
Chris Lattner | 80ec279 | 2009-08-02 00:34:36 +0000 | [diff] [blame] | 487 | SwitchToSection(getObjFileLowering().getStaticDtorSection()); |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 488 | EmitAlignment(Align, 0); |
| 489 | EmitXXStructorList(GV->getInitializer()); |
| 490 | return true; |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 496 | /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 497 | /// global in the specified llvm.used list for which emitUsedDirectiveFor |
| 498 | /// is true, as being used with this directive. |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 499 | void AsmPrinter::EmitLLVMUsedList(Constant *List) { |
| 500 | const char *Directive = TAI->getUsedDirective(); |
| 501 | |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 502 | // Should be an array of 'i8*'. |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 503 | ConstantArray *InitList = dyn_cast<ConstantArray>(List); |
| 504 | if (InitList == 0) return; |
| 505 | |
| 506 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
Chris Lattner | 16fe990 | 2009-07-17 22:00:23 +0000 | [diff] [blame] | 507 | const GlobalValue *GV = |
| 508 | dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 509 | if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) { |
Dale Johannesen | 61e6093 | 2008-09-03 20:34:58 +0000 | [diff] [blame] | 510 | O << Directive; |
| 511 | EmitConstantValueOnly(InitList->getOperand(i)); |
| 512 | O << '\n'; |
| 513 | } |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 517 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 518 | /// function pointers, ignoring the init priority. |
| 519 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 520 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 521 | // init priority, which we ignore. |
| 522 | if (!isa<ConstantArray>(List)) return; |
| 523 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 524 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 525 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 526 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 527 | |
| 528 | if (CS->getOperand(1)->isNullValue()) |
| 529 | return; // Found a null terminator, exit printing. |
| 530 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 531 | EmitGlobalConstant(CS->getOperand(1)); |
| 532 | } |
| 533 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 534 | |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 535 | /// getGlobalLinkName - Returns the asm/link name of of the specified |
| 536 | /// global variable. Should be overridden by each target asm printer to |
| 537 | /// generate the appropriate value. |
Bill Wendling | 7d16e85 | 2009-04-10 00:12:49 +0000 | [diff] [blame] | 538 | const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV, |
| 539 | std::string &LinkName) const { |
Jim Laskey | 0374248 | 2006-11-20 20:29:06 +0000 | [diff] [blame] | 540 | if (isa<Function>(GV)) { |
| 541 | LinkName += TAI->getFunctionAddrPrefix(); |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 542 | LinkName += Mang->getMangledName(GV); |
Jim Laskey | 0374248 | 2006-11-20 20:29:06 +0000 | [diff] [blame] | 543 | LinkName += TAI->getFunctionAddrSuffix(); |
| 544 | } else { |
| 545 | LinkName += TAI->getGlobalVarAddrPrefix(); |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 546 | LinkName += Mang->getMangledName(GV); |
Jim Laskey | 0374248 | 2006-11-20 20:29:06 +0000 | [diff] [blame] | 547 | LinkName += TAI->getGlobalVarAddrSuffix(); |
| 548 | } |
| 549 | |
Jim Laskey | 99e41ee | 2006-10-17 17:17:24 +0000 | [diff] [blame] | 550 | return LinkName; |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 553 | /// EmitExternalGlobal - Emit the external reference to a global variable. |
| 554 | /// Should be overridden if an indirect reference should be used. |
| 555 | void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { |
Bill Wendling | 7d16e85 | 2009-04-10 00:12:49 +0000 | [diff] [blame] | 556 | std::string GLN; |
| 557 | O << getGlobalLinkName(GV, GLN); |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
| 563 | /// LEB 128 number encoding. |
| 564 | |
| 565 | /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) |
| 566 | /// representing an unsigned leb128 value. |
| 567 | void AsmPrinter::PrintULEB128(unsigned Value) const { |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 568 | char Buffer[20]; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 569 | do { |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 570 | unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 571 | Value >>= 7; |
| 572 | if (Value) Byte |= 0x80; |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 573 | O << "0x" << utohex_buffer(Byte, Buffer+20); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 574 | if (Value) O << ", "; |
| 575 | } while (Value); |
| 576 | } |
| 577 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 578 | /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) |
| 579 | /// representing a signed leb128 value. |
| 580 | void AsmPrinter::PrintSLEB128(int Value) const { |
| 581 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 582 | bool IsMore; |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 583 | char Buffer[20]; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 584 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 585 | do { |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 586 | unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 587 | Value >>= 7; |
| 588 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 589 | if (IsMore) Byte |= 0x80; |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 590 | O << "0x" << utohex_buffer(Byte, Buffer+20); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 591 | if (IsMore) O << ", "; |
| 592 | } while (IsMore); |
| 593 | } |
| 594 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 595 | //===--------------------------------------------------------------------===// |
| 596 | // Emission and print routines |
| 597 | // |
| 598 | |
| 599 | /// PrintHex - Print a value as a hexidecimal value. |
| 600 | /// |
| 601 | void AsmPrinter::PrintHex(int Value) const { |
Chris Lattner | c6a1346 | 2008-11-10 04:30:26 +0000 | [diff] [blame] | 602 | char Buffer[20]; |
Chris Lattner | c6a1346 | 2008-11-10 04:30:26 +0000 | [diff] [blame] | 603 | O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 607 | /// then it will be printed first. Comments should not contain '\n'. |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 608 | void AsmPrinter::EOL() const { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 609 | O << '\n'; |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 610 | } |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 611 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 612 | void AsmPrinter::EOL(const std::string &Comment) const { |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 613 | if (VerboseAsm && !Comment.empty()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 614 | O << '\t' |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 615 | << TAI->getCommentString() |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 616 | << ' ' |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 617 | << Comment; |
| 618 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 619 | O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 622 | void AsmPrinter::EOL(const char* Comment) const { |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 623 | if (VerboseAsm && *Comment) { |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 624 | O << '\t' |
| 625 | << TAI->getCommentString() |
| 626 | << ' ' |
| 627 | << Comment; |
| 628 | } |
| 629 | O << '\n'; |
| 630 | } |
| 631 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 632 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
| 633 | /// unsigned leb128 value. |
| 634 | void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { |
| 635 | if (TAI->hasLEB128()) { |
| 636 | O << "\t.uleb128\t" |
| 637 | << Value; |
| 638 | } else { |
| 639 | O << TAI->getData8bitsDirective(); |
| 640 | PrintULEB128(Value); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /// EmitSLEB128Bytes - print an assembler byte data directive to compose a |
| 645 | /// signed leb128 value. |
| 646 | void AsmPrinter::EmitSLEB128Bytes(int Value) const { |
| 647 | if (TAI->hasLEB128()) { |
| 648 | O << "\t.sleb128\t" |
| 649 | << Value; |
| 650 | } else { |
| 651 | O << TAI->getData8bitsDirective(); |
| 652 | PrintSLEB128(Value); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /// EmitInt8 - Emit a byte directive and value. |
| 657 | /// |
| 658 | void AsmPrinter::EmitInt8(int Value) const { |
| 659 | O << TAI->getData8bitsDirective(); |
| 660 | PrintHex(Value & 0xFF); |
| 661 | } |
| 662 | |
| 663 | /// EmitInt16 - Emit a short directive and value. |
| 664 | /// |
| 665 | void AsmPrinter::EmitInt16(int Value) const { |
| 666 | O << TAI->getData16bitsDirective(); |
| 667 | PrintHex(Value & 0xFFFF); |
| 668 | } |
| 669 | |
| 670 | /// EmitInt32 - Emit a long directive and value. |
| 671 | /// |
| 672 | void AsmPrinter::EmitInt32(int Value) const { |
| 673 | O << TAI->getData32bitsDirective(); |
| 674 | PrintHex(Value); |
| 675 | } |
| 676 | |
| 677 | /// EmitInt64 - Emit a long long directive and value. |
| 678 | /// |
| 679 | void AsmPrinter::EmitInt64(uint64_t Value) const { |
| 680 | if (TAI->getData64bitsDirective()) { |
| 681 | O << TAI->getData64bitsDirective(); |
| 682 | PrintHex(Value); |
| 683 | } else { |
| 684 | if (TM.getTargetData()->isBigEndian()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 685 | EmitInt32(unsigned(Value >> 32)); O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 686 | EmitInt32(unsigned(Value)); |
| 687 | } else { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 688 | EmitInt32(unsigned(Value)); O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 689 | EmitInt32(unsigned(Value >> 32)); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 695 | /// |
| 696 | static inline char toOctal(int X) { |
| 697 | return (X&7)+'0'; |
| 698 | } |
| 699 | |
| 700 | /// printStringChar - Print a char, escaped if necessary. |
| 701 | /// |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 702 | static void printStringChar(formatted_raw_ostream &O, unsigned char C) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 703 | if (C == '"') { |
| 704 | O << "\\\""; |
| 705 | } else if (C == '\\') { |
| 706 | O << "\\\\"; |
Chris Lattner | bbfa244 | 2009-01-22 23:38:45 +0000 | [diff] [blame] | 707 | } else if (isprint((unsigned char)C)) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 708 | O << C; |
| 709 | } else { |
| 710 | switch(C) { |
| 711 | case '\b': O << "\\b"; break; |
| 712 | case '\f': O << "\\f"; break; |
| 713 | case '\n': O << "\\n"; break; |
| 714 | case '\r': O << "\\r"; break; |
| 715 | case '\t': O << "\\t"; break; |
| 716 | default: |
| 717 | O << '\\'; |
| 718 | O << toOctal(C >> 6); |
| 719 | O << toOctal(C >> 3); |
| 720 | O << toOctal(C >> 0); |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /// EmitString - Emit a string with quotes and a null terminator. |
| 727 | /// Special characters are emitted properly. |
| 728 | /// \literal (Eg. '\t') \endliteral |
| 729 | void AsmPrinter::EmitString(const std::string &String) const { |
Bill Wendling | f34be82 | 2009-04-09 23:51:31 +0000 | [diff] [blame] | 730 | EmitString(String.c_str(), String.size()); |
| 731 | } |
| 732 | |
| 733 | void AsmPrinter::EmitString(const char *String, unsigned Size) const { |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 734 | const char* AscizDirective = TAI->getAscizDirective(); |
| 735 | if (AscizDirective) |
| 736 | O << AscizDirective; |
| 737 | else |
| 738 | O << TAI->getAsciiDirective(); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 739 | O << '\"'; |
Bill Wendling | f34be82 | 2009-04-09 23:51:31 +0000 | [diff] [blame] | 740 | for (unsigned i = 0; i < Size; ++i) |
Chris Lattner | a118c2e | 2009-04-08 00:28:38 +0000 | [diff] [blame] | 741 | printStringChar(O, String[i]); |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 742 | if (AscizDirective) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 743 | O << '\"'; |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 744 | else |
| 745 | O << "\\0\""; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 749 | /// EmitFile - Emit a .file directive. |
| 750 | void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { |
| 751 | O << "\t.file\t" << Number << " \""; |
Chris Lattner | a118c2e | 2009-04-08 00:28:38 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, N = Name.size(); i < N; ++i) |
| 753 | printStringChar(O, Name[i]); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 754 | O << '\"'; |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 758 | //===----------------------------------------------------------------------===// |
| 759 | |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 760 | // EmitAlignment - Emit an alignment directive to the specified power of |
| 761 | // two boundary. For example, if you pass in 3 here, you will get an 8 |
| 762 | // byte alignment. If a global value is specified, and if that global has |
| 763 | // an explicit alignment requested, it will unconditionally override the |
| 764 | // alignment request. However, if ForcedAlignBits is specified, this value |
| 765 | // has final say: the ultimate alignment will be the max of ForcedAlignBits |
| 766 | // and the alignment computed with NumBits and the global. |
| 767 | // |
| 768 | // The algorithm is: |
| 769 | // Align = NumBits; |
| 770 | // if (GV && GV->hasalignment) Align = GV->getalignment(); |
| 771 | // Align = std::max(Align, ForcedAlignBits); |
| 772 | // |
| 773 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, |
Evan Cheng | 05548eb | 2008-02-29 19:36:59 +0000 | [diff] [blame] | 774 | unsigned ForcedAlignBits, |
| 775 | bool UseFillExpr) const { |
Dale Johannesen | 00d56b9 | 2007-04-23 23:33:31 +0000 | [diff] [blame] | 776 | if (GV && GV->getAlignment()) |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 777 | NumBits = Log2_32(GV->getAlignment()); |
| 778 | NumBits = std::max(NumBits, ForcedAlignBits); |
| 779 | |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 780 | if (NumBits == 0) return; // No need to emit alignment. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 781 | if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; |
Evan Cheng | 73a259a | 2007-07-25 23:35:07 +0000 | [diff] [blame] | 782 | O << TAI->getAlignDirective() << NumBits; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame^] | 784 | if (CurrentSection && CurrentSection->getKind().isText()) |
| 785 | if (unsigned FillValue = TAI->getTextAlignFillValue()) { |
| 786 | O << ','; |
| 787 | PrintHex(FillValue); |
| 788 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 789 | O << '\n'; |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 790 | } |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 791 | |
David Greene | bef8768 | 2009-07-31 21:57:10 +0000 | [diff] [blame] | 792 | /// PadToColumn - This gets called every time a tab is emitted. If |
| 793 | /// column padding is turned on, we replace the tab with the |
| 794 | /// appropriate amount of padding. If not, we replace the tab with a |
| 795 | /// space, except for the first operand so that initial operands are |
| 796 | /// always lined up by tabs. |
| 797 | void AsmPrinter::PadToColumn(unsigned Operand) const { |
| 798 | if (TAI->getOperandColumn(Operand) > 0) { |
| 799 | O.PadToColumn(TAI->getOperandColumn(Operand), 1); |
| 800 | } |
| 801 | else { |
| 802 | if (Operand == 1) { |
| 803 | // Emit the tab after the mnemonic. |
| 804 | O << '\t'; |
| 805 | } |
| 806 | else { |
| 807 | // Replace the tab with a space. |
| 808 | O << ' '; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 813 | /// EmitZeros - Emit a block of zeros. |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 814 | /// |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 815 | void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 816 | if (NumZeros) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 817 | if (TAI->getZeroDirective()) { |
| 818 | O << TAI->getZeroDirective() << NumZeros; |
| 819 | if (TAI->getZeroDirectiveSuffix()) |
| 820 | O << TAI->getZeroDirectiveSuffix(); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 821 | O << '\n'; |
Jeff Cohen | c6a057b | 2006-05-02 03:46:13 +0000 | [diff] [blame] | 822 | } else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 823 | for (; NumZeros; --NumZeros) |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 824 | O << TAI->getData8bitsDirective(AddrSpace) << "0\n"; |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 829 | // Print out the specified constant, without a storage class. Only the |
| 830 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 831 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 832 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 833 | O << '0'; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 834 | else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Scott Michel | 4315eee | 2008-06-03 06:18:19 +0000 | [diff] [blame] | 835 | O << CI->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 836 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 837 | // This is a constant address for a global variable or function. Use the |
| 838 | // name of the variable or function as the address value, possibly |
| 839 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 840 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 841 | if (isa<Function>(GV)) { |
| 842 | O << TAI->getFunctionAddrPrefix() |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 843 | << Mang->getMangledName(GV) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 844 | << TAI->getFunctionAddrSuffix(); |
| 845 | } else { |
| 846 | O << TAI->getGlobalVarAddrPrefix() |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 847 | << Mang->getMangledName(GV) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 848 | << TAI->getGlobalVarAddrSuffix(); |
| 849 | } |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 850 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 851 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 13b7d3d | 2007-02-04 23:27:42 +0000 | [diff] [blame] | 852 | unsigned Opcode = CE->getOpcode(); |
| 853 | switch (Opcode) { |
Chris Lattner | c19ee61 | 2009-08-01 22:25:12 +0000 | [diff] [blame] | 854 | case Instruction::Trunc: |
| 855 | case Instruction::ZExt: |
| 856 | case Instruction::SExt: |
| 857 | case Instruction::FPTrunc: |
| 858 | case Instruction::FPExt: |
| 859 | case Instruction::UIToFP: |
| 860 | case Instruction::SIToFP: |
| 861 | case Instruction::FPToUI: |
| 862 | case Instruction::FPToSI: |
| 863 | llvm_unreachable("FIXME: Don't support this constant cast expr"); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 864 | case Instruction::GetElementPtr: { |
| 865 | // generate a symbolic expression for the byte address |
| 866 | const Constant *ptrVal = CE->getOperand(0); |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 867 | SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); |
| 868 | if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], |
| 869 | idxVec.size())) { |
Chris Lattner | 4798bbe | 2009-02-05 06:55:21 +0000 | [diff] [blame] | 870 | // Truncate/sext the offset to the pointer size. |
| 871 | if (TD->getPointerSizeInBits() != 64) { |
| 872 | int SExtAmount = 64-TD->getPointerSizeInBits(); |
| 873 | Offset = (Offset << SExtAmount) >> SExtAmount; |
| 874 | } |
| 875 | |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 876 | if (Offset) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 877 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 878 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 879 | if (Offset > 0) |
| 880 | O << ") + " << Offset; |
| 881 | else if (Offset < 0) |
| 882 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 883 | } else { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 884 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 885 | } |
| 886 | break; |
| 887 | } |
Chris Lattner | cb0a681 | 2006-12-12 05:14:13 +0000 | [diff] [blame] | 888 | case Instruction::BitCast: |
| 889 | return EmitConstantValueOnly(CE->getOperand(0)); |
| 890 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 891 | case Instruction::IntToPtr: { |
| 892 | // Handle casts to pointers by changing them into casts to the appropriate |
| 893 | // integer type. This promotes constant folding and simplifies this code. |
| 894 | Constant *Op = CE->getOperand(0); |
| 895 | Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); |
| 896 | return EmitConstantValueOnly(Op); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | case Instruction::PtrToInt: { |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 901 | // Support only foldable casts to/from pointers that can be eliminated by |
| 902 | // changing the pointer to the appropriately sized integer type. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 903 | Constant *Op = CE->getOperand(0); |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 904 | const Type *Ty = CE->getType(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 905 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 906 | // We can emit the pointer value into this slot if the slot is an |
| 907 | // integer slot greater or equal to the size of the pointer. |
Chris Lattner | c19ee61 | 2009-08-01 22:25:12 +0000 | [diff] [blame] | 908 | if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType())) |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 909 | return EmitConstantValueOnly(Op); |
Nick Lewycky | d622738 | 2008-08-08 06:34:07 +0000 | [diff] [blame] | 910 | |
| 911 | O << "(("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 912 | EmitConstantValueOnly(Op); |
Chris Lattner | c19ee61 | 2009-08-01 22:25:12 +0000 | [diff] [blame] | 913 | APInt ptrMask = |
| 914 | APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType())); |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 915 | |
| 916 | SmallString<40> S; |
| 917 | ptrMask.toStringUnsigned(S); |
| 918 | O << ") & " << S.c_str() << ')'; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 919 | break; |
| 920 | } |
| 921 | case Instruction::Add: |
Anton Korobeynikov | 13b7d3d | 2007-02-04 23:27:42 +0000 | [diff] [blame] | 922 | case Instruction::Sub: |
Anton Korobeynikov | feb8893 | 2007-12-18 20:53:41 +0000 | [diff] [blame] | 923 | case Instruction::And: |
| 924 | case Instruction::Or: |
| 925 | case Instruction::Xor: |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 926 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 927 | EmitConstantValueOnly(CE->getOperand(0)); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 928 | O << ')'; |
Anton Korobeynikov | feb8893 | 2007-12-18 20:53:41 +0000 | [diff] [blame] | 929 | switch (Opcode) { |
| 930 | case Instruction::Add: |
| 931 | O << " + "; |
| 932 | break; |
| 933 | case Instruction::Sub: |
| 934 | O << " - "; |
| 935 | break; |
| 936 | case Instruction::And: |
| 937 | O << " & "; |
| 938 | break; |
| 939 | case Instruction::Or: |
| 940 | O << " | "; |
| 941 | break; |
| 942 | case Instruction::Xor: |
| 943 | O << " ^ "; |
| 944 | break; |
| 945 | default: |
| 946 | break; |
| 947 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 948 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 949 | EmitConstantValueOnly(CE->getOperand(1)); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 950 | O << ')'; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 951 | break; |
| 952 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 953 | llvm_unreachable("Unsupported operator!"); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 954 | } |
| 955 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 956 | llvm_unreachable("Unknown constant value!"); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 959 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 960 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 961 | /// the predicate isString is true. |
| 962 | /// |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 963 | static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA, |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 964 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 965 | assert(CVA->isString() && "Array is not string compatible!"); |
| 966 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 967 | O << '\"'; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 968 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 969 | unsigned char C = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 970 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 971 | printStringChar(O, C); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 972 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 973 | O << '\"'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 976 | /// EmitString - Emit a zero-byte-terminated string constant. |
| 977 | /// |
| 978 | void AsmPrinter::EmitString(const ConstantArray *CVA) const { |
| 979 | unsigned NumElts = CVA->getNumOperands(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 980 | if (TAI->getAscizDirective() && NumElts && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 981 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 982 | O << TAI->getAscizDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 983 | printAsCString(O, CVA, NumElts-1); |
| 984 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 985 | O << TAI->getAsciiDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 986 | printAsCString(O, CVA, NumElts); |
| 987 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 988 | O << '\n'; |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Sanjiv Gupta | d3d9657 | 2009-04-28 16:34:20 +0000 | [diff] [blame] | 991 | void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA, |
| 992 | unsigned AddrSpace) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 993 | if (CVA->isString()) { |
| 994 | EmitString(CVA); |
| 995 | } else { // Not a string. Print the values in successive locations |
| 996 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Sanjiv Gupta | d3d9657 | 2009-04-28 16:34:20 +0000 | [diff] [blame] | 997 | EmitGlobalConstant(CVA->getOperand(i), AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) { |
| 1002 | const VectorType *PTy = CP->getType(); |
| 1003 | |
| 1004 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
| 1005 | EmitGlobalConstant(CP->getOperand(I)); |
| 1006 | } |
| 1007 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1008 | void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS, |
| 1009 | unsigned AddrSpace) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1010 | // Print the fields in successive locations. Pad to align if needed! |
| 1011 | const TargetData *TD = TM.getTargetData(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1012 | unsigned Size = TD->getTypeAllocSize(CVS->getType()); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1013 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
| 1014 | uint64_t sizeSoFar = 0; |
| 1015 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 1016 | const Constant* field = CVS->getOperand(i); |
| 1017 | |
| 1018 | // Check if padding is needed and insert one or more 0s. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1019 | uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1020 | uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) |
| 1021 | - cvsLayout->getElementOffset(i)) - fieldSize; |
| 1022 | sizeSoFar += fieldSize + padSize; |
| 1023 | |
| 1024 | // Now print the actual field value. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1025 | EmitGlobalConstant(field, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Insert padding - this may include padding to increase the size of the |
| 1028 | // current field up to the ABI size (if the struct is not packed) as well |
| 1029 | // as padding to ensure that the next field starts at the right offset. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1030 | EmitZeros(padSize, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1031 | } |
| 1032 | assert(sizeSoFar == cvsLayout->getSizeInBytes() && |
| 1033 | "Layout of constant struct may be incorrect!"); |
| 1034 | } |
| 1035 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1036 | void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP, |
| 1037 | unsigned AddrSpace) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1038 | // FP Constants are printed as integer constants to avoid losing |
| 1039 | // precision... |
| 1040 | const TargetData *TD = TM.getTargetData(); |
| 1041 | if (CFP->getType() == Type::DoubleTy) { |
| 1042 | double Val = CFP->getValueAPF().convertToDouble(); // for comment only |
| 1043 | uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1044 | if (TAI->getData64bitsDirective(AddrSpace)) { |
| 1045 | O << TAI->getData64bitsDirective(AddrSpace) << i; |
| 1046 | if (VerboseAsm) |
| 1047 | O << '\t' << TAI->getCommentString() << " double value: " << Val; |
| 1048 | O << '\n'; |
| 1049 | } else if (TD->isBigEndian()) { |
| 1050 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); |
| 1051 | if (VerboseAsm) |
| 1052 | O << '\t' << TAI->getCommentString() |
| 1053 | << " double most significant word " << Val; |
| 1054 | O << '\n'; |
| 1055 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); |
| 1056 | if (VerboseAsm) |
| 1057 | O << '\t' << TAI->getCommentString() |
| 1058 | << " double least significant word " << Val; |
| 1059 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1060 | } else { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1061 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); |
| 1062 | if (VerboseAsm) |
| 1063 | O << '\t' << TAI->getCommentString() |
| 1064 | << " double least significant word " << Val; |
| 1065 | O << '\n'; |
| 1066 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); |
| 1067 | if (VerboseAsm) |
| 1068 | O << '\t' << TAI->getCommentString() |
| 1069 | << " double most significant word " << Val; |
| 1070 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1071 | } |
| 1072 | return; |
| 1073 | } else if (CFP->getType() == Type::FloatTy) { |
| 1074 | float Val = CFP->getValueAPF().convertToFloat(); // for comment only |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1075 | O << TAI->getData32bitsDirective(AddrSpace) |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1076 | << CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1077 | if (VerboseAsm) |
| 1078 | O << '\t' << TAI->getCommentString() << " float " << Val; |
| 1079 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1080 | return; |
| 1081 | } else if (CFP->getType() == Type::X86_FP80Ty) { |
| 1082 | // all long double variants are printed as hex |
| 1083 | // api needed to prevent premature destruction |
| 1084 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
| 1085 | const uint64_t *p = api.getRawData(); |
| 1086 | // Convert to double so we can print the approximate val as a comment. |
| 1087 | APFloat DoubleVal = CFP->getValueAPF(); |
| 1088 | bool ignored; |
| 1089 | DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
| 1090 | &ignored); |
| 1091 | if (TD->isBigEndian()) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1092 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); |
| 1093 | if (VerboseAsm) |
| 1094 | O << '\t' << TAI->getCommentString() |
| 1095 | << " long double most significant halfword of ~" |
| 1096 | << DoubleVal.convertToDouble(); |
| 1097 | O << '\n'; |
| 1098 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); |
| 1099 | if (VerboseAsm) |
| 1100 | O << '\t' << TAI->getCommentString() << " long double next halfword"; |
| 1101 | O << '\n'; |
| 1102 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); |
| 1103 | if (VerboseAsm) |
| 1104 | O << '\t' << TAI->getCommentString() << " long double next halfword"; |
| 1105 | O << '\n'; |
| 1106 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); |
| 1107 | if (VerboseAsm) |
| 1108 | O << '\t' << TAI->getCommentString() << " long double next halfword"; |
| 1109 | O << '\n'; |
| 1110 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); |
| 1111 | if (VerboseAsm) |
| 1112 | O << '\t' << TAI->getCommentString() |
| 1113 | << " long double least significant halfword"; |
| 1114 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1115 | } else { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1116 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); |
| 1117 | if (VerboseAsm) |
| 1118 | O << '\t' << TAI->getCommentString() |
| 1119 | << " long double least significant halfword of ~" |
| 1120 | << DoubleVal.convertToDouble(); |
| 1121 | O << '\n'; |
| 1122 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); |
| 1123 | if (VerboseAsm) |
| 1124 | O << '\t' << TAI->getCommentString() |
| 1125 | << " long double next halfword"; |
| 1126 | O << '\n'; |
| 1127 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); |
| 1128 | if (VerboseAsm) |
| 1129 | O << '\t' << TAI->getCommentString() |
| 1130 | << " long double next halfword"; |
| 1131 | O << '\n'; |
| 1132 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); |
| 1133 | if (VerboseAsm) |
| 1134 | O << '\t' << TAI->getCommentString() |
| 1135 | << " long double next halfword"; |
| 1136 | O << '\n'; |
| 1137 | O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); |
| 1138 | if (VerboseAsm) |
| 1139 | O << '\t' << TAI->getCommentString() |
| 1140 | << " long double most significant halfword"; |
| 1141 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1142 | } |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1143 | EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) - |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1144 | TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1145 | return; |
| 1146 | } else if (CFP->getType() == Type::PPC_FP128Ty) { |
| 1147 | // all long double variants are printed as hex |
| 1148 | // api needed to prevent premature destruction |
| 1149 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
| 1150 | const uint64_t *p = api.getRawData(); |
| 1151 | if (TD->isBigEndian()) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1152 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); |
| 1153 | if (VerboseAsm) |
| 1154 | O << '\t' << TAI->getCommentString() |
| 1155 | << " long double most significant word"; |
| 1156 | O << '\n'; |
| 1157 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); |
| 1158 | if (VerboseAsm) |
| 1159 | O << '\t' << TAI->getCommentString() |
| 1160 | << " long double next word"; |
| 1161 | O << '\n'; |
| 1162 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); |
| 1163 | if (VerboseAsm) |
| 1164 | O << '\t' << TAI->getCommentString() |
| 1165 | << " long double next word"; |
| 1166 | O << '\n'; |
| 1167 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); |
| 1168 | if (VerboseAsm) |
| 1169 | O << '\t' << TAI->getCommentString() |
| 1170 | << " long double least significant word"; |
| 1171 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1172 | } else { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1173 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); |
| 1174 | if (VerboseAsm) |
| 1175 | O << '\t' << TAI->getCommentString() |
| 1176 | << " long double least significant word"; |
| 1177 | O << '\n'; |
| 1178 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); |
| 1179 | if (VerboseAsm) |
| 1180 | O << '\t' << TAI->getCommentString() |
| 1181 | << " long double next word"; |
| 1182 | O << '\n'; |
| 1183 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); |
| 1184 | if (VerboseAsm) |
| 1185 | O << '\t' << TAI->getCommentString() |
| 1186 | << " long double next word"; |
| 1187 | O << '\n'; |
| 1188 | O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); |
| 1189 | if (VerboseAsm) |
| 1190 | O << '\t' << TAI->getCommentString() |
| 1191 | << " long double most significant word"; |
| 1192 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1193 | } |
| 1194 | return; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1195 | } else llvm_unreachable("Floating point constant type not handled"); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1198 | void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI, |
| 1199 | unsigned AddrSpace) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1200 | const TargetData *TD = TM.getTargetData(); |
| 1201 | unsigned BitWidth = CI->getBitWidth(); |
| 1202 | assert(isPowerOf2_32(BitWidth) && |
| 1203 | "Non-power-of-2-sized integers not handled!"); |
| 1204 | |
| 1205 | // We don't expect assemblers to support integer data directives |
| 1206 | // for more than 64 bits, so we emit the data in at most 64-bit |
| 1207 | // quantities at a time. |
| 1208 | const uint64_t *RawData = CI->getValue().getRawData(); |
| 1209 | for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { |
| 1210 | uint64_t Val; |
| 1211 | if (TD->isBigEndian()) |
| 1212 | Val = RawData[e - i - 1]; |
| 1213 | else |
| 1214 | Val = RawData[i]; |
| 1215 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1216 | if (TAI->getData64bitsDirective(AddrSpace)) |
| 1217 | O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1218 | else if (TD->isBigEndian()) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1219 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); |
| 1220 | if (VerboseAsm) |
| 1221 | O << '\t' << TAI->getCommentString() |
| 1222 | << " Double-word most significant word " << Val; |
| 1223 | O << '\n'; |
| 1224 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); |
| 1225 | if (VerboseAsm) |
| 1226 | O << '\t' << TAI->getCommentString() |
| 1227 | << " Double-word least significant word " << Val; |
| 1228 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1229 | } else { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1230 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); |
| 1231 | if (VerboseAsm) |
| 1232 | O << '\t' << TAI->getCommentString() |
| 1233 | << " Double-word least significant word " << Val; |
| 1234 | O << '\n'; |
| 1235 | O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); |
| 1236 | if (VerboseAsm) |
| 1237 | O << '\t' << TAI->getCommentString() |
| 1238 | << " Double-word most significant word " << Val; |
| 1239 | O << '\n'; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 1244 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1245 | void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1246 | const TargetData *TD = TM.getTargetData(); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1247 | const Type *type = CV->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1248 | unsigned Size = TD->getTypeAllocSize(type); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 1250 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1251 | EmitZeros(Size, AddrSpace); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1252 | return; |
| 1253 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Sanjiv Gupta | d3d9657 | 2009-04-28 16:34:20 +0000 | [diff] [blame] | 1254 | EmitGlobalConstantArray(CVA , AddrSpace); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1255 | return; |
| 1256 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1257 | EmitGlobalConstantStruct(CVS, AddrSpace); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1260 | EmitGlobalConstantFP(CFP, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 1263 | // Small integers are handled below; large integers are handled here. |
| 1264 | if (Size > 4) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1265 | EmitGlobalConstantLargeInt(CI, AddrSpace); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1268 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1269 | EmitGlobalConstantVector(CP); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 1270 | return; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1273 | printDataDirective(type, AddrSpace); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 1274 | EmitConstantValueOnly(CV); |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1275 | if (VerboseAsm) { |
| 1276 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 1277 | SmallString<40> S; |
| 1278 | CI->getValue().toStringUnsigned(S, 16); |
| 1279 | O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str(); |
| 1280 | } |
Scott Michel | eefc845 | 2008-06-03 15:39:51 +0000 | [diff] [blame] | 1281 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1282 | O << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1283 | } |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 1285 | void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1286 | // Target doesn't support this yet! |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1287 | llvm_unreachable("Target does not support EmitMachineConstantPoolValue"); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1290 | /// PrintSpecial - Print information related to the specified machine instr |
| 1291 | /// that is independent of the operand, and may be independent of the instr |
| 1292 | /// itself. This can be useful for portably encoding the comment character |
| 1293 | /// or other bits of target-specific knowledge into the asmstrings. The |
| 1294 | /// syntax used is ${:comment}. Targets can override this to add support |
| 1295 | /// for their own strange codes. |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1296 | void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const { |
Chris Lattner | bae02cf | 2006-09-27 00:06:07 +0000 | [diff] [blame] | 1297 | if (!strcmp(Code, "private")) { |
| 1298 | O << TAI->getPrivateGlobalPrefix(); |
| 1299 | } else if (!strcmp(Code, "comment")) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1300 | if (VerboseAsm) |
| 1301 | O << TAI->getCommentString(); |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1302 | } else if (!strcmp(Code, "uid")) { |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1303 | // Comparing the address of MI isn't sufficient, because machineinstrs may |
| 1304 | // be allocated to the same address across functions. |
| 1305 | const Function *ThisF = MI->getParent()->getParent()->getFunction(); |
| 1306 | |
Owen Anderson | bd58edf | 2009-06-24 22:28:12 +0000 | [diff] [blame] | 1307 | // If this is a new LastFn instruction, bump the counter. |
| 1308 | if (LastMI != MI || LastFn != ThisF) { |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1309 | ++Counter; |
| 1310 | LastMI = MI; |
Owen Anderson | bd58edf | 2009-06-24 22:28:12 +0000 | [diff] [blame] | 1311 | LastFn = ThisF; |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1312 | } |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1313 | O << Counter; |
| 1314 | } else { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1315 | std::string msg; |
| 1316 | raw_string_ostream Msg(msg); |
| 1317 | Msg << "Unknown special formatter '" << Code |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1318 | << "' for machine instr: " << *MI; |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1319 | llvm_report_error(Msg.str()); |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 1323 | /// processDebugLoc - Processes the debug information of each machine |
| 1324 | /// instruction's DebugLoc. |
| 1325 | void AsmPrinter::processDebugLoc(DebugLoc DL) { |
| 1326 | if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) { |
| 1327 | if (!DL.isUnknown()) { |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 1328 | DebugLocTuple CurDLT = MF->getDebugLocTuple(DL); |
| 1329 | |
| 1330 | if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) |
| 1331 | printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col, |
| 1332 | DICompileUnit(CurDLT.CompileUnit))); |
| 1333 | |
| 1334 | PrevDLT = CurDLT; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1339 | /// printInlineAsm - This method formats and prints the specified machine |
| 1340 | /// instruction that is an inline asm. |
| 1341 | void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1342 | unsigned NumOperands = MI->getNumOperands(); |
| 1343 | |
| 1344 | // Count the number of register definitions. |
| 1345 | unsigned NumDefs = 0; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1346 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
Chris Lattner | 67942f5 | 2006-09-05 20:02:51 +0000 | [diff] [blame] | 1347 | ++NumDefs) |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1348 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 1349 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1350 | assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1353 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1354 | |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 1355 | // If this asmstr is empty, just print the #APP/#NOAPP markers. |
| 1356 | // These are useful to see where empty asm's wound up. |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1357 | if (AsmStr[0] == 0) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1358 | O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n'; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1359 | return; |
| 1360 | } |
| 1361 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1362 | O << TAI->getInlineAsmStart() << "\n\t"; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1363 | |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 1364 | // The variant of the current asmprinter. |
| 1365 | int AsmPrinterVariant = TAI->getAssemblerDialect(); |
| 1366 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1367 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 1368 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1370 | while (*LastEmitted) { |
| 1371 | switch (*LastEmitted) { |
| 1372 | default: { |
| 1373 | // Not a special case, emit the string section literally. |
| 1374 | const char *LiteralEnd = LastEmitted+1; |
| 1375 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1376 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1377 | ++LiteralEnd; |
| 1378 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1379 | O.write(LastEmitted, LiteralEnd-LastEmitted); |
| 1380 | LastEmitted = LiteralEnd; |
| 1381 | break; |
| 1382 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1383 | case '\n': |
| 1384 | ++LastEmitted; // Consume newline character. |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1385 | O << '\n'; // Indent code with newline. |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1386 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1387 | case '$': { |
| 1388 | ++LastEmitted; // Consume '$' character. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1389 | bool Done = true; |
| 1390 | |
| 1391 | // Handle escapes. |
| 1392 | switch (*LastEmitted) { |
| 1393 | default: Done = false; break; |
| 1394 | case '$': // $$ -> $ |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1395 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1396 | O << '$'; |
| 1397 | ++LastEmitted; // Consume second '$' character. |
| 1398 | break; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1399 | case '(': // $( -> same as GCC's { character. |
| 1400 | ++LastEmitted; // Consume '(' character. |
| 1401 | if (CurVariant != -1) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1402 | llvm_report_error("Nested variants found in inline asm string: '" |
| 1403 | + std::string(AsmStr) + "'"); |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1404 | } |
| 1405 | CurVariant = 0; // We're in the first variant now. |
| 1406 | break; |
| 1407 | case '|': |
| 1408 | ++LastEmitted; // consume '|' character. |
Dale Johannesen | 8b1e054 | 2008-10-10 21:04:42 +0000 | [diff] [blame] | 1409 | if (CurVariant == -1) |
| 1410 | O << '|'; // this is gcc's behavior for | outside a variant |
| 1411 | else |
| 1412 | ++CurVariant; // We're in the next variant. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | case ')': // $) -> same as GCC's } char. |
| 1415 | ++LastEmitted; // consume ')' character. |
Dale Johannesen | 8b1e054 | 2008-10-10 21:04:42 +0000 | [diff] [blame] | 1416 | if (CurVariant == -1) |
| 1417 | O << '}'; // this is gcc's behavior for } outside a variant |
| 1418 | else |
| 1419 | CurVariant = -1; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1420 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1421 | } |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1422 | if (Done) break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1423 | |
| 1424 | bool HasCurlyBraces = false; |
| 1425 | if (*LastEmitted == '{') { // ${variable} |
| 1426 | ++LastEmitted; // Consume '{' character. |
| 1427 | HasCurlyBraces = true; |
| 1428 | } |
| 1429 | |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1430 | // If we have ${:foo}, then this is not a real operand reference, it is a |
| 1431 | // "magic" string reference, just like in .td files. Arrange to call |
| 1432 | // PrintSpecial. |
| 1433 | if (HasCurlyBraces && *LastEmitted == ':') { |
| 1434 | ++LastEmitted; |
| 1435 | const char *StrStart = LastEmitted; |
| 1436 | const char *StrEnd = strchr(StrStart, '}'); |
| 1437 | if (StrEnd == 0) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1438 | llvm_report_error("Unterminated ${:foo} operand in inline asm string: '" |
| 1439 | + std::string(AsmStr) + "'"); |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | std::string Val(StrStart, StrEnd); |
| 1443 | PrintSpecial(MI, Val.c_str()); |
| 1444 | LastEmitted = StrEnd+1; |
| 1445 | break; |
| 1446 | } |
| 1447 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1448 | const char *IDStart = LastEmitted; |
| 1449 | char *IDEnd; |
Chris Lattner | fad2912 | 2007-01-23 00:36:17 +0000 | [diff] [blame] | 1450 | errno = 0; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1451 | long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. |
| 1452 | if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1453 | llvm_report_error("Bad $ operand number in inline asm string: '" |
| 1454 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1455 | } |
| 1456 | LastEmitted = IDEnd; |
| 1457 | |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1458 | char Modifier[2] = { 0, 0 }; |
| 1459 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1460 | if (HasCurlyBraces) { |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1461 | // If we have curly braces, check for a modifier character. This |
| 1462 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 1463 | if (*LastEmitted == ':') { |
| 1464 | ++LastEmitted; // Consume ':' character. |
| 1465 | if (*LastEmitted == 0) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1466 | llvm_report_error("Bad ${:} expression in inline asm string: '" |
| 1467 | + std::string(AsmStr) + "'"); |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | Modifier[0] = *LastEmitted; |
| 1471 | ++LastEmitted; // Consume modifier character. |
| 1472 | } |
| 1473 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1474 | if (*LastEmitted != '}') { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1475 | llvm_report_error("Bad ${} expression in inline asm string: '" |
| 1476 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1477 | } |
| 1478 | ++LastEmitted; // Consume '}' character. |
| 1479 | } |
| 1480 | |
| 1481 | if ((unsigned)Val >= NumOperands-1) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1482 | llvm_report_error("Invalid $ operand number in inline asm string: '" |
| 1483 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1486 | // Okay, we finally have a value number. Ask the target to print this |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1487 | // operand! |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1488 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 1489 | unsigned OpNo = 1; |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1490 | |
| 1491 | bool Error = false; |
| 1492 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1493 | // Scan to find the machine operand number for the operand. |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1494 | for (; Val; --Val) { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1495 | if (OpNo >= MI->getNumOperands()) break; |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1496 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 1497 | OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1498 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1500 | if (OpNo >= MI->getNumOperands()) { |
| 1501 | Error = true; |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1502 | } else { |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1503 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1504 | ++OpNo; // Skip over the ID number. |
| 1505 | |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1506 | if (Modifier[0]=='l') // labels are target independent |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1507 | printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1508 | false, false, false); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1509 | else { |
| 1510 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 1511 | if ((OpFlags & 7) == 4) { |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1512 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 1513 | Modifier[0] ? Modifier : 0); |
| 1514 | } else { |
| 1515 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 1516 | Modifier[0] ? Modifier : 0); |
| 1517 | } |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1518 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1519 | } |
| 1520 | if (Error) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1521 | std::string msg; |
| 1522 | raw_string_ostream Msg(msg); |
| 1523 | Msg << "Invalid operand found in inline asm: '" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1524 | << AsmStr << "'\n"; |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1525 | MI->print(Msg); |
| 1526 | llvm_report_error(Msg.str()); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1527 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1528 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1529 | break; |
| 1530 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1533 | O << "\n\t" << TAI->getInlineAsmEnd() << '\n'; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1536 | /// printImplicitDef - This method prints the specified machine instruction |
| 1537 | /// that is an implicit def. |
| 1538 | void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1539 | if (VerboseAsm) |
| 1540 | O << '\t' << TAI->getCommentString() << " implicit-def: " |
| 1541 | << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1544 | /// printLabel - This method prints a local label used by debug and |
| 1545 | /// exception handling tables. |
| 1546 | void AsmPrinter::printLabel(const MachineInstr *MI) const { |
Dan Gohman | 528bc02 | 2008-07-01 00:16:26 +0000 | [diff] [blame] | 1547 | printLabel(MI->getOperand(0).getImm()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1550 | void AsmPrinter::printLabel(unsigned Id) const { |
Evan Cheng | 4eecdeb | 2008-02-02 08:39:46 +0000 | [diff] [blame] | 1551 | O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1554 | /// printDeclare - This method prints a local variable declaration used by |
| 1555 | /// debug tables. |
Evan Cheng | 4e3f5a4 | 2008-02-04 23:06:48 +0000 | [diff] [blame] | 1556 | /// FIXME: It doesn't really print anything rather it inserts a DebugVariable |
| 1557 | /// entry into dwarf table. |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1558 | void AsmPrinter::printDeclare(const MachineInstr *MI) const { |
Devang Patel | c48c550 | 2009-01-13 21:44:10 +0000 | [diff] [blame] | 1559 | unsigned FI = MI->getOperand(0).getIndex(); |
Evan Cheng | 4e3f5a4 | 2008-02-04 23:06:48 +0000 | [diff] [blame] | 1560 | GlobalValue *GV = MI->getOperand(1).getGlobal(); |
Devang Patel | 1be3ecc | 2009-04-15 00:10:26 +0000 | [diff] [blame] | 1561 | DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI); |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1564 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 1565 | /// instruction, using the specified assembler variant. Targets should |
| 1566 | /// overried this to format as appropriate. |
| 1567 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1568 | unsigned AsmVariant, const char *ExtraCode) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1569 | // Target doesn't support this yet! |
| 1570 | return true; |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1571 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1572 | |
| 1573 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 1574 | unsigned AsmVariant, |
| 1575 | const char *ExtraCode) { |
| 1576 | // Target doesn't support this yet! |
| 1577 | return true; |
| 1578 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1579 | |
| 1580 | /// printBasicBlockLabel - This method prints the label for the specified |
| 1581 | /// MachineBasicBlock |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1582 | void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1583 | bool printAlign, |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1584 | bool printColon, |
| 1585 | bool printComment) const { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1586 | if (printAlign) { |
| 1587 | unsigned Align = MBB->getAlignment(); |
| 1588 | if (Align) |
| 1589 | EmitAlignment(Log2_32(Align)); |
| 1590 | } |
| 1591 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1592 | O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1593 | << MBB->getNumber(); |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1594 | if (printColon) |
| 1595 | O << ':'; |
Chris Lattner | 9c78ecb | 2006-10-05 21:40:14 +0000 | [diff] [blame] | 1596 | if (printComment && MBB->getBasicBlock()) |
Dan Gohman | 286d569 | 2007-07-30 15:06:25 +0000 | [diff] [blame] | 1597 | O << '\t' << TAI->getCommentString() << ' ' |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 1598 | << MBB->getBasicBlock()->getNameStr(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1599 | } |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1600 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1601 | /// printPICJumpTableSetLabel - This method prints a set label for the |
| 1602 | /// specified MachineBasicBlock for a jumptable entry. |
| 1603 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
| 1604 | const MachineBasicBlock *MBB) const { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1605 | if (!TAI->getSetDirective()) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1606 | return; |
| 1607 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1608 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1609 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1610 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1611 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 1612 | << '_' << uid << '\n'; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1613 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1614 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1615 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, |
| 1616 | const MachineBasicBlock *MBB) const { |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1617 | if (!TAI->getSetDirective()) |
| 1618 | return; |
| 1619 | |
| 1620 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1621 | << getFunctionNumber() << '_' << uid << '_' << uid2 |
| 1622 | << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1623 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1624 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 1625 | << '_' << uid << '_' << uid2 << '\n'; |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1628 | /// printDataDirective - This method prints the asm directive for the |
| 1629 | /// specified type. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1630 | void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1631 | const TargetData *TD = TM.getTargetData(); |
| 1632 | switch (type->getTypeID()) { |
Chris Lattner | f1cfea2 | 2009-07-15 04:42:49 +0000 | [diff] [blame] | 1633 | case Type::FloatTyID: case Type::DoubleTyID: |
| 1634 | case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: |
| 1635 | assert(0 && "Should have already output floating point constant."); |
| 1636 | default: |
| 1637 | assert(0 && "Can't handle printing this type of thing"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1638 | case Type::IntegerTyID: { |
| 1639 | unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); |
| 1640 | if (BitWidth <= 8) |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1641 | O << TAI->getData8bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1642 | else if (BitWidth <= 16) |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1643 | O << TAI->getData16bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1644 | else if (BitWidth <= 32) |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1645 | O << TAI->getData32bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1646 | else if (BitWidth <= 64) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1647 | assert(TAI->getData64bitsDirective(AddrSpace) && |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1648 | "Target cannot handle 64-bit constant exprs!"); |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1649 | O << TAI->getData64bitsDirective(AddrSpace); |
Dan Gohman | 82f94f1 | 2008-09-08 16:40:13 +0000 | [diff] [blame] | 1650 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1651 | llvm_unreachable("Target cannot handle given data directive width!"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1652 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1653 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1654 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1655 | case Type::PointerTyID: |
| 1656 | if (TD->getPointerSize() == 8) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1657 | assert(TAI->getData64bitsDirective(AddrSpace) && |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1658 | "Target cannot handle 64-bit pointer exprs!"); |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1659 | O << TAI->getData64bitsDirective(AddrSpace); |
Sanjiv Gupta | fcc6f15 | 2009-01-22 10:14:21 +0000 | [diff] [blame] | 1660 | } else if (TD->getPointerSize() == 2) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1661 | O << TAI->getData16bitsDirective(AddrSpace); |
Sanjiv Gupta | fcc6f15 | 2009-01-22 10:14:21 +0000 | [diff] [blame] | 1662 | } else if (TD->getPointerSize() == 1) { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1663 | O << TAI->getData8bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1664 | } else { |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1665 | O << TAI->getData32bitsDirective(AddrSpace); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1666 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1667 | break; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |
Dale Johannesen | 4c3a5f8 | 2007-02-16 01:54:53 +0000 | [diff] [blame] | 1670 | |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 1671 | void AsmPrinter::printVisibility(const std::string& Name, |
| 1672 | unsigned Visibility) const { |
| 1673 | if (Visibility == GlobalValue::HiddenVisibility) { |
| 1674 | if (const char *Directive = TAI->getHiddenDirective()) |
| 1675 | O << Directive << Name << '\n'; |
| 1676 | } else if (Visibility == GlobalValue::ProtectedVisibility) { |
| 1677 | if (const char *Directive = TAI->getProtectedDirective()) |
| 1678 | O << Directive << Name << '\n'; |
| 1679 | } |
| 1680 | } |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1681 | |
Anton Korobeynikov | 7751ad9 | 2008-11-22 16:15:34 +0000 | [diff] [blame] | 1682 | void AsmPrinter::printOffset(int64_t Offset) const { |
| 1683 | if (Offset > 0) |
| 1684 | O << '+' << Offset; |
| 1685 | else if (Offset < 0) |
| 1686 | O << Offset; |
| 1687 | } |
| 1688 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1689 | GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { |
| 1690 | if (!S->usesMetadata()) |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1691 | return 0; |
| 1692 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1693 | gcp_iterator GCPI = GCMetadataPrinters.find(S); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1694 | if (GCPI != GCMetadataPrinters.end()) |
| 1695 | return GCPI->second; |
| 1696 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1697 | const char *Name = S->getName().c_str(); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1698 | |
| 1699 | for (GCMetadataPrinterRegistry::iterator |
| 1700 | I = GCMetadataPrinterRegistry::begin(), |
| 1701 | E = GCMetadataPrinterRegistry::end(); I != E; ++I) |
| 1702 | if (strcmp(Name, I->getName()) == 0) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1703 | GCMetadataPrinter *GMP = I->instantiate(); |
| 1704 | GMP->S = S; |
| 1705 | GCMetadataPrinters.insert(std::make_pair(S, GMP)); |
| 1706 | return GMP; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1709 | cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1710 | llvm_unreachable(0); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1711 | } |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 1712 | |
| 1713 | /// EmitComments - Pretty-print comments for instructions |
| 1714 | void AsmPrinter::EmitComments(const MachineInstr &MI) const |
| 1715 | { |
David Greene | 67e5983 | 2009-07-22 20:33:26 +0000 | [diff] [blame] | 1716 | if (VerboseAsm) { |
| 1717 | if (!MI.getDebugLoc().isUnknown()) { |
| 1718 | DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1719 | |
David Greene | 67e5983 | 2009-07-22 20:33:26 +0000 | [diff] [blame] | 1720 | // Print source line info |
| 1721 | O.PadToColumn(TAI->getCommentColumn(), 1); |
| 1722 | O << TAI->getCommentString() << " SrcLine "; |
| 1723 | if (DLT.CompileUnit->hasInitializer()) { |
| 1724 | Constant *Name = DLT.CompileUnit->getInitializer(); |
| 1725 | if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) |
| 1726 | if (NameString->isString()) { |
| 1727 | O << NameString->getAsString() << " "; |
| 1728 | } |
| 1729 | } |
| 1730 | O << DLT.Line; |
| 1731 | if (DLT.Col != 0) |
| 1732 | O << ":" << DLT.Col; |
| 1733 | } |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1734 | } |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | /// EmitComments - Pretty-print comments for instructions |
| 1738 | void AsmPrinter::EmitComments(const MCInst &MI) const |
| 1739 | { |
David Greene | 67e5983 | 2009-07-22 20:33:26 +0000 | [diff] [blame] | 1740 | if (VerboseAsm) { |
| 1741 | if (!MI.getDebugLoc().isUnknown()) { |
| 1742 | DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1743 | |
David Greene | 67e5983 | 2009-07-22 20:33:26 +0000 | [diff] [blame] | 1744 | // Print source line info |
| 1745 | O.PadToColumn(TAI->getCommentColumn(), 1); |
| 1746 | O << TAI->getCommentString() << " SrcLine "; |
| 1747 | if (DLT.CompileUnit->hasInitializer()) { |
| 1748 | Constant *Name = DLT.CompileUnit->getInitializer(); |
| 1749 | if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) |
| 1750 | if (NameString->isString()) { |
| 1751 | O << NameString->getAsString() << " "; |
| 1752 | } |
| 1753 | } |
| 1754 | O << DLT.Line; |
| 1755 | if (DLT.Col != 0) |
| 1756 | O << ":" << DLT.Col; |
| 1757 | } |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1758 | } |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 1759 | } |