blob: 16eec0c44a35b5998ebf301712a90a4f2818c833 [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());
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000227 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000228 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000229
Chris Lattner25d812b2009-09-16 00:35:39 +0000230 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000231 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000232}
233
Evan Cheng1606e8e2009-03-13 07:51:59 +0000234namespace {
235 // SectionCPs - Keep track the alignment, constpool entries per Section.
236 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000237 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000238 unsigned Alignment;
239 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000240 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000241 };
242}
243
Chris Lattner3b4fd322005-11-21 08:25:09 +0000244/// EmitConstantPool - Print to the current output stream assembly
245/// representations of the constants in the constant pool MCP. This is
246/// used to print out constants which have been "spilled to memory" by
247/// the code generator.
248///
249void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000250 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000251 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000252
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000253 // Calculate sections for constant pool entries. We collect entries to go into
254 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000255 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000256 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000257 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000258 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000259
260 SectionKind Kind;
261 switch (CPE.getRelocationInfo()) {
262 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000263 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000264 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000265 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000266 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000267 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000268 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000269 case 4: Kind = SectionKind::getMergeableConst4(); break;
270 case 8: Kind = SectionKind::getMergeableConst8(); break;
271 case 16: Kind = SectionKind::getMergeableConst16();break;
272 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000273 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000274 }
275
Chris Lattner83d77fa2009-08-01 23:46:12 +0000276 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000277
Evan Cheng1606e8e2009-03-13 07:51:59 +0000278 // The number of sections are small, just do a linear search from the
279 // last section to the first.
280 bool Found = false;
281 unsigned SecIdx = CPSections.size();
282 while (SecIdx != 0) {
283 if (CPSections[--SecIdx].S == S) {
284 Found = true;
285 break;
286 }
287 }
288 if (!Found) {
289 SecIdx = CPSections.size();
290 CPSections.push_back(SectionCPs(S, Align));
291 }
292
293 if (Align > CPSections[SecIdx].Alignment)
294 CPSections[SecIdx].Alignment = Align;
295 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000296 }
297
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000298 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000299 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000300 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000301 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000302
Evan Cheng1606e8e2009-03-13 07:51:59 +0000303 unsigned Offset = 0;
304 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
305 unsigned CPI = CPSections[i].CPEs[j];
306 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000307
Chris Lattner3029f922006-02-09 04:46:04 +0000308 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000309 unsigned AlignMask = CPE.getAlignment() - 1;
310 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
311 EmitZeros(NewOffset - Offset);
312
313 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000314 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000315
Chris Lattner33adcfb2009-08-22 21:43:10 +0000316 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000317 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000318 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000319 O.PadToColumn(MAI->getCommentColumn());
320 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000321 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000322 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000323 O << '\n';
324 if (CPE.isMachineConstantPoolEntry())
325 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
326 else
327 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000328 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000329 }
330}
331
Nate Begeman37efe672006-04-22 18:53:45 +0000332/// EmitJumpTableInfo - Print assembly representations of the jump tables used
333/// by the current function to the current output stream.
334///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000335void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
336 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000337 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
338 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000339
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000340 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000341
Nate Begeman2f1ae882006-07-27 01:13:04 +0000342 // Pick the directive to use to print the jump table entries, and switch to
343 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000344 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000345
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000346 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000347 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000348 if (F->isWeakForLinker() ||
349 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000350 // In PIC mode, we need to emit the jump table to the same section as the
351 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000352 // We should also do if the section name is NULL or function is declared in
353 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000354 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
355 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000356 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000357 // Otherwise, drop it in the readonly section.
358 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000359 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000360 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000361 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000362 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000363
364 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000365
Nate Begeman37efe672006-04-22 18:53:45 +0000366 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000367 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000368
369 // If this jump table was deleted, ignore it.
370 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000371
372 // For PIC codegen, if possible we want to use the SetDirective to reduce
373 // the number of relocations the assembler will generate for the jump table.
374 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000375 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000376 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000377 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000378 if (EmittedSets.insert(JTBBs[ii]))
379 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000380
Chris Lattner7c301912009-09-13 19:02:16 +0000381 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000382 // before each jump table. The first label is never referenced, but tells
383 // the assembler and linker the extents of the jump table object. The
384 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000385 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
386 O << MAI->getLinkerPrivateGlobalPrefix()
387 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000388 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000389
Chris Lattner33adcfb2009-08-22 21:43:10 +0000390 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000391 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000392
Nate Begeman37efe672006-04-22 18:53:45 +0000393 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000394 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000395 O << '\n';
396 }
397 }
398}
399
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000400void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
401 const MachineBasicBlock *MBB,
402 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000403 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000404
405 // Use JumpTableDirective otherwise honor the entry size from the jump table
406 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000407 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000408 bool HadJTEntryDirective = JTEntryDirective != NULL;
409 if (!HadJTEntryDirective) {
410 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000411 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000412 }
413
414 O << JTEntryDirective << ' ';
415
416 // If we have emitted set directives for the jump table entries, print
417 // them rather than the entries themselves. If we're emitting PIC, then
418 // emit the table entries as differences between two text section labels.
419 // If we're emitting non-PIC code, then emit the entries as direct
420 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000421 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000422 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000423 } else if (MAI->getSetDirective()) {
424 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000425 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000426 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000427 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000428 // If the arch uses custom Jump Table directives, don't calc relative to
429 // JT
430 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000431 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000432 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000433 }
434}
435
436
Chris Lattnered138932005-12-13 06:32:10 +0000437/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
438/// special global used by LLVM. If so, emit it and return true, otherwise
439/// do nothing and return false.
440bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000441 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000442 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000443 EmitLLVMUsedList(GV->getInitializer());
444 return true;
445 }
446
Chris Lattner401e10c2009-07-20 06:14:25 +0000447 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000448 if (GV->getSection() == "llvm.metadata" ||
449 GV->hasAvailableExternallyLinkage())
450 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000451
452 if (!GV->hasAppendingLinkage()) return false;
453
454 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000455
Evan Cheng916d07c2007-06-04 20:39:18 +0000456 const TargetData *TD = TM.getTargetData();
457 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000458 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000459 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000460 EmitAlignment(Align, 0);
461 EmitXXStructorList(GV->getInitializer());
462 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000463 }
464
Chris Lattnerf231c072009-03-09 05:52:15 +0000465 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000466 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000467 EmitAlignment(Align, 0);
468 EmitXXStructorList(GV->getInitializer());
469 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000470 }
471
472 return false;
473}
474
Chris Lattner33adcfb2009-08-22 21:43:10 +0000475/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000476/// global in the specified llvm.used list for which emitUsedDirectiveFor
477/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000478void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000479 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000480
Dan Gohmana119de82009-06-14 23:30:43 +0000481 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000482 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
483 if (InitList == 0) return;
484
485 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000486 const GlobalValue *GV =
487 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000488 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000489 O << Directive;
490 EmitConstantValueOnly(InitList->getOperand(i));
491 O << '\n';
492 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000493 }
494}
495
Chris Lattnered138932005-12-13 06:32:10 +0000496/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
497/// function pointers, ignoring the init priority.
498void AsmPrinter::EmitXXStructorList(Constant *List) {
499 // Should be an array of '{ int, void ()* }' structs. The first value is the
500 // init priority, which we ignore.
501 if (!isa<ConstantArray>(List)) return;
502 ConstantArray *InitList = cast<ConstantArray>(List);
503 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
504 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
505 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000506
507 if (CS->getOperand(1)->isNullValue())
508 return; // Found a null terminator, exit printing.
509 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000510 EmitGlobalConstant(CS->getOperand(1));
511 }
512}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000513
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000514
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000515//===----------------------------------------------------------------------===//
516/// LEB 128 number encoding.
517
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000518/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000519/// representing an unsigned leb128 value.
520void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000521 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000522 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000523 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000524 Value >>= 7;
525 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000526 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000527 if (Value) O << ", ";
528 } while (Value);
529}
530
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000531/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000532/// representing a signed leb128 value.
533void AsmPrinter::PrintSLEB128(int Value) const {
534 int Sign = Value >> (8 * sizeof(Value) - 1);
535 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000536 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000537
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000538 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000539 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000540 Value >>= 7;
541 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
542 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000543 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000544 if (IsMore) O << ", ";
545 } while (IsMore);
546}
547
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000548//===--------------------------------------------------------------------===//
549// Emission and print routines
550//
551
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000552/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000553///
554void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000555 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000556 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000557}
558
559/// EOL - Print a newline character to asm stream. If a comment is present
560/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000561void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000562 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000563}
Owen Anderson25995092008-07-01 21:16:27 +0000564
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000565void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000566 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000567 O.PadToColumn(MAI->getCommentColumn());
568 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000569 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000570 << Comment;
571 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000572 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000573}
574
Owen Anderson25995092008-07-01 21:16:27 +0000575void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000576 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000577 O.PadToColumn(MAI->getCommentColumn());
578 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000579 << ' '
580 << Comment;
581 }
582 O << '\n';
583}
584
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000585static const char *DecodeDWARFEncoding(unsigned Encoding) {
586 switch (Encoding) {
587 case dwarf::DW_EH_PE_absptr:
588 return "absptr";
589 case dwarf::DW_EH_PE_omit:
590 return "omit";
591 case dwarf::DW_EH_PE_pcrel:
592 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000593 case dwarf::DW_EH_PE_udata4:
594 return "udata4";
595 case dwarf::DW_EH_PE_udata8:
596 return "udata8";
597 case dwarf::DW_EH_PE_sdata4:
598 return "sdata4";
599 case dwarf::DW_EH_PE_sdata8:
600 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000601 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
602 return "pcrel udata4";
603 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
604 return "pcrel sdata4";
605 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
606 return "pcrel udata8";
607 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
608 return "pcrel sdata8";
609 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
610 return "indirect pcrel udata4";
611 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
612 return "indirect pcrel sdata4";
613 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
614 return "indirect pcrel udata8";
615 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
616 return "indirect pcrel sdata8";
617 }
618
619 return 0;
620}
621
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000622void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
623 if (VerboseAsm && *Comment) {
624 O.PadToColumn(MAI->getCommentColumn());
625 O << MAI->getCommentString()
626 << ' '
627 << Comment;
628
629 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
630 O << " (" << EncStr << ')';
631 }
632 O << '\n';
633}
634
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000635/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
636/// unsigned leb128 value.
637void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000638 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000639 O << "\t.uleb128\t"
640 << Value;
641 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000642 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000643 PrintULEB128(Value);
644 }
645}
646
647/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
648/// signed leb128 value.
649void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000650 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000651 O << "\t.sleb128\t"
652 << Value;
653 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000654 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000655 PrintSLEB128(Value);
656 }
657}
658
659/// EmitInt8 - Emit a byte directive and value.
660///
661void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000662 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000663 PrintHex(Value & 0xFF);
664}
665
666/// EmitInt16 - Emit a short directive and value.
667///
668void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000669 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000670 PrintHex(Value & 0xFFFF);
671}
672
673/// EmitInt32 - Emit a long directive and value.
674///
675void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000676 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000677 PrintHex(Value);
678}
679
680/// EmitInt64 - Emit a long long directive and value.
681///
682void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000683 if (MAI->getData64bitsDirective()) {
684 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000685 PrintHex(Value);
686 } else {
687 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000688 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689 EmitInt32(unsigned(Value));
690 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000691 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000692 EmitInt32(unsigned(Value >> 32));
693 }
694 }
695}
696
697/// toOctal - Convert the low order bits of X into an octal digit.
698///
699static inline char toOctal(int X) {
700 return (X&7)+'0';
701}
702
703/// printStringChar - Print a char, escaped if necessary.
704///
David Greene71847812009-07-14 20:18:05 +0000705static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000706 if (C == '"') {
707 O << "\\\"";
708 } else if (C == '\\') {
709 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000710 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000711 O << C;
712 } else {
713 switch(C) {
714 case '\b': O << "\\b"; break;
715 case '\f': O << "\\f"; break;
716 case '\n': O << "\\n"; break;
717 case '\r': O << "\\r"; break;
718 case '\t': O << "\\t"; break;
719 default:
720 O << '\\';
721 O << toOctal(C >> 6);
722 O << toOctal(C >> 3);
723 O << toOctal(C >> 0);
724 break;
725 }
726 }
727}
728
729/// EmitString - Emit a string with quotes and a null terminator.
730/// Special characters are emitted properly.
731/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000732void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000733 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000734}
735
736void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000737 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000738 if (AscizDirective)
739 O << AscizDirective;
740 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000741 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000742 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000743 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000744 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000745 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000746 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000747 else
748 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000749}
750
751
Dan Gohman189f80d2007-09-24 20:58:13 +0000752/// EmitFile - Emit a .file directive.
753void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
754 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000755 for (unsigned i = 0, N = Name.size(); i < N; ++i)
756 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000757 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000758}
759
760
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000761//===----------------------------------------------------------------------===//
762
Chris Lattner3a420532007-05-31 18:57:45 +0000763// EmitAlignment - Emit an alignment directive to the specified power of
764// two boundary. For example, if you pass in 3 here, you will get an 8
765// byte alignment. If a global value is specified, and if that global has
766// an explicit alignment requested, it will unconditionally override the
767// alignment request. However, if ForcedAlignBits is specified, this value
768// has final say: the ultimate alignment will be the max of ForcedAlignBits
769// and the alignment computed with NumBits and the global.
770//
771// The algorithm is:
772// Align = NumBits;
773// if (GV && GV->hasalignment) Align = GV->getalignment();
774// Align = std::max(Align, ForcedAlignBits);
775//
776void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000777 unsigned ForcedAlignBits,
778 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000779 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000780 NumBits = Log2_32(GV->getAlignment());
781 NumBits = std::max(NumBits, ForcedAlignBits);
782
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000783 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000784
785 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000786 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000787 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000788
789 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000790}
David Greenea5bb59f2009-08-05 21:00:52 +0000791
Chris Lattner25045bd2005-11-21 07:51:36 +0000792/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000793///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000794void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000795 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000796 if (MAI->getZeroDirective()) {
797 O << MAI->getZeroDirective() << NumZeros;
798 if (MAI->getZeroDirectiveSuffix())
799 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000800 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000801 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000802 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000803 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000804 }
805 }
806}
807
Chris Lattnera80ba712004-08-16 23:15:22 +0000808// Print out the specified constant, without a storage class. Only the
809// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000810void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000811 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000812 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000813 return;
814 }
815
816 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000817 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000818 return;
819 }
820
821 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000822 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000823 // name of the variable or function as the address value.
824 O << Mang->getMangledName(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000825 return;
826 }
827
828 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Dan Gohman6a55e622009-10-30 01:45:18 +0000829 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000830 return;
831 }
832
833 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
834 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000835 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000836 O << '0';
837 return;
838 }
839
840 switch (CE->getOpcode()) {
841 case Instruction::ZExt:
842 case Instruction::SExt:
843 case Instruction::FPTrunc:
844 case Instruction::FPExt:
845 case Instruction::UIToFP:
846 case Instruction::SIToFP:
847 case Instruction::FPToUI:
848 case Instruction::FPToSI:
849 default:
850 llvm_unreachable("FIXME: Don't support this constant cast expr");
851 case Instruction::GetElementPtr: {
852 // generate a symbolic expression for the byte address
853 const TargetData *TD = TM.getTargetData();
854 const Constant *ptrVal = CE->getOperand(0);
855 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
856 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
857 idxVec.size());
858 if (Offset == 0)
859 return EmitConstantValueOnly(ptrVal);
860
861 // Truncate/sext the offset to the pointer size.
862 if (TD->getPointerSizeInBits() != 64) {
863 int SExtAmount = 64-TD->getPointerSizeInBits();
864 Offset = (Offset << SExtAmount) >> SExtAmount;
865 }
866
867 if (Offset)
868 O << '(';
869 EmitConstantValueOnly(ptrVal);
870 if (Offset > 0)
871 O << ") + " << Offset;
872 else
873 O << ") - " << -Offset;
874 return;
875 }
876 case Instruction::BitCast:
877 return EmitConstantValueOnly(CE->getOperand(0));
878
879 case Instruction::IntToPtr: {
880 // Handle casts to pointers by changing them into casts to the appropriate
881 // integer type. This promotes constant folding and simplifies this code.
882 const TargetData *TD = TM.getTargetData();
883 Constant *Op = CE->getOperand(0);
884 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
885 false/*ZExt*/);
886 return EmitConstantValueOnly(Op);
887 }
888
889 case Instruction::PtrToInt: {
890 // Support only foldable casts to/from pointers that can be eliminated by
891 // changing the pointer to the appropriately sized integer type.
892 Constant *Op = CE->getOperand(0);
893 const Type *Ty = CE->getType();
894 const TargetData *TD = TM.getTargetData();
895
896 // We can emit the pointer value into this slot if the slot is an
897 // integer slot greater or equal to the size of the pointer.
898 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
899 return EmitConstantValueOnly(Op);
900
901 O << "((";
902 EmitConstantValueOnly(Op);
903 APInt ptrMask =
904 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
905
906 SmallString<40> S;
907 ptrMask.toStringUnsigned(S);
908 O << ") & " << S.str() << ')';
909 return;
910 }
911
912 case Instruction::Trunc:
913 // We emit the value and depend on the assembler to truncate the generated
914 // expression properly. This is important for differences between
915 // blockaddress labels. Since the two labels are in the same function, it
916 // is reasonable to treat their delta as a 32-bit value.
917 return EmitConstantValueOnly(CE->getOperand(0));
918
919 case Instruction::Add:
920 case Instruction::Sub:
921 case Instruction::And:
922 case Instruction::Or:
923 case Instruction::Xor:
924 O << '(';
925 EmitConstantValueOnly(CE->getOperand(0));
926 O << ')';
927 switch (CE->getOpcode()) {
928 case Instruction::Add:
929 O << " + ";
930 break;
931 case Instruction::Sub:
932 O << " - ";
933 break;
934 case Instruction::And:
935 O << " & ";
936 break;
937 case Instruction::Or:
938 O << " | ";
939 break;
940 case Instruction::Xor:
941 O << " ^ ";
942 break;
943 default:
944 break;
945 }
946 O << '(';
947 EmitConstantValueOnly(CE->getOperand(1));
948 O << ')';
949 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000950 }
951}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000952
Chris Lattner2980cef2005-11-10 18:06:33 +0000953/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000954/// the predicate isString is true.
955///
David Greene71847812009-07-14 20:18:05 +0000956static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000957 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000958 assert(CVA->isString() && "Array is not string compatible!");
959
Dan Gohmand19a53b2008-06-30 22:03:41 +0000960 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000961 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000962 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000963 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000964 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000965 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000966 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000967}
968
Jeff Cohenc884db42006-05-02 01:16:28 +0000969/// EmitString - Emit a zero-byte-terminated string constant.
970///
971void AsmPrinter::EmitString(const ConstantArray *CVA) const {
972 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000973 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000974 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000975 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000976 printAsCString(O, CVA, NumElts-1);
977 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000978 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000979 printAsCString(O, CVA, NumElts);
980 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000981 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000982}
983
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000984void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
985 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000986 if (CVA->isString()) {
987 EmitString(CVA);
988 } else { // Not a string. Print the values in successive locations
989 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000990 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000991 }
992}
993
994void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
995 const VectorType *PTy = CP->getType();
996
997 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
998 EmitGlobalConstant(CP->getOperand(I));
999}
1000
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001001void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1002 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001003 // Print the fields in successive locations. Pad to align if needed!
1004 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001005 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001006 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1007 uint64_t sizeSoFar = 0;
1008 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1009 const Constant* field = CVS->getOperand(i);
1010
1011 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001012 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001013 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1014 - cvsLayout->getElementOffset(i)) - fieldSize;
1015 sizeSoFar += fieldSize + padSize;
1016
1017 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001018 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001019
1020 // Insert padding - this may include padding to increase the size of the
1021 // current field up to the ABI size (if the struct is not packed) as well
1022 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001023 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001024 }
1025 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1026 "Layout of constant struct may be incorrect!");
1027}
1028
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001029void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1030 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001031 // FP Constants are printed as integer constants to avoid losing
1032 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001033 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001034 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001035 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001036 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1037 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001038 if (MAI->getData64bitsDirective(AddrSpace)) {
1039 O << MAI->getData64bitsDirective(AddrSpace) << 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() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001043 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001044 O << '\n';
1045 } else if (TD->isBigEndian()) {
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';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001053 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001054 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001055 O.PadToColumn(MAI->getCommentColumn());
1056 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001057 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001058 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001059 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001060 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001061 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001062 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001063 O.PadToColumn(MAI->getCommentColumn());
1064 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001065 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001066 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001067 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001068 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001069 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001070 O.PadToColumn(MAI->getCommentColumn());
1071 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001072 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001073 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001074 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001075 }
1076 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001077 }
1078
1079 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001080 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001081 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001082 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001083 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001084 O.PadToColumn(MAI->getCommentColumn());
1085 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001086 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001087 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001088 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001089 }
1090
1091 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001092 // all long double variants are printed as hex
1093 // api needed to prevent premature destruction
1094 APInt api = CFP->getValueAPF().bitcastToAPInt();
1095 const uint64_t *p = api.getRawData();
1096 // Convert to double so we can print the approximate val as a comment.
1097 APFloat DoubleVal = CFP->getValueAPF();
1098 bool ignored;
1099 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1100 &ignored);
1101 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001102 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001103 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001104 O.PadToColumn(MAI->getCommentColumn());
1105 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001106 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001107 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001108 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001109 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001110 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001111 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001112 O.PadToColumn(MAI->getCommentColumn());
1113 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001114 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001115 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001116 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001117 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001118 O.PadToColumn(MAI->getCommentColumn());
1119 O << MAI->getCommentString() << " next halfword";
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() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001126 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001127 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001128 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001129 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001130 O.PadToColumn(MAI->getCommentColumn());
1131 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001132 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001133 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001134 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001135 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001136 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
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 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001141 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001143 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001144 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001145 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001146 O.PadToColumn(MAI->getCommentColumn());
1147 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001148 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001149 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001150 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001151 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001152 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001153 O.PadToColumn(MAI->getCommentColumn());
1154 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001155 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001156 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001157 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001158 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001159 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001160 O.PadToColumn(MAI->getCommentColumn());
1161 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001162 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001163 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001164 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001165 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001166 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001167 O.PadToColumn(MAI->getCommentColumn());
1168 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001169 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001170 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001171 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001172 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001173 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1174 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001175 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001176 }
1177
1178 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001179 // all long double variants are printed as hex
1180 // api needed to prevent premature destruction
1181 APInt api = CFP->getValueAPF().bitcastToAPInt();
1182 const uint64_t *p = api.getRawData();
1183 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001184 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001185 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001186 O.PadToColumn(MAI->getCommentColumn());
1187 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001188 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001189 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001190 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
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 << " next word";
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[1]);
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 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001210 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001211 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001212 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001213 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001214 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001215 O.PadToColumn(MAI->getCommentColumn());
1216 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001217 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001218 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001219 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001220 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001221 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001222 O.PadToColumn(MAI->getCommentColumn());
1223 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001224 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001225 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001226 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001227 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001228 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001229 O.PadToColumn(MAI->getCommentColumn());
1230 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001231 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001232 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001233 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001234 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001235 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001236 O.PadToColumn(MAI->getCommentColumn());
1237 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001238 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001239 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001240 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001241 }
1242 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001243 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001244}
1245
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001246void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1247 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001248 const TargetData *TD = TM.getTargetData();
1249 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001250 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001251
1252 // We don't expect assemblers to support integer data directives
1253 // for more than 64 bits, so we emit the data in at most 64-bit
1254 // quantities at a time.
1255 const uint64_t *RawData = CI->getValue().getRawData();
1256 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1257 uint64_t Val;
1258 if (TD->isBigEndian())
1259 Val = RawData[e - i - 1];
1260 else
1261 Val = RawData[i];
1262
Chris Lattner5979dff2010-01-13 04:38:16 +00001263 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001264 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001265 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001266 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001267
1268 // Emit two 32-bit chunks, order depends on endianness.
1269 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1270 const char *FirstName = " least", *SecondName = " most";
1271 if (TD->isBigEndian()) {
1272 std::swap(FirstChunk, SecondChunk);
1273 std::swap(FirstName, SecondName);
1274 }
1275
1276 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1277 if (VerboseAsm) {
1278 O.PadToColumn(MAI->getCommentColumn());
1279 O << MAI->getCommentString()
1280 << FirstName << " significant half of i64 " << Val;
1281 }
1282 O << '\n';
1283
1284 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1285 if (VerboseAsm) {
1286 O.PadToColumn(MAI->getCommentColumn());
1287 O << MAI->getCommentString()
1288 << SecondName << " significant half of i64 " << Val;
1289 }
1290 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001291 }
1292}
1293
Chris Lattner25045bd2005-11-21 07:51:36 +00001294/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001295void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001296 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001297 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001298 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001299
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001300 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001301 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001302 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001303 }
1304
1305 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001306 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001307 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001308 }
1309
1310 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001311 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001312 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001313 }
1314
1315 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001316 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001317 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001318 }
1319
1320 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1321 // If we can directly emit an 8-byte constant, do it.
1322 if (Size == 8)
1323 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1324 O << Data64Dir << CI->getZExtValue() << '\n';
1325 return;
1326 }
1327
Dan Gohman00d448a2008-12-22 21:14:27 +00001328 // Small integers are handled below; large integers are handled here.
1329 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001330 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001331 return;
1332 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001333 }
1334
1335 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001336 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001337 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001338 }
1339
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001340 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001341 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001342 if (VerboseAsm) {
1343 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1344 SmallString<40> S;
1345 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001346 O.PadToColumn(MAI->getCommentColumn());
1347 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001348 }
Scott Micheleefc8452008-06-03 15:39:51 +00001349 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001350 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001351}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001352
Chris Lattnerfad86b02008-08-17 07:19:36 +00001353void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001354 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001355 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001356}
1357
Chris Lattner3ce9b672006-09-26 23:59:50 +00001358/// PrintSpecial - Print information related to the specified machine instr
1359/// that is independent of the operand, and may be independent of the instr
1360/// itself. This can be useful for portably encoding the comment character
1361/// or other bits of target-specific knowledge into the asmstrings. The
1362/// syntax used is ${:comment}. Targets can override this to add support
1363/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001364void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001365 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001366 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001367 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001368 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001369 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001370 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001371 // Comparing the address of MI isn't sufficient, because machineinstrs may
1372 // be allocated to the same address across functions.
1373 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1374
Owen Andersonbd58edf2009-06-24 22:28:12 +00001375 // If this is a new LastFn instruction, bump the counter.
1376 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001377 ++Counter;
1378 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001379 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001380 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001381 O << Counter;
1382 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001383 std::string msg;
1384 raw_string_ostream Msg(msg);
1385 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001386 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001387 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001388 }
1389}
1390
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001391/// processDebugLoc - Processes the debug information of each machine
1392/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001393void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1394 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001395 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1396 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001397 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001398 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001399 if (DL.isUnknown())
1400 return;
1401 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1402 if (CurDLT.Scope == 0)
1403 return;
1404
1405 if (BeforePrintingInsn) {
1406 if (CurDLT != PrevDLT) {
1407 unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1408 CurDLT.Scope);
1409 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001410 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001411 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001412 PrevDLT = CurDLT;
1413 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001414 } else {
1415 // After printing instruction
1416 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001417 }
1418}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001419
Devang Patel53bb5c92009-11-10 23:06:00 +00001420
Chris Lattner0264d1a2006-01-27 02:10:10 +00001421/// printInlineAsm - This method formats and prints the specified machine
1422/// instruction that is an inline asm.
1423void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001424 unsigned NumOperands = MI->getNumOperands();
1425
1426 // Count the number of register definitions.
1427 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001428 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001429 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001430 assert(NumDefs != NumOperands-1 && "No asm string?");
1431
Dan Gohmand735b802008-10-03 15:45:36 +00001432 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001433
1434 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001435 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001436
Dan Gohman40c57862009-11-06 00:04:54 +00001437 O << '\t';
1438
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001439 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1440 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001441 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001442 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1443 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001444 return;
1445 }
1446
Chris Lattner33adcfb2009-08-22 21:43:10 +00001447 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001448
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001449 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001450 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001451
Chris Lattner66099132006-02-01 22:41:11 +00001452 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1453 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001454
Chris Lattner66099132006-02-01 22:41:11 +00001455 while (*LastEmitted) {
1456 switch (*LastEmitted) {
1457 default: {
1458 // Not a special case, emit the string section literally.
1459 const char *LiteralEnd = LastEmitted+1;
1460 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001461 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001462 ++LiteralEnd;
1463 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1464 O.write(LastEmitted, LiteralEnd-LastEmitted);
1465 LastEmitted = LiteralEnd;
1466 break;
1467 }
Chris Lattner1c059972006-05-05 21:47:05 +00001468 case '\n':
1469 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001470 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001471 break;
Chris Lattner66099132006-02-01 22:41:11 +00001472 case '$': {
1473 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001474 bool Done = true;
1475
1476 // Handle escapes.
1477 switch (*LastEmitted) {
1478 default: Done = false; break;
1479 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001480 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1481 O << '$';
1482 ++LastEmitted; // Consume second '$' character.
1483 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001484 case '(': // $( -> same as GCC's { character.
1485 ++LastEmitted; // Consume '(' character.
1486 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001487 llvm_report_error("Nested variants found in inline asm string: '"
1488 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001489 }
1490 CurVariant = 0; // We're in the first variant now.
1491 break;
1492 case '|':
1493 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001494 if (CurVariant == -1)
1495 O << '|'; // this is gcc's behavior for | outside a variant
1496 else
1497 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001498 break;
1499 case ')': // $) -> same as GCC's } char.
1500 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001501 if (CurVariant == -1)
1502 O << '}'; // this is gcc's behavior for } outside a variant
1503 else
1504 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001505 break;
Chris Lattner66099132006-02-01 22:41:11 +00001506 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001507 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001508
1509 bool HasCurlyBraces = false;
1510 if (*LastEmitted == '{') { // ${variable}
1511 ++LastEmitted; // Consume '{' character.
1512 HasCurlyBraces = true;
1513 }
1514
Chris Lattner3e0cc262009-03-10 05:37:13 +00001515 // If we have ${:foo}, then this is not a real operand reference, it is a
1516 // "magic" string reference, just like in .td files. Arrange to call
1517 // PrintSpecial.
1518 if (HasCurlyBraces && *LastEmitted == ':') {
1519 ++LastEmitted;
1520 const char *StrStart = LastEmitted;
1521 const char *StrEnd = strchr(StrStart, '}');
1522 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001523 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1524 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001525 }
1526
1527 std::string Val(StrStart, StrEnd);
1528 PrintSpecial(MI, Val.c_str());
1529 LastEmitted = StrEnd+1;
1530 break;
1531 }
1532
Chris Lattner66099132006-02-01 22:41:11 +00001533 const char *IDStart = LastEmitted;
1534 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001535 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001536 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1537 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001538 llvm_report_error("Bad $ operand number in inline asm string: '"
1539 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001540 }
1541 LastEmitted = IDEnd;
1542
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001543 char Modifier[2] = { 0, 0 };
1544
Chris Lattner66099132006-02-01 22:41:11 +00001545 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001546 // If we have curly braces, check for a modifier character. This
1547 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1548 if (*LastEmitted == ':') {
1549 ++LastEmitted; // Consume ':' character.
1550 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001551 llvm_report_error("Bad ${:} expression in inline asm string: '"
1552 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001553 }
1554
1555 Modifier[0] = *LastEmitted;
1556 ++LastEmitted; // Consume modifier character.
1557 }
1558
Chris Lattner66099132006-02-01 22:41:11 +00001559 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001560 llvm_report_error("Bad ${} expression in inline asm string: '"
1561 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001562 }
1563 ++LastEmitted; // Consume '}' character.
1564 }
1565
1566 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001567 llvm_report_error("Invalid $ operand number in inline asm string: '"
1568 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001569 }
1570
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001571 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001572 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001573 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1574 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001575
1576 bool Error = false;
1577
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001578 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001579 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001580 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001581 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001582 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001583 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001584
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001585 if (OpNo >= MI->getNumOperands()) {
1586 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001587 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001588 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001589 ++OpNo; // Skip over the ID number.
1590
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001591 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001592 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1593 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001594 else {
1595 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001596 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001597 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1598 Modifier[0] ? Modifier : 0);
1599 } else {
1600 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1601 Modifier[0] ? Modifier : 0);
1602 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001603 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001604 }
1605 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001606 std::string msg;
1607 raw_string_ostream Msg(msg);
1608 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001609 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001610 MI->print(Msg);
1611 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001612 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001613 }
Chris Lattner66099132006-02-01 22:41:11 +00001614 break;
1615 }
Chris Lattner66099132006-02-01 22:41:11 +00001616 }
1617 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001618 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001619}
1620
Evan Chengda47e6e2008-03-15 00:03:38 +00001621/// printImplicitDef - This method prints the specified machine instruction
1622/// that is an implicit def.
1623void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001624 if (!VerboseAsm) return;
1625 O.PadToColumn(MAI->getCommentColumn());
1626 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001627 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001628}
1629
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001630void AsmPrinter::printKill(const MachineInstr *MI) const {
1631 if (!VerboseAsm) return;
1632 O.PadToColumn(MAI->getCommentColumn());
1633 O << MAI->getCommentString() << " kill:";
1634 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1635 const MachineOperand &op = MI->getOperand(n);
1636 assert(op.isReg() && "KILL instruction must have only register operands");
1637 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1638 }
1639}
1640
Jim Laskey1ee29252007-01-26 14:34:52 +00001641/// printLabel - This method prints a local label used by debug and
1642/// exception handling tables.
1643void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001644 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001645}
1646
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001647void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001648 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001649}
1650
Chris Lattner66099132006-02-01 22:41:11 +00001651/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1652/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001653/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001654bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001655 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001656 // Target doesn't support this yet!
1657 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001658}
Chris Lattnerdd260332006-02-24 20:21:58 +00001659
1660bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1661 unsigned AsmVariant,
1662 const char *ExtraCode) {
1663 // Target doesn't support this yet!
1664 return true;
1665}
Nate Begeman37efe672006-04-22 18:53:45 +00001666
Dan Gohman29cbade2009-11-20 23:18:13 +00001667MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1668 const char *Suffix) const {
1669 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001670}
1671
1672MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001673 const BasicBlock *BB,
1674 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001675 assert(BB->hasName() &&
1676 "Address of anonymous basic block not supported yet!");
1677
Dan Gohman568a3be2009-11-05 23:14:35 +00001678 // This code must use the function name itself, and not the function number,
1679 // since it must be possible to generate the label name from within other
1680 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001681 SmallString<60> FnName;
1682 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001683
Chris Lattner2f8cc262010-01-13 07:30:49 +00001684 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001685 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001686 Mang->getNameWithPrefix(NameResult,
1687 StringRef("BA") + Twine((unsigned)FnName.size()) +
1688 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1689 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001690
Chris Lattner48130352010-01-13 06:38:18 +00001691 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001692}
1693
Chris Lattner7cb384d2009-09-12 23:02:08 +00001694MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1695 SmallString<60> Name;
1696 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1697 << getFunctionNumber() << '_' << MBBID;
1698
1699 return OutContext.GetOrCreateSymbol(Name.str());
1700}
1701
Chris Lattner6b04ede2010-01-15 23:18:17 +00001702/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1703/// value.
1704MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1705 SmallString<60> NameStr;
1706 Mang->getNameWithPrefix(NameStr, GV, false);
1707 return OutContext.GetOrCreateSymbol(NameStr.str());
1708}
1709
Chris Lattnerd588b972010-01-15 23:25:11 +00001710/// GetPrivateGlobalValueSymbolStub - Return the MCSymbol for a symbol with
1711/// global value name as its base, with the specified suffix, and where the
1712/// symbol is forced to have private linkage.
1713MCSymbol *AsmPrinter::GetPrivateGlobalValueSymbolStub(const GlobalValue *GV,
1714 StringRef Suffix) const {
1715 SmallString<60> NameStr;
1716 Mang->getNameWithPrefix(NameStr, GV, true);
1717 NameStr.append(Suffix.begin(), Suffix.end());
1718 return OutContext.GetOrCreateSymbol(NameStr.str());
1719}
1720
Chris Lattner6b04ede2010-01-15 23:18:17 +00001721/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1722/// ExternalSymbol.
1723MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1724 SmallString<60> NameStr;
1725 Mang->getNameWithPrefix(NameStr, Sym);
1726 return OutContext.GetOrCreateSymbol(NameStr.str());
1727}
1728
Chris Lattner7cb384d2009-09-12 23:02:08 +00001729
Chris Lattner70a54c02009-09-13 18:25:37 +00001730/// EmitBasicBlockStart - This method prints the label for the specified
1731/// MachineBasicBlock, an alignment (if present) and a comment describing
1732/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001733void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001734 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001735 if (unsigned Align = MBB->getAlignment())
1736 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001737
Dan Gohmanb1cac332009-10-30 01:34:35 +00001738 // If the block has its address taken, emit a special label to satisfy
1739 // references to the block. This is done so that we don't need to
1740 // remember the number of this label, and so that we can make
1741 // forward references to labels without knowing what their numbers
1742 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001743 if (MBB->hasAddressTaken()) {
1744 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1745 MBB->getBasicBlock())->print(O, MAI);
1746 O << ':';
1747 if (VerboseAsm) {
1748 O.PadToColumn(MAI->getCommentColumn());
1749 O << MAI->getCommentString() << " Address Taken";
1750 }
1751 O << '\n';
1752 }
1753
Dan Gohmanb1cac332009-10-30 01:34:35 +00001754 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001755 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1756 if (VerboseAsm)
1757 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1758 } else {
1759 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1760 O << ':';
1761 if (!VerboseAsm)
1762 O << '\n';
1763 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001764
Dan Gohmanb1cac332009-10-30 01:34:35 +00001765 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001766 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001767 if (const BasicBlock *BB = MBB->getBasicBlock())
1768 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001769 O.PadToColumn(MAI->getCommentColumn());
1770 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001771 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001772 }
David Greeneb71d1b22009-08-10 16:38:07 +00001773
Chris Lattner70a54c02009-09-13 18:25:37 +00001774 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001775 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001776 }
Nate Begeman37efe672006-04-22 18:53:45 +00001777}
Nate Begeman52a51e382006-08-12 21:29:52 +00001778
Evan Chengcc415862007-11-09 01:32:10 +00001779/// printPICJumpTableSetLabel - This method prints a set label for the
1780/// specified MachineBasicBlock for a jumptable entry.
1781void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1782 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001783 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001784 return;
1785
Chris Lattner33adcfb2009-08-22 21:43:10 +00001786 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001787 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001788 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001789 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001790 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001791}
Evan Chengd6594ae2006-09-12 21:00:35 +00001792
Evan Chengcc415862007-11-09 01:32:10 +00001793void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1794 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001795 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001796 return;
1797
Chris Lattner33adcfb2009-08-22 21:43:10 +00001798 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001799 << getFunctionNumber() << '_' << uid << '_' << uid2
1800 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001801 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001802 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001803 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001804}
1805
Evan Chengd6594ae2006-09-12 21:00:35 +00001806/// printDataDirective - This method prints the asm directive for the
1807/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001808void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001809 const TargetData *TD = TM.getTargetData();
1810 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001811 case Type::FloatTyID: case Type::DoubleTyID:
1812 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1813 assert(0 && "Should have already output floating point constant.");
1814 default:
1815 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001816 case Type::IntegerTyID: {
1817 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1818 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001819 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001820 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001821 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001822 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001823 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001824 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001825 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001826 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001827 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001828 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001829 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001830 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001831 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001832 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001833 case Type::PointerTyID:
1834 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001835 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001836 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001837 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001838 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001839 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001840 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001841 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001842 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001843 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001844 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001845 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001846 }
1847}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001848
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001849void AsmPrinter::printVisibility(const std::string& Name,
1850 unsigned Visibility) const {
1851 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001852 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001853 O << Directive << Name << '\n';
1854 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001855 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001856 O << Directive << Name << '\n';
1857 }
1858}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001859
Chris Lattner53d4d782010-01-15 23:38:51 +00001860void AsmPrinter::printVisibility(const MCSymbol *Sym,
1861 unsigned Visibility) const {
1862 if (Visibility == GlobalValue::HiddenVisibility) {
1863 if (const char *Directive = MAI->getHiddenDirective()) {
1864 O << Directive;
1865 Sym->print(O, MAI);
1866 O << '\n';
1867 }
1868 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1869 if (const char *Directive = MAI->getProtectedDirective()) {
1870 O << Directive;
1871 Sym->print(O, MAI);
1872 O << '\n';
1873 }
1874 }
1875}
1876
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001877void AsmPrinter::printOffset(int64_t Offset) const {
1878 if (Offset > 0)
1879 O << '+' << Offset;
1880 else if (Offset < 0)
1881 O << Offset;
1882}
1883
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001884GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1885 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001886 return 0;
1887
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001888 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001889 if (GCPI != GCMetadataPrinters.end())
1890 return GCPI->second;
1891
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001892 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001893
1894 for (GCMetadataPrinterRegistry::iterator
1895 I = GCMetadataPrinterRegistry::begin(),
1896 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1897 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001898 GCMetadataPrinter *GMP = I->instantiate();
1899 GMP->S = S;
1900 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1901 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001902 }
1903
Chris Lattner103289e2009-08-23 07:19:13 +00001904 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001905 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001906}
David Greene014700c2009-07-13 20:25:48 +00001907
1908/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001909void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001910 if (!VerboseAsm)
1911 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001912
David Greene1924aab2009-11-13 21:34:57 +00001913 bool Newline = false;
1914
1915 if (!MI.getDebugLoc().isUnknown()) {
1916 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1917
1918 // Print source line info.
1919 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001920 O << MAI->getCommentString() << ' ';
1921 DIScope Scope(DLT.Scope);
1922 // Omit the directory, because it's likely to be long and uninteresting.
1923 if (!Scope.isNull())
1924 O << Scope.getFilename();
1925 else
1926 O << "<unknown>";
1927 O << ':' << DLT.Line;
David Greene1924aab2009-11-13 21:34:57 +00001928 if (DLT.Col != 0)
Dan Gohman3b9bc042009-12-05 00:23:29 +00001929 O << ':' << DLT.Col;
David Greene1924aab2009-11-13 21:34:57 +00001930 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001931 }
David Greene1924aab2009-11-13 21:34:57 +00001932
David Greeneddff9412009-11-16 15:12:23 +00001933 // Check for spills and reloads
1934 int FI;
1935
1936 const MachineFrameInfo *FrameInfo =
1937 MI.getParent()->getParent()->getFrameInfo();
1938
1939 // We assume a single instruction only has a spill or reload, not
1940 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001941 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001942 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1943 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001944 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001945 if (Newline) O << '\n';
1946 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001947 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001948 Newline = true;
1949 }
1950 }
David Greenef7ea2a52009-12-04 22:46:04 +00001951 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001952 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1953 if (Newline) O << '\n';
1954 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001955 O << MAI->getCommentString() << ' '
1956 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001957 Newline = true;
1958 }
1959 }
1960 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1961 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001962 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001963 if (Newline) O << '\n';
1964 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001965 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001966 Newline = true;
1967 }
1968 }
David Greenef7ea2a52009-12-04 22:46:04 +00001969 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001970 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1971 if (Newline) O << '\n';
1972 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001973 O << MAI->getCommentString() << ' '
1974 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001975 Newline = true;
1976 }
1977 }
1978
1979 // Check for spill-induced copies
1980 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1981 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1982 SrcSubIdx, DstSubIdx)) {
1983 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1984 if (Newline) O << '\n';
1985 O.PadToColumn(MAI->getCommentColumn());
1986 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001987 }
1988 }
David Greene014700c2009-07-13 20:25:48 +00001989}
1990
David Greenefe37ab32009-08-18 19:22:55 +00001991/// PrintChildLoopComment - Print comments about child loops within
1992/// the loop for this basic block, with nesting.
1993///
1994static void PrintChildLoopComment(formatted_raw_ostream &O,
1995 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001996 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001997 int FunctionNumber) {
1998 // Add child loop information
1999 for(MachineLoop::iterator cl = loop->begin(),
2000 clend = loop->end();
2001 cl != clend;
2002 ++cl) {
2003 MachineBasicBlock *Header = (*cl)->getHeader();
2004 assert(Header && "No header for loop");
2005
2006 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002007 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00002008
Chris Lattner33adcfb2009-08-22 21:43:10 +00002009 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002010 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002011 << " Child Loop BB" << FunctionNumber << "_"
2012 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
2013
Chris Lattner33adcfb2009-08-22 21:43:10 +00002014 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00002015 }
2016}
2017
David Greeneb71d1b22009-08-10 16:38:07 +00002018/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00002019void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00002020 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00002021 // Add loop depth information
2022 const MachineLoop *loop = LI->getLoopFor(&MBB);
2023
2024 if (loop) {
2025 // Print a newline after bb# annotation.
2026 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00002027 O.PadToColumn(MAI->getCommentColumn());
2028 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00002029 << '\n';
2030
Chris Lattner33adcfb2009-08-22 21:43:10 +00002031 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002032
2033 MachineBasicBlock *Header = loop->getHeader();
2034 assert(Header && "No header for loop");
2035
2036 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002037 O << MAI->getCommentString() << " Loop Header";
2038 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002039 }
2040 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002041 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002042 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2043 }
2044
2045 if (loop->empty()) {
2046 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002047 O.PadToColumn(MAI->getCommentColumn());
2048 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002049 }
2050
2051 // Add parent loop information
2052 for (const MachineLoop *CurLoop = loop->getParentLoop();
2053 CurLoop;
2054 CurLoop = CurLoop->getParentLoop()) {
2055 MachineBasicBlock *Header = CurLoop->getHeader();
2056 assert(Header && "No header for loop");
2057
2058 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002059 O.PadToColumn(MAI->getCommentColumn());
2060 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002061 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002062 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002063 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2064 }
2065 }
2066 }
2067}