blob: 742997aaaa55b5d6406b3a1bf24e782793bd8eda [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000023#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000025#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000026#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000029#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000030#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000032#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000034#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000035#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000037#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000038#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000040#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000041#include "llvm/ADT/StringExtras.h"
Chris Lattner66099132006-02-01 22:41:11 +000042#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000043using namespace llvm;
44
Evan Cheng42bf74b2009-03-25 01:47:28 +000045static cl::opt<cl::boolOrDefault>
46AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
47 cl::init(cl::BOU_UNSET));
48
Devang Patel19974732007-05-03 01:11:54 +000049char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000050AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000051 const TargetAsmInfo *T, bool VDef)
52 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Chengda47e6e2008-03-15 00:03:38 +000053 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000054
55 OutContext(*new MCContext()),
56 OutStreamer(*createAsmStreamer(OutContext, O)),
57
Chris Lattner290c2f52009-08-03 23:20:21 +000058 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersona8dbf362009-06-25 16:55:32 +000059 PrevDLT(0, ~0U, ~0U) {
Chris Lattner290c2f52009-08-03 23:20:21 +000060 CurrentSection = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +000061 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000062 switch (AsmVerbose) {
63 case cl::BOU_UNSET: VerboseAsm = VDef; break;
64 case cl::BOU_TRUE: VerboseAsm = true; break;
65 case cl::BOU_FALSE: VerboseAsm = false; break;
66 }
67}
Chris Lattnered138932005-12-13 06:32:10 +000068
Gordon Henriksenc317a602008-08-17 12:08:44 +000069AsmPrinter::~AsmPrinter() {
70 for (gcp_iterator I = GCMetadataPrinters.begin(),
71 E = GCMetadataPrinters.end(); I != E; ++I)
72 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000073
74 delete &OutStreamer;
75 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000076}
Chris Lattnered138932005-12-13 06:32:10 +000077
Chris Lattner38c39882009-08-03 19:12:26 +000078TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000079 return TM.getTargetLowering()->getObjFileLowering();
80}
81
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000082/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattnerd54a6b82009-08-03 18:04:42 +000083/// are not already in it! If "NS" is null, then this causes us to exit the
84/// current section and not reenter another one. This is generally used for
85/// asmprinter hacks.
86///
87/// FIXME: Remove support for null sections.
88///
Chris Lattnera87dea42009-07-31 18:48:30 +000089void AsmPrinter::SwitchToSection(const MCSection *NS) {
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000090 // If we're already in this section, we're done.
Chris Lattner290c2f52009-08-03 23:20:21 +000091 if (CurrentSection == NS) return;
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000092
93 // Close the current section, if applicable.
Chris Lattner290c2f52009-08-03 23:20:21 +000094 if (NS != 0 && TAI->getSectionEndDirectiveSuffix())
95 O << NS->getName() << TAI->getSectionEndDirectiveSuffix() << '\n';
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000096
Chris Lattner290c2f52009-08-03 23:20:21 +000097 CurrentSection = NS;
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000098
Chris Lattner290c2f52009-08-03 23:20:21 +000099 if (NS != 0) {
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000100 // If section is named we need to switch into it via special '.section'
101 // directive and also append funky flags. Otherwise - section name is just
102 // some magic assembler directive.
Chris Lattner968ff112009-08-01 21:11:14 +0000103 if (!NS->isDirective()) {
Chris Lattnerf40761d2009-07-26 07:33:58 +0000104 SmallString<32> FlagsStr;
Chris Lattnerf0144122009-07-28 03:13:23 +0000105
106 getObjFileLowering().getSectionFlagsAsString(NS->getKind(), FlagsStr);
Chris Lattner5fe575f2009-07-27 05:32:16 +0000107
108 O << TAI->getSwitchToSectionDirective()
Chris Lattner290c2f52009-08-03 23:20:21 +0000109 << CurrentSection->getName() << FlagsStr.c_str();
Chris Lattnerf40761d2009-07-26 07:33:58 +0000110 } else {
Chris Lattner290c2f52009-08-03 23:20:21 +0000111 O << CurrentSection->getName();
Chris Lattnerf40761d2009-07-26 07:33:58 +0000112 }
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000113 O << TAI->getDataSectionStartSuffix() << '\n';
114 }
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +0000115}
Chris Lattner4632d7a2006-05-09 04:59:56 +0000116
Gordon Henriksence224772008-01-07 01:30:38 +0000117void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000118 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +0000119 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000120 AU.addRequired<GCModuleInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +0000121}
122
Chris Lattnera80ba712004-08-16 23:15:22 +0000123bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000124 // Initialize TargetLoweringObjectFile.
125 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
126 .Initialize(OutContext, TM);
127
Bill Wendling4cef5842009-07-20 21:30:28 +0000128 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner90f8b702009-07-21 17:30:51 +0000129 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000130
Chris Lattnera93ca922009-06-18 23:41:35 +0000131 if (TAI->doesAllowQuotesInName())
132 Mang->setUseQuotes(true);
133
Duncan Sands1465d612009-01-28 13:14:17 +0000134 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000135 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola952b8392008-12-03 11:01:37 +0000136
137 if (TAI->hasSingleParameterDotFile()) {
138 /* Very minimal debug info. It is ignored if we emit actual
139 debug info. If we don't, this at helps the user find where
140 a function came from. */
141 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
142 }
143
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000144 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
145 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
146 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000147
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000148 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +0000149 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000150 << M.getModuleInlineAsm()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000151 << '\n' << TAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000152 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000153
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000154 SwitchToSection(0); // Reset back to no section to close off sections.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000155
Chris Lattner0de1fc42009-06-24 19:09:55 +0000156 if (TAI->doesSupportDebugInformation() ||
157 TAI->doesSupportExceptionHandling()) {
158 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
159 if (MMI)
Devang Patel14a55d92009-06-19 21:54:26 +0000160 MMI->AnalyzeModule(M);
Chris Lattner0de1fc42009-06-24 19:09:55 +0000161 DW = getAnalysisIfAvailable<DwarfWriter>();
162 if (DW)
163 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000164 }
165
Chris Lattnera80ba712004-08-16 23:15:22 +0000166 return false;
167}
168
169bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000170 // Emit global variables.
171 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
172 I != E; ++I)
173 PrintGlobalVariable(I);
174
Chris Lattner1f522fe2009-06-24 18:54:37 +0000175 // Emit final debug information.
176 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
177 DW->EndModule();
178
Chris Lattner0a7befa2009-06-24 18:52:01 +0000179 // If the target wants to know about weak references, print them all.
Rafael Espindola15404d02006-12-18 03:37:18 +0000180 if (TAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000181 // FIXME: This is not lazy, it would be nice to only print weak references
182 // to stuff that is actually used. Note that doing so would require targets
183 // to notice uses in operands (due to constant exprs etc). This should
184 // happen with the MC stuff eventually.
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000185 SwitchToSection(0);
Rafael Espindola15404d02006-12-18 03:37:18 +0000186
Chris Lattner0a7befa2009-06-24 18:52:01 +0000187 // Print out module-level global variables here.
188 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
189 I != E; ++I) {
190 if (I->hasExternalWeakLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000191 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000192 }
193
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000194 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000195 if (I->hasExternalWeakLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000196 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000197 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000198 }
199
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000200 if (TAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000201 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000202 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000203 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000204 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000205
206 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000207 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000208
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000209 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000210 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000211 else if (I->hasWeakLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000212 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000213 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000214 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000215
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000216 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000217
Dan Gohmand19a53b2008-06-30 22:03:41 +0000218 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000219 }
220 }
221
Duncan Sands1465d612009-01-28 13:14:17 +0000222 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000223 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
224 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
225 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
226 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000227
Dan Gohmana779a982008-05-05 00:28:39 +0000228 // If we don't have any trampolines, then we don't require stack memory
229 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000230 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000231 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
232 if (TAI->getNonexecutableStackDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000233 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000234
Chris Lattnera80ba712004-08-16 23:15:22 +0000235 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000236 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000237
238 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000239 return false;
240}
241
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000242std::string
243AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling6e198962007-09-18 01:47:22 +0000244 assert(MF && "No machine function?");
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000245 return Mang->getMangledName(MF->getFunction(), ".eh",
246 TAI->is_EHSymbolPrivate());
Bill Wendling6e198962007-09-18 01:47:22 +0000247}
248
Chris Lattner25045bd2005-11-21 07:51:36 +0000249void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000250 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000251 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000252 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000253}
254
Evan Cheng1606e8e2009-03-13 07:51:59 +0000255namespace {
256 // SectionCPs - Keep track the alignment, constpool entries per Section.
257 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000258 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000259 unsigned Alignment;
260 SmallVector<unsigned, 4> CPEs;
Chris Lattnera87dea42009-07-31 18:48:30 +0000261 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1606e8e2009-03-13 07:51:59 +0000262 };
263}
264
Chris Lattner3b4fd322005-11-21 08:25:09 +0000265/// EmitConstantPool - Print to the current output stream assembly
266/// representations of the constants in the constant pool MCP. This is
267/// used to print out constants which have been "spilled to memory" by
268/// the code generator.
269///
270void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000271 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000272 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000273
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000274 // Calculate sections for constant pool entries. We collect entries to go into
275 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000276 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000277 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000278 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000279 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000280
281 SectionKind Kind;
282 switch (CPE.getRelocationInfo()) {
283 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000284 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000285 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000286 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000287 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000288 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000289 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000290 case 4: Kind = SectionKind::getMergeableConst4(); break;
291 case 8: Kind = SectionKind::getMergeableConst8(); break;
292 case 16: Kind = SectionKind::getMergeableConst16();break;
293 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000294 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000295 }
296
Chris Lattner83d77fa2009-08-01 23:46:12 +0000297 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000298
Evan Cheng1606e8e2009-03-13 07:51:59 +0000299 // The number of sections are small, just do a linear search from the
300 // last section to the first.
301 bool Found = false;
302 unsigned SecIdx = CPSections.size();
303 while (SecIdx != 0) {
304 if (CPSections[--SecIdx].S == S) {
305 Found = true;
306 break;
307 }
308 }
309 if (!Found) {
310 SecIdx = CPSections.size();
311 CPSections.push_back(SectionCPs(S, Align));
312 }
313
314 if (Align > CPSections[SecIdx].Alignment)
315 CPSections[SecIdx].Alignment = Align;
316 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000317 }
318
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000319 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000320 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
321 SwitchToSection(CPSections[i].S);
322 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000323
Evan Cheng1606e8e2009-03-13 07:51:59 +0000324 unsigned Offset = 0;
325 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
326 unsigned CPI = CPSections[i].CPEs[j];
327 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000328
Chris Lattner3029f922006-02-09 04:46:04 +0000329 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000330 unsigned AlignMask = CPE.getAlignment() - 1;
331 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
332 EmitZeros(NewOffset - Offset);
333
334 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000335 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000336
337 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
338 << CPI << ":\t\t\t\t\t";
339 if (VerboseAsm) {
340 O << TAI->getCommentString() << ' ';
341 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000342 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000343 O << '\n';
344 if (CPE.isMachineConstantPoolEntry())
345 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
346 else
347 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000348 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000349 }
350}
351
Nate Begeman37efe672006-04-22 18:53:45 +0000352/// EmitJumpTableInfo - Print assembly representations of the jump tables used
353/// by the current function to the current output stream.
354///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000355void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
356 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000357 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
358 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000359
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000360 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000361
Nate Begeman2f1ae882006-07-27 01:13:04 +0000362 // Pick the directive to use to print the jump table entries, and switch to
363 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000364 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000365
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000366 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000367 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000368 if (F->isWeakForLinker() ||
369 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000370 // In PIC mode, we need to emit the jump table to the same section as the
371 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000372 // We should also do if the section name is NULL or function is declared in
373 // discardable section.
Chris Lattner83d77fa2009-08-01 23:46:12 +0000374 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000375 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000376 // Otherwise, drop it in the readonly section.
377 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000378 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner83d77fa2009-08-01 23:46:12 +0000379 SwitchToSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000380 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000381 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000382
383 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000384
Nate Begeman37efe672006-04-22 18:53:45 +0000385 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000386 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000387
388 // If this jump table was deleted, ignore it.
389 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000390
391 // For PIC codegen, if possible we want to use the SetDirective to reduce
392 // the number of relocations the assembler will generate for the jump table.
393 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000394 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000395 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000396 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000397 if (EmittedSets.insert(JTBBs[ii]))
398 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000399
Chris Lattner393a8ee2007-01-18 01:12:56 +0000400 // On some targets (e.g. darwin) we want to emit two consequtive labels
401 // before each jump table. The first label is never referenced, but tells
402 // the assembler and linker the extents of the jump table object. The
403 // second label is actually referenced by the code.
Evan Chengb13bafe2009-06-18 20:37:15 +0000404 if (JTInDiffSection) {
405 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
406 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
407 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000408
Evan Cheng347d39f2007-10-14 05:57:21 +0000409 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
410 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000411
Nate Begeman37efe672006-04-22 18:53:45 +0000412 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000413 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000414 O << '\n';
415 }
416 }
417}
418
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000419void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
420 const MachineBasicBlock *MBB,
421 unsigned uid) const {
422 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
423
424 // Use JumpTableDirective otherwise honor the entry size from the jump table
425 // info.
426 const char *JTEntryDirective = TAI->getJumpTableDirective();
427 bool HadJTEntryDirective = JTEntryDirective != NULL;
428 if (!HadJTEntryDirective) {
429 JTEntryDirective = MJTI->getEntrySize() == 4 ?
430 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
431 }
432
433 O << JTEntryDirective << ' ';
434
435 // If we have emitted set directives for the jump table entries, print
436 // them rather than the entries themselves. If we're emitting PIC, then
437 // emit the table entries as differences between two text section labels.
438 // If we're emitting non-PIC code, then emit the entries as direct
439 // references to the target basic blocks.
440 if (IsPic) {
441 if (TAI->getSetDirective()) {
442 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
443 << '_' << uid << "_set_" << MBB->getNumber();
444 } else {
Evan Chengfb8075d2008-02-28 00:43:03 +0000445 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000446 // If the arch uses custom Jump Table directives, don't calc relative to
447 // JT
448 if (!HadJTEntryDirective)
449 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
450 << getFunctionNumber() << '_' << uid;
451 }
452 } else {
Evan Chengfb8075d2008-02-28 00:43:03 +0000453 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000454 }
455}
456
457
Chris Lattnered138932005-12-13 06:32:10 +0000458/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
459/// special global used by LLVM. If so, emit it and return true, otherwise
460/// do nothing and return false.
461bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000462 if (GV->getName() == "llvm.used") {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000463 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
464 EmitLLVMUsedList(GV->getInitializer());
465 return true;
466 }
467
Chris Lattner401e10c2009-07-20 06:14:25 +0000468 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000469 if (GV->getSection() == "llvm.metadata" ||
470 GV->hasAvailableExternallyLinkage())
471 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000472
473 if (!GV->hasAppendingLinkage()) return false;
474
475 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000476
Evan Cheng916d07c2007-06-04 20:39:18 +0000477 const TargetData *TD = TM.getTargetData();
478 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000479 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner80ec2792009-08-02 00:34:36 +0000480 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000481 EmitAlignment(Align, 0);
482 EmitXXStructorList(GV->getInitializer());
483 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000484 }
485
Chris Lattnerf231c072009-03-09 05:52:15 +0000486 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner80ec2792009-08-02 00:34:36 +0000487 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000488 EmitAlignment(Align, 0);
489 EmitXXStructorList(GV->getInitializer());
490 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000491 }
492
493 return false;
494}
495
Chris Lattnercb05af82006-09-26 03:38:18 +0000496/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000497/// global in the specified llvm.used list for which emitUsedDirectiveFor
498/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000499void AsmPrinter::EmitLLVMUsedList(Constant *List) {
500 const char *Directive = TAI->getUsedDirective();
501
Dan Gohmana119de82009-06-14 23:30:43 +0000502 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000503 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
504 if (InitList == 0) return;
505
506 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000507 const GlobalValue *GV =
508 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000509 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000510 O << Directive;
511 EmitConstantValueOnly(InitList->getOperand(i));
512 O << '\n';
513 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000514 }
515}
516
Chris Lattnered138932005-12-13 06:32:10 +0000517/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
518/// function pointers, ignoring the init priority.
519void AsmPrinter::EmitXXStructorList(Constant *List) {
520 // Should be an array of '{ int, void ()* }' structs. The first value is the
521 // init priority, which we ignore.
522 if (!isa<ConstantArray>(List)) return;
523 ConstantArray *InitList = cast<ConstantArray>(List);
524 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
525 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
526 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000527
528 if (CS->getOperand(1)->isNullValue())
529 return; // Found a null terminator, exit printing.
530 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000531 EmitGlobalConstant(CS->getOperand(1));
532 }
533}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000534
Jim Laskeya1a19f82006-10-17 13:41:07 +0000535/// getGlobalLinkName - Returns the asm/link name of of the specified
536/// global variable. Should be overridden by each target asm printer to
537/// generate the appropriate value.
Bill Wendling7d16e852009-04-10 00:12:49 +0000538const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
539 std::string &LinkName) const {
Jim Laskey03742482006-11-20 20:29:06 +0000540 if (isa<Function>(GV)) {
541 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000542 LinkName += Mang->getMangledName(GV);
Jim Laskey03742482006-11-20 20:29:06 +0000543 LinkName += TAI->getFunctionAddrSuffix();
544 } else {
545 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000546 LinkName += Mang->getMangledName(GV);
Jim Laskey03742482006-11-20 20:29:06 +0000547 LinkName += TAI->getGlobalVarAddrSuffix();
548 }
549
Jim Laskey99e41ee2006-10-17 17:17:24 +0000550 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000551}
552
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000553/// EmitExternalGlobal - Emit the external reference to a global variable.
554/// Should be overridden if an indirect reference should be used.
555void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000556 std::string GLN;
557 O << getGlobalLinkName(GV, GLN);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000558}
559
560
561
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000562//===----------------------------------------------------------------------===//
563/// LEB 128 number encoding.
564
565/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
566/// representing an unsigned leb128 value.
567void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000568 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000569 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000570 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000571 Value >>= 7;
572 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000573 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000574 if (Value) O << ", ";
575 } while (Value);
576}
577
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000578/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
579/// representing a signed leb128 value.
580void AsmPrinter::PrintSLEB128(int Value) const {
581 int Sign = Value >> (8 * sizeof(Value) - 1);
582 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000583 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000584
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000585 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000586 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000587 Value >>= 7;
588 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
589 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000590 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000591 if (IsMore) O << ", ";
592 } while (IsMore);
593}
594
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000595//===--------------------------------------------------------------------===//
596// Emission and print routines
597//
598
599/// PrintHex - Print a value as a hexidecimal value.
600///
601void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000602 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000603 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000604}
605
606/// EOL - Print a newline character to asm stream. If a comment is present
607/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000608void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000609 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000610}
Owen Anderson25995092008-07-01 21:16:27 +0000611
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000612void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000613 if (VerboseAsm && !Comment.empty()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000614 O << '\t'
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000615 << TAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000616 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000617 << Comment;
618 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000619 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000620}
621
Owen Anderson25995092008-07-01 21:16:27 +0000622void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000623 if (VerboseAsm && *Comment) {
Owen Anderson25995092008-07-01 21:16:27 +0000624 O << '\t'
625 << TAI->getCommentString()
626 << ' '
627 << Comment;
628 }
629 O << '\n';
630}
631
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000632/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
633/// unsigned leb128 value.
634void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
635 if (TAI->hasLEB128()) {
636 O << "\t.uleb128\t"
637 << Value;
638 } else {
639 O << TAI->getData8bitsDirective();
640 PrintULEB128(Value);
641 }
642}
643
644/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
645/// signed leb128 value.
646void AsmPrinter::EmitSLEB128Bytes(int Value) const {
647 if (TAI->hasLEB128()) {
648 O << "\t.sleb128\t"
649 << Value;
650 } else {
651 O << TAI->getData8bitsDirective();
652 PrintSLEB128(Value);
653 }
654}
655
656/// EmitInt8 - Emit a byte directive and value.
657///
658void AsmPrinter::EmitInt8(int Value) const {
659 O << TAI->getData8bitsDirective();
660 PrintHex(Value & 0xFF);
661}
662
663/// EmitInt16 - Emit a short directive and value.
664///
665void AsmPrinter::EmitInt16(int Value) const {
666 O << TAI->getData16bitsDirective();
667 PrintHex(Value & 0xFFFF);
668}
669
670/// EmitInt32 - Emit a long directive and value.
671///
672void AsmPrinter::EmitInt32(int Value) const {
673 O << TAI->getData32bitsDirective();
674 PrintHex(Value);
675}
676
677/// EmitInt64 - Emit a long long directive and value.
678///
679void AsmPrinter::EmitInt64(uint64_t Value) const {
680 if (TAI->getData64bitsDirective()) {
681 O << TAI->getData64bitsDirective();
682 PrintHex(Value);
683 } else {
684 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000685 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000686 EmitInt32(unsigned(Value));
687 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000688 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689 EmitInt32(unsigned(Value >> 32));
690 }
691 }
692}
693
694/// toOctal - Convert the low order bits of X into an octal digit.
695///
696static inline char toOctal(int X) {
697 return (X&7)+'0';
698}
699
700/// printStringChar - Print a char, escaped if necessary.
701///
David Greene71847812009-07-14 20:18:05 +0000702static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000703 if (C == '"') {
704 O << "\\\"";
705 } else if (C == '\\') {
706 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000707 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000708 O << C;
709 } else {
710 switch(C) {
711 case '\b': O << "\\b"; break;
712 case '\f': O << "\\f"; break;
713 case '\n': O << "\\n"; break;
714 case '\r': O << "\\r"; break;
715 case '\t': O << "\\t"; break;
716 default:
717 O << '\\';
718 O << toOctal(C >> 6);
719 O << toOctal(C >> 3);
720 O << toOctal(C >> 0);
721 break;
722 }
723 }
724}
725
726/// EmitString - Emit a string with quotes and a null terminator.
727/// Special characters are emitted properly.
728/// \literal (Eg. '\t') \endliteral
729void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendlingf34be822009-04-09 23:51:31 +0000730 EmitString(String.c_str(), String.size());
731}
732
733void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000734 const char* AscizDirective = TAI->getAscizDirective();
735 if (AscizDirective)
736 O << AscizDirective;
737 else
738 O << TAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000739 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000740 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000741 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000742 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000743 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000744 else
745 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000746}
747
748
Dan Gohman189f80d2007-09-24 20:58:13 +0000749/// EmitFile - Emit a .file directive.
750void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
751 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000752 for (unsigned i = 0, N = Name.size(); i < N; ++i)
753 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000754 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000755}
756
757
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000758//===----------------------------------------------------------------------===//
759
Chris Lattner3a420532007-05-31 18:57:45 +0000760// EmitAlignment - Emit an alignment directive to the specified power of
761// two boundary. For example, if you pass in 3 here, you will get an 8
762// byte alignment. If a global value is specified, and if that global has
763// an explicit alignment requested, it will unconditionally override the
764// alignment request. However, if ForcedAlignBits is specified, this value
765// has final say: the ultimate alignment will be the max of ForcedAlignBits
766// and the alignment computed with NumBits and the global.
767//
768// The algorithm is:
769// Align = NumBits;
770// if (GV && GV->hasalignment) Align = GV->getalignment();
771// Align = std::max(Align, ForcedAlignBits);
772//
773void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000774 unsigned ForcedAlignBits,
775 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000776 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000777 NumBits = Log2_32(GV->getAlignment());
778 NumBits = std::max(NumBits, ForcedAlignBits);
779
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000780 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000781 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000782 O << TAI->getAlignDirective() << NumBits;
Evan Chengfb8075d2008-02-28 00:43:03 +0000783
Chris Lattner290c2f52009-08-03 23:20:21 +0000784 if (CurrentSection && CurrentSection->getKind().isText())
785 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
786 O << ',';
787 PrintHex(FillValue);
788 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000789 O << '\n';
Chris Lattnerbfddc202004-08-17 19:14:29 +0000790}
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000791
David Greenebef87682009-07-31 21:57:10 +0000792/// PadToColumn - This gets called every time a tab is emitted. If
793/// column padding is turned on, we replace the tab with the
794/// appropriate amount of padding. If not, we replace the tab with a
795/// space, except for the first operand so that initial operands are
796/// always lined up by tabs.
797void AsmPrinter::PadToColumn(unsigned Operand) const {
798 if (TAI->getOperandColumn(Operand) > 0) {
799 O.PadToColumn(TAI->getOperandColumn(Operand), 1);
800 }
801 else {
802 if (Operand == 1) {
803 // Emit the tab after the mnemonic.
804 O << '\t';
805 }
806 else {
807 // Replace the tab with a space.
808 O << ' ';
809 }
810 }
811}
812
Chris Lattner25045bd2005-11-21 07:51:36 +0000813/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000814///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000815void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000816 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000817 if (TAI->getZeroDirective()) {
818 O << TAI->getZeroDirective() << NumZeros;
819 if (TAI->getZeroDirectiveSuffix())
820 O << TAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000821 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000822 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000823 for (; NumZeros; --NumZeros)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000824 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000825 }
826 }
827}
828
Chris Lattnera80ba712004-08-16 23:15:22 +0000829// Print out the specified constant, without a storage class. Only the
830// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000831void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000832 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000833 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000834 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000835 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000836 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000837 // This is a constant address for a global variable or function. Use the
838 // name of the variable or function as the address value, possibly
839 // decorating it with GlobalVarAddrPrefix/Suffix or
840 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000841 if (isa<Function>(GV)) {
842 O << TAI->getFunctionAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000843 << Mang->getMangledName(GV)
Jim Laskey563321a2006-09-06 18:34:40 +0000844 << TAI->getFunctionAddrSuffix();
845 } else {
846 O << TAI->getGlobalVarAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000847 << Mang->getMangledName(GV)
Jim Laskey563321a2006-09-06 18:34:40 +0000848 << TAI->getGlobalVarAddrSuffix();
849 }
Duraid Madina855a5192005-04-02 12:21:51 +0000850 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000851 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000852 unsigned Opcode = CE->getOpcode();
853 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000854 case Instruction::Trunc:
855 case Instruction::ZExt:
856 case Instruction::SExt:
857 case Instruction::FPTrunc:
858 case Instruction::FPExt:
859 case Instruction::UIToFP:
860 case Instruction::SIToFP:
861 case Instruction::FPToUI:
862 case Instruction::FPToSI:
863 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000864 case Instruction::GetElementPtr: {
865 // generate a symbolic expression for the byte address
866 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000867 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
868 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
869 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000870 // Truncate/sext the offset to the pointer size.
871 if (TD->getPointerSizeInBits() != 64) {
872 int SExtAmount = 64-TD->getPointerSizeInBits();
873 Offset = (Offset << SExtAmount) >> SExtAmount;
874 }
875
Chris Lattner27e19212005-02-14 21:40:26 +0000876 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000877 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000878 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000879 if (Offset > 0)
880 O << ") + " << Offset;
881 else if (Offset < 0)
882 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000883 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000884 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000885 }
886 break;
887 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000888 case Instruction::BitCast:
889 return EmitConstantValueOnly(CE->getOperand(0));
890
Chris Lattnerddc94012006-12-12 05:18:19 +0000891 case Instruction::IntToPtr: {
892 // Handle casts to pointers by changing them into casts to the appropriate
893 // integer type. This promotes constant folding and simplifies this code.
894 Constant *Op = CE->getOperand(0);
895 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
896 return EmitConstantValueOnly(Op);
897 }
898
899
900 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000901 // Support only foldable casts to/from pointers that can be eliminated by
902 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000903 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000904 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000905
Chris Lattnerddc94012006-12-12 05:18:19 +0000906 // We can emit the pointer value into this slot if the slot is an
907 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000908 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000909 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000910
911 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000912 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000913 APInt ptrMask =
914 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000915
916 SmallString<40> S;
917 ptrMask.toStringUnsigned(S);
918 O << ") & " << S.c_str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000919 break;
920 }
921 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000922 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000923 case Instruction::And:
924 case Instruction::Or:
925 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000926 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000927 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000928 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000929 switch (Opcode) {
930 case Instruction::Add:
931 O << " + ";
932 break;
933 case Instruction::Sub:
934 O << " - ";
935 break;
936 case Instruction::And:
937 O << " & ";
938 break;
939 case Instruction::Or:
940 O << " | ";
941 break;
942 case Instruction::Xor:
943 O << " ^ ";
944 break;
945 default:
946 break;
947 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000948 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000949 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000950 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000951 break;
952 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000953 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000954 }
955 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000956 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000957 }
958}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000959
Chris Lattner2980cef2005-11-10 18:06:33 +0000960/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000961/// the predicate isString is true.
962///
David Greene71847812009-07-14 20:18:05 +0000963static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000964 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000965 assert(CVA->isString() && "Array is not string compatible!");
966
Dan Gohmand19a53b2008-06-30 22:03:41 +0000967 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000968 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000969 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000970 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000971 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000972 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000973 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000974}
975
Jeff Cohenc884db42006-05-02 01:16:28 +0000976/// EmitString - Emit a zero-byte-terminated string constant.
977///
978void AsmPrinter::EmitString(const ConstantArray *CVA) const {
979 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000980 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000981 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000982 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000983 printAsCString(O, CVA, NumElts-1);
984 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000985 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000986 printAsCString(O, CVA, NumElts);
987 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000988 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000989}
990
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000991void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
992 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000993 if (CVA->isString()) {
994 EmitString(CVA);
995 } else { // Not a string. Print the values in successive locations
996 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000997 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000998 }
999}
1000
1001void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1002 const VectorType *PTy = CP->getType();
1003
1004 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1005 EmitGlobalConstant(CP->getOperand(I));
1006}
1007
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001008void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1009 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001010 // Print the fields in successive locations. Pad to align if needed!
1011 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001012 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001013 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1014 uint64_t sizeSoFar = 0;
1015 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1016 const Constant* field = CVS->getOperand(i);
1017
1018 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001019 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001020 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1021 - cvsLayout->getElementOffset(i)) - fieldSize;
1022 sizeSoFar += fieldSize + padSize;
1023
1024 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001025 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001026
1027 // Insert padding - this may include padding to increase the size of the
1028 // current field up to the ABI size (if the struct is not packed) as well
1029 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001030 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001031 }
1032 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1033 "Layout of constant struct may be incorrect!");
1034}
1035
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001036void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1037 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001038 // FP Constants are printed as integer constants to avoid losing
1039 // precision...
1040 const TargetData *TD = TM.getTargetData();
1041 if (CFP->getType() == Type::DoubleTy) {
1042 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1043 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001044 if (TAI->getData64bitsDirective(AddrSpace)) {
1045 O << TAI->getData64bitsDirective(AddrSpace) << i;
1046 if (VerboseAsm)
1047 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1048 O << '\n';
1049 } else if (TD->isBigEndian()) {
1050 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1051 if (VerboseAsm)
1052 O << '\t' << TAI->getCommentString()
1053 << " double most significant word " << Val;
1054 O << '\n';
1055 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1056 if (VerboseAsm)
1057 O << '\t' << TAI->getCommentString()
1058 << " double least significant word " << Val;
1059 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001060 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001061 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1062 if (VerboseAsm)
1063 O << '\t' << TAI->getCommentString()
1064 << " double least significant word " << Val;
1065 O << '\n';
1066 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1067 if (VerboseAsm)
1068 O << '\t' << TAI->getCommentString()
1069 << " double most significant word " << Val;
1070 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001071 }
1072 return;
1073 } else if (CFP->getType() == Type::FloatTy) {
1074 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001075 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001076 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1077 if (VerboseAsm)
1078 O << '\t' << TAI->getCommentString() << " float " << Val;
1079 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001080 return;
1081 } else if (CFP->getType() == Type::X86_FP80Ty) {
1082 // all long double variants are printed as hex
1083 // api needed to prevent premature destruction
1084 APInt api = CFP->getValueAPF().bitcastToAPInt();
1085 const uint64_t *p = api.getRawData();
1086 // Convert to double so we can print the approximate val as a comment.
1087 APFloat DoubleVal = CFP->getValueAPF();
1088 bool ignored;
1089 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1090 &ignored);
1091 if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001092 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1093 if (VerboseAsm)
1094 O << '\t' << TAI->getCommentString()
1095 << " long double most significant halfword of ~"
1096 << DoubleVal.convertToDouble();
1097 O << '\n';
1098 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1099 if (VerboseAsm)
1100 O << '\t' << TAI->getCommentString() << " long double next halfword";
1101 O << '\n';
1102 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1103 if (VerboseAsm)
1104 O << '\t' << TAI->getCommentString() << " long double next halfword";
1105 O << '\n';
1106 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1107 if (VerboseAsm)
1108 O << '\t' << TAI->getCommentString() << " long double next halfword";
1109 O << '\n';
1110 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1111 if (VerboseAsm)
1112 O << '\t' << TAI->getCommentString()
1113 << " long double least significant halfword";
1114 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001115 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001116 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1117 if (VerboseAsm)
1118 O << '\t' << TAI->getCommentString()
1119 << " long double least significant halfword of ~"
1120 << DoubleVal.convertToDouble();
1121 O << '\n';
1122 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1123 if (VerboseAsm)
1124 O << '\t' << TAI->getCommentString()
1125 << " long double next halfword";
1126 O << '\n';
1127 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1128 if (VerboseAsm)
1129 O << '\t' << TAI->getCommentString()
1130 << " long double next halfword";
1131 O << '\n';
1132 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1133 if (VerboseAsm)
1134 O << '\t' << TAI->getCommentString()
1135 << " long double next halfword";
1136 O << '\n';
1137 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1138 if (VerboseAsm)
1139 O << '\t' << TAI->getCommentString()
1140 << " long double most significant halfword";
1141 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001142 }
Duncan Sands777d2302009-05-09 07:06:46 +00001143 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001144 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001145 return;
1146 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1147 // all long double variants are printed as hex
1148 // api needed to prevent premature destruction
1149 APInt api = CFP->getValueAPF().bitcastToAPInt();
1150 const uint64_t *p = api.getRawData();
1151 if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001152 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1153 if (VerboseAsm)
1154 O << '\t' << TAI->getCommentString()
1155 << " long double most significant word";
1156 O << '\n';
1157 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1158 if (VerboseAsm)
1159 O << '\t' << TAI->getCommentString()
1160 << " long double next word";
1161 O << '\n';
1162 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1163 if (VerboseAsm)
1164 O << '\t' << TAI->getCommentString()
1165 << " long double next word";
1166 O << '\n';
1167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1168 if (VerboseAsm)
1169 O << '\t' << TAI->getCommentString()
1170 << " long double least significant word";
1171 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001172 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001173 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1174 if (VerboseAsm)
1175 O << '\t' << TAI->getCommentString()
1176 << " long double least significant word";
1177 O << '\n';
1178 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1179 if (VerboseAsm)
1180 O << '\t' << TAI->getCommentString()
1181 << " long double next word";
1182 O << '\n';
1183 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1184 if (VerboseAsm)
1185 O << '\t' << TAI->getCommentString()
1186 << " long double next word";
1187 O << '\n';
1188 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1189 if (VerboseAsm)
1190 O << '\t' << TAI->getCommentString()
1191 << " long double most significant word";
1192 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001193 }
1194 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001195 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001196}
1197
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001198void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1199 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001200 const TargetData *TD = TM.getTargetData();
1201 unsigned BitWidth = CI->getBitWidth();
1202 assert(isPowerOf2_32(BitWidth) &&
1203 "Non-power-of-2-sized integers not handled!");
1204
1205 // We don't expect assemblers to support integer data directives
1206 // for more than 64 bits, so we emit the data in at most 64-bit
1207 // quantities at a time.
1208 const uint64_t *RawData = CI->getValue().getRawData();
1209 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1210 uint64_t Val;
1211 if (TD->isBigEndian())
1212 Val = RawData[e - i - 1];
1213 else
1214 Val = RawData[i];
1215
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001216 if (TAI->getData64bitsDirective(AddrSpace))
1217 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001218 else if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001219 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1220 if (VerboseAsm)
1221 O << '\t' << TAI->getCommentString()
1222 << " Double-word most significant word " << Val;
1223 O << '\n';
1224 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1225 if (VerboseAsm)
1226 O << '\t' << TAI->getCommentString()
1227 << " Double-word least significant word " << Val;
1228 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001229 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001230 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1231 if (VerboseAsm)
1232 O << '\t' << TAI->getCommentString()
1233 << " Double-word least significant word " << Val;
1234 O << '\n';
1235 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1236 if (VerboseAsm)
1237 O << '\t' << TAI->getCommentString()
1238 << " Double-word most significant word " << Val;
1239 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001240 }
1241 }
1242}
1243
Chris Lattner25045bd2005-11-21 07:51:36 +00001244/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001245void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001246 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001247 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001248 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001249
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001250 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001251 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001252 return;
1253 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001254 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001255 return;
1256 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001257 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001258 return;
1259 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001260 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001261 return;
1262 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1263 // Small integers are handled below; large integers are handled here.
1264 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001265 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001266 return;
1267 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001268 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001269 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001270 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001271 }
1272
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001273 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001274 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001275 if (VerboseAsm) {
1276 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1277 SmallString<40> S;
1278 CI->getValue().toStringUnsigned(S, 16);
1279 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1280 }
Scott Micheleefc8452008-06-03 15:39:51 +00001281 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001282 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001283}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001284
Chris Lattnerfad86b02008-08-17 07:19:36 +00001285void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001286 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001287 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001288}
1289
Chris Lattner3ce9b672006-09-26 23:59:50 +00001290/// PrintSpecial - Print information related to the specified machine instr
1291/// that is independent of the operand, and may be independent of the instr
1292/// itself. This can be useful for portably encoding the comment character
1293/// or other bits of target-specific knowledge into the asmstrings. The
1294/// syntax used is ${:comment}. Targets can override this to add support
1295/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001296void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001297 if (!strcmp(Code, "private")) {
1298 O << TAI->getPrivateGlobalPrefix();
1299 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001300 if (VerboseAsm)
1301 O << TAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001302 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001303 // Comparing the address of MI isn't sufficient, because machineinstrs may
1304 // be allocated to the same address across functions.
1305 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1306
Owen Andersonbd58edf2009-06-24 22:28:12 +00001307 // If this is a new LastFn instruction, bump the counter.
1308 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001309 ++Counter;
1310 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001311 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001312 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001313 O << Counter;
1314 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001315 std::string msg;
1316 raw_string_ostream Msg(msg);
1317 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001318 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001319 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001320 }
1321}
1322
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001323/// processDebugLoc - Processes the debug information of each machine
1324/// instruction's DebugLoc.
1325void AsmPrinter::processDebugLoc(DebugLoc DL) {
1326 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1327 if (!DL.isUnknown()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001328 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1329
1330 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1331 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1332 DICompileUnit(CurDLT.CompileUnit)));
1333
1334 PrevDLT = CurDLT;
1335 }
1336 }
1337}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001338
Chris Lattner0264d1a2006-01-27 02:10:10 +00001339/// printInlineAsm - This method formats and prints the specified machine
1340/// instruction that is an inline asm.
1341void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001342 unsigned NumOperands = MI->getNumOperands();
1343
1344 // Count the number of register definitions.
1345 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001346 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001347 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001348 assert(NumDefs != NumOperands-1 && "No asm string?");
1349
Dan Gohmand735b802008-10-03 15:45:36 +00001350 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001351
1352 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001353 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001354
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001355 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1356 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001357 if (AsmStr[0] == 0) {
Dan Gohmand19a53b2008-06-30 22:03:41 +00001358 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001359 return;
1360 }
1361
Jim Laskey563321a2006-09-06 18:34:40 +00001362 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001363
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001364 // The variant of the current asmprinter.
1365 int AsmPrinterVariant = TAI->getAssemblerDialect();
1366
Chris Lattner66099132006-02-01 22:41:11 +00001367 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1368 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001369
Chris Lattner66099132006-02-01 22:41:11 +00001370 while (*LastEmitted) {
1371 switch (*LastEmitted) {
1372 default: {
1373 // Not a special case, emit the string section literally.
1374 const char *LiteralEnd = LastEmitted+1;
1375 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001376 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001377 ++LiteralEnd;
1378 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1379 O.write(LastEmitted, LiteralEnd-LastEmitted);
1380 LastEmitted = LiteralEnd;
1381 break;
1382 }
Chris Lattner1c059972006-05-05 21:47:05 +00001383 case '\n':
1384 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001385 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001386 break;
Chris Lattner66099132006-02-01 22:41:11 +00001387 case '$': {
1388 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001389 bool Done = true;
1390
1391 // Handle escapes.
1392 switch (*LastEmitted) {
1393 default: Done = false; break;
1394 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001395 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1396 O << '$';
1397 ++LastEmitted; // Consume second '$' character.
1398 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001399 case '(': // $( -> same as GCC's { character.
1400 ++LastEmitted; // Consume '(' character.
1401 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001402 llvm_report_error("Nested variants found in inline asm string: '"
1403 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001404 }
1405 CurVariant = 0; // We're in the first variant now.
1406 break;
1407 case '|':
1408 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001409 if (CurVariant == -1)
1410 O << '|'; // this is gcc's behavior for | outside a variant
1411 else
1412 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001413 break;
1414 case ')': // $) -> same as GCC's } char.
1415 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001416 if (CurVariant == -1)
1417 O << '}'; // this is gcc's behavior for } outside a variant
1418 else
1419 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001420 break;
Chris Lattner66099132006-02-01 22:41:11 +00001421 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001422 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001423
1424 bool HasCurlyBraces = false;
1425 if (*LastEmitted == '{') { // ${variable}
1426 ++LastEmitted; // Consume '{' character.
1427 HasCurlyBraces = true;
1428 }
1429
Chris Lattner3e0cc262009-03-10 05:37:13 +00001430 // If we have ${:foo}, then this is not a real operand reference, it is a
1431 // "magic" string reference, just like in .td files. Arrange to call
1432 // PrintSpecial.
1433 if (HasCurlyBraces && *LastEmitted == ':') {
1434 ++LastEmitted;
1435 const char *StrStart = LastEmitted;
1436 const char *StrEnd = strchr(StrStart, '}');
1437 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001438 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1439 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001440 }
1441
1442 std::string Val(StrStart, StrEnd);
1443 PrintSpecial(MI, Val.c_str());
1444 LastEmitted = StrEnd+1;
1445 break;
1446 }
1447
Chris Lattner66099132006-02-01 22:41:11 +00001448 const char *IDStart = LastEmitted;
1449 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001450 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001451 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1452 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001453 llvm_report_error("Bad $ operand number in inline asm string: '"
1454 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001455 }
1456 LastEmitted = IDEnd;
1457
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001458 char Modifier[2] = { 0, 0 };
1459
Chris Lattner66099132006-02-01 22:41:11 +00001460 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001461 // If we have curly braces, check for a modifier character. This
1462 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1463 if (*LastEmitted == ':') {
1464 ++LastEmitted; // Consume ':' character.
1465 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001466 llvm_report_error("Bad ${:} expression in inline asm string: '"
1467 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001468 }
1469
1470 Modifier[0] = *LastEmitted;
1471 ++LastEmitted; // Consume modifier character.
1472 }
1473
Chris Lattner66099132006-02-01 22:41:11 +00001474 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001475 llvm_report_error("Bad ${} expression in inline asm string: '"
1476 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001477 }
1478 ++LastEmitted; // Consume '}' character.
1479 }
1480
1481 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001482 llvm_report_error("Invalid $ operand number in inline asm string: '"
1483 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001484 }
1485
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001486 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001487 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001488 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1489 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001490
1491 bool Error = false;
1492
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001493 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001494 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001495 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001496 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001497 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001498 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001499
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001500 if (OpNo >= MI->getNumOperands()) {
1501 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001502 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001503 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001504 ++OpNo; // Skip over the ID number.
1505
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001506 if (Modifier[0]=='l') // labels are target independent
Chris Lattner8aa797a2007-12-30 23:10:15 +00001507 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengfb8075d2008-02-28 00:43:03 +00001508 false, false, false);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001509 else {
1510 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001511 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001512 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1513 Modifier[0] ? Modifier : 0);
1514 } else {
1515 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1516 Modifier[0] ? Modifier : 0);
1517 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001518 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001519 }
1520 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001521 std::string msg;
1522 raw_string_ostream Msg(msg);
1523 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001524 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001525 MI->print(Msg);
1526 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001527 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001528 }
Chris Lattner66099132006-02-01 22:41:11 +00001529 break;
1530 }
Chris Lattner66099132006-02-01 22:41:11 +00001531 }
1532 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001533 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner66099132006-02-01 22:41:11 +00001534}
1535
Evan Chengda47e6e2008-03-15 00:03:38 +00001536/// printImplicitDef - This method prints the specified machine instruction
1537/// that is an implicit def.
1538void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001539 if (VerboseAsm)
1540 O << '\t' << TAI->getCommentString() << " implicit-def: "
1541 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Chengda47e6e2008-03-15 00:03:38 +00001542}
1543
Jim Laskey1ee29252007-01-26 14:34:52 +00001544/// printLabel - This method prints a local label used by debug and
1545/// exception handling tables.
1546void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001547 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001548}
1549
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001550void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng4eecdeb2008-02-02 08:39:46 +00001551 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001552}
1553
Evan Chenga844bde2008-02-02 04:07:54 +00001554/// printDeclare - This method prints a local variable declaration used by
1555/// debug tables.
Evan Cheng4e3f5a42008-02-04 23:06:48 +00001556/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1557/// entry into dwarf table.
Evan Chenga844bde2008-02-02 04:07:54 +00001558void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Patelc48c5502009-01-13 21:44:10 +00001559 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng4e3f5a42008-02-04 23:06:48 +00001560 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel1be3ecc2009-04-15 00:10:26 +00001561 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chenga844bde2008-02-02 04:07:54 +00001562}
1563
Chris Lattner66099132006-02-01 22:41:11 +00001564/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1565/// instruction, using the specified assembler variant. Targets should
1566/// overried this to format as appropriate.
1567bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001568 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001569 // Target doesn't support this yet!
1570 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001571}
Chris Lattnerdd260332006-02-24 20:21:58 +00001572
1573bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1574 unsigned AsmVariant,
1575 const char *ExtraCode) {
1576 // Target doesn't support this yet!
1577 return true;
1578}
Nate Begeman37efe672006-04-22 18:53:45 +00001579
1580/// printBasicBlockLabel - This method prints the label for the specified
1581/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001582void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengfb8075d2008-02-28 00:43:03 +00001583 bool printAlign,
Nate Begemancdf38c42006-05-02 05:37:32 +00001584 bool printColon,
1585 bool printComment) const {
Evan Chengfb8075d2008-02-28 00:43:03 +00001586 if (printAlign) {
1587 unsigned Align = MBB->getAlignment();
1588 if (Align)
1589 EmitAlignment(Log2_32(Align));
1590 }
1591
Dan Gohmand19a53b2008-06-30 22:03:41 +00001592 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng347d39f2007-10-14 05:57:21 +00001593 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001594 if (printColon)
1595 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001596 if (printComment && MBB->getBasicBlock())
Dan Gohman286d5692007-07-30 15:06:25 +00001597 O << '\t' << TAI->getCommentString() << ' '
Daniel Dunbar460f6562009-07-26 09:48:23 +00001598 << MBB->getBasicBlock()->getNameStr();
Nate Begeman37efe672006-04-22 18:53:45 +00001599}
Nate Begeman52a51e382006-08-12 21:29:52 +00001600
Evan Chengcc415862007-11-09 01:32:10 +00001601/// printPICJumpTableSetLabel - This method prints a set label for the
1602/// specified MachineBasicBlock for a jumptable entry.
1603void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1604 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001605 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001606 return;
1607
Jim Laskey563321a2006-09-06 18:34:40 +00001608 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001609 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001610 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001611 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1612 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001613}
Evan Chengd6594ae2006-09-12 21:00:35 +00001614
Evan Chengcc415862007-11-09 01:32:10 +00001615void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1616 const MachineBasicBlock *MBB) const {
Evan Cheng41349c12006-11-01 09:23:08 +00001617 if (!TAI->getSetDirective())
1618 return;
1619
1620 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001621 << getFunctionNumber() << '_' << uid << '_' << uid2
1622 << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001623 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001624 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1625 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001626}
1627
Evan Chengd6594ae2006-09-12 21:00:35 +00001628/// printDataDirective - This method prints the asm directive for the
1629/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001630void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001631 const TargetData *TD = TM.getTargetData();
1632 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001633 case Type::FloatTyID: case Type::DoubleTyID:
1634 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1635 assert(0 && "Should have already output floating point constant.");
1636 default:
1637 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001638 case Type::IntegerTyID: {
1639 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1640 if (BitWidth <= 8)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001641 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001642 else if (BitWidth <= 16)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001643 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001644 else if (BitWidth <= 32)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001645 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001646 else if (BitWidth <= 64) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001647 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001648 "Target cannot handle 64-bit constant exprs!");
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001649 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001650 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001651 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001652 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001653 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001654 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001655 case Type::PointerTyID:
1656 if (TD->getPointerSize() == 8) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001657 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001658 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001659 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001660 } else if (TD->getPointerSize() == 2) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001661 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001662 } else if (TD->getPointerSize() == 1) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001663 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001664 } else {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001665 O << TAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001666 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001667 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001668 }
1669}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001670
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001671void AsmPrinter::printVisibility(const std::string& Name,
1672 unsigned Visibility) const {
1673 if (Visibility == GlobalValue::HiddenVisibility) {
1674 if (const char *Directive = TAI->getHiddenDirective())
1675 O << Directive << Name << '\n';
1676 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1677 if (const char *Directive = TAI->getProtectedDirective())
1678 O << Directive << Name << '\n';
1679 }
1680}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001681
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001682void AsmPrinter::printOffset(int64_t Offset) const {
1683 if (Offset > 0)
1684 O << '+' << Offset;
1685 else if (Offset < 0)
1686 O << Offset;
1687}
1688
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001689GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1690 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001691 return 0;
1692
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001693 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001694 if (GCPI != GCMetadataPrinters.end())
1695 return GCPI->second;
1696
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001697 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001698
1699 for (GCMetadataPrinterRegistry::iterator
1700 I = GCMetadataPrinterRegistry::begin(),
1701 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1702 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001703 GCMetadataPrinter *GMP = I->instantiate();
1704 GMP->S = S;
1705 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1706 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001707 }
1708
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001709 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001710 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001711}
David Greene014700c2009-07-13 20:25:48 +00001712
1713/// EmitComments - Pretty-print comments for instructions
1714void AsmPrinter::EmitComments(const MachineInstr &MI) const
1715{
David Greene67e59832009-07-22 20:33:26 +00001716 if (VerboseAsm) {
1717 if (!MI.getDebugLoc().isUnknown()) {
1718 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001719
David Greene67e59832009-07-22 20:33:26 +00001720 // Print source line info
1721 O.PadToColumn(TAI->getCommentColumn(), 1);
1722 O << TAI->getCommentString() << " SrcLine ";
1723 if (DLT.CompileUnit->hasInitializer()) {
1724 Constant *Name = DLT.CompileUnit->getInitializer();
1725 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1726 if (NameString->isString()) {
1727 O << NameString->getAsString() << " ";
1728 }
1729 }
1730 O << DLT.Line;
1731 if (DLT.Col != 0)
1732 O << ":" << DLT.Col;
1733 }
David Greene3ac1ab82009-07-16 22:24:20 +00001734 }
David Greene014700c2009-07-13 20:25:48 +00001735}
1736
1737/// EmitComments - Pretty-print comments for instructions
1738void AsmPrinter::EmitComments(const MCInst &MI) const
1739{
David Greene67e59832009-07-22 20:33:26 +00001740 if (VerboseAsm) {
1741 if (!MI.getDebugLoc().isUnknown()) {
1742 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001743
David Greene67e59832009-07-22 20:33:26 +00001744 // Print source line info
1745 O.PadToColumn(TAI->getCommentColumn(), 1);
1746 O << TAI->getCommentString() << " SrcLine ";
1747 if (DLT.CompileUnit->hasInitializer()) {
1748 Constant *Name = DLT.CompileUnit->getInitializer();
1749 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1750 if (NameString->isString()) {
1751 O << NameString->getAsString() << " ";
1752 }
1753 }
1754 O << DLT.Line;
1755 if (DLT.Col != 0)
1756 O << ":" << DLT.Col;
1757 }
David Greene3ac1ab82009-07-16 22:24:20 +00001758 }
David Greene014700c2009-07-13 20:25:48 +00001759}