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