blob: 96019bf7326c38ab33bb5808606c0cc869bf0e3f [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"
David Greene1924aab2009-11-13 21:34:57 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000022#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000024#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000026#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000036#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattner66099132006-02-01 22:41:11 +000047#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000048using namespace llvm;
49
Evan Cheng42bf74b2009-03-25 01:47:28 +000050static cl::opt<cl::boolOrDefault>
51AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
52 cl::init(cl::BOU_UNSET));
53
Devang Patel19974732007-05-03 01:11:54 +000054char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000055AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000057 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000058 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000059
60 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000061 // FIXME: Pass instprinter to streamer.
62 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000063
Chris Lattner290c2f52009-08-03 23:20:21 +000064 LastMI(0), LastFn(0), Counter(~0U),
Devang Patela1434042009-10-01 01:15:28 +000065 PrevDLT(0, 0, ~0U, ~0U) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000066 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000067 switch (AsmVerbose) {
68 case cl::BOU_UNSET: VerboseAsm = VDef; break;
69 case cl::BOU_TRUE: VerboseAsm = true; break;
70 case cl::BOU_FALSE: VerboseAsm = false; break;
71 }
72}
Chris Lattnered138932005-12-13 06:32:10 +000073
Gordon Henriksenc317a602008-08-17 12:08:44 +000074AsmPrinter::~AsmPrinter() {
75 for (gcp_iterator I = GCMetadataPrinters.begin(),
76 E = GCMetadataPrinters.end(); I != E; ++I)
77 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000078
79 delete &OutStreamer;
80 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000081}
Chris Lattnered138932005-12-13 06:32:10 +000082
Chris Lattner38c39882009-08-03 19:12:26 +000083TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000084 return TM.getTargetLowering()->getObjFileLowering();
85}
86
Chris Lattnerdabf07c2009-08-18 06:15:16 +000087/// getCurrentSection() - Return the current section we are emitting to.
88const MCSection *AsmPrinter::getCurrentSection() const {
89 return OutStreamer.getCurrentSection();
90}
91
92
Gordon Henriksence224772008-01-07 01:30:38 +000093void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000094 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000095 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000096 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000097 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000098 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000099}
100
Chris Lattnera80ba712004-08-16 23:15:22 +0000101bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000102 // Initialize TargetLoweringObjectFile.
103 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
104 .Initialize(OutContext, TM);
105
Chris Lattner33adcfb2009-08-22 21:43:10 +0000106 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
107 MAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000108
Chris Lattner33adcfb2009-08-22 21:43:10 +0000109 if (MAI->doesAllowQuotesInName())
Chris Lattnera93ca922009-06-18 23:41:35 +0000110 Mang->setUseQuotes(true);
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +0000111
112 if (MAI->doesAllowNameToStartWithDigit())
113 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattnera93ca922009-06-18 23:41:35 +0000114
Bob Wilson812209a2009-09-30 22:06:26 +0000115 // Allow the target to emit any magic that it wants at the start of the file.
116 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000117
Chris Lattner33adcfb2009-08-22 21:43:10 +0000118 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000119 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000120 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000121 a function came from. */
122 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
123 }
124
Bob Wilson812209a2009-09-30 22:06:26 +0000125 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
126 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000127 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
128 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000129 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000130
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000131 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000132 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000133 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000134 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000135 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000136
Chris Lattnerb55e0682009-09-24 05:44:53 +0000137 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
138 if (MMI)
139 MMI->AnalyzeModule(M);
140 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000141 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000142 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000143
Chris Lattnera80ba712004-08-16 23:15:22 +0000144 return false;
145}
146
147bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000148 // Emit global variables.
149 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
150 I != E; ++I)
151 PrintGlobalVariable(I);
152
Chris Lattner1f522fe2009-06-24 18:54:37 +0000153 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000154 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000155 DW->EndModule();
156
Chris Lattner0a7befa2009-06-24 18:52:01 +0000157 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000158 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000159 // FIXME: This is not lazy, it would be nice to only print weak references
160 // to stuff that is actually used. Note that doing so would require targets
161 // to notice uses in operands (due to constant exprs etc). This should
162 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000163
Chris Lattner0a7befa2009-06-24 18:52:01 +0000164 // Print out module-level global variables here.
165 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
166 I != E; ++I) {
167 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000168 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000169 }
170
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000171 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000172 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000173 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000174 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000175 }
176
Chris Lattner33adcfb2009-08-22 21:43:10 +0000177 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000178 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000179 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000180 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000181 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000182
183 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000184 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000185
Chris Lattner33adcfb2009-08-22 21:43:10 +0000186 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000188 else if (I->hasWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000189 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000190 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000191 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000192
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000193 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000194
Chris Lattner33adcfb2009-08-22 21:43:10 +0000195 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000196 }
197 }
198
Duncan Sands1465d612009-01-28 13:14:17 +0000199 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000200 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
201 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
202 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000203 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000204
Dan Gohmana779a982008-05-05 00:28:39 +0000205 // If we don't have any trampolines, then we don't require stack memory
206 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000207 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000208 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000209 if (MAI->getNonexecutableStackDirective())
210 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000211
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000212
213 // Allow the target to emit any magic that it wants at the end of the file,
214 // after everything else has gone out.
215 EmitEndOfAsmFile(M);
216
Chris Lattnera80ba712004-08-16 23:15:22 +0000217 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000218 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000219
220 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000221 return false;
222}
223
Chris Lattner25045bd2005-11-21 07:51:36 +0000224void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000225 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000226 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000227 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000228
Chris Lattner25d812b2009-09-16 00:35:39 +0000229 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000230 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000231}
232
Evan Cheng1606e8e2009-03-13 07:51:59 +0000233namespace {
234 // SectionCPs - Keep track the alignment, constpool entries per Section.
235 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000236 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000237 unsigned Alignment;
238 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000239 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000240 };
241}
242
Chris Lattner3b4fd322005-11-21 08:25:09 +0000243/// EmitConstantPool - Print to the current output stream assembly
244/// representations of the constants in the constant pool MCP. This is
245/// used to print out constants which have been "spilled to memory" by
246/// the code generator.
247///
248void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000249 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000250 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000251
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000252 // Calculate sections for constant pool entries. We collect entries to go into
253 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000254 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000255 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000256 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000257 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000258
259 SectionKind Kind;
260 switch (CPE.getRelocationInfo()) {
261 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000262 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000263 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000264 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000265 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000266 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000267 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000268 case 4: Kind = SectionKind::getMergeableConst4(); break;
269 case 8: Kind = SectionKind::getMergeableConst8(); break;
270 case 16: Kind = SectionKind::getMergeableConst16();break;
271 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000272 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000273 }
274
Chris Lattner83d77fa2009-08-01 23:46:12 +0000275 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000276
Evan Cheng1606e8e2009-03-13 07:51:59 +0000277 // The number of sections are small, just do a linear search from the
278 // last section to the first.
279 bool Found = false;
280 unsigned SecIdx = CPSections.size();
281 while (SecIdx != 0) {
282 if (CPSections[--SecIdx].S == S) {
283 Found = true;
284 break;
285 }
286 }
287 if (!Found) {
288 SecIdx = CPSections.size();
289 CPSections.push_back(SectionCPs(S, Align));
290 }
291
292 if (Align > CPSections[SecIdx].Alignment)
293 CPSections[SecIdx].Alignment = Align;
294 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000295 }
296
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000297 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000298 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000299 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000300 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000301
Evan Cheng1606e8e2009-03-13 07:51:59 +0000302 unsigned Offset = 0;
303 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
304 unsigned CPI = CPSections[i].CPEs[j];
305 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000306
Chris Lattner3029f922006-02-09 04:46:04 +0000307 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000308 unsigned AlignMask = CPE.getAlignment() - 1;
309 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
310 EmitZeros(NewOffset - Offset);
311
312 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000313 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000314
Chris Lattner33adcfb2009-08-22 21:43:10 +0000315 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000316 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000317 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000318 O.PadToColumn(MAI->getCommentColumn());
319 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000320 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000321 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000322 O << '\n';
323 if (CPE.isMachineConstantPoolEntry())
324 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
325 else
326 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000327 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000328 }
329}
330
Nate Begeman37efe672006-04-22 18:53:45 +0000331/// EmitJumpTableInfo - Print assembly representations of the jump tables used
332/// by the current function to the current output stream.
333///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000334void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
335 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000336 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
337 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000338
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000339 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000340
Nate Begeman2f1ae882006-07-27 01:13:04 +0000341 // Pick the directive to use to print the jump table entries, and switch to
342 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000343 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000344
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000345 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000346 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000347 if (F->isWeakForLinker() ||
348 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000349 // In PIC mode, we need to emit the jump table to the same section as the
350 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000351 // We should also do if the section name is NULL or function is declared in
352 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000353 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
354 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000355 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000356 // Otherwise, drop it in the readonly section.
357 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000358 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000359 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000360 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000361 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000362
363 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000364
Nate Begeman37efe672006-04-22 18:53:45 +0000365 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000366 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000367
368 // If this jump table was deleted, ignore it.
369 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000370
371 // For PIC codegen, if possible we want to use the SetDirective to reduce
372 // the number of relocations the assembler will generate for the jump table.
373 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000374 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000375 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000376 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000377 if (EmittedSets.insert(JTBBs[ii]))
378 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000379
Chris Lattner7c301912009-09-13 19:02:16 +0000380 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000381 // before each jump table. The first label is never referenced, but tells
382 // the assembler and linker the extents of the jump table object. The
383 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000384 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
385 O << MAI->getLinkerPrivateGlobalPrefix()
386 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000387 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000388
Chris Lattner33adcfb2009-08-22 21:43:10 +0000389 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000390 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000391
Nate Begeman37efe672006-04-22 18:53:45 +0000392 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000393 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000394 O << '\n';
395 }
396 }
397}
398
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000399void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
400 const MachineBasicBlock *MBB,
401 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000402 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000403
404 // Use JumpTableDirective otherwise honor the entry size from the jump table
405 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000406 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000407 bool HadJTEntryDirective = JTEntryDirective != NULL;
408 if (!HadJTEntryDirective) {
409 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000410 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000411 }
412
413 O << JTEntryDirective << ' ';
414
415 // If we have emitted set directives for the jump table entries, print
416 // them rather than the entries themselves. If we're emitting PIC, then
417 // emit the table entries as differences between two text section labels.
418 // If we're emitting non-PIC code, then emit the entries as direct
419 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000420 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000421 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000422 } else if (MAI->getSetDirective()) {
423 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000424 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000425 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000426 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000427 // If the arch uses custom Jump Table directives, don't calc relative to
428 // JT
429 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000430 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000431 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000432 }
433}
434
435
Chris Lattnered138932005-12-13 06:32:10 +0000436/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
437/// special global used by LLVM. If so, emit it and return true, otherwise
438/// do nothing and return false.
439bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000440 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000441 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000442 EmitLLVMUsedList(GV->getInitializer());
443 return true;
444 }
445
Chris Lattner401e10c2009-07-20 06:14:25 +0000446 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000447 if (GV->getSection() == "llvm.metadata" ||
448 GV->hasAvailableExternallyLinkage())
449 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000450
451 if (!GV->hasAppendingLinkage()) return false;
452
453 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000454
Evan Cheng916d07c2007-06-04 20:39:18 +0000455 const TargetData *TD = TM.getTargetData();
456 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000457 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000458 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000459 EmitAlignment(Align, 0);
460 EmitXXStructorList(GV->getInitializer());
461 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000462 }
463
Chris Lattnerf231c072009-03-09 05:52:15 +0000464 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000465 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000466 EmitAlignment(Align, 0);
467 EmitXXStructorList(GV->getInitializer());
468 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000469 }
470
471 return false;
472}
473
Chris Lattner33adcfb2009-08-22 21:43:10 +0000474/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000475/// global in the specified llvm.used list for which emitUsedDirectiveFor
476/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000477void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000478 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000479
Dan Gohmana119de82009-06-14 23:30:43 +0000480 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000481 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
482 if (InitList == 0) return;
483
484 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000485 const GlobalValue *GV =
486 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000487 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000488 O << Directive;
489 EmitConstantValueOnly(InitList->getOperand(i));
490 O << '\n';
491 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000492 }
493}
494
Chris Lattnered138932005-12-13 06:32:10 +0000495/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
496/// function pointers, ignoring the init priority.
497void AsmPrinter::EmitXXStructorList(Constant *List) {
498 // Should be an array of '{ int, void ()* }' structs. The first value is the
499 // init priority, which we ignore.
500 if (!isa<ConstantArray>(List)) return;
501 ConstantArray *InitList = cast<ConstantArray>(List);
502 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
503 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
504 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000505
506 if (CS->getOperand(1)->isNullValue())
507 return; // Found a null terminator, exit printing.
508 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000509 EmitGlobalConstant(CS->getOperand(1));
510 }
511}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000512
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000513
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000514//===----------------------------------------------------------------------===//
515/// LEB 128 number encoding.
516
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000517/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000518/// representing an unsigned leb128 value.
519void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000520 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000521 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000522 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000523 Value >>= 7;
524 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000525 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000526 if (Value) O << ", ";
527 } while (Value);
528}
529
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000530/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000531/// representing a signed leb128 value.
532void AsmPrinter::PrintSLEB128(int Value) const {
533 int Sign = Value >> (8 * sizeof(Value) - 1);
534 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000535 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000536
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000537 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000538 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000539 Value >>= 7;
540 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
541 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000542 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000543 if (IsMore) O << ", ";
544 } while (IsMore);
545}
546
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000547//===--------------------------------------------------------------------===//
548// Emission and print routines
549//
550
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000551/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000552///
553void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000554 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000555 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000556}
557
558/// EOL - Print a newline character to asm stream. If a comment is present
559/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000560void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000561 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000562}
Owen Anderson25995092008-07-01 21:16:27 +0000563
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000564void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000565 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000566 O.PadToColumn(MAI->getCommentColumn());
567 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000568 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000569 << Comment;
570 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000571 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000572}
573
Owen Anderson25995092008-07-01 21:16:27 +0000574void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000575 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000576 O.PadToColumn(MAI->getCommentColumn());
577 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000578 << ' '
579 << Comment;
580 }
581 O << '\n';
582}
583
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000584static const char *DecodeDWARFEncoding(unsigned Encoding) {
585 switch (Encoding) {
586 case dwarf::DW_EH_PE_absptr:
587 return "absptr";
588 case dwarf::DW_EH_PE_omit:
589 return "omit";
590 case dwarf::DW_EH_PE_pcrel:
591 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000592 case dwarf::DW_EH_PE_udata4:
593 return "udata4";
594 case dwarf::DW_EH_PE_udata8:
595 return "udata8";
596 case dwarf::DW_EH_PE_sdata4:
597 return "sdata4";
598 case dwarf::DW_EH_PE_sdata8:
599 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000600 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
601 return "pcrel udata4";
602 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
603 return "pcrel sdata4";
604 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
605 return "pcrel udata8";
606 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
607 return "pcrel sdata8";
608 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
609 return "indirect pcrel udata4";
610 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
611 return "indirect pcrel sdata4";
612 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
613 return "indirect pcrel udata8";
614 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
615 return "indirect pcrel sdata8";
616 }
617
618 return 0;
619}
620
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000621void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
622 if (VerboseAsm && *Comment) {
623 O.PadToColumn(MAI->getCommentColumn());
624 O << MAI->getCommentString()
625 << ' '
626 << Comment;
627
628 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
629 O << " (" << EncStr << ')';
630 }
631 O << '\n';
632}
633
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000634/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
635/// unsigned leb128 value.
636void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000637 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000638 O << "\t.uleb128\t"
639 << Value;
640 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000641 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000642 PrintULEB128(Value);
643 }
644}
645
646/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
647/// signed leb128 value.
648void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000649 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000650 O << "\t.sleb128\t"
651 << Value;
652 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000653 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000654 PrintSLEB128(Value);
655 }
656}
657
658/// EmitInt8 - Emit a byte directive and value.
659///
660void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000661 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000662 PrintHex(Value & 0xFF);
663}
664
665/// EmitInt16 - Emit a short directive and value.
666///
667void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000668 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000669 PrintHex(Value & 0xFFFF);
670}
671
672/// EmitInt32 - Emit a long directive and value.
673///
674void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000675 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000676 PrintHex(Value);
677}
678
679/// EmitInt64 - Emit a long long directive and value.
680///
681void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000682 if (MAI->getData64bitsDirective()) {
683 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000684 PrintHex(Value);
685 } else {
686 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000687 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000688 EmitInt32(unsigned(Value));
689 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000690 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000691 EmitInt32(unsigned(Value >> 32));
692 }
693 }
694}
695
696/// toOctal - Convert the low order bits of X into an octal digit.
697///
698static inline char toOctal(int X) {
699 return (X&7)+'0';
700}
701
702/// printStringChar - Print a char, escaped if necessary.
703///
David Greene71847812009-07-14 20:18:05 +0000704static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000705 if (C == '"') {
706 O << "\\\"";
707 } else if (C == '\\') {
708 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000709 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000710 O << C;
711 } else {
712 switch(C) {
713 case '\b': O << "\\b"; break;
714 case '\f': O << "\\f"; break;
715 case '\n': O << "\\n"; break;
716 case '\r': O << "\\r"; break;
717 case '\t': O << "\\t"; break;
718 default:
719 O << '\\';
720 O << toOctal(C >> 6);
721 O << toOctal(C >> 3);
722 O << toOctal(C >> 0);
723 break;
724 }
725 }
726}
727
728/// EmitString - Emit a string with quotes and a null terminator.
729/// Special characters are emitted properly.
730/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000731void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000732 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000733}
734
735void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000736 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000737 if (AscizDirective)
738 O << AscizDirective;
739 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000740 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000741 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000742 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000743 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000744 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000745 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000746 else
747 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000748}
749
750
Dan Gohman189f80d2007-09-24 20:58:13 +0000751/// EmitFile - Emit a .file directive.
752void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
753 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000754 for (unsigned i = 0, N = Name.size(); i < N; ++i)
755 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000756 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000757}
758
759
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000760//===----------------------------------------------------------------------===//
761
Chris Lattner3a420532007-05-31 18:57:45 +0000762// EmitAlignment - Emit an alignment directive to the specified power of
763// two boundary. For example, if you pass in 3 here, you will get an 8
764// byte alignment. If a global value is specified, and if that global has
765// an explicit alignment requested, it will unconditionally override the
766// alignment request. However, if ForcedAlignBits is specified, this value
767// has final say: the ultimate alignment will be the max of ForcedAlignBits
768// and the alignment computed with NumBits and the global.
769//
770// The algorithm is:
771// Align = NumBits;
772// if (GV && GV->hasalignment) Align = GV->getalignment();
773// Align = std::max(Align, ForcedAlignBits);
774//
775void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000776 unsigned ForcedAlignBits,
777 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000778 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000779 NumBits = Log2_32(GV->getAlignment());
780 NumBits = std::max(NumBits, ForcedAlignBits);
781
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000782 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000783
784 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000785 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000786 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000787
788 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000789}
David Greenea5bb59f2009-08-05 21:00:52 +0000790
Chris Lattner25045bd2005-11-21 07:51:36 +0000791/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000792///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000793void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000794 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000795 if (MAI->getZeroDirective()) {
796 O << MAI->getZeroDirective() << NumZeros;
797 if (MAI->getZeroDirectiveSuffix())
798 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000799 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000800 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000801 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000802 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000803 }
804 }
805}
806
Chris Lattnera80ba712004-08-16 23:15:22 +0000807// Print out the specified constant, without a storage class. Only the
808// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000809void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000810 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000811 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000812 return;
813 }
814
815 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000816 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000817 return;
818 }
819
820 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000821 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000822 // name of the variable or function as the address value.
823 O << Mang->getMangledName(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000824 return;
825 }
826
827 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Dan Gohman6a55e622009-10-30 01:45:18 +0000828 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000829 return;
830 }
831
832 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
833 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000834 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000835 O << '0';
836 return;
837 }
838
839 switch (CE->getOpcode()) {
840 case Instruction::ZExt:
841 case Instruction::SExt:
842 case Instruction::FPTrunc:
843 case Instruction::FPExt:
844 case Instruction::UIToFP:
845 case Instruction::SIToFP:
846 case Instruction::FPToUI:
847 case Instruction::FPToSI:
848 default:
849 llvm_unreachable("FIXME: Don't support this constant cast expr");
850 case Instruction::GetElementPtr: {
851 // generate a symbolic expression for the byte address
852 const TargetData *TD = TM.getTargetData();
853 const Constant *ptrVal = CE->getOperand(0);
854 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
855 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
856 idxVec.size());
857 if (Offset == 0)
858 return EmitConstantValueOnly(ptrVal);
859
860 // Truncate/sext the offset to the pointer size.
861 if (TD->getPointerSizeInBits() != 64) {
862 int SExtAmount = 64-TD->getPointerSizeInBits();
863 Offset = (Offset << SExtAmount) >> SExtAmount;
864 }
865
866 if (Offset)
867 O << '(';
868 EmitConstantValueOnly(ptrVal);
869 if (Offset > 0)
870 O << ") + " << Offset;
871 else
872 O << ") - " << -Offset;
873 return;
874 }
875 case Instruction::BitCast:
876 return EmitConstantValueOnly(CE->getOperand(0));
877
878 case Instruction::IntToPtr: {
879 // Handle casts to pointers by changing them into casts to the appropriate
880 // integer type. This promotes constant folding and simplifies this code.
881 const TargetData *TD = TM.getTargetData();
882 Constant *Op = CE->getOperand(0);
883 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
884 false/*ZExt*/);
885 return EmitConstantValueOnly(Op);
886 }
887
888 case Instruction::PtrToInt: {
889 // Support only foldable casts to/from pointers that can be eliminated by
890 // changing the pointer to the appropriately sized integer type.
891 Constant *Op = CE->getOperand(0);
892 const Type *Ty = CE->getType();
893 const TargetData *TD = TM.getTargetData();
894
895 // We can emit the pointer value into this slot if the slot is an
896 // integer slot greater or equal to the size of the pointer.
897 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
898 return EmitConstantValueOnly(Op);
899
900 O << "((";
901 EmitConstantValueOnly(Op);
902 APInt ptrMask =
903 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
904
905 SmallString<40> S;
906 ptrMask.toStringUnsigned(S);
907 O << ") & " << S.str() << ')';
908 return;
909 }
910
911 case Instruction::Trunc:
912 // We emit the value and depend on the assembler to truncate the generated
913 // expression properly. This is important for differences between
914 // blockaddress labels. Since the two labels are in the same function, it
915 // is reasonable to treat their delta as a 32-bit value.
916 return EmitConstantValueOnly(CE->getOperand(0));
917
918 case Instruction::Add:
919 case Instruction::Sub:
920 case Instruction::And:
921 case Instruction::Or:
922 case Instruction::Xor:
923 O << '(';
924 EmitConstantValueOnly(CE->getOperand(0));
925 O << ')';
926 switch (CE->getOpcode()) {
927 case Instruction::Add:
928 O << " + ";
929 break;
930 case Instruction::Sub:
931 O << " - ";
932 break;
933 case Instruction::And:
934 O << " & ";
935 break;
936 case Instruction::Or:
937 O << " | ";
938 break;
939 case Instruction::Xor:
940 O << " ^ ";
941 break;
942 default:
943 break;
944 }
945 O << '(';
946 EmitConstantValueOnly(CE->getOperand(1));
947 O << ')';
948 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000949 }
950}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000951
Chris Lattner2980cef2005-11-10 18:06:33 +0000952/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000953/// the predicate isString is true.
954///
David Greene71847812009-07-14 20:18:05 +0000955static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000956 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000957 assert(CVA->isString() && "Array is not string compatible!");
958
Dan Gohmand19a53b2008-06-30 22:03:41 +0000959 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000960 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000961 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000962 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000963 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000964 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000965 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000966}
967
Jeff Cohenc884db42006-05-02 01:16:28 +0000968/// EmitString - Emit a zero-byte-terminated string constant.
969///
970void AsmPrinter::EmitString(const ConstantArray *CVA) const {
971 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000972 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000973 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000974 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000975 printAsCString(O, CVA, NumElts-1);
976 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000977 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000978 printAsCString(O, CVA, NumElts);
979 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000980 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000981}
982
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000983void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
984 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000985 if (CVA->isString()) {
986 EmitString(CVA);
987 } else { // Not a string. Print the values in successive locations
988 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000989 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000990 }
991}
992
993void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
994 const VectorType *PTy = CP->getType();
995
996 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
997 EmitGlobalConstant(CP->getOperand(I));
998}
999
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001000void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1001 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001002 // Print the fields in successive locations. Pad to align if needed!
1003 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001004 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001005 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1006 uint64_t sizeSoFar = 0;
1007 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1008 const Constant* field = CVS->getOperand(i);
1009
1010 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001011 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001012 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1013 - cvsLayout->getElementOffset(i)) - fieldSize;
1014 sizeSoFar += fieldSize + padSize;
1015
1016 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001017 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001018
1019 // Insert padding - this may include padding to increase the size of the
1020 // current field up to the ABI size (if the struct is not packed) as well
1021 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001022 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001023 }
1024 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1025 "Layout of constant struct may be incorrect!");
1026}
1027
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001028void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1029 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001030 // FP Constants are printed as integer constants to avoid losing
1031 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001032 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001033 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001034 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001035 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1036 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001037 if (MAI->getData64bitsDirective(AddrSpace)) {
1038 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001039 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001040 O.PadToColumn(MAI->getCommentColumn());
1041 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001042 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001043 O << '\n';
1044 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001045 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001046 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001047 O.PadToColumn(MAI->getCommentColumn());
1048 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001049 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001050 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001051 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001052 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001053 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001054 O.PadToColumn(MAI->getCommentColumn());
1055 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001056 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001057 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001058 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001059 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001060 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001061 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001062 O.PadToColumn(MAI->getCommentColumn());
1063 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001064 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001065 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001066 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001067 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001068 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001069 O.PadToColumn(MAI->getCommentColumn());
1070 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001071 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001072 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001073 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001074 }
1075 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001076 }
1077
1078 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001079 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001080 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001081 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001082 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001083 O.PadToColumn(MAI->getCommentColumn());
1084 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001085 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001086 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001087 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001088 }
1089
1090 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001091 // all long double variants are printed as hex
1092 // api needed to prevent premature destruction
1093 APInt api = CFP->getValueAPF().bitcastToAPInt();
1094 const uint64_t *p = api.getRawData();
1095 // Convert to double so we can print the approximate val as a comment.
1096 APFloat DoubleVal = CFP->getValueAPF();
1097 bool ignored;
1098 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1099 &ignored);
1100 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001101 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001102 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001103 O.PadToColumn(MAI->getCommentColumn());
1104 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001105 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001106 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001107 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001108 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001109 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001110 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001111 O.PadToColumn(MAI->getCommentColumn());
1112 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001113 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001114 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001115 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001116 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001117 O.PadToColumn(MAI->getCommentColumn());
1118 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001119 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001120 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001121 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001122 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001123 O.PadToColumn(MAI->getCommentColumn());
1124 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001125 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001126 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001127 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001128 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O.PadToColumn(MAI->getCommentColumn());
1130 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001131 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001132 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001133 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001134 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001135 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001136 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001137 O.PadToColumn(MAI->getCommentColumn());
1138 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001139 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001140 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001141 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001142 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001143 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001144 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001145 O.PadToColumn(MAI->getCommentColumn());
1146 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001147 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001148 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001149 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001150 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001151 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001152 O.PadToColumn(MAI->getCommentColumn());
1153 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001154 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001155 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001156 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001157 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001158 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001159 O.PadToColumn(MAI->getCommentColumn());
1160 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001161 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001162 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001163 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001164 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001165 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001166 O.PadToColumn(MAI->getCommentColumn());
1167 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001168 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001169 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001170 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001171 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001172 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1173 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001174 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001175 }
1176
1177 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001178 // all long double variants are printed as hex
1179 // api needed to prevent premature destruction
1180 APInt api = CFP->getValueAPF().bitcastToAPInt();
1181 const uint64_t *p = api.getRawData();
1182 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001183 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001184 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001185 O.PadToColumn(MAI->getCommentColumn());
1186 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001187 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001188 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001189 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001190 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001191 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001192 O.PadToColumn(MAI->getCommentColumn());
1193 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001194 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001195 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001196 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001197 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001198 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001199 O.PadToColumn(MAI->getCommentColumn());
1200 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001201 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001202 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001203 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001204 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001205 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001206 O.PadToColumn(MAI->getCommentColumn());
1207 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001208 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001209 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001210 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001211 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001212 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001213 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001214 O.PadToColumn(MAI->getCommentColumn());
1215 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001216 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001217 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001218 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001219 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001220 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001221 O.PadToColumn(MAI->getCommentColumn());
1222 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001223 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001224 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001225 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001226 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001227 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001228 O.PadToColumn(MAI->getCommentColumn());
1229 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001230 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001231 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001232 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001233 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001234 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001235 O.PadToColumn(MAI->getCommentColumn());
1236 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001237 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001238 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001239 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001240 }
1241 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001242 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001243}
1244
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001245void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1246 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001247 const TargetData *TD = TM.getTargetData();
1248 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001249 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001250
1251 // We don't expect assemblers to support integer data directives
1252 // for more than 64 bits, so we emit the data in at most 64-bit
1253 // quantities at a time.
1254 const uint64_t *RawData = CI->getValue().getRawData();
1255 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1256 uint64_t Val;
1257 if (TD->isBigEndian())
1258 Val = RawData[e - i - 1];
1259 else
1260 Val = RawData[i];
1261
Chris Lattner5979dff2010-01-13 04:38:16 +00001262 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001263 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001264 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001265 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001266
1267 // Emit two 32-bit chunks, order depends on endianness.
1268 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1269 const char *FirstName = " least", *SecondName = " most";
1270 if (TD->isBigEndian()) {
1271 std::swap(FirstChunk, SecondChunk);
1272 std::swap(FirstName, SecondName);
1273 }
1274
1275 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1276 if (VerboseAsm) {
1277 O.PadToColumn(MAI->getCommentColumn());
1278 O << MAI->getCommentString()
1279 << FirstName << " significant half of i64 " << Val;
1280 }
1281 O << '\n';
1282
1283 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1284 if (VerboseAsm) {
1285 O.PadToColumn(MAI->getCommentColumn());
1286 O << MAI->getCommentString()
1287 << SecondName << " significant half of i64 " << Val;
1288 }
1289 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001290 }
1291}
1292
Chris Lattner25045bd2005-11-21 07:51:36 +00001293/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001294void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001295 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001296 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001297 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001298
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001299 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001300 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001301 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001302 }
1303
1304 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001305 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001306 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001307 }
1308
1309 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001310 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001311 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001312 }
1313
1314 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001315 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001316 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001317 }
1318
1319 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1320 // If we can directly emit an 8-byte constant, do it.
1321 if (Size == 8)
1322 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1323 O << Data64Dir << CI->getZExtValue() << '\n';
1324 return;
1325 }
1326
Dan Gohman00d448a2008-12-22 21:14:27 +00001327 // Small integers are handled below; large integers are handled here.
1328 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001329 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001330 return;
1331 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001332 }
1333
1334 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001335 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001336 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001337 }
1338
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001339 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001340 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001341 if (VerboseAsm) {
1342 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1343 SmallString<40> S;
1344 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001345 O.PadToColumn(MAI->getCommentColumn());
1346 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001347 }
Scott Micheleefc8452008-06-03 15:39:51 +00001348 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001349 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001350}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001351
Chris Lattnerfad86b02008-08-17 07:19:36 +00001352void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001353 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001354 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001355}
1356
Chris Lattner3ce9b672006-09-26 23:59:50 +00001357/// PrintSpecial - Print information related to the specified machine instr
1358/// that is independent of the operand, and may be independent of the instr
1359/// itself. This can be useful for portably encoding the comment character
1360/// or other bits of target-specific knowledge into the asmstrings. The
1361/// syntax used is ${:comment}. Targets can override this to add support
1362/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001363void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001364 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001365 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001366 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001367 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001368 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001369 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001370 // Comparing the address of MI isn't sufficient, because machineinstrs may
1371 // be allocated to the same address across functions.
1372 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1373
Owen Andersonbd58edf2009-06-24 22:28:12 +00001374 // If this is a new LastFn instruction, bump the counter.
1375 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001376 ++Counter;
1377 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001378 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001379 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001380 O << Counter;
1381 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001382 std::string msg;
1383 raw_string_ostream Msg(msg);
1384 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001385 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001386 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001387 }
1388}
1389
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001390/// processDebugLoc - Processes the debug information of each machine
1391/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001392void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1393 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001394 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1395 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001396 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001397 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001398 if (DL.isUnknown())
1399 return;
1400 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1401 if (CurDLT.Scope == 0)
1402 return;
1403
1404 if (BeforePrintingInsn) {
1405 if (CurDLT != PrevDLT) {
1406 unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1407 CurDLT.Scope);
1408 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001409 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001410 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001411 PrevDLT = CurDLT;
1412 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001413 } else {
1414 // After printing instruction
1415 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001416 }
1417}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001418
Devang Patel53bb5c92009-11-10 23:06:00 +00001419
Chris Lattner0264d1a2006-01-27 02:10:10 +00001420/// printInlineAsm - This method formats and prints the specified machine
1421/// instruction that is an inline asm.
1422void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001423 unsigned NumOperands = MI->getNumOperands();
1424
1425 // Count the number of register definitions.
1426 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001427 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001428 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001429 assert(NumDefs != NumOperands-1 && "No asm string?");
1430
Dan Gohmand735b802008-10-03 15:45:36 +00001431 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001432
1433 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001434 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001435
Dan Gohman40c57862009-11-06 00:04:54 +00001436 O << '\t';
1437
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001438 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1439 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001440 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001441 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1442 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001443 return;
1444 }
1445
Chris Lattner33adcfb2009-08-22 21:43:10 +00001446 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001447
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001448 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001449 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001450
Chris Lattner66099132006-02-01 22:41:11 +00001451 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1452 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001453
Chris Lattner66099132006-02-01 22:41:11 +00001454 while (*LastEmitted) {
1455 switch (*LastEmitted) {
1456 default: {
1457 // Not a special case, emit the string section literally.
1458 const char *LiteralEnd = LastEmitted+1;
1459 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001460 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001461 ++LiteralEnd;
1462 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1463 O.write(LastEmitted, LiteralEnd-LastEmitted);
1464 LastEmitted = LiteralEnd;
1465 break;
1466 }
Chris Lattner1c059972006-05-05 21:47:05 +00001467 case '\n':
1468 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001469 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001470 break;
Chris Lattner66099132006-02-01 22:41:11 +00001471 case '$': {
1472 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001473 bool Done = true;
1474
1475 // Handle escapes.
1476 switch (*LastEmitted) {
1477 default: Done = false; break;
1478 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001479 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1480 O << '$';
1481 ++LastEmitted; // Consume second '$' character.
1482 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001483 case '(': // $( -> same as GCC's { character.
1484 ++LastEmitted; // Consume '(' character.
1485 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001486 llvm_report_error("Nested variants found in inline asm string: '"
1487 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001488 }
1489 CurVariant = 0; // We're in the first variant now.
1490 break;
1491 case '|':
1492 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001493 if (CurVariant == -1)
1494 O << '|'; // this is gcc's behavior for | outside a variant
1495 else
1496 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001497 break;
1498 case ')': // $) -> same as GCC's } char.
1499 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001500 if (CurVariant == -1)
1501 O << '}'; // this is gcc's behavior for } outside a variant
1502 else
1503 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001504 break;
Chris Lattner66099132006-02-01 22:41:11 +00001505 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001506 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001507
1508 bool HasCurlyBraces = false;
1509 if (*LastEmitted == '{') { // ${variable}
1510 ++LastEmitted; // Consume '{' character.
1511 HasCurlyBraces = true;
1512 }
1513
Chris Lattner3e0cc262009-03-10 05:37:13 +00001514 // If we have ${:foo}, then this is not a real operand reference, it is a
1515 // "magic" string reference, just like in .td files. Arrange to call
1516 // PrintSpecial.
1517 if (HasCurlyBraces && *LastEmitted == ':') {
1518 ++LastEmitted;
1519 const char *StrStart = LastEmitted;
1520 const char *StrEnd = strchr(StrStart, '}');
1521 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001522 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1523 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001524 }
1525
1526 std::string Val(StrStart, StrEnd);
1527 PrintSpecial(MI, Val.c_str());
1528 LastEmitted = StrEnd+1;
1529 break;
1530 }
1531
Chris Lattner66099132006-02-01 22:41:11 +00001532 const char *IDStart = LastEmitted;
1533 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001534 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001535 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1536 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001537 llvm_report_error("Bad $ operand number in inline asm string: '"
1538 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001539 }
1540 LastEmitted = IDEnd;
1541
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001542 char Modifier[2] = { 0, 0 };
1543
Chris Lattner66099132006-02-01 22:41:11 +00001544 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001545 // If we have curly braces, check for a modifier character. This
1546 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1547 if (*LastEmitted == ':') {
1548 ++LastEmitted; // Consume ':' character.
1549 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001550 llvm_report_error("Bad ${:} expression in inline asm string: '"
1551 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001552 }
1553
1554 Modifier[0] = *LastEmitted;
1555 ++LastEmitted; // Consume modifier character.
1556 }
1557
Chris Lattner66099132006-02-01 22:41:11 +00001558 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001559 llvm_report_error("Bad ${} expression in inline asm string: '"
1560 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001561 }
1562 ++LastEmitted; // Consume '}' character.
1563 }
1564
1565 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001566 llvm_report_error("Invalid $ operand number in inline asm string: '"
1567 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001568 }
1569
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001570 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001571 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001572 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1573 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001574
1575 bool Error = false;
1576
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001577 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001578 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001579 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001580 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001581 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001582 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001583
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001584 if (OpNo >= MI->getNumOperands()) {
1585 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001586 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001587 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001588 ++OpNo; // Skip over the ID number.
1589
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001590 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001591 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1592 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001593 else {
1594 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001595 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001596 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1597 Modifier[0] ? Modifier : 0);
1598 } else {
1599 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1600 Modifier[0] ? Modifier : 0);
1601 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001602 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001603 }
1604 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001605 std::string msg;
1606 raw_string_ostream Msg(msg);
1607 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001608 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001609 MI->print(Msg);
1610 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001611 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001612 }
Chris Lattner66099132006-02-01 22:41:11 +00001613 break;
1614 }
Chris Lattner66099132006-02-01 22:41:11 +00001615 }
1616 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001617 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001618}
1619
Evan Chengda47e6e2008-03-15 00:03:38 +00001620/// printImplicitDef - This method prints the specified machine instruction
1621/// that is an implicit def.
1622void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001623 if (!VerboseAsm) return;
1624 O.PadToColumn(MAI->getCommentColumn());
1625 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001626 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001627}
1628
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001629void AsmPrinter::printKill(const MachineInstr *MI) const {
1630 if (!VerboseAsm) return;
1631 O.PadToColumn(MAI->getCommentColumn());
1632 O << MAI->getCommentString() << " kill:";
1633 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1634 const MachineOperand &op = MI->getOperand(n);
1635 assert(op.isReg() && "KILL instruction must have only register operands");
1636 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1637 }
1638}
1639
Jim Laskey1ee29252007-01-26 14:34:52 +00001640/// printLabel - This method prints a local label used by debug and
1641/// exception handling tables.
1642void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001643 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001644}
1645
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001646void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001647 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001648}
1649
Chris Lattner66099132006-02-01 22:41:11 +00001650/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1651/// instruction, using the specified assembler variant. Targets should
1652/// overried this to format as appropriate.
1653bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001654 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001655 // Target doesn't support this yet!
1656 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001657}
Chris Lattnerdd260332006-02-24 20:21:58 +00001658
1659bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1660 unsigned AsmVariant,
1661 const char *ExtraCode) {
1662 // Target doesn't support this yet!
1663 return true;
1664}
Nate Begeman37efe672006-04-22 18:53:45 +00001665
Dan Gohman29cbade2009-11-20 23:18:13 +00001666MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1667 const char *Suffix) const {
1668 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001669}
1670
1671MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001672 const BasicBlock *BB,
1673 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001674 assert(BB->hasName() &&
1675 "Address of anonymous basic block not supported yet!");
1676
Dan Gohman568a3be2009-11-05 23:14:35 +00001677 // This code must use the function name itself, and not the function number,
1678 // since it must be possible to generate the label name from within other
1679 // functions.
1680 std::string FuncName = Mang->getMangledName(F);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001681
Dan Gohman568a3be2009-11-05 23:14:35 +00001682 SmallString<60> Name;
1683 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BA"
1684 << FuncName.size() << '_' << FuncName << '_'
Dan Gohman29cbade2009-11-20 23:18:13 +00001685 << Mang->makeNameProper(BB->getName())
1686 << Suffix;
Dan Gohman568a3be2009-11-05 23:14:35 +00001687
1688 return OutContext.GetOrCreateSymbol(Name.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001689}
1690
Chris Lattner7cb384d2009-09-12 23:02:08 +00001691MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1692 SmallString<60> Name;
1693 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1694 << getFunctionNumber() << '_' << MBBID;
1695
1696 return OutContext.GetOrCreateSymbol(Name.str());
1697}
1698
1699
Chris Lattner70a54c02009-09-13 18:25:37 +00001700/// EmitBasicBlockStart - This method prints the label for the specified
1701/// MachineBasicBlock, an alignment (if present) and a comment describing
1702/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001703void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001704 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001705 if (unsigned Align = MBB->getAlignment())
1706 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001707
Dan Gohmanb1cac332009-10-30 01:34:35 +00001708 // If the block has its address taken, emit a special label to satisfy
1709 // references to the block. This is done so that we don't need to
1710 // remember the number of this label, and so that we can make
1711 // forward references to labels without knowing what their numbers
1712 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001713 if (MBB->hasAddressTaken()) {
1714 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1715 MBB->getBasicBlock())->print(O, MAI);
1716 O << ':';
1717 if (VerboseAsm) {
1718 O.PadToColumn(MAI->getCommentColumn());
1719 O << MAI->getCommentString() << " Address Taken";
1720 }
1721 O << '\n';
1722 }
1723
Dan Gohmanb1cac332009-10-30 01:34:35 +00001724 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001725 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1726 if (VerboseAsm)
1727 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1728 } else {
1729 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1730 O << ':';
1731 if (!VerboseAsm)
1732 O << '\n';
1733 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001734
Dan Gohmanb1cac332009-10-30 01:34:35 +00001735 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001736 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001737 if (const BasicBlock *BB = MBB->getBasicBlock())
1738 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001739 O.PadToColumn(MAI->getCommentColumn());
1740 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001741 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001742 }
David Greeneb71d1b22009-08-10 16:38:07 +00001743
Chris Lattner70a54c02009-09-13 18:25:37 +00001744 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001745 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001746 }
Nate Begeman37efe672006-04-22 18:53:45 +00001747}
Nate Begeman52a51e382006-08-12 21:29:52 +00001748
Evan Chengcc415862007-11-09 01:32:10 +00001749/// printPICJumpTableSetLabel - This method prints a set label for the
1750/// specified MachineBasicBlock for a jumptable entry.
1751void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1752 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001753 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001754 return;
1755
Chris Lattner33adcfb2009-08-22 21:43:10 +00001756 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001757 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001758 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001759 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001760 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001761}
Evan Chengd6594ae2006-09-12 21:00:35 +00001762
Evan Chengcc415862007-11-09 01:32:10 +00001763void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1764 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001765 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001766 return;
1767
Chris Lattner33adcfb2009-08-22 21:43:10 +00001768 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001769 << getFunctionNumber() << '_' << uid << '_' << uid2
1770 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001771 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001772 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001773 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001774}
1775
Evan Chengd6594ae2006-09-12 21:00:35 +00001776/// printDataDirective - This method prints the asm directive for the
1777/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001778void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001779 const TargetData *TD = TM.getTargetData();
1780 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001781 case Type::FloatTyID: case Type::DoubleTyID:
1782 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1783 assert(0 && "Should have already output floating point constant.");
1784 default:
1785 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001786 case Type::IntegerTyID: {
1787 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1788 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001789 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001790 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001791 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001792 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001793 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001794 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001795 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001796 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001797 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001798 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001799 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001800 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001801 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001802 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001803 case Type::PointerTyID:
1804 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001805 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001806 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001807 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001808 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001809 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001810 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001811 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001812 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001813 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001814 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001815 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001816 }
1817}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001818
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001819void AsmPrinter::printVisibility(const std::string& Name,
1820 unsigned Visibility) const {
1821 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001822 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001823 O << Directive << Name << '\n';
1824 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001825 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001826 O << Directive << Name << '\n';
1827 }
1828}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001829
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001830void AsmPrinter::printOffset(int64_t Offset) const {
1831 if (Offset > 0)
1832 O << '+' << Offset;
1833 else if (Offset < 0)
1834 O << Offset;
1835}
1836
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001837GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1838 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001839 return 0;
1840
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001841 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001842 if (GCPI != GCMetadataPrinters.end())
1843 return GCPI->second;
1844
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001845 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001846
1847 for (GCMetadataPrinterRegistry::iterator
1848 I = GCMetadataPrinterRegistry::begin(),
1849 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1850 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001851 GCMetadataPrinter *GMP = I->instantiate();
1852 GMP->S = S;
1853 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1854 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001855 }
1856
Chris Lattner103289e2009-08-23 07:19:13 +00001857 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001858 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001859}
David Greene014700c2009-07-13 20:25:48 +00001860
1861/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001862void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001863 if (!VerboseAsm)
1864 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001865
David Greene1924aab2009-11-13 21:34:57 +00001866 bool Newline = false;
1867
1868 if (!MI.getDebugLoc().isUnknown()) {
1869 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1870
1871 // Print source line info.
1872 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001873 O << MAI->getCommentString() << ' ';
1874 DIScope Scope(DLT.Scope);
1875 // Omit the directory, because it's likely to be long and uninteresting.
1876 if (!Scope.isNull())
1877 O << Scope.getFilename();
1878 else
1879 O << "<unknown>";
1880 O << ':' << DLT.Line;
David Greene1924aab2009-11-13 21:34:57 +00001881 if (DLT.Col != 0)
Dan Gohman3b9bc042009-12-05 00:23:29 +00001882 O << ':' << DLT.Col;
David Greene1924aab2009-11-13 21:34:57 +00001883 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001884 }
David Greene1924aab2009-11-13 21:34:57 +00001885
David Greeneddff9412009-11-16 15:12:23 +00001886 // Check for spills and reloads
1887 int FI;
1888
1889 const MachineFrameInfo *FrameInfo =
1890 MI.getParent()->getParent()->getFrameInfo();
1891
1892 // We assume a single instruction only has a spill or reload, not
1893 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001894 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001895 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1896 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001897 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001898 if (Newline) O << '\n';
1899 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001900 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001901 Newline = true;
1902 }
1903 }
David Greenef7ea2a52009-12-04 22:46:04 +00001904 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001905 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1906 if (Newline) O << '\n';
1907 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001908 O << MAI->getCommentString() << ' '
1909 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001910 Newline = true;
1911 }
1912 }
1913 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1914 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001915 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001916 if (Newline) O << '\n';
1917 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001918 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001919 Newline = true;
1920 }
1921 }
David Greenef7ea2a52009-12-04 22:46:04 +00001922 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001923 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1924 if (Newline) O << '\n';
1925 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001926 O << MAI->getCommentString() << ' '
1927 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001928 Newline = true;
1929 }
1930 }
1931
1932 // Check for spill-induced copies
1933 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1934 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1935 SrcSubIdx, DstSubIdx)) {
1936 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1937 if (Newline) O << '\n';
1938 O.PadToColumn(MAI->getCommentColumn());
1939 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001940 }
1941 }
David Greene014700c2009-07-13 20:25:48 +00001942}
1943
David Greenefe37ab32009-08-18 19:22:55 +00001944/// PrintChildLoopComment - Print comments about child loops within
1945/// the loop for this basic block, with nesting.
1946///
1947static void PrintChildLoopComment(formatted_raw_ostream &O,
1948 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001949 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001950 int FunctionNumber) {
1951 // Add child loop information
1952 for(MachineLoop::iterator cl = loop->begin(),
1953 clend = loop->end();
1954 cl != clend;
1955 ++cl) {
1956 MachineBasicBlock *Header = (*cl)->getHeader();
1957 assert(Header && "No header for loop");
1958
1959 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001960 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001961
Chris Lattner33adcfb2009-08-22 21:43:10 +00001962 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001963 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001964 << " Child Loop BB" << FunctionNumber << "_"
1965 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1966
Chris Lattner33adcfb2009-08-22 21:43:10 +00001967 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001968 }
1969}
1970
David Greeneb71d1b22009-08-10 16:38:07 +00001971/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00001972void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00001973 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001974 // Add loop depth information
1975 const MachineLoop *loop = LI->getLoopFor(&MBB);
1976
1977 if (loop) {
1978 // Print a newline after bb# annotation.
1979 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001980 O.PadToColumn(MAI->getCommentColumn());
1981 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001982 << '\n';
1983
Chris Lattner33adcfb2009-08-22 21:43:10 +00001984 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001985
1986 MachineBasicBlock *Header = loop->getHeader();
1987 assert(Header && "No header for loop");
1988
1989 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001990 O << MAI->getCommentString() << " Loop Header";
1991 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001992 }
1993 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001994 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001995 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1996 }
1997
1998 if (loop->empty()) {
1999 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002000 O.PadToColumn(MAI->getCommentColumn());
2001 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002002 }
2003
2004 // Add parent loop information
2005 for (const MachineLoop *CurLoop = loop->getParentLoop();
2006 CurLoop;
2007 CurLoop = CurLoop->getParentLoop()) {
2008 MachineBasicBlock *Header = CurLoop->getHeader();
2009 assert(Header && "No header for loop");
2010
2011 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002012 O.PadToColumn(MAI->getCommentColumn());
2013 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002014 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002015 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002016 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2017 }
2018 }
2019 }
2020}