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