blob: 44fd176a79e55c20cd85ec6ef21961fe7d96e35e [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;
Chris Lattnera87dea42009-07-31 18:48:30 +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 Lattnerbd1d3822004-10-16 18:19:26 +0000810 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000811 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000812 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000813 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000814 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000815 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000816 // name of the variable or function as the address value.
817 O << Mang->getMangledName(GV);
Duraid Madina855a5192005-04-02 12:21:51 +0000818 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000819 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000820 unsigned Opcode = CE->getOpcode();
821 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000822 case Instruction::Trunc:
823 case Instruction::ZExt:
824 case Instruction::SExt:
825 case Instruction::FPTrunc:
826 case Instruction::FPExt:
827 case Instruction::UIToFP:
828 case Instruction::SIToFP:
829 case Instruction::FPToUI:
830 case Instruction::FPToSI:
831 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000832 case Instruction::GetElementPtr: {
833 // generate a symbolic expression for the byte address
834 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000835 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
836 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
837 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000838 // Truncate/sext the offset to the pointer size.
839 if (TD->getPointerSizeInBits() != 64) {
840 int SExtAmount = 64-TD->getPointerSizeInBits();
841 Offset = (Offset << SExtAmount) >> SExtAmount;
842 }
843
Chris Lattner27e19212005-02-14 21:40:26 +0000844 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000846 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000847 if (Offset > 0)
848 O << ") + " << Offset;
849 else if (Offset < 0)
850 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000851 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000852 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000853 }
854 break;
855 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000856 case Instruction::BitCast:
857 return EmitConstantValueOnly(CE->getOperand(0));
858
Chris Lattnerddc94012006-12-12 05:18:19 +0000859 case Instruction::IntToPtr: {
860 // Handle casts to pointers by changing them into casts to the appropriate
861 // integer type. This promotes constant folding and simplifies this code.
862 Constant *Op = CE->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000863 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
864 false/*ZExt*/);
Chris Lattnerddc94012006-12-12 05:18:19 +0000865 return EmitConstantValueOnly(Op);
866 }
867
868
869 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000870 // Support only foldable casts to/from pointers that can be eliminated by
871 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000872 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000873 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000874
Chris Lattnerddc94012006-12-12 05:18:19 +0000875 // We can emit the pointer value into this slot if the slot is an
876 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000877 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000878 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000879
880 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000881 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000882 APInt ptrMask =
883 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000884
885 SmallString<40> S;
886 ptrMask.toStringUnsigned(S);
Daniel Dunbardddfd342009-08-19 20:07:03 +0000887 O << ") & " << S.str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000888 break;
889 }
890 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000891 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000892 case Instruction::And:
893 case Instruction::Or:
894 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000895 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000896 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000897 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000898 switch (Opcode) {
899 case Instruction::Add:
900 O << " + ";
901 break;
902 case Instruction::Sub:
903 O << " - ";
904 break;
905 case Instruction::And:
906 O << " & ";
907 break;
908 case Instruction::Or:
909 O << " | ";
910 break;
911 case Instruction::Xor:
912 O << " ^ ";
913 break;
914 default:
915 break;
916 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000917 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000918 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000919 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000920 break;
921 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000922 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000923 }
Dan Gohman6a55e622009-10-30 01:45:18 +0000924 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
925 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnera80ba712004-08-16 23:15:22 +0000926 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000927 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000928 }
929}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000930
Chris Lattner2980cef2005-11-10 18:06:33 +0000931/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000932/// the predicate isString is true.
933///
David Greene71847812009-07-14 20:18:05 +0000934static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000935 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000936 assert(CVA->isString() && "Array is not string compatible!");
937
Dan Gohmand19a53b2008-06-30 22:03:41 +0000938 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000939 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000940 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000941 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000942 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000943 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000944 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000945}
946
Jeff Cohenc884db42006-05-02 01:16:28 +0000947/// EmitString - Emit a zero-byte-terminated string constant.
948///
949void AsmPrinter::EmitString(const ConstantArray *CVA) const {
950 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000951 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000952 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000953 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000954 printAsCString(O, CVA, NumElts-1);
955 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000956 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000957 printAsCString(O, CVA, NumElts);
958 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000959 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000960}
961
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000962void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
963 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000964 if (CVA->isString()) {
965 EmitString(CVA);
966 } else { // Not a string. Print the values in successive locations
967 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000968 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000969 }
970}
971
972void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
973 const VectorType *PTy = CP->getType();
974
975 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
976 EmitGlobalConstant(CP->getOperand(I));
977}
978
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000979void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
980 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000981 // Print the fields in successive locations. Pad to align if needed!
982 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000983 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000984 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
985 uint64_t sizeSoFar = 0;
986 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
987 const Constant* field = CVS->getOperand(i);
988
989 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +0000990 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000991 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
992 - cvsLayout->getElementOffset(i)) - fieldSize;
993 sizeSoFar += fieldSize + padSize;
994
995 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000996 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000997
998 // Insert padding - this may include padding to increase the size of the
999 // current field up to the ABI size (if the struct is not packed) as well
1000 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001001 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001002 }
1003 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1004 "Layout of constant struct may be incorrect!");
1005}
1006
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001007void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1008 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001009 // FP Constants are printed as integer constants to avoid losing
1010 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001011 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001012 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001013 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001014 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1015 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001016 if (MAI->getData64bitsDirective(AddrSpace)) {
1017 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001018 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001019 O.PadToColumn(MAI->getCommentColumn());
1020 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001021 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001022 O << '\n';
1023 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001024 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001025 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001026 O.PadToColumn(MAI->getCommentColumn());
1027 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001028 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001029 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001030 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001031 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001032 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001033 O.PadToColumn(MAI->getCommentColumn());
1034 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001035 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001036 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001037 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001038 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001039 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001040 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001041 O.PadToColumn(MAI->getCommentColumn());
1042 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001043 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001044 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001045 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001046 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001047 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001048 O.PadToColumn(MAI->getCommentColumn());
1049 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001050 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001051 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001052 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001053 }
1054 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001055 }
1056
1057 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001058 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001059 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001060 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
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() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001064 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001065 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001066 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001067 }
1068
1069 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001070 // all long double variants are printed as hex
1071 // api needed to prevent premature destruction
1072 APInt api = CFP->getValueAPF().bitcastToAPInt();
1073 const uint64_t *p = api.getRawData();
1074 // Convert to double so we can print the approximate val as a comment.
1075 APFloat DoubleVal = CFP->getValueAPF();
1076 bool ignored;
1077 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1078 &ignored);
1079 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001080 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001081 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001082 O.PadToColumn(MAI->getCommentColumn());
1083 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001084 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001085 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001086 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001087 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001088 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001089 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001090 O.PadToColumn(MAI->getCommentColumn());
1091 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001092 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001093 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001094 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001095 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001096 O.PadToColumn(MAI->getCommentColumn());
1097 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001098 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001099 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001100 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001101 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001102 O.PadToColumn(MAI->getCommentColumn());
1103 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001104 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001105 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001106 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001107 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001108 O.PadToColumn(MAI->getCommentColumn());
1109 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001110 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001111 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001112 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001113 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001114 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001115 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001116 O.PadToColumn(MAI->getCommentColumn());
1117 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001118 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001119 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001120 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001121 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001122 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001124 O.PadToColumn(MAI->getCommentColumn());
1125 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001126 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001127 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001128 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001130 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001131 O.PadToColumn(MAI->getCommentColumn());
1132 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001133 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001134 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001135 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001136 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001137 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001138 O.PadToColumn(MAI->getCommentColumn());
1139 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001140 << " next halfword";
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[1]);
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 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001148 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001149 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001150 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001151 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1152 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001153 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001154 }
1155
1156 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001157 // all long double variants are printed as hex
1158 // api needed to prevent premature destruction
1159 APInt api = CFP->getValueAPF().bitcastToAPInt();
1160 const uint64_t *p = api.getRawData();
1161 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001162 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001163 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001164 O.PadToColumn(MAI->getCommentColumn());
1165 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001166 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001167 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001168 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001169 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001170 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001171 O.PadToColumn(MAI->getCommentColumn());
1172 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001173 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001174 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001175 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001176 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001177 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001178 O.PadToColumn(MAI->getCommentColumn());
1179 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001180 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001181 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001182 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001183 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
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 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001188 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001189 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001190 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001192 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001193 O.PadToColumn(MAI->getCommentColumn());
1194 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001195 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001196 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001197 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001198 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001199 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001200 O.PadToColumn(MAI->getCommentColumn());
1201 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001202 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001203 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001204 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001205 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001206 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001207 O.PadToColumn(MAI->getCommentColumn());
1208 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001209 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001210 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001211 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001212 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
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 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001217 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001218 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001219 }
1220 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001221 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001222}
1223
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001224void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1225 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001226 const TargetData *TD = TM.getTargetData();
1227 unsigned BitWidth = CI->getBitWidth();
1228 assert(isPowerOf2_32(BitWidth) &&
1229 "Non-power-of-2-sized integers not handled!");
1230
1231 // We don't expect assemblers to support integer data directives
1232 // for more than 64 bits, so we emit the data in at most 64-bit
1233 // quantities at a time.
1234 const uint64_t *RawData = CI->getValue().getRawData();
1235 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1236 uint64_t Val;
1237 if (TD->isBigEndian())
1238 Val = RawData[e - i - 1];
1239 else
1240 Val = RawData[i];
1241
Chris Lattner33adcfb2009-08-22 21:43:10 +00001242 if (MAI->getData64bitsDirective(AddrSpace))
1243 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001244 else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001245 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001246 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001247 O.PadToColumn(MAI->getCommentColumn());
1248 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001249 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001250 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001251 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001252 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001253 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001254 O.PadToColumn(MAI->getCommentColumn());
1255 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001256 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001257 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001258 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001259 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001260 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001261 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001262 O.PadToColumn(MAI->getCommentColumn());
1263 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001264 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001265 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001266 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001267 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001268 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001269 O.PadToColumn(MAI->getCommentColumn());
1270 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001271 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001272 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001273 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001274 }
1275 }
1276}
1277
Chris Lattner25045bd2005-11-21 07:51:36 +00001278/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001279void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001280 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001281 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001282 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001283
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001284 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001285 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001286 return;
1287 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001288 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001289 return;
1290 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001291 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001292 return;
1293 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001294 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001295 return;
1296 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1297 // Small integers are handled below; large integers are handled here.
1298 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001299 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001300 return;
1301 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001302 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001303 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001304 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001305 }
1306
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001307 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001308 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001309 if (VerboseAsm) {
1310 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1311 SmallString<40> S;
1312 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001313 O.PadToColumn(MAI->getCommentColumn());
1314 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001315 }
Scott Micheleefc8452008-06-03 15:39:51 +00001316 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001317 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001318}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001319
Chris Lattnerfad86b02008-08-17 07:19:36 +00001320void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001321 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001322 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001323}
1324
Chris Lattner3ce9b672006-09-26 23:59:50 +00001325/// PrintSpecial - Print information related to the specified machine instr
1326/// that is independent of the operand, and may be independent of the instr
1327/// itself. This can be useful for portably encoding the comment character
1328/// or other bits of target-specific knowledge into the asmstrings. The
1329/// syntax used is ${:comment}. Targets can override this to add support
1330/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001331void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001332 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001333 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001334 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001335 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001336 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001337 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001338 // Comparing the address of MI isn't sufficient, because machineinstrs may
1339 // be allocated to the same address across functions.
1340 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1341
Owen Andersonbd58edf2009-06-24 22:28:12 +00001342 // If this is a new LastFn instruction, bump the counter.
1343 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001344 ++Counter;
1345 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001346 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001347 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001348 O << Counter;
1349 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001350 std::string msg;
1351 raw_string_ostream Msg(msg);
1352 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001353 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001354 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001355 }
1356}
1357
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001358/// processDebugLoc - Processes the debug information of each machine
1359/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001360void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1361 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001362 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1363 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001364 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001365 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001366 if (DL.isUnknown())
1367 return;
1368 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1369 if (CurDLT.Scope == 0)
1370 return;
1371
1372 if (BeforePrintingInsn) {
1373 if (CurDLT != PrevDLT) {
1374 unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1375 CurDLT.Scope);
1376 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001377 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001378 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001379 PrevDLT = CurDLT;
1380 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001381 } else {
1382 // After printing instruction
1383 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001384 }
1385}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001386
Devang Patel53bb5c92009-11-10 23:06:00 +00001387
Chris Lattner0264d1a2006-01-27 02:10:10 +00001388/// printInlineAsm - This method formats and prints the specified machine
1389/// instruction that is an inline asm.
1390void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001391 unsigned NumOperands = MI->getNumOperands();
1392
1393 // Count the number of register definitions.
1394 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001395 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001396 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001397 assert(NumDefs != NumOperands-1 && "No asm string?");
1398
Dan Gohmand735b802008-10-03 15:45:36 +00001399 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001400
1401 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001402 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001403
Dan Gohman40c57862009-11-06 00:04:54 +00001404 O << '\t';
1405
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001406 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1407 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001408 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001409 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1410 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001411 return;
1412 }
1413
Chris Lattner33adcfb2009-08-22 21:43:10 +00001414 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001415
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001416 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001417 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001418
Chris Lattner66099132006-02-01 22:41:11 +00001419 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1420 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001421
Chris Lattner66099132006-02-01 22:41:11 +00001422 while (*LastEmitted) {
1423 switch (*LastEmitted) {
1424 default: {
1425 // Not a special case, emit the string section literally.
1426 const char *LiteralEnd = LastEmitted+1;
1427 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001428 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001429 ++LiteralEnd;
1430 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1431 O.write(LastEmitted, LiteralEnd-LastEmitted);
1432 LastEmitted = LiteralEnd;
1433 break;
1434 }
Chris Lattner1c059972006-05-05 21:47:05 +00001435 case '\n':
1436 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001437 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001438 break;
Chris Lattner66099132006-02-01 22:41:11 +00001439 case '$': {
1440 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001441 bool Done = true;
1442
1443 // Handle escapes.
1444 switch (*LastEmitted) {
1445 default: Done = false; break;
1446 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001447 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1448 O << '$';
1449 ++LastEmitted; // Consume second '$' character.
1450 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001451 case '(': // $( -> same as GCC's { character.
1452 ++LastEmitted; // Consume '(' character.
1453 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001454 llvm_report_error("Nested variants found in inline asm string: '"
1455 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001456 }
1457 CurVariant = 0; // We're in the first variant now.
1458 break;
1459 case '|':
1460 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001461 if (CurVariant == -1)
1462 O << '|'; // this is gcc's behavior for | outside a variant
1463 else
1464 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001465 break;
1466 case ')': // $) -> same as GCC's } char.
1467 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001468 if (CurVariant == -1)
1469 O << '}'; // this is gcc's behavior for } outside a variant
1470 else
1471 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001472 break;
Chris Lattner66099132006-02-01 22:41:11 +00001473 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001474 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001475
1476 bool HasCurlyBraces = false;
1477 if (*LastEmitted == '{') { // ${variable}
1478 ++LastEmitted; // Consume '{' character.
1479 HasCurlyBraces = true;
1480 }
1481
Chris Lattner3e0cc262009-03-10 05:37:13 +00001482 // If we have ${:foo}, then this is not a real operand reference, it is a
1483 // "magic" string reference, just like in .td files. Arrange to call
1484 // PrintSpecial.
1485 if (HasCurlyBraces && *LastEmitted == ':') {
1486 ++LastEmitted;
1487 const char *StrStart = LastEmitted;
1488 const char *StrEnd = strchr(StrStart, '}');
1489 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001490 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1491 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001492 }
1493
1494 std::string Val(StrStart, StrEnd);
1495 PrintSpecial(MI, Val.c_str());
1496 LastEmitted = StrEnd+1;
1497 break;
1498 }
1499
Chris Lattner66099132006-02-01 22:41:11 +00001500 const char *IDStart = LastEmitted;
1501 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001502 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001503 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1504 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001505 llvm_report_error("Bad $ operand number in inline asm string: '"
1506 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001507 }
1508 LastEmitted = IDEnd;
1509
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001510 char Modifier[2] = { 0, 0 };
1511
Chris Lattner66099132006-02-01 22:41:11 +00001512 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001513 // If we have curly braces, check for a modifier character. This
1514 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1515 if (*LastEmitted == ':') {
1516 ++LastEmitted; // Consume ':' character.
1517 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001518 llvm_report_error("Bad ${:} expression in inline asm string: '"
1519 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001520 }
1521
1522 Modifier[0] = *LastEmitted;
1523 ++LastEmitted; // Consume modifier character.
1524 }
1525
Chris Lattner66099132006-02-01 22:41:11 +00001526 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001527 llvm_report_error("Bad ${} expression in inline asm string: '"
1528 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001529 }
1530 ++LastEmitted; // Consume '}' character.
1531 }
1532
1533 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001534 llvm_report_error("Invalid $ operand number in inline asm string: '"
1535 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001536 }
1537
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001538 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001539 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001540 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1541 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001542
1543 bool Error = false;
1544
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001545 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001546 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001547 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001548 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001549 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001550 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001551
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001552 if (OpNo >= MI->getNumOperands()) {
1553 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001554 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001555 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001556 ++OpNo; // Skip over the ID number.
1557
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001558 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001559 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1560 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001561 else {
1562 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001563 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001564 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1565 Modifier[0] ? Modifier : 0);
1566 } else {
1567 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1568 Modifier[0] ? Modifier : 0);
1569 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001570 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001571 }
1572 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001573 std::string msg;
1574 raw_string_ostream Msg(msg);
1575 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001576 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001577 MI->print(Msg);
1578 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001579 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001580 }
Chris Lattner66099132006-02-01 22:41:11 +00001581 break;
1582 }
Chris Lattner66099132006-02-01 22:41:11 +00001583 }
1584 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001585 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001586}
1587
Evan Chengda47e6e2008-03-15 00:03:38 +00001588/// printImplicitDef - This method prints the specified machine instruction
1589/// that is an implicit def.
1590void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001591 if (!VerboseAsm) return;
1592 O.PadToColumn(MAI->getCommentColumn());
1593 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001594 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001595}
1596
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001597void AsmPrinter::printKill(const MachineInstr *MI) const {
1598 if (!VerboseAsm) return;
1599 O.PadToColumn(MAI->getCommentColumn());
1600 O << MAI->getCommentString() << " kill:";
1601 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1602 const MachineOperand &op = MI->getOperand(n);
1603 assert(op.isReg() && "KILL instruction must have only register operands");
1604 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1605 }
1606}
1607
Jim Laskey1ee29252007-01-26 14:34:52 +00001608/// printLabel - This method prints a local label used by debug and
1609/// exception handling tables.
1610void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001611 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001612}
1613
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001614void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001615 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001616}
1617
Chris Lattner66099132006-02-01 22:41:11 +00001618/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1619/// instruction, using the specified assembler variant. Targets should
1620/// overried this to format as appropriate.
1621bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001622 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001623 // Target doesn't support this yet!
1624 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001625}
Chris Lattnerdd260332006-02-24 20:21:58 +00001626
1627bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1628 unsigned AsmVariant,
1629 const char *ExtraCode) {
1630 // Target doesn't support this yet!
1631 return true;
1632}
Nate Begeman37efe672006-04-22 18:53:45 +00001633
Dan Gohman29cbade2009-11-20 23:18:13 +00001634MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1635 const char *Suffix) const {
1636 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001637}
1638
1639MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001640 const BasicBlock *BB,
1641 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001642 assert(BB->hasName() &&
1643 "Address of anonymous basic block not supported yet!");
1644
Dan Gohman568a3be2009-11-05 23:14:35 +00001645 // This code must use the function name itself, and not the function number,
1646 // since it must be possible to generate the label name from within other
1647 // functions.
1648 std::string FuncName = Mang->getMangledName(F);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001649
Dan Gohman568a3be2009-11-05 23:14:35 +00001650 SmallString<60> Name;
1651 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BA"
1652 << FuncName.size() << '_' << FuncName << '_'
Dan Gohman29cbade2009-11-20 23:18:13 +00001653 << Mang->makeNameProper(BB->getName())
1654 << Suffix;
Dan Gohman568a3be2009-11-05 23:14:35 +00001655
1656 return OutContext.GetOrCreateSymbol(Name.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001657}
1658
Chris Lattner7cb384d2009-09-12 23:02:08 +00001659MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1660 SmallString<60> Name;
1661 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1662 << getFunctionNumber() << '_' << MBBID;
1663
1664 return OutContext.GetOrCreateSymbol(Name.str());
1665}
1666
1667
Chris Lattner70a54c02009-09-13 18:25:37 +00001668/// EmitBasicBlockStart - This method prints the label for the specified
1669/// MachineBasicBlock, an alignment (if present) and a comment describing
1670/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001671void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001672 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001673 if (unsigned Align = MBB->getAlignment())
1674 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001675
Dan Gohmanb1cac332009-10-30 01:34:35 +00001676 // If the block has its address taken, emit a special label to satisfy
1677 // references to the block. This is done so that we don't need to
1678 // remember the number of this label, and so that we can make
1679 // forward references to labels without knowing what their numbers
1680 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001681 if (MBB->hasAddressTaken()) {
1682 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1683 MBB->getBasicBlock())->print(O, MAI);
1684 O << ':';
1685 if (VerboseAsm) {
1686 O.PadToColumn(MAI->getCommentColumn());
1687 O << MAI->getCommentString() << " Address Taken";
1688 }
1689 O << '\n';
1690 }
1691
Dan Gohmanb1cac332009-10-30 01:34:35 +00001692 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001693 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1694 if (VerboseAsm)
1695 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1696 } else {
1697 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1698 O << ':';
1699 if (!VerboseAsm)
1700 O << '\n';
1701 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001702
Dan Gohmanb1cac332009-10-30 01:34:35 +00001703 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001704 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001705 if (const BasicBlock *BB = MBB->getBasicBlock())
1706 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001707 O.PadToColumn(MAI->getCommentColumn());
1708 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001709 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001710 }
David Greeneb71d1b22009-08-10 16:38:07 +00001711
Chris Lattner70a54c02009-09-13 18:25:37 +00001712 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001713 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001714 }
Nate Begeman37efe672006-04-22 18:53:45 +00001715}
Nate Begeman52a51e382006-08-12 21:29:52 +00001716
Evan Chengcc415862007-11-09 01:32:10 +00001717/// printPICJumpTableSetLabel - This method prints a set label for the
1718/// specified MachineBasicBlock for a jumptable entry.
1719void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1720 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001721 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001722 return;
1723
Chris Lattner33adcfb2009-08-22 21:43:10 +00001724 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001725 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001726 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001727 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001728 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001729}
Evan Chengd6594ae2006-09-12 21:00:35 +00001730
Evan Chengcc415862007-11-09 01:32:10 +00001731void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1732 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001733 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001734 return;
1735
Chris Lattner33adcfb2009-08-22 21:43:10 +00001736 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001737 << getFunctionNumber() << '_' << uid << '_' << uid2
1738 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001739 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001740 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001741 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001742}
1743
Evan Chengd6594ae2006-09-12 21:00:35 +00001744/// printDataDirective - This method prints the asm directive for the
1745/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001746void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001747 const TargetData *TD = TM.getTargetData();
1748 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001749 case Type::FloatTyID: case Type::DoubleTyID:
1750 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1751 assert(0 && "Should have already output floating point constant.");
1752 default:
1753 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001754 case Type::IntegerTyID: {
1755 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1756 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001757 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001758 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001759 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001760 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001761 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001762 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001763 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001764 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001765 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001766 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001767 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001768 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001769 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001770 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001771 case Type::PointerTyID:
1772 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001773 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001774 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001775 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001776 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001777 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001778 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001779 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001780 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001781 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001782 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001783 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001784 }
1785}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001786
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001787void AsmPrinter::printVisibility(const std::string& Name,
1788 unsigned Visibility) const {
1789 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001790 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001791 O << Directive << Name << '\n';
1792 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001793 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001794 O << Directive << Name << '\n';
1795 }
1796}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001797
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001798void AsmPrinter::printOffset(int64_t Offset) const {
1799 if (Offset > 0)
1800 O << '+' << Offset;
1801 else if (Offset < 0)
1802 O << Offset;
1803}
1804
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001805GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1806 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001807 return 0;
1808
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001809 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001810 if (GCPI != GCMetadataPrinters.end())
1811 return GCPI->second;
1812
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001813 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001814
1815 for (GCMetadataPrinterRegistry::iterator
1816 I = GCMetadataPrinterRegistry::begin(),
1817 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1818 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001819 GCMetadataPrinter *GMP = I->instantiate();
1820 GMP->S = S;
1821 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1822 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001823 }
1824
Chris Lattner103289e2009-08-23 07:19:13 +00001825 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001826 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001827}
David Greene014700c2009-07-13 20:25:48 +00001828
1829/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001830void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001831 if (!VerboseAsm)
1832 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001833
David Greene1924aab2009-11-13 21:34:57 +00001834 bool Newline = false;
1835
1836 if (!MI.getDebugLoc().isUnknown()) {
1837 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1838
1839 // Print source line info.
1840 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001841 O << MAI->getCommentString() << ' ';
1842 DIScope Scope(DLT.Scope);
1843 // Omit the directory, because it's likely to be long and uninteresting.
1844 if (!Scope.isNull())
1845 O << Scope.getFilename();
1846 else
1847 O << "<unknown>";
1848 O << ':' << DLT.Line;
David Greene1924aab2009-11-13 21:34:57 +00001849 if (DLT.Col != 0)
Dan Gohman3b9bc042009-12-05 00:23:29 +00001850 O << ':' << DLT.Col;
David Greene1924aab2009-11-13 21:34:57 +00001851 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001852 }
David Greene1924aab2009-11-13 21:34:57 +00001853
David Greeneddff9412009-11-16 15:12:23 +00001854 // Check for spills and reloads
1855 int FI;
1856
1857 const MachineFrameInfo *FrameInfo =
1858 MI.getParent()->getParent()->getFrameInfo();
1859
1860 // We assume a single instruction only has a spill or reload, not
1861 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001862 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001863 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1864 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001865 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001866 if (Newline) O << '\n';
1867 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001868 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001869 Newline = true;
1870 }
1871 }
David Greenef7ea2a52009-12-04 22:46:04 +00001872 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001873 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1874 if (Newline) O << '\n';
1875 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001876 O << MAI->getCommentString() << ' '
1877 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001878 Newline = true;
1879 }
1880 }
1881 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1882 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001883 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001884 if (Newline) O << '\n';
1885 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001886 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001887 Newline = true;
1888 }
1889 }
David Greenef7ea2a52009-12-04 22:46:04 +00001890 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001891 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1892 if (Newline) O << '\n';
1893 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001894 O << MAI->getCommentString() << ' '
1895 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001896 Newline = true;
1897 }
1898 }
1899
1900 // Check for spill-induced copies
1901 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1902 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1903 SrcSubIdx, DstSubIdx)) {
1904 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1905 if (Newline) O << '\n';
1906 O.PadToColumn(MAI->getCommentColumn());
1907 O << MAI->getCommentString() << " Reload Reuse";
1908 Newline = true;
1909 }
1910 }
David Greene014700c2009-07-13 20:25:48 +00001911}
1912
David Greenefe37ab32009-08-18 19:22:55 +00001913/// PrintChildLoopComment - Print comments about child loops within
1914/// the loop for this basic block, with nesting.
1915///
1916static void PrintChildLoopComment(formatted_raw_ostream &O,
1917 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001918 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001919 int FunctionNumber) {
1920 // Add child loop information
1921 for(MachineLoop::iterator cl = loop->begin(),
1922 clend = loop->end();
1923 cl != clend;
1924 ++cl) {
1925 MachineBasicBlock *Header = (*cl)->getHeader();
1926 assert(Header && "No header for loop");
1927
1928 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001929 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001930
Chris Lattner33adcfb2009-08-22 21:43:10 +00001931 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001932 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001933 << " Child Loop BB" << FunctionNumber << "_"
1934 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1935
Chris Lattner33adcfb2009-08-22 21:43:10 +00001936 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001937 }
1938}
1939
David Greeneb71d1b22009-08-10 16:38:07 +00001940/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00001941void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00001942 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001943 // Add loop depth information
1944 const MachineLoop *loop = LI->getLoopFor(&MBB);
1945
1946 if (loop) {
1947 // Print a newline after bb# annotation.
1948 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001949 O.PadToColumn(MAI->getCommentColumn());
1950 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001951 << '\n';
1952
Chris Lattner33adcfb2009-08-22 21:43:10 +00001953 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001954
1955 MachineBasicBlock *Header = loop->getHeader();
1956 assert(Header && "No header for loop");
1957
1958 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001959 O << MAI->getCommentString() << " Loop Header";
1960 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001961 }
1962 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001963 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001964 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1965 }
1966
1967 if (loop->empty()) {
1968 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001969 O.PadToColumn(MAI->getCommentColumn());
1970 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00001971 }
1972
1973 // Add parent loop information
1974 for (const MachineLoop *CurLoop = loop->getParentLoop();
1975 CurLoop;
1976 CurLoop = CurLoop->getParentLoop()) {
1977 MachineBasicBlock *Header = CurLoop->getHeader();
1978 assert(Header && "No header for loop");
1979
1980 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001981 O.PadToColumn(MAI->getCommentColumn());
1982 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001983 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001984 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001985 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1986 }
1987 }
1988 }
1989}